blob: 3b69b73bcab339419893d9f2ee97264cce8783de [file] [log] [blame]
Derek Sollenbergera19b71a2019-02-15 16:36:30 -05001/*
2 * Copyright (C) 2019 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#pragma once
17
John Reckf0baa242021-05-13 18:14:45 -040018#include <android-base/unique_fd.h>
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050019#include <system/graphics.h>
20#include <system/window.h>
21#include <vulkan/vulkan.h>
22
Kevin Lubick07d6aae2022-04-01 14:03:11 -040023#include <SkColorSpace.h>
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050024#include <SkRefCnt.h>
John Reck0fa0cbc2019-04-05 16:57:46 -070025#include <SkSize.h>
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050026
27#include "IRenderPipeline.h"
28
29class SkSurface;
30
31namespace android {
32namespace uirenderer {
33namespace renderthread {
34
35class VulkanManager;
36
37class VulkanSurface {
38public:
John Reck0fa0cbc2019-04-05 16:57:46 -070039 static VulkanSurface* Create(ANativeWindow* window, ColorMode colorMode, SkColorType colorType,
Adlai Hollerd2345212020-10-07 14:16:40 -040040 sk_sp<SkColorSpace> colorSpace, GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -070041 const VulkanManager& vkManager, uint32_t extraBuffers);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050042 ~VulkanSurface();
43
Stan Ilievbc5f06b2019-03-26 15:14:34 -040044 sk_sp<SkSurface> getCurrentSkSurface() {
45 return mCurrentBufferInfo ? mCurrentBufferInfo->skSurface : nullptr;
46 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050047 const SkMatrix& getCurrentPreTransform() { return mWindowInfo.preTransform; }
48
John Reck55887762023-01-25 16:51:18 -050049 void setColorSpace(sk_sp<SkColorSpace> colorSpace);
50
John Reck29b1ee02023-04-04 17:44:23 -040051 bool isBeyond8Bit() const;
52
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050053private:
54 /*
55 * All structs/methods in this private section are specifically for use by the VulkanManager
56 *
57 */
58 friend VulkanManager;
59 struct NativeBufferInfo {
60 sk_sp<SkSurface> skSurface;
61 sp<ANativeWindowBuffer> buffer;
62 // The fence is only valid when the buffer is dequeued, and should be
63 // -1 any other time. When valid, we own the fd, and must ensure it is
64 // closed: either by closing it explicitly when queueing the buffer,
65 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
John Reckf0baa242021-05-13 18:14:45 -040066 base::unique_fd dequeue_fence;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050067 bool dequeued = false;
68 uint32_t lastPresentedCount = 0;
69 bool hasValidContents = false;
70 };
71
72 NativeBufferInfo* dequeueNativeBuffer();
Stan Ilievbc5f06b2019-03-26 15:14:34 -040073 NativeBufferInfo* getCurrentBufferInfo() { return mCurrentBufferInfo; }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050074 bool presentCurrentBuffer(const SkRect& dirtyRect, int semaphoreFd);
75
76 // The width and height are are the logical width and height for when submitting draws to the
77 // surface. In reality if the window is rotated the underlying window may have the width and
78 // height swapped.
79 int logicalWidth() const { return mWindowInfo.size.width(); }
80 int logicalHeight() const { return mWindowInfo.size.height(); }
81 int getCurrentBuffersAge();
82
83private:
84 /*
85 * All code below this line while logically available to VulkanManager should not be treated
86 * as private to this class.
87 *
88 */
John Reckac513c22019-03-28 16:57:38 -070089
90 // How many buffers we want to be able to use ourselves. We want 1 in active rendering with
91 // 1 more queued, so 2. This will be added to NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, which is
92 // how many buffers the consumer needs (eg, 1 for SurfaceFlinger), getting to a typically
93 // triple-buffered queue as a result.
94 static constexpr uint32_t sTargetBufferCount = 2;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050095
96 struct WindowInfo {
97 SkISize size;
Alec Mouri45238012020-01-29 11:04:40 -080098 uint32_t bufferFormat;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050099 android_dataspace dataspace;
Derek Sollenberger89f170c2021-04-01 16:05:54 -0400100 sk_sp<SkColorSpace> colorspace;
John Reck55887762023-01-25 16:51:18 -0500101 ColorMode colorMode;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500102 int transform;
John Reckac513c22019-03-28 16:57:38 -0700103 size_t bufferCount;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500104 uint64_t windowUsageFlags;
105
106 // size of the ANativeWindow if the inverse of transform requires us to swap width/height
107 SkISize actualSize;
108 // transform to be applied to the SkSurface to map the coordinates to the provided transform
109 SkMatrix preTransform;
110 };
111
Adlai Hollerd2345212020-10-07 14:16:40 -0400112 VulkanSurface(ANativeWindow* window, const WindowInfo& windowInfo, GrDirectContext* grContext);
Yiwei Zhang68daf672019-06-26 22:02:41 -0700113 static bool InitializeWindowInfoStruct(ANativeWindow* window, ColorMode colorMode,
114 SkColorType colorType, sk_sp<SkColorSpace> colorSpace,
115 const VulkanManager& vkManager, uint32_t extraBuffers,
Yiwei Zhang68daf672019-06-26 22:02:41 -0700116 WindowInfo* outWindowInfo);
John Reck0fa0cbc2019-04-05 16:57:46 -0700117 static bool UpdateWindow(ANativeWindow* window, const WindowInfo& windowInfo);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500118 void releaseBuffers();
119
Alec Mouri45238012020-01-29 11:04:40 -0800120 // TODO: This number comes from ui/BufferQueueDefs. We're not pulling the
121 // header in so that we don't need to depend on libui, but we should share
122 // this constant somewhere. But right now it's okay to keep here because we
123 // can't safely change the slot count anyways.
124 static constexpr size_t kNumBufferSlots = 64;
John Reckac513c22019-03-28 16:57:38 -0700125 // TODO: Just use a vector?
Alec Mouri45238012020-01-29 11:04:40 -0800126 NativeBufferInfo mNativeBuffers[kNumBufferSlots];
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500127
128 sp<ANativeWindow> mNativeWindow;
129 WindowInfo mWindowInfo;
Adlai Hollerd2345212020-10-07 14:16:40 -0400130 GrDirectContext* mGrContext;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500131
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500132 uint32_t mPresentCount = 0;
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400133 NativeBufferInfo* mCurrentBufferInfo = nullptr;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500134};
135
136} /* namespace renderthread */
137} /* namespace uirenderer */
Yiwei Zhangdab6ecd2019-06-27 12:22:33 -0700138} /* namespace android */