blob: bd2362612a13dd29271fbefe0db53c063b338ccc [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>
Derek Sollenberger8b994192019-07-16 13:23:29 -040020#include <ui/BufferQueueDefs.h>
Derek Sollenbergere78f7c92019-07-31 15:18:47 -040021#include <ui/PixelFormat.h>
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050022#include <vulkan/vulkan.h>
23
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,
40 sk_sp<SkColorSpace> colorSpace, GrContext* grContext,
41 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
49private:
50 /*
51 * All structs/methods in this private section are specifically for use by the VulkanManager
52 *
53 */
54 friend VulkanManager;
55 struct NativeBufferInfo {
56 sk_sp<SkSurface> skSurface;
57 sp<ANativeWindowBuffer> buffer;
58 // The fence is only valid when the buffer is dequeued, and should be
59 // -1 any other time. When valid, we own the fd, and must ensure it is
60 // closed: either by closing it explicitly when queueing the buffer,
61 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
62 int dequeue_fence = -1;
63 bool dequeued = false;
64 uint32_t lastPresentedCount = 0;
65 bool hasValidContents = false;
66 };
67
68 NativeBufferInfo* dequeueNativeBuffer();
Stan Ilievbc5f06b2019-03-26 15:14:34 -040069 NativeBufferInfo* getCurrentBufferInfo() { return mCurrentBufferInfo; }
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050070 bool presentCurrentBuffer(const SkRect& dirtyRect, int semaphoreFd);
71
72 // The width and height are are the logical width and height for when submitting draws to the
73 // surface. In reality if the window is rotated the underlying window may have the width and
74 // height swapped.
75 int logicalWidth() const { return mWindowInfo.size.width(); }
76 int logicalHeight() const { return mWindowInfo.size.height(); }
77 int getCurrentBuffersAge();
78
79private:
80 /*
81 * All code below this line while logically available to VulkanManager should not be treated
82 * as private to this class.
83 *
84 */
John Reckac513c22019-03-28 16:57:38 -070085
86 // How many buffers we want to be able to use ourselves. We want 1 in active rendering with
87 // 1 more queued, so 2. This will be added to NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, which is
88 // how many buffers the consumer needs (eg, 1 for SurfaceFlinger), getting to a typically
89 // triple-buffered queue as a result.
90 static constexpr uint32_t sTargetBufferCount = 2;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050091
92 struct WindowInfo {
93 SkISize size;
94 PixelFormat pixelFormat;
95 android_dataspace dataspace;
96 int transform;
John Reckac513c22019-03-28 16:57:38 -070097 size_t bufferCount;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050098 uint64_t windowUsageFlags;
99
100 // size of the ANativeWindow if the inverse of transform requires us to swap width/height
101 SkISize actualSize;
102 // transform to be applied to the SkSurface to map the coordinates to the provided transform
103 SkMatrix preTransform;
104 };
105
Yiwei Zhangdab6ecd2019-06-27 12:22:33 -0700106 VulkanSurface(ANativeWindow* window, const WindowInfo& windowInfo, GrContext* grContext);
Yiwei Zhang68daf672019-06-26 22:02:41 -0700107 static bool InitializeWindowInfoStruct(ANativeWindow* window, ColorMode colorMode,
108 SkColorType colorType, sk_sp<SkColorSpace> colorSpace,
109 const VulkanManager& vkManager, uint32_t extraBuffers,
Yiwei Zhang68daf672019-06-26 22:02:41 -0700110 WindowInfo* outWindowInfo);
John Reck0fa0cbc2019-04-05 16:57:46 -0700111 static bool UpdateWindow(ANativeWindow* window, const WindowInfo& windowInfo);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500112 void releaseBuffers();
113
John Reckac513c22019-03-28 16:57:38 -0700114 // TODO: Just use a vector?
115 NativeBufferInfo mNativeBuffers[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500116
117 sp<ANativeWindow> mNativeWindow;
118 WindowInfo mWindowInfo;
119 GrContext* mGrContext;
120
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500121 uint32_t mPresentCount = 0;
Stan Ilievbc5f06b2019-03-26 15:14:34 -0400122 NativeBufferInfo* mCurrentBufferInfo = nullptr;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500123};
124
125} /* namespace renderthread */
126} /* namespace uirenderer */
Yiwei Zhangdab6ecd2019-06-27 12:22:33 -0700127} /* namespace android */