blob: f768df37ba7dd9a79341cb14fc4f98ad923e4bc2 [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
John Reck848f6512018-12-03 13:26:43 -080034 int reserveNext();
35
36 void allocateBuffers() { mSurface->allocateBuffers(); }
37
38 int query(int what, int* value) const { return mSurface->query(what, value); }
39
John Reck848f6512018-12-03 13:26:43 -080040 uint64_t getNextFrameNumber() const { return mSurface->getNextFrameNumber(); }
41
John Reck59dd2ea2019-07-26 16:51:08 -070042 int getAndClearError() {
43 int ret = mBufferQueueState;
44 mBufferQueueState = OK;
45 return ret;
46 }
47
Stan Iliev7203e1f2019-07-25 13:12:02 -040048 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 Reck848f6512018-12-03 13:26:43 -080063private:
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 Reck59dd2ea2019-07-26 16:51:08 -070075 int mBufferQueueState = OK;
John Reck848f6512018-12-03 13:26:43 -080076
77 bool isFallbackBuffer(const ANativeWindowBuffer* windowBuffer) const;
John Reck59dd2ea2019-07-26 16:51:08 -070078 ANativeWindowBuffer* acquireFallbackBuffer(int error);
John Reck848f6512018-12-03 13:26:43 -080079 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 Mouri8d0c5bd22019-08-22 19:20:41 -0700104}; // namespace android::uirenderer::renderthread