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