GrConvexPolyEffect.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 GrConvexPolyEffect_DEFINED
  8. #define GrConvexPolyEffect_DEFINED
  9. #include "include/private/GrTypesPriv.h"
  10. #include "src/gpu/GrCaps.h"
  11. #include "src/gpu/GrFragmentProcessor.h"
  12. #include "src/gpu/GrProcessor.h"
  13. class GrInvariantOutput;
  14. class SkPath;
  15. /**
  16. * An effect that renders a convex polygon. It is intended to be used as a coverage effect.
  17. * Bounding geometry is rendered and the effect computes coverage based on the fragment's
  18. * position relative to the polygon.
  19. */
  20. class GrConvexPolyEffect : public GrFragmentProcessor {
  21. public:
  22. enum {
  23. kMaxEdges = 8,
  24. };
  25. /**
  26. * edges is a set of n edge equations where n is limited to kMaxEdges. It contains 3*n values.
  27. * The edges should form a convex polygon. The positive half-plane is considered to be the
  28. * inside. The equations should be normalized such that the first two coefficients are a unit
  29. * 2d vector.
  30. *
  31. * Currently the edges are specified in device space. In the future we may prefer to specify
  32. * them in src space. There are a number of ways this could be accomplished but we'd probably
  33. * have to modify the effect/shaderbuilder interface to make it possible (e.g. give access
  34. * to the view matrix or untransformed positions in the fragment shader).
  35. */
  36. static std::unique_ptr<GrFragmentProcessor> Make(GrClipEdgeType edgeType, int n,
  37. const SkScalar edges[]) {
  38. if (n <= 0 || n > kMaxEdges || GrClipEdgeType::kHairlineAA == edgeType) {
  39. return nullptr;
  40. }
  41. return std::unique_ptr<GrFragmentProcessor>(new GrConvexPolyEffect(edgeType, n, edges));
  42. }
  43. /**
  44. * Creates an effect that clips against the path. If the path is not a convex polygon, is
  45. * inverse filled, or has too many edges, this will return nullptr.
  46. */
  47. static std::unique_ptr<GrFragmentProcessor> Make(GrClipEdgeType, const SkPath&);
  48. /**
  49. * Creates an effect that fills inside the rect with AA edges..
  50. */
  51. static std::unique_ptr<GrFragmentProcessor> Make(GrClipEdgeType, const SkRect&);
  52. ~GrConvexPolyEffect() override;
  53. const char* name() const override { return "ConvexPoly"; }
  54. std::unique_ptr<GrFragmentProcessor> clone() const override;
  55. GrClipEdgeType getEdgeType() const { return fEdgeType; }
  56. int getEdgeCount() const { return fEdgeCount; }
  57. const SkScalar* getEdges() const { return fEdges; }
  58. private:
  59. GrConvexPolyEffect(GrClipEdgeType edgeType, int n, const SkScalar edges[]);
  60. GrConvexPolyEffect(const GrConvexPolyEffect&);
  61. GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
  62. void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
  63. bool onIsEqual(const GrFragmentProcessor& other) const override;
  64. GrClipEdgeType fEdgeType;
  65. int fEdgeCount;
  66. SkScalar fEdges[3 * kMaxEdges];
  67. GR_DECLARE_FRAGMENT_PROCESSOR_TEST
  68. typedef GrFragmentProcessor INHERITED;
  69. };
  70. #endif