blob: b3444514007029deb203bfe1e8847a56f279433b [file] [log] [blame]
Xavier Ducrohet02316e52011-02-04 16:40:49 -08001/*
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;
Xavier Ducrohet02316e52011-02-04 16:40:49 -080020import com.android.layoutlib.bridge.impl.DelegateManager;
21import com.android.ninepatch.NinePatchChunk;
22import com.android.resources.Density;
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -080023import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
Xavier Ducrohet02316e52011-02-04 16:40:49 -080024
Deepanshu Gupta442aee62015-05-22 14:11:22 -070025import android.annotation.Nullable;
Deepanshu Gupta896eb1c2016-03-04 19:31:28 +053026import com.android.layoutlib.bridge.util.NinePatchInputStream;
Xavier Ducrohet02316e52011-02-04 16:40:49 -080027import android.graphics.BitmapFactory.Options;
Deepanshu Guptaed1dc232013-09-03 17:51:48 -070028import android.graphics.Bitmap_Delegate.BitmapCreateFlags;
Xavier Ducrohet02316e52011-02-04 16:40:49 -080029
30import java.io.FileDescriptor;
31import java.io.IOException;
32import java.io.InputStream;
Deepanshu Guptaed1dc232013-09-03 17:51:48 -070033import java.util.EnumSet;
34import java.util.Set;
Xavier Ducrohet02316e52011-02-04 16:40:49 -080035
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 Ducrohet94c80bf2011-02-09 17:17:49 -080050 @LayoutlibDelegate
Xavier Ducrohet506cc402012-03-23 16:07:23 -070051 /*package*/ static Bitmap nativeDecodeStream(InputStream is, byte[] storage,
Jerome Gaillard407e1812019-04-01 14:10:13 +010052 @Nullable Rect padding, @Nullable Options opts, long inBitmapHandle,
53 long colorSpaceHandle) {
Xavier Ducrohet02316e52011-02-04 16:40:49 -080054 Bitmap bm = null;
55
56 Density density = Density.MEDIUM;
Deepanshu Guptaed1dc232013-09-03 17:51:48 -070057 Set<BitmapCreateFlags> bitmapCreateFlags = EnumSet.of(BitmapCreateFlags.MUTABLE);
Xavier Ducrohet02316e52011-02-04 16:40:49 -080058 if (opts != null) {
59 density = Density.getEnum(opts.inDensity);
Deepanshu Guptaed1dc232013-09-03 17:51:48 -070060 if (opts.inPremultiplied) {
61 bitmapCreateFlags.add(BitmapCreateFlags.PREMULTIPLIED);
62 }
Deepanshu Gupta13bd2422015-10-05 14:37:54 -070063 opts.inScaled = false;
Xavier Ducrohet02316e52011-02-04 16:40:49 -080064 }
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.
Deepanshu Guptaed1dc232013-09-03 17:51:48 -070076 bm = Bitmap_Delegate.createBitmap(ninePatch.getImage(), bitmapCreateFlags,
Xavier Ducrohet02316e52011-02-04 16:40:49 -080077 density);
78 NinePatchChunk chunk = ninePatch.getChunk();
79
80 // put the chunk in the bitmap
81 bm.setNinePatchChunk(NinePatch_Delegate.serialize(chunk));
82
Deepanshu Gupta5da1b052014-06-23 18:37:53 -070083 if (padding != null) {
84 // read the padding
85 int[] paddingArray = chunk.getPadding();
86 padding.left = paddingArray[0];
87 padding.top = paddingArray[1];
88 padding.right = paddingArray[2];
89 padding.bottom = paddingArray[3];
90 }
Xavier Ducrohet02316e52011-02-04 16:40:49 -080091 } else {
92 // load the bitmap directly.
Deepanshu Guptaed1dc232013-09-03 17:51:48 -070093 bm = Bitmap_Delegate.createBitmap(is, bitmapCreateFlags, density);
Xavier Ducrohet02316e52011-02-04 16:40:49 -080094 }
95 } catch (IOException e) {
Jerome Gaillardb484b7e2020-04-14 16:10:43 +010096 Bridge.getLog().error(null, "Failed to load image", e, null, null);
Xavier Ducrohet02316e52011-02-04 16:40:49 -080097 }
98
99 return bm;
100 }
101
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800102 @LayoutlibDelegate
Xavier Ducrohet02316e52011-02-04 16:40:49 -0800103 /*package*/ static Bitmap nativeDecodeFileDescriptor(FileDescriptor fd,
Jerome Gaillard407e1812019-04-01 14:10:13 +0100104 Rect padding, Options opts, long inBitmapHandle, long colorSpaceHandle) {
Diego Perezab370022017-12-14 15:44:26 +0000105 if (opts != null) {
106 opts.inBitmap = null;
107 }
Xavier Ducrohet02316e52011-02-04 16:40:49 -0800108 return null;
109 }
110
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800111 @LayoutlibDelegate
Jerome Gaillardad7af142019-01-23 17:34:54 +0000112 /*package*/ static Bitmap nativeDecodeAsset(long asset, Rect padding, Options opts,
Jerome Gaillard407e1812019-04-01 14:10:13 +0100113 long inBitmapHandle, long colorSpaceHandle) {
Diego Perezab370022017-12-14 15:44:26 +0000114 if (opts != null) {
115 opts.inBitmap = null;
116 }
Xavier Ducrohet02316e52011-02-04 16:40:49 -0800117 return null;
118 }
119
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800120 @LayoutlibDelegate
Xavier Ducrohet02316e52011-02-04 16:40:49 -0800121 /*package*/ static Bitmap nativeDecodeByteArray(byte[] data, int offset,
Jerome Gaillard407e1812019-04-01 14:10:13 +0100122 int length, Options opts, long inBitmapHandle, long colorSpaceHandle) {
Diego Perezab370022017-12-14 15:44:26 +0000123 if (opts != null) {
124 opts.inBitmap = null;
125 }
Xavier Ducrohet02316e52011-02-04 16:40:49 -0800126 return null;
127 }
128
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800129 @LayoutlibDelegate
Xavier Ducrohet02316e52011-02-04 16:40:49 -0800130 /*package*/ static boolean nativeIsSeekable(FileDescriptor fd) {
131 return true;
132 }
Deepanshu Gupta5cd1a102015-12-07 14:02:13 -0800133
134 /**
135 * Set the newly decoded bitmap's density based on the Options.
136 *
137 * Copied from {@link BitmapFactory#setDensityFromOptions(Bitmap, Options)}.
138 */
139 @LayoutlibDelegate
140 /*package*/ static void setDensityFromOptions(Bitmap outputBitmap, Options opts) {
141 if (outputBitmap == null || opts == null) return;
142
143 final int density = opts.inDensity;
144 if (density != 0) {
145 outputBitmap.setDensity(density);
146 final int targetDensity = opts.inTargetDensity;
147 if (targetDensity == 0 || density == targetDensity || density == opts.inScreenDensity) {
148 return;
149 }
150
151 // --- Change from original implementation begins ---
152 // LayoutLib doesn't scale the nine patch when decoding it. Hence, don't change the
153 // density of the source bitmap in case of ninepatch.
154
155 if (opts.inScaled) {
156 // --- Change from original implementation ends. ---
157 outputBitmap.setDensity(targetDensity);
158 }
159 } else if (opts.inBitmap != null) {
160 // bitmap was reused, ensure density is reset
161 outputBitmap.setDensity(Bitmap.getDefaultDensity());
162 }
163 }
Xavier Ducrohet02316e52011-02-04 16:40:49 -0800164}