xref: /aosp_15_r20/external/skia/src/gpu/ganesh/ops/ClearOp.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/ganesh/ops/ClearOp.h"
9 
10 #include "include/private/base/SkAssert.h"
11 #include "src/gpu/ganesh/GrOpFlushState.h"
12 #include "src/gpu/ganesh/GrOpsRenderPass.h"
13 
14 namespace {
15 
contains_scissor(const GrScissorState & a,const GrScissorState & b)16 bool contains_scissor(const GrScissorState& a, const GrScissorState& b) {
17     return !a.enabled() || (b.enabled() && a.rect().contains(b.rect()));
18 }
19 
20 } // anonymous namespace
21 
22 namespace skgpu::ganesh {
23 
MakeColor(GrRecordingContext * context,const GrScissorState & scissor,std::array<float,4> color)24 GrOp::Owner ClearOp::MakeColor(GrRecordingContext* context,
25                                const GrScissorState& scissor,
26                                std::array<float, 4> color) {
27     return GrOp::Make<ClearOp>(context, Buffer::kColor, scissor, color, false);
28 }
29 
MakeStencilClip(GrRecordingContext * context,const GrScissorState & scissor,bool insideMask)30 GrOp::Owner ClearOp::MakeStencilClip(GrRecordingContext* context,
31                                      const GrScissorState& scissor,
32                                      bool insideMask) {
33     return GrOp::Make<ClearOp>(context,
34                                Buffer::kStencilClip,
35                                scissor,
36                                std::array<float, 4>(),
37                                insideMask);
38 }
39 
ClearOp(Buffer buffer,const GrScissorState & scissor,std::array<float,4> color,bool insideMask)40 ClearOp::ClearOp(Buffer buffer,
41                  const GrScissorState& scissor,
42                  std::array<float, 4> color,
43                  bool insideMask)
44         : GrOp(ClassID())
45         , fScissor(scissor)
46         , fColor(color)
47         , fStencilInsideMask(insideMask)
48         , fBuffer(buffer) {
49     this->setBounds(SkRect::Make(scissor.rect()), HasAABloat::kNo, IsHairline::kNo);
50 }
51 
onCombineIfPossible(GrOp * t,SkArenaAlloc *,const GrCaps & caps)52 GrOp::CombineResult ClearOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) {
53     auto other = t->cast<ClearOp>();
54 
55     if (other->fBuffer == fBuffer) {
56         // This could be much more complicated. Currently we look at cases where the new clear
57         // contains the old clear, or when the new clear is a subset of the old clear and they clear
58         // to the same value (color or stencil mask depending on target).
59         if (contains_scissor(other->fScissor, fScissor)) {
60             fScissor = other->fScissor;
61             fColor = other->fColor;
62             fStencilInsideMask = other->fStencilInsideMask;
63             return CombineResult::kMerged;
64         } else if (other->fColor == fColor && other->fStencilInsideMask == fStencilInsideMask &&
65                    contains_scissor(fScissor, other->fScissor)) {
66             return CombineResult::kMerged;
67         }
68     } else if (other->fScissor == fScissor) {
69         // When the scissors are the exact same but the buffers are different, we can combine and
70         // clear both stencil and clear together in onExecute().
71         if (other->fBuffer & Buffer::kColor) {
72             fColor = other->fColor;
73         }
74         if (other->fBuffer & Buffer::kStencilClip) {
75             fStencilInsideMask = other->fStencilInsideMask;
76         }
77         fBuffer = Buffer::kBoth;
78         return CombineResult::kMerged;
79     }
80     return CombineResult::kCannotCombine;
81 }
82 
onExecute(GrOpFlushState * state,const SkRect & chainBounds)83 void ClearOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) {
84     SkASSERT(state->opsRenderPass());
85     if (fBuffer & Buffer::kColor) {
86         state->opsRenderPass()->clear(fScissor, fColor);
87     }
88 
89     if (fBuffer & Buffer::kStencilClip) {
90         state->opsRenderPass()->clearStencilClip(fScissor, fStencilInsideMask);
91     }
92 }
93 
94 }  // namespace skgpu::ganesh
95