blob: f0eb2a8b6eab3dfcd87476c740eac92a5bbfe8ac [file] [log] [blame]
Stan Ilievaaa9e832019-09-17 14:07:23 -04001/*
2 * Copyright 2019 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 <GrBackendSurface.h>
21#include <SkImage.h>
22#include <android/hardware_buffer.h>
23#include <system/graphics.h>
24
25namespace android {
26namespace uirenderer {
27
Nolan Scobie2163e412022-10-24 19:57:43 -040028// Friend TestUtils serves as a proxy for any test cases that require access to private members.
29class TestUtils;
30
Stan Ilievaaa9e832019-09-17 14:07:23 -040031/**
32 * AutoBackendTextureRelease manages EglImage/VkImage lifetime. It is a ref-counted object
33 * that keeps GPU resources alive until the last SkImage object using them is destroyed.
34 */
35class AutoBackendTextureRelease final {
36public:
Adlai Hollerf8c434e2020-07-27 11:42:45 -040037 AutoBackendTextureRelease(GrDirectContext* context,
38 AHardwareBuffer* buffer);
Stan Ilievaaa9e832019-09-17 14:07:23 -040039
40 const GrBackendTexture& getTexture() const { return mBackendTexture; }
41
42 // Only called on the RenderThread, so it need not be thread-safe.
43 void ref() { mUsageCount++; }
44
45 void unref(bool releaseImage);
46
47 inline sk_sp<SkImage> getImage() const { return mImage; }
48
Adlai Hollerf8c434e2020-07-27 11:42:45 -040049 void makeImage(AHardwareBuffer* buffer,
50 android_dataspace dataspace,
51 GrDirectContext* context);
Stan Ilievaaa9e832019-09-17 14:07:23 -040052
Adlai Hollerf8c434e2020-07-27 11:42:45 -040053 void newBufferContent(GrDirectContext* context);
Stan Ilievaaa9e832019-09-17 14:07:23 -040054
Greg Daniel27e1fa22021-05-26 09:24:15 -040055 void releaseQueueOwnership(GrDirectContext* context);
56
Stan Ilievaaa9e832019-09-17 14:07:23 -040057private:
58 // The only way to invoke dtor is with unref, when mUsageCount is 0.
59 ~AutoBackendTextureRelease() {}
60
61 GrBackendTexture mBackendTexture;
62 GrAHardwareBufferUtils::DeleteImageProc mDeleteProc;
63 GrAHardwareBufferUtils::UpdateImageProc mUpdateProc;
64 GrAHardwareBufferUtils::TexImageCtx mImageCtx;
65
66 // Starting with refcount 1, because the first ref is held by SurfaceTexture. Additional refs
67 // are held by SkImages.
68 int mUsageCount = 1;
69
70 // mImage is the SkImage created from mBackendTexture.
71 sk_sp<SkImage> mImage;
Nolan Scobie2163e412022-10-24 19:57:43 -040072
73 // Friend TestUtils serves as a proxy for any test cases that require access to private members.
74 friend class TestUtils;
Stan Ilievaaa9e832019-09-17 14:07:23 -040075};
76
77} /* namespace uirenderer */
78} /* namespace android */