Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 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; |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 20 | import com.android.layoutlib.bridge.impl.DelegateManager; |
| 21 | import com.android.ninepatch.NinePatchChunk; |
| 22 | import com.android.resources.Density; |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 23 | import com.android.tools.layoutlib.annotations.LayoutlibDelegate; |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 24 | |
Deepanshu Gupta | 442aee6 | 2015-05-22 14:11:22 -0700 | [diff] [blame] | 25 | import android.annotation.Nullable; |
Deepanshu Gupta | 896eb1c | 2016-03-04 19:31:28 +0530 | [diff] [blame] | 26 | import com.android.layoutlib.bridge.util.NinePatchInputStream; |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 27 | import android.graphics.BitmapFactory.Options; |
Deepanshu Gupta | ed1dc23 | 2013-09-03 17:51:48 -0700 | [diff] [blame] | 28 | import android.graphics.Bitmap_Delegate.BitmapCreateFlags; |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 29 | |
| 30 | import java.io.FileDescriptor; |
| 31 | import java.io.IOException; |
| 32 | import java.io.InputStream; |
Deepanshu Gupta | ed1dc23 | 2013-09-03 17:51:48 -0700 | [diff] [blame] | 33 | import java.util.EnumSet; |
| 34 | import java.util.Set; |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 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 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 50 | @LayoutlibDelegate |
Xavier Ducrohet | 506cc40 | 2012-03-23 16:07:23 -0700 | [diff] [blame] | 51 | /*package*/ static Bitmap nativeDecodeStream(InputStream is, byte[] storage, |
Jerome Gaillard | 407e181 | 2019-04-01 14:10:13 +0100 | [diff] [blame] | 52 | @Nullable Rect padding, @Nullable Options opts, long inBitmapHandle, |
| 53 | long colorSpaceHandle) { |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 54 | Bitmap bm = null; |
| 55 | |
| 56 | Density density = Density.MEDIUM; |
Deepanshu Gupta | ed1dc23 | 2013-09-03 17:51:48 -0700 | [diff] [blame] | 57 | Set<BitmapCreateFlags> bitmapCreateFlags = EnumSet.of(BitmapCreateFlags.MUTABLE); |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 58 | if (opts != null) { |
| 59 | density = Density.getEnum(opts.inDensity); |
Deepanshu Gupta | ed1dc23 | 2013-09-03 17:51:48 -0700 | [diff] [blame] | 60 | if (opts.inPremultiplied) { |
| 61 | bitmapCreateFlags.add(BitmapCreateFlags.PREMULTIPLIED); |
| 62 | } |
Deepanshu Gupta | 13bd242 | 2015-10-05 14:37:54 -0700 | [diff] [blame] | 63 | opts.inScaled = false; |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | try { |
| 67 | if (is instanceof NinePatchInputStream) { |
| 68 | NinePatchInputStream npis = (NinePatchInputStream) is; |
| 69 | npis.disableFakeMarkSupport(); |
| 70 | |
| 71 | // load the bitmap as a nine patch |
| 72 | com.android.ninepatch.NinePatch ninePatch = com.android.ninepatch.NinePatch.load( |
| 73 | npis, true /*is9Patch*/, false /*convert*/); |
| 74 | |
| 75 | // get the bitmap and chunk objects. |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 76 | NinePatchChunk chunk = ninePatch.getChunk(); |
Leon Scroggins III | a8b2ce9 | 2020-03-06 13:39:26 -0500 | [diff] [blame] | 77 | bm = Bitmap_Delegate.createBitmap(ninePatch.getImage(), |
| 78 | NinePatch_Delegate.serialize(chunk), bitmapCreateFlags, density); |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 79 | |
Deepanshu Gupta | 5da1b05 | 2014-06-23 18:37:53 -0700 | [diff] [blame] | 80 | if (padding != null) { |
| 81 | // read the padding |
| 82 | int[] paddingArray = chunk.getPadding(); |
| 83 | padding.left = paddingArray[0]; |
| 84 | padding.top = paddingArray[1]; |
| 85 | padding.right = paddingArray[2]; |
| 86 | padding.bottom = paddingArray[3]; |
| 87 | } |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 88 | } else { |
| 89 | // load the bitmap directly. |
Deepanshu Gupta | ed1dc23 | 2013-09-03 17:51:48 -0700 | [diff] [blame] | 90 | bm = Bitmap_Delegate.createBitmap(is, bitmapCreateFlags, density); |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 91 | } |
| 92 | } catch (IOException e) { |
Jerome Gaillard | b484b7e | 2020-04-14 16:10:43 +0100 | [diff] [blame] | 93 | Bridge.getLog().error(null, "Failed to load image", e, null, null); |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | return bm; |
| 97 | } |
| 98 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 99 | @LayoutlibDelegate |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 100 | /*package*/ static Bitmap nativeDecodeFileDescriptor(FileDescriptor fd, |
Jerome Gaillard | 407e181 | 2019-04-01 14:10:13 +0100 | [diff] [blame] | 101 | Rect padding, Options opts, long inBitmapHandle, long colorSpaceHandle) { |
Diego Perez | ab37002 | 2017-12-14 15:44:26 +0000 | [diff] [blame] | 102 | if (opts != null) { |
| 103 | opts.inBitmap = null; |
| 104 | } |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 105 | return null; |
| 106 | } |
| 107 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 108 | @LayoutlibDelegate |
Jerome Gaillard | ad7af14 | 2019-01-23 17:34:54 +0000 | [diff] [blame] | 109 | /*package*/ static Bitmap nativeDecodeAsset(long asset, Rect padding, Options opts, |
Jerome Gaillard | 407e181 | 2019-04-01 14:10:13 +0100 | [diff] [blame] | 110 | long inBitmapHandle, long colorSpaceHandle) { |
Diego Perez | ab37002 | 2017-12-14 15:44:26 +0000 | [diff] [blame] | 111 | if (opts != null) { |
| 112 | opts.inBitmap = null; |
| 113 | } |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 114 | return null; |
| 115 | } |
| 116 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 117 | @LayoutlibDelegate |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 118 | /*package*/ static Bitmap nativeDecodeByteArray(byte[] data, int offset, |
Jerome Gaillard | 407e181 | 2019-04-01 14:10:13 +0100 | [diff] [blame] | 119 | int length, Options opts, long inBitmapHandle, long colorSpaceHandle) { |
Diego Perez | ab37002 | 2017-12-14 15:44:26 +0000 | [diff] [blame] | 120 | if (opts != null) { |
| 121 | opts.inBitmap = null; |
| 122 | } |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 123 | return null; |
| 124 | } |
| 125 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 126 | @LayoutlibDelegate |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 127 | /*package*/ static boolean nativeIsSeekable(FileDescriptor fd) { |
| 128 | return true; |
| 129 | } |
Deepanshu Gupta | 5cd1a10 | 2015-12-07 14:02:13 -0800 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * Set the newly decoded bitmap's density based on the Options. |
| 133 | * |
| 134 | * Copied from {@link BitmapFactory#setDensityFromOptions(Bitmap, Options)}. |
| 135 | */ |
| 136 | @LayoutlibDelegate |
| 137 | /*package*/ static void setDensityFromOptions(Bitmap outputBitmap, Options opts) { |
| 138 | if (outputBitmap == null || opts == null) return; |
| 139 | |
| 140 | final int density = opts.inDensity; |
| 141 | if (density != 0) { |
| 142 | outputBitmap.setDensity(density); |
| 143 | final int targetDensity = opts.inTargetDensity; |
| 144 | if (targetDensity == 0 || density == targetDensity || density == opts.inScreenDensity) { |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | // --- Change from original implementation begins --- |
| 149 | // LayoutLib doesn't scale the nine patch when decoding it. Hence, don't change the |
| 150 | // density of the source bitmap in case of ninepatch. |
| 151 | |
| 152 | if (opts.inScaled) { |
| 153 | // --- Change from original implementation ends. --- |
| 154 | outputBitmap.setDensity(targetDensity); |
| 155 | } |
| 156 | } else if (opts.inBitmap != null) { |
| 157 | // bitmap was reused, ensure density is reset |
| 158 | outputBitmap.setDensity(Bitmap.getDefaultDensity()); |
| 159 | } |
| 160 | } |
Xavier Ducrohet | 02316e5 | 2011-02-04 16:40:49 -0800 | [diff] [blame] | 161 | } |