blob: acdd63cb7921f9c7383442aa9a71a6ced58193b7 [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:
34 AutoBackendTextureRelease(GrContext* context, AHardwareBuffer* buffer);
35
36 const GrBackendTexture& getTexture() const { return mBackendTexture; }
37
38 // Only called on the RenderThread, so it need not be thread-safe.
39 void ref() { mUsageCount++; }
40
41 void unref(bool releaseImage);
42
43 inline sk_sp<SkImage> getImage() const { return mImage; }
44
45 void makeImage(AHardwareBuffer* buffer, android_dataspace dataspace, GrContext* context);
46
47 void newBufferContent(GrContext* context);
48
49private:
50 // The only way to invoke dtor is with unref, when mUsageCount is 0.
51 ~AutoBackendTextureRelease() {}
52
53 GrBackendTexture mBackendTexture;
54 GrAHardwareBufferUtils::DeleteImageProc mDeleteProc;
55 GrAHardwareBufferUtils::UpdateImageProc mUpdateProc;
56 GrAHardwareBufferUtils::TexImageCtx mImageCtx;
57
58 // Starting with refcount 1, because the first ref is held by SurfaceTexture. Additional refs
59 // are held by SkImages.
60 int mUsageCount = 1;
61
62 // mImage is the SkImage created from mBackendTexture.
63 sk_sp<SkImage> mImage;
64};
65
66} /* namespace uirenderer */
67} /* namespace android */