blob: e3fec19f9b7cb111202f503bc29c88b5b2df944c [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
Nolan Scobieca050282024-03-15 13:27:06 -040030#include "../AutoBackendTexture.h"
31#include "GaneshBackendTexture.h"
32#include "skia/compat/SkiaBackendTexture.h"
33
Nolan Scobiefc125ec2024-03-11 20:08:27 -040034#include <android-base/macros.h>
35#include <log/log_main.h>
Nolan Scobieca050282024-03-15 13:27:06 -040036#include <memory>
Nolan Scobiefc125ec2024-03-11 20:08:27 -040037
38namespace android::renderengine::skia {
39
40namespace {
41// TODO: b/293371537 - Graphite variant.
42static 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
52std::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
59std::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
66GaneshGpuContext::GaneshGpuContext(sk_sp<GrDirectContext> grContext) : mGrContext(grContext) {
67 LOG_ALWAYS_FATAL_IF(mGrContext.get() == nullptr, "GrDirectContext creation failed");
68}
69
70sk_sp<GrDirectContext> GaneshGpuContext::grDirectContext() {
71 return mGrContext;
72}
73
Nolan Scobieca050282024-03-15 13:27:06 -040074std::unique_ptr<SkiaBackendTexture> GaneshGpuContext::makeBackendTexture(AHardwareBuffer* buffer,
75 bool isOutputBuffer) {
76 return std::make_unique<GaneshBackendTexture>(mGrContext, buffer, isOutputBuffer);
77}
78
Nolan Scobiefc125ec2024-03-11 20:08:27 -040079sk_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
88size_t GaneshGpuContext::getMaxRenderTargetSize() const {
89 return mGrContext->maxRenderTargetSize();
90};
91
92size_t GaneshGpuContext::getMaxTextureSize() const {
93 return mGrContext->maxTextureSize();
94};
95
Nolan Scobie17ffe902024-03-25 11:07:30 -040096bool GaneshGpuContext::isAbandonedOrDeviceLost() {
Nolan Scobiefc125ec2024-03-11 20:08:27 -040097 return mGrContext->abandoned();
98}
99
100void GaneshGpuContext::setResourceCacheLimit(size_t maxResourceBytes) {
101 mGrContext->setResourceCacheLimit(maxResourceBytes);
102}
103
104void GaneshGpuContext::finishRenderingAndAbandonContext() {
105 mGrContext->flushAndSubmit(GrSyncCpu::kYes);
106 mGrContext->abandonContext();
107};
108
109void GaneshGpuContext::purgeUnlockedScratchResources() {
110 mGrContext->purgeUnlockedResources(GrPurgeResourceOptions::kScratchResourcesOnly);
111}
112
113void GaneshGpuContext::resetContextIfApplicable() {
114 mGrContext->resetContext(); // Only applicable to GL
115};
116
117void GaneshGpuContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
118 mGrContext->dumpMemoryStatistics(traceMemoryDump);
119}
120
121} // namespace android::renderengine::skia