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 | |
John Reck | f0baa24 | 2021-05-13 18:14:45 -0400 | [diff] [blame] | 18 | #include <android-base/unique_fd.h> |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 19 | #include <system/graphics.h> |
| 20 | #include <system/window.h> |
| 21 | #include <vulkan/vulkan.h> |
| 22 | |
Kevin Lubick | 07d6aae | 2022-04-01 14:03:11 -0400 | [diff] [blame] | 23 | #include <SkColorSpace.h> |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 24 | #include <SkRefCnt.h> |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 25 | #include <SkSize.h> |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 26 | |
| 27 | #include "IRenderPipeline.h" |
| 28 | |
| 29 | class SkSurface; |
| 30 | |
| 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | namespace renderthread { |
| 34 | |
| 35 | class VulkanManager; |
| 36 | |
| 37 | class VulkanSurface { |
| 38 | public: |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 39 | static VulkanSurface* Create(ANativeWindow* window, ColorMode colorMode, SkColorType colorType, |
Adlai Holler | d234521 | 2020-10-07 14:16:40 -0400 | [diff] [blame] | 40 | sk_sp<SkColorSpace> colorSpace, GrDirectContext* grContext, |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 41 | const VulkanManager& vkManager, uint32_t extraBuffers); |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 42 | ~VulkanSurface(); |
| 43 | |
Stan Iliev | bc5f06b | 2019-03-26 15:14:34 -0400 | [diff] [blame] | 44 | sk_sp<SkSurface> getCurrentSkSurface() { |
| 45 | return mCurrentBufferInfo ? mCurrentBufferInfo->skSurface : nullptr; |
| 46 | } |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 47 | const SkMatrix& getCurrentPreTransform() { return mWindowInfo.preTransform; } |
| 48 | |
John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 49 | void setColorSpace(sk_sp<SkColorSpace> colorSpace); |
John Reck | ee18427 | 2023-03-21 12:29:21 -0400 | [diff] [blame] | 50 | const SkM44& getPixelSnapMatrix() const { return mWindowInfo.pixelSnapMatrix; } |
John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 51 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 52 | private: |
| 53 | /* |
| 54 | * All structs/methods in this private section are specifically for use by the VulkanManager |
| 55 | * |
| 56 | */ |
| 57 | friend VulkanManager; |
| 58 | struct NativeBufferInfo { |
| 59 | sk_sp<SkSurface> skSurface; |
| 60 | sp<ANativeWindowBuffer> buffer; |
| 61 | // The fence is only valid when the buffer is dequeued, and should be |
| 62 | // -1 any other time. When valid, we own the fd, and must ensure it is |
| 63 | // closed: either by closing it explicitly when queueing the buffer, |
| 64 | // or by passing ownership e.g. to ANativeWindow::cancelBuffer(). |
John Reck | f0baa24 | 2021-05-13 18:14:45 -0400 | [diff] [blame] | 65 | base::unique_fd dequeue_fence; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 66 | bool dequeued = false; |
| 67 | uint32_t lastPresentedCount = 0; |
| 68 | bool hasValidContents = false; |
| 69 | }; |
| 70 | |
| 71 | NativeBufferInfo* dequeueNativeBuffer(); |
Stan Iliev | bc5f06b | 2019-03-26 15:14:34 -0400 | [diff] [blame] | 72 | NativeBufferInfo* getCurrentBufferInfo() { return mCurrentBufferInfo; } |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 73 | bool presentCurrentBuffer(const SkRect& dirtyRect, int semaphoreFd); |
| 74 | |
| 75 | // The width and height are are the logical width and height for when submitting draws to the |
| 76 | // surface. In reality if the window is rotated the underlying window may have the width and |
| 77 | // height swapped. |
| 78 | int logicalWidth() const { return mWindowInfo.size.width(); } |
| 79 | int logicalHeight() const { return mWindowInfo.size.height(); } |
| 80 | int getCurrentBuffersAge(); |
| 81 | |
| 82 | private: |
| 83 | /* |
| 84 | * All code below this line while logically available to VulkanManager should not be treated |
| 85 | * as private to this class. |
| 86 | * |
| 87 | */ |
John Reck | ac513c2 | 2019-03-28 16:57:38 -0700 | [diff] [blame] | 88 | |
| 89 | // How many buffers we want to be able to use ourselves. We want 1 in active rendering with |
| 90 | // 1 more queued, so 2. This will be added to NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, which is |
| 91 | // how many buffers the consumer needs (eg, 1 for SurfaceFlinger), getting to a typically |
| 92 | // triple-buffered queue as a result. |
| 93 | static constexpr uint32_t sTargetBufferCount = 2; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 94 | |
| 95 | struct WindowInfo { |
| 96 | SkISize size; |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 97 | uint32_t bufferFormat; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 98 | android_dataspace dataspace; |
Derek Sollenberger | 89f170c | 2021-04-01 16:05:54 -0400 | [diff] [blame] | 99 | sk_sp<SkColorSpace> colorspace; |
John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 100 | ColorMode colorMode; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 101 | int transform; |
John Reck | ac513c2 | 2019-03-28 16:57:38 -0700 | [diff] [blame] | 102 | size_t bufferCount; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 103 | uint64_t windowUsageFlags; |
| 104 | |
| 105 | // size of the ANativeWindow if the inverse of transform requires us to swap width/height |
| 106 | SkISize actualSize; |
| 107 | // transform to be applied to the SkSurface to map the coordinates to the provided transform |
| 108 | SkMatrix preTransform; |
John Reck | ee18427 | 2023-03-21 12:29:21 -0400 | [diff] [blame] | 109 | SkM44 pixelSnapMatrix; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 110 | }; |
| 111 | |
Adlai Holler | d234521 | 2020-10-07 14:16:40 -0400 | [diff] [blame] | 112 | VulkanSurface(ANativeWindow* window, const WindowInfo& windowInfo, GrDirectContext* grContext); |
Yiwei Zhang | 68daf67 | 2019-06-26 22:02:41 -0700 | [diff] [blame] | 113 | static bool InitializeWindowInfoStruct(ANativeWindow* window, ColorMode colorMode, |
| 114 | SkColorType colorType, sk_sp<SkColorSpace> colorSpace, |
| 115 | const VulkanManager& vkManager, uint32_t extraBuffers, |
Yiwei Zhang | 68daf67 | 2019-06-26 22:02:41 -0700 | [diff] [blame] | 116 | WindowInfo* outWindowInfo); |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 117 | static bool UpdateWindow(ANativeWindow* window, const WindowInfo& windowInfo); |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 118 | void releaseBuffers(); |
John Reck | 61c6447 | 2023-05-04 11:29:45 -0400 | [diff] [blame] | 119 | void invalidateBuffers(); |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 120 | |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 121 | // TODO: This number comes from ui/BufferQueueDefs. We're not pulling the |
| 122 | // header in so that we don't need to depend on libui, but we should share |
| 123 | // this constant somewhere. But right now it's okay to keep here because we |
| 124 | // can't safely change the slot count anyways. |
| 125 | static constexpr size_t kNumBufferSlots = 64; |
John Reck | ac513c2 | 2019-03-28 16:57:38 -0700 | [diff] [blame] | 126 | // TODO: Just use a vector? |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 127 | NativeBufferInfo mNativeBuffers[kNumBufferSlots]; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 128 | |
| 129 | sp<ANativeWindow> mNativeWindow; |
| 130 | WindowInfo mWindowInfo; |
Adlai Holler | d234521 | 2020-10-07 14:16:40 -0400 | [diff] [blame] | 131 | GrDirectContext* mGrContext; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 132 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 133 | uint32_t mPresentCount = 0; |
Stan Iliev | bc5f06b | 2019-03-26 15:14:34 -0400 | [diff] [blame] | 134 | NativeBufferInfo* mCurrentBufferInfo = nullptr; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | } /* namespace renderthread */ |
| 138 | } /* namespace uirenderer */ |
Yiwei Zhang | dab6ecd | 2019-06-27 12:22:33 -0700 | [diff] [blame] | 139 | } /* namespace android */ |