blob: 04b9a95bf3cf214e730d40a88d93e4ce162eedc2 [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
49 void reconfigure(const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable) {
50 if (kIndex_8_SkColorType != info.colorType()) {
51 ctable = nullptr;
52 }
53 mRowBytes = rowBytes;
54 if (mColorTable != ctable) {
55 SkSafeUnref(mColorTable);
56 mColorTable = ctable;
57 SkSafeRef(mColorTable);
58 }
59 // Dirty hack is dirty
60 // TODO: Figure something out here, Skia's current design makes this
61 // really hard to work with. Skia really, really wants immutable objects,
62 // but with the nested-ref-count hackery going on that's just not
63 // feasible without going insane trying to figure it out
64 SkImageInfo* myInfo = const_cast<SkImageInfo*>(&this->info());
65 *myInfo = info;
66
67 // Docs say to only call this in the ctor, but we're going to call
68 // it anyway even if this isn't always the ctor.
69 // TODO: Fix this too as part of the above TODO
70 setPreLocked(mStorage, mRowBytes, mColorTable);
71 }
72
73 // Can't mark as override since SkPixelRef::rowBytes isn't virtual
74 // but that's OK since we just want BitmapWrapper to be able to rely
75 // on calling rowBytes() on an unlocked pixelref, which it will be
76 // doing on a WrappedPixelRef type, not a SkPixelRef, so static
77 // dispatching will do what we want.
78 size_t rowBytes() const { return mRowBytes; }
79 SkColorTable* colorTable() const { return mColorTable; }
80
81 bool hasHardwareMipMap() const {
82 return mHasHardwareMipMap;
83 }
84
85 void setHasHardwareMipMap(bool hasMipMap) {
86 mHasHardwareMipMap = hasMipMap;
87 }
88
89protected:
90 virtual bool onNewLockPixels(LockRec* rec) override {
91 rec->fPixels = mStorage;
92 rec->fRowBytes = mRowBytes;
93 rec->fColorTable = mColorTable;
94 return true;
95 }
96
97 virtual void onUnlockPixels() override {
98 // nothing
99 }
100
101 virtual size_t getAllocatedSizeInBytes() const override {
102 return info().getSafeSize(mRowBytes);
103 }
104
105private:
106 Bitmap& mBitmap;
107 void* mStorage;
108 size_t mRowBytes = 0;
109 SkColorTable* mColorTable = nullptr;
110 bool mHasHardwareMipMap = false;
111
112 virtual void internal_dispose() const override {
113 mBitmap.onStrongRefDestroyed();
114 }
115};
116
117Bitmap::Bitmap(JNIEnv* env, jbyteArray storageObj, void* address,
118 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
119 : mPixelStorageType(PixelStorageType::Java) {
120 env->GetJavaVM(&mPixelStorage.java.jvm);
121 mPixelStorage.java.jweakRef = env->NewWeakGlobalRef(storageObj);
122 mPixelStorage.java.jstrongRef = nullptr;
123 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
124 // Note: this will trigger a call to onStrongRefDestroyed(), but
125 // we want the pixel ref to have a ref count of 0 at this point
126 mPixelRef->unref();
127}
128
129Bitmap::Bitmap(void* address, void* context, FreeFunc freeFunc,
130 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
131 : mPixelStorageType(PixelStorageType::External) {
132 mPixelStorage.external.address = address;
133 mPixelStorage.external.context = context;
134 mPixelStorage.external.freeFunc = freeFunc;
135 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
136 // Note: this will trigger a call to onStrongRefDestroyed(), but
137 // we want the pixel ref to have a ref count of 0 at this point
138 mPixelRef->unref();
139}
140
Riley Andrews39d7f302014-11-13 17:43:25 -0800141Bitmap::Bitmap(void* address, int fd,
142 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
143 : mPixelStorageType(PixelStorageType::Ashmem) {
144 mPixelStorage.ashmem.address = address;
145 mPixelStorage.ashmem.fd = fd;
146 mPixelStorage.ashmem.size = ashmem_get_size_region(fd);
147 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
148 // Note: this will trigger a call to onStrongRefDestroyed(), but
149 // we want the pixel ref to have a ref count of 0 at this point
150 mPixelRef->unref();
151}
John Reckf29ed282015-04-07 07:32:03 -0700152Bitmap::~Bitmap() {
153 doFreePixels();
154}
155
156void Bitmap::freePixels() {
157 AutoMutex _lock(mLock);
158 if (mPinnedRefCount == 0) {
159 doFreePixels();
160 mPixelStorageType = PixelStorageType::Invalid;
161 }
162}
163
164void Bitmap::doFreePixels() {
165 switch (mPixelStorageType) {
166 case PixelStorageType::Invalid:
167 // already free'd, nothing to do
168 break;
169 case PixelStorageType::External:
170 mPixelStorage.external.freeFunc(mPixelStorage.external.address,
171 mPixelStorage.external.context);
172 break;
Riley Andrews39d7f302014-11-13 17:43:25 -0800173 case PixelStorageType::Ashmem:
174 munmap(mPixelStorage.ashmem.address, mPixelStorage.ashmem.size);
175 close(mPixelStorage.ashmem.fd);
176 break;
John Reckf29ed282015-04-07 07:32:03 -0700177 case PixelStorageType::Java:
178 JNIEnv* env = jniEnv();
179 LOG_ALWAYS_FATAL_IF(mPixelStorage.java.jstrongRef,
180 "Deleting a bitmap wrapper while there are outstanding strong "
181 "references! mPinnedRefCount = %d", mPinnedRefCount);
182 env->DeleteWeakGlobalRef(mPixelStorage.java.jweakRef);
183 break;
184 }
185
186 if (android::uirenderer::Caches::hasInstance()) {
187 android::uirenderer::Caches::getInstance().textureCache.releaseTexture(
188 mPixelRef->getStableID());
189 }
190}
191
192bool Bitmap::hasHardwareMipMap() {
193 return mPixelRef->hasHardwareMipMap();
194}
195
196void Bitmap::setHasHardwareMipMap(bool hasMipMap) {
197 mPixelRef->setHasHardwareMipMap(hasMipMap);
198}
199
Riley Andrews39d7f302014-11-13 17:43:25 -0800200int Bitmap::getAshmemFd() const {
201 switch (mPixelStorageType) {
202 case PixelStorageType::Ashmem:
203 return mPixelStorage.ashmem.fd;
204 default:
205 return -1;
206 }
207}
208
John Reckf29ed282015-04-07 07:32:03 -0700209const SkImageInfo& Bitmap::info() const {
210 assertValid();
211 return mPixelRef->info();
212}
213
214size_t Bitmap::rowBytes() const {
215 return mPixelRef->rowBytes();
216}
217
John Reckae2e8b42015-05-06 14:55:05 -0700218SkPixelRef* Bitmap::peekAtPixelRef() const {
John Reckf29ed282015-04-07 07:32:03 -0700219 assertValid();
220 return mPixelRef.get();
221}
222
John Reckae2e8b42015-05-06 14:55:05 -0700223SkPixelRef* Bitmap::refPixelRef() {
224 assertValid();
225 android::AutoMutex _lock(mLock);
226 return refPixelRefLocked();
227}
228
229SkPixelRef* Bitmap::refPixelRefLocked() {
230 mPixelRef->ref();
231 if (mPixelRef->unique()) {
232 // We just restored this from 0, pin the pixels and inc the strong count
233 // Note that there *might be* an incoming onStrongRefDestroyed from whatever
234 // last unref'd
235 pinPixelsLocked();
236 mPinnedRefCount++;
237 }
238 return mPixelRef.get();
239}
240
John Reckf29ed282015-04-07 07:32:03 -0700241void Bitmap::reconfigure(const SkImageInfo& info, size_t rowBytes,
242 SkColorTable* ctable) {
John Reckae2e8b42015-05-06 14:55:05 -0700243 {
244 android::AutoMutex _lock(mLock);
245 if (mPinnedRefCount) {
246 ALOGW("Called reconfigure on a bitmap that is in use! This may"
247 " cause graphical corruption!");
248 }
249 }
John Reckf29ed282015-04-07 07:32:03 -0700250 mPixelRef->reconfigure(info, rowBytes, ctable);
251}
252
253void Bitmap::reconfigure(const SkImageInfo& info) {
Derek Sollenberger2a94a102015-05-07 13:56:14 -0400254 reconfigure(info, info.minRowBytes(), nullptr);
John Reckf29ed282015-04-07 07:32:03 -0700255}
256
257void Bitmap::detachFromJava() {
258 bool disposeSelf;
259 {
260 android::AutoMutex _lock(mLock);
261 mAttachedToJava = false;
262 disposeSelf = shouldDisposeSelfLocked();
263 }
264 if (disposeSelf) {
265 delete this;
266 }
267}
268
269bool Bitmap::shouldDisposeSelfLocked() {
270 return mPinnedRefCount == 0 && !mAttachedToJava;
271}
272
273JNIEnv* Bitmap::jniEnv() {
274 JNIEnv* env;
275 auto success = mPixelStorage.java.jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
276 LOG_ALWAYS_FATAL_IF(success != JNI_OK,
277 "Failed to get JNIEnv* from JVM: %p", mPixelStorage.java.jvm);
278 return env;
279}
280
281void Bitmap::onStrongRefDestroyed() {
282 bool disposeSelf = false;
283 {
284 android::AutoMutex _lock(mLock);
285 if (mPinnedRefCount > 0) {
286 mPinnedRefCount--;
287 if (mPinnedRefCount == 0) {
288 unpinPixelsLocked();
289 disposeSelf = shouldDisposeSelfLocked();
290 }
291 }
292 }
293 if (disposeSelf) {
294 delete this;
295 }
296}
297
298void Bitmap::pinPixelsLocked() {
299 switch (mPixelStorageType) {
300 case PixelStorageType::Invalid:
301 LOG_ALWAYS_FATAL("Cannot pin invalid pixels!");
302 break;
303 case PixelStorageType::External:
Riley Andrews39d7f302014-11-13 17:43:25 -0800304 case PixelStorageType::Ashmem:
John Reckf29ed282015-04-07 07:32:03 -0700305 // Nothing to do
306 break;
307 case PixelStorageType::Java: {
308 JNIEnv* env = jniEnv();
309 if (!mPixelStorage.java.jstrongRef) {
310 mPixelStorage.java.jstrongRef = reinterpret_cast<jbyteArray>(
311 env->NewGlobalRef(mPixelStorage.java.jweakRef));
312 if (!mPixelStorage.java.jstrongRef) {
313 LOG_ALWAYS_FATAL("Failed to acquire strong reference to pixels");
314 }
315 }
316 break;
317 }
318 }
319}
320
321void Bitmap::unpinPixelsLocked() {
322 switch (mPixelStorageType) {
323 case PixelStorageType::Invalid:
324 LOG_ALWAYS_FATAL("Cannot unpin invalid pixels!");
325 break;
326 case PixelStorageType::External:
Riley Andrews39d7f302014-11-13 17:43:25 -0800327 case PixelStorageType::Ashmem:
John Reckf29ed282015-04-07 07:32:03 -0700328 // Don't need to do anything
329 break;
330 case PixelStorageType::Java: {
331 JNIEnv* env = jniEnv();
332 if (mPixelStorage.java.jstrongRef) {
333 env->DeleteGlobalRef(mPixelStorage.java.jstrongRef);
334 mPixelStorage.java.jstrongRef = nullptr;
335 }
336 break;
337 }
338 }
339}
340
341void Bitmap::getSkBitmap(SkBitmap* outBitmap) {
342 assertValid();
343 android::AutoMutex _lock(mLock);
John Reckf29ed282015-04-07 07:32:03 -0700344 // Safe because mPixelRef is a WrappedPixelRef type, otherwise rowBytes()
345 // would require locking the pixels first.
346 outBitmap->setInfo(mPixelRef->info(), mPixelRef->rowBytes());
John Reckae2e8b42015-05-06 14:55:05 -0700347 outBitmap->setPixelRef(refPixelRefLocked())->unref();
John Reckf29ed282015-04-07 07:32:03 -0700348 outBitmap->setHasHardwareMipMap(hasHardwareMipMap());
349}
350
351void Bitmap::assertValid() const {
352 LOG_ALWAYS_FATAL_IF(mPixelStorageType == PixelStorageType::Invalid,
353 "Error, cannot access an invalid/free'd bitmap here!");
354}
355
356} // namespace android
357
358using namespace android;
359
360// Convenience class that does not take a global ref on the pixels, relying
361// on the caller already having a local JNI ref
362class LocalScopedBitmap {
363public:
364 LocalScopedBitmap(jlong bitmapHandle)
365 : mBitmap(reinterpret_cast<Bitmap*>(bitmapHandle)) {}
366
367 Bitmap* operator->() {
368 return mBitmap;
369 }
370
371 void* pixels() {
John Reckae2e8b42015-05-06 14:55:05 -0700372 return mBitmap->peekAtPixelRef()->pixels();
John Reckf29ed282015-04-07 07:32:03 -0700373 }
374
375 bool valid() {
376 return mBitmap && mBitmap->valid();
377 }
378
379private:
380 Bitmap* mBitmap;
381};
382
Chris Craik32054b02014-05-09 13:58:56 -0700383///////////////////////////////////////////////////////////////////////////////
384// Conversions to/from SkColor, for get/setPixels, and the create method, which
385// is basically like setPixels
386
387typedef void (*FromColorProc)(void* dst, const SkColor src[], int width,
388 int x, int y);
389
390static void FromColor_D32(void* dst, const SkColor src[], int width,
391 int, int) {
392 SkPMColor* d = (SkPMColor*)dst;
393
394 for (int i = 0; i < width; i++) {
395 *d++ = SkPreMultiplyColor(*src++);
396 }
397}
398
399static void FromColor_D32_Raw(void* dst, const SkColor src[], int width,
400 int, int) {
Dan Albert46d84442014-11-18 16:07:51 -0800401 // Needed to thwart the unreachable code detection from clang.
402 static const bool sk_color_ne_zero = SK_COLOR_MATCHES_PMCOLOR_BYTE_ORDER;
403
Chris Craik32054b02014-05-09 13:58:56 -0700404 // SkColor's ordering may be different from SkPMColor
Dan Albert46d84442014-11-18 16:07:51 -0800405 if (sk_color_ne_zero) {
Chris Craik32054b02014-05-09 13:58:56 -0700406 memcpy(dst, src, width * sizeof(SkColor));
407 return;
408 }
409
410 // order isn't same, repack each pixel manually
411 SkPMColor* d = (SkPMColor*)dst;
412 for (int i = 0; i < width; i++) {
413 SkColor c = *src++;
414 *d++ = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c),
415 SkColorGetG(c), SkColorGetB(c));
416 }
417}
418
419static void FromColor_D565(void* dst, const SkColor src[], int width,
420 int x, int y) {
421 uint16_t* d = (uint16_t*)dst;
422
423 DITHER_565_SCAN(y);
424 for (int stop = x + width; x < stop; x++) {
425 SkColor c = *src++;
426 *d++ = SkDitherRGBTo565(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c),
427 DITHER_VALUE(x));
428 }
429}
430
431static void FromColor_D4444(void* dst, const SkColor src[], int width,
432 int x, int y) {
433 SkPMColor16* d = (SkPMColor16*)dst;
434
435 DITHER_4444_SCAN(y);
436 for (int stop = x + width; x < stop; x++) {
437 SkPMColor pmc = SkPreMultiplyColor(*src++);
438 *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x));
439// *d++ = SkPixel32ToPixel4444(pmc);
440 }
441}
442
443static void FromColor_D4444_Raw(void* dst, const SkColor src[], int width,
444 int x, int y) {
445 SkPMColor16* d = (SkPMColor16*)dst;
446
447 DITHER_4444_SCAN(y);
448 for (int stop = x + width; x < stop; x++) {
449 SkColor c = *src++;
450
451 // SkPMColor is used because the ordering is ARGB32, even though the target actually premultiplied
452 SkPMColor pmc = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c),
453 SkColorGetG(c), SkColorGetB(c));
454 *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x));
455// *d++ = SkPixel32ToPixel4444(pmc);
456 }
457}
458
459// can return NULL
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400460static FromColorProc ChooseFromColorProc(const SkBitmap& bitmap) {
461 switch (bitmap.colorType()) {
462 case kN32_SkColorType:
463 return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D32 : FromColor_D32_Raw;
464 case kARGB_4444_SkColorType:
465 return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D4444 :
466 FromColor_D4444_Raw;
467 case kRGB_565_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700468 return FromColor_D565;
469 default:
470 break;
471 }
472 return NULL;
473}
474
475bool GraphicsJNI::SetPixels(JNIEnv* env, jintArray srcColors, int srcOffset, int srcStride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400476 int x, int y, int width, int height, const SkBitmap& dstBitmap) {
Chris Craik32054b02014-05-09 13:58:56 -0700477 SkAutoLockPixels alp(dstBitmap);
478 void* dst = dstBitmap.getPixels();
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400479 FromColorProc proc = ChooseFromColorProc(dstBitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700480
481 if (NULL == dst || NULL == proc) {
482 return false;
483 }
484
485 const jint* array = env->GetIntArrayElements(srcColors, NULL);
486 const SkColor* src = (const SkColor*)array + srcOffset;
487
488 // reset to to actual choice from caller
489 dst = dstBitmap.getAddr(x, y);
490 // now copy/convert each scanline
491 for (int y = 0; y < height; y++) {
492 proc(dst, src, width, x, y);
493 src += srcStride;
494 dst = (char*)dst + dstBitmap.rowBytes();
495 }
496
497 dstBitmap.notifyPixelsChanged();
498
499 env->ReleaseIntArrayElements(srcColors, const_cast<jint*>(array),
500 JNI_ABORT);
501 return true;
502}
503
504//////////////////// ToColor procs
505
506typedef void (*ToColorProc)(SkColor dst[], const void* src, int width,
507 SkColorTable*);
508
509static void ToColor_S32_Alpha(SkColor dst[], const void* src, int width,
510 SkColorTable*) {
511 SkASSERT(width > 0);
512 const SkPMColor* s = (const SkPMColor*)src;
513 do {
514 *dst++ = SkUnPreMultiply::PMColorToColor(*s++);
515 } while (--width != 0);
516}
517
518static void ToColor_S32_Raw(SkColor dst[], const void* src, int width,
519 SkColorTable*) {
520 SkASSERT(width > 0);
521 const SkPMColor* s = (const SkPMColor*)src;
522 do {
523 SkPMColor c = *s++;
524 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
525 SkGetPackedG32(c), SkGetPackedB32(c));
526 } while (--width != 0);
527}
528
529static void ToColor_S32_Opaque(SkColor dst[], const void* src, int width,
530 SkColorTable*) {
531 SkASSERT(width > 0);
532 const SkPMColor* s = (const SkPMColor*)src;
533 do {
534 SkPMColor c = *s++;
535 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
536 SkGetPackedB32(c));
537 } while (--width != 0);
538}
539
540static void ToColor_S4444_Alpha(SkColor dst[], const void* src, int width,
541 SkColorTable*) {
542 SkASSERT(width > 0);
543 const SkPMColor16* s = (const SkPMColor16*)src;
544 do {
545 *dst++ = SkUnPreMultiply::PMColorToColor(SkPixel4444ToPixel32(*s++));
546 } while (--width != 0);
547}
548
549static void ToColor_S4444_Raw(SkColor dst[], const void* src, int width,
550 SkColorTable*) {
551 SkASSERT(width > 0);
552 const SkPMColor16* s = (const SkPMColor16*)src;
553 do {
554 SkPMColor c = SkPixel4444ToPixel32(*s++);
555 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
556 SkGetPackedG32(c), SkGetPackedB32(c));
557 } while (--width != 0);
558}
559
560static void ToColor_S4444_Opaque(SkColor dst[], const void* src, int width,
561 SkColorTable*) {
562 SkASSERT(width > 0);
563 const SkPMColor16* s = (const SkPMColor16*)src;
564 do {
565 SkPMColor c = SkPixel4444ToPixel32(*s++);
566 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
567 SkGetPackedB32(c));
568 } while (--width != 0);
569}
570
571static void ToColor_S565(SkColor dst[], const void* src, int width,
572 SkColorTable*) {
573 SkASSERT(width > 0);
574 const uint16_t* s = (const uint16_t*)src;
575 do {
576 uint16_t c = *s++;
577 *dst++ = SkColorSetRGB(SkPacked16ToR32(c), SkPacked16ToG32(c),
578 SkPacked16ToB32(c));
579 } while (--width != 0);
580}
581
582static void ToColor_SI8_Alpha(SkColor dst[], const void* src, int width,
583 SkColorTable* ctable) {
584 SkASSERT(width > 0);
585 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500586 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700587 do {
588 *dst++ = SkUnPreMultiply::PMColorToColor(colors[*s++]);
589 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700590}
591
592static void ToColor_SI8_Raw(SkColor dst[], const void* src, int width,
593 SkColorTable* ctable) {
594 SkASSERT(width > 0);
595 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500596 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700597 do {
598 SkPMColor c = colors[*s++];
599 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
600 SkGetPackedG32(c), SkGetPackedB32(c));
601 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700602}
603
604static void ToColor_SI8_Opaque(SkColor dst[], const void* src, int width,
605 SkColorTable* ctable) {
606 SkASSERT(width > 0);
607 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500608 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700609 do {
610 SkPMColor c = colors[*s++];
611 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
612 SkGetPackedB32(c));
613 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700614}
615
616// can return NULL
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400617static ToColorProc ChooseToColorProc(const SkBitmap& src) {
Mike Reedb9330552014-06-16 17:31:48 -0400618 switch (src.colorType()) {
619 case kN32_SkColorType:
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400620 switch (src.alphaType()) {
621 case kOpaque_SkAlphaType:
622 return ToColor_S32_Opaque;
623 case kPremul_SkAlphaType:
624 return ToColor_S32_Alpha;
625 case kUnpremul_SkAlphaType:
626 return ToColor_S32_Raw;
627 default:
628 return NULL;
629 }
Mike Reedb9330552014-06-16 17:31:48 -0400630 case kARGB_4444_SkColorType:
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400631 switch (src.alphaType()) {
632 case kOpaque_SkAlphaType:
633 return ToColor_S4444_Opaque;
634 case kPremul_SkAlphaType:
635 return ToColor_S4444_Alpha;
636 case kUnpremul_SkAlphaType:
637 return ToColor_S4444_Raw;
638 default:
639 return NULL;
640 }
Mike Reedb9330552014-06-16 17:31:48 -0400641 case kRGB_565_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700642 return ToColor_S565;
Mike Reedb9330552014-06-16 17:31:48 -0400643 case kIndex_8_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700644 if (src.getColorTable() == NULL) {
645 return NULL;
646 }
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400647 switch (src.alphaType()) {
648 case kOpaque_SkAlphaType:
649 return ToColor_SI8_Opaque;
650 case kPremul_SkAlphaType:
651 return ToColor_SI8_Alpha;
652 case kUnpremul_SkAlphaType:
653 return ToColor_SI8_Raw;
654 default:
655 return NULL;
656 }
Chris Craik32054b02014-05-09 13:58:56 -0700657 default:
658 break;
659 }
660 return NULL;
661}
662
663///////////////////////////////////////////////////////////////////////////////
664///////////////////////////////////////////////////////////////////////////////
665
666static int getPremulBitmapCreateFlags(bool isMutable) {
667 int flags = GraphicsJNI::kBitmapCreateFlag_Premultiplied;
668 if (isMutable) flags |= GraphicsJNI::kBitmapCreateFlag_Mutable;
669 return flags;
670}
671
672static jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors,
673 jint offset, jint stride, jint width, jint height,
674 jint configHandle, jboolean isMutable) {
Mike Reed1103b322014-07-08 12:36:44 -0400675 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700676 if (NULL != jColors) {
677 size_t n = env->GetArrayLength(jColors);
678 if (n < SkAbs32(stride) * (size_t)height) {
679 doThrowAIOOBE(env);
680 return NULL;
681 }
682 }
683
684 // ARGB_4444 is a deprecated format, convert automatically to 8888
Mike Reedb9330552014-06-16 17:31:48 -0400685 if (colorType == kARGB_4444_SkColorType) {
686 colorType = kN32_SkColorType;
Chris Craik32054b02014-05-09 13:58:56 -0700687 }
688
689 SkBitmap bitmap;
Mike Reedb9330552014-06-16 17:31:48 -0400690 bitmap.setInfo(SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType));
Chris Craik32054b02014-05-09 13:58:56 -0700691
John Reckf29ed282015-04-07 07:32:03 -0700692 Bitmap* nativeBitmap = GraphicsJNI::allocateJavaPixelRef(env, &bitmap, NULL);
693 if (!nativeBitmap) {
Chris Craik32054b02014-05-09 13:58:56 -0700694 return NULL;
695 }
696
697 if (jColors != NULL) {
698 GraphicsJNI::SetPixels(env, jColors, offset, stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400699 0, 0, width, height, bitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700700 }
701
John Reckf29ed282015-04-07 07:32:03 -0700702 return GraphicsJNI::createBitmap(env, nativeBitmap,
703 getPremulBitmapCreateFlags(isMutable));
Chris Craik32054b02014-05-09 13:58:56 -0700704}
705
706static jobject Bitmap_copy(JNIEnv* env, jobject, jlong srcHandle,
707 jint dstConfigHandle, jboolean isMutable) {
John Reckf29ed282015-04-07 07:32:03 -0700708 SkBitmap src;
709 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
Mike Reed1103b322014-07-08 12:36:44 -0400710 SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700711 SkBitmap result;
712 JavaPixelAllocator allocator(env);
713
John Reckf29ed282015-04-07 07:32:03 -0700714 if (!src.copyTo(&result, dstCT, &allocator)) {
Chris Craik32054b02014-05-09 13:58:56 -0700715 return NULL;
716 }
John Reckf29ed282015-04-07 07:32:03 -0700717 Bitmap* bitmap = allocator.getStorageObjAndReset();
718 return GraphicsJNI::createBitmap(env, bitmap,
719 getPremulBitmapCreateFlags(isMutable));
Chris Craik32054b02014-05-09 13:58:56 -0700720}
721
Riley Andrews721ae5f2015-05-11 16:08:22 -0700722static jobject Bitmap_copyAshmem(JNIEnv* env, jobject, jlong srcHandle) {
723 SkBitmap src;
724 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
725 SkBitmap result;
726
727 AshmemPixelAllocator allocator(env);
728 if (!src.copyTo(&result, &allocator)) {
729 return NULL;
730 }
731 Bitmap* bitmap = allocator.getStorageObjAndReset();
732 bitmap->peekAtPixelRef()->setImmutable();
733 jobject ret = GraphicsJNI::createBitmap(env, bitmap, getPremulBitmapCreateFlags(false));
734 return ret;
735}
736
Chris Craik32054b02014-05-09 13:58:56 -0700737static void Bitmap_destructor(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700738 LocalScopedBitmap bitmap(bitmapHandle);
739 bitmap->detachFromJava();
Chris Craik32054b02014-05-09 13:58:56 -0700740}
741
742static jboolean Bitmap_recycle(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700743 LocalScopedBitmap bitmap(bitmapHandle);
744 bitmap->freePixels();
Chris Craik32054b02014-05-09 13:58:56 -0700745 return JNI_TRUE;
746}
747
748static void Bitmap_reconfigure(JNIEnv* env, jobject clazz, jlong bitmapHandle,
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400749 jint width, jint height, jint configHandle, jint allocSize,
750 jboolean requestPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700751 LocalScopedBitmap bitmap(bitmapHandle);
Mike Reed1103b322014-07-08 12:36:44 -0400752 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400753
754 // ARGB_4444 is a deprecated format, convert automatically to 8888
755 if (colorType == kARGB_4444_SkColorType) {
756 colorType = kN32_SkColorType;
757 }
758
759 if (width * height * SkColorTypeBytesPerPixel(colorType) > allocSize) {
Chris Craik32054b02014-05-09 13:58:56 -0700760 // done in native as there's no way to get BytesPerPixel in Java
761 doThrowIAE(env, "Bitmap not large enough to support new configuration");
762 return;
763 }
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400764 SkAlphaType alphaType;
John Reckf29ed282015-04-07 07:32:03 -0700765 if (bitmap->info().colorType() != kRGB_565_SkColorType
766 && bitmap->info().alphaType() == kOpaque_SkAlphaType) {
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400767 // If the original bitmap was set to opaque, keep that setting, unless it
768 // was 565, which is required to be opaque.
769 alphaType = kOpaque_SkAlphaType;
770 } else {
771 // Otherwise respect the premultiplied request.
772 alphaType = requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
773 }
John Reckf29ed282015-04-07 07:32:03 -0700774 bitmap->reconfigure(SkImageInfo::Make(width, height, colorType, alphaType));
Chris Craik32054b02014-05-09 13:58:56 -0700775}
776
777// These must match the int values in Bitmap.java
778enum JavaEncodeFormat {
779 kJPEG_JavaEncodeFormat = 0,
780 kPNG_JavaEncodeFormat = 1,
781 kWEBP_JavaEncodeFormat = 2
782};
783
784static jboolean Bitmap_compress(JNIEnv* env, jobject clazz, jlong bitmapHandle,
785 jint format, jint quality,
786 jobject jstream, jbyteArray jstorage) {
John Reckf29ed282015-04-07 07:32:03 -0700787
788 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700789 SkImageEncoder::Type fm;
790
791 switch (format) {
792 case kJPEG_JavaEncodeFormat:
793 fm = SkImageEncoder::kJPEG_Type;
794 break;
795 case kPNG_JavaEncodeFormat:
796 fm = SkImageEncoder::kPNG_Type;
797 break;
798 case kWEBP_JavaEncodeFormat:
799 fm = SkImageEncoder::kWEBP_Type;
800 break;
801 default:
802 return JNI_FALSE;
803 }
804
John Reckf29ed282015-04-07 07:32:03 -0700805 if (!bitmap.valid()) {
806 return JNI_FALSE;
807 }
808
Chris Craik32054b02014-05-09 13:58:56 -0700809 bool success = false;
Chris Craik32054b02014-05-09 13:58:56 -0700810
John Reckf29ed282015-04-07 07:32:03 -0700811 std::unique_ptr<SkWStream> strm(CreateJavaOutputStreamAdaptor(env, jstream, jstorage));
812 if (!strm.get()) {
813 return JNI_FALSE;
814 }
Chris Craik32054b02014-05-09 13:58:56 -0700815
John Reckf29ed282015-04-07 07:32:03 -0700816 std::unique_ptr<SkImageEncoder> encoder(SkImageEncoder::Create(fm));
817 if (encoder.get()) {
818 SkBitmap skbitmap;
819 bitmap->getSkBitmap(&skbitmap);
820 success = encoder->encodeStream(strm.get(), skbitmap, quality);
Chris Craik32054b02014-05-09 13:58:56 -0700821 }
822 return success ? JNI_TRUE : JNI_FALSE;
823}
824
825static void Bitmap_erase(JNIEnv* env, jobject, jlong bitmapHandle, jint color) {
John Reckf29ed282015-04-07 07:32:03 -0700826 LocalScopedBitmap bitmap(bitmapHandle);
827 SkBitmap skBitmap;
828 bitmap->getSkBitmap(&skBitmap);
829 skBitmap.eraseColor(color);
Chris Craik32054b02014-05-09 13:58:56 -0700830}
831
832static jint Bitmap_rowBytes(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700833 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700834 return static_cast<jint>(bitmap->rowBytes());
835}
836
837static jint Bitmap_config(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700838 LocalScopedBitmap bitmap(bitmapHandle);
839 return GraphicsJNI::colorTypeToLegacyBitmapConfig(bitmap->info().colorType());
Chris Craik32054b02014-05-09 13:58:56 -0700840}
841
842static jint Bitmap_getGenerationId(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700843 LocalScopedBitmap bitmap(bitmapHandle);
John Reckae2e8b42015-05-06 14:55:05 -0700844 return static_cast<jint>(bitmap->peekAtPixelRef()->getGenerationID());
Chris Craik32054b02014-05-09 13:58:56 -0700845}
846
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400847static jboolean Bitmap_isPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700848 LocalScopedBitmap bitmap(bitmapHandle);
849 if (bitmap->info().alphaType() == kPremul_SkAlphaType) {
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400850 return JNI_TRUE;
851 }
852 return JNI_FALSE;
853}
854
Chris Craik32054b02014-05-09 13:58:56 -0700855static jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700856 LocalScopedBitmap bitmap(bitmapHandle);
857 return !bitmap->info().isOpaque() ? JNI_TRUE : JNI_FALSE;
Chris Craik32054b02014-05-09 13:58:56 -0700858}
859
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400860static void Bitmap_setHasAlpha(JNIEnv* env, jobject, jlong bitmapHandle,
861 jboolean hasAlpha, jboolean requestPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700862 LocalScopedBitmap bitmap(bitmapHandle);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400863 if (hasAlpha) {
John Reckae2e8b42015-05-06 14:55:05 -0700864 bitmap->peekAtPixelRef()->changeAlphaType(
John Reckf29ed282015-04-07 07:32:03 -0700865 requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType);
Chris Craik32054b02014-05-09 13:58:56 -0700866 } else {
John Reckae2e8b42015-05-06 14:55:05 -0700867 bitmap->peekAtPixelRef()->changeAlphaType(kOpaque_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400868 }
869}
870
871static void Bitmap_setPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle,
872 jboolean isPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700873 LocalScopedBitmap bitmap(bitmapHandle);
874 if (!bitmap->info().isOpaque()) {
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400875 if (isPremul) {
John Reckae2e8b42015-05-06 14:55:05 -0700876 bitmap->peekAtPixelRef()->changeAlphaType(kPremul_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400877 } else {
John Reckae2e8b42015-05-06 14:55:05 -0700878 bitmap->peekAtPixelRef()->changeAlphaType(kUnpremul_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400879 }
Chris Craik32054b02014-05-09 13:58:56 -0700880 }
881}
882
883static jboolean Bitmap_hasMipMap(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700884 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700885 return bitmap->hasHardwareMipMap() ? JNI_TRUE : JNI_FALSE;
886}
887
888static void Bitmap_setHasMipMap(JNIEnv* env, jobject, jlong bitmapHandle,
889 jboolean hasMipMap) {
John Reckf29ed282015-04-07 07:32:03 -0700890 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700891 bitmap->setHasHardwareMipMap(hasMipMap);
892}
893
894///////////////////////////////////////////////////////////////////////////////
895
896static jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) {
897 if (parcel == NULL) {
898 SkDebugf("-------- unparcel parcel is NULL\n");
899 return NULL;
900 }
901
902 android::Parcel* p = android::parcelForJavaObject(env, parcel);
903
Mike Reedb9330552014-06-16 17:31:48 -0400904 const bool isMutable = p->readInt32() != 0;
905 const SkColorType colorType = (SkColorType)p->readInt32();
906 const SkAlphaType alphaType = (SkAlphaType)p->readInt32();
907 const int width = p->readInt32();
908 const int height = p->readInt32();
909 const int rowBytes = p->readInt32();
910 const int density = p->readInt32();
Chris Craik32054b02014-05-09 13:58:56 -0700911
Mike Reedb9330552014-06-16 17:31:48 -0400912 if (kN32_SkColorType != colorType &&
913 kRGB_565_SkColorType != colorType &&
914 kARGB_4444_SkColorType != colorType &&
915 kIndex_8_SkColorType != colorType &&
916 kAlpha_8_SkColorType != colorType) {
917 SkDebugf("Bitmap_createFromParcel unknown colortype: %d\n", colorType);
Chris Craik32054b02014-05-09 13:58:56 -0700918 return NULL;
919 }
920
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400921 std::unique_ptr<SkBitmap> bitmap(new SkBitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700922
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400923 if (!bitmap->setInfo(SkImageInfo::Make(width, height, colorType, alphaType), rowBytes)) {
924 return NULL;
925 }
Chris Craik32054b02014-05-09 13:58:56 -0700926
927 SkColorTable* ctable = NULL;
Mike Reedb9330552014-06-16 17:31:48 -0400928 if (colorType == kIndex_8_SkColorType) {
Chris Craik32054b02014-05-09 13:58:56 -0700929 int count = p->readInt32();
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400930 if (count < 0 || count > 256) {
931 // The data is corrupt, since SkColorTable enforces a value between 0 and 256,
932 // inclusive.
933 return NULL;
934 }
Chris Craik32054b02014-05-09 13:58:56 -0700935 if (count > 0) {
936 size_t size = count * sizeof(SkPMColor);
937 const SkPMColor* src = (const SkPMColor*)p->readInplace(size);
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400938 if (src == NULL) {
939 return NULL;
940 }
Chris Craik32054b02014-05-09 13:58:56 -0700941 ctable = new SkColorTable(src, count);
942 }
943 }
944
Riley Andrews39d7f302014-11-13 17:43:25 -0800945 int fd = p->readFileDescriptor();
946 int dupFd = dup(fd);
947 if (dupFd < 0) {
Chris Craik32054b02014-05-09 13:58:56 -0700948 SkSafeUnref(ctable);
Riley Andrews39d7f302014-11-13 17:43:25 -0800949 doThrowRE(env, "Could not dup parcel fd.");
Chris Craik32054b02014-05-09 13:58:56 -0700950 return NULL;
951 }
952
Riley Andrews39d7f302014-11-13 17:43:25 -0800953 bool readOnlyMapping = !isMutable;
954 Bitmap* nativeBitmap = GraphicsJNI::mapAshmemPixelRef(env, bitmap.get(),
955 ctable, dupFd, readOnlyMapping);
Chris Craik32054b02014-05-09 13:58:56 -0700956 SkSafeUnref(ctable);
Riley Andrews39d7f302014-11-13 17:43:25 -0800957 if (!nativeBitmap) {
958 close(dupFd);
959 doThrowRE(env, "Could not allocate ashmem pixel ref.");
Chris Craik32054b02014-05-09 13:58:56 -0700960 return NULL;
961 }
Riley Andrews39d7f302014-11-13 17:43:25 -0800962 bitmap->pixelRef()->setImmutable();
Chris Craik32054b02014-05-09 13:58:56 -0700963
John Reckf29ed282015-04-07 07:32:03 -0700964 return GraphicsJNI::createBitmap(env, nativeBitmap,
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400965 getPremulBitmapCreateFlags(isMutable), NULL, NULL, density);
Chris Craik32054b02014-05-09 13:58:56 -0700966}
967
Riley Andrews39d7f302014-11-13 17:43:25 -0800968class Ashmem {
969public:
970 Ashmem(size_t sz, bool removeWritePerm) : mSize(sz) {
971 int fd = -1;
972 void *addr = nullptr;
973
974 // Create new ashmem region with read/write priv
975 fd = ashmem_create_region("bitmap", sz);
976 if (fd < 0) {
977 goto error;
978 }
979 addr = mmap(nullptr, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
980 if (addr == MAP_FAILED) {
981 goto error;
982 }
983 // If requested, remove the ability to make additional writeable to
984 // this memory.
985 if (removeWritePerm) {
986 if (ashmem_set_prot_region(fd, PROT_READ) < 0) {
987 goto error;
988 }
989 }
990 mFd = fd;
991 mPtr = addr;
992 return;
993error:
994 if (fd >= 0) {
995 close(fd);
996 }
997 if (addr) {
998 munmap(addr, sz);
999 }
1000 }
1001 ~Ashmem() {
1002 if (mPtr) {
1003 close(mFd);
1004 munmap(mPtr, mSize);
1005 }
1006 }
1007 void *getPtr() const { return mPtr; }
1008 int getFd() const { return mFd; }
1009private:
1010 int mFd = -1;
1011 int mSize;
1012 void* mPtr = nullptr;
1013};
1014
Chris Craik32054b02014-05-09 13:58:56 -07001015static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject,
1016 jlong bitmapHandle,
1017 jboolean isMutable, jint density,
1018 jobject parcel) {
Chris Craik32054b02014-05-09 13:58:56 -07001019 if (parcel == NULL) {
1020 SkDebugf("------- writeToParcel null parcel\n");
1021 return JNI_FALSE;
1022 }
1023
1024 android::Parcel* p = android::parcelForJavaObject(env, parcel);
John Reckf29ed282015-04-07 07:32:03 -07001025 SkBitmap bitmap;
Riley Andrews39d7f302014-11-13 17:43:25 -08001026
1027 android::Bitmap* androidBitmap = reinterpret_cast<Bitmap*>(bitmapHandle);
1028 androidBitmap->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001029
1030 p->writeInt32(isMutable);
John Reckf29ed282015-04-07 07:32:03 -07001031 p->writeInt32(bitmap.colorType());
1032 p->writeInt32(bitmap.alphaType());
1033 p->writeInt32(bitmap.width());
1034 p->writeInt32(bitmap.height());
1035 p->writeInt32(bitmap.rowBytes());
Chris Craik32054b02014-05-09 13:58:56 -07001036 p->writeInt32(density);
1037
John Reckf29ed282015-04-07 07:32:03 -07001038 if (bitmap.colorType() == kIndex_8_SkColorType) {
1039 SkColorTable* ctable = bitmap.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001040 if (ctable != NULL) {
1041 int count = ctable->count();
1042 p->writeInt32(count);
1043 memcpy(p->writeInplace(count * sizeof(SkPMColor)),
Mike Reed71487eb2014-11-19 16:13:20 -05001044 ctable->readColors(), count * sizeof(SkPMColor));
Chris Craik32054b02014-05-09 13:58:56 -07001045 } else {
1046 p->writeInt32(0); // indicate no ctable
1047 }
1048 }
1049
Riley Andrews39d7f302014-11-13 17:43:25 -08001050 bool ashmemSrc = androidBitmap->getAshmemFd() >= 0;
1051 if (ashmemSrc && !isMutable) {
1052 p->writeDupFileDescriptor(androidBitmap->getAshmemFd());
Chris Craik32054b02014-05-09 13:58:56 -07001053 } else {
Riley Andrews39d7f302014-11-13 17:43:25 -08001054 Ashmem dstAshmem(bitmap.getSize(), !isMutable);
1055 if (!dstAshmem.getPtr()) {
1056 doThrowRE(env, "Could not allocate ashmem for new bitmap.");
1057 return JNI_FALSE;
1058 }
Chris Craik32054b02014-05-09 13:58:56 -07001059
Riley Andrews39d7f302014-11-13 17:43:25 -08001060 bitmap.lockPixels();
1061 const void* pSrc = bitmap.getPixels();
1062 if (pSrc == NULL) {
1063 memset(dstAshmem.getPtr(), 0, bitmap.getSize());
1064 } else {
1065 memcpy(dstAshmem.getPtr(), pSrc, bitmap.getSize());
1066 }
1067 bitmap.unlockPixels();
1068 p->writeDupFileDescriptor(dstAshmem.getFd());
1069 }
Chris Craik32054b02014-05-09 13:58:56 -07001070 return JNI_TRUE;
1071}
1072
1073static jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz,
1074 jlong srcHandle, jlong paintHandle,
1075 jintArray offsetXY) {
John Reckf29ed282015-04-07 07:32:03 -07001076 SkBitmap src;
1077 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -04001078 const android::Paint* paint = reinterpret_cast<android::Paint*>(paintHandle);
Chris Craik32054b02014-05-09 13:58:56 -07001079 SkIPoint offset;
John Reckf29ed282015-04-07 07:32:03 -07001080 SkBitmap dst;
Chris Craik32054b02014-05-09 13:58:56 -07001081 JavaPixelAllocator allocator(env);
1082
John Reckf29ed282015-04-07 07:32:03 -07001083 src.extractAlpha(&dst, paint, &allocator, &offset);
Chris Craik32054b02014-05-09 13:58:56 -07001084 // If Skia can't allocate pixels for destination bitmap, it resets
1085 // it, that is set its pixels buffer to NULL, and zero width and height.
John Reckf29ed282015-04-07 07:32:03 -07001086 if (dst.getPixels() == NULL && src.getPixels() != NULL) {
Chris Craik32054b02014-05-09 13:58:56 -07001087 doThrowOOME(env, "failed to allocate pixels for alpha");
1088 return NULL;
1089 }
1090 if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) {
1091 int* array = env->GetIntArrayElements(offsetXY, NULL);
1092 array[0] = offset.fX;
1093 array[1] = offset.fY;
1094 env->ReleaseIntArrayElements(offsetXY, array, 0);
1095 }
1096
John Reckf29ed282015-04-07 07:32:03 -07001097 return GraphicsJNI::createBitmap(env, allocator.getStorageObjAndReset(),
1098 getPremulBitmapCreateFlags(true));
Chris Craik32054b02014-05-09 13:58:56 -07001099}
1100
1101///////////////////////////////////////////////////////////////////////////////
1102
1103static jint Bitmap_getPixel(JNIEnv* env, jobject, jlong bitmapHandle,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001104 jint x, jint y) {
John Reckf29ed282015-04-07 07:32:03 -07001105 SkBitmap bitmap;
1106 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1107 SkAutoLockPixels alp(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001108
John Reckf29ed282015-04-07 07:32:03 -07001109 ToColorProc proc = ChooseToColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001110 if (NULL == proc) {
1111 return 0;
1112 }
John Reckf29ed282015-04-07 07:32:03 -07001113 const void* src = bitmap.getAddr(x, y);
Chris Craik32054b02014-05-09 13:58:56 -07001114 if (NULL == src) {
1115 return 0;
1116 }
1117
1118 SkColor dst[1];
John Reckf29ed282015-04-07 07:32:03 -07001119 proc(dst, src, 1, bitmap.getColorTable());
Chris Craik32054b02014-05-09 13:58:56 -07001120 return static_cast<jint>(dst[0]);
1121}
1122
1123static void Bitmap_getPixels(JNIEnv* env, jobject, jlong bitmapHandle,
1124 jintArray pixelArray, jint offset, jint stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001125 jint x, jint y, jint width, jint height) {
John Reckf29ed282015-04-07 07:32:03 -07001126 SkBitmap bitmap;
1127 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1128 SkAutoLockPixels alp(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001129
John Reckf29ed282015-04-07 07:32:03 -07001130 ToColorProc proc = ChooseToColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001131 if (NULL == proc) {
1132 return;
1133 }
John Reckf29ed282015-04-07 07:32:03 -07001134 const void* src = bitmap.getAddr(x, y);
Chris Craik32054b02014-05-09 13:58:56 -07001135 if (NULL == src) {
1136 return;
1137 }
1138
John Reckf29ed282015-04-07 07:32:03 -07001139 SkColorTable* ctable = bitmap.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001140 jint* dst = env->GetIntArrayElements(pixelArray, NULL);
1141 SkColor* d = (SkColor*)dst + offset;
1142 while (--height >= 0) {
1143 proc(d, src, width, ctable);
1144 d += stride;
John Reckf29ed282015-04-07 07:32:03 -07001145 src = (void*)((const char*)src + bitmap.rowBytes());
Chris Craik32054b02014-05-09 13:58:56 -07001146 }
1147 env->ReleaseIntArrayElements(pixelArray, dst, 0);
1148}
1149
1150///////////////////////////////////////////////////////////////////////////////
1151
1152static void Bitmap_setPixel(JNIEnv* env, jobject, jlong bitmapHandle,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001153 jint x, jint y, jint colorHandle) {
John Reckf29ed282015-04-07 07:32:03 -07001154 SkBitmap bitmap;
1155 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001156 SkColor color = static_cast<SkColor>(colorHandle);
John Reckf29ed282015-04-07 07:32:03 -07001157 SkAutoLockPixels alp(bitmap);
1158 if (NULL == bitmap.getPixels()) {
Chris Craik32054b02014-05-09 13:58:56 -07001159 return;
1160 }
1161
John Reckf29ed282015-04-07 07:32:03 -07001162 FromColorProc proc = ChooseFromColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001163 if (NULL == proc) {
1164 return;
1165 }
1166
John Reckf29ed282015-04-07 07:32:03 -07001167 proc(bitmap.getAddr(x, y), &color, 1, x, y);
1168 bitmap.notifyPixelsChanged();
Chris Craik32054b02014-05-09 13:58:56 -07001169}
1170
1171static void Bitmap_setPixels(JNIEnv* env, jobject, jlong bitmapHandle,
1172 jintArray pixelArray, jint offset, jint stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001173 jint x, jint y, jint width, jint height) {
John Reckf29ed282015-04-07 07:32:03 -07001174 SkBitmap bitmap;
1175 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001176 GraphicsJNI::SetPixels(env, pixelArray, offset, stride,
John Reckf29ed282015-04-07 07:32:03 -07001177 x, y, width, height, bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001178}
1179
1180static void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject,
1181 jlong bitmapHandle, jobject jbuffer) {
John Reckf29ed282015-04-07 07:32:03 -07001182 SkBitmap bitmap;
1183 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1184 SkAutoLockPixels alp(bitmap);
1185 const void* src = bitmap.getPixels();
Chris Craik32054b02014-05-09 13:58:56 -07001186
1187 if (NULL != src) {
1188 android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE);
1189
1190 // the java side has already checked that buffer is large enough
John Reckf29ed282015-04-07 07:32:03 -07001191 memcpy(abp.pointer(), src, bitmap.getSize());
Chris Craik32054b02014-05-09 13:58:56 -07001192 }
1193}
1194
1195static void Bitmap_copyPixelsFromBuffer(JNIEnv* env, jobject,
1196 jlong bitmapHandle, jobject jbuffer) {
John Reckf29ed282015-04-07 07:32:03 -07001197 SkBitmap bitmap;
1198 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1199 SkAutoLockPixels alp(bitmap);
1200 void* dst = bitmap.getPixels();
Chris Craik32054b02014-05-09 13:58:56 -07001201
1202 if (NULL != dst) {
1203 android::AutoBufferPointer abp(env, jbuffer, JNI_FALSE);
1204 // the java side has already checked that buffer is large enough
John Reckf29ed282015-04-07 07:32:03 -07001205 memcpy(dst, abp.pointer(), bitmap.getSize());
1206 bitmap.notifyPixelsChanged();
Chris Craik32054b02014-05-09 13:58:56 -07001207 }
1208}
1209
1210static jboolean Bitmap_sameAs(JNIEnv* env, jobject, jlong bm0Handle,
1211 jlong bm1Handle) {
John Reckf29ed282015-04-07 07:32:03 -07001212 SkBitmap bm0;
1213 SkBitmap bm1;
1214 reinterpret_cast<Bitmap*>(bm0Handle)->getSkBitmap(&bm0);
1215 reinterpret_cast<Bitmap*>(bm1Handle)->getSkBitmap(&bm1);
1216 if (bm0.width() != bm1.width() ||
1217 bm0.height() != bm1.height() ||
1218 bm0.colorType() != bm1.colorType()) {
Chris Craik32054b02014-05-09 13:58:56 -07001219 return JNI_FALSE;
1220 }
1221
John Reckf29ed282015-04-07 07:32:03 -07001222 SkAutoLockPixels alp0(bm0);
1223 SkAutoLockPixels alp1(bm1);
Chris Craik32054b02014-05-09 13:58:56 -07001224
1225 // if we can't load the pixels, return false
John Reckf29ed282015-04-07 07:32:03 -07001226 if (NULL == bm0.getPixels() || NULL == bm1.getPixels()) {
Chris Craik32054b02014-05-09 13:58:56 -07001227 return JNI_FALSE;
1228 }
1229
John Reckf29ed282015-04-07 07:32:03 -07001230 if (bm0.colorType() == kIndex_8_SkColorType) {
1231 SkColorTable* ct0 = bm0.getColorTable();
1232 SkColorTable* ct1 = bm1.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001233 if (NULL == ct0 || NULL == ct1) {
1234 return JNI_FALSE;
1235 }
1236 if (ct0->count() != ct1->count()) {
1237 return JNI_FALSE;
1238 }
1239
Chris Craik32054b02014-05-09 13:58:56 -07001240 const size_t size = ct0->count() * sizeof(SkPMColor);
Mike Reed71487eb2014-11-19 16:13:20 -05001241 if (memcmp(ct0->readColors(), ct1->readColors(), size) != 0) {
Chris Craik32054b02014-05-09 13:58:56 -07001242 return JNI_FALSE;
1243 }
1244 }
1245
1246 // now compare each scanline. We can't do the entire buffer at once,
1247 // since we don't care about the pixel values that might extend beyond
1248 // the width (since the scanline might be larger than the logical width)
John Reckf29ed282015-04-07 07:32:03 -07001249 const int h = bm0.height();
1250 const size_t size = bm0.width() * bm0.bytesPerPixel();
Chris Craik32054b02014-05-09 13:58:56 -07001251 for (int y = 0; y < h; y++) {
henry.uh_chen53001ca2014-07-03 20:40:22 +08001252 // SkBitmap::getAddr(int, int) may return NULL due to unrecognized config
1253 // (ex: kRLE_Index8_Config). This will cause memcmp method to crash. Since bm0
1254 // and bm1 both have pixel data() (have passed NULL == getPixels() check),
1255 // those 2 bitmaps should be valid (only unrecognized), we return JNI_FALSE
1256 // to warn user those 2 unrecognized config bitmaps may be different.
John Reckf29ed282015-04-07 07:32:03 -07001257 void *bm0Addr = bm0.getAddr(0, y);
1258 void *bm1Addr = bm1.getAddr(0, y);
henry.uh_chen53001ca2014-07-03 20:40:22 +08001259
1260 if(bm0Addr == NULL || bm1Addr == NULL) {
1261 return JNI_FALSE;
1262 }
1263
1264 if (memcmp(bm0Addr, bm1Addr, size) != 0) {
Chris Craik32054b02014-05-09 13:58:56 -07001265 return JNI_FALSE;
1266 }
1267 }
1268 return JNI_TRUE;
1269}
1270
John Reckc6e2e8f2015-04-15 13:24:47 -07001271static jlong Bitmap_refPixelRef(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -07001272 LocalScopedBitmap bitmap(bitmapHandle);
John Reckae2e8b42015-05-06 14:55:05 -07001273 SkPixelRef* pixelRef = bitmap.valid() ? bitmap->peekAtPixelRef() : nullptr;
John Reckc6e2e8f2015-04-15 13:24:47 -07001274 SkSafeRef(pixelRef);
1275 return reinterpret_cast<jlong>(pixelRef);
1276}
1277
Chris Craik32054b02014-05-09 13:58:56 -07001278///////////////////////////////////////////////////////////////////////////////
1279
Chris Craik32054b02014-05-09 13:58:56 -07001280static JNINativeMethod gBitmapMethods[] = {
1281 { "nativeCreate", "([IIIIIIZ)Landroid/graphics/Bitmap;",
1282 (void*)Bitmap_creator },
1283 { "nativeCopy", "(JIZ)Landroid/graphics/Bitmap;",
1284 (void*)Bitmap_copy },
Riley Andrews721ae5f2015-05-11 16:08:22 -07001285 { "nativeCopyAshmem", "(J)Landroid/graphics/Bitmap;",
1286 (void*)Bitmap_copyAshmem },
Chris Craik32054b02014-05-09 13:58:56 -07001287 { "nativeDestructor", "(J)V", (void*)Bitmap_destructor },
1288 { "nativeRecycle", "(J)Z", (void*)Bitmap_recycle },
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -04001289 { "nativeReconfigure", "(JIIIIZ)V", (void*)Bitmap_reconfigure },
Chris Craik32054b02014-05-09 13:58:56 -07001290 { "nativeCompress", "(JIILjava/io/OutputStream;[B)Z",
1291 (void*)Bitmap_compress },
1292 { "nativeErase", "(JI)V", (void*)Bitmap_erase },
1293 { "nativeRowBytes", "(J)I", (void*)Bitmap_rowBytes },
1294 { "nativeConfig", "(J)I", (void*)Bitmap_config },
1295 { "nativeHasAlpha", "(J)Z", (void*)Bitmap_hasAlpha },
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001296 { "nativeIsPremultiplied", "(J)Z", (void*)Bitmap_isPremultiplied},
1297 { "nativeSetHasAlpha", "(JZZ)V", (void*)Bitmap_setHasAlpha},
1298 { "nativeSetPremultiplied", "(JZ)V", (void*)Bitmap_setPremultiplied},
Chris Craik32054b02014-05-09 13:58:56 -07001299 { "nativeHasMipMap", "(J)Z", (void*)Bitmap_hasMipMap },
1300 { "nativeSetHasMipMap", "(JZ)V", (void*)Bitmap_setHasMipMap },
1301 { "nativeCreateFromParcel",
1302 "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;",
1303 (void*)Bitmap_createFromParcel },
1304 { "nativeWriteToParcel", "(JZILandroid/os/Parcel;)Z",
1305 (void*)Bitmap_writeToParcel },
1306 { "nativeExtractAlpha", "(JJ[I)Landroid/graphics/Bitmap;",
1307 (void*)Bitmap_extractAlpha },
1308 { "nativeGenerationId", "(J)I", (void*)Bitmap_getGenerationId },
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001309 { "nativeGetPixel", "(JII)I", (void*)Bitmap_getPixel },
1310 { "nativeGetPixels", "(J[IIIIIII)V", (void*)Bitmap_getPixels },
1311 { "nativeSetPixel", "(JIII)V", (void*)Bitmap_setPixel },
1312 { "nativeSetPixels", "(J[IIIIIII)V", (void*)Bitmap_setPixels },
Chris Craik32054b02014-05-09 13:58:56 -07001313 { "nativeCopyPixelsToBuffer", "(JLjava/nio/Buffer;)V",
1314 (void*)Bitmap_copyPixelsToBuffer },
1315 { "nativeCopyPixelsFromBuffer", "(JLjava/nio/Buffer;)V",
1316 (void*)Bitmap_copyPixelsFromBuffer },
1317 { "nativeSameAs", "(JJ)Z", (void*)Bitmap_sameAs },
John Reckc6e2e8f2015-04-15 13:24:47 -07001318 { "nativeRefPixelRef", "(J)J", (void*)Bitmap_refPixelRef },
Chris Craik32054b02014-05-09 13:58:56 -07001319};
1320
Chris Craik32054b02014-05-09 13:58:56 -07001321int register_android_graphics_Bitmap(JNIEnv* env)
1322{
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001323 return android::RegisterMethodsOrDie(env, "android/graphics/Bitmap", gBitmapMethods,
1324 NELEM(gBitmapMethods));
Chris Craik32054b02014-05-09 13:58:56 -07001325}