Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2024 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 "GaneshBackendTexture.h" |
| 18 | |
| 19 | #undef LOG_TAG |
| 20 | #define LOG_TAG "RenderEngine" |
| 21 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 22 | |
| 23 | #include <include/core/SkImage.h> |
Nolan Scobie | bc3f360 | 2024-08-30 13:51:37 -0400 | [diff] [blame] | 24 | #include <include/gpu/ganesh/GrDirectContext.h> |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 25 | #include <include/gpu/ganesh/SkImageGanesh.h> |
| 26 | #include <include/gpu/ganesh/SkSurfaceGanesh.h> |
| 27 | #include <include/gpu/ganesh/gl/GrGLBackendSurface.h> |
| 28 | #include <include/gpu/ganesh/vk/GrVkBackendSurface.h> |
Nolan Scobie | bc3f360 | 2024-08-30 13:51:37 -0400 | [diff] [blame] | 29 | #include <include/gpu/ganesh/vk/GrVkTypes.h> |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 30 | |
| 31 | #include "skia/ColorSpaces.h" |
| 32 | #include "skia/compat/SkiaBackendTexture.h" |
| 33 | |
| 34 | #include <android/hardware_buffer.h> |
Vishnu Nair | 40d8001 | 2024-07-13 23:25:06 +0000 | [diff] [blame] | 35 | #include <common/trace.h> |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 36 | #include <log/log_main.h> |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 37 | |
| 38 | namespace android::renderengine::skia { |
| 39 | |
| 40 | GaneshBackendTexture::GaneshBackendTexture(sk_sp<GrDirectContext> grContext, |
| 41 | AHardwareBuffer* buffer, bool isOutputBuffer) |
| 42 | : SkiaBackendTexture(buffer, isOutputBuffer), mGrContext(grContext) { |
Vishnu Nair | 40d8001 | 2024-07-13 23:25:06 +0000 | [diff] [blame] | 43 | SFTRACE_CALL(); |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 44 | AHardwareBuffer_Desc desc; |
| 45 | AHardwareBuffer_describe(buffer, &desc); |
| 46 | const bool createProtectedImage = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT); |
| 47 | |
| 48 | GrBackendFormat backendFormat; |
| 49 | const GrBackendApi graphicsApi = grContext->backend(); |
| 50 | if (graphicsApi == GrBackendApi::kOpenGL) { |
| 51 | backendFormat = |
| 52 | GrAHardwareBufferUtils::GetGLBackendFormat(grContext.get(), desc.format, false); |
| 53 | mBackendTexture = |
| 54 | GrAHardwareBufferUtils::MakeGLBackendTexture(grContext.get(), buffer, desc.width, |
| 55 | desc.height, &mDeleteProc, |
| 56 | &mUpdateProc, &mImageCtx, |
| 57 | createProtectedImage, backendFormat, |
| 58 | isOutputBuffer); |
| 59 | } else if (graphicsApi == GrBackendApi::kVulkan) { |
| 60 | backendFormat = GrAHardwareBufferUtils::GetVulkanBackendFormat(grContext.get(), buffer, |
| 61 | desc.format, false); |
| 62 | mBackendTexture = |
| 63 | GrAHardwareBufferUtils::MakeVulkanBackendTexture(grContext.get(), buffer, |
| 64 | desc.width, desc.height, |
| 65 | &mDeleteProc, &mUpdateProc, |
| 66 | &mImageCtx, createProtectedImage, |
| 67 | backendFormat, isOutputBuffer); |
| 68 | } else { |
| 69 | LOG_ALWAYS_FATAL("Unexpected graphics API %u", static_cast<unsigned>(graphicsApi)); |
| 70 | } |
| 71 | |
| 72 | if (!mBackendTexture.isValid() || !desc.width || !desc.height) { |
| 73 | LOG_ALWAYS_FATAL("Failed to create a valid texture. [%p]:[%d,%d] isProtected:%d " |
| 74 | "isWriteable:%d format:%d", |
| 75 | this, desc.width, desc.height, createProtectedImage, isOutputBuffer, |
| 76 | desc.format); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | GaneshBackendTexture::~GaneshBackendTexture() { |
| 81 | if (mBackendTexture.isValid()) { |
| 82 | mDeleteProc(mImageCtx); |
| 83 | mBackendTexture = {}; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | sk_sp<SkImage> GaneshBackendTexture::makeImage(SkAlphaType alphaType, ui::Dataspace dataspace, |
| 88 | TextureReleaseProc releaseImageProc, |
| 89 | ReleaseContext releaseContext) { |
| 90 | if (mBackendTexture.isValid()) { |
| 91 | mUpdateProc(mImageCtx, mGrContext.get()); |
| 92 | } |
| 93 | |
| 94 | const SkColorType colorType = colorTypeForImage(alphaType); |
| 95 | sk_sp<SkImage> image = |
| 96 | SkImages::BorrowTextureFrom(mGrContext.get(), mBackendTexture, kTopLeft_GrSurfaceOrigin, |
| 97 | colorType, alphaType, toSkColorSpace(dataspace), |
| 98 | releaseImageProc, releaseContext); |
| 99 | if (!image) { |
| 100 | logFatalTexture("Unable to generate SkImage.", dataspace, colorType); |
| 101 | } |
| 102 | return image; |
| 103 | } |
| 104 | |
| 105 | sk_sp<SkSurface> GaneshBackendTexture::makeSurface(ui::Dataspace dataspace, |
| 106 | TextureReleaseProc releaseSurfaceProc, |
| 107 | ReleaseContext releaseContext) { |
| 108 | const SkColorType colorType = internalColorType(); |
| 109 | sk_sp<SkSurface> surface = |
| 110 | SkSurfaces::WrapBackendTexture(mGrContext.get(), mBackendTexture, |
| 111 | kTopLeft_GrSurfaceOrigin, 0, colorType, |
| 112 | toSkColorSpace(dataspace), nullptr, releaseSurfaceProc, |
| 113 | releaseContext); |
| 114 | if (!surface) { |
| 115 | logFatalTexture("Unable to generate SkSurface.", dataspace, colorType); |
| 116 | } |
| 117 | return surface; |
| 118 | } |
| 119 | |
| 120 | void GaneshBackendTexture::logFatalTexture(const char* msg, ui::Dataspace dataspace, |
| 121 | SkColorType colorType) { |
| 122 | switch (mBackendTexture.backend()) { |
| 123 | case GrBackendApi::kOpenGL: { |
| 124 | GrGLTextureInfo textureInfo; |
| 125 | bool retrievedTextureInfo = |
| 126 | GrBackendTextures::GetGLTextureInfo(mBackendTexture, &textureInfo); |
| 127 | LOG_ALWAYS_FATAL("%s isTextureValid:%d dataspace:%d" |
| 128 | "\n\tGrBackendTexture: (%i x %i) hasMipmaps: %i isProtected: %i " |
| 129 | "texType: %i\n\t\tGrGLTextureInfo: success: %i fTarget: %u fFormat: %u" |
| 130 | " colorType %i", |
| 131 | msg, mBackendTexture.isValid(), static_cast<int32_t>(dataspace), |
| 132 | mBackendTexture.width(), mBackendTexture.height(), |
| 133 | mBackendTexture.hasMipmaps(), mBackendTexture.isProtected(), |
| 134 | static_cast<int>(mBackendTexture.textureType()), retrievedTextureInfo, |
| 135 | textureInfo.fTarget, textureInfo.fFormat, colorType); |
| 136 | break; |
| 137 | } |
| 138 | case GrBackendApi::kVulkan: { |
| 139 | GrVkImageInfo imageInfo; |
| 140 | bool retrievedImageInfo = |
| 141 | GrBackendTextures::GetVkImageInfo(mBackendTexture, &imageInfo); |
| 142 | LOG_ALWAYS_FATAL("%s isTextureValid:%d dataspace:%d" |
| 143 | "\n\tGrBackendTexture: (%i x %i) hasMipmaps: %i isProtected: %i " |
| 144 | "texType: %i\n\t\tVkImageInfo: success: %i fFormat: %i " |
| 145 | "fSampleCount: %u fLevelCount: %u colorType %i", |
| 146 | msg, mBackendTexture.isValid(), static_cast<int32_t>(dataspace), |
| 147 | mBackendTexture.width(), mBackendTexture.height(), |
| 148 | mBackendTexture.hasMipmaps(), mBackendTexture.isProtected(), |
| 149 | static_cast<int>(mBackendTexture.textureType()), retrievedImageInfo, |
| 150 | imageInfo.fFormat, imageInfo.fSampleCount, imageInfo.fLevelCount, |
| 151 | colorType); |
| 152 | break; |
| 153 | } |
| 154 | default: |
| 155 | LOG_ALWAYS_FATAL("%s Unexpected backend %u", msg, |
| 156 | static_cast<unsigned>(mBackendTexture.backend())); |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | } // namespace android::renderengine::skia |