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 | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame^] | 19 | #include <apex/window.h> |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 20 | #include <gui/Surface.h> |
| 21 | #include <utils/Macros.h> |
| 22 | #include <utils/StrongPointer.h> |
| 23 | |
| 24 | #include <memory> |
| 25 | |
| 26 | namespace android::uirenderer::renderthread { |
| 27 | |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame^] | 28 | class ReliableSurface { |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 29 | PREVENT_COPY_AND_ASSIGN(ReliableSurface); |
| 30 | |
| 31 | public: |
| 32 | ReliableSurface(sp<Surface>&& surface); |
| 33 | ~ReliableSurface(); |
| 34 | |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame^] | 35 | // Performs initialization that is not safe to do in the constructor. |
| 36 | // For instance, registering ANativeWindow interceptors with ReliableSurface |
| 37 | // passed as the data pointer is not safe. |
| 38 | void init(); |
| 39 | |
| 40 | ANativeWindow* getNativeWindow() { return mSurface.get(); } |
| 41 | |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 42 | int reserveNext(); |
| 43 | |
| 44 | void allocateBuffers() { mSurface->allocateBuffers(); } |
| 45 | |
| 46 | int query(int what, int* value) const { return mSurface->query(what, value); } |
| 47 | |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 48 | uint64_t getNextFrameNumber() const { return mSurface->getNextFrameNumber(); } |
| 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 | |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 56 | status_t getFrameTimestamps(uint64_t frameNumber, |
| 57 | nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime, |
| 58 | nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime, |
| 59 | nsecs_t* outLastRefreshStartTime, nsecs_t* outGlCompositionDoneTime, |
| 60 | nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime, |
| 61 | nsecs_t* outReleaseTime) { |
| 62 | return mSurface->getFrameTimestamps(frameNumber, outRequestedPresentTime, outAcquireTime, |
| 63 | outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime, |
| 64 | outGlCompositionDoneTime, outDisplayPresentTime, outDequeueReadyTime, outReleaseTime); |
| 65 | } |
| 66 | |
| 67 | void enableFrameTimestamps(bool enable) { |
| 68 | return mSurface->enableFrameTimestamps(enable); |
| 69 | } |
| 70 | |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 71 | private: |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame^] | 72 | sp<Surface> mSurface; |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 73 | |
| 74 | mutable std::mutex mMutex; |
| 75 | |
| 76 | uint64_t mUsage = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER; |
| 77 | PixelFormat mFormat = PIXEL_FORMAT_RGBA_8888; |
| 78 | std::unique_ptr<AHardwareBuffer, void (*)(AHardwareBuffer*)> mScratchBuffer{ |
| 79 | nullptr, AHardwareBuffer_release}; |
| 80 | ANativeWindowBuffer* mReservedBuffer = nullptr; |
| 81 | base::unique_fd mReservedFenceFd; |
| 82 | bool mHasDequeuedBuffer = false; |
John Reck | 59dd2ea | 2019-07-26 16:51:08 -0700 | [diff] [blame] | 83 | int mBufferQueueState = OK; |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 84 | |
| 85 | bool isFallbackBuffer(const ANativeWindowBuffer* windowBuffer) const; |
John Reck | 59dd2ea | 2019-07-26 16:51:08 -0700 | [diff] [blame] | 86 | ANativeWindowBuffer* acquireFallbackBuffer(int error); |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 87 | void clearReservedBuffer(); |
| 88 | |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame^] | 89 | // ANativeWindow hooks. When an ANativeWindow_* method is called on the |
| 90 | // underlying ANativeWindow, these methods will intercept the original call. |
| 91 | // For example, an EGL driver would call into these hooks instead of the |
| 92 | // original methods. |
| 93 | static int hook_cancelBuffer(ANativeWindow* window, ANativeWindow_cancelBufferFn cancelBuffer, |
| 94 | void* data, ANativeWindowBuffer* buffer, int fenceFd); |
| 95 | static int hook_dequeueBuffer(ANativeWindow* window, |
| 96 | ANativeWindow_dequeueBufferFn dequeueBuffer, void* data, |
| 97 | ANativeWindowBuffer** buffer, int* fenceFd); |
| 98 | static int hook_queueBuffer(ANativeWindow* window, ANativeWindow_queueBufferFn queueBuffer, |
| 99 | void* data, ANativeWindowBuffer* buffer, int fenceFd); |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 100 | |
Alec Mouri | f023a32 | 2019-11-25 10:02:21 -0800 | [diff] [blame^] | 101 | static int hook_perform(ANativeWindow* window, ANativeWindow_performFn perform, void* data, |
| 102 | int operation, va_list args); |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
Alec Mouri | 8d0c5bd2 | 2019-08-22 19:20:41 -0700 | [diff] [blame] | 105 | }; // namespace android::uirenderer::renderthread |