The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | #include "SkBitmap.h"
|
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 2 | #include "SkPixelRef.h"
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3 | #include "SkImageEncoder.h"
|
| 4 | #include "SkColorPriv.h"
|
| 5 | #include "GraphicsJNI.h"
|
| 6 | #include "SkDither.h"
|
| 7 | #include "SkUnPreMultiply.h"
|
| 8 |
|
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 9 | #include <binder/Parcel.h>
|
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame^] | 10 | #include "android_os_Parcel.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11 | #include "android_util_Binder.h"
|
| 12 | #include "android_nio_utils.h"
|
| 13 | #include "CreateJavaOutputStreamAdaptor.h"
|
| 14 |
|
| 15 | #include <jni.h>
|
| 16 |
|
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 17 | #include <Caches.h>
|
| 18 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | #if 0
|
| 20 | #define TRACE_BITMAP(code) code
|
| 21 | #else
|
| 22 | #define TRACE_BITMAP(code)
|
| 23 | #endif
|
| 24 |
|
| 25 | ///////////////////////////////////////////////////////////////////////////////
|
| 26 | // Conversions to/from SkColor, for get/setPixels, and the create method, which
|
| 27 | // is basically like setPixels
|
| 28 |
|
| 29 | typedef void (*FromColorProc)(void* dst, const SkColor src[], int width,
|
| 30 | int x, int y);
|
| 31 |
|
| 32 | static void FromColor_D32(void* dst, const SkColor src[], int width,
|
| 33 | int, int) {
|
| 34 | SkPMColor* d = (SkPMColor*)dst;
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 35 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | for (int i = 0; i < width; i++) {
|
| 37 | *d++ = SkPreMultiplyColor(*src++);
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | static void FromColor_D565(void* dst, const SkColor src[], int width,
|
| 42 | int x, int y) {
|
| 43 | uint16_t* d = (uint16_t*)dst;
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 44 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | DITHER_565_SCAN(y);
|
| 46 | for (int stop = x + width; x < stop; x++) {
|
| 47 | SkColor c = *src++;
|
| 48 | *d++ = SkDitherRGBTo565(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c),
|
| 49 | DITHER_VALUE(x));
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | static void FromColor_D4444(void* dst, const SkColor src[], int width,
|
| 54 | int x, int y) {
|
| 55 | SkPMColor16* d = (SkPMColor16*)dst;
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 56 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | DITHER_4444_SCAN(y);
|
| 58 | for (int stop = x + width; x < stop; x++) {
|
| 59 | SkPMColor c = SkPreMultiplyColor(*src++);
|
| 60 | *d++ = SkDitherARGB32To4444(c, DITHER_VALUE(x));
|
| 61 | // *d++ = SkPixel32ToPixel4444(c);
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | // can return NULL
|
| 66 | static FromColorProc ChooseFromColorProc(SkBitmap::Config config) {
|
| 67 | switch (config) {
|
| 68 | case SkBitmap::kARGB_8888_Config:
|
| 69 | return FromColor_D32;
|
| 70 | case SkBitmap::kARGB_4444_Config:
|
| 71 | return FromColor_D4444;
|
| 72 | case SkBitmap::kRGB_565_Config:
|
| 73 | return FromColor_D565;
|
| 74 | default:
|
| 75 | break;
|
| 76 | }
|
| 77 | return NULL;
|
| 78 | }
|
| 79 |
|
| 80 | bool GraphicsJNI::SetPixels(JNIEnv* env, jintArray srcColors,
|
| 81 | int srcOffset, int srcStride,
|
| 82 | int x, int y, int width, int height,
|
| 83 | const SkBitmap& dstBitmap) {
|
| 84 | SkAutoLockPixels alp(dstBitmap);
|
| 85 | void* dst = dstBitmap.getPixels();
|
| 86 | FromColorProc proc = ChooseFromColorProc(dstBitmap.config());
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 87 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | if (NULL == dst || NULL == proc) {
|
| 89 | return false;
|
| 90 | }
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 91 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | const jint* array = env->GetIntArrayElements(srcColors, NULL);
|
| 93 | const SkColor* src = (const SkColor*)array + srcOffset;
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 94 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | // reset to to actual choice from caller
|
| 96 | dst = dstBitmap.getAddr(x, y);
|
| 97 | // now copy/convert each scanline
|
| 98 | for (int y = 0; y < height; y++) {
|
| 99 | proc(dst, src, width, x, y);
|
| 100 | src += srcStride;
|
| 101 | dst = (char*)dst + dstBitmap.rowBytes();
|
| 102 | }
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 103 |
|
Romain Guy | 1a81aea | 2011-03-21 15:24:51 -0700 | [diff] [blame] | 104 | dstBitmap.notifyPixelsChanged();
|
| 105 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | env->ReleaseIntArrayElements(srcColors, const_cast<jint*>(array),
|
| 107 | JNI_ABORT);
|
| 108 | return true;
|
| 109 | }
|
| 110 |
|
| 111 | //////////////////// ToColor procs
|
| 112 |
|
| 113 | typedef void (*ToColorProc)(SkColor dst[], const void* src, int width,
|
| 114 | SkColorTable*);
|
| 115 |
|
| 116 | static void ToColor_S32_Alpha(SkColor dst[], const void* src, int width,
|
| 117 | SkColorTable*) {
|
| 118 | SkASSERT(width > 0);
|
| 119 | const SkPMColor* s = (const SkPMColor*)src;
|
| 120 | do {
|
| 121 | *dst++ = SkUnPreMultiply::PMColorToColor(*s++);
|
| 122 | } while (--width != 0);
|
| 123 | }
|
| 124 |
|
| 125 | static void ToColor_S32_Opaque(SkColor dst[], const void* src, int width,
|
| 126 | SkColorTable*) {
|
| 127 | SkASSERT(width > 0);
|
| 128 | const SkPMColor* s = (const SkPMColor*)src;
|
| 129 | do {
|
| 130 | SkPMColor c = *s++;
|
| 131 | *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
|
| 132 | SkGetPackedB32(c));
|
| 133 | } while (--width != 0);
|
| 134 | }
|
| 135 |
|
| 136 | static void ToColor_S4444_Alpha(SkColor dst[], const void* src, int width,
|
| 137 | SkColorTable*) {
|
| 138 | SkASSERT(width > 0);
|
| 139 | const SkPMColor16* s = (const SkPMColor16*)src;
|
| 140 | do {
|
| 141 | *dst++ = SkUnPreMultiply::PMColorToColor(SkPixel4444ToPixel32(*s++));
|
| 142 | } while (--width != 0);
|
| 143 | }
|
| 144 |
|
| 145 | static void ToColor_S4444_Opaque(SkColor dst[], const void* src, int width,
|
| 146 | SkColorTable*) {
|
| 147 | SkASSERT(width > 0);
|
| 148 | const SkPMColor* s = (const SkPMColor*)src;
|
| 149 | do {
|
| 150 | SkPMColor c = SkPixel4444ToPixel32(*s++);
|
| 151 | *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
|
| 152 | SkGetPackedB32(c));
|
| 153 | } while (--width != 0);
|
| 154 | }
|
| 155 |
|
| 156 | static void ToColor_S565(SkColor dst[], const void* src, int width,
|
| 157 | SkColorTable*) {
|
| 158 | SkASSERT(width > 0);
|
| 159 | const uint16_t* s = (const uint16_t*)src;
|
| 160 | do {
|
| 161 | uint16_t c = *s++;
|
| 162 | *dst++ = SkColorSetRGB(SkPacked16ToR32(c), SkPacked16ToG32(c),
|
| 163 | SkPacked16ToB32(c));
|
| 164 | } while (--width != 0);
|
| 165 | }
|
| 166 |
|
| 167 | static void ToColor_SI8_Alpha(SkColor dst[], const void* src, int width,
|
| 168 | SkColorTable* ctable) {
|
| 169 | SkASSERT(width > 0);
|
| 170 | const uint8_t* s = (const uint8_t*)src;
|
| 171 | const SkPMColor* colors = ctable->lockColors();
|
| 172 | do {
|
| 173 | *dst++ = SkUnPreMultiply::PMColorToColor(colors[*s++]);
|
| 174 | } while (--width != 0);
|
| 175 | ctable->unlockColors(false);
|
| 176 | }
|
| 177 |
|
| 178 | static void ToColor_SI8_Opaque(SkColor dst[], const void* src, int width,
|
| 179 | SkColorTable* ctable) {
|
| 180 | SkASSERT(width > 0);
|
| 181 | const uint8_t* s = (const uint8_t*)src;
|
| 182 | const SkPMColor* colors = ctable->lockColors();
|
| 183 | do {
|
| 184 | SkPMColor c = colors[*s++];
|
| 185 | *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c),
|
| 186 | SkGetPackedB32(c));
|
| 187 | } while (--width != 0);
|
| 188 | ctable->unlockColors(false);
|
| 189 | }
|
| 190 |
|
| 191 | // can return NULL
|
| 192 | static ToColorProc ChooseToColorProc(const SkBitmap& src) {
|
| 193 | switch (src.config()) {
|
| 194 | case SkBitmap::kARGB_8888_Config:
|
| 195 | return src.isOpaque() ? ToColor_S32_Opaque : ToColor_S32_Alpha;
|
| 196 | case SkBitmap::kARGB_4444_Config:
|
| 197 | return src.isOpaque() ? ToColor_S4444_Opaque : ToColor_S4444_Alpha;
|
| 198 | case SkBitmap::kRGB_565_Config:
|
| 199 | return ToColor_S565;
|
| 200 | case SkBitmap::kIndex8_Config:
|
| 201 | if (src.getColorTable() == NULL) {
|
| 202 | return NULL;
|
| 203 | }
|
| 204 | return src.isOpaque() ? ToColor_SI8_Opaque : ToColor_SI8_Alpha;
|
| 205 | default:
|
| 206 | break;
|
| 207 | }
|
| 208 | return NULL;
|
| 209 | }
|
| 210 |
|
| 211 | ///////////////////////////////////////////////////////////////////////////////
|
| 212 | ///////////////////////////////////////////////////////////////////////////////
|
| 213 |
|
| 214 | static jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors,
|
| 215 | int offset, int stride, int width, int height,
|
| 216 | SkBitmap::Config config, jboolean isMutable) {
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | if (NULL != jColors) {
|
| 218 | size_t n = env->GetArrayLength(jColors);
|
| 219 | if (n < SkAbs32(stride) * (size_t)height) {
|
| 220 | doThrowAIOOBE(env);
|
| 221 | return NULL;
|
| 222 | }
|
| 223 | }
|
| 224 |
|
| 225 | SkBitmap bitmap;
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 226 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | bitmap.setConfig(config, width, height);
|
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 228 |
|
| 229 | jbyteArray buff = GraphicsJNI::allocateJavaPixelRef(env, &bitmap, NULL);
|
| 230 | if (NULL == buff) {
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | return NULL;
|
| 232 | }
|
| 233 |
|
| 234 | if (jColors != NULL) {
|
| 235 | GraphicsJNI::SetPixels(env, jColors, offset, stride,
|
| 236 | 0, 0, width, height, bitmap);
|
| 237 | }
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 238 |
|
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 239 | return GraphicsJNI::createBitmap(env, new SkBitmap(bitmap), buff, isMutable, NULL);
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | }
|
| 241 |
|
| 242 | static jobject Bitmap_copy(JNIEnv* env, jobject, const SkBitmap* src,
|
| 243 | SkBitmap::Config dstConfig, jboolean isMutable) {
|
| 244 | SkBitmap result;
|
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 245 | JavaPixelAllocator allocator(env);
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 |
|
| 247 | if (!src->copyTo(&result, dstConfig, &allocator)) {
|
| 248 | return NULL;
|
| 249 | }
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 250 |
|
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 251 | return GraphicsJNI::createBitmap(env, new SkBitmap(result), allocator.getStorageObj(), isMutable, NULL);
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | }
|
| 253 |
|
| 254 | static void Bitmap_destructor(JNIEnv* env, jobject, SkBitmap* bitmap) {
|
Romain Guy | 9aaa826 | 2010-09-08 15:15:43 -0700 | [diff] [blame] | 255 | #ifdef USE_OPENGL_RENDERER
|
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 256 | if (android::uirenderer::Caches::hasInstance()) {
|
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 257 | android::uirenderer::Caches::getInstance().resourceCache.destructor(bitmap);
|
Chet Haase | 27f0b17 | 2010-10-22 08:52:33 -0700 | [diff] [blame] | 258 | return;
|
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 259 | }
|
Chet Haase | 27f0b17 | 2010-10-22 08:52:33 -0700 | [diff] [blame] | 260 | #endif // USE_OPENGL_RENDERER
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | delete bitmap;
|
| 262 | }
|
| 263 |
|
| 264 | static void Bitmap_recycle(JNIEnv* env, jobject, SkBitmap* bitmap) {
|
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 265 | #ifdef USE_OPENGL_RENDERER
|
| 266 | if (android::uirenderer::Caches::hasInstance()) {
|
| 267 | android::uirenderer::Caches::getInstance().resourceCache.recycle(bitmap);
|
Chet Haase | 27f0b17 | 2010-10-22 08:52:33 -0700 | [diff] [blame] | 268 | return;
|
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 269 | }
|
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 270 | #endif // USE_OPENGL_RENDERER
|
Chet Haase | 27f0b17 | 2010-10-22 08:52:33 -0700 | [diff] [blame] | 271 | bitmap->setPixels(NULL, NULL);
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | }
|
| 273 |
|
| 274 | // These must match the int values in Bitmap.java
|
| 275 | enum JavaEncodeFormat {
|
| 276 | kJPEG_JavaEncodeFormat = 0,
|
Vikas Arora | 2305ac9 | 2011-06-23 13:11:13 +0530 | [diff] [blame] | 277 | kPNG_JavaEncodeFormat = 1,
|
| 278 | kWEBP_JavaEncodeFormat = 2
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | };
|
| 280 |
|
| 281 | static bool Bitmap_compress(JNIEnv* env, jobject clazz, SkBitmap* bitmap,
|
| 282 | int format, int quality,
|
| 283 | jobject jstream, jbyteArray jstorage) {
|
| 284 | SkImageEncoder::Type fm;
|
| 285 |
|
| 286 | switch (format) {
|
| 287 | case kJPEG_JavaEncodeFormat:
|
| 288 | fm = SkImageEncoder::kJPEG_Type;
|
| 289 | break;
|
| 290 | case kPNG_JavaEncodeFormat:
|
| 291 | fm = SkImageEncoder::kPNG_Type;
|
| 292 | break;
|
Vikas Arora | 2305ac9 | 2011-06-23 13:11:13 +0530 | [diff] [blame] | 293 | case kWEBP_JavaEncodeFormat:
|
| 294 | fm = SkImageEncoder::kWEBP_Type;
|
| 295 | break;
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | default:
|
| 297 | return false;
|
| 298 | }
|
| 299 |
|
| 300 | bool success = false;
|
| 301 | SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage);
|
| 302 | if (NULL != strm) {
|
| 303 | SkImageEncoder* encoder = SkImageEncoder::Create(fm);
|
| 304 | if (NULL != encoder) {
|
| 305 | success = encoder->encodeStream(strm, *bitmap, quality);
|
| 306 | delete encoder;
|
| 307 | }
|
| 308 | delete strm;
|
| 309 | }
|
| 310 | return success;
|
| 311 | }
|
| 312 |
|
| 313 | static void Bitmap_erase(JNIEnv* env, jobject, SkBitmap* bitmap, jint color) {
|
| 314 | bitmap->eraseColor(color);
|
| 315 | }
|
| 316 |
|
| 317 | static int Bitmap_width(JNIEnv* env, jobject, SkBitmap* bitmap) {
|
| 318 | return bitmap->width();
|
| 319 | }
|
| 320 |
|
| 321 | static int Bitmap_height(JNIEnv* env, jobject, SkBitmap* bitmap) {
|
| 322 | return bitmap->height();
|
| 323 | }
|
| 324 |
|
| 325 | static int Bitmap_rowBytes(JNIEnv* env, jobject, SkBitmap* bitmap) {
|
| 326 | return bitmap->rowBytes();
|
| 327 | }
|
| 328 |
|
| 329 | static int Bitmap_config(JNIEnv* env, jobject, SkBitmap* bitmap) {
|
| 330 | return bitmap->config();
|
| 331 | }
|
| 332 |
|
Romain Guy | 0bbae08 | 2010-06-15 18:03:40 -0700 | [diff] [blame] | 333 | static int Bitmap_getGenerationId(JNIEnv* env, jobject, SkBitmap* bitmap) {
|
| 334 | return bitmap->getGenerationID();
|
| 335 | }
|
| 336 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | static jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, SkBitmap* bitmap) {
|
| 338 | return !bitmap->isOpaque();
|
| 339 | }
|
| 340 |
|
Mike Reed | a78b0a2 | 2009-10-07 11:38:05 -0700 | [diff] [blame] | 341 | static void Bitmap_setHasAlpha(JNIEnv* env, jobject, SkBitmap* bitmap,
|
| 342 | jboolean hasAlpha) {
|
| 343 | bitmap->setIsOpaque(!hasAlpha);
|
| 344 | }
|
| 345 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | ///////////////////////////////////////////////////////////////////////////////
|
| 347 |
|
| 348 | static jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) {
|
| 349 | if (parcel == NULL) {
|
| 350 | SkDebugf("-------- unparcel parcel is NULL\n");
|
| 351 | return NULL;
|
| 352 | }
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 353 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | android::Parcel* p = android::parcelForJavaObject(env, parcel);
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 355 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | const bool isMutable = p->readInt32() != 0;
|
| 357 | const SkBitmap::Config config = (SkBitmap::Config)p->readInt32();
|
| 358 | const int width = p->readInt32();
|
| 359 | const int height = p->readInt32();
|
| 360 | const int rowBytes = p->readInt32();
|
Dianne Hackborn | de0dfb7 | 2009-09-23 14:09:34 -0700 | [diff] [blame] | 361 | const int density = p->readInt32();
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 362 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | if (SkBitmap::kARGB_8888_Config != config &&
|
| 364 | SkBitmap::kRGB_565_Config != config &&
|
| 365 | SkBitmap::kARGB_4444_Config != config &&
|
| 366 | SkBitmap::kIndex8_Config != config &&
|
| 367 | SkBitmap::kA8_Config != config) {
|
| 368 | SkDebugf("Bitmap_createFromParcel unknown config: %d\n", config);
|
| 369 | return NULL;
|
| 370 | }
|
| 371 |
|
| 372 | SkBitmap* bitmap = new SkBitmap;
|
| 373 |
|
| 374 | bitmap->setConfig(config, width, height, rowBytes);
|
| 375 |
|
| 376 | SkColorTable* ctable = NULL;
|
| 377 | if (config == SkBitmap::kIndex8_Config) {
|
| 378 | int count = p->readInt32();
|
| 379 | if (count > 0) {
|
| 380 | size_t size = count * sizeof(SkPMColor);
|
| 381 | const SkPMColor* src = (const SkPMColor*)p->readInplace(size);
|
| 382 | ctable = new SkColorTable(src, count);
|
| 383 | }
|
| 384 | }
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 385 |
|
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 386 | jbyteArray buffer = GraphicsJNI::allocateJavaPixelRef(env, bitmap, ctable);
|
| 387 | if (NULL == buffer) {
|
Derek Sollenberger | 6062c59 | 2011-02-22 13:55:04 -0500 | [diff] [blame] | 388 | SkSafeUnref(ctable);
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | delete bitmap;
|
| 390 | return NULL;
|
| 391 | }
|
| 392 |
|
Derek Sollenberger | 6062c59 | 2011-02-22 13:55:04 -0500 | [diff] [blame] | 393 | SkSafeUnref(ctable);
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 |
|
| 395 | size_t size = bitmap->getSize();
|
Jeff Brown | 6579a9d | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 396 |
|
| 397 | android::Parcel::ReadableBlob blob;
|
| 398 | android::status_t status = p->readBlob(size, &blob);
|
| 399 | if (status) {
|
| 400 | doThrowRE(env, "Could not read bitmap from parcel blob.");
|
| 401 | delete bitmap;
|
| 402 | return NULL;
|
| 403 | }
|
| 404 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | bitmap->lockPixels();
|
Jeff Brown | 6579a9d | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 406 | memcpy(bitmap->getPixels(), blob.data(), size);
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | bitmap->unlockPixels();
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 408 |
|
Jeff Brown | 6579a9d | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 409 | blob.release();
|
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 410 | return GraphicsJNI::createBitmap(env, bitmap, buffer, isMutable, NULL, density);
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | }
|
| 412 |
|
| 413 | static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject,
|
| 414 | const SkBitmap* bitmap,
|
Dianne Hackborn | de0dfb7 | 2009-09-23 14:09:34 -0700 | [diff] [blame] | 415 | jboolean isMutable, jint density,
|
| 416 | jobject parcel) {
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | if (parcel == NULL) {
|
| 418 | SkDebugf("------- writeToParcel null parcel\n");
|
| 419 | return false;
|
| 420 | }
|
| 421 |
|
| 422 | android::Parcel* p = android::parcelForJavaObject(env, parcel);
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 423 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | p->writeInt32(isMutable);
|
| 425 | p->writeInt32(bitmap->config());
|
| 426 | p->writeInt32(bitmap->width());
|
| 427 | p->writeInt32(bitmap->height());
|
| 428 | p->writeInt32(bitmap->rowBytes());
|
Dianne Hackborn | de0dfb7 | 2009-09-23 14:09:34 -0700 | [diff] [blame] | 429 | p->writeInt32(density);
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 |
|
| 431 | if (bitmap->getConfig() == SkBitmap::kIndex8_Config) {
|
| 432 | SkColorTable* ctable = bitmap->getColorTable();
|
| 433 | if (ctable != NULL) {
|
| 434 | int count = ctable->count();
|
| 435 | p->writeInt32(count);
|
| 436 | memcpy(p->writeInplace(count * sizeof(SkPMColor)),
|
| 437 | ctable->lockColors(), count * sizeof(SkPMColor));
|
| 438 | ctable->unlockColors(false);
|
| 439 | } else {
|
| 440 | p->writeInt32(0); // indicate no ctable
|
| 441 | }
|
| 442 | }
|
| 443 |
|
| 444 | size_t size = bitmap->getSize();
|
Jeff Brown | 6579a9d | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 445 |
|
| 446 | android::Parcel::WritableBlob blob;
|
| 447 | android::status_t status = p->writeBlob(size, &blob);
|
| 448 | if (status) {
|
| 449 | doThrowRE(env, "Could not write bitmap to parcel blob.");
|
| 450 | return false;
|
| 451 | }
|
| 452 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | bitmap->lockPixels();
|
Jack Palevich | dee4cb0 | 2010-12-13 11:13:32 -0800 | [diff] [blame] | 454 | const void* pSrc = bitmap->getPixels();
|
Jack Palevich | dee4cb0 | 2010-12-13 11:13:32 -0800 | [diff] [blame] | 455 | if (pSrc == NULL) {
|
Jeff Brown | 6579a9d | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 456 | memset(blob.data(), 0, size);
|
Jack Palevich | dee4cb0 | 2010-12-13 11:13:32 -0800 | [diff] [blame] | 457 | } else {
|
Jeff Brown | 6579a9d | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 458 | memcpy(blob.data(), pSrc, size);
|
Jack Palevich | dee4cb0 | 2010-12-13 11:13:32 -0800 | [diff] [blame] | 459 | }
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | bitmap->unlockPixels();
|
Jeff Brown | 6579a9d | 2011-09-23 21:17:56 -0700 | [diff] [blame] | 461 |
|
| 462 | blob.release();
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | return true;
|
| 464 | }
|
| 465 |
|
| 466 | static jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz,
|
| 467 | const SkBitmap* src, const SkPaint* paint,
|
| 468 | jintArray offsetXY) {
|
| 469 | SkIPoint offset;
|
| 470 | SkBitmap* dst = new SkBitmap;
|
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 471 | JavaPixelAllocator allocator(env);
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 472 |
|
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 473 | src->extractAlpha(dst, paint, &allocator, &offset);
|
Eric Hassold | ef7be26 | 2011-02-23 16:46:11 -0800 | [diff] [blame] | 474 | // If Skia can't allocate pixels for destination bitmap, it resets
|
| 475 | // it, that is set its pixels buffer to NULL, and zero width and height.
|
| 476 | if (dst->getPixels() == NULL && src->getPixels() != NULL) {
|
| 477 | delete dst;
|
| 478 | doThrowOOME(env, "failed to allocate pixels for alpha");
|
| 479 | return NULL;
|
| 480 | }
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) {
|
| 482 | int* array = env->GetIntArrayElements(offsetXY, NULL);
|
| 483 | array[0] = offset.fX;
|
| 484 | array[1] = offset.fY;
|
| 485 | env->ReleaseIntArrayElements(offsetXY, array, 0);
|
| 486 | }
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 487 |
|
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 488 | return GraphicsJNI::createBitmap(env, dst, allocator.getStorageObj(), true, NULL);
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | }
|
| 490 |
|
| 491 | ///////////////////////////////////////////////////////////////////////////////
|
| 492 |
|
| 493 | static int Bitmap_getPixel(JNIEnv* env, jobject, const SkBitmap* bitmap,
|
| 494 | int x, int y) {
|
| 495 | SkAutoLockPixels alp(*bitmap);
|
| 496 |
|
| 497 | ToColorProc proc = ChooseToColorProc(*bitmap);
|
| 498 | if (NULL == proc) {
|
| 499 | return 0;
|
| 500 | }
|
| 501 | const void* src = bitmap->getAddr(x, y);
|
| 502 | if (NULL == src) {
|
| 503 | return 0;
|
| 504 | }
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 505 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | SkColor dst[1];
|
| 507 | proc(dst, src, 1, bitmap->getColorTable());
|
| 508 | return dst[0];
|
| 509 | }
|
| 510 |
|
| 511 | static void Bitmap_getPixels(JNIEnv* env, jobject, const SkBitmap* bitmap,
|
| 512 | jintArray pixelArray, int offset, int stride,
|
| 513 | int x, int y, int width, int height) {
|
| 514 | SkAutoLockPixels alp(*bitmap);
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 515 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | ToColorProc proc = ChooseToColorProc(*bitmap);
|
| 517 | if (NULL == proc) {
|
| 518 | return;
|
| 519 | }
|
| 520 | const void* src = bitmap->getAddr(x, y);
|
| 521 | if (NULL == src) {
|
| 522 | return;
|
| 523 | }
|
| 524 |
|
| 525 | SkColorTable* ctable = bitmap->getColorTable();
|
| 526 | jint* dst = env->GetIntArrayElements(pixelArray, NULL);
|
| 527 | SkColor* d = (SkColor*)dst + offset;
|
| 528 | while (--height >= 0) {
|
| 529 | proc(d, src, width, ctable);
|
| 530 | d += stride;
|
| 531 | src = (void*)((const char*)src + bitmap->rowBytes());
|
| 532 | }
|
| 533 | env->ReleaseIntArrayElements(pixelArray, dst, 0);
|
| 534 | }
|
| 535 |
|
| 536 | ///////////////////////////////////////////////////////////////////////////////
|
| 537 |
|
| 538 | static void Bitmap_setPixel(JNIEnv* env, jobject, const SkBitmap* bitmap,
|
| 539 | int x, int y, SkColor color) {
|
| 540 | SkAutoLockPixels alp(*bitmap);
|
| 541 | if (NULL == bitmap->getPixels()) {
|
| 542 | return;
|
| 543 | }
|
| 544 |
|
| 545 | FromColorProc proc = ChooseFromColorProc(bitmap->config());
|
| 546 | if (NULL == proc) {
|
| 547 | return;
|
| 548 | }
|
| 549 |
|
| 550 | proc(bitmap->getAddr(x, y), &color, 1, x, y);
|
Romain Guy | 1a81aea | 2011-03-21 15:24:51 -0700 | [diff] [blame] | 551 | bitmap->notifyPixelsChanged();
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | }
|
| 553 |
|
| 554 | static void Bitmap_setPixels(JNIEnv* env, jobject, const SkBitmap* bitmap,
|
| 555 | jintArray pixelArray, int offset, int stride,
|
| 556 | int x, int y, int width, int height) {
|
| 557 | GraphicsJNI::SetPixels(env, pixelArray, offset, stride,
|
| 558 | x, y, width, height, *bitmap);
|
| 559 | }
|
| 560 |
|
| 561 | static void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject,
|
| 562 | const SkBitmap* bitmap, jobject jbuffer) {
|
| 563 | SkAutoLockPixels alp(*bitmap);
|
| 564 | const void* src = bitmap->getPixels();
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 565 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | if (NULL != src) {
|
| 567 | android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE);
|
| 568 |
|
| 569 | // the java side has already checked that buffer is large enough
|
| 570 | memcpy(abp.pointer(), src, bitmap->getSize());
|
| 571 | }
|
| 572 | }
|
| 573 |
|
| 574 | static void Bitmap_copyPixelsFromBuffer(JNIEnv* env, jobject,
|
| 575 | const SkBitmap* bitmap, jobject jbuffer) {
|
| 576 | SkAutoLockPixels alp(*bitmap);
|
| 577 | void* dst = bitmap->getPixels();
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 578 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | if (NULL != dst) {
|
| 580 | android::AutoBufferPointer abp(env, jbuffer, JNI_FALSE);
|
| 581 | // the java side has already checked that buffer is large enough
|
| 582 | memcpy(dst, abp.pointer(), bitmap->getSize());
|
Romain Guy | 2c183fa | 2011-10-18 17:56:06 -0700 | [diff] [blame] | 583 | bitmap->notifyPixelsChanged();
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | }
|
| 585 | }
|
| 586 |
|
Mike Reed | 76d1e01 | 2010-03-05 17:42:30 -0500 | [diff] [blame] | 587 | static bool Bitmap_sameAs(JNIEnv* env, jobject, const SkBitmap* bm0,
|
| 588 | const SkBitmap* bm1) {
|
| 589 | if (bm0->width() != bm1->width() ||
|
| 590 | bm0->height() != bm1->height() ||
|
| 591 | bm0->config() != bm1->config()) {
|
| 592 | return false;
|
| 593 | }
|
| 594 |
|
| 595 | SkAutoLockPixels alp0(*bm0);
|
| 596 | SkAutoLockPixels alp1(*bm1);
|
| 597 |
|
| 598 | // if we can't load the pixels, return false
|
| 599 | if (NULL == bm0->getPixels() || NULL == bm1->getPixels()) {
|
| 600 | return false;
|
| 601 | }
|
| 602 |
|
| 603 | if (bm0->config() == SkBitmap::kIndex8_Config) {
|
| 604 | SkColorTable* ct0 = bm0->getColorTable();
|
| 605 | SkColorTable* ct1 = bm1->getColorTable();
|
| 606 | if (NULL == ct0 || NULL == ct1) {
|
| 607 | return false;
|
| 608 | }
|
| 609 | if (ct0->count() != ct1->count()) {
|
| 610 | return false;
|
| 611 | }
|
| 612 |
|
| 613 | SkAutoLockColors alc0(ct0);
|
| 614 | SkAutoLockColors alc1(ct1);
|
| 615 | const size_t size = ct0->count() * sizeof(SkPMColor);
|
| 616 | if (memcmp(alc0.colors(), alc1.colors(), size) != 0) {
|
| 617 | return false;
|
| 618 | }
|
| 619 | }
|
| 620 |
|
| 621 | // now compare each scanline. We can't do the entire buffer at once,
|
| 622 | // since we don't care about the pixel values that might extend beyond
|
| 623 | // the width (since the scanline might be larger than the logical width)
|
| 624 | const int h = bm0->height();
|
| 625 | const size_t size = bm0->width() * bm0->bytesPerPixel();
|
| 626 | for (int y = 0; y < h; y++) {
|
| 627 | if (memcmp(bm0->getAddr(0, y), bm1->getAddr(0, y), size) != 0) {
|
| 628 | return false;
|
| 629 | }
|
| 630 | }
|
| 631 | return true;
|
| 632 | }
|
| 633 |
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 634 | static void Bitmap_prepareToDraw(JNIEnv* env, jobject, SkBitmap* bitmap) {
|
| 635 | bitmap->lockPixels();
|
| 636 | bitmap->unlockPixels();
|
| 637 | }
|
| 638 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | ///////////////////////////////////////////////////////////////////////////////
|
| 640 |
|
| 641 | #include <android_runtime/AndroidRuntime.h>
|
| 642 |
|
| 643 | static JNINativeMethod gBitmapMethods[] = {
|
| 644 | { "nativeCreate", "([IIIIIIZ)Landroid/graphics/Bitmap;",
|
| 645 | (void*)Bitmap_creator },
|
| 646 | { "nativeCopy", "(IIZ)Landroid/graphics/Bitmap;",
|
| 647 | (void*)Bitmap_copy },
|
| 648 | { "nativeDestructor", "(I)V", (void*)Bitmap_destructor },
|
| 649 | { "nativeRecycle", "(I)V", (void*)Bitmap_recycle },
|
| 650 | { "nativeCompress", "(IIILjava/io/OutputStream;[B)Z",
|
| 651 | (void*)Bitmap_compress },
|
| 652 | { "nativeErase", "(II)V", (void*)Bitmap_erase },
|
| 653 | { "nativeWidth", "(I)I", (void*)Bitmap_width },
|
| 654 | { "nativeHeight", "(I)I", (void*)Bitmap_height },
|
| 655 | { "nativeRowBytes", "(I)I", (void*)Bitmap_rowBytes },
|
| 656 | { "nativeConfig", "(I)I", (void*)Bitmap_config },
|
| 657 | { "nativeHasAlpha", "(I)Z", (void*)Bitmap_hasAlpha },
|
Mike Reed | a78b0a2 | 2009-10-07 11:38:05 -0700 | [diff] [blame] | 658 | { "nativeSetHasAlpha", "(IZ)V", (void*)Bitmap_setHasAlpha },
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | { "nativeCreateFromParcel",
|
| 660 | "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;",
|
| 661 | (void*)Bitmap_createFromParcel },
|
Dianne Hackborn | de0dfb7 | 2009-09-23 14:09:34 -0700 | [diff] [blame] | 662 | { "nativeWriteToParcel", "(IZILandroid/os/Parcel;)Z",
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | (void*)Bitmap_writeToParcel },
|
| 664 | { "nativeExtractAlpha", "(II[I)Landroid/graphics/Bitmap;",
|
| 665 | (void*)Bitmap_extractAlpha },
|
Romain Guy | 0bbae08 | 2010-06-15 18:03:40 -0700 | [diff] [blame] | 666 | { "nativeGenerationId", "(I)I", (void*)Bitmap_getGenerationId },
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | { "nativeGetPixel", "(III)I", (void*)Bitmap_getPixel },
|
| 668 | { "nativeGetPixels", "(I[IIIIIII)V", (void*)Bitmap_getPixels },
|
| 669 | { "nativeSetPixel", "(IIII)V", (void*)Bitmap_setPixel },
|
| 670 | { "nativeSetPixels", "(I[IIIIIII)V", (void*)Bitmap_setPixels },
|
| 671 | { "nativeCopyPixelsToBuffer", "(ILjava/nio/Buffer;)V",
|
| 672 | (void*)Bitmap_copyPixelsToBuffer },
|
| 673 | { "nativeCopyPixelsFromBuffer", "(ILjava/nio/Buffer;)V",
|
Wei-Ta Chen | 8cdcb12 | 2009-06-18 19:14:38 +0800 | [diff] [blame] | 674 | (void*)Bitmap_copyPixelsFromBuffer },
|
Mike Reed | 76d1e01 | 2010-03-05 17:42:30 -0500 | [diff] [blame] | 675 | { "nativeSameAs", "(II)Z", (void*)Bitmap_sameAs },
|
| 676 | { "nativePrepareToDraw", "(I)V", (void*)Bitmap_prepareToDraw },
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | };
|
| 678 |
|
| 679 | #define kClassPathName "android/graphics/Bitmap"
|
| 680 |
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | int register_android_graphics_Bitmap(JNIEnv* env)
|
| 682 | {
|
| 683 | return android::AndroidRuntime::registerNativeMethods(env, kClassPathName,
|
| 684 | gBitmapMethods, SK_ARRAY_COUNT(gBitmapMethods));
|
| 685 | }
|