blob: 8aeef9f4bce2b234dc15c2ad2b0720e7f73ce16a [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
Nolan Scobieca050282024-03-15 13:27:06 -040023#include <include/core/SkImage.h>
24#include <include/core/SkSurface.h>
25
26#include "compat/SkiaBackendTexture.h"
27
28#include <log/log_main.h>
29#include <utils/Trace.h>
Alec Mouric7f6c8b2020-11-09 18:35:20 -080030
31namespace android {
32namespace renderengine {
33namespace skia {
34
Nolan Scobieca050282024-03-15 13:27:06 -040035AutoBackendTexture::AutoBackendTexture(std::unique_ptr<SkiaBackendTexture> backendTexture,
36 CleanupManager& cleanupMgr)
37 : mCleanupMgr(cleanupMgr), mBackendTexture(std::move(backendTexture)) {}
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040038
Alec Mouric7f6c8b2020-11-09 18:35:20 -080039void AutoBackendTexture::unref(bool releaseLocalResources) {
40 if (releaseLocalResources) {
41 mSurface = nullptr;
42 mImage = nullptr;
43 }
44
45 mUsageCount--;
46 if (mUsageCount <= 0) {
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040047 mCleanupMgr.add(this);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080048 }
49}
50
51// releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use.
52// "releaseContext" contains an "AutoBackendTexture*".
53void AutoBackendTexture::releaseSurfaceProc(SkSurface::ReleaseContext releaseContext) {
54 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
55 textureRelease->unref(false);
56}
57
58// releaseImageProc is invoked by SkImage, when the texture is no longer in use.
59// "releaseContext" contains an "AutoBackendTexture*".
Kevin Lubick95afb8a2023-05-11 14:57:41 +000060void AutoBackendTexture::releaseImageProc(SkImages::ReleaseContext releaseContext) {
Alec Mouric7f6c8b2020-11-09 18:35:20 -080061 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
62 textureRelease->unref(false);
63}
64
Nolan Scobie17e25512024-03-13 18:02:48 -040065sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType) {
Alec Mouric7f6c8b2020-11-09 18:35:20 -080066 ATRACE_CALL();
67
Nolan Scobieca050282024-03-15 13:27:06 -040068 sk_sp<SkImage> image = mBackendTexture->makeImage(alphaType, dataspace, releaseImageProc, this);
69 // The following ref will be counteracted by releaseProc, when SkImage is discarded.
70 ref();
Alec Mouric7f6c8b2020-11-09 18:35:20 -080071
72 mImage = image;
73 mDataspace = dataspace;
Alec Mouric7f6c8b2020-11-09 18:35:20 -080074 return mImage;
75}
76
Nolan Scobie17e25512024-03-13 18:02:48 -040077sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace) {
Alec Mouric7f6c8b2020-11-09 18:35:20 -080078 ATRACE_CALL();
Nolan Scobieca050282024-03-15 13:27:06 -040079 LOG_ALWAYS_FATAL_IF(!mBackendTexture->isOutputBuffer(),
80 "You can't generate an SkSurface for a read-only texture");
Alec Mouric7f6c8b2020-11-09 18:35:20 -080081 if (!mSurface.get() || mDataspace != dataspace) {
82 sk_sp<SkSurface> surface =
Nolan Scobieca050282024-03-15 13:27:06 -040083 mBackendTexture->makeSurface(dataspace, releaseSurfaceProc, this);
84 // The following ref will be counteracted by releaseProc, when SkSurface is discarded.
85 ref();
86
Alec Mouric7f6c8b2020-11-09 18:35:20 -080087 mSurface = surface;
88 }
89
90 mDataspace = dataspace;
Alec Mouric7f6c8b2020-11-09 18:35:20 -080091 return mSurface;
92}
93
94} // namespace skia
95} // namespace renderengine
Alec Mouric6709cc2021-04-22 17:59:00 -070096} // namespace android