blob: 9b5607eff25cc5871f09b8b456d4dfb3cef50c54 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
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.com1e945b72012-04-16 18:03:03 +00007#ifndef GrClipMaskManager_DEFINED
8#define GrClipMaskManager_DEFINED
9
egdaniel8dd688b2015-01-22 10:16:09 -080010#include "GrPipelineBuilder.h"
bsalomon@google.com4c2443e2012-12-06 20:58:57 +000011#include "GrReducedClip.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000012#include "GrStencil.h"
13#include "GrTexture.h"
robertphillips@google.com641f8b12012-07-31 19:15:58 +000014#include "SkClipStack.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000015#include "SkDeque.h"
16#include "SkPath.h"
17#include "SkRefCnt.h"
bsalomon@google.com8182fa02012-12-04 14:06:06 +000018#include "SkTLList.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000019#include "SkTypes.h"
robertphillips@google.com1fcc1b82012-08-29 12:52:05 +000020
bsalomonb3b9aec2015-09-10 11:16:35 -070021class GrDrawTarget;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000022class GrPathRenderer;
23class GrPathRendererChain;
robertphillips@google.comf294b772012-04-27 14:29:26 +000024class GrTexture;
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000025class SkPath;
bsalomon0ba8c242015-10-07 09:20:28 -070026
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 */
33class GrAppliedClip : public SkNoncopyable {
34public:
35 GrAppliedClip() {}
36 const GrFragmentProcessor* clipCoverageFragmentProcessor() const { return fClipCoverageFP; }
bsalomone91f7b52015-10-27 06:42:50 -070037 const GrScissorState& scissorState() const { return fScissorState; }
bsalomon0ba8c242015-10-07 09:20:28 -070038
39private:
40 SkAutoTUnref<const GrFragmentProcessor> fClipCoverageFP;
bsalomone91f7b52015-10-27 06:42:50 -070041 GrScissorState fScissorState;
bsalomon0ba8c242015-10-07 09:20:28 -070042 friend class GrClipMaskManager;
43
44 typedef SkNoncopyable INHERITED;
45};
46
robertphillips@google.com1e945b72012-04-16 18:03:03 +000047/**
rmistry@google.comd6176b02012-08-23 18:14:13 +000048 * 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.com1e945b72012-04-16 18:03:03 +000051 * 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.orge3beb6b2014-04-07 19:34:38 +000055class GrClipMaskManager : SkNoncopyable {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000056public:
bsalomonb3b9aec2015-09-10 11:16:35 -070057 GrClipMaskManager(GrDrawTarget* owner);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000058
bsalomon@google.coma3201942012-06-21 19:58:20 +000059 /**
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.org3ae0e6c2014-02-11 18:24:25 +000062 * 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.coma3201942012-06-21 19:58:20 +000065 */
joshualitt5e6ba212015-07-13 07:35:05 -070066 bool setupClipping(const GrPipelineBuilder&,
egdaniel8dd688b2015-01-22 10:16:09 -080067 GrPipelineBuilder::AutoRestoreStencil*,
bsalomon0ba8c242015-10-07 09:20:28 -070068 const SkRect* devBounds,
69 GrAppliedClip*);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000070
egdaniel8dc7c3a2015-04-16 11:22:42 -070071 void adjustPathStencilParams(const GrStencilAttachment*, GrStencilSettings*);
joshualitt329bf482014-10-29 12:31:28 -070072
bsalomon@google.coma3201942012-06-21 19:58:20 +000073private:
bsalomonedd77a12015-05-29 09:45:57 -070074 inline GrContext* getContext();
75
bsalomon@google.com411dad02012-06-05 20:24:20 +000076 /**
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.orge5a041c2014-03-07 19:43:43 +000090 // Attempts to install a series of coverage effects to implement the clip. Return indicates
bsalomon0b5b6b22015-10-14 08:31:34 -070091 // 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
bsalomona912dde2015-10-14 15:01:50 -070093 // 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.
bsalomon0b5b6b22015-10-14 08:31:34 -070095 bool getAnalyticClipProcessor(const GrReducedClip::ElementList&,
bsalomona912dde2015-10-14 15:01:50 -070096 bool abortIfAA,
bsalomon0b5b6b22015-10-14 08:31:34 -070097 SkVector& clipOffset,
98 const SkRect* devBounds,
99 const GrFragmentProcessor** fp);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000100
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000101 // Draws the clip into the stencil buffer
joshualitt9853cce2014-11-17 14:22:48 -0800102 bool createStencilClipMask(GrRenderTarget*,
103 int32_t elementsGenID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000104 GrReducedClip::InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000105 const GrReducedClip::ElementList& elements,
106 const SkIRect& clipSpaceIBounds,
107 const SkIPoint& clipSpaceToStencilOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800108
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000109 // Creates an alpha mask of the clip. The mask is a rasterization of elements through the
110 // rect specified by clipSpaceIBounds.
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000111 GrTexture* createAlphaClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000112 GrReducedClip::InitialState initialState,
113 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800114 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000115 const SkIRect& clipSpaceIBounds);
joshualitt9853cce2014-11-17 14:22:48 -0800116
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000117 // Similar to createAlphaClipMask but it rasterizes in SW and uploads to the result texture.
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000118 GrTexture* createSoftwareClipMask(int32_t elementsGenID,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000119 GrReducedClip::InitialState initialState,
120 const GrReducedClip::ElementList& elements,
joshualitt8059eb92014-12-29 15:10:07 -0800121 const SkVector& clipToMaskOffset,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000122 const SkIRect& clipSpaceIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000123
bsalomon473addf2015-10-02 07:49:05 -0700124 bool useSWOnlyPath(const GrPipelineBuilder&,
joshualitt8059eb92014-12-29 15:10:07 -0800125 const SkVector& clipToMaskOffset,
126 const GrReducedClip::ElementList& elements);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000127
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000128 // Draws a clip element into the target alpha mask. The caller should have already setup the
robertphillips@google.come79f3202014-02-11 16:30:21 +0000129 // 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.
egdaniel8dd688b2015-01-22 10:16:09 -0800131 bool drawElement(GrPipelineBuilder*,
joshualitt8059eb92014-12-29 15:10:07 -0800132 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800133 GrTexture* target,
134 const SkClipStack::Element*,
halcanary96fcdcc2015-08-27 07:41:13 -0700135 GrPathRenderer* pr = nullptr);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000136
robertphillips24cdec12015-10-26 14:12:25 -0700137 // 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
egdaniel8dd688b2015-01-22 10:16:09 -0800145 void mergeMask(GrPipelineBuilder*,
joshualitt9853cce2014-11-17 14:22:48 -0800146 GrTexture* dstMask,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000147 GrTexture* srcMask,
148 SkRegion::Op op,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000149 const SkIRect& dstBound,
150 const SkIRect& srcBound);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000151
bsalomon427cf282014-10-16 13:41:43 -0700152 GrTexture* createTempMask(int width, int height);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000153
bsalomon@google.coma3201942012-06-21 19:58:20 +0000154 /**
bsalomon9e5fc722015-02-23 10:01:36 -0800155 * 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.coma3201942012-06-21 19:58:20 +0000157 */
joshualitt5e6ba212015-07-13 07:35:05 -0700158 void setPipelineBuilderStencil(const GrPipelineBuilder&,
159 GrPipelineBuilder::AutoRestoreStencil*);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000160
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
bsalomon473addf2015-10-02 07:49:05 -0700169 GrTexture* createCachedMask(int width, int height, const GrUniqueKey& key, bool renderTarget);
170
bsalomon0ba8c242015-10-07 09:20:28 -0700171 static const int kMaxAnalyticElements = 4;
172
bsalomonb3b9aec2015-09-10 11:16:35 -0700173 GrDrawTarget* fDrawTarget; // This is our owning draw target.
joshualitt7a6184f2014-10-29 18:29:27 -0700174 StencilClipMode fClipMode;
joshualitt329bf482014-10-29 12:31:28 -0700175
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000176 typedef SkNoncopyable INHERITED;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000177};
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000178#endif // GrClipMaskManager_DEFINED