Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <android/bitmap.h> |
Andreas Gampe | cad87d7 | 2014-11-10 17:13:06 -0800 | [diff] [blame] | 18 | |
| 19 | #pragma GCC diagnostic push |
| 20 | #pragma GCC diagnostic ignored "-Wunused-parameter" |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 21 | #include <GraphicsJNI.h> |
Andreas Gampe | cad87d7 | 2014-11-10 17:13:06 -0800 | [diff] [blame] | 22 | #pragma GCC diagnostic pop |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 23 | |
| 24 | int AndroidBitmap_getInfo(JNIEnv* env, jobject jbitmap, |
| 25 | AndroidBitmapInfo* info) { |
| 26 | if (NULL == env || NULL == jbitmap) { |
| 27 | return ANDROID_BITMAP_RESULT_BAD_PARAMETER; |
| 28 | } |
| 29 | |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 30 | SkBitmap bm; |
| 31 | GraphicsJNI::getSkBitmap(env, jbitmap, &bm); |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 32 | |
| 33 | if (info) { |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 34 | info->width = bm.width(); |
| 35 | info->height = bm.height(); |
| 36 | info->stride = bm.rowBytes(); |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 37 | info->flags = 0; |
| 38 | |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 39 | switch (bm.colorType()) { |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 40 | case kN32_SkColorType: |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 41 | info->format = ANDROID_BITMAP_FORMAT_RGBA_8888; |
| 42 | break; |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 43 | case kRGB_565_SkColorType: |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 44 | info->format = ANDROID_BITMAP_FORMAT_RGB_565; |
| 45 | break; |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 46 | case kARGB_4444_SkColorType: |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 47 | info->format = ANDROID_BITMAP_FORMAT_RGBA_4444; |
| 48 | break; |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 49 | case kAlpha_8_SkColorType: |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 50 | info->format = ANDROID_BITMAP_FORMAT_A_8; |
| 51 | break; |
| 52 | default: |
| 53 | info->format = ANDROID_BITMAP_FORMAT_NONE; |
| 54 | break; |
| 55 | } |
| 56 | } |
Andrew Hsieh | eba8254 | 2012-12-12 11:27:44 +0800 | [diff] [blame] | 57 | return ANDROID_BITMAP_RESULT_SUCCESS; |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | int AndroidBitmap_lockPixels(JNIEnv* env, jobject jbitmap, void** addrPtr) { |
| 61 | if (NULL == env || NULL == jbitmap) { |
| 62 | return ANDROID_BITMAP_RESULT_BAD_PARAMETER; |
| 63 | } |
| 64 | |
John Reck | ae2e8b4 | 2015-05-06 14:55:05 -0700 | [diff] [blame] | 65 | SkPixelRef* pixelRef = GraphicsJNI::refSkPixelRef(env, jbitmap); |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 66 | if (!pixelRef) { |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 67 | return ANDROID_BITMAP_RESULT_JNI_EXCEPTION; |
| 68 | } |
| 69 | |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 70 | pixelRef->lockPixels(); |
| 71 | void* addr = pixelRef->pixels(); |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 72 | if (NULL == addr) { |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 73 | pixelRef->unlockPixels(); |
John Reck | ae2e8b4 | 2015-05-06 14:55:05 -0700 | [diff] [blame] | 74 | pixelRef->unref(); |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 75 | return ANDROID_BITMAP_RESULT_ALLOCATION_FAILED; |
| 76 | } |
| 77 | |
| 78 | if (addrPtr) { |
| 79 | *addrPtr = addr; |
| 80 | } |
Andrew Hsieh | eba8254 | 2012-12-12 11:27:44 +0800 | [diff] [blame] | 81 | return ANDROID_BITMAP_RESULT_SUCCESS; |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | int AndroidBitmap_unlockPixels(JNIEnv* env, jobject jbitmap) { |
| 85 | if (NULL == env || NULL == jbitmap) { |
| 86 | return ANDROID_BITMAP_RESULT_BAD_PARAMETER; |
| 87 | } |
| 88 | |
John Reck | ae2e8b4 | 2015-05-06 14:55:05 -0700 | [diff] [blame] | 89 | SkPixelRef* pixelRef = GraphicsJNI::refSkPixelRef(env, jbitmap); |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 90 | if (!pixelRef) { |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 91 | return ANDROID_BITMAP_RESULT_JNI_EXCEPTION; |
| 92 | } |
| 93 | |
Alexandre Elias | 17e5f4c | 2011-07-12 17:58:23 -0700 | [diff] [blame] | 94 | // notifyPixelsChanged() needs be called to apply writes to GL-backed |
| 95 | // bitmaps. Note that this will slow down read-only accesses to the |
| 96 | // bitmaps, but the NDK methods are primarily intended to be used for |
| 97 | // writes. |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 98 | pixelRef->notifyPixelsChanged(); |
Alexandre Elias | 17e5f4c | 2011-07-12 17:58:23 -0700 | [diff] [blame] | 99 | |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 100 | pixelRef->unlockPixels(); |
John Reck | ae2e8b4 | 2015-05-06 14:55:05 -0700 | [diff] [blame] | 101 | // Awkward in that we need to double-unref as the call to get the SkPixelRef |
| 102 | // did a ref(), so we need to unref() for the local ref and for the previous |
| 103 | // AndroidBitmap_lockPixels(). However this keeps GraphicsJNI a bit safer |
| 104 | // if others start using it without knowing about android::Bitmap's "fun" |
| 105 | // ref counting mechanism(s). |
| 106 | pixelRef->unref(); |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame] | 107 | pixelRef->unref(); |
| 108 | |
Andrew Hsieh | eba8254 | 2012-12-12 11:27:44 +0800 | [diff] [blame] | 109 | return ANDROID_BITMAP_RESULT_SUCCESS; |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 110 | } |
| 111 | |