blob: a9e8430d66680b1c13bef5fa280c25cd91a10426 [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:
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -040044 LocalRef(GrDirectContext* context, AHardwareBuffer* buffer, bool isOutputBuffer) {
45 mTexture = new AutoBackendTexture(context, buffer, isOutputBuffer);
46 mTexture->ref();
Alec Mouric7f6c8b2020-11-09 18:35:20 -080047 }
48
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -040049 ~LocalRef() {
50 if (mTexture != nullptr) {
51 mTexture->unref(true);
52 }
53 }
54
55 // Makes a new SkImage from the texture content.
56 // As SkImages are immutable but buffer content is not, we create
57 // a new SkImage every time.
58 sk_sp<SkImage> makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
59 GrDirectContext* context) {
60 return mTexture->makeImage(dataspace, alphaType, context);
61 }
62
63 // Makes a new SkSurface from the texture content, if needed.
64 sk_sp<SkSurface> getOrCreateSurface(ui::Dataspace dataspace, GrDirectContext* context) {
65 return mTexture->getOrCreateSurface(dataspace, context);
66 }
Alec Mouria90a5702021-04-16 16:36:21 +000067
Leon Scroggins IIIc4e0cbd2021-05-25 10:25:20 -040068 SkColorType colorType() const { return mTexture->mColorType; }
69
Alec Mouria90a5702021-04-16 16:36:21 +000070 DISALLOW_COPY_AND_ASSIGN(LocalRef);
71
72 private:
Alec Mouric7f6c8b2020-11-09 18:35:20 -080073 AutoBackendTexture* mTexture = nullptr;
74 };
75
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -040076private:
Alec Mouric7f6c8b2020-11-09 18:35:20 -080077 // Creates a GrBackendTexture whose contents come from the provided buffer.
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -040078 AutoBackendTexture(GrDirectContext* context, AHardwareBuffer* buffer, bool isOutputBuffer);
79
80 // The only way to invoke dtor is with unref, when mUsageCount is 0.
81 ~AutoBackendTexture() {}
Alec Mouric7f6c8b2020-11-09 18:35:20 -080082
83 void ref() { mUsageCount++; }
84
85 // releaseLocalResources is true if the underlying SkImage and SkSurface
86 // should be deleted from local tracking.
87 void unref(bool releaseLocalResources);
88
89 // Makes a new SkImage from the texture content.
90 // As SkImages are immutable but buffer content is not, we create
91 // a new SkImage every time.
92 sk_sp<SkImage> makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
93 GrDirectContext* context);
94
95 // Makes a new SkSurface from the texture content, if needed.
96 sk_sp<SkSurface> getOrCreateSurface(ui::Dataspace dataspace, GrDirectContext* context);
97
Alec Mouric7f6c8b2020-11-09 18:35:20 -080098 GrBackendTexture mBackendTexture;
99 GrAHardwareBufferUtils::DeleteImageProc mDeleteProc;
100 GrAHardwareBufferUtils::UpdateImageProc mUpdateProc;
101 GrAHardwareBufferUtils::TexImageCtx mImageCtx;
102
103 static void releaseSurfaceProc(SkSurface::ReleaseContext releaseContext);
104 static void releaseImageProc(SkImage::ReleaseContext releaseContext);
105
106 int mUsageCount = 0;
107
Derek Sollenbergerd8fdae32021-04-09 13:52:59 -0400108 const bool mIsOutputBuffer;
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800109 sk_sp<SkImage> mImage = nullptr;
110 sk_sp<SkSurface> mSurface = nullptr;
111 ui::Dataspace mDataspace = ui::Dataspace::UNKNOWN;
112 SkColorType mColorType = kUnknown_SkColorType;
113};
114
115} // namespace skia
116} // namespace renderengine
117} // namespace android