blob: 0fdec1c3aa84222df5be54faaa965f4d4e86338d [file] [log] [blame]
reedd5fa1a42014-08-09 11:08:05 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkPaint.h"
10#include "include/core/SkPath.h"
11#include "include/core/SkPictureRecorder.h"
reedd5fa1a42014-08-09 11:08:05 -070012
reedca2622b2016-03-18 07:25:55 -070013static sk_sp<SkPicture> make_picture() {
reedd5fa1a42014-08-09 11:08:05 -070014 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);
halcanary9d524f22016-03-29 09:03:52 -070027
reedd5fa1a42014-08-09 11:08:05 -070028 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);
reed374772b2016-10-05 17:33:02 -070033 paint.setBlendMode(SkBlendMode::kPlus);
reedd5fa1a42014-08-09 11:08:05 -070034 canvas->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), paint);
35
reedca2622b2016-03-18 07:25:55 -070036 return rec.finishRecordingAsPicture();
reedd5fa1a42014-08-09 11:08:05 -070037}
38
39// Exercise the optional arguments to drawPicture
40//
41class PictureGM : public skiagm::GM {
42public:
caryclark63c684a2015-02-25 09:04:04 -080043 PictureGM()
halcanary96fcdcc2015-08-27 07:41:13 -070044 : fPicture(nullptr)
caryclark63c684a2015-02-25 09:04:04 -080045 {}
reedd5fa1a42014-08-09 11:08:05 -070046
47protected:
mtklein36352bf2015-03-25 18:17:31 -070048 void onOnceBeforeDraw() override {
reedca2622b2016-03-18 07:25:55 -070049 fPicture = make_picture();
caryclark63c684a2015-02-25 09:04:04 -080050 }
51
mtklein36352bf2015-03-25 18:17:31 -070052 SkString onShortName() override {
reedd5fa1a42014-08-09 11:08:05 -070053 return SkString("pictures");
54 }
55
mtklein36352bf2015-03-25 18:17:31 -070056 SkISize onISize() override {
reedd5fa1a42014-08-09 11:08:05 -070057 return SkISize::Make(450, 120);
58 }
59
mtklein36352bf2015-03-25 18:17:31 -070060 void onDraw(SkCanvas* canvas) override {
reedd5fa1a42014-08-09 11:08:05 -070061 canvas->translate(10, 10);
62
63 SkMatrix matrix;
64 SkPaint paint;
65
66 canvas->drawPicture(fPicture);
halcanary9d524f22016-03-29 09:03:52 -070067
reedd5fa1a42014-08-09 11:08:05 -070068 matrix.setTranslate(110, 0);
halcanary96fcdcc2015-08-27 07:41:13 -070069 canvas->drawPicture(fPicture, &matrix, nullptr);
halcanary9d524f22016-03-29 09:03:52 -070070
reedd5fa1a42014-08-09 11:08:05 -070071 matrix.postTranslate(110, 0);
72 canvas->drawPicture(fPicture, &matrix, &paint);
73
Mike Reed9407e242019-02-15 16:13:57 -050074 paint.setAlphaf(0.5f);
reedd5fa1a42014-08-09 11:08:05 -070075 matrix.postTranslate(110, 0);
76 canvas->drawPicture(fPicture, &matrix, &paint);
77 }
78
79private:
reedca2622b2016-03-18 07:25:55 -070080 sk_sp<SkPicture> fPicture;
reedd5fa1a42014-08-09 11:08:05 -070081
82 typedef skiagm::GM INHERITED;
83};
84
halcanary385fe4d2015-08-26 13:07:48 -070085DEF_GM(return new PictureGM;)