android_view_HardwareLayer.cpp revision d72e0a339b54af0c4e731513bbad120dff694723
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 <android_runtime/AndroidRuntime.h>
24#include <android_runtime/android_graphics_SurfaceTexture.h>
25
26#include <gui/GLConsumer.h>
27
28#include <SkBitmap.h>
29#include <SkCanvas.h>
30#include <SkMatrix.h>
31#include <SkPaint.h>
32#include <SkXfermode.h>
33
34#include <DeferredLayerUpdater.h>
35#include <LayerRenderer.h>
36#include <SkiaShader.h>
37#include <Rect.h>
38#include <RenderNode.h>
39
40namespace android {
41
42using namespace uirenderer;
43
44#ifdef USE_OPENGL_RENDERER
45
46static jlong android_view_HardwareLayer_createTextureLayer(JNIEnv* env, jobject clazz) {
47    Layer* layer = LayerRenderer::createTextureLayer();
48    if (!layer) return 0;
49
50    return reinterpret_cast<jlong>( new DeferredLayerUpdater(layer) );
51}
52
53static jlong android_view_HardwareLayer_createRenderLayer(JNIEnv* env, jobject clazz,
54        jint width, jint height) {
55    Layer* layer = LayerRenderer::createRenderLayer(width, height);
56    if (!layer) return 0;
57
58    return reinterpret_cast<jlong>( new DeferredLayerUpdater(layer) );
59}
60
61static void android_view_HardwareLayer_onTextureDestroyed(JNIEnv* env, jobject clazz,
62        jlong layerUpdaterPtr) {
63    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
64    layer->backingLayer()->clearTexture();
65}
66
67static jboolean android_view_HardwareLayer_prepare(JNIEnv* env, jobject clazz,
68        jlong layerUpdaterPtr, jint width, jint height, jboolean isOpaque) {
69    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
70    bool changed = false;
71    changed |= layer->setSize(width, height);
72    changed |= layer->setBlend(!isOpaque);
73    return changed;
74}
75
76static void android_view_HardwareLayer_setLayerPaint(JNIEnv* env, jobject clazz,
77        jlong layerUpdaterPtr, jlong paintPtr) {
78    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
79    if (layer) {
80        SkPaint* paint = reinterpret_cast<SkPaint*>(paintPtr);
81        layer->setPaint(paint);
82    }
83}
84
85static void android_view_HardwareLayer_setTransform(JNIEnv* env, jobject clazz,
86        jlong layerUpdaterPtr, jlong matrixPtr) {
87    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
88    SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixPtr);
89    layer->setTransform(matrix);
90}
91
92static void android_view_HardwareLayer_setSurfaceTexture(JNIEnv* env, jobject clazz,
93        jlong layerUpdaterPtr, jobject surface, jboolean isAlreadyAttached) {
94    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
95    sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, surface));
96    layer->setSurfaceTexture(surfaceTexture, !isAlreadyAttached);
97}
98
99static void android_view_HardwareLayer_updateSurfaceTexture(JNIEnv* env, jobject clazz,
100        jlong layerUpdaterPtr) {
101    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
102    layer->updateTexImage();
103}
104
105static void android_view_HardwareLayer_updateRenderLayer(JNIEnv* env, jobject clazz,
106        jlong layerUpdaterPtr, jlong displayListPtr,
107        jint left, jint top, jint right, jint bottom) {
108    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
109    RenderNode* displayList = reinterpret_cast<RenderNode*>(displayListPtr);
110    layer->setDisplayList(displayList, left, top, right, bottom);
111}
112
113static jboolean android_view_HardwareLayer_flushChanges(JNIEnv* env, jobject clazz,
114        jlong layerUpdaterPtr) {
115    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
116    TreeInfo ignoredInfo;
117    return layer->apply(ignoredInfo);
118}
119
120static jlong android_view_HardwareLayer_getLayer(JNIEnv* env, jobject clazz,
121        jlong layerUpdaterPtr) {
122    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
123    return reinterpret_cast<jlong>( layer->backingLayer() );
124}
125
126static jint android_view_HardwareLayer_getTexName(JNIEnv* env, jobject clazz,
127        jlong layerUpdaterPtr) {
128    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
129    return layer->backingLayer()->getTexture();
130}
131
132#endif // USE_OPENGL_RENDERER
133
134// ----------------------------------------------------------------------------
135// JNI Glue
136// ----------------------------------------------------------------------------
137
138const char* const kClassPathName = "android/view/HardwareLayer";
139
140static JNINativeMethod gMethods[] = {
141#ifdef USE_OPENGL_RENDERER
142
143    { "nCreateTextureLayer",     "()J",        (void*) android_view_HardwareLayer_createTextureLayer },
144    { "nCreateRenderLayer",      "(II)J",      (void*) android_view_HardwareLayer_createRenderLayer },
145    { "nOnTextureDestroyed",     "(J)V",       (void*) android_view_HardwareLayer_onTextureDestroyed },
146
147    { "nPrepare",                "(JIIZ)Z",    (void*) android_view_HardwareLayer_prepare },
148    { "nSetLayerPaint",          "(JJ)V",      (void*) android_view_HardwareLayer_setLayerPaint },
149    { "nSetTransform",           "(JJ)V",      (void*) android_view_HardwareLayer_setTransform },
150    { "nSetSurfaceTexture",      "(JLandroid/graphics/SurfaceTexture;Z)V",
151            (void*) android_view_HardwareLayer_setSurfaceTexture },
152    { "nUpdateSurfaceTexture",   "(J)V",       (void*) android_view_HardwareLayer_updateSurfaceTexture },
153    { "nUpdateRenderLayer",      "(JJIIII)V",  (void*) android_view_HardwareLayer_updateRenderLayer },
154
155    { "nFlushChanges",           "(J)Z",       (void*) android_view_HardwareLayer_flushChanges },
156
157    { "nGetLayer",               "(J)J",       (void*) android_view_HardwareLayer_getLayer },
158    { "nGetTexName",             "(J)I",       (void*) android_view_HardwareLayer_getTexName },
159#endif
160};
161
162int register_android_view_HardwareLayer(JNIEnv* env) {
163    return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
164}
165
166};
167