blob: c80a7eabe47f8956d1a3305c368b817ca4a3d7e0 [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>
Alec Mourif023a322019-11-25 10:02:21 -080020#include <apex/window.h>
Alec Mouri43fe6fc2019-12-23 07:46:19 -080021#include <utils/Errors.h>
John Reck848f6512018-12-03 13:26:43 -080022#include <utils/Macros.h>
Alec Mouri45238012020-01-29 11:04:40 -080023#include <utils/NdkUtils.h>
John Reck848f6512018-12-03 13:26:43 -080024#include <utils/StrongPointer.h>
25
26#include <memory>
Alec Mouri43fe6fc2019-12-23 07:46:19 -080027#include <mutex>
John Reck848f6512018-12-03 13:26:43 -080028
29namespace android::uirenderer::renderthread {
30
Alec Mourif023a322019-11-25 10:02:21 -080031class ReliableSurface {
John Reck848f6512018-12-03 13:26:43 -080032 PREVENT_COPY_AND_ASSIGN(ReliableSurface);
33
34public:
Alec Mouri43fe6fc2019-12-23 07:46:19 -080035 ReliableSurface(ANativeWindow* window);
John Reck848f6512018-12-03 13:26:43 -080036 ~ReliableSurface();
37
Alec Mourif023a322019-11-25 10:02:21 -080038 // Performs initialization that is not safe to do in the constructor.
39 // For instance, registering ANativeWindow interceptors with ReliableSurface
40 // passed as the data pointer is not safe.
41 void init();
42
Alec Mouri43fe6fc2019-12-23 07:46:19 -080043 ANativeWindow* getNativeWindow() { return mWindow; }
Alec Mourif023a322019-11-25 10:02:21 -080044
John Reck848f6512018-12-03 13:26:43 -080045 int reserveNext();
46
John Reck59dd2ea2019-07-26 16:51:08 -070047 int getAndClearError() {
48 int ret = mBufferQueueState;
49 mBufferQueueState = OK;
50 return ret;
51 }
52
John Reck848f6512018-12-03 13:26:43 -080053private:
Alec Mouri43fe6fc2019-12-23 07:46:19 -080054 ANativeWindow* mWindow;
John Reck848f6512018-12-03 13:26:43 -080055
56 mutable std::mutex mMutex;
57
58 uint64_t mUsage = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
Alec Mouri43fe6fc2019-12-23 07:46:19 -080059 AHardwareBuffer_Format mFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
Alec Mouri45238012020-01-29 11:04:40 -080060 UniqueAHardwareBuffer mScratchBuffer;
John Reck848f6512018-12-03 13:26:43 -080061 ANativeWindowBuffer* mReservedBuffer = nullptr;
62 base::unique_fd mReservedFenceFd;
63 bool mHasDequeuedBuffer = false;
John Reck59dd2ea2019-07-26 16:51:08 -070064 int mBufferQueueState = OK;
John Reck848f6512018-12-03 13:26:43 -080065
66 bool isFallbackBuffer(const ANativeWindowBuffer* windowBuffer) const;
John Reck59dd2ea2019-07-26 16:51:08 -070067 ANativeWindowBuffer* acquireFallbackBuffer(int error);
John Reck848f6512018-12-03 13:26:43 -080068 void clearReservedBuffer();
69
Alec Mourif023a322019-11-25 10:02:21 -080070 // ANativeWindow hooks. When an ANativeWindow_* method is called on the
71 // underlying ANativeWindow, these methods will intercept the original call.
72 // For example, an EGL driver would call into these hooks instead of the
73 // original methods.
74 static int hook_cancelBuffer(ANativeWindow* window, ANativeWindow_cancelBufferFn cancelBuffer,
75 void* data, ANativeWindowBuffer* buffer, int fenceFd);
76 static int hook_dequeueBuffer(ANativeWindow* window,
77 ANativeWindow_dequeueBufferFn dequeueBuffer, void* data,
78 ANativeWindowBuffer** buffer, int* fenceFd);
79 static int hook_queueBuffer(ANativeWindow* window, ANativeWindow_queueBufferFn queueBuffer,
80 void* data, ANativeWindowBuffer* buffer, int fenceFd);
John Reck848f6512018-12-03 13:26:43 -080081
Alec Mourif023a322019-11-25 10:02:21 -080082 static int hook_perform(ANativeWindow* window, ANativeWindow_performFn perform, void* data,
83 int operation, va_list args);
John Reck848f6512018-12-03 13:26:43 -080084};
85
Alec Mouri8d0c5bd22019-08-22 19:20:41 -070086}; // namespace android::uirenderer::renderthread