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/src/Caps.h"
11#include "experimental/graphite/src/CommandBuffer.h"
12#include "experimental/graphite/src/ContextUtils.h"
13#include "experimental/graphite/src/Gpu.h"
14#include "experimental/graphite/src/ProgramCache.h"
15#include "experimental/graphite/src/Recorder.h"
16#include "experimental/graphite/src/Recording.h"
17
18#ifdef SK_METAL
19#include "experimental/graphite/src/mtl/MtlTrampoline.h"
20#endif
21
22namespace skgpu {
23
24Context::Context(sk_sp<Gpu> gpu) : fGpu(std::move(gpu)) {}
25Context::~Context() {}
26
27#ifdef SK_METAL
28sk_sp<Context> Context::MakeMetal(const mtl::BackendContext& backendContext) {
29    sk_sp<Gpu> gpu = mtl::Trampoline::MakeGpu(backendContext);
30    if (!gpu) {
31        return nullptr;
32    }
33
34    return sk_sp<Context>(new Context(std::move(gpu)));
35}
36#endif
37
38sk_sp<Recorder> Context::createRecorder() {
39    return sk_make_sp<Recorder>(sk_ref_sp(this));
40}
41
42void Context::insertRecording(std::unique_ptr<Recording> recording) {
43    fRecordings.emplace_back(std::move(recording));
44}
45
46void Context::submit(SyncToCpu syncToCpu) {
47    // TODO: we want Gpu::submit to take an array of command buffers but, for now, it just takes
48    // one. Once we have more than one recording queued up we will need to extract the
49    // command buffers and submit them as a block.
50    SkASSERT(fRecordings.size() == 1);
51    fGpu->submit(fRecordings[0]->fCommandBuffer);
52
53    fGpu->checkForFinishedWork(syncToCpu);
54    fRecordings.clear();
55}
56
57void Context::preCompile(const PaintCombo& paintCombo) {
58    ProgramCache cache;
59
60    for (auto bm: paintCombo.fBlendModes) {
61        for (auto& shaderCombo: paintCombo.fShaders) {
62            for (auto shaderType: shaderCombo.fTypes) {
63                for (auto tm: shaderCombo.fTileModes) {
64                    Combination c {shaderType, tm, bm};
65
66                    sk_sp<ProgramCache::ProgramInfo> pi = cache.findOrCreateProgram(c);
67                    // TODO: this should be getSkSL
68                    // TODO: it should also return the uniform information
69                    std::string msl = pi->getMSL();
70                    // TODO: compile the MSL and store the result back into the ProgramInfo
71                    // To do this we will need the path rendering options from Chris and
72                    // a stock set of RenderPasses.
73                }
74            }
75        }
76    }
77}
78
79} // namespace skgpu
80