blob: a823d9da0ab382c255b15c03ab5727c5a676b9fd [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
44 void allocateBuffers() { mSurface->allocateBuffers(); }
45
46 int query(int what, int* value) const { return mSurface->query(what, value); }
47
John Reck848f6512018-12-03 13:26:43 -080048 uint64_t getNextFrameNumber() const { return mSurface->getNextFrameNumber(); }
49
John Reck59dd2ea2019-07-26 16:51:08 -070050 int getAndClearError() {
51 int ret = mBufferQueueState;
52 mBufferQueueState = OK;
53 return ret;
54 }
55
Stan Iliev7203e1f2019-07-25 13:12:02 -040056 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 Reck848f6512018-12-03 13:26:43 -080071private:
Alec Mourif023a322019-11-25 10:02:21 -080072 sp<Surface> mSurface;
John Reck848f6512018-12-03 13:26:43 -080073
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 Reck59dd2ea2019-07-26 16:51:08 -070083 int mBufferQueueState = OK;
John Reck848f6512018-12-03 13:26:43 -080084
85 bool isFallbackBuffer(const ANativeWindowBuffer* windowBuffer) const;
John Reck59dd2ea2019-07-26 16:51:08 -070086 ANativeWindowBuffer* acquireFallbackBuffer(int error);
John Reck848f6512018-12-03 13:26:43 -080087 void clearReservedBuffer();
88
Alec Mourif023a322019-11-25 10:02:21 -080089 // 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 Reck848f6512018-12-03 13:26:43 -0800100
Alec Mourif023a322019-11-25 10:02:21 -0800101 static int hook_perform(ANativeWindow* window, ANativeWindow_performFn perform, void* data,
102 int operation, va_list args);
John Reck848f6512018-12-03 13:26:43 -0800103};
104
Alec Mouri8d0c5bd22019-08-22 19:20:41 -0700105}; // namespace android::uirenderer::renderthread