• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.graphics;
18 
19 import com.android.layoutlib.bridge.Bridge;
20 import com.android.layoutlib.bridge.impl.DelegateManager;
21 import com.android.ninepatch.NinePatchChunk;
22 import com.android.resources.Density;
23 import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
24 
25 import android.annotation.Nullable;
26 import com.android.layoutlib.bridge.util.NinePatchInputStream;
27 import android.graphics.BitmapFactory.Options;
28 import android.graphics.Bitmap_Delegate.BitmapCreateFlags;
29 
30 import java.io.FileDescriptor;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.EnumSet;
34 import java.util.Set;
35 
36 /**
37  * Delegate implementing the native methods of android.graphics.BitmapFactory
38  *
39  * Through the layoutlib_create tool, the original native methods of BitmapFactory have been
40  * replaced by calls to methods of the same name in this delegate class.
41  *
42  * Because it's a stateless class to start with, there's no need to keep a {@link DelegateManager}
43  * around to map int to instance of the delegate.
44  *
45  */
46 /*package*/ class BitmapFactory_Delegate {
47 
48     // ------ Native Delegates ------
49 
50     @LayoutlibDelegate
nativeDecodeStream(InputStream is, byte[] storage, @Nullable Rect padding, @Nullable Options opts)51     /*package*/ static Bitmap nativeDecodeStream(InputStream is, byte[] storage,
52             @Nullable Rect padding, @Nullable Options opts) {
53         Bitmap bm = null;
54 
55         Density density = Density.MEDIUM;
56         Set<BitmapCreateFlags> bitmapCreateFlags = EnumSet.of(BitmapCreateFlags.MUTABLE);
57         if (opts != null) {
58             density = Density.getEnum(opts.inDensity);
59             if (opts.inPremultiplied) {
60                 bitmapCreateFlags.add(BitmapCreateFlags.PREMULTIPLIED);
61             }
62             opts.inScaled = false;
63         }
64 
65         try {
66             if (is instanceof NinePatchInputStream) {
67                 NinePatchInputStream npis = (NinePatchInputStream) is;
68                 npis.disableFakeMarkSupport();
69 
70                 // load the bitmap as a nine patch
71                 com.android.ninepatch.NinePatch ninePatch = com.android.ninepatch.NinePatch.load(
72                         npis, true /*is9Patch*/, false /*convert*/);
73 
74                 // get the bitmap and chunk objects.
75                 bm = Bitmap_Delegate.createBitmap(ninePatch.getImage(), bitmapCreateFlags,
76                         density);
77                 NinePatchChunk chunk = ninePatch.getChunk();
78 
79                 // put the chunk in the bitmap
80                 bm.setNinePatchChunk(NinePatch_Delegate.serialize(chunk));
81 
82                 if (padding != null) {
83                     // read the padding
84                     int[] paddingArray = chunk.getPadding();
85                     padding.left = paddingArray[0];
86                     padding.top = paddingArray[1];
87                     padding.right = paddingArray[2];
88                     padding.bottom = paddingArray[3];
89                 }
90             } else {
91                 // load the bitmap directly.
92                 bm = Bitmap_Delegate.createBitmap(is, bitmapCreateFlags, density);
93             }
94         } catch (IOException e) {
95             Bridge.getLog().error(null, "Failed to load image", e, null);
96         }
97 
98         return bm;
99     }
100 
101     @LayoutlibDelegate
nativeDecodeFileDescriptor(FileDescriptor fd, Rect padding, Options opts)102     /*package*/ static Bitmap nativeDecodeFileDescriptor(FileDescriptor fd,
103             Rect padding, Options opts) {
104         opts.inBitmap = null;
105         return null;
106     }
107 
108     @LayoutlibDelegate
nativeDecodeAsset(long asset, Rect padding, Options opts)109     /*package*/ static Bitmap nativeDecodeAsset(long asset, Rect padding, Options opts) {
110         opts.inBitmap = null;
111         return null;
112     }
113 
114     @LayoutlibDelegate
nativeDecodeByteArray(byte[] data, int offset, int length, Options opts)115     /*package*/ static Bitmap nativeDecodeByteArray(byte[] data, int offset,
116             int length, Options opts) {
117         opts.inBitmap = null;
118         return null;
119     }
120 
121     @LayoutlibDelegate
nativeIsSeekable(FileDescriptor fd)122     /*package*/ static boolean nativeIsSeekable(FileDescriptor fd) {
123         return true;
124     }
125 
126     /**
127      * Set the newly decoded bitmap's density based on the Options.
128      *
129      * Copied from {@link BitmapFactory#setDensityFromOptions(Bitmap, Options)}.
130      */
131     @LayoutlibDelegate
setDensityFromOptions(Bitmap outputBitmap, Options opts)132     /*package*/ static void setDensityFromOptions(Bitmap outputBitmap, Options opts) {
133         if (outputBitmap == null || opts == null) return;
134 
135         final int density = opts.inDensity;
136         if (density != 0) {
137             outputBitmap.setDensity(density);
138             final int targetDensity = opts.inTargetDensity;
139             if (targetDensity == 0 || density == targetDensity || density == opts.inScreenDensity) {
140                 return;
141             }
142 
143             // --- Change from original implementation begins ---
144             // LayoutLib doesn't scale the nine patch when decoding it. Hence, don't change the
145             // density of the source bitmap in case of ninepatch.
146 
147             if (opts.inScaled) {
148             // --- Change from original implementation ends. ---
149                 outputBitmap.setDensity(targetDensity);
150             }
151         } else if (opts.inBitmap != null) {
152             // bitmap was reused, ensure density is reset
153             outputBitmap.setDensity(Bitmap.getDefaultDensity());
154         }
155     }
156 }
157