blob: 5c122d4154f8ccce51d77a05c947fc92f9e57548 [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
85sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
86 GrDirectContext* context) {
87 ATRACE_CALL();
88
89 if (mBackendTexture.isValid()) {
90 mUpdateProc(mImageCtx, context);
91 }
92
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -040093 auto colorType = mColorType;
94 if (alphaType == kOpaque_SkAlphaType) {
95 if (colorType == kRGBA_8888_SkColorType) {
96 colorType = kRGB_888x_SkColorType;
97 }
98 }
99
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800100 sk_sp<SkImage> image =
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -0400101 SkImage::MakeFromTexture(context, mBackendTexture, kTopLeft_GrSurfaceOrigin, colorType,
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800102 alphaType, toSkColorSpace(dataspace), releaseImageProc, this);
103 if (image.get()) {
104 // The following ref will be counteracted by releaseProc, when SkImage is discarded.
105 ref();
106 }
107
108 mImage = image;
109 mDataspace = dataspace;
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400110 LOG_ALWAYS_FATAL_IF(mImage == nullptr,
111 "Unable to generate SkImage. isTextureValid:%d dataspace:%d",
112 mBackendTexture.isValid(), dataspace);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800113 return mImage;
114}
115
116sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace,
117 GrDirectContext* context) {
118 ATRACE_CALL();
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400119 LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800120 if (!mSurface.get() || mDataspace != dataspace) {
121 sk_sp<SkSurface> surface =
122 SkSurface::MakeFromBackendTexture(context, mBackendTexture,
123 kTopLeft_GrSurfaceOrigin, 0, mColorType,
124 toSkColorSpace(dataspace), nullptr,
125 releaseSurfaceProc, this);
126 if (surface.get()) {
127 // The following ref will be counteracted by releaseProc, when SkSurface is discarded.
128 ref();
129 }
130 mSurface = surface;
131 }
132
133 mDataspace = dataspace;
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400134 LOG_ALWAYS_FATAL_IF(mSurface == nullptr,
135 "Unable to generate SkSurface. isTextureValid:%d dataspace:%d",
136 mBackendTexture.isValid(), dataspace);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800137 return mSurface;
138}
139
140} // namespace skia
141} // namespace renderengine
Alec Mouric6709cc2021-04-22 17:59:00 -0700142} // namespace android