blob: 2d61cf854b7e442218362a9cbc89061831bb406a [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#pragma once
18
19#include <GrAHardwareBufferUtils.h>
20#include <GrDirectContext.h>
21#include <SkImage.h>
22#include <SkSurface.h>
23#include <sys/types.h>
Alec Mouria90a5702021-04-16 16:36:21 +000024#include <ui/GraphicTypes.h>
Alec Mouric7f6c8b2020-11-09 18:35:20 -080025
26#include "android-base/macros.h"
Alec Mouric7f6c8b2020-11-09 18:35:20 -080027
28namespace android {
29namespace renderengine {
30namespace skia {
31
32/**
Ana Krulecdfec8f52021-01-13 12:51:47 -080033 * AutoBackendTexture manages GPU image lifetime. It is a ref-counted object
Alec Mouric7f6c8b2020-11-09 18:35:20 -080034 * that keeps GPU resources alive until the last SkImage or SkSurface object using them is
35 * destroyed.
36 */
37class AutoBackendTexture {
38public:
39 // Local reference that supports RAII-style management of an AutoBackendTexture
40 // AutoBackendTexture by itself can't be managed in a similar fashion because
41 // of shared ownership with Skia objects, so we wrap it here instead.
42 class LocalRef {
43 public:
Alec Mouria90a5702021-04-16 16:36:21 +000044 LocalRef(AutoBackendTexture* texture) { setTexture(texture); }
Alec Mouric7f6c8b2020-11-09 18:35:20 -080045
46 ~LocalRef() {
47 // Destroying the texture is the same as setting it to null
48 setTexture(nullptr);
49 }
50
Alec Mouria90a5702021-04-16 16:36:21 +000051 AutoBackendTexture* getTexture() const { return mTexture; }
52
53 DISALLOW_COPY_AND_ASSIGN(LocalRef);
54
55 private:
Alec Mouric7f6c8b2020-11-09 18:35:20 -080056 // Sets the texture to locally ref-track.
57 void setTexture(AutoBackendTexture* texture) {
58 if (mTexture != nullptr) {
59 mTexture->unref(true);
60 }
61
62 mTexture = texture;
63 if (mTexture != nullptr) {
64 mTexture->ref();
65 }
66 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -080067 AutoBackendTexture* mTexture = nullptr;
68 };
69
70 // Creates a GrBackendTexture whose contents come from the provided buffer.
Derek Sollenberger34257882021-04-06 18:32:34 +000071 AutoBackendTexture(GrDirectContext* context, AHardwareBuffer* buffer, bool isRender);
Alec Mouric7f6c8b2020-11-09 18:35:20 -080072
73 void ref() { mUsageCount++; }
74
75 // releaseLocalResources is true if the underlying SkImage and SkSurface
76 // should be deleted from local tracking.
77 void unref(bool releaseLocalResources);
78
79 // Makes a new SkImage from the texture content.
80 // As SkImages are immutable but buffer content is not, we create
81 // a new SkImage every time.
82 sk_sp<SkImage> makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
83 GrDirectContext* context);
84
85 // Makes a new SkSurface from the texture content, if needed.
86 sk_sp<SkSurface> getOrCreateSurface(ui::Dataspace dataspace, GrDirectContext* context);
87
88private:
89 // The only way to invoke dtor is with unref, when mUsageCount is 0.
90 ~AutoBackendTexture() {}
91
92 GrBackendTexture mBackendTexture;
93 GrAHardwareBufferUtils::DeleteImageProc mDeleteProc;
94 GrAHardwareBufferUtils::UpdateImageProc mUpdateProc;
95 GrAHardwareBufferUtils::TexImageCtx mImageCtx;
96
97 static void releaseSurfaceProc(SkSurface::ReleaseContext releaseContext);
98 static void releaseImageProc(SkImage::ReleaseContext releaseContext);
99
100 int mUsageCount = 0;
101
102 sk_sp<SkImage> mImage = nullptr;
103 sk_sp<SkSurface> mSurface = nullptr;
104 ui::Dataspace mDataspace = ui::Dataspace::UNKNOWN;
105 SkColorType mColorType = kUnknown_SkColorType;
106};
107
108} // namespace skia
109} // namespace renderengine
110} // namespace android