distantclip.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright 2012 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/SkPath.h"
  11. #include "include/core/SkPicture.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/SkSize.h"
  17. #include "include/core/SkString.h"
  18. namespace skiagm {
  19. class DistantClipGM : public GM {
  20. SkString onShortName() override { return SkString("distantclip"); }
  21. SkISize onISize() override { return {100, 100}; }
  22. void onDraw(SkCanvas* canvas) override {
  23. constexpr SkScalar kOffset = 35000.0f;
  24. constexpr SkScalar kExtents = 1000.0f;
  25. SkPictureRecorder recorder;
  26. // We record a picture of huge vertical extents in which we clear the canvas to red, create
  27. // a 'extents' by 'extents' round rect clip at a vertical offset of 'offset', then draw
  28. // green into that.
  29. SkCanvas* rec = recorder.beginRecording(kExtents, kOffset + kExtents, nullptr, 0);
  30. rec->drawColor(SK_ColorRED);
  31. rec->save();
  32. SkRect r = SkRect::MakeXYWH(-kExtents, kOffset - kExtents, 2 * kExtents, 2 * kExtents);
  33. SkPath p;
  34. p.addRoundRect(r, 5, 5);
  35. rec->clipPath(p, true);
  36. rec->drawColor(SK_ColorGREEN);
  37. rec->restore();
  38. sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
  39. // Next we play that picture into another picture of the same size.
  40. pict->playback(recorder.beginRecording(pict->cullRect().width(),
  41. pict->cullRect().height(),
  42. nullptr, 0));
  43. sk_sp<SkPicture> pict2(recorder.finishRecordingAsPicture());
  44. // Finally we play the part of that second picture that should be green into the canvas.
  45. canvas->save();
  46. canvas->translate(kExtents / 2, -(kOffset - kExtents / 2));
  47. pict2->playback(canvas);
  48. canvas->restore();
  49. // If the image is red, we erroneously decided the clipPath was empty and didn't record
  50. // the green drawColor, if it's green we're all good.
  51. }
  52. };
  53. ///////////////////////////////////////////////////////////////////////////////
  54. DEF_GM( return new DistantClipGM; )
  55. }