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/ClearOp.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
17 namespace {
18
contains_scissor(const GrScissorState & a,const GrScissorState & b)19 bool contains_scissor(const GrScissorState& a, const GrScissorState& b) {
20 return !a.enabled() || (b.enabled() && a.rect().contains(b.rect()));
21 }
22
23 } // anonymous namespace
24
25 namespace skgpu::v1 {
26
MakeColor(GrRecordingContext * context,const GrScissorState & scissor,std::array<float,4> color)27 GrOp::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);
31 }
32
MakeStencilClip(GrRecordingContext * context,const GrScissorState & scissor,bool insideMask)33 GrOp::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);
41 }
42
43 #ifdef SK_ENABLE_STENCIL_CULLING_OHOS
MakeStencil(GrRecordingContext * context,const GrScissorState & scissor,uint32_t stencilVal)44 GrOp::Owner ClearOp::MakeStencil(GrRecordingContext* context,
45 const GrScissorState& scissor,
46 uint32_t stencilVal) {
47 return GrOp::Make<ClearOp>(context,
48 Buffer::kStencil,
49 scissor,
50 stencilVal,
51 true);
52 }
53
ClearOp(Buffer buffer,const GrScissorState & scissor,uint32_t stencilVal,bool insideMask)54 ClearOp::ClearOp(Buffer buffer,
55 const GrScissorState& scissor,
56 uint32_t stencilVal,
57 bool insideMask)
58 : GrOp(ClassID())
59 , fScissor(scissor)
60 , fStencilVal(stencilVal)
61 , fStencilInsideMask(insideMask)
62 , fBuffer(buffer) {
63 this->setBounds(SkRect::Make(scissor.rect()), HasAABloat::kNo, IsHairline::kNo);
64 }
65 #endif
66
ClearOp(Buffer buffer,const GrScissorState & scissor,std::array<float,4> color,bool insideMask)67 ClearOp::ClearOp(Buffer buffer,
68 const GrScissorState& scissor,
69 std::array<float, 4> color,
70 bool insideMask)
71 : GrOp(ClassID())
72 , fScissor(scissor)
73 , fColor(color)
74 , fStencilInsideMask(insideMask)
75 , fBuffer(buffer) {
76 this->setBounds(SkRect::Make(scissor.rect()), HasAABloat::kNo, IsHairline::kNo);
77 }
78
onCombineIfPossible(GrOp * t,SkArenaAlloc *,const GrCaps & caps)79 GrOp::CombineResult ClearOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
80 auto other = t->cast<ClearOp>();
81 #ifdef SK_ENABLE_STENCIL_CULLING_OHOS
82 if (fBuffer == Buffer::kStencil || other->fBuffer == Buffer::kStencil) {
83 return CombineResult::kCannotCombine;
84 }
85 #endif
86 if (other->fBuffer == fBuffer) {
87 // This could be much more complicated. Currently we look at cases where the new clear
88 // contains the old clear, or when the new clear is a subset of the old clear and they clear
89 // to the same value (color or stencil mask depending on target).
90 if (contains_scissor(other->fScissor, fScissor)) {
91 fScissor = other->fScissor;
92 fColor = other->fColor;
93 fStencilInsideMask = other->fStencilInsideMask;
94 return CombineResult::kMerged;
95 } else if (other->fColor == fColor && other->fStencilInsideMask == fStencilInsideMask &&
96 contains_scissor(fScissor, other->fScissor)) {
97 return CombineResult::kMerged;
98 }
99 } else if (other->fScissor == fScissor) {
100 // When the scissors are the exact same but the buffers are different, we can combine and
101 // clear both stencil and clear together in onExecute().
102 if (other->fBuffer & Buffer::kColor) {
103 fColor = other->fColor;
104 }
105 if (other->fBuffer & Buffer::kStencilClip) {
106 fStencilInsideMask = other->fStencilInsideMask;
107 }
108 fBuffer = Buffer::kBoth;
109 return CombineResult::kMerged;
110 }
111 return CombineResult::kCannotCombine;
112 }
113
onExecute(GrOpFlushState * state,const SkRect & chainBounds)114 void ClearOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) {
115 SkASSERT(state->opsRenderPass());
116 if (fBuffer & Buffer::kColor) {
117 state->opsRenderPass()->clear(fScissor, fColor);
118 }
119
120 if (fBuffer & Buffer::kStencilClip) {
121 state->opsRenderPass()->clearStencilClip(fScissor, fStencilInsideMask);
122 }
123 #ifdef SK_ENABLE_STENCIL_CULLING_OHOS
124 if (fBuffer == Buffer::kStencil && !fShouldDisableStencilCulling) {
125 TRACE_EVENT0("skia", "StencilCullingOpt ClearOp::onExecute with stencil");
126 state->opsRenderPass()->clearStencil(fScissor, fStencilVal);
127 }
128 #endif
129 }
130
131 } // namespace skgpu::v1
132
133