blob: 638af054b1533f340ee6f22c765675688d1f34f0 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
reed@google.combf0001d2014-01-13 14:53:55 +00007
tfarinabcbc1782014-06-18 14:32:48 -07008#include "gm.h"
9
10#include "Resources.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SampleCode.h"
tfarinabcbc1782014-06-18 14:32:48 -070012#include "SkCanvas.h"
13#include "SkColorFilter.h"
14#include "SkColorPriv.h"
halcanary@google.come2380572013-12-18 15:08:35 +000015#include "SkData.h"
reed5965c8a2015-01-07 18:04:45 -080016#include "SkImageGenerator.h"
reed@android.comcb608442009-12-04 21:32:27 +000017#include "SkDumpCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkGradientShader.h"
19#include "SkGraphics.h"
20#include "SkImageDecoder.h"
halcanary@google.come2380572013-12-18 15:08:35 +000021#include "SkOSFile.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkPath.h"
23#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000024#include "SkPictureRecorder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025#include "SkRandom.h"
26#include "SkRegion.h"
27#include "SkShader.h"
tfarinabcbc1782014-06-18 14:32:48 -070028#include "SkStream.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000029#include "SkTime.h"
30#include "SkTypeface.h"
tfarinabcbc1782014-06-18 14:32:48 -070031#include "SkUtils.h"
32#include "SkView.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000033#include "SkXMLParser.h"
tfarinabcbc1782014-06-18 14:32:48 -070034#include "SkXfermode.h"
reed@android.com7d970c72010-04-22 16:07:49 +000035
halcanary@google.come2380572013-12-18 15:08:35 +000036///////////////////////////////////////////////////////////////////////////////
reed@android.comc6ddc112009-11-10 15:54:55 +000037
38static SkBitmap load_bitmap() {
reed@android.comc6ddc112009-11-10 15:54:55 +000039 SkBitmap bm;
tfarinac846f4a2014-07-01 12:35:49 -070040 SkString pngFilename = GetResourcePath("mandrill_512.png");
41 SkAutoDataUnref data(SkData::NewFromFileName(pngFilename.c_str()));
halcanary@google.come2380572013-12-18 15:08:35 +000042 if (data.get() != NULL) {
reed5965c8a2015-01-07 18:04:45 -080043 SkInstallDiscardablePixelRef(data, &bm);
reed@android.comc6ddc112009-11-10 15:54:55 +000044 }
45 return bm;
46}
47
reed@android.com8a1c16f2008-12-17 15:59:43 +000048static void drawCircle(SkCanvas* canvas, int r, SkColor color) {
49 SkPaint paint;
50 paint.setAntiAlias(true);
51 paint.setColor(color);
52
53 canvas->drawCircle(SkIntToScalar(r), SkIntToScalar(r), SkIntToScalar(r),
54 paint);
55}
56
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000057class PictureView : public SampleView {
reed@android.comc6ddc112009-11-10 15:54:55 +000058 SkBitmap fBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +000059public:
halcanary@google.come2380572013-12-18 15:08:35 +000060 PictureView() {
reed@android.comc6ddc112009-11-10 15:54:55 +000061
62 fBitmap = load_bitmap();
63
robertphillips@google.com84b18c72014-04-13 19:09:42 +000064 SkPictureRecorder recorder;
65
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000066 recorder.beginRecording(100, 100, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000067 fSubPicture = recorder.endRecording();
68
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000069 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 SkPaint paint;
71 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
reed@android.comc6ddc112009-11-10 15:54:55 +000073 canvas->drawBitmap(fBitmap, 0, 0, NULL);
74
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 drawCircle(canvas, 50, SK_ColorBLACK);
robertphillips9b14f262014-06-04 05:40:44 -070076 canvas->drawPicture(fSubPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 canvas->translate(SkIntToScalar(50), 0);
robertphillips9b14f262014-06-04 05:40:44 -070078 canvas->drawPicture(fSubPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 canvas->translate(0, SkIntToScalar(50));
robertphillips9b14f262014-06-04 05:40:44 -070080 canvas->drawPicture(fSubPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 canvas->translate(SkIntToScalar(-50), 0);
robertphillips9b14f262014-06-04 05:40:44 -070082 canvas->drawPicture(fSubPicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000083
84 fPicture = recorder.endRecording();
85
skia.committer@gmail.com51997012014-04-14 03:04:57 +000086 // fPicture now has (4) references to fSubPicture. We can release our ref,
robertphillips@google.com84b18c72014-04-13 19:09:42 +000087 // and just unref fPicture in our destructor, and it will in turn take care of
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 // the other references to fSubPicture
89 fSubPicture->unref();
90 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000091
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 virtual ~PictureView() {
93 fPicture->unref();
94 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000095
reed@android.com8a1c16f2008-12-17 15:59:43 +000096protected:
97 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070098 bool onQuery(SkEvent* evt) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 if (SampleCode::TitleQ(*evt)) {
100 SampleCode::TitleR(evt, "Picture");
101 return true;
102 }
103 return this->INHERITED::onQuery(evt);
104 }
105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 void drawSomething(SkCanvas* canvas) {
107 SkPaint paint;
reed@android.comc6ddc112009-11-10 15:54:55 +0000108
109 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000110 canvas->scale(0.5f, 0.5f);
reed@android.comc6ddc112009-11-10 15:54:55 +0000111 canvas->drawBitmap(fBitmap, 0, 0, NULL);
112 canvas->restore();
113
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000115
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 paint.setColor(SK_ColorRED);
117 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
118 SkIntToScalar(40), paint);
119 paint.setColor(SK_ColorBLACK);
120 paint.setTextSize(SkIntToScalar(40));
121 canvas->drawText("Picture", 7, SkIntToScalar(50), SkIntToScalar(62),
122 paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000123
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 }
125
mtklein36352bf2015-03-25 18:17:31 -0700126 void onDrawContent(SkCanvas* canvas) override {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000127 this->drawSomething(canvas);
reed@android.com149e2f62009-05-22 14:39:03 +0000128
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000129 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000130 this->drawSomething(recorder.beginRecording(100, 100, NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000131 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 canvas->save();
134 canvas->translate(SkIntToScalar(300), SkIntToScalar(50));
135 canvas->scale(-SK_Scalar1, -SK_Scalar1);
136 canvas->translate(-SkIntToScalar(100), -SkIntToScalar(50));
robertphillips9b14f262014-06-04 05:40:44 -0700137 canvas->drawPicture(pict);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 canvas->restore();
139
140 canvas->save();
141 canvas->translate(SkIntToScalar(200), SkIntToScalar(150));
142 canvas->scale(SK_Scalar1, -SK_Scalar1);
143 canvas->translate(0, -SkIntToScalar(50));
robertphillips9b14f262014-06-04 05:40:44 -0700144 canvas->drawPicture(pict);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 canvas->save();
148 canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
149 canvas->scale(-SK_Scalar1, SK_Scalar1);
150 canvas->translate(-SkIntToScalar(100), 0);
robertphillips9b14f262014-06-04 05:40:44 -0700151 canvas->drawPicture(pict);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 canvas->restore();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000154
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155private:
156 #define INVAL_ALL_TYPE "inval-all"
rmistry@google.comae933ce2012-08-23 18:19:56 +0000157
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 void delayInval(SkMSec delay) {
reed@google.com87fac4a2011-08-04 13:50:17 +0000159 (new SkEvent(INVAL_ALL_TYPE, this->getSinkID()))->postDelay(delay);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000161
mtklein36352bf2015-03-25 18:17:31 -0700162 bool onEvent(const SkEvent& evt) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 if (evt.isType(INVAL_ALL_TYPE)) {
164 this->inval(NULL);
165 return true;
166 }
167 return this->INHERITED::onEvent(evt);
168 }
169
170 SkPicture* fPicture;
171 SkPicture* fSubPicture;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000173 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174};
175
176//////////////////////////////////////////////////////////////////////////////
177
178static SkView* MyFactory() { return new PictureView; }
179static SkViewRegister reg(MyFactory);