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 | #pragma once |
| 18 | |
| 19 | #undef LOG_TAG |
| 20 | #define LOG_TAG "RenderEngine" |
| 21 | |
| 22 | #include <include/core/SkSurface.h> |
| 23 | #include <include/gpu/GrDirectContext.h> |
| 24 | #include <include/gpu/gl/GrGLInterface.h> |
Nolan Scobie | 609e597 | 2024-03-20 14:47:34 -0400 | [diff] [blame] | 25 | #include <include/gpu/graphite/Context.h> |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 26 | #include <include/gpu/vk/GrVkBackendContext.h> |
Nolan Scobie | 609e597 | 2024-03-20 14:47:34 -0400 | [diff] [blame] | 27 | #include "include/gpu/vk/VulkanBackendContext.h" |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 28 | |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 29 | #include "SkiaBackendTexture.h" |
| 30 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 31 | #include <log/log.h> |
| 32 | |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 33 | #include <memory> |
| 34 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 35 | namespace android::renderengine::skia { |
| 36 | |
| 37 | /** |
| 38 | * Abstraction over Ganesh and Graphite's underlying context-like objects. |
Nolan Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 39 | * |
| 40 | * On destruction, subclasses will submit any pending work before destroying their internal Skia |
| 41 | * context(s). Any unused cached SkiaBackendTextures created from a SkiaGpuContext that are awaiting |
| 42 | * cleanup must be deleted before destroying that SkiaGpuContext, and any textures that are released |
| 43 | * during ~SkiaGpuContext must be configured to be deleted immediately. |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 44 | */ |
| 45 | class SkiaGpuContext { |
| 46 | public: |
Nolan Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 47 | /** |
| 48 | * glInterface must remain valid until after SkiaGpuContext is destroyed. |
| 49 | */ |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 50 | static std::unique_ptr<SkiaGpuContext> MakeGL_Ganesh( |
| 51 | sk_sp<const GrGLInterface> glInterface, |
| 52 | GrContextOptions::PersistentCache& skSLCacheMonitor); |
| 53 | |
Nolan Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 54 | /** |
| 55 | * grVkBackendContext must remain valid until after SkiaGpuContext is destroyed. |
| 56 | */ |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 57 | static std::unique_ptr<SkiaGpuContext> MakeVulkan_Ganesh( |
| 58 | const GrVkBackendContext& grVkBackendContext, |
| 59 | GrContextOptions::PersistentCache& skSLCacheMonitor); |
| 60 | |
Nolan Scobie | 609e597 | 2024-03-20 14:47:34 -0400 | [diff] [blame] | 61 | // TODO: b/293371537 - Need shader / pipeline monitoring support in Graphite. |
Nolan Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 62 | /** |
| 63 | * vulkanBackendContext must remain valid until after SkiaGpuContext is destroyed. |
| 64 | */ |
Nolan Scobie | 609e597 | 2024-03-20 14:47:34 -0400 | [diff] [blame] | 65 | static std::unique_ptr<SkiaGpuContext> MakeVulkan_Graphite( |
| 66 | const skgpu::VulkanBackendContext& vulkanBackendContext); |
| 67 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 68 | virtual ~SkiaGpuContext() = default; |
| 69 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 70 | /** |
| 71 | * Only callable on Ganesh-backed instances of SkiaGpuContext, otherwise fatal. |
| 72 | */ |
| 73 | virtual sk_sp<GrDirectContext> grDirectContext() { |
| 74 | LOG_ALWAYS_FATAL("grDirectContext() called on a non-Ganesh instance of SkiaGpuContext!"); |
| 75 | } |
| 76 | |
Nolan Scobie | 609e597 | 2024-03-20 14:47:34 -0400 | [diff] [blame] | 77 | /** |
| 78 | * Only callable on Graphite-backed instances of SkiaGpuContext, otherwise fatal. |
| 79 | */ |
| 80 | virtual std::shared_ptr<skgpu::graphite::Context> graphiteContext() { |
| 81 | LOG_ALWAYS_FATAL("graphiteContext() called on a non-Graphite instance of SkiaGpuContext!"); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Only callable on Graphite-backed instances of SkiaGpuContext, otherwise fatal. |
| 86 | */ |
| 87 | virtual std::shared_ptr<skgpu::graphite::Recorder> graphiteRecorder() { |
| 88 | LOG_ALWAYS_FATAL("graphiteRecorder() called on a non-Graphite instance of SkiaGpuContext!"); |
| 89 | } |
| 90 | |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 91 | virtual std::unique_ptr<SkiaBackendTexture> makeBackendTexture(AHardwareBuffer* buffer, |
| 92 | bool isOutputBuffer) = 0; |
| 93 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 94 | /** |
| 95 | * Notes: |
| 96 | * - The surface doesn't count against Skia's caching budgets. |
| 97 | * - Protected status is set to match the implementation's underlying context. |
| 98 | * - The origin of the surface in texture space corresponds to the top-left content pixel. |
| 99 | * - AA is always enabled. |
| 100 | */ |
| 101 | virtual sk_sp<SkSurface> createRenderTarget(SkImageInfo imageInfo) = 0; |
| 102 | |
Nolan Scobie | 17ffe90 | 2024-03-25 11:07:30 -0400 | [diff] [blame] | 103 | virtual bool isAbandonedOrDeviceLost() = 0; |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 104 | virtual size_t getMaxRenderTargetSize() const = 0; |
| 105 | virtual size_t getMaxTextureSize() const = 0; |
| 106 | virtual void setResourceCacheLimit(size_t maxResourceBytes) = 0; |
| 107 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 108 | virtual void purgeUnlockedScratchResources() = 0; |
| 109 | virtual void resetContextIfApplicable() = 0; // No-op outside of GL (&& Ganesh at this point.) |
| 110 | |
| 111 | virtual void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const = 0; |
| 112 | }; |
| 113 | |
| 114 | } // namespace android::renderengine::skia |