blob: 832f92f07a052611aca26c88cfab05ff3b612983 [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
John Reckf29ed282015-04-07 07:32:03 -070030namespace android {
31
32class WrappedPixelRef : public SkPixelRef {
33public:
34 WrappedPixelRef(Bitmap* wrapper, void* storage,
35 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
36 : SkPixelRef(info)
37 , mBitmap(*wrapper)
38 , mStorage(storage) {
39 reconfigure(info, rowBytes, ctable);
40 }
41
42 ~WrappedPixelRef() {
43 // Tell SkRefCnt that everything is as it expects by forcing
44 // the refcnt to 1
45 internal_dispose_restore_refcnt_to_1();
46 SkSafeUnref(mColorTable);
47 }
48
John Reck0781a2f2015-05-27 16:29:17 -070049 void reconfigure(const SkImageInfo& newInfo, size_t rowBytes, SkColorTable* ctable) {
50 if (kIndex_8_SkColorType != newInfo.colorType()) {
John Reckf29ed282015-04-07 07:32:03 -070051 ctable = nullptr;
52 }
53 mRowBytes = rowBytes;
54 if (mColorTable != ctable) {
55 SkSafeUnref(mColorTable);
56 mColorTable = ctable;
57 SkSafeRef(mColorTable);
58 }
John Reck0781a2f2015-05-27 16:29:17 -070059
60 // Need to validate the alpha type to filter against the color type
61 // to prevent things like a non-opaque RGB565 bitmap
62 SkAlphaType alphaType;
63 LOG_ALWAYS_FATAL_IF(!SkColorTypeValidateAlphaType(
64 newInfo.colorType(), newInfo.alphaType(), &alphaType),
65 "Failed to validate alpha type!");
66
John Reckf29ed282015-04-07 07:32:03 -070067 // Dirty hack is dirty
68 // TODO: Figure something out here, Skia's current design makes this
69 // really hard to work with. Skia really, really wants immutable objects,
70 // but with the nested-ref-count hackery going on that's just not
71 // feasible without going insane trying to figure it out
72 SkImageInfo* myInfo = const_cast<SkImageInfo*>(&this->info());
John Reck0781a2f2015-05-27 16:29:17 -070073 *myInfo = newInfo;
74 changeAlphaType(alphaType);
John Reckf29ed282015-04-07 07:32:03 -070075
76 // Docs say to only call this in the ctor, but we're going to call
77 // it anyway even if this isn't always the ctor.
78 // TODO: Fix this too as part of the above TODO
79 setPreLocked(mStorage, mRowBytes, mColorTable);
80 }
81
82 // Can't mark as override since SkPixelRef::rowBytes isn't virtual
83 // but that's OK since we just want BitmapWrapper to be able to rely
84 // on calling rowBytes() on an unlocked pixelref, which it will be
85 // doing on a WrappedPixelRef type, not a SkPixelRef, so static
86 // dispatching will do what we want.
87 size_t rowBytes() const { return mRowBytes; }
88 SkColorTable* colorTable() const { return mColorTable; }
89
90 bool hasHardwareMipMap() const {
91 return mHasHardwareMipMap;
92 }
93
94 void setHasHardwareMipMap(bool hasMipMap) {
95 mHasHardwareMipMap = hasMipMap;
96 }
97
98protected:
99 virtual bool onNewLockPixels(LockRec* rec) override {
100 rec->fPixels = mStorage;
101 rec->fRowBytes = mRowBytes;
102 rec->fColorTable = mColorTable;
103 return true;
104 }
105
106 virtual void onUnlockPixels() override {
107 // nothing
108 }
109
110 virtual size_t getAllocatedSizeInBytes() const override {
111 return info().getSafeSize(mRowBytes);
112 }
113
114private:
115 Bitmap& mBitmap;
116 void* mStorage;
117 size_t mRowBytes = 0;
118 SkColorTable* mColorTable = nullptr;
119 bool mHasHardwareMipMap = false;
120
121 virtual void internal_dispose() const override {
122 mBitmap.onStrongRefDestroyed();
123 }
124};
125
126Bitmap::Bitmap(JNIEnv* env, jbyteArray storageObj, void* address,
127 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
128 : mPixelStorageType(PixelStorageType::Java) {
129 env->GetJavaVM(&mPixelStorage.java.jvm);
130 mPixelStorage.java.jweakRef = env->NewWeakGlobalRef(storageObj);
131 mPixelStorage.java.jstrongRef = nullptr;
132 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
133 // Note: this will trigger a call to onStrongRefDestroyed(), but
134 // we want the pixel ref to have a ref count of 0 at this point
135 mPixelRef->unref();
136}
137
138Bitmap::Bitmap(void* address, void* context, FreeFunc freeFunc,
139 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
140 : mPixelStorageType(PixelStorageType::External) {
141 mPixelStorage.external.address = address;
142 mPixelStorage.external.context = context;
143 mPixelStorage.external.freeFunc = freeFunc;
144 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
145 // Note: this will trigger a call to onStrongRefDestroyed(), but
146 // we want the pixel ref to have a ref count of 0 at this point
147 mPixelRef->unref();
148}
149
Riley Andrews39d7f302014-11-13 17:43:25 -0800150Bitmap::Bitmap(void* address, int fd,
151 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
152 : mPixelStorageType(PixelStorageType::Ashmem) {
153 mPixelStorage.ashmem.address = address;
154 mPixelStorage.ashmem.fd = fd;
155 mPixelStorage.ashmem.size = ashmem_get_size_region(fd);
156 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
157 // Note: this will trigger a call to onStrongRefDestroyed(), but
158 // we want the pixel ref to have a ref count of 0 at this point
159 mPixelRef->unref();
160}
John Reckf29ed282015-04-07 07:32:03 -0700161Bitmap::~Bitmap() {
162 doFreePixels();
163}
164
165void Bitmap::freePixels() {
166 AutoMutex _lock(mLock);
167 if (mPinnedRefCount == 0) {
168 doFreePixels();
169 mPixelStorageType = PixelStorageType::Invalid;
170 }
171}
172
173void Bitmap::doFreePixels() {
174 switch (mPixelStorageType) {
175 case PixelStorageType::Invalid:
176 // already free'd, nothing to do
177 break;
178 case PixelStorageType::External:
179 mPixelStorage.external.freeFunc(mPixelStorage.external.address,
180 mPixelStorage.external.context);
181 break;
Riley Andrews39d7f302014-11-13 17:43:25 -0800182 case PixelStorageType::Ashmem:
183 munmap(mPixelStorage.ashmem.address, mPixelStorage.ashmem.size);
184 close(mPixelStorage.ashmem.fd);
185 break;
John Reckf29ed282015-04-07 07:32:03 -0700186 case PixelStorageType::Java:
187 JNIEnv* env = jniEnv();
188 LOG_ALWAYS_FATAL_IF(mPixelStorage.java.jstrongRef,
189 "Deleting a bitmap wrapper while there are outstanding strong "
190 "references! mPinnedRefCount = %d", mPinnedRefCount);
191 env->DeleteWeakGlobalRef(mPixelStorage.java.jweakRef);
192 break;
193 }
194
195 if (android::uirenderer::Caches::hasInstance()) {
196 android::uirenderer::Caches::getInstance().textureCache.releaseTexture(
197 mPixelRef->getStableID());
198 }
199}
200
201bool Bitmap::hasHardwareMipMap() {
202 return mPixelRef->hasHardwareMipMap();
203}
204
205void Bitmap::setHasHardwareMipMap(bool hasMipMap) {
206 mPixelRef->setHasHardwareMipMap(hasMipMap);
207}
208
Riley Andrews39d7f302014-11-13 17:43:25 -0800209int Bitmap::getAshmemFd() const {
210 switch (mPixelStorageType) {
211 case PixelStorageType::Ashmem:
212 return mPixelStorage.ashmem.fd;
213 default:
214 return -1;
215 }
216}
217
John Reckf29ed282015-04-07 07:32:03 -0700218const SkImageInfo& Bitmap::info() const {
219 assertValid();
220 return mPixelRef->info();
221}
222
223size_t Bitmap::rowBytes() const {
224 return mPixelRef->rowBytes();
225}
226
John Reckae2e8b42015-05-06 14:55:05 -0700227SkPixelRef* Bitmap::peekAtPixelRef() const {
John Reckf29ed282015-04-07 07:32:03 -0700228 assertValid();
229 return mPixelRef.get();
230}
231
John Reckae2e8b42015-05-06 14:55:05 -0700232SkPixelRef* Bitmap::refPixelRef() {
233 assertValid();
234 android::AutoMutex _lock(mLock);
235 return refPixelRefLocked();
236}
237
238SkPixelRef* Bitmap::refPixelRefLocked() {
239 mPixelRef->ref();
240 if (mPixelRef->unique()) {
241 // We just restored this from 0, pin the pixels and inc the strong count
242 // Note that there *might be* an incoming onStrongRefDestroyed from whatever
243 // last unref'd
244 pinPixelsLocked();
245 mPinnedRefCount++;
246 }
247 return mPixelRef.get();
248}
249
John Reckf29ed282015-04-07 07:32:03 -0700250void Bitmap::reconfigure(const SkImageInfo& info, size_t rowBytes,
251 SkColorTable* ctable) {
John Reckae2e8b42015-05-06 14:55:05 -0700252 {
253 android::AutoMutex _lock(mLock);
254 if (mPinnedRefCount) {
255 ALOGW("Called reconfigure on a bitmap that is in use! This may"
256 " cause graphical corruption!");
257 }
258 }
John Reckf29ed282015-04-07 07:32:03 -0700259 mPixelRef->reconfigure(info, rowBytes, ctable);
260}
261
262void Bitmap::reconfigure(const SkImageInfo& info) {
Derek Sollenberger2a94a102015-05-07 13:56:14 -0400263 reconfigure(info, info.minRowBytes(), nullptr);
John Reckf29ed282015-04-07 07:32:03 -0700264}
265
John Reck0781a2f2015-05-27 16:29:17 -0700266void Bitmap::setAlphaType(SkAlphaType alphaType) {
267 if (!SkColorTypeValidateAlphaType(info().colorType(), alphaType, &alphaType)) {
268 return;
269 }
270
271 mPixelRef->changeAlphaType(alphaType);
272}
273
John Reckf29ed282015-04-07 07:32:03 -0700274void Bitmap::detachFromJava() {
275 bool disposeSelf;
276 {
277 android::AutoMutex _lock(mLock);
278 mAttachedToJava = false;
279 disposeSelf = shouldDisposeSelfLocked();
280 }
281 if (disposeSelf) {
282 delete this;
283 }
284}
285
286bool Bitmap::shouldDisposeSelfLocked() {
287 return mPinnedRefCount == 0 && !mAttachedToJava;
288}
289
290JNIEnv* Bitmap::jniEnv() {
291 JNIEnv* env;
292 auto success = mPixelStorage.java.jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
293 LOG_ALWAYS_FATAL_IF(success != JNI_OK,
294 "Failed to get JNIEnv* from JVM: %p", mPixelStorage.java.jvm);
295 return env;
296}
297
298void Bitmap::onStrongRefDestroyed() {
299 bool disposeSelf = false;
300 {
301 android::AutoMutex _lock(mLock);
302 if (mPinnedRefCount > 0) {
303 mPinnedRefCount--;
304 if (mPinnedRefCount == 0) {
305 unpinPixelsLocked();
306 disposeSelf = shouldDisposeSelfLocked();
307 }
308 }
309 }
310 if (disposeSelf) {
311 delete this;
312 }
313}
314
315void Bitmap::pinPixelsLocked() {
316 switch (mPixelStorageType) {
317 case PixelStorageType::Invalid:
318 LOG_ALWAYS_FATAL("Cannot pin invalid pixels!");
319 break;
320 case PixelStorageType::External:
Riley Andrews39d7f302014-11-13 17:43:25 -0800321 case PixelStorageType::Ashmem:
John Reckf29ed282015-04-07 07:32:03 -0700322 // Nothing to do
323 break;
324 case PixelStorageType::Java: {
325 JNIEnv* env = jniEnv();
326 if (!mPixelStorage.java.jstrongRef) {
327 mPixelStorage.java.jstrongRef = reinterpret_cast<jbyteArray>(
328 env->NewGlobalRef(mPixelStorage.java.jweakRef));
329 if (!mPixelStorage.java.jstrongRef) {
330 LOG_ALWAYS_FATAL("Failed to acquire strong reference to pixels");
331 }
332 }
333 break;
334 }
335 }
336}
337
338void Bitmap::unpinPixelsLocked() {
339 switch (mPixelStorageType) {
340 case PixelStorageType::Invalid:
341 LOG_ALWAYS_FATAL("Cannot unpin invalid pixels!");
342 break;
343 case PixelStorageType::External:
Riley Andrews39d7f302014-11-13 17:43:25 -0800344 case PixelStorageType::Ashmem:
John Reckf29ed282015-04-07 07:32:03 -0700345 // Don't need to do anything
346 break;
347 case PixelStorageType::Java: {
348 JNIEnv* env = jniEnv();
349 if (mPixelStorage.java.jstrongRef) {
350 env->DeleteGlobalRef(mPixelStorage.java.jstrongRef);
351 mPixelStorage.java.jstrongRef = nullptr;
352 }
353 break;
354 }
355 }
356}
357
358void Bitmap::getSkBitmap(SkBitmap* outBitmap) {
359 assertValid();
360 android::AutoMutex _lock(mLock);
John Reckf29ed282015-04-07 07:32:03 -0700361 // Safe because mPixelRef is a WrappedPixelRef type, otherwise rowBytes()
362 // would require locking the pixels first.
363 outBitmap->setInfo(mPixelRef->info(), mPixelRef->rowBytes());
John Reckae2e8b42015-05-06 14:55:05 -0700364 outBitmap->setPixelRef(refPixelRefLocked())->unref();
John Reckf29ed282015-04-07 07:32:03 -0700365 outBitmap->setHasHardwareMipMap(hasHardwareMipMap());
366}
367
368void Bitmap::assertValid() const {
369 LOG_ALWAYS_FATAL_IF(mPixelStorageType == PixelStorageType::Invalid,
370 "Error, cannot access an invalid/free'd bitmap here!");
371}
372
373} // namespace android
374
375using namespace android;
376
377// Convenience class that does not take a global ref on the pixels, relying
378// on the caller already having a local JNI ref
379class LocalScopedBitmap {
380public:
381 LocalScopedBitmap(jlong bitmapHandle)
382 : mBitmap(reinterpret_cast<Bitmap*>(bitmapHandle)) {}
383
384 Bitmap* operator->() {
385 return mBitmap;
386 }
387
388 void* pixels() {
John Reckae2e8b42015-05-06 14:55:05 -0700389 return mBitmap->peekAtPixelRef()->pixels();
John Reckf29ed282015-04-07 07:32:03 -0700390 }
391
392 bool valid() {
393 return mBitmap && mBitmap->valid();
394 }
395
396private:
397 Bitmap* mBitmap;
398};
399
Chris Craik32054b02014-05-09 13:58:56 -0700400///////////////////////////////////////////////////////////////////////////////
401// Conversions to/from SkColor, for get/setPixels, and the create method, which
402// is basically like setPixels
403
404typedef void (*FromColorProc)(void* dst, const SkColor src[], int width,
405 int x, int y);
406
407static void FromColor_D32(void* dst, const SkColor src[], int width,
408 int, int) {
409 SkPMColor* d = (SkPMColor*)dst;
410
411 for (int i = 0; i < width; i++) {
412 *d++ = SkPreMultiplyColor(*src++);
413 }
414}
415
416static void FromColor_D32_Raw(void* dst, const SkColor src[], int width,
417 int, int) {
Dan Albert46d84442014-11-18 16:07:51 -0800418 // Needed to thwart the unreachable code detection from clang.
419 static const bool sk_color_ne_zero = SK_COLOR_MATCHES_PMCOLOR_BYTE_ORDER;
420
Chris Craik32054b02014-05-09 13:58:56 -0700421 // SkColor's ordering may be different from SkPMColor
Dan Albert46d84442014-11-18 16:07:51 -0800422 if (sk_color_ne_zero) {
Chris Craik32054b02014-05-09 13:58:56 -0700423 memcpy(dst, src, width * sizeof(SkColor));
424 return;
425 }
426
427 // order isn't same, repack each pixel manually
428 SkPMColor* d = (SkPMColor*)dst;
429 for (int i = 0; i < width; i++) {
430 SkColor c = *src++;
431 *d++ = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c),
432 SkColorGetG(c), SkColorGetB(c));
433 }
434}
435
436static void FromColor_D565(void* dst, const SkColor src[], int width,
437 int x, int y) {
438 uint16_t* d = (uint16_t*)dst;
439
440 DITHER_565_SCAN(y);
441 for (int stop = x + width; x < stop; x++) {
442 SkColor c = *src++;
443 *d++ = SkDitherRGBTo565(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c),
444 DITHER_VALUE(x));
445 }
446}
447
448static void FromColor_D4444(void* dst, const SkColor src[], int width,
449 int x, int y) {
450 SkPMColor16* d = (SkPMColor16*)dst;
451
452 DITHER_4444_SCAN(y);
453 for (int stop = x + width; x < stop; x++) {
454 SkPMColor pmc = SkPreMultiplyColor(*src++);
455 *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x));
456// *d++ = SkPixel32ToPixel4444(pmc);
457 }
458}
459
460static void FromColor_D4444_Raw(void* dst, const SkColor src[], int width,
461 int x, int y) {
462 SkPMColor16* d = (SkPMColor16*)dst;
463
464 DITHER_4444_SCAN(y);
465 for (int stop = x + width; x < stop; x++) {
466 SkColor c = *src++;
467
468 // SkPMColor is used because the ordering is ARGB32, even though the target actually premultiplied
469 SkPMColor pmc = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c),
470 SkColorGetG(c), SkColorGetB(c));
471 *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x));
472// *d++ = SkPixel32ToPixel4444(pmc);
473 }
474}
475
476// can return NULL
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400477static FromColorProc ChooseFromColorProc(const SkBitmap& bitmap) {
478 switch (bitmap.colorType()) {
479 case kN32_SkColorType:
480 return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D32 : FromColor_D32_Raw;
481 case kARGB_4444_SkColorType:
482 return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D4444 :
483 FromColor_D4444_Raw;
484 case kRGB_565_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700485 return FromColor_D565;
486 default:
487 break;
488 }
489 return NULL;
490}
491
492bool GraphicsJNI::SetPixels(JNIEnv* env, jintArray srcColors, int srcOffset, int srcStride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400493 int x, int y, int width, int height, const SkBitmap& dstBitmap) {
Chris Craik32054b02014-05-09 13:58:56 -0700494 SkAutoLockPixels alp(dstBitmap);
495 void* dst = dstBitmap.getPixels();
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400496 FromColorProc proc = ChooseFromColorProc(dstBitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700497
498 if (NULL == dst || NULL == proc) {
499 return false;
500 }
501
502 const jint* array = env->GetIntArrayElements(srcColors, NULL);
503 const SkColor* src = (const SkColor*)array + srcOffset;
504
505 // reset to to actual choice from caller
506 dst = dstBitmap.getAddr(x, y);
507 // now copy/convert each scanline
508 for (int y = 0; y < height; y++) {
509 proc(dst, src, width, x, y);
510 src += srcStride;
511 dst = (char*)dst + dstBitmap.rowBytes();
512 }
513
514 dstBitmap.notifyPixelsChanged();
515
516 env->ReleaseIntArrayElements(srcColors, const_cast<jint*>(array),
517 JNI_ABORT);
518 return true;
519}
520
521//////////////////// ToColor procs
522
523typedef void (*ToColorProc)(SkColor dst[], const void* src, int width,
524 SkColorTable*);
525
526static void ToColor_S32_Alpha(SkColor dst[], const void* src, int width,
527 SkColorTable*) {
528 SkASSERT(width > 0);
529 const SkPMColor* s = (const SkPMColor*)src;
530 do {
531 *dst++ = SkUnPreMultiply::PMColorToColor(*s++);
532 } while (--width != 0);
533}
534
535static void ToColor_S32_Raw(SkColor dst[], const void* src, int width,
536 SkColorTable*) {
537 SkASSERT(width > 0);
538 const SkPMColor* s = (const SkPMColor*)src;
539 do {
540 SkPMColor c = *s++;
541 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
542 SkGetPackedG32(c), SkGetPackedB32(c));
543 } while (--width != 0);
544}
545
546static void ToColor_S32_Opaque(SkColor dst[], const void* src, int width,
547 SkColorTable*) {
548 SkASSERT(width > 0);
549 const SkPMColor* s = (const SkPMColor*)src;
550 do {
551 SkPMColor c = *s++;
552 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
553 SkGetPackedB32(c));
554 } while (--width != 0);
555}
556
557static void ToColor_S4444_Alpha(SkColor dst[], const void* src, int width,
558 SkColorTable*) {
559 SkASSERT(width > 0);
560 const SkPMColor16* s = (const SkPMColor16*)src;
561 do {
562 *dst++ = SkUnPreMultiply::PMColorToColor(SkPixel4444ToPixel32(*s++));
563 } while (--width != 0);
564}
565
566static void ToColor_S4444_Raw(SkColor dst[], const void* src, int width,
567 SkColorTable*) {
568 SkASSERT(width > 0);
569 const SkPMColor16* s = (const SkPMColor16*)src;
570 do {
571 SkPMColor c = SkPixel4444ToPixel32(*s++);
572 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
573 SkGetPackedG32(c), SkGetPackedB32(c));
574 } while (--width != 0);
575}
576
577static void ToColor_S4444_Opaque(SkColor dst[], const void* src, int width,
578 SkColorTable*) {
579 SkASSERT(width > 0);
580 const SkPMColor16* s = (const SkPMColor16*)src;
581 do {
582 SkPMColor c = SkPixel4444ToPixel32(*s++);
583 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
584 SkGetPackedB32(c));
585 } while (--width != 0);
586}
587
588static void ToColor_S565(SkColor dst[], const void* src, int width,
589 SkColorTable*) {
590 SkASSERT(width > 0);
591 const uint16_t* s = (const uint16_t*)src;
592 do {
593 uint16_t c = *s++;
594 *dst++ = SkColorSetRGB(SkPacked16ToR32(c), SkPacked16ToG32(c),
595 SkPacked16ToB32(c));
596 } while (--width != 0);
597}
598
599static void ToColor_SI8_Alpha(SkColor dst[], const void* src, int width,
600 SkColorTable* ctable) {
601 SkASSERT(width > 0);
602 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500603 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700604 do {
605 *dst++ = SkUnPreMultiply::PMColorToColor(colors[*s++]);
606 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700607}
608
609static void ToColor_SI8_Raw(SkColor dst[], const void* src, int width,
610 SkColorTable* ctable) {
611 SkASSERT(width > 0);
612 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500613 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700614 do {
615 SkPMColor c = colors[*s++];
616 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
617 SkGetPackedG32(c), SkGetPackedB32(c));
618 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700619}
620
621static void ToColor_SI8_Opaque(SkColor dst[], const void* src, int width,
622 SkColorTable* ctable) {
623 SkASSERT(width > 0);
624 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500625 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700626 do {
627 SkPMColor c = colors[*s++];
628 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
629 SkGetPackedB32(c));
630 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700631}
632
633// can return NULL
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400634static ToColorProc ChooseToColorProc(const SkBitmap& src) {
Mike Reedb9330552014-06-16 17:31:48 -0400635 switch (src.colorType()) {
636 case kN32_SkColorType:
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400637 switch (src.alphaType()) {
638 case kOpaque_SkAlphaType:
639 return ToColor_S32_Opaque;
640 case kPremul_SkAlphaType:
641 return ToColor_S32_Alpha;
642 case kUnpremul_SkAlphaType:
643 return ToColor_S32_Raw;
644 default:
645 return NULL;
646 }
Mike Reedb9330552014-06-16 17:31:48 -0400647 case kARGB_4444_SkColorType:
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400648 switch (src.alphaType()) {
649 case kOpaque_SkAlphaType:
650 return ToColor_S4444_Opaque;
651 case kPremul_SkAlphaType:
652 return ToColor_S4444_Alpha;
653 case kUnpremul_SkAlphaType:
654 return ToColor_S4444_Raw;
655 default:
656 return NULL;
657 }
Mike Reedb9330552014-06-16 17:31:48 -0400658 case kRGB_565_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700659 return ToColor_S565;
Mike Reedb9330552014-06-16 17:31:48 -0400660 case kIndex_8_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700661 if (src.getColorTable() == NULL) {
662 return NULL;
663 }
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400664 switch (src.alphaType()) {
665 case kOpaque_SkAlphaType:
666 return ToColor_SI8_Opaque;
667 case kPremul_SkAlphaType:
668 return ToColor_SI8_Alpha;
669 case kUnpremul_SkAlphaType:
670 return ToColor_SI8_Raw;
671 default:
672 return NULL;
673 }
Chris Craik32054b02014-05-09 13:58:56 -0700674 default:
675 break;
676 }
677 return NULL;
678}
679
680///////////////////////////////////////////////////////////////////////////////
681///////////////////////////////////////////////////////////////////////////////
682
683static int getPremulBitmapCreateFlags(bool isMutable) {
684 int flags = GraphicsJNI::kBitmapCreateFlag_Premultiplied;
685 if (isMutable) flags |= GraphicsJNI::kBitmapCreateFlag_Mutable;
686 return flags;
687}
688
689static jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors,
690 jint offset, jint stride, jint width, jint height,
691 jint configHandle, jboolean isMutable) {
Mike Reed1103b322014-07-08 12:36:44 -0400692 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700693 if (NULL != jColors) {
694 size_t n = env->GetArrayLength(jColors);
695 if (n < SkAbs32(stride) * (size_t)height) {
696 doThrowAIOOBE(env);
697 return NULL;
698 }
699 }
700
701 // ARGB_4444 is a deprecated format, convert automatically to 8888
Mike Reedb9330552014-06-16 17:31:48 -0400702 if (colorType == kARGB_4444_SkColorType) {
703 colorType = kN32_SkColorType;
Chris Craik32054b02014-05-09 13:58:56 -0700704 }
705
706 SkBitmap bitmap;
Mike Reedb9330552014-06-16 17:31:48 -0400707 bitmap.setInfo(SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType));
Chris Craik32054b02014-05-09 13:58:56 -0700708
John Reckf29ed282015-04-07 07:32:03 -0700709 Bitmap* nativeBitmap = GraphicsJNI::allocateJavaPixelRef(env, &bitmap, NULL);
710 if (!nativeBitmap) {
Chris Craik32054b02014-05-09 13:58:56 -0700711 return NULL;
712 }
713
714 if (jColors != NULL) {
715 GraphicsJNI::SetPixels(env, jColors, offset, stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400716 0, 0, width, height, bitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700717 }
718
John Reckf29ed282015-04-07 07:32:03 -0700719 return GraphicsJNI::createBitmap(env, nativeBitmap,
720 getPremulBitmapCreateFlags(isMutable));
Chris Craik32054b02014-05-09 13:58:56 -0700721}
722
723static jobject Bitmap_copy(JNIEnv* env, jobject, jlong srcHandle,
724 jint dstConfigHandle, jboolean isMutable) {
John Reckf29ed282015-04-07 07:32:03 -0700725 SkBitmap src;
726 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
Mike Reed1103b322014-07-08 12:36:44 -0400727 SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700728 SkBitmap result;
729 JavaPixelAllocator allocator(env);
730
John Reckf29ed282015-04-07 07:32:03 -0700731 if (!src.copyTo(&result, dstCT, &allocator)) {
Chris Craik32054b02014-05-09 13:58:56 -0700732 return NULL;
733 }
John Reckf29ed282015-04-07 07:32:03 -0700734 Bitmap* bitmap = allocator.getStorageObjAndReset();
735 return GraphicsJNI::createBitmap(env, bitmap,
736 getPremulBitmapCreateFlags(isMutable));
Chris Craik32054b02014-05-09 13:58:56 -0700737}
738
Riley Andrews721ae5f2015-05-11 16:08:22 -0700739static jobject Bitmap_copyAshmem(JNIEnv* env, jobject, jlong srcHandle) {
740 SkBitmap src;
741 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
742 SkBitmap result;
743
744 AshmemPixelAllocator allocator(env);
745 if (!src.copyTo(&result, &allocator)) {
746 return NULL;
747 }
748 Bitmap* bitmap = allocator.getStorageObjAndReset();
749 bitmap->peekAtPixelRef()->setImmutable();
750 jobject ret = GraphicsJNI::createBitmap(env, bitmap, getPremulBitmapCreateFlags(false));
751 return ret;
752}
753
Chris Craik32054b02014-05-09 13:58:56 -0700754static void Bitmap_destructor(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700755 LocalScopedBitmap bitmap(bitmapHandle);
756 bitmap->detachFromJava();
Chris Craik32054b02014-05-09 13:58:56 -0700757}
758
759static jboolean Bitmap_recycle(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700760 LocalScopedBitmap bitmap(bitmapHandle);
761 bitmap->freePixels();
Chris Craik32054b02014-05-09 13:58:56 -0700762 return JNI_TRUE;
763}
764
765static void Bitmap_reconfigure(JNIEnv* env, jobject clazz, jlong bitmapHandle,
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400766 jint width, jint height, jint configHandle, jint allocSize,
767 jboolean requestPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700768 LocalScopedBitmap bitmap(bitmapHandle);
Mike Reed1103b322014-07-08 12:36:44 -0400769 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400770
771 // ARGB_4444 is a deprecated format, convert automatically to 8888
772 if (colorType == kARGB_4444_SkColorType) {
773 colorType = kN32_SkColorType;
774 }
775
776 if (width * height * SkColorTypeBytesPerPixel(colorType) > allocSize) {
Chris Craik32054b02014-05-09 13:58:56 -0700777 // done in native as there's no way to get BytesPerPixel in Java
778 doThrowIAE(env, "Bitmap not large enough to support new configuration");
779 return;
780 }
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400781 SkAlphaType alphaType;
John Reckf29ed282015-04-07 07:32:03 -0700782 if (bitmap->info().colorType() != kRGB_565_SkColorType
783 && bitmap->info().alphaType() == kOpaque_SkAlphaType) {
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400784 // If the original bitmap was set to opaque, keep that setting, unless it
785 // was 565, which is required to be opaque.
786 alphaType = kOpaque_SkAlphaType;
787 } else {
788 // Otherwise respect the premultiplied request.
789 alphaType = requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
790 }
John Reckf29ed282015-04-07 07:32:03 -0700791 bitmap->reconfigure(SkImageInfo::Make(width, height, colorType, alphaType));
Chris Craik32054b02014-05-09 13:58:56 -0700792}
793
794// These must match the int values in Bitmap.java
795enum JavaEncodeFormat {
796 kJPEG_JavaEncodeFormat = 0,
797 kPNG_JavaEncodeFormat = 1,
798 kWEBP_JavaEncodeFormat = 2
799};
800
801static jboolean Bitmap_compress(JNIEnv* env, jobject clazz, jlong bitmapHandle,
802 jint format, jint quality,
803 jobject jstream, jbyteArray jstorage) {
John Reckf29ed282015-04-07 07:32:03 -0700804
805 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700806 SkImageEncoder::Type fm;
807
808 switch (format) {
809 case kJPEG_JavaEncodeFormat:
810 fm = SkImageEncoder::kJPEG_Type;
811 break;
812 case kPNG_JavaEncodeFormat:
813 fm = SkImageEncoder::kPNG_Type;
814 break;
815 case kWEBP_JavaEncodeFormat:
816 fm = SkImageEncoder::kWEBP_Type;
817 break;
818 default:
819 return JNI_FALSE;
820 }
821
John Reckf29ed282015-04-07 07:32:03 -0700822 if (!bitmap.valid()) {
823 return JNI_FALSE;
824 }
825
Chris Craik32054b02014-05-09 13:58:56 -0700826 bool success = false;
Chris Craik32054b02014-05-09 13:58:56 -0700827
John Reckf29ed282015-04-07 07:32:03 -0700828 std::unique_ptr<SkWStream> strm(CreateJavaOutputStreamAdaptor(env, jstream, jstorage));
829 if (!strm.get()) {
830 return JNI_FALSE;
831 }
Chris Craik32054b02014-05-09 13:58:56 -0700832
John Reckf29ed282015-04-07 07:32:03 -0700833 std::unique_ptr<SkImageEncoder> encoder(SkImageEncoder::Create(fm));
834 if (encoder.get()) {
835 SkBitmap skbitmap;
836 bitmap->getSkBitmap(&skbitmap);
837 success = encoder->encodeStream(strm.get(), skbitmap, quality);
Chris Craik32054b02014-05-09 13:58:56 -0700838 }
839 return success ? JNI_TRUE : JNI_FALSE;
840}
841
842static void Bitmap_erase(JNIEnv* env, jobject, jlong bitmapHandle, jint color) {
John Reckf29ed282015-04-07 07:32:03 -0700843 LocalScopedBitmap bitmap(bitmapHandle);
844 SkBitmap skBitmap;
845 bitmap->getSkBitmap(&skBitmap);
846 skBitmap.eraseColor(color);
Chris Craik32054b02014-05-09 13:58:56 -0700847}
848
849static jint Bitmap_rowBytes(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700850 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700851 return static_cast<jint>(bitmap->rowBytes());
852}
853
854static jint Bitmap_config(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700855 LocalScopedBitmap bitmap(bitmapHandle);
856 return GraphicsJNI::colorTypeToLegacyBitmapConfig(bitmap->info().colorType());
Chris Craik32054b02014-05-09 13:58:56 -0700857}
858
859static jint Bitmap_getGenerationId(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700860 LocalScopedBitmap bitmap(bitmapHandle);
John Reckae2e8b42015-05-06 14:55:05 -0700861 return static_cast<jint>(bitmap->peekAtPixelRef()->getGenerationID());
Chris Craik32054b02014-05-09 13:58:56 -0700862}
863
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400864static jboolean Bitmap_isPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700865 LocalScopedBitmap bitmap(bitmapHandle);
866 if (bitmap->info().alphaType() == kPremul_SkAlphaType) {
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400867 return JNI_TRUE;
868 }
869 return JNI_FALSE;
870}
871
Chris Craik32054b02014-05-09 13:58:56 -0700872static jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700873 LocalScopedBitmap bitmap(bitmapHandle);
874 return !bitmap->info().isOpaque() ? JNI_TRUE : JNI_FALSE;
Chris Craik32054b02014-05-09 13:58:56 -0700875}
876
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400877static void Bitmap_setHasAlpha(JNIEnv* env, jobject, jlong bitmapHandle,
878 jboolean hasAlpha, jboolean requestPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700879 LocalScopedBitmap bitmap(bitmapHandle);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400880 if (hasAlpha) {
John Reck0781a2f2015-05-27 16:29:17 -0700881 bitmap->setAlphaType(
John Reckf29ed282015-04-07 07:32:03 -0700882 requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType);
Chris Craik32054b02014-05-09 13:58:56 -0700883 } else {
John Reck0781a2f2015-05-27 16:29:17 -0700884 bitmap->setAlphaType(kOpaque_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400885 }
886}
887
888static void Bitmap_setPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle,
889 jboolean isPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700890 LocalScopedBitmap bitmap(bitmapHandle);
891 if (!bitmap->info().isOpaque()) {
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400892 if (isPremul) {
John Reck0781a2f2015-05-27 16:29:17 -0700893 bitmap->setAlphaType(kPremul_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400894 } else {
John Reck0781a2f2015-05-27 16:29:17 -0700895 bitmap->setAlphaType(kUnpremul_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400896 }
Chris Craik32054b02014-05-09 13:58:56 -0700897 }
898}
899
900static jboolean Bitmap_hasMipMap(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700901 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700902 return bitmap->hasHardwareMipMap() ? JNI_TRUE : JNI_FALSE;
903}
904
905static void Bitmap_setHasMipMap(JNIEnv* env, jobject, jlong bitmapHandle,
906 jboolean hasMipMap) {
John Reckf29ed282015-04-07 07:32:03 -0700907 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700908 bitmap->setHasHardwareMipMap(hasMipMap);
909}
910
911///////////////////////////////////////////////////////////////////////////////
912
913static jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) {
914 if (parcel == NULL) {
915 SkDebugf("-------- unparcel parcel is NULL\n");
916 return NULL;
917 }
918
919 android::Parcel* p = android::parcelForJavaObject(env, parcel);
920
Mike Reedb9330552014-06-16 17:31:48 -0400921 const bool isMutable = p->readInt32() != 0;
922 const SkColorType colorType = (SkColorType)p->readInt32();
923 const SkAlphaType alphaType = (SkAlphaType)p->readInt32();
924 const int width = p->readInt32();
925 const int height = p->readInt32();
926 const int rowBytes = p->readInt32();
927 const int density = p->readInt32();
Chris Craik32054b02014-05-09 13:58:56 -0700928
Mike Reedb9330552014-06-16 17:31:48 -0400929 if (kN32_SkColorType != colorType &&
930 kRGB_565_SkColorType != colorType &&
931 kARGB_4444_SkColorType != colorType &&
932 kIndex_8_SkColorType != colorType &&
933 kAlpha_8_SkColorType != colorType) {
934 SkDebugf("Bitmap_createFromParcel unknown colortype: %d\n", colorType);
Chris Craik32054b02014-05-09 13:58:56 -0700935 return NULL;
936 }
937
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400938 std::unique_ptr<SkBitmap> bitmap(new SkBitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700939
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400940 if (!bitmap->setInfo(SkImageInfo::Make(width, height, colorType, alphaType), rowBytes)) {
941 return NULL;
942 }
Chris Craik32054b02014-05-09 13:58:56 -0700943
944 SkColorTable* ctable = NULL;
Mike Reedb9330552014-06-16 17:31:48 -0400945 if (colorType == kIndex_8_SkColorType) {
Chris Craik32054b02014-05-09 13:58:56 -0700946 int count = p->readInt32();
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400947 if (count < 0 || count > 256) {
948 // The data is corrupt, since SkColorTable enforces a value between 0 and 256,
949 // inclusive.
950 return NULL;
951 }
Chris Craik32054b02014-05-09 13:58:56 -0700952 if (count > 0) {
953 size_t size = count * sizeof(SkPMColor);
954 const SkPMColor* src = (const SkPMColor*)p->readInplace(size);
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400955 if (src == NULL) {
956 return NULL;
957 }
Chris Craik32054b02014-05-09 13:58:56 -0700958 ctable = new SkColorTable(src, count);
959 }
960 }
961
Riley Andrews39d7f302014-11-13 17:43:25 -0800962 int fd = p->readFileDescriptor();
963 int dupFd = dup(fd);
964 if (dupFd < 0) {
Chris Craik32054b02014-05-09 13:58:56 -0700965 SkSafeUnref(ctable);
Riley Andrews39d7f302014-11-13 17:43:25 -0800966 doThrowRE(env, "Could not dup parcel fd.");
Chris Craik32054b02014-05-09 13:58:56 -0700967 return NULL;
968 }
969
Riley Andrews39d7f302014-11-13 17:43:25 -0800970 bool readOnlyMapping = !isMutable;
971 Bitmap* nativeBitmap = GraphicsJNI::mapAshmemPixelRef(env, bitmap.get(),
972 ctable, dupFd, readOnlyMapping);
Chris Craik32054b02014-05-09 13:58:56 -0700973 SkSafeUnref(ctable);
Riley Andrews39d7f302014-11-13 17:43:25 -0800974 if (!nativeBitmap) {
975 close(dupFd);
976 doThrowRE(env, "Could not allocate ashmem pixel ref.");
Chris Craik32054b02014-05-09 13:58:56 -0700977 return NULL;
978 }
Riley Andrews39d7f302014-11-13 17:43:25 -0800979 bitmap->pixelRef()->setImmutable();
Chris Craik32054b02014-05-09 13:58:56 -0700980
John Reckf29ed282015-04-07 07:32:03 -0700981 return GraphicsJNI::createBitmap(env, nativeBitmap,
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400982 getPremulBitmapCreateFlags(isMutable), NULL, NULL, density);
Chris Craik32054b02014-05-09 13:58:56 -0700983}
984
Riley Andrews39d7f302014-11-13 17:43:25 -0800985class Ashmem {
986public:
987 Ashmem(size_t sz, bool removeWritePerm) : mSize(sz) {
988 int fd = -1;
989 void *addr = nullptr;
990
991 // Create new ashmem region with read/write priv
992 fd = ashmem_create_region("bitmap", sz);
993 if (fd < 0) {
994 goto error;
995 }
996 addr = mmap(nullptr, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
997 if (addr == MAP_FAILED) {
998 goto error;
999 }
1000 // If requested, remove the ability to make additional writeable to
1001 // this memory.
1002 if (removeWritePerm) {
1003 if (ashmem_set_prot_region(fd, PROT_READ) < 0) {
1004 goto error;
1005 }
1006 }
1007 mFd = fd;
1008 mPtr = addr;
1009 return;
1010error:
1011 if (fd >= 0) {
1012 close(fd);
1013 }
1014 if (addr) {
1015 munmap(addr, sz);
1016 }
1017 }
1018 ~Ashmem() {
1019 if (mPtr) {
1020 close(mFd);
1021 munmap(mPtr, mSize);
1022 }
1023 }
1024 void *getPtr() const { return mPtr; }
1025 int getFd() const { return mFd; }
1026private:
1027 int mFd = -1;
1028 int mSize;
1029 void* mPtr = nullptr;
1030};
1031
Chris Craik32054b02014-05-09 13:58:56 -07001032static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject,
1033 jlong bitmapHandle,
1034 jboolean isMutable, jint density,
1035 jobject parcel) {
Chris Craik32054b02014-05-09 13:58:56 -07001036 if (parcel == NULL) {
1037 SkDebugf("------- writeToParcel null parcel\n");
1038 return JNI_FALSE;
1039 }
1040
1041 android::Parcel* p = android::parcelForJavaObject(env, parcel);
John Reckf29ed282015-04-07 07:32:03 -07001042 SkBitmap bitmap;
Riley Andrews39d7f302014-11-13 17:43:25 -08001043
1044 android::Bitmap* androidBitmap = reinterpret_cast<Bitmap*>(bitmapHandle);
1045 androidBitmap->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001046
1047 p->writeInt32(isMutable);
John Reckf29ed282015-04-07 07:32:03 -07001048 p->writeInt32(bitmap.colorType());
1049 p->writeInt32(bitmap.alphaType());
1050 p->writeInt32(bitmap.width());
1051 p->writeInt32(bitmap.height());
1052 p->writeInt32(bitmap.rowBytes());
Chris Craik32054b02014-05-09 13:58:56 -07001053 p->writeInt32(density);
1054
John Reckf29ed282015-04-07 07:32:03 -07001055 if (bitmap.colorType() == kIndex_8_SkColorType) {
1056 SkColorTable* ctable = bitmap.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001057 if (ctable != NULL) {
1058 int count = ctable->count();
1059 p->writeInt32(count);
1060 memcpy(p->writeInplace(count * sizeof(SkPMColor)),
Mike Reed71487eb2014-11-19 16:13:20 -05001061 ctable->readColors(), count * sizeof(SkPMColor));
Chris Craik32054b02014-05-09 13:58:56 -07001062 } else {
1063 p->writeInt32(0); // indicate no ctable
1064 }
1065 }
1066
Riley Andrews39d7f302014-11-13 17:43:25 -08001067 bool ashmemSrc = androidBitmap->getAshmemFd() >= 0;
1068 if (ashmemSrc && !isMutable) {
1069 p->writeDupFileDescriptor(androidBitmap->getAshmemFd());
Chris Craik32054b02014-05-09 13:58:56 -07001070 } else {
Riley Andrews39d7f302014-11-13 17:43:25 -08001071 Ashmem dstAshmem(bitmap.getSize(), !isMutable);
1072 if (!dstAshmem.getPtr()) {
1073 doThrowRE(env, "Could not allocate ashmem for new bitmap.");
1074 return JNI_FALSE;
1075 }
Chris Craik32054b02014-05-09 13:58:56 -07001076
Riley Andrews39d7f302014-11-13 17:43:25 -08001077 bitmap.lockPixels();
1078 const void* pSrc = bitmap.getPixels();
1079 if (pSrc == NULL) {
1080 memset(dstAshmem.getPtr(), 0, bitmap.getSize());
1081 } else {
1082 memcpy(dstAshmem.getPtr(), pSrc, bitmap.getSize());
1083 }
1084 bitmap.unlockPixels();
1085 p->writeDupFileDescriptor(dstAshmem.getFd());
1086 }
Chris Craik32054b02014-05-09 13:58:56 -07001087 return JNI_TRUE;
1088}
1089
1090static jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz,
1091 jlong srcHandle, jlong paintHandle,
1092 jintArray offsetXY) {
John Reckf29ed282015-04-07 07:32:03 -07001093 SkBitmap src;
1094 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -04001095 const android::Paint* paint = reinterpret_cast<android::Paint*>(paintHandle);
Chris Craik32054b02014-05-09 13:58:56 -07001096 SkIPoint offset;
John Reckf29ed282015-04-07 07:32:03 -07001097 SkBitmap dst;
Chris Craik32054b02014-05-09 13:58:56 -07001098 JavaPixelAllocator allocator(env);
1099
John Reckf29ed282015-04-07 07:32:03 -07001100 src.extractAlpha(&dst, paint, &allocator, &offset);
Chris Craik32054b02014-05-09 13:58:56 -07001101 // If Skia can't allocate pixels for destination bitmap, it resets
1102 // it, that is set its pixels buffer to NULL, and zero width and height.
John Reckf29ed282015-04-07 07:32:03 -07001103 if (dst.getPixels() == NULL && src.getPixels() != NULL) {
Chris Craik32054b02014-05-09 13:58:56 -07001104 doThrowOOME(env, "failed to allocate pixels for alpha");
1105 return NULL;
1106 }
1107 if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) {
1108 int* array = env->GetIntArrayElements(offsetXY, NULL);
1109 array[0] = offset.fX;
1110 array[1] = offset.fY;
1111 env->ReleaseIntArrayElements(offsetXY, array, 0);
1112 }
1113
John Reckf29ed282015-04-07 07:32:03 -07001114 return GraphicsJNI::createBitmap(env, allocator.getStorageObjAndReset(),
1115 getPremulBitmapCreateFlags(true));
Chris Craik32054b02014-05-09 13:58:56 -07001116}
1117
1118///////////////////////////////////////////////////////////////////////////////
1119
1120static jint Bitmap_getPixel(JNIEnv* env, jobject, jlong bitmapHandle,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001121 jint x, jint y) {
John Reckf29ed282015-04-07 07:32:03 -07001122 SkBitmap bitmap;
1123 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1124 SkAutoLockPixels alp(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001125
John Reckf29ed282015-04-07 07:32:03 -07001126 ToColorProc proc = ChooseToColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001127 if (NULL == proc) {
1128 return 0;
1129 }
John Reckf29ed282015-04-07 07:32:03 -07001130 const void* src = bitmap.getAddr(x, y);
Chris Craik32054b02014-05-09 13:58:56 -07001131 if (NULL == src) {
1132 return 0;
1133 }
1134
1135 SkColor dst[1];
John Reckf29ed282015-04-07 07:32:03 -07001136 proc(dst, src, 1, bitmap.getColorTable());
Chris Craik32054b02014-05-09 13:58:56 -07001137 return static_cast<jint>(dst[0]);
1138}
1139
1140static void Bitmap_getPixels(JNIEnv* env, jobject, jlong bitmapHandle,
1141 jintArray pixelArray, jint offset, jint stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001142 jint x, jint y, jint width, jint height) {
John Reckf29ed282015-04-07 07:32:03 -07001143 SkBitmap bitmap;
1144 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1145 SkAutoLockPixels alp(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001146
John Reckf29ed282015-04-07 07:32:03 -07001147 ToColorProc proc = ChooseToColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001148 if (NULL == proc) {
1149 return;
1150 }
John Reckf29ed282015-04-07 07:32:03 -07001151 const void* src = bitmap.getAddr(x, y);
Chris Craik32054b02014-05-09 13:58:56 -07001152 if (NULL == src) {
1153 return;
1154 }
1155
John Reckf29ed282015-04-07 07:32:03 -07001156 SkColorTable* ctable = bitmap.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001157 jint* dst = env->GetIntArrayElements(pixelArray, NULL);
1158 SkColor* d = (SkColor*)dst + offset;
1159 while (--height >= 0) {
1160 proc(d, src, width, ctable);
1161 d += stride;
John Reckf29ed282015-04-07 07:32:03 -07001162 src = (void*)((const char*)src + bitmap.rowBytes());
Chris Craik32054b02014-05-09 13:58:56 -07001163 }
1164 env->ReleaseIntArrayElements(pixelArray, dst, 0);
1165}
1166
1167///////////////////////////////////////////////////////////////////////////////
1168
1169static void Bitmap_setPixel(JNIEnv* env, jobject, jlong bitmapHandle,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001170 jint x, jint y, jint colorHandle) {
John Reckf29ed282015-04-07 07:32:03 -07001171 SkBitmap bitmap;
1172 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001173 SkColor color = static_cast<SkColor>(colorHandle);
John Reckf29ed282015-04-07 07:32:03 -07001174 SkAutoLockPixels alp(bitmap);
1175 if (NULL == bitmap.getPixels()) {
Chris Craik32054b02014-05-09 13:58:56 -07001176 return;
1177 }
1178
John Reckf29ed282015-04-07 07:32:03 -07001179 FromColorProc proc = ChooseFromColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001180 if (NULL == proc) {
1181 return;
1182 }
1183
John Reckf29ed282015-04-07 07:32:03 -07001184 proc(bitmap.getAddr(x, y), &color, 1, x, y);
1185 bitmap.notifyPixelsChanged();
Chris Craik32054b02014-05-09 13:58:56 -07001186}
1187
1188static void Bitmap_setPixels(JNIEnv* env, jobject, jlong bitmapHandle,
1189 jintArray pixelArray, jint offset, jint stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001190 jint x, jint y, jint width, jint height) {
John Reckf29ed282015-04-07 07:32:03 -07001191 SkBitmap bitmap;
1192 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001193 GraphicsJNI::SetPixels(env, pixelArray, offset, stride,
John Reckf29ed282015-04-07 07:32:03 -07001194 x, y, width, height, bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001195}
1196
1197static void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject,
1198 jlong bitmapHandle, jobject jbuffer) {
John Reckf29ed282015-04-07 07:32:03 -07001199 SkBitmap bitmap;
1200 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1201 SkAutoLockPixels alp(bitmap);
1202 const void* src = bitmap.getPixels();
Chris Craik32054b02014-05-09 13:58:56 -07001203
1204 if (NULL != src) {
1205 android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE);
1206
1207 // the java side has already checked that buffer is large enough
John Reckf29ed282015-04-07 07:32:03 -07001208 memcpy(abp.pointer(), src, bitmap.getSize());
Chris Craik32054b02014-05-09 13:58:56 -07001209 }
1210}
1211
1212static void Bitmap_copyPixelsFromBuffer(JNIEnv* env, jobject,
1213 jlong bitmapHandle, jobject jbuffer) {
John Reckf29ed282015-04-07 07:32:03 -07001214 SkBitmap bitmap;
1215 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1216 SkAutoLockPixels alp(bitmap);
1217 void* dst = bitmap.getPixels();
Chris Craik32054b02014-05-09 13:58:56 -07001218
1219 if (NULL != dst) {
1220 android::AutoBufferPointer abp(env, jbuffer, JNI_FALSE);
1221 // the java side has already checked that buffer is large enough
John Reckf29ed282015-04-07 07:32:03 -07001222 memcpy(dst, abp.pointer(), bitmap.getSize());
1223 bitmap.notifyPixelsChanged();
Chris Craik32054b02014-05-09 13:58:56 -07001224 }
1225}
1226
1227static jboolean Bitmap_sameAs(JNIEnv* env, jobject, jlong bm0Handle,
1228 jlong bm1Handle) {
John Reckf29ed282015-04-07 07:32:03 -07001229 SkBitmap bm0;
1230 SkBitmap bm1;
1231 reinterpret_cast<Bitmap*>(bm0Handle)->getSkBitmap(&bm0);
1232 reinterpret_cast<Bitmap*>(bm1Handle)->getSkBitmap(&bm1);
1233 if (bm0.width() != bm1.width() ||
1234 bm0.height() != bm1.height() ||
1235 bm0.colorType() != bm1.colorType()) {
Chris Craik32054b02014-05-09 13:58:56 -07001236 return JNI_FALSE;
1237 }
1238
John Reckf29ed282015-04-07 07:32:03 -07001239 SkAutoLockPixels alp0(bm0);
1240 SkAutoLockPixels alp1(bm1);
Chris Craik32054b02014-05-09 13:58:56 -07001241
1242 // if we can't load the pixels, return false
John Reckf29ed282015-04-07 07:32:03 -07001243 if (NULL == bm0.getPixels() || NULL == bm1.getPixels()) {
Chris Craik32054b02014-05-09 13:58:56 -07001244 return JNI_FALSE;
1245 }
1246
John Reckf29ed282015-04-07 07:32:03 -07001247 if (bm0.colorType() == kIndex_8_SkColorType) {
1248 SkColorTable* ct0 = bm0.getColorTable();
1249 SkColorTable* ct1 = bm1.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001250 if (NULL == ct0 || NULL == ct1) {
1251 return JNI_FALSE;
1252 }
1253 if (ct0->count() != ct1->count()) {
1254 return JNI_FALSE;
1255 }
1256
Chris Craik32054b02014-05-09 13:58:56 -07001257 const size_t size = ct0->count() * sizeof(SkPMColor);
Mike Reed71487eb2014-11-19 16:13:20 -05001258 if (memcmp(ct0->readColors(), ct1->readColors(), size) != 0) {
Chris Craik32054b02014-05-09 13:58:56 -07001259 return JNI_FALSE;
1260 }
1261 }
1262
1263 // now compare each scanline. We can't do the entire buffer at once,
1264 // since we don't care about the pixel values that might extend beyond
1265 // the width (since the scanline might be larger than the logical width)
John Reckf29ed282015-04-07 07:32:03 -07001266 const int h = bm0.height();
1267 const size_t size = bm0.width() * bm0.bytesPerPixel();
Chris Craik32054b02014-05-09 13:58:56 -07001268 for (int y = 0; y < h; y++) {
henry.uh_chen53001ca2014-07-03 20:40:22 +08001269 // SkBitmap::getAddr(int, int) may return NULL due to unrecognized config
1270 // (ex: kRLE_Index8_Config). This will cause memcmp method to crash. Since bm0
1271 // and bm1 both have pixel data() (have passed NULL == getPixels() check),
1272 // those 2 bitmaps should be valid (only unrecognized), we return JNI_FALSE
1273 // to warn user those 2 unrecognized config bitmaps may be different.
John Reckf29ed282015-04-07 07:32:03 -07001274 void *bm0Addr = bm0.getAddr(0, y);
1275 void *bm1Addr = bm1.getAddr(0, y);
henry.uh_chen53001ca2014-07-03 20:40:22 +08001276
1277 if(bm0Addr == NULL || bm1Addr == NULL) {
1278 return JNI_FALSE;
1279 }
1280
1281 if (memcmp(bm0Addr, bm1Addr, size) != 0) {
Chris Craik32054b02014-05-09 13:58:56 -07001282 return JNI_FALSE;
1283 }
1284 }
1285 return JNI_TRUE;
1286}
1287
John Reckc6e2e8f2015-04-15 13:24:47 -07001288static jlong Bitmap_refPixelRef(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -07001289 LocalScopedBitmap bitmap(bitmapHandle);
John Reckae2e8b42015-05-06 14:55:05 -07001290 SkPixelRef* pixelRef = bitmap.valid() ? bitmap->peekAtPixelRef() : nullptr;
John Reckc6e2e8f2015-04-15 13:24:47 -07001291 SkSafeRef(pixelRef);
1292 return reinterpret_cast<jlong>(pixelRef);
1293}
1294
Chris Craik32054b02014-05-09 13:58:56 -07001295///////////////////////////////////////////////////////////////////////////////
1296
Chris Craik32054b02014-05-09 13:58:56 -07001297static JNINativeMethod gBitmapMethods[] = {
1298 { "nativeCreate", "([IIIIIIZ)Landroid/graphics/Bitmap;",
1299 (void*)Bitmap_creator },
1300 { "nativeCopy", "(JIZ)Landroid/graphics/Bitmap;",
1301 (void*)Bitmap_copy },
Riley Andrews721ae5f2015-05-11 16:08:22 -07001302 { "nativeCopyAshmem", "(J)Landroid/graphics/Bitmap;",
1303 (void*)Bitmap_copyAshmem },
Chris Craik32054b02014-05-09 13:58:56 -07001304 { "nativeDestructor", "(J)V", (void*)Bitmap_destructor },
1305 { "nativeRecycle", "(J)Z", (void*)Bitmap_recycle },
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -04001306 { "nativeReconfigure", "(JIIIIZ)V", (void*)Bitmap_reconfigure },
Chris Craik32054b02014-05-09 13:58:56 -07001307 { "nativeCompress", "(JIILjava/io/OutputStream;[B)Z",
1308 (void*)Bitmap_compress },
1309 { "nativeErase", "(JI)V", (void*)Bitmap_erase },
1310 { "nativeRowBytes", "(J)I", (void*)Bitmap_rowBytes },
1311 { "nativeConfig", "(J)I", (void*)Bitmap_config },
1312 { "nativeHasAlpha", "(J)Z", (void*)Bitmap_hasAlpha },
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001313 { "nativeIsPremultiplied", "(J)Z", (void*)Bitmap_isPremultiplied},
1314 { "nativeSetHasAlpha", "(JZZ)V", (void*)Bitmap_setHasAlpha},
1315 { "nativeSetPremultiplied", "(JZ)V", (void*)Bitmap_setPremultiplied},
Chris Craik32054b02014-05-09 13:58:56 -07001316 { "nativeHasMipMap", "(J)Z", (void*)Bitmap_hasMipMap },
1317 { "nativeSetHasMipMap", "(JZ)V", (void*)Bitmap_setHasMipMap },
1318 { "nativeCreateFromParcel",
1319 "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;",
1320 (void*)Bitmap_createFromParcel },
1321 { "nativeWriteToParcel", "(JZILandroid/os/Parcel;)Z",
1322 (void*)Bitmap_writeToParcel },
1323 { "nativeExtractAlpha", "(JJ[I)Landroid/graphics/Bitmap;",
1324 (void*)Bitmap_extractAlpha },
1325 { "nativeGenerationId", "(J)I", (void*)Bitmap_getGenerationId },
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001326 { "nativeGetPixel", "(JII)I", (void*)Bitmap_getPixel },
1327 { "nativeGetPixels", "(J[IIIIIII)V", (void*)Bitmap_getPixels },
1328 { "nativeSetPixel", "(JIII)V", (void*)Bitmap_setPixel },
1329 { "nativeSetPixels", "(J[IIIIIII)V", (void*)Bitmap_setPixels },
Chris Craik32054b02014-05-09 13:58:56 -07001330 { "nativeCopyPixelsToBuffer", "(JLjava/nio/Buffer;)V",
1331 (void*)Bitmap_copyPixelsToBuffer },
1332 { "nativeCopyPixelsFromBuffer", "(JLjava/nio/Buffer;)V",
1333 (void*)Bitmap_copyPixelsFromBuffer },
1334 { "nativeSameAs", "(JJ)Z", (void*)Bitmap_sameAs },
John Reckc6e2e8f2015-04-15 13:24:47 -07001335 { "nativeRefPixelRef", "(J)J", (void*)Bitmap_refPixelRef },
Chris Craik32054b02014-05-09 13:58:56 -07001336};
1337
Chris Craik32054b02014-05-09 13:58:56 -07001338int register_android_graphics_Bitmap(JNIEnv* env)
1339{
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001340 return android::RegisterMethodsOrDie(env, "android/graphics/Bitmap", gBitmapMethods,
1341 NELEM(gBitmapMethods));
Chris Craik32054b02014-05-09 13:58:56 -07001342}