blob: 32472539f616cf9c5810f21aa306d3959e635236 [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
Alec Mourif023a322019-11-25 10:02:21 -080019#include <apex/window.h>
John Reck848f6512018-12-03 13:26:43 -080020#include <gui/Surface.h>
21#include <utils/Macros.h>
22#include <utils/StrongPointer.h>
23
24#include <memory>
25
26namespace android::uirenderer::renderthread {
27
Alec Mourif023a322019-11-25 10:02:21 -080028class ReliableSurface {
John Reck848f6512018-12-03 13:26:43 -080029 PREVENT_COPY_AND_ASSIGN(ReliableSurface);
30
31public:
32 ReliableSurface(sp<Surface>&& surface);
33 ~ReliableSurface();
34
Alec Mourif023a322019-11-25 10:02:21 -080035 // 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 Reck848f6512018-12-03 13:26:43 -080042 int reserveNext();
43
John Reck848f6512018-12-03 13:26:43 -080044 int query(int what, int* value) const { return mSurface->query(what, value); }
45
John Reck848f6512018-12-03 13:26:43 -080046 uint64_t getNextFrameNumber() const { return mSurface->getNextFrameNumber(); }
47
John Reck59dd2ea2019-07-26 16:51:08 -070048 int getAndClearError() {
49 int ret = mBufferQueueState;
50 mBufferQueueState = OK;
51 return ret;
52 }
53
Stan Iliev7203e1f2019-07-25 13:12:02 -040054 status_t getFrameTimestamps(uint64_t frameNumber,
55 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
56 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
57 nsecs_t* outLastRefreshStartTime, nsecs_t* outGlCompositionDoneTime,
58 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
59 nsecs_t* outReleaseTime) {
60 return mSurface->getFrameTimestamps(frameNumber, outRequestedPresentTime, outAcquireTime,
61 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
62 outGlCompositionDoneTime, outDisplayPresentTime, outDequeueReadyTime, outReleaseTime);
63 }
64
65 void enableFrameTimestamps(bool enable) {
66 return mSurface->enableFrameTimestamps(enable);
67 }
68
John Reck848f6512018-12-03 13:26:43 -080069private:
Alec Mourif023a322019-11-25 10:02:21 -080070 sp<Surface> mSurface;
John Reck848f6512018-12-03 13:26:43 -080071
72 mutable std::mutex mMutex;
73
74 uint64_t mUsage = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
75 PixelFormat mFormat = PIXEL_FORMAT_RGBA_8888;
76 std::unique_ptr<AHardwareBuffer, void (*)(AHardwareBuffer*)> mScratchBuffer{
77 nullptr, AHardwareBuffer_release};
78 ANativeWindowBuffer* mReservedBuffer = nullptr;
79 base::unique_fd mReservedFenceFd;
80 bool mHasDequeuedBuffer = false;
John Reck59dd2ea2019-07-26 16:51:08 -070081 int mBufferQueueState = OK;
John Reck848f6512018-12-03 13:26:43 -080082
83 bool isFallbackBuffer(const ANativeWindowBuffer* windowBuffer) const;
John Reck59dd2ea2019-07-26 16:51:08 -070084 ANativeWindowBuffer* acquireFallbackBuffer(int error);
John Reck848f6512018-12-03 13:26:43 -080085 void clearReservedBuffer();
86
Alec Mourif023a322019-11-25 10:02:21 -080087 // ANativeWindow hooks. When an ANativeWindow_* method is called on the
88 // underlying ANativeWindow, these methods will intercept the original call.
89 // For example, an EGL driver would call into these hooks instead of the
90 // original methods.
91 static int hook_cancelBuffer(ANativeWindow* window, ANativeWindow_cancelBufferFn cancelBuffer,
92 void* data, ANativeWindowBuffer* buffer, int fenceFd);
93 static int hook_dequeueBuffer(ANativeWindow* window,
94 ANativeWindow_dequeueBufferFn dequeueBuffer, void* data,
95 ANativeWindowBuffer** buffer, int* fenceFd);
96 static int hook_queueBuffer(ANativeWindow* window, ANativeWindow_queueBufferFn queueBuffer,
97 void* data, ANativeWindowBuffer* buffer, int fenceFd);
John Reck848f6512018-12-03 13:26:43 -080098
Alec Mourif023a322019-11-25 10:02:21 -080099 static int hook_perform(ANativeWindow* window, ANativeWindow_performFn perform, void* data,
100 int operation, va_list args);
John Reck848f6512018-12-03 13:26:43 -0800101};
102
Alec Mouri8d0c5bd22019-08-22 19:20:41 -0700103}; // namespace android::uirenderer::renderthread