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