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