blob: 8356005911ff76341632dcd6e3bbad23038c38f4 [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 Sollenbergerd8fdae32021-04-09 13:52:59 -040032 bool isOutputBuffer)
33 : 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
52void AutoBackendTexture::unref(bool releaseLocalResources) {
53 if (releaseLocalResources) {
54 mSurface = nullptr;
55 mImage = nullptr;
56 }
57
58 mUsageCount--;
59 if (mUsageCount <= 0) {
60 if (mBackendTexture.isValid()) {
61 mDeleteProc(mImageCtx);
62 mBackendTexture = {};
63 }
64 delete this;
65 }
66}
67
68// releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use.
69// "releaseContext" contains an "AutoBackendTexture*".
70void AutoBackendTexture::releaseSurfaceProc(SkSurface::ReleaseContext releaseContext) {
71 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
72 textureRelease->unref(false);
73}
74
75// releaseImageProc is invoked by SkImage, when the texture is no longer in use.
76// "releaseContext" contains an "AutoBackendTexture*".
77void AutoBackendTexture::releaseImageProc(SkImage::ReleaseContext releaseContext) {
78 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
79 textureRelease->unref(false);
80}
81
82sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
83 GrDirectContext* context) {
84 ATRACE_CALL();
85
86 if (mBackendTexture.isValid()) {
87 mUpdateProc(mImageCtx, context);
88 }
89
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -040090 auto colorType = mColorType;
91 if (alphaType == kOpaque_SkAlphaType) {
92 if (colorType == kRGBA_8888_SkColorType) {
93 colorType = kRGB_888x_SkColorType;
94 }
95 }
96
Alec Mouric7f6c8b2020-11-09 18:35:20 -080097 sk_sp<SkImage> image =
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -040098 SkImage::MakeFromTexture(context, mBackendTexture, kTopLeft_GrSurfaceOrigin, colorType,
Alec Mouric7f6c8b2020-11-09 18:35:20 -080099 alphaType, toSkColorSpace(dataspace), releaseImageProc, this);
100 if (image.get()) {
101 // The following ref will be counteracted by releaseProc, when SkImage is discarded.
102 ref();
103 }
104
105 mImage = image;
106 mDataspace = dataspace;
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400107 LOG_ALWAYS_FATAL_IF(mImage == nullptr,
108 "Unable to generate SkImage. isTextureValid:%d dataspace:%d",
109 mBackendTexture.isValid(), dataspace);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800110 return mImage;
111}
112
113sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace,
114 GrDirectContext* context) {
115 ATRACE_CALL();
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400116 LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800117 if (!mSurface.get() || mDataspace != dataspace) {
118 sk_sp<SkSurface> surface =
119 SkSurface::MakeFromBackendTexture(context, mBackendTexture,
120 kTopLeft_GrSurfaceOrigin, 0, mColorType,
121 toSkColorSpace(dataspace), nullptr,
122 releaseSurfaceProc, this);
123 if (surface.get()) {
124 // The following ref will be counteracted by releaseProc, when SkSurface is discarded.
125 ref();
126 }
127 mSurface = surface;
128 }
129
130 mDataspace = dataspace;
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400131 LOG_ALWAYS_FATAL_IF(mSurface == nullptr,
132 "Unable to generate SkSurface. isTextureValid:%d dataspace:%d",
133 mBackendTexture.isValid(), dataspace);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800134 return mSurface;
135}
136
137} // namespace skia
138} // namespace renderengine
Alec Mouric6709cc2021-04-22 17:59:00 -0700139} // namespace android