blob: ecc59415a0b54254c5248625390fa085b48a5e03 [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;
tfarinabcbc1782014-06-18 14:32:48 -070040 SkString resourcePath = GetResourcePath();
41 SkString path = SkOSPath::SkPathJoin(resourcePath.c_str(), "mandrill_512.png");
halcanary@google.come2380572013-12-18 15:08:35 +000042 SkAutoDataUnref data(SkData::NewFromFileName(path.c_str()));
43 if (data.get() != NULL) {
halcanary@google.com3d50ea12014-01-02 13:15:13 +000044 SkInstallDiscardablePixelRef(SkDecodingImageGenerator::Create(
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +000045 data, SkDecodingImageGenerator::Options()), &bm);
reed@android.comc6ddc112009-11-10 15:54:55 +000046 }
47 return bm;
48}
49
reed@android.com8a1c16f2008-12-17 15:59:43 +000050static void drawCircle(SkCanvas* canvas, int r, SkColor color) {
51 SkPaint paint;
52 paint.setAntiAlias(true);
53 paint.setColor(color);
54
55 canvas->drawCircle(SkIntToScalar(r), SkIntToScalar(r), SkIntToScalar(r),
56 paint);
57}
58
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000059class PictureView : public SampleView {
reed@android.comc6ddc112009-11-10 15:54:55 +000060 SkBitmap fBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +000061public:
halcanary@google.come2380572013-12-18 15:08:35 +000062 PictureView() {
reed@android.comc6ddc112009-11-10 15:54:55 +000063
64 fBitmap = load_bitmap();
65
robertphillips@google.com84b18c72014-04-13 19:09:42 +000066 SkPictureRecorder recorder;
67
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000068 recorder.beginRecording(100, 100, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000069 fSubPicture = recorder.endRecording();
70
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000071 SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 SkPaint paint;
73 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +000074
reed@android.comc6ddc112009-11-10 15:54:55 +000075 canvas->drawBitmap(fBitmap, 0, 0, NULL);
76
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 drawCircle(canvas, 50, SK_ColorBLACK);
robertphillips9b14f262014-06-04 05:40:44 -070078 canvas->drawPicture(fSubPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 canvas->translate(SkIntToScalar(50), 0);
robertphillips9b14f262014-06-04 05:40:44 -070080 canvas->drawPicture(fSubPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 canvas->translate(0, SkIntToScalar(50));
robertphillips9b14f262014-06-04 05:40:44 -070082 canvas->drawPicture(fSubPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 canvas->translate(SkIntToScalar(-50), 0);
robertphillips9b14f262014-06-04 05:40:44 -070084 canvas->drawPicture(fSubPicture);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000085
86 fPicture = recorder.endRecording();
87
skia.committer@gmail.com51997012014-04-14 03:04:57 +000088 // fPicture now has (4) references to fSubPicture. We can release our ref,
robertphillips@google.com84b18c72014-04-13 19:09:42 +000089 // and just unref fPicture in our destructor, and it will in turn take care of
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 // the other references to fSubPicture
91 fSubPicture->unref();
92 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000093
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 virtual ~PictureView() {
95 fPicture->unref();
96 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000097
reed@android.com8a1c16f2008-12-17 15:59:43 +000098protected:
99 // overrides from SkEventSink
100 virtual bool onQuery(SkEvent* evt) {
101 if (SampleCode::TitleQ(*evt)) {
102 SampleCode::TitleR(evt, "Picture");
103 return true;
104 }
105 return this->INHERITED::onQuery(evt);
106 }
107
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 void drawSomething(SkCanvas* canvas) {
109 SkPaint paint;
reed@android.comc6ddc112009-11-10 15:54:55 +0000110
111 canvas->save();
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000112 canvas->scale(0.5f, 0.5f);
reed@android.comc6ddc112009-11-10 15:54:55 +0000113 canvas->drawBitmap(fBitmap, 0, 0, NULL);
114 canvas->restore();
115
reed@android.comcb608442009-12-04 21:32:27 +0000116 const char beforeStr[] = "before circle";
117 const char afterStr[] = "after circle";
118
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 paint.setColor(SK_ColorRED);
reed@android.comcb608442009-12-04 21:32:27 +0000122 canvas->drawData(beforeStr, sizeof(beforeStr));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
124 SkIntToScalar(40), paint);
reed@android.comcb608442009-12-04 21:32:27 +0000125 canvas->drawData(afterStr, sizeof(afterStr));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 paint.setColor(SK_ColorBLACK);
127 paint.setTextSize(SkIntToScalar(40));
128 canvas->drawText("Picture", 7, SkIntToScalar(50), SkIntToScalar(62),
129 paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000130
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 }
132
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000133 virtual void onDrawContent(SkCanvas* canvas) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000134 this->drawSomething(canvas);
reed@android.com149e2f62009-05-22 14:39:03 +0000135
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000136 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000137 this->drawSomething(recorder.beginRecording(100, 100, NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000138 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000139
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 canvas->save();
141 canvas->translate(SkIntToScalar(300), SkIntToScalar(50));
142 canvas->scale(-SK_Scalar1, -SK_Scalar1);
143 canvas->translate(-SkIntToScalar(100), -SkIntToScalar(50));
robertphillips9b14f262014-06-04 05:40:44 -0700144 canvas->drawPicture(pict);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 canvas->restore();
146
147 canvas->save();
148 canvas->translate(SkIntToScalar(200), SkIntToScalar(150));
149 canvas->scale(SK_Scalar1, -SK_Scalar1);
150 canvas->translate(0, -SkIntToScalar(50));
robertphillips9b14f262014-06-04 05:40:44 -0700151 canvas->drawPicture(pict);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000153
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 canvas->save();
155 canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
156 canvas->scale(-SK_Scalar1, SK_Scalar1);
157 canvas->translate(-SkIntToScalar(100), 0);
robertphillips9b14f262014-06-04 05:40:44 -0700158 canvas->drawPicture(pict);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 canvas->restore();
reed@android.comcb608442009-12-04 21:32:27 +0000160
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000161#ifdef SK_DEVELOPER
reed@android.comcb608442009-12-04 21:32:27 +0000162 if (false) {
163 SkDebugfDumper dumper;
164 SkDumpCanvas dumpCanvas(&dumper);
robertphillips9b14f262014-06-04 05:40:44 -0700165 dumpCanvas.drawPicture(pict);
reed@android.comcb608442009-12-04 21:32:27 +0000166 }
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000167#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000168
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000169 // This used to re-record the sub-picture and redraw the parent
170 // A capability that is now forbidden!
rmistry@google.comae933ce2012-08-23 18:19:56 +0000171
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000172 SkRandom rand(SampleCode::GetAnimTime());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 canvas->translate(SkIntToScalar(10), SkIntToScalar(250));
robertphillips9b14f262014-06-04 05:40:44 -0700174 canvas->drawPicture(fPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 delayInval(500);
176 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000177
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178private:
179 #define INVAL_ALL_TYPE "inval-all"
rmistry@google.comae933ce2012-08-23 18:19:56 +0000180
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 void delayInval(SkMSec delay) {
reed@google.com87fac4a2011-08-04 13:50:17 +0000182 (new SkEvent(INVAL_ALL_TYPE, this->getSinkID()))->postDelay(delay);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000184
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 virtual bool onEvent(const SkEvent& evt) {
186 if (evt.isType(INVAL_ALL_TYPE)) {
187 this->inval(NULL);
188 return true;
189 }
190 return this->INHERITED::onEvent(evt);
191 }
192
193 SkPicture* fPicture;
194 SkPicture* fSubPicture;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000196 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197};
198
199//////////////////////////////////////////////////////////////////////////////
200
201static SkView* MyFactory() { return new PictureView; }
202static SkViewRegister reg(MyFactory);