blob: 02e733705384b7fad5807bf70b658eccdbaed37e [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>
Nolan Scobiefc125ec2024-03-11 20:08:27 -040024#include <android/hardware_buffer.h>
Kevin Lubicke7fb46f2023-03-28 15:46:28 +000025#include <include/gpu/ganesh/SkImageGanesh.h>
Kevin Lubick00bec722023-05-12 19:26:03 +000026#include <include/gpu/ganesh/SkSurfaceGanesh.h>
Kevin Lubick15f58d72023-08-07 15:00:58 +000027#include <include/gpu/ganesh/gl/GrGLBackendSurface.h>
Brian Osmanc75487e2023-08-29 18:10:51 +000028#include <include/gpu/ganesh/vk/GrVkBackendSurface.h>
Brian Osman613bb8e2023-08-21 19:23:42 +000029#include <include/gpu/vk/GrVkTypes.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
Nolan Scobiefc125ec2024-03-11 20:08:27 -040038AutoBackendTexture::AutoBackendTexture(SkiaGpuContext* context, AHardwareBuffer* buffer,
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040039 bool isOutputBuffer, CleanupManager& cleanupMgr)
Nolan Scobiefc125ec2024-03-11 20:08:27 -040040 : mGrContext(context->grDirectContext()),
41 mCleanupMgr(cleanupMgr),
42 mIsOutputBuffer(isOutputBuffer) {
Alec Mourie1f81982021-01-11 10:24:27 -080043 ATRACE_CALL();
Nolan Scobiefc125ec2024-03-11 20:08:27 -040044
Alec Mouric7f6c8b2020-11-09 18:35:20 -080045 AHardwareBuffer_Desc desc;
46 AHardwareBuffer_describe(buffer, &desc);
Derek Sollenberger34257882021-04-06 18:32:34 +000047 bool createProtectedImage = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
Kevin Lubick15f58d72023-08-07 15:00:58 +000048 GrBackendFormat backendFormat;
49
Nolan Scobiefc125ec2024-03-11 20:08:27 -040050 GrBackendApi backend = mGrContext->backend();
Kevin Lubick15f58d72023-08-07 15:00:58 +000051 if (backend == GrBackendApi::kOpenGL) {
52 backendFormat =
Nolan Scobiefc125ec2024-03-11 20:08:27 -040053 GrAHardwareBufferUtils::GetGLBackendFormat(mGrContext.get(), desc.format, false);
Kevin Lubick15f58d72023-08-07 15:00:58 +000054 mBackendTexture =
Nolan Scobiefc125ec2024-03-11 20:08:27 -040055 GrAHardwareBufferUtils::MakeGLBackendTexture(mGrContext.get(), buffer, desc.width,
56 desc.height, &mDeleteProc,
57 &mUpdateProc, &mImageCtx,
58 createProtectedImage, backendFormat,
Kevin Lubick15f58d72023-08-07 15:00:58 +000059 isOutputBuffer);
60 } else if (backend == GrBackendApi::kVulkan) {
Nolan Scobiefc125ec2024-03-11 20:08:27 -040061 backendFormat = GrAHardwareBufferUtils::GetVulkanBackendFormat(mGrContext.get(), buffer,
62 desc.format, false);
Kevin Lubick15f58d72023-08-07 15:00:58 +000063 mBackendTexture =
Nolan Scobiefc125ec2024-03-11 20:08:27 -040064 GrAHardwareBufferUtils::MakeVulkanBackendTexture(mGrContext.get(), buffer,
65 desc.width, desc.height,
66 &mDeleteProc, &mUpdateProc,
67 &mImageCtx, createProtectedImage,
68 backendFormat, isOutputBuffer);
Kevin Lubick15f58d72023-08-07 15:00:58 +000069 } else {
Yi Kongcf05d7a2023-12-07 14:56:33 +090070 LOG_ALWAYS_FATAL("Unexpected backend %u", static_cast<unsigned>(backend));
Kevin Lubick15f58d72023-08-07 15:00:58 +000071 }
72
Alec Mouric7f6c8b2020-11-09 18:35:20 -080073 mColorType = GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(desc.format);
Leon Scroggins IIIfd1f5572023-04-26 15:59:48 -040074 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 Mouric7f6c8b2020-11-09 18:35:20 -080080}
81
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040082AutoBackendTexture::~AutoBackendTexture() {
83 if (mBackendTexture.isValid()) {
84 mDeleteProc(mImageCtx);
85 mBackendTexture = {};
86 }
87}
88
Alec Mouric7f6c8b2020-11-09 18:35:20 -080089void AutoBackendTexture::unref(bool releaseLocalResources) {
90 if (releaseLocalResources) {
91 mSurface = nullptr;
92 mImage = nullptr;
93 }
94
95 mUsageCount--;
96 if (mUsageCount <= 0) {
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040097 mCleanupMgr.add(this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080098 }
99}
100
101// releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use.
102// "releaseContext" contains an "AutoBackendTexture*".
103void 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 Lubick95afb8a2023-05-11 14:57:41 +0000110void AutoBackendTexture::releaseImageProc(SkImages::ReleaseContext releaseContext) {
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800111 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
112 textureRelease->unref(false);
113}
114
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400115void logFatalTexture(const char* msg, const GrBackendTexture& tex, ui::Dataspace dataspace,
116 SkColorType colorType) {
Leon Scroggins III41c00c52023-06-21 15:55:55 -0400117 switch (tex.backend()) {
118 case GrBackendApi::kOpenGL: {
119 GrGLTextureInfo textureInfo;
Kevin Lubick15f58d72023-08-07 15:00:58 +0000120 bool retrievedTextureInfo = GrBackendTextures::GetGLTextureInfo(tex, &textureInfo);
Leon Scroggins III41c00c52023-06-21 15:55:55 -0400121 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 Kongd90ebf32023-12-05 16:59:10 +0900125 msg, tex.isValid(), static_cast<int32_t>(dataspace), tex.width(),
126 tex.height(), tex.hasMipmaps(), tex.isProtected(),
Leon Scroggins III41c00c52023-06-21 15:55:55 -0400127 static_cast<int>(tex.textureType()), retrievedTextureInfo,
128 textureInfo.fTarget, textureInfo.fFormat, colorType);
129 break;
130 }
131 case GrBackendApi::kVulkan: {
132 GrVkImageInfo imageInfo;
Brian Osmanc75487e2023-08-29 18:10:51 +0000133 bool retrievedImageInfo = GrBackendTextures::GetVkImageInfo(tex, &imageInfo);
Leon Scroggins III41c00c52023-06-21 15:55:55 -0400134 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 Kongcf05d7a2023-12-07 14:56:33 +0900138 msg, tex.isValid(), static_cast<int32_t>(dataspace), tex.width(),
139 tex.height(), tex.hasMipmaps(), tex.isProtected(),
Leon Scroggins III41c00c52023-06-21 15:55:55 -0400140 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 IIIc10321d2023-04-14 17:01:09 -0400149}
150
Nolan Scobie17e25512024-03-13 18:02:48 -0400151sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType) {
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800152 ATRACE_CALL();
153
154 if (mBackendTexture.isValid()) {
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400155 mUpdateProc(mImageCtx, mGrContext.get());
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800156 }
157
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -0400158 auto colorType = mColorType;
159 if (alphaType == kOpaque_SkAlphaType) {
160 if (colorType == kRGBA_8888_SkColorType) {
161 colorType = kRGB_888x_SkColorType;
162 }
163 }
164
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800165 sk_sp<SkImage> image =
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400166 SkImages::BorrowTextureFrom(mGrContext.get(), mBackendTexture, kTopLeft_GrSurfaceOrigin,
Kevin Lubicke7fb46f2023-03-28 15:46:28 +0000167 colorType, alphaType, toSkColorSpace(dataspace),
168 releaseImageProc, this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800169 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 IIIc10321d2023-04-14 17:01:09 -0400176 if (!mImage) {
177 logFatalTexture("Unable to generate SkImage.", mBackendTexture, dataspace, colorType);
178 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800179 return mImage;
180}
181
Nolan Scobie17e25512024-03-13 18:02:48 -0400182sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace) {
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800183 ATRACE_CALL();
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400184 LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800185 if (!mSurface.get() || mDataspace != dataspace) {
186 sk_sp<SkSurface> surface =
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400187 SkSurfaces::WrapBackendTexture(mGrContext.get(), mBackendTexture,
Kevin Lubick00bec722023-05-12 19:26:03 +0000188 kTopLeft_GrSurfaceOrigin, 0, mColorType,
189 toSkColorSpace(dataspace), nullptr,
190 releaseSurfaceProc, this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800191 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 IIIc10321d2023-04-14 17:01:09 -0400199 if (!mSurface) {
200 logFatalTexture("Unable to generate SkSurface.", mBackendTexture, dataspace, mColorType);
201 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800202 return mSurface;
203}
204
205} // namespace skia
206} // namespace renderengine
Alec Mouric6709cc2021-04-22 17:59:00 -0700207} // namespace android