blob: ef0c2be040ce4cd97a5ecc56404c7bd55c5b7467 [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;
robertphillips544b9aa2015-10-28 11:01:41 -070024class GrResourceProvider;
robertphillips@google.comf294b772012-04-27 14:29:26 +000025class GrTexture;
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000026class SkPath;
bsalomon0ba8c242015-10-07 09:20:28 -070027
28/**
29 * Produced by GrClipMaskManager. It provides a set of modifications to the drawing state that
30 * are used to create the final GrPipeline for a GrBatch. This is a work in progress. It will
31 * eventually encapsulate all mechanisms for modifying the scissor, shaders, and stencil state
32 * to implement clipping.
33 */
34class GrAppliedClip : public SkNoncopyable {
35public:
cdaltond4727922015-11-10 12:49:06 -080036 GrAppliedClip() {}
bsalomon0ba8c242015-10-07 09:20:28 -070037 const GrFragmentProcessor* clipCoverageFragmentProcessor() const { return fClipCoverageFP; }
bsalomone91f7b52015-10-27 06:42:50 -070038 const GrScissorState& scissorState() const { return fScissorState; }
bsalomon0ba8c242015-10-07 09:20:28 -070039
40private:
41 SkAutoTUnref<const GrFragmentProcessor> fClipCoverageFP;
bsalomone91f7b52015-10-27 06:42:50 -070042 GrScissorState fScissorState;
bsalomon0ba8c242015-10-07 09:20:28 -070043 friend class GrClipMaskManager;
44
45 typedef SkNoncopyable INHERITED;
46};
47
robertphillips@google.com1e945b72012-04-16 18:03:03 +000048/**
rmistry@google.comd6176b02012-08-23 18:14:13 +000049 * The clip mask creator handles the generation of the clip mask. If anti
50 * aliasing is requested it will (in the future) generate a single channel
51 * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit
robertphillips@google.com1e945b72012-04-16 18:03:03 +000052 * mask in the stencil buffer. In the non anti-aliasing case, if the clip
53 * mask can be represented as a rectangle then scissoring is used. In all
54 * cases scissoring is used to bound the range of the clip mask.
55 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000056class GrClipMaskManager : SkNoncopyable {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000057public:
bsalomon69cfe952015-11-30 13:27:47 -080058 GrClipMaskManager(GrDrawTarget* owner, bool debugClipBatchToBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000059
bsalomon@google.coma3201942012-06-21 19:58:20 +000060 /**
61 * Creates a clip mask if necessary as a stencil buffer or alpha texture
62 * and sets the GrGpu's scissor and stencil state. If the return is false
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +000063 * then the draw can be skipped. The AutoRestoreEffects is initialized by
64 * the manager when it must install additional effects to implement the
65 * clip. devBounds is optional but can help optimize clipping.
bsalomon@google.coma3201942012-06-21 19:58:20 +000066 */
joshualitt5e6ba212015-07-13 07:35:05 -070067 bool setupClipping(const GrPipelineBuilder&,
egdaniel8dd688b2015-01-22 10:16:09 -080068 GrPipelineBuilder::AutoRestoreStencil*,
bsalomon0ba8c242015-10-07 09:20:28 -070069 const SkRect* devBounds,
70 GrAppliedClip*);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000071
robertphillips391395d2016-03-02 09:26:36 -080072 bool setupScissorClip(const GrPipelineBuilder& pipelineBuilder,
73 GrPipelineBuilder::AutoRestoreStencil* ars,
halcanary9d524f22016-03-29 09:03:52 -070074 const SkIRect& scissor,
robertphillips391395d2016-03-02 09:26:36 -080075 const SkRect* devBounds,
76 GrAppliedClip* out);
77
egdaniel8dc7c3a2015-04-16 11:22:42 -070078 void adjustPathStencilParams(const GrStencilAttachment*, GrStencilSettings*);
joshualitt329bf482014-10-29 12:31:28 -070079
bsalomon@google.coma3201942012-06-21 19:58:20 +000080private:
bsalomonedd77a12015-05-29 09:45:57 -070081 inline GrContext* getContext();
robertphillips544b9aa2015-10-28 11:01:41 -070082 inline const GrCaps* caps() const;
83 inline GrResourceProvider* resourceProvider();
bsalomonedd77a12015-05-29 09:45:57 -070084
robertphillips68737822015-10-29 12:12:21 -070085 static bool PathNeedsSWRenderer(GrContext* context,
86 bool isStencilDisabled,
87 const GrRenderTarget* rt,
88 const SkMatrix& viewMatrix,
89 const SkClipStack::Element* element,
90 GrPathRenderer** prOut,
91 bool needsStencil);
92 static GrPathRenderer* GetPathRenderer(GrContext* context,
93 GrTexture* texture,
94 const SkMatrix& viewMatrix,
95 const SkClipStack::Element* element);
96
bsalomon@google.com411dad02012-06-05 20:24:20 +000097 /**
98 * Informs the helper function adjustStencilParams() about how the stencil
99 * buffer clip is being used.
100 */
101 enum StencilClipMode {
102 // Draw to the clip bit of the stencil buffer
103 kModifyClip_StencilClipMode,
104 // Clip against the existing representation of the clip in the high bit
105 // of the stencil buffer.
106 kRespectClip_StencilClipMode,
107 // Neither writing to nor clipping against the clip bit.
108 kIgnoreClip_StencilClipMode,
109 };
110
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000111 // Attempts to install a series of coverage effects to implement the clip. Return indicates
bsalomon0b5b6b22015-10-14 08:31:34 -0700112 // whether the element list was successfully converted to processors. *fp may be nullptr even
113 // when the function succeeds because all the elements were ignored. TODO: Make clip reduction
bsalomona912dde2015-10-14 15:01:50 -0700114 // bounds-aware and stop checking bounds in this function. Similarly, we shouldn't need to pass
115 // abortIfAA, but we don't yet know if all the AA elements will be eliminated.
bsalomon0b5b6b22015-10-14 08:31:34 -0700116 bool getAnalyticClipProcessor(const GrReducedClip::ElementList&,
bsalomona912dde2015-10-14 15:01:50 -0700117 bool abortIfAA,
bsalomon0b5b6b22015-10-14 08:31:34 -0700118 SkVector& clipOffset,
119 const SkRect* devBounds,
120 const GrFragmentProcessor** fp);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000121
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000122 // Draws the clip into the stencil buffer
joshualitt9853cce2014-11-17 14:22:48 -0800123 bool createStencilClipMask(GrRenderTarget*,
124 int32_t elementsGenID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000125 GrReducedClip::InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000126 const GrReducedClip::ElementList& elements,
127 const SkIRect& clipSpaceIBounds,
128 const SkIPoint& clipSpaceToStencilOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800129
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000130 // Creates an alpha mask of the clip. The mask is a rasterization of elements through the
131 // rect specified by clipSpaceIBounds.
robertphillips391395d2016-03-02 09:26:36 -0800132 static GrTexture* CreateAlphaClipMask(GrContext*,
133 int32_t elementsGenID,
134 GrReducedClip::InitialState initialState,
135 const GrReducedClip::ElementList& elements,
136 const SkVector& clipToMaskOffset,
137 const SkIRect& clipSpaceIBounds);
joshualitt9853cce2014-11-17 14:22:48 -0800138
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000139 // Similar to createAlphaClipMask but it rasterizes in SW and uploads to the result texture.
robertphillips391395d2016-03-02 09:26:36 -0800140 static GrTexture* CreateSoftwareClipMask(GrContext*,
141 int32_t elementsGenID,
142 GrReducedClip::InitialState initialState,
143 const GrReducedClip::ElementList& elements,
144 const SkVector& clipToMaskOffset,
145 const SkIRect& clipSpaceIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000146
robertphillips391395d2016-03-02 09:26:36 -0800147 static bool UseSWOnlyPath(GrContext*,
148 const GrPipelineBuilder&,
149 const GrRenderTarget* rt,
150 const SkVector& clipToMaskOffset,
151 const GrReducedClip::ElementList& elements);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000152
bsalomon@google.coma3201942012-06-21 19:58:20 +0000153 /**
bsalomon9e5fc722015-02-23 10:01:36 -0800154 * Called prior to return control back the GrGpu in setupClipping. It updates the
155 * GrPipelineBuilder with stencil settings that account for stencil-based clipping.
bsalomon@google.coma3201942012-06-21 19:58:20 +0000156 */
joshualitt5e6ba212015-07-13 07:35:05 -0700157 void setPipelineBuilderStencil(const GrPipelineBuilder&,
158 GrPipelineBuilder::AutoRestoreStencil*);
bsalomon@google.coma3201942012-06-21 19:58:20 +0000159
160 /**
161 * Adjusts the stencil settings to account for interaction with stencil
162 * clipping.
163 */
164 void adjustStencilParams(GrStencilSettings* settings,
165 StencilClipMode mode,
166 int stencilBitCnt);
167
bsalomon473addf2015-10-02 07:49:05 -0700168 GrTexture* createCachedMask(int width, int height, const GrUniqueKey& key, bool renderTarget);
169
bsalomon0ba8c242015-10-07 09:20:28 -0700170 static const int kMaxAnalyticElements = 4;
171
bsalomonb3b9aec2015-09-10 11:16:35 -0700172 GrDrawTarget* fDrawTarget; // This is our owning draw target.
joshualitt7a6184f2014-10-29 18:29:27 -0700173 StencilClipMode fClipMode;
bsalomon69cfe952015-11-30 13:27:47 -0800174 bool fDebugClipBatchToBounds;
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