blob: 46c57d7f398133167b839b629f50a822f758be21 [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"
16#include "SkDecodingImageGenerator.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) {
halcanary@google.com3d50ea12014-01-02 13:15:13 +000043 SkInstallDiscardablePixelRef(SkDecodingImageGenerator::Create(
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +000044 data, SkDecodingImageGenerator::Options()), &bm);
reed@android.comc6ddc112009-11-10 15:54:55 +000045 }
46 return bm;
47}
48
reed@android.com8a1c16f2008-12-17 15:59:43 +000049static void drawCircle(SkCanvas* canvas, int r, SkColor color) {
50 SkPaint paint;
51 paint.setAntiAlias(true);
52 paint.setColor(color);
53
54 canvas->drawCircle(SkIntToScalar(r), SkIntToScalar(r), SkIntToScalar(r),
55 paint);
56}
57
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000058class PictureView : public SampleView {
reed@android.comc6ddc112009-11-10 15:54:55 +000059 SkBitmap fBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +000060public:
halcanary@google.come2380572013-12-18 15:08:35 +000061 PictureView() {
reed@android.comc6ddc112009-11-10 15:54:55 +000062
63 fBitmap = load_bitmap();
64
robertphillips@google.com84b18c72014-04-13 19:09:42 +000065 SkPictureRecorder recorder;
66
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000067 recorder.beginRecording(100, 100, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000068 fSubPicture = recorder.endRecording();
69
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000070 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 SkPaint paint;
72 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +000073
reed@android.comc6ddc112009-11-10 15:54:55 +000074 canvas->drawBitmap(fBitmap, 0, 0, NULL);
75
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 drawCircle(canvas, 50, SK_ColorBLACK);
robertphillips9b14f262014-06-04 05:40:44 -070077 canvas->drawPicture(fSubPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 canvas->translate(SkIntToScalar(50), 0);
robertphillips9b14f262014-06-04 05:40:44 -070079 canvas->drawPicture(fSubPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 canvas->translate(0, SkIntToScalar(50));
robertphillips9b14f262014-06-04 05:40:44 -070081 canvas->drawPicture(fSubPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 canvas->translate(SkIntToScalar(-50), 0);
robertphillips9b14f262014-06-04 05:40:44 -070083 canvas->drawPicture(fSubPicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000084
85 fPicture = recorder.endRecording();
86
skia.committer@gmail.com51997012014-04-14 03:04:57 +000087 // fPicture now has (4) references to fSubPicture. We can release our ref,
robertphillips@google.com84b18c72014-04-13 19:09:42 +000088 // and just unref fPicture in our destructor, and it will in turn take care of
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 // the other references to fSubPicture
90 fSubPicture->unref();
91 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000092
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 virtual ~PictureView() {
94 fPicture->unref();
95 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
reed@android.com8a1c16f2008-12-17 15:59:43 +000097protected:
98 // overrides from SkEventSink
99 virtual bool onQuery(SkEvent* evt) {
100 if (SampleCode::TitleQ(*evt)) {
101 SampleCode::TitleR(evt, "Picture");
102 return true;
103 }
104 return this->INHERITED::onQuery(evt);
105 }
106
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 void drawSomething(SkCanvas* canvas) {
108 SkPaint paint;
reed@android.comc6ddc112009-11-10 15:54:55 +0000109
110 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000111 canvas->scale(0.5f, 0.5f);
reed@android.comc6ddc112009-11-10 15:54:55 +0000112 canvas->drawBitmap(fBitmap, 0, 0, NULL);
113 canvas->restore();
114
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 paint.setColor(SK_ColorRED);
118 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
119 SkIntToScalar(40), paint);
120 paint.setColor(SK_ColorBLACK);
121 paint.setTextSize(SkIntToScalar(40));
122 canvas->drawText("Picture", 7, SkIntToScalar(50), SkIntToScalar(62),
123 paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 }
126
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000127 virtual void onDrawContent(SkCanvas* canvas) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000128 this->drawSomething(canvas);
reed@android.com149e2f62009-05-22 14:39:03 +0000129
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000130 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000131 this->drawSomething(recorder.beginRecording(100, 100, NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000132 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 canvas->save();
135 canvas->translate(SkIntToScalar(300), SkIntToScalar(50));
136 canvas->scale(-SK_Scalar1, -SK_Scalar1);
137 canvas->translate(-SkIntToScalar(100), -SkIntToScalar(50));
robertphillips9b14f262014-06-04 05:40:44 -0700138 canvas->drawPicture(pict);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 canvas->restore();
140
141 canvas->save();
142 canvas->translate(SkIntToScalar(200), SkIntToScalar(150));
143 canvas->scale(SK_Scalar1, -SK_Scalar1);
144 canvas->translate(0, -SkIntToScalar(50));
robertphillips9b14f262014-06-04 05:40:44 -0700145 canvas->drawPicture(pict);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000147
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 canvas->save();
149 canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
150 canvas->scale(-SK_Scalar1, SK_Scalar1);
151 canvas->translate(-SkIntToScalar(100), 0);
robertphillips9b14f262014-06-04 05:40:44 -0700152 canvas->drawPicture(pict);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 canvas->restore();
reed@android.comcb608442009-12-04 21:32:27 +0000154
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000155#ifdef SK_DEVELOPER
reed@android.comcb608442009-12-04 21:32:27 +0000156 if (false) {
157 SkDebugfDumper dumper;
158 SkDumpCanvas dumpCanvas(&dumper);
robertphillips9b14f262014-06-04 05:40:44 -0700159 dumpCanvas.drawPicture(pict);
reed@android.comcb608442009-12-04 21:32:27 +0000160 }
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000161#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000162
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000163 // This used to re-record the sub-picture and redraw the parent
164 // A capability that is now forbidden!
rmistry@google.comae933ce2012-08-23 18:19:56 +0000165
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000166 SkRandom rand(SampleCode::GetAnimTime());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 canvas->translate(SkIntToScalar(10), SkIntToScalar(250));
robertphillips9b14f262014-06-04 05:40:44 -0700168 canvas->drawPicture(fPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 delayInval(500);
170 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000171
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172private:
173 #define INVAL_ALL_TYPE "inval-all"
rmistry@google.comae933ce2012-08-23 18:19:56 +0000174
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 void delayInval(SkMSec delay) {
reed@google.com87fac4a2011-08-04 13:50:17 +0000176 (new SkEvent(INVAL_ALL_TYPE, this->getSinkID()))->postDelay(delay);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000178
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 virtual bool onEvent(const SkEvent& evt) {
180 if (evt.isType(INVAL_ALL_TYPE)) {
181 this->inval(NULL);
182 return true;
183 }
184 return this->INHERITED::onEvent(evt);
185 }
186
187 SkPicture* fPicture;
188 SkPicture* fSubPicture;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000190 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191};
192
193//////////////////////////////////////////////////////////////////////////////
194
195static SkView* MyFactory() { return new PictureView; }
196static SkViewRegister reg(MyFactory);