blob: c9bb767a31857de4a518fbcaf3f67bc234c0eb18 [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
28/**
29 * AutoBackendTextureRelease manages EglImage/VkImage lifetime. It is a ref-counted object
30 * that keeps GPU resources alive until the last SkImage object using them is destroyed.
31 */
32class AutoBackendTextureRelease final {
33public:
Adlai Hollerf8c434e2020-07-27 11:42:45 -040034 AutoBackendTextureRelease(GrDirectContext* context,
35 AHardwareBuffer* buffer);
Stan Ilievaaa9e832019-09-17 14:07:23 -040036
37 const GrBackendTexture& getTexture() const { return mBackendTexture; }
38
39 // Only called on the RenderThread, so it need not be thread-safe.
40 void ref() { mUsageCount++; }
41
42 void unref(bool releaseImage);
43
44 inline sk_sp<SkImage> getImage() const { return mImage; }
45
Adlai Hollerf8c434e2020-07-27 11:42:45 -040046 void makeImage(AHardwareBuffer* buffer,
47 android_dataspace dataspace,
48 GrDirectContext* context);
Stan Ilievaaa9e832019-09-17 14:07:23 -040049
Adlai Hollerf8c434e2020-07-27 11:42:45 -040050 void newBufferContent(GrDirectContext* context);
Stan Ilievaaa9e832019-09-17 14:07:23 -040051
Greg Daniel27e1fa22021-05-26 09:24:15 -040052 void releaseQueueOwnership(GrDirectContext* context);
53
Stan Ilievaaa9e832019-09-17 14:07:23 -040054private:
55 // The only way to invoke dtor is with unref, when mUsageCount is 0.
56 ~AutoBackendTextureRelease() {}
57
58 GrBackendTexture mBackendTexture;
59 GrAHardwareBufferUtils::DeleteImageProc mDeleteProc;
60 GrAHardwareBufferUtils::UpdateImageProc mUpdateProc;
61 GrAHardwareBufferUtils::TexImageCtx mImageCtx;
62
63 // Starting with refcount 1, because the first ref is held by SurfaceTexture. Additional refs
64 // are held by SkImages.
65 int mUsageCount = 1;
66
67 // mImage is the SkImage created from mBackendTexture.
68 sk_sp<SkImage> mImage;
69};
70
71} /* namespace uirenderer */
72} /* namespace android */