1 /*
2 * Copyright (C) 2010 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 #include "BitmapRegionDecoder.h"
18
19 #include <HardwareBitmapUploader.h>
20 #include <androidfw/Asset.h>
21 #include <sys/stat.h>
22
23 #include <memory>
24
25 #include "BitmapFactory.h"
26 #include "CreateJavaOutputStreamAdaptor.h"
27 #include "Gainmap.h"
28 #include "GraphicsJNI.h"
29 #include "SkBitmap.h"
30 #include "SkCodec.h"
31 #include "SkColorSpace.h"
32 #include "SkData.h"
33 #include "SkGainmapInfo.h"
34 #include "SkStream.h"
35 #include "SkStreamPriv.h"
36 #include "Utils.h"
37
38 using namespace android;
39
40 namespace android {
41 class BitmapRegionDecoderWrapper {
42 public:
Make(sk_sp<SkData> data)43 static std::unique_ptr<BitmapRegionDecoderWrapper> Make(sk_sp<SkData> data) {
44 std::unique_ptr<skia::BitmapRegionDecoder> mainImageBRD =
45 skia::BitmapRegionDecoder::Make(std::move(data));
46 if (!mainImageBRD) {
47 return nullptr;
48 }
49
50 SkGainmapInfo gainmapInfo;
51 std::unique_ptr<SkStream> gainmapStream;
52 std::unique_ptr<skia::BitmapRegionDecoder> gainmapBRD = nullptr;
53 if (mainImageBRD->getAndroidGainmap(&gainmapInfo, &gainmapStream)) {
54 sk_sp<SkData> data = nullptr;
55 if (gainmapStream->getMemoryBase()) {
56 // It is safe to make without copy because we'll hold onto the stream.
57 data = SkData::MakeWithoutCopy(gainmapStream->getMemoryBase(),
58 gainmapStream->getLength());
59 } else {
60 data = SkCopyStreamToData(gainmapStream.get());
61 // We don't need to hold the stream anymore
62 gainmapStream = nullptr;
63 }
64 gainmapBRD = skia::BitmapRegionDecoder::Make(std::move(data));
65 }
66
67 return std::unique_ptr<BitmapRegionDecoderWrapper>(
68 new BitmapRegionDecoderWrapper(std::move(mainImageBRD), std::move(gainmapBRD),
69 gainmapInfo, std::move(gainmapStream)));
70 }
71
getEncodedFormat()72 SkEncodedImageFormat getEncodedFormat() { return mMainImageBRD->getEncodedFormat(); }
73
computeOutputColorType(SkColorType requestedColorType)74 SkColorType computeOutputColorType(SkColorType requestedColorType) {
75 return mMainImageBRD->computeOutputColorType(requestedColorType);
76 }
77
computeOutputColorSpace(SkColorType outputColorType,sk_sp<SkColorSpace> prefColorSpace=nullptr)78 sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType,
79 sk_sp<SkColorSpace> prefColorSpace = nullptr) {
80 return mMainImageBRD->computeOutputColorSpace(outputColorType, prefColorSpace);
81 }
82
decodeRegion(SkBitmap * bitmap,skia::BRDAllocator * allocator,const SkIRect & desiredSubset,int sampleSize,SkColorType colorType,bool requireUnpremul,sk_sp<SkColorSpace> prefColorSpace)83 bool decodeRegion(SkBitmap* bitmap, skia::BRDAllocator* allocator, const SkIRect& desiredSubset,
84 int sampleSize, SkColorType colorType, bool requireUnpremul,
85 sk_sp<SkColorSpace> prefColorSpace) {
86 return mMainImageBRD->decodeRegion(bitmap, allocator, desiredSubset, sampleSize, colorType,
87 requireUnpremul, prefColorSpace);
88 }
89
decodeGainmapRegion(sp<uirenderer::Gainmap> * outGainmap,int outWidth,int outHeight,const SkIRect & desiredSubset,int sampleSize,bool requireUnpremul)90 bool decodeGainmapRegion(sp<uirenderer::Gainmap>* outGainmap, int outWidth, int outHeight,
91 const SkIRect& desiredSubset, int sampleSize, bool requireUnpremul) {
92 SkColorType decodeColorType = mGainmapBRD->computeOutputColorType(kN32_SkColorType);
93 sk_sp<SkColorSpace> decodeColorSpace =
94 mGainmapBRD->computeOutputColorSpace(decodeColorType, nullptr);
95 SkBitmap bm;
96 // Because we must match the dimensions of the base bitmap, we always use a
97 // recycling allocator even though we are allocating a new bitmap. This is to ensure
98 // that if a recycled bitmap was used for the base image that we match the relative
99 // dimensions of that base image. The behavior of BRD here is:
100 // if inBitmap is specified -> output dimensions are always equal to the inBitmap's
101 // if no bitmap is reused -> output dimensions are the intersect of the desiredSubset &
102 // the image bounds
103 // The handling of the above conditionals are baked into the desiredSubset, so we
104 // simply need to ensure that the resulting bitmap is the exact same width/height as
105 // the specified desiredSubset regardless of the intersection to the image bounds.
106 // kPremul_SkAlphaType is used just as a placeholder as it doesn't change the underlying
107 // allocation type. RecyclingClippingPixelAllocator will populate this with the
108 // actual alpha type in either allocPixelRef() or copyIfNecessary()
109 sk_sp<Bitmap> nativeBitmap = Bitmap::allocateHeapBitmap(SkImageInfo::Make(
110 outWidth, outHeight, decodeColorType, kPremul_SkAlphaType, decodeColorSpace));
111 if (!nativeBitmap) {
112 ALOGE("OOM allocating Bitmap for Gainmap");
113 return false;
114 }
115 RecyclingClippingPixelAllocator allocator(nativeBitmap.get(), false);
116 if (!mGainmapBRD->decodeRegion(&bm, &allocator, desiredSubset, sampleSize, decodeColorType,
117 requireUnpremul, decodeColorSpace)) {
118 ALOGE("Error decoding Gainmap region");
119 return false;
120 }
121 allocator.copyIfNecessary();
122 auto gainmap = sp<uirenderer::Gainmap>::make();
123 if (!gainmap) {
124 ALOGE("OOM allocating Gainmap");
125 return false;
126 }
127 gainmap->info = mGainmapInfo;
128 gainmap->bitmap = std::move(nativeBitmap);
129 *outGainmap = std::move(gainmap);
130 return true;
131 }
132
calculateGainmapRegion(const SkIRect & mainImageRegion,int * inOutWidth,int * inOutHeight)133 SkIRect calculateGainmapRegion(const SkIRect& mainImageRegion, int* inOutWidth,
134 int* inOutHeight) {
135 const float scaleX = ((float)mGainmapBRD->width()) / mMainImageBRD->width();
136 const float scaleY = ((float)mGainmapBRD->height()) / mMainImageBRD->height();
137 *inOutWidth *= scaleX;
138 *inOutHeight *= scaleY;
139 // TODO: Account for rounding error?
140 return SkIRect::MakeLTRB(mainImageRegion.left() * scaleX, mainImageRegion.top() * scaleY,
141 mainImageRegion.right() * scaleX,
142 mainImageRegion.bottom() * scaleY);
143 }
144
hasGainmap()145 bool hasGainmap() { return mGainmapBRD != nullptr; }
146
width() const147 int width() const { return mMainImageBRD->width(); }
height() const148 int height() const { return mMainImageBRD->height(); }
149
150 private:
BitmapRegionDecoderWrapper(std::unique_ptr<skia::BitmapRegionDecoder> mainImageBRD,std::unique_ptr<skia::BitmapRegionDecoder> gainmapBRD,SkGainmapInfo info,std::unique_ptr<SkStream> stream)151 BitmapRegionDecoderWrapper(std::unique_ptr<skia::BitmapRegionDecoder> mainImageBRD,
152 std::unique_ptr<skia::BitmapRegionDecoder> gainmapBRD,
153 SkGainmapInfo info, std::unique_ptr<SkStream> stream)
154 : mMainImageBRD(std::move(mainImageBRD))
155 , mGainmapBRD(std::move(gainmapBRD))
156 , mGainmapInfo(info)
157 , mGainmapStream(std::move(stream)) {}
158
159 std::unique_ptr<skia::BitmapRegionDecoder> mMainImageBRD;
160 std::unique_ptr<skia::BitmapRegionDecoder> mGainmapBRD;
161 SkGainmapInfo mGainmapInfo;
162 std::unique_ptr<SkStream> mGainmapStream;
163 };
164 } // namespace android
165
createBitmapRegionDecoder(JNIEnv * env,sk_sp<SkData> data)166 static jobject createBitmapRegionDecoder(JNIEnv* env, sk_sp<SkData> data) {
167 auto brd = android::BitmapRegionDecoderWrapper::Make(std::move(data));
168 if (!brd) {
169 doThrowIOE(env, "Image format not supported");
170 return nullObjectReturn("CreateBitmapRegionDecoder returned null");
171 }
172
173 return GraphicsJNI::createBitmapRegionDecoder(env, brd.release());
174 }
175
nativeNewInstanceFromByteArray(JNIEnv * env,jobject,jbyteArray byteArray,jint offset,jint length)176 static jobject nativeNewInstanceFromByteArray(JNIEnv* env, jobject, jbyteArray byteArray,
177 jint offset, jint length) {
178 AutoJavaByteArray ar(env, byteArray);
179 return createBitmapRegionDecoder(env, SkData::MakeWithCopy(ar.ptr() + offset, length));
180 }
181
nativeNewInstanceFromFileDescriptor(JNIEnv * env,jobject clazz,jobject fileDescriptor)182 static jobject nativeNewInstanceFromFileDescriptor(JNIEnv* env, jobject clazz,
183 jobject fileDescriptor) {
184 NPE_CHECK_RETURN_ZERO(env, fileDescriptor);
185
186 jint descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
187
188 struct stat fdStat;
189 if (fstat(descriptor, &fdStat) == -1) {
190 doThrowIOE(env, "broken file descriptor");
191 return nullObjectReturn("fstat return -1");
192 }
193
194 return createBitmapRegionDecoder(env, SkData::MakeFromFD(descriptor));
195 }
196
nativeNewInstanceFromStream(JNIEnv * env,jobject clazz,jobject is,jbyteArray storage)197 static jobject nativeNewInstanceFromStream(JNIEnv* env, jobject clazz, jobject is, // InputStream
198 jbyteArray storage) { // byte[]
199 jobject brd = nullptr;
200 sk_sp<SkData> data = CopyJavaInputStream(env, is, storage);
201
202 if (data) {
203 brd = createBitmapRegionDecoder(env, std::move(data));
204 }
205 return brd;
206 }
207
nativeNewInstanceFromAsset(JNIEnv * env,jobject clazz,jlong native_asset)208 static jobject nativeNewInstanceFromAsset(JNIEnv* env, jobject clazz, jlong native_asset) {
209 Asset* asset = reinterpret_cast<Asset*>(native_asset);
210 sk_sp<SkData> data = CopyAssetToData(asset);
211 if (!data) {
212 return nullptr;
213 }
214
215 return createBitmapRegionDecoder(env, data);
216 }
217
218 /*
219 * nine patch not supported
220 * purgeable not supported
221 * reportSizeToVM not supported
222 */
nativeDecodeRegion(JNIEnv * env,jobject,jlong brdHandle,jint inputX,jint inputY,jint inputWidth,jint inputHeight,jobject options,jlong inBitmapHandle,jlong colorSpaceHandle)223 static jobject nativeDecodeRegion(JNIEnv* env, jobject, jlong brdHandle, jint inputX,
224 jint inputY, jint inputWidth, jint inputHeight, jobject options, jlong inBitmapHandle,
225 jlong colorSpaceHandle) {
226
227 // Set default options.
228 int sampleSize = 1;
229 SkColorType colorType = kN32_SkColorType;
230 bool requireUnpremul = false;
231 jobject javaBitmap = nullptr;
232 bool isHardware = false;
233 sk_sp<SkColorSpace> colorSpace = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
234 // Update the default options with any options supplied by the client.
235 if (NULL != options) {
236 sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID);
237 jobject jconfig = env->GetObjectField(options, gOptions_configFieldID);
238 colorType = GraphicsJNI::getNativeBitmapColorType(env, jconfig);
239 isHardware = GraphicsJNI::isHardwareConfig(env, jconfig);
240 requireUnpremul = !env->GetBooleanField(options, gOptions_premultipliedFieldID);
241 javaBitmap = env->GetObjectField(options, gOptions_bitmapFieldID);
242 // The Java options of ditherMode and preferQualityOverSpeed are deprecated. We will
243 // ignore the values of these fields.
244
245 // Initialize these fields to indicate a failure. If the decode succeeds, we
246 // will update them later on.
247 env->SetIntField(options, gOptions_widthFieldID, -1);
248 env->SetIntField(options, gOptions_heightFieldID, -1);
249 env->SetObjectField(options, gOptions_mimeFieldID, 0);
250 env->SetObjectField(options, gOptions_outConfigFieldID, 0);
251 env->SetObjectField(options, gOptions_outColorSpaceFieldID, 0);
252 }
253
254 // Recycle a bitmap if possible.
255 android::Bitmap* recycledBitmap = nullptr;
256 if (javaBitmap) {
257 recycledBitmap = &bitmap::toBitmap(inBitmapHandle);
258 if (recycledBitmap->isImmutable()) {
259 ALOGW("Warning: Reusing an immutable bitmap as an image decoder target.");
260 }
261 }
262
263 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
264 SkColorType decodeColorType = brd->computeOutputColorType(colorType);
265
266 if (isHardware) {
267 if (decodeColorType == kRGBA_F16_SkColorType &&
268 !uirenderer::HardwareBitmapUploader::hasFP16Support()) {
269 decodeColorType = kN32_SkColorType;
270 }
271 if (decodeColorType == kRGBA_1010102_SkColorType &&
272 !uirenderer::HardwareBitmapUploader::has1010102Support()) {
273 decodeColorType = kN32_SkColorType;
274 }
275 }
276
277 // Set up the pixel allocator
278 skia::BRDAllocator* allocator = nullptr;
279 RecyclingClippingPixelAllocator recycleAlloc(recycledBitmap);
280 HeapAllocator heapAlloc;
281 if (javaBitmap) {
282 allocator = &recycleAlloc;
283 // We are required to match the color type of the recycled bitmap.
284 decodeColorType = recycledBitmap->info().colorType();
285 } else {
286 allocator = &heapAlloc;
287 }
288
289 sk_sp<SkColorSpace> decodeColorSpace = brd->computeOutputColorSpace(
290 decodeColorType, colorSpace);
291
292 // Decode the region.
293 const SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight);
294 SkBitmap bitmap;
295 if (!brd->decodeRegion(&bitmap, allocator, subset, sampleSize,
296 decodeColorType, requireUnpremul, decodeColorSpace)) {
297 return nullObjectReturn("Failed to decode region.");
298 }
299
300 // If the client provided options, indicate that the decode was successful.
301 if (NULL != options) {
302 env->SetIntField(options, gOptions_widthFieldID, bitmap.width());
303 env->SetIntField(options, gOptions_heightFieldID, bitmap.height());
304
305 env->SetObjectField(options, gOptions_mimeFieldID,
306 getMimeTypeAsJavaString(env, brd->getEncodedFormat()));
307 if (env->ExceptionCheck()) {
308 return nullObjectReturn("OOM in encodedFormatToString()");
309 }
310
311 jint configID = GraphicsJNI::colorTypeToLegacyBitmapConfig(decodeColorType);
312 if (isHardware) {
313 configID = GraphicsJNI::kHardware_LegacyBitmapConfig;
314 }
315 jobject config = env->CallStaticObjectMethod(gBitmapConfig_class,
316 gBitmapConfig_nativeToConfigMethodID, configID);
317 env->SetObjectField(options, gOptions_outConfigFieldID, config);
318
319 env->SetObjectField(options, gOptions_outColorSpaceFieldID,
320 GraphicsJNI::getColorSpace(env, decodeColorSpace.get(), decodeColorType));
321 }
322
323 if (javaBitmap) {
324 recycleAlloc.copyIfNecessary();
325 }
326
327 sp<uirenderer::Gainmap> gainmap;
328 bool hasGainmap = brd->hasGainmap();
329 if (hasGainmap) {
330 int gainmapWidth = bitmap.width();
331 int gainmapHeight = bitmap.height();
332 if (javaBitmap) {
333 // If we are recycling we must match the inBitmap's relative dimensions
334 gainmapWidth = recycledBitmap->width();
335 gainmapHeight = recycledBitmap->height();
336 }
337 SkIRect gainmapSubset = brd->calculateGainmapRegion(subset, &gainmapWidth, &gainmapHeight);
338 if (!brd->decodeGainmapRegion(&gainmap, gainmapWidth, gainmapHeight, gainmapSubset,
339 sampleSize, requireUnpremul)) {
340 // If there is an error decoding Gainmap - we don't fail. We just don't provide Gainmap
341 hasGainmap = false;
342 }
343 }
344
345 // If we may have reused a bitmap, we need to indicate that the pixels have changed.
346 if (javaBitmap) {
347 if (hasGainmap) {
348 recycledBitmap->setGainmap(std::move(gainmap));
349 }
350 bitmap::reinitBitmap(env, javaBitmap, recycledBitmap->info(), !requireUnpremul);
351 return javaBitmap;
352 }
353
354 int bitmapCreateFlags = 0;
355 if (!requireUnpremul) {
356 bitmapCreateFlags |= android::bitmap::kBitmapCreateFlag_Premultiplied;
357 }
358
359 if (isHardware) {
360 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(bitmap);
361 if (hasGainmap) {
362 auto gm = uirenderer::Gainmap::allocateHardwareGainmap(gainmap);
363 if (gm) {
364 hardwareBitmap->setGainmap(std::move(gm));
365 }
366 }
367 return bitmap::createBitmap(env, hardwareBitmap.release(), bitmapCreateFlags);
368 }
369 Bitmap* heapBitmap = heapAlloc.getStorageObjAndReset();
370 if (hasGainmap && heapBitmap != nullptr) {
371 heapBitmap->setGainmap(std::move(gainmap));
372 }
373 return android::bitmap::createBitmap(env, heapBitmap, bitmapCreateFlags);
374 }
375
nativeGetHeight(JNIEnv * env,jobject,jlong brdHandle)376 static jint nativeGetHeight(JNIEnv* env, jobject, jlong brdHandle) {
377 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
378 return static_cast<jint>(brd->height());
379 }
380
nativeGetWidth(JNIEnv * env,jobject,jlong brdHandle)381 static jint nativeGetWidth(JNIEnv* env, jobject, jlong brdHandle) {
382 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
383 return static_cast<jint>(brd->width());
384 }
385
nativeClean(JNIEnv * env,jobject,jlong brdHandle)386 static void nativeClean(JNIEnv* env, jobject, jlong brdHandle) {
387 auto* brd = reinterpret_cast<BitmapRegionDecoderWrapper*>(brdHandle);
388 delete brd;
389 }
390
391 ///////////////////////////////////////////////////////////////////////////////
392
393 static const JNINativeMethod gBitmapRegionDecoderMethods[] = {
394 { "nativeDecodeRegion",
395 "(JIIIILandroid/graphics/BitmapFactory$Options;JJ)Landroid/graphics/Bitmap;",
396 (void*)nativeDecodeRegion},
397
398 { "nativeGetHeight", "(J)I", (void*)nativeGetHeight},
399
400 { "nativeGetWidth", "(J)I", (void*)nativeGetWidth},
401
402 { "nativeClean", "(J)V", (void*)nativeClean},
403
404 { "nativeNewInstance",
405 "([BII)Landroid/graphics/BitmapRegionDecoder;",
406 (void*)nativeNewInstanceFromByteArray
407 },
408
409 { "nativeNewInstance",
410 "(Ljava/io/InputStream;[B)Landroid/graphics/BitmapRegionDecoder;",
411 (void*)nativeNewInstanceFromStream
412 },
413
414 { "nativeNewInstance",
415 "(Ljava/io/FileDescriptor;)Landroid/graphics/BitmapRegionDecoder;",
416 (void*)nativeNewInstanceFromFileDescriptor
417 },
418
419 { "nativeNewInstance",
420 "(J)Landroid/graphics/BitmapRegionDecoder;",
421 (void*)nativeNewInstanceFromAsset
422 },
423 };
424
register_android_graphics_BitmapRegionDecoder(JNIEnv * env)425 int register_android_graphics_BitmapRegionDecoder(JNIEnv* env)
426 {
427 return android::RegisterMethodsOrDie(env, "android/graphics/BitmapRegionDecoder",
428 gBitmapRegionDecoderMethods, NELEM(gBitmapRegionDecoderMethods));
429 }
430