13227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin/*
23227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin * Copyright (C) 2009 The Android Open Source Project
33227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin *
43227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin * Licensed under the Apache License, Version 2.0 (the "License");
53227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin * you may not use this file except in compliance with the License.
63227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin * You may obtain a copy of the License at
73227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin *
83227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin *      http://www.apache.org/licenses/LICENSE-2.0
93227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin *
103227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin * Unless required by applicable law or agreed to in writing, software
113227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin * distributed under the License is distributed on an "AS IS" BASIS,
123227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin * See the License for the specific language governing permissions and
143227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin * limitations under the License.
153227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin */
163227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin
173227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin#include <android/bitmap.h>
183227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin#include <GraphicsJNI.h>
193227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin
203227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavinint AndroidBitmap_getInfo(JNIEnv* env, jobject jbitmap,
213227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin                          AndroidBitmapInfo* info) {
223227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    if (NULL == env || NULL == jbitmap) {
233227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin        return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
243227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    }
253227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin
263227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    SkBitmap* bm = GraphicsJNI::getNativeBitmap(env, jbitmap);
273227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    if (NULL == bm) {
283227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin        return ANDROID_BITMAP_RESULT_JNI_EXCEPTION;
293227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    }
303227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin
313227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    if (info) {
323227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin        info->width     = bm->width();
333227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin        info->height    = bm->height();
343227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin        info->stride    = bm->rowBytes();
353227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin        info->flags     = 0;
363227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin
371103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed        switch (bm->colorType()) {
381103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed            case kN32_SkColorType:
393227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin                info->format = ANDROID_BITMAP_FORMAT_RGBA_8888;
403227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin                break;
411103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed            case kRGB_565_SkColorType:
423227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin                info->format = ANDROID_BITMAP_FORMAT_RGB_565;
433227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin                break;
441103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed            case kARGB_4444_SkColorType:
453227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin                info->format = ANDROID_BITMAP_FORMAT_RGBA_4444;
463227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin                break;
471103b3255945d2eb2fa9c191e84e2270b343cca9Mike Reed            case kAlpha_8_SkColorType:
483227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin                info->format = ANDROID_BITMAP_FORMAT_A_8;
493227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin                break;
503227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin            default:
513227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin                info->format = ANDROID_BITMAP_FORMAT_NONE;
523227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin                break;
533227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin        }
543227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    }
55eba8254c8cae92d653c5203f65cb36f3fc724f9dAndrew Hsieh    return ANDROID_BITMAP_RESULT_SUCCESS;
563227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin}
573227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin
583227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavinint AndroidBitmap_lockPixels(JNIEnv* env, jobject jbitmap, void** addrPtr) {
593227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    if (NULL == env || NULL == jbitmap) {
603227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin        return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
613227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    }
623227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin
633227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    SkBitmap* bm = GraphicsJNI::getNativeBitmap(env, jbitmap);
643227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    if (NULL == bm) {
653227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin        return ANDROID_BITMAP_RESULT_JNI_EXCEPTION;
663227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    }
673227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin
683227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    bm->lockPixels();
693227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    void* addr = bm->getPixels();
703227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    if (NULL == addr) {
713227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin        bm->unlockPixels();
723227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin        return ANDROID_BITMAP_RESULT_ALLOCATION_FAILED;
733227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    }
743227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin
753227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    if (addrPtr) {
763227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin        *addrPtr = addr;
773227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    }
78eba8254c8cae92d653c5203f65cb36f3fc724f9dAndrew Hsieh    return ANDROID_BITMAP_RESULT_SUCCESS;
793227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin}
803227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin
813227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavinint AndroidBitmap_unlockPixels(JNIEnv* env, jobject jbitmap) {
823227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    if (NULL == env || NULL == jbitmap) {
833227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin        return ANDROID_BITMAP_RESULT_BAD_PARAMETER;
843227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    }
853227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin
863227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    SkBitmap* bm = GraphicsJNI::getNativeBitmap(env, jbitmap);
873227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    if (NULL == bm) {
883227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin        return ANDROID_BITMAP_RESULT_JNI_EXCEPTION;
893227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    }
903227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin
9117e5f4cc706bed08367af8fa60ffb8c7c3ca7f62Alexandre Elias    // notifyPixelsChanged() needs be called to apply writes to GL-backed
9217e5f4cc706bed08367af8fa60ffb8c7c3ca7f62Alexandre Elias    // bitmaps.  Note that this will slow down read-only accesses to the
9317e5f4cc706bed08367af8fa60ffb8c7c3ca7f62Alexandre Elias    // bitmaps, but the NDK methods are primarily intended to be used for
9417e5f4cc706bed08367af8fa60ffb8c7c3ca7f62Alexandre Elias    // writes.
9517e5f4cc706bed08367af8fa60ffb8c7c3ca7f62Alexandre Elias    bm->notifyPixelsChanged();
9617e5f4cc706bed08367af8fa60ffb8c7c3ca7f62Alexandre Elias
973227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin    bm->unlockPixels();
98eba8254c8cae92d653c5203f65cb36f3fc724f9dAndrew Hsieh    return ANDROID_BITMAP_RESULT_SUCCESS;
993227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin}
1003227631fe99c271a47f9f5135c4ffe1bec3e72f0Dima Zavin
101