picture.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/SkBlendMode.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkMatrix.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkPath.h"
  13. #include "include/core/SkPicture.h"
  14. #include "include/core/SkPictureRecorder.h"
  15. #include "include/core/SkRect.h"
  16. #include "include/core/SkRefCnt.h"
  17. #include "include/core/SkSize.h"
  18. #include "include/core/SkString.h"
  19. static sk_sp<SkPicture> make_picture() {
  20. SkPictureRecorder rec;
  21. SkCanvas* canvas = rec.beginRecording(100, 100);
  22. SkPaint paint;
  23. paint.setAntiAlias(true);
  24. SkPath path;
  25. paint.setColor(0x800000FF);
  26. canvas->drawRect(SkRect::MakeWH(100, 100), paint);
  27. paint.setColor(0x80FF0000);
  28. path.moveTo(0, 0); path.lineTo(100, 0); path.lineTo(100, 100);
  29. canvas->drawPath(path, paint);
  30. paint.setColor(0x8000FF00);
  31. path.reset(); path.moveTo(0, 0); path.lineTo(100, 0); path.lineTo(0, 100);
  32. canvas->drawPath(path, paint);
  33. paint.setColor(0x80FFFFFF);
  34. paint.setBlendMode(SkBlendMode::kPlus);
  35. canvas->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), paint);
  36. return rec.finishRecordingAsPicture();
  37. }
  38. // Exercise the optional arguments to drawPicture
  39. //
  40. class PictureGM : public skiagm::GM {
  41. public:
  42. PictureGM()
  43. : fPicture(nullptr)
  44. {}
  45. protected:
  46. void onOnceBeforeDraw() override {
  47. fPicture = make_picture();
  48. }
  49. SkString onShortName() override {
  50. return SkString("pictures");
  51. }
  52. SkISize onISize() override {
  53. return SkISize::Make(450, 120);
  54. }
  55. void onDraw(SkCanvas* canvas) override {
  56. canvas->translate(10, 10);
  57. SkMatrix matrix;
  58. SkPaint paint;
  59. canvas->drawPicture(fPicture);
  60. matrix.setTranslate(110, 0);
  61. canvas->drawPicture(fPicture, &matrix, nullptr);
  62. matrix.postTranslate(110, 0);
  63. canvas->drawPicture(fPicture, &matrix, &paint);
  64. paint.setAlphaf(0.5f);
  65. matrix.postTranslate(110, 0);
  66. canvas->drawPicture(fPicture, &matrix, &paint);
  67. }
  68. private:
  69. sk_sp<SkPicture> fPicture;
  70. typedef skiagm::GM INHERITED;
  71. };
  72. DEF_GM(return new PictureGM;)