John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 1 | /* |
| 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 Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 19 | #include <android-base/unique_fd.h> |
John Reck | 8ddbc59 | 2020-05-07 16:11:18 -0700 | [diff] [blame] | 20 | #include <system/window.h> |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame] | 21 | #include <apex/window.h> |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 22 | #include <utils/Errors.h> |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 23 | #include <utils/Macros.h> |
Jerome Gaillard | d26915c | 2024-03-26 12:05:52 +0000 | [diff] [blame] | 24 | #ifdef __ANDROID__ |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 25 | #include <utils/NdkUtils.h> |
Jerome Gaillard | d26915c | 2024-03-26 12:05:52 +0000 | [diff] [blame] | 26 | #endif |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 27 | #include <utils/StrongPointer.h> |
| 28 | |
| 29 | #include <memory> |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 30 | #include <mutex> |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 31 | |
| 32 | namespace android::uirenderer::renderthread { |
| 33 | |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame] | 34 | class ReliableSurface { |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 35 | PREVENT_COPY_AND_ASSIGN(ReliableSurface); |
| 36 | |
| 37 | public: |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 38 | ReliableSurface(ANativeWindow* window); |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 39 | ~ReliableSurface(); |
| 40 | |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame] | 41 | // 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 Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 46 | ANativeWindow* getNativeWindow() { return mWindow; } |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame] | 47 | |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 48 | int reserveNext(); |
| 49 | |
John Reck | 59dd2ea | 2019-07-26 16:51:08 -0700 | [diff] [blame] | 50 | int getAndClearError() { |
| 51 | int ret = mBufferQueueState; |
| 52 | mBufferQueueState = OK; |
| 53 | return ret; |
| 54 | } |
| 55 | |
John Reck | 8ddbc59 | 2020-05-07 16:11:18 -0700 | [diff] [blame] | 56 | bool didSetExtraBuffers() const { |
| 57 | std::lock_guard _lock{mMutex}; |
| 58 | return mDidSetExtraBuffers; |
| 59 | } |
| 60 | |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 61 | private: |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 62 | ANativeWindow* mWindow; |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 63 | |
| 64 | mutable std::mutex mMutex; |
| 65 | |
| 66 | uint64_t mUsage = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER; |
Jerome Gaillard | d26915c | 2024-03-26 12:05:52 +0000 | [diff] [blame] | 67 | #ifdef __ANDROID__ |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 68 | AHardwareBuffer_Format mFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM; |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 69 | UniqueAHardwareBuffer mScratchBuffer; |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 70 | ANativeWindowBuffer* mReservedBuffer = nullptr; |
Jerome Gaillard | d26915c | 2024-03-26 12:05:52 +0000 | [diff] [blame] | 71 | #endif |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 72 | base::unique_fd mReservedFenceFd; |
| 73 | bool mHasDequeuedBuffer = false; |
John Reck | 59dd2ea | 2019-07-26 16:51:08 -0700 | [diff] [blame] | 74 | int mBufferQueueState = OK; |
John Reck | 8ddbc59 | 2020-05-07 16:11:18 -0700 | [diff] [blame] | 75 | size_t mExpectedBufferCount = 0; |
| 76 | bool mDidSetExtraBuffers = false; |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 77 | |
| 78 | bool isFallbackBuffer(const ANativeWindowBuffer* windowBuffer) const; |
John Reck | 59dd2ea | 2019-07-26 16:51:08 -0700 | [diff] [blame] | 79 | ANativeWindowBuffer* acquireFallbackBuffer(int error); |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 80 | void clearReservedBuffer(); |
| 81 | |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame] | 82 | // 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 Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 93 | |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame] | 94 | static int hook_perform(ANativeWindow* window, ANativeWindow_performFn perform, void* data, |
| 95 | int operation, va_list args); |
John Reck | 8ddbc59 | 2020-05-07 16:11:18 -0700 | [diff] [blame] | 96 | static int hook_query(const ANativeWindow* window, ANativeWindow_queryFn query, void* data, |
| 97 | int what, int* value); |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
Alec Mouri | 8d0c5bd2 | 2019-08-22 19:20:41 -0700 | [diff] [blame] | 100 | }; // namespace android::uirenderer::renderthread |