Bitmap.cpp revision b6377170960d40e66858d8b4d335a95eac773762
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkBitmap.h"
2e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy#include "SkPixelRef.h"
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkImageEncoder.h"
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkColorPriv.h"
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "GraphicsJNI.h"
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkDither.h"
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkUnPreMultiply.h"
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
90795272aa226f4e965968a03daddc53ce30b7cdaMathias Agopian#include <binder/Parcel.h>
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "android_util_Binder.h"
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "android_nio_utils.h"
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "CreateJavaOutputStreamAdaptor.h"
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <jni.h>
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
169aaa8269a3e7291aab84d01c3fc9c744d8f2d2f4Romain Guy#include <Caches.h>
179aaa8269a3e7291aab84d01c3fc9c744d8f2d2f4Romain Guy
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#if 0
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    #define TRACE_BITMAP(code)  code
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#else
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    #define TRACE_BITMAP(code)
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// Conversions to/from SkColor, for get/setPixels, and the create method, which
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// is basically like setPixels
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projecttypedef void (*FromColorProc)(void* dst, const SkColor src[], int width,
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              int x, int y);
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void FromColor_D32(void* dst, const SkColor src[], int width,
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                          int, int) {
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkPMColor* d = (SkPMColor*)dst;
348cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (int i = 0; i < width; i++) {
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *d++ = SkPreMultiplyColor(*src++);
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void FromColor_D565(void* dst, const SkColor src[], int width,
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           int x, int y) {
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    uint16_t* d = (uint16_t*)dst;
438cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    DITHER_565_SCAN(y);
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (int stop = x + width; x < stop; x++) {
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkColor c = *src++;
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *d++ = SkDitherRGBTo565(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c),
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                DITHER_VALUE(x));
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void FromColor_D4444(void* dst, const SkColor src[], int width,
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            int x, int y) {
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkPMColor16* d = (SkPMColor16*)dst;
558cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    DITHER_4444_SCAN(y);
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (int stop = x + width; x < stop; x++) {
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPMColor c = SkPreMultiplyColor(*src++);
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *d++ = SkDitherARGB32To4444(c, DITHER_VALUE(x));
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//        *d++ = SkPixel32ToPixel4444(c);
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// can return NULL
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic FromColorProc ChooseFromColorProc(SkBitmap::Config config) {
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    switch (config) {
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kARGB_8888_Config:
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return FromColor_D32;
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kARGB_4444_Config:
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return FromColor_D4444;
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kRGB_565_Config:
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return FromColor_D565;
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        default:
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NULL;
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectbool GraphicsJNI::SetPixels(JNIEnv* env, jintArray srcColors,
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            int srcOffset, int srcStride,
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            int x, int y, int width, int height,
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            const SkBitmap& dstBitmap) {
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkAutoLockPixels alp(dstBitmap);
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void* dst = dstBitmap.getPixels();
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    FromColorProc proc = ChooseFromColorProc(dstBitmap.config());
868cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL == dst || NULL == proc) {
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
908cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jint* array = env->GetIntArrayElements(srcColors, NULL);
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const SkColor* src = (const SkColor*)array + srcOffset;
938cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // reset to to actual choice from caller
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    dst = dstBitmap.getAddr(x, y);
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // now copy/convert each scanline
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (int y = 0; y < height; y++) {
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        proc(dst, src, width, x, y);
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        src += srcStride;
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        dst = (char*)dst + dstBitmap.rowBytes();
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1028cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
1031a81aea8144b92ebaf807af9de69a0ab3415a5aaRomain Guy    dstBitmap.notifyPixelsChanged();
1041a81aea8144b92ebaf807af9de69a0ab3415a5aaRomain Guy
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->ReleaseIntArrayElements(srcColors, const_cast<jint*>(array),
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                 JNI_ABORT);
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return true;
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//////////////////// ToColor procs
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projecttypedef void (*ToColorProc)(SkColor dst[], const void* src, int width,
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            SkColorTable*);
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void ToColor_S32_Alpha(SkColor dst[], const void* src, int width,
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              SkColorTable*) {
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(width > 0);
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const SkPMColor* s = (const SkPMColor*)src;
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst++ = SkUnPreMultiply::PMColorToColor(*s++);
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (--width != 0);
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void ToColor_S32_Opaque(SkColor dst[], const void* src, int width,
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               SkColorTable*) {
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(width > 0);
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const SkPMColor* s = (const SkPMColor*)src;
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPMColor c = *s++;
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               SkGetPackedB32(c));
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (--width != 0);
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void ToColor_S4444_Alpha(SkColor dst[], const void* src, int width,
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                SkColorTable*) {
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(width > 0);
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const SkPMColor16* s = (const SkPMColor16*)src;
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst++ = SkUnPreMultiply::PMColorToColor(SkPixel4444ToPixel32(*s++));
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (--width != 0);
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void ToColor_S4444_Opaque(SkColor dst[], const void* src, int width,
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                 SkColorTable*) {
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(width > 0);
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const SkPMColor* s = (const SkPMColor*)src;
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPMColor c = SkPixel4444ToPixel32(*s++);
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               SkGetPackedB32(c));
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (--width != 0);
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void ToColor_S565(SkColor dst[], const void* src, int width,
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         SkColorTable*) {
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(width > 0);
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const uint16_t* s = (const uint16_t*)src;
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        uint16_t c = *s++;
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst++ =  SkColorSetRGB(SkPacked16ToR32(c), SkPacked16ToG32(c),
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                SkPacked16ToB32(c));
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (--width != 0);
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void ToColor_SI8_Alpha(SkColor dst[], const void* src, int width,
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              SkColorTable* ctable) {
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(width > 0);
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const uint8_t* s = (const uint8_t*)src;
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const SkPMColor* colors = ctable->lockColors();
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst++ = SkUnPreMultiply::PMColorToColor(colors[*s++]);
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (--width != 0);
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ctable->unlockColors(false);
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void ToColor_SI8_Opaque(SkColor dst[], const void* src, int width,
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               SkColorTable* ctable) {
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(width > 0);
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const uint8_t* s = (const uint8_t*)src;
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const SkPMColor* colors = ctable->lockColors();
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPMColor c = colors[*s++];
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               SkGetPackedB32(c));
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (--width != 0);
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ctable->unlockColors(false);
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// can return NULL
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic ToColorProc ChooseToColorProc(const SkBitmap& src) {
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    switch (src.config()) {
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kARGB_8888_Config:
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return src.isOpaque() ? ToColor_S32_Opaque : ToColor_S32_Alpha;
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kARGB_4444_Config:
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return src.isOpaque() ? ToColor_S4444_Opaque : ToColor_S4444_Alpha;
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kRGB_565_Config:
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return ToColor_S565;
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kIndex8_Config:
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (src.getColorTable() == NULL) {
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return NULL;
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return src.isOpaque() ? ToColor_SI8_Opaque : ToColor_SI8_Alpha;
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        default:
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NULL;
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors,
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              int offset, int stride, int width, int height,
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              SkBitmap::Config config, jboolean isMutable) {
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL != jColors) {
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        size_t n = env->GetArrayLength(jColors);
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (n < SkAbs32(stride) * (size_t)height) {
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            doThrowAIOOBE(env);
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return NULL;
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkBitmap bitmap;
2258cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bitmap.setConfig(config, width, height);
227e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy
228e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    jbyteArray buff = GraphicsJNI::allocateJavaPixelRef(env, &bitmap, NULL);
229e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    if (NULL == buff) {
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (jColors != NULL) {
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::SetPixels(env, jColors, offset, stride,
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               0, 0, width, height, bitmap);
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2378cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
238e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    return GraphicsJNI::createBitmap(env, new SkBitmap(bitmap), buff, isMutable, NULL);
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jobject Bitmap_copy(JNIEnv* env, jobject, const SkBitmap* src,
2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           SkBitmap::Config dstConfig, jboolean isMutable) {
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkBitmap            result;
244e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    JavaPixelAllocator  allocator(env);
2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (!src->copyTo(&result, dstConfig, &allocator)) {
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2498cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
250e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    return GraphicsJNI::createBitmap(env, new SkBitmap(result), allocator.getStorageObj(), isMutable, NULL);
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Bitmap_destructor(JNIEnv* env, jobject, SkBitmap* bitmap) {
2549aaa8269a3e7291aab84d01c3fc9c744d8f2d2f4Romain Guy#ifdef USE_OPENGL_RENDERER
255a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65Romain Guy    if (android::uirenderer::Caches::hasInstance()) {
2565c13d89c1332fcc499379b9064b891187b75ca32Chet Haase        android::uirenderer::Caches::getInstance().resourceCache.destructor(bitmap);
25727f0b17d853d8bef918c3d869044e50cf3904ee3Chet Haase        return;
258a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65Romain Guy    }
25927f0b17d853d8bef918c3d869044e50cf3904ee3Chet Haase#endif // USE_OPENGL_RENDERER
2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    delete bitmap;
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Bitmap_recycle(JNIEnv* env, jobject, SkBitmap* bitmap) {
2645c13d89c1332fcc499379b9064b891187b75ca32Chet Haase#ifdef USE_OPENGL_RENDERER
2655c13d89c1332fcc499379b9064b891187b75ca32Chet Haase    if (android::uirenderer::Caches::hasInstance()) {
2665c13d89c1332fcc499379b9064b891187b75ca32Chet Haase        android::uirenderer::Caches::getInstance().resourceCache.recycle(bitmap);
26727f0b17d853d8bef918c3d869044e50cf3904ee3Chet Haase        return;
2685c13d89c1332fcc499379b9064b891187b75ca32Chet Haase    }
2695c13d89c1332fcc499379b9064b891187b75ca32Chet Haase#endif // USE_OPENGL_RENDERER
27027f0b17d853d8bef918c3d869044e50cf3904ee3Chet Haase    bitmap->setPixels(NULL, NULL);
2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// These must match the int values in Bitmap.java
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectenum JavaEncodeFormat {
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    kJPEG_JavaEncodeFormat = 0,
2762305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora    kPNG_JavaEncodeFormat = 1,
2772305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora    kWEBP_JavaEncodeFormat = 2
2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic bool Bitmap_compress(JNIEnv* env, jobject clazz, SkBitmap* bitmap,
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            int format, int quality,
2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            jobject jstream, jbyteArray jstorage) {
2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkImageEncoder::Type fm;
2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    switch (format) {
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    case kJPEG_JavaEncodeFormat:
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fm = SkImageEncoder::kJPEG_Type;
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        break;
2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    case kPNG_JavaEncodeFormat:
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fm = SkImageEncoder::kPNG_Type;
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        break;
2922305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora    case kWEBP_JavaEncodeFormat:
2932305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora        fm = SkImageEncoder::kWEBP_Type;
2949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        break;
2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    default:
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool success = false;
3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage);
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL != strm) {
3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkImageEncoder* encoder = SkImageEncoder::Create(fm);
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (NULL != encoder) {
3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            success = encoder->encodeStream(strm, *bitmap, quality);
3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            delete encoder;
3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        delete strm;
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return success;
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Bitmap_erase(JNIEnv* env, jobject, SkBitmap* bitmap, jint color) {
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bitmap->eraseColor(color);
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int Bitmap_width(JNIEnv* env, jobject, SkBitmap* bitmap) {
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return bitmap->width();
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int Bitmap_height(JNIEnv* env, jobject, SkBitmap* bitmap) {
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return bitmap->height();
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int Bitmap_rowBytes(JNIEnv* env, jobject, SkBitmap* bitmap) {
3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return bitmap->rowBytes();
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int Bitmap_config(JNIEnv* env, jobject, SkBitmap* bitmap) {
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return bitmap->config();
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3320bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guystatic int Bitmap_getGenerationId(JNIEnv* env, jobject, SkBitmap* bitmap) {
3330bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    return bitmap->getGenerationID();
3340bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy}
3350bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, SkBitmap* bitmap) {
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return !bitmap->isOpaque();
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
340a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reedstatic void Bitmap_setHasAlpha(JNIEnv* env, jobject, SkBitmap* bitmap,
341a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed                               jboolean hasAlpha) {
342a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    bitmap->setIsOpaque(!hasAlpha);
343a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed}
344a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) {
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (parcel == NULL) {
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkDebugf("-------- unparcel parcel is NULL\n");
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3528cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    android::Parcel* p = android::parcelForJavaObject(env, parcel);
3548cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const bool              isMutable = p->readInt32() != 0;
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const SkBitmap::Config  config = (SkBitmap::Config)p->readInt32();
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const int               width = p->readInt32();
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const int               height = p->readInt32();
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const int               rowBytes = p->readInt32();
360de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn    const int               density = p->readInt32();
3618cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (SkBitmap::kARGB_8888_Config != config &&
3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkBitmap::kRGB_565_Config != config &&
3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkBitmap::kARGB_4444_Config != config &&
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkBitmap::kIndex8_Config != config &&
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkBitmap::kA8_Config != config) {
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkDebugf("Bitmap_createFromParcel unknown config: %d\n", config);
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkBitmap* bitmap = new SkBitmap;
3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bitmap->setConfig(config, width, height, rowBytes);
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkColorTable* ctable = NULL;
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (config == SkBitmap::kIndex8_Config) {
3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int count = p->readInt32();
3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (count > 0) {
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            size_t size = count * sizeof(SkPMColor);
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const SkPMColor* src = (const SkPMColor*)p->readInplace(size);
3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            ctable = new SkColorTable(src, count);
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3848cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
385e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    jbyteArray buffer = GraphicsJNI::allocateJavaPixelRef(env, bitmap, ctable);
386e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    if (NULL == buffer) {
3876062c5912dc79704b489fc8c289b78a400ed05eeDerek Sollenberger        SkSafeUnref(ctable);
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        delete bitmap;
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3926062c5912dc79704b489fc8c289b78a400ed05eeDerek Sollenberger    SkSafeUnref(ctable);
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    size_t size = bitmap->getSize();
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bitmap->lockPixels();
396b6377170960d40e66858d8b4d335a95eac773762Bart Sears    memcpy(bitmap->getPixels(), p->readInplace(size), size);
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bitmap->unlockPixels();
3988cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
399e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    return GraphicsJNI::createBitmap(env, bitmap, buffer, isMutable, NULL, density);
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jboolean Bitmap_writeToParcel(JNIEnv* env, jobject,
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                     const SkBitmap* bitmap,
404de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn                                     jboolean isMutable, jint density,
405de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn                                     jobject parcel) {
4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (parcel == NULL) {
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkDebugf("------- writeToParcel null parcel\n");
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    android::Parcel* p = android::parcelForJavaObject(env, parcel);
4128cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    p->writeInt32(isMutable);
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    p->writeInt32(bitmap->config());
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    p->writeInt32(bitmap->width());
4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    p->writeInt32(bitmap->height());
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    p->writeInt32(bitmap->rowBytes());
418de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn    p->writeInt32(density);
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (bitmap->getConfig() == SkBitmap::kIndex8_Config) {
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkColorTable* ctable = bitmap->getColorTable();
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (ctable != NULL) {
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int count = ctable->count();
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            p->writeInt32(count);
4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            memcpy(p->writeInplace(count * sizeof(SkPMColor)),
4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                   ctable->lockColors(), count * sizeof(SkPMColor));
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            ctable->unlockColors(false);
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            p->writeInt32(0);   // indicate no ctable
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    size_t size = bitmap->getSize();
43456c58f66b97d22fe7e7de1f7d9548bcbe1973029Jeff Brown    bitmap->lockPixels();
435b6377170960d40e66858d8b4d335a95eac773762Bart Sears    void* pDst = p->writeInplace(size);
436b6377170960d40e66858d8b4d335a95eac773762Bart Sears
43756c58f66b97d22fe7e7de1f7d9548bcbe1973029Jeff Brown    const void* pSrc =  bitmap->getPixels();
438b6377170960d40e66858d8b4d335a95eac773762Bart Sears
439dee4cb07a3652457e18a0bf4be57d2cedeca7359Jack Palevich    if (pSrc == NULL) {
440b6377170960d40e66858d8b4d335a95eac773762Bart Sears        memset(pDst, 0, size);
441dee4cb07a3652457e18a0bf4be57d2cedeca7359Jack Palevich    } else {
442b6377170960d40e66858d8b4d335a95eac773762Bart Sears        memcpy(pDst, pSrc, size);
443dee4cb07a3652457e18a0bf4be57d2cedeca7359Jack Palevich    }
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bitmap->unlockPixels();
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return true;
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz,
4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                   const SkBitmap* src, const SkPaint* paint,
4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                   jintArray offsetXY) {
4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkIPoint  offset;
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkBitmap* dst = new SkBitmap;
453e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    JavaPixelAllocator allocator(env);
4548cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
455e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    src->extractAlpha(dst, paint, &allocator, &offset);
456ef7be262e5859efdf805d1b9612bc2b250d5971eEric Hassold    // If Skia can't allocate pixels for destination bitmap, it resets
457ef7be262e5859efdf805d1b9612bc2b250d5971eEric Hassold    // it, that is set its pixels buffer to NULL, and zero width and height.
458ef7be262e5859efdf805d1b9612bc2b250d5971eEric Hassold    if (dst->getPixels() == NULL && src->getPixels() != NULL) {
459ef7be262e5859efdf805d1b9612bc2b250d5971eEric Hassold        delete dst;
460ef7be262e5859efdf805d1b9612bc2b250d5971eEric Hassold        doThrowOOME(env, "failed to allocate pixels for alpha");
461ef7be262e5859efdf805d1b9612bc2b250d5971eEric Hassold        return NULL;
462ef7be262e5859efdf805d1b9612bc2b250d5971eEric Hassold    }
4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) {
4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int* array = env->GetIntArrayElements(offsetXY, NULL);
4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        array[0] = offset.fX;
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        array[1] = offset.fY;
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseIntArrayElements(offsetXY, array, 0);
4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4698cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
470e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    return GraphicsJNI::createBitmap(env, dst, allocator.getStorageObj(), true, NULL);
4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int Bitmap_getPixel(JNIEnv* env, jobject, const SkBitmap* bitmap,
4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           int x, int y) {
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkAutoLockPixels alp(*bitmap);
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ToColorProc proc = ChooseToColorProc(*bitmap);
4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL == proc) {
4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const void* src = bitmap->getAddr(x, y);
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL == src) {
4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4878cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkColor dst[1];
4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    proc(dst, src, 1, bitmap->getColorTable());
4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return dst[0];
4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Bitmap_getPixels(JNIEnv* env, jobject, const SkBitmap* bitmap,
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             jintArray pixelArray, int offset, int stride,
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             int x, int y, int width, int height) {
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkAutoLockPixels alp(*bitmap);
4978cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ToColorProc proc = ChooseToColorProc(*bitmap);
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL == proc) {
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const void* src = bitmap->getAddr(x, y);
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL == src) {
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkColorTable* ctable = bitmap->getColorTable();
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jint* dst = env->GetIntArrayElements(pixelArray, NULL);
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkColor* d = (SkColor*)dst + offset;
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    while (--height >= 0) {
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        proc(d, src, width, ctable);
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        d += stride;
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        src = (void*)((const char*)src + bitmap->rowBytes());
5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->ReleaseIntArrayElements(pixelArray, dst, 0);
5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Bitmap_setPixel(JNIEnv* env, jobject, const SkBitmap* bitmap,
5219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            int x, int y, SkColor color) {
5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkAutoLockPixels alp(*bitmap);
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL == bitmap->getPixels()) {
5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    FromColorProc proc = ChooseFromColorProc(bitmap->config());
5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL == proc) {
5299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
5309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    proc(bitmap->getAddr(x, y), &color, 1, x, y);
5331a81aea8144b92ebaf807af9de69a0ab3415a5aaRomain Guy    bitmap->notifyPixelsChanged();
5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Bitmap_setPixels(JNIEnv* env, jobject, const SkBitmap* bitmap,
5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             jintArray pixelArray, int offset, int stride,
5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             int x, int y, int width, int height) {
5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    GraphicsJNI::SetPixels(env, pixelArray, offset, stride,
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           x, y, width, height, *bitmap);
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject,
5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                      const SkBitmap* bitmap, jobject jbuffer) {
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkAutoLockPixels alp(*bitmap);
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const void* src = bitmap->getPixels();
5478cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL != src) {
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE);
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // the java side has already checked that buffer is large enough
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        memcpy(abp.pointer(), src, bitmap->getSize());
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Bitmap_copyPixelsFromBuffer(JNIEnv* env, jobject,
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                    const SkBitmap* bitmap, jobject jbuffer) {
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkAutoLockPixels alp(*bitmap);
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void* dst = bitmap->getPixels();
5608cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL != dst) {
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        android::AutoBufferPointer abp(env, jbuffer, JNI_FALSE);
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // the java side has already checked that buffer is large enough
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        memcpy(dst, abp.pointer(), bitmap->getSize());
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
56876d1e01d5e65c4631c827831e98ad4e300d99eabMike Reedstatic bool Bitmap_sameAs(JNIEnv* env, jobject, const SkBitmap* bm0,
56976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed                             const SkBitmap* bm1) {
57076d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    if (bm0->width() != bm1->width() ||
57176d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        bm0->height() != bm1->height() ||
57276d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        bm0->config() != bm1->config()) {
57376d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        return false;
57476d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    }
57576d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed
57676d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    SkAutoLockPixels alp0(*bm0);
57776d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    SkAutoLockPixels alp1(*bm1);
57876d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed
57976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    // if we can't load the pixels, return false
58076d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    if (NULL == bm0->getPixels() || NULL == bm1->getPixels()) {
58176d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        return false;
58276d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    }
58376d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed
58476d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    if (bm0->config() == SkBitmap::kIndex8_Config) {
58576d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        SkColorTable* ct0 = bm0->getColorTable();
58676d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        SkColorTable* ct1 = bm1->getColorTable();
58776d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        if (NULL == ct0 || NULL == ct1) {
58876d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed            return false;
58976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        }
59076d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        if (ct0->count() != ct1->count()) {
59176d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed            return false;
59276d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        }
59376d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed
59476d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        SkAutoLockColors alc0(ct0);
59576d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        SkAutoLockColors alc1(ct1);
59676d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        const size_t size = ct0->count() * sizeof(SkPMColor);
59776d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        if (memcmp(alc0.colors(), alc1.colors(), size) != 0) {
59876d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed            return false;
59976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        }
60076d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    }
60176d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed
60276d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    // now compare each scanline. We can't do the entire buffer at once,
60376d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    // since we don't care about the pixel values that might extend beyond
60476d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    // the width (since the scanline might be larger than the logical width)
60576d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    const int h = bm0->height();
60676d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    const size_t size = bm0->width() * bm0->bytesPerPixel();
60776d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    for (int y = 0; y < h; y++) {
60876d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        if (memcmp(bm0->getAddr(0, y), bm1->getAddr(0, y), size) != 0) {
60976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed            return false;
61076d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        }
61176d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    }
61276d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    return true;
61376d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed}
61476d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed
6158cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chenstatic void Bitmap_prepareToDraw(JNIEnv* env, jobject, SkBitmap* bitmap) {
6168cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen    bitmap->lockPixels();
6178cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen    bitmap->unlockPixels();
6188cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen}
6198cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
6209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
6219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <android_runtime/AndroidRuntime.h>
6239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JNINativeMethod gBitmapMethods[] = {
6259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeCreate",             "([IIIIIIZ)Landroid/graphics/Bitmap;",
6269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void*)Bitmap_creator },
6279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeCopy",               "(IIZ)Landroid/graphics/Bitmap;",
6289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void*)Bitmap_copy },
6299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeDestructor",         "(I)V", (void*)Bitmap_destructor },
6309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeRecycle",            "(I)V", (void*)Bitmap_recycle },
6319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeCompress",           "(IIILjava/io/OutputStream;[B)Z",
6329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void*)Bitmap_compress },
6339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeErase",              "(II)V", (void*)Bitmap_erase },
6349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeWidth",              "(I)I", (void*)Bitmap_width },
6359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeHeight",             "(I)I", (void*)Bitmap_height },
6369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeRowBytes",           "(I)I", (void*)Bitmap_rowBytes },
6379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeConfig",             "(I)I", (void*)Bitmap_config },
6389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeHasAlpha",           "(I)Z", (void*)Bitmap_hasAlpha },
639a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    {   "nativeSetHasAlpha",        "(IZ)V", (void*)Bitmap_setHasAlpha },
6409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeCreateFromParcel",
6419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;",
6429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void*)Bitmap_createFromParcel },
643de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn    {   "nativeWriteToParcel",      "(IZILandroid/os/Parcel;)Z",
6449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void*)Bitmap_writeToParcel },
6459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeExtractAlpha",       "(II[I)Landroid/graphics/Bitmap;",
6469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void*)Bitmap_extractAlpha },
6470bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    {   "nativeGenerationId",       "(I)I", (void*)Bitmap_getGenerationId },
6489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeGetPixel",           "(III)I", (void*)Bitmap_getPixel },
6499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeGetPixels",          "(I[IIIIIII)V", (void*)Bitmap_getPixels },
6509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeSetPixel",           "(IIII)V", (void*)Bitmap_setPixel },
6519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeSetPixels",          "(I[IIIIIII)V", (void*)Bitmap_setPixels },
6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeCopyPixelsToBuffer", "(ILjava/nio/Buffer;)V",
6539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            (void*)Bitmap_copyPixelsToBuffer },
6549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeCopyPixelsFromBuffer", "(ILjava/nio/Buffer;)V",
6558cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen                                            (void*)Bitmap_copyPixelsFromBuffer },
65676d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    {   "nativeSameAs",             "(II)Z", (void*)Bitmap_sameAs },
65776d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    {   "nativePrepareToDraw",      "(I)V", (void*)Bitmap_prepareToDraw },
6589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define kClassPathName  "android/graphics/Bitmap"
6619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint register_android_graphics_Bitmap(JNIEnv* env)
6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
6649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return android::AndroidRuntime::registerNativeMethods(env, kClassPathName,
6659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                gBitmapMethods, SK_ARRAY_COUNT(gBitmapMethods));
6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
667