blob: 02d1e1e748249c1a19da81e8b6ddcc43965aab76 [file] [log] [blame]
Alec Mouric7f6c8b2020-11-09 18:35:20 -08001/*
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 Lubicke7fb46f2023-03-28 15:46:28 +000023#include <SkImage.h>
24#include <include/gpu/ganesh/SkImageGanesh.h>
Kevin Lubick00bec722023-05-12 19:26:03 +000025#include <include/gpu/ganesh/SkSurfaceGanesh.h>
Kevin Lubick15f58d72023-08-07 15:00:58 +000026#include <include/gpu/ganesh/gl/GrGLBackendSurface.h>
Brian Osman613bb8e2023-08-21 19:23:42 +000027#include <include/gpu/vk/GrVkTypes.h>
Kevin Lubick40ff9892023-05-19 14:02:22 +000028#include <android/hardware_buffer.h>
Alec Mouric0aae732021-01-12 13:32:18 -080029#include "ColorSpaces.h"
Alec Mouric7f6c8b2020-11-09 18:35:20 -080030#include "log/log_main.h"
31#include "utils/Trace.h"
32
33namespace android {
34namespace renderengine {
35namespace skia {
36
Derek Sollenberger34257882021-04-06 18:32:34 +000037AutoBackendTexture::AutoBackendTexture(GrDirectContext* context, AHardwareBuffer* buffer,
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040038 bool isOutputBuffer, CleanupManager& cleanupMgr)
39 : mCleanupMgr(cleanupMgr), mIsOutputBuffer(isOutputBuffer) {
Alec Mourie1f81982021-01-11 10:24:27 -080040 ATRACE_CALL();
Alec Mouric7f6c8b2020-11-09 18:35:20 -080041 AHardwareBuffer_Desc desc;
42 AHardwareBuffer_describe(buffer, &desc);
Derek Sollenberger34257882021-04-06 18:32:34 +000043 bool createProtectedImage = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
Kevin Lubick15f58d72023-08-07 15:00:58 +000044 GrBackendFormat backendFormat;
45
46 GrBackendApi backend = context->backend();
47 if (backend == GrBackendApi::kOpenGL) {
48 backendFormat =
49 GrAHardwareBufferUtils::GetGLBackendFormat(context, desc.format, false);
50 mBackendTexture =
51 GrAHardwareBufferUtils::MakeGLBackendTexture(context,
52 buffer,
53 desc.width,
54 desc.height,
55 &mDeleteProc,
56 &mUpdateProc,
57 &mImageCtx,
58 createProtectedImage,
59 backendFormat,
60 isOutputBuffer);
61 } else if (backend == GrBackendApi::kVulkan) {
62 backendFormat =
63 GrAHardwareBufferUtils::GetVulkanBackendFormat(context,
64 buffer,
65 desc.format,
66 false);
67 mBackendTexture =
68 GrAHardwareBufferUtils::MakeVulkanBackendTexture(context,
69 buffer,
70 desc.width,
71 desc.height,
72 &mDeleteProc,
73 &mUpdateProc,
74 &mImageCtx,
75 createProtectedImage,
76 backendFormat,
77 isOutputBuffer);
78 } else {
79 LOG_ALWAYS_FATAL("Unexpected backend %d", backend);
80 }
81
Alec Mouric7f6c8b2020-11-09 18:35:20 -080082 mColorType = GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(desc.format);
Leon Scroggins IIIfd1f5572023-04-26 15:59:48 -040083 if (!mBackendTexture.isValid() || !desc.width || !desc.height) {
84 LOG_ALWAYS_FATAL("Failed to create a valid texture. [%p]:[%d,%d] isProtected:%d "
85 "isWriteable:%d format:%d",
86 this, desc.width, desc.height, createProtectedImage, isOutputBuffer,
87 desc.format);
88 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -080089}
90
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040091AutoBackendTexture::~AutoBackendTexture() {
92 if (mBackendTexture.isValid()) {
93 mDeleteProc(mImageCtx);
94 mBackendTexture = {};
95 }
96}
97
Alec Mouric7f6c8b2020-11-09 18:35:20 -080098void AutoBackendTexture::unref(bool releaseLocalResources) {
99 if (releaseLocalResources) {
100 mSurface = nullptr;
101 mImage = nullptr;
102 }
103
104 mUsageCount--;
105 if (mUsageCount <= 0) {
Derek Sollenbergerd3f60652021-06-11 15:34:36 -0400106 mCleanupMgr.add(this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800107 }
108}
109
110// releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use.
111// "releaseContext" contains an "AutoBackendTexture*".
112void AutoBackendTexture::releaseSurfaceProc(SkSurface::ReleaseContext releaseContext) {
113 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
114 textureRelease->unref(false);
115}
116
117// releaseImageProc is invoked by SkImage, when the texture is no longer in use.
118// "releaseContext" contains an "AutoBackendTexture*".
Kevin Lubick95afb8a2023-05-11 14:57:41 +0000119void AutoBackendTexture::releaseImageProc(SkImages::ReleaseContext releaseContext) {
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800120 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
121 textureRelease->unref(false);
122}
123
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400124void logFatalTexture(const char* msg, const GrBackendTexture& tex, ui::Dataspace dataspace,
125 SkColorType colorType) {
Leon Scroggins III41c00c52023-06-21 15:55:55 -0400126 switch (tex.backend()) {
127 case GrBackendApi::kOpenGL: {
128 GrGLTextureInfo textureInfo;
Kevin Lubick15f58d72023-08-07 15:00:58 +0000129 bool retrievedTextureInfo = GrBackendTextures::GetGLTextureInfo(tex, &textureInfo);
Leon Scroggins III41c00c52023-06-21 15:55:55 -0400130 LOG_ALWAYS_FATAL("%s isTextureValid:%d dataspace:%d"
131 "\n\tGrBackendTexture: (%i x %i) hasMipmaps: %i isProtected: %i "
132 "texType: %i\n\t\tGrGLTextureInfo: success: %i fTarget: %u fFormat: %u"
133 " colorType %i",
134 msg, tex.isValid(), dataspace, tex.width(), tex.height(),
135 tex.hasMipmaps(), tex.isProtected(),
136 static_cast<int>(tex.textureType()), retrievedTextureInfo,
137 textureInfo.fTarget, textureInfo.fFormat, colorType);
138 break;
139 }
140 case GrBackendApi::kVulkan: {
141 GrVkImageInfo imageInfo;
142 bool retrievedImageInfo = tex.getVkImageInfo(&imageInfo);
143 LOG_ALWAYS_FATAL("%s isTextureValid:%d dataspace:%d"
144 "\n\tGrBackendTexture: (%i x %i) hasMipmaps: %i isProtected: %i "
145 "texType: %i\n\t\tVkImageInfo: success: %i fFormat: %i "
146 "fSampleCount: %u fLevelCount: %u colorType %i",
147 msg, tex.isValid(), dataspace, tex.width(), tex.height(),
148 tex.hasMipmaps(), tex.isProtected(),
149 static_cast<int>(tex.textureType()), retrievedImageInfo,
150 imageInfo.fFormat, imageInfo.fSampleCount, imageInfo.fLevelCount,
151 colorType);
152 break;
153 }
154 default:
155 LOG_ALWAYS_FATAL("%s Unexpected backend %u", msg, static_cast<unsigned>(tex.backend()));
156 break;
157 }
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400158}
159
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800160sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
161 GrDirectContext* context) {
162 ATRACE_CALL();
163
164 if (mBackendTexture.isValid()) {
165 mUpdateProc(mImageCtx, context);
166 }
167
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -0400168 auto colorType = mColorType;
169 if (alphaType == kOpaque_SkAlphaType) {
170 if (colorType == kRGBA_8888_SkColorType) {
171 colorType = kRGB_888x_SkColorType;
172 }
173 }
174
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800175 sk_sp<SkImage> image =
Kevin Lubicke7fb46f2023-03-28 15:46:28 +0000176 SkImages::BorrowTextureFrom(context, mBackendTexture, kTopLeft_GrSurfaceOrigin,
177 colorType, alphaType, toSkColorSpace(dataspace),
178 releaseImageProc, this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800179 if (image.get()) {
180 // The following ref will be counteracted by releaseProc, when SkImage is discarded.
181 ref();
182 }
183
184 mImage = image;
185 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400186 if (!mImage) {
187 logFatalTexture("Unable to generate SkImage.", mBackendTexture, dataspace, colorType);
188 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800189 return mImage;
190}
191
192sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace,
193 GrDirectContext* context) {
194 ATRACE_CALL();
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400195 LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800196 if (!mSurface.get() || mDataspace != dataspace) {
197 sk_sp<SkSurface> surface =
Kevin Lubick00bec722023-05-12 19:26:03 +0000198 SkSurfaces::WrapBackendTexture(context, mBackendTexture,
199 kTopLeft_GrSurfaceOrigin, 0, mColorType,
200 toSkColorSpace(dataspace), nullptr,
201 releaseSurfaceProc, this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800202 if (surface.get()) {
203 // The following ref will be counteracted by releaseProc, when SkSurface is discarded.
204 ref();
205 }
206 mSurface = surface;
207 }
208
209 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400210 if (!mSurface) {
211 logFatalTexture("Unable to generate SkSurface.", mBackendTexture, dataspace, mColorType);
212 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800213 return mSurface;
214}
215
216} // namespace skia
217} // namespace renderengine
Alec Mouric6709cc2021-04-22 17:59:00 -0700218} // namespace android