1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright 2020 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #include "AutoBackendTexture.h"
18*38e8c45fSAndroid Build Coastguard Worker
19*38e8c45fSAndroid Build Coastguard Worker #undef LOG_TAG
20*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "RenderEngine"
21*38e8c45fSAndroid Build Coastguard Worker #define ATRACE_TAG ATRACE_TAG_GRAPHICS
22*38e8c45fSAndroid Build Coastguard Worker
23*38e8c45fSAndroid Build Coastguard Worker #include <include/core/SkImage.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <include/core/SkSurface.h>
25*38e8c45fSAndroid Build Coastguard Worker
26*38e8c45fSAndroid Build Coastguard Worker #include "compat/SkiaBackendTexture.h"
27*38e8c45fSAndroid Build Coastguard Worker
28*38e8c45fSAndroid Build Coastguard Worker #include <common/trace.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <log/log_main.h>
30*38e8c45fSAndroid Build Coastguard Worker
31*38e8c45fSAndroid Build Coastguard Worker namespace android {
32*38e8c45fSAndroid Build Coastguard Worker namespace renderengine {
33*38e8c45fSAndroid Build Coastguard Worker namespace skia {
34*38e8c45fSAndroid Build Coastguard Worker
AutoBackendTexture(std::unique_ptr<SkiaBackendTexture> backendTexture,CleanupManager & cleanupMgr)35*38e8c45fSAndroid Build Coastguard Worker AutoBackendTexture::AutoBackendTexture(std::unique_ptr<SkiaBackendTexture> backendTexture,
36*38e8c45fSAndroid Build Coastguard Worker CleanupManager& cleanupMgr)
37*38e8c45fSAndroid Build Coastguard Worker : mCleanupMgr(cleanupMgr), mBackendTexture(std::move(backendTexture)) {}
38*38e8c45fSAndroid Build Coastguard Worker
unref(bool releaseLocalResources)39*38e8c45fSAndroid Build Coastguard Worker void AutoBackendTexture::unref(bool releaseLocalResources) {
40*38e8c45fSAndroid Build Coastguard Worker if (releaseLocalResources) {
41*38e8c45fSAndroid Build Coastguard Worker mSurface = nullptr;
42*38e8c45fSAndroid Build Coastguard Worker mImage = nullptr;
43*38e8c45fSAndroid Build Coastguard Worker }
44*38e8c45fSAndroid Build Coastguard Worker
45*38e8c45fSAndroid Build Coastguard Worker mUsageCount--;
46*38e8c45fSAndroid Build Coastguard Worker if (mUsageCount <= 0) {
47*38e8c45fSAndroid Build Coastguard Worker mCleanupMgr.add(this);
48*38e8c45fSAndroid Build Coastguard Worker }
49*38e8c45fSAndroid Build Coastguard Worker }
50*38e8c45fSAndroid Build Coastguard Worker
51*38e8c45fSAndroid Build Coastguard Worker // releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use.
52*38e8c45fSAndroid Build Coastguard Worker // "releaseContext" contains an "AutoBackendTexture*".
releaseSurfaceProc(SkSurface::ReleaseContext releaseContext)53*38e8c45fSAndroid Build Coastguard Worker void AutoBackendTexture::releaseSurfaceProc(SkSurface::ReleaseContext releaseContext) {
54*38e8c45fSAndroid Build Coastguard Worker AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
55*38e8c45fSAndroid Build Coastguard Worker textureRelease->unref(false);
56*38e8c45fSAndroid Build Coastguard Worker }
57*38e8c45fSAndroid Build Coastguard Worker
58*38e8c45fSAndroid Build Coastguard Worker // releaseImageProc is invoked by SkImage, when the texture is no longer in use.
59*38e8c45fSAndroid Build Coastguard Worker // "releaseContext" contains an "AutoBackendTexture*".
releaseImageProc(SkImages::ReleaseContext releaseContext)60*38e8c45fSAndroid Build Coastguard Worker void AutoBackendTexture::releaseImageProc(SkImages::ReleaseContext releaseContext) {
61*38e8c45fSAndroid Build Coastguard Worker AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
62*38e8c45fSAndroid Build Coastguard Worker textureRelease->unref(false);
63*38e8c45fSAndroid Build Coastguard Worker }
64*38e8c45fSAndroid Build Coastguard Worker
makeImage(ui::Dataspace dataspace,SkAlphaType alphaType)65*38e8c45fSAndroid Build Coastguard Worker sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType) {
66*38e8c45fSAndroid Build Coastguard Worker SFTRACE_CALL();
67*38e8c45fSAndroid Build Coastguard Worker
68*38e8c45fSAndroid Build Coastguard Worker sk_sp<SkImage> image = mBackendTexture->makeImage(alphaType, dataspace, releaseImageProc, this);
69*38e8c45fSAndroid Build Coastguard Worker // The following ref will be counteracted by releaseProc, when SkImage is discarded.
70*38e8c45fSAndroid Build Coastguard Worker ref();
71*38e8c45fSAndroid Build Coastguard Worker
72*38e8c45fSAndroid Build Coastguard Worker mImage = image;
73*38e8c45fSAndroid Build Coastguard Worker mDataspace = dataspace;
74*38e8c45fSAndroid Build Coastguard Worker return mImage;
75*38e8c45fSAndroid Build Coastguard Worker }
76*38e8c45fSAndroid Build Coastguard Worker
getOrCreateSurface(ui::Dataspace dataspace)77*38e8c45fSAndroid Build Coastguard Worker sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace) {
78*38e8c45fSAndroid Build Coastguard Worker SFTRACE_CALL();
79*38e8c45fSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(!mBackendTexture->isOutputBuffer(),
80*38e8c45fSAndroid Build Coastguard Worker "You can't generate an SkSurface for a read-only texture");
81*38e8c45fSAndroid Build Coastguard Worker if (!mSurface.get() || mDataspace != dataspace) {
82*38e8c45fSAndroid Build Coastguard Worker sk_sp<SkSurface> surface =
83*38e8c45fSAndroid Build Coastguard Worker mBackendTexture->makeSurface(dataspace, releaseSurfaceProc, this);
84*38e8c45fSAndroid Build Coastguard Worker // The following ref will be counteracted by releaseProc, when SkSurface is discarded.
85*38e8c45fSAndroid Build Coastguard Worker ref();
86*38e8c45fSAndroid Build Coastguard Worker
87*38e8c45fSAndroid Build Coastguard Worker mSurface = surface;
88*38e8c45fSAndroid Build Coastguard Worker }
89*38e8c45fSAndroid Build Coastguard Worker
90*38e8c45fSAndroid Build Coastguard Worker mDataspace = dataspace;
91*38e8c45fSAndroid Build Coastguard Worker return mSurface;
92*38e8c45fSAndroid Build Coastguard Worker }
93*38e8c45fSAndroid Build Coastguard Worker
94*38e8c45fSAndroid Build Coastguard Worker } // namespace skia
95*38e8c45fSAndroid Build Coastguard Worker } // namespace renderengine
96*38e8c45fSAndroid Build Coastguard Worker } // namespace android
97