blob: 932be56cdeb322c7aa2af28976cb073be346e879 [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);
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -040046 ALOGE_IF(!mBackendTexture.isValid(),
47 "Failed to create a valid texture. [%p]:[%d,%d] isProtected:%d isWriteable:%d "
48 "format:%d",
Alec Mouric6709cc2021-04-22 17:59:00 -070049 this, desc.width, desc.height, createProtectedImage, isOutputBuffer, desc.format);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080050}
51
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040052AutoBackendTexture::~AutoBackendTexture() {
53 if (mBackendTexture.isValid()) {
54 mDeleteProc(mImageCtx);
55 mBackendTexture = {};
56 }
57}
58
Alec Mouric7f6c8b2020-11-09 18:35:20 -080059void AutoBackendTexture::unref(bool releaseLocalResources) {
60 if (releaseLocalResources) {
61 mSurface = nullptr;
62 mImage = nullptr;
63 }
64
65 mUsageCount--;
66 if (mUsageCount <= 0) {
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040067 mCleanupMgr.add(this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080068 }
69}
70
71// releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use.
72// "releaseContext" contains an "AutoBackendTexture*".
73void AutoBackendTexture::releaseSurfaceProc(SkSurface::ReleaseContext releaseContext) {
74 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
75 textureRelease->unref(false);
76}
77
78// releaseImageProc is invoked by SkImage, when the texture is no longer in use.
79// "releaseContext" contains an "AutoBackendTexture*".
80void AutoBackendTexture::releaseImageProc(SkImage::ReleaseContext releaseContext) {
81 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
82 textureRelease->unref(false);
83}
84
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -040085void logFatalTexture(const char* msg, const GrBackendTexture& tex, ui::Dataspace dataspace,
86 SkColorType colorType) {
87 GrGLTextureInfo textureInfo;
88 bool retrievedTextureInfo = tex.getGLTextureInfo(&textureInfo);
89 LOG_ALWAYS_FATAL("%s isTextureValid:%d dataspace:%d"
90 "\n\tGrBackendTexture: (%i x %i) hasMipmaps: %i isProtected: %i texType: %i"
91 "\n\t\tGrGLTextureInfo: success: %i fTarget: %u fFormat: %u colorType %i",
92 msg, tex.isValid(), dataspace, tex.width(), tex.height(), tex.hasMipmaps(),
93 tex.isProtected(), static_cast<int>(tex.textureType()), retrievedTextureInfo,
94 textureInfo.fTarget, textureInfo.fFormat, colorType);
95}
96
Alec Mouric7f6c8b2020-11-09 18:35:20 -080097sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
98 GrDirectContext* context) {
99 ATRACE_CALL();
100
101 if (mBackendTexture.isValid()) {
102 mUpdateProc(mImageCtx, context);
103 }
104
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -0400105 auto colorType = mColorType;
106 if (alphaType == kOpaque_SkAlphaType) {
107 if (colorType == kRGBA_8888_SkColorType) {
108 colorType = kRGB_888x_SkColorType;
109 }
110 }
111
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800112 sk_sp<SkImage> image =
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -0400113 SkImage::MakeFromTexture(context, mBackendTexture, kTopLeft_GrSurfaceOrigin, colorType,
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800114 alphaType, toSkColorSpace(dataspace), releaseImageProc, this);
115 if (image.get()) {
116 // The following ref will be counteracted by releaseProc, when SkImage is discarded.
117 ref();
118 }
119
120 mImage = image;
121 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400122 if (!mImage) {
123 logFatalTexture("Unable to generate SkImage.", mBackendTexture, dataspace, colorType);
124 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800125 return mImage;
126}
127
128sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace,
129 GrDirectContext* context) {
130 ATRACE_CALL();
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400131 LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800132 if (!mSurface.get() || mDataspace != dataspace) {
133 sk_sp<SkSurface> surface =
134 SkSurface::MakeFromBackendTexture(context, mBackendTexture,
135 kTopLeft_GrSurfaceOrigin, 0, mColorType,
136 toSkColorSpace(dataspace), nullptr,
137 releaseSurfaceProc, this);
138 if (surface.get()) {
139 // The following ref will be counteracted by releaseProc, when SkSurface is discarded.
140 ref();
141 }
142 mSurface = surface;
143 }
144
145 mDataspace = dataspace;
Leon Scroggins IIIc10321d2023-04-14 17:01:09 -0400146 if (!mSurface) {
147 logFatalTexture("Unable to generate SkSurface.", mBackendTexture, dataspace, mColorType);
148 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800149 return mSurface;
150}
151
152} // namespace skia
153} // namespace renderengine
Alec Mouric6709cc2021-04-22 17:59:00 -0700154} // namespace android