Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
Nolan Scobie | 2163e41 | 2022-10-24 19:57:43 -0400 | [diff] [blame] | 28 | // Friend TestUtils serves as a proxy for any test cases that require access to private members. |
| 29 | class TestUtils; |
| 30 | |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 31 | /** |
| 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 | */ |
| 35 | class AutoBackendTextureRelease final { |
| 36 | public: |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 37 | AutoBackendTextureRelease(GrDirectContext* context, |
| 38 | AHardwareBuffer* buffer); |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 39 | |
| 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 Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 49 | void makeImage(AHardwareBuffer* buffer, |
| 50 | android_dataspace dataspace, |
| 51 | GrDirectContext* context); |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 52 | |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 53 | void newBufferContent(GrDirectContext* context); |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 54 | |
Greg Daniel | 27e1fa2 | 2021-05-26 09:24:15 -0400 | [diff] [blame] | 55 | void releaseQueueOwnership(GrDirectContext* context); |
| 56 | |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 57 | private: |
| 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 Scobie | 2163e41 | 2022-10-24 19:57:43 -0400 | [diff] [blame] | 72 | |
| 73 | // Friend TestUtils serves as a proxy for any test cases that require access to private members. |
| 74 | friend class TestUtils; |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | } /* namespace uirenderer */ |
| 78 | } /* namespace android */ |