blob: 0c86524293e772f7e17dde1ffa312cdadbdab6c6 [file] [log] [blame]
Vishnu Nair57928e62020-09-16 19:01:04 -07001/*
2 * Copyright (C) 2020 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/log.h>
18#include <android/native_window.h>
19#include <android/native_window_jni.h>
20#include <android/window.h>
21#include <gui/Surface.h>
22#include <jni.h>
23#include <system/window.h>
24#include <utils/RefBase.h>
25#include <cassert>
26#include <chrono>
27#include <thread>
28
29#define TAG "SurfaceViewBufferTests"
30#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
31
32extern "C" {
33int i = 0;
34static ANativeWindow* sAnw;
35
36JNIEXPORT jint JNICALL Java_com_android_test_SurfaceProxy_setSurface(JNIEnv* env, jclass,
37 jobject surfaceObject) {
38 sAnw = ANativeWindow_fromSurface(env, surfaceObject);
39 assert(sAnw);
40 android::sp<android::Surface> surface = static_cast<android::Surface*>(sAnw);
41 surface->enableFrameTimestamps(true);
42 return 0;
43}
44
45JNIEXPORT jint JNICALL Java_com_android_test_SurfaceProxy_waitUntilBufferDisplayed(
46 JNIEnv*, jclass, jint jFrameNumber, jint timeoutSec) {
47 using namespace std::chrono_literals;
48 assert(sAnw);
49 android::sp<android::Surface> surface = static_cast<android::Surface*>(sAnw);
50
51 uint64_t frameNumber = static_cast<uint64_t>(jFrameNumber);
52 nsecs_t outRequestedPresentTime, outAcquireTime, outLatchTime, outFirstRefreshStartTime;
53 nsecs_t outLastRefreshStartTime, outGlCompositionDoneTime, outDequeueReadyTime;
54 nsecs_t outDisplayPresentTime = -1;
55 nsecs_t outReleaseTime;
56
57 auto start = std::chrono::steady_clock::now();
58 while (outDisplayPresentTime < 0) {
59 std::this_thread::sleep_for(8ms);
60 surface->getFrameTimestamps(frameNumber, &outRequestedPresentTime, &outAcquireTime,
61 &outLatchTime, &outFirstRefreshStartTime,
62 &outLastRefreshStartTime, &outGlCompositionDoneTime,
63 &outDisplayPresentTime, &outDequeueReadyTime, &outReleaseTime);
64 if (outDisplayPresentTime < 0) {
65 auto end = std::chrono::steady_clock::now();
66 if (std::chrono::duration_cast<std::chrono::seconds>(end - start).count() >
67 timeoutSec) {
68 return -1;
69 }
70 }
71 }
72 return 0;
73}
74
75JNIEXPORT jint JNICALL Java_com_android_test_SurfaceProxy_draw(JNIEnv*, jclass) {
76 assert(sAnw);
77 ANativeWindow_Buffer outBuffer;
78 ANativeWindow_lock(sAnw, &outBuffer, nullptr);
79 return 0;
80}
81
82JNIEXPORT jint JNICALL Java_com_android_test_SurfaceProxy_ANativeWindowLock(JNIEnv*, jclass) {
83 assert(sAnw);
84 ANativeWindow_Buffer outBuffer;
85 ANativeWindow_lock(sAnw, &outBuffer, nullptr);
86 return 0;
87}
88
89JNIEXPORT jint JNICALL Java_com_android_test_SurfaceProxy_ANativeWindowUnlockAndPost(JNIEnv*,
90 jclass) {
91 assert(sAnw);
92 ANativeWindow_unlockAndPost(sAnw);
93 return 0;
94}
95
96JNIEXPORT jint JNICALL Java_com_android_test_SurfaceProxy_ANativeWindowSetBuffersGeometry(
97 JNIEnv* /* env */, jclass /* clazz */, jobject /* surfaceObject */, jint w, jint h,
98 jint format) {
99 assert(sAnw);
100 return ANativeWindow_setBuffersGeometry(sAnw, w, h, format);
101}
102}