blob: 825e561654284a0575a097ad444669cc01ca0769 [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() {
reed@android.comc6ddc112009-11-10 15:54:55 +000036 SkBitmap bm;
reed@google.com84cfce12013-05-29 19:22:20 +000037 SkStream* stream = SkStream::NewFromFile("/skimages/sesame_street_ensemble-hp.jpg");
38 if (stream) {
39 SkAutoUnref aur(stream);
40
41 if (SkImageDecoder::DecodeStream(stream, &bm, SkBitmap::kNo_Config,
42 SkImageDecoder::kDecodeBounds_Mode)) {
43 SkPixelRef* pr = new SkImageRef_GlobalPool(stream, bm.config(), 1);
44 bm.setPixelRef(pr)->unref();
45 }
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:
robertphillips@google.com6853e802012-04-16 15:50:18 +000062 PictureView() {
reed@android.comc6ddc112009-11-10 15:54:55 +000063 SkImageRef_GlobalPool::SetRAMBudget(16 * 1024);
64
65 fBitmap = load_bitmap();
66
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 fPicture = new SkPicture;
68 SkCanvas* canvas = fPicture->beginRecording(100, 100);
69 SkPaint paint;
70 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +000071
reed@android.comc6ddc112009-11-10 15:54:55 +000072 canvas->drawBitmap(fBitmap, 0, 0, NULL);
73
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 drawCircle(canvas, 50, SK_ColorBLACK);
75 fSubPicture = new SkPicture;
76 canvas->drawPicture(*fSubPicture);
77 canvas->translate(SkIntToScalar(50), 0);
78 canvas->drawPicture(*fSubPicture);
79 canvas->translate(0, SkIntToScalar(50));
80 canvas->drawPicture(*fSubPicture);
81 canvas->translate(SkIntToScalar(-50), 0);
82 canvas->drawPicture(*fSubPicture);
83 // fPicture now has (4) references to us. We can release ours, and just
84 // unref fPicture in our destructor, and it will in turn take care of
85 // the other references to fSubPicture
86 fSubPicture->unref();
87 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 virtual ~PictureView() {
90 fPicture->unref();
91 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000092
reed@android.com8a1c16f2008-12-17 15:59:43 +000093protected:
94 // overrides from SkEventSink
95 virtual bool onQuery(SkEvent* evt) {
96 if (SampleCode::TitleQ(*evt)) {
97 SampleCode::TitleR(evt, "Picture");
98 return true;
99 }
100 return this->INHERITED::onQuery(evt);
101 }
102
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 void drawSomething(SkCanvas* canvas) {
104 SkPaint paint;
reed@android.comc6ddc112009-11-10 15:54:55 +0000105
106 canvas->save();
robertphillips@google.com6853e802012-04-16 15:50:18 +0000107 canvas->scale(SkFloatToScalar(0.5f), SkFloatToScalar(0.5f));
reed@android.comc6ddc112009-11-10 15:54:55 +0000108 canvas->drawBitmap(fBitmap, 0, 0, NULL);
109 canvas->restore();
110
reed@android.comcb608442009-12-04 21:32:27 +0000111 const char beforeStr[] = "before circle";
112 const char afterStr[] = "after circle";
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);
reed@android.comcb608442009-12-04 21:32:27 +0000117 canvas->drawData(beforeStr, sizeof(beforeStr));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
119 SkIntToScalar(40), paint);
reed@android.comcb608442009-12-04 21:32:27 +0000120 canvas->drawData(afterStr, sizeof(afterStr));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 paint.setColor(SK_ColorBLACK);
122 paint.setTextSize(SkIntToScalar(40));
123 canvas->drawText("Picture", 7, SkIntToScalar(50), SkIntToScalar(62),
124 paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 }
127
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000128 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 drawSomething(canvas);
reed@android.com149e2f62009-05-22 14:39:03 +0000130
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 SkPicture* pict = new SkPicture;
132 SkAutoUnref aur(pict);
133
134 drawSomething(pict->beginRecording(100, 100));
135 pict->endRecording();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000136
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 canvas->save();
138 canvas->translate(SkIntToScalar(300), SkIntToScalar(50));
139 canvas->scale(-SK_Scalar1, -SK_Scalar1);
140 canvas->translate(-SkIntToScalar(100), -SkIntToScalar(50));
141 canvas->drawPicture(*pict);
142 canvas->restore();
143
144 canvas->save();
145 canvas->translate(SkIntToScalar(200), SkIntToScalar(150));
146 canvas->scale(SK_Scalar1, -SK_Scalar1);
147 canvas->translate(0, -SkIntToScalar(50));
148 canvas->drawPicture(*pict);
149 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 canvas->save();
152 canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
153 canvas->scale(-SK_Scalar1, SK_Scalar1);
154 canvas->translate(-SkIntToScalar(100), 0);
155 canvas->drawPicture(*pict);
156 canvas->restore();
reed@android.comcb608442009-12-04 21:32:27 +0000157
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000158#ifdef SK_DEVELOPER
reed@android.comcb608442009-12-04 21:32:27 +0000159 if (false) {
160 SkDebugfDumper dumper;
161 SkDumpCanvas dumpCanvas(&dumper);
162 dumpCanvas.drawPicture(*pict);
163 }
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000164#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000165
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 // test that we can re-record a subpicture, and see the results
rmistry@google.comae933ce2012-08-23 18:19:56 +0000167
reed@android.com44177402009-11-23 21:07:51 +0000168 SkRandom rand(SampleCode::GetAnimTime());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 canvas->translate(SkIntToScalar(10), SkIntToScalar(250));
170 drawCircle(fSubPicture->beginRecording(50, 50), 25,
reed@android.com44177402009-11-23 21:07:51 +0000171 rand.nextU() | 0xFF000000);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 canvas->drawPicture(*fPicture);
173 delayInval(500);
174 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000175
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176private:
177 #define INVAL_ALL_TYPE "inval-all"
rmistry@google.comae933ce2012-08-23 18:19:56 +0000178
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 void delayInval(SkMSec delay) {
reed@google.com87fac4a2011-08-04 13:50:17 +0000180 (new SkEvent(INVAL_ALL_TYPE, this->getSinkID()))->postDelay(delay);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000182
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 virtual bool onEvent(const SkEvent& evt) {
184 if (evt.isType(INVAL_ALL_TYPE)) {
185 this->inval(NULL);
186 return true;
187 }
188 return this->INHERITED::onEvent(evt);
189 }
190
191 SkPicture* fPicture;
192 SkPicture* fSubPicture;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000194 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195};
196
197//////////////////////////////////////////////////////////////////////////////
198
199static SkView* MyFactory() { return new PictureView; }
200static SkViewRegister reg(MyFactory);