blob: 94ccff6fb094ccbe20b494591010f7782158b157 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ops/GrClearOp.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
Michael Ludwig9cced932020-06-08 09:30:09 -040017static bool contains_scissor(const GrScissorState& a, const GrScissorState& b) {
18 return !a.enabled() || (b.enabled() && a.rect().contains(b.rect()));
Robert Phillips7c525e62018-06-12 10:11:12 -040019}
20
Herb Derbyc76d4092020-10-07 16:46:15 -040021GrOp::Owner GrClearOp::MakeColor(GrRecordingContext* context,
22 const GrScissorState& scissor,
Brian Salomon07bc9a22020-12-02 13:37:16 -050023 std::array<float, 4> color) {
Herb Derbyc76d4092020-10-07 16:46:15 -040024 return GrOp::Make<GrClearOp>(context, Buffer::kColor, scissor, color, false);
Michael Ludwig9cced932020-06-08 09:30:09 -040025}
26
Herb Derbyc76d4092020-10-07 16:46:15 -040027GrOp::Owner GrClearOp::MakeStencilClip(GrRecordingContext* context,
28 const GrScissorState& scissor,
29 bool insideMask) {
Brian Salomon07bc9a22020-12-02 13:37:16 -050030 return GrOp::Make<GrClearOp>(context,
31 Buffer::kStencilClip,
32 scissor,
33 std::array<float, 4>(),
34 insideMask);
Michael Ludwig9cced932020-06-08 09:30:09 -040035}
36
Brian Salomon07bc9a22020-12-02 13:37:16 -050037GrClearOp::GrClearOp(Buffer buffer,
38 const GrScissorState& scissor,
39 std::array<float, 4> color,
40 bool insideMask)
Jim Van Verth6a40abc2017-11-02 16:56:09 +000041 : INHERITED(ClassID())
Michael Ludwig1e632792020-05-21 12:45:31 -040042 , fScissor(scissor)
Michael Ludwig9cced932020-06-08 09:30:09 -040043 , fColor(color)
44 , fStencilInsideMask(insideMask)
45 , fBuffer(buffer) {
Michael Ludwigd1d997e2020-06-04 15:52:44 -040046 this->setBounds(SkRect::Make(scissor.rect()), HasAABloat::kNo, IsHairline::kNo);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040047}
48
Herb Derbye25c3002020-10-27 15:57:27 -040049GrOp::CombineResult GrClearOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
Michael Ludwig9cced932020-06-08 09:30:09 -040050 GrClearOp* other = t->cast<GrClearOp>();
51
52 if (other->fBuffer == fBuffer) {
53 // This could be much more complicated. Currently we look at cases where the new clear
54 // contains the old clear, or when the new clear is a subset of the old clear and they clear
55 // to the same value (color or stencil mask depending on target).
56 if (contains_scissor(other->fScissor, fScissor)) {
57 fScissor = other->fScissor;
58 fColor = other->fColor;
59 fStencilInsideMask = other->fStencilInsideMask;
60 return CombineResult::kMerged;
61 } else if (other->fColor == fColor && other->fStencilInsideMask == fStencilInsideMask &&
62 contains_scissor(fScissor, other->fScissor)) {
63 return CombineResult::kMerged;
64 }
65 } else if (other->fScissor == fScissor) {
66 // When the scissors are the exact same but the buffers are different, we can combine and
67 // clear both stencil and clear together in onExecute().
68 if (other->fBuffer & Buffer::kColor) {
Michael Ludwig9cced932020-06-08 09:30:09 -040069 fColor = other->fColor;
70 }
71 if (other->fBuffer & Buffer::kStencilClip) {
Michael Ludwig9cced932020-06-08 09:30:09 -040072 fStencilInsideMask = other->fStencilInsideMask;
73 }
74 fBuffer = Buffer::kBoth;
Robert Phillips4b10d582020-12-02 18:27:18 -050075 return CombineResult::kMerged;
Michael Ludwig9cced932020-06-08 09:30:09 -040076 }
77 return CombineResult::kCannotCombine;
78}
79
Brian Salomon588cec72018-11-14 13:56:37 -050080void GrClearOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -040081 SkASSERT(state->opsRenderPass());
Michael Ludwig9cced932020-06-08 09:30:09 -040082 if (fBuffer & Buffer::kColor) {
83 state->opsRenderPass()->clear(fScissor, fColor);
84 }
85
86 if (fBuffer & Buffer::kStencilClip) {
87 state->opsRenderPass()->clearStencilClip(fScissor, fStencilInsideMask);
88 }
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040089}