blob: 8eea3218ac42091efa1c9828729a3a5cb0677612 [file] [log] [blame]
kkinnunenccdaa042014-08-20 01:36:23 -07001/*
2 * Copyright 2014 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 */
7
8#ifndef GrPathRendering_DEFINED
9#define GrPathRendering_DEFINED
10
11#include "SkPath.h"
kkinnunencabe20c2015-06-01 01:37:26 -070012#include "GrGpu.h"
cdalton855d83f2014-09-18 13:51:53 -070013#include "GrPathRange.h"
bsalomoncb02b382015-08-12 11:14:50 -070014#include "GrPipeline.h"
kkinnunenccdaa042014-08-20 01:36:23 -070015
cdalton855d83f2014-09-18 13:51:53 -070016class SkDescriptor;
17class SkTypeface;
kkinnunenccdaa042014-08-20 01:36:23 -070018class GrPath;
joshualitt92e496f2014-10-31 13:56:50 -070019class GrStencilSettings;
bsalomon6663acf2016-05-10 09:14:17 -070020class GrStyle;
kkinnunenccdaa042014-08-20 01:36:23 -070021
22/**
23 * Abstract class wrapping HW path rendering API.
24 *
25 * The subclasses of this class use the possible HW API to render paths (as opposed to path
26 * rendering implemented in Skia on top of a "3d" HW API).
27 * The subclasses hold the global state needed to render paths, including shadow of the global HW
28 * API state. Similar to GrGpu.
29 *
30 * It is expected that the lifetimes of GrGpuXX and GrXXPathRendering are the same. The call context
31 * interface (eg. * the concrete instance of GrGpu subclass) should be provided to the instance
32 * during construction.
33 */
34class GrPathRendering {
35public:
36 virtual ~GrPathRendering() { }
37
cdalton55b24af2014-11-25 11:00:56 -080038 typedef GrPathRange::PathIndexType PathIndexType;
joshualitt92e496f2014-10-31 13:56:50 -070039
kkinnunenccdaa042014-08-20 01:36:23 -070040 enum PathTransformType {
41 kNone_PathTransformType, //!< []
42 kTranslateX_PathTransformType, //!< [kMTransX]
43 kTranslateY_PathTransformType, //!< [kMTransY]
44 kTranslate_PathTransformType, //!< [kMTransX, kMTransY]
45 kAffine_PathTransformType, //!< [kMScaleX, kMSkewX, kMTransX, kMSkewY, kMScaleY, kMTransY]
46
47 kLast_PathTransformType = kAffine_PathTransformType
48 };
49
50 static inline int PathTransformSize(PathTransformType type) {
51 switch (type) {
52 case kNone_PathTransformType:
53 return 0;
54 case kTranslateX_PathTransformType:
55 case kTranslateY_PathTransformType:
56 return 1;
57 case kTranslate_PathTransformType:
58 return 2;
59 case kAffine_PathTransformType:
60 return 6;
61
62 default:
63 SkFAIL("Unknown path transform type");
64 return 0;
65 }
66 }
67
cdalton55b24af2014-11-25 11:00:56 -080068 // No native support for inverse at this time
69 enum FillType {
70 /** Specifies that "inside" is computed by a non-zero sum of signed
71 edge crossings
72 */
73 kWinding_FillType,
74 /** Specifies that "inside" is computed by an odd number of edge
75 crossings
76 */
77 kEvenOdd_FillType,
78 };
79
cdalton4e205b12014-09-17 09:41:24 -070080 /**
81 * Creates a new gpu path, based on the specified path and stroke and returns it.
82 * The caller owns a ref on the returned path which must be balanced by a call to unref.
83 *
bsalomon6663acf2016-05-10 09:14:17 -070084 * @param SkPath the geometry.
85 * @param GrStyle the style applied to the path. Styles with non-dash path effects are not
86 * allowed.
87 * @return a new GPU path object.
cdalton4e205b12014-09-17 09:41:24 -070088 */
bsalomon6663acf2016-05-10 09:14:17 -070089 virtual GrPath* createPath(const SkPath&, const GrStyle&) = 0;
cdalton4e205b12014-09-17 09:41:24 -070090
91 /**
bsalomon6663acf2016-05-10 09:14:17 -070092 * Creates a range of gpu paths with a common style. The caller owns a ref on the
cdalton4e205b12014-09-17 09:41:24 -070093 * returned path range which must be balanced by a call to unref.
94 *
95 * @param PathGenerator class that generates SkPath objects for each path in the range.
bsalomon6663acf2016-05-10 09:14:17 -070096 * @param GrStyle the common style applied to each path in the range. Styles with non-dash
97 * path effects are not allowed.
cdalton4e205b12014-09-17 09:41:24 -070098 * @return a new path range.
99 */
bsalomon6663acf2016-05-10 09:14:17 -0700100 virtual GrPathRange* createPathRange(GrPathRange::PathGenerator*, const GrStyle&) = 0;
cdalton855d83f2014-09-18 13:51:53 -0700101
102 /**
103 * Creates a range of glyph paths, indexed by glyph id. The glyphs will have an
104 * inverted y-direction in order to match the raw font path data. The caller owns
105 * a ref on the returned path range which must be balanced by a call to unref.
106 *
107 * @param SkTypeface Typeface that defines the glyphs.
108 * If null, the default typeface will be used.
109 *
110 * @param SkDescriptor Additional font configuration that specifies the font's size,
111 * stroke, and other flags. This will generally come from an
112 * SkGlyphCache.
113 *
114 * It is recommended to leave this value null when possible, in
115 * which case the glyphs will be loaded directly from the font's
116 * raw path data and sized at SkPaint::kCanonicalTextSizeForPaths.
117 * This will result in less memory usage and more efficient paths.
118 *
119 * If non-null, the glyph paths will match the font descriptor,
120 * including with the stroke information baked directly into
121 * the outlines.
122 *
bsalomon6663acf2016-05-10 09:14:17 -0700123 * @param GrStyle Common style that the GPU will apply to every path. Note that
124 * if the glyph outlines contain baked-in styles from the font
125 * descriptor, the GPU style will be applied on top of those
cdalton855d83f2014-09-18 13:51:53 -0700126 * outlines.
127 *
128 * @return a new path range populated with glyphs.
129 */
reeda9322c22016-04-12 06:47:05 -0700130 GrPathRange* createGlyphs(const SkTypeface*, const SkScalerContextEffects&,
bsalomon6663acf2016-05-10 09:14:17 -0700131 const SkDescriptor*, const GrStyle&);
cdalton4e205b12014-09-17 09:41:24 -0700132
kkinnunencabe20c2015-06-01 01:37:26 -0700133 /** None of these params are optional, pointers used just to avoid making copies. */
134 struct StencilPathArgs {
135 StencilPathArgs(bool useHWAA,
136 GrRenderTarget* renderTarget,
137 const SkMatrix* viewMatrix,
138 const GrScissorState* scissor,
139 const GrStencilSettings* stencil)
140 : fUseHWAA(useHWAA)
141 , fRenderTarget(renderTarget)
142 , fViewMatrix(viewMatrix)
143 , fScissor(scissor)
144 , fStencil(stencil) {
145 }
146 bool fUseHWAA;
147 GrRenderTarget* fRenderTarget;
148 const SkMatrix* fViewMatrix;
149 const GrScissorState* fScissor;
150 const GrStencilSettings* fStencil;
151 };
kkinnunenccdaa042014-08-20 01:36:23 -0700152
kkinnunencabe20c2015-06-01 01:37:26 -0700153 void stencilPath(const StencilPathArgs& args, const GrPath* path) {
154 fGpu->handleDirtyContext();
155 this->onStencilPath(args, path);
156 }
157
stephana1dc17212016-04-25 07:01:22 -0700158 void drawPath(const GrPipeline& pipeline,
159 const GrPrimitiveProcessor& primProc,
160 const GrStencilSettings& stencil,
161 const GrPath* path) {
162 fGpu->handleDirtyContext();
163 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*fGpu->caps())) {
164 fGpu->xferBarrier(pipeline.getRenderTarget(), barrierType);
165 }
166 this->onDrawPath(pipeline, primProc, stencil, path);
167 }
168
egdaniel0e1853c2016-03-17 11:35:45 -0700169 void drawPaths(const GrPipeline& pipeline,
170 const GrPrimitiveProcessor& primProc,
171 const GrStencilSettings& stencil,
172 const GrPathRange* pathRange,
173 const void* indices,
174 PathIndexType indexType,
175 const float transformValues[],
176 PathTransformType transformType,
177 int count) {
kkinnunencabe20c2015-06-01 01:37:26 -0700178 fGpu->handleDirtyContext();
egdaniel0e1853c2016-03-17 11:35:45 -0700179 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*fGpu->caps())) {
180 fGpu->xferBarrier(pipeline.getRenderTarget(), barrierType);
bsalomoncb02b382015-08-12 11:14:50 -0700181 }
cdaltond1201052015-10-05 15:56:34 -0700182#ifdef SK_DEBUG
183 pathRange->assertPathsLoaded(indices, indexType, count);
184#endif
egdaniel0e1853c2016-03-17 11:35:45 -0700185 this->onDrawPaths(pipeline, primProc, stencil, pathRange, indices, indexType,
186 transformValues, transformType, count);
kkinnunencabe20c2015-06-01 01:37:26 -0700187 }
bsalomoncb02b382015-08-12 11:14:50 -0700188
kkinnunencabe20c2015-06-01 01:37:26 -0700189protected:
190 GrPathRendering(GrGpu* gpu)
191 : fGpu(gpu) {
192 }
193 virtual void onStencilPath(const StencilPathArgs&, const GrPath*) = 0;
stephana1dc17212016-04-25 07:01:22 -0700194 virtual void onDrawPath(const GrPipeline&,
195 const GrPrimitiveProcessor&,
196 const GrStencilSettings&,
197 const GrPath*) = 0;
egdaniel0e1853c2016-03-17 11:35:45 -0700198 virtual void onDrawPaths(const GrPipeline&,
199 const GrPrimitiveProcessor&,
200 const GrStencilSettings&,
201 const GrPathRange*,
202 const void* indices,
203 PathIndexType,
204 const float transformValues[],
205 PathTransformType,
206 int count) = 0;
kkinnunencabe20c2015-06-01 01:37:26 -0700207
208 GrGpu* fGpu;
kkinnunenccdaa042014-08-20 01:36:23 -0700209private:
210 GrPathRendering& operator=(const GrPathRendering&);
211};
212
213#endif