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 | #include "AutoBackendTexture.h" |
| 18 | |
| 19 | #undef LOG_TAG |
| 20 | #define LOG_TAG "RenderEngine" |
| 21 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 22 | |
Kevin Lubick | e7fb46f | 2023-03-28 15:46:28 +0000 | [diff] [blame] | 23 | #include <SkImage.h> |
| 24 | #include <include/gpu/ganesh/SkImageGanesh.h> |
Kevin Lubick | 00bec72 | 2023-05-12 19:26:03 +0000 | [diff] [blame] | 25 | #include <include/gpu/ganesh/SkSurfaceGanesh.h> |
Kevin Lubick | 15f58d7 | 2023-08-07 15:00:58 +0000 | [diff] [blame] | 26 | #include <include/gpu/ganesh/gl/GrGLBackendSurface.h> |
Brian Osman | c75487e | 2023-08-29 18:10:51 +0000 | [diff] [blame] | 27 | #include <include/gpu/ganesh/vk/GrVkBackendSurface.h> |
Brian Osman | 613bb8e | 2023-08-21 19:23:42 +0000 | [diff] [blame] | 28 | #include <include/gpu/vk/GrVkTypes.h> |
Kevin Lubick | 40ff989 | 2023-05-19 14:02:22 +0000 | [diff] [blame] | 29 | #include <android/hardware_buffer.h> |
Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 30 | #include "ColorSpaces.h" |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 31 | #include "log/log_main.h" |
| 32 | #include "utils/Trace.h" |
| 33 | |
| 34 | namespace android { |
| 35 | namespace renderengine { |
| 36 | namespace skia { |
| 37 | |
Derek Sollenberger | 3425788 | 2021-04-06 18:32:34 +0000 | [diff] [blame] | 38 | AutoBackendTexture::AutoBackendTexture(GrDirectContext* context, AHardwareBuffer* buffer, |
Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 39 | bool isOutputBuffer, CleanupManager& cleanupMgr) |
| 40 | : mCleanupMgr(cleanupMgr), mIsOutputBuffer(isOutputBuffer) { |
Alec Mouri | e1f8198 | 2021-01-11 10:24:27 -0800 | [diff] [blame] | 41 | ATRACE_CALL(); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 42 | AHardwareBuffer_Desc desc; |
| 43 | AHardwareBuffer_describe(buffer, &desc); |
Derek Sollenberger | 3425788 | 2021-04-06 18:32:34 +0000 | [diff] [blame] | 44 | bool createProtectedImage = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT); |
Kevin Lubick | 15f58d7 | 2023-08-07 15:00:58 +0000 | [diff] [blame] | 45 | GrBackendFormat backendFormat; |
| 46 | |
| 47 | GrBackendApi backend = context->backend(); |
| 48 | if (backend == GrBackendApi::kOpenGL) { |
| 49 | backendFormat = |
| 50 | GrAHardwareBufferUtils::GetGLBackendFormat(context, desc.format, false); |
| 51 | mBackendTexture = |
| 52 | GrAHardwareBufferUtils::MakeGLBackendTexture(context, |
| 53 | buffer, |
| 54 | desc.width, |
| 55 | desc.height, |
| 56 | &mDeleteProc, |
| 57 | &mUpdateProc, |
| 58 | &mImageCtx, |
| 59 | createProtectedImage, |
| 60 | backendFormat, |
| 61 | isOutputBuffer); |
| 62 | } else if (backend == GrBackendApi::kVulkan) { |
| 63 | backendFormat = |
| 64 | GrAHardwareBufferUtils::GetVulkanBackendFormat(context, |
| 65 | buffer, |
| 66 | desc.format, |
| 67 | false); |
| 68 | mBackendTexture = |
| 69 | GrAHardwareBufferUtils::MakeVulkanBackendTexture(context, |
| 70 | buffer, |
| 71 | desc.width, |
| 72 | desc.height, |
| 73 | &mDeleteProc, |
| 74 | &mUpdateProc, |
| 75 | &mImageCtx, |
| 76 | createProtectedImage, |
| 77 | backendFormat, |
| 78 | isOutputBuffer); |
| 79 | } else { |
Yi Kong | 9bb855d | 2024-04-11 23:17:27 +0000 | [diff] [blame] | 80 | LOG_ALWAYS_FATAL("Unexpected backend %u", static_cast<unsigned>(backend)); |
Kevin Lubick | 15f58d7 | 2023-08-07 15:00:58 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 83 | mColorType = GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(desc.format); |
Leon Scroggins III | fd1f557 | 2023-04-26 15:59:48 -0400 | [diff] [blame] | 84 | if (!mBackendTexture.isValid() || !desc.width || !desc.height) { |
| 85 | LOG_ALWAYS_FATAL("Failed to create a valid texture. [%p]:[%d,%d] isProtected:%d " |
| 86 | "isWriteable:%d format:%d", |
| 87 | this, desc.width, desc.height, createProtectedImage, isOutputBuffer, |
| 88 | desc.format); |
| 89 | } |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 92 | AutoBackendTexture::~AutoBackendTexture() { |
| 93 | if (mBackendTexture.isValid()) { |
| 94 | mDeleteProc(mImageCtx); |
| 95 | mBackendTexture = {}; |
| 96 | } |
| 97 | } |
| 98 | |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 99 | void AutoBackendTexture::unref(bool releaseLocalResources) { |
| 100 | if (releaseLocalResources) { |
| 101 | mSurface = nullptr; |
| 102 | mImage = nullptr; |
| 103 | } |
| 104 | |
| 105 | mUsageCount--; |
| 106 | if (mUsageCount <= 0) { |
Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 107 | mCleanupMgr.add(this); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | // releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use. |
| 112 | // "releaseContext" contains an "AutoBackendTexture*". |
| 113 | void AutoBackendTexture::releaseSurfaceProc(SkSurface::ReleaseContext releaseContext) { |
| 114 | AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext); |
| 115 | textureRelease->unref(false); |
| 116 | } |
| 117 | |
| 118 | // releaseImageProc is invoked by SkImage, when the texture is no longer in use. |
| 119 | // "releaseContext" contains an "AutoBackendTexture*". |
Kevin Lubick | 95afb8a | 2023-05-11 14:57:41 +0000 | [diff] [blame] | 120 | void AutoBackendTexture::releaseImageProc(SkImages::ReleaseContext releaseContext) { |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 121 | AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext); |
| 122 | textureRelease->unref(false); |
| 123 | } |
| 124 | |
Leon Scroggins III | c10321d | 2023-04-14 17:01:09 -0400 | [diff] [blame] | 125 | void logFatalTexture(const char* msg, const GrBackendTexture& tex, ui::Dataspace dataspace, |
| 126 | SkColorType colorType) { |
Leon Scroggins III | 41c00c5 | 2023-06-21 15:55:55 -0400 | [diff] [blame] | 127 | switch (tex.backend()) { |
| 128 | case GrBackendApi::kOpenGL: { |
| 129 | GrGLTextureInfo textureInfo; |
Kevin Lubick | 15f58d7 | 2023-08-07 15:00:58 +0000 | [diff] [blame] | 130 | bool retrievedTextureInfo = GrBackendTextures::GetGLTextureInfo(tex, &textureInfo); |
Leon Scroggins III | 41c00c5 | 2023-06-21 15:55:55 -0400 | [diff] [blame] | 131 | LOG_ALWAYS_FATAL("%s isTextureValid:%d dataspace:%d" |
| 132 | "\n\tGrBackendTexture: (%i x %i) hasMipmaps: %i isProtected: %i " |
| 133 | "texType: %i\n\t\tGrGLTextureInfo: success: %i fTarget: %u fFormat: %u" |
| 134 | " colorType %i", |
Yi Kong | d90ebf3 | 2023-12-05 16:59:10 +0900 | [diff] [blame] | 135 | msg, tex.isValid(), static_cast<int32_t>(dataspace), tex.width(), |
| 136 | tex.height(), tex.hasMipmaps(), tex.isProtected(), |
Leon Scroggins III | 41c00c5 | 2023-06-21 15:55:55 -0400 | [diff] [blame] | 137 | static_cast<int>(tex.textureType()), retrievedTextureInfo, |
| 138 | textureInfo.fTarget, textureInfo.fFormat, colorType); |
| 139 | break; |
| 140 | } |
| 141 | case GrBackendApi::kVulkan: { |
| 142 | GrVkImageInfo imageInfo; |
Brian Osman | c75487e | 2023-08-29 18:10:51 +0000 | [diff] [blame] | 143 | bool retrievedImageInfo = GrBackendTextures::GetVkImageInfo(tex, &imageInfo); |
Leon Scroggins III | 41c00c5 | 2023-06-21 15:55:55 -0400 | [diff] [blame] | 144 | LOG_ALWAYS_FATAL("%s isTextureValid:%d dataspace:%d" |
| 145 | "\n\tGrBackendTexture: (%i x %i) hasMipmaps: %i isProtected: %i " |
| 146 | "texType: %i\n\t\tVkImageInfo: success: %i fFormat: %i " |
| 147 | "fSampleCount: %u fLevelCount: %u colorType %i", |
Yi Kong | 9bb855d | 2024-04-11 23:17:27 +0000 | [diff] [blame] | 148 | msg, tex.isValid(), static_cast<int32_t>(dataspace), tex.width(), |
| 149 | tex.height(), tex.hasMipmaps(), tex.isProtected(), |
Leon Scroggins III | 41c00c5 | 2023-06-21 15:55:55 -0400 | [diff] [blame] | 150 | static_cast<int>(tex.textureType()), retrievedImageInfo, |
| 151 | imageInfo.fFormat, imageInfo.fSampleCount, imageInfo.fLevelCount, |
| 152 | colorType); |
| 153 | break; |
| 154 | } |
| 155 | default: |
| 156 | LOG_ALWAYS_FATAL("%s Unexpected backend %u", msg, static_cast<unsigned>(tex.backend())); |
| 157 | break; |
| 158 | } |
Leon Scroggins III | c10321d | 2023-04-14 17:01:09 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 161 | sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType, |
| 162 | GrDirectContext* context) { |
| 163 | ATRACE_CALL(); |
| 164 | |
| 165 | if (mBackendTexture.isValid()) { |
| 166 | mUpdateProc(mImageCtx, context); |
| 167 | } |
| 168 | |
Leon Scroggins III | c4e0cbd | 2021-05-25 10:25:20 -0400 | [diff] [blame] | 169 | auto colorType = mColorType; |
| 170 | if (alphaType == kOpaque_SkAlphaType) { |
| 171 | if (colorType == kRGBA_8888_SkColorType) { |
| 172 | colorType = kRGB_888x_SkColorType; |
| 173 | } |
| 174 | } |
| 175 | |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 176 | sk_sp<SkImage> image = |
Kevin Lubick | e7fb46f | 2023-03-28 15:46:28 +0000 | [diff] [blame] | 177 | SkImages::BorrowTextureFrom(context, mBackendTexture, kTopLeft_GrSurfaceOrigin, |
| 178 | colorType, alphaType, toSkColorSpace(dataspace), |
| 179 | releaseImageProc, this); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 180 | if (image.get()) { |
| 181 | // The following ref will be counteracted by releaseProc, when SkImage is discarded. |
| 182 | ref(); |
| 183 | } |
| 184 | |
| 185 | mImage = image; |
| 186 | mDataspace = dataspace; |
Leon Scroggins III | c10321d | 2023-04-14 17:01:09 -0400 | [diff] [blame] | 187 | if (!mImage) { |
| 188 | logFatalTexture("Unable to generate SkImage.", mBackendTexture, dataspace, colorType); |
| 189 | } |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 190 | return mImage; |
| 191 | } |
| 192 | |
| 193 | sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace, |
| 194 | GrDirectContext* context) { |
| 195 | ATRACE_CALL(); |
Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 196 | LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture"); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 197 | if (!mSurface.get() || mDataspace != dataspace) { |
| 198 | sk_sp<SkSurface> surface = |
Kevin Lubick | 00bec72 | 2023-05-12 19:26:03 +0000 | [diff] [blame] | 199 | SkSurfaces::WrapBackendTexture(context, mBackendTexture, |
| 200 | kTopLeft_GrSurfaceOrigin, 0, mColorType, |
| 201 | toSkColorSpace(dataspace), nullptr, |
| 202 | releaseSurfaceProc, this); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 203 | if (surface.get()) { |
| 204 | // The following ref will be counteracted by releaseProc, when SkSurface is discarded. |
| 205 | ref(); |
| 206 | } |
| 207 | mSurface = surface; |
| 208 | } |
| 209 | |
| 210 | mDataspace = dataspace; |
Leon Scroggins III | c10321d | 2023-04-14 17:01:09 -0400 | [diff] [blame] | 211 | if (!mSurface) { |
| 212 | logFatalTexture("Unable to generate SkSurface.", mBackendTexture, dataspace, mColorType); |
| 213 | } |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 214 | return mSurface; |
| 215 | } |
| 216 | |
| 217 | } // namespace skia |
| 218 | } // namespace renderengine |
Alec Mouri | c6709cc | 2021-04-22 17:59:00 -0700 | [diff] [blame] | 219 | } // namespace android |