imageblurtiled.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkFont.h"
  10. #include "include/core/SkImageFilter.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkRect.h"
  13. #include "include/core/SkScalar.h"
  14. #include "include/core/SkSize.h"
  15. #include "include/core/SkString.h"
  16. #include "include/core/SkTypeface.h"
  17. #include "include/core/SkTypes.h"
  18. #include "include/effects/SkBlurImageFilter.h"
  19. #include "tools/ToolUtils.h"
  20. #define WIDTH 640
  21. #define HEIGHT 480
  22. namespace skiagm {
  23. class ImageBlurTiledGM : public GM {
  24. public:
  25. ImageBlurTiledGM(SkScalar sigmaX, SkScalar sigmaY)
  26. : fSigmaX(sigmaX), fSigmaY(sigmaY) {
  27. }
  28. protected:
  29. SkString onShortName() override {
  30. return SkString("imageblurtiled");
  31. }
  32. SkISize onISize() override {
  33. return SkISize::Make(WIDTH, HEIGHT);
  34. }
  35. void onDraw(SkCanvas* canvas) override {
  36. SkPaint paint;
  37. paint.setImageFilter(SkBlurImageFilter::Make(fSigmaX, fSigmaY, nullptr));
  38. const SkScalar tileSize = SkIntToScalar(128);
  39. SkRect bounds = canvas->getLocalClipBounds();
  40. for (SkScalar y = bounds.top(); y < bounds.bottom(); y += tileSize) {
  41. for (SkScalar x = bounds.left(); x < bounds.right(); x += tileSize) {
  42. canvas->save();
  43. canvas->clipRect(SkRect::MakeXYWH(x, y, tileSize, tileSize));
  44. canvas->saveLayer(nullptr, &paint);
  45. const char* str[] = {
  46. "The quick",
  47. "brown fox",
  48. "jumped over",
  49. "the lazy dog.",
  50. };
  51. SkFont font(ToolUtils::create_portable_typeface(), 100);
  52. int posY = 0;
  53. for (unsigned i = 0; i < SK_ARRAY_COUNT(str); i++) {
  54. posY += 100;
  55. canvas->drawString(str[i], 0, SkIntToScalar(posY), font, SkPaint());
  56. }
  57. canvas->restore();
  58. canvas->restore();
  59. }
  60. }
  61. }
  62. private:
  63. SkScalar fSigmaX;
  64. SkScalar fSigmaY;
  65. typedef GM INHERITED;
  66. };
  67. //////////////////////////////////////////////////////////////////////////////
  68. DEF_GM(return new ImageBlurTiledGM(3.0f, 3.0f);)
  69. }