1 #include "SkBitmap.h"
2 #include "SkPixelRef.h"
3 #include "SkImageEncoder.h"
4 #include "SkColorPriv.h"
5 #include "GraphicsJNI.h"
6 #include "SkDither.h"
7 #include "SkUnPreMultiply.h"
8
9 #include <binder/Parcel.h>
10 #include "android_os_Parcel.h"
11 #include "android_util_Binder.h"
12 #include "android_nio_utils.h"
13 #include "CreateJavaOutputStreamAdaptor.h"
14
15 #include <jni.h>
16
17 #include <Caches.h>
18
19 #if 0
20 #define TRACE_BITMAP(code) code
21 #else
22 #define TRACE_BITMAP(code)
23 #endif
24
25 ///////////////////////////////////////////////////////////////////////////////
26 // Conversions to/from SkColor, for get/setPixels, and the create method, which
27 // is basically like setPixels
28
29 typedef void (*FromColorProc)(void* dst, const SkColor src[], int width,
30 int x, int y);
31
FromColor_D32(void * dst,const SkColor src[],int width,int,int)32 static void FromColor_D32(void* dst, const SkColor src[], int width,
33 int, int) {
34 SkPMColor* d = (SkPMColor*)dst;
35
36 for (int i = 0; i < width; i++) {
37 *d++ = SkPreMultiplyColor(*src++);
38 }
39 }
40
FromColor_D565(void * dst,const SkColor src[],int width,int x,int y)41 static void FromColor_D565(void* dst, const SkColor src[], int width,
42 int x, int y) {
43 uint16_t* d = (uint16_t*)dst;
44
45 DITHER_565_SCAN(y);
46 for (int stop = x + width; x < stop; x++) {
47 SkColor c = *src++;
48 *d++ = SkDitherRGBTo565(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c),
49 DITHER_VALUE(x));
50 }
51 }
52
FromColor_D4444(void * dst,const SkColor src[],int width,int x,int y)53 static void FromColor_D4444(void* dst, const SkColor src[], int width,
54 int x, int y) {
55 SkPMColor16* d = (SkPMColor16*)dst;
56
57 DITHER_4444_SCAN(y);
58 for (int stop = x + width; x < stop; x++) {
59 SkPMColor c = SkPreMultiplyColor(*src++);
60 *d++ = SkDitherARGB32To4444(c, DITHER_VALUE(x));
61 // *d++ = SkPixel32ToPixel4444(c);
62 }
63 }
64
65 // can return NULL
ChooseFromColorProc(SkBitmap::Config config)66 static FromColorProc ChooseFromColorProc(SkBitmap::Config config) {
67 switch (config) {
68 case SkBitmap::kARGB_8888_Config:
69 return FromColor_D32;
70 case SkBitmap::kARGB_4444_Config:
71 return FromColor_D4444;
72 case SkBitmap::kRGB_565_Config:
73 return FromColor_D565;
74 default:
75 break;
76 }
77 return NULL;
78 }
79
SetPixels(JNIEnv * env,jintArray srcColors,int srcOffset,int srcStride,int x,int y,int width,int height,const SkBitmap & dstBitmap)80 bool GraphicsJNI::SetPixels(JNIEnv* env, jintArray srcColors,
81 int srcOffset, int srcStride,
82 int x, int y, int width, int height,
83 const SkBitmap& dstBitmap) {
84 SkAutoLockPixels alp(dstBitmap);
85 void* dst = dstBitmap.getPixels();
86 FromColorProc proc = ChooseFromColorProc(dstBitmap.config());
87
88 if (NULL == dst || NULL == proc) {
89 return false;
90 }
91
92 const jint* array = env->GetIntArrayElements(srcColors, NULL);
93 const SkColor* src = (const SkColor*)array + srcOffset;
94
95 // reset to to actual choice from caller
96 dst = dstBitmap.getAddr(x, y);
97 // now copy/convert each scanline
98 for (int y = 0; y < height; y++) {
99 proc(dst, src, width, x, y);
100 src += srcStride;
101 dst = (char*)dst + dstBitmap.rowBytes();
102 }
103
104 dstBitmap.notifyPixelsChanged();
105
106 env->ReleaseIntArrayElements(srcColors, const_cast<jint*>(array),
107 JNI_ABORT);
108 return true;
109 }
110
111 //////////////////// ToColor procs
112
113 typedef void (*ToColorProc)(SkColor dst[], const void* src, int width,
114 SkColorTable*);
115
ToColor_S32_Alpha(SkColor dst[],const void * src,int width,SkColorTable *)116 static void ToColor_S32_Alpha(SkColor dst[], const void* src, int width,
117 SkColorTable*) {
118 SkASSERT(width > 0);
119 const SkPMColor* s = (const SkPMColor*)src;
120 do {
121 *dst++ = SkUnPreMultiply::PMColorToColor(*s++);
122 } while (--width != 0);
123 }
124
ToColor_S32_Opaque(SkColor dst[],const void * src,int width,SkColorTable *)125 static void ToColor_S32_Opaque(SkColor dst[], const void* src, int width,
126 SkColorTable*) {
127 SkASSERT(width > 0);
128 const SkPMColor* s = (const SkPMColor*)src;
129 do {
130 SkPMColor c = *s++;
131 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
132 SkGetPackedB32(c));
133 } while (--width != 0);
134 }
135
ToColor_S4444_Alpha(SkColor dst[],const void * src,int width,SkColorTable *)136 static void ToColor_S4444_Alpha(SkColor dst[], const void* src, int width,
137 SkColorTable*) {
138 SkASSERT(width > 0);
139 const SkPMColor16* s = (const SkPMColor16*)src;
140 do {
141 *dst++ = SkUnPreMultiply::PMColorToColor(SkPixel4444ToPixel32(*s++));
142 } while (--width != 0);
143 }
144
ToColor_S4444_Opaque(SkColor dst[],const void * src,int width,SkColorTable *)145 static void ToColor_S4444_Opaque(SkColor dst[], const void* src, int width,
146 SkColorTable*) {
147 SkASSERT(width > 0);
148 const SkPMColor* s = (const SkPMColor*)src;
149 do {
150 SkPMColor c = SkPixel4444ToPixel32(*s++);
151 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
152 SkGetPackedB32(c));
153 } while (--width != 0);
154 }
155
ToColor_S565(SkColor dst[],const void * src,int width,SkColorTable *)156 static void ToColor_S565(SkColor dst[], const void* src, int width,
157 SkColorTable*) {
158 SkASSERT(width > 0);
159 const uint16_t* s = (const uint16_t*)src;
160 do {
161 uint16_t c = *s++;
162 *dst++ = SkColorSetRGB(SkPacked16ToR32(c), SkPacked16ToG32(c),
163 SkPacked16ToB32(c));
164 } while (--width != 0);
165 }
166
ToColor_SI8_Alpha(SkColor dst[],const void * src,int width,SkColorTable * ctable)167 static void ToColor_SI8_Alpha(SkColor dst[], const void* src, int width,
168 SkColorTable* ctable) {
169 SkASSERT(width > 0);
170 const uint8_t* s = (const uint8_t*)src;
171 const SkPMColor* colors = ctable->lockColors();
172 do {
173 *dst++ = SkUnPreMultiply::PMColorToColor(colors[*s++]);
174 } while (--width != 0);
175 ctable->unlockColors(false);
176 }
177
ToColor_SI8_Opaque(SkColor dst[],const void * src,int width,SkColorTable * ctable)178 static void ToColor_SI8_Opaque(SkColor dst[], const void* src, int width,
179 SkColorTable* ctable) {
180 SkASSERT(width > 0);
181 const uint8_t* s = (const uint8_t*)src;
182 const SkPMColor* colors = ctable->lockColors();
183 do {
184 SkPMColor c = colors[*s++];
185 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
186 SkGetPackedB32(c));
187 } while (--width != 0);
188 ctable->unlockColors(false);
189 }
190
191 // can return NULL
ChooseToColorProc(const SkBitmap & src)192 static ToColorProc ChooseToColorProc(const SkBitmap& src) {
193 switch (src.config()) {
194 case SkBitmap::kARGB_8888_Config:
195 return src.isOpaque() ? ToColor_S32_Opaque : ToColor_S32_Alpha;
196 case SkBitmap::kARGB_4444_Config:
197 return src.isOpaque() ? ToColor_S4444_Opaque : ToColor_S4444_Alpha;
198 case SkBitmap::kRGB_565_Config:
199 return ToColor_S565;
200 case SkBitmap::kIndex8_Config:
201 if (src.getColorTable() == NULL) {
202 return NULL;
203 }
204 return src.isOpaque() ? ToColor_SI8_Opaque : ToColor_SI8_Alpha;
205 default:
206 break;
207 }
208 return NULL;
209 }
210
211 ///////////////////////////////////////////////////////////////////////////////
212 ///////////////////////////////////////////////////////////////////////////////
213
Bitmap_creator(JNIEnv * env,jobject,jintArray jColors,int offset,int stride,int width,int height,SkBitmap::Config config,jboolean isMutable)214 static jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors,
215 int offset, int stride, int width, int height,
216 SkBitmap::Config config, jboolean isMutable) {
217 if (NULL != jColors) {
218 size_t n = env->GetArrayLength(jColors);
219 if (n < SkAbs32(stride) * (size_t)height) {
220 doThrowAIOOBE(env);
221 return NULL;
222 }
223 }
224
225 SkBitmap bitmap;
226
227 bitmap.setConfig(config, width, height);
228
229 jbyteArray buff = GraphicsJNI::allocateJavaPixelRef(env, &bitmap, NULL);
230 if (NULL == buff) {
231 return NULL;
232 }
233
234 if (jColors != NULL) {
235 GraphicsJNI::SetPixels(env, jColors, offset, stride,
236 0, 0, width, height, bitmap);
237 }
238
239 return GraphicsJNI::createBitmap(env, new SkBitmap(bitmap), buff, isMutable, NULL, NULL);
240 }
241
Bitmap_copy(JNIEnv * env,jobject,const SkBitmap * src,SkBitmap::Config dstConfig,jboolean isMutable)242 static jobject Bitmap_copy(JNIEnv* env, jobject, const SkBitmap* src,
243 SkBitmap::Config dstConfig, jboolean isMutable) {
244 SkBitmap result;
245 JavaPixelAllocator allocator(env);
246
247 if (!src->copyTo(&result, dstConfig, &allocator)) {
248 return NULL;
249 }
250
251 return GraphicsJNI::createBitmap(env, new SkBitmap(result), allocator.getStorageObj(), isMutable, NULL, NULL);
252 }
253
Bitmap_destructor(JNIEnv * env,jobject,SkBitmap * bitmap)254 static void Bitmap_destructor(JNIEnv* env, jobject, SkBitmap* bitmap) {
255 #ifdef USE_OPENGL_RENDERER
256 if (android::uirenderer::Caches::hasInstance()) {
257 android::uirenderer::Caches::getInstance().resourceCache.destructor(bitmap);
258 return;
259 }
260 #endif // USE_OPENGL_RENDERER
261 delete bitmap;
262 }
263
Bitmap_recycle(JNIEnv * env,jobject,SkBitmap * bitmap)264 static void Bitmap_recycle(JNIEnv* env, jobject, SkBitmap* bitmap) {
265 #ifdef USE_OPENGL_RENDERER
266 if (android::uirenderer::Caches::hasInstance()) {
267 android::uirenderer::Caches::getInstance().resourceCache.recycle(bitmap);
268 return;
269 }
270 #endif // USE_OPENGL_RENDERER
271 bitmap->setPixels(NULL, NULL);
272 }
273
274 // These must match the int values in Bitmap.java
275 enum JavaEncodeFormat {
276 kJPEG_JavaEncodeFormat = 0,
277 kPNG_JavaEncodeFormat = 1,
278 kWEBP_JavaEncodeFormat = 2
279 };
280
Bitmap_compress(JNIEnv * env,jobject clazz,SkBitmap * bitmap,int format,int quality,jobject jstream,jbyteArray jstorage)281 static bool Bitmap_compress(JNIEnv* env, jobject clazz, SkBitmap* bitmap,
282 int format, int quality,
283 jobject jstream, jbyteArray jstorage) {
284 SkImageEncoder::Type fm;
285
286 switch (format) {
287 case kJPEG_JavaEncodeFormat:
288 fm = SkImageEncoder::kJPEG_Type;
289 break;
290 case kPNG_JavaEncodeFormat:
291 fm = SkImageEncoder::kPNG_Type;
292 break;
293 case kWEBP_JavaEncodeFormat:
294 fm = SkImageEncoder::kWEBP_Type;
295 break;
296 default:
297 return false;
298 }
299
300 bool success = false;
301 SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage);
302 if (NULL != strm) {
303 SkImageEncoder* encoder = SkImageEncoder::Create(fm);
304 if (NULL != encoder) {
305 success = encoder->encodeStream(strm, *bitmap, quality);
306 delete encoder;
307 }
308 delete strm;
309 }
310 return success;
311 }
312
Bitmap_erase(JNIEnv * env,jobject,SkBitmap * bitmap,jint color)313 static void Bitmap_erase(JNIEnv* env, jobject, SkBitmap* bitmap, jint color) {
314 bitmap->eraseColor(color);
315 }
316
Bitmap_width(JNIEnv * env,jobject,SkBitmap * bitmap)317 static int Bitmap_width(JNIEnv* env, jobject, SkBitmap* bitmap) {
318 return bitmap->width();
319 }
320
Bitmap_height(JNIEnv * env,jobject,SkBitmap * bitmap)321 static int Bitmap_height(JNIEnv* env, jobject, SkBitmap* bitmap) {
322 return bitmap->height();
323 }
324
Bitmap_rowBytes(JNIEnv * env,jobject,SkBitmap * bitmap)325 static int Bitmap_rowBytes(JNIEnv* env, jobject, SkBitmap* bitmap) {
326 return bitmap->rowBytes();
327 }
328
Bitmap_config(JNIEnv * env,jobject,SkBitmap * bitmap)329 static int Bitmap_config(JNIEnv* env, jobject, SkBitmap* bitmap) {
330 return bitmap->config();
331 }
332
Bitmap_getGenerationId(JNIEnv * env,jobject,SkBitmap * bitmap)333 static int Bitmap_getGenerationId(JNIEnv* env, jobject, SkBitmap* bitmap) {
334 return bitmap->getGenerationID();
335 }
336
Bitmap_hasAlpha(JNIEnv * env,jobject,SkBitmap * bitmap)337 static jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, SkBitmap* bitmap) {
338 return !bitmap->isOpaque();
339 }
340
Bitmap_setHasAlpha(JNIEnv * env,jobject,SkBitmap * bitmap,jboolean hasAlpha)341 static void Bitmap_setHasAlpha(JNIEnv* env, jobject, SkBitmap* bitmap,
342 jboolean hasAlpha) {
343 bitmap->setIsOpaque(!hasAlpha);
344 }
345
346 ///////////////////////////////////////////////////////////////////////////////
347
Bitmap_createFromParcel(JNIEnv * env,jobject,jobject parcel)348 static jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) {
349 if (parcel == NULL) {
350 SkDebugf("-------- unparcel parcel is NULL\n");
351 return NULL;
352 }
353
354 android::Parcel* p = android::parcelForJavaObject(env, parcel);
355
356 const bool isMutable = p->readInt32() != 0;
357 const SkBitmap::Config config = (SkBitmap::Config)p->readInt32();
358 const int width = p->readInt32();
359 const int height = p->readInt32();
360 const int rowBytes = p->readInt32();
361 const int density = p->readInt32();
362
363 if (SkBitmap::kARGB_8888_Config != config &&
364 SkBitmap::kRGB_565_Config != config &&
365 SkBitmap::kARGB_4444_Config != config &&
366 SkBitmap::kIndex8_Config != config &&
367 SkBitmap::kA8_Config != config) {
368 SkDebugf("Bitmap_createFromParcel unknown config: %d\n", config);
369 return NULL;
370 }
371
372 SkBitmap* bitmap = new SkBitmap;
373
374 bitmap->setConfig(config, width, height, rowBytes);
375
376 SkColorTable* ctable = NULL;
377 if (config == SkBitmap::kIndex8_Config) {
378 int count = p->readInt32();
379 if (count > 0) {
380 size_t size = count * sizeof(SkPMColor);
381 const SkPMColor* src = (const SkPMColor*)p->readInplace(size);
382 ctable = new SkColorTable(src, count);
383 }
384 }
385
386 jbyteArray buffer = GraphicsJNI::allocateJavaPixelRef(env, bitmap, ctable);
387 if (NULL == buffer) {
388 SkSafeUnref(ctable);
389 delete bitmap;
390 return NULL;
391 }
392
393 SkSafeUnref(ctable);
394
395 size_t size = bitmap->getSize();
396
397 android::Parcel::ReadableBlob blob;
398 android::status_t status = p->readBlob(size, &blob);
399 if (status) {
400 doThrowRE(env, "Could not read bitmap from parcel blob.");
401 delete bitmap;
402 return NULL;
403 }
404
405 bitmap->lockPixels();
406 memcpy(bitmap->getPixels(), blob.data(), size);
407 bitmap->unlockPixels();
408
409 blob.release();
410 return GraphicsJNI::createBitmap(env, bitmap, buffer, isMutable, NULL, NULL, density);
411 }
412
Bitmap_writeToParcel(JNIEnv * env,jobject,const SkBitmap * bitmap,jboolean isMutable,jint density,jobject parcel)413 static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject,
414 const SkBitmap* bitmap,
415 jboolean isMutable, jint density,
416 jobject parcel) {
417 if (parcel == NULL) {
418 SkDebugf("------- writeToParcel null parcel\n");
419 return false;
420 }
421
422 android::Parcel* p = android::parcelForJavaObject(env, parcel);
423
424 p->writeInt32(isMutable);
425 p->writeInt32(bitmap->config());
426 p->writeInt32(bitmap->width());
427 p->writeInt32(bitmap->height());
428 p->writeInt32(bitmap->rowBytes());
429 p->writeInt32(density);
430
431 if (bitmap->getConfig() == SkBitmap::kIndex8_Config) {
432 SkColorTable* ctable = bitmap->getColorTable();
433 if (ctable != NULL) {
434 int count = ctable->count();
435 p->writeInt32(count);
436 memcpy(p->writeInplace(count * sizeof(SkPMColor)),
437 ctable->lockColors(), count * sizeof(SkPMColor));
438 ctable->unlockColors(false);
439 } else {
440 p->writeInt32(0); // indicate no ctable
441 }
442 }
443
444 size_t size = bitmap->getSize();
445
446 android::Parcel::WritableBlob blob;
447 android::status_t status = p->writeBlob(size, &blob);
448 if (status) {
449 doThrowRE(env, "Could not write bitmap to parcel blob.");
450 return false;
451 }
452
453 bitmap->lockPixels();
454 const void* pSrc = bitmap->getPixels();
455 if (pSrc == NULL) {
456 memset(blob.data(), 0, size);
457 } else {
458 memcpy(blob.data(), pSrc, size);
459 }
460 bitmap->unlockPixels();
461
462 blob.release();
463 return true;
464 }
465
Bitmap_extractAlpha(JNIEnv * env,jobject clazz,const SkBitmap * src,const SkPaint * paint,jintArray offsetXY)466 static jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz,
467 const SkBitmap* src, const SkPaint* paint,
468 jintArray offsetXY) {
469 SkIPoint offset;
470 SkBitmap* dst = new SkBitmap;
471 JavaPixelAllocator allocator(env);
472
473 src->extractAlpha(dst, paint, &allocator, &offset);
474 // If Skia can't allocate pixels for destination bitmap, it resets
475 // it, that is set its pixels buffer to NULL, and zero width and height.
476 if (dst->getPixels() == NULL && src->getPixels() != NULL) {
477 delete dst;
478 doThrowOOME(env, "failed to allocate pixels for alpha");
479 return NULL;
480 }
481 if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) {
482 int* array = env->GetIntArrayElements(offsetXY, NULL);
483 array[0] = offset.fX;
484 array[1] = offset.fY;
485 env->ReleaseIntArrayElements(offsetXY, array, 0);
486 }
487
488 return GraphicsJNI::createBitmap(env, dst, allocator.getStorageObj(), true, NULL, NULL);
489 }
490
491 ///////////////////////////////////////////////////////////////////////////////
492
Bitmap_getPixel(JNIEnv * env,jobject,const SkBitmap * bitmap,int x,int y)493 static int Bitmap_getPixel(JNIEnv* env, jobject, const SkBitmap* bitmap,
494 int x, int y) {
495 SkAutoLockPixels alp(*bitmap);
496
497 ToColorProc proc = ChooseToColorProc(*bitmap);
498 if (NULL == proc) {
499 return 0;
500 }
501 const void* src = bitmap->getAddr(x, y);
502 if (NULL == src) {
503 return 0;
504 }
505
506 SkColor dst[1];
507 proc(dst, src, 1, bitmap->getColorTable());
508 return dst[0];
509 }
510
Bitmap_getPixels(JNIEnv * env,jobject,const SkBitmap * bitmap,jintArray pixelArray,int offset,int stride,int x,int y,int width,int height)511 static void Bitmap_getPixels(JNIEnv* env, jobject, const SkBitmap* bitmap,
512 jintArray pixelArray, int offset, int stride,
513 int x, int y, int width, int height) {
514 SkAutoLockPixels alp(*bitmap);
515
516 ToColorProc proc = ChooseToColorProc(*bitmap);
517 if (NULL == proc) {
518 return;
519 }
520 const void* src = bitmap->getAddr(x, y);
521 if (NULL == src) {
522 return;
523 }
524
525 SkColorTable* ctable = bitmap->getColorTable();
526 jint* dst = env->GetIntArrayElements(pixelArray, NULL);
527 SkColor* d = (SkColor*)dst + offset;
528 while (--height >= 0) {
529 proc(d, src, width, ctable);
530 d += stride;
531 src = (void*)((const char*)src + bitmap->rowBytes());
532 }
533 env->ReleaseIntArrayElements(pixelArray, dst, 0);
534 }
535
536 ///////////////////////////////////////////////////////////////////////////////
537
Bitmap_setPixel(JNIEnv * env,jobject,const SkBitmap * bitmap,int x,int y,SkColor color)538 static void Bitmap_setPixel(JNIEnv* env, jobject, const SkBitmap* bitmap,
539 int x, int y, SkColor color) {
540 SkAutoLockPixels alp(*bitmap);
541 if (NULL == bitmap->getPixels()) {
542 return;
543 }
544
545 FromColorProc proc = ChooseFromColorProc(bitmap->config());
546 if (NULL == proc) {
547 return;
548 }
549
550 proc(bitmap->getAddr(x, y), &color, 1, x, y);
551 bitmap->notifyPixelsChanged();
552 }
553
Bitmap_setPixels(JNIEnv * env,jobject,const SkBitmap * bitmap,jintArray pixelArray,int offset,int stride,int x,int y,int width,int height)554 static void Bitmap_setPixels(JNIEnv* env, jobject, const SkBitmap* bitmap,
555 jintArray pixelArray, int offset, int stride,
556 int x, int y, int width, int height) {
557 GraphicsJNI::SetPixels(env, pixelArray, offset, stride,
558 x, y, width, height, *bitmap);
559 }
560
Bitmap_copyPixelsToBuffer(JNIEnv * env,jobject,const SkBitmap * bitmap,jobject jbuffer)561 static void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject,
562 const SkBitmap* bitmap, jobject jbuffer) {
563 SkAutoLockPixels alp(*bitmap);
564 const void* src = bitmap->getPixels();
565
566 if (NULL != src) {
567 android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE);
568
569 // the java side has already checked that buffer is large enough
570 memcpy(abp.pointer(), src, bitmap->getSize());
571 }
572 }
573
Bitmap_copyPixelsFromBuffer(JNIEnv * env,jobject,const SkBitmap * bitmap,jobject jbuffer)574 static void Bitmap_copyPixelsFromBuffer(JNIEnv* env, jobject,
575 const SkBitmap* bitmap, jobject jbuffer) {
576 SkAutoLockPixels alp(*bitmap);
577 void* dst = bitmap->getPixels();
578
579 if (NULL != dst) {
580 android::AutoBufferPointer abp(env, jbuffer, JNI_FALSE);
581 // the java side has already checked that buffer is large enough
582 memcpy(dst, abp.pointer(), bitmap->getSize());
583 bitmap->notifyPixelsChanged();
584 }
585 }
586
Bitmap_sameAs(JNIEnv * env,jobject,const SkBitmap * bm0,const SkBitmap * bm1)587 static bool Bitmap_sameAs(JNIEnv* env, jobject, const SkBitmap* bm0,
588 const SkBitmap* bm1) {
589 if (bm0->width() != bm1->width() ||
590 bm0->height() != bm1->height() ||
591 bm0->config() != bm1->config()) {
592 return false;
593 }
594
595 SkAutoLockPixels alp0(*bm0);
596 SkAutoLockPixels alp1(*bm1);
597
598 // if we can't load the pixels, return false
599 if (NULL == bm0->getPixels() || NULL == bm1->getPixels()) {
600 return false;
601 }
602
603 if (bm0->config() == SkBitmap::kIndex8_Config) {
604 SkColorTable* ct0 = bm0->getColorTable();
605 SkColorTable* ct1 = bm1->getColorTable();
606 if (NULL == ct0 || NULL == ct1) {
607 return false;
608 }
609 if (ct0->count() != ct1->count()) {
610 return false;
611 }
612
613 SkAutoLockColors alc0(ct0);
614 SkAutoLockColors alc1(ct1);
615 const size_t size = ct0->count() * sizeof(SkPMColor);
616 if (memcmp(alc0.colors(), alc1.colors(), size) != 0) {
617 return false;
618 }
619 }
620
621 // now compare each scanline. We can't do the entire buffer at once,
622 // since we don't care about the pixel values that might extend beyond
623 // the width (since the scanline might be larger than the logical width)
624 const int h = bm0->height();
625 const size_t size = bm0->width() * bm0->bytesPerPixel();
626 for (int y = 0; y < h; y++) {
627 if (memcmp(bm0->getAddr(0, y), bm1->getAddr(0, y), size) != 0) {
628 return false;
629 }
630 }
631 return true;
632 }
633
Bitmap_prepareToDraw(JNIEnv * env,jobject,SkBitmap * bitmap)634 static void Bitmap_prepareToDraw(JNIEnv* env, jobject, SkBitmap* bitmap) {
635 bitmap->lockPixels();
636 bitmap->unlockPixels();
637 }
638
639 ///////////////////////////////////////////////////////////////////////////////
640
641 #include <android_runtime/AndroidRuntime.h>
642
643 static JNINativeMethod gBitmapMethods[] = {
644 { "nativeCreate", "([IIIIIIZ)Landroid/graphics/Bitmap;",
645 (void*)Bitmap_creator },
646 { "nativeCopy", "(IIZ)Landroid/graphics/Bitmap;",
647 (void*)Bitmap_copy },
648 { "nativeDestructor", "(I)V", (void*)Bitmap_destructor },
649 { "nativeRecycle", "(I)V", (void*)Bitmap_recycle },
650 { "nativeCompress", "(IIILjava/io/OutputStream;[B)Z",
651 (void*)Bitmap_compress },
652 { "nativeErase", "(II)V", (void*)Bitmap_erase },
653 { "nativeWidth", "(I)I", (void*)Bitmap_width },
654 { "nativeHeight", "(I)I", (void*)Bitmap_height },
655 { "nativeRowBytes", "(I)I", (void*)Bitmap_rowBytes },
656 { "nativeConfig", "(I)I", (void*)Bitmap_config },
657 { "nativeHasAlpha", "(I)Z", (void*)Bitmap_hasAlpha },
658 { "nativeSetHasAlpha", "(IZ)V", (void*)Bitmap_setHasAlpha },
659 { "nativeCreateFromParcel",
660 "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;",
661 (void*)Bitmap_createFromParcel },
662 { "nativeWriteToParcel", "(IZILandroid/os/Parcel;)Z",
663 (void*)Bitmap_writeToParcel },
664 { "nativeExtractAlpha", "(II[I)Landroid/graphics/Bitmap;",
665 (void*)Bitmap_extractAlpha },
666 { "nativeGenerationId", "(I)I", (void*)Bitmap_getGenerationId },
667 { "nativeGetPixel", "(III)I", (void*)Bitmap_getPixel },
668 { "nativeGetPixels", "(I[IIIIIII)V", (void*)Bitmap_getPixels },
669 { "nativeSetPixel", "(IIII)V", (void*)Bitmap_setPixel },
670 { "nativeSetPixels", "(I[IIIIIII)V", (void*)Bitmap_setPixels },
671 { "nativeCopyPixelsToBuffer", "(ILjava/nio/Buffer;)V",
672 (void*)Bitmap_copyPixelsToBuffer },
673 { "nativeCopyPixelsFromBuffer", "(ILjava/nio/Buffer;)V",
674 (void*)Bitmap_copyPixelsFromBuffer },
675 { "nativeSameAs", "(II)Z", (void*)Bitmap_sameAs },
676 { "nativePrepareToDraw", "(I)V", (void*)Bitmap_prepareToDraw },
677 };
678
679 #define kClassPathName "android/graphics/Bitmap"
680
register_android_graphics_Bitmap(JNIEnv * env)681 int register_android_graphics_Bitmap(JNIEnv* env)
682 {
683 return android::AndroidRuntime::registerNativeMethods(env, kClassPathName,
684 gBitmapMethods, SK_ARRAY_COUNT(gBitmapMethods));
685 }
686