robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | */ |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 7 | #ifndef GrClipMaskManager_DEFINED |
| 8 | #define GrClipMaskManager_DEFINED |
| 9 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 10 | #include "GrPipelineBuilder.h" |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 11 | #include "GrReducedClip.h" |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 12 | #include "GrStencil.h" |
| 13 | #include "GrTexture.h" |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 14 | #include "SkClipStack.h" |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 15 | #include "SkDeque.h" |
| 16 | #include "SkPath.h" |
| 17 | #include "SkRefCnt.h" |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 18 | #include "SkTLList.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 19 | #include "SkTypes.h" |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 20 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 21 | class GrDrawTarget; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 22 | class GrPathRenderer; |
| 23 | class GrPathRendererChain; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 24 | class GrTexture; |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 25 | class SkPath; |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Produced by GrClipMaskManager. It provides a set of modifications to the drawing state that |
| 29 | * are used to create the final GrPipeline for a GrBatch. This is a work in progress. It will |
| 30 | * eventually encapsulate all mechanisms for modifying the scissor, shaders, and stencil state |
| 31 | * to implement clipping. |
| 32 | */ |
| 33 | class GrAppliedClip : public SkNoncopyable { |
| 34 | public: |
| 35 | GrAppliedClip() {} |
| 36 | const GrFragmentProcessor* clipCoverageFragmentProcessor() const { return fClipCoverageFP; } |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame^] | 37 | const GrScissorState& scissorState() const { return fScissorState; } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 38 | |
| 39 | private: |
| 40 | SkAutoTUnref<const GrFragmentProcessor> fClipCoverageFP; |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame^] | 41 | GrScissorState fScissorState; |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 42 | friend class GrClipMaskManager; |
| 43 | |
| 44 | typedef SkNoncopyable INHERITED; |
| 45 | }; |
| 46 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 47 | /** |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 48 | * The clip mask creator handles the generation of the clip mask. If anti |
| 49 | * aliasing is requested it will (in the future) generate a single channel |
| 50 | * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 51 | * mask in the stencil buffer. In the non anti-aliasing case, if the clip |
| 52 | * mask can be represented as a rectangle then scissoring is used. In all |
| 53 | * cases scissoring is used to bound the range of the clip mask. |
| 54 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 55 | class GrClipMaskManager : SkNoncopyable { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 56 | public: |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 57 | GrClipMaskManager(GrDrawTarget* owner); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 58 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 59 | /** |
| 60 | * Creates a clip mask if necessary as a stencil buffer or alpha texture |
| 61 | * and sets the GrGpu's scissor and stencil state. If the return is false |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 62 | * then the draw can be skipped. The AutoRestoreEffects is initialized by |
| 63 | * the manager when it must install additional effects to implement the |
| 64 | * clip. devBounds is optional but can help optimize clipping. |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 65 | */ |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 66 | bool setupClipping(const GrPipelineBuilder&, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 67 | GrPipelineBuilder::AutoRestoreStencil*, |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 68 | const SkRect* devBounds, |
| 69 | GrAppliedClip*); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 70 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 71 | void adjustPathStencilParams(const GrStencilAttachment*, GrStencilSettings*); |
joshualitt | 329bf48 | 2014-10-29 12:31:28 -0700 | [diff] [blame] | 72 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 73 | private: |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 74 | inline GrContext* getContext(); |
| 75 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 76 | /** |
| 77 | * Informs the helper function adjustStencilParams() about how the stencil |
| 78 | * buffer clip is being used. |
| 79 | */ |
| 80 | enum StencilClipMode { |
| 81 | // Draw to the clip bit of the stencil buffer |
| 82 | kModifyClip_StencilClipMode, |
| 83 | // Clip against the existing representation of the clip in the high bit |
| 84 | // of the stencil buffer. |
| 85 | kRespectClip_StencilClipMode, |
| 86 | // Neither writing to nor clipping against the clip bit. |
| 87 | kIgnoreClip_StencilClipMode, |
| 88 | }; |
| 89 | |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 90 | // Attempts to install a series of coverage effects to implement the clip. Return indicates |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 91 | // whether the element list was successfully converted to processors. *fp may be nullptr even |
| 92 | // when the function succeeds because all the elements were ignored. TODO: Make clip reduction |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 93 | // bounds-aware and stop checking bounds in this function. Similarly, we shouldn't need to pass |
| 94 | // abortIfAA, but we don't yet know if all the AA elements will be eliminated. |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 95 | bool getAnalyticClipProcessor(const GrReducedClip::ElementList&, |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 96 | bool abortIfAA, |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 97 | SkVector& clipOffset, |
| 98 | const SkRect* devBounds, |
| 99 | const GrFragmentProcessor** fp); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 100 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 101 | // Draws the clip into the stencil buffer |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 102 | bool createStencilClipMask(GrRenderTarget*, |
| 103 | int32_t elementsGenID, |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 104 | GrReducedClip::InitialState initialState, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 105 | const GrReducedClip::ElementList& elements, |
| 106 | const SkIRect& clipSpaceIBounds, |
| 107 | const SkIPoint& clipSpaceToStencilOffset); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 108 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 109 | // Creates an alpha mask of the clip. The mask is a rasterization of elements through the |
| 110 | // rect specified by clipSpaceIBounds. |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 111 | GrTexture* createAlphaClipMask(int32_t elementsGenID, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 112 | GrReducedClip::InitialState initialState, |
| 113 | const GrReducedClip::ElementList& elements, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 114 | const SkVector& clipToMaskOffset, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 115 | const SkIRect& clipSpaceIBounds); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 116 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 117 | // Similar to createAlphaClipMask but it rasterizes in SW and uploads to the result texture. |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 118 | GrTexture* createSoftwareClipMask(int32_t elementsGenID, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 119 | GrReducedClip::InitialState initialState, |
| 120 | const GrReducedClip::ElementList& elements, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 121 | const SkVector& clipToMaskOffset, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 122 | const SkIRect& clipSpaceIBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 123 | |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 124 | bool useSWOnlyPath(const GrPipelineBuilder&, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 125 | const SkVector& clipToMaskOffset, |
| 126 | const GrReducedClip::ElementList& elements); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 127 | |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 128 | // Draws a clip element into the target alpha mask. The caller should have already setup the |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 129 | // desired blend operation. Optionally if the caller already selected a path renderer it can |
| 130 | // be passed. Otherwise the function will select one if the element is a path. |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 131 | bool drawElement(GrPipelineBuilder*, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 132 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 133 | GrTexture* target, |
| 134 | const SkClipStack::Element*, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 135 | GrPathRenderer* pr = nullptr); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 136 | |
robertphillips | 24cdec1 | 2015-10-26 14:12:25 -0700 | [diff] [blame] | 137 | // Determines whether it is possible to draw the element to both the stencil buffer and the |
| 138 | // alpha mask simultaneously. If so and the element is a path a compatible path renderer is |
| 139 | // also returned. |
| 140 | bool canStencilAndDrawElement(GrPipelineBuilder*, |
| 141 | GrTexture* target, |
| 142 | GrPathRenderer**, |
| 143 | const SkClipStack::Element*); |
| 144 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 145 | void mergeMask(GrPipelineBuilder*, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 146 | GrTexture* dstMask, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 147 | GrTexture* srcMask, |
| 148 | SkRegion::Op op, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 149 | const SkIRect& dstBound, |
| 150 | const SkIRect& srcBound); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 151 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 152 | GrTexture* createTempMask(int width, int height); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 153 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 154 | /** |
bsalomon | 9e5fc72 | 2015-02-23 10:01:36 -0800 | [diff] [blame] | 155 | * Called prior to return control back the GrGpu in setupClipping. It updates the |
| 156 | * GrPipelineBuilder with stencil settings that account for stencil-based clipping. |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 157 | */ |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 158 | void setPipelineBuilderStencil(const GrPipelineBuilder&, |
| 159 | GrPipelineBuilder::AutoRestoreStencil*); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * Adjusts the stencil settings to account for interaction with stencil |
| 163 | * clipping. |
| 164 | */ |
| 165 | void adjustStencilParams(GrStencilSettings* settings, |
| 166 | StencilClipMode mode, |
| 167 | int stencilBitCnt); |
| 168 | |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 169 | GrTexture* createCachedMask(int width, int height, const GrUniqueKey& key, bool renderTarget); |
| 170 | |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 171 | static const int kMaxAnalyticElements = 4; |
| 172 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 173 | GrDrawTarget* fDrawTarget; // This is our owning draw target. |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 174 | StencilClipMode fClipMode; |
joshualitt | 329bf48 | 2014-10-29 12:31:28 -0700 | [diff] [blame] | 175 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 176 | typedef SkNoncopyable INHERITED; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 177 | }; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 178 | #endif // GrClipMaskManager_DEFINED |