blob: 76720a128ebb6b2d7de2d556c7e375ddc3486c8a [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,
23 const SkPMColor4f& color) {
24 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) {
30 return GrOp::Make<GrClearOp>(context, Buffer::kStencilClip, scissor, SkPMColor4f(), insideMask);
Michael Ludwig9cced932020-06-08 09:30:09 -040031}
32
33GrClearOp::GrClearOp(Buffer buffer, const GrScissorState& scissor,
34 const SkPMColor4f& color, bool insideMask)
Jim Van Verth6a40abc2017-11-02 16:56:09 +000035 : INHERITED(ClassID())
Michael Ludwig1e632792020-05-21 12:45:31 -040036 , fScissor(scissor)
Michael Ludwig9cced932020-06-08 09:30:09 -040037 , fColor(color)
38 , fStencilInsideMask(insideMask)
39 , fBuffer(buffer) {
Michael Ludwigd1d997e2020-06-04 15:52:44 -040040 this->setBounds(SkRect::Make(scissor.rect()), HasAABloat::kNo, IsHairline::kNo);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040041}
42
Herb Derbye25c3002020-10-27 15:57:27 -040043GrOp::CombineResult GrClearOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
Michael Ludwig9cced932020-06-08 09:30:09 -040044 GrClearOp* other = t->cast<GrClearOp>();
45
46 if (other->fBuffer == fBuffer) {
47 // This could be much more complicated. Currently we look at cases where the new clear
48 // contains the old clear, or when the new clear is a subset of the old clear and they clear
49 // to the same value (color or stencil mask depending on target).
50 if (contains_scissor(other->fScissor, fScissor)) {
51 fScissor = other->fScissor;
52 fColor = other->fColor;
53 fStencilInsideMask = other->fStencilInsideMask;
54 return CombineResult::kMerged;
55 } else if (other->fColor == fColor && other->fStencilInsideMask == fStencilInsideMask &&
56 contains_scissor(fScissor, other->fScissor)) {
57 return CombineResult::kMerged;
58 }
59 } else if (other->fScissor == fScissor) {
60 // When the scissors are the exact same but the buffers are different, we can combine and
61 // clear both stencil and clear together in onExecute().
62 if (other->fBuffer & Buffer::kColor) {
Michael Ludwig9cced932020-06-08 09:30:09 -040063 fColor = other->fColor;
64 }
65 if (other->fBuffer & Buffer::kStencilClip) {
Michael Ludwig9cced932020-06-08 09:30:09 -040066 fStencilInsideMask = other->fStencilInsideMask;
67 }
68 fBuffer = Buffer::kBoth;
Robert Phillips35f1b202020-12-01 17:46:04 -050069 return CombineResult::kMerged;
Michael Ludwig9cced932020-06-08 09:30:09 -040070 }
71 return CombineResult::kCannotCombine;
72}
73
Brian Salomon588cec72018-11-14 13:56:37 -050074void GrClearOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -040075 SkASSERT(state->opsRenderPass());
Michael Ludwig9cced932020-06-08 09:30:09 -040076 if (fBuffer & Buffer::kColor) {
77 state->opsRenderPass()->clear(fScissor, fColor);
78 }
79
80 if (fBuffer & Buffer::kStencilClip) {
81 state->opsRenderPass()->clearStencilClip(fScissor, fStencilInsideMask);
82 }
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040083}