GrGLPathRendering.h 4.2 KB

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