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 #define LOG_TAG "OpenGLRenderer"
18 
19 #include "jni.h"
20 #include "GraphicsJNI.h"
21 #include <nativehelper/JNIHelp.h>
22 
23 #include "core_jni_helpers.h"
24 #include <android_runtime/android_graphics_SurfaceTexture.h>
25 
26 #include <gui/GLConsumer.h>
27 #include <hwui/Paint.h>
28 
29 #include <SkBitmap.h>
30 #include <SkCanvas.h>
31 #include <SkMatrix.h>
32 #include <SkBlendMode.h>
33 
34 #include <DeferredLayerUpdater.h>
35 #include <Rect.h>
36 #include <RenderNode.h>
37 
38 namespace android {
39 
40 using namespace uirenderer;
41 
TextureLayer_prepare(JNIEnv * env,jobject clazz,jlong layerUpdaterPtr,jint width,jint height,jboolean isOpaque)42 static jboolean TextureLayer_prepare(JNIEnv* env, jobject clazz,
43         jlong layerUpdaterPtr, jint width, jint height, jboolean isOpaque) {
44     DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
45     bool changed = false;
46     changed |= layer->setSize(width, height);
47     changed |= layer->setBlend(!isOpaque);
48     return changed;
49 }
50 
TextureLayer_setLayerPaint(JNIEnv * env,jobject clazz,jlong layerUpdaterPtr,jlong paintPtr)51 static void TextureLayer_setLayerPaint(JNIEnv* env, jobject clazz,
52         jlong layerUpdaterPtr, jlong paintPtr) {
53     DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
54     if (layer) {
55         Paint* paint = reinterpret_cast<Paint*>(paintPtr);
56         layer->setPaint(paint);
57     }
58 }
59 
TextureLayer_setTransform(JNIEnv * env,jobject clazz,jlong layerUpdaterPtr,jlong matrixPtr)60 static void TextureLayer_setTransform(JNIEnv* env, jobject clazz,
61         jlong layerUpdaterPtr, jlong matrixPtr) {
62     DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
63     SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixPtr);
64     layer->setTransform(matrix);
65 }
66 
TextureLayer_setSurfaceTexture(JNIEnv * env,jobject clazz,jlong layerUpdaterPtr,jobject surface)67 static void TextureLayer_setSurfaceTexture(JNIEnv* env, jobject clazz,
68         jlong layerUpdaterPtr, jobject surface) {
69     DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
70     layer->setSurfaceTexture(SurfaceTexture_getSurfaceTexture(env, surface));
71 }
72 
TextureLayer_updateSurfaceTexture(JNIEnv * env,jobject clazz,jlong layerUpdaterPtr)73 static void TextureLayer_updateSurfaceTexture(JNIEnv* env, jobject clazz,
74         jlong layerUpdaterPtr) {
75     DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
76     layer->updateTexImage();
77 }
78 
79 // ----------------------------------------------------------------------------
80 // JNI Glue
81 // ----------------------------------------------------------------------------
82 
83 const char* const kClassPathName = "android/view/TextureLayer";
84 
85 static const JNINativeMethod gMethods[] = {
86     { "nPrepare",                "(JIIZ)Z",    (void*) TextureLayer_prepare },
87     { "nSetLayerPaint",          "(JJ)V",      (void*) TextureLayer_setLayerPaint },
88     { "nSetTransform",           "(JJ)V",      (void*) TextureLayer_setTransform },
89     { "nSetSurfaceTexture",      "(JLandroid/graphics/SurfaceTexture;)V",
90             (void*) TextureLayer_setSurfaceTexture },
91     { "nUpdateSurfaceTexture",   "(J)V",       (void*) TextureLayer_updateSurfaceTexture },
92 };
93 
register_android_view_TextureLayer(JNIEnv * env)94 int register_android_view_TextureLayer(JNIEnv* env) {
95     return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
96 }
97 
98 };
99