blob: 0d80a7f9fbe2cf681a52989b85c2441bcea133f4 [file] [log] [blame]
John Reckf29ed282015-04-07 07:32:03 -07001#define LOG_TAG "Bitmap"
John Reckf29ed282015-04-07 07:32:03 -07002#include "Bitmap.h"
3
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -04004#include "Paint.h"
Chris Craik32054b02014-05-09 13:58:56 -07005#include "SkBitmap.h"
6#include "SkPixelRef.h"
7#include "SkImageEncoder.h"
Leon Scroggins III57ee6202014-06-04 18:51:07 -04008#include "SkImageInfo.h"
Chris Craik32054b02014-05-09 13:58:56 -07009#include "SkColorPriv.h"
10#include "GraphicsJNI.h"
11#include "SkDither.h"
12#include "SkUnPreMultiply.h"
13#include "SkStream.h"
14
15#include <binder/Parcel.h>
16#include "android_os_Parcel.h"
17#include "android_util_Binder.h"
18#include "android_nio_utils.h"
19#include "CreateJavaOutputStreamAdaptor.h"
John Reckf29ed282015-04-07 07:32:03 -070020#include <Caches.h>
Chris Craik32054b02014-05-09 13:58:56 -070021
Andreas Gampeed6b9df2014-11-20 22:02:20 -080022#include "core_jni_helpers.h"
23
Chris Craik32054b02014-05-09 13:58:56 -070024#include <jni.h>
Riley Andrews39d7f302014-11-13 17:43:25 -080025#include <memory>
26#include <string>
27#include <sys/mman.h>
28#include <cutils/ashmem.h>
Chris Craik32054b02014-05-09 13:58:56 -070029
Jeff Browna316c5d2015-06-05 15:14:06 -070030#define DEBUG_PARCEL 0
31
John Reckf29ed282015-04-07 07:32:03 -070032namespace android {
33
34class WrappedPixelRef : public SkPixelRef {
35public:
36 WrappedPixelRef(Bitmap* wrapper, void* storage,
37 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
38 : SkPixelRef(info)
39 , mBitmap(*wrapper)
40 , mStorage(storage) {
41 reconfigure(info, rowBytes, ctable);
42 }
43
44 ~WrappedPixelRef() {
45 // Tell SkRefCnt that everything is as it expects by forcing
46 // the refcnt to 1
47 internal_dispose_restore_refcnt_to_1();
48 SkSafeUnref(mColorTable);
49 }
50
John Reck0781a2f2015-05-27 16:29:17 -070051 void reconfigure(const SkImageInfo& newInfo, size_t rowBytes, SkColorTable* ctable) {
52 if (kIndex_8_SkColorType != newInfo.colorType()) {
John Reckf29ed282015-04-07 07:32:03 -070053 ctable = nullptr;
54 }
55 mRowBytes = rowBytes;
56 if (mColorTable != ctable) {
57 SkSafeUnref(mColorTable);
58 mColorTable = ctable;
59 SkSafeRef(mColorTable);
60 }
John Reck0781a2f2015-05-27 16:29:17 -070061
62 // Need to validate the alpha type to filter against the color type
63 // to prevent things like a non-opaque RGB565 bitmap
64 SkAlphaType alphaType;
65 LOG_ALWAYS_FATAL_IF(!SkColorTypeValidateAlphaType(
66 newInfo.colorType(), newInfo.alphaType(), &alphaType),
67 "Failed to validate alpha type!");
68
John Reckf29ed282015-04-07 07:32:03 -070069 // Dirty hack is dirty
70 // TODO: Figure something out here, Skia's current design makes this
71 // really hard to work with. Skia really, really wants immutable objects,
72 // but with the nested-ref-count hackery going on that's just not
73 // feasible without going insane trying to figure it out
74 SkImageInfo* myInfo = const_cast<SkImageInfo*>(&this->info());
John Reck0781a2f2015-05-27 16:29:17 -070075 *myInfo = newInfo;
76 changeAlphaType(alphaType);
John Reckf29ed282015-04-07 07:32:03 -070077
78 // Docs say to only call this in the ctor, but we're going to call
79 // it anyway even if this isn't always the ctor.
80 // TODO: Fix this too as part of the above TODO
81 setPreLocked(mStorage, mRowBytes, mColorTable);
82 }
83
84 // Can't mark as override since SkPixelRef::rowBytes isn't virtual
85 // but that's OK since we just want BitmapWrapper to be able to rely
86 // on calling rowBytes() on an unlocked pixelref, which it will be
87 // doing on a WrappedPixelRef type, not a SkPixelRef, so static
88 // dispatching will do what we want.
89 size_t rowBytes() const { return mRowBytes; }
90 SkColorTable* colorTable() const { return mColorTable; }
91
92 bool hasHardwareMipMap() const {
93 return mHasHardwareMipMap;
94 }
95
96 void setHasHardwareMipMap(bool hasMipMap) {
97 mHasHardwareMipMap = hasMipMap;
98 }
99
100protected:
101 virtual bool onNewLockPixels(LockRec* rec) override {
102 rec->fPixels = mStorage;
103 rec->fRowBytes = mRowBytes;
104 rec->fColorTable = mColorTable;
105 return true;
106 }
107
108 virtual void onUnlockPixels() override {
109 // nothing
110 }
111
112 virtual size_t getAllocatedSizeInBytes() const override {
113 return info().getSafeSize(mRowBytes);
114 }
115
116private:
117 Bitmap& mBitmap;
118 void* mStorage;
119 size_t mRowBytes = 0;
120 SkColorTable* mColorTable = nullptr;
121 bool mHasHardwareMipMap = false;
122
123 virtual void internal_dispose() const override {
124 mBitmap.onStrongRefDestroyed();
125 }
126};
127
128Bitmap::Bitmap(JNIEnv* env, jbyteArray storageObj, void* address,
129 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
130 : mPixelStorageType(PixelStorageType::Java) {
131 env->GetJavaVM(&mPixelStorage.java.jvm);
132 mPixelStorage.java.jweakRef = env->NewWeakGlobalRef(storageObj);
133 mPixelStorage.java.jstrongRef = nullptr;
134 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
135 // Note: this will trigger a call to onStrongRefDestroyed(), but
136 // we want the pixel ref to have a ref count of 0 at this point
137 mPixelRef->unref();
138}
139
140Bitmap::Bitmap(void* address, void* context, FreeFunc freeFunc,
141 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
142 : mPixelStorageType(PixelStorageType::External) {
143 mPixelStorage.external.address = address;
144 mPixelStorage.external.context = context;
145 mPixelStorage.external.freeFunc = freeFunc;
146 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
147 // Note: this will trigger a call to onStrongRefDestroyed(), but
148 // we want the pixel ref to have a ref count of 0 at this point
149 mPixelRef->unref();
150}
151
Riley Andrews39d7f302014-11-13 17:43:25 -0800152Bitmap::Bitmap(void* address, int fd,
153 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
154 : mPixelStorageType(PixelStorageType::Ashmem) {
155 mPixelStorage.ashmem.address = address;
156 mPixelStorage.ashmem.fd = fd;
157 mPixelStorage.ashmem.size = ashmem_get_size_region(fd);
158 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
159 // Note: this will trigger a call to onStrongRefDestroyed(), but
160 // we want the pixel ref to have a ref count of 0 at this point
161 mPixelRef->unref();
162}
John Reckf29ed282015-04-07 07:32:03 -0700163Bitmap::~Bitmap() {
164 doFreePixels();
165}
166
167void Bitmap::freePixels() {
168 AutoMutex _lock(mLock);
169 if (mPinnedRefCount == 0) {
170 doFreePixels();
171 mPixelStorageType = PixelStorageType::Invalid;
172 }
173}
174
175void Bitmap::doFreePixels() {
176 switch (mPixelStorageType) {
177 case PixelStorageType::Invalid:
178 // already free'd, nothing to do
179 break;
180 case PixelStorageType::External:
181 mPixelStorage.external.freeFunc(mPixelStorage.external.address,
182 mPixelStorage.external.context);
183 break;
Riley Andrews39d7f302014-11-13 17:43:25 -0800184 case PixelStorageType::Ashmem:
185 munmap(mPixelStorage.ashmem.address, mPixelStorage.ashmem.size);
186 close(mPixelStorage.ashmem.fd);
187 break;
John Reckf29ed282015-04-07 07:32:03 -0700188 case PixelStorageType::Java:
189 JNIEnv* env = jniEnv();
190 LOG_ALWAYS_FATAL_IF(mPixelStorage.java.jstrongRef,
191 "Deleting a bitmap wrapper while there are outstanding strong "
192 "references! mPinnedRefCount = %d", mPinnedRefCount);
193 env->DeleteWeakGlobalRef(mPixelStorage.java.jweakRef);
194 break;
195 }
196
197 if (android::uirenderer::Caches::hasInstance()) {
198 android::uirenderer::Caches::getInstance().textureCache.releaseTexture(
199 mPixelRef->getStableID());
200 }
201}
202
203bool Bitmap::hasHardwareMipMap() {
204 return mPixelRef->hasHardwareMipMap();
205}
206
207void Bitmap::setHasHardwareMipMap(bool hasMipMap) {
208 mPixelRef->setHasHardwareMipMap(hasMipMap);
209}
210
Riley Andrews39d7f302014-11-13 17:43:25 -0800211int Bitmap::getAshmemFd() const {
212 switch (mPixelStorageType) {
213 case PixelStorageType::Ashmem:
214 return mPixelStorage.ashmem.fd;
215 default:
216 return -1;
217 }
218}
219
John Reckf29ed282015-04-07 07:32:03 -0700220const SkImageInfo& Bitmap::info() const {
221 assertValid();
222 return mPixelRef->info();
223}
224
225size_t Bitmap::rowBytes() const {
226 return mPixelRef->rowBytes();
227}
228
John Reckae2e8b42015-05-06 14:55:05 -0700229SkPixelRef* Bitmap::peekAtPixelRef() const {
John Reckf29ed282015-04-07 07:32:03 -0700230 assertValid();
231 return mPixelRef.get();
232}
233
John Reckae2e8b42015-05-06 14:55:05 -0700234SkPixelRef* Bitmap::refPixelRef() {
235 assertValid();
236 android::AutoMutex _lock(mLock);
237 return refPixelRefLocked();
238}
239
240SkPixelRef* Bitmap::refPixelRefLocked() {
241 mPixelRef->ref();
242 if (mPixelRef->unique()) {
243 // We just restored this from 0, pin the pixels and inc the strong count
244 // Note that there *might be* an incoming onStrongRefDestroyed from whatever
245 // last unref'd
246 pinPixelsLocked();
247 mPinnedRefCount++;
248 }
249 return mPixelRef.get();
250}
251
John Reckf29ed282015-04-07 07:32:03 -0700252void Bitmap::reconfigure(const SkImageInfo& info, size_t rowBytes,
253 SkColorTable* ctable) {
John Reckae2e8b42015-05-06 14:55:05 -0700254 {
255 android::AutoMutex _lock(mLock);
256 if (mPinnedRefCount) {
257 ALOGW("Called reconfigure on a bitmap that is in use! This may"
258 " cause graphical corruption!");
259 }
260 }
John Reckf29ed282015-04-07 07:32:03 -0700261 mPixelRef->reconfigure(info, rowBytes, ctable);
262}
263
264void Bitmap::reconfigure(const SkImageInfo& info) {
Derek Sollenberger2a94a102015-05-07 13:56:14 -0400265 reconfigure(info, info.minRowBytes(), nullptr);
John Reckf29ed282015-04-07 07:32:03 -0700266}
267
John Reck0781a2f2015-05-27 16:29:17 -0700268void Bitmap::setAlphaType(SkAlphaType alphaType) {
269 if (!SkColorTypeValidateAlphaType(info().colorType(), alphaType, &alphaType)) {
270 return;
271 }
272
273 mPixelRef->changeAlphaType(alphaType);
274}
275
John Reckf29ed282015-04-07 07:32:03 -0700276void Bitmap::detachFromJava() {
277 bool disposeSelf;
278 {
279 android::AutoMutex _lock(mLock);
280 mAttachedToJava = false;
281 disposeSelf = shouldDisposeSelfLocked();
282 }
283 if (disposeSelf) {
284 delete this;
285 }
286}
287
288bool Bitmap::shouldDisposeSelfLocked() {
289 return mPinnedRefCount == 0 && !mAttachedToJava;
290}
291
292JNIEnv* Bitmap::jniEnv() {
293 JNIEnv* env;
294 auto success = mPixelStorage.java.jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
295 LOG_ALWAYS_FATAL_IF(success != JNI_OK,
296 "Failed to get JNIEnv* from JVM: %p", mPixelStorage.java.jvm);
297 return env;
298}
299
300void Bitmap::onStrongRefDestroyed() {
301 bool disposeSelf = false;
302 {
303 android::AutoMutex _lock(mLock);
304 if (mPinnedRefCount > 0) {
305 mPinnedRefCount--;
306 if (mPinnedRefCount == 0) {
307 unpinPixelsLocked();
308 disposeSelf = shouldDisposeSelfLocked();
309 }
310 }
311 }
312 if (disposeSelf) {
313 delete this;
314 }
315}
316
317void Bitmap::pinPixelsLocked() {
318 switch (mPixelStorageType) {
319 case PixelStorageType::Invalid:
320 LOG_ALWAYS_FATAL("Cannot pin invalid pixels!");
321 break;
322 case PixelStorageType::External:
Riley Andrews39d7f302014-11-13 17:43:25 -0800323 case PixelStorageType::Ashmem:
John Reckf29ed282015-04-07 07:32:03 -0700324 // Nothing to do
325 break;
326 case PixelStorageType::Java: {
327 JNIEnv* env = jniEnv();
328 if (!mPixelStorage.java.jstrongRef) {
329 mPixelStorage.java.jstrongRef = reinterpret_cast<jbyteArray>(
330 env->NewGlobalRef(mPixelStorage.java.jweakRef));
331 if (!mPixelStorage.java.jstrongRef) {
332 LOG_ALWAYS_FATAL("Failed to acquire strong reference to pixels");
333 }
334 }
335 break;
336 }
337 }
338}
339
340void Bitmap::unpinPixelsLocked() {
341 switch (mPixelStorageType) {
342 case PixelStorageType::Invalid:
343 LOG_ALWAYS_FATAL("Cannot unpin invalid pixels!");
344 break;
345 case PixelStorageType::External:
Riley Andrews39d7f302014-11-13 17:43:25 -0800346 case PixelStorageType::Ashmem:
John Reckf29ed282015-04-07 07:32:03 -0700347 // Don't need to do anything
348 break;
349 case PixelStorageType::Java: {
350 JNIEnv* env = jniEnv();
351 if (mPixelStorage.java.jstrongRef) {
352 env->DeleteGlobalRef(mPixelStorage.java.jstrongRef);
353 mPixelStorage.java.jstrongRef = nullptr;
354 }
355 break;
356 }
357 }
358}
359
360void Bitmap::getSkBitmap(SkBitmap* outBitmap) {
361 assertValid();
362 android::AutoMutex _lock(mLock);
John Reckf29ed282015-04-07 07:32:03 -0700363 // Safe because mPixelRef is a WrappedPixelRef type, otherwise rowBytes()
364 // would require locking the pixels first.
365 outBitmap->setInfo(mPixelRef->info(), mPixelRef->rowBytes());
John Reckae2e8b42015-05-06 14:55:05 -0700366 outBitmap->setPixelRef(refPixelRefLocked())->unref();
John Reckf29ed282015-04-07 07:32:03 -0700367 outBitmap->setHasHardwareMipMap(hasHardwareMipMap());
368}
369
370void Bitmap::assertValid() const {
371 LOG_ALWAYS_FATAL_IF(mPixelStorageType == PixelStorageType::Invalid,
372 "Error, cannot access an invalid/free'd bitmap here!");
373}
374
375} // namespace android
376
377using namespace android;
378
379// Convenience class that does not take a global ref on the pixels, relying
380// on the caller already having a local JNI ref
381class LocalScopedBitmap {
382public:
383 LocalScopedBitmap(jlong bitmapHandle)
384 : mBitmap(reinterpret_cast<Bitmap*>(bitmapHandle)) {}
385
386 Bitmap* operator->() {
387 return mBitmap;
388 }
389
390 void* pixels() {
John Reckae2e8b42015-05-06 14:55:05 -0700391 return mBitmap->peekAtPixelRef()->pixels();
John Reckf29ed282015-04-07 07:32:03 -0700392 }
393
394 bool valid() {
395 return mBitmap && mBitmap->valid();
396 }
397
398private:
399 Bitmap* mBitmap;
400};
401
Chris Craik32054b02014-05-09 13:58:56 -0700402///////////////////////////////////////////////////////////////////////////////
403// Conversions to/from SkColor, for get/setPixels, and the create method, which
404// is basically like setPixels
405
406typedef void (*FromColorProc)(void* dst, const SkColor src[], int width,
407 int x, int y);
408
409static void FromColor_D32(void* dst, const SkColor src[], int width,
410 int, int) {
411 SkPMColor* d = (SkPMColor*)dst;
412
413 for (int i = 0; i < width; i++) {
414 *d++ = SkPreMultiplyColor(*src++);
415 }
416}
417
418static void FromColor_D32_Raw(void* dst, const SkColor src[], int width,
419 int, int) {
Dan Albert46d84442014-11-18 16:07:51 -0800420 // Needed to thwart the unreachable code detection from clang.
421 static const bool sk_color_ne_zero = SK_COLOR_MATCHES_PMCOLOR_BYTE_ORDER;
422
Chris Craik32054b02014-05-09 13:58:56 -0700423 // SkColor's ordering may be different from SkPMColor
Dan Albert46d84442014-11-18 16:07:51 -0800424 if (sk_color_ne_zero) {
Chris Craik32054b02014-05-09 13:58:56 -0700425 memcpy(dst, src, width * sizeof(SkColor));
426 return;
427 }
428
429 // order isn't same, repack each pixel manually
430 SkPMColor* d = (SkPMColor*)dst;
431 for (int i = 0; i < width; i++) {
432 SkColor c = *src++;
433 *d++ = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c),
434 SkColorGetG(c), SkColorGetB(c));
435 }
436}
437
438static void FromColor_D565(void* dst, const SkColor src[], int width,
439 int x, int y) {
440 uint16_t* d = (uint16_t*)dst;
441
442 DITHER_565_SCAN(y);
443 for (int stop = x + width; x < stop; x++) {
444 SkColor c = *src++;
445 *d++ = SkDitherRGBTo565(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c),
446 DITHER_VALUE(x));
447 }
448}
449
450static void FromColor_D4444(void* dst, const SkColor src[], int width,
451 int x, int y) {
452 SkPMColor16* d = (SkPMColor16*)dst;
453
454 DITHER_4444_SCAN(y);
455 for (int stop = x + width; x < stop; x++) {
456 SkPMColor pmc = SkPreMultiplyColor(*src++);
457 *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x));
458// *d++ = SkPixel32ToPixel4444(pmc);
459 }
460}
461
462static void FromColor_D4444_Raw(void* dst, const SkColor src[], int width,
463 int x, int y) {
464 SkPMColor16* d = (SkPMColor16*)dst;
465
466 DITHER_4444_SCAN(y);
467 for (int stop = x + width; x < stop; x++) {
468 SkColor c = *src++;
469
470 // SkPMColor is used because the ordering is ARGB32, even though the target actually premultiplied
471 SkPMColor pmc = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c),
472 SkColorGetG(c), SkColorGetB(c));
473 *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x));
474// *d++ = SkPixel32ToPixel4444(pmc);
475 }
476}
477
478// can return NULL
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400479static FromColorProc ChooseFromColorProc(const SkBitmap& bitmap) {
480 switch (bitmap.colorType()) {
481 case kN32_SkColorType:
482 return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D32 : FromColor_D32_Raw;
483 case kARGB_4444_SkColorType:
484 return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D4444 :
485 FromColor_D4444_Raw;
486 case kRGB_565_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700487 return FromColor_D565;
488 default:
489 break;
490 }
491 return NULL;
492}
493
494bool GraphicsJNI::SetPixels(JNIEnv* env, jintArray srcColors, int srcOffset, int srcStride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400495 int x, int y, int width, int height, const SkBitmap& dstBitmap) {
Chris Craik32054b02014-05-09 13:58:56 -0700496 SkAutoLockPixels alp(dstBitmap);
497 void* dst = dstBitmap.getPixels();
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400498 FromColorProc proc = ChooseFromColorProc(dstBitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700499
500 if (NULL == dst || NULL == proc) {
501 return false;
502 }
503
504 const jint* array = env->GetIntArrayElements(srcColors, NULL);
505 const SkColor* src = (const SkColor*)array + srcOffset;
506
507 // reset to to actual choice from caller
508 dst = dstBitmap.getAddr(x, y);
509 // now copy/convert each scanline
510 for (int y = 0; y < height; y++) {
511 proc(dst, src, width, x, y);
512 src += srcStride;
513 dst = (char*)dst + dstBitmap.rowBytes();
514 }
515
516 dstBitmap.notifyPixelsChanged();
517
518 env->ReleaseIntArrayElements(srcColors, const_cast<jint*>(array),
519 JNI_ABORT);
520 return true;
521}
522
523//////////////////// ToColor procs
524
525typedef void (*ToColorProc)(SkColor dst[], const void* src, int width,
526 SkColorTable*);
527
528static void ToColor_S32_Alpha(SkColor dst[], const void* src, int width,
529 SkColorTable*) {
530 SkASSERT(width > 0);
531 const SkPMColor* s = (const SkPMColor*)src;
532 do {
533 *dst++ = SkUnPreMultiply::PMColorToColor(*s++);
534 } while (--width != 0);
535}
536
537static void ToColor_S32_Raw(SkColor dst[], const void* src, int width,
538 SkColorTable*) {
539 SkASSERT(width > 0);
540 const SkPMColor* s = (const SkPMColor*)src;
541 do {
542 SkPMColor c = *s++;
543 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
544 SkGetPackedG32(c), SkGetPackedB32(c));
545 } while (--width != 0);
546}
547
548static void ToColor_S32_Opaque(SkColor dst[], const void* src, int width,
549 SkColorTable*) {
550 SkASSERT(width > 0);
551 const SkPMColor* s = (const SkPMColor*)src;
552 do {
553 SkPMColor c = *s++;
554 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
555 SkGetPackedB32(c));
556 } while (--width != 0);
557}
558
559static void ToColor_S4444_Alpha(SkColor dst[], const void* src, int width,
560 SkColorTable*) {
561 SkASSERT(width > 0);
562 const SkPMColor16* s = (const SkPMColor16*)src;
563 do {
564 *dst++ = SkUnPreMultiply::PMColorToColor(SkPixel4444ToPixel32(*s++));
565 } while (--width != 0);
566}
567
568static void ToColor_S4444_Raw(SkColor dst[], const void* src, int width,
569 SkColorTable*) {
570 SkASSERT(width > 0);
571 const SkPMColor16* s = (const SkPMColor16*)src;
572 do {
573 SkPMColor c = SkPixel4444ToPixel32(*s++);
574 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
575 SkGetPackedG32(c), SkGetPackedB32(c));
576 } while (--width != 0);
577}
578
579static void ToColor_S4444_Opaque(SkColor dst[], const void* src, int width,
580 SkColorTable*) {
581 SkASSERT(width > 0);
582 const SkPMColor16* s = (const SkPMColor16*)src;
583 do {
584 SkPMColor c = SkPixel4444ToPixel32(*s++);
585 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
586 SkGetPackedB32(c));
587 } while (--width != 0);
588}
589
590static void ToColor_S565(SkColor dst[], const void* src, int width,
591 SkColorTable*) {
592 SkASSERT(width > 0);
593 const uint16_t* s = (const uint16_t*)src;
594 do {
595 uint16_t c = *s++;
596 *dst++ = SkColorSetRGB(SkPacked16ToR32(c), SkPacked16ToG32(c),
597 SkPacked16ToB32(c));
598 } while (--width != 0);
599}
600
601static void ToColor_SI8_Alpha(SkColor dst[], const void* src, int width,
602 SkColorTable* ctable) {
603 SkASSERT(width > 0);
604 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500605 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700606 do {
607 *dst++ = SkUnPreMultiply::PMColorToColor(colors[*s++]);
608 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700609}
610
611static void ToColor_SI8_Raw(SkColor dst[], const void* src, int width,
612 SkColorTable* ctable) {
613 SkASSERT(width > 0);
614 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500615 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700616 do {
617 SkPMColor c = colors[*s++];
618 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
619 SkGetPackedG32(c), SkGetPackedB32(c));
620 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700621}
622
623static void ToColor_SI8_Opaque(SkColor dst[], const void* src, int width,
624 SkColorTable* ctable) {
625 SkASSERT(width > 0);
626 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500627 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700628 do {
629 SkPMColor c = colors[*s++];
630 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
631 SkGetPackedB32(c));
632 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700633}
634
635// can return NULL
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400636static ToColorProc ChooseToColorProc(const SkBitmap& src) {
Mike Reedb9330552014-06-16 17:31:48 -0400637 switch (src.colorType()) {
638 case kN32_SkColorType:
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400639 switch (src.alphaType()) {
640 case kOpaque_SkAlphaType:
641 return ToColor_S32_Opaque;
642 case kPremul_SkAlphaType:
643 return ToColor_S32_Alpha;
644 case kUnpremul_SkAlphaType:
645 return ToColor_S32_Raw;
646 default:
647 return NULL;
648 }
Mike Reedb9330552014-06-16 17:31:48 -0400649 case kARGB_4444_SkColorType:
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400650 switch (src.alphaType()) {
651 case kOpaque_SkAlphaType:
652 return ToColor_S4444_Opaque;
653 case kPremul_SkAlphaType:
654 return ToColor_S4444_Alpha;
655 case kUnpremul_SkAlphaType:
656 return ToColor_S4444_Raw;
657 default:
658 return NULL;
659 }
Mike Reedb9330552014-06-16 17:31:48 -0400660 case kRGB_565_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700661 return ToColor_S565;
Mike Reedb9330552014-06-16 17:31:48 -0400662 case kIndex_8_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700663 if (src.getColorTable() == NULL) {
664 return NULL;
665 }
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400666 switch (src.alphaType()) {
667 case kOpaque_SkAlphaType:
668 return ToColor_SI8_Opaque;
669 case kPremul_SkAlphaType:
670 return ToColor_SI8_Alpha;
671 case kUnpremul_SkAlphaType:
672 return ToColor_SI8_Raw;
673 default:
674 return NULL;
675 }
Chris Craik32054b02014-05-09 13:58:56 -0700676 default:
677 break;
678 }
679 return NULL;
680}
681
682///////////////////////////////////////////////////////////////////////////////
683///////////////////////////////////////////////////////////////////////////////
684
685static int getPremulBitmapCreateFlags(bool isMutable) {
686 int flags = GraphicsJNI::kBitmapCreateFlag_Premultiplied;
687 if (isMutable) flags |= GraphicsJNI::kBitmapCreateFlag_Mutable;
688 return flags;
689}
690
691static jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors,
692 jint offset, jint stride, jint width, jint height,
693 jint configHandle, jboolean isMutable) {
Mike Reed1103b322014-07-08 12:36:44 -0400694 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700695 if (NULL != jColors) {
696 size_t n = env->GetArrayLength(jColors);
697 if (n < SkAbs32(stride) * (size_t)height) {
698 doThrowAIOOBE(env);
699 return NULL;
700 }
701 }
702
703 // ARGB_4444 is a deprecated format, convert automatically to 8888
Mike Reedb9330552014-06-16 17:31:48 -0400704 if (colorType == kARGB_4444_SkColorType) {
705 colorType = kN32_SkColorType;
Chris Craik32054b02014-05-09 13:58:56 -0700706 }
707
708 SkBitmap bitmap;
Mike Reedb9330552014-06-16 17:31:48 -0400709 bitmap.setInfo(SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType));
Chris Craik32054b02014-05-09 13:58:56 -0700710
John Reckf29ed282015-04-07 07:32:03 -0700711 Bitmap* nativeBitmap = GraphicsJNI::allocateJavaPixelRef(env, &bitmap, NULL);
712 if (!nativeBitmap) {
Chris Craik32054b02014-05-09 13:58:56 -0700713 return NULL;
714 }
715
716 if (jColors != NULL) {
717 GraphicsJNI::SetPixels(env, jColors, offset, stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400718 0, 0, width, height, bitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700719 }
720
John Reckf29ed282015-04-07 07:32:03 -0700721 return GraphicsJNI::createBitmap(env, nativeBitmap,
722 getPremulBitmapCreateFlags(isMutable));
Chris Craik32054b02014-05-09 13:58:56 -0700723}
724
725static jobject Bitmap_copy(JNIEnv* env, jobject, jlong srcHandle,
726 jint dstConfigHandle, jboolean isMutable) {
John Reckf29ed282015-04-07 07:32:03 -0700727 SkBitmap src;
728 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
Mike Reed1103b322014-07-08 12:36:44 -0400729 SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700730 SkBitmap result;
731 JavaPixelAllocator allocator(env);
732
John Reckf29ed282015-04-07 07:32:03 -0700733 if (!src.copyTo(&result, dstCT, &allocator)) {
Chris Craik32054b02014-05-09 13:58:56 -0700734 return NULL;
735 }
John Reckf29ed282015-04-07 07:32:03 -0700736 Bitmap* bitmap = allocator.getStorageObjAndReset();
737 return GraphicsJNI::createBitmap(env, bitmap,
738 getPremulBitmapCreateFlags(isMutable));
Chris Craik32054b02014-05-09 13:58:56 -0700739}
740
Riley Andrews721ae5f2015-05-11 16:08:22 -0700741static jobject Bitmap_copyAshmem(JNIEnv* env, jobject, jlong srcHandle) {
742 SkBitmap src;
743 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
744 SkBitmap result;
745
746 AshmemPixelAllocator allocator(env);
747 if (!src.copyTo(&result, &allocator)) {
748 return NULL;
749 }
750 Bitmap* bitmap = allocator.getStorageObjAndReset();
751 bitmap->peekAtPixelRef()->setImmutable();
752 jobject ret = GraphicsJNI::createBitmap(env, bitmap, getPremulBitmapCreateFlags(false));
753 return ret;
754}
755
Chris Craik32054b02014-05-09 13:58:56 -0700756static void Bitmap_destructor(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700757 LocalScopedBitmap bitmap(bitmapHandle);
758 bitmap->detachFromJava();
Chris Craik32054b02014-05-09 13:58:56 -0700759}
760
761static jboolean Bitmap_recycle(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700762 LocalScopedBitmap bitmap(bitmapHandle);
763 bitmap->freePixels();
Chris Craik32054b02014-05-09 13:58:56 -0700764 return JNI_TRUE;
765}
766
767static void Bitmap_reconfigure(JNIEnv* env, jobject clazz, jlong bitmapHandle,
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400768 jint width, jint height, jint configHandle, jint allocSize,
769 jboolean requestPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700770 LocalScopedBitmap bitmap(bitmapHandle);
Mike Reed1103b322014-07-08 12:36:44 -0400771 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400772
773 // ARGB_4444 is a deprecated format, convert automatically to 8888
774 if (colorType == kARGB_4444_SkColorType) {
775 colorType = kN32_SkColorType;
776 }
777
778 if (width * height * SkColorTypeBytesPerPixel(colorType) > allocSize) {
Chris Craik32054b02014-05-09 13:58:56 -0700779 // done in native as there's no way to get BytesPerPixel in Java
780 doThrowIAE(env, "Bitmap not large enough to support new configuration");
781 return;
782 }
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400783 SkAlphaType alphaType;
John Reckf29ed282015-04-07 07:32:03 -0700784 if (bitmap->info().colorType() != kRGB_565_SkColorType
785 && bitmap->info().alphaType() == kOpaque_SkAlphaType) {
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400786 // If the original bitmap was set to opaque, keep that setting, unless it
787 // was 565, which is required to be opaque.
788 alphaType = kOpaque_SkAlphaType;
789 } else {
790 // Otherwise respect the premultiplied request.
791 alphaType = requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
792 }
John Reckf29ed282015-04-07 07:32:03 -0700793 bitmap->reconfigure(SkImageInfo::Make(width, height, colorType, alphaType));
Chris Craik32054b02014-05-09 13:58:56 -0700794}
795
796// These must match the int values in Bitmap.java
797enum JavaEncodeFormat {
798 kJPEG_JavaEncodeFormat = 0,
799 kPNG_JavaEncodeFormat = 1,
800 kWEBP_JavaEncodeFormat = 2
801};
802
803static jboolean Bitmap_compress(JNIEnv* env, jobject clazz, jlong bitmapHandle,
804 jint format, jint quality,
805 jobject jstream, jbyteArray jstorage) {
John Reckf29ed282015-04-07 07:32:03 -0700806
807 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700808 SkImageEncoder::Type fm;
809
810 switch (format) {
811 case kJPEG_JavaEncodeFormat:
812 fm = SkImageEncoder::kJPEG_Type;
813 break;
814 case kPNG_JavaEncodeFormat:
815 fm = SkImageEncoder::kPNG_Type;
816 break;
817 case kWEBP_JavaEncodeFormat:
818 fm = SkImageEncoder::kWEBP_Type;
819 break;
820 default:
821 return JNI_FALSE;
822 }
823
John Reckf29ed282015-04-07 07:32:03 -0700824 if (!bitmap.valid()) {
825 return JNI_FALSE;
826 }
827
Chris Craik32054b02014-05-09 13:58:56 -0700828 bool success = false;
Chris Craik32054b02014-05-09 13:58:56 -0700829
John Reckf29ed282015-04-07 07:32:03 -0700830 std::unique_ptr<SkWStream> strm(CreateJavaOutputStreamAdaptor(env, jstream, jstorage));
831 if (!strm.get()) {
832 return JNI_FALSE;
833 }
Chris Craik32054b02014-05-09 13:58:56 -0700834
John Reckf29ed282015-04-07 07:32:03 -0700835 std::unique_ptr<SkImageEncoder> encoder(SkImageEncoder::Create(fm));
836 if (encoder.get()) {
837 SkBitmap skbitmap;
838 bitmap->getSkBitmap(&skbitmap);
839 success = encoder->encodeStream(strm.get(), skbitmap, quality);
Chris Craik32054b02014-05-09 13:58:56 -0700840 }
841 return success ? JNI_TRUE : JNI_FALSE;
842}
843
844static void Bitmap_erase(JNIEnv* env, jobject, jlong bitmapHandle, jint color) {
John Reckf29ed282015-04-07 07:32:03 -0700845 LocalScopedBitmap bitmap(bitmapHandle);
846 SkBitmap skBitmap;
847 bitmap->getSkBitmap(&skBitmap);
848 skBitmap.eraseColor(color);
Chris Craik32054b02014-05-09 13:58:56 -0700849}
850
851static jint Bitmap_rowBytes(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700852 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700853 return static_cast<jint>(bitmap->rowBytes());
854}
855
856static jint Bitmap_config(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700857 LocalScopedBitmap bitmap(bitmapHandle);
858 return GraphicsJNI::colorTypeToLegacyBitmapConfig(bitmap->info().colorType());
Chris Craik32054b02014-05-09 13:58:56 -0700859}
860
861static jint Bitmap_getGenerationId(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700862 LocalScopedBitmap bitmap(bitmapHandle);
John Reckae2e8b42015-05-06 14:55:05 -0700863 return static_cast<jint>(bitmap->peekAtPixelRef()->getGenerationID());
Chris Craik32054b02014-05-09 13:58:56 -0700864}
865
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400866static jboolean Bitmap_isPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700867 LocalScopedBitmap bitmap(bitmapHandle);
868 if (bitmap->info().alphaType() == kPremul_SkAlphaType) {
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400869 return JNI_TRUE;
870 }
871 return JNI_FALSE;
872}
873
Chris Craik32054b02014-05-09 13:58:56 -0700874static jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700875 LocalScopedBitmap bitmap(bitmapHandle);
876 return !bitmap->info().isOpaque() ? JNI_TRUE : JNI_FALSE;
Chris Craik32054b02014-05-09 13:58:56 -0700877}
878
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400879static void Bitmap_setHasAlpha(JNIEnv* env, jobject, jlong bitmapHandle,
880 jboolean hasAlpha, jboolean requestPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700881 LocalScopedBitmap bitmap(bitmapHandle);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400882 if (hasAlpha) {
John Reck0781a2f2015-05-27 16:29:17 -0700883 bitmap->setAlphaType(
John Reckf29ed282015-04-07 07:32:03 -0700884 requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType);
Chris Craik32054b02014-05-09 13:58:56 -0700885 } else {
John Reck0781a2f2015-05-27 16:29:17 -0700886 bitmap->setAlphaType(kOpaque_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400887 }
888}
889
890static void Bitmap_setPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle,
891 jboolean isPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700892 LocalScopedBitmap bitmap(bitmapHandle);
893 if (!bitmap->info().isOpaque()) {
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400894 if (isPremul) {
John Reck0781a2f2015-05-27 16:29:17 -0700895 bitmap->setAlphaType(kPremul_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400896 } else {
John Reck0781a2f2015-05-27 16:29:17 -0700897 bitmap->setAlphaType(kUnpremul_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400898 }
Chris Craik32054b02014-05-09 13:58:56 -0700899 }
900}
901
902static jboolean Bitmap_hasMipMap(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700903 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700904 return bitmap->hasHardwareMipMap() ? JNI_TRUE : JNI_FALSE;
905}
906
907static void Bitmap_setHasMipMap(JNIEnv* env, jobject, jlong bitmapHandle,
908 jboolean hasMipMap) {
John Reckf29ed282015-04-07 07:32:03 -0700909 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700910 bitmap->setHasHardwareMipMap(hasMipMap);
911}
912
913///////////////////////////////////////////////////////////////////////////////
914
915static jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) {
916 if (parcel == NULL) {
917 SkDebugf("-------- unparcel parcel is NULL\n");
918 return NULL;
919 }
920
921 android::Parcel* p = android::parcelForJavaObject(env, parcel);
922
Mike Reedb9330552014-06-16 17:31:48 -0400923 const bool isMutable = p->readInt32() != 0;
924 const SkColorType colorType = (SkColorType)p->readInt32();
925 const SkAlphaType alphaType = (SkAlphaType)p->readInt32();
926 const int width = p->readInt32();
927 const int height = p->readInt32();
928 const int rowBytes = p->readInt32();
929 const int density = p->readInt32();
Chris Craik32054b02014-05-09 13:58:56 -0700930
Mike Reedb9330552014-06-16 17:31:48 -0400931 if (kN32_SkColorType != colorType &&
932 kRGB_565_SkColorType != colorType &&
933 kARGB_4444_SkColorType != colorType &&
934 kIndex_8_SkColorType != colorType &&
935 kAlpha_8_SkColorType != colorType) {
936 SkDebugf("Bitmap_createFromParcel unknown colortype: %d\n", colorType);
Chris Craik32054b02014-05-09 13:58:56 -0700937 return NULL;
938 }
939
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400940 std::unique_ptr<SkBitmap> bitmap(new SkBitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700941
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400942 if (!bitmap->setInfo(SkImageInfo::Make(width, height, colorType, alphaType), rowBytes)) {
943 return NULL;
944 }
Chris Craik32054b02014-05-09 13:58:56 -0700945
946 SkColorTable* ctable = NULL;
Mike Reedb9330552014-06-16 17:31:48 -0400947 if (colorType == kIndex_8_SkColorType) {
Chris Craik32054b02014-05-09 13:58:56 -0700948 int count = p->readInt32();
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400949 if (count < 0 || count > 256) {
950 // The data is corrupt, since SkColorTable enforces a value between 0 and 256,
951 // inclusive.
952 return NULL;
953 }
Chris Craik32054b02014-05-09 13:58:56 -0700954 if (count > 0) {
955 size_t size = count * sizeof(SkPMColor);
956 const SkPMColor* src = (const SkPMColor*)p->readInplace(size);
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400957 if (src == NULL) {
958 return NULL;
959 }
Chris Craik32054b02014-05-09 13:58:56 -0700960 ctable = new SkColorTable(src, count);
961 }
962 }
963
Jeff Browna316c5d2015-06-05 15:14:06 -0700964 // Read the bitmap blob.
965 size_t size = bitmap->getSize();
966 android::Parcel::ReadableBlob blob;
967 android::status_t status = p->readBlob(size, &blob);
968 if (status) {
Chris Craik32054b02014-05-09 13:58:56 -0700969 SkSafeUnref(ctable);
Jeff Browna316c5d2015-06-05 15:14:06 -0700970 doThrowRE(env, "Could not read bitmap blob.");
Chris Craik32054b02014-05-09 13:58:56 -0700971 return NULL;
972 }
973
Jeff Browna316c5d2015-06-05 15:14:06 -0700974 // Map the bitmap in place from the ashmem region if possible otherwise copy.
975 Bitmap* nativeBitmap;
976 if (blob.fd() >= 0 && (blob.isMutable() || !isMutable)) {
977#if DEBUG_PARCEL
978 ALOGD("Bitmap.createFromParcel: mapped contents of %s bitmap from %s blob "
979 "(fds %s)",
980 isMutable ? "mutable" : "immutable",
981 blob.isMutable() ? "mutable" : "immutable",
982 p->allowFds() ? "allowed" : "forbidden");
983#endif
984 // Dup the file descriptor so we can keep a reference to it after the Parcel
985 // is disposed.
986 int dupFd = dup(blob.fd());
987 if (dupFd < 0) {
988 blob.release();
989 SkSafeUnref(ctable);
990 doThrowRE(env, "Could not allocate dup blob fd.");
991 return NULL;
992 }
993
994 // Map the pixels in place and take ownership of the ashmem region.
995 nativeBitmap = GraphicsJNI::mapAshmemPixelRef(env, bitmap.get(),
996 ctable, dupFd, const_cast<void*>(blob.data()), !isMutable);
997 SkSafeUnref(ctable);
998 if (!nativeBitmap) {
999 close(dupFd);
1000 blob.release();
1001 doThrowRE(env, "Could not allocate ashmem pixel ref.");
1002 return NULL;
1003 }
1004
1005 // Clear the blob handle, don't release it.
1006 blob.clear();
1007 } else {
1008#if DEBUG_PARCEL
1009 if (blob.fd() >= 0) {
1010 ALOGD("Bitmap.createFromParcel: copied contents of mutable bitmap "
1011 "from immutable blob (fds %s)",
1012 p->allowFds() ? "allowed" : "forbidden");
1013 } else {
1014 ALOGD("Bitmap.createFromParcel: copied contents from %s blob "
1015 "(fds %s)",
1016 blob.isMutable() ? "mutable" : "immutable",
1017 p->allowFds() ? "allowed" : "forbidden");
1018 }
1019#endif
1020
1021 // Copy the pixels into a new buffer.
1022 nativeBitmap = GraphicsJNI::allocateJavaPixelRef(env, bitmap.get(), ctable);
1023 SkSafeUnref(ctable);
1024 if (!nativeBitmap) {
1025 blob.release();
1026 doThrowRE(env, "Could not allocate java pixel ref.");
1027 return NULL;
1028 }
1029 bitmap->lockPixels();
1030 memcpy(bitmap->getPixels(), blob.data(), size);
1031 bitmap->unlockPixels();
1032
1033 // Release the blob handle.
1034 blob.release();
Chris Craik32054b02014-05-09 13:58:56 -07001035 }
Chris Craik32054b02014-05-09 13:58:56 -07001036
John Reckf29ed282015-04-07 07:32:03 -07001037 return GraphicsJNI::createBitmap(env, nativeBitmap,
Leon Scroggins IIIec419e02015-03-11 13:12:06 -04001038 getPremulBitmapCreateFlags(isMutable), NULL, NULL, density);
Chris Craik32054b02014-05-09 13:58:56 -07001039}
1040
1041static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject,
1042 jlong bitmapHandle,
1043 jboolean isMutable, jint density,
1044 jobject parcel) {
Chris Craik32054b02014-05-09 13:58:56 -07001045 if (parcel == NULL) {
1046 SkDebugf("------- writeToParcel null parcel\n");
1047 return JNI_FALSE;
1048 }
1049
1050 android::Parcel* p = android::parcelForJavaObject(env, parcel);
John Reckf29ed282015-04-07 07:32:03 -07001051 SkBitmap bitmap;
Riley Andrews39d7f302014-11-13 17:43:25 -08001052
1053 android::Bitmap* androidBitmap = reinterpret_cast<Bitmap*>(bitmapHandle);
1054 androidBitmap->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001055
1056 p->writeInt32(isMutable);
John Reckf29ed282015-04-07 07:32:03 -07001057 p->writeInt32(bitmap.colorType());
1058 p->writeInt32(bitmap.alphaType());
1059 p->writeInt32(bitmap.width());
1060 p->writeInt32(bitmap.height());
1061 p->writeInt32(bitmap.rowBytes());
Chris Craik32054b02014-05-09 13:58:56 -07001062 p->writeInt32(density);
1063
John Reckf29ed282015-04-07 07:32:03 -07001064 if (bitmap.colorType() == kIndex_8_SkColorType) {
1065 SkColorTable* ctable = bitmap.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001066 if (ctable != NULL) {
1067 int count = ctable->count();
1068 p->writeInt32(count);
1069 memcpy(p->writeInplace(count * sizeof(SkPMColor)),
Mike Reed71487eb2014-11-19 16:13:20 -05001070 ctable->readColors(), count * sizeof(SkPMColor));
Chris Craik32054b02014-05-09 13:58:56 -07001071 } else {
1072 p->writeInt32(0); // indicate no ctable
1073 }
1074 }
1075
Jeff Browna316c5d2015-06-05 15:14:06 -07001076 // Transfer the underlying ashmem region if we have one and it's immutable.
1077 android::status_t status;
1078 int fd = androidBitmap->getAshmemFd();
1079 if (fd >= 0 && !isMutable && p->allowFds()) {
1080#if DEBUG_PARCEL
1081 ALOGD("Bitmap.writeToParcel: transferring immutable bitmap's ashmem fd as "
1082 "immutable blob (fds %s)",
1083 p->allowFds() ? "allowed" : "forbidden");
1084#endif
1085
1086 status = p->writeDupImmutableBlobFileDescriptor(fd);
1087 if (status) {
1088 doThrowRE(env, "Could not write bitmap blob file descriptor.");
Riley Andrews39d7f302014-11-13 17:43:25 -08001089 return JNI_FALSE;
1090 }
Jeff Browna316c5d2015-06-05 15:14:06 -07001091 return JNI_TRUE;
Riley Andrews39d7f302014-11-13 17:43:25 -08001092 }
Jeff Browna316c5d2015-06-05 15:14:06 -07001093
1094 // Copy the bitmap to a new blob.
1095 bool mutableCopy = isMutable;
1096#if DEBUG_PARCEL
1097 ALOGD("Bitmap.writeToParcel: copying %s bitmap into new %s blob (fds %s)",
1098 isMutable ? "mutable" : "immutable",
1099 mutableCopy ? "mutable" : "immutable",
1100 p->allowFds() ? "allowed" : "forbidden");
1101#endif
1102
1103 size_t size = bitmap.getSize();
1104 android::Parcel::WritableBlob blob;
1105 status = p->writeBlob(size, mutableCopy, &blob);
1106 if (status) {
1107 doThrowRE(env, "Could not copy bitmap to parcel blob.");
1108 return JNI_FALSE;
1109 }
1110
1111 bitmap.lockPixels();
1112 const void* pSrc = bitmap.getPixels();
1113 if (pSrc == NULL) {
1114 memset(blob.data(), 0, size);
1115 } else {
1116 memcpy(blob.data(), pSrc, size);
1117 }
1118 bitmap.unlockPixels();
1119
1120 blob.release();
Chris Craik32054b02014-05-09 13:58:56 -07001121 return JNI_TRUE;
1122}
1123
1124static jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz,
1125 jlong srcHandle, jlong paintHandle,
1126 jintArray offsetXY) {
John Reckf29ed282015-04-07 07:32:03 -07001127 SkBitmap src;
1128 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -04001129 const android::Paint* paint = reinterpret_cast<android::Paint*>(paintHandle);
Chris Craik32054b02014-05-09 13:58:56 -07001130 SkIPoint offset;
John Reckf29ed282015-04-07 07:32:03 -07001131 SkBitmap dst;
Chris Craik32054b02014-05-09 13:58:56 -07001132 JavaPixelAllocator allocator(env);
1133
John Reckf29ed282015-04-07 07:32:03 -07001134 src.extractAlpha(&dst, paint, &allocator, &offset);
Chris Craik32054b02014-05-09 13:58:56 -07001135 // If Skia can't allocate pixels for destination bitmap, it resets
1136 // it, that is set its pixels buffer to NULL, and zero width and height.
John Reckf29ed282015-04-07 07:32:03 -07001137 if (dst.getPixels() == NULL && src.getPixels() != NULL) {
Chris Craik32054b02014-05-09 13:58:56 -07001138 doThrowOOME(env, "failed to allocate pixels for alpha");
1139 return NULL;
1140 }
1141 if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) {
1142 int* array = env->GetIntArrayElements(offsetXY, NULL);
1143 array[0] = offset.fX;
1144 array[1] = offset.fY;
1145 env->ReleaseIntArrayElements(offsetXY, array, 0);
1146 }
1147
John Reckf29ed282015-04-07 07:32:03 -07001148 return GraphicsJNI::createBitmap(env, allocator.getStorageObjAndReset(),
1149 getPremulBitmapCreateFlags(true));
Chris Craik32054b02014-05-09 13:58:56 -07001150}
1151
1152///////////////////////////////////////////////////////////////////////////////
1153
1154static jint Bitmap_getPixel(JNIEnv* env, jobject, jlong bitmapHandle,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001155 jint x, jint y) {
John Reckf29ed282015-04-07 07:32:03 -07001156 SkBitmap bitmap;
1157 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1158 SkAutoLockPixels alp(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001159
John Reckf29ed282015-04-07 07:32:03 -07001160 ToColorProc proc = ChooseToColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001161 if (NULL == proc) {
1162 return 0;
1163 }
John Reckf29ed282015-04-07 07:32:03 -07001164 const void* src = bitmap.getAddr(x, y);
Chris Craik32054b02014-05-09 13:58:56 -07001165 if (NULL == src) {
1166 return 0;
1167 }
1168
1169 SkColor dst[1];
John Reckf29ed282015-04-07 07:32:03 -07001170 proc(dst, src, 1, bitmap.getColorTable());
Chris Craik32054b02014-05-09 13:58:56 -07001171 return static_cast<jint>(dst[0]);
1172}
1173
1174static void Bitmap_getPixels(JNIEnv* env, jobject, jlong bitmapHandle,
1175 jintArray pixelArray, jint offset, jint stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001176 jint x, jint y, jint width, jint height) {
John Reckf29ed282015-04-07 07:32:03 -07001177 SkBitmap bitmap;
1178 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1179 SkAutoLockPixels alp(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001180
John Reckf29ed282015-04-07 07:32:03 -07001181 ToColorProc proc = ChooseToColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001182 if (NULL == proc) {
1183 return;
1184 }
John Reckf29ed282015-04-07 07:32:03 -07001185 const void* src = bitmap.getAddr(x, y);
Chris Craik32054b02014-05-09 13:58:56 -07001186 if (NULL == src) {
1187 return;
1188 }
1189
John Reckf29ed282015-04-07 07:32:03 -07001190 SkColorTable* ctable = bitmap.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001191 jint* dst = env->GetIntArrayElements(pixelArray, NULL);
1192 SkColor* d = (SkColor*)dst + offset;
1193 while (--height >= 0) {
1194 proc(d, src, width, ctable);
1195 d += stride;
John Reckf29ed282015-04-07 07:32:03 -07001196 src = (void*)((const char*)src + bitmap.rowBytes());
Chris Craik32054b02014-05-09 13:58:56 -07001197 }
1198 env->ReleaseIntArrayElements(pixelArray, dst, 0);
1199}
1200
1201///////////////////////////////////////////////////////////////////////////////
1202
1203static void Bitmap_setPixel(JNIEnv* env, jobject, jlong bitmapHandle,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001204 jint x, jint y, jint colorHandle) {
John Reckf29ed282015-04-07 07:32:03 -07001205 SkBitmap bitmap;
1206 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001207 SkColor color = static_cast<SkColor>(colorHandle);
John Reckf29ed282015-04-07 07:32:03 -07001208 SkAutoLockPixels alp(bitmap);
1209 if (NULL == bitmap.getPixels()) {
Chris Craik32054b02014-05-09 13:58:56 -07001210 return;
1211 }
1212
John Reckf29ed282015-04-07 07:32:03 -07001213 FromColorProc proc = ChooseFromColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001214 if (NULL == proc) {
1215 return;
1216 }
1217
John Reckf29ed282015-04-07 07:32:03 -07001218 proc(bitmap.getAddr(x, y), &color, 1, x, y);
1219 bitmap.notifyPixelsChanged();
Chris Craik32054b02014-05-09 13:58:56 -07001220}
1221
1222static void Bitmap_setPixels(JNIEnv* env, jobject, jlong bitmapHandle,
1223 jintArray pixelArray, jint offset, jint stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001224 jint x, jint y, jint width, jint height) {
John Reckf29ed282015-04-07 07:32:03 -07001225 SkBitmap bitmap;
1226 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001227 GraphicsJNI::SetPixels(env, pixelArray, offset, stride,
John Reckf29ed282015-04-07 07:32:03 -07001228 x, y, width, height, bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001229}
1230
1231static void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject,
1232 jlong bitmapHandle, jobject jbuffer) {
John Reckf29ed282015-04-07 07:32:03 -07001233 SkBitmap bitmap;
1234 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1235 SkAutoLockPixels alp(bitmap);
1236 const void* src = bitmap.getPixels();
Chris Craik32054b02014-05-09 13:58:56 -07001237
1238 if (NULL != src) {
1239 android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE);
1240
1241 // the java side has already checked that buffer is large enough
John Reckf29ed282015-04-07 07:32:03 -07001242 memcpy(abp.pointer(), src, bitmap.getSize());
Chris Craik32054b02014-05-09 13:58:56 -07001243 }
1244}
1245
1246static void Bitmap_copyPixelsFromBuffer(JNIEnv* env, jobject,
1247 jlong bitmapHandle, jobject jbuffer) {
John Reckf29ed282015-04-07 07:32:03 -07001248 SkBitmap bitmap;
1249 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1250 SkAutoLockPixels alp(bitmap);
1251 void* dst = bitmap.getPixels();
Chris Craik32054b02014-05-09 13:58:56 -07001252
1253 if (NULL != dst) {
1254 android::AutoBufferPointer abp(env, jbuffer, JNI_FALSE);
1255 // the java side has already checked that buffer is large enough
John Reckf29ed282015-04-07 07:32:03 -07001256 memcpy(dst, abp.pointer(), bitmap.getSize());
1257 bitmap.notifyPixelsChanged();
Chris Craik32054b02014-05-09 13:58:56 -07001258 }
1259}
1260
1261static jboolean Bitmap_sameAs(JNIEnv* env, jobject, jlong bm0Handle,
1262 jlong bm1Handle) {
John Reckf29ed282015-04-07 07:32:03 -07001263 SkBitmap bm0;
1264 SkBitmap bm1;
1265 reinterpret_cast<Bitmap*>(bm0Handle)->getSkBitmap(&bm0);
1266 reinterpret_cast<Bitmap*>(bm1Handle)->getSkBitmap(&bm1);
1267 if (bm0.width() != bm1.width() ||
1268 bm0.height() != bm1.height() ||
1269 bm0.colorType() != bm1.colorType()) {
Chris Craik32054b02014-05-09 13:58:56 -07001270 return JNI_FALSE;
1271 }
1272
John Reckf29ed282015-04-07 07:32:03 -07001273 SkAutoLockPixels alp0(bm0);
1274 SkAutoLockPixels alp1(bm1);
Chris Craik32054b02014-05-09 13:58:56 -07001275
1276 // if we can't load the pixels, return false
John Reckf29ed282015-04-07 07:32:03 -07001277 if (NULL == bm0.getPixels() || NULL == bm1.getPixels()) {
Chris Craik32054b02014-05-09 13:58:56 -07001278 return JNI_FALSE;
1279 }
1280
John Reckf29ed282015-04-07 07:32:03 -07001281 if (bm0.colorType() == kIndex_8_SkColorType) {
1282 SkColorTable* ct0 = bm0.getColorTable();
1283 SkColorTable* ct1 = bm1.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001284 if (NULL == ct0 || NULL == ct1) {
1285 return JNI_FALSE;
1286 }
1287 if (ct0->count() != ct1->count()) {
1288 return JNI_FALSE;
1289 }
1290
Chris Craik32054b02014-05-09 13:58:56 -07001291 const size_t size = ct0->count() * sizeof(SkPMColor);
Mike Reed71487eb2014-11-19 16:13:20 -05001292 if (memcmp(ct0->readColors(), ct1->readColors(), size) != 0) {
Chris Craik32054b02014-05-09 13:58:56 -07001293 return JNI_FALSE;
1294 }
1295 }
1296
1297 // now compare each scanline. We can't do the entire buffer at once,
1298 // since we don't care about the pixel values that might extend beyond
1299 // the width (since the scanline might be larger than the logical width)
John Reckf29ed282015-04-07 07:32:03 -07001300 const int h = bm0.height();
1301 const size_t size = bm0.width() * bm0.bytesPerPixel();
Chris Craik32054b02014-05-09 13:58:56 -07001302 for (int y = 0; y < h; y++) {
henry.uh_chen53001ca2014-07-03 20:40:22 +08001303 // SkBitmap::getAddr(int, int) may return NULL due to unrecognized config
1304 // (ex: kRLE_Index8_Config). This will cause memcmp method to crash. Since bm0
1305 // and bm1 both have pixel data() (have passed NULL == getPixels() check),
1306 // those 2 bitmaps should be valid (only unrecognized), we return JNI_FALSE
1307 // to warn user those 2 unrecognized config bitmaps may be different.
John Reckf29ed282015-04-07 07:32:03 -07001308 void *bm0Addr = bm0.getAddr(0, y);
1309 void *bm1Addr = bm1.getAddr(0, y);
henry.uh_chen53001ca2014-07-03 20:40:22 +08001310
1311 if(bm0Addr == NULL || bm1Addr == NULL) {
1312 return JNI_FALSE;
1313 }
1314
1315 if (memcmp(bm0Addr, bm1Addr, size) != 0) {
Chris Craik32054b02014-05-09 13:58:56 -07001316 return JNI_FALSE;
1317 }
1318 }
1319 return JNI_TRUE;
1320}
1321
John Reckc6e2e8f2015-04-15 13:24:47 -07001322static jlong Bitmap_refPixelRef(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -07001323 LocalScopedBitmap bitmap(bitmapHandle);
John Reckae2e8b42015-05-06 14:55:05 -07001324 SkPixelRef* pixelRef = bitmap.valid() ? bitmap->peekAtPixelRef() : nullptr;
John Reckc6e2e8f2015-04-15 13:24:47 -07001325 SkSafeRef(pixelRef);
1326 return reinterpret_cast<jlong>(pixelRef);
1327}
1328
Chris Craik32054b02014-05-09 13:58:56 -07001329///////////////////////////////////////////////////////////////////////////////
1330
Chris Craik32054b02014-05-09 13:58:56 -07001331static JNINativeMethod gBitmapMethods[] = {
1332 { "nativeCreate", "([IIIIIIZ)Landroid/graphics/Bitmap;",
1333 (void*)Bitmap_creator },
1334 { "nativeCopy", "(JIZ)Landroid/graphics/Bitmap;",
1335 (void*)Bitmap_copy },
Riley Andrews721ae5f2015-05-11 16:08:22 -07001336 { "nativeCopyAshmem", "(J)Landroid/graphics/Bitmap;",
1337 (void*)Bitmap_copyAshmem },
Chris Craik32054b02014-05-09 13:58:56 -07001338 { "nativeDestructor", "(J)V", (void*)Bitmap_destructor },
1339 { "nativeRecycle", "(J)Z", (void*)Bitmap_recycle },
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -04001340 { "nativeReconfigure", "(JIIIIZ)V", (void*)Bitmap_reconfigure },
Chris Craik32054b02014-05-09 13:58:56 -07001341 { "nativeCompress", "(JIILjava/io/OutputStream;[B)Z",
1342 (void*)Bitmap_compress },
1343 { "nativeErase", "(JI)V", (void*)Bitmap_erase },
1344 { "nativeRowBytes", "(J)I", (void*)Bitmap_rowBytes },
1345 { "nativeConfig", "(J)I", (void*)Bitmap_config },
1346 { "nativeHasAlpha", "(J)Z", (void*)Bitmap_hasAlpha },
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001347 { "nativeIsPremultiplied", "(J)Z", (void*)Bitmap_isPremultiplied},
1348 { "nativeSetHasAlpha", "(JZZ)V", (void*)Bitmap_setHasAlpha},
1349 { "nativeSetPremultiplied", "(JZ)V", (void*)Bitmap_setPremultiplied},
Chris Craik32054b02014-05-09 13:58:56 -07001350 { "nativeHasMipMap", "(J)Z", (void*)Bitmap_hasMipMap },
1351 { "nativeSetHasMipMap", "(JZ)V", (void*)Bitmap_setHasMipMap },
1352 { "nativeCreateFromParcel",
1353 "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;",
1354 (void*)Bitmap_createFromParcel },
1355 { "nativeWriteToParcel", "(JZILandroid/os/Parcel;)Z",
1356 (void*)Bitmap_writeToParcel },
1357 { "nativeExtractAlpha", "(JJ[I)Landroid/graphics/Bitmap;",
1358 (void*)Bitmap_extractAlpha },
1359 { "nativeGenerationId", "(J)I", (void*)Bitmap_getGenerationId },
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001360 { "nativeGetPixel", "(JII)I", (void*)Bitmap_getPixel },
1361 { "nativeGetPixels", "(J[IIIIIII)V", (void*)Bitmap_getPixels },
1362 { "nativeSetPixel", "(JIII)V", (void*)Bitmap_setPixel },
1363 { "nativeSetPixels", "(J[IIIIIII)V", (void*)Bitmap_setPixels },
Chris Craik32054b02014-05-09 13:58:56 -07001364 { "nativeCopyPixelsToBuffer", "(JLjava/nio/Buffer;)V",
1365 (void*)Bitmap_copyPixelsToBuffer },
1366 { "nativeCopyPixelsFromBuffer", "(JLjava/nio/Buffer;)V",
1367 (void*)Bitmap_copyPixelsFromBuffer },
1368 { "nativeSameAs", "(JJ)Z", (void*)Bitmap_sameAs },
John Reckc6e2e8f2015-04-15 13:24:47 -07001369 { "nativeRefPixelRef", "(J)J", (void*)Bitmap_refPixelRef },
Chris Craik32054b02014-05-09 13:58:56 -07001370};
1371
Chris Craik32054b02014-05-09 13:58:56 -07001372int register_android_graphics_Bitmap(JNIEnv* env)
1373{
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001374 return android::RegisterMethodsOrDie(env, "android/graphics/Bitmap", gBitmapMethods,
1375 NELEM(gBitmapMethods));
Chris Craik32054b02014-05-09 13:58:56 -07001376}