Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 | |
| 17 | #pragma once |
| 18 | |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 19 | #include <SkImage.h> |
| 20 | #include <SkSurface.h> |
Nolan Scobie | bc3f360 | 2024-08-30 13:51:37 -0400 | [diff] [blame] | 21 | #include <include/gpu/ganesh/GrDirectContext.h> |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 22 | #include <sys/types.h> |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 23 | #include <ui/GraphicTypes.h> |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 24 | |
| 25 | #include "android-base/macros.h" |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 26 | #include "compat/SkiaBackendTexture.h" |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 27 | |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 28 | #include <memory> |
Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace renderengine { |
| 33 | namespace skia { |
| 34 | |
| 35 | /** |
Ana Krulec | dfec8f5 | 2021-01-13 12:51:47 -0800 | [diff] [blame] | 36 | * AutoBackendTexture manages GPU image lifetime. It is a ref-counted object |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 37 | * that keeps GPU resources alive until the last SkImage or SkSurface object using them is |
| 38 | * destroyed. |
| 39 | */ |
| 40 | class AutoBackendTexture { |
| 41 | public: |
Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 42 | // Manager class that is responsible for the immediate or deferred cleanup |
| 43 | // of AutoBackendTextures. Clients of AutoBackendTexture are responsible for |
| 44 | // ensuring that access to this class is thread safe. Clients also control when |
| 45 | // the resources are reclaimed by setting the manager into deferred mode. |
| 46 | class CleanupManager { |
| 47 | public: |
| 48 | CleanupManager() = default; |
| 49 | void add(AutoBackendTexture* abt) { |
| 50 | if (mDeferCleanup) { |
| 51 | mCleanupList.push_back(abt); |
| 52 | } else { |
| 53 | delete abt; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void setDeferredStatus(bool enabled) { mDeferCleanup = enabled; } |
| 58 | |
| 59 | bool isEmpty() const { return mCleanupList.empty(); } |
| 60 | |
| 61 | // If any AutoBackedTextures were added while in deferred mode this method |
| 62 | // will ensure they are deleted before returning. It must only be called |
| 63 | // on the thread where the GPU context that created the AutoBackedTexture |
| 64 | // is active. |
| 65 | void cleanup() { |
| 66 | for (auto abt : mCleanupList) { |
| 67 | delete abt; |
| 68 | } |
| 69 | mCleanupList.clear(); |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | DISALLOW_COPY_AND_ASSIGN(CleanupManager); |
| 74 | bool mDeferCleanup = false; |
| 75 | std::vector<AutoBackendTexture*> mCleanupList; |
| 76 | }; |
| 77 | |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 78 | // Local reference that supports RAII-style management of an AutoBackendTexture |
| 79 | // AutoBackendTexture by itself can't be managed in a similar fashion because |
| 80 | // of shared ownership with Skia objects, so we wrap it here instead. |
| 81 | class LocalRef { |
| 82 | public: |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 83 | LocalRef(std::unique_ptr<SkiaBackendTexture> backendTexture, CleanupManager& cleanupMgr) { |
| 84 | mTexture = new AutoBackendTexture(std::move(backendTexture), cleanupMgr); |
Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 85 | mTexture->ref(); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 88 | ~LocalRef() { |
| 89 | if (mTexture != nullptr) { |
| 90 | mTexture->unref(true); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Makes a new SkImage from the texture content. |
| 95 | // As SkImages are immutable but buffer content is not, we create |
| 96 | // a new SkImage every time. |
Nolan Scobie | 17e2551 | 2024-03-13 18:02:48 -0400 | [diff] [blame] | 97 | sk_sp<SkImage> makeImage(ui::Dataspace dataspace, SkAlphaType alphaType) { |
| 98 | return mTexture->makeImage(dataspace, alphaType); |
Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Makes a new SkSurface from the texture content, if needed. |
Nolan Scobie | 17e2551 | 2024-03-13 18:02:48 -0400 | [diff] [blame] | 102 | sk_sp<SkSurface> getOrCreateSurface(ui::Dataspace dataspace) { |
| 103 | return mTexture->getOrCreateSurface(dataspace); |
Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 104 | } |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 105 | |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 106 | SkColorType colorType() const { return mTexture->mBackendTexture->internalColorType(); } |
Leon Scroggins III | c4e0cbd | 2021-05-25 10:25:20 -0400 | [diff] [blame] | 107 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 108 | DISALLOW_COPY_AND_ASSIGN(LocalRef); |
| 109 | |
| 110 | private: |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 111 | AutoBackendTexture* mTexture = nullptr; |
| 112 | }; |
| 113 | |
Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 114 | private: |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 115 | DISALLOW_COPY_AND_ASSIGN(AutoBackendTexture); |
| 116 | |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 117 | // Creates an AutoBackendTexture to manage the lifecycle of a given SkiaBackendTexture, which is |
| 118 | // in turn backed by an underlying backend-specific texture type. |
| 119 | AutoBackendTexture(std::unique_ptr<SkiaBackendTexture> backendTexture, |
Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 120 | CleanupManager& cleanupMgr); |
Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 121 | |
| 122 | // The only way to invoke dtor is with unref, when mUsageCount is 0. |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 123 | ~AutoBackendTexture() = default; |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 124 | |
| 125 | void ref() { mUsageCount++; } |
| 126 | |
| 127 | // releaseLocalResources is true if the underlying SkImage and SkSurface |
| 128 | // should be deleted from local tracking. |
| 129 | void unref(bool releaseLocalResources); |
| 130 | |
| 131 | // Makes a new SkImage from the texture content. |
| 132 | // As SkImages are immutable but buffer content is not, we create |
| 133 | // a new SkImage every time. |
Nolan Scobie | 17e2551 | 2024-03-13 18:02:48 -0400 | [diff] [blame] | 134 | sk_sp<SkImage> makeImage(ui::Dataspace dataspace, SkAlphaType alphaType); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 135 | |
| 136 | // Makes a new SkSurface from the texture content, if needed. |
Nolan Scobie | 17e2551 | 2024-03-13 18:02:48 -0400 | [diff] [blame] | 137 | sk_sp<SkSurface> getOrCreateSurface(ui::Dataspace dataspace); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 138 | |
Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 139 | CleanupManager& mCleanupMgr; |
| 140 | |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 141 | static void releaseSurfaceProc(SkSurface::ReleaseContext releaseContext); |
Kevin Lubick | 95afb8a | 2023-05-11 14:57:41 +0000 | [diff] [blame] | 142 | static void releaseImageProc(SkImages::ReleaseContext releaseContext); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 143 | |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 144 | std::unique_ptr<SkiaBackendTexture> mBackendTexture; |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 145 | int mUsageCount = 0; |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 146 | sk_sp<SkImage> mImage = nullptr; |
| 147 | sk_sp<SkSurface> mSurface = nullptr; |
| 148 | ui::Dataspace mDataspace = ui::Dataspace::UNKNOWN; |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | } // namespace skia |
| 152 | } // namespace renderengine |
| 153 | } // namespace android |