| 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 | 957f7b3 | 2021-03-19 15:42:19 -0400 | [diff] [blame] | 31 | AutoBackendTexture::AutoBackendTexture(GrDirectContext* context, AHardwareBuffer* buffer) { | 
| Alec Mouri | e1f8198 | 2021-01-11 10:24:27 -0800 | [diff] [blame] | 32 | ATRACE_CALL(); | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 33 | AHardwareBuffer_Desc desc; | 
|  | 34 | AHardwareBuffer_describe(buffer, &desc); | 
| Derek Sollenberger | 957f7b3 | 2021-03-19 15:42:19 -0400 | [diff] [blame] | 35 | const bool createProtectedImage = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT); | 
|  | 36 | const bool isRenderable = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER); | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 37 | GrBackendFormat backendFormat = | 
|  | 38 | GrAHardwareBufferUtils::GetBackendFormat(context, buffer, desc.format, false); | 
|  | 39 | mBackendTexture = | 
|  | 40 | GrAHardwareBufferUtils::MakeBackendTexture(context, buffer, desc.width, desc.height, | 
|  | 41 | &mDeleteProc, &mUpdateProc, &mImageCtx, | 
|  | 42 | createProtectedImage, backendFormat, | 
| Derek Sollenberger | 957f7b3 | 2021-03-19 15:42:19 -0400 | [diff] [blame] | 43 | isRenderable); | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 44 | mColorType = GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(desc.format); | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | void AutoBackendTexture::unref(bool releaseLocalResources) { | 
|  | 48 | if (releaseLocalResources) { | 
|  | 49 | mSurface = nullptr; | 
|  | 50 | mImage = nullptr; | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | mUsageCount--; | 
|  | 54 | if (mUsageCount <= 0) { | 
|  | 55 | if (mBackendTexture.isValid()) { | 
|  | 56 | mDeleteProc(mImageCtx); | 
|  | 57 | mBackendTexture = {}; | 
|  | 58 | } | 
|  | 59 | delete this; | 
|  | 60 | } | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | // releaseSurfaceProc is invoked by SkSurface, when the texture is no longer in use. | 
|  | 64 | // "releaseContext" contains an "AutoBackendTexture*". | 
|  | 65 | void AutoBackendTexture::releaseSurfaceProc(SkSurface::ReleaseContext releaseContext) { | 
|  | 66 | AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext); | 
|  | 67 | textureRelease->unref(false); | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | // releaseImageProc is invoked by SkImage, when the texture is no longer in use. | 
|  | 71 | // "releaseContext" contains an "AutoBackendTexture*". | 
|  | 72 | void AutoBackendTexture::releaseImageProc(SkImage::ReleaseContext releaseContext) { | 
|  | 73 | AutoBackendTexture* textureRelease = reinterpret_cast<AutoBackendTexture*>(releaseContext); | 
|  | 74 | textureRelease->unref(false); | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType, | 
|  | 78 | GrDirectContext* context) { | 
|  | 79 | ATRACE_CALL(); | 
|  | 80 |  | 
|  | 81 | if (mBackendTexture.isValid()) { | 
|  | 82 | mUpdateProc(mImageCtx, context); | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | sk_sp<SkImage> image = | 
|  | 86 | SkImage::MakeFromTexture(context, mBackendTexture, kTopLeft_GrSurfaceOrigin, mColorType, | 
|  | 87 | alphaType, toSkColorSpace(dataspace), releaseImageProc, this); | 
|  | 88 | if (image.get()) { | 
|  | 89 | // The following ref will be counteracted by releaseProc, when SkImage is discarded. | 
|  | 90 | ref(); | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | mImage = image; | 
|  | 94 | mDataspace = dataspace; | 
|  | 95 | LOG_ALWAYS_FATAL_IF(mImage == nullptr, "Unable to generate SkImage from buffer"); | 
|  | 96 | return mImage; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace, | 
|  | 100 | GrDirectContext* context) { | 
|  | 101 | ATRACE_CALL(); | 
|  | 102 | if (!mSurface.get() || mDataspace != dataspace) { | 
|  | 103 | sk_sp<SkSurface> surface = | 
|  | 104 | SkSurface::MakeFromBackendTexture(context, mBackendTexture, | 
|  | 105 | kTopLeft_GrSurfaceOrigin, 0, mColorType, | 
|  | 106 | toSkColorSpace(dataspace), nullptr, | 
|  | 107 | releaseSurfaceProc, this); | 
|  | 108 | if (surface.get()) { | 
|  | 109 | // The following ref will be counteracted by releaseProc, when SkSurface is discarded. | 
|  | 110 | ref(); | 
|  | 111 | } | 
|  | 112 | mSurface = surface; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | mDataspace = dataspace; | 
|  | 116 | LOG_ALWAYS_FATAL_IF(mSurface == nullptr, "Unable to generate SkSurface"); | 
|  | 117 | return mSurface; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | } // namespace skia | 
|  | 121 | } // namespace renderengine | 
|  | 122 | } // namespace android |