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 #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 "../AutoBackendTexture.h"
31 #include "GaneshBackendTexture.h"
32 #include "skia/compat/SkiaBackendTexture.h"
33
34 #include <android-base/macros.h>
35 #include <log/log_main.h>
36 #include <memory>
37
38 namespace android::renderengine::skia {
39
40 namespace {
ganeshOptions(GrContextOptions::PersistentCache & skSLCacheMonitor)41 static 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
MakeGL_Ganesh(sk_sp<const GrGLInterface> glInterface,GrContextOptions::PersistentCache & skSLCacheMonitor)51 std::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
MakeVulkan_Ganesh(const GrVkBackendContext & grVkBackendContext,GrContextOptions::PersistentCache & skSLCacheMonitor)58 std::unique_ptr<SkiaGpuContext> SkiaGpuContext::MakeVulkan_Ganesh(
59 const GrVkBackendContext& grVkBackendContext,
60 GrContextOptions::PersistentCache& skSLCacheMonitor) {
61 return std::make_unique<GaneshGpuContext>(
62 GrDirectContexts::MakeVulkan(grVkBackendContext, ganeshOptions(skSLCacheMonitor)));
63 }
64
GaneshGpuContext(sk_sp<GrDirectContext> grContext)65 GaneshGpuContext::GaneshGpuContext(sk_sp<GrDirectContext> grContext) : mGrContext(grContext) {
66 LOG_ALWAYS_FATAL_IF(mGrContext.get() == nullptr, "GrDirectContext creation failed");
67 }
68
~GaneshGpuContext()69 GaneshGpuContext::~GaneshGpuContext() {
70 mGrContext->flushAndSubmit(GrSyncCpu::kYes);
71 mGrContext->abandonContext();
72 };
73
grDirectContext()74 sk_sp<GrDirectContext> GaneshGpuContext::grDirectContext() {
75 return mGrContext;
76 }
77
makeBackendTexture(AHardwareBuffer * buffer,bool isOutputBuffer)78 std::unique_ptr<SkiaBackendTexture> GaneshGpuContext::makeBackendTexture(AHardwareBuffer* buffer,
79 bool isOutputBuffer) {
80 return std::make_unique<GaneshBackendTexture>(mGrContext, buffer, isOutputBuffer);
81 }
82
createRenderTarget(SkImageInfo imageInfo)83 sk_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
getMaxRenderTargetSize() const92 size_t GaneshGpuContext::getMaxRenderTargetSize() const {
93 return mGrContext->maxRenderTargetSize();
94 };
95
getMaxTextureSize() const96 size_t GaneshGpuContext::getMaxTextureSize() const {
97 return mGrContext->maxTextureSize();
98 };
99
isAbandonedOrDeviceLost()100 bool GaneshGpuContext::isAbandonedOrDeviceLost() {
101 return mGrContext->abandoned();
102 }
103
setResourceCacheLimit(size_t maxResourceBytes)104 void GaneshGpuContext::setResourceCacheLimit(size_t maxResourceBytes) {
105 mGrContext->setResourceCacheLimit(maxResourceBytes);
106 }
107
purgeUnlockedScratchResources()108 void GaneshGpuContext::purgeUnlockedScratchResources() {
109 mGrContext->purgeUnlockedResources(GrPurgeResourceOptions::kScratchResourcesOnly);
110 }
111
resetContextIfApplicable()112 void GaneshGpuContext::resetContextIfApplicable() {
113 mGrContext->resetContext(); // Only applicable to GL
114 };
115
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump) const116 void GaneshGpuContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
117 mGrContext->dumpMemoryStatistics(traceMemoryDump);
118 }
119
120 } // namespace android::renderengine::skia
121