blob: 72755ce105c8abff87e223d11d499d8296ee5b70 [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
130Bitmap::Bitmap(JNIEnv* env, jbyteArray storageObj, void* address,
131 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
132 : mPixelStorageType(PixelStorageType::Java) {
133 env->GetJavaVM(&mPixelStorage.java.jvm);
134 mPixelStorage.java.jweakRef = env->NewWeakGlobalRef(storageObj);
135 mPixelStorage.java.jstrongRef = nullptr;
136 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
137 // Note: this will trigger a call to onStrongRefDestroyed(), but
138 // we want the pixel ref to have a ref count of 0 at this point
139 mPixelRef->unref();
140}
141
142Bitmap::Bitmap(void* address, void* context, FreeFunc freeFunc,
143 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
144 : mPixelStorageType(PixelStorageType::External) {
145 mPixelStorage.external.address = address;
146 mPixelStorage.external.context = context;
147 mPixelStorage.external.freeFunc = freeFunc;
148 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
149 // Note: this will trigger a call to onStrongRefDestroyed(), but
150 // we want the pixel ref to have a ref count of 0 at this point
151 mPixelRef->unref();
152}
153
John Reckaa394dd2016-09-12 10:43:35 -0700154Bitmap::Bitmap(void* address, int fd, size_t mappedSize,
Riley Andrews39d7f302014-11-13 17:43:25 -0800155 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable)
156 : mPixelStorageType(PixelStorageType::Ashmem) {
157 mPixelStorage.ashmem.address = address;
158 mPixelStorage.ashmem.fd = fd;
John Reckaa394dd2016-09-12 10:43:35 -0700159 mPixelStorage.ashmem.size = mappedSize;
Riley Andrews39d7f302014-11-13 17:43:25 -0800160 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable));
161 // Note: this will trigger a call to onStrongRefDestroyed(), but
162 // we want the pixel ref to have a ref count of 0 at this point
163 mPixelRef->unref();
164}
John Reckf29ed282015-04-07 07:32:03 -0700165Bitmap::~Bitmap() {
166 doFreePixels();
167}
168
169void Bitmap::freePixels() {
170 AutoMutex _lock(mLock);
171 if (mPinnedRefCount == 0) {
172 doFreePixels();
173 mPixelStorageType = PixelStorageType::Invalid;
174 }
175}
176
177void Bitmap::doFreePixels() {
178 switch (mPixelStorageType) {
179 case PixelStorageType::Invalid:
180 // already free'd, nothing to do
181 break;
182 case PixelStorageType::External:
183 mPixelStorage.external.freeFunc(mPixelStorage.external.address,
184 mPixelStorage.external.context);
185 break;
Riley Andrews39d7f302014-11-13 17:43:25 -0800186 case PixelStorageType::Ashmem:
187 munmap(mPixelStorage.ashmem.address, mPixelStorage.ashmem.size);
188 close(mPixelStorage.ashmem.fd);
189 break;
John Reckf29ed282015-04-07 07:32:03 -0700190 case PixelStorageType::Java:
191 JNIEnv* env = jniEnv();
192 LOG_ALWAYS_FATAL_IF(mPixelStorage.java.jstrongRef,
193 "Deleting a bitmap wrapper while there are outstanding strong "
194 "references! mPinnedRefCount = %d", mPinnedRefCount);
195 env->DeleteWeakGlobalRef(mPixelStorage.java.jweakRef);
196 break;
197 }
198
199 if (android::uirenderer::Caches::hasInstance()) {
200 android::uirenderer::Caches::getInstance().textureCache.releaseTexture(
201 mPixelRef->getStableID());
202 }
203}
204
205bool Bitmap::hasHardwareMipMap() {
206 return mPixelRef->hasHardwareMipMap();
207}
208
209void Bitmap::setHasHardwareMipMap(bool hasMipMap) {
210 mPixelRef->setHasHardwareMipMap(hasMipMap);
211}
212
Riley Andrews39d7f302014-11-13 17:43:25 -0800213int Bitmap::getAshmemFd() const {
214 switch (mPixelStorageType) {
215 case PixelStorageType::Ashmem:
216 return mPixelStorage.ashmem.fd;
217 default:
218 return -1;
219 }
220}
221
John Reckf29ed282015-04-07 07:32:03 -0700222const SkImageInfo& Bitmap::info() const {
John Reckf29ed282015-04-07 07:32:03 -0700223 return mPixelRef->info();
224}
225
226size_t Bitmap::rowBytes() const {
227 return mPixelRef->rowBytes();
228}
229
John Reckae2e8b42015-05-06 14:55:05 -0700230SkPixelRef* Bitmap::peekAtPixelRef() const {
John Reckf29ed282015-04-07 07:32:03 -0700231 assertValid();
232 return mPixelRef.get();
233}
234
John Reckae2e8b42015-05-06 14:55:05 -0700235SkPixelRef* Bitmap::refPixelRef() {
236 assertValid();
237 android::AutoMutex _lock(mLock);
238 return refPixelRefLocked();
239}
240
241SkPixelRef* Bitmap::refPixelRefLocked() {
242 mPixelRef->ref();
243 if (mPixelRef->unique()) {
244 // We just restored this from 0, pin the pixels and inc the strong count
245 // Note that there *might be* an incoming onStrongRefDestroyed from whatever
246 // last unref'd
247 pinPixelsLocked();
248 mPinnedRefCount++;
249 }
250 return mPixelRef.get();
251}
252
John Reckf29ed282015-04-07 07:32:03 -0700253void Bitmap::reconfigure(const SkImageInfo& info, size_t rowBytes,
254 SkColorTable* ctable) {
255 mPixelRef->reconfigure(info, rowBytes, ctable);
256}
257
258void Bitmap::reconfigure(const SkImageInfo& info) {
Derek Sollenberger2a94a102015-05-07 13:56:14 -0400259 reconfigure(info, info.minRowBytes(), nullptr);
John Reckf29ed282015-04-07 07:32:03 -0700260}
261
John Reck0781a2f2015-05-27 16:29:17 -0700262void Bitmap::setAlphaType(SkAlphaType alphaType) {
263 if (!SkColorTypeValidateAlphaType(info().colorType(), alphaType, &alphaType)) {
264 return;
265 }
266
267 mPixelRef->changeAlphaType(alphaType);
268}
269
John Reckf29ed282015-04-07 07:32:03 -0700270void Bitmap::detachFromJava() {
271 bool disposeSelf;
272 {
273 android::AutoMutex _lock(mLock);
274 mAttachedToJava = false;
275 disposeSelf = shouldDisposeSelfLocked();
276 }
277 if (disposeSelf) {
278 delete this;
279 }
280}
281
282bool Bitmap::shouldDisposeSelfLocked() {
283 return mPinnedRefCount == 0 && !mAttachedToJava;
284}
285
286JNIEnv* Bitmap::jniEnv() {
287 JNIEnv* env;
288 auto success = mPixelStorage.java.jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
289 LOG_ALWAYS_FATAL_IF(success != JNI_OK,
290 "Failed to get JNIEnv* from JVM: %p", mPixelStorage.java.jvm);
291 return env;
292}
293
294void Bitmap::onStrongRefDestroyed() {
295 bool disposeSelf = false;
296 {
297 android::AutoMutex _lock(mLock);
298 if (mPinnedRefCount > 0) {
299 mPinnedRefCount--;
300 if (mPinnedRefCount == 0) {
301 unpinPixelsLocked();
302 disposeSelf = shouldDisposeSelfLocked();
303 }
304 }
305 }
306 if (disposeSelf) {
307 delete this;
308 }
309}
310
311void Bitmap::pinPixelsLocked() {
312 switch (mPixelStorageType) {
313 case PixelStorageType::Invalid:
314 LOG_ALWAYS_FATAL("Cannot pin invalid pixels!");
315 break;
316 case PixelStorageType::External:
Riley Andrews39d7f302014-11-13 17:43:25 -0800317 case PixelStorageType::Ashmem:
John Reckf29ed282015-04-07 07:32:03 -0700318 // Nothing to do
319 break;
320 case PixelStorageType::Java: {
321 JNIEnv* env = jniEnv();
322 if (!mPixelStorage.java.jstrongRef) {
323 mPixelStorage.java.jstrongRef = reinterpret_cast<jbyteArray>(
324 env->NewGlobalRef(mPixelStorage.java.jweakRef));
325 if (!mPixelStorage.java.jstrongRef) {
326 LOG_ALWAYS_FATAL("Failed to acquire strong reference to pixels");
327 }
328 }
329 break;
330 }
331 }
332}
333
334void Bitmap::unpinPixelsLocked() {
335 switch (mPixelStorageType) {
336 case PixelStorageType::Invalid:
337 LOG_ALWAYS_FATAL("Cannot unpin invalid pixels!");
338 break;
339 case PixelStorageType::External:
Riley Andrews39d7f302014-11-13 17:43:25 -0800340 case PixelStorageType::Ashmem:
John Reckf29ed282015-04-07 07:32:03 -0700341 // Don't need to do anything
342 break;
343 case PixelStorageType::Java: {
344 JNIEnv* env = jniEnv();
345 if (mPixelStorage.java.jstrongRef) {
346 env->DeleteGlobalRef(mPixelStorage.java.jstrongRef);
347 mPixelStorage.java.jstrongRef = nullptr;
348 }
349 break;
350 }
351 }
352}
353
354void Bitmap::getSkBitmap(SkBitmap* outBitmap) {
355 assertValid();
356 android::AutoMutex _lock(mLock);
John Reckf29ed282015-04-07 07:32:03 -0700357 // Safe because mPixelRef is a WrappedPixelRef type, otherwise rowBytes()
358 // would require locking the pixels first.
359 outBitmap->setInfo(mPixelRef->info(), mPixelRef->rowBytes());
John Reckae2e8b42015-05-06 14:55:05 -0700360 outBitmap->setPixelRef(refPixelRefLocked())->unref();
John Reckf29ed282015-04-07 07:32:03 -0700361 outBitmap->setHasHardwareMipMap(hasHardwareMipMap());
362}
363
364void Bitmap::assertValid() const {
365 LOG_ALWAYS_FATAL_IF(mPixelStorageType == PixelStorageType::Invalid,
366 "Error, cannot access an invalid/free'd bitmap here!");
367}
368
369} // namespace android
370
371using namespace android;
372
373// Convenience class that does not take a global ref on the pixels, relying
374// on the caller already having a local JNI ref
375class LocalScopedBitmap {
376public:
377 LocalScopedBitmap(jlong bitmapHandle)
378 : mBitmap(reinterpret_cast<Bitmap*>(bitmapHandle)) {}
379
380 Bitmap* operator->() {
381 return mBitmap;
382 }
383
384 void* pixels() {
John Reckae2e8b42015-05-06 14:55:05 -0700385 return mBitmap->peekAtPixelRef()->pixels();
John Reckf29ed282015-04-07 07:32:03 -0700386 }
387
388 bool valid() {
389 return mBitmap && mBitmap->valid();
390 }
391
392private:
393 Bitmap* mBitmap;
394};
395
Chris Craik32054b02014-05-09 13:58:56 -0700396///////////////////////////////////////////////////////////////////////////////
397// Conversions to/from SkColor, for get/setPixels, and the create method, which
398// is basically like setPixels
399
400typedef void (*FromColorProc)(void* dst, const SkColor src[], int width,
401 int x, int y);
402
403static void FromColor_D32(void* dst, const SkColor src[], int width,
404 int, int) {
405 SkPMColor* d = (SkPMColor*)dst;
406
407 for (int i = 0; i < width; i++) {
408 *d++ = SkPreMultiplyColor(*src++);
409 }
410}
411
412static void FromColor_D32_Raw(void* dst, const SkColor src[], int width,
413 int, int) {
Dan Albert46d84442014-11-18 16:07:51 -0800414 // Needed to thwart the unreachable code detection from clang.
415 static const bool sk_color_ne_zero = SK_COLOR_MATCHES_PMCOLOR_BYTE_ORDER;
416
Chris Craik32054b02014-05-09 13:58:56 -0700417 // SkColor's ordering may be different from SkPMColor
Dan Albert46d84442014-11-18 16:07:51 -0800418 if (sk_color_ne_zero) {
Chris Craik32054b02014-05-09 13:58:56 -0700419 memcpy(dst, src, width * sizeof(SkColor));
420 return;
421 }
422
423 // order isn't same, repack each pixel manually
424 SkPMColor* d = (SkPMColor*)dst;
425 for (int i = 0; i < width; i++) {
426 SkColor c = *src++;
427 *d++ = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c),
428 SkColorGetG(c), SkColorGetB(c));
429 }
430}
431
432static void FromColor_D565(void* dst, const SkColor src[], int width,
433 int x, int y) {
434 uint16_t* d = (uint16_t*)dst;
435
436 DITHER_565_SCAN(y);
437 for (int stop = x + width; x < stop; x++) {
438 SkColor c = *src++;
439 *d++ = SkDitherRGBTo565(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c),
440 DITHER_VALUE(x));
441 }
442}
443
444static void FromColor_D4444(void* dst, const SkColor src[], int width,
445 int x, int y) {
446 SkPMColor16* d = (SkPMColor16*)dst;
447
448 DITHER_4444_SCAN(y);
449 for (int stop = x + width; x < stop; x++) {
450 SkPMColor pmc = SkPreMultiplyColor(*src++);
451 *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x));
452// *d++ = SkPixel32ToPixel4444(pmc);
453 }
454}
455
456static void FromColor_D4444_Raw(void* dst, const SkColor src[], int width,
457 int x, int y) {
458 SkPMColor16* d = (SkPMColor16*)dst;
459
460 DITHER_4444_SCAN(y);
461 for (int stop = x + width; x < stop; x++) {
462 SkColor c = *src++;
463
464 // SkPMColor is used because the ordering is ARGB32, even though the target actually premultiplied
465 SkPMColor pmc = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c),
466 SkColorGetG(c), SkColorGetB(c));
467 *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x));
468// *d++ = SkPixel32ToPixel4444(pmc);
469 }
470}
471
Chris Craik6260b222015-07-24 15:17:29 -0700472static void FromColor_DA8(void* dst, const SkColor src[], int width, int x, int y) {
473 uint8_t* d = (uint8_t*)dst;
474
475 for (int stop = x + width; x < stop; x++) {
476 *d++ = SkColorGetA(*src++);
477 }
478}
479
Chris Craik32054b02014-05-09 13:58:56 -0700480// can return NULL
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400481static FromColorProc ChooseFromColorProc(const SkBitmap& bitmap) {
482 switch (bitmap.colorType()) {
483 case kN32_SkColorType:
484 return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D32 : FromColor_D32_Raw;
485 case kARGB_4444_SkColorType:
486 return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D4444 :
487 FromColor_D4444_Raw;
488 case kRGB_565_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700489 return FromColor_D565;
Chris Craik6260b222015-07-24 15:17:29 -0700490 case kAlpha_8_SkColorType:
491 return FromColor_DA8;
Chris Craik32054b02014-05-09 13:58:56 -0700492 default:
493 break;
494 }
495 return NULL;
496}
497
498bool GraphicsJNI::SetPixels(JNIEnv* env, jintArray srcColors, int srcOffset, int srcStride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400499 int x, int y, int width, int height, const SkBitmap& dstBitmap) {
Chris Craik32054b02014-05-09 13:58:56 -0700500 SkAutoLockPixels alp(dstBitmap);
501 void* dst = dstBitmap.getPixels();
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400502 FromColorProc proc = ChooseFromColorProc(dstBitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700503
504 if (NULL == dst || NULL == proc) {
505 return false;
506 }
507
508 const jint* array = env->GetIntArrayElements(srcColors, NULL);
509 const SkColor* src = (const SkColor*)array + srcOffset;
510
511 // reset to to actual choice from caller
512 dst = dstBitmap.getAddr(x, y);
513 // now copy/convert each scanline
514 for (int y = 0; y < height; y++) {
515 proc(dst, src, width, x, y);
516 src += srcStride;
517 dst = (char*)dst + dstBitmap.rowBytes();
518 }
519
520 dstBitmap.notifyPixelsChanged();
521
522 env->ReleaseIntArrayElements(srcColors, const_cast<jint*>(array),
523 JNI_ABORT);
524 return true;
525}
526
527//////////////////// ToColor procs
528
529typedef void (*ToColorProc)(SkColor dst[], const void* src, int width,
530 SkColorTable*);
531
532static void ToColor_S32_Alpha(SkColor dst[], const void* src, int width,
533 SkColorTable*) {
534 SkASSERT(width > 0);
535 const SkPMColor* s = (const SkPMColor*)src;
536 do {
537 *dst++ = SkUnPreMultiply::PMColorToColor(*s++);
538 } while (--width != 0);
539}
540
541static void ToColor_S32_Raw(SkColor dst[], const void* src, int width,
542 SkColorTable*) {
543 SkASSERT(width > 0);
544 const SkPMColor* s = (const SkPMColor*)src;
545 do {
546 SkPMColor c = *s++;
547 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
548 SkGetPackedG32(c), SkGetPackedB32(c));
549 } while (--width != 0);
550}
551
552static void ToColor_S32_Opaque(SkColor dst[], const void* src, int width,
553 SkColorTable*) {
554 SkASSERT(width > 0);
555 const SkPMColor* s = (const SkPMColor*)src;
556 do {
557 SkPMColor c = *s++;
558 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
559 SkGetPackedB32(c));
560 } while (--width != 0);
561}
562
563static void ToColor_S4444_Alpha(SkColor dst[], const void* src, int width,
564 SkColorTable*) {
565 SkASSERT(width > 0);
566 const SkPMColor16* s = (const SkPMColor16*)src;
567 do {
568 *dst++ = SkUnPreMultiply::PMColorToColor(SkPixel4444ToPixel32(*s++));
569 } while (--width != 0);
570}
571
572static void ToColor_S4444_Raw(SkColor dst[], const void* src, int width,
573 SkColorTable*) {
574 SkASSERT(width > 0);
575 const SkPMColor16* s = (const SkPMColor16*)src;
576 do {
577 SkPMColor c = SkPixel4444ToPixel32(*s++);
578 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
579 SkGetPackedG32(c), SkGetPackedB32(c));
580 } while (--width != 0);
581}
582
583static void ToColor_S4444_Opaque(SkColor dst[], const void* src, int width,
584 SkColorTable*) {
585 SkASSERT(width > 0);
586 const SkPMColor16* s = (const SkPMColor16*)src;
587 do {
588 SkPMColor c = SkPixel4444ToPixel32(*s++);
589 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
590 SkGetPackedB32(c));
591 } while (--width != 0);
592}
593
594static void ToColor_S565(SkColor dst[], const void* src, int width,
595 SkColorTable*) {
596 SkASSERT(width > 0);
597 const uint16_t* s = (const uint16_t*)src;
598 do {
599 uint16_t c = *s++;
600 *dst++ = SkColorSetRGB(SkPacked16ToR32(c), SkPacked16ToG32(c),
601 SkPacked16ToB32(c));
602 } while (--width != 0);
603}
604
605static void ToColor_SI8_Alpha(SkColor dst[], const void* src, int width,
606 SkColorTable* ctable) {
607 SkASSERT(width > 0);
608 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500609 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700610 do {
611 *dst++ = SkUnPreMultiply::PMColorToColor(colors[*s++]);
612 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700613}
614
615static void ToColor_SI8_Raw(SkColor dst[], const void* src, int width,
616 SkColorTable* ctable) {
617 SkASSERT(width > 0);
618 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500619 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700620 do {
621 SkPMColor c = colors[*s++];
622 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c),
623 SkGetPackedG32(c), SkGetPackedB32(c));
624 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700625}
626
627static void ToColor_SI8_Opaque(SkColor dst[], const void* src, int width,
628 SkColorTable* ctable) {
629 SkASSERT(width > 0);
630 const uint8_t* s = (const uint8_t*)src;
Mike Reed71487eb2014-11-19 16:13:20 -0500631 const SkPMColor* colors = ctable->readColors();
Chris Craik32054b02014-05-09 13:58:56 -0700632 do {
633 SkPMColor c = colors[*s++];
634 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
635 SkGetPackedB32(c));
636 } while (--width != 0);
Chris Craik32054b02014-05-09 13:58:56 -0700637}
638
Chris Craik6260b222015-07-24 15:17:29 -0700639static void ToColor_SA8(SkColor dst[], const void* src, int width, SkColorTable*) {
640 SkASSERT(width > 0);
641 const uint8_t* s = (const uint8_t*)src;
642 do {
643 uint8_t c = *s++;
644 *dst++ = SkColorSetARGB(c, c, c, c);
645 } while (--width != 0);
646}
647
Chris Craik32054b02014-05-09 13:58:56 -0700648// can return NULL
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400649static ToColorProc ChooseToColorProc(const SkBitmap& src) {
Mike Reedb9330552014-06-16 17:31:48 -0400650 switch (src.colorType()) {
651 case kN32_SkColorType:
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400652 switch (src.alphaType()) {
653 case kOpaque_SkAlphaType:
654 return ToColor_S32_Opaque;
655 case kPremul_SkAlphaType:
656 return ToColor_S32_Alpha;
657 case kUnpremul_SkAlphaType:
658 return ToColor_S32_Raw;
659 default:
660 return NULL;
661 }
Mike Reedb9330552014-06-16 17:31:48 -0400662 case kARGB_4444_SkColorType:
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400663 switch (src.alphaType()) {
664 case kOpaque_SkAlphaType:
665 return ToColor_S4444_Opaque;
666 case kPremul_SkAlphaType:
667 return ToColor_S4444_Alpha;
668 case kUnpremul_SkAlphaType:
669 return ToColor_S4444_Raw;
670 default:
671 return NULL;
672 }
Mike Reedb9330552014-06-16 17:31:48 -0400673 case kRGB_565_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700674 return ToColor_S565;
Mike Reedb9330552014-06-16 17:31:48 -0400675 case kIndex_8_SkColorType:
Chris Craik32054b02014-05-09 13:58:56 -0700676 if (src.getColorTable() == NULL) {
677 return NULL;
678 }
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400679 switch (src.alphaType()) {
680 case kOpaque_SkAlphaType:
681 return ToColor_SI8_Opaque;
682 case kPremul_SkAlphaType:
683 return ToColor_SI8_Alpha;
684 case kUnpremul_SkAlphaType:
685 return ToColor_SI8_Raw;
686 default:
687 return NULL;
688 }
Chris Craik6260b222015-07-24 15:17:29 -0700689 case kAlpha_8_SkColorType:
690 return ToColor_SA8;
Chris Craik32054b02014-05-09 13:58:56 -0700691 default:
692 break;
693 }
694 return NULL;
695}
696
697///////////////////////////////////////////////////////////////////////////////
698///////////////////////////////////////////////////////////////////////////////
699
700static int getPremulBitmapCreateFlags(bool isMutable) {
701 int flags = GraphicsJNI::kBitmapCreateFlag_Premultiplied;
702 if (isMutable) flags |= GraphicsJNI::kBitmapCreateFlag_Mutable;
703 return flags;
704}
705
706static jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors,
707 jint offset, jint stride, jint width, jint height,
708 jint configHandle, jboolean isMutable) {
Mike Reed1103b322014-07-08 12:36:44 -0400709 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700710 if (NULL != jColors) {
711 size_t n = env->GetArrayLength(jColors);
712 if (n < SkAbs32(stride) * (size_t)height) {
713 doThrowAIOOBE(env);
714 return NULL;
715 }
716 }
717
718 // ARGB_4444 is a deprecated format, convert automatically to 8888
Mike Reedb9330552014-06-16 17:31:48 -0400719 if (colorType == kARGB_4444_SkColorType) {
720 colorType = kN32_SkColorType;
Chris Craik32054b02014-05-09 13:58:56 -0700721 }
722
723 SkBitmap bitmap;
Mike Reedb9330552014-06-16 17:31:48 -0400724 bitmap.setInfo(SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType));
Chris Craik32054b02014-05-09 13:58:56 -0700725
John Reckf29ed282015-04-07 07:32:03 -0700726 Bitmap* nativeBitmap = GraphicsJNI::allocateJavaPixelRef(env, &bitmap, NULL);
727 if (!nativeBitmap) {
Chris Craik32054b02014-05-09 13:58:56 -0700728 return NULL;
729 }
730
731 if (jColors != NULL) {
732 GraphicsJNI::SetPixels(env, jColors, offset, stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400733 0, 0, width, height, bitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700734 }
735
John Reckf29ed282015-04-07 07:32:03 -0700736 return GraphicsJNI::createBitmap(env, nativeBitmap,
737 getPremulBitmapCreateFlags(isMutable));
Chris Craik32054b02014-05-09 13:58:56 -0700738}
739
740static jobject Bitmap_copy(JNIEnv* env, jobject, jlong srcHandle,
741 jint dstConfigHandle, jboolean isMutable) {
John Reckf29ed282015-04-07 07:32:03 -0700742 SkBitmap src;
743 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
Mike Reed1103b322014-07-08 12:36:44 -0400744 SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700745 SkBitmap result;
746 JavaPixelAllocator allocator(env);
747
John Reckf29ed282015-04-07 07:32:03 -0700748 if (!src.copyTo(&result, dstCT, &allocator)) {
Chris Craik32054b02014-05-09 13:58:56 -0700749 return NULL;
750 }
John Reckf29ed282015-04-07 07:32:03 -0700751 Bitmap* bitmap = allocator.getStorageObjAndReset();
752 return GraphicsJNI::createBitmap(env, bitmap,
753 getPremulBitmapCreateFlags(isMutable));
Chris Craik32054b02014-05-09 13:58:56 -0700754}
755
Winsona5fdde92016-04-14 15:27:15 -0700756static Bitmap* Bitmap_copyAshmemImpl(JNIEnv* env, SkBitmap& src, SkColorType& dstCT) {
Riley Andrews721ae5f2015-05-11 16:08:22 -0700757 SkBitmap result;
758
759 AshmemPixelAllocator allocator(env);
Winsona5fdde92016-04-14 15:27:15 -0700760 if (!src.copyTo(&result, dstCT, &allocator)) {
Riley Andrews721ae5f2015-05-11 16:08:22 -0700761 return NULL;
762 }
763 Bitmap* bitmap = allocator.getStorageObjAndReset();
764 bitmap->peekAtPixelRef()->setImmutable();
Winsona5fdde92016-04-14 15:27:15 -0700765 return bitmap;
766}
767
768static jobject Bitmap_copyAshmem(JNIEnv* env, jobject, jlong srcHandle) {
769 SkBitmap src;
770 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
771 SkColorType dstCT = src.colorType();
772 Bitmap* bitmap = Bitmap_copyAshmemImpl(env, src, dstCT);
773 jobject ret = GraphicsJNI::createBitmap(env, bitmap, getPremulBitmapCreateFlags(false));
774 return ret;
775}
776
777static jobject Bitmap_copyAshmemConfig(JNIEnv* env, jobject, jlong srcHandle, jint dstConfigHandle) {
778 SkBitmap src;
779 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
780 SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle);
781 Bitmap* bitmap = Bitmap_copyAshmemImpl(env, src, dstCT);
Riley Andrews721ae5f2015-05-11 16:08:22 -0700782 jobject ret = GraphicsJNI::createBitmap(env, bitmap, getPremulBitmapCreateFlags(false));
783 return ret;
784}
785
Richard Uhler775873a2015-12-29 12:37:39 -0800786static void Bitmap_destruct(Bitmap* bitmap) {
John Reckf29ed282015-04-07 07:32:03 -0700787 bitmap->detachFromJava();
Chris Craik32054b02014-05-09 13:58:56 -0700788}
789
Richard Uhler775873a2015-12-29 12:37:39 -0800790static jlong Bitmap_getNativeFinalizer(JNIEnv*, jobject) {
791 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&Bitmap_destruct));
792}
793
Chris Craik32054b02014-05-09 13:58:56 -0700794static jboolean Bitmap_recycle(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700795 LocalScopedBitmap bitmap(bitmapHandle);
796 bitmap->freePixels();
Chris Craik32054b02014-05-09 13:58:56 -0700797 return JNI_TRUE;
798}
799
800static void Bitmap_reconfigure(JNIEnv* env, jobject clazz, jlong bitmapHandle,
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400801 jint width, jint height, jint configHandle, jint allocSize,
802 jboolean requestPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700803 LocalScopedBitmap bitmap(bitmapHandle);
Mike Reed1103b322014-07-08 12:36:44 -0400804 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400805
806 // ARGB_4444 is a deprecated format, convert automatically to 8888
807 if (colorType == kARGB_4444_SkColorType) {
808 colorType = kN32_SkColorType;
809 }
810
811 if (width * height * SkColorTypeBytesPerPixel(colorType) > allocSize) {
Chris Craik32054b02014-05-09 13:58:56 -0700812 // done in native as there's no way to get BytesPerPixel in Java
813 doThrowIAE(env, "Bitmap not large enough to support new configuration");
814 return;
815 }
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400816 SkAlphaType alphaType;
John Reckf29ed282015-04-07 07:32:03 -0700817 if (bitmap->info().colorType() != kRGB_565_SkColorType
818 && bitmap->info().alphaType() == kOpaque_SkAlphaType) {
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -0400819 // If the original bitmap was set to opaque, keep that setting, unless it
820 // was 565, which is required to be opaque.
821 alphaType = kOpaque_SkAlphaType;
822 } else {
823 // Otherwise respect the premultiplied request.
824 alphaType = requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
825 }
John Reckf29ed282015-04-07 07:32:03 -0700826 bitmap->reconfigure(SkImageInfo::Make(width, height, colorType, alphaType));
Chris Craik32054b02014-05-09 13:58:56 -0700827}
828
829// These must match the int values in Bitmap.java
830enum JavaEncodeFormat {
831 kJPEG_JavaEncodeFormat = 0,
832 kPNG_JavaEncodeFormat = 1,
833 kWEBP_JavaEncodeFormat = 2
834};
835
836static jboolean Bitmap_compress(JNIEnv* env, jobject clazz, jlong bitmapHandle,
837 jint format, jint quality,
838 jobject jstream, jbyteArray jstorage) {
John Reckf29ed282015-04-07 07:32:03 -0700839
840 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700841 SkImageEncoder::Type fm;
842
843 switch (format) {
844 case kJPEG_JavaEncodeFormat:
845 fm = SkImageEncoder::kJPEG_Type;
846 break;
847 case kPNG_JavaEncodeFormat:
848 fm = SkImageEncoder::kPNG_Type;
849 break;
850 case kWEBP_JavaEncodeFormat:
851 fm = SkImageEncoder::kWEBP_Type;
852 break;
853 default:
854 return JNI_FALSE;
855 }
856
John Reckf29ed282015-04-07 07:32:03 -0700857 if (!bitmap.valid()) {
858 return JNI_FALSE;
859 }
860
Chris Craik32054b02014-05-09 13:58:56 -0700861 bool success = false;
Chris Craik32054b02014-05-09 13:58:56 -0700862
John Reckf29ed282015-04-07 07:32:03 -0700863 std::unique_ptr<SkWStream> strm(CreateJavaOutputStreamAdaptor(env, jstream, jstorage));
864 if (!strm.get()) {
865 return JNI_FALSE;
866 }
Chris Craik32054b02014-05-09 13:58:56 -0700867
John Reckf29ed282015-04-07 07:32:03 -0700868 std::unique_ptr<SkImageEncoder> encoder(SkImageEncoder::Create(fm));
869 if (encoder.get()) {
870 SkBitmap skbitmap;
871 bitmap->getSkBitmap(&skbitmap);
872 success = encoder->encodeStream(strm.get(), skbitmap, quality);
Chris Craik32054b02014-05-09 13:58:56 -0700873 }
874 return success ? JNI_TRUE : JNI_FALSE;
875}
876
877static void Bitmap_erase(JNIEnv* env, jobject, jlong bitmapHandle, jint color) {
John Reckf29ed282015-04-07 07:32:03 -0700878 LocalScopedBitmap bitmap(bitmapHandle);
879 SkBitmap skBitmap;
880 bitmap->getSkBitmap(&skBitmap);
881 skBitmap.eraseColor(color);
Chris Craik32054b02014-05-09 13:58:56 -0700882}
883
884static jint Bitmap_rowBytes(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700885 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700886 return static_cast<jint>(bitmap->rowBytes());
887}
888
889static jint Bitmap_config(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700890 LocalScopedBitmap bitmap(bitmapHandle);
891 return GraphicsJNI::colorTypeToLegacyBitmapConfig(bitmap->info().colorType());
Chris Craik32054b02014-05-09 13:58:56 -0700892}
893
894static jint Bitmap_getGenerationId(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700895 LocalScopedBitmap bitmap(bitmapHandle);
John Reckae2e8b42015-05-06 14:55:05 -0700896 return static_cast<jint>(bitmap->peekAtPixelRef()->getGenerationID());
Chris Craik32054b02014-05-09 13:58:56 -0700897}
898
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400899static jboolean Bitmap_isPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700900 LocalScopedBitmap bitmap(bitmapHandle);
901 if (bitmap->info().alphaType() == kPremul_SkAlphaType) {
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400902 return JNI_TRUE;
903 }
904 return JNI_FALSE;
905}
906
Chris Craik32054b02014-05-09 13:58:56 -0700907static jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700908 LocalScopedBitmap bitmap(bitmapHandle);
909 return !bitmap->info().isOpaque() ? JNI_TRUE : JNI_FALSE;
Chris Craik32054b02014-05-09 13:58:56 -0700910}
911
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400912static void Bitmap_setHasAlpha(JNIEnv* env, jobject, jlong bitmapHandle,
913 jboolean hasAlpha, jboolean requestPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700914 LocalScopedBitmap bitmap(bitmapHandle);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400915 if (hasAlpha) {
John Reck0781a2f2015-05-27 16:29:17 -0700916 bitmap->setAlphaType(
John Reckf29ed282015-04-07 07:32:03 -0700917 requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType);
Chris Craik32054b02014-05-09 13:58:56 -0700918 } else {
John Reck0781a2f2015-05-27 16:29:17 -0700919 bitmap->setAlphaType(kOpaque_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400920 }
921}
922
923static void Bitmap_setPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle,
924 jboolean isPremul) {
John Reckf29ed282015-04-07 07:32:03 -0700925 LocalScopedBitmap bitmap(bitmapHandle);
926 if (!bitmap->info().isOpaque()) {
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400927 if (isPremul) {
John Reck0781a2f2015-05-27 16:29:17 -0700928 bitmap->setAlphaType(kPremul_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400929 } else {
John Reck0781a2f2015-05-27 16:29:17 -0700930 bitmap->setAlphaType(kUnpremul_SkAlphaType);
Leon Scroggins III57ee6202014-06-04 18:51:07 -0400931 }
Chris Craik32054b02014-05-09 13:58:56 -0700932 }
933}
934
935static jboolean Bitmap_hasMipMap(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -0700936 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700937 return bitmap->hasHardwareMipMap() ? JNI_TRUE : JNI_FALSE;
938}
939
940static void Bitmap_setHasMipMap(JNIEnv* env, jobject, jlong bitmapHandle,
941 jboolean hasMipMap) {
John Reckf29ed282015-04-07 07:32:03 -0700942 LocalScopedBitmap bitmap(bitmapHandle);
Chris Craik32054b02014-05-09 13:58:56 -0700943 bitmap->setHasHardwareMipMap(hasMipMap);
944}
945
946///////////////////////////////////////////////////////////////////////////////
947
948static jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) {
949 if (parcel == NULL) {
950 SkDebugf("-------- unparcel parcel is NULL\n");
951 return NULL;
952 }
953
954 android::Parcel* p = android::parcelForJavaObject(env, parcel);
955
Mike Reedb9330552014-06-16 17:31:48 -0400956 const bool isMutable = p->readInt32() != 0;
957 const SkColorType colorType = (SkColorType)p->readInt32();
958 const SkAlphaType alphaType = (SkAlphaType)p->readInt32();
959 const int width = p->readInt32();
960 const int height = p->readInt32();
961 const int rowBytes = p->readInt32();
962 const int density = p->readInt32();
Chris Craik32054b02014-05-09 13:58:56 -0700963
Mike Reedb9330552014-06-16 17:31:48 -0400964 if (kN32_SkColorType != colorType &&
965 kRGB_565_SkColorType != colorType &&
966 kARGB_4444_SkColorType != colorType &&
967 kIndex_8_SkColorType != colorType &&
968 kAlpha_8_SkColorType != colorType) {
969 SkDebugf("Bitmap_createFromParcel unknown colortype: %d\n", colorType);
Chris Craik32054b02014-05-09 13:58:56 -0700970 return NULL;
971 }
972
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400973 std::unique_ptr<SkBitmap> bitmap(new SkBitmap);
Chris Craik32054b02014-05-09 13:58:56 -0700974
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400975 if (!bitmap->setInfo(SkImageInfo::Make(width, height, colorType, alphaType), rowBytes)) {
976 return NULL;
977 }
Chris Craik32054b02014-05-09 13:58:56 -0700978
979 SkColorTable* ctable = NULL;
Mike Reedb9330552014-06-16 17:31:48 -0400980 if (colorType == kIndex_8_SkColorType) {
Chris Craik32054b02014-05-09 13:58:56 -0700981 int count = p->readInt32();
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400982 if (count < 0 || count > 256) {
983 // The data is corrupt, since SkColorTable enforces a value between 0 and 256,
984 // inclusive.
985 return NULL;
986 }
Chris Craik32054b02014-05-09 13:58:56 -0700987 if (count > 0) {
988 size_t size = count * sizeof(SkPMColor);
989 const SkPMColor* src = (const SkPMColor*)p->readInplace(size);
Leon Scroggins IIIec419e02015-03-11 13:12:06 -0400990 if (src == NULL) {
991 return NULL;
992 }
Chris Craik32054b02014-05-09 13:58:56 -0700993 ctable = new SkColorTable(src, count);
994 }
995 }
996
Jeff Browna316c5d2015-06-05 15:14:06 -0700997 // Read the bitmap blob.
998 size_t size = bitmap->getSize();
999 android::Parcel::ReadableBlob blob;
1000 android::status_t status = p->readBlob(size, &blob);
1001 if (status) {
Chris Craik32054b02014-05-09 13:58:56 -07001002 SkSafeUnref(ctable);
Jeff Browna316c5d2015-06-05 15:14:06 -07001003 doThrowRE(env, "Could not read bitmap blob.");
Chris Craik32054b02014-05-09 13:58:56 -07001004 return NULL;
1005 }
1006
Jeff Browna316c5d2015-06-05 15:14:06 -07001007 // Map the bitmap in place from the ashmem region if possible otherwise copy.
1008 Bitmap* nativeBitmap;
Riley Andrews8cee7c12015-11-01 23:36:04 -08001009 if (blob.fd() >= 0 && (blob.isMutable() || !isMutable) && (size >= ASHMEM_BITMAP_MIN_SIZE)) {
Jeff Browna316c5d2015-06-05 15:14:06 -07001010#if DEBUG_PARCEL
1011 ALOGD("Bitmap.createFromParcel: mapped contents of %s bitmap from %s blob "
1012 "(fds %s)",
1013 isMutable ? "mutable" : "immutable",
1014 blob.isMutable() ? "mutable" : "immutable",
1015 p->allowFds() ? "allowed" : "forbidden");
1016#endif
1017 // Dup the file descriptor so we can keep a reference to it after the Parcel
1018 // is disposed.
1019 int dupFd = dup(blob.fd());
1020 if (dupFd < 0) {
Erik Wolsheimer211abad2015-11-13 11:54:47 -08001021 ALOGE("Error allocating dup fd. Error:%d", errno);
Jeff Browna316c5d2015-06-05 15:14:06 -07001022 blob.release();
1023 SkSafeUnref(ctable);
1024 doThrowRE(env, "Could not allocate dup blob fd.");
1025 return NULL;
1026 }
1027
1028 // Map the pixels in place and take ownership of the ashmem region.
1029 nativeBitmap = GraphicsJNI::mapAshmemPixelRef(env, bitmap.get(),
John Reckaa394dd2016-09-12 10:43:35 -07001030 ctable, dupFd, const_cast<void*>(blob.data()), size, !isMutable);
Jeff Browna316c5d2015-06-05 15:14:06 -07001031 SkSafeUnref(ctable);
1032 if (!nativeBitmap) {
1033 close(dupFd);
1034 blob.release();
1035 doThrowRE(env, "Could not allocate ashmem pixel ref.");
1036 return NULL;
1037 }
1038
1039 // Clear the blob handle, don't release it.
1040 blob.clear();
1041 } else {
1042#if DEBUG_PARCEL
1043 if (blob.fd() >= 0) {
1044 ALOGD("Bitmap.createFromParcel: copied contents of mutable bitmap "
1045 "from immutable blob (fds %s)",
1046 p->allowFds() ? "allowed" : "forbidden");
1047 } else {
1048 ALOGD("Bitmap.createFromParcel: copied contents from %s blob "
1049 "(fds %s)",
1050 blob.isMutable() ? "mutable" : "immutable",
1051 p->allowFds() ? "allowed" : "forbidden");
1052 }
1053#endif
1054
1055 // Copy the pixels into a new buffer.
1056 nativeBitmap = GraphicsJNI::allocateJavaPixelRef(env, bitmap.get(), ctable);
1057 SkSafeUnref(ctable);
1058 if (!nativeBitmap) {
1059 blob.release();
1060 doThrowRE(env, "Could not allocate java pixel ref.");
1061 return NULL;
1062 }
1063 bitmap->lockPixels();
1064 memcpy(bitmap->getPixels(), blob.data(), size);
1065 bitmap->unlockPixels();
1066
1067 // Release the blob handle.
1068 blob.release();
Chris Craik32054b02014-05-09 13:58:56 -07001069 }
Chris Craik32054b02014-05-09 13:58:56 -07001070
John Reckf29ed282015-04-07 07:32:03 -07001071 return GraphicsJNI::createBitmap(env, nativeBitmap,
Leon Scroggins IIIec419e02015-03-11 13:12:06 -04001072 getPremulBitmapCreateFlags(isMutable), NULL, NULL, density);
Chris Craik32054b02014-05-09 13:58:56 -07001073}
1074
1075static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject,
1076 jlong bitmapHandle,
1077 jboolean isMutable, jint density,
1078 jobject parcel) {
Chris Craik32054b02014-05-09 13:58:56 -07001079 if (parcel == NULL) {
1080 SkDebugf("------- writeToParcel null parcel\n");
1081 return JNI_FALSE;
1082 }
1083
1084 android::Parcel* p = android::parcelForJavaObject(env, parcel);
John Reckf29ed282015-04-07 07:32:03 -07001085 SkBitmap bitmap;
Riley Andrews39d7f302014-11-13 17:43:25 -08001086
1087 android::Bitmap* androidBitmap = reinterpret_cast<Bitmap*>(bitmapHandle);
1088 androidBitmap->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001089
1090 p->writeInt32(isMutable);
John Reckf29ed282015-04-07 07:32:03 -07001091 p->writeInt32(bitmap.colorType());
1092 p->writeInt32(bitmap.alphaType());
1093 p->writeInt32(bitmap.width());
1094 p->writeInt32(bitmap.height());
1095 p->writeInt32(bitmap.rowBytes());
Chris Craik32054b02014-05-09 13:58:56 -07001096 p->writeInt32(density);
1097
John Reckf29ed282015-04-07 07:32:03 -07001098 if (bitmap.colorType() == kIndex_8_SkColorType) {
Leon Scroggins III66ce1c32016-02-02 11:11:55 -05001099 // The bitmap needs to be locked to access its color table.
1100 SkAutoLockPixels alp(bitmap);
John Reckf29ed282015-04-07 07:32:03 -07001101 SkColorTable* ctable = bitmap.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001102 if (ctable != NULL) {
1103 int count = ctable->count();
1104 p->writeInt32(count);
1105 memcpy(p->writeInplace(count * sizeof(SkPMColor)),
Mike Reed71487eb2014-11-19 16:13:20 -05001106 ctable->readColors(), count * sizeof(SkPMColor));
Chris Craik32054b02014-05-09 13:58:56 -07001107 } else {
1108 p->writeInt32(0); // indicate no ctable
1109 }
1110 }
1111
Jeff Browna316c5d2015-06-05 15:14:06 -07001112 // Transfer the underlying ashmem region if we have one and it's immutable.
1113 android::status_t status;
1114 int fd = androidBitmap->getAshmemFd();
1115 if (fd >= 0 && !isMutable && p->allowFds()) {
1116#if DEBUG_PARCEL
1117 ALOGD("Bitmap.writeToParcel: transferring immutable bitmap's ashmem fd as "
1118 "immutable blob (fds %s)",
1119 p->allowFds() ? "allowed" : "forbidden");
1120#endif
1121
1122 status = p->writeDupImmutableBlobFileDescriptor(fd);
1123 if (status) {
1124 doThrowRE(env, "Could not write bitmap blob file descriptor.");
Riley Andrews39d7f302014-11-13 17:43:25 -08001125 return JNI_FALSE;
1126 }
Jeff Browna316c5d2015-06-05 15:14:06 -07001127 return JNI_TRUE;
Riley Andrews39d7f302014-11-13 17:43:25 -08001128 }
Jeff Browna316c5d2015-06-05 15:14:06 -07001129
1130 // Copy the bitmap to a new blob.
1131 bool mutableCopy = isMutable;
1132#if DEBUG_PARCEL
1133 ALOGD("Bitmap.writeToParcel: copying %s bitmap into new %s blob (fds %s)",
1134 isMutable ? "mutable" : "immutable",
1135 mutableCopy ? "mutable" : "immutable",
1136 p->allowFds() ? "allowed" : "forbidden");
1137#endif
1138
1139 size_t size = bitmap.getSize();
1140 android::Parcel::WritableBlob blob;
1141 status = p->writeBlob(size, mutableCopy, &blob);
1142 if (status) {
1143 doThrowRE(env, "Could not copy bitmap to parcel blob.");
1144 return JNI_FALSE;
1145 }
1146
1147 bitmap.lockPixels();
1148 const void* pSrc = bitmap.getPixels();
1149 if (pSrc == NULL) {
1150 memset(blob.data(), 0, size);
1151 } else {
1152 memcpy(blob.data(), pSrc, size);
1153 }
1154 bitmap.unlockPixels();
1155
1156 blob.release();
Chris Craik32054b02014-05-09 13:58:56 -07001157 return JNI_TRUE;
1158}
1159
1160static jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz,
1161 jlong srcHandle, jlong paintHandle,
1162 jintArray offsetXY) {
John Reckf29ed282015-04-07 07:32:03 -07001163 SkBitmap src;
1164 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src);
Behdad Esfahbod6ba30b82014-07-15 16:22:32 -04001165 const android::Paint* paint = reinterpret_cast<android::Paint*>(paintHandle);
Chris Craik32054b02014-05-09 13:58:56 -07001166 SkIPoint offset;
John Reckf29ed282015-04-07 07:32:03 -07001167 SkBitmap dst;
Chris Craik32054b02014-05-09 13:58:56 -07001168 JavaPixelAllocator allocator(env);
1169
John Reckf29ed282015-04-07 07:32:03 -07001170 src.extractAlpha(&dst, paint, &allocator, &offset);
Chris Craik32054b02014-05-09 13:58:56 -07001171 // If Skia can't allocate pixels for destination bitmap, it resets
1172 // it, that is set its pixels buffer to NULL, and zero width and height.
John Reckf29ed282015-04-07 07:32:03 -07001173 if (dst.getPixels() == NULL && src.getPixels() != NULL) {
Chris Craik32054b02014-05-09 13:58:56 -07001174 doThrowOOME(env, "failed to allocate pixels for alpha");
1175 return NULL;
1176 }
1177 if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) {
1178 int* array = env->GetIntArrayElements(offsetXY, NULL);
1179 array[0] = offset.fX;
1180 array[1] = offset.fY;
1181 env->ReleaseIntArrayElements(offsetXY, array, 0);
1182 }
1183
John Reckf29ed282015-04-07 07:32:03 -07001184 return GraphicsJNI::createBitmap(env, allocator.getStorageObjAndReset(),
1185 getPremulBitmapCreateFlags(true));
Chris Craik32054b02014-05-09 13:58:56 -07001186}
1187
1188///////////////////////////////////////////////////////////////////////////////
1189
1190static jint Bitmap_getPixel(JNIEnv* env, jobject, jlong bitmapHandle,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001191 jint x, jint y) {
John Reckf29ed282015-04-07 07:32:03 -07001192 SkBitmap bitmap;
1193 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1194 SkAutoLockPixels alp(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001195
John Reckf29ed282015-04-07 07:32:03 -07001196 ToColorProc proc = ChooseToColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001197 if (NULL == proc) {
1198 return 0;
1199 }
John Reckf29ed282015-04-07 07:32:03 -07001200 const void* src = bitmap.getAddr(x, y);
Chris Craik32054b02014-05-09 13:58:56 -07001201 if (NULL == src) {
1202 return 0;
1203 }
1204
1205 SkColor dst[1];
John Reckf29ed282015-04-07 07:32:03 -07001206 proc(dst, src, 1, bitmap.getColorTable());
Chris Craik32054b02014-05-09 13:58:56 -07001207 return static_cast<jint>(dst[0]);
1208}
1209
1210static void Bitmap_getPixels(JNIEnv* env, jobject, jlong bitmapHandle,
1211 jintArray pixelArray, jint offset, jint stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001212 jint x, jint y, jint width, jint height) {
John Reckf29ed282015-04-07 07:32:03 -07001213 SkBitmap bitmap;
1214 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1215 SkAutoLockPixels alp(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001216
John Reckf29ed282015-04-07 07:32:03 -07001217 ToColorProc proc = ChooseToColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001218 if (NULL == proc) {
1219 return;
1220 }
John Reckf29ed282015-04-07 07:32:03 -07001221 const void* src = bitmap.getAddr(x, y);
Chris Craik32054b02014-05-09 13:58:56 -07001222 if (NULL == src) {
1223 return;
1224 }
1225
John Reckf29ed282015-04-07 07:32:03 -07001226 SkColorTable* ctable = bitmap.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001227 jint* dst = env->GetIntArrayElements(pixelArray, NULL);
1228 SkColor* d = (SkColor*)dst + offset;
1229 while (--height >= 0) {
1230 proc(d, src, width, ctable);
1231 d += stride;
John Reckf29ed282015-04-07 07:32:03 -07001232 src = (void*)((const char*)src + bitmap.rowBytes());
Chris Craik32054b02014-05-09 13:58:56 -07001233 }
1234 env->ReleaseIntArrayElements(pixelArray, dst, 0);
1235}
1236
1237///////////////////////////////////////////////////////////////////////////////
1238
1239static void Bitmap_setPixel(JNIEnv* env, jobject, jlong bitmapHandle,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001240 jint x, jint y, jint colorHandle) {
John Reckf29ed282015-04-07 07:32:03 -07001241 SkBitmap bitmap;
1242 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001243 SkColor color = static_cast<SkColor>(colorHandle);
John Reckf29ed282015-04-07 07:32:03 -07001244 SkAutoLockPixels alp(bitmap);
1245 if (NULL == bitmap.getPixels()) {
Chris Craik32054b02014-05-09 13:58:56 -07001246 return;
1247 }
1248
John Reckf29ed282015-04-07 07:32:03 -07001249 FromColorProc proc = ChooseFromColorProc(bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001250 if (NULL == proc) {
1251 return;
1252 }
1253
John Reckf29ed282015-04-07 07:32:03 -07001254 proc(bitmap.getAddr(x, y), &color, 1, x, y);
1255 bitmap.notifyPixelsChanged();
Chris Craik32054b02014-05-09 13:58:56 -07001256}
1257
1258static void Bitmap_setPixels(JNIEnv* env, jobject, jlong bitmapHandle,
1259 jintArray pixelArray, jint offset, jint stride,
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001260 jint x, jint y, jint width, jint height) {
John Reckf29ed282015-04-07 07:32:03 -07001261 SkBitmap bitmap;
1262 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001263 GraphicsJNI::SetPixels(env, pixelArray, offset, stride,
John Reckf29ed282015-04-07 07:32:03 -07001264 x, y, width, height, bitmap);
Chris Craik32054b02014-05-09 13:58:56 -07001265}
1266
1267static void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject,
1268 jlong bitmapHandle, jobject jbuffer) {
John Reckf29ed282015-04-07 07:32:03 -07001269 SkBitmap bitmap;
1270 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1271 SkAutoLockPixels alp(bitmap);
1272 const void* src = bitmap.getPixels();
Chris Craik32054b02014-05-09 13:58:56 -07001273
1274 if (NULL != src) {
1275 android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE);
1276
1277 // the java side has already checked that buffer is large enough
John Reckf29ed282015-04-07 07:32:03 -07001278 memcpy(abp.pointer(), src, bitmap.getSize());
Chris Craik32054b02014-05-09 13:58:56 -07001279 }
1280}
1281
1282static void Bitmap_copyPixelsFromBuffer(JNIEnv* env, jobject,
1283 jlong bitmapHandle, jobject jbuffer) {
John Reckf29ed282015-04-07 07:32:03 -07001284 SkBitmap bitmap;
1285 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap);
1286 SkAutoLockPixels alp(bitmap);
1287 void* dst = bitmap.getPixels();
Chris Craik32054b02014-05-09 13:58:56 -07001288
1289 if (NULL != dst) {
1290 android::AutoBufferPointer abp(env, jbuffer, JNI_FALSE);
1291 // the java side has already checked that buffer is large enough
John Reckf29ed282015-04-07 07:32:03 -07001292 memcpy(dst, abp.pointer(), bitmap.getSize());
1293 bitmap.notifyPixelsChanged();
Chris Craik32054b02014-05-09 13:58:56 -07001294 }
1295}
1296
1297static jboolean Bitmap_sameAs(JNIEnv* env, jobject, jlong bm0Handle,
1298 jlong bm1Handle) {
John Reckf29ed282015-04-07 07:32:03 -07001299 SkBitmap bm0;
1300 SkBitmap bm1;
1301 reinterpret_cast<Bitmap*>(bm0Handle)->getSkBitmap(&bm0);
1302 reinterpret_cast<Bitmap*>(bm1Handle)->getSkBitmap(&bm1);
1303 if (bm0.width() != bm1.width() ||
1304 bm0.height() != bm1.height() ||
1305 bm0.colorType() != bm1.colorType()) {
Chris Craik32054b02014-05-09 13:58:56 -07001306 return JNI_FALSE;
1307 }
1308
John Reckf29ed282015-04-07 07:32:03 -07001309 SkAutoLockPixels alp0(bm0);
1310 SkAutoLockPixels alp1(bm1);
Chris Craik32054b02014-05-09 13:58:56 -07001311
1312 // if we can't load the pixels, return false
John Reckf29ed282015-04-07 07:32:03 -07001313 if (NULL == bm0.getPixels() || NULL == bm1.getPixels()) {
Chris Craik32054b02014-05-09 13:58:56 -07001314 return JNI_FALSE;
1315 }
1316
John Reckf29ed282015-04-07 07:32:03 -07001317 if (bm0.colorType() == kIndex_8_SkColorType) {
1318 SkColorTable* ct0 = bm0.getColorTable();
1319 SkColorTable* ct1 = bm1.getColorTable();
Chris Craik32054b02014-05-09 13:58:56 -07001320 if (NULL == ct0 || NULL == ct1) {
1321 return JNI_FALSE;
1322 }
1323 if (ct0->count() != ct1->count()) {
1324 return JNI_FALSE;
1325 }
1326
Chris Craik32054b02014-05-09 13:58:56 -07001327 const size_t size = ct0->count() * sizeof(SkPMColor);
Mike Reed71487eb2014-11-19 16:13:20 -05001328 if (memcmp(ct0->readColors(), ct1->readColors(), size) != 0) {
Chris Craik32054b02014-05-09 13:58:56 -07001329 return JNI_FALSE;
1330 }
1331 }
1332
1333 // now compare each scanline. We can't do the entire buffer at once,
1334 // since we don't care about the pixel values that might extend beyond
1335 // the width (since the scanline might be larger than the logical width)
John Reckf29ed282015-04-07 07:32:03 -07001336 const int h = bm0.height();
1337 const size_t size = bm0.width() * bm0.bytesPerPixel();
Chris Craik32054b02014-05-09 13:58:56 -07001338 for (int y = 0; y < h; y++) {
henry.uh_chen53001ca2014-07-03 20:40:22 +08001339 // SkBitmap::getAddr(int, int) may return NULL due to unrecognized config
1340 // (ex: kRLE_Index8_Config). This will cause memcmp method to crash. Since bm0
1341 // and bm1 both have pixel data() (have passed NULL == getPixels() check),
1342 // those 2 bitmaps should be valid (only unrecognized), we return JNI_FALSE
1343 // to warn user those 2 unrecognized config bitmaps may be different.
John Reckf29ed282015-04-07 07:32:03 -07001344 void *bm0Addr = bm0.getAddr(0, y);
1345 void *bm1Addr = bm1.getAddr(0, y);
henry.uh_chen53001ca2014-07-03 20:40:22 +08001346
1347 if(bm0Addr == NULL || bm1Addr == NULL) {
1348 return JNI_FALSE;
1349 }
1350
1351 if (memcmp(bm0Addr, bm1Addr, size) != 0) {
Chris Craik32054b02014-05-09 13:58:56 -07001352 return JNI_FALSE;
1353 }
1354 }
1355 return JNI_TRUE;
1356}
1357
John Reckc6e2e8f2015-04-15 13:24:47 -07001358static jlong Bitmap_refPixelRef(JNIEnv* env, jobject, jlong bitmapHandle) {
John Reckf29ed282015-04-07 07:32:03 -07001359 LocalScopedBitmap bitmap(bitmapHandle);
John Reckae2e8b42015-05-06 14:55:05 -07001360 SkPixelRef* pixelRef = bitmap.valid() ? bitmap->peekAtPixelRef() : nullptr;
John Reckc6e2e8f2015-04-15 13:24:47 -07001361 SkSafeRef(pixelRef);
1362 return reinterpret_cast<jlong>(pixelRef);
1363}
1364
John Reck43871902016-08-01 14:39:24 -07001365static void Bitmap_prepareToDraw(JNIEnv* env, jobject, jlong bitmapPtr) {
1366 LocalScopedBitmap bitmapHandle(bitmapPtr);
1367 if (!bitmapHandle.valid()) return;
1368 SkBitmap bitmap;
1369 bitmapHandle->getSkBitmap(&bitmap);
1370 android::uirenderer::renderthread::RenderProxy::prepareToDraw(bitmap);
1371}
1372
Chris Craik32054b02014-05-09 13:58:56 -07001373///////////////////////////////////////////////////////////////////////////////
1374
Daniel Micay76f6a862015-09-19 17:31:01 -04001375static const JNINativeMethod gBitmapMethods[] = {
Chris Craik32054b02014-05-09 13:58:56 -07001376 { "nativeCreate", "([IIIIIIZ)Landroid/graphics/Bitmap;",
1377 (void*)Bitmap_creator },
1378 { "nativeCopy", "(JIZ)Landroid/graphics/Bitmap;",
1379 (void*)Bitmap_copy },
Riley Andrews721ae5f2015-05-11 16:08:22 -07001380 { "nativeCopyAshmem", "(J)Landroid/graphics/Bitmap;",
1381 (void*)Bitmap_copyAshmem },
Winsona5fdde92016-04-14 15:27:15 -07001382 { "nativeCopyAshmemConfig", "(JI)Landroid/graphics/Bitmap;",
1383 (void*)Bitmap_copyAshmemConfig },
Richard Uhler775873a2015-12-29 12:37:39 -08001384 { "nativeGetNativeFinalizer", "()J", (void*)Bitmap_getNativeFinalizer },
Chris Craik32054b02014-05-09 13:58:56 -07001385 { "nativeRecycle", "(J)Z", (void*)Bitmap_recycle },
Leon Scroggins III17a8bfc2014-06-03 16:15:15 -04001386 { "nativeReconfigure", "(JIIIIZ)V", (void*)Bitmap_reconfigure },
Chris Craik32054b02014-05-09 13:58:56 -07001387 { "nativeCompress", "(JIILjava/io/OutputStream;[B)Z",
1388 (void*)Bitmap_compress },
1389 { "nativeErase", "(JI)V", (void*)Bitmap_erase },
1390 { "nativeRowBytes", "(J)I", (void*)Bitmap_rowBytes },
1391 { "nativeConfig", "(J)I", (void*)Bitmap_config },
1392 { "nativeHasAlpha", "(J)Z", (void*)Bitmap_hasAlpha },
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001393 { "nativeIsPremultiplied", "(J)Z", (void*)Bitmap_isPremultiplied},
1394 { "nativeSetHasAlpha", "(JZZ)V", (void*)Bitmap_setHasAlpha},
1395 { "nativeSetPremultiplied", "(JZ)V", (void*)Bitmap_setPremultiplied},
Chris Craik32054b02014-05-09 13:58:56 -07001396 { "nativeHasMipMap", "(J)Z", (void*)Bitmap_hasMipMap },
1397 { "nativeSetHasMipMap", "(JZ)V", (void*)Bitmap_setHasMipMap },
1398 { "nativeCreateFromParcel",
1399 "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;",
1400 (void*)Bitmap_createFromParcel },
1401 { "nativeWriteToParcel", "(JZILandroid/os/Parcel;)Z",
1402 (void*)Bitmap_writeToParcel },
1403 { "nativeExtractAlpha", "(JJ[I)Landroid/graphics/Bitmap;",
1404 (void*)Bitmap_extractAlpha },
1405 { "nativeGenerationId", "(J)I", (void*)Bitmap_getGenerationId },
Leon Scroggins III57ee6202014-06-04 18:51:07 -04001406 { "nativeGetPixel", "(JII)I", (void*)Bitmap_getPixel },
1407 { "nativeGetPixels", "(J[IIIIIII)V", (void*)Bitmap_getPixels },
1408 { "nativeSetPixel", "(JIII)V", (void*)Bitmap_setPixel },
1409 { "nativeSetPixels", "(J[IIIIIII)V", (void*)Bitmap_setPixels },
Chris Craik32054b02014-05-09 13:58:56 -07001410 { "nativeCopyPixelsToBuffer", "(JLjava/nio/Buffer;)V",
1411 (void*)Bitmap_copyPixelsToBuffer },
1412 { "nativeCopyPixelsFromBuffer", "(JLjava/nio/Buffer;)V",
1413 (void*)Bitmap_copyPixelsFromBuffer },
1414 { "nativeSameAs", "(JJ)Z", (void*)Bitmap_sameAs },
John Reckc6e2e8f2015-04-15 13:24:47 -07001415 { "nativeRefPixelRef", "(J)J", (void*)Bitmap_refPixelRef },
John Reck43871902016-08-01 14:39:24 -07001416 { "nativePrepareToDraw", "(J)V", (void*)Bitmap_prepareToDraw },
Chris Craik32054b02014-05-09 13:58:56 -07001417};
1418
Chris Craik32054b02014-05-09 13:58:56 -07001419int register_android_graphics_Bitmap(JNIEnv* env)
1420{
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001421 return android::RegisterMethodsOrDie(env, "android/graphics/Bitmap", gBitmapMethods,
1422 NELEM(gBitmapMethods));
Chris Craik32054b02014-05-09 13:58:56 -07001423}