blob: fc9b4da8bf115d75f175972531c049fe218116f1 [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) {
94 GrGLTextureInfo textureInfo;
95 bool retrievedTextureInfo = tex.getGLTextureInfo(&textureInfo);
96 LOG_ALWAYS_FATAL("%s isTextureValid:%d dataspace:%d"
97 "\n\tGrBackendTexture: (%i x %i) hasMipmaps: %i isProtected: %i texType: %i"
98 "\n\t\tGrGLTextureInfo: success: %i fTarget: %u fFormat: %u colorType %i",
99 msg, tex.isValid(), dataspace, tex.width(), tex.height(), tex.hasMipmaps(),
100 tex.isProtected(), static_cast<int>(tex.textureType()), retrievedTextureInfo,
101 textureInfo.fTarget, textureInfo.fFormat, colorType);
102}
103
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800104sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
105 GrDirectContext* context) {
106 ATRACE_CALL();
107
108 if (mBackendTexture.isValid()) {
109 mUpdateProc(mImageCtx, context);
110 }
111
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -0400112 auto colorType = mColorType;
113 if (alphaType == kOpaque_SkAlphaType) {
114 if (colorType == kRGBA_8888_SkColorType) {
115 colorType = kRGB_888x_SkColorType;
116 }
117 }
118
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800119 sk_sp<SkImage> image =
Kevin Lubicke7fb46f2023-03-28 15:46:28 +0000120 SkImages::BorrowTextureFrom(context, mBackendTexture, kTopLeft_GrSurfaceOrigin,
121 colorType, alphaType, toSkColorSpace(dataspace),
122 releaseImageProc, this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800123 if (image.get()) {
124 // The following ref will be counteracted by releaseProc, when SkImage is discarded.
125 ref();
126 }
127
128 mImage = image;
129 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400130 if (!mImage) {
131 logFatalTexture("Unable to generate SkImage.", mBackendTexture, dataspace, colorType);
132 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800133 return mImage;
134}
135
136sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace,
137 GrDirectContext* context) {
138 ATRACE_CALL();
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400139 LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800140 if (!mSurface.get() || mDataspace != dataspace) {
141 sk_sp<SkSurface> surface =
Kevin Lubick00bec722023-05-12 19:26:03 +0000142 SkSurfaces::WrapBackendTexture(context, mBackendTexture,
143 kTopLeft_GrSurfaceOrigin, 0, mColorType,
144 toSkColorSpace(dataspace), nullptr,
145 releaseSurfaceProc, this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800146 if (surface.get()) {
147 // The following ref will be counteracted by releaseProc, when SkSurface is discarded.
148 ref();
149 }
150 mSurface = surface;
151 }
152
153 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400154 if (!mSurface) {
155 logFatalTexture("Unable to generate SkSurface.", mBackendTexture, dataspace, mColorType);
156 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800157 return mSurface;
158}
159
160} // namespace skia
161} // namespace renderengine
Alec Mouric6709cc2021-04-22 17:59:00 -0700162} // namespace android