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 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 23 | #include "core_jni_helpers.h" |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 24 | #include <android_runtime/android_graphics_SurfaceTexture.h> |
| 25 | |
| 26 | #include <gui/GLConsumer.h> |
| 27 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 28 | #include <Paint.h> |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 29 | #include <SkBitmap.h> |
| 30 | #include <SkCanvas.h> |
| 31 | #include <SkMatrix.h> |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 32 | #include <SkXfermode.h> |
| 33 | |
| 34 | #include <DeferredLayerUpdater.h> |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 35 | #include <LayerRenderer.h> |
| 36 | #include <SkiaShader.h> |
| 37 | #include <Rect.h> |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 38 | #include <RenderNode.h> |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 39 | |
| 40 | namespace android { |
| 41 | |
| 42 | using namespace uirenderer; |
| 43 | |
| 44 | #ifdef USE_OPENGL_RENDERER |
| 45 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 46 | static jboolean android_view_HardwareLayer_prepare(JNIEnv* env, jobject clazz, |
| 47 | jlong layerUpdaterPtr, jint width, jint height, jboolean isOpaque) { |
| 48 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 49 | bool changed = false; |
| 50 | changed |= layer->setSize(width, height); |
| 51 | changed |= layer->setBlend(!isOpaque); |
| 52 | return changed; |
| 53 | } |
| 54 | |
| 55 | static void android_view_HardwareLayer_setLayerPaint(JNIEnv* env, jobject clazz, |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 56 | jlong layerUpdaterPtr, jlong paintPtr) { |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 57 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 58 | if (layer) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 59 | Paint* paint = reinterpret_cast<Paint*>(paintPtr); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 60 | layer->setPaint(paint); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | static void android_view_HardwareLayer_setTransform(JNIEnv* env, jobject clazz, |
| 65 | jlong layerUpdaterPtr, jlong matrixPtr) { |
| 66 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 67 | SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixPtr); |
| 68 | layer->setTransform(matrix); |
| 69 | } |
| 70 | |
| 71 | static void android_view_HardwareLayer_setSurfaceTexture(JNIEnv* env, jobject clazz, |
| 72 | jlong layerUpdaterPtr, jobject surface, jboolean isAlreadyAttached) { |
| 73 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 74 | sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, surface)); |
| 75 | layer->setSurfaceTexture(surfaceTexture, !isAlreadyAttached); |
| 76 | } |
| 77 | |
| 78 | static void android_view_HardwareLayer_updateSurfaceTexture(JNIEnv* env, jobject clazz, |
| 79 | jlong layerUpdaterPtr) { |
| 80 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 81 | layer->updateTexImage(); |
| 82 | } |
| 83 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 84 | static jint android_view_HardwareLayer_getTexName(JNIEnv* env, jobject clazz, |
| 85 | jlong layerUpdaterPtr) { |
| 86 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr); |
| 87 | return layer->backingLayer()->getTexture(); |
| 88 | } |
| 89 | |
| 90 | #endif // USE_OPENGL_RENDERER |
| 91 | |
| 92 | // ---------------------------------------------------------------------------- |
| 93 | // JNI Glue |
| 94 | // ---------------------------------------------------------------------------- |
| 95 | |
| 96 | const char* const kClassPathName = "android/view/HardwareLayer"; |
| 97 | |
| 98 | static JNINativeMethod gMethods[] = { |
| 99 | #ifdef USE_OPENGL_RENDERER |
| 100 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 101 | { "nPrepare", "(JIIZ)Z", (void*) android_view_HardwareLayer_prepare }, |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 102 | { "nSetLayerPaint", "(JJ)V", (void*) android_view_HardwareLayer_setLayerPaint }, |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 103 | { "nSetTransform", "(JJ)V", (void*) android_view_HardwareLayer_setTransform }, |
| 104 | { "nSetSurfaceTexture", "(JLandroid/graphics/SurfaceTexture;Z)V", |
| 105 | (void*) android_view_HardwareLayer_setSurfaceTexture }, |
| 106 | { "nUpdateSurfaceTexture", "(J)V", (void*) android_view_HardwareLayer_updateSurfaceTexture }, |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 107 | |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 108 | { "nGetTexName", "(J)I", (void*) android_view_HardwareLayer_getTexName }, |
| 109 | #endif |
| 110 | }; |
| 111 | |
| 112 | int register_android_view_HardwareLayer(JNIEnv* env) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 113 | return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | }; |