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 "include/core/SkRefCnt.h" 12 #include "include/gpu/gl/GrGLTypes.h" 13 #include "src/gpu/GrGpu.h" 14 #include "src/gpu/GrPathRendering.h" 15 #include "src/gpu/GrStencilSettings.h" 16 #include "src/gpu/glsl/GrGLSLUtil.h" 17 18 class GrGLNameAllocator; 19 class GrGLGpu; 20 class GrStyle; 21 22 /** 23 * This class wraps the NV_path_rendering extension and manages its various 24 * API versions. If a method is not present in the GrGLInterface of the GrGLGpu 25 * (because the driver version is old), it tries to provide a backup 26 * implementation. But if a backup implementation is not practical, it marks the 27 * method as not supported. 28 */ 29 class GrGLPathRendering : public GrPathRendering { 30 public: 31 /** 32 * Create a new GrGLPathRendering object from a given GrGLGpu. 33 */ 34 GrGLPathRendering(GrGLGpu* gpu); 35 ~GrGLPathRendering() override; 36 37 // GrPathRendering implementations. 38 sk_sp<GrPath> createPath(const SkPath&, const GrStyle&) override; 39 40 /* Called when the 3D context state is unknown. */ 41 void resetContext(); 42 43 /** 44 * Called when the context either is about to be lost or is lost. DisconnectType indicates 45 * whether GPU resources should be cleaned up or abandoned when this is called. 46 */ 47 void disconnect(GrGpu::DisconnectType); 48 shouldBindFragmentInputs()49 bool shouldBindFragmentInputs() const { 50 return fCaps.bindFragmentInputSupport; 51 } 52 53 // Functions for "separable shader" texturing support. 54 void setProgramPathFragmentInputTransform(GrGLuint program, GrGLint location, 55 GrGLenum genMode, GrGLint components, 56 const SkMatrix&); 57 58 /* Sets the projection matrix for path rendering */ 59 void setProjectionMatrix(const SkMatrix& matrix, 60 const SkISize& renderTargetSize, 61 GrSurfaceOrigin renderTargetOrigin); 62 63 GrGLuint genPaths(GrGLsizei range); 64 GrGLvoid deletePaths(GrGLuint path, GrGLsizei range); 65 66 protected: 67 void onStencilPath(const StencilPathArgs&, const GrPath*) override; 68 void onDrawPath(GrRenderTarget*, GrSurfaceOrigin, 69 const GrPrimitiveProcessor&, 70 const GrPipeline&, 71 const GrPipeline::FixedDynamicState&, 72 const GrStencilSettings&, 73 const GrPath*) override; 74 75 private: 76 /** 77 * Mark certain functionality as not supported. 78 */ 79 struct Caps { 80 bool bindFragmentInputSupport : 1; 81 }; 82 83 void flushPathStencilSettings(const GrStencilSettings&); 84 85 struct MatrixState { 86 SkMatrix fViewMatrix; 87 SkISize fRenderTargetSize; 88 GrSurfaceOrigin fRenderTargetOrigin; 89 MatrixStateMatrixState90 MatrixState() { this->invalidate(); } invalidateMatrixState91 void invalidate() { 92 fViewMatrix = SkMatrix::InvalidMatrix(); 93 fRenderTargetSize.fWidth = -1; 94 fRenderTargetSize.fHeight = -1; 95 fRenderTargetOrigin = (GrSurfaceOrigin) -1; 96 } 97 98 /** 99 * Gets a matrix that goes from local coordinates to GL normalized device coords. 100 */ getRTAdjustedGLMatrixMatrixState101 template<int Size> void getRTAdjustedGLMatrix(float* destMatrix) { 102 SkMatrix combined; 103 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { 104 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1, 105 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1, 106 0, 0, 1); 107 } else { 108 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1, 109 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1, 110 0, 0, 1); 111 } 112 combined.preConcat(fViewMatrix); 113 GrGLSLGetMatrix<Size>(destMatrix, combined); 114 } 115 }; 116 GrGLGpu* gpu(); 117 118 GrGLuint fFirstPreallocatedPathID; 119 GrGLsizei fPreallocatedPathCount; 120 MatrixState fHWProjectionMatrixState; 121 GrStencilSettings fHWPathStencilSettings; 122 Caps fCaps; 123 }; 124 125 #endif 126