blob: f3ef968d40495d2dc72e88055e40e39d27917c94 [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>
25
Alec Mouric0aae732021-01-12 13:32:18 -080026#include "ColorSpaces.h"
Alec Mouric7f6c8b2020-11-09 18:35:20 -080027#include "log/log_main.h"
28#include "utils/Trace.h"
29
30namespace android {
31namespace renderengine {
32namespace skia {
33
Derek Sollenberger34257882021-04-06 18:32:34 +000034AutoBackendTexture::AutoBackendTexture(GrDirectContext* context, AHardwareBuffer* buffer,
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040035 bool isOutputBuffer, CleanupManager& cleanupMgr)
36 : mCleanupMgr(cleanupMgr), mIsOutputBuffer(isOutputBuffer) {
Alec Mourie1f81982021-01-11 10:24:27 -080037 ATRACE_CALL();
Alec Mouric7f6c8b2020-11-09 18:35:20 -080038 AHardwareBuffer_Desc desc;
39 AHardwareBuffer_describe(buffer, &desc);
Derek Sollenberger34257882021-04-06 18:32:34 +000040 bool createProtectedImage = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080041 GrBackendFormat backendFormat =
42 GrAHardwareBufferUtils::GetBackendFormat(context, buffer, desc.format, false);
43 mBackendTexture =
44 GrAHardwareBufferUtils::MakeBackendTexture(context, buffer, desc.width, desc.height,
45 &mDeleteProc, &mUpdateProc, &mImageCtx,
46 createProtectedImage, backendFormat,
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -040047 isOutputBuffer);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080048 mColorType = GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(desc.format);
Leon Scroggins IIIfd1f5572023-04-26 15:59:48 -040049 if (!mBackendTexture.isValid() || !desc.width || !desc.height) {
50 LOG_ALWAYS_FATAL("Failed to create a valid texture. [%p]:[%d,%d] isProtected:%d "
51 "isWriteable:%d format:%d",
52 this, desc.width, desc.height, createProtectedImage, isOutputBuffer,
53 desc.format);
54 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -080055}
56
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040057AutoBackendTexture::~AutoBackendTexture() {
58 if (mBackendTexture.isValid()) {
59 mDeleteProc(mImageCtx);
60 mBackendTexture = {};
61 }
62}
63
Alec Mouric7f6c8b2020-11-09 18:35:20 -080064void AutoBackendTexture::unref(bool releaseLocalResources) {
65 if (releaseLocalResources) {
66 mSurface = nullptr;
67 mImage = nullptr;
68 }
69
70 mUsageCount--;
71 if (mUsageCount <= 0) {
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040072 mCleanupMgr.add(this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080073 }
74}
75
76// releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use.
77// "releaseContext" contains an "AutoBackendTexture*".
78void AutoBackendTexture::releaseSurfaceProc(SkSurface::ReleaseContext releaseContext) {
79 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
80 textureRelease->unref(false);
81}
82
83// releaseImageProc is invoked by SkImage, when the texture is no longer in use.
84// "releaseContext" contains an "AutoBackendTexture*".
Kevin Lubick95afb8a2023-05-11 14:57:41 +000085void AutoBackendTexture::releaseImageProc(SkImages::ReleaseContext releaseContext) {
Alec Mouric7f6c8b2020-11-09 18:35:20 -080086 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
87 textureRelease->unref(false);
88}
89
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -040090void logFatalTexture(const char* msg, const GrBackendTexture& tex, ui::Dataspace dataspace,
91 SkColorType colorType) {
92 GrGLTextureInfo textureInfo;
93 bool retrievedTextureInfo = tex.getGLTextureInfo(&textureInfo);
94 LOG_ALWAYS_FATAL("%s isTextureValid:%d dataspace:%d"
95 "\n\tGrBackendTexture: (%i x %i) hasMipmaps: %i isProtected: %i texType: %i"
96 "\n\t\tGrGLTextureInfo: success: %i fTarget: %u fFormat: %u colorType %i",
97 msg, tex.isValid(), dataspace, tex.width(), tex.height(), tex.hasMipmaps(),
98 tex.isProtected(), static_cast<int>(tex.textureType()), retrievedTextureInfo,
99 textureInfo.fTarget, textureInfo.fFormat, colorType);
100}
101
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800102sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
103 GrDirectContext* context) {
104 ATRACE_CALL();
105
106 if (mBackendTexture.isValid()) {
107 mUpdateProc(mImageCtx, context);
108 }
109
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -0400110 auto colorType = mColorType;
111 if (alphaType == kOpaque_SkAlphaType) {
112 if (colorType == kRGBA_8888_SkColorType) {
113 colorType = kRGB_888x_SkColorType;
114 }
115 }
116
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800117 sk_sp<SkImage> image =
Kevin Lubicke7fb46f2023-03-28 15:46:28 +0000118 SkImages::BorrowTextureFrom(context, mBackendTexture, kTopLeft_GrSurfaceOrigin,
119 colorType, alphaType, toSkColorSpace(dataspace),
120 releaseImageProc, this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800121 if (image.get()) {
122 // The following ref will be counteracted by releaseProc, when SkImage is discarded.
123 ref();
124 }
125
126 mImage = image;
127 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400128 if (!mImage) {
129 logFatalTexture("Unable to generate SkImage.", mBackendTexture, dataspace, colorType);
130 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800131 return mImage;
132}
133
134sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace,
135 GrDirectContext* context) {
136 ATRACE_CALL();
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400137 LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800138 if (!mSurface.get() || mDataspace != dataspace) {
139 sk_sp<SkSurface> surface =
140 SkSurface::MakeFromBackendTexture(context, mBackendTexture,
141 kTopLeft_GrSurfaceOrigin, 0, mColorType,
142 toSkColorSpace(dataspace), nullptr,
143 releaseSurfaceProc, this);
144 if (surface.get()) {
145 // The following ref will be counteracted by releaseProc, when SkSurface is discarded.
146 ref();
147 }
148 mSurface = surface;
149 }
150
151 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400152 if (!mSurface) {
153 logFatalTexture("Unable to generate SkSurface.", mBackendTexture, dataspace, mColorType);
154 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800155 return mSurface;
156}
157
158} // namespace skia
159} // namespace renderengine
Alec Mouric6709cc2021-04-22 17:59:00 -0700160} // namespace android