blob: 33fb97eeb986340990e14abb2a6649cd9f75ee00 [file] [log] [blame]
Robert Phillipsfbcef6e2017-06-15 12:07:18 -04001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04006 */
7
Robert Phillips46d7bd52021-09-01 13:02:28 -04008#include "src/gpu/ops/ClearOp.h"
Robert Phillipsb0e93a22017-08-29 08:26:54 -04009
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040010#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrMemoryPool.h"
12#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040013#include "src/gpu/GrOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrProxyProvider.h"
15#include "src/gpu/GrRecordingContextPriv.h"
Robert Phillipsb0e93a22017-08-29 08:26:54 -040016
Robert Phillips46d7bd52021-09-01 13:02:28 -040017namespace {
18
19bool contains_scissor(const GrScissorState& a, const GrScissorState& b) {
Michael Ludwig9cced932020-06-08 09:30:09 -040020 return !a.enabled() || (b.enabled() && a.rect().contains(b.rect()));
Robert Phillips7c525e62018-06-12 10:11:12 -040021}
22
Robert Phillips46d7bd52021-09-01 13:02:28 -040023} // anonymous namespace
24
25namespace skgpu::v1 {
26
27GrOp::Owner ClearOp::MakeColor(GrRecordingContext* context,
28 const GrScissorState& scissor,
29 std::array<float, 4> color) {
30 return GrOp::Make<ClearOp>(context, Buffer::kColor, scissor, color, false);
Michael Ludwig9cced932020-06-08 09:30:09 -040031}
32
Robert Phillips46d7bd52021-09-01 13:02:28 -040033GrOp::Owner ClearOp::MakeStencilClip(GrRecordingContext* context,
34 const GrScissorState& scissor,
35 bool insideMask) {
36 return GrOp::Make<ClearOp>(context,
37 Buffer::kStencilClip,
38 scissor,
39 std::array<float, 4>(),
40 insideMask);
Michael Ludwig9cced932020-06-08 09:30:09 -040041}
42
Robert Phillips46d7bd52021-09-01 13:02:28 -040043ClearOp::ClearOp(Buffer buffer,
44 const GrScissorState& scissor,
45 std::array<float, 4> color,
46 bool insideMask)
47 : GrOp(ClassID())
Michael Ludwig1e632792020-05-21 12:45:31 -040048 , fScissor(scissor)
Michael Ludwig9cced932020-06-08 09:30:09 -040049 , fColor(color)
50 , fStencilInsideMask(insideMask)
51 , fBuffer(buffer) {
Michael Ludwigd1d997e2020-06-04 15:52:44 -040052 this->setBounds(SkRect::Make(scissor.rect()), HasAABloat::kNo, IsHairline::kNo);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040053}
54
Robert Phillips46d7bd52021-09-01 13:02:28 -040055GrOp::CombineResult ClearOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
56 auto other = t->cast<ClearOp>();
Michael Ludwig9cced932020-06-08 09:30:09 -040057
58 if (other->fBuffer == fBuffer) {
59 // This could be much more complicated. Currently we look at cases where the new clear
60 // contains the old clear, or when the new clear is a subset of the old clear and they clear
61 // to the same value (color or stencil mask depending on target).
62 if (contains_scissor(other->fScissor, fScissor)) {
63 fScissor = other->fScissor;
64 fColor = other->fColor;
65 fStencilInsideMask = other->fStencilInsideMask;
66 return CombineResult::kMerged;
67 } else if (other->fColor == fColor && other->fStencilInsideMask == fStencilInsideMask &&
68 contains_scissor(fScissor, other->fScissor)) {
69 return CombineResult::kMerged;
70 }
71 } else if (other->fScissor == fScissor) {
72 // When the scissors are the exact same but the buffers are different, we can combine and
73 // clear both stencil and clear together in onExecute().
74 if (other->fBuffer & Buffer::kColor) {
Michael Ludwig9cced932020-06-08 09:30:09 -040075 fColor = other->fColor;
76 }
77 if (other->fBuffer & Buffer::kStencilClip) {
Michael Ludwig9cced932020-06-08 09:30:09 -040078 fStencilInsideMask = other->fStencilInsideMask;
79 }
80 fBuffer = Buffer::kBoth;
Robert Phillips4b10d582020-12-02 18:27:18 -050081 return CombineResult::kMerged;
Michael Ludwig9cced932020-06-08 09:30:09 -040082 }
83 return CombineResult::kCannotCombine;
84}
85
Robert Phillips46d7bd52021-09-01 13:02:28 -040086void ClearOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -040087 SkASSERT(state->opsRenderPass());
Michael Ludwig9cced932020-06-08 09:30:09 -040088 if (fBuffer & Buffer::kColor) {
89 state->opsRenderPass()->clear(fScissor, fColor);
90 }
91
92 if (fBuffer & Buffer::kStencilClip) {
93 state->opsRenderPass()->clearStencilClip(fScissor, fStencilInsideMask);
94 }
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040095}
Robert Phillips46d7bd52021-09-01 13:02:28 -040096
97} // namespace skgpu::v1
98