Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -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 "GaneshGpuContext.h" |
| 18 | |
| 19 | #include <include/core/SkImageInfo.h> |
| 20 | #include <include/core/SkSurface.h> |
| 21 | #include <include/core/SkTraceMemoryDump.h> |
| 22 | #include <include/gpu/GrDirectContext.h> |
| 23 | #include <include/gpu/GrTypes.h> |
| 24 | #include <include/gpu/ganesh/SkSurfaceGanesh.h> |
| 25 | #include <include/gpu/ganesh/gl/GrGLDirectContext.h> |
| 26 | #include <include/gpu/ganesh/vk/GrVkDirectContext.h> |
| 27 | #include <include/gpu/gl/GrGLInterface.h> |
| 28 | #include <include/gpu/vk/GrVkBackendContext.h> |
| 29 | |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 30 | #include "../AutoBackendTexture.h" |
| 31 | #include "GaneshBackendTexture.h" |
| 32 | #include "skia/compat/SkiaBackendTexture.h" |
| 33 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 34 | #include <android-base/macros.h> |
| 35 | #include <log/log_main.h> |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 36 | #include <memory> |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 37 | |
| 38 | namespace android::renderengine::skia { |
| 39 | |
| 40 | namespace { |
| 41 | // TODO: b/293371537 - Graphite variant. |
| 42 | static GrContextOptions ganeshOptions(GrContextOptions::PersistentCache& skSLCacheMonitor) { |
| 43 | GrContextOptions options; |
| 44 | options.fDisableDriverCorrectnessWorkarounds = true; |
| 45 | options.fDisableDistanceFieldPaths = true; |
| 46 | options.fReducedShaderVariations = true; |
| 47 | options.fPersistentCache = &skSLCacheMonitor; |
| 48 | return options; |
| 49 | } |
| 50 | } // namespace |
| 51 | |
| 52 | std::unique_ptr<SkiaGpuContext> SkiaGpuContext::MakeGL_Ganesh( |
| 53 | sk_sp<const GrGLInterface> glInterface, |
| 54 | GrContextOptions::PersistentCache& skSLCacheMonitor) { |
| 55 | return std::make_unique<GaneshGpuContext>( |
| 56 | GrDirectContexts::MakeGL(glInterface, ganeshOptions(skSLCacheMonitor))); |
| 57 | } |
| 58 | |
| 59 | std::unique_ptr<SkiaGpuContext> SkiaGpuContext::MakeVulkan_Ganesh( |
| 60 | const GrVkBackendContext& grVkBackendContext, |
| 61 | GrContextOptions::PersistentCache& skSLCacheMonitor) { |
| 62 | return std::make_unique<GaneshGpuContext>( |
| 63 | GrDirectContexts::MakeVulkan(grVkBackendContext, ganeshOptions(skSLCacheMonitor))); |
| 64 | } |
| 65 | |
| 66 | GaneshGpuContext::GaneshGpuContext(sk_sp<GrDirectContext> grContext) : mGrContext(grContext) { |
| 67 | LOG_ALWAYS_FATAL_IF(mGrContext.get() == nullptr, "GrDirectContext creation failed"); |
| 68 | } |
| 69 | |
| 70 | sk_sp<GrDirectContext> GaneshGpuContext::grDirectContext() { |
| 71 | return mGrContext; |
| 72 | } |
| 73 | |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 74 | std::unique_ptr<SkiaBackendTexture> GaneshGpuContext::makeBackendTexture(AHardwareBuffer* buffer, |
| 75 | bool isOutputBuffer) { |
| 76 | return std::make_unique<GaneshBackendTexture>(mGrContext, buffer, isOutputBuffer); |
| 77 | } |
| 78 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 79 | sk_sp<SkSurface> GaneshGpuContext::createRenderTarget(SkImageInfo imageInfo) { |
| 80 | constexpr int kSampleCount = 1; // enable AA |
| 81 | constexpr SkSurfaceProps* kProps = nullptr; |
| 82 | constexpr bool kMipmapped = false; |
| 83 | return SkSurfaces::RenderTarget(mGrContext.get(), skgpu::Budgeted::kNo, imageInfo, kSampleCount, |
| 84 | kTopLeft_GrSurfaceOrigin, kProps, kMipmapped, |
| 85 | mGrContext->supportsProtectedContent()); |
| 86 | } |
| 87 | |
| 88 | size_t GaneshGpuContext::getMaxRenderTargetSize() const { |
| 89 | return mGrContext->maxRenderTargetSize(); |
| 90 | }; |
| 91 | |
| 92 | size_t GaneshGpuContext::getMaxTextureSize() const { |
| 93 | return mGrContext->maxTextureSize(); |
| 94 | }; |
| 95 | |
Nolan Scobie | 17ffe90 | 2024-03-25 11:07:30 -0400 | [diff] [blame^] | 96 | bool GaneshGpuContext::isAbandonedOrDeviceLost() { |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 97 | return mGrContext->abandoned(); |
| 98 | } |
| 99 | |
| 100 | void GaneshGpuContext::setResourceCacheLimit(size_t maxResourceBytes) { |
| 101 | mGrContext->setResourceCacheLimit(maxResourceBytes); |
| 102 | } |
| 103 | |
| 104 | void GaneshGpuContext::finishRenderingAndAbandonContext() { |
| 105 | mGrContext->flushAndSubmit(GrSyncCpu::kYes); |
| 106 | mGrContext->abandonContext(); |
| 107 | }; |
| 108 | |
| 109 | void GaneshGpuContext::purgeUnlockedScratchResources() { |
| 110 | mGrContext->purgeUnlockedResources(GrPurgeResourceOptions::kScratchResourcesOnly); |
| 111 | } |
| 112 | |
| 113 | void GaneshGpuContext::resetContextIfApplicable() { |
| 114 | mGrContext->resetContext(); // Only applicable to GL |
| 115 | }; |
| 116 | |
| 117 | void GaneshGpuContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
| 118 | mGrContext->dumpMemoryStatistics(traceMemoryDump); |
| 119 | } |
| 120 | |
| 121 | } // namespace android::renderengine::skia |