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