drawlooper.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "gm/gm.h"
  8. #include "include/core/SkBlendMode.h"
  9. #include "include/core/SkBlurTypes.h"
  10. #include "include/core/SkCanvas.h"
  11. #include "include/core/SkColor.h"
  12. #include "include/core/SkDrawLooper.h"
  13. #include "include/core/SkFont.h"
  14. #include "include/core/SkMaskFilter.h"
  15. #include "include/core/SkPaint.h"
  16. #include "include/core/SkPoint.h"
  17. #include "include/core/SkRefCnt.h"
  18. #include "include/core/SkScalar.h"
  19. #include "include/core/SkSize.h"
  20. #include "include/core/SkString.h"
  21. #include "include/core/SkTypeface.h"
  22. #include "include/core/SkTypes.h"
  23. #include "include/effects/SkLayerDrawLooper.h"
  24. #include "src/core/SkBlurMask.h"
  25. #include "tools/ToolUtils.h"
  26. #ifdef SK_SUPPORT_LEGACY_DRAWLOOPER
  27. #define WIDTH 200
  28. #define HEIGHT 200
  29. class DrawLooperGM : public skiagm::GM {
  30. public:
  31. DrawLooperGM() : fLooper(nullptr) {
  32. this->setBGColor(0xFFDDDDDD);
  33. }
  34. protected:
  35. virtual SkISize onISize() override {
  36. return SkISize::Make(520, 160);
  37. }
  38. SkString onShortName() override {
  39. return SkString("drawlooper");
  40. }
  41. void onDraw(SkCanvas* canvas) override {
  42. this->init();
  43. SkPaint paint;
  44. paint.setAntiAlias(true);
  45. paint.setLooper(fLooper);
  46. SkFont font(ToolUtils::create_portable_typeface(), 72);
  47. canvas->drawCircle(50, 50, 30, paint);
  48. canvas->drawRect({ 150, 50, 200, 100 }, paint);
  49. canvas->drawString("Looper", 230, 100, font, paint);
  50. }
  51. private:
  52. sk_sp<SkDrawLooper> fLooper;
  53. void init() {
  54. if (fLooper) return;
  55. constexpr struct {
  56. SkColor fColor;
  57. SkPaint::Style fStyle;
  58. SkScalar fWidth;
  59. SkScalar fOffset;
  60. SkScalar fBlur;
  61. } gParams[] = {
  62. { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 },
  63. { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 },
  64. { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 },
  65. { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), SkIntToScalar(3) }
  66. };
  67. SkLayerDrawLooper::Builder looperBuilder;
  68. SkLayerDrawLooper::LayerInfo info;
  69. info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit;
  70. info.fColorMode = SkBlendMode::kSrc;
  71. for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
  72. info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
  73. SkPaint* paint = looperBuilder.addLayer(info);
  74. paint->setColor(gParams[i].fColor);
  75. paint->setStyle(gParams[i].fStyle);
  76. paint->setStrokeWidth(gParams[i].fWidth);
  77. if (gParams[i].fBlur > 0) {
  78. paint->setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
  79. SkBlurMask::ConvertRadiusToSigma(gParams[i].fBlur)));
  80. }
  81. }
  82. fLooper = looperBuilder.detach();
  83. }
  84. typedef GM INHERITED;
  85. };
  86. //////////////////////////////////////////////////////////////////////////////
  87. DEF_GM( return new DrawLooperGM; )
  88. #endif