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> |
Derek Sollenberger | 8b99419 | 2019-07-16 13:23:29 -0400 | [diff] [blame^] | 20 | #include <ui/BufferQueueDefs.h> |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 21 | #include <vulkan/vulkan.h> |
| 22 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 23 | #include <SkRefCnt.h> |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 24 | #include <SkSize.h> |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 25 | |
| 26 | #include "IRenderPipeline.h" |
| 27 | |
| 28 | class SkSurface; |
| 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | namespace renderthread { |
| 33 | |
| 34 | class VulkanManager; |
| 35 | |
| 36 | class VulkanSurface { |
| 37 | public: |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 38 | static VulkanSurface* Create(ANativeWindow* window, ColorMode colorMode, SkColorType colorType, |
| 39 | sk_sp<SkColorSpace> colorSpace, GrContext* grContext, |
| 40 | const VulkanManager& vkManager, uint32_t extraBuffers); |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 41 | ~VulkanSurface(); |
| 42 | |
Stan Iliev | bc5f06b | 2019-03-26 15:14:34 -0400 | [diff] [blame] | 43 | sk_sp<SkSurface> getCurrentSkSurface() { |
| 44 | return mCurrentBufferInfo ? mCurrentBufferInfo->skSurface : nullptr; |
| 45 | } |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 46 | const SkMatrix& getCurrentPreTransform() { return mWindowInfo.preTransform; } |
| 47 | |
| 48 | private: |
| 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(); |
Stan Iliev | bc5f06b | 2019-03-26 15:14:34 -0400 | [diff] [blame] | 68 | NativeBufferInfo* getCurrentBufferInfo() { return mCurrentBufferInfo; } |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 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 | |
| 78 | private: |
| 79 | /* |
| 80 | * All code below this line while logically available to VulkanManager should not be treated |
| 81 | * as private to this class. |
| 82 | * |
| 83 | */ |
John Reck | ac513c2 | 2019-03-28 16:57:38 -0700 | [diff] [blame] | 84 | |
| 85 | // How many buffers we want to be able to use ourselves. We want 1 in active rendering with |
| 86 | // 1 more queued, so 2. This will be added to NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, which is |
| 87 | // how many buffers the consumer needs (eg, 1 for SurfaceFlinger), getting to a typically |
| 88 | // triple-buffered queue as a result. |
| 89 | static constexpr uint32_t sTargetBufferCount = 2; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 90 | |
| 91 | struct WindowInfo { |
| 92 | SkISize size; |
| 93 | PixelFormat pixelFormat; |
| 94 | android_dataspace dataspace; |
| 95 | int transform; |
John Reck | ac513c2 | 2019-03-28 16:57:38 -0700 | [diff] [blame] | 96 | size_t bufferCount; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 97 | uint64_t windowUsageFlags; |
| 98 | |
| 99 | // size of the ANativeWindow if the inverse of transform requires us to swap width/height |
| 100 | SkISize actualSize; |
| 101 | // transform to be applied to the SkSurface to map the coordinates to the provided transform |
| 102 | SkMatrix preTransform; |
| 103 | }; |
| 104 | |
Yiwei Zhang | dab6ecd | 2019-06-27 12:22:33 -0700 | [diff] [blame] | 105 | VulkanSurface(ANativeWindow* window, const WindowInfo& windowInfo, GrContext* grContext); |
Yiwei Zhang | 68daf67 | 2019-06-26 22:02:41 -0700 | [diff] [blame] | 106 | static bool InitializeWindowInfoStruct(ANativeWindow* window, ColorMode colorMode, |
| 107 | SkColorType colorType, sk_sp<SkColorSpace> colorSpace, |
| 108 | const VulkanManager& vkManager, uint32_t extraBuffers, |
Yiwei Zhang | 68daf67 | 2019-06-26 22:02:41 -0700 | [diff] [blame] | 109 | WindowInfo* outWindowInfo); |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 110 | static bool UpdateWindow(ANativeWindow* window, const WindowInfo& windowInfo); |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 111 | void releaseBuffers(); |
| 112 | |
John Reck | ac513c2 | 2019-03-28 16:57:38 -0700 | [diff] [blame] | 113 | // TODO: Just use a vector? |
| 114 | NativeBufferInfo mNativeBuffers[android::BufferQueueDefs::NUM_BUFFER_SLOTS]; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 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 | |
| 124 | } /* namespace renderthread */ |
| 125 | } /* namespace uirenderer */ |
Yiwei Zhang | dab6ecd | 2019-06-27 12:22:33 -0700 | [diff] [blame] | 126 | } /* namespace android */ |