blob: ac56a9231ba109cf7a96cce76cf8136208aef6d1 [file] [log] [blame]
cdaltonc7103a12014-08-11 14:05:05 -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 GrGLPathRendering_DEFINED
9#define GrGLPathRendering_DEFINED
10
11#include "SkRefCnt.h"
kkinnunenccdaa042014-08-20 01:36:23 -070012#include "GrPathRendering.h"
13#include "GrStencil.h"
cdaltonc7103a12014-08-11 14:05:05 -070014#include "gl/GrGLFunctions.h"
kkinnunenccdaa042014-08-20 01:36:23 -070015#include "gl/GrGLProgram.h"
cdaltonc7103a12014-08-11 14:05:05 -070016
17class GrGLNameAllocator;
bsalomon861e1032014-12-16 07:33:49 -080018class GrGLGpu;
cdaltonc7103a12014-08-11 14:05:05 -070019
20/**
21 * This class wraps the NV_path_rendering extension and manages its various
bsalomon861e1032014-12-16 07:33:49 -080022 * API versions. If a method is not present in the GrGLInterface of the GrGLGpu
kkinnunenccdaa042014-08-20 01:36:23 -070023 * (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.
cdaltonc7103a12014-08-11 14:05:05 -070026 */
kkinnunenccdaa042014-08-20 01:36:23 -070027class GrGLPathRendering : public GrPathRendering {
cdaltonc7103a12014-08-11 14:05:05 -070028public:
29 /**
bsalomon861e1032014-12-16 07:33:49 -080030 * Create a new GrGLPathRendering object from a given GrGLGpu.
cdaltonc7103a12014-08-11 14:05:05 -070031 */
bsalomon861e1032014-12-16 07:33:49 -080032 GrGLPathRendering(GrGLGpu* gpu);
cdaltonc7103a12014-08-11 14:05:05 -070033 virtual ~GrGLPathRendering();
34
kkinnunenccdaa042014-08-20 01:36:23 -070035 // GrPathRendering implementations.
mtklein72c9faa2015-01-09 10:06:39 -080036 GrPath* createPath(const SkPath&, const SkStrokeRec&) SK_OVERRIDE;
cdalton855d83f2014-09-18 13:51:53 -070037 virtual GrPathRange* createPathRange(GrPathRange::PathGenerator*,
38 const SkStrokeRec&) SK_OVERRIDE;
39 virtual GrPathRange* createGlyphs(const SkTypeface*,
40 const SkDescriptor*,
41 const SkStrokeRec&) SK_OVERRIDE;
mtklein72c9faa2015-01-09 10:06:39 -080042 void stencilPath(const GrPath*, const GrStencilSettings&) SK_OVERRIDE;
43 void drawPath(const GrPath*, const GrStencilSettings&) SK_OVERRIDE;
cdalton55b24af2014-11-25 11:00:56 -080044 virtual void drawPaths(const GrPathRange*, const void* indices, PathIndexType,
45 const float transformValues[], PathTransformType, int count,
joshualitt92e496f2014-10-31 13:56:50 -070046 const GrStencilSettings&) SK_OVERRIDE;
kkinnunenccdaa042014-08-20 01:36:23 -070047
kkinnunenccdaa042014-08-20 01:36:23 -070048 /* Called when the 3D context state is unknown. */
49 void resetContext();
50
cdaltonc7103a12014-08-11 14:05:05 -070051 /**
52 * Called when the GPU resources have been lost and need to be abandoned
53 * (for example after a context loss).
54 */
55 void abandonGpuResources();
56
kkinnunen5b653572014-08-20 04:13:27 -070057
58 enum TexturingMode {
59 FixedFunction_TexturingMode,
60 SeparableShaders_TexturingMode
61 };
62
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
68 * available.
69 */
70 TexturingMode texturingMode() const {
71 return caps().fragmentInputGenSupport ?
72 SeparableShaders_TexturingMode : FixedFunction_TexturingMode;
73 }
74
75 // Functions for fixed function texturing support.
kkinnunenccdaa042014-08-20 01:36:23 -070076 enum PathTexGenComponents {
77 kS_PathTexGenComponents = 1,
78 kST_PathTexGenComponents = 2,
79 kSTR_PathTexGenComponents = 3
80 };
81 void enablePathTexGen(int unitIdx, PathTexGenComponents, const GrGLfloat* coefficients);
82 void enablePathTexGen(int unitIdx, PathTexGenComponents, const SkMatrix& matrix);
83 void flushPathTexGenSettings(int numUsedTexCoordSets);
kkinnunen5b653572014-08-20 04:13:27 -070084
85 // Functions for "separable shader" texturing support.
86 void setProgramPathFragmentInputTransform(GrGLuint program, GrGLint location,
87 GrGLenum genMode, GrGLint components,
88 const SkMatrix&);
89
90 /* Sets the projection matrix for path rendering */
kkinnunenccdaa042014-08-20 01:36:23 -070091 void setProjectionMatrix(const SkMatrix& matrix,
92 const SkISize& renderTargetSize,
93 GrSurfaceOrigin renderTargetOrigin);
94
cdaltonc7103a12014-08-11 14:05:05 -070095 GrGLuint genPaths(GrGLsizei range);
96 GrGLvoid deletePaths(GrGLuint path, GrGLsizei range);
kkinnunen5b653572014-08-20 04:13:27 -070097
98private:
99 /**
100 * Mark certain functionality as not supported if the driver version is too
101 * old and a backup implementation is not practical.
102 */
103 struct Caps {
104 bool stencilThenCoverSupport : 1;
105 bool fragmentInputGenSupport : 1;
cdalton149b3ec2014-09-17 09:19:18 -0700106 bool glyphLoadingSupport : 1;
kkinnunen5b653572014-08-20 04:13:27 -0700107 };
108 const Caps& caps() const { return fCaps; }
109
joshualitt92e496f2014-10-31 13:56:50 -0700110 void flushPathStencilSettings(const GrStencilSettings&);
cdaltonc7103a12014-08-11 14:05:05 -0700111
112 // NV_path_rendering v1.2
kkinnunen5b653572014-08-20 04:13:27 -0700113 void stencilThenCoverFillPath(GrGLuint path, GrGLenum fillMode,
114 GrGLuint mask, GrGLenum coverMode);
115
116 void stencilThenCoverStrokePath(GrGLuint path, GrGLint reference,
117 GrGLuint mask, GrGLenum coverMode);
118
119 void stencilThenCoverFillPathInstanced(
120 GrGLsizei numPaths, GrGLenum pathNameType, const GrGLvoid *paths,
cdaltonc7103a12014-08-11 14:05:05 -0700121 GrGLuint pathBase, GrGLenum fillMode, GrGLuint mask, GrGLenum coverMode,
122 GrGLenum transformType, const GrGLfloat *transformValues);
kkinnunen5b653572014-08-20 04:13:27 -0700123
124 void stencilThenCoverStrokePathInstanced(
cdaltonc7103a12014-08-11 14:05:05 -0700125 GrGLsizei numPaths, GrGLenum pathNameType, const GrGLvoid *paths,
126 GrGLuint pathBase, GrGLint reference, GrGLuint mask, GrGLenum coverMode,
127 GrGLenum transformType, const GrGLfloat *transformValues);
128
joshualittee2af952014-12-30 09:04:15 -0800129 struct MatrixState {
130 SkMatrix fViewMatrix;
131 SkISize fRenderTargetSize;
132 GrSurfaceOrigin fRenderTargetOrigin;
133
134 MatrixState() { this->invalidate(); }
135 void invalidate() {
136 fViewMatrix = SkMatrix::InvalidMatrix();
137 fRenderTargetSize.fWidth = -1;
138 fRenderTargetSize.fHeight = -1;
139 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
140 }
141
142 /**
143 * Gets a matrix that goes from local coordinates to GL normalized device coords.
144 */
145 template<int Size> void getRTAdjustedGLMatrix(GrGLfloat* destMatrix) {
146 SkMatrix combined;
147 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
148 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
149 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1,
150 0, 0, 1);
151 } else {
152 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
153 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1,
154 0, 0, 1);
155 }
156 combined.preConcat(fViewMatrix);
157 GrGLGetMatrix<Size>(destMatrix, combined);
158 }
159 };
160
bsalomon861e1032014-12-16 07:33:49 -0800161 GrGLGpu* fGpu;
cdaltonc7103a12014-08-11 14:05:05 -0700162 SkAutoTDelete<GrGLNameAllocator> fPathNameAllocator;
163 Caps fCaps;
joshualittee2af952014-12-30 09:04:15 -0800164 MatrixState fHWProjectionMatrixState;
kkinnunenccdaa042014-08-20 01:36:23 -0700165 GrStencilSettings fHWPathStencilSettings;
166 struct PathTexGenData {
167 GrGLenum fMode;
168 GrGLint fNumComponents;
169 GrGLfloat fCoefficients[3 * 3];
170 };
171 int fHWActivePathTexGenSets;
172 SkTArray<PathTexGenData, true> fHWPathTexGenSettings;
cdaltonc7103a12014-08-11 14:05:05 -0700173};
174
175#endif