SampleEffects.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2011 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. #include "include/core/SkCanvas.h"
  8. #include "include/core/SkPaint.h"
  9. #include "include/effects/SkColorMatrixFilter.h"
  10. #include "include/effects/SkDiscretePathEffect.h"
  11. #include "include/effects/SkGradientShader.h"
  12. #include "samplecode/Sample.h"
  13. #include "src/core/SkBlurMask.h"
  14. #include "src/effects/SkEmbossMaskFilter.h"
  15. //#define COLOR 0xFFFF8844
  16. #define COLOR 0xFF888888
  17. static void paint_proc0(SkPaint*) {
  18. }
  19. static void paint_proc1(SkPaint* paint) {
  20. paint->setMaskFilter(SkMaskFilter::MakeBlur(
  21. kNormal_SkBlurStyle,
  22. SkBlurMask::ConvertRadiusToSigma(2)));
  23. }
  24. static void paint_proc2(SkPaint* paint) {
  25. paint->setMaskFilter(SkEmbossMaskFilter::Make(
  26. SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(1)),
  27. { { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 64, 16 }));
  28. }
  29. static void paint_proc3(SkPaint* paint) {
  30. SkColor colors[] = { SK_ColorRED, COLOR, SK_ColorBLUE };
  31. SkPoint pts[] = { { 3, 0 }, { 7, 5 } };
  32. paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
  33. SkTileMode::kMirror));
  34. }
  35. static void paint_proc5(SkPaint* paint) {
  36. paint_proc3(paint);
  37. paint_proc2(paint);
  38. }
  39. typedef void (*PaintProc)(SkPaint*);
  40. const PaintProc gPaintProcs[] = {
  41. paint_proc0,
  42. paint_proc1,
  43. paint_proc2,
  44. paint_proc3,
  45. paint_proc5,
  46. };
  47. ///////////////////////////////////////////////////////////////////////////////
  48. class EffectsView : public Sample {
  49. public:
  50. SkPath fPath;
  51. SkPaint fPaint[SK_ARRAY_COUNT(gPaintProcs)];
  52. EffectsView() {
  53. size_t i;
  54. const float pts[] = {
  55. 0, 0,
  56. 10, 0,
  57. 10, 5,
  58. 20, -5,
  59. 10, -15,
  60. 10, -10,
  61. 0, -10
  62. };
  63. fPath.moveTo(pts[0], pts[1]);
  64. for (i = 2; i < SK_ARRAY_COUNT(pts); i += 2) {
  65. fPath.lineTo(pts[i], pts[i+1]);
  66. }
  67. for (i = 0; i < SK_ARRAY_COUNT(gPaintProcs); i++) {
  68. fPaint[i].setAntiAlias(true);
  69. fPaint[i].setColor(COLOR);
  70. gPaintProcs[i](&fPaint[i]);
  71. }
  72. SkColorMatrix cm;
  73. cm.setRotate(SkColorMatrix::kG_Axis, 180);
  74. cm.setIdentity();
  75. this->setBGColor(0xFFDDDDDD);
  76. }
  77. protected:
  78. virtual SkString name() { return SkString("Effects"); }
  79. virtual void onDrawContent(SkCanvas* canvas) {
  80. canvas->scale(3, 3);
  81. canvas->translate(10, 30);
  82. for (size_t i = 0; i < SK_ARRAY_COUNT(fPaint); i++) {
  83. canvas->drawPath(fPath, fPaint[i]);
  84. canvas->translate(32, 0);
  85. }
  86. }
  87. private:
  88. typedef Sample INHERITED;
  89. };
  90. ///////////////////////////////////////////////////////////////////////////////
  91. DEF_SAMPLE( return new EffectsView(); )