Bitmap.cpp revision 4508218850faedea95371188da587b6734f5f3da
1#define LOG_TAG "Bitmap" 2#include "Bitmap.h" 3 4#include "SkBitmap.h" 5#include "SkPixelRef.h" 6#include "SkImageEncoder.h" 7#include "SkImageInfo.h" 8#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" 19#include <Caches.h> 20#include <hwui/Paint.h> 21#include <renderthread/RenderProxy.h> 22 23#include "core_jni_helpers.h" 24 25#include <jni.h> 26#include <memory> 27#include <string> 28#include <sys/mman.h> 29#include <cutils/ashmem.h> 30 31#define DEBUG_PARCEL 0 32#define ASHMEM_BITMAP_MIN_SIZE (128 * (1 << 10)) 33 34namespace 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 53 void reconfigure(const SkImageInfo& newInfo, size_t rowBytes, SkColorTable* ctable) { 54 if (kIndex_8_SkColorType != newInfo.colorType()) { 55 ctable = nullptr; 56 } 57 mRowBytes = rowBytes; 58 if (mColorTable != ctable) { 59 SkSafeUnref(mColorTable); 60 mColorTable = ctable; 61 SkSafeRef(mColorTable); 62 } 63 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 71 // 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()); 77 *myInfo = newInfo; 78 changeAlphaType(alphaType); 79 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(void* address, size_t size, const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable) 131 : mPixelStorageType(PixelStorageType::Heap) { 132 mPixelStorage.heap.address = address; 133 mPixelStorage.heap.size = size; 134 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable)); 135 // Note: this will trigger a call to onStrongRefDestroyed(), but 136 // we want the pixel ref to have a ref count of 0 at this point 137 mPixelRef->unref(); 138} 139 140Bitmap::Bitmap(void* address, void* context, FreeFunc freeFunc, 141 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable) 142 : mPixelStorageType(PixelStorageType::External) { 143 mPixelStorage.external.address = address; 144 mPixelStorage.external.context = context; 145 mPixelStorage.external.freeFunc = freeFunc; 146 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable)); 147 // Note: this will trigger a call to onStrongRefDestroyed(), but 148 // we want the pixel ref to have a ref count of 0 at this point 149 mPixelRef->unref(); 150} 151 152Bitmap::Bitmap(void* address, int fd, size_t mappedSize, 153 const SkImageInfo& info, size_t rowBytes, SkColorTable* ctable) 154 : mPixelStorageType(PixelStorageType::Ashmem) { 155 mPixelStorage.ashmem.address = address; 156 mPixelStorage.ashmem.fd = fd; 157 mPixelStorage.ashmem.size = mappedSize; 158 mPixelRef.reset(new WrappedPixelRef(this, address, info, rowBytes, ctable)); 159 // Note: this will trigger a call to onStrongRefDestroyed(), but 160 // we want the pixel ref to have a ref count of 0 at this point 161 mPixelRef->unref(); 162} 163Bitmap::~Bitmap() { 164 doFreePixels(); 165} 166 167void Bitmap::freePixels() { 168 AutoMutex _lock(mLock); 169 if (mPinnedRefCount == 0) { 170 doFreePixels(); 171 mPixelStorageType = PixelStorageType::Invalid; 172 } 173} 174 175void Bitmap::doFreePixels() { 176 switch (mPixelStorageType) { 177 case PixelStorageType::Invalid: 178 // already free'd, nothing to do 179 break; 180 case PixelStorageType::External: 181 mPixelStorage.external.freeFunc(mPixelStorage.external.address, 182 mPixelStorage.external.context); 183 break; 184 case PixelStorageType::Ashmem: 185 munmap(mPixelStorage.ashmem.address, mPixelStorage.ashmem.size); 186 close(mPixelStorage.ashmem.fd); 187 break; 188 case PixelStorageType::Heap: 189 free(mPixelStorage.heap.address); 190 break; 191 } 192 193 if (android::uirenderer::Caches::hasInstance()) { 194 android::uirenderer::Caches::getInstance().textureCache.releaseTexture( 195 mPixelRef->getStableID()); 196 } 197} 198 199bool Bitmap::hasHardwareMipMap() { 200 return mPixelRef->hasHardwareMipMap(); 201} 202 203void Bitmap::setHasHardwareMipMap(bool hasMipMap) { 204 mPixelRef->setHasHardwareMipMap(hasMipMap); 205} 206 207int Bitmap::getAshmemFd() const { 208 switch (mPixelStorageType) { 209 case PixelStorageType::Ashmem: 210 return mPixelStorage.ashmem.fd; 211 default: 212 return -1; 213 } 214} 215 216size_t Bitmap::getAllocationByteCount() const { 217 switch (mPixelStorageType) { 218 case PixelStorageType::Heap: 219 return mPixelStorage.heap.size; 220 default: 221 return rowBytes() * height(); 222 } 223} 224 225const SkImageInfo& Bitmap::info() const { 226 return mPixelRef->info(); 227} 228 229size_t Bitmap::rowBytes() const { 230 return mPixelRef->rowBytes(); 231} 232 233SkPixelRef* Bitmap::peekAtPixelRef() const { 234 assertValid(); 235 return mPixelRef.get(); 236} 237 238SkPixelRef* Bitmap::refPixelRef() { 239 assertValid(); 240 android::AutoMutex _lock(mLock); 241 return refPixelRefLocked(); 242} 243 244SkPixelRef* Bitmap::refPixelRefLocked() { 245 mPixelRef->ref(); 246 if (mPixelRef->unique()) { 247 // We just restored this from 0, pin the pixels and inc the strong count 248 // Note that there *might be* an incoming onStrongRefDestroyed from whatever 249 // last unref'd 250 mPinnedRefCount++; 251 } 252 return mPixelRef.get(); 253} 254 255void Bitmap::reconfigure(const SkImageInfo& info, size_t rowBytes, 256 SkColorTable* ctable) { 257 mPixelRef->reconfigure(info, rowBytes, ctable); 258} 259 260void Bitmap::reconfigure(const SkImageInfo& info) { 261 reconfigure(info, info.minRowBytes(), nullptr); 262} 263 264void Bitmap::setAlphaType(SkAlphaType alphaType) { 265 if (!SkColorTypeValidateAlphaType(info().colorType(), alphaType, &alphaType)) { 266 return; 267 } 268 269 mPixelRef->changeAlphaType(alphaType); 270} 271 272void Bitmap::detachFromJava() { 273 bool disposeSelf; 274 { 275 android::AutoMutex _lock(mLock); 276 mAttachedToJava = false; 277 disposeSelf = shouldDisposeSelfLocked(); 278 } 279 if (disposeSelf) { 280 delete this; 281 } 282} 283 284bool Bitmap::shouldDisposeSelfLocked() { 285 return mPinnedRefCount == 0 && !mAttachedToJava; 286} 287 288 289void Bitmap::onStrongRefDestroyed() { 290 bool disposeSelf = false; 291 { 292 android::AutoMutex _lock(mLock); 293 if (mPinnedRefCount > 0) { 294 mPinnedRefCount--; 295 if (mPinnedRefCount == 0) { 296 disposeSelf = shouldDisposeSelfLocked(); 297 } 298 } 299 } 300 if (disposeSelf) { 301 delete this; 302 } 303} 304 305void Bitmap::getSkBitmap(SkBitmap* outBitmap) { 306 assertValid(); 307 android::AutoMutex _lock(mLock); 308 // Safe because mPixelRef is a WrappedPixelRef type, otherwise rowBytes() 309 // would require locking the pixels first. 310 outBitmap->setInfo(mPixelRef->info(), mPixelRef->rowBytes()); 311 outBitmap->setPixelRef(refPixelRefLocked())->unref(); 312 outBitmap->setHasHardwareMipMap(hasHardwareMipMap()); 313} 314 315void Bitmap::assertValid() const { 316 LOG_ALWAYS_FATAL_IF(mPixelStorageType == PixelStorageType::Invalid, 317 "Error, cannot access an invalid/free'd bitmap here!"); 318} 319 320} // namespace android 321 322using namespace android; 323 324// Convenience class that does not take a global ref on the pixels, relying 325// on the caller already having a local JNI ref 326class LocalScopedBitmap { 327public: 328 explicit LocalScopedBitmap(jlong bitmapHandle) 329 : mBitmap(reinterpret_cast<Bitmap*>(bitmapHandle)) {} 330 331 Bitmap* operator->() { 332 return mBitmap; 333 } 334 335 void* pixels() { 336 return mBitmap->peekAtPixelRef()->pixels(); 337 } 338 339 bool valid() { 340 return mBitmap && mBitmap->valid(); 341 } 342 343private: 344 Bitmap* mBitmap; 345}; 346 347/////////////////////////////////////////////////////////////////////////////// 348// Conversions to/from SkColor, for get/setPixels, and the create method, which 349// is basically like setPixels 350 351typedef void (*FromColorProc)(void* dst, const SkColor src[], int width, 352 int x, int y); 353 354static void FromColor_D32(void* dst, const SkColor src[], int width, 355 int, int) { 356 SkPMColor* d = (SkPMColor*)dst; 357 358 for (int i = 0; i < width; i++) { 359 *d++ = SkPreMultiplyColor(*src++); 360 } 361} 362 363static void FromColor_D32_Raw(void* dst, const SkColor src[], int width, 364 int, int) { 365 // Needed to thwart the unreachable code detection from clang. 366 static const bool sk_color_ne_zero = SK_COLOR_MATCHES_PMCOLOR_BYTE_ORDER; 367 368 // SkColor's ordering may be different from SkPMColor 369 if (sk_color_ne_zero) { 370 memcpy(dst, src, width * sizeof(SkColor)); 371 return; 372 } 373 374 // order isn't same, repack each pixel manually 375 SkPMColor* d = (SkPMColor*)dst; 376 for (int i = 0; i < width; i++) { 377 SkColor c = *src++; 378 *d++ = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), 379 SkColorGetG(c), SkColorGetB(c)); 380 } 381} 382 383static void FromColor_D565(void* dst, const SkColor src[], int width, 384 int x, int y) { 385 uint16_t* d = (uint16_t*)dst; 386 387 DITHER_565_SCAN(y); 388 for (int stop = x + width; x < stop; x++) { 389 SkColor c = *src++; 390 *d++ = SkDitherRGBTo565(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c), 391 DITHER_VALUE(x)); 392 } 393} 394 395static void FromColor_D4444(void* dst, const SkColor src[], int width, 396 int x, int y) { 397 SkPMColor16* d = (SkPMColor16*)dst; 398 399 DITHER_4444_SCAN(y); 400 for (int stop = x + width; x < stop; x++) { 401 SkPMColor pmc = SkPreMultiplyColor(*src++); 402 *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x)); 403// *d++ = SkPixel32ToPixel4444(pmc); 404 } 405} 406 407static void FromColor_D4444_Raw(void* dst, const SkColor src[], int width, 408 int x, int y) { 409 SkPMColor16* d = (SkPMColor16*)dst; 410 411 DITHER_4444_SCAN(y); 412 for (int stop = x + width; x < stop; x++) { 413 SkColor c = *src++; 414 415 // SkPMColor is used because the ordering is ARGB32, even though the target actually premultiplied 416 SkPMColor pmc = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), 417 SkColorGetG(c), SkColorGetB(c)); 418 *d++ = SkDitherARGB32To4444(pmc, DITHER_VALUE(x)); 419// *d++ = SkPixel32ToPixel4444(pmc); 420 } 421} 422 423static void FromColor_DA8(void* dst, const SkColor src[], int width, int x, int y) { 424 uint8_t* d = (uint8_t*)dst; 425 426 for (int stop = x + width; x < stop; x++) { 427 *d++ = SkColorGetA(*src++); 428 } 429} 430 431// can return NULL 432static FromColorProc ChooseFromColorProc(const SkBitmap& bitmap) { 433 switch (bitmap.colorType()) { 434 case kN32_SkColorType: 435 return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D32 : FromColor_D32_Raw; 436 case kARGB_4444_SkColorType: 437 return bitmap.alphaType() == kPremul_SkAlphaType ? FromColor_D4444 : 438 FromColor_D4444_Raw; 439 case kRGB_565_SkColorType: 440 return FromColor_D565; 441 case kAlpha_8_SkColorType: 442 return FromColor_DA8; 443 default: 444 break; 445 } 446 return NULL; 447} 448 449bool GraphicsJNI::SetPixels(JNIEnv* env, jintArray srcColors, int srcOffset, int srcStride, 450 int x, int y, int width, int height, const SkBitmap& dstBitmap) { 451 SkAutoLockPixels alp(dstBitmap); 452 void* dst = dstBitmap.getPixels(); 453 FromColorProc proc = ChooseFromColorProc(dstBitmap); 454 455 if (NULL == dst || NULL == proc) { 456 return false; 457 } 458 459 const jint* array = env->GetIntArrayElements(srcColors, NULL); 460 const SkColor* src = (const SkColor*)array + srcOffset; 461 462 // reset to to actual choice from caller 463 dst = dstBitmap.getAddr(x, y); 464 // now copy/convert each scanline 465 for (int y = 0; y < height; y++) { 466 proc(dst, src, width, x, y); 467 src += srcStride; 468 dst = (char*)dst + dstBitmap.rowBytes(); 469 } 470 471 dstBitmap.notifyPixelsChanged(); 472 473 env->ReleaseIntArrayElements(srcColors, const_cast<jint*>(array), 474 JNI_ABORT); 475 return true; 476} 477 478//////////////////// ToColor procs 479 480typedef void (*ToColorProc)(SkColor dst[], const void* src, int width, 481 SkColorTable*); 482 483static void ToColor_S32_Alpha(SkColor dst[], const void* src, int width, 484 SkColorTable*) { 485 SkASSERT(width > 0); 486 const SkPMColor* s = (const SkPMColor*)src; 487 do { 488 *dst++ = SkUnPreMultiply::PMColorToColor(*s++); 489 } while (--width != 0); 490} 491 492static void ToColor_S32_Raw(SkColor dst[], const void* src, int width, 493 SkColorTable*) { 494 SkASSERT(width > 0); 495 const SkPMColor* s = (const SkPMColor*)src; 496 do { 497 SkPMColor c = *s++; 498 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c), 499 SkGetPackedG32(c), SkGetPackedB32(c)); 500 } while (--width != 0); 501} 502 503static void ToColor_S32_Opaque(SkColor dst[], const void* src, int width, 504 SkColorTable*) { 505 SkASSERT(width > 0); 506 const SkPMColor* s = (const SkPMColor*)src; 507 do { 508 SkPMColor c = *s++; 509 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c), 510 SkGetPackedB32(c)); 511 } while (--width != 0); 512} 513 514static void ToColor_S4444_Alpha(SkColor dst[], const void* src, int width, 515 SkColorTable*) { 516 SkASSERT(width > 0); 517 const SkPMColor16* s = (const SkPMColor16*)src; 518 do { 519 *dst++ = SkUnPreMultiply::PMColorToColor(SkPixel4444ToPixel32(*s++)); 520 } while (--width != 0); 521} 522 523static void ToColor_S4444_Raw(SkColor dst[], const void* src, int width, 524 SkColorTable*) { 525 SkASSERT(width > 0); 526 const SkPMColor16* s = (const SkPMColor16*)src; 527 do { 528 SkPMColor c = SkPixel4444ToPixel32(*s++); 529 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c), 530 SkGetPackedG32(c), SkGetPackedB32(c)); 531 } while (--width != 0); 532} 533 534static void ToColor_S4444_Opaque(SkColor dst[], const void* src, int width, 535 SkColorTable*) { 536 SkASSERT(width > 0); 537 const SkPMColor16* s = (const SkPMColor16*)src; 538 do { 539 SkPMColor c = SkPixel4444ToPixel32(*s++); 540 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c), 541 SkGetPackedB32(c)); 542 } while (--width != 0); 543} 544 545static void ToColor_S565(SkColor dst[], const void* src, int width, 546 SkColorTable*) { 547 SkASSERT(width > 0); 548 const uint16_t* s = (const uint16_t*)src; 549 do { 550 uint16_t c = *s++; 551 *dst++ = SkColorSetRGB(SkPacked16ToR32(c), SkPacked16ToG32(c), 552 SkPacked16ToB32(c)); 553 } while (--width != 0); 554} 555 556static void ToColor_SI8_Alpha(SkColor dst[], const void* src, int width, 557 SkColorTable* ctable) { 558 SkASSERT(width > 0); 559 const uint8_t* s = (const uint8_t*)src; 560 const SkPMColor* colors = ctable->readColors(); 561 do { 562 *dst++ = SkUnPreMultiply::PMColorToColor(colors[*s++]); 563 } while (--width != 0); 564} 565 566static void ToColor_SI8_Raw(SkColor dst[], const void* src, int width, 567 SkColorTable* ctable) { 568 SkASSERT(width > 0); 569 const uint8_t* s = (const uint8_t*)src; 570 const SkPMColor* colors = ctable->readColors(); 571 do { 572 SkPMColor c = colors[*s++]; 573 *dst++ = SkColorSetARGB(SkGetPackedA32(c), SkGetPackedR32(c), 574 SkGetPackedG32(c), SkGetPackedB32(c)); 575 } while (--width != 0); 576} 577 578static void ToColor_SI8_Opaque(SkColor dst[], const void* src, int width, 579 SkColorTable* ctable) { 580 SkASSERT(width > 0); 581 const uint8_t* s = (const uint8_t*)src; 582 const SkPMColor* colors = ctable->readColors(); 583 do { 584 SkPMColor c = colors[*s++]; 585 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c), 586 SkGetPackedB32(c)); 587 } while (--width != 0); 588} 589 590static void ToColor_SA8(SkColor dst[], const void* src, int width, SkColorTable*) { 591 SkASSERT(width > 0); 592 const uint8_t* s = (const uint8_t*)src; 593 do { 594 uint8_t c = *s++; 595 *dst++ = SkColorSetARGB(c, c, c, c); 596 } while (--width != 0); 597} 598 599// can return NULL 600static ToColorProc ChooseToColorProc(const SkBitmap& src) { 601 switch (src.colorType()) { 602 case kN32_SkColorType: 603 switch (src.alphaType()) { 604 case kOpaque_SkAlphaType: 605 return ToColor_S32_Opaque; 606 case kPremul_SkAlphaType: 607 return ToColor_S32_Alpha; 608 case kUnpremul_SkAlphaType: 609 return ToColor_S32_Raw; 610 default: 611 return NULL; 612 } 613 case kARGB_4444_SkColorType: 614 switch (src.alphaType()) { 615 case kOpaque_SkAlphaType: 616 return ToColor_S4444_Opaque; 617 case kPremul_SkAlphaType: 618 return ToColor_S4444_Alpha; 619 case kUnpremul_SkAlphaType: 620 return ToColor_S4444_Raw; 621 default: 622 return NULL; 623 } 624 case kRGB_565_SkColorType: 625 return ToColor_S565; 626 case kIndex_8_SkColorType: 627 if (src.getColorTable() == NULL) { 628 return NULL; 629 } 630 switch (src.alphaType()) { 631 case kOpaque_SkAlphaType: 632 return ToColor_SI8_Opaque; 633 case kPremul_SkAlphaType: 634 return ToColor_SI8_Alpha; 635 case kUnpremul_SkAlphaType: 636 return ToColor_SI8_Raw; 637 default: 638 return NULL; 639 } 640 case kAlpha_8_SkColorType: 641 return ToColor_SA8; 642 default: 643 break; 644 } 645 return NULL; 646} 647 648/////////////////////////////////////////////////////////////////////////////// 649/////////////////////////////////////////////////////////////////////////////// 650 651static int getPremulBitmapCreateFlags(bool isMutable) { 652 int flags = GraphicsJNI::kBitmapCreateFlag_Premultiplied; 653 if (isMutable) flags |= GraphicsJNI::kBitmapCreateFlag_Mutable; 654 return flags; 655} 656 657static jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors, 658 jint offset, jint stride, jint width, jint height, 659 jint configHandle, jboolean isMutable) { 660 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle); 661 if (NULL != jColors) { 662 size_t n = env->GetArrayLength(jColors); 663 if (n < SkAbs32(stride) * (size_t)height) { 664 doThrowAIOOBE(env); 665 return NULL; 666 } 667 } 668 669 // ARGB_4444 is a deprecated format, convert automatically to 8888 670 if (colorType == kARGB_4444_SkColorType) { 671 colorType = kN32_SkColorType; 672 } 673 674 SkBitmap bitmap; 675 bitmap.setInfo(SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType)); 676 677 Bitmap* nativeBitmap = GraphicsJNI::allocateHeapPixelRef(&bitmap, NULL); 678 if (!nativeBitmap) { 679 return NULL; 680 } 681 682 if (jColors != NULL) { 683 GraphicsJNI::SetPixels(env, jColors, offset, stride, 684 0, 0, width, height, bitmap); 685 } 686 687 return GraphicsJNI::createBitmap(env, nativeBitmap, 688 getPremulBitmapCreateFlags(isMutable)); 689} 690 691static jobject Bitmap_copy(JNIEnv* env, jobject, jlong srcHandle, 692 jint dstConfigHandle, jboolean isMutable) { 693 SkBitmap src; 694 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src); 695 SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle); 696 SkBitmap result; 697 HeapAllocator allocator; 698 699 if (!src.copyTo(&result, dstCT, &allocator)) { 700 return NULL; 701 } 702 Bitmap* bitmap = allocator.getStorageObjAndReset(); 703 return GraphicsJNI::createBitmap(env, bitmap, 704 getPremulBitmapCreateFlags(isMutable)); 705} 706 707static Bitmap* Bitmap_copyAshmemImpl(JNIEnv* env, SkBitmap& src, SkColorType& dstCT) { 708 SkBitmap result; 709 710 AshmemPixelAllocator allocator(env); 711 if (!src.copyTo(&result, dstCT, &allocator)) { 712 return NULL; 713 } 714 Bitmap* bitmap = allocator.getStorageObjAndReset(); 715 bitmap->peekAtPixelRef()->setImmutable(); 716 return bitmap; 717} 718 719static jobject Bitmap_copyAshmem(JNIEnv* env, jobject, jlong srcHandle) { 720 SkBitmap src; 721 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src); 722 SkColorType dstCT = src.colorType(); 723 Bitmap* bitmap = Bitmap_copyAshmemImpl(env, src, dstCT); 724 jobject ret = GraphicsJNI::createBitmap(env, bitmap, getPremulBitmapCreateFlags(false)); 725 return ret; 726} 727 728static jobject Bitmap_copyAshmemConfig(JNIEnv* env, jobject, jlong srcHandle, jint dstConfigHandle) { 729 SkBitmap src; 730 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src); 731 SkColorType dstCT = GraphicsJNI::legacyBitmapConfigToColorType(dstConfigHandle); 732 Bitmap* bitmap = Bitmap_copyAshmemImpl(env, src, dstCT); 733 jobject ret = GraphicsJNI::createBitmap(env, bitmap, getPremulBitmapCreateFlags(false)); 734 return ret; 735} 736 737static void Bitmap_destruct(Bitmap* bitmap) { 738 bitmap->detachFromJava(); 739} 740 741static jlong Bitmap_getNativeFinalizer(JNIEnv*, jobject) { 742 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&Bitmap_destruct)); 743} 744 745static jboolean Bitmap_recycle(JNIEnv* env, jobject, jlong bitmapHandle) { 746 LocalScopedBitmap bitmap(bitmapHandle); 747 bitmap->freePixels(); 748 return JNI_TRUE; 749} 750 751static void Bitmap_reconfigure(JNIEnv* env, jobject clazz, jlong bitmapHandle, 752 jint width, jint height, jint configHandle, jboolean requestPremul) { 753 LocalScopedBitmap bitmap(bitmapHandle); 754 SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle); 755 756 // ARGB_4444 is a deprecated format, convert automatically to 8888 757 if (colorType == kARGB_4444_SkColorType) { 758 colorType = kN32_SkColorType; 759 } 760 size_t requestedSize = width * height * SkColorTypeBytesPerPixel(colorType); 761 if (requestedSize > bitmap->getAllocationByteCount()) { 762 // done in native as there's no way to get BytesPerPixel in Java 763 doThrowIAE(env, "Bitmap not large enough to support new configuration"); 764 return; 765 } 766 SkAlphaType alphaType; 767 if (bitmap->info().colorType() != kRGB_565_SkColorType 768 && bitmap->info().alphaType() == kOpaque_SkAlphaType) { 769 // If the original bitmap was set to opaque, keep that setting, unless it 770 // was 565, which is required to be opaque. 771 alphaType = kOpaque_SkAlphaType; 772 } else { 773 // Otherwise respect the premultiplied request. 774 alphaType = requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType; 775 } 776 bitmap->reconfigure(SkImageInfo::Make(width, height, colorType, alphaType)); 777} 778 779// These must match the int values in Bitmap.java 780enum JavaEncodeFormat { 781 kJPEG_JavaEncodeFormat = 0, 782 kPNG_JavaEncodeFormat = 1, 783 kWEBP_JavaEncodeFormat = 2 784}; 785 786static jboolean Bitmap_compress(JNIEnv* env, jobject clazz, jlong bitmapHandle, 787 jint format, jint quality, 788 jobject jstream, jbyteArray jstorage) { 789 790 LocalScopedBitmap bitmap(bitmapHandle); 791 SkImageEncoder::Type fm; 792 793 switch (format) { 794 case kJPEG_JavaEncodeFormat: 795 fm = SkImageEncoder::kJPEG_Type; 796 break; 797 case kPNG_JavaEncodeFormat: 798 fm = SkImageEncoder::kPNG_Type; 799 break; 800 case kWEBP_JavaEncodeFormat: 801 fm = SkImageEncoder::kWEBP_Type; 802 break; 803 default: 804 return JNI_FALSE; 805 } 806 807 if (!bitmap.valid()) { 808 return JNI_FALSE; 809 } 810 811 bool success = false; 812 813 std::unique_ptr<SkWStream> strm(CreateJavaOutputStreamAdaptor(env, jstream, jstorage)); 814 if (!strm.get()) { 815 return JNI_FALSE; 816 } 817 818 std::unique_ptr<SkImageEncoder> encoder(SkImageEncoder::Create(fm)); 819 if (encoder.get()) { 820 SkBitmap skbitmap; 821 bitmap->getSkBitmap(&skbitmap); 822 success = encoder->encodeStream(strm.get(), skbitmap, quality); 823 } 824 return success ? JNI_TRUE : JNI_FALSE; 825} 826 827static void Bitmap_erase(JNIEnv* env, jobject, jlong bitmapHandle, jint color) { 828 LocalScopedBitmap bitmap(bitmapHandle); 829 SkBitmap skBitmap; 830 bitmap->getSkBitmap(&skBitmap); 831 skBitmap.eraseColor(color); 832} 833 834static jint Bitmap_rowBytes(JNIEnv* env, jobject, jlong bitmapHandle) { 835 LocalScopedBitmap bitmap(bitmapHandle); 836 return static_cast<jint>(bitmap->rowBytes()); 837} 838 839static jint Bitmap_config(JNIEnv* env, jobject, jlong bitmapHandle) { 840 LocalScopedBitmap bitmap(bitmapHandle); 841 return GraphicsJNI::colorTypeToLegacyBitmapConfig(bitmap->info().colorType()); 842} 843 844static jint Bitmap_getGenerationId(JNIEnv* env, jobject, jlong bitmapHandle) { 845 LocalScopedBitmap bitmap(bitmapHandle); 846 return static_cast<jint>(bitmap->peekAtPixelRef()->getGenerationID()); 847} 848 849static jboolean Bitmap_isPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle) { 850 LocalScopedBitmap bitmap(bitmapHandle); 851 if (bitmap->info().alphaType() == kPremul_SkAlphaType) { 852 return JNI_TRUE; 853 } 854 return JNI_FALSE; 855} 856 857static jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, jlong bitmapHandle) { 858 LocalScopedBitmap bitmap(bitmapHandle); 859 return !bitmap->info().isOpaque() ? JNI_TRUE : JNI_FALSE; 860} 861 862static void Bitmap_setHasAlpha(JNIEnv* env, jobject, jlong bitmapHandle, 863 jboolean hasAlpha, jboolean requestPremul) { 864 LocalScopedBitmap bitmap(bitmapHandle); 865 if (hasAlpha) { 866 bitmap->setAlphaType( 867 requestPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType); 868 } else { 869 bitmap->setAlphaType(kOpaque_SkAlphaType); 870 } 871} 872 873static void Bitmap_setPremultiplied(JNIEnv* env, jobject, jlong bitmapHandle, 874 jboolean isPremul) { 875 LocalScopedBitmap bitmap(bitmapHandle); 876 if (!bitmap->info().isOpaque()) { 877 if (isPremul) { 878 bitmap->setAlphaType(kPremul_SkAlphaType); 879 } else { 880 bitmap->setAlphaType(kUnpremul_SkAlphaType); 881 } 882 } 883} 884 885static jboolean Bitmap_hasMipMap(JNIEnv* env, jobject, jlong bitmapHandle) { 886 LocalScopedBitmap bitmap(bitmapHandle); 887 return bitmap->hasHardwareMipMap() ? JNI_TRUE : JNI_FALSE; 888} 889 890static void Bitmap_setHasMipMap(JNIEnv* env, jobject, jlong bitmapHandle, 891 jboolean hasMipMap) { 892 LocalScopedBitmap bitmap(bitmapHandle); 893 bitmap->setHasHardwareMipMap(hasMipMap); 894} 895 896/////////////////////////////////////////////////////////////////////////////// 897 898static jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) { 899 if (parcel == NULL) { 900 SkDebugf("-------- unparcel parcel is NULL\n"); 901 return NULL; 902 } 903 904 android::Parcel* p = android::parcelForJavaObject(env, parcel); 905 906 const bool isMutable = p->readInt32() != 0; 907 const SkColorType colorType = (SkColorType)p->readInt32(); 908 const SkAlphaType alphaType = (SkAlphaType)p->readInt32(); 909 const int width = p->readInt32(); 910 const int height = p->readInt32(); 911 const int rowBytes = p->readInt32(); 912 const int density = p->readInt32(); 913 914 if (kN32_SkColorType != colorType && 915 kRGB_565_SkColorType != colorType && 916 kARGB_4444_SkColorType != colorType && 917 kIndex_8_SkColorType != colorType && 918 kAlpha_8_SkColorType != colorType) { 919 SkDebugf("Bitmap_createFromParcel unknown colortype: %d\n", colorType); 920 return NULL; 921 } 922 923 std::unique_ptr<SkBitmap> bitmap(new SkBitmap); 924 925 if (!bitmap->setInfo(SkImageInfo::Make(width, height, colorType, alphaType), rowBytes)) { 926 return NULL; 927 } 928 929 SkColorTable* ctable = NULL; 930 if (colorType == kIndex_8_SkColorType) { 931 int count = p->readInt32(); 932 if (count < 0 || count > 256) { 933 // The data is corrupt, since SkColorTable enforces a value between 0 and 256, 934 // inclusive. 935 return NULL; 936 } 937 if (count > 0) { 938 size_t size = count * sizeof(SkPMColor); 939 const SkPMColor* src = (const SkPMColor*)p->readInplace(size); 940 if (src == NULL) { 941 return NULL; 942 } 943 ctable = new SkColorTable(src, count); 944 } 945 } 946 947 // Read the bitmap blob. 948 size_t size = bitmap->getSize(); 949 android::Parcel::ReadableBlob blob; 950 android::status_t status = p->readBlob(size, &blob); 951 if (status) { 952 SkSafeUnref(ctable); 953 doThrowRE(env, "Could not read bitmap blob."); 954 return NULL; 955 } 956 957 // Map the bitmap in place from the ashmem region if possible otherwise copy. 958 Bitmap* nativeBitmap; 959 if (blob.fd() >= 0 && (blob.isMutable() || !isMutable) && (size >= ASHMEM_BITMAP_MIN_SIZE)) { 960#if DEBUG_PARCEL 961 ALOGD("Bitmap.createFromParcel: mapped contents of %s bitmap from %s blob " 962 "(fds %s)", 963 isMutable ? "mutable" : "immutable", 964 blob.isMutable() ? "mutable" : "immutable", 965 p->allowFds() ? "allowed" : "forbidden"); 966#endif 967 // Dup the file descriptor so we can keep a reference to it after the Parcel 968 // is disposed. 969 int dupFd = dup(blob.fd()); 970 if (dupFd < 0) { 971 ALOGE("Error allocating dup fd. Error:%d", errno); 972 blob.release(); 973 SkSafeUnref(ctable); 974 doThrowRE(env, "Could not allocate dup blob fd."); 975 return NULL; 976 } 977 978 // Map the pixels in place and take ownership of the ashmem region. 979 nativeBitmap = GraphicsJNI::mapAshmemPixelRef(env, bitmap.get(), 980 ctable, dupFd, const_cast<void*>(blob.data()), size, !isMutable); 981 SkSafeUnref(ctable); 982 if (!nativeBitmap) { 983 close(dupFd); 984 blob.release(); 985 doThrowRE(env, "Could not allocate ashmem pixel ref."); 986 return NULL; 987 } 988 989 // Clear the blob handle, don't release it. 990 blob.clear(); 991 } else { 992#if DEBUG_PARCEL 993 if (blob.fd() >= 0) { 994 ALOGD("Bitmap.createFromParcel: copied contents of mutable bitmap " 995 "from immutable blob (fds %s)", 996 p->allowFds() ? "allowed" : "forbidden"); 997 } else { 998 ALOGD("Bitmap.createFromParcel: copied contents from %s blob " 999 "(fds %s)", 1000 blob.isMutable() ? "mutable" : "immutable", 1001 p->allowFds() ? "allowed" : "forbidden"); 1002 } 1003#endif 1004 1005 // Copy the pixels into a new buffer. 1006 nativeBitmap = GraphicsJNI::allocateHeapPixelRef(bitmap.get(), ctable); 1007 SkSafeUnref(ctable); 1008 if (!nativeBitmap) { 1009 blob.release(); 1010 doThrowRE(env, "Could not allocate java pixel ref."); 1011 return NULL; 1012 } 1013 bitmap->lockPixels(); 1014 memcpy(bitmap->getPixels(), blob.data(), size); 1015 bitmap->unlockPixels(); 1016 1017 // Release the blob handle. 1018 blob.release(); 1019 } 1020 1021 return GraphicsJNI::createBitmap(env, nativeBitmap, 1022 getPremulBitmapCreateFlags(isMutable), NULL, NULL, density); 1023} 1024 1025static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject, 1026 jlong bitmapHandle, 1027 jboolean isMutable, jint density, 1028 jobject parcel) { 1029 if (parcel == NULL) { 1030 SkDebugf("------- writeToParcel null parcel\n"); 1031 return JNI_FALSE; 1032 } 1033 1034 android::Parcel* p = android::parcelForJavaObject(env, parcel); 1035 SkBitmap bitmap; 1036 1037 android::Bitmap* androidBitmap = reinterpret_cast<Bitmap*>(bitmapHandle); 1038 androidBitmap->getSkBitmap(&bitmap); 1039 1040 p->writeInt32(isMutable); 1041 p->writeInt32(bitmap.colorType()); 1042 p->writeInt32(bitmap.alphaType()); 1043 p->writeInt32(bitmap.width()); 1044 p->writeInt32(bitmap.height()); 1045 p->writeInt32(bitmap.rowBytes()); 1046 p->writeInt32(density); 1047 1048 if (bitmap.colorType() == kIndex_8_SkColorType) { 1049 // The bitmap needs to be locked to access its color table. 1050 SkAutoLockPixels alp(bitmap); 1051 SkColorTable* ctable = bitmap.getColorTable(); 1052 if (ctable != NULL) { 1053 int count = ctable->count(); 1054 p->writeInt32(count); 1055 memcpy(p->writeInplace(count * sizeof(SkPMColor)), 1056 ctable->readColors(), count * sizeof(SkPMColor)); 1057 } else { 1058 p->writeInt32(0); // indicate no ctable 1059 } 1060 } 1061 1062 // Transfer the underlying ashmem region if we have one and it's immutable. 1063 android::status_t status; 1064 int fd = androidBitmap->getAshmemFd(); 1065 if (fd >= 0 && !isMutable && p->allowFds()) { 1066#if DEBUG_PARCEL 1067 ALOGD("Bitmap.writeToParcel: transferring immutable bitmap's ashmem fd as " 1068 "immutable blob (fds %s)", 1069 p->allowFds() ? "allowed" : "forbidden"); 1070#endif 1071 1072 status = p->writeDupImmutableBlobFileDescriptor(fd); 1073 if (status) { 1074 doThrowRE(env, "Could not write bitmap blob file descriptor."); 1075 return JNI_FALSE; 1076 } 1077 return JNI_TRUE; 1078 } 1079 1080 // Copy the bitmap to a new blob. 1081 bool mutableCopy = isMutable; 1082#if DEBUG_PARCEL 1083 ALOGD("Bitmap.writeToParcel: copying %s bitmap into new %s blob (fds %s)", 1084 isMutable ? "mutable" : "immutable", 1085 mutableCopy ? "mutable" : "immutable", 1086 p->allowFds() ? "allowed" : "forbidden"); 1087#endif 1088 1089 size_t size = bitmap.getSize(); 1090 android::Parcel::WritableBlob blob; 1091 status = p->writeBlob(size, mutableCopy, &blob); 1092 if (status) { 1093 doThrowRE(env, "Could not copy bitmap to parcel blob."); 1094 return JNI_FALSE; 1095 } 1096 1097 bitmap.lockPixels(); 1098 const void* pSrc = bitmap.getPixels(); 1099 if (pSrc == NULL) { 1100 memset(blob.data(), 0, size); 1101 } else { 1102 memcpy(blob.data(), pSrc, size); 1103 } 1104 bitmap.unlockPixels(); 1105 1106 blob.release(); 1107 return JNI_TRUE; 1108} 1109 1110static jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz, 1111 jlong srcHandle, jlong paintHandle, 1112 jintArray offsetXY) { 1113 SkBitmap src; 1114 reinterpret_cast<Bitmap*>(srcHandle)->getSkBitmap(&src); 1115 const android::Paint* paint = reinterpret_cast<android::Paint*>(paintHandle); 1116 SkIPoint offset; 1117 SkBitmap dst; 1118 HeapAllocator allocator; 1119 1120 src.extractAlpha(&dst, paint, &allocator, &offset); 1121 // If Skia can't allocate pixels for destination bitmap, it resets 1122 // it, that is set its pixels buffer to NULL, and zero width and height. 1123 if (dst.getPixels() == NULL && src.getPixels() != NULL) { 1124 doThrowOOME(env, "failed to allocate pixels for alpha"); 1125 return NULL; 1126 } 1127 if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) { 1128 int* array = env->GetIntArrayElements(offsetXY, NULL); 1129 array[0] = offset.fX; 1130 array[1] = offset.fY; 1131 env->ReleaseIntArrayElements(offsetXY, array, 0); 1132 } 1133 1134 return GraphicsJNI::createBitmap(env, allocator.getStorageObjAndReset(), 1135 getPremulBitmapCreateFlags(true)); 1136} 1137 1138/////////////////////////////////////////////////////////////////////////////// 1139 1140static jint Bitmap_getPixel(JNIEnv* env, jobject, jlong bitmapHandle, 1141 jint x, jint y) { 1142 SkBitmap bitmap; 1143 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap); 1144 SkAutoLockPixels alp(bitmap); 1145 1146 ToColorProc proc = ChooseToColorProc(bitmap); 1147 if (NULL == proc) { 1148 return 0; 1149 } 1150 const void* src = bitmap.getAddr(x, y); 1151 if (NULL == src) { 1152 return 0; 1153 } 1154 1155 SkColor dst[1]; 1156 proc(dst, src, 1, bitmap.getColorTable()); 1157 return static_cast<jint>(dst[0]); 1158} 1159 1160static void Bitmap_getPixels(JNIEnv* env, jobject, jlong bitmapHandle, 1161 jintArray pixelArray, jint offset, jint stride, 1162 jint x, jint y, jint width, jint height) { 1163 SkBitmap bitmap; 1164 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap); 1165 SkAutoLockPixels alp(bitmap); 1166 1167 ToColorProc proc = ChooseToColorProc(bitmap); 1168 if (NULL == proc) { 1169 return; 1170 } 1171 const void* src = bitmap.getAddr(x, y); 1172 if (NULL == src) { 1173 return; 1174 } 1175 1176 SkColorTable* ctable = bitmap.getColorTable(); 1177 jint* dst = env->GetIntArrayElements(pixelArray, NULL); 1178 SkColor* d = (SkColor*)dst + offset; 1179 while (--height >= 0) { 1180 proc(d, src, width, ctable); 1181 d += stride; 1182 src = (void*)((const char*)src + bitmap.rowBytes()); 1183 } 1184 env->ReleaseIntArrayElements(pixelArray, dst, 0); 1185} 1186 1187/////////////////////////////////////////////////////////////////////////////// 1188 1189static void Bitmap_setPixel(JNIEnv* env, jobject, jlong bitmapHandle, 1190 jint x, jint y, jint colorHandle) { 1191 SkBitmap bitmap; 1192 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap); 1193 SkColor color = static_cast<SkColor>(colorHandle); 1194 SkAutoLockPixels alp(bitmap); 1195 if (NULL == bitmap.getPixels()) { 1196 return; 1197 } 1198 1199 FromColorProc proc = ChooseFromColorProc(bitmap); 1200 if (NULL == proc) { 1201 return; 1202 } 1203 1204 proc(bitmap.getAddr(x, y), &color, 1, x, y); 1205 bitmap.notifyPixelsChanged(); 1206} 1207 1208static void Bitmap_setPixels(JNIEnv* env, jobject, jlong bitmapHandle, 1209 jintArray pixelArray, jint offset, jint stride, 1210 jint x, jint y, jint width, jint height) { 1211 SkBitmap bitmap; 1212 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap); 1213 GraphicsJNI::SetPixels(env, pixelArray, offset, stride, 1214 x, y, width, height, bitmap); 1215} 1216 1217static void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject, 1218 jlong bitmapHandle, jobject jbuffer) { 1219 SkBitmap bitmap; 1220 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap); 1221 SkAutoLockPixels alp(bitmap); 1222 const void* src = bitmap.getPixels(); 1223 1224 if (NULL != src) { 1225 android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE); 1226 1227 // the java side has already checked that buffer is large enough 1228 memcpy(abp.pointer(), src, bitmap.getSize()); 1229 } 1230} 1231 1232static void Bitmap_copyPixelsFromBuffer(JNIEnv* env, jobject, 1233 jlong bitmapHandle, jobject jbuffer) { 1234 SkBitmap bitmap; 1235 reinterpret_cast<Bitmap*>(bitmapHandle)->getSkBitmap(&bitmap); 1236 SkAutoLockPixels alp(bitmap); 1237 void* dst = bitmap.getPixels(); 1238 1239 if (NULL != dst) { 1240 android::AutoBufferPointer abp(env, jbuffer, JNI_FALSE); 1241 // the java side has already checked that buffer is large enough 1242 memcpy(dst, abp.pointer(), bitmap.getSize()); 1243 bitmap.notifyPixelsChanged(); 1244 } 1245} 1246 1247static jboolean Bitmap_sameAs(JNIEnv* env, jobject, jlong bm0Handle, 1248 jlong bm1Handle) { 1249 SkBitmap bm0; 1250 SkBitmap bm1; 1251 reinterpret_cast<Bitmap*>(bm0Handle)->getSkBitmap(&bm0); 1252 reinterpret_cast<Bitmap*>(bm1Handle)->getSkBitmap(&bm1); 1253 if (bm0.width() != bm1.width() || 1254 bm0.height() != bm1.height() || 1255 bm0.colorType() != bm1.colorType()) { 1256 return JNI_FALSE; 1257 } 1258 1259 SkAutoLockPixels alp0(bm0); 1260 SkAutoLockPixels alp1(bm1); 1261 1262 // if we can't load the pixels, return false 1263 if (NULL == bm0.getPixels() || NULL == bm1.getPixels()) { 1264 return JNI_FALSE; 1265 } 1266 1267 if (bm0.colorType() == kIndex_8_SkColorType) { 1268 SkColorTable* ct0 = bm0.getColorTable(); 1269 SkColorTable* ct1 = bm1.getColorTable(); 1270 if (NULL == ct0 || NULL == ct1) { 1271 return JNI_FALSE; 1272 } 1273 if (ct0->count() != ct1->count()) { 1274 return JNI_FALSE; 1275 } 1276 1277 const size_t size = ct0->count() * sizeof(SkPMColor); 1278 if (memcmp(ct0->readColors(), ct1->readColors(), size) != 0) { 1279 return JNI_FALSE; 1280 } 1281 } 1282 1283 // now compare each scanline. We can't do the entire buffer at once, 1284 // since we don't care about the pixel values that might extend beyond 1285 // the width (since the scanline might be larger than the logical width) 1286 const int h = bm0.height(); 1287 const size_t size = bm0.width() * bm0.bytesPerPixel(); 1288 for (int y = 0; y < h; y++) { 1289 // SkBitmap::getAddr(int, int) may return NULL due to unrecognized config 1290 // (ex: kRLE_Index8_Config). This will cause memcmp method to crash. Since bm0 1291 // and bm1 both have pixel data() (have passed NULL == getPixels() check), 1292 // those 2 bitmaps should be valid (only unrecognized), we return JNI_FALSE 1293 // to warn user those 2 unrecognized config bitmaps may be different. 1294 void *bm0Addr = bm0.getAddr(0, y); 1295 void *bm1Addr = bm1.getAddr(0, y); 1296 1297 if(bm0Addr == NULL || bm1Addr == NULL) { 1298 return JNI_FALSE; 1299 } 1300 1301 if (memcmp(bm0Addr, bm1Addr, size) != 0) { 1302 return JNI_FALSE; 1303 } 1304 } 1305 return JNI_TRUE; 1306} 1307 1308static jlong Bitmap_refPixelRef(JNIEnv* env, jobject, jlong bitmapHandle) { 1309 LocalScopedBitmap bitmap(bitmapHandle); 1310 SkPixelRef* pixelRef = bitmap.valid() ? bitmap->peekAtPixelRef() : nullptr; 1311 SkSafeRef(pixelRef); 1312 return reinterpret_cast<jlong>(pixelRef); 1313} 1314 1315static void Bitmap_prepareToDraw(JNIEnv* env, jobject, jlong bitmapPtr) { 1316 LocalScopedBitmap bitmapHandle(bitmapPtr); 1317 if (!bitmapHandle.valid()) return; 1318 SkBitmap bitmap; 1319 bitmapHandle->getSkBitmap(&bitmap); 1320 android::uirenderer::renderthread::RenderProxy::prepareToDraw(bitmap); 1321} 1322 1323static jint Bitmap_getAllocationByteCount(JNIEnv* env, jobject, jlong bitmapPtr) { 1324 LocalScopedBitmap bitmapHandle(bitmapPtr); 1325 return static_cast<jint>(bitmapHandle->getAllocationByteCount()); 1326} 1327 1328/////////////////////////////////////////////////////////////////////////////// 1329 1330static const JNINativeMethod gBitmapMethods[] = { 1331 { "nativeCreate", "([IIIIIIZ)Landroid/graphics/Bitmap;", 1332 (void*)Bitmap_creator }, 1333 { "nativeCopy", "(JIZ)Landroid/graphics/Bitmap;", 1334 (void*)Bitmap_copy }, 1335 { "nativeCopyAshmem", "(J)Landroid/graphics/Bitmap;", 1336 (void*)Bitmap_copyAshmem }, 1337 { "nativeCopyAshmemConfig", "(JI)Landroid/graphics/Bitmap;", 1338 (void*)Bitmap_copyAshmemConfig }, 1339 { "nativeGetNativeFinalizer", "()J", (void*)Bitmap_getNativeFinalizer }, 1340 { "nativeRecycle", "(J)Z", (void*)Bitmap_recycle }, 1341 { "nativeReconfigure", "(JIIIZ)V", (void*)Bitmap_reconfigure }, 1342 { "nativeCompress", "(JIILjava/io/OutputStream;[B)Z", 1343 (void*)Bitmap_compress }, 1344 { "nativeErase", "(JI)V", (void*)Bitmap_erase }, 1345 { "nativeRowBytes", "(J)I", (void*)Bitmap_rowBytes }, 1346 { "nativeConfig", "(J)I", (void*)Bitmap_config }, 1347 { "nativeHasAlpha", "(J)Z", (void*)Bitmap_hasAlpha }, 1348 { "nativeIsPremultiplied", "(J)Z", (void*)Bitmap_isPremultiplied}, 1349 { "nativeSetHasAlpha", "(JZZ)V", (void*)Bitmap_setHasAlpha}, 1350 { "nativeSetPremultiplied", "(JZ)V", (void*)Bitmap_setPremultiplied}, 1351 { "nativeHasMipMap", "(J)Z", (void*)Bitmap_hasMipMap }, 1352 { "nativeSetHasMipMap", "(JZ)V", (void*)Bitmap_setHasMipMap }, 1353 { "nativeCreateFromParcel", 1354 "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;", 1355 (void*)Bitmap_createFromParcel }, 1356 { "nativeWriteToParcel", "(JZILandroid/os/Parcel;)Z", 1357 (void*)Bitmap_writeToParcel }, 1358 { "nativeExtractAlpha", "(JJ[I)Landroid/graphics/Bitmap;", 1359 (void*)Bitmap_extractAlpha }, 1360 { "nativeGenerationId", "(J)I", (void*)Bitmap_getGenerationId }, 1361 { "nativeGetPixel", "(JII)I", (void*)Bitmap_getPixel }, 1362 { "nativeGetPixels", "(J[IIIIIII)V", (void*)Bitmap_getPixels }, 1363 { "nativeSetPixel", "(JIII)V", (void*)Bitmap_setPixel }, 1364 { "nativeSetPixels", "(J[IIIIIII)V", (void*)Bitmap_setPixels }, 1365 { "nativeCopyPixelsToBuffer", "(JLjava/nio/Buffer;)V", 1366 (void*)Bitmap_copyPixelsToBuffer }, 1367 { "nativeCopyPixelsFromBuffer", "(JLjava/nio/Buffer;)V", 1368 (void*)Bitmap_copyPixelsFromBuffer }, 1369 { "nativeSameAs", "(JJ)Z", (void*)Bitmap_sameAs }, 1370 { "nativeRefPixelRef", "(J)J", (void*)Bitmap_refPixelRef }, 1371 { "nativePrepareToDraw", "(J)V", (void*)Bitmap_prepareToDraw }, 1372 { "nativeGetAllocationByteCount", "(J)I", (void*)Bitmap_getAllocationByteCount }, 1373}; 1374 1375int register_android_graphics_Bitmap(JNIEnv* env) 1376{ 1377 return android::RegisterMethodsOrDie(env, "android/graphics/Bitmap", gBitmapMethods, 1378 NELEM(gBitmapMethods)); 1379} 1380