blob: d6a4d50d33273b26be29a29229ab15a7e1563be9 [file] [log] [blame]
John Reck848f6512018-12-03 13:26:43 -08001/*
2 * Copyright (C) 2018 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#pragma once
18
Alec Mouri43fe6fc2019-12-23 07:46:19 -080019#include <android-base/unique_fd.h>
John Reck8ddbc592020-05-07 16:11:18 -070020#include <system/window.h>
Alec Mourif023a322019-11-25 10:02:21 -080021#include <apex/window.h>
Alec Mouri43fe6fc2019-12-23 07:46:19 -080022#include <utils/Errors.h>
John Reck848f6512018-12-03 13:26:43 -080023#include <utils/Macros.h>
Jerome Gaillardd26915c2024-03-26 12:05:52 +000024#ifdef __ANDROID__
Alec Mouri45238012020-01-29 11:04:40 -080025#include <utils/NdkUtils.h>
Jerome Gaillardd26915c2024-03-26 12:05:52 +000026#endif
John Reck848f6512018-12-03 13:26:43 -080027#include <utils/StrongPointer.h>
28
29#include <memory>
Alec Mouri43fe6fc2019-12-23 07:46:19 -080030#include <mutex>
John Reck848f6512018-12-03 13:26:43 -080031
32namespace android::uirenderer::renderthread {
33
Alec Mourif023a322019-11-25 10:02:21 -080034class ReliableSurface {
John Reck848f6512018-12-03 13:26:43 -080035 PREVENT_COPY_AND_ASSIGN(ReliableSurface);
36
37public:
Alec Mouri43fe6fc2019-12-23 07:46:19 -080038 ReliableSurface(ANativeWindow* window);
John Reck848f6512018-12-03 13:26:43 -080039 ~ReliableSurface();
40
Alec Mourif023a322019-11-25 10:02:21 -080041 // Performs initialization that is not safe to do in the constructor.
42 // For instance, registering ANativeWindow interceptors with ReliableSurface
43 // passed as the data pointer is not safe.
44 void init();
45
Alec Mouri43fe6fc2019-12-23 07:46:19 -080046 ANativeWindow* getNativeWindow() { return mWindow; }
Alec Mourif023a322019-11-25 10:02:21 -080047
John Reck848f6512018-12-03 13:26:43 -080048 int reserveNext();
49
John Reck59dd2ea2019-07-26 16:51:08 -070050 int getAndClearError() {
51 int ret = mBufferQueueState;
52 mBufferQueueState = OK;
53 return ret;
54 }
55
John Reck8ddbc592020-05-07 16:11:18 -070056 bool didSetExtraBuffers() const {
57 std::lock_guard _lock{mMutex};
58 return mDidSetExtraBuffers;
59 }
60
John Reck848f6512018-12-03 13:26:43 -080061private:
Alec Mouri43fe6fc2019-12-23 07:46:19 -080062 ANativeWindow* mWindow;
John Reck848f6512018-12-03 13:26:43 -080063
64 mutable std::mutex mMutex;
65
66 uint64_t mUsage = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
Jerome Gaillardd26915c2024-03-26 12:05:52 +000067#ifdef __ANDROID__
Alec Mouri43fe6fc2019-12-23 07:46:19 -080068 AHardwareBuffer_Format mFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
Alec Mouri45238012020-01-29 11:04:40 -080069 UniqueAHardwareBuffer mScratchBuffer;
John Reck848f6512018-12-03 13:26:43 -080070 ANativeWindowBuffer* mReservedBuffer = nullptr;
Jerome Gaillardd26915c2024-03-26 12:05:52 +000071#endif
John Reck848f6512018-12-03 13:26:43 -080072 base::unique_fd mReservedFenceFd;
73 bool mHasDequeuedBuffer = false;
John Reck59dd2ea2019-07-26 16:51:08 -070074 int mBufferQueueState = OK;
John Reck8ddbc592020-05-07 16:11:18 -070075 size_t mExpectedBufferCount = 0;
76 bool mDidSetExtraBuffers = false;
John Reck848f6512018-12-03 13:26:43 -080077
78 bool isFallbackBuffer(const ANativeWindowBuffer* windowBuffer) const;
John Reck59dd2ea2019-07-26 16:51:08 -070079 ANativeWindowBuffer* acquireFallbackBuffer(int error);
John Reck848f6512018-12-03 13:26:43 -080080 void clearReservedBuffer();
81
Alec Mourif023a322019-11-25 10:02:21 -080082 // ANativeWindow hooks. When an ANativeWindow_* method is called on the
83 // underlying ANativeWindow, these methods will intercept the original call.
84 // For example, an EGL driver would call into these hooks instead of the
85 // original methods.
86 static int hook_cancelBuffer(ANativeWindow* window, ANativeWindow_cancelBufferFn cancelBuffer,
87 void* data, ANativeWindowBuffer* buffer, int fenceFd);
88 static int hook_dequeueBuffer(ANativeWindow* window,
89 ANativeWindow_dequeueBufferFn dequeueBuffer, void* data,
90 ANativeWindowBuffer** buffer, int* fenceFd);
91 static int hook_queueBuffer(ANativeWindow* window, ANativeWindow_queueBufferFn queueBuffer,
92 void* data, ANativeWindowBuffer* buffer, int fenceFd);
John Reck848f6512018-12-03 13:26:43 -080093
Alec Mourif023a322019-11-25 10:02:21 -080094 static int hook_perform(ANativeWindow* window, ANativeWindow_performFn perform, void* data,
95 int operation, va_list args);
John Reck8ddbc592020-05-07 16:11:18 -070096 static int hook_query(const ANativeWindow* window, ANativeWindow_queryFn query, void* data,
97 int what, int* value);
John Reck848f6512018-12-03 13:26:43 -080098};
99
Alec Mouri8d0c5bd22019-08-22 19:20:41 -0700100}; // namespace android::uirenderer::renderthread