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>
10713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy#include "android_os_Parcel.h"
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "android_util_Binder.h"
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "android_nio_utils.h"
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "CreateJavaOutputStreamAdaptor.h"
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <jni.h>
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179aaa8269a3e7291aab84d01c3fc9c744d8f2d2f4Romain Guy#include <Caches.h>
189aaa8269a3e7291aab84d01c3fc9c744d8f2d2f4Romain Guy
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#if 0
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    #define TRACE_BITMAP(code)  code
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#else
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    #define TRACE_BITMAP(code)
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// Conversions to/from SkColor, for get/setPixels, and the create method, which
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// is basically like setPixels
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projecttypedef void (*FromColorProc)(void* dst, const SkColor src[], int width,
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              int x, int y);
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void FromColor_D32(void* dst, const SkColor src[], int width,
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                          int, int) {
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkPMColor* d = (SkPMColor*)dst;
358cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (int i = 0; i < width; i++) {
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *d++ = SkPreMultiplyColor(*src++);
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void FromColor_D565(void* dst, const SkColor src[], int width,
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           int x, int y) {
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    uint16_t* d = (uint16_t*)dst;
448cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    DITHER_565_SCAN(y);
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (int stop = x + width; x < stop; x++) {
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkColor c = *src++;
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *d++ = SkDitherRGBTo565(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c),
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                DITHER_VALUE(x));
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void FromColor_D4444(void* dst, const SkColor src[], int width,
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            int x, int y) {
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkPMColor16* d = (SkPMColor16*)dst;
568cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    DITHER_4444_SCAN(y);
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (int stop = x + width; x < stop; x++) {
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPMColor c = SkPreMultiplyColor(*src++);
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *d++ = SkDitherARGB32To4444(c, DITHER_VALUE(x));
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//        *d++ = SkPixel32ToPixel4444(c);
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// can return NULL
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic FromColorProc ChooseFromColorProc(SkBitmap::Config config) {
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    switch (config) {
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kARGB_8888_Config:
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return FromColor_D32;
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kARGB_4444_Config:
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return FromColor_D4444;
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kRGB_565_Config:
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return FromColor_D565;
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        default:
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NULL;
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectbool GraphicsJNI::SetPixels(JNIEnv* env, jintArray srcColors,
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            int srcOffset, int srcStride,
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            int x, int y, int width, int height,
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            const SkBitmap& dstBitmap) {
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkAutoLockPixels alp(dstBitmap);
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void* dst = dstBitmap.getPixels();
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    FromColorProc proc = ChooseFromColorProc(dstBitmap.config());
878cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL == dst || NULL == proc) {
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
918cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const jint* array = env->GetIntArrayElements(srcColors, NULL);
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const SkColor* src = (const SkColor*)array + srcOffset;
948cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // reset to to actual choice from caller
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    dst = dstBitmap.getAddr(x, y);
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // now copy/convert each scanline
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (int y = 0; y < height; y++) {
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        proc(dst, src, width, x, y);
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        src += srcStride;
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        dst = (char*)dst + dstBitmap.rowBytes();
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1038cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
1041a81aea8144b92ebaf807af9de69a0ab3415a5aaRomain Guy    dstBitmap.notifyPixelsChanged();
1051a81aea8144b92ebaf807af9de69a0ab3415a5aaRomain Guy
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->ReleaseIntArrayElements(srcColors, const_cast<jint*>(array),
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                 JNI_ABORT);
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return true;
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//////////////////// ToColor procs
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projecttypedef void (*ToColorProc)(SkColor dst[], const void* src, int width,
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            SkColorTable*);
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void ToColor_S32_Alpha(SkColor dst[], const void* src, int width,
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              SkColorTable*) {
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(width > 0);
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const SkPMColor* s = (const SkPMColor*)src;
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst++ = SkUnPreMultiply::PMColorToColor(*s++);
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (--width != 0);
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void ToColor_S32_Opaque(SkColor dst[], const void* src, int width,
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               SkColorTable*) {
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(width > 0);
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const SkPMColor* s = (const SkPMColor*)src;
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPMColor c = *s++;
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               SkGetPackedB32(c));
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (--width != 0);
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void ToColor_S4444_Alpha(SkColor dst[], const void* src, int width,
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                SkColorTable*) {
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(width > 0);
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const SkPMColor16* s = (const SkPMColor16*)src;
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst++ = SkUnPreMultiply::PMColorToColor(SkPixel4444ToPixel32(*s++));
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (--width != 0);
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void ToColor_S4444_Opaque(SkColor dst[], const void* src, int width,
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                 SkColorTable*) {
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(width > 0);
148038953da05c641f6551134f710ac0e4f46b624b7Keun young Park    const SkPMColor16* s = (const SkPMColor16*)src;
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPMColor c = SkPixel4444ToPixel32(*s++);
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               SkGetPackedB32(c));
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (--width != 0);
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void ToColor_S565(SkColor dst[], const void* src, int width,
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         SkColorTable*) {
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(width > 0);
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const uint16_t* s = (const uint16_t*)src;
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        uint16_t c = *s++;
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst++ =  SkColorSetRGB(SkPacked16ToR32(c), SkPacked16ToG32(c),
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                SkPacked16ToB32(c));
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (--width != 0);
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void ToColor_SI8_Alpha(SkColor dst[], const void* src, int width,
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              SkColorTable* ctable) {
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(width > 0);
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const uint8_t* s = (const uint8_t*)src;
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const SkPMColor* colors = ctable->lockColors();
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst++ = SkUnPreMultiply::PMColorToColor(colors[*s++]);
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (--width != 0);
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ctable->unlockColors(false);
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void ToColor_SI8_Opaque(SkColor dst[], const void* src, int width,
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               SkColorTable* ctable) {
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkASSERT(width > 0);
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const uint8_t* s = (const uint8_t*)src;
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const SkPMColor* colors = ctable->lockColors();
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPMColor c = colors[*s++];
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               SkGetPackedB32(c));
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (--width != 0);
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ctable->unlockColors(false);
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// can return NULL
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic ToColorProc ChooseToColorProc(const SkBitmap& src) {
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    switch (src.config()) {
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kARGB_8888_Config:
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return src.isOpaque() ? ToColor_S32_Opaque : ToColor_S32_Alpha;
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kARGB_4444_Config:
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return src.isOpaque() ? ToColor_S4444_Opaque : ToColor_S4444_Alpha;
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kRGB_565_Config:
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return ToColor_S565;
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kIndex8_Config:
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (src.getColorTable() == NULL) {
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return NULL;
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return src.isOpaque() ? ToColor_SI8_Opaque : ToColor_SI8_Alpha;
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        default:
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NULL;
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors,
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              int offset, int stride, int width, int height,
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              SkBitmap::Config config, jboolean isMutable) {
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL != jColors) {
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        size_t n = env->GetArrayLength(jColors);
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (n < SkAbs32(stride) * (size_t)height) {
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            doThrowAIOOBE(env);
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return NULL;
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkBitmap bitmap;
2268cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bitmap.setConfig(config, width, height);
228e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy
229e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    jbyteArray buff = GraphicsJNI::allocateJavaPixelRef(env, &bitmap, NULL);
230e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    if (NULL == buff) {
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (jColors != NULL) {
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::SetPixels(env, jColors, offset, stride,
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               0, 0, width, height, bitmap);
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2388cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
239ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani    return GraphicsJNI::createBitmap(env, new SkBitmap(bitmap), buff, isMutable, NULL, NULL);
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jobject Bitmap_copy(JNIEnv* env, jobject, const SkBitmap* src,
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           SkBitmap::Config dstConfig, jboolean isMutable) {
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkBitmap            result;
245e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    JavaPixelAllocator  allocator(env);
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (!src->copyTo(&result, dstConfig, &allocator)) {
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2508cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
251ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani    return GraphicsJNI::createBitmap(env, new SkBitmap(result), allocator.getStorageObj(), isMutable, NULL, NULL);
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Bitmap_destructor(JNIEnv* env, jobject, SkBitmap* bitmap) {
2559aaa8269a3e7291aab84d01c3fc9c744d8f2d2f4Romain Guy#ifdef USE_OPENGL_RENDERER
256a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65Romain Guy    if (android::uirenderer::Caches::hasInstance()) {
2575c13d89c1332fcc499379b9064b891187b75ca32Chet Haase        android::uirenderer::Caches::getInstance().resourceCache.destructor(bitmap);
25827f0b17d853d8bef918c3d869044e50cf3904ee3Chet Haase        return;
259a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65Romain Guy    }
26027f0b17d853d8bef918c3d869044e50cf3904ee3Chet Haase#endif // USE_OPENGL_RENDERER
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    delete bitmap;
2629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
264547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haasestatic jboolean Bitmap_recycle(JNIEnv* env, jobject, SkBitmap* bitmap) {
2655c13d89c1332fcc499379b9064b891187b75ca32Chet Haase#ifdef USE_OPENGL_RENDERER
2665c13d89c1332fcc499379b9064b891187b75ca32Chet Haase    if (android::uirenderer::Caches::hasInstance()) {
267547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase        return android::uirenderer::Caches::getInstance().resourceCache.recycle(bitmap);
2685c13d89c1332fcc499379b9064b891187b75ca32Chet Haase    }
2695c13d89c1332fcc499379b9064b891187b75ca32Chet Haase#endif // USE_OPENGL_RENDERER
27027f0b17d853d8bef918c3d869044e50cf3904ee3Chet Haase    bitmap->setPixels(NULL, NULL);
271547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase    return true;
2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// These must match the int values in Bitmap.java
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectenum JavaEncodeFormat {
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    kJPEG_JavaEncodeFormat = 0,
2772305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora    kPNG_JavaEncodeFormat = 1,
2782305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora    kWEBP_JavaEncodeFormat = 2
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic bool Bitmap_compress(JNIEnv* env, jobject clazz, SkBitmap* bitmap,
2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            int format, int quality,
2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            jobject jstream, jbyteArray jstorage) {
2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkImageEncoder::Type fm;
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    switch (format) {
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    case kJPEG_JavaEncodeFormat:
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fm = SkImageEncoder::kJPEG_Type;
2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        break;
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    case kPNG_JavaEncodeFormat:
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fm = SkImageEncoder::kPNG_Type;
2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        break;
2932305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora    case kWEBP_JavaEncodeFormat:
2942305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora        fm = SkImageEncoder::kWEBP_Type;
2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        break;
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    default:
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bool success = false;
30135ef567140e42f354be4a98cce6a7666ac085c13Michal Stawinski    if (NULL != bitmap) {
30235ef567140e42f354be4a98cce6a7666ac085c13Michal Stawinski        SkAutoLockPixels alp(*bitmap);
30335ef567140e42f354be4a98cce6a7666ac085c13Michal Stawinski
30435ef567140e42f354be4a98cce6a7666ac085c13Michal Stawinski        if (NULL == bitmap->getPixels()) {
30535ef567140e42f354be4a98cce6a7666ac085c13Michal Stawinski            return false;
30635ef567140e42f354be4a98cce6a7666ac085c13Michal Stawinski        }
30735ef567140e42f354be4a98cce6a7666ac085c13Michal Stawinski
30835ef567140e42f354be4a98cce6a7666ac085c13Michal Stawinski        SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage);
30935ef567140e42f354be4a98cce6a7666ac085c13Michal Stawinski        if (NULL == strm) {
31035ef567140e42f354be4a98cce6a7666ac085c13Michal Stawinski            return false;
31135ef567140e42f354be4a98cce6a7666ac085c13Michal Stawinski        }
31235ef567140e42f354be4a98cce6a7666ac085c13Michal Stawinski
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkImageEncoder* encoder = SkImageEncoder::Create(fm);
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (NULL != encoder) {
3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            success = encoder->encodeStream(strm, *bitmap, quality);
3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            delete encoder;
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        delete strm;
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return success;
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Bitmap_erase(JNIEnv* env, jobject, SkBitmap* bitmap, jint color) {
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bitmap->eraseColor(color);
3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int Bitmap_width(JNIEnv* env, jobject, SkBitmap* bitmap) {
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return bitmap->width();
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int Bitmap_height(JNIEnv* env, jobject, SkBitmap* bitmap) {
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return bitmap->height();
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int Bitmap_rowBytes(JNIEnv* env, jobject, SkBitmap* bitmap) {
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return bitmap->rowBytes();
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int Bitmap_config(JNIEnv* env, jobject, SkBitmap* bitmap) {
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return bitmap->config();
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3430bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guystatic int Bitmap_getGenerationId(JNIEnv* env, jobject, SkBitmap* bitmap) {
3440bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    return bitmap->getGenerationID();
3450bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy}
3460bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, SkBitmap* bitmap) {
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return !bitmap->isOpaque();
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
351a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reedstatic void Bitmap_setHasAlpha(JNIEnv* env, jobject, SkBitmap* bitmap,
352a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed                               jboolean hasAlpha) {
353a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    bitmap->setIsOpaque(!hasAlpha);
354a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed}
355a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed
356713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guystatic jboolean Bitmap_hasMipMap(JNIEnv* env, jobject, SkBitmap* bitmap) {
357713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    return bitmap->hasHardwareMipMap();
358713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy}
359713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy
360713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guystatic void Bitmap_setHasMipMap(JNIEnv* env, jobject, SkBitmap* bitmap,
361713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy                                jboolean hasMipMap) {
362713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    bitmap->setHasHardwareMipMap(hasMipMap);
363713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy}
364713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) {
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (parcel == NULL) {
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkDebugf("-------- unparcel parcel is NULL\n");
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3728cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    android::Parcel* p = android::parcelForJavaObject(env, parcel);
3748cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const bool              isMutable = p->readInt32() != 0;
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const SkBitmap::Config  config = (SkBitmap::Config)p->readInt32();
3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const int               width = p->readInt32();
3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const int               height = p->readInt32();
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const int               rowBytes = p->readInt32();
380de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn    const int               density = p->readInt32();
3818cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (SkBitmap::kARGB_8888_Config != config &&
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkBitmap::kRGB_565_Config != config &&
3849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkBitmap::kARGB_4444_Config != config &&
3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkBitmap::kIndex8_Config != config &&
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkBitmap::kA8_Config != config) {
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkDebugf("Bitmap_createFromParcel unknown config: %d\n", config);
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkBitmap* bitmap = new SkBitmap;
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bitmap->setConfig(config, width, height, rowBytes);
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkColorTable* ctable = NULL;
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (config == SkBitmap::kIndex8_Config) {
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int count = p->readInt32();
3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (count > 0) {
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            size_t size = count * sizeof(SkPMColor);
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const SkPMColor* src = (const SkPMColor*)p->readInplace(size);
4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            ctable = new SkColorTable(src, count);
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4048cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
405e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    jbyteArray buffer = GraphicsJNI::allocateJavaPixelRef(env, bitmap, ctable);
406e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    if (NULL == buffer) {
4076062c5912dc79704b489fc8c289b78a400ed05eeDerek Sollenberger        SkSafeUnref(ctable);
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        delete bitmap;
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4126062c5912dc79704b489fc8c289b78a400ed05eeDerek Sollenberger    SkSafeUnref(ctable);
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    size_t size = bitmap->getSize();
4156579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown
4166579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown    android::Parcel::ReadableBlob blob;
4176579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown    android::status_t status = p->readBlob(size, &blob);
4186579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown    if (status) {
4196579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown        doThrowRE(env, "Could not read bitmap from parcel blob.");
4206579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown        delete bitmap;
4216579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown        return NULL;
4226579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown    }
4236579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bitmap->lockPixels();
4256579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown    memcpy(bitmap->getPixels(), blob.data(), size);
4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bitmap->unlockPixels();
4278cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
4286579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown    blob.release();
429ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani    return GraphicsJNI::createBitmap(env, bitmap, buffer, isMutable, NULL, NULL, density);
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jboolean Bitmap_writeToParcel(JNIEnv* env, jobject,
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                     const SkBitmap* bitmap,
434de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn                                     jboolean isMutable, jint density,
435de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn                                     jobject parcel) {
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (parcel == NULL) {
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkDebugf("------- writeToParcel null parcel\n");
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    android::Parcel* p = android::parcelForJavaObject(env, parcel);
4428cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    p->writeInt32(isMutable);
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    p->writeInt32(bitmap->config());
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    p->writeInt32(bitmap->width());
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    p->writeInt32(bitmap->height());
4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    p->writeInt32(bitmap->rowBytes());
448de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn    p->writeInt32(density);
4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (bitmap->getConfig() == SkBitmap::kIndex8_Config) {
4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkColorTable* ctable = bitmap->getColorTable();
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (ctable != NULL) {
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int count = ctable->count();
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            p->writeInt32(count);
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            memcpy(p->writeInplace(count * sizeof(SkPMColor)),
4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                   ctable->lockColors(), count * sizeof(SkPMColor));
4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            ctable->unlockColors(false);
4589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            p->writeInt32(0);   // indicate no ctable
4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    size_t size = bitmap->getSize();
464b6377170960d40e66858d8b4d335a95eac773762Bart Sears
4656579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown    android::Parcel::WritableBlob blob;
4666579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown    android::status_t status = p->writeBlob(size, &blob);
4676579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown    if (status) {
4686579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown        doThrowRE(env, "Could not write bitmap to parcel blob.");
4696579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown        return false;
4706579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown    }
471b6377170960d40e66858d8b4d335a95eac773762Bart Sears
4726579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown    bitmap->lockPixels();
4736579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown    const void* pSrc =  bitmap->getPixels();
474dee4cb07a3652457e18a0bf4be57d2cedeca7359Jack Palevich    if (pSrc == NULL) {
4756579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown        memset(blob.data(), 0, size);
476dee4cb07a3652457e18a0bf4be57d2cedeca7359Jack Palevich    } else {
4776579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown        memcpy(blob.data(), pSrc, size);
478dee4cb07a3652457e18a0bf4be57d2cedeca7359Jack Palevich    }
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bitmap->unlockPixels();
4806579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown
4816579a9d6fe2302fa149452f66c4062ebc60c2523Jeff Brown    blob.release();
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return true;
4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz,
4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                   const SkBitmap* src, const SkPaint* paint,
4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                   jintArray offsetXY) {
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkIPoint  offset;
4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkBitmap* dst = new SkBitmap;
490e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    JavaPixelAllocator allocator(env);
4918cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
492e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    src->extractAlpha(dst, paint, &allocator, &offset);
493ef7be262e5859efdf805d1b9612bc2b250d5971eEric Hassold    // If Skia can't allocate pixels for destination bitmap, it resets
494ef7be262e5859efdf805d1b9612bc2b250d5971eEric Hassold    // it, that is set its pixels buffer to NULL, and zero width and height.
495ef7be262e5859efdf805d1b9612bc2b250d5971eEric Hassold    if (dst->getPixels() == NULL && src->getPixels() != NULL) {
496ef7be262e5859efdf805d1b9612bc2b250d5971eEric Hassold        delete dst;
497ef7be262e5859efdf805d1b9612bc2b250d5971eEric Hassold        doThrowOOME(env, "failed to allocate pixels for alpha");
498ef7be262e5859efdf805d1b9612bc2b250d5971eEric Hassold        return NULL;
499ef7be262e5859efdf805d1b9612bc2b250d5971eEric Hassold    }
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) {
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int* array = env->GetIntArrayElements(offsetXY, NULL);
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        array[0] = offset.fX;
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        array[1] = offset.fY;
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ReleaseIntArrayElements(offsetXY, array, 0);
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5068cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
507ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani    return GraphicsJNI::createBitmap(env, dst, allocator.getStorageObj(), true, NULL, NULL);
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic int Bitmap_getPixel(JNIEnv* env, jobject, const SkBitmap* bitmap,
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           int x, int y) {
5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkAutoLockPixels alp(*bitmap);
5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ToColorProc proc = ChooseToColorProc(*bitmap);
5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL == proc) {
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const void* src = bitmap->getAddr(x, y);
5219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL == src) {
5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5248cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkColor dst[1];
5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    proc(dst, src, 1, bitmap->getColorTable());
5279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return dst[0];
5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Bitmap_getPixels(JNIEnv* env, jobject, const SkBitmap* bitmap,
5319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             jintArray pixelArray, int offset, int stride,
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             int x, int y, int width, int height) {
5339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkAutoLockPixels alp(*bitmap);
5348cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ToColorProc proc = ChooseToColorProc(*bitmap);
5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL == proc) {
5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const void* src = bitmap->getAddr(x, y);
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL == src) {
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkColorTable* ctable = bitmap->getColorTable();
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jint* dst = env->GetIntArrayElements(pixelArray, NULL);
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkColor* d = (SkColor*)dst + offset;
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    while (--height >= 0) {
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        proc(d, src, width, ctable);
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        d += stride;
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        src = (void*)((const char*)src + bitmap->rowBytes());
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->ReleaseIntArrayElements(pixelArray, dst, 0);
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Bitmap_setPixel(JNIEnv* env, jobject, const SkBitmap* bitmap,
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            int x, int y, SkColor color) {
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkAutoLockPixels alp(*bitmap);
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL == bitmap->getPixels()) {
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    FromColorProc proc = ChooseFromColorProc(bitmap->config());
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL == proc) {
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    proc(bitmap->getAddr(x, y), &color, 1, x, y);
5701a81aea8144b92ebaf807af9de69a0ab3415a5aaRomain Guy    bitmap->notifyPixelsChanged();
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Bitmap_setPixels(JNIEnv* env, jobject, const SkBitmap* bitmap,
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             jintArray pixelArray, int offset, int stride,
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             int x, int y, int width, int height) {
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    GraphicsJNI::SetPixels(env, pixelArray, offset, stride,
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           x, y, width, height, *bitmap);
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject,
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                      const SkBitmap* bitmap, jobject jbuffer) {
5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkAutoLockPixels alp(*bitmap);
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const void* src = bitmap->getPixels();
5848cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL != src) {
5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE);
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // the java side has already checked that buffer is large enough
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        memcpy(abp.pointer(), src, bitmap->getSize());
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Bitmap_copyPixelsFromBuffer(JNIEnv* env, jobject,
5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                    const SkBitmap* bitmap, jobject jbuffer) {
5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkAutoLockPixels alp(*bitmap);
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    void* dst = bitmap->getPixels();
5978cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (NULL != dst) {
5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        android::AutoBufferPointer abp(env, jbuffer, JNI_FALSE);
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // the java side has already checked that buffer is large enough
6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        memcpy(dst, abp.pointer(), bitmap->getSize());
6022c183fa5bf666c5f6b2221fd04db8ee3c738d1fdRomain Guy        bitmap->notifyPixelsChanged();
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
60676d1e01d5e65c4631c827831e98ad4e300d99eabMike Reedstatic bool Bitmap_sameAs(JNIEnv* env, jobject, const SkBitmap* bm0,
60776d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed                             const SkBitmap* bm1) {
60876d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    if (bm0->width() != bm1->width() ||
60976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        bm0->height() != bm1->height() ||
61076d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        bm0->config() != bm1->config()) {
61176d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        return false;
61276d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    }
61376d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed
61476d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    SkAutoLockPixels alp0(*bm0);
61576d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    SkAutoLockPixels alp1(*bm1);
61676d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed
61776d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    // if we can't load the pixels, return false
61876d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    if (NULL == bm0->getPixels() || NULL == bm1->getPixels()) {
61976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        return false;
62076d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    }
62176d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed
62276d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    if (bm0->config() == SkBitmap::kIndex8_Config) {
62376d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        SkColorTable* ct0 = bm0->getColorTable();
62476d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        SkColorTable* ct1 = bm1->getColorTable();
62576d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        if (NULL == ct0 || NULL == ct1) {
62676d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed            return false;
62776d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        }
62876d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        if (ct0->count() != ct1->count()) {
62976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed            return false;
63076d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        }
63176d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed
63276d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        SkAutoLockColors alc0(ct0);
63376d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        SkAutoLockColors alc1(ct1);
63476d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        const size_t size = ct0->count() * sizeof(SkPMColor);
63576d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        if (memcmp(alc0.colors(), alc1.colors(), size) != 0) {
63676d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed            return false;
63776d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        }
63876d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    }
63976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed
64076d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    // now compare each scanline. We can't do the entire buffer at once,
64176d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    // since we don't care about the pixel values that might extend beyond
64276d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    // the width (since the scanline might be larger than the logical width)
64376d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    const int h = bm0->height();
64476d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    const size_t size = bm0->width() * bm0->bytesPerPixel();
64576d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    for (int y = 0; y < h; y++) {
64676d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        if (memcmp(bm0->getAddr(0, y), bm1->getAddr(0, y), size) != 0) {
64776d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed            return false;
64876d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed        }
64976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    }
65076d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    return true;
65176d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed}
65276d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed
6538cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chenstatic void Bitmap_prepareToDraw(JNIEnv* env, jobject, SkBitmap* bitmap) {
6548cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen    bitmap->lockPixels();
6558cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen    bitmap->unlockPixels();
6568cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen}
6578cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
6589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project///////////////////////////////////////////////////////////////////////////////
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <android_runtime/AndroidRuntime.h>
6619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JNINativeMethod gBitmapMethods[] = {
6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeCreate",             "([IIIIIIZ)Landroid/graphics/Bitmap;",
6649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void*)Bitmap_creator },
6659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeCopy",               "(IIZ)Landroid/graphics/Bitmap;",
6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void*)Bitmap_copy },
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeDestructor",         "(I)V", (void*)Bitmap_destructor },
668547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase    {   "nativeRecycle",            "(I)Z", (void*)Bitmap_recycle },
6699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeCompress",           "(IIILjava/io/OutputStream;[B)Z",
6709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void*)Bitmap_compress },
6719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeErase",              "(II)V", (void*)Bitmap_erase },
6729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeWidth",              "(I)I", (void*)Bitmap_width },
6739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeHeight",             "(I)I", (void*)Bitmap_height },
6749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeRowBytes",           "(I)I", (void*)Bitmap_rowBytes },
6759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeConfig",             "(I)I", (void*)Bitmap_config },
6769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeHasAlpha",           "(I)Z", (void*)Bitmap_hasAlpha },
677a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    {   "nativeSetHasAlpha",        "(IZ)V", (void*)Bitmap_setHasAlpha },
678713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    {   "nativeHasMipMap",          "(I)Z", (void*)Bitmap_hasMipMap },
679713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    {   "nativeSetHasMipMap",       "(IZ)V", (void*)Bitmap_setHasMipMap },
6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeCreateFromParcel",
6819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;",
6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void*)Bitmap_createFromParcel },
683de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn    {   "nativeWriteToParcel",      "(IZILandroid/os/Parcel;)Z",
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void*)Bitmap_writeToParcel },
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeExtractAlpha",       "(II[I)Landroid/graphics/Bitmap;",
6869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void*)Bitmap_extractAlpha },
6870bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    {   "nativeGenerationId",       "(I)I", (void*)Bitmap_getGenerationId },
6889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeGetPixel",           "(III)I", (void*)Bitmap_getPixel },
6899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeGetPixels",          "(I[IIIIIII)V", (void*)Bitmap_getPixels },
6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeSetPixel",           "(IIII)V", (void*)Bitmap_setPixel },
6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeSetPixels",          "(I[IIIIIII)V", (void*)Bitmap_setPixels },
6929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeCopyPixelsToBuffer", "(ILjava/nio/Buffer;)V",
6939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            (void*)Bitmap_copyPixelsToBuffer },
6949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {   "nativeCopyPixelsFromBuffer", "(ILjava/nio/Buffer;)V",
6958cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen                                            (void*)Bitmap_copyPixelsFromBuffer },
69676d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    {   "nativeSameAs",             "(II)Z", (void*)Bitmap_sameAs },
69776d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    {   "nativePrepareToDraw",      "(I)V", (void*)Bitmap_prepareToDraw },
6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
6999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define kClassPathName  "android/graphics/Bitmap"
7019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint register_android_graphics_Bitmap(JNIEnv* env)
7039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return android::AndroidRuntime::registerNativeMethods(env, kClassPathName,
7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                gBitmapMethods, SK_ARRAY_COUNT(gBitmapMethods));
7069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
707