blob: 40a44b11c0bc7ed24f5947c85586d4948e346081 [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
18#include <system/graphics.h>
19#include <system/window.h>
20#include <vulkan/vulkan.h>
21
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050022#include <SkRefCnt.h>
John Reck0fa0cbc2019-04-05 16:57:46 -070023#include <SkSize.h>
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050024
25#include "IRenderPipeline.h"
26
27class SkSurface;
28
29namespace android {
30namespace uirenderer {
31namespace renderthread {
32
33class VulkanManager;
34
35class VulkanSurface {
36public:
John Reck0fa0cbc2019-04-05 16:57:46 -070037 static VulkanSurface* Create(ANativeWindow* window, ColorMode colorMode, SkColorType colorType,
38 sk_sp<SkColorSpace> colorSpace, GrContext* grContext,
39 const VulkanManager& vkManager, uint32_t extraBuffers);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050040 ~VulkanSurface();
41
Stan Ilievbc5f06b2019-03-26 15:14:34 -040042 sk_sp<SkSurface> getCurrentSkSurface() {
43 return mCurrentBufferInfo ? mCurrentBufferInfo->skSurface : nullptr;
44 }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050045 const SkMatrix& getCurrentPreTransform() { return mWindowInfo.preTransform; }
46
47private:
48 /*
49 * All structs/methods in this private section are specifically for use by the VulkanManager
50 *
51 */
52 friend VulkanManager;
53 struct NativeBufferInfo {
54 sk_sp<SkSurface> skSurface;
55 sp<ANativeWindowBuffer> buffer;
56 // The fence is only valid when the buffer is dequeued, and should be
57 // -1 any other time. When valid, we own the fd, and must ensure it is
58 // closed: either by closing it explicitly when queueing the buffer,
59 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
60 int dequeue_fence = -1;
61 bool dequeued = false;
62 uint32_t lastPresentedCount = 0;
63 bool hasValidContents = false;
64 };
65
66 NativeBufferInfo* dequeueNativeBuffer();
Stan Ilievbc5f06b2019-03-26 15:14:34 -040067 NativeBufferInfo* getCurrentBufferInfo() { return mCurrentBufferInfo; }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050068 bool presentCurrentBuffer(const SkRect& dirtyRect, int semaphoreFd);
69
70 // The width and height are are the logical width and height for when submitting draws to the
71 // surface. In reality if the window is rotated the underlying window may have the width and
72 // height swapped.
73 int logicalWidth() const { return mWindowInfo.size.width(); }
74 int logicalHeight() const { return mWindowInfo.size.height(); }
75 int getCurrentBuffersAge();
76
77private:
78 /*
79 * All code below this line while logically available to VulkanManager should not be treated
80 * as private to this class.
81 *
82 */
John Reckac513c22019-03-28 16:57:38 -070083
84 // How many buffers we want to be able to use ourselves. We want 1 in active rendering with
85 // 1 more queued, so 2. This will be added to NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, which is
86 // how many buffers the consumer needs (eg, 1 for SurfaceFlinger), getting to a typically
87 // triple-buffered queue as a result.
88 static constexpr uint32_t sTargetBufferCount = 2;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050089
90 struct WindowInfo {
91 SkISize size;
Alec Mouri45238012020-01-29 11:04:40 -080092 uint32_t bufferFormat;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050093 android_dataspace dataspace;
94 int transform;
John Reckac513c22019-03-28 16:57:38 -070095 size_t bufferCount;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050096 uint64_t windowUsageFlags;
97
98 // size of the ANativeWindow if the inverse of transform requires us to swap width/height
99 SkISize actualSize;
100 // transform to be applied to the SkSurface to map the coordinates to the provided transform
101 SkMatrix preTransform;
102 };
103
Yiwei Zhangdab6ecd2019-06-27 12:22:33 -0700104 VulkanSurface(ANativeWindow* window, const WindowInfo& windowInfo, GrContext* grContext);
Yiwei Zhang68daf672019-06-26 22:02:41 -0700105 static bool InitializeWindowInfoStruct(ANativeWindow* window, ColorMode colorMode,
106 SkColorType colorType, sk_sp<SkColorSpace> colorSpace,
107 const VulkanManager& vkManager, uint32_t extraBuffers,
Yiwei Zhang68daf672019-06-26 22:02:41 -0700108 WindowInfo* outWindowInfo);
John Reck0fa0cbc2019-04-05 16:57:46 -0700109 static bool UpdateWindow(ANativeWindow* window, const WindowInfo& windowInfo);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500110 void releaseBuffers();
111
Alec Mouri45238012020-01-29 11:04:40 -0800112 // TODO: This number comes from ui/BufferQueueDefs. We're not pulling the
113 // header in so that we don't need to depend on libui, but we should share
114 // this constant somewhere. But right now it's okay to keep here because we
115 // can't safely change the slot count anyways.
116 static constexpr size_t kNumBufferSlots = 64;
John Reckac513c22019-03-28 16:57:38 -0700117 // TODO: Just use a vector?
Alec Mouri45238012020-01-29 11:04:40 -0800118 NativeBufferInfo mNativeBuffers[kNumBufferSlots];
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500119
120 sp<ANativeWindow> mNativeWindow;
121 WindowInfo mWindowInfo;
122 GrContext* mGrContext;
123
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500124 uint32_t mPresentCount = 0;
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400125 NativeBufferInfo* mCurrentBufferInfo = nullptr;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500126};
127
128} /* namespace renderthread */
129} /* namespace uirenderer */
Yiwei Zhangdab6ecd2019-06-27 12:22:33 -0700130} /* namespace android */