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>
18#include <GraphicsJNI.h>
19
20int AndroidBitmap_getInfo(JNIEnv* env, jobject jbitmap,
21                          AndroidBitmapInfo* info) {
22    if (NULL == env || NULL == jbitmap) {
23        return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
24    }
25
26    SkBitmap* bm = GraphicsJNI::getNativeBitmap(env, jbitmap);
27    if (NULL == bm) {
28        return ANDROID_BITMAP_RESULT_JNI_EXCEPTION;
29    }
30
31    if (info) {
32        info->width     = bm->width();
33        info->height    = bm->height();
34        info->stride    = bm->rowBytes();
35        info->flags     = 0;
36
37        switch (bm->config()) {
38            case SkBitmap::kARGB_8888_Config:
39                info->format = ANDROID_BITMAP_FORMAT_RGBA_8888;
40                break;
41            case SkBitmap::kRGB_565_Config:
42                info->format = ANDROID_BITMAP_FORMAT_RGB_565;
43                break;
44            case SkBitmap::kARGB_4444_Config:
45                info->format = ANDROID_BITMAP_FORMAT_RGBA_4444;
46                break;
47            case SkBitmap::kA8_Config:
48                info->format = ANDROID_BITMAP_FORMAT_A_8;
49                break;
50            default:
51                info->format = ANDROID_BITMAP_FORMAT_NONE;
52                break;
53        }
54    }
55    return ANDROID_BITMAP_RESUT_SUCCESS;
56}
57
58int AndroidBitmap_lockPixels(JNIEnv* env, jobject jbitmap, void** addrPtr) {
59    if (NULL == env || NULL == jbitmap) {
60        return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
61    }
62
63    SkBitmap* bm = GraphicsJNI::getNativeBitmap(env, jbitmap);
64    if (NULL == bm) {
65        return ANDROID_BITMAP_RESULT_JNI_EXCEPTION;
66    }
67
68    bm->lockPixels();
69    void* addr = bm->getPixels();
70    if (NULL == addr) {
71        bm->unlockPixels();
72        return ANDROID_BITMAP_RESULT_ALLOCATION_FAILED;
73    }
74
75    if (addrPtr) {
76        *addrPtr = addr;
77    }
78    return ANDROID_BITMAP_RESUT_SUCCESS;
79}
80
81int AndroidBitmap_unlockPixels(JNIEnv* env, jobject jbitmap) {
82    if (NULL == env || NULL == jbitmap) {
83        return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
84    }
85
86    SkBitmap* bm = GraphicsJNI::getNativeBitmap(env, jbitmap);
87    if (NULL == bm) {
88        return ANDROID_BITMAP_RESULT_JNI_EXCEPTION;
89    }
90
91    // notifyPixelsChanged() needs be called to apply writes to GL-backed
92    // bitmaps.  Note that this will slow down read-only accesses to the
93    // bitmaps, but the NDK methods are primarily intended to be used for
94    // writes.
95    bm->notifyPixelsChanged();
96
97    bm->unlockPixels();
98    return ANDROID_BITMAP_RESUT_SUCCESS;
99}
100
101