blob: 2b70f02e38689504749ab7096169c146262035d6 [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 "GrTexture.h"
robertphillips@google.com641f8b12012-07-31 19:15:58 +000013#include "SkClipStack.h"
bsalomon@google.com411dad02012-06-05 20:24:20 +000014#include "SkDeque.h"
15#include "SkPath.h"
16#include "SkRefCnt.h"
bsalomon@google.com8182fa02012-12-04 14:06:06 +000017#include "SkTLList.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000018#include "SkTypes.h"
robertphillips@google.com1fcc1b82012-08-29 12:52:05 +000019
cdalton862cff32016-05-12 15:09:48 -070020class GrClip;
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:
cdalton93a379b2016-05-11 13:58:08 -070036 GrAppliedClip() : fHasStencilClip(false) {}
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; }
cdalton93a379b2016-05-11 13:58:08 -070039 bool hasStencilClip() const { return fHasStencilClip; }
bsalomon0ba8c242015-10-07 09:20:28 -070040
41private:
42 SkAutoTUnref<const GrFragmentProcessor> fClipCoverageFP;
bsalomone91f7b52015-10-27 06:42:50 -070043 GrScissorState fScissorState;
cdalton93a379b2016-05-11 13:58:08 -070044 bool fHasStencilClip;
bsalomon0ba8c242015-10-07 09:20:28 -070045 friend class GrClipMaskManager;
46
47 typedef SkNoncopyable INHERITED;
48};
49
robertphillips@google.com1e945b72012-04-16 18:03:03 +000050/**
rmistry@google.comd6176b02012-08-23 18:14:13 +000051 * The clip mask creator handles the generation of the clip mask. If anti
52 * aliasing is requested it will (in the future) generate a single channel
53 * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit
robertphillips@google.com1e945b72012-04-16 18:03:03 +000054 * mask in the stencil buffer. In the non anti-aliasing case, if the clip
55 * mask can be represented as a rectangle then scissoring is used. In all
56 * cases scissoring is used to bound the range of the clip mask.
57 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000058class GrClipMaskManager : SkNoncopyable {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000059public:
cdalton862cff32016-05-12 15:09:48 -070060 GrClipMaskManager(GrDrawTarget* owner);
robertphillips@google.com1e945b72012-04-16 18:03:03 +000061
bsalomon@google.coma3201942012-06-21 19:58:20 +000062 /**
63 * Creates a clip mask if necessary as a stencil buffer or alpha texture
64 * and sets the GrGpu's scissor and stencil state. If the return is false
cdalton93a379b2016-05-11 13:58:08 -070065 * then the draw can be skipped. devBounds is optional but can help optimize
66 * clipping.
bsalomon@google.coma3201942012-06-21 19:58:20 +000067 */
cdalton862cff32016-05-12 15:09:48 -070068 bool setupClipping(const GrPipelineBuilder&, const GrClip&, const SkRect* devBounds,
69 GrAppliedClip*);
robertphillips391395d2016-03-02 09:26:36 -080070
bsalomon@google.coma3201942012-06-21 19:58:20 +000071private:
bsalomonedd77a12015-05-29 09:45:57 -070072 inline GrContext* getContext();
robertphillips544b9aa2015-10-28 11:01:41 -070073 inline const GrCaps* caps() const;
74 inline GrResourceProvider* resourceProvider();
bsalomonedd77a12015-05-29 09:45:57 -070075
robertphillips68737822015-10-29 12:12:21 -070076 static bool PathNeedsSWRenderer(GrContext* context,
cdalton93a379b2016-05-11 13:58:08 -070077 bool hasUserStencilSettings,
robertphillips68737822015-10-29 12:12:21 -070078 const GrRenderTarget* rt,
79 const SkMatrix& viewMatrix,
80 const SkClipStack::Element* element,
81 GrPathRenderer** prOut,
82 bool needsStencil);
83 static GrPathRenderer* GetPathRenderer(GrContext* context,
84 GrTexture* texture,
85 const SkMatrix& viewMatrix,
86 const SkClipStack::Element* element);
87
bsalomon@google.com411dad02012-06-05 20:24:20 +000088 /**
89 * Informs the helper function adjustStencilParams() about how the stencil
90 * buffer clip is being used.
91 */
92 enum StencilClipMode {
93 // Draw to the clip bit of the stencil buffer
94 kModifyClip_StencilClipMode,
95 // Clip against the existing representation of the clip in the high bit
96 // of the stencil buffer.
97 kRespectClip_StencilClipMode,
98 // Neither writing to nor clipping against the clip bit.
99 kIgnoreClip_StencilClipMode,
100 };
101
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000102 // Attempts to install a series of coverage effects to implement the clip. Return indicates
bsalomon0b5b6b22015-10-14 08:31:34 -0700103 // whether the element list was successfully converted to processors. *fp may be nullptr even
104 // when the function succeeds because all the elements were ignored. TODO: Make clip reduction
bsalomona912dde2015-10-14 15:01:50 -0700105 // bounds-aware and stop checking bounds in this function. Similarly, we shouldn't need to pass
106 // abortIfAA, but we don't yet know if all the AA elements will be eliminated.
bsalomon0b5b6b22015-10-14 08:31:34 -0700107 bool getAnalyticClipProcessor(const GrReducedClip::ElementList&,
bsalomona912dde2015-10-14 15:01:50 -0700108 bool abortIfAA,
bsalomon0b5b6b22015-10-14 08:31:34 -0700109 SkVector& clipOffset,
110 const SkRect* devBounds,
111 const GrFragmentProcessor** fp);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000112
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000113 // Draws the clip into the stencil buffer
joshualitt9853cce2014-11-17 14:22:48 -0800114 bool createStencilClipMask(GrRenderTarget*,
115 int32_t elementsGenID,
commit-bot@chromium.orgd3e58422013-11-05 15:03:08 +0000116 GrReducedClip::InitialState initialState,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000117 const GrReducedClip::ElementList& elements,
118 const SkIRect& clipSpaceIBounds,
119 const SkIPoint& clipSpaceToStencilOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800120
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000121 // Creates an alpha mask of the clip. The mask is a rasterization of elements through the
122 // rect specified by clipSpaceIBounds.
robertphillips391395d2016-03-02 09:26:36 -0800123 static GrTexture* CreateAlphaClipMask(GrContext*,
124 int32_t elementsGenID,
125 GrReducedClip::InitialState initialState,
126 const GrReducedClip::ElementList& elements,
127 const SkVector& clipToMaskOffset,
128 const SkIRect& clipSpaceIBounds);
joshualitt9853cce2014-11-17 14:22:48 -0800129
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000130 // Similar to createAlphaClipMask but it rasterizes in SW and uploads to the result texture.
robertphillips391395d2016-03-02 09:26:36 -0800131 static GrTexture* CreateSoftwareClipMask(GrContext*,
132 int32_t elementsGenID,
133 GrReducedClip::InitialState initialState,
134 const GrReducedClip::ElementList& elements,
135 const SkVector& clipToMaskOffset,
136 const SkIRect& clipSpaceIBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000137
robertphillips391395d2016-03-02 09:26:36 -0800138 static bool UseSWOnlyPath(GrContext*,
139 const GrPipelineBuilder&,
140 const GrRenderTarget* rt,
141 const SkVector& clipToMaskOffset,
142 const GrReducedClip::ElementList& elements);
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000143
bsalomon473addf2015-10-02 07:49:05 -0700144 GrTexture* createCachedMask(int width, int height, const GrUniqueKey& key, bool renderTarget);
145
bsalomon0ba8c242015-10-07 09:20:28 -0700146 static const int kMaxAnalyticElements = 4;
147
bsalomonb3b9aec2015-09-10 11:16:35 -0700148 GrDrawTarget* fDrawTarget; // This is our owning draw target.
joshualitt7a6184f2014-10-29 18:29:27 -0700149 StencilClipMode fClipMode;
joshualitt329bf482014-10-29 12:31:28 -0700150
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000151 typedef SkNoncopyable INHERITED;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000152};
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000153#endif // GrClipMaskManager_DEFINED