blob: 51c6a6cd1c4abb4423e28fd75e5a65ba8d37ff81 [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#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
30#include <android-base/macros.h>
31#include <log/log_main.h>
32
33namespace android::renderengine::skia {
34
35namespace {
36// TODO: b/293371537 - Graphite variant.
37static GrContextOptions ganeshOptions(GrContextOptions::PersistentCache& skSLCacheMonitor) {
38 GrContextOptions options;
39 options.fDisableDriverCorrectnessWorkarounds = true;
40 options.fDisableDistanceFieldPaths = true;
41 options.fReducedShaderVariations = true;
42 options.fPersistentCache = &skSLCacheMonitor;
43 return options;
44}
45} // namespace
46
47std::unique_ptr<SkiaGpuContext> SkiaGpuContext::MakeGL_Ganesh(
48 sk_sp<const GrGLInterface> glInterface,
49 GrContextOptions::PersistentCache& skSLCacheMonitor) {
50 return std::make_unique<GaneshGpuContext>(
51 GrDirectContexts::MakeGL(glInterface, ganeshOptions(skSLCacheMonitor)));
52}
53
54std::unique_ptr<SkiaGpuContext> SkiaGpuContext::MakeVulkan_Ganesh(
55 const GrVkBackendContext& grVkBackendContext,
56 GrContextOptions::PersistentCache& skSLCacheMonitor) {
57 return std::make_unique<GaneshGpuContext>(
58 GrDirectContexts::MakeVulkan(grVkBackendContext, ganeshOptions(skSLCacheMonitor)));
59}
60
61GaneshGpuContext::GaneshGpuContext(sk_sp<GrDirectContext> grContext) : mGrContext(grContext) {
62 LOG_ALWAYS_FATAL_IF(mGrContext.get() == nullptr, "GrDirectContext creation failed");
63}
64
65sk_sp<GrDirectContext> GaneshGpuContext::grDirectContext() {
66 return mGrContext;
67}
68
69sk_sp<SkSurface> GaneshGpuContext::createRenderTarget(SkImageInfo imageInfo) {
70 constexpr int kSampleCount = 1; // enable AA
71 constexpr SkSurfaceProps* kProps = nullptr;
72 constexpr bool kMipmapped = false;
73 return SkSurfaces::RenderTarget(mGrContext.get(), skgpu::Budgeted::kNo, imageInfo, kSampleCount,
74 kTopLeft_GrSurfaceOrigin, kProps, kMipmapped,
75 mGrContext->supportsProtectedContent());
76}
77
78size_t GaneshGpuContext::getMaxRenderTargetSize() const {
79 return mGrContext->maxRenderTargetSize();
80};
81
82size_t GaneshGpuContext::getMaxTextureSize() const {
83 return mGrContext->maxTextureSize();
84};
85
86bool GaneshGpuContext::isAbandoned() {
87 return mGrContext->abandoned();
88}
89
90void GaneshGpuContext::setResourceCacheLimit(size_t maxResourceBytes) {
91 mGrContext->setResourceCacheLimit(maxResourceBytes);
92}
93
94void GaneshGpuContext::finishRenderingAndAbandonContext() {
95 mGrContext->flushAndSubmit(GrSyncCpu::kYes);
96 mGrContext->abandonContext();
97};
98
99void GaneshGpuContext::purgeUnlockedScratchResources() {
100 mGrContext->purgeUnlockedResources(GrPurgeResourceOptions::kScratchResourcesOnly);
101}
102
103void GaneshGpuContext::resetContextIfApplicable() {
104 mGrContext->resetContext(); // Only applicable to GL
105};
106
107void GaneshGpuContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
108 mGrContext->dumpMemoryStatistics(traceMemoryDump);
109}
110
111} // namespace android::renderengine::skia