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