2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrGLPathRendering_DEFINED
9 #define GrGLPathRendering_DEFINED
12 #include "GrPathRendering.h"
13 #include "GrStencil.h"
14 #include "gl/GrGLFunctions.h"
15 #include "gl/GrGLProgram.h"
17 class GrGLNameAllocator;
21 * This class wraps the NV_path_rendering extension and manages its various
22 * API versions. If a method is not present in the GrGLInterface of the GrGLGpu
23 * (because the driver version is old), it tries to provide a backup
24 * implementation. But if a backup implementation is not practical, it marks the
25 * method as not supported.
27 class GrGLPathRendering : public GrPathRendering {
30 * Create a new GrGLPathRendering object from a given GrGLGpu.
32 GrGLPathRendering(GrGLGpu* gpu);
33 virtual ~GrGLPathRendering();
35 // GrPathRendering implementations.
36 GrPath* createPath(const SkPath&, const SkStrokeRec&) SK_OVERRIDE;
37 virtual GrPathRange* createPathRange(GrPathRange::PathGenerator*,
38 const SkStrokeRec&) SK_OVERRIDE;
39 virtual GrPathRange* createGlyphs(const SkTypeface*,
41 const SkStrokeRec&) SK_OVERRIDE;
42 void stencilPath(const GrPath*, const GrStencilSettings&) SK_OVERRIDE;
43 void drawPath(const GrPath*, const GrStencilSettings&) SK_OVERRIDE;
44 virtual void drawPaths(const GrPathRange*, const void* indices, PathIndexType,
45 const float transformValues[], PathTransformType, int count,
46 const GrStencilSettings&) SK_OVERRIDE;
48 /* Called when the 3D context state is unknown. */
52 * Called when the GPU resources have been lost and need to be abandoned
53 * (for example after a context loss).
55 void abandonGpuResources();
59 FixedFunction_TexturingMode,
60 SeparableShaders_TexturingMode
63 /** Specifies whether texturing should use fixed fuction pipe or separable shaders
64 * Specifies whether texturing should use fixed fuction pipe or whether
65 * it is ok to use normal vertex and fragment shaders, and for path rendering
66 * populate fragment shaders with setProgramPathFragmentInputTransform.
67 * The fixed function mode will be removed once the other mode is more widely
70 TexturingMode texturingMode() const {
71 return caps().fragmentInputGenSupport ?
72 SeparableShaders_TexturingMode : FixedFunction_TexturingMode;
75 // Functions for fixed function texturing support.
76 enum PathTexGenComponents {
77 kS_PathTexGenComponents = 1,
78 kST_PathTexGenComponents = 2,
79 kSTR_PathTexGenComponents = 3
81 void enablePathTexGen(int unitIdx, PathTexGenComponents, const GrGLfloat* coefficients);
82 void enablePathTexGen(int unitIdx, PathTexGenComponents, const SkMatrix& matrix);
83 void flushPathTexGenSettings(int numUsedTexCoordSets);
85 // Functions for "separable shader" texturing support.
86 void setProgramPathFragmentInputTransform(GrGLuint program, GrGLint location,
87 GrGLenum genMode, GrGLint components,
90 /* Sets the projection matrix for path rendering */
91 void setProjectionMatrix(const SkMatrix& matrix,
92 const SkISize& renderTargetSize,
93 GrSurfaceOrigin renderTargetOrigin);
95 GrGLuint genPaths(GrGLsizei range);
96 GrGLvoid deletePaths(GrGLuint path, GrGLsizei range);
100 * Mark certain functionality as not supported if the driver version is too
101 * old and a backup implementation is not practical.
104 bool stencilThenCoverSupport : 1;
105 bool fragmentInputGenSupport : 1;
106 bool glyphLoadingSupport : 1;
108 const Caps& caps() const { return fCaps; }
110 void flushPathStencilSettings(const GrStencilSettings&);
112 // NV_path_rendering v1.2
113 void stencilThenCoverFillPath(GrGLuint path, GrGLenum fillMode,
114 GrGLuint mask, GrGLenum coverMode);
116 void stencilThenCoverStrokePath(GrGLuint path, GrGLint reference,
117 GrGLuint mask, GrGLenum coverMode);
119 void stencilThenCoverFillPathInstanced(
120 GrGLsizei numPaths, GrGLenum pathNameType, const GrGLvoid *paths,
121 GrGLuint pathBase, GrGLenum fillMode, GrGLuint mask, GrGLenum coverMode,
122 GrGLenum transformType, const GrGLfloat *transformValues);
124 void stencilThenCoverStrokePathInstanced(
125 GrGLsizei numPaths, GrGLenum pathNameType, const GrGLvoid *paths,
126 GrGLuint pathBase, GrGLint reference, GrGLuint mask, GrGLenum coverMode,
127 GrGLenum transformType, const GrGLfloat *transformValues);
130 SkMatrix fViewMatrix;
131 SkISize fRenderTargetSize;
132 GrSurfaceOrigin fRenderTargetOrigin;
134 MatrixState() { this->invalidate(); }
136 fViewMatrix = SkMatrix::InvalidMatrix();
137 fRenderTargetSize.fWidth = -1;
138 fRenderTargetSize.fHeight = -1;
139 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
143 * Gets a matrix that goes from local coordinates to GL normalized device coords.
145 template<int Size> void getRTAdjustedGLMatrix(GrGLfloat* destMatrix) {
147 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
148 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
149 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1,
152 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
153 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1,
156 combined.preConcat(fViewMatrix);
157 GrGLGetMatrix<Size>(destMatrix, combined);
162 SkAutoTDelete<GrGLNameAllocator> fPathNameAllocator;
164 MatrixState fHWProjectionMatrixState;
165 GrStencilSettings fHWPathStencilSettings;
166 struct PathTexGenData {
168 GrGLint fNumComponents;
169 GrGLfloat fCoefficients[3 * 3];
171 int fHWActivePathTexGenSets;
172 SkTArray<PathTexGenData, true> fHWPathTexGenSettings;