blob: ee95e59d90fcb56b3c0deb6c3478c93679962635 [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 Osmanc75487e2023-08-29 18:10:51 +000027#include <include/gpu/ganesh/vk/GrVkBackendSurface.h>
Brian Osman613bb8e2023-08-21 19:23:42 +000028#include <include/gpu/vk/GrVkTypes.h>
Kevin Lubick40ff9892023-05-19 14:02:22 +000029#include <android/hardware_buffer.h>
Alec Mouric0aae732021-01-12 13:32:18 -080030#include "ColorSpaces.h"
Alec Mouric7f6c8b2020-11-09 18:35:20 -080031#include "log/log_main.h"
32#include "utils/Trace.h"
33
34namespace android {
35namespace renderengine {
36namespace skia {
37
Derek Sollenberger34257882021-04-06 18:32:34 +000038AutoBackendTexture::AutoBackendTexture(GrDirectContext* context, AHardwareBuffer* buffer,
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040039 bool isOutputBuffer, CleanupManager& cleanupMgr)
40 : mCleanupMgr(cleanupMgr), mIsOutputBuffer(isOutputBuffer) {
Alec Mourie1f81982021-01-11 10:24:27 -080041 ATRACE_CALL();
Alec Mouric7f6c8b2020-11-09 18:35:20 -080042 AHardwareBuffer_Desc desc;
43 AHardwareBuffer_describe(buffer, &desc);
Derek Sollenberger34257882021-04-06 18:32:34 +000044 bool createProtectedImage = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
Kevin Lubick15f58d72023-08-07 15:00:58 +000045 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 Kong9bb855d2024-04-11 23:17:27 +000080 LOG_ALWAYS_FATAL("Unexpected backend %u", static_cast<unsigned>(backend));
Kevin Lubick15f58d72023-08-07 15:00:58 +000081 }
82
Alec Mouric7f6c8b2020-11-09 18:35:20 -080083 mColorType = GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(desc.format);
Leon Scroggins IIIfd1f5572023-04-26 15:59:48 -040084 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 Mouric7f6c8b2020-11-09 18:35:20 -080090}
91
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040092AutoBackendTexture::~AutoBackendTexture() {
93 if (mBackendTexture.isValid()) {
94 mDeleteProc(mImageCtx);
95 mBackendTexture = {};
96 }
97}
98
Alec Mouric7f6c8b2020-11-09 18:35:20 -080099void AutoBackendTexture::unref(bool releaseLocalResources) {
100 if (releaseLocalResources) {
101 mSurface = nullptr;
102 mImage = nullptr;
103 }
104
105 mUsageCount--;
106 if (mUsageCount <= 0) {
Derek Sollenbergerd3f60652021-06-11 15:34:36 -0400107 mCleanupMgr.add(this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800108 }
109}
110
111// releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use.
112// "releaseContext" contains an "AutoBackendTexture*".
113void 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 Lubick95afb8a2023-05-11 14:57:41 +0000120void AutoBackendTexture::releaseImageProc(SkImages::ReleaseContext releaseContext) {
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800121 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
122 textureRelease->unref(false);
123}
124
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400125void logFatalTexture(const char* msg, const GrBackendTexture& tex, ui::Dataspace dataspace,
126 SkColorType colorType) {
Leon Scroggins III41c00c52023-06-21 15:55:55 -0400127 switch (tex.backend()) {
128 case GrBackendApi::kOpenGL: {
129 GrGLTextureInfo textureInfo;
Kevin Lubick15f58d72023-08-07 15:00:58 +0000130 bool retrievedTextureInfo = GrBackendTextures::GetGLTextureInfo(tex, &textureInfo);
Leon Scroggins III41c00c52023-06-21 15:55:55 -0400131 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 Kongd90ebf32023-12-05 16:59:10 +0900135 msg, tex.isValid(), static_cast<int32_t>(dataspace), tex.width(),
136 tex.height(), tex.hasMipmaps(), tex.isProtected(),
Leon Scroggins III41c00c52023-06-21 15:55:55 -0400137 static_cast<int>(tex.textureType()), retrievedTextureInfo,
138 textureInfo.fTarget, textureInfo.fFormat, colorType);
139 break;
140 }
141 case GrBackendApi::kVulkan: {
142 GrVkImageInfo imageInfo;
Brian Osmanc75487e2023-08-29 18:10:51 +0000143 bool retrievedImageInfo = GrBackendTextures::GetVkImageInfo(tex, &imageInfo);
Leon Scroggins III41c00c52023-06-21 15:55:55 -0400144 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 Kong9bb855d2024-04-11 23:17:27 +0000148 msg, tex.isValid(), static_cast<int32_t>(dataspace), tex.width(),
149 tex.height(), tex.hasMipmaps(), tex.isProtected(),
Leon Scroggins III41c00c52023-06-21 15:55:55 -0400150 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 IIIc10321d2023-04-14 17:01:09 -0400159}
160
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800161sk_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 IIIc4e0cbd2021-05-25 10:25:20 -0400169 auto colorType = mColorType;
170 if (alphaType == kOpaque_SkAlphaType) {
171 if (colorType == kRGBA_8888_SkColorType) {
172 colorType = kRGB_888x_SkColorType;
173 }
174 }
175
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800176 sk_sp<SkImage> image =
Kevin Lubicke7fb46f2023-03-28 15:46:28 +0000177 SkImages::BorrowTextureFrom(context, mBackendTexture, kTopLeft_GrSurfaceOrigin,
178 colorType, alphaType, toSkColorSpace(dataspace),
179 releaseImageProc, this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800180 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 IIIc10321d2023-04-14 17:01:09 -0400187 if (!mImage) {
188 logFatalTexture("Unable to generate SkImage.", mBackendTexture, dataspace, colorType);
189 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800190 return mImage;
191}
192
193sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace,
194 GrDirectContext* context) {
195 ATRACE_CALL();
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400196 LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800197 if (!mSurface.get() || mDataspace != dataspace) {
198 sk_sp<SkSurface> surface =
Kevin Lubick00bec722023-05-12 19:26:03 +0000199 SkSurfaces::WrapBackendTexture(context, mBackendTexture,
200 kTopLeft_GrSurfaceOrigin, 0, mColorType,
201 toSkColorSpace(dataspace), nullptr,
202 releaseSurfaceProc, this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800203 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 IIIc10321d2023-04-14 17:01:09 -0400211 if (!mSurface) {
212 logFatalTexture("Unable to generate SkSurface.", mBackendTexture, dataspace, mColorType);
213 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800214 return mSurface;
215}
216
217} // namespace skia
218} // namespace renderengine
Alec Mouric6709cc2021-04-22 17:59:00 -0700219} // namespace android