1 /*
2 * Copyright (C) 2014 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/surface_texture_jni.h>
18 #include "graphics_jni_helpers.h"
19
20 #include <hwui/Paint.h>
21 #include <SkMatrix.h>
22 #include <DeferredLayerUpdater.h>
23
24 namespace android {
25
26 using namespace uirenderer;
27
TextureLayer_prepare(JNIEnv * env,jobject clazz,jlong layerUpdaterPtr,jint width,jint height,jboolean isOpaque)28 static jboolean TextureLayer_prepare(JNIEnv* env, jobject clazz,
29 jlong layerUpdaterPtr, jint width, jint height, jboolean isOpaque) {
30 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
31 bool changed = false;
32 changed |= layer->setSize(width, height);
33 changed |= layer->setBlend(!isOpaque);
34 return changed;
35 }
36
TextureLayer_setLayerPaint(JNIEnv * env,jobject clazz,jlong layerUpdaterPtr,jlong paintPtr)37 static void TextureLayer_setLayerPaint(JNIEnv* env, jobject clazz,
38 jlong layerUpdaterPtr, jlong paintPtr) {
39 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
40 if (layer) {
41 Paint* paint = reinterpret_cast<Paint*>(paintPtr);
42 layer->setPaint(paint);
43 }
44 }
45
TextureLayer_setTransform(JNIEnv * env,jobject clazz,jlong layerUpdaterPtr,jlong matrixPtr)46 static void TextureLayer_setTransform(JNIEnv* env, jobject clazz,
47 jlong layerUpdaterPtr, jlong matrixPtr) {
48 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
49 SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixPtr);
50 layer->setTransform(matrix);
51 }
52
TextureLayer_setSurfaceTexture(JNIEnv * env,jobject clazz,jlong layerUpdaterPtr,jobject surface)53 static void TextureLayer_setSurfaceTexture(JNIEnv* env, jobject clazz,
54 jlong layerUpdaterPtr, jobject surface) {
55 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
56 ASurfaceTexture* surfaceTexture = ASurfaceTexture_fromSurfaceTexture(env, surface);
57 layer->setSurfaceTexture(AutoTextureRelease(surfaceTexture, &ASurfaceTexture_release));
58 }
59
TextureLayer_updateSurfaceTexture(JNIEnv * env,jobject clazz,jlong layerUpdaterPtr)60 static void TextureLayer_updateSurfaceTexture(JNIEnv* env, jobject clazz,
61 jlong layerUpdaterPtr) {
62 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
63 layer->updateTexImage();
64 }
65
66 // ----------------------------------------------------------------------------
67 // JNI Glue
68 // ----------------------------------------------------------------------------
69
70 const char* const kClassPathName = "android/graphics/TextureLayer";
71
72 static const JNINativeMethod gMethods[] = {
73 { "nPrepare", "(JIIZ)Z", (void*) TextureLayer_prepare },
74 { "nSetLayerPaint", "(JJ)V", (void*) TextureLayer_setLayerPaint },
75 { "nSetTransform", "(JJ)V", (void*) TextureLayer_setTransform },
76 { "nSetSurfaceTexture", "(JLandroid/graphics/SurfaceTexture;)V",
77 (void*) TextureLayer_setSurfaceTexture },
78 { "nUpdateSurfaceTexture", "(J)V", (void*) TextureLayer_updateSurfaceTexture },
79 };
80
register_android_graphics_TextureLayer(JNIEnv * env)81 int register_android_graphics_TextureLayer(JNIEnv* env) {
82 return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
83 }
84
85 };
86