Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 "AutoBackendTexture.h" |
| 18 | |
| 19 | #undef LOG_TAG |
| 20 | #define LOG_TAG "RenderEngine" |
| 21 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 22 | |
Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 23 | #include "ColorSpaces.h" |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 24 | #include "log/log_main.h" |
| 25 | #include "utils/Trace.h" |
| 26 | |
| 27 | namespace android { |
| 28 | namespace renderengine { |
| 29 | namespace skia { |
| 30 | |
Derek Sollenberger | 3425788 | 2021-04-06 18:32:34 +0000 | [diff] [blame] | 31 | AutoBackendTexture::AutoBackendTexture(GrDirectContext* context, AHardwareBuffer* buffer, |
Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 32 | bool isOutputBuffer) |
| 33 | : mIsOutputBuffer(isOutputBuffer) { |
Alec Mouri | e1f8198 | 2021-01-11 10:24:27 -0800 | [diff] [blame] | 34 | ATRACE_CALL(); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 35 | AHardwareBuffer_Desc desc; |
| 36 | AHardwareBuffer_describe(buffer, &desc); |
Derek Sollenberger | 3425788 | 2021-04-06 18:32:34 +0000 | [diff] [blame] | 37 | bool createProtectedImage = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 38 | GrBackendFormat backendFormat = |
| 39 | GrAHardwareBufferUtils::GetBackendFormat(context, buffer, desc.format, false); |
| 40 | mBackendTexture = |
| 41 | GrAHardwareBufferUtils::MakeBackendTexture(context, buffer, desc.width, desc.height, |
| 42 | &mDeleteProc, &mUpdateProc, &mImageCtx, |
| 43 | createProtectedImage, backendFormat, |
Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 44 | isOutputBuffer); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 45 | mColorType = GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(desc.format); |
Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 46 | ALOGE_IF(!mBackendTexture.isValid(), |
| 47 | "Failed to create a valid texture. [%p]:[%d,%d] isProtected:%d isWriteable:%d " |
| 48 | "format:%d", |
Alec Mouri | c6709cc | 2021-04-22 17:59:00 -0700 | [diff] [blame] | 49 | this, desc.width, desc.height, createProtectedImage, isOutputBuffer, desc.format); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void AutoBackendTexture::unref(bool releaseLocalResources) { |
| 53 | if (releaseLocalResources) { |
| 54 | mSurface = nullptr; |
| 55 | mImage = nullptr; |
| 56 | } |
| 57 | |
| 58 | mUsageCount--; |
| 59 | if (mUsageCount <= 0) { |
| 60 | if (mBackendTexture.isValid()) { |
| 61 | mDeleteProc(mImageCtx); |
| 62 | mBackendTexture = {}; |
| 63 | } |
| 64 | delete this; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use. |
| 69 | // "releaseContext" contains an "AutoBackendTexture*". |
| 70 | void AutoBackendTexture::releaseSurfaceProc(SkSurface::ReleaseContext releaseContext) { |
| 71 | AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext); |
| 72 | textureRelease->unref(false); |
| 73 | } |
| 74 | |
| 75 | // releaseImageProc is invoked by SkImage, when the texture is no longer in use. |
| 76 | // "releaseContext" contains an "AutoBackendTexture*". |
| 77 | void AutoBackendTexture::releaseImageProc(SkImage::ReleaseContext releaseContext) { |
| 78 | AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext); |
| 79 | textureRelease->unref(false); |
| 80 | } |
| 81 | |
| 82 | sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType, |
| 83 | GrDirectContext* context) { |
| 84 | ATRACE_CALL(); |
| 85 | |
| 86 | if (mBackendTexture.isValid()) { |
| 87 | mUpdateProc(mImageCtx, context); |
| 88 | } |
| 89 | |
Leon Scroggins III | c4e0cbd | 2021-05-25 10:25:20 -0400 | [diff] [blame^] | 90 | auto colorType = mColorType; |
| 91 | if (alphaType == kOpaque_SkAlphaType) { |
| 92 | if (colorType == kRGBA_8888_SkColorType) { |
| 93 | colorType = kRGB_888x_SkColorType; |
| 94 | } |
| 95 | } |
| 96 | |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 97 | sk_sp<SkImage> image = |
Leon Scroggins III | c4e0cbd | 2021-05-25 10:25:20 -0400 | [diff] [blame^] | 98 | SkImage::MakeFromTexture(context, mBackendTexture, kTopLeft_GrSurfaceOrigin, colorType, |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 99 | alphaType, toSkColorSpace(dataspace), releaseImageProc, this); |
| 100 | if (image.get()) { |
| 101 | // The following ref will be counteracted by releaseProc, when SkImage is discarded. |
| 102 | ref(); |
| 103 | } |
| 104 | |
| 105 | mImage = image; |
| 106 | mDataspace = dataspace; |
Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 107 | LOG_ALWAYS_FATAL_IF(mImage == nullptr, |
| 108 | "Unable to generate SkImage. isTextureValid:%d dataspace:%d", |
| 109 | mBackendTexture.isValid(), dataspace); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 110 | return mImage; |
| 111 | } |
| 112 | |
| 113 | sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace, |
| 114 | GrDirectContext* context) { |
| 115 | ATRACE_CALL(); |
Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 116 | LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture"); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 117 | if (!mSurface.get() || mDataspace != dataspace) { |
| 118 | sk_sp<SkSurface> surface = |
| 119 | SkSurface::MakeFromBackendTexture(context, mBackendTexture, |
| 120 | kTopLeft_GrSurfaceOrigin, 0, mColorType, |
| 121 | toSkColorSpace(dataspace), nullptr, |
| 122 | releaseSurfaceProc, this); |
| 123 | if (surface.get()) { |
| 124 | // The following ref will be counteracted by releaseProc, when SkSurface is discarded. |
| 125 | ref(); |
| 126 | } |
| 127 | mSurface = surface; |
| 128 | } |
| 129 | |
| 130 | mDataspace = dataspace; |
Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 131 | LOG_ALWAYS_FATAL_IF(mSurface == nullptr, |
| 132 | "Unable to generate SkSurface. isTextureValid:%d dataspace:%d", |
| 133 | mBackendTexture.isValid(), dataspace); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 134 | return mSurface; |
| 135 | } |
| 136 | |
| 137 | } // namespace skia |
| 138 | } // namespace renderengine |
Alec Mouri | c6709cc | 2021-04-22 17:59:00 -0700 | [diff] [blame] | 139 | } // namespace android |