android_view_DisplayListCanvas.cpp revision 15d556e5d3729a287718c7be5c36079a7f8633c6
1/*
2 * Copyright (C) 2010 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
25#include <cutils/properties.h>
26
27#include <SkBitmap.h>
28#include <SkRegion.h>
29
30#if HWUI_NEW_OPS
31#include <RecordingCanvas.h>
32typedef android::uirenderer::RecordingCanvas canvas_t;
33#else
34#include <DisplayListCanvas.h>
35typedef android::uirenderer::DisplayListCanvas canvas_t;
36#endif
37#include <Rect.h>
38#include <RenderNode.h>
39#include <CanvasProperty.h>
40#include <Paint.h>
41#include <renderthread/RenderProxy.h>
42
43#include "core_jni_helpers.h"
44
45namespace android {
46
47using namespace uirenderer;
48
49// ----------------------------------------------------------------------------
50// Setup
51// ----------------------------------------------------------------------------
52
53static void android_view_DisplayListCanvas_insertReorderBarrier(JNIEnv* env, jobject clazz,
54        jlong canvasPtr, jboolean reorderEnable) {
55    canvas_t* canvas = reinterpret_cast<canvas_t*>(canvasPtr);
56    canvas->insertReorderBarrier(reorderEnable);
57}
58
59// ----------------------------------------------------------------------------
60// Functor
61// ----------------------------------------------------------------------------
62
63static void android_view_DisplayListCanvas_callDrawGLFunction(JNIEnv* env, jobject clazz,
64        jlong canvasPtr, jlong functorPtr) {
65    canvas_t* canvas = reinterpret_cast<canvas_t*>(canvasPtr);
66    Functor* functor = reinterpret_cast<Functor*>(functorPtr);
67    canvas->callDrawGLFunction(functor);
68}
69
70// ----------------------------------------------------------------------------
71// Misc
72// ----------------------------------------------------------------------------
73
74static jint android_view_DisplayListCanvas_getMaxTextureWidth(JNIEnv* env, jobject clazz) {
75    return Caches::getInstance().maxTextureSize;
76}
77
78static jint android_view_DisplayListCanvas_getMaxTextureHeight(JNIEnv* env, jobject clazz) {
79    return Caches::getInstance().maxTextureSize;
80}
81
82// ----------------------------------------------------------------------------
83// Drawing
84// ----------------------------------------------------------------------------
85
86static void android_view_DisplayListCanvas_drawRoundRectProps(JNIEnv* env, jobject clazz,
87        jlong canvasPtr, jlong leftPropPtr, jlong topPropPtr, jlong rightPropPtr,
88        jlong bottomPropPtr, jlong rxPropPtr, jlong ryPropPtr, jlong paintPropPtr) {
89    canvas_t* canvas = reinterpret_cast<canvas_t*>(canvasPtr);
90    CanvasPropertyPrimitive* leftProp = reinterpret_cast<CanvasPropertyPrimitive*>(leftPropPtr);
91    CanvasPropertyPrimitive* topProp = reinterpret_cast<CanvasPropertyPrimitive*>(topPropPtr);
92    CanvasPropertyPrimitive* rightProp = reinterpret_cast<CanvasPropertyPrimitive*>(rightPropPtr);
93    CanvasPropertyPrimitive* bottomProp = reinterpret_cast<CanvasPropertyPrimitive*>(bottomPropPtr);
94    CanvasPropertyPrimitive* rxProp = reinterpret_cast<CanvasPropertyPrimitive*>(rxPropPtr);
95    CanvasPropertyPrimitive* ryProp = reinterpret_cast<CanvasPropertyPrimitive*>(ryPropPtr);
96    CanvasPropertyPaint* paintProp = reinterpret_cast<CanvasPropertyPaint*>(paintPropPtr);
97    canvas->drawRoundRect(leftProp, topProp, rightProp, bottomProp, rxProp, ryProp, paintProp);
98}
99
100static void android_view_DisplayListCanvas_drawCircleProps(JNIEnv* env, jobject clazz,
101        jlong canvasPtr, jlong xPropPtr, jlong yPropPtr, jlong radiusPropPtr, jlong paintPropPtr) {
102    canvas_t* canvas = reinterpret_cast<canvas_t*>(canvasPtr);
103    CanvasPropertyPrimitive* xProp = reinterpret_cast<CanvasPropertyPrimitive*>(xPropPtr);
104    CanvasPropertyPrimitive* yProp = reinterpret_cast<CanvasPropertyPrimitive*>(yPropPtr);
105    CanvasPropertyPrimitive* radiusProp = reinterpret_cast<CanvasPropertyPrimitive*>(radiusPropPtr);
106    CanvasPropertyPaint* paintProp = reinterpret_cast<CanvasPropertyPaint*>(paintPropPtr);
107    canvas->drawCircle(xProp, yProp, radiusProp, paintProp);
108}
109
110// ----------------------------------------------------------------------------
111// Display lists
112// ----------------------------------------------------------------------------
113
114static jlong android_view_DisplayListCanvas_finishRecording(JNIEnv* env,
115        jobject clazz, jlong canvasPtr) {
116    canvas_t* canvas = reinterpret_cast<canvas_t*>(canvasPtr);
117    return reinterpret_cast<jlong>(canvas->finishRecording());
118}
119
120static jlong android_view_DisplayListCanvas_createDisplayListCanvas(JNIEnv* env, jobject clazz,
121        jint width, jint height) {
122    return reinterpret_cast<jlong>(new canvas_t(width, height));
123}
124
125static void android_view_DisplayListCanvas_resetDisplayListCanvas(JNIEnv* env, jobject clazz,
126        jlong canvasPtr, jint width, jint height) {
127    canvas_t* canvas = reinterpret_cast<canvas_t*>(canvasPtr);
128    canvas->reset(width, height);
129}
130
131
132static void android_view_DisplayListCanvas_drawRenderNode(JNIEnv* env,
133        jobject clazz, jlong canvasPtr, jlong renderNodePtr) {
134    canvas_t* canvas = reinterpret_cast<canvas_t*>(canvasPtr);
135    RenderNode* renderNode = reinterpret_cast<RenderNode*>(renderNodePtr);
136    canvas->drawRenderNode(renderNode);
137}
138
139// ----------------------------------------------------------------------------
140// Layers
141// ----------------------------------------------------------------------------
142
143static void android_view_DisplayListCanvas_drawLayer(JNIEnv* env, jobject clazz,
144        jlong canvasPtr, jlong layerPtr) {
145    canvas_t* canvas = reinterpret_cast<canvas_t*>(canvasPtr);
146    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerPtr);
147    canvas->drawLayer(layer);
148}
149
150// ----------------------------------------------------------------------------
151// Common
152// ----------------------------------------------------------------------------
153
154static jboolean android_view_DisplayListCanvas_isAvailable(JNIEnv* env, jobject clazz) {
155    char prop[PROPERTY_VALUE_MAX];
156    if (property_get("ro.kernel.qemu", prop, NULL) == 0) {
157        // not in the emulator
158        return JNI_TRUE;
159    }
160    // In the emulator this property will be set > 0 when OpenGL ES 2.0 is
161    // enabled, 0 otherwise. On old emulator versions it will be undefined.
162    property_get("ro.kernel.qemu.gles", prop, "0");
163    return atoi(prop) > 0 ? JNI_TRUE : JNI_FALSE;
164}
165
166// ----------------------------------------------------------------------------
167// Logging
168// ----------------------------------------------------------------------------
169
170static void
171android_app_ActivityThread_dumpGraphics(JNIEnv* env, jobject clazz, jobject javaFileDescriptor) {
172    int fd = jniGetFDFromFileDescriptor(env, javaFileDescriptor);
173    android::uirenderer::renderthread::RenderProxy::dumpGraphicsMemory(fd);
174}
175
176// ----------------------------------------------------------------------------
177// JNI Glue
178// ----------------------------------------------------------------------------
179
180const char* const kClassPathName = "android/view/DisplayListCanvas";
181
182static JNINativeMethod gMethods[] = {
183    { "nIsAvailable",       "!()Z",             (void*) android_view_DisplayListCanvas_isAvailable },
184    { "nInsertReorderBarrier","!(JZ)V",         (void*) android_view_DisplayListCanvas_insertReorderBarrier },
185
186    { "nCallDrawGLFunction", "!(JJ)V",          (void*) android_view_DisplayListCanvas_callDrawGLFunction },
187
188    { "nDrawRoundRect",     "!(JJJJJJJJ)V",     (void*) android_view_DisplayListCanvas_drawRoundRectProps },
189    { "nDrawCircle",        "!(JJJJJ)V",        (void*) android_view_DisplayListCanvas_drawCircleProps },
190
191    { "nFinishRecording",   "!(J)J",            (void*) android_view_DisplayListCanvas_finishRecording },
192    { "nDrawRenderNode",    "!(JJ)V",           (void*) android_view_DisplayListCanvas_drawRenderNode },
193
194    { "nCreateDisplayListCanvas", "!(II)J",     (void*) android_view_DisplayListCanvas_createDisplayListCanvas },
195    { "nResetDisplayListCanvas", "!(JII)V",     (void*) android_view_DisplayListCanvas_resetDisplayListCanvas },
196
197    { "nDrawLayer",               "!(JJ)V",     (void*) android_view_DisplayListCanvas_drawLayer },
198
199    { "nGetMaximumTextureWidth",  "!()I",       (void*) android_view_DisplayListCanvas_getMaxTextureWidth },
200    { "nGetMaximumTextureHeight", "!()I",       (void*) android_view_DisplayListCanvas_getMaxTextureHeight },
201};
202
203static JNINativeMethod gActivityThreadMethods[] = {
204    { "dumpGraphicsInfo",        "(Ljava/io/FileDescriptor;)V",
205                                               (void*) android_app_ActivityThread_dumpGraphics }
206};
207
208int register_android_view_DisplayListCanvas(JNIEnv* env) {
209    return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
210}
211
212int register_android_app_ActivityThread(JNIEnv* env) {
213    return RegisterMethodsOrDie(env, "android/app/ActivityThread",
214            gActivityThreadMethods, NELEM(gActivityThreadMethods));
215}
216
217};
218