• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #include <android/hardware_buffer.h>
18 #include <android/hardware_buffer_jni.h>
19 #include <android/native_window.h>
20 #include <android/native_window_jni.h>
21 #include <android/surface_control.h>
22 #include <jni.h>
23 
24 struct MyWrapper {
MyWrapperMyWrapper25     MyWrapper(ANativeWindow* parent) {
26         surfaceControl = ASurfaceControl_createFromWindow(parent, "PenLayer");
27     }
28 
~MyWrapperMyWrapper29     ~MyWrapper() { ASurfaceControl_release(surfaceControl); }
30 
setBufferMyWrapper31     void setBuffer(AHardwareBuffer* buffer) {
32         ASurfaceTransaction* transaction = ASurfaceTransaction_create();
33         ASurfaceTransaction_setBuffer(transaction, surfaceControl, buffer, -1);
34         ASurfaceTransaction_setVisibility(transaction, surfaceControl,
35                                           ASURFACE_TRANSACTION_VISIBILITY_SHOW);
36         ASurfaceTransaction_apply(transaction);
37         ASurfaceTransaction_delete(transaction);
38     }
39 
40     ASurfaceControl* surfaceControl = nullptr;
41 };
42 
43 extern "C" JNIEXPORT jlong JNICALL
Java_com_android_test_hwui_FrontBufferedLayer_nCreate(JNIEnv * env,jclass,jobject jSurface)44 Java_com_android_test_hwui_FrontBufferedLayer_nCreate(JNIEnv* env, jclass, jobject jSurface) {
45     ANativeWindow* window = ANativeWindow_fromSurface(env, jSurface);
46     MyWrapper* wrapper = new MyWrapper(window);
47     ANativeWindow_release(window);
48     return reinterpret_cast<jlong>(wrapper);
49 }
50 
51 extern "C" JNIEXPORT void JNICALL
Java_com_android_test_hwui_FrontBufferedLayer_nDestroy(JNIEnv *,jclass,jlong ptr)52 Java_com_android_test_hwui_FrontBufferedLayer_nDestroy(JNIEnv*, jclass, jlong ptr) {
53     MyWrapper* wrapper = reinterpret_cast<MyWrapper*>(ptr);
54     delete wrapper;
55 }
56 
Java_com_android_test_hwui_FrontBufferedLayer_nUpdateBuffer(JNIEnv * env,jclass,jlong ptr,jobject jbuffer)57 extern "C" JNIEXPORT void JNICALL Java_com_android_test_hwui_FrontBufferedLayer_nUpdateBuffer(
58         JNIEnv* env, jclass, jlong ptr, jobject jbuffer) {
59     MyWrapper* wrapper = reinterpret_cast<MyWrapper*>(ptr);
60     AHardwareBuffer* buffer = AHardwareBuffer_fromHardwareBuffer(env, jbuffer);
61     wrapper->setBuffer(buffer);
62 }