blob: 76fc92dd18eb14a983ab7fb1afa01d06027235aa [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
Alec Mouric0aae732021-01-12 13:32:18 -080027#include "ColorSpaces.h"
Alec Mouric7f6c8b2020-11-09 18:35:20 -080028#include "log/log_main.h"
29#include "utils/Trace.h"
30
31namespace android {
32namespace renderengine {
33namespace skia {
34
Derek Sollenberger34257882021-04-06 18:32:34 +000035AutoBackendTexture::AutoBackendTexture(GrDirectContext* context, AHardwareBuffer* buffer,
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040036 bool isOutputBuffer, CleanupManager& cleanupMgr)
37 : mCleanupMgr(cleanupMgr), mIsOutputBuffer(isOutputBuffer) {
Alec Mourie1f81982021-01-11 10:24:27 -080038 ATRACE_CALL();
Alec Mouric7f6c8b2020-11-09 18:35:20 -080039 AHardwareBuffer_Desc desc;
40 AHardwareBuffer_describe(buffer, &desc);
Derek Sollenberger34257882021-04-06 18:32:34 +000041 bool createProtectedImage = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080042 GrBackendFormat backendFormat =
43 GrAHardwareBufferUtils::GetBackendFormat(context, buffer, desc.format, false);
44 mBackendTexture =
45 GrAHardwareBufferUtils::MakeBackendTexture(context, buffer, desc.width, desc.height,
46 &mDeleteProc, &mUpdateProc, &mImageCtx,
47 createProtectedImage, backendFormat,
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -040048 isOutputBuffer);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080049 mColorType = GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(desc.format);
Leon Scroggins IIIfd1f5572023-04-26 15:59:48 -040050 if (!mBackendTexture.isValid() || !desc.width || !desc.height) {
51 LOG_ALWAYS_FATAL("Failed to create a valid texture. [%p]:[%d,%d] isProtected:%d "
52 "isWriteable:%d format:%d",
53 this, desc.width, desc.height, createProtectedImage, isOutputBuffer,
54 desc.format);
55 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -080056}
57
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040058AutoBackendTexture::~AutoBackendTexture() {
59 if (mBackendTexture.isValid()) {
60 mDeleteProc(mImageCtx);
61 mBackendTexture = {};
62 }
63}
64
Alec Mouric7f6c8b2020-11-09 18:35:20 -080065void AutoBackendTexture::unref(bool releaseLocalResources) {
66 if (releaseLocalResources) {
67 mSurface = nullptr;
68 mImage = nullptr;
69 }
70
71 mUsageCount--;
72 if (mUsageCount <= 0) {
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040073 mCleanupMgr.add(this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080074 }
75}
76
77// releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use.
78// "releaseContext" contains an "AutoBackendTexture*".
79void AutoBackendTexture::releaseSurfaceProc(SkSurface::ReleaseContext releaseContext) {
80 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
81 textureRelease->unref(false);
82}
83
84// releaseImageProc is invoked by SkImage, when the texture is no longer in use.
85// "releaseContext" contains an "AutoBackendTexture*".
Kevin Lubick95afb8a2023-05-11 14:57:41 +000086void AutoBackendTexture::releaseImageProc(SkImages::ReleaseContext releaseContext) {
Alec Mouric7f6c8b2020-11-09 18:35:20 -080087 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
88 textureRelease->unref(false);
89}
90
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -040091void logFatalTexture(const char* msg, const GrBackendTexture& tex, ui::Dataspace dataspace,
92 SkColorType colorType) {
93 GrGLTextureInfo textureInfo;
94 bool retrievedTextureInfo = tex.getGLTextureInfo(&textureInfo);
95 LOG_ALWAYS_FATAL("%s isTextureValid:%d dataspace:%d"
96 "\n\tGrBackendTexture: (%i x %i) hasMipmaps: %i isProtected: %i texType: %i"
97 "\n\t\tGrGLTextureInfo: success: %i fTarget: %u fFormat: %u colorType %i",
98 msg, tex.isValid(), dataspace, tex.width(), tex.height(), tex.hasMipmaps(),
99 tex.isProtected(), static_cast<int>(tex.textureType()), retrievedTextureInfo,
100 textureInfo.fTarget, textureInfo.fFormat, colorType);
101}
102
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800103sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
104 GrDirectContext* context) {
105 ATRACE_CALL();
106
107 if (mBackendTexture.isValid()) {
108 mUpdateProc(mImageCtx, context);
109 }
110
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -0400111 auto colorType = mColorType;
112 if (alphaType == kOpaque_SkAlphaType) {
113 if (colorType == kRGBA_8888_SkColorType) {
114 colorType = kRGB_888x_SkColorType;
115 }
116 }
117
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800118 sk_sp<SkImage> image =
Kevin Lubicke7fb46f2023-03-28 15:46:28 +0000119 SkImages::BorrowTextureFrom(context, mBackendTexture, kTopLeft_GrSurfaceOrigin,
120 colorType, alphaType, toSkColorSpace(dataspace),
121 releaseImageProc, this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800122 if (image.get()) {
123 // The following ref will be counteracted by releaseProc, when SkImage is discarded.
124 ref();
125 }
126
127 mImage = image;
128 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400129 if (!mImage) {
130 logFatalTexture("Unable to generate SkImage.", mBackendTexture, dataspace, colorType);
131 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800132 return mImage;
133}
134
135sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace,
136 GrDirectContext* context) {
137 ATRACE_CALL();
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400138 LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800139 if (!mSurface.get() || mDataspace != dataspace) {
140 sk_sp<SkSurface> surface =
Kevin Lubick00bec722023-05-12 19:26:03 +0000141 SkSurfaces::WrapBackendTexture(context, mBackendTexture,
142 kTopLeft_GrSurfaceOrigin, 0, mColorType,
143 toSkColorSpace(dataspace), nullptr,
144 releaseSurfaceProc, this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800145 if (surface.get()) {
146 // The following ref will be counteracted by releaseProc, when SkSurface is discarded.
147 ref();
148 }
149 mSurface = surface;
150 }
151
152 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400153 if (!mSurface) {
154 logFatalTexture("Unable to generate SkSurface.", mBackendTexture, dataspace, mColorType);
155 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800156 return mSurface;
157}
158
159} // namespace skia
160} // namespace renderengine
Alec Mouric6709cc2021-04-22 17:59:00 -0700161} // namespace android