blob: 407d4bf7633673b2af6a14c272ecbe126e25d26e [file] [log] [blame]
Angel Aguayo8baf6fa2021-10-01 00:39:49 +00001/*
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
24struct MyWrapper {
25 MyWrapper(ANativeWindow* parent) {
26 surfaceControl = ASurfaceControl_createFromWindow(parent, "PenLayer");
27 }
28
29 ~MyWrapper() { ASurfaceControl_release(surfaceControl); }
30
31 void setBuffer(AHardwareBuffer* buffer) {
32 ASurfaceTransaction* transaction = ASurfaceTransaction_create();
33 ASurfaceTransaction_setBuffer(transaction, surfaceControl, buffer);
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
43extern "C" JNIEXPORT jlong JNICALL
Nader Jawad454bbcd2021-11-05 12:06:08 -070044Java_com_android_test_hwui_FrontBufferedLayer_nCreate(JNIEnv* env, jclass, jobject jSurface) {
Angel Aguayo8baf6fa2021-10-01 00:39:49 +000045 ANativeWindow* window = ANativeWindow_fromSurface(env, jSurface);
46 MyWrapper* wrapper = new MyWrapper(window);
47 ANativeWindow_release(window);
48 return reinterpret_cast<jlong>(wrapper);
49}
50
51extern "C" JNIEXPORT void JNICALL
Nader Jawad454bbcd2021-11-05 12:06:08 -070052Java_com_android_test_hwui_FrontBufferedLayer_nDestroy(JNIEnv*, jclass, jlong ptr) {
Angel Aguayo8baf6fa2021-10-01 00:39:49 +000053 MyWrapper* wrapper = reinterpret_cast<MyWrapper*>(ptr);
54 delete wrapper;
55}
56
57extern "C" JNIEXPORT void JNICALL Java_com_android_test_hwui_FrontBufferedLayer_nUpdateBuffer(
Nader Jawad454bbcd2021-11-05 12:06:08 -070058 JNIEnv* env, jclass, jlong ptr, jobject jbuffer) {
Angel Aguayo8baf6fa2021-10-01 00:39:49 +000059 MyWrapper* wrapper = reinterpret_cast<MyWrapper*>(ptr);
60 AHardwareBuffer* buffer = AHardwareBuffer_fromHardwareBuffer(env, jbuffer);
61 wrapper->setBuffer(buffer);
62}