1 /*
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
23 #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>
30
31 namespace android {
32 namespace renderengine {
33 namespace skia {
34
AutoBackendTexture(std::unique_ptr<SkiaBackendTexture> backendTexture,CleanupManager & cleanupMgr)35 AutoBackendTexture::AutoBackendTexture(std::unique_ptr<SkiaBackendTexture> backendTexture,
36 CleanupManager& cleanupMgr)
37 : mCleanupMgr(cleanupMgr), mBackendTexture(std::move(backendTexture)) {}
38
unref(bool releaseLocalResources)39 void AutoBackendTexture::unref(bool releaseLocalResources) {
40 if (releaseLocalResources) {
41 mSurface = nullptr;
42 mImage = nullptr;
43 }
44
45 mUsageCount--;
46 if (mUsageCount <= 0) {
47 mCleanupMgr.add(this);
48 }
49 }
50
51 // releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use.
52 // "releaseContext" contains an "AutoBackendTexture*".
releaseSurfaceProc(SkSurface::ReleaseContext releaseContext)53 void 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*".
releaseImageProc(SkImages::ReleaseContext releaseContext)60 void AutoBackendTexture::releaseImageProc(SkImages::ReleaseContext releaseContext) {
61 AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext);
62 textureRelease->unref(false);
63 }
64
makeImage(ui::Dataspace dataspace,SkAlphaType alphaType)65 sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType) {
66 ATRACE_CALL();
67
68 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();
71
72 mImage = image;
73 mDataspace = dataspace;
74 return mImage;
75 }
76
getOrCreateSurface(ui::Dataspace dataspace)77 sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace) {
78 ATRACE_CALL();
79 LOG_ALWAYS_FATAL_IF(!mBackendTexture->isOutputBuffer(),
80 "You can't generate an SkSurface for a read-only texture");
81 if (!mSurface.get() || mDataspace != dataspace) {
82 sk_sp<SkSurface> surface =
83 mBackendTexture->makeSurface(dataspace, releaseSurfaceProc, this);
84 // The following ref will be counteracted by releaseProc, when SkSurface is discarded.
85 ref();
86
87 mSurface = surface;
88 }
89
90 mDataspace = dataspace;
91 return mSurface;
92 }
93
94 } // namespace skia
95 } // namespace renderengine
96 } // namespace android
97