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