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 | f4faeac | 2015-03-05 13:50:31 -0800 | [diff] [blame^] | 30 | SkBitmap* bm = GraphicsJNI::getSkBitmap(env, jbitmap); |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 31 | if (NULL == bm) { |
| 32 | return ANDROID_BITMAP_RESULT_JNI_EXCEPTION; |
| 33 | } |
| 34 | |
| 35 | if (info) { |
| 36 | info->width = bm->width(); |
| 37 | info->height = bm->height(); |
| 38 | info->stride = bm->rowBytes(); |
| 39 | info->flags = 0; |
| 40 | |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 41 | switch (bm->colorType()) { |
| 42 | case kN32_SkColorType: |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 43 | info->format = ANDROID_BITMAP_FORMAT_RGBA_8888; |
| 44 | break; |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 45 | case kRGB_565_SkColorType: |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 46 | info->format = ANDROID_BITMAP_FORMAT_RGB_565; |
| 47 | break; |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 48 | case kARGB_4444_SkColorType: |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 49 | info->format = ANDROID_BITMAP_FORMAT_RGBA_4444; |
| 50 | break; |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 51 | case kAlpha_8_SkColorType: |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 52 | info->format = ANDROID_BITMAP_FORMAT_A_8; |
| 53 | break; |
| 54 | default: |
| 55 | info->format = ANDROID_BITMAP_FORMAT_NONE; |
| 56 | break; |
| 57 | } |
| 58 | } |
Andrew Hsieh | eba8254 | 2012-12-12 11:27:44 +0800 | [diff] [blame] | 59 | return ANDROID_BITMAP_RESULT_SUCCESS; |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int AndroidBitmap_lockPixels(JNIEnv* env, jobject jbitmap, void** addrPtr) { |
| 63 | if (NULL == env || NULL == jbitmap) { |
| 64 | return ANDROID_BITMAP_RESULT_BAD_PARAMETER; |
| 65 | } |
| 66 | |
John Reck | f4faeac | 2015-03-05 13:50:31 -0800 | [diff] [blame^] | 67 | SkBitmap* bm = GraphicsJNI::getSkBitmap(env, jbitmap); |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 68 | if (NULL == bm) { |
| 69 | return ANDROID_BITMAP_RESULT_JNI_EXCEPTION; |
| 70 | } |
| 71 | |
| 72 | bm->lockPixels(); |
| 73 | void* addr = bm->getPixels(); |
| 74 | if (NULL == addr) { |
| 75 | bm->unlockPixels(); |
| 76 | return ANDROID_BITMAP_RESULT_ALLOCATION_FAILED; |
| 77 | } |
| 78 | |
| 79 | if (addrPtr) { |
| 80 | *addrPtr = addr; |
| 81 | } |
Andrew Hsieh | eba8254 | 2012-12-12 11:27:44 +0800 | [diff] [blame] | 82 | return ANDROID_BITMAP_RESULT_SUCCESS; |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | int AndroidBitmap_unlockPixels(JNIEnv* env, jobject jbitmap) { |
| 86 | if (NULL == env || NULL == jbitmap) { |
| 87 | return ANDROID_BITMAP_RESULT_BAD_PARAMETER; |
| 88 | } |
| 89 | |
John Reck | f4faeac | 2015-03-05 13:50:31 -0800 | [diff] [blame^] | 90 | SkBitmap* bm = GraphicsJNI::getSkBitmap(env, jbitmap); |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 91 | if (NULL == bm) { |
| 92 | return ANDROID_BITMAP_RESULT_JNI_EXCEPTION; |
| 93 | } |
| 94 | |
Alexandre Elias | 17e5f4c | 2011-07-12 17:58:23 -0700 | [diff] [blame] | 95 | // notifyPixelsChanged() needs be called to apply writes to GL-backed |
| 96 | // bitmaps. Note that this will slow down read-only accesses to the |
| 97 | // bitmaps, but the NDK methods are primarily intended to be used for |
| 98 | // writes. |
| 99 | bm->notifyPixelsChanged(); |
| 100 | |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 101 | bm->unlockPixels(); |
Andrew Hsieh | eba8254 | 2012-12-12 11:27:44 +0800 | [diff] [blame] | 102 | return ANDROID_BITMAP_RESULT_SUCCESS; |
Dima Zavin | 3227631 | 2010-02-04 12:15:09 -0800 | [diff] [blame] | 103 | } |
| 104 | |