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 | #include "AutoBackendTextureRelease.h" |
| 18 | |
Kevin Lubick | dd8b2ea | 2023-03-17 19:55:58 +0000 | [diff] [blame] | 19 | #include <SkImage.h> |
| 20 | #include <include/gpu/ganesh/SkImageGanesh.h> |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 21 | #include "renderthread/RenderThread.h" |
| 22 | #include "utils/Color.h" |
| 23 | #include "utils/PaintUtils.h" |
| 24 | |
| 25 | using namespace android::uirenderer::renderthread; |
| 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 30 | AutoBackendTextureRelease::AutoBackendTextureRelease(GrDirectContext* context, |
| 31 | AHardwareBuffer* buffer) { |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 32 | AHardwareBuffer_Desc desc; |
| 33 | AHardwareBuffer_describe(buffer, &desc); |
| 34 | bool createProtectedImage = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT); |
| 35 | GrBackendFormat backendFormat = |
| 36 | GrAHardwareBufferUtils::GetBackendFormat(context, buffer, desc.format, false); |
Nolan Scobie | 2163e41 | 2022-10-24 19:57:43 -0400 | [diff] [blame] | 37 | LOG_ALWAYS_FATAL_IF(!backendFormat.isValid(), |
| 38 | __FILE__ " Invalid GrBackendFormat. GrBackendApi==%" PRIu32 |
| 39 | ", AHardwareBuffer_Format==%" PRIu32 ".", |
| 40 | static_cast<int>(context->backend()), desc.format); |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 41 | mBackendTexture = GrAHardwareBufferUtils::MakeBackendTexture( |
| 42 | context, buffer, desc.width, desc.height, &mDeleteProc, &mUpdateProc, &mImageCtx, |
| 43 | createProtectedImage, backendFormat, false); |
Nolan Scobie | 2163e41 | 2022-10-24 19:57:43 -0400 | [diff] [blame] | 44 | LOG_ALWAYS_FATAL_IF(!mBackendTexture.isValid(), |
| 45 | __FILE__ " Invalid GrBackendTexture. Width==%" PRIu32 ", height==%" PRIu32 |
| 46 | ", protected==%d", |
| 47 | desc.width, desc.height, createProtectedImage); |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void AutoBackendTextureRelease::unref(bool releaseImage) { |
| 51 | if (!RenderThread::isCurrent()) { |
| 52 | // EGLImage needs to be destroyed on RenderThread to prevent memory leak. |
| 53 | // ~SkImage dtor for both pipelines needs to be invoked on RenderThread, because it is not |
| 54 | // thread safe. |
| 55 | RenderThread::getInstance().queue().post([this, releaseImage]() { unref(releaseImage); }); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | if (releaseImage) { |
| 60 | mImage.reset(); |
| 61 | } |
| 62 | |
| 63 | mUsageCount--; |
| 64 | if (mUsageCount <= 0) { |
| 65 | if (mBackendTexture.isValid()) { |
| 66 | mDeleteProc(mImageCtx); |
| 67 | mBackendTexture = {}; |
| 68 | } |
| 69 | delete this; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // releaseProc is invoked by SkImage, when texture is no longer in use. |
| 74 | // "releaseContext" contains an "AutoBackendTextureRelease*". |
| 75 | static void releaseProc(SkImage::ReleaseContext releaseContext) { |
| 76 | AutoBackendTextureRelease* textureRelease = |
| 77 | reinterpret_cast<AutoBackendTextureRelease*>(releaseContext); |
| 78 | textureRelease->unref(false); |
| 79 | } |
| 80 | |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 81 | void AutoBackendTextureRelease::makeImage(AHardwareBuffer* buffer, |
| 82 | android_dataspace dataspace, |
| 83 | GrDirectContext* context) { |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 84 | AHardwareBuffer_Desc desc; |
| 85 | AHardwareBuffer_describe(buffer, &desc); |
| 86 | SkColorType colorType = GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(desc.format); |
Nolan Scobie | 2163e41 | 2022-10-24 19:57:43 -0400 | [diff] [blame] | 87 | // The following ref will be counteracted by Skia calling releaseProc, either during |
Kevin Lubick | dd8b2ea | 2023-03-17 19:55:58 +0000 | [diff] [blame] | 88 | // BorrowTextureFrom if there is a failure, or later when SkImage is discarded. It must |
| 89 | // be called before BorrowTextureFrom, otherwise Skia may remove HWUI's ref on failure. |
Nolan Scobie | 2163e41 | 2022-10-24 19:57:43 -0400 | [diff] [blame] | 90 | ref(); |
Kevin Lubick | dd8b2ea | 2023-03-17 19:55:58 +0000 | [diff] [blame] | 91 | mImage = SkImages::BorrowTextureFrom( |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 92 | context, mBackendTexture, kTopLeft_GrSurfaceOrigin, colorType, kPremul_SkAlphaType, |
| 93 | uirenderer::DataSpaceToColorSpace(dataspace), releaseProc, this); |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 96 | void AutoBackendTextureRelease::newBufferContent(GrDirectContext* context) { |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 97 | if (mBackendTexture.isValid()) { |
| 98 | mUpdateProc(mImageCtx, context); |
| 99 | } |
| 100 | } |
| 101 | |
Greg Daniel | 27e1fa2 | 2021-05-26 09:24:15 -0400 | [diff] [blame] | 102 | void AutoBackendTextureRelease::releaseQueueOwnership(GrDirectContext* context) { |
| 103 | if (!context) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | LOG_ALWAYS_FATAL_IF(Properties::getRenderPipelineType() != RenderPipelineType::SkiaVulkan); |
| 108 | if (mBackendTexture.isValid()) { |
| 109 | // Passing in VK_IMAGE_LAYOUT_UNDEFINED means we keep the old layout. |
| 110 | GrBackendSurfaceMutableState newState(VK_IMAGE_LAYOUT_UNDEFINED, |
| 111 | VK_QUEUE_FAMILY_FOREIGN_EXT); |
| 112 | |
| 113 | // The unref for this ref happens in the releaseProc passed into setBackendTextureState. The |
| 114 | // releaseProc callback will be made when the work to set the new state has finished on the |
| 115 | // gpu. |
| 116 | ref(); |
| 117 | // Note that we don't have an explicit call to set the backend texture back onto the |
| 118 | // graphics queue when we use the VkImage again. Internally, Skia will notice that the image |
| 119 | // is not on the graphics queue and will do the transition automatically. |
| 120 | context->setBackendTextureState(mBackendTexture, newState, nullptr, releaseProc, this); |
| 121 | } |
| 122 | } |
| 123 | |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 124 | } /* namespace uirenderer */ |
| 125 | } /* namespace android */ |