blob: 2921b3789e40e80c1462ffed0d403139a37f5f45 [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 {
Romain Guy6be3d552011-07-14 18:08:37 -070051 jfieldID nativeWindow;
52} gTextureViewClassInfo;
53
54#define GET_INT(object, field) \
55 env->GetIntField(object, field)
56
Ashok Bhat36bef0b2014-01-20 20:08:01 +000057#define GET_LONG(object, field) \
58 env->GetLongField(object, field)
59
Romain Guy6be3d552011-07-14 18:08:37 -070060#define SET_INT(object, field, value) \
61 env->SetIntField(object, field, value)
62
Ashok Bhat36bef0b2014-01-20 20:08:01 +000063#define SET_LONG(object, field, value) \
64 env->SetLongField(object, field, value)
65
Romain Guy6be3d552011-07-14 18:08:37 -070066#define INVOKEV(object, method, ...) \
67 env->CallVoidMethod(object, method, __VA_ARGS__)
68
69// ----------------------------------------------------------------------------
Romain Guy8f0095c2011-05-02 17:24:22 -070070// Native layer
71// ----------------------------------------------------------------------------
72
Mike Reedb9330552014-06-16 17:31:48 -040073// FIXME: consider exporting this to share (e.g. android_view_Surface.cpp)
74static inline SkImageInfo convertPixelFormat(const ANativeWindow_Buffer& buffer) {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -040075 SkColorType colorType = kUnknown_SkColorType;
76 SkAlphaType alphaType = kOpaque_SkAlphaType;
Mike Reedb9330552014-06-16 17:31:48 -040077 switch (buffer.format) {
Romain Guy6be3d552011-07-14 18:08:37 -070078 case WINDOW_FORMAT_RGBA_8888:
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -040079 colorType = kN32_SkColorType;
80 alphaType = kPremul_SkAlphaType;
Mike Reedb9330552014-06-16 17:31:48 -040081 break;
Romain Guy6be3d552011-07-14 18:08:37 -070082 case WINDOW_FORMAT_RGBX_8888:
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -040083 colorType = kN32_SkColorType;
84 alphaType = kOpaque_SkAlphaType;
Mike Reed73f9c7d2015-02-27 09:59:37 -050085 break;
Jesse Hall7e7d0d52017-04-19 15:49:33 -070086 case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
Romain Guy9505a652016-12-14 09:43:50 -080087 colorType = kRGBA_F16_SkColorType;
88 alphaType = kPremul_SkAlphaType;
89 break;
Romain Guy6be3d552011-07-14 18:08:37 -070090 case WINDOW_FORMAT_RGB_565:
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -040091 colorType = kRGB_565_SkColorType;
92 alphaType = kOpaque_SkAlphaType;
Mike Reed73f9c7d2015-02-27 09:59:37 -050093 break;
Romain Guy6be3d552011-07-14 18:08:37 -070094 default:
Mike Reedb9330552014-06-16 17:31:48 -040095 break;
Romain Guy6be3d552011-07-14 18:08:37 -070096 }
Romain Guy253f2c22016-09-28 17:34:42 -070097 return SkImageInfo::Make(buffer.width, buffer.height, colorType, alphaType,
98 GraphicsJNI::defaultColorSpace());
Romain Guy6be3d552011-07-14 18:08:37 -070099}
100
101/**
102 * This is a private API, and this implementation is also provided in the NDK.
103 * However, the NDK links against android_runtime, which means that using the
104 * NDK implementation would create a circular dependency between the libraries.
105 */
106static int32_t native_window_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
107 Rect* inOutDirtyBounds) {
108 return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
109}
110
111static int32_t native_window_unlockAndPost(ANativeWindow* window) {
112 return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
113}
114
115static void android_view_TextureView_createNativeWindow(JNIEnv* env, jobject textureView,
116 jobject surface) {
117
Mathias Agopian52a9a102013-08-02 01:38:38 -0700118 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surface));
Mathias Agopian24f43c92013-08-06 20:16:12 -0700119 sp<ANativeWindow> window = new Surface(producer, true);
Romain Guy6be3d552011-07-14 18:08:37 -0700120
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800121 window->incStrong((void*)android_view_TextureView_createNativeWindow);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000122 SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, jlong(window.get()));
Romain Guy6be3d552011-07-14 18:08:37 -0700123}
124
125static void android_view_TextureView_destroyNativeWindow(JNIEnv* env, jobject textureView) {
126
127 ANativeWindow* nativeWindow = (ANativeWindow*)
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000128 GET_LONG(textureView, gTextureViewClassInfo.nativeWindow);
Romain Guy6be3d552011-07-14 18:08:37 -0700129
130 if (nativeWindow) {
131 sp<ANativeWindow> window(nativeWindow);
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800132 window->decStrong((void*)android_view_TextureView_createNativeWindow);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000133 SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, 0);
Romain Guy6be3d552011-07-14 18:08:37 -0700134 }
135}
136
Romain Guy53bacf52013-04-30 11:30:10 -0700137static jboolean android_view_TextureView_lockCanvas(JNIEnv* env, jobject,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000138 jlong nativeWindow, jobject canvas, jobject dirtyRect) {
Romain Guy6be3d552011-07-14 18:08:37 -0700139
140 if (!nativeWindow) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000141 return JNI_FALSE;
Romain Guy6be3d552011-07-14 18:08:37 -0700142 }
143
144 ANativeWindow_Buffer buffer;
145
Pablo Ceballos82702922015-08-07 17:28:03 -0700146 Rect rect(Rect::EMPTY_RECT);
Romain Guy6be3d552011-07-14 18:08:37 -0700147 if (dirtyRect) {
148 rect.left = GET_INT(dirtyRect, gRectClassInfo.left);
149 rect.top = GET_INT(dirtyRect, gRectClassInfo.top);
150 rect.right = GET_INT(dirtyRect, gRectClassInfo.right);
151 rect.bottom = GET_INT(dirtyRect, gRectClassInfo.bottom);
152 } else {
153 rect.set(Rect(0x3FFF, 0x3FFF));
154 }
155
156 sp<ANativeWindow> window((ANativeWindow*) nativeWindow);
Romain Guy53bacf52013-04-30 11:30:10 -0700157 int32_t status = native_window_lock(window.get(), &buffer, &rect);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000158 if (status) return JNI_FALSE;
Romain Guy6be3d552011-07-14 18:08:37 -0700159
160 ssize_t bytesCount = buffer.stride * bytesPerPixel(buffer.format);
161
162 SkBitmap bitmap;
Mike Reedb9330552014-06-16 17:31:48 -0400163 bitmap.setInfo(convertPixelFormat(buffer), bytesCount);
Romain Guy6be3d552011-07-14 18:08:37 -0700164
165 if (buffer.width > 0 && buffer.height > 0) {
166 bitmap.setPixels(buffer.bits);
167 } else {
168 bitmap.setPixels(NULL);
169 }
170
John Reckc1b33d62015-04-22 09:04:45 -0700171 Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvas);
172 nativeCanvas->setBitmap(bitmap);
Mike Reed6e49c9f2016-12-02 15:36:59 -0500173 nativeCanvas->clipRect(rect.left, rect.top, rect.right, rect.bottom,
Mike Reed6c67f1d2016-12-14 10:29:54 -0500174 SkClipOp::kIntersect);
Romain Guy6be3d552011-07-14 18:08:37 -0700175
176 if (dirtyRect) {
177 INVOKEV(dirtyRect, gRectClassInfo.set,
178 int(rect.left), int(rect.top), int(rect.right), int(rect.bottom));
179 }
Romain Guy53bacf52013-04-30 11:30:10 -0700180
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000181 return JNI_TRUE;
Romain Guy6be3d552011-07-14 18:08:37 -0700182}
183
184static void android_view_TextureView_unlockCanvasAndPost(JNIEnv* env, jobject,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000185 jlong nativeWindow, jobject canvas) {
Romain Guy6be3d552011-07-14 18:08:37 -0700186
John Reckc1b33d62015-04-22 09:04:45 -0700187 Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvas);
188 nativeCanvas->setBitmap(SkBitmap());
Romain Guy6be3d552011-07-14 18:08:37 -0700189
190 if (nativeWindow) {
191 sp<ANativeWindow> window((ANativeWindow*) nativeWindow);
192 native_window_unlockAndPost(window.get());
193 }
194}
195
Romain Guy8f0095c2011-05-02 17:24:22 -0700196// ----------------------------------------------------------------------------
197// JNI Glue
198// ----------------------------------------------------------------------------
199
200const char* const kClassPathName = "android/view/TextureView";
201
Daniel Micay76f6a862015-09-19 17:31:01 -0400202static const JNINativeMethod gMethods[] = {
Romain Guy6be3d552011-07-14 18:08:37 -0700203 { "nCreateNativeWindow", "(Landroid/graphics/SurfaceTexture;)V",
204 (void*) android_view_TextureView_createNativeWindow },
205 { "nDestroyNativeWindow", "()V",
206 (void*) android_view_TextureView_destroyNativeWindow },
207
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000208 { "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z",
Romain Guy6be3d552011-07-14 18:08:37 -0700209 (void*) android_view_TextureView_lockCanvas },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000210 { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V",
Romain Guy6be3d552011-07-14 18:08:37 -0700211 (void*) android_view_TextureView_unlockCanvasAndPost },
Romain Guy8f0095c2011-05-02 17:24:22 -0700212};
213
214int register_android_view_TextureView(JNIEnv* env) {
Andreas Gampe987f79f2014-11-18 17:29:46 -0800215 jclass clazz = FindClassOrDie(env, "android/graphics/Rect");
216 gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V");
217 gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
218 gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
219 gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
220 gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
Romain Guy6be3d552011-07-14 18:08:37 -0700221
Andreas Gampe987f79f2014-11-18 17:29:46 -0800222 clazz = FindClassOrDie(env, "android/view/TextureView");
223 gTextureViewClassInfo.nativeWindow = GetFieldIDOrDie(env, clazz, "mNativeWindow", "J");
Romain Guy6be3d552011-07-14 18:08:37 -0700224
Andreas Gampe987f79f2014-11-18 17:29:46 -0800225 return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
Romain Guy8f0095c2011-05-02 17:24:22 -0700226}
227
228};