blob: a2457e5c154c67bee413166702970844a2645610 [file] [log] [blame]
Nolan Scobiefc125ec2024-03-11 20:08:27 -04001/*
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 Scobie609e5972024-03-20 14:47:34 -040025#include <include/gpu/graphite/Context.h>
Nolan Scobiefc125ec2024-03-11 20:08:27 -040026#include <include/gpu/vk/GrVkBackendContext.h>
Nolan Scobie609e5972024-03-20 14:47:34 -040027#include "include/gpu/vk/VulkanBackendContext.h"
Nolan Scobiefc125ec2024-03-11 20:08:27 -040028
Nolan Scobieca050282024-03-15 13:27:06 -040029#include "SkiaBackendTexture.h"
30
Nolan Scobiefc125ec2024-03-11 20:08:27 -040031#include <log/log.h>
32
Nolan Scobieca050282024-03-15 13:27:06 -040033#include <memory>
34
Nolan Scobiefc125ec2024-03-11 20:08:27 -040035namespace android::renderengine::skia {
36
37/**
38 * Abstraction over Ganesh and Graphite's underlying context-like objects.
39 */
40class SkiaGpuContext {
41public:
42 static std::unique_ptr<SkiaGpuContext> MakeGL_Ganesh(
43 sk_sp<const GrGLInterface> glInterface,
44 GrContextOptions::PersistentCache& skSLCacheMonitor);
45
Nolan Scobiefc125ec2024-03-11 20:08:27 -040046 static std::unique_ptr<SkiaGpuContext> MakeVulkan_Ganesh(
47 const GrVkBackendContext& grVkBackendContext,
48 GrContextOptions::PersistentCache& skSLCacheMonitor);
49
Nolan Scobie609e5972024-03-20 14:47:34 -040050 // TODO: b/293371537 - Need shader / pipeline monitoring support in Graphite.
51 static std::unique_ptr<SkiaGpuContext> MakeVulkan_Graphite(
52 const skgpu::VulkanBackendContext& vulkanBackendContext);
53
Nolan Scobiefc125ec2024-03-11 20:08:27 -040054 virtual ~SkiaGpuContext() = default;
55
Nolan Scobiefc125ec2024-03-11 20:08:27 -040056 /**
57 * Only callable on Ganesh-backed instances of SkiaGpuContext, otherwise fatal.
58 */
59 virtual sk_sp<GrDirectContext> grDirectContext() {
60 LOG_ALWAYS_FATAL("grDirectContext() called on a non-Ganesh instance of SkiaGpuContext!");
61 }
62
Nolan Scobie609e5972024-03-20 14:47:34 -040063 /**
64 * Only callable on Graphite-backed instances of SkiaGpuContext, otherwise fatal.
65 */
66 virtual std::shared_ptr<skgpu::graphite::Context> graphiteContext() {
67 LOG_ALWAYS_FATAL("graphiteContext() called on a non-Graphite instance of SkiaGpuContext!");
68 }
69
70 /**
71 * Only callable on Graphite-backed instances of SkiaGpuContext, otherwise fatal.
72 */
73 virtual std::shared_ptr<skgpu::graphite::Recorder> graphiteRecorder() {
74 LOG_ALWAYS_FATAL("graphiteRecorder() called on a non-Graphite instance of SkiaGpuContext!");
75 }
76
Nolan Scobieca050282024-03-15 13:27:06 -040077 virtual std::unique_ptr<SkiaBackendTexture> makeBackendTexture(AHardwareBuffer* buffer,
78 bool isOutputBuffer) = 0;
79
Nolan Scobiefc125ec2024-03-11 20:08:27 -040080 /**
81 * Notes:
82 * - The surface doesn't count against Skia's caching budgets.
83 * - Protected status is set to match the implementation's underlying context.
84 * - The origin of the surface in texture space corresponds to the top-left content pixel.
85 * - AA is always enabled.
86 */
87 virtual sk_sp<SkSurface> createRenderTarget(SkImageInfo imageInfo) = 0;
88
Nolan Scobie17ffe902024-03-25 11:07:30 -040089 virtual bool isAbandonedOrDeviceLost() = 0;
Nolan Scobiefc125ec2024-03-11 20:08:27 -040090 virtual size_t getMaxRenderTargetSize() const = 0;
91 virtual size_t getMaxTextureSize() const = 0;
92 virtual void setResourceCacheLimit(size_t maxResourceBytes) = 0;
93
94 virtual void finishRenderingAndAbandonContext() = 0;
95 virtual void purgeUnlockedScratchResources() = 0;
96 virtual void resetContextIfApplicable() = 0; // No-op outside of GL (&& Ganesh at this point.)
97
98 virtual void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const = 0;
99};
100
101} // namespace android::renderengine::skia