shadows.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/SkBitmap.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkDrawLooper.h"
  12. #include "include/core/SkImageInfo.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkPath.h"
  15. #include "include/core/SkRect.h"
  16. #include "include/core/SkRefCnt.h"
  17. #include "include/core/SkScalar.h"
  18. #include "include/core/SkShader.h"
  19. #include "include/core/SkSize.h"
  20. #include "include/core/SkString.h"
  21. #include "include/core/SkTileMode.h"
  22. #include "include/core/SkTypes.h"
  23. #include "include/effects/SkBlurDrawLooper.h"
  24. #include "src/core/SkBlurMask.h"
  25. #ifdef SK_SUPPORT_LEGACY_DRAWLOOPER
  26. namespace skiagm {
  27. ///////////////////////////////////////////////////////////////////////////////
  28. static void setup(SkPaint* paint, SkColor c, SkScalar strokeWidth) {
  29. paint->setColor(c);
  30. if (strokeWidth < 0) {
  31. paint->setStyle(SkPaint::kFill_Style);
  32. } else {
  33. paint->setStyle(SkPaint::kStroke_Style);
  34. paint->setStrokeWidth(strokeWidth);
  35. }
  36. }
  37. class ShadowsGM : public GM {
  38. public:
  39. SkPath fCirclePath;
  40. SkRect fRect;
  41. SkBitmap fBitmap;
  42. protected:
  43. void onOnceBeforeDraw() override {
  44. this->setBGColor(0xFFDDDDDD);
  45. fCirclePath.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(10) );
  46. fRect.set(SkIntToScalar(10), SkIntToScalar(10),
  47. SkIntToScalar(30), SkIntToScalar(30));
  48. fBitmap.allocPixels(SkImageInfo::Make(20, 20, SkColorType::kAlpha_8_SkColorType,
  49. kPremul_SkAlphaType));
  50. SkCanvas canvas(fBitmap);
  51. canvas.clear(0x0);
  52. SkPaint p;
  53. canvas.drawRect(SkRect::MakeXYWH(10, 0, 10, 10), p);
  54. canvas.drawRect(SkRect::MakeXYWH(0, 10, 10, 10), p);
  55. }
  56. SkString onShortName() override {
  57. return SkString("shadows");
  58. }
  59. SkISize onISize() override {
  60. return SkISize::Make(200, 200);
  61. }
  62. void onDraw(SkCanvas* canvas) override {
  63. sk_sp<SkDrawLooper> shadowLoopers[] = {
  64. SkBlurDrawLooper::Make(SK_ColorBLUE,
  65. SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
  66. SkIntToScalar(5), SkIntToScalar(10)),
  67. SkBlurDrawLooper::Make(SK_ColorBLUE,
  68. SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
  69. SkIntToScalar(5), SkIntToScalar(10)),
  70. SkBlurDrawLooper::Make(SK_ColorBLACK,
  71. SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
  72. SkIntToScalar(5),
  73. SkIntToScalar(10)),
  74. SkBlurDrawLooper::Make(0x7FFF0000,
  75. SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
  76. SkIntToScalar(-5), SkIntToScalar(-10)),
  77. SkBlurDrawLooper::Make(SK_ColorBLACK, SkIntToScalar(0),
  78. SkIntToScalar(5), SkIntToScalar(5)),
  79. };
  80. constexpr struct {
  81. SkColor fColor;
  82. SkScalar fStrokeWidth;
  83. } gRec[] = {
  84. { SK_ColorRED, -SK_Scalar1 },
  85. { SK_ColorGREEN, SkIntToScalar(4) },
  86. { SK_ColorBLUE, SkIntToScalar(0)},
  87. };
  88. SkPaint paint;
  89. paint.setAntiAlias(true);
  90. for (size_t i = 0; i < SK_ARRAY_COUNT(shadowLoopers); ++i) {
  91. SkAutoCanvasRestore acr(canvas, true);
  92. paint.setLooper(shadowLoopers[i]);
  93. canvas->translate(SkIntToScalar((unsigned int)i*40), SkIntToScalar(0));
  94. setup(&paint, gRec[0].fColor, gRec[0].fStrokeWidth);
  95. canvas->drawRect(fRect, paint);
  96. canvas->translate(SkIntToScalar(0), SkIntToScalar(40));
  97. setup(&paint, gRec[1].fColor, gRec[1].fStrokeWidth);
  98. canvas->drawPath(fCirclePath, paint);
  99. canvas->translate(SkIntToScalar(0), SkIntToScalar(40));
  100. setup(&paint, gRec[2].fColor, gRec[2].fStrokeWidth);
  101. canvas->drawPath(fCirclePath, paint);
  102. // see bug.skia.org/562 (reference, draws correct)
  103. canvas->translate(0, 40);
  104. paint.setColor(SK_ColorBLACK);
  105. canvas->drawBitmap(fBitmap, 10, 10, &paint);
  106. canvas->translate(0, 40);
  107. paint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat));
  108. // see bug.skia.org/562 (shows bug as reported)
  109. paint.setStyle(SkPaint::kFill_Style);
  110. canvas->drawRect(SkRect::MakeXYWH(10, 10, 20, 20), paint);
  111. paint.setShader(nullptr);
  112. }
  113. }
  114. private:
  115. typedef GM INHERITED;
  116. };
  117. ///////////////////////////////////////////////////////////////////////////////
  118. DEF_GM( return new ShadowsGM; )
  119. }
  120. #endif