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 | |
| 19 | #include <gui/Surface.h> |
| 20 | #include <utils/Macros.h> |
| 21 | #include <utils/StrongPointer.h> |
| 22 | |
| 23 | #include <memory> |
| 24 | |
| 25 | namespace android::uirenderer::renderthread { |
| 26 | |
| 27 | class ReliableSurface : public ANativeObjectBase<ANativeWindow, ReliableSurface, RefBase> { |
| 28 | PREVENT_COPY_AND_ASSIGN(ReliableSurface); |
| 29 | |
| 30 | public: |
| 31 | ReliableSurface(sp<Surface>&& surface); |
| 32 | ~ReliableSurface(); |
| 33 | |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 34 | int reserveNext(); |
| 35 | |
| 36 | void allocateBuffers() { mSurface->allocateBuffers(); } |
| 37 | |
| 38 | int query(int what, int* value) const { return mSurface->query(what, value); } |
| 39 | |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 40 | uint64_t getNextFrameNumber() const { return mSurface->getNextFrameNumber(); } |
| 41 | |
John Reck | 59dd2ea | 2019-07-26 16:51:08 -0700 | [diff] [blame] | 42 | int getAndClearError() { |
| 43 | int ret = mBufferQueueState; |
| 44 | mBufferQueueState = OK; |
| 45 | return ret; |
| 46 | } |
| 47 | |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 48 | status_t getFrameTimestamps(uint64_t frameNumber, |
| 49 | nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime, |
| 50 | nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime, |
| 51 | nsecs_t* outLastRefreshStartTime, nsecs_t* outGlCompositionDoneTime, |
| 52 | nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime, |
| 53 | nsecs_t* outReleaseTime) { |
| 54 | return mSurface->getFrameTimestamps(frameNumber, outRequestedPresentTime, outAcquireTime, |
| 55 | outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime, |
| 56 | outGlCompositionDoneTime, outDisplayPresentTime, outDequeueReadyTime, outReleaseTime); |
| 57 | } |
| 58 | |
| 59 | void enableFrameTimestamps(bool enable) { |
| 60 | return mSurface->enableFrameTimestamps(enable); |
| 61 | } |
| 62 | |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 63 | private: |
| 64 | const sp<Surface> mSurface; |
| 65 | |
| 66 | mutable std::mutex mMutex; |
| 67 | |
| 68 | uint64_t mUsage = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER; |
| 69 | PixelFormat mFormat = PIXEL_FORMAT_RGBA_8888; |
| 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; |
John Reck | 59dd2ea | 2019-07-26 16:51:08 -0700 | [diff] [blame] | 75 | int mBufferQueueState = OK; |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 76 | |
| 77 | bool isFallbackBuffer(const ANativeWindowBuffer* windowBuffer) const; |
John Reck | 59dd2ea | 2019-07-26 16:51:08 -0700 | [diff] [blame] | 78 | ANativeWindowBuffer* acquireFallbackBuffer(int error); |
John Reck | 848f651 | 2018-12-03 13:26:43 -0800 | [diff] [blame] | 79 | void clearReservedBuffer(); |
| 80 | |
| 81 | void perform(int operation, va_list args); |
| 82 | int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd); |
| 83 | int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd); |
| 84 | int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd); |
| 85 | |
| 86 | static Surface* getWrapped(const ANativeWindow*); |
| 87 | |
| 88 | // ANativeWindow hooks |
| 89 | static int hook_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd); |
| 90 | static int hook_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer, |
| 91 | int* fenceFd); |
| 92 | static int hook_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd); |
| 93 | |
| 94 | static int hook_perform(ANativeWindow* window, int operation, ...); |
| 95 | static int hook_query(const ANativeWindow* window, int what, int* value); |
| 96 | static int hook_setSwapInterval(ANativeWindow* window, int interval); |
| 97 | |
| 98 | static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window, ANativeWindowBuffer* buffer); |
| 99 | static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window, ANativeWindowBuffer** buffer); |
| 100 | static int hook_lockBuffer_DEPRECATED(ANativeWindow* window, ANativeWindowBuffer* buffer); |
| 101 | static int hook_queueBuffer_DEPRECATED(ANativeWindow* window, ANativeWindowBuffer* buffer); |
| 102 | }; |
| 103 | |
Alec Mouri | 8d0c5bd2 | 2019-08-22 19:20:41 -0700 | [diff] [blame] | 104 | }; // namespace android::uirenderer::renderthread |