blob: 28256e56752dd392cc838478abfaa4a3447f0e47 [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.comcb608442009-12-04 21:32:27 +0000115 const char beforeStr[] = "before circle";
116 const char afterStr[] = "after circle";
117
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 paint.setColor(SK_ColorRED);
reed@android.comcb608442009-12-04 21:32:27 +0000121 canvas->drawData(beforeStr, sizeof(beforeStr));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
123 SkIntToScalar(40), paint);
reed@android.comcb608442009-12-04 21:32:27 +0000124 canvas->drawData(afterStr, sizeof(afterStr));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 paint.setColor(SK_ColorBLACK);
126 paint.setTextSize(SkIntToScalar(40));
127 canvas->drawText("Picture", 7, SkIntToScalar(50), SkIntToScalar(62),
128 paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 }
131
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000132 virtual void onDrawContent(SkCanvas* canvas) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000133 this->drawSomething(canvas);
reed@android.com149e2f62009-05-22 14:39:03 +0000134
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000135 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000136 this->drawSomething(recorder.beginRecording(100, 100, NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000137 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 canvas->save();
140 canvas->translate(SkIntToScalar(300), SkIntToScalar(50));
141 canvas->scale(-SK_Scalar1, -SK_Scalar1);
142 canvas->translate(-SkIntToScalar(100), -SkIntToScalar(50));
robertphillips9b14f262014-06-04 05:40:44 -0700143 canvas->drawPicture(pict);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 canvas->restore();
145
146 canvas->save();
147 canvas->translate(SkIntToScalar(200), SkIntToScalar(150));
148 canvas->scale(SK_Scalar1, -SK_Scalar1);
149 canvas->translate(0, -SkIntToScalar(50));
robertphillips9b14f262014-06-04 05:40:44 -0700150 canvas->drawPicture(pict);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 canvas->save();
154 canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
155 canvas->scale(-SK_Scalar1, SK_Scalar1);
156 canvas->translate(-SkIntToScalar(100), 0);
robertphillips9b14f262014-06-04 05:40:44 -0700157 canvas->drawPicture(pict);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 canvas->restore();
reed@android.comcb608442009-12-04 21:32:27 +0000159
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000160#ifdef SK_DEVELOPER
reed@android.comcb608442009-12-04 21:32:27 +0000161 if (false) {
162 SkDebugfDumper dumper;
163 SkDumpCanvas dumpCanvas(&dumper);
robertphillips9b14f262014-06-04 05:40:44 -0700164 dumpCanvas.drawPicture(pict);
reed@android.comcb608442009-12-04 21:32:27 +0000165 }
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000166#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000167
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000168 // This used to re-record the sub-picture and redraw the parent
169 // A capability that is now forbidden!
rmistry@google.comae933ce2012-08-23 18:19:56 +0000170
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000171 SkRandom rand(SampleCode::GetAnimTime());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 canvas->translate(SkIntToScalar(10), SkIntToScalar(250));
robertphillips9b14f262014-06-04 05:40:44 -0700173 canvas->drawPicture(fPicture);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 delayInval(500);
175 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000176
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177private:
178 #define INVAL_ALL_TYPE "inval-all"
rmistry@google.comae933ce2012-08-23 18:19:56 +0000179
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 void delayInval(SkMSec delay) {
reed@google.com87fac4a2011-08-04 13:50:17 +0000181 (new SkEvent(INVAL_ALL_TYPE, this->getSinkID()))->postDelay(delay);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000183
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184 virtual bool onEvent(const SkEvent& evt) {
185 if (evt.isType(INVAL_ALL_TYPE)) {
186 this->inval(NULL);
187 return true;
188 }
189 return this->INHERITED::onEvent(evt);
190 }
191
192 SkPicture* fPicture;
193 SkPicture* fSubPicture;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000195 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196};
197
198//////////////////////////////////////////////////////////////////////////////
199
200static SkView* MyFactory() { return new PictureView; }
201static SkViewRegister reg(MyFactory);