blob: 58abcb8b714fdb75dcdf3266c0e4b79651fefa1d [file] [log] [blame]
/*
* Copyright 2021 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "experimental/graphite/include/Context.h"
#include "experimental/graphite/include/BackendTexture.h"
#include "experimental/graphite/include/Recorder.h"
#include "experimental/graphite/include/Recording.h"
#include "experimental/graphite/include/TextureInfo.h"
#include "experimental/graphite/src/Caps.h"
#include "experimental/graphite/src/CommandBuffer.h"
#include "experimental/graphite/src/ContextUtils.h"
#include "experimental/graphite/src/Gpu.h"
#include "experimental/graphite/src/GraphicsPipelineDesc.h"
#include "experimental/graphite/src/Renderer.h"
#include "include/core/SkPathTypes.h"
#include "include/private/SkShaderCodeDictionary.h"
#include "src/core/SkKeyHelpers.h"
#ifdef SK_METAL
#include "experimental/graphite/src/mtl/MtlTrampoline.h"
#endif
namespace skgpu {
Context::Context(sk_sp<Gpu> gpu, BackendApi backend)
: fGpu(std::move(gpu))
, fBackend(backend)
, fShaderCodeDictionary(std::make_unique<SkShaderCodeDictionary>()) {
}
Context::~Context() {}
#ifdef SK_METAL
sk_sp<Context> Context::MakeMetal(const mtl::BackendContext& backendContext) {
sk_sp<Gpu> gpu = mtl::Trampoline::MakeGpu(backendContext);
if (!gpu) {
return nullptr;
}
return sk_sp<Context>(new Context(std::move(gpu), BackendApi::kMetal));
}
#endif
std::unique_ptr<Recorder> Context::makeRecorder() {
return std::unique_ptr<Recorder>(new Recorder(sk_ref_sp(this)));
}
void Context::insertRecording(std::unique_ptr<Recording> recording) {
fRecordings.emplace_back(std::move(recording));
}
void Context::submit(SyncToCpu syncToCpu) {
// TODO: we want Gpu::submit to take an array of command buffers but, for now, it just takes
// one. Once we have more than one recording queued up we will need to extract the
// command buffers and submit them as a block.
SkASSERT(fRecordings.size() == 1);
fGpu->submit(fRecordings[0]->fCommandBuffer);
fGpu->checkForFinishedWork(syncToCpu);
fRecordings.clear();
}
void Context::preCompile(const PaintCombo& paintCombo) {
static const Renderer* kRenderers[] = {
&Renderer::StencilAndFillPath(SkPathFillType::kWinding),
&Renderer::StencilAndFillPath(SkPathFillType::kEvenOdd),
&Renderer::StencilAndFillPath(SkPathFillType::kInverseWinding),
&Renderer::StencilAndFillPath(SkPathFillType::kInverseEvenOdd)
};
for (auto bm: paintCombo.fBlendModes) {
for (auto& shaderCombo: paintCombo.fShaders) {
for (auto shaderType: shaderCombo.fTypes) {
for (auto tm: shaderCombo.fTileModes) {
SkPaintParamsKey key = CreateKey(shaderType, tm, bm);
GraphicsPipelineDesc desc;
for (const Renderer* r : kRenderers) {
for (auto&& s : r->steps()) {
if (s->performsShading()) {
auto entry = fShaderCodeDictionary->findOrCreate(key);
desc.setProgram(s, entry->uniqueID());
}
// TODO: Combine with renderpass description set to generate full
// GraphicsPipeline and MSL program. Cache that compiled pipeline on
// the resource provider in a map from desc -> pipeline so that any
// later desc created from equivalent RenderStep + Combination get it.
}
}
}
}
}
}
// TODO: Iterate over the renderers and make descriptions for the steps that don't perform
// shading, and just use ShaderType::kNone.
}
BackendTexture Context::createBackendTexture(SkISize dimensions, const TextureInfo& info) {
if (!info.isValid() || info.backend() != this->backend()) {
return {};
}
return fGpu->createBackendTexture(dimensions, info);
}
void Context::deleteBackendTexture(BackendTexture& texture) {
if (!texture.isValid() || texture.backend() != this->backend()) {
return;
}
fGpu->deleteBackendTexture(texture);
}
} // namespace skgpu