blob: 931f8433da144b1e0aacffed0b3a10a7040ba6b4 [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>
Nolan Scobiebc3f3602024-08-30 13:51:37 -040022#include <include/gpu/ganesh/GrDirectContext.h>
23#include <include/gpu/ganesh/GrTypes.h>
Nolan Scobiefc125ec2024-03-11 20:08:27 -040024#include <include/gpu/ganesh/SkSurfaceGanesh.h>
25#include <include/gpu/ganesh/gl/GrGLDirectContext.h>
Nolan Scobiebc3f3602024-08-30 13:51:37 -040026#include <include/gpu/ganesh/gl/GrGLInterface.h>
Nolan Scobiefc125ec2024-03-11 20:08:27 -040027#include <include/gpu/ganesh/vk/GrVkDirectContext.h>
Kaylee Lubick1a25e4e2024-06-20 17:35:57 +000028#include <include/gpu/vk/VulkanBackendContext.h>
Nolan Scobiefc125ec2024-03-11 20:08:27 -040029
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 {
Nolan Scobiefc125ec2024-03-11 20:08:27 -040041static GrContextOptions ganeshOptions(GrContextOptions::PersistentCache& skSLCacheMonitor) {
42 GrContextOptions options;
43 options.fDisableDriverCorrectnessWorkarounds = true;
44 options.fDisableDistanceFieldPaths = true;
45 options.fReducedShaderVariations = true;
46 options.fPersistentCache = &skSLCacheMonitor;
47 return options;
48}
49} // namespace
50
51std::unique_ptr<SkiaGpuContext> SkiaGpuContext::MakeGL_Ganesh(
52 sk_sp<const GrGLInterface> glInterface,
53 GrContextOptions::PersistentCache& skSLCacheMonitor) {
54 return std::make_unique<GaneshGpuContext>(
55 GrDirectContexts::MakeGL(glInterface, ganeshOptions(skSLCacheMonitor)));
56}
57
58std::unique_ptr<SkiaGpuContext> SkiaGpuContext::MakeVulkan_Ganesh(
Kaylee Lubick1a25e4e2024-06-20 17:35:57 +000059 const skgpu::VulkanBackendContext& vkBackendContext,
Nolan Scobiefc125ec2024-03-11 20:08:27 -040060 GrContextOptions::PersistentCache& skSLCacheMonitor) {
61 return std::make_unique<GaneshGpuContext>(
Kaylee Lubick1a25e4e2024-06-20 17:35:57 +000062 GrDirectContexts::MakeVulkan(vkBackendContext, ganeshOptions(skSLCacheMonitor)));
Nolan Scobiefc125ec2024-03-11 20:08:27 -040063}
64
65GaneshGpuContext::GaneshGpuContext(sk_sp<GrDirectContext> grContext) : mGrContext(grContext) {
66 LOG_ALWAYS_FATAL_IF(mGrContext.get() == nullptr, "GrDirectContext creation failed");
67}
68
Nolan Scobie2526b2f2024-04-16 15:12:22 -040069GaneshGpuContext::~GaneshGpuContext() {
70 mGrContext->flushAndSubmit(GrSyncCpu::kYes);
71 mGrContext->abandonContext();
72};
73
Nolan Scobiefc125ec2024-03-11 20:08:27 -040074sk_sp<GrDirectContext> GaneshGpuContext::grDirectContext() {
75 return mGrContext;
76}
77
Nolan Scobieca050282024-03-15 13:27:06 -040078std::unique_ptr<SkiaBackendTexture> GaneshGpuContext::makeBackendTexture(AHardwareBuffer* buffer,
79 bool isOutputBuffer) {
80 return std::make_unique<GaneshBackendTexture>(mGrContext, buffer, isOutputBuffer);
81}
82
Nolan Scobiefc125ec2024-03-11 20:08:27 -040083sk_sp<SkSurface> GaneshGpuContext::createRenderTarget(SkImageInfo imageInfo) {
84 constexpr int kSampleCount = 1; // enable AA
85 constexpr SkSurfaceProps* kProps = nullptr;
86 constexpr bool kMipmapped = false;
87 return SkSurfaces::RenderTarget(mGrContext.get(), skgpu::Budgeted::kNo, imageInfo, kSampleCount,
88 kTopLeft_GrSurfaceOrigin, kProps, kMipmapped,
89 mGrContext->supportsProtectedContent());
90}
91
92size_t GaneshGpuContext::getMaxRenderTargetSize() const {
93 return mGrContext->maxRenderTargetSize();
94};
95
96size_t GaneshGpuContext::getMaxTextureSize() const {
97 return mGrContext->maxTextureSize();
98};
99
Nolan Scobie17ffe902024-03-25 11:07:30 -0400100bool GaneshGpuContext::isAbandonedOrDeviceLost() {
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400101 return mGrContext->abandoned();
102}
103
104void GaneshGpuContext::setResourceCacheLimit(size_t maxResourceBytes) {
105 mGrContext->setResourceCacheLimit(maxResourceBytes);
106}
107
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400108void GaneshGpuContext::purgeUnlockedScratchResources() {
109 mGrContext->purgeUnlockedResources(GrPurgeResourceOptions::kScratchResourcesOnly);
110}
111
112void GaneshGpuContext::resetContextIfApplicable() {
113 mGrContext->resetContext(); // Only applicable to GL
114};
115
116void GaneshGpuContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
117 mGrContext->dumpMemoryStatistics(traceMemoryDump);
118}
119
120} // namespace android::renderengine::skia