1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2021 Google LLC
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "experimental/graphite/src/RenderPassTask.h"
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "experimental/graphite/src/CommandBuffer.h"
11cb93a386Sopenharmony_ci#include "experimental/graphite/src/DrawPass.h"
12cb93a386Sopenharmony_ci#include "experimental/graphite/src/Texture.h"
13cb93a386Sopenharmony_ci#include "experimental/graphite/src/TextureProxy.h"
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_cinamespace skgpu {
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_cisk_sp<RenderPassTask> RenderPassTask::Make(std::vector<std::unique_ptr<DrawPass>> passes,
18cb93a386Sopenharmony_ci                                           const RenderPassDesc& desc) {
19cb93a386Sopenharmony_ci    // For now we have one DrawPass per RenderPassTask
20cb93a386Sopenharmony_ci    SkASSERT(passes.size() == 1);
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_ci    return sk_sp<RenderPassTask>(new RenderPassTask(std::move(passes), desc));
23cb93a386Sopenharmony_ci}
24cb93a386Sopenharmony_ci
25cb93a386Sopenharmony_ciRenderPassTask::RenderPassTask(std::vector<std::unique_ptr<DrawPass>> passes,
26cb93a386Sopenharmony_ci                               const RenderPassDesc& desc)
27cb93a386Sopenharmony_ci        : fDrawPasses(std::move(passes))
28cb93a386Sopenharmony_ci        , fRenderPassDesc(desc) {}
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_ciRenderPassTask::~RenderPassTask() = default;
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_civoid RenderPassTask::addCommands(ResourceProvider* resourceProvider, CommandBuffer* commandBuffer) {
33cb93a386Sopenharmony_ci    // TBD: Expose the surfaces that will need to be attached within the renderpass?
34cb93a386Sopenharmony_ci
35cb93a386Sopenharmony_ci    // TODO: for task execution, start the render pass, then iterate passes and
36cb93a386Sopenharmony_ci    // possibly(?) start each subpass, and call DrawPass::addCommands() on the command buffer
37cb93a386Sopenharmony_ci    // provided to the task. Then close the render pass and we should have pixels..
38cb93a386Sopenharmony_ci
39cb93a386Sopenharmony_ci    // Instantiate the attachments
40cb93a386Sopenharmony_ci    if (fRenderPassDesc.fColorAttachment.fTextureProxy) {
41cb93a386Sopenharmony_ci        auto target = fRenderPassDesc.fColorAttachment.fTextureProxy;
42cb93a386Sopenharmony_ci        if (!target->instantiate(resourceProvider)) {
43cb93a386Sopenharmony_ci            SkDebugf("WARNING: given invalid texture proxy. Will not create renderpass!\n");
44cb93a386Sopenharmony_ci            SkDebugf("Dimensions are (%d, %d).\n", target->dimensions().width(),
45cb93a386Sopenharmony_ci                     target->dimensions().height());
46cb93a386Sopenharmony_ci            return;
47cb93a386Sopenharmony_ci        }
48cb93a386Sopenharmony_ci    }
49cb93a386Sopenharmony_ci    // TODO: instantiate depth and stencil
50cb93a386Sopenharmony_ci
51cb93a386Sopenharmony_ci    commandBuffer->beginRenderPass(fRenderPassDesc);
52cb93a386Sopenharmony_ci
53cb93a386Sopenharmony_ci    // Assuming one draw pass per renderpasstask for now
54cb93a386Sopenharmony_ci    SkASSERT(fDrawPasses.size() == 1);
55cb93a386Sopenharmony_ci    for (const auto& drawPass: fDrawPasses) {
56cb93a386Sopenharmony_ci        drawPass->addCommands(commandBuffer);
57cb93a386Sopenharmony_ci    }
58cb93a386Sopenharmony_ci
59cb93a386Sopenharmony_ci    commandBuffer->endRenderPass();
60cb93a386Sopenharmony_ci}
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_ci} // namespace skgpu
63