blob: 4fd9cd29b92011bfa639830805710e7a06ddcf13 [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
22#include <SkSize.h>
23#include <SkRefCnt.h>
24
25#include "IRenderPipeline.h"
26
27class SkSurface;
28
29namespace android {
30namespace uirenderer {
31namespace renderthread {
32
33class VulkanManager;
34
35class VulkanSurface {
36public:
37 static VulkanSurface* Create(ANativeWindow* window,
38 ColorMode colorMode,
39 SkColorType colorType,
40 sk_sp<SkColorSpace> colorSpace,
41 GrContext* grContext,
42 const VulkanManager& vkManager);
43 ~VulkanSurface();
44
45 sk_sp<SkSurface> getCurrentSkSurface() { return mNativeBuffers[mDequeuedIndex].skSurface; }
46 const SkMatrix& getCurrentPreTransform() { return mWindowInfo.preTransform; }
47
48private:
49 /*
50 * All structs/methods in this private section are specifically for use by the VulkanManager
51 *
52 */
53 friend VulkanManager;
54 struct NativeBufferInfo {
55 sk_sp<SkSurface> skSurface;
56 sp<ANativeWindowBuffer> buffer;
57 // The fence is only valid when the buffer is dequeued, and should be
58 // -1 any other time. When valid, we own the fd, and must ensure it is
59 // closed: either by closing it explicitly when queueing the buffer,
60 // or by passing ownership e.g. to ANativeWindow::cancelBuffer().
61 int dequeue_fence = -1;
62 bool dequeued = false;
63 uint32_t lastPresentedCount = 0;
64 bool hasValidContents = false;
65 };
66
67 NativeBufferInfo* dequeueNativeBuffer();
68 NativeBufferInfo* getCurrentBufferInfo() { return &mNativeBuffers[mDequeuedIndex]; }
69 bool presentCurrentBuffer(const SkRect& dirtyRect, int semaphoreFd);
70
71 // The width and height are are the logical width and height for when submitting draws to the
72 // surface. In reality if the window is rotated the underlying window may have the width and
73 // height swapped.
74 int logicalWidth() const { return mWindowInfo.size.width(); }
75 int logicalHeight() const { return mWindowInfo.size.height(); }
76 int getCurrentBuffersAge();
77
78private:
79 /*
80 * All code below this line while logically available to VulkanManager should not be treated
81 * as private to this class.
82 *
83 */
84 static constexpr int sMaxBufferCount = 3;
85
86 struct WindowInfo {
87 SkISize size;
88 PixelFormat pixelFormat;
89 android_dataspace dataspace;
90 int transform;
91 int bufferCount;
92 uint64_t windowUsageFlags;
93
94 // size of the ANativeWindow if the inverse of transform requires us to swap width/height
95 SkISize actualSize;
96 // transform to be applied to the SkSurface to map the coordinates to the provided transform
97 SkMatrix preTransform;
98 };
99
100 VulkanSurface(ANativeWindow* window,
101 const WindowInfo& windowInfo,
102 SkISize minWindowSize,
103 SkISize maxWindowSize,
104 GrContext* grContext);
105 static bool UpdateWindow(ANativeWindow* window,
106 const WindowInfo& windowInfo);
107 static void ComputeWindowSizeAndTransform(WindowInfo* windowInfo,
108 const SkISize& minSize,
109 const SkISize& maxSize);
110 void releaseBuffers();
111
112 NativeBufferInfo mNativeBuffers[VulkanSurface::sMaxBufferCount];
113
114 sp<ANativeWindow> mNativeWindow;
115 WindowInfo mWindowInfo;
116 GrContext* mGrContext;
117
118 int mDequeuedIndex = -1;
119 uint32_t mPresentCount = 0;
120
121 const SkISize mMinWindowSize;
122 const SkISize mMaxWindowSize;
123};
124
125} /* namespace renderthread */
126} /* namespace uirenderer */
127} /* namespace android */