Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 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 | |
Romain Guy | a1db574 | 2010-07-20 13:09:13 -0700 | [diff] [blame] | 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 19 | #include "jni.h" |
Patrick Dubroy | e4ac2d6 | 2010-12-01 11:23:13 -0800 | [diff] [blame] | 20 | #include "GraphicsJNI.h" |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 21 | #include <nativehelper/JNIHelp.h> |
Romain Guy | 6554943 | 2012-03-26 16:45:05 -0700 | [diff] [blame] | 22 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 23 | #include <android_runtime/AndroidRuntime.h> |
Romain Guy | 6554943 | 2012-03-26 16:45:05 -0700 | [diff] [blame] | 24 | |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 25 | #include <utils/Looper.h> |
Romain Guy | 6554943 | 2012-03-26 16:45:05 -0700 | [diff] [blame] | 26 | #include <cutils/properties.h> |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 27 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 28 | #include <SkBitmap.h> |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 29 | #include <SkRegion.h> |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 30 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 31 | #include <Rect.h> |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 32 | #include <RenderNode.h> |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 33 | #include <CanvasProperty.h> |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 34 | #include <hwui/Canvas.h> |
| 35 | #include <hwui/Paint.h> |
Seigo Nonaka | 2d46442 | 2017-08-23 13:49:51 -0700 | [diff] [blame] | 36 | #include <minikin/Layout.h> |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 37 | #include <renderthread/RenderProxy.h> |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 38 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 39 | #include "core_jni_helpers.h" |
| 40 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 43 | using namespace uirenderer; |
| 44 | |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 45 | jmethodID gRunnableMethodId; |
| 46 | |
| 47 | static JNIEnv* jnienv(JavaVM* vm) { |
| 48 | JNIEnv* env; |
| 49 | if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 50 | LOG_ALWAYS_FATAL("Failed to get JNIEnv for JavaVM: %p", vm); |
| 51 | } |
| 52 | return env; |
| 53 | } |
| 54 | |
| 55 | class InvokeRunnableMessage : public MessageHandler { |
| 56 | public: |
| 57 | InvokeRunnableMessage(JNIEnv* env, jobject runnable) { |
| 58 | mRunnable = env->NewGlobalRef(runnable); |
| 59 | env->GetJavaVM(&mVm); |
| 60 | } |
| 61 | |
| 62 | virtual ~InvokeRunnableMessage() { |
| 63 | jnienv(mVm)->DeleteGlobalRef(mRunnable); |
| 64 | } |
| 65 | |
| 66 | virtual void handleMessage(const Message&) { |
| 67 | jnienv(mVm)->CallVoidMethod(mRunnable, gRunnableMethodId); |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | JavaVM* mVm; |
| 72 | jobject mRunnable; |
| 73 | }; |
| 74 | |
| 75 | class GlFunctorReleasedCallbackBridge : public GlFunctorLifecycleListener { |
| 76 | public: |
| 77 | GlFunctorReleasedCallbackBridge(JNIEnv* env, jobject javaCallback) { |
| 78 | mLooper = Looper::getForThread(); |
| 79 | mMessage = new InvokeRunnableMessage(env, javaCallback); |
| 80 | } |
| 81 | |
| 82 | virtual void onGlFunctorReleased(Functor* functor) override { |
| 83 | mLooper->sendMessage(mMessage, 0); |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | sp<Looper> mLooper; |
| 88 | sp<InvokeRunnableMessage> mMessage; |
| 89 | }; |
| 90 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 91 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 92 | // ---------------- Regular JNI ----------------------------- |
| 93 | |
| 94 | static void |
| 95 | android_app_ActivityThread_dumpGraphics(JNIEnv* env, jobject clazz, jobject javaFileDescriptor) { |
| 96 | int fd = jniGetFDFromFileDescriptor(env, javaFileDescriptor); |
| 97 | android::uirenderer::renderthread::RenderProxy::dumpGraphicsMemory(fd); |
Seigo Nonaka | 2d46442 | 2017-08-23 13:49:51 -0700 | [diff] [blame] | 98 | minikin::Layout::dumpMinikinStats(fd); |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 101 | |
| 102 | // ---------------- @FastNative ----------------------------- |
Romain Guy | 530041d | 2012-01-25 18:56:29 -0800 | [diff] [blame] | 103 | |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 104 | static void android_view_DisplayListCanvas_callDrawGLFunction(JNIEnv* env, jobject clazz, |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 105 | jlong canvasPtr, jlong functorPtr, jobject releasedCallback) { |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 106 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
Chris Craik | 9f66ea0 | 2013-12-27 15:15:55 -0800 | [diff] [blame] | 107 | Functor* functor = reinterpret_cast<Functor*>(functorPtr); |
John Reck | 4afbed1 | 2016-04-18 09:29:36 -0700 | [diff] [blame] | 108 | sp<GlFunctorReleasedCallbackBridge> bridge; |
| 109 | if (releasedCallback) { |
| 110 | bridge = new GlFunctorReleasedCallbackBridge(env, releasedCallback); |
| 111 | } |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 112 | canvas->callDrawGLFunction(functor, bridge.get()); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 115 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 116 | // ---------------- @CriticalNative ------------------------- |
| 117 | |
| 118 | static jlong android_view_DisplayListCanvas_createDisplayListCanvas(jlong renderNodePtr, |
| 119 | jint width, jint height) { |
| 120 | RenderNode* renderNode = reinterpret_cast<RenderNode*>(renderNodePtr); |
| 121 | return reinterpret_cast<jlong>(Canvas::create_recording_canvas(width, height, renderNode)); |
| 122 | } |
| 123 | |
| 124 | static void android_view_DisplayListCanvas_resetDisplayListCanvas(jlong canvasPtr, |
| 125 | jlong renderNodePtr, jint width, jint height) { |
| 126 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
| 127 | RenderNode* renderNode = reinterpret_cast<RenderNode*>(renderNodePtr); |
| 128 | canvas->resetRecording(width, height, renderNode); |
| 129 | } |
| 130 | |
| 131 | static jint android_view_DisplayListCanvas_getMaxTextureWidth() { |
Thomas Buhot | c0a0e1a | 2016-01-18 10:31:58 +0100 | [diff] [blame] | 132 | if (!Caches::hasInstance()) { |
| 133 | android::uirenderer::renderthread::RenderProxy::staticFence(); |
| 134 | } |
Romain Guy | f61970fc | 2011-07-07 14:10:06 -0700 | [diff] [blame] | 135 | return Caches::getInstance().maxTextureSize; |
| 136 | } |
| 137 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 138 | static jint android_view_DisplayListCanvas_getMaxTextureHeight() { |
Thomas Buhot | c0a0e1a | 2016-01-18 10:31:58 +0100 | [diff] [blame] | 139 | if (!Caches::hasInstance()) { |
| 140 | android::uirenderer::renderthread::RenderProxy::staticFence(); |
| 141 | } |
Romain Guy | f61970fc | 2011-07-07 14:10:06 -0700 | [diff] [blame] | 142 | return Caches::getInstance().maxTextureSize; |
| 143 | } |
| 144 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 145 | static void android_view_DisplayListCanvas_insertReorderBarrier(jlong canvasPtr, |
| 146 | jboolean reorderEnable) { |
| 147 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
| 148 | canvas->insertReorderBarrier(reorderEnable); |
| 149 | } |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 150 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 151 | static jlong android_view_DisplayListCanvas_finishRecording(jlong canvasPtr) { |
| 152 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
| 153 | return reinterpret_cast<jlong>(canvas->finishRecording()); |
| 154 | } |
| 155 | |
| 156 | static void android_view_DisplayListCanvas_drawRenderNode(jlong canvasPtr, jlong renderNodePtr) { |
| 157 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
| 158 | RenderNode* renderNode = reinterpret_cast<RenderNode*>(renderNodePtr); |
| 159 | canvas->drawRenderNode(renderNode); |
| 160 | } |
| 161 | |
| 162 | static void android_view_DisplayListCanvas_drawLayer(jlong canvasPtr, jlong layerPtr) { |
| 163 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
| 164 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerPtr); |
| 165 | canvas->drawLayer(layer); |
| 166 | } |
| 167 | |
| 168 | static void android_view_DisplayListCanvas_drawRoundRectProps(jlong canvasPtr, |
| 169 | jlong leftPropPtr, jlong topPropPtr, jlong rightPropPtr, jlong bottomPropPtr, |
| 170 | jlong rxPropPtr, jlong ryPropPtr, jlong paintPropPtr) { |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 171 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
Jorim Jaggi | 072707d | 2014-09-15 17:20:08 +0200 | [diff] [blame] | 172 | CanvasPropertyPrimitive* leftProp = reinterpret_cast<CanvasPropertyPrimitive*>(leftPropPtr); |
| 173 | CanvasPropertyPrimitive* topProp = reinterpret_cast<CanvasPropertyPrimitive*>(topPropPtr); |
| 174 | CanvasPropertyPrimitive* rightProp = reinterpret_cast<CanvasPropertyPrimitive*>(rightPropPtr); |
| 175 | CanvasPropertyPrimitive* bottomProp = reinterpret_cast<CanvasPropertyPrimitive*>(bottomPropPtr); |
| 176 | CanvasPropertyPrimitive* rxProp = reinterpret_cast<CanvasPropertyPrimitive*>(rxPropPtr); |
| 177 | CanvasPropertyPrimitive* ryProp = reinterpret_cast<CanvasPropertyPrimitive*>(ryPropPtr); |
| 178 | CanvasPropertyPaint* paintProp = reinterpret_cast<CanvasPropertyPaint*>(paintPropPtr); |
Chris Craik | 86c03ca | 2015-08-21 10:44:56 -0700 | [diff] [blame] | 179 | canvas->drawRoundRect(leftProp, topProp, rightProp, bottomProp, rxProp, ryProp, paintProp); |
Jorim Jaggi | 072707d | 2014-09-15 17:20:08 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 182 | static void android_view_DisplayListCanvas_drawCircleProps(jlong canvasPtr, |
| 183 | jlong xPropPtr, jlong yPropPtr, jlong radiusPropPtr, jlong paintPropPtr) { |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 184 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 185 | CanvasPropertyPrimitive* xProp = reinterpret_cast<CanvasPropertyPrimitive*>(xPropPtr); |
| 186 | CanvasPropertyPrimitive* yProp = reinterpret_cast<CanvasPropertyPrimitive*>(yPropPtr); |
| 187 | CanvasPropertyPrimitive* radiusProp = reinterpret_cast<CanvasPropertyPrimitive*>(radiusPropPtr); |
| 188 | CanvasPropertyPaint* paintProp = reinterpret_cast<CanvasPropertyPaint*>(paintPropPtr); |
Chris Craik | 86c03ca | 2015-08-21 10:44:56 -0700 | [diff] [blame] | 189 | canvas->drawCircle(xProp, yProp, radiusProp, paintProp); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 192 | // ---------------------------------------------------------------------------- |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 193 | // JNI Glue |
| 194 | // ---------------------------------------------------------------------------- |
| 195 | |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 196 | const char* const kClassPathName = "android/view/DisplayListCanvas"; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 197 | |
| 198 | static JNINativeMethod gMethods[] = { |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 199 | |
John Reck | 5cb74bc | 2016-10-07 11:24:44 -0700 | [diff] [blame] | 200 | // ------------ @FastNative ------------------ |
| 201 | |
John Reck | 5cb74bc | 2016-10-07 11:24:44 -0700 | [diff] [blame] | 202 | { "nCallDrawGLFunction", "(JJLjava/lang/Runnable;)V", |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 203 | (void*) android_view_DisplayListCanvas_callDrawGLFunction }, |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 204 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 205 | // ------------ @CriticalNative -------------- |
Stan Iliev | c0e7a90 | 2016-10-13 17:07:09 -0400 | [diff] [blame] | 206 | { "nCreateDisplayListCanvas", "(JII)J", (void*) android_view_DisplayListCanvas_createDisplayListCanvas }, |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 207 | { "nResetDisplayListCanvas", "(JJII)V", (void*) android_view_DisplayListCanvas_resetDisplayListCanvas }, |
| 208 | { "nGetMaximumTextureWidth", "()I", (void*) android_view_DisplayListCanvas_getMaxTextureWidth }, |
| 209 | { "nGetMaximumTextureHeight", "()I", (void*) android_view_DisplayListCanvas_getMaxTextureHeight }, |
| 210 | { "nInsertReorderBarrier", "(JZ)V", (void*) android_view_DisplayListCanvas_insertReorderBarrier }, |
| 211 | { "nFinishRecording", "(J)J", (void*) android_view_DisplayListCanvas_finishRecording }, |
| 212 | { "nDrawRenderNode", "(JJ)V", (void*) android_view_DisplayListCanvas_drawRenderNode }, |
| 213 | { "nDrawLayer", "(JJ)V", (void*) android_view_DisplayListCanvas_drawLayer }, |
| 214 | { "nDrawCircle", "(JJJJJ)V", (void*) android_view_DisplayListCanvas_drawCircleProps }, |
| 215 | { "nDrawRoundRect", "(JJJJJJJJ)V",(void*) android_view_DisplayListCanvas_drawRoundRectProps }, |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 216 | }; |
| 217 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 218 | static JNINativeMethod gActivityThreadMethods[] = { |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 219 | // ------------ Regular JNI ------------------ |
| 220 | { "nDumpGraphicsInfo", "(Ljava/io/FileDescriptor;)V", |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 221 | (void*) android_app_ActivityThread_dumpGraphics } |
| 222 | }; |
| 223 | |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 224 | int register_android_view_DisplayListCanvas(JNIEnv* env) { |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 225 | jclass runnableClass = FindClassOrDie(env, "java/lang/Runnable"); |
| 226 | gRunnableMethodId = GetMethodIDOrDie(env, runnableClass, "run", "()V"); |
| 227 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 228 | return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 231 | int register_android_app_ActivityThread(JNIEnv* env) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 232 | return RegisterMethodsOrDie(env, "android/app/ActivityThread", |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 233 | gActivityThreadMethods, NELEM(gActivityThreadMethods)); |
| 234 | } |
| 235 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 236 | }; |