blob: 294f63d21e1c83e4727ba8b82be9022ba8217fba [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/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/Recorder.h"
#include "experimental/graphite/src/Recording.h"
#include "experimental/graphite/src/Renderer.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) {}
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
sk_sp<Recorder> Context::createRecorder() {
return sk_make_sp<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) {
for (auto bm: paintCombo.fBlendModes) {
for (auto& shaderCombo: paintCombo.fShaders) {
for (auto shaderType: shaderCombo.fTypes) {
for (auto tm: shaderCombo.fTileModes) {
Combination c {shaderType, tm, bm};
GraphicsPipelineDesc desc;
for (const Renderer* r : {&Renderer::StencilAndFillPath()}) {
for (auto&& s : r->steps()) {
desc.setProgram(s, c);
// 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.
}
}
}
}
}
}
}
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