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