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 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 91 | // ---------------- @FastNative ----------------------------- |
Romain Guy | 530041d | 2012-01-25 18:56:29 -0800 | [diff] [blame] | 92 | |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 93 | static void android_view_DisplayListCanvas_callDrawGLFunction(JNIEnv* env, jobject clazz, |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 94 | jlong canvasPtr, jlong functorPtr, jobject releasedCallback) { |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 95 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
Chris Craik | 9f66ea0 | 2013-12-27 15:15:55 -0800 | [diff] [blame] | 96 | Functor* functor = reinterpret_cast<Functor*>(functorPtr); |
John Reck | 4afbed1 | 2016-04-18 09:29:36 -0700 | [diff] [blame] | 97 | sp<GlFunctorReleasedCallbackBridge> bridge; |
| 98 | if (releasedCallback) { |
| 99 | bridge = new GlFunctorReleasedCallbackBridge(env, releasedCallback); |
| 100 | } |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 101 | canvas->callDrawGLFunction(functor, bridge.get()); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 104 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 105 | // ---------------- @CriticalNative ------------------------- |
| 106 | |
| 107 | static jlong android_view_DisplayListCanvas_createDisplayListCanvas(jlong renderNodePtr, |
| 108 | jint width, jint height) { |
| 109 | RenderNode* renderNode = reinterpret_cast<RenderNode*>(renderNodePtr); |
| 110 | return reinterpret_cast<jlong>(Canvas::create_recording_canvas(width, height, renderNode)); |
| 111 | } |
| 112 | |
| 113 | static void android_view_DisplayListCanvas_resetDisplayListCanvas(jlong canvasPtr, |
| 114 | jlong renderNodePtr, jint width, jint height) { |
| 115 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
| 116 | RenderNode* renderNode = reinterpret_cast<RenderNode*>(renderNodePtr); |
| 117 | canvas->resetRecording(width, height, renderNode); |
| 118 | } |
| 119 | |
| 120 | static jint android_view_DisplayListCanvas_getMaxTextureWidth() { |
Thomas Buhot | c0a0e1a | 2016-01-18 10:31:58 +0100 | [diff] [blame] | 121 | if (!Caches::hasInstance()) { |
| 122 | android::uirenderer::renderthread::RenderProxy::staticFence(); |
| 123 | } |
Romain Guy | f61970fc | 2011-07-07 14:10:06 -0700 | [diff] [blame] | 124 | return Caches::getInstance().maxTextureSize; |
| 125 | } |
| 126 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 127 | static jint android_view_DisplayListCanvas_getMaxTextureHeight() { |
Thomas Buhot | c0a0e1a | 2016-01-18 10:31:58 +0100 | [diff] [blame] | 128 | if (!Caches::hasInstance()) { |
| 129 | android::uirenderer::renderthread::RenderProxy::staticFence(); |
| 130 | } |
Romain Guy | f61970fc | 2011-07-07 14:10:06 -0700 | [diff] [blame] | 131 | return Caches::getInstance().maxTextureSize; |
| 132 | } |
| 133 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 134 | static void android_view_DisplayListCanvas_insertReorderBarrier(jlong canvasPtr, |
| 135 | jboolean reorderEnable) { |
| 136 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
| 137 | canvas->insertReorderBarrier(reorderEnable); |
| 138 | } |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 139 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 140 | static jlong android_view_DisplayListCanvas_finishRecording(jlong canvasPtr) { |
| 141 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
| 142 | return reinterpret_cast<jlong>(canvas->finishRecording()); |
| 143 | } |
| 144 | |
| 145 | static void android_view_DisplayListCanvas_drawRenderNode(jlong canvasPtr, jlong renderNodePtr) { |
| 146 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
| 147 | RenderNode* renderNode = reinterpret_cast<RenderNode*>(renderNodePtr); |
| 148 | canvas->drawRenderNode(renderNode); |
| 149 | } |
| 150 | |
John Reck | 9d8d99d | 2018-02-21 12:55:41 -0800 | [diff] [blame] | 151 | static void android_view_DisplayListCanvas_drawTextureLayer(jlong canvasPtr, jlong layerPtr) { |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 152 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
| 153 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerPtr); |
| 154 | canvas->drawLayer(layer); |
| 155 | } |
| 156 | |
| 157 | static void android_view_DisplayListCanvas_drawRoundRectProps(jlong canvasPtr, |
| 158 | jlong leftPropPtr, jlong topPropPtr, jlong rightPropPtr, jlong bottomPropPtr, |
| 159 | jlong rxPropPtr, jlong ryPropPtr, jlong paintPropPtr) { |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 160 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
Jorim Jaggi | 072707d | 2014-09-15 17:20:08 +0200 | [diff] [blame] | 161 | CanvasPropertyPrimitive* leftProp = reinterpret_cast<CanvasPropertyPrimitive*>(leftPropPtr); |
| 162 | CanvasPropertyPrimitive* topProp = reinterpret_cast<CanvasPropertyPrimitive*>(topPropPtr); |
| 163 | CanvasPropertyPrimitive* rightProp = reinterpret_cast<CanvasPropertyPrimitive*>(rightPropPtr); |
| 164 | CanvasPropertyPrimitive* bottomProp = reinterpret_cast<CanvasPropertyPrimitive*>(bottomPropPtr); |
| 165 | CanvasPropertyPrimitive* rxProp = reinterpret_cast<CanvasPropertyPrimitive*>(rxPropPtr); |
| 166 | CanvasPropertyPrimitive* ryProp = reinterpret_cast<CanvasPropertyPrimitive*>(ryPropPtr); |
| 167 | CanvasPropertyPaint* paintProp = reinterpret_cast<CanvasPropertyPaint*>(paintPropPtr); |
Chris Craik | 86c03ca | 2015-08-21 10:44:56 -0700 | [diff] [blame] | 168 | canvas->drawRoundRect(leftProp, topProp, rightProp, bottomProp, rxProp, ryProp, paintProp); |
Jorim Jaggi | 072707d | 2014-09-15 17:20:08 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 171 | static void android_view_DisplayListCanvas_drawCircleProps(jlong canvasPtr, |
| 172 | jlong xPropPtr, jlong yPropPtr, jlong radiusPropPtr, jlong paintPropPtr) { |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 173 | Canvas* canvas = reinterpret_cast<Canvas*>(canvasPtr); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 174 | CanvasPropertyPrimitive* xProp = reinterpret_cast<CanvasPropertyPrimitive*>(xPropPtr); |
| 175 | CanvasPropertyPrimitive* yProp = reinterpret_cast<CanvasPropertyPrimitive*>(yPropPtr); |
| 176 | CanvasPropertyPrimitive* radiusProp = reinterpret_cast<CanvasPropertyPrimitive*>(radiusPropPtr); |
| 177 | CanvasPropertyPaint* paintProp = reinterpret_cast<CanvasPropertyPaint*>(paintPropPtr); |
Chris Craik | 86c03ca | 2015-08-21 10:44:56 -0700 | [diff] [blame] | 178 | canvas->drawCircle(xProp, yProp, radiusProp, paintProp); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 181 | // ---------------------------------------------------------------------------- |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 182 | // JNI Glue |
| 183 | // ---------------------------------------------------------------------------- |
| 184 | |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 185 | const char* const kClassPathName = "android/view/DisplayListCanvas"; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 186 | |
| 187 | static JNINativeMethod gMethods[] = { |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 188 | |
John Reck | 5cb74bc | 2016-10-07 11:24:44 -0700 | [diff] [blame] | 189 | // ------------ @FastNative ------------------ |
| 190 | |
John Reck | 5cb74bc | 2016-10-07 11:24:44 -0700 | [diff] [blame] | 191 | { "nCallDrawGLFunction", "(JJLjava/lang/Runnable;)V", |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 192 | (void*) android_view_DisplayListCanvas_callDrawGLFunction }, |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 193 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 194 | // ------------ @CriticalNative -------------- |
Stan Iliev | c0e7a90 | 2016-10-13 17:07:09 -0400 | [diff] [blame] | 195 | { "nCreateDisplayListCanvas", "(JII)J", (void*) android_view_DisplayListCanvas_createDisplayListCanvas }, |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 196 | { "nResetDisplayListCanvas", "(JJII)V", (void*) android_view_DisplayListCanvas_resetDisplayListCanvas }, |
| 197 | { "nGetMaximumTextureWidth", "()I", (void*) android_view_DisplayListCanvas_getMaxTextureWidth }, |
| 198 | { "nGetMaximumTextureHeight", "()I", (void*) android_view_DisplayListCanvas_getMaxTextureHeight }, |
| 199 | { "nInsertReorderBarrier", "(JZ)V", (void*) android_view_DisplayListCanvas_insertReorderBarrier }, |
| 200 | { "nFinishRecording", "(J)J", (void*) android_view_DisplayListCanvas_finishRecording }, |
| 201 | { "nDrawRenderNode", "(JJ)V", (void*) android_view_DisplayListCanvas_drawRenderNode }, |
John Reck | 9d8d99d | 2018-02-21 12:55:41 -0800 | [diff] [blame] | 202 | { "nDrawTextureLayer", "(JJ)V", (void*) android_view_DisplayListCanvas_drawTextureLayer }, |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 203 | { "nDrawCircle", "(JJJJJ)V", (void*) android_view_DisplayListCanvas_drawCircleProps }, |
| 204 | { "nDrawRoundRect", "(JJJJJJJJ)V",(void*) android_view_DisplayListCanvas_drawRoundRectProps }, |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 207 | int register_android_view_DisplayListCanvas(JNIEnv* env) { |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 208 | jclass runnableClass = FindClassOrDie(env, "java/lang/Runnable"); |
| 209 | gRunnableMethodId = GetMethodIDOrDie(env, runnableClass, "run", "()V"); |
| 210 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 211 | return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 214 | }; |