blob: dad3c1993bc5187fd6f167f57471d29477091841 [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 Lubicke7fb46f2023-03-28 15:46:28 +000026
Kevin Lubick40ff9892023-05-19 14:02:22 +000027#include <android/hardware_buffer.h>
Alec Mouric0aae732021-01-12 13:32:18 -080028#include "ColorSpaces.h"
Alec Mouric7f6c8b2020-11-09 18:35:20 -080029#include "log/log_main.h"
30#include "utils/Trace.h"
31
32namespace android {
33namespace renderengine {
34namespace skia {
35
Derek Sollenberger34257882021-04-06 18:32:34 +000036AutoBackendTexture::AutoBackendTexture(GrDirectContext* context, AHardwareBuffer* buffer,
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040037 bool isOutputBuffer, CleanupManager& cleanupMgr)
38 : mCleanupMgr(cleanupMgr), mIsOutputBuffer(isOutputBuffer) {
Alec Mourie1f81982021-01-11 10:24:27 -080039 ATRACE_CALL();
Alec Mouric7f6c8b2020-11-09 18:35:20 -080040 AHardwareBuffer_Desc desc;
41 AHardwareBuffer_describe(buffer, &desc);
Derek Sollenberger34257882021-04-06 18:32:34 +000042 bool createProtectedImage = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080043 GrBackendFormat backendFormat =
44 GrAHardwareBufferUtils::GetBackendFormat(context, buffer, desc.format, false);
45 mBackendTexture =
46 GrAHardwareBufferUtils::MakeBackendTexture(context, buffer, desc.width, desc.height,
47 &mDeleteProc, &mUpdateProc, &mImageCtx,
48 createProtectedImage, backendFormat,
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -040049 isOutputBuffer);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080050 mColorType = GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(desc.format);
Leon Scroggins IIIfd1f5572023-04-26 15:59:48 -040051 if (!mBackendTexture.isValid() || !desc.width || !desc.height) {
52 LOG_ALWAYS_FATAL("Failed to create a valid texture. [%p]:[%d,%d] isProtected:%d "
53 "isWriteable:%d format:%d",
54 this, desc.width, desc.height, createProtectedImage, isOutputBuffer,
55 desc.format);
56 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -080057}
58
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040059AutoBackendTexture::~AutoBackendTexture() {
60 if (mBackendTexture.isValid()) {
61 mDeleteProc(mImageCtx);
62 mBackendTexture = {};
63 }
64}
65
Alec Mouric7f6c8b2020-11-09 18:35:20 -080066void AutoBackendTexture::unref(bool releaseLocalResources) {
67 if (releaseLocalResources) {
68 mSurface = nullptr;
69 mImage = nullptr;
70 }
71
72 mUsageCount--;
73 if (mUsageCount <= 0) {
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040074 mCleanupMgr.add(this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080075 }
76}
77
78// releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use.
79// "releaseContext" contains an "AutoBackendTexture*".
80void AutoBackendTexture::releaseSurfaceProc(SkSurface::ReleaseContext releaseContext) {
81 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
82 textureRelease->unref(false);
83}
84
85// releaseImageProc is invoked by SkImage, when the texture is no longer in use.
86// "releaseContext" contains an "AutoBackendTexture*".
Kevin Lubick95afb8a2023-05-11 14:57:41 +000087void AutoBackendTexture::releaseImageProc(SkImages::ReleaseContext releaseContext) {
Alec Mouric7f6c8b2020-11-09 18:35:20 -080088 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
89 textureRelease->unref(false);
90}
91
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -040092void logFatalTexture(const char* msg, const GrBackendTexture& tex, ui::Dataspace dataspace,
93 SkColorType colorType) {
Leon Scroggins III41c00c52023-06-21 15:55:55 -040094 switch (tex.backend()) {
95 case GrBackendApi::kOpenGL: {
96 GrGLTextureInfo textureInfo;
97 bool retrievedTextureInfo = tex.getGLTextureInfo(&textureInfo);
98 LOG_ALWAYS_FATAL("%s isTextureValid:%d dataspace:%d"
99 "\n\tGrBackendTexture: (%i x %i) hasMipmaps: %i isProtected: %i "
100 "texType: %i\n\t\tGrGLTextureInfo: success: %i fTarget: %u fFormat: %u"
101 " colorType %i",
102 msg, tex.isValid(), dataspace, tex.width(), tex.height(),
103 tex.hasMipmaps(), tex.isProtected(),
104 static_cast<int>(tex.textureType()), retrievedTextureInfo,
105 textureInfo.fTarget, textureInfo.fFormat, colorType);
106 break;
107 }
108 case GrBackendApi::kVulkan: {
109 GrVkImageInfo imageInfo;
110 bool retrievedImageInfo = tex.getVkImageInfo(&imageInfo);
111 LOG_ALWAYS_FATAL("%s isTextureValid:%d dataspace:%d"
112 "\n\tGrBackendTexture: (%i x %i) hasMipmaps: %i isProtected: %i "
113 "texType: %i\n\t\tVkImageInfo: success: %i fFormat: %i "
114 "fSampleCount: %u fLevelCount: %u colorType %i",
115 msg, tex.isValid(), dataspace, tex.width(), tex.height(),
116 tex.hasMipmaps(), tex.isProtected(),
117 static_cast<int>(tex.textureType()), retrievedImageInfo,
118 imageInfo.fFormat, imageInfo.fSampleCount, imageInfo.fLevelCount,
119 colorType);
120 break;
121 }
122 default:
123 LOG_ALWAYS_FATAL("%s Unexpected backend %u", msg, static_cast<unsigned>(tex.backend()));
124 break;
125 }
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400126}
127
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800128sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
129 GrDirectContext* context) {
130 ATRACE_CALL();
131
132 if (mBackendTexture.isValid()) {
133 mUpdateProc(mImageCtx, context);
134 }
135
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -0400136 auto colorType = mColorType;
137 if (alphaType == kOpaque_SkAlphaType) {
138 if (colorType == kRGBA_8888_SkColorType) {
139 colorType = kRGB_888x_SkColorType;
140 }
141 }
142
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800143 sk_sp<SkImage> image =
Kevin Lubicke7fb46f2023-03-28 15:46:28 +0000144 SkImages::BorrowTextureFrom(context, mBackendTexture, kTopLeft_GrSurfaceOrigin,
145 colorType, alphaType, toSkColorSpace(dataspace),
146 releaseImageProc, this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800147 if (image.get()) {
148 // The following ref will be counteracted by releaseProc, when SkImage is discarded.
149 ref();
150 }
151
152 mImage = image;
153 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400154 if (!mImage) {
155 logFatalTexture("Unable to generate SkImage.", mBackendTexture, dataspace, colorType);
156 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800157 return mImage;
158}
159
160sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace,
161 GrDirectContext* context) {
162 ATRACE_CALL();
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400163 LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800164 if (!mSurface.get() || mDataspace != dataspace) {
165 sk_sp<SkSurface> surface =
Kevin Lubick00bec722023-05-12 19:26:03 +0000166 SkSurfaces::WrapBackendTexture(context, mBackendTexture,
167 kTopLeft_GrSurfaceOrigin, 0, mColorType,
168 toSkColorSpace(dataspace), nullptr,
169 releaseSurfaceProc, this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800170 if (surface.get()) {
171 // The following ref will be counteracted by releaseProc, when SkSurface is discarded.
172 ref();
173 }
174 mSurface = surface;
175 }
176
177 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400178 if (!mSurface) {
179 logFatalTexture("Unable to generate SkSurface.", mBackendTexture, dataspace, mColorType);
180 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800181 return mSurface;
182}
183
184} // namespace skia
185} // namespace renderengine
Alec Mouric6709cc2021-04-22 17:59:00 -0700186} // namespace android