blob: 0d251b1f10e75aae368afb899717d3141f7ecc67 [file] [log] [blame]
John Reck848f6512018-12-03 13:26:43 -08001/*
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
25namespace android::uirenderer::renderthread {
26
27class ReliableSurface : public ANativeObjectBase<ANativeWindow, ReliableSurface, RefBase> {
28 PREVENT_COPY_AND_ASSIGN(ReliableSurface);
29
30public:
31 ReliableSurface(sp<Surface>&& surface);
32 ~ReliableSurface();
33
34 void setDequeueTimeout(nsecs_t timeout) { mSurface->setDequeueTimeout(timeout); }
35
36 int reserveNext();
37
38 void allocateBuffers() { mSurface->allocateBuffers(); }
39
40 int query(int what, int* value) const { return mSurface->query(what, value); }
41
John Reck848f6512018-12-03 13:26:43 -080042 uint64_t getNextFrameNumber() const { return mSurface->getNextFrameNumber(); }
43
John Reck59dd2ea2019-07-26 16:51:08 -070044 int getAndClearError() {
45 int ret = mBufferQueueState;
46 mBufferQueueState = OK;
47 return ret;
48 }
49
Stan Iliev7203e1f2019-07-25 13:12:02 -040050 status_t getFrameTimestamps(uint64_t frameNumber,
51 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
52 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
53 nsecs_t* outLastRefreshStartTime, nsecs_t* outGlCompositionDoneTime,
54 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
55 nsecs_t* outReleaseTime) {
56 return mSurface->getFrameTimestamps(frameNumber, outRequestedPresentTime, outAcquireTime,
57 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
58 outGlCompositionDoneTime, outDisplayPresentTime, outDequeueReadyTime, outReleaseTime);
59 }
60
61 void enableFrameTimestamps(bool enable) {
62 return mSurface->enableFrameTimestamps(enable);
63 }
64
John Reck848f6512018-12-03 13:26:43 -080065private:
66 const sp<Surface> mSurface;
67
68 mutable std::mutex mMutex;
69
70 uint64_t mUsage = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
71 PixelFormat mFormat = PIXEL_FORMAT_RGBA_8888;
72 std::unique_ptr<AHardwareBuffer, void (*)(AHardwareBuffer*)> mScratchBuffer{
73 nullptr, AHardwareBuffer_release};
74 ANativeWindowBuffer* mReservedBuffer = nullptr;
75 base::unique_fd mReservedFenceFd;
76 bool mHasDequeuedBuffer = false;
John Reck59dd2ea2019-07-26 16:51:08 -070077 int mBufferQueueState = OK;
John Reck848f6512018-12-03 13:26:43 -080078
79 bool isFallbackBuffer(const ANativeWindowBuffer* windowBuffer) const;
John Reck59dd2ea2019-07-26 16:51:08 -070080 ANativeWindowBuffer* acquireFallbackBuffer(int error);
John Reck848f6512018-12-03 13:26:43 -080081 void clearReservedBuffer();
82
83 void perform(int operation, va_list args);
84 int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd);
85 int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd);
86 int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd);
87
88 static Surface* getWrapped(const ANativeWindow*);
89
90 // ANativeWindow hooks
91 static int hook_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd);
92 static int hook_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer,
93 int* fenceFd);
94 static int hook_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd);
95
96 static int hook_perform(ANativeWindow* window, int operation, ...);
97 static int hook_query(const ANativeWindow* window, int what, int* value);
98 static int hook_setSwapInterval(ANativeWindow* window, int interval);
99
100 static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window, ANativeWindowBuffer* buffer);
101 static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window, ANativeWindowBuffer** buffer);
102 static int hook_lockBuffer_DEPRECATED(ANativeWindow* window, ANativeWindowBuffer* buffer);
103 static int hook_queueBuffer_DEPRECATED(ANativeWindow* window, ANativeWindowBuffer* buffer);
104};
105
Alec Mouri8d0c5bd22019-08-22 19:20:41 -0700106}; // namespace android::uirenderer::renderthread