blob: 595964741049044b75d15a1e7e031aa04105bbfe [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 Mouri43fe6fc2019-12-23 07:46:19 -080019#include <android-base/unique_fd.h>
John Reck8ddbc592020-05-07 16:11:18 -070020#include <system/window.h>
Alec Mourif023a322019-11-25 10:02:21 -080021#include <apex/window.h>
Alec Mouri43fe6fc2019-12-23 07:46:19 -080022#include <utils/Errors.h>
John Reck848f6512018-12-03 13:26:43 -080023#include <utils/Macros.h>
Alec Mouri45238012020-01-29 11:04:40 -080024#include <utils/NdkUtils.h>
John Reck848f6512018-12-03 13:26:43 -080025#include <utils/StrongPointer.h>
26
27#include <memory>
Alec Mouri43fe6fc2019-12-23 07:46:19 -080028#include <mutex>
John Reck848f6512018-12-03 13:26:43 -080029
30namespace android::uirenderer::renderthread {
31
Alec Mourif023a322019-11-25 10:02:21 -080032class ReliableSurface {
John Reck848f6512018-12-03 13:26:43 -080033 PREVENT_COPY_AND_ASSIGN(ReliableSurface);
34
35public:
Alec Mouri43fe6fc2019-12-23 07:46:19 -080036 ReliableSurface(ANativeWindow* window);
John Reck848f6512018-12-03 13:26:43 -080037 ~ReliableSurface();
38
Alec Mourif023a322019-11-25 10:02:21 -080039 // Performs initialization that is not safe to do in the constructor.
40 // For instance, registering ANativeWindow interceptors with ReliableSurface
41 // passed as the data pointer is not safe.
42 void init();
43
Alec Mouri43fe6fc2019-12-23 07:46:19 -080044 ANativeWindow* getNativeWindow() { return mWindow; }
Alec Mourif023a322019-11-25 10:02:21 -080045
John Reck848f6512018-12-03 13:26:43 -080046 int reserveNext();
47
John Reck59dd2ea2019-07-26 16:51:08 -070048 int getAndClearError() {
49 int ret = mBufferQueueState;
50 mBufferQueueState = OK;
51 return ret;
52 }
53
John Reck8ddbc592020-05-07 16:11:18 -070054 bool didSetExtraBuffers() const {
55 std::lock_guard _lock{mMutex};
56 return mDidSetExtraBuffers;
57 }
58
John Reck848f6512018-12-03 13:26:43 -080059private:
Alec Mouri43fe6fc2019-12-23 07:46:19 -080060 ANativeWindow* mWindow;
John Reck848f6512018-12-03 13:26:43 -080061
62 mutable std::mutex mMutex;
63
64 uint64_t mUsage = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
Alec Mouri43fe6fc2019-12-23 07:46:19 -080065 AHardwareBuffer_Format mFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
Alec Mouri45238012020-01-29 11:04:40 -080066 UniqueAHardwareBuffer mScratchBuffer;
John Reck848f6512018-12-03 13:26:43 -080067 ANativeWindowBuffer* mReservedBuffer = nullptr;
68 base::unique_fd mReservedFenceFd;
69 bool mHasDequeuedBuffer = false;
John Reck59dd2ea2019-07-26 16:51:08 -070070 int mBufferQueueState = OK;
John Reck8ddbc592020-05-07 16:11:18 -070071 size_t mExpectedBufferCount = 0;
72 bool mDidSetExtraBuffers = false;
John Reck848f6512018-12-03 13:26:43 -080073
74 bool isFallbackBuffer(const ANativeWindowBuffer* windowBuffer) const;
John Reck59dd2ea2019-07-26 16:51:08 -070075 ANativeWindowBuffer* acquireFallbackBuffer(int error);
John Reck848f6512018-12-03 13:26:43 -080076 void clearReservedBuffer();
77
Alec Mourif023a322019-11-25 10:02:21 -080078 // ANativeWindow hooks. When an ANativeWindow_* method is called on the
79 // underlying ANativeWindow, these methods will intercept the original call.
80 // For example, an EGL driver would call into these hooks instead of the
81 // original methods.
82 static int hook_cancelBuffer(ANativeWindow* window, ANativeWindow_cancelBufferFn cancelBuffer,
83 void* data, ANativeWindowBuffer* buffer, int fenceFd);
84 static int hook_dequeueBuffer(ANativeWindow* window,
85 ANativeWindow_dequeueBufferFn dequeueBuffer, void* data,
86 ANativeWindowBuffer** buffer, int* fenceFd);
87 static int hook_queueBuffer(ANativeWindow* window, ANativeWindow_queueBufferFn queueBuffer,
88 void* data, ANativeWindowBuffer* buffer, int fenceFd);
John Reck848f6512018-12-03 13:26:43 -080089
Alec Mourif023a322019-11-25 10:02:21 -080090 static int hook_perform(ANativeWindow* window, ANativeWindow_performFn perform, void* data,
91 int operation, va_list args);
John Reck8ddbc592020-05-07 16:11:18 -070092 static int hook_query(const ANativeWindow* window, ANativeWindow_queryFn query, void* data,
93 int what, int* value);
John Reck848f6512018-12-03 13:26:43 -080094};
95
Alec Mouri8d0c5bd22019-08-22 19:20:41 -070096}; // namespace android::uirenderer::renderthread