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