blob: e19d66f3c2772d95367fc93a90b6d525d3bbba13 [file] [log] [blame]
Nolan Scobie609e5972024-03-20 14:47:34 -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 "GraphiteGpuContext.h"
18
19#include <include/core/SkImageInfo.h>
20#include <include/core/SkSurface.h>
21#include <include/core/SkTraceMemoryDump.h>
22#include <include/gpu/graphite/GraphiteTypes.h>
23#include <include/gpu/graphite/Surface.h>
24#include <include/gpu/graphite/vk/VulkanGraphiteUtils.h>
25
26#include "GpuTypes.h"
27#include "skia/compat/GraphiteBackendTexture.h"
28
29#include <android-base/macros.h>
30#include <log/log_main.h>
31#include <memory>
32
33namespace android::renderengine::skia {
34
35namespace {
36static skgpu::graphite::ContextOptions graphiteOptions() {
37 skgpu::graphite::ContextOptions options;
38 options.fDisableDriverCorrectnessWorkarounds = true;
39 return options;
40}
41} // namespace
42
43std::unique_ptr<SkiaGpuContext> SkiaGpuContext::MakeVulkan_Graphite(
44 const skgpu::VulkanBackendContext& vulkanBackendContext) {
45 return std::make_unique<GraphiteGpuContext>(
46 skgpu::graphite::ContextFactory::MakeVulkan(vulkanBackendContext, graphiteOptions()));
47}
48
49GraphiteGpuContext::GraphiteGpuContext(std::unique_ptr<skgpu::graphite::Context> context)
50 : mContext(std::move(context)) {
51 LOG_ALWAYS_FATAL_IF(mContext.get() == nullptr, "graphite::Context creation failed");
52 LOG_ALWAYS_FATAL_IF(mContext->backend() != skgpu::BackendApi::kVulkan,
53 "graphite::Context::backend() == %d, but GraphiteBackendContext makes "
54 "assumptions that are only valid for Vulkan (%d)",
55 static_cast<int>(mContext->backend()),
56 static_cast<int>(skgpu::BackendApi::kVulkan));
57
58 // TODO: b/293371537 - Iterate on default cache limits (the Recorder should have the majority of
59 // the budget, and the Context should be given a smaller fraction.)
60 skgpu::graphite::RecorderOptions recorderOptions = skgpu::graphite::RecorderOptions();
61 this->mRecorder = mContext->makeRecorder(recorderOptions);
62 LOG_ALWAYS_FATAL_IF(mRecorder.get() == nullptr, "graphite::Recorder creation failed");
63}
64
65std::shared_ptr<skgpu::graphite::Context> GraphiteGpuContext::graphiteContext() {
66 return mContext;
67}
68
69std::shared_ptr<skgpu::graphite::Recorder> GraphiteGpuContext::graphiteRecorder() {
70 return mRecorder;
71}
72
73std::unique_ptr<SkiaBackendTexture> GraphiteGpuContext::makeBackendTexture(AHardwareBuffer* buffer,
74 bool isOutputBuffer) {
75 return std::make_unique<GraphiteBackendTexture>(graphiteRecorder(), buffer, isOutputBuffer);
76}
77
78sk_sp<SkSurface> GraphiteGpuContext::createRenderTarget(SkImageInfo imageInfo) {
79 constexpr SkSurfaceProps* kProps = nullptr;
80 return SkSurfaces::RenderTarget(mRecorder.get(), imageInfo, skgpu::Mipmapped::kNo, kProps);
81}
82
83size_t GraphiteGpuContext::getMaxRenderTargetSize() const {
84 // maxRenderTargetSize only differs from maxTextureSize on GL, so as long as Graphite implies
85 // Vk, then the distinction is irrelevant.
86 return getMaxTextureSize();
87};
88
89size_t GraphiteGpuContext::getMaxTextureSize() const {
90 return mContext->maxTextureSize();
91};
92
93bool GraphiteGpuContext::isAbandonedOrDeviceLost() {
94 return mContext->isDeviceLost();
95}
96
97void GraphiteGpuContext::finishRenderingAndAbandonContext() {
98 // TODO: b/293371537 - Validate that nothing else needs to be explicitly abandoned.
99 mContext->submit(skgpu::graphite::SyncToCpu::kYes);
100};
101
102void GraphiteGpuContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
103 mContext->dumpMemoryStatistics(traceMemoryDump);
104}
105
106} // namespace android::renderengine::skia