xref: /aosp_15_r20/external/skia/src/gpu/ganesh/ops/GrDrawOp.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2015 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 #ifndef GrDrawOp_DEFINED
9 #define GrDrawOp_DEFINED
10 
11 #include "include/core/SkTypes.h"
12 #include "include/private/base/SkMacros.h"
13 #include "src/gpu/ganesh/GrCaps.h"
14 #include "src/gpu/ganesh/GrProcessorSet.h"
15 #include "src/gpu/ganesh/ops/GrOp.h"
16 
17 #include <cstdint>
18 
19 class GrAppliedClip;
20 class GrShape;
21 class SkMatrix;
22 enum class GrAA : bool;
23 enum class GrClampType;
24 enum class SkClipOp;
25 namespace skgpu::ganesh { class SurfaceDrawContext; }
26 
27 /**
28  * Base class for GrOps that draw. These ops can draw into an op list's GrRenderTarget.
29  */
30 class GrDrawOp : public GrOp {
31 public:
GrDrawOp(uint32_t classID)32     GrDrawOp(uint32_t classID) : INHERITED(classID) {}
33 
34     /**
35      * Called before setting up the GrAppliedClip and before finalize. This information is required
36      * to determine how to compute a GrAppliedClip from a GrClip for this op.
37      */
usesMSAA()38     virtual bool usesMSAA() const {
39         return this->fixedFunctionFlags() & FixedFunctionFlags::kUsesHWAA;
40     }
41 
42     /**
43      * Specifies the effect of clipToShape().
44      */
45     enum class ClipResult {
46         // No clip was applied.
47         kFail,
48         // The clip was applied to the op's actual geometry. The clip stack is free to disable the
49         // scissor test.
50         kClippedGeometrically,
51         // The clip was applied via shader coverage. The clip stack will still use a scissor test
52         // in order to reduce overdraw of transparent pixels.
53         kClippedInShader,
54         // The op can be thrown out entirely.
55         kClippedOut
56     };
57 
58     /**
59      * This is called while the clip is being computed, before finalize(), and before any attempts
60      * to combine with other ops. If the op knows how to clip its own geometry then it will
61      * generally be much faster than a generalized clip method.
62      */
clipToShape(skgpu::ganesh::SurfaceDrawContext *,SkClipOp,const SkMatrix &,const GrShape &,GrAA)63     virtual ClipResult clipToShape(skgpu::ganesh::SurfaceDrawContext*,
64                                    SkClipOp,
65                                    const SkMatrix& /* clipMatrix */,
66                                    const GrShape&,
67                                    GrAA) {
68         return ClipResult::kFail;
69     }
70 
71     /**
72      * This is called after the GrAppliedClip has been computed and just prior to recording the op
73      * or combining it with a previously recorded op. The op should convert any proxies or resources
74      * it owns to "pending io" status so that resource allocation can be more optimal. Additionally,
75      * at this time the op must report whether a copy of the destination (or destination texture
76      * itself) needs to be provided to the GrXferProcessor when this op executes.
77      */
78     virtual GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) = 0;
79 
80     /**
81      * Called after finalize, at which point every op should know whether it will need stencil.
82      */
usesStencil()83     virtual bool usesStencil() const {
84         return this->fixedFunctionFlags() & FixedFunctionFlags::kUsesStencil;
85     }
86 
87 #ifdef SK_DEBUG
88     bool fAddDrawOpCalled = false;
89 
validate()90     void validate() const override {
91         SkASSERT(fAddDrawOpCalled);
92     }
93 #endif
94 
95 #if defined(GPU_TEST_UTILS)
96     // This is really only intended for TextureOp and FillRectOp to override
numQuads()97     virtual int numQuads() const { return -1; }
98 #endif
99 
100 protected:
101     /**
102      * DEPRECATED: This is a legacy implementation for usesMSAA() and usesStencil(). Newer ops
103      * should override those methods directly.
104      */
105     enum class FixedFunctionFlags : uint32_t {
106         kNone = 0x0,
107         /** Indices that the op will enable MSAA. */
108         kUsesHWAA = 0x1,
109         /** Indices that the op reads and/or writes the stencil buffer */
110         kUsesStencil = 0x2,
111     };
112     SK_DECL_BITFIELD_CLASS_OPS_FRIENDS(FixedFunctionFlags);
fixedFunctionFlags()113     virtual FixedFunctionFlags fixedFunctionFlags() const {
114         // Override usesMSAA() and usesStencil() instead.
115         SK_ABORT("fixedFunctionFlags() not implemented.");
116     }
117 
118 private:
119     friend class GrSimpleMeshDrawOpHelper;  // For FixedFunctionFlags.
120     friend class GrSimpleMeshDrawOpHelperWithStencil;  // For FixedFunctionFlags.
121 
122     using INHERITED = GrOp;
123 };
124 
125 SK_MAKE_BITFIELD_CLASS_OPS(GrDrawOp::FixedFunctionFlags)
126 
127 #endif
128