blob: c2bd0b3c428a40d405c82c2d63d664caef1ea316 [file] [log] [blame]
Romain Guy8f0095c2011-05-02 17:24:22 -07001/*
2 * Copyright (C) 2011 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 "jni.h"
18#include <nativehelper/JNIHelp.h>
19#include <android_runtime/AndroidRuntime.h>
Romain Guy035f10102011-06-15 17:57:28 -070020#include <android_runtime/android_graphics_SurfaceTexture.h>
Romain Guy8f0095c2011-05-02 17:24:22 -070021
Romain Guy6be3d552011-07-14 18:08:37 -070022#include <ui/Region.h>
23#include <ui/Rect.h>
24
Andy McFaddend47f7d82012-12-18 09:48:38 -080025#include <gui/GLConsumer.h>
Mathias Agopian52800612013-02-14 17:11:20 -080026#include <gui/Surface.h>
Romain Guy6be3d552011-07-14 18:08:37 -070027
28#include <SkBitmap.h>
29#include <SkCanvas.h>
Leon Scroggins III8790be62013-12-03 16:26:51 -050030#include <SkImage.h>
Romain Guy8f0095c2011-05-02 17:24:22 -070031
Florin Malita5c3d9272014-05-08 10:35:36 -040032#include "android/graphics/GraphicsJNI.h"
33
Andreas Gampe987f79f2014-11-18 17:29:46 -080034#include "core_jni_helpers.h"
35
Romain Guy8f0095c2011-05-02 17:24:22 -070036namespace android {
37
38// ----------------------------------------------------------------------------
Romain Guy6be3d552011-07-14 18:08:37 -070039// JNI Glue
40// ----------------------------------------------------------------------------
41
42static struct {
43 jmethodID set;
44 jfieldID left;
45 jfieldID top;
46 jfieldID right;
47 jfieldID bottom;
48} gRectClassInfo;
49
50static struct {
John Reck9d4efdf2015-04-17 20:45:40 +000051 jfieldID mSurfaceFormat;
52 jmethodID setNativeBitmap;
53} gCanvasClassInfo;
54
55static struct {
Romain Guy6be3d552011-07-14 18:08:37 -070056 jfieldID nativeWindow;
57} gTextureViewClassInfo;
58
59#define GET_INT(object, field) \
60 env->GetIntField(object, field)
61
Ashok Bhat36bef0b2014-01-20 20:08:01 +000062#define GET_LONG(object, field) \
63 env->GetLongField(object, field)
64
Romain Guy6be3d552011-07-14 18:08:37 -070065#define SET_INT(object, field, value) \
66 env->SetIntField(object, field, value)
67
Ashok Bhat36bef0b2014-01-20 20:08:01 +000068#define SET_LONG(object, field, value) \
69 env->SetLongField(object, field, value)
70
Romain Guy6be3d552011-07-14 18:08:37 -070071#define INVOKEV(object, method, ...) \
72 env->CallVoidMethod(object, method, __VA_ARGS__)
73
74// ----------------------------------------------------------------------------
Romain Guy8f0095c2011-05-02 17:24:22 -070075// Native layer
76// ----------------------------------------------------------------------------
77
Mike Reedb9330552014-06-16 17:31:48 -040078// FIXME: consider exporting this to share (e.g. android_view_Surface.cpp)
79static inline SkImageInfo convertPixelFormat(const ANativeWindow_Buffer& buffer) {
80 SkImageInfo info;
81 info.fWidth = buffer.width;
82 info.fHeight = buffer.height;
83 switch (buffer.format) {
Romain Guy6be3d552011-07-14 18:08:37 -070084 case WINDOW_FORMAT_RGBA_8888:
Mike Reedb9330552014-06-16 17:31:48 -040085 info.fColorType = kN32_SkColorType;
86 info.fAlphaType = kPremul_SkAlphaType;
87 break;
Romain Guy6be3d552011-07-14 18:08:37 -070088 case WINDOW_FORMAT_RGBX_8888:
Mike Reedb9330552014-06-16 17:31:48 -040089 info.fColorType = kN32_SkColorType;
90 info.fAlphaType = kOpaque_SkAlphaType;
Mike Reed73f9c7d2015-02-27 09:59:37 -050091 break;
Romain Guy6be3d552011-07-14 18:08:37 -070092 case WINDOW_FORMAT_RGB_565:
Mike Reedb9330552014-06-16 17:31:48 -040093 info.fColorType = kRGB_565_SkColorType;
94 info.fAlphaType = kOpaque_SkAlphaType;
Mike Reed73f9c7d2015-02-27 09:59:37 -050095 break;
Romain Guy6be3d552011-07-14 18:08:37 -070096 default:
Mike Reedb9330552014-06-16 17:31:48 -040097 info.fColorType = kUnknown_SkColorType;
Mike Reed73f9c7d2015-02-27 09:59:37 -050098 // switch to kUnknown_SkAlphaType when its in skia
99 info.fAlphaType = kOpaque_SkAlphaType;
Mike Reedb9330552014-06-16 17:31:48 -0400100 break;
Romain Guy6be3d552011-07-14 18:08:37 -0700101 }
Mike Reedb9330552014-06-16 17:31:48 -0400102 return info;
Romain Guy6be3d552011-07-14 18:08:37 -0700103}
104
105/**
106 * This is a private API, and this implementation is also provided in the NDK.
107 * However, the NDK links against android_runtime, which means that using the
108 * NDK implementation would create a circular dependency between the libraries.
109 */
110static int32_t native_window_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
111 Rect* inOutDirtyBounds) {
112 return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
113}
114
115static int32_t native_window_unlockAndPost(ANativeWindow* window) {
116 return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
117}
118
119static void android_view_TextureView_createNativeWindow(JNIEnv* env, jobject textureView,
120 jobject surface) {
121
Mathias Agopian52a9a102013-08-02 01:38:38 -0700122 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surface));
Mathias Agopian24f43c92013-08-06 20:16:12 -0700123 sp<ANativeWindow> window = new Surface(producer, true);
Romain Guy6be3d552011-07-14 18:08:37 -0700124
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800125 window->incStrong((void*)android_view_TextureView_createNativeWindow);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000126 SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, jlong(window.get()));
Romain Guy6be3d552011-07-14 18:08:37 -0700127}
128
129static void android_view_TextureView_destroyNativeWindow(JNIEnv* env, jobject textureView) {
130
131 ANativeWindow* nativeWindow = (ANativeWindow*)
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000132 GET_LONG(textureView, gTextureViewClassInfo.nativeWindow);
Romain Guy6be3d552011-07-14 18:08:37 -0700133
134 if (nativeWindow) {
135 sp<ANativeWindow> window(nativeWindow);
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800136 window->decStrong((void*)android_view_TextureView_createNativeWindow);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000137 SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, 0);
Romain Guy6be3d552011-07-14 18:08:37 -0700138 }
139}
140
Romain Guy53bacf52013-04-30 11:30:10 -0700141static jboolean android_view_TextureView_lockCanvas(JNIEnv* env, jobject,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000142 jlong nativeWindow, jobject canvas, jobject dirtyRect) {
Romain Guy6be3d552011-07-14 18:08:37 -0700143
144 if (!nativeWindow) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000145 return JNI_FALSE;
Romain Guy6be3d552011-07-14 18:08:37 -0700146 }
147
148 ANativeWindow_Buffer buffer;
149
150 Rect rect;
151 if (dirtyRect) {
152 rect.left = GET_INT(dirtyRect, gRectClassInfo.left);
153 rect.top = GET_INT(dirtyRect, gRectClassInfo.top);
154 rect.right = GET_INT(dirtyRect, gRectClassInfo.right);
155 rect.bottom = GET_INT(dirtyRect, gRectClassInfo.bottom);
156 } else {
157 rect.set(Rect(0x3FFF, 0x3FFF));
158 }
159
160 sp<ANativeWindow> window((ANativeWindow*) nativeWindow);
Romain Guy53bacf52013-04-30 11:30:10 -0700161 int32_t status = native_window_lock(window.get(), &buffer, &rect);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000162 if (status) return JNI_FALSE;
Romain Guy6be3d552011-07-14 18:08:37 -0700163
164 ssize_t bytesCount = buffer.stride * bytesPerPixel(buffer.format);
165
166 SkBitmap bitmap;
Mike Reedb9330552014-06-16 17:31:48 -0400167 bitmap.setInfo(convertPixelFormat(buffer), bytesCount);
Romain Guy6be3d552011-07-14 18:08:37 -0700168
169 if (buffer.width > 0 && buffer.height > 0) {
170 bitmap.setPixels(buffer.bits);
171 } else {
172 bitmap.setPixels(NULL);
173 }
174
John Reck9d4efdf2015-04-17 20:45:40 +0000175 SET_INT(canvas, gCanvasClassInfo.mSurfaceFormat, buffer.format);
176 INVOKEV(canvas, gCanvasClassInfo.setNativeBitmap, reinterpret_cast<jlong>(&bitmap));
177
178 SkRect clipRect;
179 clipRect.set(rect.left, rect.top, rect.right, rect.bottom);
180 SkCanvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvas);
181 nativeCanvas->clipRect(clipRect);
Romain Guy6be3d552011-07-14 18:08:37 -0700182
183 if (dirtyRect) {
184 INVOKEV(dirtyRect, gRectClassInfo.set,
185 int(rect.left), int(rect.top), int(rect.right), int(rect.bottom));
186 }
Romain Guy53bacf52013-04-30 11:30:10 -0700187
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000188 return JNI_TRUE;
Romain Guy6be3d552011-07-14 18:08:37 -0700189}
190
191static void android_view_TextureView_unlockCanvasAndPost(JNIEnv* env, jobject,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000192 jlong nativeWindow, jobject canvas) {
Romain Guy6be3d552011-07-14 18:08:37 -0700193
John Reck9d4efdf2015-04-17 20:45:40 +0000194 INVOKEV(canvas, gCanvasClassInfo.setNativeBitmap, (jlong)0);
Romain Guy6be3d552011-07-14 18:08:37 -0700195
196 if (nativeWindow) {
197 sp<ANativeWindow> window((ANativeWindow*) nativeWindow);
198 native_window_unlockAndPost(window.get());
199 }
200}
201
Romain Guy8f0095c2011-05-02 17:24:22 -0700202// ----------------------------------------------------------------------------
203// JNI Glue
204// ----------------------------------------------------------------------------
205
206const char* const kClassPathName = "android/view/TextureView";
207
208static JNINativeMethod gMethods[] = {
Romain Guy6be3d552011-07-14 18:08:37 -0700209 { "nCreateNativeWindow", "(Landroid/graphics/SurfaceTexture;)V",
210 (void*) android_view_TextureView_createNativeWindow },
211 { "nDestroyNativeWindow", "()V",
212 (void*) android_view_TextureView_destroyNativeWindow },
213
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000214 { "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z",
Romain Guy6be3d552011-07-14 18:08:37 -0700215 (void*) android_view_TextureView_lockCanvas },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000216 { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V",
Romain Guy6be3d552011-07-14 18:08:37 -0700217 (void*) android_view_TextureView_unlockCanvasAndPost },
Romain Guy8f0095c2011-05-02 17:24:22 -0700218};
219
220int register_android_view_TextureView(JNIEnv* env) {
Andreas Gampe987f79f2014-11-18 17:29:46 -0800221 jclass clazz = FindClassOrDie(env, "android/graphics/Rect");
222 gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V");
223 gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
224 gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
225 gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
226 gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
Romain Guy6be3d552011-07-14 18:08:37 -0700227
John Reck9d4efdf2015-04-17 20:45:40 +0000228 clazz = FindClassOrDie(env, "android/graphics/Canvas");
229 gCanvasClassInfo.mSurfaceFormat = GetFieldIDOrDie(env, clazz, "mSurfaceFormat", "I");
230 gCanvasClassInfo.setNativeBitmap = GetMethodIDOrDie(env, clazz, "setNativeBitmap", "(J)V");
231
Andreas Gampe987f79f2014-11-18 17:29:46 -0800232 clazz = FindClassOrDie(env, "android/view/TextureView");
233 gTextureViewClassInfo.nativeWindow = GetFieldIDOrDie(env, clazz, "mNativeWindow", "J");
Romain Guy6be3d552011-07-14 18:08:37 -0700234
Andreas Gampe987f79f2014-11-18 17:29:46 -0800235 return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
Romain Guy8f0095c2011-05-02 17:24:22 -0700236}
237
238};