blob: 516ec95e3076cc2d16b1d41f705e7d44cd9c6aac [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#ifndef GrClipMaskManager_DEFINED
10#define GrClipMaskManager_DEFINED
11
12#include "GrRect.h"
bsalomon@google.com8d033a12012-04-27 15:52:53 +000013#include "SkPath.h"
robertphillips@google.comfd6daf52012-05-02 19:32:32 +000014#include "GrNoncopyable.h"
15#include "GrClip.h"
16#include "SkRefCnt.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000017
18class GrGpu;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000019class GrPathRenderer;
20class GrPathRendererChain;
21class SkPath;
robertphillips@google.comf294b772012-04-27 14:29:26 +000022class GrTexture;
23class GrDrawState;
robertphillips@google.com1e945b72012-04-16 18:03:03 +000024
25/**
26 * Scissoring needs special handling during stencil clip mask creation
27 * since the creation process re-entrantly invokes setupClipAndFlushState.
28 * During this process the call stack is used to keep
29 * track of (and apply to the GPU) the current scissor settings.
30 */
31struct ScissoringSettings {
32 bool fEnableScissoring;
33 GrIRect fScissorRect;
34
35 void setupScissoring(GrGpu* gpu);
36};
37
robertphillips@google.comfd6daf52012-05-02 19:32:32 +000038/**
39 * The stencil buffer stores the last clip path - providing a single entry
40 * "cache". This class provides similar functionality for AA clip paths
41 */
42class GrClipMaskCache : public GrNoncopyable {
43public:
44 GrClipMaskCache()
45 : fLastWidth(-1)
46 , fLastHeight(-1) {
47 fLastBound.MakeEmpty();
48 }
49
50 bool canReuse(const GrClip& clip, int width, int height) {
51 if (fLastWidth >= width &&
52 fLastHeight >= height &&
53 clip == fLastClip) {
54 return true;
55 }
56
57 return false;
58 }
59
60 void set(const GrClip& clip, int width, int height,
61 GrTexture* mask, const GrRect& bound) {
62
63 fLastWidth = width;
64 fLastHeight = height;
65 fLastClip = clip;
66 SkSafeRef(mask);
67 fLastMask.reset(mask);
68 fLastBound = bound;
69 }
70
71 GrTexture* getLastMask() {
72 return fLastMask.get();
73 }
74
75 const GrRect& getLastBound() const {
76 return fLastBound;
77 }
78
79protected:
80private:
81 int fLastWidth;
82 int fLastHeight;
83 GrClip fLastClip;
84 SkAutoTUnref<GrTexture> fLastMask;
85 GrRect fLastBound;
86
87 typedef GrNoncopyable INHERITED;
88};
robertphillips@google.com1e945b72012-04-16 18:03:03 +000089
90/**
91 * The clip mask creator handles the generation of the clip mask. If anti
92 * aliasing is requested it will (in the future) generate a single channel
93 * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit
94 * mask in the stencil buffer. In the non anti-aliasing case, if the clip
95 * mask can be represented as a rectangle then scissoring is used. In all
96 * cases scissoring is used to bound the range of the clip mask.
97 */
robertphillips@google.comfd6daf52012-05-02 19:32:32 +000098class GrClipMaskManager : public GrNoncopyable {
robertphillips@google.com1e945b72012-04-16 18:03:03 +000099public:
100 GrClipMaskManager()
101 : fClipMaskInStencil(false)
robertphillips@google.comf294b772012-04-27 14:29:26 +0000102 , fClipMaskInAlpha(false)
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000103 , fPathRendererChain(NULL) {
104 }
105
106 bool createClipMask(GrGpu* gpu,
107 const GrClip& clip,
108 ScissoringSettings* scissorSettings);
109
110 void freeResources();
111
112 bool isClipInStencil() const { return fClipMaskInStencil; }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000113 bool isClipInAlpha() const { return fClipMaskInAlpha; }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000114
115 void resetMask() {
116 fClipMaskInStencil = false;
117 }
118
119protected:
120private:
121 bool fClipMaskInStencil; // is the clip mask in the stencil buffer?
robertphillips@google.comf294b772012-04-27 14:29:26 +0000122 bool fClipMaskInAlpha; // is the clip mask in an alpha texture?
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000123 GrClipMaskCache fAACache; // cache for the AA path
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000124
125 // must be instantiated after GrGpu object has been given its owning
126 // GrContext ptr. (GrGpu is constructed first then handed off to GrContext).
127 GrPathRendererChain* fPathRendererChain;
128
129 bool createStencilClipMask(GrGpu* gpu,
130 const GrClip& clip,
131 const GrRect& bounds,
132 ScissoringSettings* scissorSettings);
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000133 bool createAlphaClipMask(GrGpu* gpu,
134 const GrClip& clipIn,
135 GrTexture** result,
136 GrRect *resultBounds);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000137
138 bool drawPath(GrGpu* gpu,
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000139 const SkPath& path,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000140 GrPathFill fill,
141 bool doAA);
142
143 bool drawClipShape(GrGpu* gpu,
144 GrTexture* target,
145 const GrClip& clipIn,
146 int index);
147
148 void drawTexture(GrGpu* gpu,
149 GrTexture* target,
150 const GrRect& rect,
151 GrTexture* texture);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000152
153 // determines the path renderer used to draw a clip path element.
154 GrPathRenderer* getClipPathRenderer(GrGpu* gpu,
155 const SkPath& path,
robertphillips@google.comf294b772012-04-27 14:29:26 +0000156 GrPathFill fill,
157 bool antiAlias);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000158
robertphillips@google.comfd6daf52012-05-02 19:32:32 +0000159 typedef GrNoncopyable INHERITED;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000160};
161
162#endif // GrClipMaskManager_DEFINED