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 19 #include <android-base/unique_fd.h> 20 #include <system/window.h> 21 #include <apex/window.h> 22 #include <utils/Errors.h> 23 #include <utils/Macros.h> 24 #include <utils/StrongPointer.h> 25 26 #include <memory> 27 #include <mutex> 28 29 namespace android::uirenderer::renderthread { 30 31 class ReliableSurface { 32 PREVENT_COPY_AND_ASSIGN(ReliableSurface); 33 34 public: 35 ReliableSurface(ANativeWindow* window); 36 ~ReliableSurface(); 37 38 // Performs initialization that is not safe to do in the constructor. 39 // For instance, registering ANativeWindow interceptors with ReliableSurface 40 // passed as the data pointer is not safe. 41 void init(); 42 getNativeWindow()43 ANativeWindow* getNativeWindow() { return mWindow; } 44 45 int reserveNext(); 46 getAndClearError()47 int getAndClearError() { 48 int ret = mBufferQueueState; 49 mBufferQueueState = OK; 50 return ret; 51 } 52 setExtraBufferCount(size_t extraBuffers)53 void setExtraBufferCount(size_t extraBuffers) { 54 std::lock_guard _lock{mMutex}; 55 mExtraBuffers = extraBuffers; 56 } 57 didSetExtraBuffers()58 bool didSetExtraBuffers() const { 59 std::lock_guard _lock{mMutex}; 60 return mDidSetExtraBuffers; 61 } 62 63 private: 64 ANativeWindow* mWindow; 65 66 mutable std::mutex mMutex; 67 68 uint64_t mUsage = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER; 69 AHardwareBuffer_Format mFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM; 70 std::unique_ptr<AHardwareBuffer, void (*)(AHardwareBuffer*)> mScratchBuffer{ 71 nullptr, AHardwareBuffer_release}; 72 ANativeWindowBuffer* mReservedBuffer = nullptr; 73 base::unique_fd mReservedFenceFd; 74 bool mHasDequeuedBuffer = false; 75 int mBufferQueueState = OK; 76 size_t mExtraBuffers = 0; 77 size_t mExpectedBufferCount = 0; 78 bool mDidSetExtraBuffers = false; 79 80 bool isFallbackBuffer(const ANativeWindowBuffer* windowBuffer) const; 81 ANativeWindowBuffer* acquireFallbackBuffer(int error); 82 void clearReservedBuffer(); 83 84 // ANativeWindow hooks. When an ANativeWindow_* method is called on the 85 // underlying ANativeWindow, these methods will intercept the original call. 86 // For example, an EGL driver would call into these hooks instead of the 87 // original methods. 88 static int hook_cancelBuffer(ANativeWindow* window, ANativeWindow_cancelBufferFn cancelBuffer, 89 void* data, ANativeWindowBuffer* buffer, int fenceFd); 90 static int hook_dequeueBuffer(ANativeWindow* window, 91 ANativeWindow_dequeueBufferFn dequeueBuffer, void* data, 92 ANativeWindowBuffer** buffer, int* fenceFd); 93 static int hook_queueBuffer(ANativeWindow* window, ANativeWindow_queueBufferFn queueBuffer, 94 void* data, ANativeWindowBuffer* buffer, int fenceFd); 95 96 static int hook_perform(ANativeWindow* window, ANativeWindow_performFn perform, void* data, 97 int operation, va_list args); 98 static int hook_query(const ANativeWindow* window, ANativeWindow_queryFn query, void* data, 99 int what, int* value); 100 }; 101 102 }; // namespace android::uirenderer::renderthread 103