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> |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame] | 20 | #include <apex/window.h> |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame^] | 21 | #include <utils/Errors.h> |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 22 | #include <utils/Macros.h> |
| 23 | #include <utils/StrongPointer.h> |
| 24 | |
| 25 | #include <memory> |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame^] | 26 | #include <mutex> |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 27 | |
| 28 | namespace android::uirenderer::renderthread { |
| 29 | |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame] | 30 | class ReliableSurface { |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 31 | PREVENT_COPY_AND_ASSIGN(ReliableSurface); |
| 32 | |
| 33 | public: |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame^] | 34 | ReliableSurface(ANativeWindow* window); |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 35 | ~ReliableSurface(); |
| 36 | |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame] | 37 | // Performs initialization that is not safe to do in the constructor. |
| 38 | // For instance, registering ANativeWindow interceptors with ReliableSurface |
| 39 | // passed as the data pointer is not safe. |
| 40 | void init(); |
| 41 | |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame^] | 42 | ANativeWindow* getNativeWindow() { return mWindow; } |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame] | 43 | |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 44 | int reserveNext(); |
| 45 | |
John Reck | 59dd2ea | 2019-07-26 16:51:08 -0700 | [diff] [blame] | 46 | int getAndClearError() { |
| 47 | int ret = mBufferQueueState; |
| 48 | mBufferQueueState = OK; |
| 49 | return ret; |
| 50 | } |
| 51 | |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 52 | private: |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame^] | 53 | ANativeWindow* mWindow; |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 54 | |
| 55 | mutable std::mutex mMutex; |
| 56 | |
| 57 | uint64_t mUsage = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER; |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame^] | 58 | AHardwareBuffer_Format mFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM; |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 59 | std::unique_ptr<AHardwareBuffer, void (*)(AHardwareBuffer*)> mScratchBuffer{ |
| 60 | nullptr, AHardwareBuffer_release}; |
| 61 | ANativeWindowBuffer* mReservedBuffer = nullptr; |
| 62 | base::unique_fd mReservedFenceFd; |
| 63 | bool mHasDequeuedBuffer = false; |
John Reck | 59dd2ea | 2019-07-26 16:51:08 -0700 | [diff] [blame] | 64 | int mBufferQueueState = OK; |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 65 | |
| 66 | bool isFallbackBuffer(const ANativeWindowBuffer* windowBuffer) const; |
John Reck | 59dd2ea | 2019-07-26 16:51:08 -0700 | [diff] [blame] | 67 | ANativeWindowBuffer* acquireFallbackBuffer(int error); |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 68 | void clearReservedBuffer(); |
| 69 | |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame] | 70 | // ANativeWindow hooks. When an ANativeWindow_* method is called on the |
| 71 | // underlying ANativeWindow, these methods will intercept the original call. |
| 72 | // For example, an EGL driver would call into these hooks instead of the |
| 73 | // original methods. |
| 74 | static int hook_cancelBuffer(ANativeWindow* window, ANativeWindow_cancelBufferFn cancelBuffer, |
| 75 | void* data, ANativeWindowBuffer* buffer, int fenceFd); |
| 76 | static int hook_dequeueBuffer(ANativeWindow* window, |
| 77 | ANativeWindow_dequeueBufferFn dequeueBuffer, void* data, |
| 78 | ANativeWindowBuffer** buffer, int* fenceFd); |
| 79 | static int hook_queueBuffer(ANativeWindow* window, ANativeWindow_queueBufferFn queueBuffer, |
| 80 | void* data, ANativeWindowBuffer* buffer, int fenceFd); |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 81 | |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame] | 82 | static int hook_perform(ANativeWindow* window, ANativeWindow_performFn perform, void* data, |
| 83 | int operation, va_list args); |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
Alec Mouri | 8d0c5bd2 | 2019-08-22 19:20:41 -0700 | [diff] [blame] | 86 | }; // namespace android::uirenderer::renderthread |