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 17package android.graphics; 18 19import com.android.layoutlib.bridge.Bridge; 20import com.android.layoutlib.bridge.impl.DelegateManager; 21import com.android.ninepatch.NinePatchChunk; 22import com.android.resources.Density; 23import com.android.tools.layoutlib.annotations.LayoutlibDelegate; 24 25import android.annotation.Nullable; 26import com.android.layoutlib.bridge.util.NinePatchInputStream; 27import android.graphics.BitmapFactory.Options; 28import android.graphics.Bitmap_Delegate.BitmapCreateFlags; 29 30import java.io.FileDescriptor; 31import java.io.IOException; 32import java.io.InputStream; 33import java.util.EnumSet; 34import 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 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 102 /*package*/ static Bitmap nativeDecodeFileDescriptor(FileDescriptor fd, 103 Rect padding, Options opts) { 104 if (opts != null) { 105 opts.inBitmap = null; 106 } 107 return null; 108 } 109 110 @LayoutlibDelegate 111 /*package*/ static Bitmap nativeDecodeAsset(long asset, Rect padding, Options opts) { 112 if (opts != null) { 113 opts.inBitmap = null; 114 } 115 return null; 116 } 117 118 @LayoutlibDelegate 119 /*package*/ static Bitmap nativeDecodeByteArray(byte[] data, int offset, 120 int length, Options opts) { 121 if (opts != null) { 122 opts.inBitmap = null; 123 } 124 return null; 125 } 126 127 @LayoutlibDelegate 128 /*package*/ static boolean nativeIsSeekable(FileDescriptor fd) { 129 return true; 130 } 131 132 /** 133 * Set the newly decoded bitmap's density based on the Options. 134 * 135 * Copied from {@link BitmapFactory#setDensityFromOptions(Bitmap, Options)}. 136 */ 137 @LayoutlibDelegate 138 /*package*/ static void setDensityFromOptions(Bitmap outputBitmap, Options opts) { 139 if (outputBitmap == null || opts == null) return; 140 141 final int density = opts.inDensity; 142 if (density != 0) { 143 outputBitmap.setDensity(density); 144 final int targetDensity = opts.inTargetDensity; 145 if (targetDensity == 0 || density == targetDensity || density == opts.inScreenDensity) { 146 return; 147 } 148 149 // --- Change from original implementation begins --- 150 // LayoutLib doesn't scale the nine patch when decoding it. Hence, don't change the 151 // density of the source bitmap in case of ninepatch. 152 153 if (opts.inScaled) { 154 // --- Change from original implementation ends. --- 155 outputBitmap.setDensity(targetDensity); 156 } 157 } else if (opts.inBitmap != null) { 158 // bitmap was reused, ensure density is reset 159 outputBitmap.setDensity(Bitmap.getDefaultDensity()); 160 } 161 } 162} 163