Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | #define LOG_TAG "GraphicBuffer" |
| 18 | |
| 19 | #include "jni.h" |
| 20 | #include "JNIHelp.h" |
| 21 | |
| 22 | #include "android_os_Parcel.h" |
| 23 | #include "android_view_GraphicBuffer.h" |
Florin Malita | 5c3d927 | 2014-05-08 10:35:36 -0400 | [diff] [blame] | 24 | #include "android/graphics/GraphicsJNI.h" |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 25 | |
| 26 | #include <android_runtime/AndroidRuntime.h> |
| 27 | |
| 28 | #include <binder/Parcel.h> |
| 29 | |
| 30 | #include <ui/GraphicBuffer.h> |
| 31 | #include <ui/PixelFormat.h> |
| 32 | |
| 33 | #include <gui/IGraphicBufferAlloc.h> |
| 34 | #include <gui/ISurfaceComposer.h> |
| 35 | |
| 36 | #include <SkCanvas.h> |
| 37 | #include <SkBitmap.h> |
| 38 | |
| 39 | #include <private/gui/ComposerService.h> |
| 40 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 41 | #include "core_jni_helpers.h" |
| 42 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 43 | namespace android { |
| 44 | |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | // Defines |
| 47 | // ---------------------------------------------------------------------------- |
| 48 | |
| 49 | // Debug |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 50 | static const bool kDebugGraphicBuffer = false; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 51 | |
| 52 | #define LOCK_CANVAS_USAGE GraphicBuffer::USAGE_SW_READ_OFTEN | GraphicBuffer::USAGE_SW_WRITE_OFTEN |
| 53 | |
| 54 | // ---------------------------------------------------------------------------- |
| 55 | // JNI Helpers |
| 56 | // ---------------------------------------------------------------------------- |
| 57 | |
| 58 | static struct { |
| 59 | jfieldID mNativeObject; |
| 60 | } gGraphicBufferClassInfo; |
| 61 | |
| 62 | static struct { |
| 63 | jmethodID set; |
| 64 | jfieldID left; |
| 65 | jfieldID top; |
| 66 | jfieldID right; |
| 67 | jfieldID bottom; |
| 68 | } gRectClassInfo; |
| 69 | |
John Reck | 9d4efdf | 2015-04-17 20:45:40 +0000 | [diff] [blame] | 70 | static struct { |
| 71 | jfieldID mSurfaceFormat; |
| 72 | jmethodID setNativeBitmap; |
| 73 | } gCanvasClassInfo; |
| 74 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 75 | #define GET_INT(object, field) \ |
| 76 | env->GetIntField(object, field) |
| 77 | |
| 78 | #define SET_INT(object, field, value) \ |
| 79 | env->SetIntField(object, field, value) |
| 80 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 81 | #define GET_LONG(object, field) \ |
| 82 | env->GetLongField(object, field) |
| 83 | |
| 84 | #define SET_LONG(object, field, value) \ |
| 85 | env->SetLongField(object, field, value) |
| 86 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 87 | #define INVOKEV(object, method, ...) \ |
| 88 | env->CallVoidMethod(object, method, __VA_ARGS__) |
| 89 | |
| 90 | // ---------------------------------------------------------------------------- |
| 91 | // Types |
| 92 | // ---------------------------------------------------------------------------- |
| 93 | |
| 94 | class GraphicBufferWrapper { |
| 95 | public: |
| 96 | GraphicBufferWrapper(const sp<GraphicBuffer>& buffer): buffer(buffer) { |
| 97 | } |
| 98 | |
| 99 | sp<GraphicBuffer> buffer; |
| 100 | }; |
| 101 | |
| 102 | // ---------------------------------------------------------------------------- |
| 103 | // GraphicBuffer lifecycle |
| 104 | // ---------------------------------------------------------------------------- |
| 105 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 106 | static jlong android_view_GraphiceBuffer_create(JNIEnv* env, jobject clazz, |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 107 | jint width, jint height, jint format, jint usage) { |
| 108 | |
| 109 | sp<ISurfaceComposer> composer(ComposerService::getComposerService()); |
| 110 | sp<IGraphicBufferAlloc> alloc(composer->createGraphicBufferAlloc()); |
| 111 | if (alloc == NULL) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 112 | if (kDebugGraphicBuffer) { |
| 113 | ALOGW("createGraphicBufferAlloc() failed in GraphicBuffer.create()"); |
| 114 | } |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | status_t error; |
| 119 | sp<GraphicBuffer> buffer(alloc->createGraphicBuffer(width, height, format, usage, &error)); |
| 120 | if (buffer == NULL) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 121 | if (kDebugGraphicBuffer) { |
| 122 | ALOGW("createGraphicBuffer() failed in GraphicBuffer.create()"); |
| 123 | } |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 124 | return NULL; |
| 125 | } |
| 126 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 127 | GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer); |
| 128 | return reinterpret_cast<jlong>(wrapper); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void android_view_GraphiceBuffer_destroy(JNIEnv* env, jobject clazz, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 132 | jlong wrapperHandle) { |
| 133 | GraphicBufferWrapper* wrapper = |
| 134 | reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 135 | delete wrapper; |
| 136 | } |
| 137 | |
| 138 | // ---------------------------------------------------------------------------- |
| 139 | // Canvas management |
| 140 | // ---------------------------------------------------------------------------- |
| 141 | |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 142 | static inline SkColorType convertPixelFormat(int32_t format) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 143 | switch (format) { |
| 144 | case PIXEL_FORMAT_RGBA_8888: |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 145 | return kN32_SkColorType; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 146 | case PIXEL_FORMAT_RGBX_8888: |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 147 | return kN32_SkColorType; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 148 | case PIXEL_FORMAT_RGB_565: |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 149 | return kRGB_565_SkColorType; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 150 | default: |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 151 | return kUnknown_SkColorType; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | static jboolean android_view_GraphicBuffer_lockCanvas(JNIEnv* env, jobject, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 156 | jlong wrapperHandle, jobject canvas, jobject dirtyRect) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 157 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 158 | GraphicBufferWrapper* wrapper = |
| 159 | reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 160 | if (!wrapper) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 161 | return JNI_FALSE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | sp<GraphicBuffer> buffer(wrapper->buffer); |
| 165 | |
| 166 | Rect rect; |
| 167 | if (dirtyRect) { |
| 168 | rect.left = GET_INT(dirtyRect, gRectClassInfo.left); |
| 169 | rect.top = GET_INT(dirtyRect, gRectClassInfo.top); |
| 170 | rect.right = GET_INT(dirtyRect, gRectClassInfo.right); |
| 171 | rect.bottom = GET_INT(dirtyRect, gRectClassInfo.bottom); |
| 172 | } else { |
| 173 | rect.set(Rect(buffer->getWidth(), buffer->getHeight())); |
| 174 | } |
| 175 | |
| 176 | void* bits = NULL; |
| 177 | status_t status = buffer->lock(LOCK_CANVAS_USAGE, rect, &bits); |
| 178 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 179 | if (status) return JNI_FALSE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 180 | if (!bits) { |
| 181 | buffer->unlock(); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 182 | return JNI_FALSE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | ssize_t bytesCount = buffer->getStride() * bytesPerPixel(buffer->getPixelFormat()); |
| 186 | |
| 187 | SkBitmap bitmap; |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 188 | bitmap.setInfo(SkImageInfo::Make(buffer->getWidth(), buffer->getHeight(), |
| 189 | convertPixelFormat(buffer->getPixelFormat()), |
| 190 | kPremul_SkAlphaType), |
| 191 | bytesCount); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 192 | |
| 193 | if (buffer->getWidth() > 0 && buffer->getHeight() > 0) { |
| 194 | bitmap.setPixels(bits); |
| 195 | } else { |
| 196 | bitmap.setPixels(NULL); |
| 197 | } |
| 198 | |
John Reck | 9d4efdf | 2015-04-17 20:45:40 +0000 | [diff] [blame] | 199 | SET_INT(canvas, gCanvasClassInfo.mSurfaceFormat, buffer->getPixelFormat()); |
| 200 | INVOKEV(canvas, gCanvasClassInfo.setNativeBitmap, reinterpret_cast<jlong>(&bitmap)); |
| 201 | |
| 202 | SkRect clipRect; |
| 203 | clipRect.set(rect.left, rect.top, rect.right, rect.bottom); |
| 204 | SkCanvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvas); |
| 205 | nativeCanvas->clipRect(clipRect); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 206 | |
| 207 | if (dirtyRect) { |
| 208 | INVOKEV(dirtyRect, gRectClassInfo.set, |
| 209 | int(rect.left), int(rect.top), int(rect.right), int(rect.bottom)); |
| 210 | } |
| 211 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 212 | return JNI_TRUE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static jboolean android_view_GraphicBuffer_unlockCanvasAndPost(JNIEnv* env, jobject, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 216 | jlong wrapperHandle, jobject canvas) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 217 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 218 | GraphicBufferWrapper* wrapper = |
| 219 | reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle); |
John Reck | 9d4efdf | 2015-04-17 20:45:40 +0000 | [diff] [blame] | 220 | INVOKEV(canvas, gCanvasClassInfo.setNativeBitmap, (jlong)0); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 221 | |
| 222 | if (wrapper) { |
| 223 | status_t status = wrapper->buffer->unlock(); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 224 | return status == 0 ? JNI_TRUE : JNI_FALSE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 227 | return JNI_FALSE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // ---------------------------------------------------------------------------- |
| 231 | // Serialization |
| 232 | // ---------------------------------------------------------------------------- |
| 233 | |
| 234 | static void android_view_GraphiceBuffer_write(JNIEnv* env, jobject clazz, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 235 | jlong wrapperHandle, jobject dest) { |
| 236 | GraphicBufferWrapper* wrapper = |
| 237 | reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 238 | Parcel* parcel = parcelForJavaObject(env, dest); |
| 239 | if (parcel) { |
| 240 | parcel->write(*wrapper->buffer); |
| 241 | } |
| 242 | } |
| 243 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 244 | static jlong android_view_GraphiceBuffer_read(JNIEnv* env, jobject clazz, |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 245 | jobject in) { |
| 246 | |
| 247 | Parcel* parcel = parcelForJavaObject(env, in); |
| 248 | if (parcel) { |
| 249 | sp<GraphicBuffer> buffer = new GraphicBuffer(); |
| 250 | parcel->read(*buffer); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 251 | return reinterpret_cast<jlong>(new GraphicBufferWrapper(buffer)); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | // ---------------------------------------------------------------------------- |
| 258 | // External helpers |
| 259 | // ---------------------------------------------------------------------------- |
| 260 | |
| 261 | sp<GraphicBuffer> graphicBufferForJavaObject(JNIEnv* env, jobject obj) { |
| 262 | if (obj) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 263 | jlong nativeObject = env->GetLongField(obj, gGraphicBufferClassInfo.mNativeObject); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 264 | GraphicBufferWrapper* wrapper = (GraphicBufferWrapper*) nativeObject; |
| 265 | if (wrapper != NULL) { |
| 266 | sp<GraphicBuffer> buffer(wrapper->buffer); |
| 267 | return buffer; |
| 268 | } |
| 269 | } |
| 270 | return NULL; |
| 271 | } |
| 272 | |
| 273 | // ---------------------------------------------------------------------------- |
| 274 | // JNI Glue |
| 275 | // ---------------------------------------------------------------------------- |
| 276 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 277 | const char* const kClassPathName = "android/view/GraphicBuffer"; |
| 278 | |
| 279 | static JNINativeMethod gMethods[] = { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 280 | { "nCreateGraphicBuffer", "(IIII)J", (void*) android_view_GraphiceBuffer_create }, |
| 281 | { "nDestroyGraphicBuffer", "(J)V", (void*) android_view_GraphiceBuffer_destroy }, |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 282 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 283 | { "nWriteGraphicBufferToParcel", "(JLandroid/os/Parcel;)V", |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 284 | (void*) android_view_GraphiceBuffer_write }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 285 | { "nReadGraphicBufferFromParcel", "(Landroid/os/Parcel;)J", |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 286 | (void*) android_view_GraphiceBuffer_read }, |
| 287 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 288 | { "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z", |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 289 | (void*) android_view_GraphicBuffer_lockCanvas }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 290 | { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)Z", |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 291 | (void*) android_view_GraphicBuffer_unlockCanvasAndPost }, |
| 292 | }; |
| 293 | |
| 294 | int register_android_view_GraphicBuffer(JNIEnv* env) { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 295 | jclass clazz = FindClassOrDie(env, "android/view/GraphicBuffer"); |
| 296 | gGraphicBufferClassInfo.mNativeObject = GetFieldIDOrDie(env, clazz, "mNativeObject", "J"); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 297 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 298 | clazz = FindClassOrDie(env, "android/graphics/Rect"); |
| 299 | gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V"); |
| 300 | gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I"); |
| 301 | gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I"); |
| 302 | gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I"); |
| 303 | gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I"); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 304 | |
John Reck | 9d4efdf | 2015-04-17 20:45:40 +0000 | [diff] [blame] | 305 | clazz = FindClassOrDie(env, "android/graphics/Canvas"); |
| 306 | gCanvasClassInfo.mSurfaceFormat = GetFieldIDOrDie(env, clazz, "mSurfaceFormat", "I"); |
| 307 | gCanvasClassInfo.setNativeBitmap = GetMethodIDOrDie(env, clazz, "setNativeBitmap", "(J)V"); |
| 308 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 309 | return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | }; |