1 /*
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.
6 */
7
8 #include "src/gpu/ops/GrClearOp.h"
9
10 #include "include/gpu/GrRecordingContext.h"
11 #include "src/gpu/GrMemoryPool.h"
12 #include "src/gpu/GrOpFlushState.h"
13 #include "src/gpu/GrOpsRenderPass.h"
14 #include "src/gpu/GrProxyProvider.h"
15 #include "src/gpu/GrRecordingContextPriv.h"
16
contains_scissor(const GrScissorState & a,const GrScissorState & b)17 static bool contains_scissor(const GrScissorState& a, const GrScissorState& b) {
18 return !a.enabled() || (b.enabled() && a.rect().contains(b.rect()));
19 }
20
MakeColor(GrRecordingContext * context,const GrScissorState & scissor,std::array<float,4> color)21 GrOp::Owner GrClearOp::MakeColor(GrRecordingContext* context,
22 const GrScissorState& scissor,
23 std::array<float, 4> color) {
24 return GrOp::Make<GrClearOp>(context, Buffer::kColor, scissor, color, false);
25 }
26
MakeStencilClip(GrRecordingContext * context,const GrScissorState & scissor,bool insideMask)27 GrOp::Owner GrClearOp::MakeStencilClip(GrRecordingContext* context,
28 const GrScissorState& scissor,
29 bool insideMask) {
30 return GrOp::Make<GrClearOp>(context,
31 Buffer::kStencilClip,
32 scissor,
33 std::array<float, 4>(),
34 insideMask);
35 }
36
GrClearOp(Buffer buffer,const GrScissorState & scissor,std::array<float,4> color,bool insideMask)37 GrClearOp::GrClearOp(Buffer buffer,
38 const GrScissorState& scissor,
39 std::array<float, 4> color,
40 bool insideMask)
41 : INHERITED(ClassID())
42 , fScissor(scissor)
43 , fColor(color)
44 , fStencilInsideMask(insideMask)
45 , fBuffer(buffer) {
46 this->setBounds(SkRect::Make(scissor.rect()), HasAABloat::kNo, IsHairline::kNo);
47 }
48
onCombineIfPossible(GrOp * t,SkArenaAlloc *,const GrCaps & caps)49 GrOp::CombineResult GrClearOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
50 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) {
69 fColor = other->fColor;
70 }
71 if (other->fBuffer & Buffer::kStencilClip) {
72 fStencilInsideMask = other->fStencilInsideMask;
73 }
74 fBuffer = Buffer::kBoth;
75 return CombineResult::kMerged;
76 }
77 return CombineResult::kCannotCombine;
78 }
79
onExecute(GrOpFlushState * state,const SkRect & chainBounds)80 void GrClearOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) {
81 SkASSERT(state->opsRenderPass());
82 if (fBuffer & Buffer::kColor) {
83 state->opsRenderPass()->clear(fScissor, fColor);
84 }
85
86 if (fBuffer & Buffer::kStencilClip) {
87 state->opsRenderPass()->clearStencilClip(fScissor, fStencilInsideMask);
88 }
89 }
90