backdrop.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2019 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/SkColor.h"
  10. #include "include/core/SkImageFilter.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkPictureRecorder.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkRefCnt.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkShader.h"
  17. #include "include/core/SkTypes.h"
  18. #include "include/effects/SkBlurImageFilter.h"
  19. #include "include/effects/SkGradientShader.h"
  20. #include <initializer_list>
  21. // Make a noisy (with hard-edges) background, so we can see the effect of the blur
  22. //
  23. static sk_sp<SkShader> make_shader(SkScalar cx, SkScalar cy, SkScalar rad) {
  24. const SkColor colors[] = {
  25. SK_ColorRED, SK_ColorRED, SK_ColorBLUE, SK_ColorBLUE, SK_ColorGREEN, SK_ColorGREEN,
  26. SK_ColorRED, SK_ColorRED, SK_ColorBLUE, SK_ColorBLUE, SK_ColorGREEN, SK_ColorGREEN,
  27. };
  28. constexpr int count = SK_ARRAY_COUNT(colors);
  29. SkScalar pos[count] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6 };
  30. for (int i = 0; i < count; ++i) {
  31. pos[i] *= 1.0f/6;
  32. }
  33. return SkGradientShader::MakeSweep(cx, cy, colors, pos, count);
  34. }
  35. static void do_draw(SkCanvas* canvas, bool useClip, bool useHintRect) {
  36. SkAutoCanvasRestore acr(canvas, true);
  37. canvas->clipRect({0, 0, 256, 256});
  38. const SkScalar cx = 128, cy = 128, rad = 100;
  39. SkPaint p;
  40. p.setShader(make_shader(cx, cy, rad));
  41. p.setAntiAlias(true);
  42. canvas->drawCircle(cx, cy, rad, p);
  43. // now setup a saveLayer that will pull in the backdrop and blur it
  44. //
  45. const SkRect r = {cx-50, cy-50, cx+50, cy+50};
  46. const SkRect* drawrptr = useHintRect ? &r : nullptr;
  47. const SkScalar sigma = 10;
  48. if (useClip) {
  49. canvas->clipRect(r);
  50. }
  51. auto blur = SkBlurImageFilter::Make(sigma, sigma, nullptr);
  52. auto rec = SkCanvas::SaveLayerRec(drawrptr, nullptr, blur.get(), 0);
  53. canvas->saveLayer(rec);
  54. // draw something inside, just to demonstrate that we don't blur the new contents,
  55. // just the backdrop.
  56. p.setColor(SK_ColorYELLOW);
  57. p.setShader(nullptr);
  58. canvas->drawCircle(cx, cy, 30, p);
  59. canvas->restore();
  60. }
  61. /*
  62. * Draws a 2x4 grid of sweep circles.
  63. * - for a given row, each col should be identical (canvas, picture)
  64. * - row:0 no-hint-rect no-clip-rect expect big blur (except inner circle)
  65. * - row:1 no-hint-rect clip-rect expect small blur (except inner circle)
  66. * - row:2 hint-rect no-clip-rect expect big blur (except inner circle)
  67. * - row:3 hint-rect clip-rect expect small blur (except inner circle)
  68. *
  69. * The test is that backdrop effects should be independent of the hint-rect, but should
  70. * respect the clip-rect.
  71. */
  72. DEF_SIMPLE_GM(backdrop_hintrect_clipping, canvas, 512, 1024) {
  73. for (bool useHintRect : {false, true}) {
  74. for (bool useClip : {false, true}) {
  75. canvas->save();
  76. do_draw(canvas, useClip, useHintRect);
  77. SkPictureRecorder rec;
  78. do_draw(rec.beginRecording(256, 256), useClip, useHintRect);
  79. canvas->translate(256, 0);
  80. canvas->drawPicture(rec.finishRecordingAsPicture());
  81. canvas->restore();
  82. canvas->translate(0, 256);
  83. }
  84. }
  85. }