blob: c412c9cff7e697a3e8a6156a51542c5005358ac8 [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) {
89 GrGLTextureInfo textureInfo;
90 bool retrievedTextureInfo = tex.getGLTextureInfo(&textureInfo);
91 LOG_ALWAYS_FATAL("%s isTextureValid:%d dataspace:%d"
92 "\n\tGrBackendTexture: (%i x %i) hasMipmaps: %i isProtected: %i texType: %i"
93 "\n\t\tGrGLTextureInfo: success: %i fTarget: %u fFormat: %u colorType %i",
94 msg, tex.isValid(), dataspace, tex.width(), tex.height(), tex.hasMipmaps(),
95 tex.isProtected(), static_cast<int>(tex.textureType()), retrievedTextureInfo,
96 textureInfo.fTarget, textureInfo.fFormat, colorType);
97}
98
Alec Mouric7f6c8b2020-11-09 18:35:20 -080099sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
100 GrDirectContext* context) {
101 ATRACE_CALL();
102
103 if (mBackendTexture.isValid()) {
104 mUpdateProc(mImageCtx, context);
105 }
106
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -0400107 auto colorType = mColorType;
108 if (alphaType == kOpaque_SkAlphaType) {
109 if (colorType == kRGBA_8888_SkColorType) {
110 colorType = kRGB_888x_SkColorType;
111 }
112 }
113
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800114 sk_sp<SkImage> image =
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -0400115 SkImage::MakeFromTexture(context, mBackendTexture, kTopLeft_GrSurfaceOrigin, colorType,
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800116 alphaType, toSkColorSpace(dataspace), releaseImageProc, this);
117 if (image.get()) {
118 // The following ref will be counteracted by releaseProc, when SkImage is discarded.
119 ref();
120 }
121
122 mImage = image;
123 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400124 if (!mImage) {
125 logFatalTexture("Unable to generate SkImage.", mBackendTexture, dataspace, colorType);
126 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800127 return mImage;
128}
129
130sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace,
131 GrDirectContext* context) {
132 ATRACE_CALL();
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400133 LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800134 if (!mSurface.get() || mDataspace != dataspace) {
135 sk_sp<SkSurface> surface =
136 SkSurface::MakeFromBackendTexture(context, mBackendTexture,
137 kTopLeft_GrSurfaceOrigin, 0, mColorType,
138 toSkColorSpace(dataspace), nullptr,
139 releaseSurfaceProc, this);
140 if (surface.get()) {
141 // The following ref will be counteracted by releaseProc, when SkSurface is discarded.
142 ref();
143 }
144 mSurface = surface;
145 }
146
147 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400148 if (!mSurface) {
149 logFatalTexture("Unable to generate SkSurface.", mBackendTexture, dataspace, mColorType);
150 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800151 return mSurface;
152}
153
154} // namespace skia
155} // namespace renderengine
Alec Mouric6709cc2021-04-22 17:59:00 -0700156} // namespace android