blob: 94cea65897cf46cf64c069f668fd3e47cf0411a2 [file] [log] [blame]
Derek Sollenberger5368eda2019-10-25 11:20:03 -04001#undef LOG_TAG
John Reckf29ed282015-04-07 07:32:03 -07002#define LOG_TAG "Bitmap"
John Reckf29ed282015-04-07 07:32:03 -07003#include "Bitmap.h"
4
Kevin Lubick1175dc02022-02-28 12:41:27 -05005#include "GraphicsJNI.h"
Chris Craik32054b02014-05-09 13:58:56 -07006#include "SkBitmap.h"
Kevin Lubick1175dc02022-02-28 12:41:27 -05007#include "SkBlendMode.h"
John Reckbe671952021-01-13 22:39:32 -05008#include "SkCanvas.h"
9#include "SkColor.h"
10#include "SkColorSpace.h"
Kevin Lubick1175dc02022-02-28 12:41:27 -050011#include "SkData.h"
Chris Craik32054b02014-05-09 13:58:56 -070012#include "SkImageEncoder.h"
Leon Scroggins III57ee6202014-06-04 18:51:07 -040013#include "SkImageInfo.h"
Kevin Lubick1175dc02022-02-28 12:41:27 -050014#include "SkPaint.h"
15#include "SkPixelRef.h"
16#include "SkPixmap.h"
17#include "SkPoint.h"
18#include "SkRefCnt.h"
Chris Craik32054b02014-05-09 13:58:56 -070019#include "SkStream.h"
Kevin Lubick1175dc02022-02-28 12:41:27 -050020#include "SkTypes.h"
Leon Scroggins III94d294b2019-09-06 13:22:46 -040021#include "SkWebpEncoder.h"
Chris Craik32054b02014-05-09 13:58:56 -070022
Kevin Lubick1175dc02022-02-28 12:41:27 -050023
Chris Craik32054b02014-05-09 13:58:56 -070024#include "android_nio_utils.h"
25#include "CreateJavaOutputStreamAdaptor.h"
sergeyvdccca442016-03-21 15:38:21 -070026#include <hwui/Paint.h>
sergeyvc1c54062016-10-19 18:47:26 -070027#include <hwui/Bitmap.h>
Derek Sollenberger6e35e632019-01-22 13:56:25 -050028#include <utils/Color.h>
Chris Craik32054b02014-05-09 13:58:56 -070029
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010030#ifdef __ANDROID__ // Layoutlib does not support graphic buffer, parcel or render thread
John Recka18d7c82020-03-02 09:41:33 -080031#include <android-base/unique_fd.h>
32#include <android/binder_parcel.h>
33#include <android/binder_parcel_jni.h>
34#include <android/binder_parcel_platform.h>
35#include <android/binder_parcel_utils.h>
Derek Sollenberger42c50042020-02-18 14:51:17 -050036#include <private/android/AHardwareBufferHelpers.h>
John Recka18d7c82020-03-02 09:41:33 -080037#include <cutils/ashmem.h>
Leon Scroggins III898ce752020-02-18 12:22:17 -050038#include <dlfcn.h>
Leon Scroggins III5a190b12020-02-18 13:52:18 -050039#include <renderthread/RenderProxy.h>
John Recka18d7c82020-03-02 09:41:33 -080040#include <sys/mman.h>
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010041#endif
rennb2e9f522018-09-26 10:49:00 -070042
John Recka18d7c82020-03-02 09:41:33 -080043#include <inttypes.h>
Romain Guyce217fa2017-03-08 15:58:06 -080044#include <string.h>
Riley Andrews39d7f302014-11-13 17:43:25 -080045#include <memory>
46#include <string>
Chris Craik32054b02014-05-09 13:58:56 -070047
Jeff Browna316c5d2015-06-05 15:14:06 -070048#define DEBUG_PARCEL 0
49
sergeyvc69853c2016-10-07 14:14:09 -070050static jclass gBitmap_class;
51static jfieldID gBitmap_nativePtr;
52static jmethodID gBitmap_constructorMethodID;
53static jmethodID gBitmap_reinitMethodID;
sergeyvc69853c2016-10-07 14:14:09 -070054
John Reckf29ed282015-04-07 07:32:03 -070055namespace android {
56
sergeyvc1c54062016-10-19 18:47:26 -070057class BitmapWrapper {
John Reckf29ed282015-04-07 07:32:03 -070058public:
Chih-Hung Hsieh0727be12018-12-20 13:43:46 -080059 explicit BitmapWrapper(Bitmap* bitmap)
sergeyvc1c54062016-10-19 18:47:26 -070060 : mBitmap(bitmap) { }
sergeyvc69853c2016-10-07 14:14:09 -070061
62 void freePixels() {
sergeyvc1c54062016-10-19 18:47:26 -070063 mInfo = mBitmap->info();
64 mHasHardwareMipMap = mBitmap->hasHardwareMipMap();
65 mAllocationSize = mBitmap->getAllocationByteCount();
66 mRowBytes = mBitmap->rowBytes();
67 mGenerationId = mBitmap->getGenerationID();
sergeyv15a10852016-12-27 14:32:03 -080068 mIsHardware = mBitmap->isHardware();
sergeyvc1c54062016-10-19 18:47:26 -070069 mBitmap.reset();
John Reckf29ed282015-04-07 07:32:03 -070070 }
71
sergeyvc69853c2016-10-07 14:14:09 -070072 bool valid() {
Ben Wagner6b62ac02018-05-29 14:16:02 -040073 return mBitmap != nullptr;
John Reckf29ed282015-04-07 07:32:03 -070074 }
75
sergeyvaed7f582016-10-14 16:30:21 -070076 Bitmap& bitmap() {
77 assertValid();
78 return *mBitmap;
79 }
sergeyvc69853c2016-10-07 14:14:09 -070080
81 void assertValid() {
82 LOG_ALWAYS_FATAL_IF(!valid(), "Error, cannot access an invalid/free'd bitmap here!");
83 }
84
85 void getSkBitmap(SkBitmap* outBitmap) {
86 assertValid();
sergeyvc1c54062016-10-19 18:47:26 -070087 mBitmap->getSkBitmap(outBitmap);
sergeyvc69853c2016-10-07 14:14:09 -070088 }
89
90 bool hasHardwareMipMap() {
sergeyvc1c54062016-10-19 18:47:26 -070091 if (mBitmap) {
92 return mBitmap->hasHardwareMipMap();
John Reckf29ed282015-04-07 07:32:03 -070093 }
John Reckf29ed282015-04-07 07:32:03 -070094 return mHasHardwareMipMap;
95 }
96
97 void setHasHardwareMipMap(bool hasMipMap) {
sergeyvc69853c2016-10-07 14:14:09 -070098 assertValid();
sergeyvc1c54062016-10-19 18:47:26 -070099 mBitmap->setHasHardwareMipMap(hasMipMap);
John Reckf29ed282015-04-07 07:32:03 -0700100 }
101
sergeyvc69853c2016-10-07 14:14:09 -0700102 void setAlphaType(SkAlphaType alphaType) {
103 assertValid();
sergeyvc1c54062016-10-19 18:47:26 -0700104 mBitmap->setAlphaType(alphaType);
John Reckf29ed282015-04-07 07:32:03 -0700105 }
106
Derek Sollenberger202084c2019-01-14 13:55:08 -0500107 void setColorSpace(sk_sp<SkColorSpace> colorSpace) {
108 assertValid();
109 mBitmap->setColorSpace(colorSpace);
110 }
111
sergeyvc69853c2016-10-07 14:14:09 -0700112 const SkImageInfo& info() {
sergeyvc1c54062016-10-19 18:47:26 -0700113 if (mBitmap) {
114 return mBitmap->info();
sergeyvc69853c2016-10-07 14:14:09 -0700115 }
116 return mInfo;
John Reckf29ed282015-04-07 07:32:03 -0700117 }
118
sergeyvc69853c2016-10-07 14:14:09 -0700119 size_t getAllocationByteCount() const {
sergeyvc1c54062016-10-19 18:47:26 -0700120 if (mBitmap) {
121 return mBitmap->getAllocationByteCount();
sergeyvc69853c2016-10-07 14:14:09 -0700122 }
123 return mAllocationSize;
John Reckf29ed282015-04-07 07:32:03 -0700124 }
125
sergeyvc69853c2016-10-07 14:14:09 -0700126 size_t rowBytes() const {
sergeyvc1c54062016-10-19 18:47:26 -0700127 if (mBitmap) {
128 return mBitmap->rowBytes();
sergeyvc69853c2016-10-07 14:14:09 -0700129 }
130 return mRowBytes;
131 }
132
133 uint32_t getGenerationID() const {
sergeyvc1c54062016-10-19 18:47:26 -0700134 if (mBitmap) {
135 return mBitmap->getGenerationID();
sergeyvc69853c2016-10-07 14:14:09 -0700136 }
137 return mGenerationId;
138 }
139
sergeyv15a10852016-12-27 14:32:03 -0800140 bool isHardware() {
141 if (mBitmap) {
142 return mBitmap->isHardware();
143 }
144 return mIsHardware;
145 }
146
sergeyvc1c54062016-10-19 18:47:26 -0700147 ~BitmapWrapper() { }
sergeyvc69853c2016-10-07 14:14:09 -0700148
John Reckf29ed282015-04-07 07:32:03 -0700149private:
sergeyvc1c54062016-10-19 18:47:26 -0700150 sk_sp<Bitmap> mBitmap;
sergeyvc69853c2016-10-07 14:14:09 -0700151 SkImageInfo mInfo;
152 bool mHasHardwareMipMap;
153 size_t mAllocationSize;
154 size_t mRowBytes;
155 uint32_t mGenerationId;
sergeyv15a10852016-12-27 14:32:03 -0800156 bool mIsHardware;
John Reckf29ed282015-04-07 07:32:03 -0700157};
158
John Reckf29ed282015-04-07 07:32:03 -0700159// Convenience class that does not take a global ref on the pixels, relying
160// on the caller already having a local JNI ref
161class LocalScopedBitmap {
162public:
Chih-Hung Hsiehc6baf562016-04-27 11:29:23 -0700163 explicit LocalScopedBitmap(jlong bitmapHandle)
sergeyvc1c54062016-10-19 18:47:26 -0700164 : mBitmapWrapper(reinterpret_cast<BitmapWrapper*>(bitmapHandle)) {}
John Reckf29ed282015-04-07 07:32:03 -0700165
sergeyvc1c54062016-10-19 18:47:26 -0700166 BitmapWrapper* operator->() {
167 return mBitmapWrapper;
John Reckf29ed282015-04-07 07:32:03 -0700168 }
169
170 void* pixels() {
sergeyvaed7f582016-10-14 16:30:21 -0700171 return mBitmapWrapper->bitmap().pixels();
John Reckf29ed282015-04-07 07:32:03 -0700172 }
173
174 bool valid() {
sergeyvc1c54062016-10-19 18:47:26 -0700175 return mBitmapWrapper && mBitmapWrapper->valid();
John Reckf29ed282015-04-07 07:32:03 -0700176 }
177
178private:
sergeyvc1c54062016-10-19 18:47:26 -0700179 BitmapWrapper* mBitmapWrapper;
John Reckf29ed282015-04-07 07:32:03 -0700180};
181
sergeyvc69853c2016-10-07 14:14:09 -0700182namespace bitmap {
183
184// Assert that bitmap's SkAlphaType is consistent with isPremultiplied.
185static void assert_premultiplied(const SkImageInfo& info, bool isPremultiplied) {
186 // kOpaque_SkAlphaType and kIgnore_SkAlphaType mean that isPremultiplied is
187 // irrelevant. This just tests to ensure that the SkAlphaType is not
188 // opposite of isPremultiplied.
189 if (isPremultiplied) {
190 SkASSERT(info.alphaType() != kUnpremul_SkAlphaType);
191 } else {
192 SkASSERT(info.alphaType() != kPremul_SkAlphaType);
193 }
194}
195
196void reinitBitmap(JNIEnv* env, jobject javaBitmap, const SkImageInfo& info,
197 bool isPremultiplied)
198{
199 // The caller needs to have already set the alpha type properly, so the
200 // native SkBitmap stays in sync with the Java Bitmap.
201 assert_premultiplied(info, isPremultiplied);
202
203 env->CallVoidMethod(javaBitmap, gBitmap_reinitMethodID,
204 info.width(), info.height(), isPremultiplied);
205}
206
sergeyvc1c54062016-10-19 18:47:26 -0700207jobject createBitmap(JNIEnv* env, Bitmap* bitmap,
sergeyvc69853c2016-10-07 14:14:09 -0700208 int bitmapCreateFlags, jbyteArray ninePatchChunk, jobject ninePatchInsets,
209 int density) {
210 bool isMutable = bitmapCreateFlags & kBitmapCreateFlag_Mutable;
211 bool isPremultiplied = bitmapCreateFlags & kBitmapCreateFlag_Premultiplied;
212 // The caller needs to have already set the alpha type properly, so the
213 // native SkBitmap stays in sync with the Java Bitmap.
sergeyvc1c54062016-10-19 18:47:26 -0700214 assert_premultiplied(bitmap->info(), isPremultiplied);
Leon Scroggins IIIbbdb7312019-01-31 14:35:54 -0500215 bool fromMalloc = bitmap->pixelStorageType() == PixelStorageType::Heap;
sergeyvc1c54062016-10-19 18:47:26 -0700216 BitmapWrapper* bitmapWrapper = new BitmapWrapper(bitmap);
Nader Jawade7b51292018-04-12 17:55:31 -0700217 if (!isMutable) {
218 bitmapWrapper->bitmap().setImmutable();
219 }
sergeyvc69853c2016-10-07 14:14:09 -0700220 jobject obj = env->NewObject(gBitmap_class, gBitmap_constructorMethodID,
sergeyvc1c54062016-10-19 18:47:26 -0700221 reinterpret_cast<jlong>(bitmapWrapper), bitmap->width(), bitmap->height(), density,
Leon Scroggins IIIbbdb7312019-01-31 14:35:54 -0500222 isPremultiplied, ninePatchChunk, ninePatchInsets, fromMalloc);
sergeyvc69853c2016-10-07 14:14:09 -0700223
224 if (env->ExceptionCheck() != 0) {
225 ALOGE("*** Uncaught exception returned from Java call!\n");
226 env->ExceptionDescribe();
227 }
228 return obj;
229}
230
231void toSkBitmap(jlong bitmapHandle, SkBitmap* outBitmap) {
232 LocalScopedBitmap bitmap(bitmapHandle);
233 bitmap->getSkBitmap(outBitmap);
234}
235
Leon Scroggins III71fae622019-03-26 16:28:41 -0400236Bitmap& toBitmap(jlong bitmapHandle) {
sergeyv5fd2a1c2016-10-20 15:04:28 -0700237 LocalScopedBitmap localBitmap(bitmapHandle);
238 return localBitmap->bitmap();
239}
240
sergeyvc69853c2016-10-07 14:14:09 -0700241} // namespace bitmap
242
243} // namespace android
244
245using namespace android;
246using namespace android::bitmap;
247
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500248Bitmap* GraphicsJNI::getNativeBitmap(JNIEnv* env, jobject bitmap) {
249 SkASSERT(env);
250 SkASSERT(bitmap);
251 SkASSERT(env->IsInstanceOf(bitmap, gBitmap_class));
252 jlong bitmapHandle = env->GetLongField(bitmap, gBitmap_nativePtr);
253 LocalScopedBitmap localBitmap(bitmapHandle);
254 return localBitmap.valid() ? &localBitmap->bitmap() : nullptr;
255}
256
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500257SkImageInfo GraphicsJNI::getBitmapInfo(JNIEnv* env, jobject bitmap, uint32_t* outRowBytes,
258 bool* isHardware) {
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500259 SkASSERT(env);
260 SkASSERT(bitmap);
261 SkASSERT(env->IsInstanceOf(bitmap, gBitmap_class));
262 jlong bitmapHandle = env->GetLongField(bitmap, gBitmap_nativePtr);
263 LocalScopedBitmap localBitmap(bitmapHandle);
264 if (outRowBytes) {
265 *outRowBytes = localBitmap->rowBytes();
266 }
Leon Scroggins III84a2afc2020-01-19 19:27:16 -0500267 if (isHardware) {
268 *isHardware = localBitmap->isHardware();
269 }
Derek Sollenberger6c41ab12019-11-08 08:50:58 -0500270 return localBitmap->info();
271}
272
Chris Craik32054b02014-05-09 13:58:56 -0700273bool GraphicsJNI::SetPixels(JNIEnv* env, jintArray srcColors, int srcOffset, int srcStride,
Brian Osman91c9c282018-08-17 16:57:15 -0400274 int x, int y, int width, int height, SkBitmap* dstBitmap) {
Chris Craik32054b02014-05-09 13:58:56 -0700275 const jint* array = env->GetIntArrayElements(srcColors, NULL);
276 const SkColor* src = (const SkColor*)array + srcOffset;
277
Brian Osman91c9c282018-08-17 16:57:15 -0400278 auto sRGB = SkColorSpace::MakeSRGB();
279 SkImageInfo srcInfo = SkImageInfo::Make(
280 width, height, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType, sRGB);
281 SkPixmap srcPM(srcInfo, src, srcStride * 4);
Romain Guyce217fa2017-03-08 15:58:06 -0800282
Brian Osman91c9c282018-08-17 16:57:15 -0400283 dstBitmap->writePixels(srcPM, x, y);
Chris Craik32054b02014-05-09 13:58:56 -0700284
Romain Guy9505a652016-12-14 09:43:50 -0800285 env->ReleaseIntArrayElements(srcColors, const_cast<jint*>(array), JNI_ABORT);
Chris Craik32054b02014-05-09 13:58:56 -0700286 return true;
287}
288
Chris Craik32054b02014-05-09 13:58:56 -0700289///////////////////////////////////////////////////////////////////////////////
290///////////////////////////////////////////////////////////////////////////////
291
292static int getPremulBitmapCreateFlags(bool isMutable) {
sergeyvc69853c2016-10-07 14:14:09 -0700293 int flags = android::bitmap::kBitmapCreateFlag_Premultiplied;
294 if (isMutable) flags |= android::bitmap::kBitmapCreateFlag_Mutable;
Chris Craik32054b02014-05-09 13:58:56 -0700295 return flags;
296}
297
298static jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors,
299 jint offset, jint stride, jint width, jint height,
Romain Guy82426562017-04-04 19:38:50 -0700300 jint configHandle, jboolean isMutable,
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500301 jlong colorSpacePtr) {
Mike Reed1103b322014-07-08 12:36:44 -0400302 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700303 if (NULL != jColors) {
304 size_t n = env->GetArrayLength(jColors);
305 if (n < SkAbs32(stride) * (size_t)height) {
306 doThrowAIOOBE(env);
307 return NULL;
308 }
309 }
310
311 // ARGB_4444 is a deprecated format, convert automatically to 8888
Mike Reedb9330552014-06-16 17:31:48 -0400312 if (colorType == kARGB_4444_SkColorType) {
313 colorType = kN32_SkColorType;
Chris Craik32054b02014-05-09 13:58:56 -0700314 }
315
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500316 sk_sp<SkColorSpace> colorSpace;
317 if (colorType == kAlpha_8_SkColorType) {
318 colorSpace = nullptr;
319 } else {
320 colorSpace = GraphicsJNI::getNativeColorSpace(colorSpacePtr);
321 }
322
Chris Craik32054b02014-05-09 13:58:56 -0700323 SkBitmap bitmap;
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500324 bitmap.setInfo(SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType,
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500325 colorSpace));
Chris Craik32054b02014-05-09 13:58:56 -0700326
Leon Scroggins IIIf51a80d2017-07-12 10:46:35 -0400327 sk_sp<Bitmap> nativeBitmap = Bitmap::allocateHeapBitmap(&bitmap);
John Reckf29ed282015-04-07 07:32:03 -0700328 if (!nativeBitmap) {
Leon Scroggins IIIf3a02992017-10-03 14:00:20 -0400329 ALOGE("OOM allocating Bitmap with dimensions %i x %i", width, height);
330 doThrowOOME(env);
Chris Craik32054b02014-05-09 13:58:56 -0700331 return NULL;
332 }
333
334 if (jColors != NULL) {
Brian Osman91c9c282018-08-17 16:57:15 -0400335 GraphicsJNI::SetPixels(env, jColors, offset, stride, 0, 0, width, height, &bitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700336 }
337
sergeyvc36bd6c2016-10-11 15:49:16 -0700338 return createBitmap(env, nativeBitmap.release(), getPremulBitmapCreateFlags(isMutable));
Chris Craik32054b02014-05-09 13:58:56 -0700339}
340
Matt Sarett5320a722017-03-20 13:51:29 -0400341static bool bitmapCopyTo(SkBitmap* dst, SkColorType dstCT, const SkBitmap& src,
342 SkBitmap::Allocator* alloc) {
Matt Sarette9834402017-04-25 13:49:42 -0400343 SkPixmap srcPM;
344 if (!src.peekPixels(&srcPM)) {
345 return false;
346 }
347
348 SkImageInfo dstInfo = srcPM.info().makeColorType(dstCT);
349 switch (dstCT) {
350 case kRGB_565_SkColorType:
Brian Osmanbaf13e82018-09-21 11:21:30 -0400351 dstInfo = dstInfo.makeAlphaType(kOpaque_SkAlphaType);
Matt Sarette9834402017-04-25 13:49:42 -0400352 break;
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500353 case kAlpha_8_SkColorType:
354 dstInfo = dstInfo.makeColorSpace(nullptr);
Matt Sarette9834402017-04-25 13:49:42 -0400355 break;
356 default:
357 break;
358 }
359
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500360 if (!dstInfo.colorSpace() && dstCT != kAlpha_8_SkColorType) {
361 dstInfo = dstInfo.makeColorSpace(SkColorSpace::MakeSRGB());
362 }
363
Matt Sarette9834402017-04-25 13:49:42 -0400364 if (!dst->setInfo(dstInfo)) {
365 return false;
366 }
Mike Reed81397c42017-07-18 17:04:16 -0400367 if (!dst->tryAllocPixels(alloc)) {
Matt Sarette9834402017-04-25 13:49:42 -0400368 return false;
369 }
370
Matt Sarette9834402017-04-25 13:49:42 -0400371 SkPixmap dstPM;
372 if (!dst->peekPixels(&dstPM)) {
373 return false;
374 }
375
Matt Sarette9834402017-04-25 13:49:42 -0400376 return srcPM.readPixels(dstPM);
Matt Sarett5320a722017-03-20 13:51:29 -0400377}
378
Chris Craik32054b02014-05-09 13:58:56 -0700379static jobject Bitmap_copy(JNIEnv* env, jobject, jlong srcHandle,
380 jint dstConfigHandle, jboolean isMutable) {
John Reckf29ed282015-04-07 07:32:03 -0700381 SkBitmap src;
sergeyvc1c54062016-10-19 18:47:26 -0700382 reinterpret_cast<BitmapWrapper*>(srcHandle)->getSkBitmap(&src);
sergeyv05126d12016-12-15 19:50:15 -0800383 if (dstConfigHandle == GraphicsJNI::hardwareLegacyBitmapConfig()) {
384 sk_sp<Bitmap> bitmap(Bitmap::allocateHardwareBitmap(src));
385 if (!bitmap.get()) {
386 return NULL;
387 }
sergeyv656117b2017-02-28 15:25:10 -0800388 return createBitmap(env, bitmap.release(), getPremulBitmapCreateFlags(isMutable));
sergeyv05126d12016-12-15 19:50:15 -0800389 }
390
Mike Reed1103b322014-07-08 12:36:44 -0400391 SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle);
sergeyv45082182016-09-29 18:25:40 -0700392 SkBitmap result;
393 HeapAllocator allocator;
Chris Craik32054b02014-05-09 13:58:56 -0700394
Matt Sarett5320a722017-03-20 13:51:29 -0400395 if (!bitmapCopyTo(&result, dstCT, src, &allocator)) {
Chris Craik32054b02014-05-09 13:58:56 -0700396 return NULL;
397 }
sergeyvc1c54062016-10-19 18:47:26 -0700398 auto bitmap = allocator.getStorageObjAndReset();
399 return createBitmap(env, bitmap, getPremulBitmapCreateFlags(isMutable));
Chris Craik32054b02014-05-09 13:58:56 -0700400}
401
sergeyvc1c54062016-10-19 18:47:26 -0700402static Bitmap* Bitmap_copyAshmemImpl(JNIEnv* env, SkBitmap& src, SkColorType& dstCT) {
Riley Andrews721ae5f2015-05-11 16:08:22 -0700403 SkBitmap result;
404
405 AshmemPixelAllocator allocator(env);
Matt Sarett5320a722017-03-20 13:51:29 -0400406 if (!bitmapCopyTo(&result, dstCT, src, &allocator)) {
Riley Andrews721ae5f2015-05-11 16:08:22 -0700407 return NULL;
408 }
sergeyvc1c54062016-10-19 18:47:26 -0700409 auto bitmap = allocator.getStorageObjAndReset();
410 bitmap->setImmutable();
411 return bitmap;
Winsona5fdde92016-04-14 15:27:15 -0700412}
413
414static jobject Bitmap_copyAshmem(JNIEnv* env, jobject, jlong srcHandle) {
415 SkBitmap src;
sergeyvc1c54062016-10-19 18:47:26 -0700416 reinterpret_cast<BitmapWrapper*>(srcHandle)->getSkBitmap(&src);
Winsona5fdde92016-04-14 15:27:15 -0700417 SkColorType dstCT = src.colorType();
sergeyvc1c54062016-10-19 18:47:26 -0700418 auto bitmap = Bitmap_copyAshmemImpl(env, src, dstCT);
419 jobject ret = createBitmap(env, bitmap, getPremulBitmapCreateFlags(false));
Winsona5fdde92016-04-14 15:27:15 -0700420 return ret;
421}
422
423static jobject Bitmap_copyAshmemConfig(JNIEnv* env, jobject, jlong srcHandle, jint dstConfigHandle) {
424 SkBitmap src;
sergeyvc1c54062016-10-19 18:47:26 -0700425 reinterpret_cast<BitmapWrapper*>(srcHandle)->getSkBitmap(&src);
Winsona5fdde92016-04-14 15:27:15 -0700426 SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle);
sergeyvc1c54062016-10-19 18:47:26 -0700427 auto bitmap = Bitmap_copyAshmemImpl(env, src, dstCT);
428 jobject ret = createBitmap(env, bitmap, getPremulBitmapCreateFlags(false));
Riley Andrews721ae5f2015-05-11 16:08:22 -0700429 return ret;
430}
431
sergeyvc1c54062016-10-19 18:47:26 -0700432static void Bitmap_destruct(BitmapWrapper* bitmap) {
sergeyvc69853c2016-10-07 14:14:09 -0700433 delete bitmap;
Chris Craik32054b02014-05-09 13:58:56 -0700434}
435
Richard Uhler775873a2015-12-29 12:37:39 -0800436static jlong Bitmap_getNativeFinalizer(JNIEnv*, jobject) {
437 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&Bitmap_destruct));
438}
439
Leon Scroggins IIIf8adae12018-05-24 15:25:08 -0400440static void Bitmap_recycle(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700441 LocalScopedBitmap bitmap(bitmapHandle);
442 bitmap->freePixels();
Chris Craik32054b02014-05-09 13:58:56 -0700443}
444
445static void Bitmap_reconfigure(JNIEnv* env, jobject clazz, jlong bitmapHandle,
sergeyv45082182016-09-29 18:25:40 -0700446 jint width, jint height, jint configHandle, jboolean requestPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700447 LocalScopedBitmap bitmap(bitmapHandle);
sergeyvc69853c2016-10-07 14:14:09 -0700448 bitmap->assertValid();
Mike Reed1103b322014-07-08 12:36:44 -0400449 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400450
451 // ARGB_4444 is a deprecated format, convert automatically to 8888
452 if (colorType == kARGB_4444_SkColorType) {
453 colorType = kN32_SkColorType;
454 }
sergeyv45082182016-09-29 18:25:40 -0700455 size_t requestedSize = width * height * SkColorTypeBytesPerPixel(colorType);
456 if (requestedSize > bitmap->getAllocationByteCount()) {
Chris Craik32054b02014-05-09 13:58:56 -0700457 // done in native as there's no way to get BytesPerPixel in Java
458 doThrowIAE(env, "Bitmap not large enough to support new configuration");
459 return;
460 }
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400461 SkAlphaType alphaType;
John Reckf29ed282015-04-07 07:32:03 -0700462 if (bitmap->info().colorType() != kRGB_565_SkColorType
463 && bitmap->info().alphaType() == kOpaque_SkAlphaType) {
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400464 // If the original bitmap was set to opaque, keep that setting, unless it
465 // was 565, which is required to be opaque.
466 alphaType = kOpaque_SkAlphaType;
467 } else {
468 // Otherwise respect the premultiplied request.
469 alphaType = requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
470 }
sergeyvaed7f582016-10-14 16:30:21 -0700471 bitmap->bitmap().reconfigure(SkImageInfo::Make(width, height, colorType, alphaType,
sergeyv7d5219f2016-11-03 16:18:16 -0700472 sk_ref_sp(bitmap->info().colorSpace())));
Chris Craik32054b02014-05-09 13:58:56 -0700473}
474
Chris Craik32054b02014-05-09 13:58:56 -0700475static jboolean Bitmap_compress(JNIEnv* env, jobject clazz, jlong bitmapHandle,
476 jint format, jint quality,
477 jobject jstream, jbyteArray jstorage) {
Hal Canary10219fb2016-11-23 20:41:22 -0500478 LocalScopedBitmap bitmap(bitmapHandle);
John Reckf29ed282015-04-07 07:32:03 -0700479 if (!bitmap.valid()) {
480 return JNI_FALSE;
481 }
482
John Reckf29ed282015-04-07 07:32:03 -0700483 std::unique_ptr<SkWStream> strm(CreateJavaOutputStreamAdaptor(env, jstream, jstorage));
484 if (!strm.get()) {
485 return JNI_FALSE;
486 }
Chris Craik32054b02014-05-09 13:58:56 -0700487
Leon Scroggins III9010e8b2019-08-20 11:27:17 -0400488 auto fm = static_cast<Bitmap::JavaCompressFormat>(format);
Leon Scroggins III949c6002020-01-23 16:20:39 -0500489 return bitmap->bitmap().compress(fm, quality, strm.get()) ? JNI_TRUE : JNI_FALSE;
Chris Craik32054b02014-05-09 13:58:56 -0700490}
491
Leon Scroggins III4c4259b2018-12-17 10:40:07 -0500492static inline void bitmapErase(SkBitmap bitmap, const SkColor4f& color,
493 const sk_sp<SkColorSpace>& colorSpace) {
494 SkPaint p;
495 p.setColor4f(color, colorSpace.get());
496 p.setBlendMode(SkBlendMode::kSrc);
497 SkCanvas canvas(bitmap);
498 canvas.drawPaint(p);
499}
500
Chris Craik32054b02014-05-09 13:58:56 -0700501static void Bitmap_erase(JNIEnv* env, jobject, jlong bitmapHandle, jint color) {
John Reckf29ed282015-04-07 07:32:03 -0700502 LocalScopedBitmap bitmap(bitmapHandle);
503 SkBitmap skBitmap;
504 bitmap->getSkBitmap(&skBitmap);
Leon Scroggins III4c4259b2018-12-17 10:40:07 -0500505 bitmapErase(skBitmap, SkColor4f::FromColor(color), SkColorSpace::MakeSRGB());
506}
507
Leon Scroggins III94ba1002019-01-17 13:34:51 -0500508static void Bitmap_eraseLong(JNIEnv* env, jobject, jlong bitmapHandle,
509 jlong colorSpaceHandle, jlong colorLong) {
Leon Scroggins III4c4259b2018-12-17 10:40:07 -0500510 LocalScopedBitmap bitmap(bitmapHandle);
511 SkBitmap skBitmap;
512 bitmap->getSkBitmap(&skBitmap);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500513
Leon Scroggins III94ba1002019-01-17 13:34:51 -0500514 SkColor4f color = GraphicsJNI::convertColorLong(colorLong);
Leon Scroggins III0e443d12018-12-19 11:38:35 -0500515 sk_sp<SkColorSpace> cs = GraphicsJNI::getNativeColorSpace(colorSpaceHandle);
Leon Scroggins III4c4259b2018-12-17 10:40:07 -0500516 bitmapErase(skBitmap, color, cs);
Chris Craik32054b02014-05-09 13:58:56 -0700517}
518
519static jint Bitmap_rowBytes(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700520 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700521 return static_cast<jint>(bitmap->rowBytes());
522}
523
524static jint Bitmap_config(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700525 LocalScopedBitmap bitmap(bitmapHandle);
sergeyv15a10852016-12-27 14:32:03 -0800526 if (bitmap->isHardware()) {
sergeyv19b4b012016-12-13 16:06:00 -0800527 return GraphicsJNI::hardwareLegacyBitmapConfig();
528 }
John Reckf29ed282015-04-07 07:32:03 -0700529 return GraphicsJNI::colorTypeToLegacyBitmapConfig(bitmap->info().colorType());
Chris Craik32054b02014-05-09 13:58:56 -0700530}
531
532static jint Bitmap_getGenerationId(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700533 LocalScopedBitmap bitmap(bitmapHandle);
sergeyvc69853c2016-10-07 14:14:09 -0700534 return static_cast<jint>(bitmap->getGenerationID());
Chris Craik32054b02014-05-09 13:58:56 -0700535}
536
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400537static jboolean Bitmap_isPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700538 LocalScopedBitmap bitmap(bitmapHandle);
539 if (bitmap->info().alphaType() == kPremul_SkAlphaType) {
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400540 return JNI_TRUE;
541 }
542 return JNI_FALSE;
543}
544
Chris Craik32054b02014-05-09 13:58:56 -0700545static jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700546 LocalScopedBitmap bitmap(bitmapHandle);
547 return !bitmap->info().isOpaque() ? JNI_TRUE : JNI_FALSE;
Chris Craik32054b02014-05-09 13:58:56 -0700548}
549
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400550static void Bitmap_setHasAlpha(JNIEnv* env, jobject, jlong bitmapHandle,
551 jboolean hasAlpha, jboolean requestPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700552 LocalScopedBitmap bitmap(bitmapHandle);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400553 if (hasAlpha) {
John Reck0781a2f2015-05-27 16:29:17 -0700554 bitmap->setAlphaType(
John Reckf29ed282015-04-07 07:32:03 -0700555 requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType);
Chris Craik32054b02014-05-09 13:58:56 -0700556 } else {
John Reck0781a2f2015-05-27 16:29:17 -0700557 bitmap->setAlphaType(kOpaque_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400558 }
559}
560
561static void Bitmap_setPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle,
562 jboolean isPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700563 LocalScopedBitmap bitmap(bitmapHandle);
564 if (!bitmap->info().isOpaque()) {
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400565 if (isPremul) {
John Reck0781a2f2015-05-27 16:29:17 -0700566 bitmap->setAlphaType(kPremul_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400567 } else {
John Reck0781a2f2015-05-27 16:29:17 -0700568 bitmap->setAlphaType(kUnpremul_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400569 }
Chris Craik32054b02014-05-09 13:58:56 -0700570 }
571}
572
573static jboolean Bitmap_hasMipMap(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700574 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700575 return bitmap->hasHardwareMipMap() ? JNI_TRUE : JNI_FALSE;
576}
577
578static void Bitmap_setHasMipMap(JNIEnv* env, jobject, jlong bitmapHandle,
579 jboolean hasMipMap) {
John Reckf29ed282015-04-07 07:32:03 -0700580 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700581 bitmap->setHasHardwareMipMap(hasMipMap);
582}
583
584///////////////////////////////////////////////////////////////////////////////
585
John Recka18d7c82020-03-02 09:41:33 -0800586// TODO: Move somewhere else
Derek Sollenberger42c50042020-02-18 14:51:17 -0500587#ifdef __ANDROID__ // Layoutlib does not support parcel
Derek Sollenberger42c50042020-02-18 14:51:17 -0500588
John Recka18d7c82020-03-02 09:41:33 -0800589class ScopedParcel {
590public:
591 explicit ScopedParcel(JNIEnv* env, jobject parcel) {
592 mParcel = AParcel_fromJavaParcel(env, parcel);
Derek Sollenberger42c50042020-02-18 14:51:17 -0500593 }
John Recka18d7c82020-03-02 09:41:33 -0800594
595 ~ScopedParcel() { AParcel_delete(mParcel); }
596
597 int32_t readInt32() {
598 int32_t temp = 0;
599 // TODO: This behavior-matches what android::Parcel does
600 // but this should probably be better
601 if (AParcel_readInt32(mParcel, &temp) != STATUS_OK) {
602 temp = 0;
603 }
604 return temp;
605 }
606
607 uint32_t readUint32() {
608 uint32_t temp = 0;
609 // TODO: This behavior-matches what android::Parcel does
610 // but this should probably be better
611 if (AParcel_readUint32(mParcel, &temp) != STATUS_OK) {
612 temp = 0;
613 }
614 return temp;
615 }
616
617 void writeInt32(int32_t value) { AParcel_writeInt32(mParcel, value); }
618
619 void writeUint32(uint32_t value) { AParcel_writeUint32(mParcel, value); }
620
621 bool allowFds() const { return AParcel_getAllowFds(mParcel); }
622
623 std::optional<sk_sp<SkData>> readData() {
624 struct Data {
625 void* ptr = nullptr;
626 size_t size = 0;
627 } data;
628 auto error = AParcel_readByteArray(mParcel, &data,
629 [](void* arrayData, int32_t length,
630 int8_t** outBuffer) -> bool {
631 Data* data = reinterpret_cast<Data*>(arrayData);
632 if (length > 0) {
633 data->ptr = sk_malloc_canfail(length);
634 if (!data->ptr) {
635 return false;
636 }
637 *outBuffer =
638 reinterpret_cast<int8_t*>(data->ptr);
639 data->size = length;
640 }
641 return true;
642 });
643 if (error != STATUS_OK || data.size <= 0) {
644 sk_free(data.ptr);
645 return std::nullopt;
646 } else {
647 return SkData::MakeFromMalloc(data.ptr, data.size);
648 }
649 }
650
651 void writeData(const std::optional<sk_sp<SkData>>& optData) {
652 if (optData) {
653 const auto& data = *optData;
654 AParcel_writeByteArray(mParcel, reinterpret_cast<const int8_t*>(data->data()),
655 data->size());
656 } else {
657 AParcel_writeByteArray(mParcel, nullptr, -1);
658 }
659 }
660
661 AParcel* get() { return mParcel; }
662
663private:
664 AParcel* mParcel;
665};
666
667enum class BlobType : int32_t {
668 IN_PLACE,
669 ASHMEM,
670};
671
672#define ON_ERROR_RETURN(X) \
673 if ((error = (X)) != STATUS_OK) return error
674
675template <typename T, typename U>
676static binder_status_t readBlob(AParcel* parcel, T inPlaceCallback, U ashmemCallback) {
677 binder_status_t error = STATUS_OK;
678 BlobType type;
679 static_assert(sizeof(BlobType) == sizeof(int32_t));
680 ON_ERROR_RETURN(AParcel_readInt32(parcel, (int32_t*)&type));
681 if (type == BlobType::IN_PLACE) {
682 struct Data {
683 std::unique_ptr<int8_t[]> ptr = nullptr;
684 int32_t size = 0;
685 } data;
686 ON_ERROR_RETURN(
687 AParcel_readByteArray(parcel, &data,
688 [](void* arrayData, int32_t length, int8_t** outBuffer) {
689 Data* data = reinterpret_cast<Data*>(arrayData);
690 if (length > 0) {
691 data->ptr = std::make_unique<int8_t[]>(length);
692 data->size = length;
693 *outBuffer = data->ptr.get();
694 }
695 return data->ptr != nullptr;
696 }));
John Reckaca134f2022-01-18 13:23:00 -0500697 return inPlaceCallback(std::move(data.ptr), data.size);
John Recka18d7c82020-03-02 09:41:33 -0800698 } else if (type == BlobType::ASHMEM) {
699 int rawFd = -1;
700 int32_t size = 0;
701 ON_ERROR_RETURN(AParcel_readInt32(parcel, &size));
702 ON_ERROR_RETURN(AParcel_readParcelFileDescriptor(parcel, &rawFd));
703 android::base::unique_fd fd(rawFd);
John Reckaca134f2022-01-18 13:23:00 -0500704 return ashmemCallback(std::move(fd), size);
John Recka18d7c82020-03-02 09:41:33 -0800705 } else {
706 // Although the above if/else was "exhaustive" guard against unknown types
707 return STATUS_UNKNOWN_ERROR;
708 }
Derek Sollenberger42c50042020-02-18 14:51:17 -0500709}
John Recka18d7c82020-03-02 09:41:33 -0800710
711static constexpr size_t BLOB_INPLACE_LIMIT = 12 * 1024;
712// Fail fast if we can't use ashmem and the size exceeds this limit - the binder transaction
713// wouldn't go through, anyway
714// TODO: Can we get this from somewhere?
715static constexpr size_t BLOB_MAX_INPLACE_LIMIT = 1 * 1024 * 1024;
716static constexpr bool shouldUseAshmem(AParcel* parcel, int32_t size) {
717 return size > BLOB_INPLACE_LIMIT && AParcel_getAllowFds(parcel);
718}
719
720static binder_status_t writeBlobFromFd(AParcel* parcel, int32_t size, int fd) {
721 binder_status_t error = STATUS_OK;
722 ON_ERROR_RETURN(AParcel_writeInt32(parcel, static_cast<int32_t>(BlobType::ASHMEM)));
723 ON_ERROR_RETURN(AParcel_writeInt32(parcel, size));
724 ON_ERROR_RETURN(AParcel_writeParcelFileDescriptor(parcel, fd));
725 return STATUS_OK;
726}
727
John Reck6a93b4f2020-04-10 17:12:00 -0700728static binder_status_t writeBlob(AParcel* parcel, const int32_t size, const void* data, bool immutable) {
John Recka18d7c82020-03-02 09:41:33 -0800729 if (size <= 0 || data == nullptr) {
730 return STATUS_NOT_ENOUGH_DATA;
731 }
732 binder_status_t error = STATUS_OK;
733 if (shouldUseAshmem(parcel, size)) {
734 // Create new ashmem region with read/write priv
735 base::unique_fd fd(ashmem_create_region("bitmap", size));
736 if (fd.get() < 0) {
737 return STATUS_NO_MEMORY;
738 }
739
740 {
741 void* dest = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd.get(), 0);
742 if (dest == MAP_FAILED) {
743 return STATUS_NO_MEMORY;
744 }
745 memcpy(dest, data, size);
746 munmap(dest, size);
747 }
748
John Reck6a93b4f2020-04-10 17:12:00 -0700749 if (immutable && ashmem_set_prot_region(fd.get(), PROT_READ) < 0) {
John Recka18d7c82020-03-02 09:41:33 -0800750 return STATUS_UNKNOWN_ERROR;
751 }
752 // Workaround b/149851140 in AParcel_writeParcelFileDescriptor
753 int rawFd = fd.release();
754 error = writeBlobFromFd(parcel, size, rawFd);
755 close(rawFd);
756 return error;
757 } else {
758 if (size > BLOB_MAX_INPLACE_LIMIT) {
759 return STATUS_FAILED_TRANSACTION;
760 }
761 ON_ERROR_RETURN(AParcel_writeInt32(parcel, static_cast<int32_t>(BlobType::IN_PLACE)));
762 ON_ERROR_RETURN(AParcel_writeByteArray(parcel, static_cast<const int8_t*>(data), size));
763 return STATUS_OK;
764 }
765}
766
767#undef ON_ERROR_RETURN
768
769#endif // __ANDROID__ // Layoutlib does not support parcel
Derek Sollenberger42c50042020-02-18 14:51:17 -0500770
Matt Sarett3ca39752017-05-26 10:55:38 -0400771// This is the maximum possible size because the SkColorSpace must be
772// representable (and therefore serializable) using a matrix and numerical
773// transfer function. If we allow more color space representations in the
774// framework, we may need to update this maximum size.
John Recka18d7c82020-03-02 09:41:33 -0800775static constexpr size_t kMaxColorSpaceSerializedBytes = 80;
776
John Reckaca134f2022-01-18 13:23:00 -0500777static constexpr auto BadParcelableException = "android/os/BadParcelableException";
John Recka18d7c82020-03-02 09:41:33 -0800778
779static bool validateImageInfo(const SkImageInfo& info, int32_t rowBytes) {
780 // TODO: Can we avoid making a SkBitmap for this?
781 return SkBitmap().setInfo(info, rowBytes);
782}
Matt Sarett3ca39752017-05-26 10:55:38 -0400783
Chris Craik32054b02014-05-09 13:58:56 -0700784static jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) {
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100785#ifdef __ANDROID__ // Layoutlib does not support parcel
Chris Craik32054b02014-05-09 13:58:56 -0700786 if (parcel == NULL) {
John Recka18d7c82020-03-02 09:41:33 -0800787 jniThrowNullPointerException(env, "parcel cannot be null");
Chris Craik32054b02014-05-09 13:58:56 -0700788 return NULL;
789 }
790
John Recka18d7c82020-03-02 09:41:33 -0800791 ScopedParcel p(env, parcel);
Chris Craik32054b02014-05-09 13:58:56 -0700792
John Reck6a93b4f2020-04-10 17:12:00 -0700793 const bool isMutable = p.readInt32();
John Recka18d7c82020-03-02 09:41:33 -0800794 const SkColorType colorType = static_cast<SkColorType>(p.readInt32());
795 const SkAlphaType alphaType = static_cast<SkAlphaType>(p.readInt32());
Romain Guy5acc4762017-03-07 15:29:27 -0800796 sk_sp<SkColorSpace> colorSpace;
John Recka18d7c82020-03-02 09:41:33 -0800797 const auto optColorSpaceData = p.readData();
798 if (optColorSpaceData) {
799 const auto& colorSpaceData = *optColorSpaceData;
800 if (colorSpaceData->size() > kMaxColorSpaceSerializedBytes) {
Matt Sarett3ca39752017-05-26 10:55:38 -0400801 ALOGD("Bitmap_createFromParcel: Serialized SkColorSpace is larger than expected: "
John Recka18d7c82020-03-02 09:41:33 -0800802 "%zu bytes (max: %zu)\n",
803 colorSpaceData->size(), kMaxColorSpaceSerializedBytes);
Matt Sarett3ca39752017-05-26 10:55:38 -0400804 }
805
John Recka18d7c82020-03-02 09:41:33 -0800806 colorSpace = SkColorSpace::Deserialize(colorSpaceData->data(), colorSpaceData->size());
Romain Guy5acc4762017-03-07 15:29:27 -0800807 }
John Recka18d7c82020-03-02 09:41:33 -0800808 const int32_t width = p.readInt32();
809 const int32_t height = p.readInt32();
810 const int32_t rowBytes = p.readInt32();
811 const int32_t density = p.readInt32();
Chris Craik32054b02014-05-09 13:58:56 -0700812
Mike Reedb9330552014-06-16 17:31:48 -0400813 if (kN32_SkColorType != colorType &&
Romain Guy9505a652016-12-14 09:43:50 -0800814 kRGBA_F16_SkColorType != colorType &&
Mike Reedb9330552014-06-16 17:31:48 -0400815 kRGB_565_SkColorType != colorType &&
816 kARGB_4444_SkColorType != colorType &&
Mike Reedb9330552014-06-16 17:31:48 -0400817 kAlpha_8_SkColorType != colorType) {
John Reckaca134f2022-01-18 13:23:00 -0500818 jniThrowExceptionFmt(env, BadParcelableException,
John Recka18d7c82020-03-02 09:41:33 -0800819 "Bitmap_createFromParcel unknown colortype: %d\n", colorType);
Chris Craik32054b02014-05-09 13:58:56 -0700820 return NULL;
821 }
822
John Recka18d7c82020-03-02 09:41:33 -0800823 auto imageInfo = SkImageInfo::Make(width, height, colorType, alphaType, colorSpace);
824 size_t allocationSize = 0;
825 if (!validateImageInfo(imageInfo, rowBytes)) {
826 jniThrowRuntimeException(env, "Received bad SkImageInfo");
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400827 return NULL;
828 }
John Recka18d7c82020-03-02 09:41:33 -0800829 if (!Bitmap::computeAllocationSize(rowBytes, height, &allocationSize)) {
John Reckaca134f2022-01-18 13:23:00 -0500830 jniThrowExceptionFmt(env, BadParcelableException,
John Recka18d7c82020-03-02 09:41:33 -0800831 "Received bad bitmap size: width=%d, height=%d, rowBytes=%d", width,
832 height, rowBytes);
Chris Craik32054b02014-05-09 13:58:56 -0700833 return NULL;
834 }
sergeyvc1c54062016-10-19 18:47:26 -0700835 sk_sp<Bitmap> nativeBitmap;
John Recka18d7c82020-03-02 09:41:33 -0800836 binder_status_t error = readBlob(
837 p.get(),
838 // In place callback
839 [&](std::unique_ptr<int8_t[]> buffer, int32_t size) {
John Reckaca134f2022-01-18 13:23:00 -0500840 if (allocationSize > size) {
841 android_errorWriteLog(0x534e4554, "213169612");
842 return STATUS_BAD_VALUE;
843 }
John Recka18d7c82020-03-02 09:41:33 -0800844 nativeBitmap = Bitmap::allocateHeapBitmap(allocationSize, imageInfo, rowBytes);
845 if (nativeBitmap) {
John Reckaca134f2022-01-18 13:23:00 -0500846 memcpy(nativeBitmap->pixels(), buffer.get(), allocationSize);
847 return STATUS_OK;
John Recka18d7c82020-03-02 09:41:33 -0800848 }
John Reckaca134f2022-01-18 13:23:00 -0500849 return STATUS_NO_MEMORY;
John Recka18d7c82020-03-02 09:41:33 -0800850 },
851 // Ashmem callback
852 [&](android::base::unique_fd fd, int32_t size) {
John Reckaca134f2022-01-18 13:23:00 -0500853 if (allocationSize > size) {
854 android_errorWriteLog(0x534e4554, "213169612");
855 return STATUS_BAD_VALUE;
856 }
John Reck6a93b4f2020-04-10 17:12:00 -0700857 int flags = PROT_READ;
858 if (isMutable) {
859 flags |= PROT_WRITE;
860 }
861 void* addr = mmap(nullptr, size, flags, MAP_SHARED, fd.get(), 0);
John Recka18d7c82020-03-02 09:41:33 -0800862 if (addr == MAP_FAILED) {
John Reck6a93b4f2020-04-10 17:12:00 -0700863 const int err = errno;
864 ALOGW("mmap failed, error %d (%s)", err, strerror(err));
John Reckaca134f2022-01-18 13:23:00 -0500865 return STATUS_NO_MEMORY;
John Recka18d7c82020-03-02 09:41:33 -0800866 }
867 nativeBitmap =
John Reck6a93b4f2020-04-10 17:12:00 -0700868 Bitmap::createFrom(imageInfo, rowBytes, fd.release(), addr, size, !isMutable);
John Reckaca134f2022-01-18 13:23:00 -0500869 return STATUS_OK;
John Recka18d7c82020-03-02 09:41:33 -0800870 });
John Reckaca134f2022-01-18 13:23:00 -0500871
872 if (error != STATUS_OK && error != STATUS_NO_MEMORY) {
John Recka18d7c82020-03-02 09:41:33 -0800873 // TODO: Stringify the error, see signalExceptionForError in android_util_Binder.cpp
John Reckaca134f2022-01-18 13:23:00 -0500874 jniThrowExceptionFmt(env, BadParcelableException, "Failed to read from Parcel, error=%d",
875 error);
John Recka18d7c82020-03-02 09:41:33 -0800876 return nullptr;
877 }
John Reckaca134f2022-01-18 13:23:00 -0500878 if (error == STATUS_NO_MEMORY || !nativeBitmap) {
879 jniThrowRuntimeException(env, "Could not allocate bitmap data.");
John Recka18d7c82020-03-02 09:41:33 -0800880 return nullptr;
Chris Craik32054b02014-05-09 13:58:56 -0700881 }
Chris Craik32054b02014-05-09 13:58:56 -0700882
John Reck6a93b4f2020-04-10 17:12:00 -0700883 return createBitmap(env, nativeBitmap.release(), getPremulBitmapCreateFlags(isMutable), nullptr,
John Recka18d7c82020-03-02 09:41:33 -0800884 nullptr, density);
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100885#else
John Recka18d7c82020-03-02 09:41:33 -0800886 jniThrowRuntimeException(env, "Cannot use parcels outside of Android");
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100887 return NULL;
888#endif
Chris Craik32054b02014-05-09 13:58:56 -0700889}
890
891static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject,
John Reckefac0522020-01-24 16:04:26 -0800892 jlong bitmapHandle, jint density, jobject parcel) {
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100893#ifdef __ANDROID__ // Layoutlib does not support parcel
Chris Craik32054b02014-05-09 13:58:56 -0700894 if (parcel == NULL) {
Sally Qi7e3f93b2021-07-15 00:00:54 +0000895 ALOGD("------- writeToParcel null parcel\n");
Chris Craik32054b02014-05-09 13:58:56 -0700896 return JNI_FALSE;
897 }
898
John Recka18d7c82020-03-02 09:41:33 -0800899 ScopedParcel p(env, parcel);
John Reckf29ed282015-04-07 07:32:03 -0700900 SkBitmap bitmap;
Riley Andrews39d7f302014-11-13 17:43:25 -0800901
sergeyvc1c54062016-10-19 18:47:26 -0700902 auto bitmapWrapper = reinterpret_cast<BitmapWrapper*>(bitmapHandle);
903 bitmapWrapper->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700904
John Reck6a93b4f2020-04-10 17:12:00 -0700905 p.writeInt32(!bitmap.isImmutable());
John Recka18d7c82020-03-02 09:41:33 -0800906 p.writeInt32(bitmap.colorType());
907 p.writeInt32(bitmap.alphaType());
Romain Guy5acc4762017-03-07 15:29:27 -0800908 SkColorSpace* colorSpace = bitmap.colorSpace();
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -0500909 if (colorSpace != nullptr) {
John Recka18d7c82020-03-02 09:41:33 -0800910 p.writeData(colorSpace->serialize());
Romain Guy5acc4762017-03-07 15:29:27 -0800911 } else {
John Recka18d7c82020-03-02 09:41:33 -0800912 p.writeData(std::nullopt);
Romain Guy5acc4762017-03-07 15:29:27 -0800913 }
John Recka18d7c82020-03-02 09:41:33 -0800914 p.writeInt32(bitmap.width());
915 p.writeInt32(bitmap.height());
916 p.writeInt32(bitmap.rowBytes());
917 p.writeInt32(density);
Chris Craik32054b02014-05-09 13:58:56 -0700918
Jeff Browna316c5d2015-06-05 15:14:06 -0700919 // Transfer the underlying ashmem region if we have one and it's immutable.
John Recka18d7c82020-03-02 09:41:33 -0800920 binder_status_t status;
sergeyvaed7f582016-10-14 16:30:21 -0700921 int fd = bitmapWrapper->bitmap().getAshmemFd();
John Reck6a93b4f2020-04-10 17:12:00 -0700922 if (fd >= 0 && p.allowFds() && bitmap.isImmutable()) {
Jeff Browna316c5d2015-06-05 15:14:06 -0700923#if DEBUG_PARCEL
924 ALOGD("Bitmap.writeToParcel: transferring immutable bitmap's ashmem fd as "
John Recka18d7c82020-03-02 09:41:33 -0800925 "immutable blob (fds %s)",
926 p.allowFds() ? "allowed" : "forbidden");
Jeff Browna316c5d2015-06-05 15:14:06 -0700927#endif
928
John Recka18d7c82020-03-02 09:41:33 -0800929 status = writeBlobFromFd(p.get(), bitmapWrapper->bitmap().getAllocationByteCount(), fd);
930 if (status != STATUS_OK) {
Jeff Browna316c5d2015-06-05 15:14:06 -0700931 doThrowRE(env, "Could not write bitmap blob file descriptor.");
Riley Andrews39d7f302014-11-13 17:43:25 -0800932 return JNI_FALSE;
933 }
Jeff Browna316c5d2015-06-05 15:14:06 -0700934 return JNI_TRUE;
Riley Andrews39d7f302014-11-13 17:43:25 -0800935 }
Jeff Browna316c5d2015-06-05 15:14:06 -0700936
937 // Copy the bitmap to a new blob.
Jeff Browna316c5d2015-06-05 15:14:06 -0700938#if DEBUG_PARCEL
John Reckefac0522020-01-24 16:04:26 -0800939 ALOGD("Bitmap.writeToParcel: copying bitmap into new blob (fds %s)",
John Recka18d7c82020-03-02 09:41:33 -0800940 p.allowFds() ? "allowed" : "forbidden");
Jeff Browna316c5d2015-06-05 15:14:06 -0700941#endif
942
Mike Reed7569de02017-10-06 16:25:49 -0400943 size_t size = bitmap.computeByteSize();
John Reck6a93b4f2020-04-10 17:12:00 -0700944 status = writeBlob(p.get(), size, bitmap.getPixels(), bitmap.isImmutable());
Jeff Browna316c5d2015-06-05 15:14:06 -0700945 if (status) {
946 doThrowRE(env, "Could not copy bitmap to parcel blob.");
947 return JNI_FALSE;
948 }
Chris Craik32054b02014-05-09 13:58:56 -0700949 return JNI_TRUE;
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100950#else
951 doThrowRE(env, "Cannot use parcels outside of Android");
952 return JNI_FALSE;
953#endif
Chris Craik32054b02014-05-09 13:58:56 -0700954}
955
956static jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz,
957 jlong srcHandle, jlong paintHandle,
958 jintArray offsetXY) {
John Reckf29ed282015-04-07 07:32:03 -0700959 SkBitmap src;
sergeyvc1c54062016-10-19 18:47:26 -0700960 reinterpret_cast<BitmapWrapper*>(srcHandle)->getSkBitmap(&src);
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -0400961 const android::Paint* paint = reinterpret_cast<android::Paint*>(paintHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700962 SkIPoint offset;
John Reckf29ed282015-04-07 07:32:03 -0700963 SkBitmap dst;
sergeyv45082182016-09-29 18:25:40 -0700964 HeapAllocator allocator;
Chris Craik32054b02014-05-09 13:58:56 -0700965
John Reckf29ed282015-04-07 07:32:03 -0700966 src.extractAlpha(&dst, paint, &allocator, &offset);
Chris Craik32054b02014-05-09 13:58:56 -0700967 // If Skia can't allocate pixels for destination bitmap, it resets
968 // it, that is set its pixels buffer to NULL, and zero width and height.
John Reckf29ed282015-04-07 07:32:03 -0700969 if (dst.getPixels() == NULL && src.getPixels() != NULL) {
Chris Craik32054b02014-05-09 13:58:56 -0700970 doThrowOOME(env, "failed to allocate pixels for alpha");
971 return NULL;
972 }
973 if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) {
974 int* array = env->GetIntArrayElements(offsetXY, NULL);
975 array[0] = offset.fX;
976 array[1] = offset.fY;
977 env->ReleaseIntArrayElements(offsetXY, array, 0);
978 }
979
sergeyvc69853c2016-10-07 14:14:09 -0700980 return createBitmap(env, allocator.getStorageObjAndReset(),
John Reckf29ed282015-04-07 07:32:03 -0700981 getPremulBitmapCreateFlags(true));
Chris Craik32054b02014-05-09 13:58:56 -0700982}
983
984///////////////////////////////////////////////////////////////////////////////
985
Romain Guyefb4b062017-02-27 11:00:04 -0800986static jboolean Bitmap_isSRGB(JNIEnv* env, jobject, jlong bitmapHandle) {
987 LocalScopedBitmap bitmapHolder(bitmapHandle);
988 if (!bitmapHolder.valid()) return JNI_TRUE;
989
990 SkColorSpace* colorSpace = bitmapHolder->info().colorSpace();
Brian Osman91c9c282018-08-17 16:57:15 -0400991 return colorSpace == nullptr || colorSpace->isSRGB();
Romain Guyefb4b062017-02-27 11:00:04 -0800992}
993
Leon Scroggins IIIce89a6e2018-03-13 15:39:39 -0400994static jboolean Bitmap_isSRGBLinear(JNIEnv* env, jobject, jlong bitmapHandle) {
995 LocalScopedBitmap bitmapHolder(bitmapHandle);
996 if (!bitmapHolder.valid()) return JNI_FALSE;
997
998 SkColorSpace* colorSpace = bitmapHolder->info().colorSpace();
999 sk_sp<SkColorSpace> srgbLinear = SkColorSpace::MakeSRGBLinear();
1000 return colorSpace == srgbLinear.get() ? JNI_TRUE : JNI_FALSE;
1001}
1002
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -05001003static jobject Bitmap_computeColorSpace(JNIEnv* env, jobject, jlong bitmapHandle) {
Romain Guyefb4b062017-02-27 11:00:04 -08001004 LocalScopedBitmap bitmapHolder(bitmapHandle);
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -05001005 if (!bitmapHolder.valid()) return nullptr;
Romain Guyefb4b062017-02-27 11:00:04 -08001006
1007 SkColorSpace* colorSpace = bitmapHolder->info().colorSpace();
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -05001008 if (colorSpace == nullptr) return nullptr;
Romain Guyefb4b062017-02-27 11:00:04 -08001009
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -05001010 return GraphicsJNI::getColorSpace(env, colorSpace, bitmapHolder->info().colorType());
Romain Guyefb4b062017-02-27 11:00:04 -08001011}
1012
Derek Sollenberger202084c2019-01-14 13:55:08 -05001013static void Bitmap_setColorSpace(JNIEnv* env, jobject, jlong bitmapHandle, jlong colorSpacePtr) {
1014 LocalScopedBitmap bitmapHolder(bitmapHandle);
1015 sk_sp<SkColorSpace> cs = GraphicsJNI::getNativeColorSpace(colorSpacePtr);
1016 bitmapHolder->setColorSpace(cs);
1017}
1018
Romain Guyefb4b062017-02-27 11:00:04 -08001019///////////////////////////////////////////////////////////////////////////////
1020
Chris Craik32054b02014-05-09 13:58:56 -07001021static jint Bitmap_getPixel(JNIEnv* env, jobject, jlong bitmapHandle,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001022 jint x, jint y) {
John Reckf29ed282015-04-07 07:32:03 -07001023 SkBitmap bitmap;
sergeyvc1c54062016-10-19 18:47:26 -07001024 reinterpret_cast<BitmapWrapper*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001025
Brian Osman91c9c282018-08-17 16:57:15 -04001026 auto sRGB = SkColorSpace::MakeSRGB();
1027 SkImageInfo dstInfo = SkImageInfo::Make(
1028 1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType, sRGB);
Chris Craik32054b02014-05-09 13:58:56 -07001029
Brian Osman91c9c282018-08-17 16:57:15 -04001030 SkColor dst;
1031 bitmap.readPixels(dstInfo, &dst, dstInfo.minRowBytes(), x, y);
1032 return static_cast<jint>(dst);
Chris Craik32054b02014-05-09 13:58:56 -07001033}
1034
Leon Scroggins III870053d2019-01-24 08:37:27 -05001035static jlong Bitmap_getColor(JNIEnv* env, jobject, jlong bitmapHandle,
1036 jint x, jint y) {
1037 SkBitmap bitmap;
1038 reinterpret_cast<BitmapWrapper*>(bitmapHandle)->getSkBitmap(&bitmap);
1039
1040 SkImageInfo dstInfo = SkImageInfo::Make(
1041 1, 1, kRGBA_F16_SkColorType, kUnpremul_SkAlphaType, bitmap.refColorSpace());
1042
1043 uint64_t dst;
1044 bitmap.readPixels(dstInfo, &dst, dstInfo.minRowBytes(), x, y);
1045 return static_cast<jlong>(dst);
1046}
1047
Chris Craik32054b02014-05-09 13:58:56 -07001048static void Bitmap_getPixels(JNIEnv* env, jobject, jlong bitmapHandle,
1049 jintArray pixelArray, jint offset, jint stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001050 jint x, jint y, jint width, jint height) {
John Reckf29ed282015-04-07 07:32:03 -07001051 SkBitmap bitmap;
sergeyvc1c54062016-10-19 18:47:26 -07001052 reinterpret_cast<BitmapWrapper*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001053
Brian Osman91c9c282018-08-17 16:57:15 -04001054 auto sRGB = SkColorSpace::MakeSRGB();
1055 SkImageInfo dstInfo = SkImageInfo::Make(
1056 width, height, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType, sRGB);
Chris Craik32054b02014-05-09 13:58:56 -07001057
Chris Craik32054b02014-05-09 13:58:56 -07001058 jint* dst = env->GetIntArrayElements(pixelArray, NULL);
Brian Osman91c9c282018-08-17 16:57:15 -04001059 bitmap.readPixels(dstInfo, dst + offset, stride * 4, x, y);
Chris Craik32054b02014-05-09 13:58:56 -07001060 env->ReleaseIntArrayElements(pixelArray, dst, 0);
1061}
1062
1063///////////////////////////////////////////////////////////////////////////////
1064
1065static void Bitmap_setPixel(JNIEnv* env, jobject, jlong bitmapHandle,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001066 jint x, jint y, jint colorHandle) {
John Reckf29ed282015-04-07 07:32:03 -07001067 SkBitmap bitmap;
sergeyvc1c54062016-10-19 18:47:26 -07001068 reinterpret_cast<BitmapWrapper*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001069 SkColor color = static_cast<SkColor>(colorHandle);
Chris Craik32054b02014-05-09 13:58:56 -07001070
Brian Osman91c9c282018-08-17 16:57:15 -04001071 auto sRGB = SkColorSpace::MakeSRGB();
1072 SkImageInfo srcInfo = SkImageInfo::Make(
1073 1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType, sRGB);
1074 SkPixmap srcPM(srcInfo, &color, srcInfo.minRowBytes());
Chris Craik32054b02014-05-09 13:58:56 -07001075
Brian Osman91c9c282018-08-17 16:57:15 -04001076 bitmap.writePixels(srcPM, x, y);
Chris Craik32054b02014-05-09 13:58:56 -07001077}
1078
1079static void Bitmap_setPixels(JNIEnv* env, jobject, jlong bitmapHandle,
1080 jintArray pixelArray, jint offset, jint stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001081 jint x, jint y, jint width, jint height) {
John Reckf29ed282015-04-07 07:32:03 -07001082 SkBitmap bitmap;
sergeyvc1c54062016-10-19 18:47:26 -07001083 reinterpret_cast<BitmapWrapper*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001084 GraphicsJNI::SetPixels(env, pixelArray, offset, stride,
Brian Osman91c9c282018-08-17 16:57:15 -04001085 x, y, width, height, &bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001086}
1087
1088static void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject,
1089 jlong bitmapHandle, jobject jbuffer) {
John Reckf29ed282015-04-07 07:32:03 -07001090 SkBitmap bitmap;
sergeyvc1c54062016-10-19 18:47:26 -07001091 reinterpret_cast<BitmapWrapper*>(bitmapHandle)->getSkBitmap(&bitmap);
John Reckf29ed282015-04-07 07:32:03 -07001092 const void* src = bitmap.getPixels();
Chris Craik32054b02014-05-09 13:58:56 -07001093
1094 if (NULL != src) {
1095 android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE);
1096
1097 // the java side has already checked that buffer is large enough
Mike Reed7569de02017-10-06 16:25:49 -04001098 memcpy(abp.pointer(), src, bitmap.computeByteSize());
Chris Craik32054b02014-05-09 13:58:56 -07001099 }
1100}
1101
1102static void Bitmap_copyPixelsFromBuffer(JNIEnv* env, jobject,
1103 jlong bitmapHandle, jobject jbuffer) {
John Reckf29ed282015-04-07 07:32:03 -07001104 SkBitmap bitmap;
sergeyvc1c54062016-10-19 18:47:26 -07001105 reinterpret_cast<BitmapWrapper*>(bitmapHandle)->getSkBitmap(&bitmap);
John Reckf29ed282015-04-07 07:32:03 -07001106 void* dst = bitmap.getPixels();
Chris Craik32054b02014-05-09 13:58:56 -07001107
1108 if (NULL != dst) {
1109 android::AutoBufferPointer abp(env, jbuffer, JNI_FALSE);
1110 // the java side has already checked that buffer is large enough
Mike Reed7569de02017-10-06 16:25:49 -04001111 memcpy(dst, abp.pointer(), bitmap.computeByteSize());
John Reckf29ed282015-04-07 07:32:03 -07001112 bitmap.notifyPixelsChanged();
Chris Craik32054b02014-05-09 13:58:56 -07001113 }
1114}
1115
Chris Craik795bd0f2016-12-16 15:22:31 -08001116static jboolean Bitmap_sameAs(JNIEnv* env, jobject, jlong bm0Handle, jlong bm1Handle) {
John Reckf29ed282015-04-07 07:32:03 -07001117 SkBitmap bm0;
1118 SkBitmap bm1;
sergeyv1eabed32016-12-14 14:19:47 -08001119
1120 LocalScopedBitmap bitmap0(bm0Handle);
1121 LocalScopedBitmap bitmap1(bm1Handle);
1122
1123 // Paying the price for making Hardware Bitmap as Config:
1124 // later check for colorType will pass successfully,
1125 // because Hardware Config internally may be RGBA8888 or smth like that.
sergeyv15a10852016-12-27 14:32:03 -08001126 if (bitmap0->isHardware() != bitmap1->isHardware()) {
sergeyv1eabed32016-12-14 14:19:47 -08001127 return JNI_FALSE;
1128 }
1129
1130 bitmap0->bitmap().getSkBitmap(&bm0);
1131 bitmap1->bitmap().getSkBitmap(&bm1);
Chris Craik795bd0f2016-12-16 15:22:31 -08001132 if (bm0.width() != bm1.width()
1133 || bm0.height() != bm1.height()
1134 || bm0.colorType() != bm1.colorType()
1135 || bm0.alphaType() != bm1.alphaType()
1136 || !SkColorSpace::Equals(bm0.colorSpace(), bm1.colorSpace())) {
Chris Craik32054b02014-05-09 13:58:56 -07001137 return JNI_FALSE;
1138 }
1139
Chris Craik32054b02014-05-09 13:58:56 -07001140 // if we can't load the pixels, return false
John Reckf29ed282015-04-07 07:32:03 -07001141 if (NULL == bm0.getPixels() || NULL == bm1.getPixels()) {
Chris Craik32054b02014-05-09 13:58:56 -07001142 return JNI_FALSE;
1143 }
1144
Chris Craik32054b02014-05-09 13:58:56 -07001145 // now compare each scanline. We can't do the entire buffer at once,
1146 // since we don't care about the pixel values that might extend beyond
1147 // the width (since the scanline might be larger than the logical width)
John Reckf29ed282015-04-07 07:32:03 -07001148 const int h = bm0.height();
1149 const size_t size = bm0.width() * bm0.bytesPerPixel();
Chris Craik32054b02014-05-09 13:58:56 -07001150 for (int y = 0; y < h; y++) {
henry.uh_chen53001ca2014-07-03 20:40:22 +08001151 // SkBitmap::getAddr(int, int) may return NULL due to unrecognized config
1152 // (ex: kRLE_Index8_Config). This will cause memcmp method to crash. Since bm0
1153 // and bm1 both have pixel data() (have passed NULL == getPixels() check),
1154 // those 2 bitmaps should be valid (only unrecognized), we return JNI_FALSE
1155 // to warn user those 2 unrecognized config bitmaps may be different.
John Reckf29ed282015-04-07 07:32:03 -07001156 void *bm0Addr = bm0.getAddr(0, y);
1157 void *bm1Addr = bm1.getAddr(0, y);
henry.uh_chen53001ca2014-07-03 20:40:22 +08001158
1159 if(bm0Addr == NULL || bm1Addr == NULL) {
1160 return JNI_FALSE;
1161 }
1162
1163 if (memcmp(bm0Addr, bm1Addr, size) != 0) {
Chris Craik32054b02014-05-09 13:58:56 -07001164 return JNI_FALSE;
1165 }
1166 }
1167 return JNI_TRUE;
1168}
1169
John Reck43871902016-08-01 14:39:24 -07001170static void Bitmap_prepareToDraw(JNIEnv* env, jobject, jlong bitmapPtr) {
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +01001171#ifdef __ANDROID__ // Layoutlib does not support render thread
John Reck43871902016-08-01 14:39:24 -07001172 LocalScopedBitmap bitmapHandle(bitmapPtr);
1173 if (!bitmapHandle.valid()) return;
sergeyvec4a4b12016-10-20 18:39:04 -07001174 android::uirenderer::renderthread::RenderProxy::prepareToDraw(bitmapHandle->bitmap());
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +01001175#endif
John Reck43871902016-08-01 14:39:24 -07001176}
1177
sergeyv45082182016-09-29 18:25:40 -07001178static jint Bitmap_getAllocationByteCount(JNIEnv* env, jobject, jlong bitmapPtr) {
1179 LocalScopedBitmap bitmapHandle(bitmapPtr);
1180 return static_cast<jint>(bitmapHandle->getAllocationByteCount());
1181}
1182
sergeyv6e3658a2017-01-04 16:57:51 -08001183static jobject Bitmap_copyPreserveInternalConfig(JNIEnv* env, jobject, jlong bitmapPtr) {
sergeyv81f97ee2016-12-27 18:08:01 -08001184 LocalScopedBitmap bitmapHandle(bitmapPtr);
1185 LOG_ALWAYS_FATAL_IF(!bitmapHandle->isHardware(),
1186 "Hardware config is only supported config in Bitmap_nativeCopyPreserveInternalConfig");
1187 Bitmap& hwuiBitmap = bitmapHandle->bitmap();
1188 SkBitmap src;
1189 hwuiBitmap.getSkBitmap(&src);
1190
Derek Sollenbergere2169482018-11-20 10:57:20 -05001191 if (src.pixelRef() == nullptr) {
sergeyv81f97ee2016-12-27 18:08:01 -08001192 doThrowRE(env, "Could not copy a hardware bitmap.");
1193 return NULL;
1194 }
Derek Sollenbergere2169482018-11-20 10:57:20 -05001195
1196 sk_sp<Bitmap> bitmap = Bitmap::createFrom(src.info(), *src.pixelRef());
1197 return createBitmap(env, bitmap.release(), getPremulBitmapCreateFlags(false));
sergeyv81f97ee2016-12-27 18:08:01 -08001198}
1199
Leon Scroggins III898ce752020-02-18 12:22:17 -05001200#ifdef __ANDROID__ // Layoutlib does not support graphic buffer
1201typedef AHardwareBuffer* (*AHB_from_HB)(JNIEnv*, jobject);
1202AHB_from_HB AHardwareBuffer_fromHardwareBuffer;
Leon Scroggins III5a190b12020-02-18 13:52:18 -05001203
1204typedef jobject (*AHB_to_HB)(JNIEnv*, AHardwareBuffer*);
1205AHB_to_HB AHardwareBuffer_toHardwareBuffer;
Leon Scroggins III898ce752020-02-18 12:22:17 -05001206#endif
1207
rennb2e9f522018-09-26 10:49:00 -07001208static jobject Bitmap_wrapHardwareBufferBitmap(JNIEnv* env, jobject, jobject hardwareBuffer,
Leon Scroggins III0e443d12018-12-19 11:38:35 -05001209 jlong colorSpacePtr) {
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +01001210#ifdef __ANDROID__ // Layoutlib does not support graphic buffer
Leon Scroggins III898ce752020-02-18 12:22:17 -05001211 AHardwareBuffer* buffer = AHardwareBuffer_fromHardwareBuffer(env, hardwareBuffer);
Derek Sollenbergere78f7c92019-07-31 15:18:47 -04001212 sk_sp<Bitmap> bitmap = Bitmap::createFrom(buffer,
1213 GraphicsJNI::getNativeColorSpace(colorSpacePtr));
rennb2e9f522018-09-26 10:49:00 -07001214 if (!bitmap.get()) {
1215 ALOGW("failed to create hardware bitmap from hardware buffer");
1216 return NULL;
1217 }
1218 return bitmap::createBitmap(env, bitmap.release(), getPremulBitmapCreateFlags(false));
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +01001219#else
1220 return NULL;
1221#endif
rennb2e9f522018-09-26 10:49:00 -07001222}
1223
Leon Scroggins III5a190b12020-02-18 13:52:18 -05001224static jobject Bitmap_getHardwareBuffer(JNIEnv* env, jobject, jlong bitmapPtr) {
1225#ifdef __ANDROID__ // Layoutlib does not support graphic buffer
1226 LocalScopedBitmap bitmapHandle(bitmapPtr);
Leon Scroggins IIIa72a7ff2020-02-28 15:41:56 -05001227 if (!bitmapHandle->isHardware()) {
1228 jniThrowException(env, "java/lang/IllegalStateException",
Leon Scroggins III5a190b12020-02-18 13:52:18 -05001229 "Hardware config is only supported config in Bitmap_getHardwareBuffer");
Leon Scroggins IIIa72a7ff2020-02-28 15:41:56 -05001230 return nullptr;
1231 }
Leon Scroggins III5a190b12020-02-18 13:52:18 -05001232
1233 Bitmap& bitmap = bitmapHandle->bitmap();
1234 return AHardwareBuffer_toHardwareBuffer(env, bitmap.hardwareBuffer());
1235#else
Leon Scroggins IIIa72a7ff2020-02-28 15:41:56 -05001236 return nullptr;
Leon Scroggins III5a190b12020-02-18 13:52:18 -05001237#endif
1238}
1239
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +01001240static jboolean Bitmap_isImmutable(CRITICAL_JNI_PARAMS_COMMA jlong bitmapHandle) {
Nader Jawade7b51292018-04-12 17:55:31 -07001241 LocalScopedBitmap bitmapHolder(bitmapHandle);
1242 if (!bitmapHolder.valid()) return JNI_FALSE;
1243
1244 return bitmapHolder->bitmap().isImmutable() ? JNI_TRUE : JNI_FALSE;
1245}
1246
Leon Scroggins III3eb98432020-03-11 15:38:12 -04001247static jboolean Bitmap_isBackedByAshmem(CRITICAL_JNI_PARAMS_COMMA jlong bitmapHandle) {
1248 LocalScopedBitmap bitmapHolder(bitmapHandle);
1249 if (!bitmapHolder.valid()) return JNI_FALSE;
1250
1251 return bitmapHolder->bitmap().pixelStorageType() == PixelStorageType::Ashmem ? JNI_TRUE
1252 : JNI_FALSE;
1253}
1254
Nader Jawade7b51292018-04-12 17:55:31 -07001255static void Bitmap_setImmutable(JNIEnv* env, jobject, jlong bitmapHandle) {
1256 LocalScopedBitmap bitmapHolder(bitmapHandle);
1257 if (!bitmapHolder.valid()) return;
1258
1259 return bitmapHolder->bitmap().setImmutable();
1260}
1261
Chris Craik32054b02014-05-09 13:58:56 -07001262///////////////////////////////////////////////////////////////////////////////
1263
Daniel Micay76f6a862015-09-19 17:31:01 -04001264static const JNINativeMethod gBitmapMethods[] = {
Leon Scroggins III0e443d12018-12-19 11:38:35 -05001265 { "nativeCreate", "([IIIIIIZJ)Landroid/graphics/Bitmap;",
Chris Craik32054b02014-05-09 13:58:56 -07001266 (void*)Bitmap_creator },
1267 { "nativeCopy", "(JIZ)Landroid/graphics/Bitmap;",
1268 (void*)Bitmap_copy },
Riley Andrews721ae5f2015-05-11 16:08:22 -07001269 { "nativeCopyAshmem", "(J)Landroid/graphics/Bitmap;",
1270 (void*)Bitmap_copyAshmem },
Winsona5fdde92016-04-14 15:27:15 -07001271 { "nativeCopyAshmemConfig", "(JI)Landroid/graphics/Bitmap;",
1272 (void*)Bitmap_copyAshmemConfig },
Richard Uhler775873a2015-12-29 12:37:39 -08001273 { "nativeGetNativeFinalizer", "()J", (void*)Bitmap_getNativeFinalizer },
Leon Scroggins IIIf8adae12018-05-24 15:25:08 -04001274 { "nativeRecycle", "(J)V", (void*)Bitmap_recycle },
sergeyv45082182016-09-29 18:25:40 -07001275 { "nativeReconfigure", "(JIIIZ)V", (void*)Bitmap_reconfigure },
Chris Craik32054b02014-05-09 13:58:56 -07001276 { "nativeCompress", "(JIILjava/io/OutputStream;[B)Z",
1277 (void*)Bitmap_compress },
1278 { "nativeErase", "(JI)V", (void*)Bitmap_erase },
Leon Scroggins III94ba1002019-01-17 13:34:51 -05001279 { "nativeErase", "(JJJ)V", (void*)Bitmap_eraseLong },
Chris Craik32054b02014-05-09 13:58:56 -07001280 { "nativeRowBytes", "(J)I", (void*)Bitmap_rowBytes },
1281 { "nativeConfig", "(J)I", (void*)Bitmap_config },
1282 { "nativeHasAlpha", "(J)Z", (void*)Bitmap_hasAlpha },
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001283 { "nativeIsPremultiplied", "(J)Z", (void*)Bitmap_isPremultiplied},
1284 { "nativeSetHasAlpha", "(JZZ)V", (void*)Bitmap_setHasAlpha},
1285 { "nativeSetPremultiplied", "(JZ)V", (void*)Bitmap_setPremultiplied},
Chris Craik32054b02014-05-09 13:58:56 -07001286 { "nativeHasMipMap", "(J)Z", (void*)Bitmap_hasMipMap },
1287 { "nativeSetHasMipMap", "(JZ)V", (void*)Bitmap_setHasMipMap },
1288 { "nativeCreateFromParcel",
1289 "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;",
1290 (void*)Bitmap_createFromParcel },
John Reckefac0522020-01-24 16:04:26 -08001291 { "nativeWriteToParcel", "(JILandroid/os/Parcel;)Z",
Chris Craik32054b02014-05-09 13:58:56 -07001292 (void*)Bitmap_writeToParcel },
1293 { "nativeExtractAlpha", "(JJ[I)Landroid/graphics/Bitmap;",
1294 (void*)Bitmap_extractAlpha },
1295 { "nativeGenerationId", "(J)I", (void*)Bitmap_getGenerationId },
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001296 { "nativeGetPixel", "(JII)I", (void*)Bitmap_getPixel },
Leon Scroggins III870053d2019-01-24 08:37:27 -05001297 { "nativeGetColor", "(JII)J", (void*)Bitmap_getColor },
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001298 { "nativeGetPixels", "(J[IIIIIII)V", (void*)Bitmap_getPixels },
1299 { "nativeSetPixel", "(JIII)V", (void*)Bitmap_setPixel },
1300 { "nativeSetPixels", "(J[IIIIIII)V", (void*)Bitmap_setPixels },
Chris Craik32054b02014-05-09 13:58:56 -07001301 { "nativeCopyPixelsToBuffer", "(JLjava/nio/Buffer;)V",
1302 (void*)Bitmap_copyPixelsToBuffer },
1303 { "nativeCopyPixelsFromBuffer", "(JLjava/nio/Buffer;)V",
1304 (void*)Bitmap_copyPixelsFromBuffer },
1305 { "nativeSameAs", "(JJ)Z", (void*)Bitmap_sameAs },
John Reck43871902016-08-01 14:39:24 -07001306 { "nativePrepareToDraw", "(J)V", (void*)Bitmap_prepareToDraw },
sergeyv45082182016-09-29 18:25:40 -07001307 { "nativeGetAllocationByteCount", "(J)I", (void*)Bitmap_getAllocationByteCount },
sergeyv81f97ee2016-12-27 18:08:01 -08001308 { "nativeCopyPreserveInternalConfig", "(J)Landroid/graphics/Bitmap;",
sergeyv6e3658a2017-01-04 16:57:51 -08001309 (void*)Bitmap_copyPreserveInternalConfig },
Leon Scroggins III0e443d12018-12-19 11:38:35 -05001310 { "nativeWrapHardwareBufferBitmap", "(Landroid/hardware/HardwareBuffer;J)Landroid/graphics/Bitmap;",
rennb2e9f522018-09-26 10:49:00 -07001311 (void*) Bitmap_wrapHardwareBufferBitmap },
Leon Scroggins III5a190b12020-02-18 13:52:18 -05001312 { "nativeGetHardwareBuffer", "(J)Landroid/hardware/HardwareBuffer;",
1313 (void*) Bitmap_getHardwareBuffer },
Derek Sollenbergerbf3e4642019-01-30 11:28:27 -05001314 { "nativeComputeColorSpace", "(J)Landroid/graphics/ColorSpace;", (void*)Bitmap_computeColorSpace },
Derek Sollenberger202084c2019-01-14 13:55:08 -05001315 { "nativeSetColorSpace", "(JJ)V", (void*)Bitmap_setColorSpace },
Romain Guyefb4b062017-02-27 11:00:04 -08001316 { "nativeIsSRGB", "(J)Z", (void*)Bitmap_isSRGB },
Leon Scroggins IIIce89a6e2018-03-13 15:39:39 -04001317 { "nativeIsSRGBLinear", "(J)Z", (void*)Bitmap_isSRGBLinear},
Nader Jawade7b51292018-04-12 17:55:31 -07001318 { "nativeSetImmutable", "(J)V", (void*)Bitmap_setImmutable},
1319
1320 // ------------ @CriticalNative ----------------
Leon Scroggins III3eb98432020-03-11 15:38:12 -04001321 { "nativeIsImmutable", "(J)Z", (void*)Bitmap_isImmutable},
1322 { "nativeIsBackedByAshmem", "(J)Z", (void*)Bitmap_isBackedByAshmem}
Nader Jawade7b51292018-04-12 17:55:31 -07001323
Chris Craik32054b02014-05-09 13:58:56 -07001324};
1325
Chris Craik32054b02014-05-09 13:58:56 -07001326int register_android_graphics_Bitmap(JNIEnv* env)
1327{
Romain Guy95648b82017-04-13 18:43:42 -07001328 gBitmap_class = MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/graphics/Bitmap"));
1329 gBitmap_nativePtr = GetFieldIDOrDie(env, gBitmap_class, "mNativePtr", "J");
Leon Scroggins IIIbbdb7312019-01-31 14:35:54 -05001330 gBitmap_constructorMethodID = GetMethodIDOrDie(env, gBitmap_class, "<init>", "(JIIIZ[BLandroid/graphics/NinePatch$InsetStruct;Z)V");
Romain Guy95648b82017-04-13 18:43:42 -07001331 gBitmap_reinitMethodID = GetMethodIDOrDie(env, gBitmap_class, "reinit", "(IIZ)V");
Leon Scroggins III898ce752020-02-18 12:22:17 -05001332
Derek Sollenberger42c50042020-02-18 14:51:17 -05001333#ifdef __ANDROID__ // Layoutlib does not support graphic buffer or parcel
Leon Scroggins III898ce752020-02-18 12:22:17 -05001334 void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE);
1335 AHardwareBuffer_fromHardwareBuffer =
1336 (AHB_from_HB)dlsym(handle_, "AHardwareBuffer_fromHardwareBuffer");
1337 LOG_ALWAYS_FATAL_IF(AHardwareBuffer_fromHardwareBuffer == nullptr,
1338 "Failed to find required symbol AHardwareBuffer_fromHardwareBuffer!");
Leon Scroggins III5a190b12020-02-18 13:52:18 -05001339
1340 AHardwareBuffer_toHardwareBuffer = (AHB_to_HB)dlsym(handle_, "AHardwareBuffer_toHardwareBuffer");
1341 LOG_ALWAYS_FATAL_IF(AHardwareBuffer_toHardwareBuffer == nullptr,
1342 " Failed to find required symbol AHardwareBuffer_toHardwareBuffer!");
Leon Scroggins III898ce752020-02-18 12:22:17 -05001343#endif
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001344 return android::RegisterMethodsOrDie(env, "android/graphics/Bitmap", gBitmapMethods,
1345 NELEM(gBitmapMethods));
John Reck9192d5e2016-10-31 10:32:09 -07001346}