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