blob: 6acb76d365b57fed0fe07742efe2058f2aa243a5 [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
Chris Craik32054b02014-05-09 13:58:56 -07004#include "SkBitmap.h"
5#include "SkPixelRef.h"
6#include "SkImageEncoder.h"
Leon Scroggins III57ee6202014-06-04 18:51:07 -04007#include "SkImageInfo.h"
Chris Craik32054b02014-05-09 13:58:56 -07008#include "SkColorPriv.h"
9#include "GraphicsJNI.h"
10#include "SkDither.h"
11#include "SkUnPreMultiply.h"
12#include "SkStream.h"
13
14#include <binder/Parcel.h>
15#include "android_os_Parcel.h"
16#include "android_util_Binder.h"
17#include "android_nio_utils.h"
18#include "CreateJavaOutputStreamAdaptor.h"
John Reckf29ed282015-04-07 07:32:03 -070019#include <Caches.h>
sergeyvdccca442016-03-21 15:38:21 -070020#include <hwui/Paint.h>
John Reck43871902016-08-01 14:39:24 -070021#include <renderthread/RenderProxy.h>
Chris Craik32054b02014-05-09 13:58:56 -070022
Andreas Gampeed6b9df2014-11-20 22:02:20 -080023#include "core_jni_helpers.h"
24
Chris Craik32054b02014-05-09 13:58:56 -070025#include <jni.h>
Riley Andrews39d7f302014-11-13 17:43:25 -080026#include <memory>
27#include <string>
28#include <sys/mman.h>
29#include <cutils/ashmem.h>
Chris Craik32054b02014-05-09 13:58:56 -070030
Jeff Browna316c5d2015-06-05 15:14:06 -070031#define DEBUG_PARCEL 0
Riley Andrews8cee7c12015-11-01 23:36:04 -080032#define ASHMEM_BITMAP_MIN_SIZE (128 * (1 << 10))
Jeff Browna316c5d2015-06-05 15:14:06 -070033
John Reckf29ed282015-04-07 07:32:03 -070034namespace android {
35
36class WrappedPixelRef : public SkPixelRef {
37public:
38 WrappedPixelRef(Bitmap* wrapper, void* storage,
39 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
40 : SkPixelRef(info)
41 , mBitmap(*wrapper)
42 , mStorage(storage) {
43 reconfigure(info, rowBytes, ctable);
44 }
45
46 ~WrappedPixelRef() {
47 // Tell SkRefCnt that everything is as it expects by forcing
48 // the refcnt to 1
49 internal_dispose_restore_refcnt_to_1();
50 SkSafeUnref(mColorTable);
51 }
52
John Reck0781a2f2015-05-27 16:29:17 -070053 void reconfigure(const SkImageInfo& newInfo, size_t rowBytes, SkColorTable* ctable) {
54 if (kIndex_8_SkColorType != newInfo.colorType()) {
John Reckf29ed282015-04-07 07:32:03 -070055 ctable = nullptr;
56 }
57 mRowBytes = rowBytes;
58 if (mColorTable != ctable) {
59 SkSafeUnref(mColorTable);
60 mColorTable = ctable;
61 SkSafeRef(mColorTable);
62 }
John Reck0781a2f2015-05-27 16:29:17 -070063
64 // Need to validate the alpha type to filter against the color type
65 // to prevent things like a non-opaque RGB565 bitmap
66 SkAlphaType alphaType;
67 LOG_ALWAYS_FATAL_IF(!SkColorTypeValidateAlphaType(
68 newInfo.colorType(), newInfo.alphaType(), &alphaType),
69 "Failed to validate alpha type!");
70
John Reckf29ed282015-04-07 07:32:03 -070071 // Dirty hack is dirty
72 // TODO: Figure something out here, Skia's current design makes this
73 // really hard to work with. Skia really, really wants immutable objects,
74 // but with the nested-ref-count hackery going on that's just not
75 // feasible without going insane trying to figure it out
76 SkImageInfo* myInfo = const_cast<SkImageInfo*>(&this->info());
John Reck0781a2f2015-05-27 16:29:17 -070077 *myInfo = newInfo;
78 changeAlphaType(alphaType);
John Reckf29ed282015-04-07 07:32:03 -070079
80 // Docs say to only call this in the ctor, but we're going to call
81 // it anyway even if this isn't always the ctor.
82 // TODO: Fix this too as part of the above TODO
83 setPreLocked(mStorage, mRowBytes, mColorTable);
84 }
85
86 // Can't mark as override since SkPixelRef::rowBytes isn't virtual
87 // but that's OK since we just want BitmapWrapper to be able to rely
88 // on calling rowBytes() on an unlocked pixelref, which it will be
89 // doing on a WrappedPixelRef type, not a SkPixelRef, so static
90 // dispatching will do what we want.
91 size_t rowBytes() const { return mRowBytes; }
92 SkColorTable* colorTable() const { return mColorTable; }
93
94 bool hasHardwareMipMap() const {
95 return mHasHardwareMipMap;
96 }
97
98 void setHasHardwareMipMap(bool hasMipMap) {
99 mHasHardwareMipMap = hasMipMap;
100 }
101
102protected:
103 virtual bool onNewLockPixels(LockRec* rec) override {
104 rec->fPixels = mStorage;
105 rec->fRowBytes = mRowBytes;
106 rec->fColorTable = mColorTable;
107 return true;
108 }
109
110 virtual void onUnlockPixels() override {
111 // nothing
112 }
113
114 virtual size_t getAllocatedSizeInBytes() const override {
115 return info().getSafeSize(mRowBytes);
116 }
117
118private:
119 Bitmap& mBitmap;
120 void* mStorage;
121 size_t mRowBytes = 0;
122 SkColorTable* mColorTable = nullptr;
123 bool mHasHardwareMipMap = false;
124
125 virtual void internal_dispose() const override {
126 mBitmap.onStrongRefDestroyed();
127 }
128};
129
sergeyv45082182016-09-29 18:25:40 -0700130Bitmap::Bitmap(void* address, size_t size, const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
131 : mPixelStorageType(PixelStorageType::Heap) {
132 mPixelStorage.heap.address = address;
133 mPixelStorage.heap.size = size;
John Reckf29ed282015-04-07 07:32:03 -0700134 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
135 // Note: this will trigger a call to onStrongRefDestroyed(), but
136 // we want the pixel ref to have a ref count of 0 at this point
137 mPixelRef->unref();
138}
139
140Bitmap::Bitmap(void* address, void* context, FreeFunc freeFunc,
141 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
142 : mPixelStorageType(PixelStorageType::External) {
143 mPixelStorage.external.address = address;
144 mPixelStorage.external.context = context;
145 mPixelStorage.external.freeFunc = freeFunc;
146 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
147 // Note: this will trigger a call to onStrongRefDestroyed(), but
148 // we want the pixel ref to have a ref count of 0 at this point
149 mPixelRef->unref();
150}
151
John Reck003bdee2016-09-12 10:43:35 -0700152Bitmap::Bitmap(void* address, int fd, size_t mappedSize,
Riley Andrews39d7f302014-11-13 17:43:25 -0800153 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
154 : mPixelStorageType(PixelStorageType::Ashmem) {
155 mPixelStorage.ashmem.address = address;
156 mPixelStorage.ashmem.fd = fd;
John Reck003bdee2016-09-12 10:43:35 -0700157 mPixelStorage.ashmem.size = mappedSize;
Riley Andrews39d7f302014-11-13 17:43:25 -0800158 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
159 // Note: this will trigger a call to onStrongRefDestroyed(), but
160 // we want the pixel ref to have a ref count of 0 at this point
161 mPixelRef->unref();
162}
John Reckf29ed282015-04-07 07:32:03 -0700163Bitmap::~Bitmap() {
164 doFreePixels();
165}
166
167void Bitmap::freePixels() {
168 AutoMutex _lock(mLock);
169 if (mPinnedRefCount == 0) {
170 doFreePixels();
171 mPixelStorageType = PixelStorageType::Invalid;
172 }
173}
174
175void Bitmap::doFreePixels() {
176 switch (mPixelStorageType) {
177 case PixelStorageType::Invalid:
178 // already free'd, nothing to do
179 break;
180 case PixelStorageType::External:
181 mPixelStorage.external.freeFunc(mPixelStorage.external.address,
182 mPixelStorage.external.context);
183 break;
Riley Andrews39d7f302014-11-13 17:43:25 -0800184 case PixelStorageType::Ashmem:
185 munmap(mPixelStorage.ashmem.address, mPixelStorage.ashmem.size);
186 close(mPixelStorage.ashmem.fd);
187 break;
sergeyv45082182016-09-29 18:25:40 -0700188 case PixelStorageType::Heap:
189 free(mPixelStorage.heap.address);
John Reckf29ed282015-04-07 07:32:03 -0700190 break;
191 }
192
193 if (android::uirenderer::Caches::hasInstance()) {
194 android::uirenderer::Caches::getInstance().textureCache.releaseTexture(
195 mPixelRef->getStableID());
196 }
197}
198
199bool Bitmap::hasHardwareMipMap() {
200 return mPixelRef->hasHardwareMipMap();
201}
202
203void Bitmap::setHasHardwareMipMap(bool hasMipMap) {
204 mPixelRef->setHasHardwareMipMap(hasMipMap);
205}
206
Riley Andrews39d7f302014-11-13 17:43:25 -0800207int Bitmap::getAshmemFd() const {
208 switch (mPixelStorageType) {
209 case PixelStorageType::Ashmem:
210 return mPixelStorage.ashmem.fd;
211 default:
212 return -1;
213 }
214}
215
sergeyv45082182016-09-29 18:25:40 -0700216size_t Bitmap::getAllocationByteCount() const {
217 switch (mPixelStorageType) {
218 case PixelStorageType::Heap:
219 return mPixelStorage.heap.size;
220 default:
221 return rowBytes() * height();
222 }
223}
224
John Reckf29ed282015-04-07 07:32:03 -0700225const SkImageInfo& Bitmap::info() const {
John Reckf29ed282015-04-07 07:32:03 -0700226 return mPixelRef->info();
227}
228
229size_t Bitmap::rowBytes() const {
230 return mPixelRef->rowBytes();
231}
232
John Reckae2e8b42015-05-06 14:55:05 -0700233SkPixelRef* Bitmap::peekAtPixelRef() const {
John Reckf29ed282015-04-07 07:32:03 -0700234 assertValid();
235 return mPixelRef.get();
236}
237
John Reckae2e8b42015-05-06 14:55:05 -0700238SkPixelRef* Bitmap::refPixelRef() {
239 assertValid();
240 android::AutoMutex _lock(mLock);
241 return refPixelRefLocked();
242}
243
244SkPixelRef* Bitmap::refPixelRefLocked() {
245 mPixelRef->ref();
246 if (mPixelRef->unique()) {
247 // We just restored this from 0, pin the pixels and inc the strong count
248 // Note that there *might be* an incoming onStrongRefDestroyed from whatever
249 // last unref'd
John Reckae2e8b42015-05-06 14:55:05 -0700250 mPinnedRefCount++;
251 }
252 return mPixelRef.get();
253}
254
John Reckf29ed282015-04-07 07:32:03 -0700255void Bitmap::reconfigure(const SkImageInfo& info, size_t rowBytes,
256 SkColorTable* ctable) {
257 mPixelRef->reconfigure(info, rowBytes, ctable);
258}
259
260void Bitmap::reconfigure(const SkImageInfo& info) {
Derek Sollenberger2a94a102015-05-07 13:56:14 -0400261 reconfigure(info, info.minRowBytes(), nullptr);
John Reckf29ed282015-04-07 07:32:03 -0700262}
263
John Reck0781a2f2015-05-27 16:29:17 -0700264void Bitmap::setAlphaType(SkAlphaType alphaType) {
265 if (!SkColorTypeValidateAlphaType(info().colorType(), alphaType, &alphaType)) {
266 return;
267 }
268
269 mPixelRef->changeAlphaType(alphaType);
270}
271
John Reckf29ed282015-04-07 07:32:03 -0700272void Bitmap::detachFromJava() {
273 bool disposeSelf;
274 {
275 android::AutoMutex _lock(mLock);
276 mAttachedToJava = false;
277 disposeSelf = shouldDisposeSelfLocked();
278 }
279 if (disposeSelf) {
280 delete this;
281 }
282}
283
284bool Bitmap::shouldDisposeSelfLocked() {
285 return mPinnedRefCount == 0 && !mAttachedToJava;
286}
287
John Reckf29ed282015-04-07 07:32:03 -0700288
289void Bitmap::onStrongRefDestroyed() {
290 bool disposeSelf = false;
291 {
292 android::AutoMutex _lock(mLock);
293 if (mPinnedRefCount > 0) {
294 mPinnedRefCount--;
295 if (mPinnedRefCount == 0) {
John Reckf29ed282015-04-07 07:32:03 -0700296 disposeSelf = shouldDisposeSelfLocked();
297 }
298 }
299 }
300 if (disposeSelf) {
301 delete this;
302 }
303}
304
John Reckf29ed282015-04-07 07:32:03 -0700305void Bitmap::getSkBitmap(SkBitmap* outBitmap) {
306 assertValid();
307 android::AutoMutex _lock(mLock);
John Reckf29ed282015-04-07 07:32:03 -0700308 // Safe because mPixelRef is a WrappedPixelRef type, otherwise rowBytes()
309 // would require locking the pixels first.
310 outBitmap->setInfo(mPixelRef->info(), mPixelRef->rowBytes());
John Reckae2e8b42015-05-06 14:55:05 -0700311 outBitmap->setPixelRef(refPixelRefLocked())->unref();
John Reckf29ed282015-04-07 07:32:03 -0700312 outBitmap->setHasHardwareMipMap(hasHardwareMipMap());
313}
314
315void Bitmap::assertValid() const {
316 LOG_ALWAYS_FATAL_IF(mPixelStorageType == PixelStorageType::Invalid,
317 "Error, cannot access an invalid/free'd bitmap here!");
318}
319
320} // namespace android
321
322using namespace android;
323
324// Convenience class that does not take a global ref on the pixels, relying
325// on the caller already having a local JNI ref
326class LocalScopedBitmap {
327public:
Chih-Hung Hsiehc6baf562016-04-27 11:29:23 -0700328 explicit LocalScopedBitmap(jlong bitmapHandle)
John Reckf29ed282015-04-07 07:32:03 -0700329 : mBitmap(reinterpret_cast<Bitmap*>(bitmapHandle)) {}
330
331 Bitmap* operator->() {
332 return mBitmap;
333 }
334
335 void* pixels() {
John Reckae2e8b42015-05-06 14:55:05 -0700336 return mBitmap->peekAtPixelRef()->pixels();
John Reckf29ed282015-04-07 07:32:03 -0700337 }
338
339 bool valid() {
340 return mBitmap && mBitmap->valid();
341 }
342
343private:
344 Bitmap* mBitmap;
345};
346
Chris Craik32054b02014-05-09 13:58:56 -0700347///////////////////////////////////////////////////////////////////////////////
348// Conversions to/from SkColor, for get/setPixels, and the create method, which
349// is basically like setPixels
350
351typedef void (*FromColorProc)(void* dst, const SkColor src[], int width,
352 int x, int y);
353
354static void FromColor_D32(void* dst, const SkColor src[], int width,
355 int, int) {
356 SkPMColor* d = (SkPMColor*)dst;
357
358 for (int i = 0; i < width; i++) {
359 *d++ = SkPreMultiplyColor(*src++);
360 }
361}
362
363static void FromColor_D32_Raw(void* dst, const SkColor src[], int width,
364 int, int) {
Dan Albert46d84442014-11-18 16:07:51 -0800365 // Needed to thwart the unreachable code detection from clang.
366 static const bool sk_color_ne_zero = SK_COLOR_MATCHES_PMCOLOR_BYTE_ORDER;
367
Chris Craik32054b02014-05-09 13:58:56 -0700368 // SkColor's ordering may be different from SkPMColor
Dan Albert46d84442014-11-18 16:07:51 -0800369 if (sk_color_ne_zero) {
Chris Craik32054b02014-05-09 13:58:56 -0700370 memcpy(dst, src, width * sizeof(SkColor));
371 return;
372 }
373
374 // order isn't same, repack each pixel manually
375 SkPMColor* d = (SkPMColor*)dst;
376 for (int i = 0; i < width; i++) {
377 SkColor c = *src++;
378 *d++ = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c),
379 SkColorGetG(c), SkColorGetB(c));
380 }
381}
382
383static void FromColor_D565(void* dst, const SkColor src[], int width,
384 int x, int y) {
385 uint16_t* d = (uint16_t*)dst;
386
387 DITHER_565_SCAN(y);
388 for (int stop = x + width; x < stop; x++) {
389 SkColor c = *src++;
390 *d++ = SkDitherRGBTo565(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c),
391 DITHER_VALUE(x));
392 }
393}
394
395static void FromColor_D4444(void* dst, const SkColor src[], int width,
396 int x, int y) {
397 SkPMColor16* d = (SkPMColor16*)dst;
398
399 DITHER_4444_SCAN(y);
400 for (int stop = x + width; x < stop; x++) {
401 SkPMColor pmc = SkPreMultiplyColor(*src++);
402 *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x));
403// *d++ = SkPixel32ToPixel4444(pmc);
404 }
405}
406
407static void FromColor_D4444_Raw(void* dst, const SkColor src[], int width,
408 int x, int y) {
409 SkPMColor16* d = (SkPMColor16*)dst;
410
411 DITHER_4444_SCAN(y);
412 for (int stop = x + width; x < stop; x++) {
413 SkColor c = *src++;
414
415 // SkPMColor is used because the ordering is ARGB32, even though the target actually premultiplied
416 SkPMColor pmc = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c),
417 SkColorGetG(c), SkColorGetB(c));
418 *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x));
419// *d++ = SkPixel32ToPixel4444(pmc);
420 }
421}
422
Chris Craik6260b222015-07-24 15:17:29 -0700423static void FromColor_DA8(void* dst, const SkColor src[], int width, int x, int y) {
424 uint8_t* d = (uint8_t*)dst;
425
426 for (int stop = x + width; x < stop; x++) {
427 *d++ = SkColorGetA(*src++);
428 }
429}
430
Chris Craik32054b02014-05-09 13:58:56 -0700431// can return NULL
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400432static FromColorProc ChooseFromColorProc(const SkBitmap& bitmap) {
433 switch (bitmap.colorType()) {
434 case kN32_SkColorType:
435 return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D32 : FromColor_D32_Raw;
436 case kARGB_4444_SkColorType:
437 return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D4444 :
438 FromColor_D4444_Raw;
439 case kRGB_565_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700440 return FromColor_D565;
Chris Craik6260b222015-07-24 15:17:29 -0700441 case kAlpha_8_SkColorType:
442 return FromColor_DA8;
Chris Craik32054b02014-05-09 13:58:56 -0700443 default:
444 break;
445 }
446 return NULL;
447}
448
449bool GraphicsJNI::SetPixels(JNIEnv* env, jintArray srcColors, int srcOffset, int srcStride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400450 int x, int y, int width, int height, const SkBitmap& dstBitmap) {
Chris Craik32054b02014-05-09 13:58:56 -0700451 SkAutoLockPixels alp(dstBitmap);
452 void* dst = dstBitmap.getPixels();
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400453 FromColorProc proc = ChooseFromColorProc(dstBitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700454
455 if (NULL == dst || NULL == proc) {
456 return false;
457 }
458
459 const jint* array = env->GetIntArrayElements(srcColors, NULL);
460 const SkColor* src = (const SkColor*)array + srcOffset;
461
462 // reset to to actual choice from caller
463 dst = dstBitmap.getAddr(x, y);
464 // now copy/convert each scanline
465 for (int y = 0; y < height; y++) {
466 proc(dst, src, width, x, y);
467 src += srcStride;
468 dst = (char*)dst + dstBitmap.rowBytes();
469 }
470
471 dstBitmap.notifyPixelsChanged();
472
473 env->ReleaseIntArrayElements(srcColors, const_cast<jint*>(array),
474 JNI_ABORT);
475 return true;
476}
477
478//////////////////// ToColor procs
479
480typedef void (*ToColorProc)(SkColor dst[], const void* src, int width,
481 SkColorTable*);
482
483static void ToColor_S32_Alpha(SkColor dst[], const void* src, int width,
484 SkColorTable*) {
485 SkASSERT(width > 0);
486 const SkPMColor* s = (const SkPMColor*)src;
487 do {
488 *dst++ = SkUnPreMultiply::PMColorToColor(*s++);
489 } while (--width != 0);
490}
491
492static void ToColor_S32_Raw(SkColor dst[], const void* src, int width,
493 SkColorTable*) {
494 SkASSERT(width > 0);
495 const SkPMColor* s = (const SkPMColor*)src;
496 do {
497 SkPMColor c = *s++;
498 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
499 SkGetPackedG32(c), SkGetPackedB32(c));
500 } while (--width != 0);
501}
502
503static void ToColor_S32_Opaque(SkColor dst[], const void* src, int width,
504 SkColorTable*) {
505 SkASSERT(width > 0);
506 const SkPMColor* s = (const SkPMColor*)src;
507 do {
508 SkPMColor c = *s++;
509 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
510 SkGetPackedB32(c));
511 } while (--width != 0);
512}
513
514static void ToColor_S4444_Alpha(SkColor dst[], const void* src, int width,
515 SkColorTable*) {
516 SkASSERT(width > 0);
517 const SkPMColor16* s = (const SkPMColor16*)src;
518 do {
519 *dst++ = SkUnPreMultiply::PMColorToColor(SkPixel4444ToPixel32(*s++));
520 } while (--width != 0);
521}
522
523static void ToColor_S4444_Raw(SkColor dst[], const void* src, int width,
524 SkColorTable*) {
525 SkASSERT(width > 0);
526 const SkPMColor16* s = (const SkPMColor16*)src;
527 do {
528 SkPMColor c = SkPixel4444ToPixel32(*s++);
529 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
530 SkGetPackedG32(c), SkGetPackedB32(c));
531 } while (--width != 0);
532}
533
534static void ToColor_S4444_Opaque(SkColor dst[], const void* src, int width,
535 SkColorTable*) {
536 SkASSERT(width > 0);
537 const SkPMColor16* s = (const SkPMColor16*)src;
538 do {
539 SkPMColor c = SkPixel4444ToPixel32(*s++);
540 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
541 SkGetPackedB32(c));
542 } while (--width != 0);
543}
544
545static void ToColor_S565(SkColor dst[], const void* src, int width,
546 SkColorTable*) {
547 SkASSERT(width > 0);
548 const uint16_t* s = (const uint16_t*)src;
549 do {
550 uint16_t c = *s++;
551 *dst++ = SkColorSetRGB(SkPacked16ToR32(c), SkPacked16ToG32(c),
552 SkPacked16ToB32(c));
553 } while (--width != 0);
554}
555
556static void ToColor_SI8_Alpha(SkColor dst[], const void* src, int width,
557 SkColorTable* ctable) {
558 SkASSERT(width > 0);
559 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500560 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700561 do {
562 *dst++ = SkUnPreMultiply::PMColorToColor(colors[*s++]);
563 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700564}
565
566static void ToColor_SI8_Raw(SkColor dst[], const void* src, int width,
567 SkColorTable* ctable) {
568 SkASSERT(width > 0);
569 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500570 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700571 do {
572 SkPMColor c = colors[*s++];
573 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
574 SkGetPackedG32(c), SkGetPackedB32(c));
575 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700576}
577
578static void ToColor_SI8_Opaque(SkColor dst[], const void* src, int width,
579 SkColorTable* ctable) {
580 SkASSERT(width > 0);
581 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500582 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700583 do {
584 SkPMColor c = colors[*s++];
585 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
586 SkGetPackedB32(c));
587 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700588}
589
Chris Craik6260b222015-07-24 15:17:29 -0700590static void ToColor_SA8(SkColor dst[], const void* src, int width, SkColorTable*) {
591 SkASSERT(width > 0);
592 const uint8_t* s = (const uint8_t*)src;
593 do {
594 uint8_t c = *s++;
595 *dst++ = SkColorSetARGB(c, c, c, c);
596 } while (--width != 0);
597}
598
Chris Craik32054b02014-05-09 13:58:56 -0700599// can return NULL
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400600static ToColorProc ChooseToColorProc(const SkBitmap& src) {
Mike Reedb9330552014-06-16 17:31:48 -0400601 switch (src.colorType()) {
602 case kN32_SkColorType:
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400603 switch (src.alphaType()) {
604 case kOpaque_SkAlphaType:
605 return ToColor_S32_Opaque;
606 case kPremul_SkAlphaType:
607 return ToColor_S32_Alpha;
608 case kUnpremul_SkAlphaType:
609 return ToColor_S32_Raw;
610 default:
611 return NULL;
612 }
Mike Reedb9330552014-06-16 17:31:48 -0400613 case kARGB_4444_SkColorType:
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400614 switch (src.alphaType()) {
615 case kOpaque_SkAlphaType:
616 return ToColor_S4444_Opaque;
617 case kPremul_SkAlphaType:
618 return ToColor_S4444_Alpha;
619 case kUnpremul_SkAlphaType:
620 return ToColor_S4444_Raw;
621 default:
622 return NULL;
623 }
Mike Reedb9330552014-06-16 17:31:48 -0400624 case kRGB_565_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700625 return ToColor_S565;
Mike Reedb9330552014-06-16 17:31:48 -0400626 case kIndex_8_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700627 if (src.getColorTable() == NULL) {
628 return NULL;
629 }
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400630 switch (src.alphaType()) {
631 case kOpaque_SkAlphaType:
632 return ToColor_SI8_Opaque;
633 case kPremul_SkAlphaType:
634 return ToColor_SI8_Alpha;
635 case kUnpremul_SkAlphaType:
636 return ToColor_SI8_Raw;
637 default:
638 return NULL;
639 }
Chris Craik6260b222015-07-24 15:17:29 -0700640 case kAlpha_8_SkColorType:
641 return ToColor_SA8;
Chris Craik32054b02014-05-09 13:58:56 -0700642 default:
643 break;
644 }
645 return NULL;
646}
647
648///////////////////////////////////////////////////////////////////////////////
649///////////////////////////////////////////////////////////////////////////////
650
651static int getPremulBitmapCreateFlags(bool isMutable) {
652 int flags = GraphicsJNI::kBitmapCreateFlag_Premultiplied;
653 if (isMutable) flags |= GraphicsJNI::kBitmapCreateFlag_Mutable;
654 return flags;
655}
656
657static jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors,
658 jint offset, jint stride, jint width, jint height,
659 jint configHandle, jboolean isMutable) {
Mike Reed1103b322014-07-08 12:36:44 -0400660 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700661 if (NULL != jColors) {
662 size_t n = env->GetArrayLength(jColors);
663 if (n < SkAbs32(stride) * (size_t)height) {
664 doThrowAIOOBE(env);
665 return NULL;
666 }
667 }
668
669 // ARGB_4444 is a deprecated format, convert automatically to 8888
Mike Reedb9330552014-06-16 17:31:48 -0400670 if (colorType == kARGB_4444_SkColorType) {
671 colorType = kN32_SkColorType;
Chris Craik32054b02014-05-09 13:58:56 -0700672 }
673
674 SkBitmap bitmap;
Mike Reedb9330552014-06-16 17:31:48 -0400675 bitmap.setInfo(SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType));
Chris Craik32054b02014-05-09 13:58:56 -0700676
sergeyv45082182016-09-29 18:25:40 -0700677 Bitmap* nativeBitmap = GraphicsJNI::allocateHeapPixelRef(&bitmap, NULL);
John Reckf29ed282015-04-07 07:32:03 -0700678 if (!nativeBitmap) {
Chris Craik32054b02014-05-09 13:58:56 -0700679 return NULL;
680 }
681
682 if (jColors != NULL) {
683 GraphicsJNI::SetPixels(env, jColors, offset, stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400684 0, 0, width, height, bitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700685 }
686
John Reckf29ed282015-04-07 07:32:03 -0700687 return GraphicsJNI::createBitmap(env, nativeBitmap,
688 getPremulBitmapCreateFlags(isMutable));
Chris Craik32054b02014-05-09 13:58:56 -0700689}
690
691static jobject Bitmap_copy(JNIEnv* env, jobject, jlong srcHandle,
692 jint dstConfigHandle, jboolean isMutable) {
John Reckf29ed282015-04-07 07:32:03 -0700693 SkBitmap src;
694 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
Mike Reed1103b322014-07-08 12:36:44 -0400695 SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle);
sergeyv45082182016-09-29 18:25:40 -0700696 SkBitmap result;
697 HeapAllocator allocator;
Chris Craik32054b02014-05-09 13:58:56 -0700698
John Reckf29ed282015-04-07 07:32:03 -0700699 if (!src.copyTo(&result, dstCT, &allocator)) {
Chris Craik32054b02014-05-09 13:58:56 -0700700 return NULL;
701 }
John Reckf29ed282015-04-07 07:32:03 -0700702 Bitmap* bitmap = allocator.getStorageObjAndReset();
703 return GraphicsJNI::createBitmap(env, bitmap,
704 getPremulBitmapCreateFlags(isMutable));
Chris Craik32054b02014-05-09 13:58:56 -0700705}
706
Winsona5fdde92016-04-14 15:27:15 -0700707static Bitmap* Bitmap_copyAshmemImpl(JNIEnv* env, SkBitmap& src, SkColorType& dstCT) {
Riley Andrews721ae5f2015-05-11 16:08:22 -0700708 SkBitmap result;
709
710 AshmemPixelAllocator allocator(env);
Winsona5fdde92016-04-14 15:27:15 -0700711 if (!src.copyTo(&result, dstCT, &allocator)) {
Riley Andrews721ae5f2015-05-11 16:08:22 -0700712 return NULL;
713 }
714 Bitmap* bitmap = allocator.getStorageObjAndReset();
715 bitmap->peekAtPixelRef()->setImmutable();
Winsona5fdde92016-04-14 15:27:15 -0700716 return bitmap;
717}
718
719static jobject Bitmap_copyAshmem(JNIEnv* env, jobject, jlong srcHandle) {
720 SkBitmap src;
721 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
722 SkColorType dstCT = src.colorType();
723 Bitmap* bitmap = Bitmap_copyAshmemImpl(env, src, dstCT);
724 jobject ret = GraphicsJNI::createBitmap(env, bitmap, getPremulBitmapCreateFlags(false));
725 return ret;
726}
727
728static jobject Bitmap_copyAshmemConfig(JNIEnv* env, jobject, jlong srcHandle, jint dstConfigHandle) {
729 SkBitmap src;
730 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
731 SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle);
732 Bitmap* bitmap = Bitmap_copyAshmemImpl(env, src, dstCT);
Riley Andrews721ae5f2015-05-11 16:08:22 -0700733 jobject ret = GraphicsJNI::createBitmap(env, bitmap, getPremulBitmapCreateFlags(false));
734 return ret;
735}
736
Richard Uhler775873a2015-12-29 12:37:39 -0800737static void Bitmap_destruct(Bitmap* bitmap) {
John Reckf29ed282015-04-07 07:32:03 -0700738 bitmap->detachFromJava();
Chris Craik32054b02014-05-09 13:58:56 -0700739}
740
Richard Uhler775873a2015-12-29 12:37:39 -0800741static jlong Bitmap_getNativeFinalizer(JNIEnv*, jobject) {
742 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&Bitmap_destruct));
743}
744
Chris Craik32054b02014-05-09 13:58:56 -0700745static jboolean Bitmap_recycle(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700746 LocalScopedBitmap bitmap(bitmapHandle);
747 bitmap->freePixels();
Chris Craik32054b02014-05-09 13:58:56 -0700748 return JNI_TRUE;
749}
750
751static void Bitmap_reconfigure(JNIEnv* env, jobject clazz, jlong bitmapHandle,
sergeyv45082182016-09-29 18:25:40 -0700752 jint width, jint height, jint configHandle, jboolean requestPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700753 LocalScopedBitmap bitmap(bitmapHandle);
Mike Reed1103b322014-07-08 12:36:44 -0400754 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400755
756 // ARGB_4444 is a deprecated format, convert automatically to 8888
757 if (colorType == kARGB_4444_SkColorType) {
758 colorType = kN32_SkColorType;
759 }
sergeyv45082182016-09-29 18:25:40 -0700760 size_t requestedSize = width * height * SkColorTypeBytesPerPixel(colorType);
761 if (requestedSize > bitmap->getAllocationByteCount()) {
Chris Craik32054b02014-05-09 13:58:56 -0700762 // done in native as there's no way to get BytesPerPixel in Java
763 doThrowIAE(env, "Bitmap not large enough to support new configuration");
764 return;
765 }
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400766 SkAlphaType alphaType;
John Reckf29ed282015-04-07 07:32:03 -0700767 if (bitmap->info().colorType() != kRGB_565_SkColorType
768 && bitmap->info().alphaType() == kOpaque_SkAlphaType) {
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400769 // If the original bitmap was set to opaque, keep that setting, unless it
770 // was 565, which is required to be opaque.
771 alphaType = kOpaque_SkAlphaType;
772 } else {
773 // Otherwise respect the premultiplied request.
774 alphaType = requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
775 }
John Reckf29ed282015-04-07 07:32:03 -0700776 bitmap->reconfigure(SkImageInfo::Make(width, height, colorType, alphaType));
Chris Craik32054b02014-05-09 13:58:56 -0700777}
778
779// These must match the int values in Bitmap.java
780enum JavaEncodeFormat {
781 kJPEG_JavaEncodeFormat = 0,
782 kPNG_JavaEncodeFormat = 1,
783 kWEBP_JavaEncodeFormat = 2
784};
785
786static jboolean Bitmap_compress(JNIEnv* env, jobject clazz, jlong bitmapHandle,
787 jint format, jint quality,
788 jobject jstream, jbyteArray jstorage) {
John Reckf29ed282015-04-07 07:32:03 -0700789
790 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700791 SkImageEncoder::Type fm;
792
793 switch (format) {
794 case kJPEG_JavaEncodeFormat:
795 fm = SkImageEncoder::kJPEG_Type;
796 break;
797 case kPNG_JavaEncodeFormat:
798 fm = SkImageEncoder::kPNG_Type;
799 break;
800 case kWEBP_JavaEncodeFormat:
801 fm = SkImageEncoder::kWEBP_Type;
802 break;
803 default:
804 return JNI_FALSE;
805 }
806
John Reckf29ed282015-04-07 07:32:03 -0700807 if (!bitmap.valid()) {
808 return JNI_FALSE;
809 }
810
Chris Craik32054b02014-05-09 13:58:56 -0700811 bool success = false;
Chris Craik32054b02014-05-09 13:58:56 -0700812
John Reckf29ed282015-04-07 07:32:03 -0700813 std::unique_ptr<SkWStream> strm(CreateJavaOutputStreamAdaptor(env, jstream, jstorage));
814 if (!strm.get()) {
815 return JNI_FALSE;
816 }
Chris Craik32054b02014-05-09 13:58:56 -0700817
John Reckf29ed282015-04-07 07:32:03 -0700818 std::unique_ptr<SkImageEncoder> encoder(SkImageEncoder::Create(fm));
819 if (encoder.get()) {
820 SkBitmap skbitmap;
821 bitmap->getSkBitmap(&skbitmap);
822 success = encoder->encodeStream(strm.get(), skbitmap, quality);
Chris Craik32054b02014-05-09 13:58:56 -0700823 }
824 return success ? JNI_TRUE : JNI_FALSE;
825}
826
827static void Bitmap_erase(JNIEnv* env, jobject, jlong bitmapHandle, jint color) {
John Reckf29ed282015-04-07 07:32:03 -0700828 LocalScopedBitmap bitmap(bitmapHandle);
829 SkBitmap skBitmap;
830 bitmap->getSkBitmap(&skBitmap);
831 skBitmap.eraseColor(color);
Chris Craik32054b02014-05-09 13:58:56 -0700832}
833
834static jint Bitmap_rowBytes(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700835 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700836 return static_cast<jint>(bitmap->rowBytes());
837}
838
839static jint Bitmap_config(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700840 LocalScopedBitmap bitmap(bitmapHandle);
841 return GraphicsJNI::colorTypeToLegacyBitmapConfig(bitmap->info().colorType());
Chris Craik32054b02014-05-09 13:58:56 -0700842}
843
844static jint Bitmap_getGenerationId(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700845 LocalScopedBitmap bitmap(bitmapHandle);
John Reckae2e8b42015-05-06 14:55:05 -0700846 return static_cast<jint>(bitmap->peekAtPixelRef()->getGenerationID());
Chris Craik32054b02014-05-09 13:58:56 -0700847}
848
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400849static jboolean Bitmap_isPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700850 LocalScopedBitmap bitmap(bitmapHandle);
851 if (bitmap->info().alphaType() == kPremul_SkAlphaType) {
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400852 return JNI_TRUE;
853 }
854 return JNI_FALSE;
855}
856
Chris Craik32054b02014-05-09 13:58:56 -0700857static jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700858 LocalScopedBitmap bitmap(bitmapHandle);
859 return !bitmap->info().isOpaque() ? JNI_TRUE : JNI_FALSE;
Chris Craik32054b02014-05-09 13:58:56 -0700860}
861
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400862static void Bitmap_setHasAlpha(JNIEnv* env, jobject, jlong bitmapHandle,
863 jboolean hasAlpha, jboolean requestPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700864 LocalScopedBitmap bitmap(bitmapHandle);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400865 if (hasAlpha) {
John Reck0781a2f2015-05-27 16:29:17 -0700866 bitmap->setAlphaType(
John Reckf29ed282015-04-07 07:32:03 -0700867 requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType);
Chris Craik32054b02014-05-09 13:58:56 -0700868 } else {
John Reck0781a2f2015-05-27 16:29:17 -0700869 bitmap->setAlphaType(kOpaque_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400870 }
871}
872
873static void Bitmap_setPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle,
874 jboolean isPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700875 LocalScopedBitmap bitmap(bitmapHandle);
876 if (!bitmap->info().isOpaque()) {
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400877 if (isPremul) {
John Reck0781a2f2015-05-27 16:29:17 -0700878 bitmap->setAlphaType(kPremul_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400879 } else {
John Reck0781a2f2015-05-27 16:29:17 -0700880 bitmap->setAlphaType(kUnpremul_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400881 }
Chris Craik32054b02014-05-09 13:58:56 -0700882 }
883}
884
885static jboolean Bitmap_hasMipMap(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700886 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700887 return bitmap->hasHardwareMipMap() ? JNI_TRUE : JNI_FALSE;
888}
889
890static void Bitmap_setHasMipMap(JNIEnv* env, jobject, jlong bitmapHandle,
891 jboolean hasMipMap) {
John Reckf29ed282015-04-07 07:32:03 -0700892 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700893 bitmap->setHasHardwareMipMap(hasMipMap);
894}
895
896///////////////////////////////////////////////////////////////////////////////
897
898static jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) {
899 if (parcel == NULL) {
900 SkDebugf("-------- unparcel parcel is NULL\n");
901 return NULL;
902 }
903
904 android::Parcel* p = android::parcelForJavaObject(env, parcel);
905
Mike Reedb9330552014-06-16 17:31:48 -0400906 const bool isMutable = p->readInt32() != 0;
907 const SkColorType colorType = (SkColorType)p->readInt32();
908 const SkAlphaType alphaType = (SkAlphaType)p->readInt32();
909 const int width = p->readInt32();
910 const int height = p->readInt32();
911 const int rowBytes = p->readInt32();
912 const int density = p->readInt32();
Chris Craik32054b02014-05-09 13:58:56 -0700913
Mike Reedb9330552014-06-16 17:31:48 -0400914 if (kN32_SkColorType != colorType &&
915 kRGB_565_SkColorType != colorType &&
916 kARGB_4444_SkColorType != colorType &&
917 kIndex_8_SkColorType != colorType &&
918 kAlpha_8_SkColorType != colorType) {
919 SkDebugf("Bitmap_createFromParcel unknown colortype: %d\n", colorType);
Chris Craik32054b02014-05-09 13:58:56 -0700920 return NULL;
921 }
922
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400923 std::unique_ptr<SkBitmap> bitmap(new SkBitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700924
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400925 if (!bitmap->setInfo(SkImageInfo::Make(width, height, colorType, alphaType), rowBytes)) {
926 return NULL;
927 }
Chris Craik32054b02014-05-09 13:58:56 -0700928
929 SkColorTable* ctable = NULL;
Mike Reedb9330552014-06-16 17:31:48 -0400930 if (colorType == kIndex_8_SkColorType) {
Chris Craik32054b02014-05-09 13:58:56 -0700931 int count = p->readInt32();
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400932 if (count < 0 || count > 256) {
933 // The data is corrupt, since SkColorTable enforces a value between 0 and 256,
934 // inclusive.
935 return NULL;
936 }
Chris Craik32054b02014-05-09 13:58:56 -0700937 if (count > 0) {
938 size_t size = count * sizeof(SkPMColor);
939 const SkPMColor* src = (const SkPMColor*)p->readInplace(size);
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400940 if (src == NULL) {
941 return NULL;
942 }
Chris Craik32054b02014-05-09 13:58:56 -0700943 ctable = new SkColorTable(src, count);
944 }
945 }
946
Jeff Browna316c5d2015-06-05 15:14:06 -0700947 // Read the bitmap blob.
948 size_t size = bitmap->getSize();
949 android::Parcel::ReadableBlob blob;
950 android::status_t status = p->readBlob(size, &blob);
951 if (status) {
Chris Craik32054b02014-05-09 13:58:56 -0700952 SkSafeUnref(ctable);
Jeff Browna316c5d2015-06-05 15:14:06 -0700953 doThrowRE(env, "Could not read bitmap blob.");
Chris Craik32054b02014-05-09 13:58:56 -0700954 return NULL;
955 }
956
Jeff Browna316c5d2015-06-05 15:14:06 -0700957 // Map the bitmap in place from the ashmem region if possible otherwise copy.
958 Bitmap* nativeBitmap;
Riley Andrews8cee7c12015-11-01 23:36:04 -0800959 if (blob.fd() >= 0 && (blob.isMutable() || !isMutable) && (size >= ASHMEM_BITMAP_MIN_SIZE)) {
Jeff Browna316c5d2015-06-05 15:14:06 -0700960#if DEBUG_PARCEL
961 ALOGD("Bitmap.createFromParcel: mapped contents of %s bitmap from %s blob "
962 "(fds %s)",
963 isMutable ? "mutable" : "immutable",
964 blob.isMutable() ? "mutable" : "immutable",
965 p->allowFds() ? "allowed" : "forbidden");
966#endif
967 // Dup the file descriptor so we can keep a reference to it after the Parcel
968 // is disposed.
969 int dupFd = dup(blob.fd());
970 if (dupFd < 0) {
Erik Wolsheimer211abad2015-11-13 11:54:47 -0800971 ALOGE("Error allocating dup fd. Error:%d", errno);
Jeff Browna316c5d2015-06-05 15:14:06 -0700972 blob.release();
973 SkSafeUnref(ctable);
974 doThrowRE(env, "Could not allocate dup blob fd.");
975 return NULL;
976 }
977
978 // Map the pixels in place and take ownership of the ashmem region.
979 nativeBitmap = GraphicsJNI::mapAshmemPixelRef(env, bitmap.get(),
John Reck003bdee2016-09-12 10:43:35 -0700980 ctable, dupFd, const_cast<void*>(blob.data()), size, !isMutable);
Jeff Browna316c5d2015-06-05 15:14:06 -0700981 SkSafeUnref(ctable);
982 if (!nativeBitmap) {
983 close(dupFd);
984 blob.release();
985 doThrowRE(env, "Could not allocate ashmem pixel ref.");
986 return NULL;
987 }
988
989 // Clear the blob handle, don't release it.
990 blob.clear();
991 } else {
992#if DEBUG_PARCEL
993 if (blob.fd() >= 0) {
994 ALOGD("Bitmap.createFromParcel: copied contents of mutable bitmap "
995 "from immutable blob (fds %s)",
996 p->allowFds() ? "allowed" : "forbidden");
997 } else {
998 ALOGD("Bitmap.createFromParcel: copied contents from %s blob "
999 "(fds %s)",
1000 blob.isMutable() ? "mutable" : "immutable",
1001 p->allowFds() ? "allowed" : "forbidden");
1002 }
1003#endif
1004
1005 // Copy the pixels into a new buffer.
sergeyv45082182016-09-29 18:25:40 -07001006 nativeBitmap = GraphicsJNI::allocateHeapPixelRef(bitmap.get(), ctable);
Jeff Browna316c5d2015-06-05 15:14:06 -07001007 SkSafeUnref(ctable);
1008 if (!nativeBitmap) {
1009 blob.release();
1010 doThrowRE(env, "Could not allocate java pixel ref.");
1011 return NULL;
1012 }
1013 bitmap->lockPixels();
1014 memcpy(bitmap->getPixels(), blob.data(), size);
1015 bitmap->unlockPixels();
1016
1017 // Release the blob handle.
1018 blob.release();
Chris Craik32054b02014-05-09 13:58:56 -07001019 }
Chris Craik32054b02014-05-09 13:58:56 -07001020
John Reckf29ed282015-04-07 07:32:03 -07001021 return GraphicsJNI::createBitmap(env, nativeBitmap,
Leon Scroggins IIIec419e02015-03-11 13:12:06 -04001022 getPremulBitmapCreateFlags(isMutable), NULL, NULL, density);
Chris Craik32054b02014-05-09 13:58:56 -07001023}
1024
1025static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject,
1026 jlong bitmapHandle,
1027 jboolean isMutable, jint density,
1028 jobject parcel) {
Chris Craik32054b02014-05-09 13:58:56 -07001029 if (parcel == NULL) {
1030 SkDebugf("------- writeToParcel null parcel\n");
1031 return JNI_FALSE;
1032 }
1033
1034 android::Parcel* p = android::parcelForJavaObject(env, parcel);
John Reckf29ed282015-04-07 07:32:03 -07001035 SkBitmap bitmap;
Riley Andrews39d7f302014-11-13 17:43:25 -08001036
1037 android::Bitmap* androidBitmap = reinterpret_cast<Bitmap*>(bitmapHandle);
1038 androidBitmap->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001039
1040 p->writeInt32(isMutable);
John Reckf29ed282015-04-07 07:32:03 -07001041 p->writeInt32(bitmap.colorType());
1042 p->writeInt32(bitmap.alphaType());
1043 p->writeInt32(bitmap.width());
1044 p->writeInt32(bitmap.height());
1045 p->writeInt32(bitmap.rowBytes());
Chris Craik32054b02014-05-09 13:58:56 -07001046 p->writeInt32(density);
1047
John Reckf29ed282015-04-07 07:32:03 -07001048 if (bitmap.colorType() == kIndex_8_SkColorType) {
Leon Scroggins III66ce1c32016-02-02 11:11:55 -05001049 // The bitmap needs to be locked to access its color table.
1050 SkAutoLockPixels alp(bitmap);
John Reckf29ed282015-04-07 07:32:03 -07001051 SkColorTable* ctable = bitmap.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001052 if (ctable != NULL) {
1053 int count = ctable->count();
1054 p->writeInt32(count);
1055 memcpy(p->writeInplace(count * sizeof(SkPMColor)),
Mike Reed71487eb2014-11-19 16:13:20 -05001056 ctable->readColors(), count * sizeof(SkPMColor));
Chris Craik32054b02014-05-09 13:58:56 -07001057 } else {
1058 p->writeInt32(0); // indicate no ctable
1059 }
1060 }
1061
Jeff Browna316c5d2015-06-05 15:14:06 -07001062 // Transfer the underlying ashmem region if we have one and it's immutable.
1063 android::status_t status;
1064 int fd = androidBitmap->getAshmemFd();
1065 if (fd >= 0 && !isMutable && p->allowFds()) {
1066#if DEBUG_PARCEL
1067 ALOGD("Bitmap.writeToParcel: transferring immutable bitmap's ashmem fd as "
1068 "immutable blob (fds %s)",
1069 p->allowFds() ? "allowed" : "forbidden");
1070#endif
1071
1072 status = p->writeDupImmutableBlobFileDescriptor(fd);
1073 if (status) {
1074 doThrowRE(env, "Could not write bitmap blob file descriptor.");
Riley Andrews39d7f302014-11-13 17:43:25 -08001075 return JNI_FALSE;
1076 }
Jeff Browna316c5d2015-06-05 15:14:06 -07001077 return JNI_TRUE;
Riley Andrews39d7f302014-11-13 17:43:25 -08001078 }
Jeff Browna316c5d2015-06-05 15:14:06 -07001079
1080 // Copy the bitmap to a new blob.
1081 bool mutableCopy = isMutable;
1082#if DEBUG_PARCEL
1083 ALOGD("Bitmap.writeToParcel: copying %s bitmap into new %s blob (fds %s)",
1084 isMutable ? "mutable" : "immutable",
1085 mutableCopy ? "mutable" : "immutable",
1086 p->allowFds() ? "allowed" : "forbidden");
1087#endif
1088
1089 size_t size = bitmap.getSize();
1090 android::Parcel::WritableBlob blob;
1091 status = p->writeBlob(size, mutableCopy, &blob);
1092 if (status) {
1093 doThrowRE(env, "Could not copy bitmap to parcel blob.");
1094 return JNI_FALSE;
1095 }
1096
1097 bitmap.lockPixels();
1098 const void* pSrc = bitmap.getPixels();
1099 if (pSrc == NULL) {
1100 memset(blob.data(), 0, size);
1101 } else {
1102 memcpy(blob.data(), pSrc, size);
1103 }
1104 bitmap.unlockPixels();
1105
1106 blob.release();
Chris Craik32054b02014-05-09 13:58:56 -07001107 return JNI_TRUE;
1108}
1109
1110static jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz,
1111 jlong srcHandle, jlong paintHandle,
1112 jintArray offsetXY) {
John Reckf29ed282015-04-07 07:32:03 -07001113 SkBitmap src;
1114 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -04001115 const android::Paint* paint = reinterpret_cast<android::Paint*>(paintHandle);
Chris Craik32054b02014-05-09 13:58:56 -07001116 SkIPoint offset;
John Reckf29ed282015-04-07 07:32:03 -07001117 SkBitmap dst;
sergeyv45082182016-09-29 18:25:40 -07001118 HeapAllocator allocator;
Chris Craik32054b02014-05-09 13:58:56 -07001119
John Reckf29ed282015-04-07 07:32:03 -07001120 src.extractAlpha(&dst, paint, &allocator, &offset);
Chris Craik32054b02014-05-09 13:58:56 -07001121 // If Skia can't allocate pixels for destination bitmap, it resets
1122 // it, that is set its pixels buffer to NULL, and zero width and height.
John Reckf29ed282015-04-07 07:32:03 -07001123 if (dst.getPixels() == NULL && src.getPixels() != NULL) {
Chris Craik32054b02014-05-09 13:58:56 -07001124 doThrowOOME(env, "failed to allocate pixels for alpha");
1125 return NULL;
1126 }
1127 if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) {
1128 int* array = env->GetIntArrayElements(offsetXY, NULL);
1129 array[0] = offset.fX;
1130 array[1] = offset.fY;
1131 env->ReleaseIntArrayElements(offsetXY, array, 0);
1132 }
1133
John Reckf29ed282015-04-07 07:32:03 -07001134 return GraphicsJNI::createBitmap(env, allocator.getStorageObjAndReset(),
1135 getPremulBitmapCreateFlags(true));
Chris Craik32054b02014-05-09 13:58:56 -07001136}
1137
1138///////////////////////////////////////////////////////////////////////////////
1139
1140static jint Bitmap_getPixel(JNIEnv* env, jobject, jlong bitmapHandle,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001141 jint x, jint y) {
John Reckf29ed282015-04-07 07:32:03 -07001142 SkBitmap bitmap;
1143 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1144 SkAutoLockPixels alp(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001145
John Reckf29ed282015-04-07 07:32:03 -07001146 ToColorProc proc = ChooseToColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001147 if (NULL == proc) {
1148 return 0;
1149 }
John Reckf29ed282015-04-07 07:32:03 -07001150 const void* src = bitmap.getAddr(x, y);
Chris Craik32054b02014-05-09 13:58:56 -07001151 if (NULL == src) {
1152 return 0;
1153 }
1154
1155 SkColor dst[1];
John Reckf29ed282015-04-07 07:32:03 -07001156 proc(dst, src, 1, bitmap.getColorTable());
Chris Craik32054b02014-05-09 13:58:56 -07001157 return static_cast<jint>(dst[0]);
1158}
1159
1160static void Bitmap_getPixels(JNIEnv* env, jobject, jlong bitmapHandle,
1161 jintArray pixelArray, jint offset, jint stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001162 jint x, jint y, jint width, jint height) {
John Reckf29ed282015-04-07 07:32:03 -07001163 SkBitmap bitmap;
1164 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1165 SkAutoLockPixels alp(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001166
John Reckf29ed282015-04-07 07:32:03 -07001167 ToColorProc proc = ChooseToColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001168 if (NULL == proc) {
1169 return;
1170 }
John Reckf29ed282015-04-07 07:32:03 -07001171 const void* src = bitmap.getAddr(x, y);
Chris Craik32054b02014-05-09 13:58:56 -07001172 if (NULL == src) {
1173 return;
1174 }
1175
John Reckf29ed282015-04-07 07:32:03 -07001176 SkColorTable* ctable = bitmap.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001177 jint* dst = env->GetIntArrayElements(pixelArray, NULL);
1178 SkColor* d = (SkColor*)dst + offset;
1179 while (--height >= 0) {
1180 proc(d, src, width, ctable);
1181 d += stride;
John Reckf29ed282015-04-07 07:32:03 -07001182 src = (void*)((const char*)src + bitmap.rowBytes());
Chris Craik32054b02014-05-09 13:58:56 -07001183 }
1184 env->ReleaseIntArrayElements(pixelArray, dst, 0);
1185}
1186
1187///////////////////////////////////////////////////////////////////////////////
1188
1189static void Bitmap_setPixel(JNIEnv* env, jobject, jlong bitmapHandle,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001190 jint x, jint y, jint colorHandle) {
John Reckf29ed282015-04-07 07:32:03 -07001191 SkBitmap bitmap;
1192 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001193 SkColor color = static_cast<SkColor>(colorHandle);
John Reckf29ed282015-04-07 07:32:03 -07001194 SkAutoLockPixels alp(bitmap);
1195 if (NULL == bitmap.getPixels()) {
Chris Craik32054b02014-05-09 13:58:56 -07001196 return;
1197 }
1198
John Reckf29ed282015-04-07 07:32:03 -07001199 FromColorProc proc = ChooseFromColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001200 if (NULL == proc) {
1201 return;
1202 }
1203
John Reckf29ed282015-04-07 07:32:03 -07001204 proc(bitmap.getAddr(x, y), &color, 1, x, y);
1205 bitmap.notifyPixelsChanged();
Chris Craik32054b02014-05-09 13:58:56 -07001206}
1207
1208static void Bitmap_setPixels(JNIEnv* env, jobject, jlong bitmapHandle,
1209 jintArray pixelArray, jint offset, jint stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001210 jint x, jint y, jint width, jint height) {
John Reckf29ed282015-04-07 07:32:03 -07001211 SkBitmap bitmap;
1212 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001213 GraphicsJNI::SetPixels(env, pixelArray, offset, stride,
John Reckf29ed282015-04-07 07:32:03 -07001214 x, y, width, height, bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001215}
1216
1217static void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject,
1218 jlong bitmapHandle, jobject jbuffer) {
John Reckf29ed282015-04-07 07:32:03 -07001219 SkBitmap bitmap;
1220 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1221 SkAutoLockPixels alp(bitmap);
1222 const void* src = bitmap.getPixels();
Chris Craik32054b02014-05-09 13:58:56 -07001223
1224 if (NULL != src) {
1225 android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE);
1226
1227 // the java side has already checked that buffer is large enough
John Reckf29ed282015-04-07 07:32:03 -07001228 memcpy(abp.pointer(), src, bitmap.getSize());
Chris Craik32054b02014-05-09 13:58:56 -07001229 }
1230}
1231
1232static void Bitmap_copyPixelsFromBuffer(JNIEnv* env, jobject,
1233 jlong bitmapHandle, jobject jbuffer) {
John Reckf29ed282015-04-07 07:32:03 -07001234 SkBitmap bitmap;
1235 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1236 SkAutoLockPixels alp(bitmap);
1237 void* dst = bitmap.getPixels();
Chris Craik32054b02014-05-09 13:58:56 -07001238
1239 if (NULL != dst) {
1240 android::AutoBufferPointer abp(env, jbuffer, JNI_FALSE);
1241 // the java side has already checked that buffer is large enough
John Reckf29ed282015-04-07 07:32:03 -07001242 memcpy(dst, abp.pointer(), bitmap.getSize());
1243 bitmap.notifyPixelsChanged();
Chris Craik32054b02014-05-09 13:58:56 -07001244 }
1245}
1246
1247static jboolean Bitmap_sameAs(JNIEnv* env, jobject, jlong bm0Handle,
1248 jlong bm1Handle) {
John Reckf29ed282015-04-07 07:32:03 -07001249 SkBitmap bm0;
1250 SkBitmap bm1;
1251 reinterpret_cast<Bitmap*>(bm0Handle)->getSkBitmap(&bm0);
1252 reinterpret_cast<Bitmap*>(bm1Handle)->getSkBitmap(&bm1);
1253 if (bm0.width() != bm1.width() ||
1254 bm0.height() != bm1.height() ||
1255 bm0.colorType() != bm1.colorType()) {
Chris Craik32054b02014-05-09 13:58:56 -07001256 return JNI_FALSE;
1257 }
1258
John Reckf29ed282015-04-07 07:32:03 -07001259 SkAutoLockPixels alp0(bm0);
1260 SkAutoLockPixels alp1(bm1);
Chris Craik32054b02014-05-09 13:58:56 -07001261
1262 // if we can't load the pixels, return false
John Reckf29ed282015-04-07 07:32:03 -07001263 if (NULL == bm0.getPixels() || NULL == bm1.getPixels()) {
Chris Craik32054b02014-05-09 13:58:56 -07001264 return JNI_FALSE;
1265 }
1266
John Reckf29ed282015-04-07 07:32:03 -07001267 if (bm0.colorType() == kIndex_8_SkColorType) {
1268 SkColorTable* ct0 = bm0.getColorTable();
1269 SkColorTable* ct1 = bm1.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001270 if (NULL == ct0 || NULL == ct1) {
1271 return JNI_FALSE;
1272 }
1273 if (ct0->count() != ct1->count()) {
1274 return JNI_FALSE;
1275 }
1276
Chris Craik32054b02014-05-09 13:58:56 -07001277 const size_t size = ct0->count() * sizeof(SkPMColor);
Mike Reed71487eb2014-11-19 16:13:20 -05001278 if (memcmp(ct0->readColors(), ct1->readColors(), size) != 0) {
Chris Craik32054b02014-05-09 13:58:56 -07001279 return JNI_FALSE;
1280 }
1281 }
1282
1283 // now compare each scanline. We can't do the entire buffer at once,
1284 // since we don't care about the pixel values that might extend beyond
1285 // the width (since the scanline might be larger than the logical width)
John Reckf29ed282015-04-07 07:32:03 -07001286 const int h = bm0.height();
1287 const size_t size = bm0.width() * bm0.bytesPerPixel();
Chris Craik32054b02014-05-09 13:58:56 -07001288 for (int y = 0; y < h; y++) {
henry.uh_chen53001ca2014-07-03 20:40:22 +08001289 // SkBitmap::getAddr(int, int) may return NULL due to unrecognized config
1290 // (ex: kRLE_Index8_Config). This will cause memcmp method to crash. Since bm0
1291 // and bm1 both have pixel data() (have passed NULL == getPixels() check),
1292 // those 2 bitmaps should be valid (only unrecognized), we return JNI_FALSE
1293 // to warn user those 2 unrecognized config bitmaps may be different.
John Reckf29ed282015-04-07 07:32:03 -07001294 void *bm0Addr = bm0.getAddr(0, y);
1295 void *bm1Addr = bm1.getAddr(0, y);
henry.uh_chen53001ca2014-07-03 20:40:22 +08001296
1297 if(bm0Addr == NULL || bm1Addr == NULL) {
1298 return JNI_FALSE;
1299 }
1300
1301 if (memcmp(bm0Addr, bm1Addr, size) != 0) {
Chris Craik32054b02014-05-09 13:58:56 -07001302 return JNI_FALSE;
1303 }
1304 }
1305 return JNI_TRUE;
1306}
1307
John Reckc6e2e8f2015-04-15 13:24:47 -07001308static jlong Bitmap_refPixelRef(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -07001309 LocalScopedBitmap bitmap(bitmapHandle);
John Reckae2e8b42015-05-06 14:55:05 -07001310 SkPixelRef* pixelRef = bitmap.valid() ? bitmap->peekAtPixelRef() : nullptr;
John Reckc6e2e8f2015-04-15 13:24:47 -07001311 SkSafeRef(pixelRef);
1312 return reinterpret_cast<jlong>(pixelRef);
1313}
1314
John Reck43871902016-08-01 14:39:24 -07001315static void Bitmap_prepareToDraw(JNIEnv* env, jobject, jlong bitmapPtr) {
1316 LocalScopedBitmap bitmapHandle(bitmapPtr);
1317 if (!bitmapHandle.valid()) return;
1318 SkBitmap bitmap;
1319 bitmapHandle->getSkBitmap(&bitmap);
1320 android::uirenderer::renderthread::RenderProxy::prepareToDraw(bitmap);
1321}
1322
sergeyv45082182016-09-29 18:25:40 -07001323static jint Bitmap_getAllocationByteCount(JNIEnv* env, jobject, jlong bitmapPtr) {
1324 LocalScopedBitmap bitmapHandle(bitmapPtr);
1325 return static_cast<jint>(bitmapHandle->getAllocationByteCount());
1326}
1327
Chris Craik32054b02014-05-09 13:58:56 -07001328///////////////////////////////////////////////////////////////////////////////
1329
Daniel Micay76f6a862015-09-19 17:31:01 -04001330static const JNINativeMethod gBitmapMethods[] = {
Chris Craik32054b02014-05-09 13:58:56 -07001331 { "nativeCreate", "([IIIIIIZ)Landroid/graphics/Bitmap;",
1332 (void*)Bitmap_creator },
1333 { "nativeCopy", "(JIZ)Landroid/graphics/Bitmap;",
1334 (void*)Bitmap_copy },
Riley Andrews721ae5f2015-05-11 16:08:22 -07001335 { "nativeCopyAshmem", "(J)Landroid/graphics/Bitmap;",
1336 (void*)Bitmap_copyAshmem },
Winsona5fdde92016-04-14 15:27:15 -07001337 { "nativeCopyAshmemConfig", "(JI)Landroid/graphics/Bitmap;",
1338 (void*)Bitmap_copyAshmemConfig },
Richard Uhler775873a2015-12-29 12:37:39 -08001339 { "nativeGetNativeFinalizer", "()J", (void*)Bitmap_getNativeFinalizer },
Chris Craik32054b02014-05-09 13:58:56 -07001340 { "nativeRecycle", "(J)Z", (void*)Bitmap_recycle },
sergeyv45082182016-09-29 18:25:40 -07001341 { "nativeReconfigure", "(JIIIZ)V", (void*)Bitmap_reconfigure },
Chris Craik32054b02014-05-09 13:58:56 -07001342 { "nativeCompress", "(JIILjava/io/OutputStream;[B)Z",
1343 (void*)Bitmap_compress },
1344 { "nativeErase", "(JI)V", (void*)Bitmap_erase },
1345 { "nativeRowBytes", "(J)I", (void*)Bitmap_rowBytes },
1346 { "nativeConfig", "(J)I", (void*)Bitmap_config },
1347 { "nativeHasAlpha", "(J)Z", (void*)Bitmap_hasAlpha },
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001348 { "nativeIsPremultiplied", "(J)Z", (void*)Bitmap_isPremultiplied},
1349 { "nativeSetHasAlpha", "(JZZ)V", (void*)Bitmap_setHasAlpha},
1350 { "nativeSetPremultiplied", "(JZ)V", (void*)Bitmap_setPremultiplied},
Chris Craik32054b02014-05-09 13:58:56 -07001351 { "nativeHasMipMap", "(J)Z", (void*)Bitmap_hasMipMap },
1352 { "nativeSetHasMipMap", "(JZ)V", (void*)Bitmap_setHasMipMap },
1353 { "nativeCreateFromParcel",
1354 "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;",
1355 (void*)Bitmap_createFromParcel },
1356 { "nativeWriteToParcel", "(JZILandroid/os/Parcel;)Z",
1357 (void*)Bitmap_writeToParcel },
1358 { "nativeExtractAlpha", "(JJ[I)Landroid/graphics/Bitmap;",
1359 (void*)Bitmap_extractAlpha },
1360 { "nativeGenerationId", "(J)I", (void*)Bitmap_getGenerationId },
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001361 { "nativeGetPixel", "(JII)I", (void*)Bitmap_getPixel },
1362 { "nativeGetPixels", "(J[IIIIIII)V", (void*)Bitmap_getPixels },
1363 { "nativeSetPixel", "(JIII)V", (void*)Bitmap_setPixel },
1364 { "nativeSetPixels", "(J[IIIIIII)V", (void*)Bitmap_setPixels },
Chris Craik32054b02014-05-09 13:58:56 -07001365 { "nativeCopyPixelsToBuffer", "(JLjava/nio/Buffer;)V",
1366 (void*)Bitmap_copyPixelsToBuffer },
1367 { "nativeCopyPixelsFromBuffer", "(JLjava/nio/Buffer;)V",
1368 (void*)Bitmap_copyPixelsFromBuffer },
1369 { "nativeSameAs", "(JJ)Z", (void*)Bitmap_sameAs },
John Reckc6e2e8f2015-04-15 13:24:47 -07001370 { "nativeRefPixelRef", "(J)J", (void*)Bitmap_refPixelRef },
John Reck43871902016-08-01 14:39:24 -07001371 { "nativePrepareToDraw", "(J)V", (void*)Bitmap_prepareToDraw },
sergeyv45082182016-09-29 18:25:40 -07001372 { "nativeGetAllocationByteCount", "(J)I", (void*)Bitmap_getAllocationByteCount },
Chris Craik32054b02014-05-09 13:58:56 -07001373};
1374
Chris Craik32054b02014-05-09 13:58:56 -07001375int register_android_graphics_Bitmap(JNIEnv* env)
1376{
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001377 return android::RegisterMethodsOrDie(env, "android/graphics/Bitmap", gBitmapMethods,
1378 NELEM(gBitmapMethods));
Chris Craik32054b02014-05-09 13:58:56 -07001379}