blob: 7317fbe856bb87973cbed341d32c8f2f684ef5ca [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
reed@android.comcb608442009-12-04 21:32:27 +00009#include "SkDumpCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkView.h"
11#include "SkCanvas.h"
12#include "Sk64.h"
13#include "SkGradientShader.h"
14#include "SkGraphics.h"
15#include "SkImageDecoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkPath.h"
17#include "SkPicture.h"
18#include "SkRandom.h"
19#include "SkRegion.h"
20#include "SkShader.h"
21#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkColorPriv.h"
23#include "SkColorFilter.h"
24#include "SkTime.h"
25#include "SkTypeface.h"
26#include "SkXfermode.h"
27
28#include "SkStream.h"
29#include "SkXMLParser.h"
30
reed@android.com7d970c72010-04-22 16:07:49 +000031///////////////////////////////////////////////////////////////////////////////
32
reed@android.comc6ddc112009-11-10 15:54:55 +000033#include "SkImageRef_GlobalPool.h"
34
35static SkBitmap load_bitmap() {
36 SkStream* stream = new SkFILEStream("/skimages/sesame_street_ensemble-hp.jpg");
37 SkAutoUnref aur(stream);
38
39 SkBitmap bm;
40 if (SkImageDecoder::DecodeStream(stream, &bm, SkBitmap::kNo_Config,
41 SkImageDecoder::kDecodeBounds_Mode)) {
42 SkPixelRef* pr = new SkImageRef_GlobalPool(stream, bm.config(), 1);
43 bm.setPixelRef(pr)->unref();
44 }
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:
robertphillips@google.com6853e802012-04-16 15:50:18 +000060 PictureView() {
reed@android.comc6ddc112009-11-10 15:54:55 +000061 SkImageRef_GlobalPool::SetRAMBudget(16 * 1024);
62
63 fBitmap = load_bitmap();
64
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 fPicture = new SkPicture;
66 SkCanvas* canvas = fPicture->beginRecording(100, 100);
67 SkPaint paint;
68 paint.setAntiAlias(true);
69
reed@android.comc6ddc112009-11-10 15:54:55 +000070 canvas->drawBitmap(fBitmap, 0, 0, NULL);
71
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 drawCircle(canvas, 50, SK_ColorBLACK);
73 fSubPicture = new SkPicture;
74 canvas->drawPicture(*fSubPicture);
75 canvas->translate(SkIntToScalar(50), 0);
76 canvas->drawPicture(*fSubPicture);
77 canvas->translate(0, SkIntToScalar(50));
78 canvas->drawPicture(*fSubPicture);
79 canvas->translate(SkIntToScalar(-50), 0);
80 canvas->drawPicture(*fSubPicture);
81 // fPicture now has (4) references to us. We can release ours, and just
82 // unref fPicture in our destructor, and it will in turn take care of
83 // the other references to fSubPicture
84 fSubPicture->unref();
85 }
86
87 virtual ~PictureView() {
88 fPicture->unref();
89 }
90
91protected:
92 // overrides from SkEventSink
93 virtual bool onQuery(SkEvent* evt) {
94 if (SampleCode::TitleQ(*evt)) {
95 SampleCode::TitleR(evt, "Picture");
96 return true;
97 }
98 return this->INHERITED::onQuery(evt);
99 }
100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 void drawSomething(SkCanvas* canvas) {
102 SkPaint paint;
reed@android.comc6ddc112009-11-10 15:54:55 +0000103
104 canvas->save();
robertphillips@google.com6853e802012-04-16 15:50:18 +0000105 canvas->scale(SkFloatToScalar(0.5f), SkFloatToScalar(0.5f));
reed@android.comc6ddc112009-11-10 15:54:55 +0000106 canvas->drawBitmap(fBitmap, 0, 0, NULL);
107 canvas->restore();
108
reed@android.comcb608442009-12-04 21:32:27 +0000109 const char beforeStr[] = "before circle";
110 const char afterStr[] = "after circle";
111
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 paint.setAntiAlias(true);
113
114 paint.setColor(SK_ColorRED);
reed@android.comcb608442009-12-04 21:32:27 +0000115 canvas->drawData(beforeStr, sizeof(beforeStr));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
117 SkIntToScalar(40), paint);
reed@android.comcb608442009-12-04 21:32:27 +0000118 canvas->drawData(afterStr, sizeof(afterStr));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 paint.setColor(SK_ColorBLACK);
120 paint.setTextSize(SkIntToScalar(40));
121 canvas->drawText("Picture", 7, SkIntToScalar(50), SkIntToScalar(62),
122 paint);
reed@android.comcb608442009-12-04 21:32:27 +0000123
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 }
125
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000126 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 drawSomething(canvas);
reed@android.com149e2f62009-05-22 14:39:03 +0000128
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 SkPicture* pict = new SkPicture;
130 SkAutoUnref aur(pict);
131
132 drawSomething(pict->beginRecording(100, 100));
133 pict->endRecording();
reed@android.com149e2f62009-05-22 14:39:03 +0000134
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 canvas->save();
136 canvas->translate(SkIntToScalar(300), SkIntToScalar(50));
137 canvas->scale(-SK_Scalar1, -SK_Scalar1);
138 canvas->translate(-SkIntToScalar(100), -SkIntToScalar(50));
139 canvas->drawPicture(*pict);
140 canvas->restore();
141
142 canvas->save();
143 canvas->translate(SkIntToScalar(200), SkIntToScalar(150));
144 canvas->scale(SK_Scalar1, -SK_Scalar1);
145 canvas->translate(0, -SkIntToScalar(50));
146 canvas->drawPicture(*pict);
147 canvas->restore();
148
149 canvas->save();
150 canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
151 canvas->scale(-SK_Scalar1, SK_Scalar1);
152 canvas->translate(-SkIntToScalar(100), 0);
153 canvas->drawPicture(*pict);
154 canvas->restore();
reed@android.comcb608442009-12-04 21:32:27 +0000155
156 if (false) {
157 SkDebugfDumper dumper;
158 SkDumpCanvas dumpCanvas(&dumper);
159 dumpCanvas.drawPicture(*pict);
160 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161
162 // test that we can re-record a subpicture, and see the results
163
reed@android.com44177402009-11-23 21:07:51 +0000164 SkRandom rand(SampleCode::GetAnimTime());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 canvas->translate(SkIntToScalar(10), SkIntToScalar(250));
166 drawCircle(fSubPicture->beginRecording(50, 50), 25,
reed@android.com44177402009-11-23 21:07:51 +0000167 rand.nextU() | 0xFF000000);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 canvas->drawPicture(*fPicture);
169 delayInval(500);
170 }
171
172private:
173 #define INVAL_ALL_TYPE "inval-all"
174
175 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 }
178
179 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);
197