cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 1 | /* |
| 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" |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 12 | #include "GrPathRendering.h" |
| 13 | #include "GrStencil.h" |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 14 | #include "gl/GrGLFunctions.h" |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 15 | #include "gl/GrGLProgram.h" |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 16 | |
| 17 | class GrGLNameAllocator; |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 18 | class GrGLGpu; |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * This class wraps the NV_path_rendering extension and manages its various |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 22 | * API versions. If a method is not present in the GrGLInterface of the GrGLGpu |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 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. |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 26 | */ |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 27 | class GrGLPathRendering : public GrPathRendering { |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 28 | public: |
| 29 | /** |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 30 | * Create a new GrGLPathRendering object from a given GrGLGpu. |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 31 | */ |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 32 | GrGLPathRendering(GrGLGpu* gpu); |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 33 | virtual ~GrGLPathRendering(); |
| 34 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 35 | // GrPathRendering implementations. |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 36 | GrPath* createPath(const SkPath&, const SkStrokeRec&) SK_OVERRIDE; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 37 | virtual GrPathRange* createPathRange(GrPathRange::PathGenerator*, |
| 38 | const SkStrokeRec&) SK_OVERRIDE; |
| 39 | virtual GrPathRange* createGlyphs(const SkTypeface*, |
| 40 | const SkDescriptor*, |
| 41 | const SkStrokeRec&) SK_OVERRIDE; |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 42 | void stencilPath(const GrPath*, const GrStencilSettings&) SK_OVERRIDE; |
| 43 | void drawPath(const GrPath*, const GrStencilSettings&) SK_OVERRIDE; |
cdalton | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame] | 44 | virtual void drawPaths(const GrPathRange*, const void* indices, PathIndexType, |
| 45 | const float transformValues[], PathTransformType, int count, |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 46 | const GrStencilSettings&) SK_OVERRIDE; |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 47 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 48 | /* Called when the 3D context state is unknown. */ |
| 49 | void resetContext(); |
| 50 | |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 51 | /** |
| 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 | |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 57 | |
| 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. |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 76 | 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); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 84 | |
| 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 */ |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 91 | void setProjectionMatrix(const SkMatrix& matrix, |
| 92 | const SkISize& renderTargetSize, |
| 93 | GrSurfaceOrigin renderTargetOrigin); |
| 94 | |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 95 | GrGLuint genPaths(GrGLsizei range); |
| 96 | GrGLvoid deletePaths(GrGLuint path, GrGLsizei range); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 97 | |
| 98 | private: |
| 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; |
cdalton | 149b3ec | 2014-09-17 09:19:18 -0700 | [diff] [blame] | 106 | bool glyphLoadingSupport : 1; |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 107 | }; |
| 108 | const Caps& caps() const { return fCaps; } |
| 109 | |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 110 | void flushPathStencilSettings(const GrStencilSettings&); |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 111 | |
| 112 | // NV_path_rendering v1.2 |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 113 | 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, |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 121 | GrGLuint pathBase, GrGLenum fillMode, GrGLuint mask, GrGLenum coverMode, |
| 122 | GrGLenum transformType, const GrGLfloat *transformValues); |
kkinnunen | 5b65357 | 2014-08-20 04:13:27 -0700 | [diff] [blame] | 123 | |
| 124 | void stencilThenCoverStrokePathInstanced( |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 125 | GrGLsizei numPaths, GrGLenum pathNameType, const GrGLvoid *paths, |
| 126 | GrGLuint pathBase, GrGLint reference, GrGLuint mask, GrGLenum coverMode, |
| 127 | GrGLenum transformType, const GrGLfloat *transformValues); |
| 128 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 129 | 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 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 161 | GrGLGpu* fGpu; |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 162 | SkAutoTDelete<GrGLNameAllocator> fPathNameAllocator; |
| 163 | Caps fCaps; |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 164 | MatrixState fHWProjectionMatrixState; |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 165 | 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; |
cdalton | c7103a1 | 2014-08-11 14:05:05 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | #endif |