blob: 23c99b0796faea73ccbe07093d4ae09f8179cf6a [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
Alec Mouric0aae732021-01-12 13:32:18 -080023#include "ColorSpaces.h"
Alec Mouric7f6c8b2020-11-09 18:35:20 -080024#include "log/log_main.h"
25#include "utils/Trace.h"
26
27namespace android {
28namespace renderengine {
29namespace skia {
30
Derek Sollenberger34257882021-04-06 18:32:34 +000031AutoBackendTexture::AutoBackendTexture(GrDirectContext* context, AHardwareBuffer* buffer,
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040032 bool isOutputBuffer, CleanupManager& cleanupMgr)
33 : mCleanupMgr(cleanupMgr), mIsOutputBuffer(isOutputBuffer) {
Alec Mourie1f81982021-01-11 10:24:27 -080034 ATRACE_CALL();
Alec Mouric7f6c8b2020-11-09 18:35:20 -080035 AHardwareBuffer_Desc desc;
36 AHardwareBuffer_describe(buffer, &desc);
Derek Sollenberger34257882021-04-06 18:32:34 +000037 bool createProtectedImage = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080038 GrBackendFormat backendFormat =
39 GrAHardwareBufferUtils::GetBackendFormat(context, buffer, desc.format, false);
40 mBackendTexture =
41 GrAHardwareBufferUtils::MakeBackendTexture(context, buffer, desc.width, desc.height,
42 &mDeleteProc, &mUpdateProc, &mImageCtx,
43 createProtectedImage, backendFormat,
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -040044 isOutputBuffer);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080045 mColorType = GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(desc.format);
Leon Scroggins IIIfd1f5572023-04-26 15:59:48 -040046 if (!mBackendTexture.isValid() || !desc.width || !desc.height) {
47 LOG_ALWAYS_FATAL("Failed to create a valid texture. [%p]:[%d,%d] isProtected:%d "
48 "isWriteable:%d format:%d",
49 this, desc.width, desc.height, createProtectedImage, isOutputBuffer,
50 desc.format);
51 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -080052}
53
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040054AutoBackendTexture::~AutoBackendTexture() {
55 if (mBackendTexture.isValid()) {
56 mDeleteProc(mImageCtx);
57 mBackendTexture = {};
58 }
59}
60
Alec Mouric7f6c8b2020-11-09 18:35:20 -080061void AutoBackendTexture::unref(bool releaseLocalResources) {
62 if (releaseLocalResources) {
63 mSurface = nullptr;
64 mImage = nullptr;
65 }
66
67 mUsageCount--;
68 if (mUsageCount <= 0) {
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040069 mCleanupMgr.add(this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080070 }
71}
72
73// releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use.
74// "releaseContext" contains an "AutoBackendTexture*".
75void AutoBackendTexture::releaseSurfaceProc(SkSurface::ReleaseContext releaseContext) {
76 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
77 textureRelease->unref(false);
78}
79
80// releaseImageProc is invoked by SkImage, when the texture is no longer in use.
81// "releaseContext" contains an "AutoBackendTexture*".
82void AutoBackendTexture::releaseImageProc(SkImage::ReleaseContext releaseContext) {
83 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
84 textureRelease->unref(false);
85}
86
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -040087void logFatalTexture(const char* msg, const GrBackendTexture& tex, ui::Dataspace dataspace,
88 SkColorType colorType) {
Leon Scroggins III41c00c52023-06-21 15:55:55 -040089 switch (tex.backend()) {
90 case GrBackendApi::kOpenGL: {
91 GrGLTextureInfo textureInfo;
92 bool retrievedTextureInfo = tex.getGLTextureInfo(&textureInfo);
93 LOG_ALWAYS_FATAL("%s isTextureValid:%d dataspace:%d"
94 "\n\tGrBackendTexture: (%i x %i) hasMipmaps: %i isProtected: %i "
95 "texType: %i\n\t\tGrGLTextureInfo: success: %i fTarget: %u fFormat: %u"
96 " colorType %i",
97 msg, tex.isValid(), dataspace, tex.width(), tex.height(),
98 tex.hasMipmaps(), tex.isProtected(),
99 static_cast<int>(tex.textureType()), retrievedTextureInfo,
100 textureInfo.fTarget, textureInfo.fFormat, colorType);
101 break;
102 }
103 case GrBackendApi::kVulkan: {
104 GrVkImageInfo imageInfo;
105 bool retrievedImageInfo = tex.getVkImageInfo(&imageInfo);
106 LOG_ALWAYS_FATAL("%s isTextureValid:%d dataspace:%d"
107 "\n\tGrBackendTexture: (%i x %i) hasMipmaps: %i isProtected: %i "
108 "texType: %i\n\t\tVkImageInfo: success: %i fFormat: %i "
109 "fSampleCount: %u fLevelCount: %u colorType %i",
110 msg, tex.isValid(), dataspace, tex.width(), tex.height(),
111 tex.hasMipmaps(), tex.isProtected(),
112 static_cast<int>(tex.textureType()), retrievedImageInfo,
113 imageInfo.fFormat, imageInfo.fSampleCount, imageInfo.fLevelCount,
114 colorType);
115 break;
116 }
117 default:
118 LOG_ALWAYS_FATAL("%s Unexpected backend %u", msg, static_cast<unsigned>(tex.backend()));
119 break;
120 }
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400121}
122
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800123sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
124 GrDirectContext* context) {
125 ATRACE_CALL();
126
127 if (mBackendTexture.isValid()) {
128 mUpdateProc(mImageCtx, context);
129 }
130
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -0400131 auto colorType = mColorType;
132 if (alphaType == kOpaque_SkAlphaType) {
133 if (colorType == kRGBA_8888_SkColorType) {
134 colorType = kRGB_888x_SkColorType;
135 }
136 }
137
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800138 sk_sp<SkImage> image =
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -0400139 SkImage::MakeFromTexture(context, mBackendTexture, kTopLeft_GrSurfaceOrigin, colorType,
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800140 alphaType, toSkColorSpace(dataspace), releaseImageProc, this);
141 if (image.get()) {
142 // The following ref will be counteracted by releaseProc, when SkImage is discarded.
143 ref();
144 }
145
146 mImage = image;
147 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400148 if (!mImage) {
149 logFatalTexture("Unable to generate SkImage.", mBackendTexture, dataspace, colorType);
150 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800151 return mImage;
152}
153
154sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace,
155 GrDirectContext* context) {
156 ATRACE_CALL();
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400157 LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800158 if (!mSurface.get() || mDataspace != dataspace) {
159 sk_sp<SkSurface> surface =
160 SkSurface::MakeFromBackendTexture(context, mBackendTexture,
161 kTopLeft_GrSurfaceOrigin, 0, mColorType,
162 toSkColorSpace(dataspace), nullptr,
163 releaseSurfaceProc, this);
164 if (surface.get()) {
165 // The following ref will be counteracted by releaseProc, when SkSurface is discarded.
166 ref();
167 }
168 mSurface = surface;
169 }
170
171 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400172 if (!mSurface) {
173 logFatalTexture("Unable to generate SkSurface.", mBackendTexture, dataspace, mColorType);
174 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800175 return mSurface;
176}
177
178} // namespace skia
179} // namespace renderengine
Alec Mouric6709cc2021-04-22 17:59:00 -0700180} // namespace android