John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 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 <DisplayList.h> |
| 36 | #include <LayerRenderer.h> |
| 37 | #include <SkiaShader.h> |
| 38 | #include <Rect.h> |
| 39 | |
| 40 | namespace android { |
| 41 | |
| 42 | using namespace uirenderer; |
| 43 | |
| 44 | #ifdef USE_OPENGL_RENDERER |
| 45 | |
| 46 | static 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 | |
| 53 | static 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 | OpenGLRenderer* renderer = new LayerRenderer(layer); |
| 59 | renderer->initProperties(); |
| 60 | return reinterpret_cast<jlong>( new DeferredLayerUpdater(layer, renderer) ); |
| 61 | } |
| 62 | |
| 63 | static void android_view_HardwareLayer_onTextureDestroyed(JNIEnv* env, jobject clazz, |
| 64 | jlong layerUpdaterPtr) { |
| 65 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 66 | layer->backingLayer()->clearTexture(); |
| 67 | } |
| 68 | |
| 69 | static jlong android_view_HardwareLayer_detachBackingLayer(JNIEnv* env, jobject clazz, |
| 70 | jlong layerUpdaterPtr) { |
| 71 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 72 | return reinterpret_cast<jlong>( layer->detachBackingLayer() ); |
| 73 | } |
| 74 | |
| 75 | static void android_view_HardwareLayer_destroyLayerUpdater(JNIEnv* env, jobject clazz, |
| 76 | jlong layerUpdaterPtr) { |
| 77 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 78 | delete layer; |
| 79 | } |
| 80 | |
| 81 | static jboolean android_view_HardwareLayer_prepare(JNIEnv* env, jobject clazz, |
| 82 | jlong layerUpdaterPtr, jint width, jint height, jboolean isOpaque) { |
| 83 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 84 | bool changed = false; |
| 85 | changed |= layer->setSize(width, height); |
| 86 | changed |= layer->setBlend(!isOpaque); |
| 87 | return changed; |
| 88 | } |
| 89 | |
| 90 | static void android_view_HardwareLayer_setLayerPaint(JNIEnv* env, jobject clazz, |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 91 | jlong layerUpdaterPtr, jlong paintPtr) { |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 92 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 93 | if (layer) { |
| 94 | SkPaint* paint = reinterpret_cast<SkPaint*>(paintPtr); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 95 | layer->setPaint(paint); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | static void android_view_HardwareLayer_setTransform(JNIEnv* env, jobject clazz, |
| 100 | jlong layerUpdaterPtr, jlong matrixPtr) { |
| 101 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 102 | SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixPtr); |
| 103 | layer->setTransform(matrix); |
| 104 | } |
| 105 | |
| 106 | static void android_view_HardwareLayer_setSurfaceTexture(JNIEnv* env, jobject clazz, |
| 107 | jlong layerUpdaterPtr, jobject surface, jboolean isAlreadyAttached) { |
| 108 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 109 | sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, surface)); |
| 110 | layer->setSurfaceTexture(surfaceTexture, !isAlreadyAttached); |
| 111 | } |
| 112 | |
| 113 | static void android_view_HardwareLayer_updateSurfaceTexture(JNIEnv* env, jobject clazz, |
| 114 | jlong layerUpdaterPtr) { |
| 115 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 116 | layer->updateTexImage(); |
| 117 | } |
| 118 | |
| 119 | static void android_view_HardwareLayer_updateRenderLayer(JNIEnv* env, jobject clazz, |
| 120 | jlong layerUpdaterPtr, jlong displayListPtr, |
| 121 | jint left, jint top, jint right, jint bottom) { |
| 122 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 123 | DisplayList* displayList = reinterpret_cast<DisplayList*>(displayListPtr); |
| 124 | layer->setDisplayList(displayList, left, top, right, bottom); |
| 125 | } |
| 126 | |
| 127 | static jboolean android_view_HardwareLayer_flushChanges(JNIEnv* env, jobject clazz, |
| 128 | jlong layerUpdaterPtr) { |
| 129 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 130 | return layer->apply(); |
| 131 | } |
| 132 | |
| 133 | static jlong android_view_HardwareLayer_getLayer(JNIEnv* env, jobject clazz, |
| 134 | jlong layerUpdaterPtr) { |
| 135 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 136 | return reinterpret_cast<jlong>( layer->backingLayer() ); |
| 137 | } |
| 138 | |
| 139 | static jint android_view_HardwareLayer_getTexName(JNIEnv* env, jobject clazz, |
| 140 | jlong layerUpdaterPtr) { |
| 141 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 142 | return layer->backingLayer()->getTexture(); |
| 143 | } |
| 144 | |
| 145 | #endif // USE_OPENGL_RENDERER |
| 146 | |
| 147 | // ---------------------------------------------------------------------------- |
| 148 | // JNI Glue |
| 149 | // ---------------------------------------------------------------------------- |
| 150 | |
| 151 | const char* const kClassPathName = "android/view/HardwareLayer"; |
| 152 | |
| 153 | static JNINativeMethod gMethods[] = { |
| 154 | #ifdef USE_OPENGL_RENDERER |
| 155 | |
| 156 | { "nCreateTextureLayer", "()J", (void*) android_view_HardwareLayer_createTextureLayer }, |
| 157 | { "nCreateRenderLayer", "(II)J", (void*) android_view_HardwareLayer_createRenderLayer }, |
| 158 | { "nOnTextureDestroyed", "(J)V", (void*) android_view_HardwareLayer_onTextureDestroyed }, |
| 159 | { "nDetachBackingLayer", "(J)J", (void*) android_view_HardwareLayer_detachBackingLayer }, |
| 160 | { "nDestroyLayerUpdater", "(J)V", (void*) android_view_HardwareLayer_destroyLayerUpdater }, |
| 161 | |
| 162 | { "nPrepare", "(JIIZ)Z", (void*) android_view_HardwareLayer_prepare }, |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 163 | { "nSetLayerPaint", "(JJ)V", (void*) android_view_HardwareLayer_setLayerPaint }, |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 164 | { "nSetTransform", "(JJ)V", (void*) android_view_HardwareLayer_setTransform }, |
| 165 | { "nSetSurfaceTexture", "(JLandroid/graphics/SurfaceTexture;Z)V", |
| 166 | (void*) android_view_HardwareLayer_setSurfaceTexture }, |
| 167 | { "nUpdateSurfaceTexture", "(J)V", (void*) android_view_HardwareLayer_updateSurfaceTexture }, |
| 168 | { "nUpdateRenderLayer", "(JJIIII)V", (void*) android_view_HardwareLayer_updateRenderLayer }, |
| 169 | |
| 170 | { "nFlushChanges", "(J)Z", (void*) android_view_HardwareLayer_flushChanges }, |
| 171 | |
| 172 | { "nGetLayer", "(J)J", (void*) android_view_HardwareLayer_getLayer }, |
| 173 | { "nGetTexName", "(J)I", (void*) android_view_HardwareLayer_getTexName }, |
| 174 | #endif |
| 175 | }; |
| 176 | |
| 177 | int register_android_view_HardwareLayer(JNIEnv* env) { |
| 178 | return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods)); |
| 179 | } |
| 180 | |
| 181 | }; |