blob: 80ccb614ad734243f8b15f175d64779f2f5c0aba [file] [log] [blame]
John Reckf29ed282015-04-07 07:32:03 -07001#define LOG_TAG "Bitmap"
John Reckf29ed282015-04-07 07:32:03 -07002#include "Bitmap.h"
3
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -04004#include "Paint.h"
Chris Craik32054b02014-05-09 13:58:56 -07005#include "SkBitmap.h"
6#include "SkPixelRef.h"
7#include "SkImageEncoder.h"
Leon Scroggins III57ee6202014-06-04 18:51:07 -04008#include "SkImageInfo.h"
Chris Craik32054b02014-05-09 13:58:56 -07009#include "SkColorPriv.h"
10#include "GraphicsJNI.h"
11#include "SkDither.h"
12#include "SkUnPreMultiply.h"
13#include "SkStream.h"
14
15#include <binder/Parcel.h>
16#include "android_os_Parcel.h"
17#include "android_util_Binder.h"
18#include "android_nio_utils.h"
19#include "CreateJavaOutputStreamAdaptor.h"
John Reckf29ed282015-04-07 07:32:03 -070020#include <Caches.h>
Chris Craik32054b02014-05-09 13:58:56 -070021
Andreas Gampeed6b9df2014-11-20 22:02:20 -080022#include "core_jni_helpers.h"
23
Chris Craik32054b02014-05-09 13:58:56 -070024#include <jni.h>
Riley Andrews39d7f302014-11-13 17:43:25 -080025#include <memory>
26#include <string>
27#include <sys/mman.h>
28#include <cutils/ashmem.h>
Chris Craik32054b02014-05-09 13:58:56 -070029
Jeff Browna316c5d2015-06-05 15:14:06 -070030#define DEBUG_PARCEL 0
Riley Andrews8cee7c12015-11-01 23:36:04 -080031#define ASHMEM_BITMAP_MIN_SIZE (128 * (1 << 10))
Jeff Browna316c5d2015-06-05 15:14:06 -070032
John Reckf29ed282015-04-07 07:32:03 -070033namespace android {
34
35class WrappedPixelRef : public SkPixelRef {
36public:
37 WrappedPixelRef(Bitmap* wrapper, void* storage,
38 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
39 : SkPixelRef(info)
40 , mBitmap(*wrapper)
41 , mStorage(storage) {
42 reconfigure(info, rowBytes, ctable);
43 }
44
45 ~WrappedPixelRef() {
46 // Tell SkRefCnt that everything is as it expects by forcing
47 // the refcnt to 1
48 internal_dispose_restore_refcnt_to_1();
49 SkSafeUnref(mColorTable);
50 }
51
John Reck0781a2f2015-05-27 16:29:17 -070052 void reconfigure(const SkImageInfo& newInfo, size_t rowBytes, SkColorTable* ctable) {
53 if (kIndex_8_SkColorType != newInfo.colorType()) {
John Reckf29ed282015-04-07 07:32:03 -070054 ctable = nullptr;
55 }
56 mRowBytes = rowBytes;
57 if (mColorTable != ctable) {
58 SkSafeUnref(mColorTable);
59 mColorTable = ctable;
60 SkSafeRef(mColorTable);
61 }
John Reck0781a2f2015-05-27 16:29:17 -070062
63 // Need to validate the alpha type to filter against the color type
64 // to prevent things like a non-opaque RGB565 bitmap
65 SkAlphaType alphaType;
66 LOG_ALWAYS_FATAL_IF(!SkColorTypeValidateAlphaType(
67 newInfo.colorType(), newInfo.alphaType(), &alphaType),
68 "Failed to validate alpha type!");
69
John Reckf29ed282015-04-07 07:32:03 -070070 // Dirty hack is dirty
71 // TODO: Figure something out here, Skia's current design makes this
72 // really hard to work with. Skia really, really wants immutable objects,
73 // but with the nested-ref-count hackery going on that's just not
74 // feasible without going insane trying to figure it out
75 SkImageInfo* myInfo = const_cast<SkImageInfo*>(&this->info());
John Reck0781a2f2015-05-27 16:29:17 -070076 *myInfo = newInfo;
77 changeAlphaType(alphaType);
John Reckf29ed282015-04-07 07:32:03 -070078
79 // Docs say to only call this in the ctor, but we're going to call
80 // it anyway even if this isn't always the ctor.
81 // TODO: Fix this too as part of the above TODO
82 setPreLocked(mStorage, mRowBytes, mColorTable);
83 }
84
85 // Can't mark as override since SkPixelRef::rowBytes isn't virtual
86 // but that's OK since we just want BitmapWrapper to be able to rely
87 // on calling rowBytes() on an unlocked pixelref, which it will be
88 // doing on a WrappedPixelRef type, not a SkPixelRef, so static
89 // dispatching will do what we want.
90 size_t rowBytes() const { return mRowBytes; }
91 SkColorTable* colorTable() const { return mColorTable; }
92
93 bool hasHardwareMipMap() const {
94 return mHasHardwareMipMap;
95 }
96
97 void setHasHardwareMipMap(bool hasMipMap) {
98 mHasHardwareMipMap = hasMipMap;
99 }
100
101protected:
102 virtual bool onNewLockPixels(LockRec* rec) override {
103 rec->fPixels = mStorage;
104 rec->fRowBytes = mRowBytes;
105 rec->fColorTable = mColorTable;
106 return true;
107 }
108
109 virtual void onUnlockPixels() override {
110 // nothing
111 }
112
113 virtual size_t getAllocatedSizeInBytes() const override {
114 return info().getSafeSize(mRowBytes);
115 }
116
117private:
118 Bitmap& mBitmap;
119 void* mStorage;
120 size_t mRowBytes = 0;
121 SkColorTable* mColorTable = nullptr;
122 bool mHasHardwareMipMap = false;
123
124 virtual void internal_dispose() const override {
125 mBitmap.onStrongRefDestroyed();
126 }
127};
128
129Bitmap::Bitmap(JNIEnv* env, jbyteArray storageObj, void* address,
130 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
131 : mPixelStorageType(PixelStorageType::Java) {
132 env->GetJavaVM(&mPixelStorage.java.jvm);
133 mPixelStorage.java.jweakRef = env->NewWeakGlobalRef(storageObj);
134 mPixelStorage.java.jstrongRef = nullptr;
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
141Bitmap::Bitmap(void* address, void* context, FreeFunc freeFunc,
142 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
143 : mPixelStorageType(PixelStorageType::External) {
144 mPixelStorage.external.address = address;
145 mPixelStorage.external.context = context;
146 mPixelStorage.external.freeFunc = freeFunc;
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}
152
Riley Andrews39d7f302014-11-13 17:43:25 -0800153Bitmap::Bitmap(void* address, int fd,
154 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
155 : mPixelStorageType(PixelStorageType::Ashmem) {
156 mPixelStorage.ashmem.address = address;
157 mPixelStorage.ashmem.fd = fd;
158 mPixelStorage.ashmem.size = ashmem_get_size_region(fd);
159 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
160 // Note: this will trigger a call to onStrongRefDestroyed(), but
161 // we want the pixel ref to have a ref count of 0 at this point
162 mPixelRef->unref();
163}
John Reckf29ed282015-04-07 07:32:03 -0700164Bitmap::~Bitmap() {
165 doFreePixels();
166}
167
168void Bitmap::freePixels() {
169 AutoMutex _lock(mLock);
170 if (mPinnedRefCount == 0) {
171 doFreePixels();
172 mPixelStorageType = PixelStorageType::Invalid;
173 }
174}
175
176void Bitmap::doFreePixels() {
177 switch (mPixelStorageType) {
178 case PixelStorageType::Invalid:
179 // already free'd, nothing to do
180 break;
181 case PixelStorageType::External:
182 mPixelStorage.external.freeFunc(mPixelStorage.external.address,
183 mPixelStorage.external.context);
184 break;
Riley Andrews39d7f302014-11-13 17:43:25 -0800185 case PixelStorageType::Ashmem:
186 munmap(mPixelStorage.ashmem.address, mPixelStorage.ashmem.size);
187 close(mPixelStorage.ashmem.fd);
188 break;
John Reckf29ed282015-04-07 07:32:03 -0700189 case PixelStorageType::Java:
190 JNIEnv* env = jniEnv();
191 LOG_ALWAYS_FATAL_IF(mPixelStorage.java.jstrongRef,
192 "Deleting a bitmap wrapper while there are outstanding strong "
193 "references! mPinnedRefCount = %d", mPinnedRefCount);
194 env->DeleteWeakGlobalRef(mPixelStorage.java.jweakRef);
195 break;
196 }
197
198 if (android::uirenderer::Caches::hasInstance()) {
199 android::uirenderer::Caches::getInstance().textureCache.releaseTexture(
200 mPixelRef->getStableID());
201 }
202}
203
204bool Bitmap::hasHardwareMipMap() {
205 return mPixelRef->hasHardwareMipMap();
206}
207
208void Bitmap::setHasHardwareMipMap(bool hasMipMap) {
209 mPixelRef->setHasHardwareMipMap(hasMipMap);
210}
211
Riley Andrews39d7f302014-11-13 17:43:25 -0800212int Bitmap::getAshmemFd() const {
213 switch (mPixelStorageType) {
214 case PixelStorageType::Ashmem:
215 return mPixelStorage.ashmem.fd;
216 default:
217 return -1;
218 }
219}
220
John Reckf29ed282015-04-07 07:32:03 -0700221const SkImageInfo& Bitmap::info() const {
John Reckf29ed282015-04-07 07:32:03 -0700222 return mPixelRef->info();
223}
224
225size_t Bitmap::rowBytes() const {
226 return mPixelRef->rowBytes();
227}
228
John Reckae2e8b42015-05-06 14:55:05 -0700229SkPixelRef* Bitmap::peekAtPixelRef() const {
John Reckf29ed282015-04-07 07:32:03 -0700230 assertValid();
231 return mPixelRef.get();
232}
233
John Reckae2e8b42015-05-06 14:55:05 -0700234SkPixelRef* Bitmap::refPixelRef() {
235 assertValid();
236 android::AutoMutex _lock(mLock);
237 return refPixelRefLocked();
238}
239
240SkPixelRef* Bitmap::refPixelRefLocked() {
241 mPixelRef->ref();
242 if (mPixelRef->unique()) {
243 // We just restored this from 0, pin the pixels and inc the strong count
244 // Note that there *might be* an incoming onStrongRefDestroyed from whatever
245 // last unref'd
246 pinPixelsLocked();
247 mPinnedRefCount++;
248 }
249 return mPixelRef.get();
250}
251
John Reckf29ed282015-04-07 07:32:03 -0700252void Bitmap::reconfigure(const SkImageInfo& info, size_t rowBytes,
253 SkColorTable* ctable) {
John Reckae2e8b42015-05-06 14:55:05 -0700254 {
255 android::AutoMutex _lock(mLock);
256 if (mPinnedRefCount) {
257 ALOGW("Called reconfigure on a bitmap that is in use! This may"
258 " cause graphical corruption!");
259 }
260 }
John Reckf29ed282015-04-07 07:32:03 -0700261 mPixelRef->reconfigure(info, rowBytes, ctable);
262}
263
264void Bitmap::reconfigure(const SkImageInfo& info) {
Derek Sollenberger2a94a102015-05-07 13:56:14 -0400265 reconfigure(info, info.minRowBytes(), nullptr);
John Reckf29ed282015-04-07 07:32:03 -0700266}
267
John Reck0781a2f2015-05-27 16:29:17 -0700268void Bitmap::setAlphaType(SkAlphaType alphaType) {
269 if (!SkColorTypeValidateAlphaType(info().colorType(), alphaType, &alphaType)) {
270 return;
271 }
272
273 mPixelRef->changeAlphaType(alphaType);
274}
275
John Reckf29ed282015-04-07 07:32:03 -0700276void Bitmap::detachFromJava() {
277 bool disposeSelf;
278 {
279 android::AutoMutex _lock(mLock);
280 mAttachedToJava = false;
281 disposeSelf = shouldDisposeSelfLocked();
282 }
283 if (disposeSelf) {
284 delete this;
285 }
286}
287
288bool Bitmap::shouldDisposeSelfLocked() {
289 return mPinnedRefCount == 0 && !mAttachedToJava;
290}
291
292JNIEnv* Bitmap::jniEnv() {
293 JNIEnv* env;
294 auto success = mPixelStorage.java.jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
295 LOG_ALWAYS_FATAL_IF(success != JNI_OK,
296 "Failed to get JNIEnv* from JVM: %p", mPixelStorage.java.jvm);
297 return env;
298}
299
300void Bitmap::onStrongRefDestroyed() {
301 bool disposeSelf = false;
302 {
303 android::AutoMutex _lock(mLock);
304 if (mPinnedRefCount > 0) {
305 mPinnedRefCount--;
306 if (mPinnedRefCount == 0) {
307 unpinPixelsLocked();
308 disposeSelf = shouldDisposeSelfLocked();
309 }
310 }
311 }
312 if (disposeSelf) {
313 delete this;
314 }
315}
316
317void Bitmap::pinPixelsLocked() {
318 switch (mPixelStorageType) {
319 case PixelStorageType::Invalid:
320 LOG_ALWAYS_FATAL("Cannot pin invalid pixels!");
321 break;
322 case PixelStorageType::External:
Riley Andrews39d7f302014-11-13 17:43:25 -0800323 case PixelStorageType::Ashmem:
John Reckf29ed282015-04-07 07:32:03 -0700324 // Nothing to do
325 break;
326 case PixelStorageType::Java: {
327 JNIEnv* env = jniEnv();
328 if (!mPixelStorage.java.jstrongRef) {
329 mPixelStorage.java.jstrongRef = reinterpret_cast<jbyteArray>(
330 env->NewGlobalRef(mPixelStorage.java.jweakRef));
331 if (!mPixelStorage.java.jstrongRef) {
332 LOG_ALWAYS_FATAL("Failed to acquire strong reference to pixels");
333 }
334 }
335 break;
336 }
337 }
338}
339
340void Bitmap::unpinPixelsLocked() {
341 switch (mPixelStorageType) {
342 case PixelStorageType::Invalid:
343 LOG_ALWAYS_FATAL("Cannot unpin invalid pixels!");
344 break;
345 case PixelStorageType::External:
Riley Andrews39d7f302014-11-13 17:43:25 -0800346 case PixelStorageType::Ashmem:
John Reckf29ed282015-04-07 07:32:03 -0700347 // Don't need to do anything
348 break;
349 case PixelStorageType::Java: {
350 JNIEnv* env = jniEnv();
351 if (mPixelStorage.java.jstrongRef) {
352 env->DeleteGlobalRef(mPixelStorage.java.jstrongRef);
353 mPixelStorage.java.jstrongRef = nullptr;
354 }
355 break;
356 }
357 }
358}
359
360void Bitmap::getSkBitmap(SkBitmap* outBitmap) {
361 assertValid();
362 android::AutoMutex _lock(mLock);
John Reckf29ed282015-04-07 07:32:03 -0700363 // Safe because mPixelRef is a WrappedPixelRef type, otherwise rowBytes()
364 // would require locking the pixels first.
365 outBitmap->setInfo(mPixelRef->info(), mPixelRef->rowBytes());
John Reckae2e8b42015-05-06 14:55:05 -0700366 outBitmap->setPixelRef(refPixelRefLocked())->unref();
John Reckf29ed282015-04-07 07:32:03 -0700367 outBitmap->setHasHardwareMipMap(hasHardwareMipMap());
368}
369
370void Bitmap::assertValid() const {
371 LOG_ALWAYS_FATAL_IF(mPixelStorageType == PixelStorageType::Invalid,
372 "Error, cannot access an invalid/free'd bitmap here!");
373}
374
375} // namespace android
376
377using namespace android;
378
379// Convenience class that does not take a global ref on the pixels, relying
380// on the caller already having a local JNI ref
381class LocalScopedBitmap {
382public:
383 LocalScopedBitmap(jlong bitmapHandle)
384 : mBitmap(reinterpret_cast<Bitmap*>(bitmapHandle)) {}
385
386 Bitmap* operator->() {
387 return mBitmap;
388 }
389
390 void* pixels() {
John Reckae2e8b42015-05-06 14:55:05 -0700391 return mBitmap->peekAtPixelRef()->pixels();
John Reckf29ed282015-04-07 07:32:03 -0700392 }
393
394 bool valid() {
395 return mBitmap && mBitmap->valid();
396 }
397
398private:
399 Bitmap* mBitmap;
400};
401
Chris Craik32054b02014-05-09 13:58:56 -0700402///////////////////////////////////////////////////////////////////////////////
403// Conversions to/from SkColor, for get/setPixels, and the create method, which
404// is basically like setPixels
405
406typedef void (*FromColorProc)(void* dst, const SkColor src[], int width,
407 int x, int y);
408
409static void FromColor_D32(void* dst, const SkColor src[], int width,
410 int, int) {
411 SkPMColor* d = (SkPMColor*)dst;
412
413 for (int i = 0; i < width; i++) {
414 *d++ = SkPreMultiplyColor(*src++);
415 }
416}
417
418static void FromColor_D32_Raw(void* dst, const SkColor src[], int width,
419 int, int) {
Dan Albert46d84442014-11-18 16:07:51 -0800420 // Needed to thwart the unreachable code detection from clang.
421 static const bool sk_color_ne_zero = SK_COLOR_MATCHES_PMCOLOR_BYTE_ORDER;
422
Chris Craik32054b02014-05-09 13:58:56 -0700423 // SkColor's ordering may be different from SkPMColor
Dan Albert46d84442014-11-18 16:07:51 -0800424 if (sk_color_ne_zero) {
Chris Craik32054b02014-05-09 13:58:56 -0700425 memcpy(dst, src, width * sizeof(SkColor));
426 return;
427 }
428
429 // order isn't same, repack each pixel manually
430 SkPMColor* d = (SkPMColor*)dst;
431 for (int i = 0; i < width; i++) {
432 SkColor c = *src++;
433 *d++ = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c),
434 SkColorGetG(c), SkColorGetB(c));
435 }
436}
437
438static void FromColor_D565(void* dst, const SkColor src[], int width,
439 int x, int y) {
440 uint16_t* d = (uint16_t*)dst;
441
442 DITHER_565_SCAN(y);
443 for (int stop = x + width; x < stop; x++) {
444 SkColor c = *src++;
445 *d++ = SkDitherRGBTo565(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c),
446 DITHER_VALUE(x));
447 }
448}
449
450static void FromColor_D4444(void* dst, const SkColor src[], int width,
451 int x, int y) {
452 SkPMColor16* d = (SkPMColor16*)dst;
453
454 DITHER_4444_SCAN(y);
455 for (int stop = x + width; x < stop; x++) {
456 SkPMColor pmc = SkPreMultiplyColor(*src++);
457 *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x));
458// *d++ = SkPixel32ToPixel4444(pmc);
459 }
460}
461
462static void FromColor_D4444_Raw(void* dst, const SkColor src[], int width,
463 int x, int y) {
464 SkPMColor16* d = (SkPMColor16*)dst;
465
466 DITHER_4444_SCAN(y);
467 for (int stop = x + width; x < stop; x++) {
468 SkColor c = *src++;
469
470 // SkPMColor is used because the ordering is ARGB32, even though the target actually premultiplied
471 SkPMColor pmc = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c),
472 SkColorGetG(c), SkColorGetB(c));
473 *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x));
474// *d++ = SkPixel32ToPixel4444(pmc);
475 }
476}
477
Chris Craik6260b222015-07-24 15:17:29 -0700478static void FromColor_DA8(void* dst, const SkColor src[], int width, int x, int y) {
479 uint8_t* d = (uint8_t*)dst;
480
481 for (int stop = x + width; x < stop; x++) {
482 *d++ = SkColorGetA(*src++);
483 }
484}
485
Chris Craik32054b02014-05-09 13:58:56 -0700486// can return NULL
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400487static FromColorProc ChooseFromColorProc(const SkBitmap& bitmap) {
488 switch (bitmap.colorType()) {
489 case kN32_SkColorType:
490 return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D32 : FromColor_D32_Raw;
491 case kARGB_4444_SkColorType:
492 return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D4444 :
493 FromColor_D4444_Raw;
494 case kRGB_565_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700495 return FromColor_D565;
Chris Craik6260b222015-07-24 15:17:29 -0700496 case kAlpha_8_SkColorType:
497 return FromColor_DA8;
Chris Craik32054b02014-05-09 13:58:56 -0700498 default:
499 break;
500 }
501 return NULL;
502}
503
504bool GraphicsJNI::SetPixels(JNIEnv* env, jintArray srcColors, int srcOffset, int srcStride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400505 int x, int y, int width, int height, const SkBitmap& dstBitmap) {
Chris Craik32054b02014-05-09 13:58:56 -0700506 SkAutoLockPixels alp(dstBitmap);
507 void* dst = dstBitmap.getPixels();
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400508 FromColorProc proc = ChooseFromColorProc(dstBitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700509
510 if (NULL == dst || NULL == proc) {
511 return false;
512 }
513
514 const jint* array = env->GetIntArrayElements(srcColors, NULL);
515 const SkColor* src = (const SkColor*)array + srcOffset;
516
517 // reset to to actual choice from caller
518 dst = dstBitmap.getAddr(x, y);
519 // now copy/convert each scanline
520 for (int y = 0; y < height; y++) {
521 proc(dst, src, width, x, y);
522 src += srcStride;
523 dst = (char*)dst + dstBitmap.rowBytes();
524 }
525
526 dstBitmap.notifyPixelsChanged();
527
528 env->ReleaseIntArrayElements(srcColors, const_cast<jint*>(array),
529 JNI_ABORT);
530 return true;
531}
532
533//////////////////// ToColor procs
534
535typedef void (*ToColorProc)(SkColor dst[], const void* src, int width,
536 SkColorTable*);
537
538static void ToColor_S32_Alpha(SkColor dst[], const void* src, int width,
539 SkColorTable*) {
540 SkASSERT(width > 0);
541 const SkPMColor* s = (const SkPMColor*)src;
542 do {
543 *dst++ = SkUnPreMultiply::PMColorToColor(*s++);
544 } while (--width != 0);
545}
546
547static void ToColor_S32_Raw(SkColor dst[], const void* src, int width,
548 SkColorTable*) {
549 SkASSERT(width > 0);
550 const SkPMColor* s = (const SkPMColor*)src;
551 do {
552 SkPMColor c = *s++;
553 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
554 SkGetPackedG32(c), SkGetPackedB32(c));
555 } while (--width != 0);
556}
557
558static void ToColor_S32_Opaque(SkColor dst[], const void* src, int width,
559 SkColorTable*) {
560 SkASSERT(width > 0);
561 const SkPMColor* s = (const SkPMColor*)src;
562 do {
563 SkPMColor c = *s++;
564 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
565 SkGetPackedB32(c));
566 } while (--width != 0);
567}
568
569static void ToColor_S4444_Alpha(SkColor dst[], const void* src, int width,
570 SkColorTable*) {
571 SkASSERT(width > 0);
572 const SkPMColor16* s = (const SkPMColor16*)src;
573 do {
574 *dst++ = SkUnPreMultiply::PMColorToColor(SkPixel4444ToPixel32(*s++));
575 } while (--width != 0);
576}
577
578static void ToColor_S4444_Raw(SkColor dst[], const void* src, int width,
579 SkColorTable*) {
580 SkASSERT(width > 0);
581 const SkPMColor16* s = (const SkPMColor16*)src;
582 do {
583 SkPMColor c = SkPixel4444ToPixel32(*s++);
584 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
585 SkGetPackedG32(c), SkGetPackedB32(c));
586 } while (--width != 0);
587}
588
589static void ToColor_S4444_Opaque(SkColor dst[], const void* src, int width,
590 SkColorTable*) {
591 SkASSERT(width > 0);
592 const SkPMColor16* s = (const SkPMColor16*)src;
593 do {
594 SkPMColor c = SkPixel4444ToPixel32(*s++);
595 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
596 SkGetPackedB32(c));
597 } while (--width != 0);
598}
599
600static void ToColor_S565(SkColor dst[], const void* src, int width,
601 SkColorTable*) {
602 SkASSERT(width > 0);
603 const uint16_t* s = (const uint16_t*)src;
604 do {
605 uint16_t c = *s++;
606 *dst++ = SkColorSetRGB(SkPacked16ToR32(c), SkPacked16ToG32(c),
607 SkPacked16ToB32(c));
608 } while (--width != 0);
609}
610
611static void ToColor_SI8_Alpha(SkColor dst[], const void* src, int width,
612 SkColorTable* ctable) {
613 SkASSERT(width > 0);
614 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500615 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700616 do {
617 *dst++ = SkUnPreMultiply::PMColorToColor(colors[*s++]);
618 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700619}
620
621static void ToColor_SI8_Raw(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++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
629 SkGetPackedG32(c), SkGetPackedB32(c));
630 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700631}
632
633static void ToColor_SI8_Opaque(SkColor dst[], const void* src, int width,
634 SkColorTable* ctable) {
635 SkASSERT(width > 0);
636 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500637 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700638 do {
639 SkPMColor c = colors[*s++];
640 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
641 SkGetPackedB32(c));
642 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700643}
644
Chris Craik6260b222015-07-24 15:17:29 -0700645static void ToColor_SA8(SkColor dst[], const void* src, int width, SkColorTable*) {
646 SkASSERT(width > 0);
647 const uint8_t* s = (const uint8_t*)src;
648 do {
649 uint8_t c = *s++;
650 *dst++ = SkColorSetARGB(c, c, c, c);
651 } while (--width != 0);
652}
653
Chris Craik32054b02014-05-09 13:58:56 -0700654// can return NULL
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400655static ToColorProc ChooseToColorProc(const SkBitmap& src) {
Mike Reedb9330552014-06-16 17:31:48 -0400656 switch (src.colorType()) {
657 case kN32_SkColorType:
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400658 switch (src.alphaType()) {
659 case kOpaque_SkAlphaType:
660 return ToColor_S32_Opaque;
661 case kPremul_SkAlphaType:
662 return ToColor_S32_Alpha;
663 case kUnpremul_SkAlphaType:
664 return ToColor_S32_Raw;
665 default:
666 return NULL;
667 }
Mike Reedb9330552014-06-16 17:31:48 -0400668 case kARGB_4444_SkColorType:
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400669 switch (src.alphaType()) {
670 case kOpaque_SkAlphaType:
671 return ToColor_S4444_Opaque;
672 case kPremul_SkAlphaType:
673 return ToColor_S4444_Alpha;
674 case kUnpremul_SkAlphaType:
675 return ToColor_S4444_Raw;
676 default:
677 return NULL;
678 }
Mike Reedb9330552014-06-16 17:31:48 -0400679 case kRGB_565_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700680 return ToColor_S565;
Mike Reedb9330552014-06-16 17:31:48 -0400681 case kIndex_8_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700682 if (src.getColorTable() == NULL) {
683 return NULL;
684 }
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400685 switch (src.alphaType()) {
686 case kOpaque_SkAlphaType:
687 return ToColor_SI8_Opaque;
688 case kPremul_SkAlphaType:
689 return ToColor_SI8_Alpha;
690 case kUnpremul_SkAlphaType:
691 return ToColor_SI8_Raw;
692 default:
693 return NULL;
694 }
Chris Craik6260b222015-07-24 15:17:29 -0700695 case kAlpha_8_SkColorType:
696 return ToColor_SA8;
Chris Craik32054b02014-05-09 13:58:56 -0700697 default:
698 break;
699 }
700 return NULL;
701}
702
703///////////////////////////////////////////////////////////////////////////////
704///////////////////////////////////////////////////////////////////////////////
705
706static int getPremulBitmapCreateFlags(bool isMutable) {
707 int flags = GraphicsJNI::kBitmapCreateFlag_Premultiplied;
708 if (isMutable) flags |= GraphicsJNI::kBitmapCreateFlag_Mutable;
709 return flags;
710}
711
712static jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors,
713 jint offset, jint stride, jint width, jint height,
714 jint configHandle, jboolean isMutable) {
Mike Reed1103b322014-07-08 12:36:44 -0400715 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700716 if (NULL != jColors) {
717 size_t n = env->GetArrayLength(jColors);
718 if (n < SkAbs32(stride) * (size_t)height) {
719 doThrowAIOOBE(env);
720 return NULL;
721 }
722 }
723
724 // ARGB_4444 is a deprecated format, convert automatically to 8888
Mike Reedb9330552014-06-16 17:31:48 -0400725 if (colorType == kARGB_4444_SkColorType) {
726 colorType = kN32_SkColorType;
Chris Craik32054b02014-05-09 13:58:56 -0700727 }
728
729 SkBitmap bitmap;
Mike Reedb9330552014-06-16 17:31:48 -0400730 bitmap.setInfo(SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType));
Chris Craik32054b02014-05-09 13:58:56 -0700731
John Reckf29ed282015-04-07 07:32:03 -0700732 Bitmap* nativeBitmap = GraphicsJNI::allocateJavaPixelRef(env, &bitmap, NULL);
733 if (!nativeBitmap) {
Chris Craik32054b02014-05-09 13:58:56 -0700734 return NULL;
735 }
736
737 if (jColors != NULL) {
738 GraphicsJNI::SetPixels(env, jColors, offset, stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400739 0, 0, width, height, bitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700740 }
741
John Reckf29ed282015-04-07 07:32:03 -0700742 return GraphicsJNI::createBitmap(env, nativeBitmap,
743 getPremulBitmapCreateFlags(isMutable));
Chris Craik32054b02014-05-09 13:58:56 -0700744}
745
746static jobject Bitmap_copy(JNIEnv* env, jobject, jlong srcHandle,
747 jint dstConfigHandle, jboolean isMutable) {
John Reckf29ed282015-04-07 07:32:03 -0700748 SkBitmap src;
749 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
Mike Reed1103b322014-07-08 12:36:44 -0400750 SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700751 SkBitmap result;
752 JavaPixelAllocator allocator(env);
753
John Reckf29ed282015-04-07 07:32:03 -0700754 if (!src.copyTo(&result, dstCT, &allocator)) {
Chris Craik32054b02014-05-09 13:58:56 -0700755 return NULL;
756 }
John Reckf29ed282015-04-07 07:32:03 -0700757 Bitmap* bitmap = allocator.getStorageObjAndReset();
758 return GraphicsJNI::createBitmap(env, bitmap,
759 getPremulBitmapCreateFlags(isMutable));
Chris Craik32054b02014-05-09 13:58:56 -0700760}
761
Riley Andrews721ae5f2015-05-11 16:08:22 -0700762static jobject Bitmap_copyAshmem(JNIEnv* env, jobject, jlong srcHandle) {
763 SkBitmap src;
764 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
765 SkBitmap result;
766
767 AshmemPixelAllocator allocator(env);
768 if (!src.copyTo(&result, &allocator)) {
769 return NULL;
770 }
771 Bitmap* bitmap = allocator.getStorageObjAndReset();
772 bitmap->peekAtPixelRef()->setImmutable();
773 jobject ret = GraphicsJNI::createBitmap(env, bitmap, getPremulBitmapCreateFlags(false));
774 return ret;
775}
776
Richard Uhler775873a2015-12-29 12:37:39 -0800777static void Bitmap_destruct(Bitmap* bitmap) {
John Reckf29ed282015-04-07 07:32:03 -0700778 bitmap->detachFromJava();
Chris Craik32054b02014-05-09 13:58:56 -0700779}
780
Richard Uhler775873a2015-12-29 12:37:39 -0800781static jlong Bitmap_getNativeFinalizer(JNIEnv*, jobject) {
782 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&Bitmap_destruct));
783}
784
Chris Craik32054b02014-05-09 13:58:56 -0700785static jboolean Bitmap_recycle(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700786 LocalScopedBitmap bitmap(bitmapHandle);
787 bitmap->freePixels();
Chris Craik32054b02014-05-09 13:58:56 -0700788 return JNI_TRUE;
789}
790
791static void Bitmap_reconfigure(JNIEnv* env, jobject clazz, jlong bitmapHandle,
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400792 jint width, jint height, jint configHandle, jint allocSize,
793 jboolean requestPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700794 LocalScopedBitmap bitmap(bitmapHandle);
Mike Reed1103b322014-07-08 12:36:44 -0400795 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400796
797 // ARGB_4444 is a deprecated format, convert automatically to 8888
798 if (colorType == kARGB_4444_SkColorType) {
799 colorType = kN32_SkColorType;
800 }
801
802 if (width * height * SkColorTypeBytesPerPixel(colorType) > allocSize) {
Chris Craik32054b02014-05-09 13:58:56 -0700803 // done in native as there's no way to get BytesPerPixel in Java
804 doThrowIAE(env, "Bitmap not large enough to support new configuration");
805 return;
806 }
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400807 SkAlphaType alphaType;
John Reckf29ed282015-04-07 07:32:03 -0700808 if (bitmap->info().colorType() != kRGB_565_SkColorType
809 && bitmap->info().alphaType() == kOpaque_SkAlphaType) {
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400810 // If the original bitmap was set to opaque, keep that setting, unless it
811 // was 565, which is required to be opaque.
812 alphaType = kOpaque_SkAlphaType;
813 } else {
814 // Otherwise respect the premultiplied request.
815 alphaType = requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
816 }
John Reckf29ed282015-04-07 07:32:03 -0700817 bitmap->reconfigure(SkImageInfo::Make(width, height, colorType, alphaType));
Chris Craik32054b02014-05-09 13:58:56 -0700818}
819
820// These must match the int values in Bitmap.java
821enum JavaEncodeFormat {
822 kJPEG_JavaEncodeFormat = 0,
823 kPNG_JavaEncodeFormat = 1,
824 kWEBP_JavaEncodeFormat = 2
825};
826
827static jboolean Bitmap_compress(JNIEnv* env, jobject clazz, jlong bitmapHandle,
828 jint format, jint quality,
829 jobject jstream, jbyteArray jstorage) {
John Reckf29ed282015-04-07 07:32:03 -0700830
831 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700832 SkImageEncoder::Type fm;
833
834 switch (format) {
835 case kJPEG_JavaEncodeFormat:
836 fm = SkImageEncoder::kJPEG_Type;
837 break;
838 case kPNG_JavaEncodeFormat:
839 fm = SkImageEncoder::kPNG_Type;
840 break;
841 case kWEBP_JavaEncodeFormat:
842 fm = SkImageEncoder::kWEBP_Type;
843 break;
844 default:
845 return JNI_FALSE;
846 }
847
John Reckf29ed282015-04-07 07:32:03 -0700848 if (!bitmap.valid()) {
849 return JNI_FALSE;
850 }
851
Chris Craik32054b02014-05-09 13:58:56 -0700852 bool success = false;
Chris Craik32054b02014-05-09 13:58:56 -0700853
John Reckf29ed282015-04-07 07:32:03 -0700854 std::unique_ptr<SkWStream> strm(CreateJavaOutputStreamAdaptor(env, jstream, jstorage));
855 if (!strm.get()) {
856 return JNI_FALSE;
857 }
Chris Craik32054b02014-05-09 13:58:56 -0700858
John Reckf29ed282015-04-07 07:32:03 -0700859 std::unique_ptr<SkImageEncoder> encoder(SkImageEncoder::Create(fm));
860 if (encoder.get()) {
861 SkBitmap skbitmap;
862 bitmap->getSkBitmap(&skbitmap);
863 success = encoder->encodeStream(strm.get(), skbitmap, quality);
Chris Craik32054b02014-05-09 13:58:56 -0700864 }
865 return success ? JNI_TRUE : JNI_FALSE;
866}
867
868static void Bitmap_erase(JNIEnv* env, jobject, jlong bitmapHandle, jint color) {
John Reckf29ed282015-04-07 07:32:03 -0700869 LocalScopedBitmap bitmap(bitmapHandle);
870 SkBitmap skBitmap;
871 bitmap->getSkBitmap(&skBitmap);
872 skBitmap.eraseColor(color);
Chris Craik32054b02014-05-09 13:58:56 -0700873}
874
875static jint Bitmap_rowBytes(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700876 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700877 return static_cast<jint>(bitmap->rowBytes());
878}
879
880static jint Bitmap_config(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700881 LocalScopedBitmap bitmap(bitmapHandle);
882 return GraphicsJNI::colorTypeToLegacyBitmapConfig(bitmap->info().colorType());
Chris Craik32054b02014-05-09 13:58:56 -0700883}
884
885static jint Bitmap_getGenerationId(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700886 LocalScopedBitmap bitmap(bitmapHandle);
John Reckae2e8b42015-05-06 14:55:05 -0700887 return static_cast<jint>(bitmap->peekAtPixelRef()->getGenerationID());
Chris Craik32054b02014-05-09 13:58:56 -0700888}
889
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400890static jboolean Bitmap_isPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700891 LocalScopedBitmap bitmap(bitmapHandle);
892 if (bitmap->info().alphaType() == kPremul_SkAlphaType) {
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400893 return JNI_TRUE;
894 }
895 return JNI_FALSE;
896}
897
Chris Craik32054b02014-05-09 13:58:56 -0700898static jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700899 LocalScopedBitmap bitmap(bitmapHandle);
900 return !bitmap->info().isOpaque() ? JNI_TRUE : JNI_FALSE;
Chris Craik32054b02014-05-09 13:58:56 -0700901}
902
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400903static void Bitmap_setHasAlpha(JNIEnv* env, jobject, jlong bitmapHandle,
904 jboolean hasAlpha, jboolean requestPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700905 LocalScopedBitmap bitmap(bitmapHandle);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400906 if (hasAlpha) {
John Reck0781a2f2015-05-27 16:29:17 -0700907 bitmap->setAlphaType(
John Reckf29ed282015-04-07 07:32:03 -0700908 requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType);
Chris Craik32054b02014-05-09 13:58:56 -0700909 } else {
John Reck0781a2f2015-05-27 16:29:17 -0700910 bitmap->setAlphaType(kOpaque_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400911 }
912}
913
914static void Bitmap_setPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle,
915 jboolean isPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700916 LocalScopedBitmap bitmap(bitmapHandle);
917 if (!bitmap->info().isOpaque()) {
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400918 if (isPremul) {
John Reck0781a2f2015-05-27 16:29:17 -0700919 bitmap->setAlphaType(kPremul_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400920 } else {
John Reck0781a2f2015-05-27 16:29:17 -0700921 bitmap->setAlphaType(kUnpremul_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400922 }
Chris Craik32054b02014-05-09 13:58:56 -0700923 }
924}
925
926static jboolean Bitmap_hasMipMap(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700927 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700928 return bitmap->hasHardwareMipMap() ? JNI_TRUE : JNI_FALSE;
929}
930
931static void Bitmap_setHasMipMap(JNIEnv* env, jobject, jlong bitmapHandle,
932 jboolean hasMipMap) {
John Reckf29ed282015-04-07 07:32:03 -0700933 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700934 bitmap->setHasHardwareMipMap(hasMipMap);
935}
936
937///////////////////////////////////////////////////////////////////////////////
938
939static jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) {
940 if (parcel == NULL) {
941 SkDebugf("-------- unparcel parcel is NULL\n");
942 return NULL;
943 }
944
945 android::Parcel* p = android::parcelForJavaObject(env, parcel);
946
Mike Reedb9330552014-06-16 17:31:48 -0400947 const bool isMutable = p->readInt32() != 0;
948 const SkColorType colorType = (SkColorType)p->readInt32();
949 const SkAlphaType alphaType = (SkAlphaType)p->readInt32();
950 const int width = p->readInt32();
951 const int height = p->readInt32();
952 const int rowBytes = p->readInt32();
953 const int density = p->readInt32();
Chris Craik32054b02014-05-09 13:58:56 -0700954
Mike Reedb9330552014-06-16 17:31:48 -0400955 if (kN32_SkColorType != colorType &&
956 kRGB_565_SkColorType != colorType &&
957 kARGB_4444_SkColorType != colorType &&
958 kIndex_8_SkColorType != colorType &&
959 kAlpha_8_SkColorType != colorType) {
960 SkDebugf("Bitmap_createFromParcel unknown colortype: %d\n", colorType);
Chris Craik32054b02014-05-09 13:58:56 -0700961 return NULL;
962 }
963
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400964 std::unique_ptr<SkBitmap> bitmap(new SkBitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700965
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400966 if (!bitmap->setInfo(SkImageInfo::Make(width, height, colorType, alphaType), rowBytes)) {
967 return NULL;
968 }
Chris Craik32054b02014-05-09 13:58:56 -0700969
970 SkColorTable* ctable = NULL;
Mike Reedb9330552014-06-16 17:31:48 -0400971 if (colorType == kIndex_8_SkColorType) {
Chris Craik32054b02014-05-09 13:58:56 -0700972 int count = p->readInt32();
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400973 if (count < 0 || count > 256) {
974 // The data is corrupt, since SkColorTable enforces a value between 0 and 256,
975 // inclusive.
976 return NULL;
977 }
Chris Craik32054b02014-05-09 13:58:56 -0700978 if (count > 0) {
979 size_t size = count * sizeof(SkPMColor);
980 const SkPMColor* src = (const SkPMColor*)p->readInplace(size);
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400981 if (src == NULL) {
982 return NULL;
983 }
Chris Craik32054b02014-05-09 13:58:56 -0700984 ctable = new SkColorTable(src, count);
985 }
986 }
987
Jeff Browna316c5d2015-06-05 15:14:06 -0700988 // Read the bitmap blob.
989 size_t size = bitmap->getSize();
990 android::Parcel::ReadableBlob blob;
991 android::status_t status = p->readBlob(size, &blob);
992 if (status) {
Chris Craik32054b02014-05-09 13:58:56 -0700993 SkSafeUnref(ctable);
Jeff Browna316c5d2015-06-05 15:14:06 -0700994 doThrowRE(env, "Could not read bitmap blob.");
Chris Craik32054b02014-05-09 13:58:56 -0700995 return NULL;
996 }
997
Jeff Browna316c5d2015-06-05 15:14:06 -0700998 // Map the bitmap in place from the ashmem region if possible otherwise copy.
999 Bitmap* nativeBitmap;
Riley Andrews8cee7c12015-11-01 23:36:04 -08001000 if (blob.fd() >= 0 && (blob.isMutable() || !isMutable) && (size >= ASHMEM_BITMAP_MIN_SIZE)) {
Jeff Browna316c5d2015-06-05 15:14:06 -07001001#if DEBUG_PARCEL
1002 ALOGD("Bitmap.createFromParcel: mapped contents of %s bitmap from %s blob "
1003 "(fds %s)",
1004 isMutable ? "mutable" : "immutable",
1005 blob.isMutable() ? "mutable" : "immutable",
1006 p->allowFds() ? "allowed" : "forbidden");
1007#endif
1008 // Dup the file descriptor so we can keep a reference to it after the Parcel
1009 // is disposed.
1010 int dupFd = dup(blob.fd());
1011 if (dupFd < 0) {
Erik Wolsheimer211abad2015-11-13 11:54:47 -08001012 ALOGE("Error allocating dup fd. Error:%d", errno);
Jeff Browna316c5d2015-06-05 15:14:06 -07001013 blob.release();
1014 SkSafeUnref(ctable);
1015 doThrowRE(env, "Could not allocate dup blob fd.");
1016 return NULL;
1017 }
1018
1019 // Map the pixels in place and take ownership of the ashmem region.
1020 nativeBitmap = GraphicsJNI::mapAshmemPixelRef(env, bitmap.get(),
1021 ctable, dupFd, const_cast<void*>(blob.data()), !isMutable);
1022 SkSafeUnref(ctable);
1023 if (!nativeBitmap) {
1024 close(dupFd);
1025 blob.release();
1026 doThrowRE(env, "Could not allocate ashmem pixel ref.");
1027 return NULL;
1028 }
1029
1030 // Clear the blob handle, don't release it.
1031 blob.clear();
1032 } else {
1033#if DEBUG_PARCEL
1034 if (blob.fd() >= 0) {
1035 ALOGD("Bitmap.createFromParcel: copied contents of mutable bitmap "
1036 "from immutable blob (fds %s)",
1037 p->allowFds() ? "allowed" : "forbidden");
1038 } else {
1039 ALOGD("Bitmap.createFromParcel: copied contents from %s blob "
1040 "(fds %s)",
1041 blob.isMutable() ? "mutable" : "immutable",
1042 p->allowFds() ? "allowed" : "forbidden");
1043 }
1044#endif
1045
1046 // Copy the pixels into a new buffer.
1047 nativeBitmap = GraphicsJNI::allocateJavaPixelRef(env, bitmap.get(), ctable);
1048 SkSafeUnref(ctable);
1049 if (!nativeBitmap) {
1050 blob.release();
1051 doThrowRE(env, "Could not allocate java pixel ref.");
1052 return NULL;
1053 }
1054 bitmap->lockPixels();
1055 memcpy(bitmap->getPixels(), blob.data(), size);
1056 bitmap->unlockPixels();
1057
1058 // Release the blob handle.
1059 blob.release();
Chris Craik32054b02014-05-09 13:58:56 -07001060 }
Chris Craik32054b02014-05-09 13:58:56 -07001061
John Reckf29ed282015-04-07 07:32:03 -07001062 return GraphicsJNI::createBitmap(env, nativeBitmap,
Leon Scroggins IIIec419e02015-03-11 13:12:06 -04001063 getPremulBitmapCreateFlags(isMutable), NULL, NULL, density);
Chris Craik32054b02014-05-09 13:58:56 -07001064}
1065
1066static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject,
1067 jlong bitmapHandle,
1068 jboolean isMutable, jint density,
1069 jobject parcel) {
Chris Craik32054b02014-05-09 13:58:56 -07001070 if (parcel == NULL) {
1071 SkDebugf("------- writeToParcel null parcel\n");
1072 return JNI_FALSE;
1073 }
1074
1075 android::Parcel* p = android::parcelForJavaObject(env, parcel);
John Reckf29ed282015-04-07 07:32:03 -07001076 SkBitmap bitmap;
Riley Andrews39d7f302014-11-13 17:43:25 -08001077
1078 android::Bitmap* androidBitmap = reinterpret_cast<Bitmap*>(bitmapHandle);
1079 androidBitmap->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001080
1081 p->writeInt32(isMutable);
John Reckf29ed282015-04-07 07:32:03 -07001082 p->writeInt32(bitmap.colorType());
1083 p->writeInt32(bitmap.alphaType());
1084 p->writeInt32(bitmap.width());
1085 p->writeInt32(bitmap.height());
1086 p->writeInt32(bitmap.rowBytes());
Chris Craik32054b02014-05-09 13:58:56 -07001087 p->writeInt32(density);
1088
John Reckf29ed282015-04-07 07:32:03 -07001089 if (bitmap.colorType() == kIndex_8_SkColorType) {
1090 SkColorTable* ctable = bitmap.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001091 if (ctable != NULL) {
1092 int count = ctable->count();
1093 p->writeInt32(count);
1094 memcpy(p->writeInplace(count * sizeof(SkPMColor)),
Mike Reed71487eb2014-11-19 16:13:20 -05001095 ctable->readColors(), count * sizeof(SkPMColor));
Chris Craik32054b02014-05-09 13:58:56 -07001096 } else {
1097 p->writeInt32(0); // indicate no ctable
1098 }
1099 }
1100
Jeff Browna316c5d2015-06-05 15:14:06 -07001101 // Transfer the underlying ashmem region if we have one and it's immutable.
1102 android::status_t status;
1103 int fd = androidBitmap->getAshmemFd();
1104 if (fd >= 0 && !isMutable && p->allowFds()) {
1105#if DEBUG_PARCEL
1106 ALOGD("Bitmap.writeToParcel: transferring immutable bitmap's ashmem fd as "
1107 "immutable blob (fds %s)",
1108 p->allowFds() ? "allowed" : "forbidden");
1109#endif
1110
1111 status = p->writeDupImmutableBlobFileDescriptor(fd);
1112 if (status) {
1113 doThrowRE(env, "Could not write bitmap blob file descriptor.");
Riley Andrews39d7f302014-11-13 17:43:25 -08001114 return JNI_FALSE;
1115 }
Jeff Browna316c5d2015-06-05 15:14:06 -07001116 return JNI_TRUE;
Riley Andrews39d7f302014-11-13 17:43:25 -08001117 }
Jeff Browna316c5d2015-06-05 15:14:06 -07001118
1119 // Copy the bitmap to a new blob.
1120 bool mutableCopy = isMutable;
1121#if DEBUG_PARCEL
1122 ALOGD("Bitmap.writeToParcel: copying %s bitmap into new %s blob (fds %s)",
1123 isMutable ? "mutable" : "immutable",
1124 mutableCopy ? "mutable" : "immutable",
1125 p->allowFds() ? "allowed" : "forbidden");
1126#endif
1127
1128 size_t size = bitmap.getSize();
1129 android::Parcel::WritableBlob blob;
1130 status = p->writeBlob(size, mutableCopy, &blob);
1131 if (status) {
1132 doThrowRE(env, "Could not copy bitmap to parcel blob.");
1133 return JNI_FALSE;
1134 }
1135
1136 bitmap.lockPixels();
1137 const void* pSrc = bitmap.getPixels();
1138 if (pSrc == NULL) {
1139 memset(blob.data(), 0, size);
1140 } else {
1141 memcpy(blob.data(), pSrc, size);
1142 }
1143 bitmap.unlockPixels();
1144
1145 blob.release();
Chris Craik32054b02014-05-09 13:58:56 -07001146 return JNI_TRUE;
1147}
1148
1149static jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz,
1150 jlong srcHandle, jlong paintHandle,
1151 jintArray offsetXY) {
John Reckf29ed282015-04-07 07:32:03 -07001152 SkBitmap src;
1153 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -04001154 const android::Paint* paint = reinterpret_cast<android::Paint*>(paintHandle);
Chris Craik32054b02014-05-09 13:58:56 -07001155 SkIPoint offset;
John Reckf29ed282015-04-07 07:32:03 -07001156 SkBitmap dst;
Chris Craik32054b02014-05-09 13:58:56 -07001157 JavaPixelAllocator allocator(env);
1158
John Reckf29ed282015-04-07 07:32:03 -07001159 src.extractAlpha(&dst, paint, &allocator, &offset);
Chris Craik32054b02014-05-09 13:58:56 -07001160 // If Skia can't allocate pixels for destination bitmap, it resets
1161 // it, that is set its pixels buffer to NULL, and zero width and height.
John Reckf29ed282015-04-07 07:32:03 -07001162 if (dst.getPixels() == NULL && src.getPixels() != NULL) {
Chris Craik32054b02014-05-09 13:58:56 -07001163 doThrowOOME(env, "failed to allocate pixels for alpha");
1164 return NULL;
1165 }
1166 if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) {
1167 int* array = env->GetIntArrayElements(offsetXY, NULL);
1168 array[0] = offset.fX;
1169 array[1] = offset.fY;
1170 env->ReleaseIntArrayElements(offsetXY, array, 0);
1171 }
1172
John Reckf29ed282015-04-07 07:32:03 -07001173 return GraphicsJNI::createBitmap(env, allocator.getStorageObjAndReset(),
1174 getPremulBitmapCreateFlags(true));
Chris Craik32054b02014-05-09 13:58:56 -07001175}
1176
1177///////////////////////////////////////////////////////////////////////////////
1178
1179static jint Bitmap_getPixel(JNIEnv* env, jobject, jlong bitmapHandle,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001180 jint x, jint y) {
John Reckf29ed282015-04-07 07:32:03 -07001181 SkBitmap bitmap;
1182 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1183 SkAutoLockPixels alp(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001184
John Reckf29ed282015-04-07 07:32:03 -07001185 ToColorProc proc = ChooseToColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001186 if (NULL == proc) {
1187 return 0;
1188 }
John Reckf29ed282015-04-07 07:32:03 -07001189 const void* src = bitmap.getAddr(x, y);
Chris Craik32054b02014-05-09 13:58:56 -07001190 if (NULL == src) {
1191 return 0;
1192 }
1193
1194 SkColor dst[1];
John Reckf29ed282015-04-07 07:32:03 -07001195 proc(dst, src, 1, bitmap.getColorTable());
Chris Craik32054b02014-05-09 13:58:56 -07001196 return static_cast<jint>(dst[0]);
1197}
1198
1199static void Bitmap_getPixels(JNIEnv* env, jobject, jlong bitmapHandle,
1200 jintArray pixelArray, jint offset, jint stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001201 jint x, jint y, jint width, jint height) {
John Reckf29ed282015-04-07 07:32:03 -07001202 SkBitmap bitmap;
1203 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1204 SkAutoLockPixels alp(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001205
John Reckf29ed282015-04-07 07:32:03 -07001206 ToColorProc proc = ChooseToColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001207 if (NULL == proc) {
1208 return;
1209 }
John Reckf29ed282015-04-07 07:32:03 -07001210 const void* src = bitmap.getAddr(x, y);
Chris Craik32054b02014-05-09 13:58:56 -07001211 if (NULL == src) {
1212 return;
1213 }
1214
John Reckf29ed282015-04-07 07:32:03 -07001215 SkColorTable* ctable = bitmap.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001216 jint* dst = env->GetIntArrayElements(pixelArray, NULL);
1217 SkColor* d = (SkColor*)dst + offset;
1218 while (--height >= 0) {
1219 proc(d, src, width, ctable);
1220 d += stride;
John Reckf29ed282015-04-07 07:32:03 -07001221 src = (void*)((const char*)src + bitmap.rowBytes());
Chris Craik32054b02014-05-09 13:58:56 -07001222 }
1223 env->ReleaseIntArrayElements(pixelArray, dst, 0);
1224}
1225
1226///////////////////////////////////////////////////////////////////////////////
1227
1228static void Bitmap_setPixel(JNIEnv* env, jobject, jlong bitmapHandle,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001229 jint x, jint y, jint colorHandle) {
John Reckf29ed282015-04-07 07:32:03 -07001230 SkBitmap bitmap;
1231 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001232 SkColor color = static_cast<SkColor>(colorHandle);
John Reckf29ed282015-04-07 07:32:03 -07001233 SkAutoLockPixels alp(bitmap);
1234 if (NULL == bitmap.getPixels()) {
Chris Craik32054b02014-05-09 13:58:56 -07001235 return;
1236 }
1237
John Reckf29ed282015-04-07 07:32:03 -07001238 FromColorProc proc = ChooseFromColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001239 if (NULL == proc) {
1240 return;
1241 }
1242
John Reckf29ed282015-04-07 07:32:03 -07001243 proc(bitmap.getAddr(x, y), &color, 1, x, y);
1244 bitmap.notifyPixelsChanged();
Chris Craik32054b02014-05-09 13:58:56 -07001245}
1246
1247static void Bitmap_setPixels(JNIEnv* env, jobject, jlong bitmapHandle,
1248 jintArray pixelArray, jint offset, jint stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001249 jint x, jint y, jint width, jint height) {
John Reckf29ed282015-04-07 07:32:03 -07001250 SkBitmap bitmap;
1251 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001252 GraphicsJNI::SetPixels(env, pixelArray, offset, stride,
John Reckf29ed282015-04-07 07:32:03 -07001253 x, y, width, height, bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001254}
1255
1256static void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject,
1257 jlong bitmapHandle, jobject jbuffer) {
John Reckf29ed282015-04-07 07:32:03 -07001258 SkBitmap bitmap;
1259 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1260 SkAutoLockPixels alp(bitmap);
1261 const void* src = bitmap.getPixels();
Chris Craik32054b02014-05-09 13:58:56 -07001262
1263 if (NULL != src) {
1264 android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE);
1265
1266 // the java side has already checked that buffer is large enough
John Reckf29ed282015-04-07 07:32:03 -07001267 memcpy(abp.pointer(), src, bitmap.getSize());
Chris Craik32054b02014-05-09 13:58:56 -07001268 }
1269}
1270
1271static void Bitmap_copyPixelsFromBuffer(JNIEnv* env, jobject,
1272 jlong bitmapHandle, jobject jbuffer) {
John Reckf29ed282015-04-07 07:32:03 -07001273 SkBitmap bitmap;
1274 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1275 SkAutoLockPixels alp(bitmap);
1276 void* dst = bitmap.getPixels();
Chris Craik32054b02014-05-09 13:58:56 -07001277
1278 if (NULL != dst) {
1279 android::AutoBufferPointer abp(env, jbuffer, JNI_FALSE);
1280 // the java side has already checked that buffer is large enough
John Reckf29ed282015-04-07 07:32:03 -07001281 memcpy(dst, abp.pointer(), bitmap.getSize());
1282 bitmap.notifyPixelsChanged();
Chris Craik32054b02014-05-09 13:58:56 -07001283 }
1284}
1285
1286static jboolean Bitmap_sameAs(JNIEnv* env, jobject, jlong bm0Handle,
1287 jlong bm1Handle) {
John Reckf29ed282015-04-07 07:32:03 -07001288 SkBitmap bm0;
1289 SkBitmap bm1;
1290 reinterpret_cast<Bitmap*>(bm0Handle)->getSkBitmap(&bm0);
1291 reinterpret_cast<Bitmap*>(bm1Handle)->getSkBitmap(&bm1);
1292 if (bm0.width() != bm1.width() ||
1293 bm0.height() != bm1.height() ||
1294 bm0.colorType() != bm1.colorType()) {
Chris Craik32054b02014-05-09 13:58:56 -07001295 return JNI_FALSE;
1296 }
1297
John Reckf29ed282015-04-07 07:32:03 -07001298 SkAutoLockPixels alp0(bm0);
1299 SkAutoLockPixels alp1(bm1);
Chris Craik32054b02014-05-09 13:58:56 -07001300
1301 // if we can't load the pixels, return false
John Reckf29ed282015-04-07 07:32:03 -07001302 if (NULL == bm0.getPixels() || NULL == bm1.getPixels()) {
Chris Craik32054b02014-05-09 13:58:56 -07001303 return JNI_FALSE;
1304 }
1305
John Reckf29ed282015-04-07 07:32:03 -07001306 if (bm0.colorType() == kIndex_8_SkColorType) {
1307 SkColorTable* ct0 = bm0.getColorTable();
1308 SkColorTable* ct1 = bm1.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001309 if (NULL == ct0 || NULL == ct1) {
1310 return JNI_FALSE;
1311 }
1312 if (ct0->count() != ct1->count()) {
1313 return JNI_FALSE;
1314 }
1315
Chris Craik32054b02014-05-09 13:58:56 -07001316 const size_t size = ct0->count() * sizeof(SkPMColor);
Mike Reed71487eb2014-11-19 16:13:20 -05001317 if (memcmp(ct0->readColors(), ct1->readColors(), size) != 0) {
Chris Craik32054b02014-05-09 13:58:56 -07001318 return JNI_FALSE;
1319 }
1320 }
1321
1322 // now compare each scanline. We can't do the entire buffer at once,
1323 // since we don't care about the pixel values that might extend beyond
1324 // the width (since the scanline might be larger than the logical width)
John Reckf29ed282015-04-07 07:32:03 -07001325 const int h = bm0.height();
1326 const size_t size = bm0.width() * bm0.bytesPerPixel();
Chris Craik32054b02014-05-09 13:58:56 -07001327 for (int y = 0; y < h; y++) {
henry.uh_chen53001ca2014-07-03 20:40:22 +08001328 // SkBitmap::getAddr(int, int) may return NULL due to unrecognized config
1329 // (ex: kRLE_Index8_Config). This will cause memcmp method to crash. Since bm0
1330 // and bm1 both have pixel data() (have passed NULL == getPixels() check),
1331 // those 2 bitmaps should be valid (only unrecognized), we return JNI_FALSE
1332 // to warn user those 2 unrecognized config bitmaps may be different.
John Reckf29ed282015-04-07 07:32:03 -07001333 void *bm0Addr = bm0.getAddr(0, y);
1334 void *bm1Addr = bm1.getAddr(0, y);
henry.uh_chen53001ca2014-07-03 20:40:22 +08001335
1336 if(bm0Addr == NULL || bm1Addr == NULL) {
1337 return JNI_FALSE;
1338 }
1339
1340 if (memcmp(bm0Addr, bm1Addr, size) != 0) {
Chris Craik32054b02014-05-09 13:58:56 -07001341 return JNI_FALSE;
1342 }
1343 }
1344 return JNI_TRUE;
1345}
1346
John Reckc6e2e8f2015-04-15 13:24:47 -07001347static jlong Bitmap_refPixelRef(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -07001348 LocalScopedBitmap bitmap(bitmapHandle);
John Reckae2e8b42015-05-06 14:55:05 -07001349 SkPixelRef* pixelRef = bitmap.valid() ? bitmap->peekAtPixelRef() : nullptr;
John Reckc6e2e8f2015-04-15 13:24:47 -07001350 SkSafeRef(pixelRef);
1351 return reinterpret_cast<jlong>(pixelRef);
1352}
1353
Chris Craik32054b02014-05-09 13:58:56 -07001354///////////////////////////////////////////////////////////////////////////////
1355
Daniel Micay76f6a862015-09-19 17:31:01 -04001356static const JNINativeMethod gBitmapMethods[] = {
Chris Craik32054b02014-05-09 13:58:56 -07001357 { "nativeCreate", "([IIIIIIZ)Landroid/graphics/Bitmap;",
1358 (void*)Bitmap_creator },
1359 { "nativeCopy", "(JIZ)Landroid/graphics/Bitmap;",
1360 (void*)Bitmap_copy },
Riley Andrews721ae5f2015-05-11 16:08:22 -07001361 { "nativeCopyAshmem", "(J)Landroid/graphics/Bitmap;",
1362 (void*)Bitmap_copyAshmem },
Richard Uhler775873a2015-12-29 12:37:39 -08001363 { "nativeGetNativeFinalizer", "()J", (void*)Bitmap_getNativeFinalizer },
Chris Craik32054b02014-05-09 13:58:56 -07001364 { "nativeRecycle", "(J)Z", (void*)Bitmap_recycle },
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -04001365 { "nativeReconfigure", "(JIIIIZ)V", (void*)Bitmap_reconfigure },
Chris Craik32054b02014-05-09 13:58:56 -07001366 { "nativeCompress", "(JIILjava/io/OutputStream;[B)Z",
1367 (void*)Bitmap_compress },
1368 { "nativeErase", "(JI)V", (void*)Bitmap_erase },
1369 { "nativeRowBytes", "(J)I", (void*)Bitmap_rowBytes },
1370 { "nativeConfig", "(J)I", (void*)Bitmap_config },
1371 { "nativeHasAlpha", "(J)Z", (void*)Bitmap_hasAlpha },
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001372 { "nativeIsPremultiplied", "(J)Z", (void*)Bitmap_isPremultiplied},
1373 { "nativeSetHasAlpha", "(JZZ)V", (void*)Bitmap_setHasAlpha},
1374 { "nativeSetPremultiplied", "(JZ)V", (void*)Bitmap_setPremultiplied},
Chris Craik32054b02014-05-09 13:58:56 -07001375 { "nativeHasMipMap", "(J)Z", (void*)Bitmap_hasMipMap },
1376 { "nativeSetHasMipMap", "(JZ)V", (void*)Bitmap_setHasMipMap },
1377 { "nativeCreateFromParcel",
1378 "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;",
1379 (void*)Bitmap_createFromParcel },
1380 { "nativeWriteToParcel", "(JZILandroid/os/Parcel;)Z",
1381 (void*)Bitmap_writeToParcel },
1382 { "nativeExtractAlpha", "(JJ[I)Landroid/graphics/Bitmap;",
1383 (void*)Bitmap_extractAlpha },
1384 { "nativeGenerationId", "(J)I", (void*)Bitmap_getGenerationId },
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001385 { "nativeGetPixel", "(JII)I", (void*)Bitmap_getPixel },
1386 { "nativeGetPixels", "(J[IIIIIII)V", (void*)Bitmap_getPixels },
1387 { "nativeSetPixel", "(JIII)V", (void*)Bitmap_setPixel },
1388 { "nativeSetPixels", "(J[IIIIIII)V", (void*)Bitmap_setPixels },
Chris Craik32054b02014-05-09 13:58:56 -07001389 { "nativeCopyPixelsToBuffer", "(JLjava/nio/Buffer;)V",
1390 (void*)Bitmap_copyPixelsToBuffer },
1391 { "nativeCopyPixelsFromBuffer", "(JLjava/nio/Buffer;)V",
1392 (void*)Bitmap_copyPixelsFromBuffer },
1393 { "nativeSameAs", "(JJ)Z", (void*)Bitmap_sameAs },
John Reckc6e2e8f2015-04-15 13:24:47 -07001394 { "nativeRefPixelRef", "(J)J", (void*)Bitmap_refPixelRef },
Chris Craik32054b02014-05-09 13:58:56 -07001395};
1396
Chris Craik32054b02014-05-09 13:58:56 -07001397int register_android_graphics_Bitmap(JNIEnv* env)
1398{
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001399 return android::RegisterMethodsOrDie(env, "android/graphics/Bitmap", gBitmapMethods,
1400 NELEM(gBitmapMethods));
Chris Craik32054b02014-05-09 13:58:56 -07001401}