• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "experimental/graphite/include/Context.h"
9 
10 #include "experimental/graphite/include/BackendTexture.h"
11 #include "experimental/graphite/include/Recorder.h"
12 #include "experimental/graphite/include/Recording.h"
13 #include "experimental/graphite/include/TextureInfo.h"
14 #include "experimental/graphite/src/Caps.h"
15 #include "experimental/graphite/src/CommandBuffer.h"
16 #include "experimental/graphite/src/GlobalCache.h"
17 #include "experimental/graphite/src/Gpu.h"
18 #include "experimental/graphite/src/GraphicsPipelineDesc.h"
19 #include "experimental/graphite/src/Renderer.h"
20 #include "experimental/graphite/src/ResourceProvider.h"
21 #include "include/core/SkPathTypes.h"
22 #include "src/core/SkKeyHelpers.h"
23 #include "src/core/SkShaderCodeDictionary.h"
24 
25 #ifdef SK_METAL
26 #include "experimental/graphite/src/mtl/MtlTrampoline.h"
27 #endif
28 
29 namespace skgpu {
30 
Context(sk_sp<Gpu> gpu,BackendApi backend)31 Context::Context(sk_sp<Gpu> gpu, BackendApi backend)
32         : fGpu(std::move(gpu))
33         , fGlobalCache(sk_make_sp<GlobalCache>())
34         , fBackend(backend) {
35 }
~Context()36 Context::~Context() {}
37 
38 #ifdef SK_METAL
MakeMetal(const mtl::BackendContext & backendContext)39 std::unique_ptr<Context> Context::MakeMetal(const mtl::BackendContext& backendContext) {
40     sk_sp<Gpu> gpu = mtl::Trampoline::MakeGpu(backendContext);
41     if (!gpu) {
42         return nullptr;
43     }
44 
45     return std::unique_ptr<Context>(new Context(std::move(gpu), BackendApi::kMetal));
46 }
47 #endif
48 
makeRecorder()49 std::unique_ptr<Recorder> Context::makeRecorder() {
50     return std::unique_ptr<Recorder>(new Recorder(fGpu, fGlobalCache));
51 }
52 
insertRecording(std::unique_ptr<Recording> recording)53 void Context::insertRecording(std::unique_ptr<Recording> recording) {
54     fRecordings.emplace_back(std::move(recording));
55 }
56 
submit(SyncToCpu syncToCpu)57 void Context::submit(SyncToCpu syncToCpu) {
58     // TODO: we want Gpu::submit to take an array of command buffers but, for now, it just takes
59     // one. Once we have more than one recording queued up we will need to extract the
60     // command buffers and submit them as a block.
61     SkASSERT(fRecordings.size() == 1);
62     fGpu->submit(fRecordings[0]->fCommandBuffer);
63 
64     fGpu->checkForFinishedWork(syncToCpu);
65     fRecordings.clear();
66 }
67 
preCompile(const PaintCombo & paintCombo)68 void Context::preCompile(const PaintCombo& paintCombo) {
69     static const Renderer* kRenderers[] = {
70             &Renderer::StencilAndFillPath(SkPathFillType::kWinding),
71             &Renderer::StencilAndFillPath(SkPathFillType::kEvenOdd),
72             &Renderer::StencilAndFillPath(SkPathFillType::kInverseWinding),
73             &Renderer::StencilAndFillPath(SkPathFillType::kInverseEvenOdd)
74     };
75 
76     SkShaderCodeDictionary* dict = fGlobalCache->shaderCodeDictionary();
77 
78     for (auto bm: paintCombo.fBlendModes) {
79         for (auto& shaderCombo: paintCombo.fShaders) {
80             for (auto shaderType: shaderCombo.fTypes) {
81                 for (auto tm: shaderCombo.fTileModes) {
82                     std::unique_ptr<SkPaintParamsKey> key = CreateKey(dict, SkBackend::kGraphite,
83                                                                       shaderType, tm, bm);
84                     auto entry = dict->findOrCreate(std::move(key));
85 
86                     GraphicsPipelineDesc desc;
87 
88                     for (const Renderer* r : kRenderers) {
89                         for (auto&& s : r->steps()) {
90                             if (s->performsShading()) {
91                                 desc.setProgram(s, entry->uniqueID());
92                             }
93                             // TODO: Combine with renderpass description set to generate full
94                             // GraphicsPipeline and MSL program. Cache that compiled pipeline on
95                             // the resource provider in a map from desc -> pipeline so that any
96                             // later desc created from equivalent RenderStep + Combination get it.
97                         }
98                     }
99                 }
100             }
101         }
102     }
103     // TODO: Iterate over the renderers and make descriptions for the steps that don't perform
104     // shading, and just use ShaderType::kNone.
105 }
106 
createBackendTexture(SkISize dimensions,const TextureInfo & info)107 BackendTexture Context::createBackendTexture(SkISize dimensions, const TextureInfo& info) {
108     if (!info.isValid() || info.backend() != this->backend()) {
109         return {};
110     }
111     return fGpu->createBackendTexture(dimensions, info);
112 }
113 
deleteBackendTexture(BackendTexture & texture)114 void Context::deleteBackendTexture(BackendTexture& texture) {
115     if (!texture.isValid() || texture.backend() != this->backend()) {
116         return;
117     }
118     fGpu->deleteBackendTexture(texture);
119 }
120 
121 } // namespace skgpu
122