blob: 3b32154cfa76ec02d74d143be90aeead7ed0e066 [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 */
Scroggo2c8208f2011-06-15 16:49:08 +00008#include "SampleApp.h"
9
reed@google.com8a85d0c2011-06-24 19:12:12 +000010#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkCanvas.h"
12#include "SkDevice.h"
reed@google.comaf951c92011-06-16 19:10:39 +000013#include "SkGpuDevice.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkGraphics.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +000015#include "SkImageEncoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkPaint.h"
17#include "SkPicture.h"
18#include "SkStream.h"
reed@android.com44177402009-11-23 21:07:51 +000019#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkWindow.h"
21
22#include "SampleCode.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023#include "GrContext.h"
Scroggo0f185c22011-03-24 18:35:50 +000024#include "SkTypeface.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000025
Scroggo3e7ff9f2011-06-16 15:31:26 +000026#include "GrGLInterface.h"
bsalomon@google.com583a1e32011-08-17 13:42:46 +000027#include "GrRenderTarget.h"
Scroggo2c8208f2011-06-15 16:49:08 +000028
Scroggo8ac0d542011-06-21 14:44:57 +000029#include "SkPDFDevice.h"
30#include "SkPDFDocument.h"
31#include "SkStream.h"
32
yangsu@google.comef7bdfa2011-08-12 14:27:47 +000033#define TEST_GPIPE
yangsu@google.com1f394212011-06-01 18:03:34 +000034
35#ifdef TEST_GPIPE
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000036#define PIPE_FILEx
37#ifdef PIPE_FILE
yangsu@google.com1f394212011-06-01 18:03:34 +000038#define FILE_PATH "/path/to/drawing.data"
39#endif
40
yangsu@google.comf121b052011-08-08 16:02:51 +000041#define PIPE_NETx
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000042#ifdef PIPE_NET
43#include "SkSockets.h"
44SkTCPServer gServer;
45#endif
yangsu@google.comef7bdfa2011-08-12 14:27:47 +000046
47#define DEBUGGERx
48#ifdef DEBUGGER
49extern SkView* create_debugger(const char* data, size_t size);
50extern bool is_debugger(SkView* view);
51SkTDArray<char> gTempDataStore;
52#endif
53
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000054#endif
55
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +000056#define USE_ARROWS_FOR_ZOOM true
reed@android.comf2b98d62010-12-20 18:26:13 +000057//#define DEFAULT_TO_GPU
58
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000059extern SkView* create_overview(int, const SkViewFactory*[]);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +000060extern bool is_overview(SkView* view);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000061extern SkView* create_transition(SkView*, SkView*, int);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +000062extern bool is_transition(SkView* view);
63
reed@android.com34245c72009-11-03 04:00:48 +000064
reed@android.com8a1c16f2008-12-17 15:59:43 +000065#define ANIMATING_EVENTTYPE "nextSample"
66#define ANIMATING_DELAY 750
67
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000068#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +000069 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000070#else
reed@google.combad8c872011-05-18 20:10:31 +000071 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000072#endif
reed@google.combad8c872011-05-18 20:10:31 +000073#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000074
reed@google.com3cec4d72011-07-06 13:59:47 +000075static SampleWindow* gSampleWindow;
76
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000077///////////////
bsalomon@google.com098e96d2011-07-14 14:30:46 +000078class SampleWindow::DefaultDeviceManager : public SampleWindow::DeviceManager {
79public:
80
81 DefaultDeviceManager() {
82 fGrRenderTarget = NULL;
83 fGrContext = NULL;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000084 fGL = NULL;
bsalomon@google.com74913722011-10-27 20:44:19 +000085 fNullGrContext = NULL;
86 fNullGrRenderTarget = NULL;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000087 }
88
89 virtual ~DefaultDeviceManager() {
90 SkSafeUnref(fGrRenderTarget);
91 SkSafeUnref(fGrContext);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +000092 SkSafeUnref(fGL);
bsalomon@google.com74913722011-10-27 20:44:19 +000093 SkSafeUnref(fNullGrContext);
94 SkSafeUnref(fNullGrRenderTarget);
bsalomon@google.com098e96d2011-07-14 14:30:46 +000095 }
96
97 virtual void init(SampleWindow* win) {
bsalomon@google.com74913722011-10-27 20:44:19 +000098 if (!win->attachGL()) {
99 SkDebugf("Failed to initialize GL");
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000100 }
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000101 if (NULL == fGL) {
bsalomon@google.com74913722011-10-27 20:44:19 +0000102 fGL = GrGLCreateNativeInterface();
103 GrAssert(NULL == fGrContext);
104 fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine,
105 (GrPlatform3DContext) fGL);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000106 }
107 if (NULL == fGrContext || NULL == fGL) {
bsalomon@google.com74913722011-10-27 20:44:19 +0000108 SkSafeUnref(fGrContext);
109 SkSafeUnref(fGL);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000110 SkDebugf("Failed to setup 3D");
111 win->detachGL();
112 }
bsalomon@google.com74913722011-10-27 20:44:19 +0000113 if (NULL == fNullGrContext) {
114 const GrGLInterface* nullGL = GrGLCreateNullInterface();
115 fNullGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine,
116 (GrPlatform3DContext) nullGL);
117 nullGL->unref();
118 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000119 }
120
121 virtual bool supportsDeviceType(SampleWindow::DeviceType dType) {
122 switch (dType) {
123 case kRaster_DeviceType:
124 case kPicture_DeviceType: // fallthru
125 return true;
126 case kGPU_DeviceType:
127 return NULL != fGrContext && NULL != fGrRenderTarget;
bsalomon@google.com74913722011-10-27 20:44:19 +0000128 case kNullGPU_DeviceType:
129 return NULL != fNullGrContext && NULL != fNullGrRenderTarget;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000130 default:
131 return false;
132 }
133 }
134
135 virtual bool prepareCanvas(SampleWindow::DeviceType dType,
136 SkCanvas* canvas,
137 SampleWindow* win) {
bsalomon@google.com74913722011-10-27 20:44:19 +0000138 switch (dType) {
139 case kGPU_DeviceType:
140 if (fGrContext) {
141 canvas->setDevice(new SkGpuDevice(fGrContext,
142 fGrRenderTarget))->unref();
143 } else {
144 return false;
145 }
146 break;
147 case kNullGPU_DeviceType:
148 if (fNullGrContext) {
149 canvas->setDevice(new SkGpuDevice(fNullGrContext,
150 fNullGrRenderTarget))->unref();
151 } else {
152 return false;
153 }
154 break;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000155 }
156 return true;
157 }
158
159 virtual void publishCanvas(SampleWindow::DeviceType dType,
160 SkCanvas* canvas,
161 SampleWindow* win) {
162 if (fGrContext) {
163 // in case we have queued drawing calls
164 fGrContext->flush();
bsalomon@google.com74913722011-10-27 20:44:19 +0000165 if (NULL != fNullGrContext) {
166 fNullGrContext->flush();
167 }
168 if (dType != kGPU_DeviceType &&
169 dType != kNullGPU_DeviceType) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000170 // need to send the raster bits to the (gpu) window
171 fGrContext->setRenderTarget(fGrRenderTarget);
172 const SkBitmap& bm = win->getBitmap();
173 fGrContext->writePixels(0, 0, bm.width(), bm.height(),
174 kRGBA_8888_GrPixelConfig, bm.getPixels(),
175 bm.rowBytes());
176 }
177 }
178 win->presentGL();
179 }
180
181 virtual void windowSizeChanged(SampleWindow* win) {
182 if (fGrContext) {
183 win->attachGL();
184
bsalomon@google.comb87d2b52011-11-07 01:58:39 +0000185 GrPlatformSurfaceDesc desc;
186 desc.reset();
187 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000188 desc.fWidth = SkScalarRound(win->width());
189 desc.fHeight = SkScalarRound(win->height());
190 desc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.com6d9adae2011-11-07 01:37:22 +0000191 GR_GL_GetIntegerv(fGL, GR_GL_STENCIL_BITS, &desc.fStencilBits);
bsalomon@google.comb87d2b52011-11-07 01:58:39 +0000192 GR_GL_GetIntegerv(fGL, GR_GL_SAMPLES, &desc.fSampleCnt);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000193 GrGLint buffer;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000194 GR_GL_GetIntegerv(fGL, GR_GL_FRAMEBUFFER_BINDING, &buffer);
bsalomon@google.comb87d2b52011-11-07 01:58:39 +0000195 desc.fPlatformRenderTarget = buffer;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000196
197 SkSafeUnref(fGrRenderTarget);
bsalomon@google.comb87d2b52011-11-07 01:58:39 +0000198 fGrRenderTarget = static_cast<GrRenderTarget*>(
199 fGrContext->createPlatformSurface(desc));
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000200 }
bsalomon@google.com74913722011-10-27 20:44:19 +0000201 if (NULL != fNullGrContext) {
bsalomon@google.comb87d2b52011-11-07 01:58:39 +0000202 GrPlatformSurfaceDesc desc;
203 desc.reset();
204 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
bsalomon@google.com74913722011-10-27 20:44:19 +0000205 desc.fWidth = SkScalarRound(win->width());
206 desc.fHeight = SkScalarRound(win->height());
207 desc.fConfig = kRGBA_8888_GrPixelConfig;
208 desc.fStencilBits = 8;
209 desc.fSampleCnt = 0;
bsalomon@google.comb87d2b52011-11-07 01:58:39 +0000210 desc.fPlatformRenderTarget = 0;
211 fNullGrRenderTarget = static_cast<GrRenderTarget*>(
212 fNullGrContext->createPlatformSurface(desc));
bsalomon@google.com74913722011-10-27 20:44:19 +0000213 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000214 }
215
bsalomon@google.com74913722011-10-27 20:44:19 +0000216 virtual GrContext* getGrContext(SampleWindow::DeviceType dType) {
217 if (kNullGPU_DeviceType == dType) {
218 return fNullGrContext;
219 } else {
220 return fGrContext;
221 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000222 }
223private:
224 GrContext* fGrContext;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000225 const GrGLInterface* fGL;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000226 GrRenderTarget* fGrRenderTarget;
bsalomon@google.com74913722011-10-27 20:44:19 +0000227 GrContext* fNullGrContext;
228 GrRenderTarget* fNullGrRenderTarget;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000229};
230
231///////////////
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000232static const char view_inval_msg[] = "view-inval-msg";
233
Scroggo62b65b02011-06-21 16:01:26 +0000234void SampleWindow::postInvalDelay() {
reed@google.com87fac4a2011-08-04 13:50:17 +0000235 (new SkEvent(view_inval_msg, this->getSinkID()))->postDelay(1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000236}
237
238static bool isInvalEvent(const SkEvent& evt) {
239 return evt.isType(view_inval_msg);
240}
241//////////////////
242
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000243SkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func)
244 : fCreateFunc(func) {
245}
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000246
bsalomon@google.com8301de12011-10-31 16:47:13 +0000247SkView* SkFuncViewFactory::operator() () const {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000248 return (*fCreateFunc)();
249}
250
251#include "GMSampleView.h"
252
253SkGMSampleViewFactory::SkGMSampleViewFactory(GMFactoryFunc func)
254 : fFunc(func) {
255}
256
257SkView* SkGMSampleViewFactory::operator() () const {
258 return new GMSampleView(fFunc(NULL));
259}
260
261SkViewRegister* SkViewRegister::gHead;
262SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) {
263 fFact->ref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264 fChain = gHead;
265 gHead = this;
266}
267
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000268SkViewRegister::SkViewRegister(SkViewCreateFunc func) {
269 fFact = new SkFuncViewFactory(func);
270 fChain = gHead;
271 gHead = this;
272}
273
274SkViewRegister::SkViewRegister(GMFactoryFunc func) {
275 fFact = new SkGMSampleViewFactory(func);
276 fChain = gHead;
277 gHead = this;
278}
279
280class AutoUnrefArray {
281public:
282 AutoUnrefArray() {}
283 ~AutoUnrefArray() {
284 int count = fObjs.count();
285 for (int i = 0; i < count; ++i) {
286 fObjs[i]->unref();
287 }
288 }
289 SkRefCnt*& push_back() { return *fObjs.append(); }
290
291private:
292 SkTDArray<SkRefCnt*> fObjs;
293};
294
295// registers GMs as Samples
296// This can't be performed during static initialization because it could be
297// run before GMRegistry has been fully built.
298void SkGMRegistyToSampleRegistry() {
299 static bool gOnce;
300 static AutoUnrefArray fRegisters;
301
302 if (!gOnce) {
303 const skiagm::GMRegistry* gmreg = skiagm::GMRegistry::Head();
304 while (gmreg) {
305 fRegisters.push_back() = new SkViewRegister(gmreg->factory());
306 gmreg = gmreg->next();
307 }
308 gOnce = true;
309 }
310}
311
reed@google.com29038ed2011-07-06 17:56:47 +0000312#if 0
reed@google.comf0b5f682011-03-11 20:08:25 +0000313#include <CoreFoundation/CoreFoundation.h>
314#include <CoreFoundation/CFURLAccess.h>
315
316static void testpdf() {
317 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
318 kCFStringEncodingUTF8);
319 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
320 kCFURLPOSIXPathStyle,
321 false);
322 CFRelease(path);
323 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
324 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
325 CFRelease(url);
326
327 CGContextBeginPage(cg, &box);
328 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
329 CGContextFillEllipseInRect(cg, r);
330 CGContextEndPage(cg);
331 CGContextRelease(cg);
332
333 if (false) {
334 SkBitmap bm;
335 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
336 bm.allocPixels();
337 bm.eraseColor(0);
338
339 SkCanvas canvas(bm);
340
341 }
342}
343#endif
344
345//////////////////////////////////////////////////////////////////////////////
346
reed@google.com569e0432011-04-05 13:07:03 +0000347enum FlipAxisEnum {
348 kFlipAxis_X = (1 << 0),
349 kFlipAxis_Y = (1 << 1)
350};
351
reed@google.comf0b5f682011-03-11 20:08:25 +0000352#include "SkDrawFilter.h"
353
reed@google.com569e0432011-04-05 13:07:03 +0000354class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000355public:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000356 FlagsDrawFilter(SkOSMenu::TriState lcd, SkOSMenu::TriState aa, SkOSMenu::TriState filter,
357 SkOSMenu::TriState hinting) :
reed@google.com09e3baa2011-05-18 12:04:31 +0000358 fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000359
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000360 virtual void filter(SkPaint* paint, Type t) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000361 if (kText_Type == t && SkOSMenu::kMixedState != fLCDState) {
362 paint->setLCDRenderText(SkOSMenu::kOnState == fLCDState);
reed@google.com569e0432011-04-05 13:07:03 +0000363 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000364 if (SkOSMenu::kMixedState != fAAState) {
365 paint->setAntiAlias(SkOSMenu::kOnState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000366 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000367 if (SkOSMenu::kMixedState != fFilterState) {
368 paint->setFilterBitmap(SkOSMenu::kOnState == fFilterState);
reed@google.com176753a2011-05-17 15:32:04 +0000369 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000370 if (SkOSMenu::kMixedState != fHintingState) {
371 paint->setHinting(SkOSMenu::kOnState == fHintingState ?
reed@google.com09e3baa2011-05-18 12:04:31 +0000372 SkPaint::kNormal_Hinting :
373 SkPaint::kSlight_Hinting);
374 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000375 }
376
377private:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000378 SkOSMenu::TriState fLCDState;
379 SkOSMenu::TriState fAAState;
380 SkOSMenu::TriState fFilterState;
381 SkOSMenu::TriState fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000382};
383
reed@android.com8a1c16f2008-12-17 15:59:43 +0000384//////////////////////////////////////////////////////////////////////////////
385
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000386#define MAX_ZOOM_LEVEL 8
387#define MIN_ZOOM_LEVEL -8
388
reed@android.comf2b98d62010-12-20 18:26:13 +0000389static const char gCharEvtName[] = "SampleCode_Char_Event";
390static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000391static const char gTitleEvtName[] = "SampleCode_Title_Event";
392static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000393static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
394
395bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
396 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
397 if (outUni) {
398 *outUni = evt.getFast32();
399 }
400 return true;
401 }
402 return false;
403}
404
405bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
406 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
407 if (outKey) {
408 *outKey = (SkKey)evt.getFast32();
409 }
410 return true;
411 }
412 return false;
413}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414
415bool SampleCode::TitleQ(const SkEvent& evt) {
416 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
417}
418
419void SampleCode::TitleR(SkEvent* evt, const char title[]) {
420 SkASSERT(evt && TitleQ(*evt));
421 evt->setString(gTitleEvtName, title);
422}
423
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000424bool SampleCode::RequestTitle(SkView* view, SkString* title) {
425 SkEvent evt(gTitleEvtName);
426 if (view->doQuery(&evt)) {
427 title->set(evt.findString(gTitleEvtName));
428 return true;
429 }
430 return false;
431}
432
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433bool SampleCode::PrefSizeQ(const SkEvent& evt) {
434 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
435}
436
437void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
438 SkASSERT(evt && PrefSizeQ(*evt));
439 SkScalar size[2];
440 size[0] = width;
441 size[1] = height;
442 evt->setScalars(gPrefSizeEvtName, 2, size);
443}
444
reed@android.comf2b98d62010-12-20 18:26:13 +0000445bool SampleCode::FastTextQ(const SkEvent& evt) {
446 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
447}
448
449///////////////////////////////////////////////////////////////////////////////
450
reed@android.com44177402009-11-23 21:07:51 +0000451static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000452static SkMSec gAnimTimePrev;
453
reed@android.com44177402009-11-23 21:07:51 +0000454SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000455SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
456SkScalar SampleCode::GetAnimSecondsDelta() {
457 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
458}
reed@android.com44177402009-11-23 21:07:51 +0000459
460SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000461 // since gAnimTime can be up to 32 bits, we can't convert it to a float
462 // or we'll lose the low bits. Hence we use doubles for the intermediate
463 // calculations
464 double seconds = (double)gAnimTime / 1000.0;
465 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000466 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000467 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000468 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000469 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000470}
471
reed@google.com3cec4d72011-07-06 13:59:47 +0000472GrContext* SampleCode::GetGr() {
473 return gSampleWindow ? gSampleWindow->getGrContext() : NULL;
474}
475
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000476// some GMs rely on having a skiagm::GetGr function defined
477namespace skiagm {
478 GrContext* GetGr() { return SampleCode::GetGr(); }
479}
480
reed@android.com8a1c16f2008-12-17 15:59:43 +0000481//////////////////////////////////////////////////////////////////////////////
482
reed@android.comf2b98d62010-12-20 18:26:13 +0000483static SkView* curr_view(SkWindow* wind) {
484 SkView::F2BIter iter(wind);
485 return iter.next();
486}
487
Scroggo2c8208f2011-06-15 16:49:08 +0000488void SampleWindow::setZoomCenter(float x, float y)
489{
490 fZoomCenterX = SkFloatToScalar(x);
491 fZoomCenterY = SkFloatToScalar(y);
492}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000493
Scroggo0f185c22011-03-24 18:35:50 +0000494bool SampleWindow::zoomIn()
495{
496 // Arbitrarily decided
497 if (fFatBitsScale == 25) return false;
498 fFatBitsScale++;
499 this->inval(NULL);
500 return true;
501}
502
503bool SampleWindow::zoomOut()
504{
505 if (fFatBitsScale == 1) return false;
506 fFatBitsScale--;
507 this->inval(NULL);
508 return true;
509}
510
Scroggo0f185c22011-03-24 18:35:50 +0000511void SampleWindow::updatePointer(int x, int y)
512{
513 fMouseX = x;
514 fMouseY = y;
515 if (fShowZoomer) {
516 this->inval(NULL);
517 }
518}
519
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000520static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
521 static const SampleWindow::DeviceType gCT[] = {
522 SampleWindow::kPicture_DeviceType,
523 SampleWindow::kGPU_DeviceType,
bsalomon@google.com74913722011-10-27 20:44:19 +0000524 SampleWindow::kRaster_DeviceType, // skip the null gpu device in normal cycling
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000525 SampleWindow::kRaster_DeviceType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000526 };
527 return gCT[ct];
528}
529
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000530SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager) : INHERITED(hwnd) {
reed@google.com3cec4d72011-07-06 13:59:47 +0000531 gSampleWindow = this;
532
yangsu@google.com1f394212011-06-01 18:03:34 +0000533#ifdef PIPE_FILE
534 //Clear existing file or create file if it doesn't exist
535 FILE* f = fopen(FILE_PATH, "wb");
536 fclose(f);
537#endif
538
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539 fPicture = NULL;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000540
reed@android.comf2b98d62010-12-20 18:26:13 +0000541#ifdef DEFAULT_TO_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000542 fDeviceType = kGPU_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000543#else
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000544 fDeviceType = kRaster_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000545#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000546 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000547 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000548 fRepeatDrawing = false;
549 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000550 fRotate = false;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000551 fPerspAnim = false;
552 fPerspAnimTime = 0;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000553 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000554 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000555 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000556 fMeasureFPS = false;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000557 fLCDState = SkOSMenu::kMixedState;
558 fAAState = SkOSMenu::kMixedState;
559 fFilterState = SkOSMenu::kMixedState;
560 fHintingState = SkOSMenu::kMixedState;
reed@google.com569e0432011-04-05 13:07:03 +0000561 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000562 fScrollTestX = fScrollTestY = 0;
563
Scroggo0f185c22011-03-24 18:35:50 +0000564 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000565 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000566 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
567 fShowZoomer = false;
yangsu@google.com921091f2011-08-02 13:39:12 +0000568
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000569 fZoomLevel = 0;
570 fZoomScale = SK_Scalar1;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000571
572 fMagnify = false;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000573 fDebugger = false;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000574
Scroggo8ac0d542011-06-21 14:44:57 +0000575 fSaveToPdf = false;
576 fPdfCanvas = NULL;
577
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000578 fTransitionNext = 6;
579 fTransitionPrev = 2;
580
yangsu@google.com921091f2011-08-02 13:39:12 +0000581 int sinkID = this->getSinkID();
582 fAppMenu.setTitle("Global Settings");
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000583 int itemID;
584
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000585 itemID =fAppMenu.appendList("Device Type", "Device Type", sinkID, 0,
586 "Raster", "Picture", "OpenGL", NULL);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000587 fAppMenu.assignKeyEquivalentToItem(itemID, 'd');
588 itemID = fAppMenu.appendTriState("AA", "AA", sinkID, fAAState);
589 fAppMenu.assignKeyEquivalentToItem(itemID, 'b');
590 itemID = fAppMenu.appendTriState("LCD", "LCD", sinkID, fLCDState);
591 fAppMenu.assignKeyEquivalentToItem(itemID, 'l');
592 itemID = fAppMenu.appendTriState("Filter", "Filter", sinkID, fFilterState);
593 fAppMenu.assignKeyEquivalentToItem(itemID, 'n');
594 itemID = fAppMenu.appendTriState("Hinting", "Hinting", sinkID, fHintingState);
595 fAppMenu.assignKeyEquivalentToItem(itemID, 'h');
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000596 fUsePipeMenuItemID = fAppMenu.appendSwitch("Pipe", "Pipe" , sinkID, fUsePipe);
597 fAppMenu.assignKeyEquivalentToItem(fUsePipeMenuItemID, 'p');
598#ifdef DEBUGGER
599 itemID = fAppMenu.appendSwitch("Debugger", "Debugger", sinkID, fDebugger);
600 fAppMenu.assignKeyEquivalentToItem(itemID, 'q');
601#endif
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000602 itemID = fAppMenu.appendSwitch("Slide Show", "Slide Show" , sinkID, false);
603 fAppMenu.assignKeyEquivalentToItem(itemID, 'a');
604 itemID = fAppMenu.appendSwitch("Clip", "Clip" , sinkID, fUseClip);
605 fAppMenu.assignKeyEquivalentToItem(itemID, 'c');
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000606 itemID = fAppMenu.appendSwitch("Flip X", "Flip X" , sinkID, false);
607 fAppMenu.assignKeyEquivalentToItem(itemID, 'x');
608 itemID = fAppMenu.appendSwitch("Flip Y", "Flip Y" , sinkID, false);
609 fAppMenu.assignKeyEquivalentToItem(itemID, 'y');
610 itemID = fAppMenu.appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
611 fAppMenu.assignKeyEquivalentToItem(itemID, 'z');
612 itemID = fAppMenu.appendSwitch("Magnify", "Magnify" , sinkID, fMagnify);
613 fAppMenu.assignKeyEquivalentToItem(itemID, 'm');
614 itemID =fAppMenu.appendList("Transition-Next", "Transition-Next", sinkID,
615 fTransitionNext, "Up", "Up and Right", "Right",
616 "Down and Right", "Down", "Down and Left",
617 "Left", "Up and Left", NULL);
618 fAppMenu.assignKeyEquivalentToItem(itemID, 'j');
619 itemID =fAppMenu.appendList("Transition-Prev", "Transition-Prev", sinkID,
620 fTransitionPrev, "Up", "Up and Right", "Right",
621 "Down and Right", "Down", "Down and Left",
622 "Left", "Up and Left", NULL);
623 fAppMenu.assignKeyEquivalentToItem(itemID, 'k');
624 itemID = fAppMenu.appendAction("Save to PDF", sinkID);
625 fAppMenu.assignKeyEquivalentToItem(itemID, 'e');
yangsu@google.com921091f2011-08-02 13:39:12 +0000626
627 this->addMenu(&fAppMenu);
628 this->addMenu(&fSlideMenu);
629
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000630// this->setConfig(SkBitmap::kRGB_565_Config);
631 this->setConfig(SkBitmap::kARGB_8888_Config);
632 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000633 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000634
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000635 SkGMRegistyToSampleRegistry();
reed@android.com34245c72009-11-03 04:00:48 +0000636 {
637 const SkViewRegister* reg = SkViewRegister::Head();
638 while (reg) {
639 *fSamples.append() = reg->factory();
640 reg = reg->next();
641 }
642 }
643 fCurrIndex = 0;
senorblanco@chromium.org78b82532011-06-28 19:44:03 +0000644 if (argc > 1) {
645 int i, count = fSamples.count();
646 for (i = 0; i < count; i++) {
647 SkString title = getSampleTitle(i);
648 if (title.equals(argv[1])) {
649 fCurrIndex = i;
650 break;
651 }
652 }
653 if (i == count) {
654 fprintf(stderr, "Unknown sample \"%s\"\n", argv[1]);
655 }
656 }
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000657 this->loadView((*fSamples[fCurrIndex])());
bsalomon@google.com840e9f32011-07-06 21:59:09 +0000658
659 fPDFData = NULL;
reed@google.comf0b5f682011-03-11 20:08:25 +0000660
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000661 if (NULL == devManager) {
662 fDevManager = new DefaultDeviceManager();
663 } else {
664 devManager->ref();
665 fDevManager = devManager;
666 }
667 fDevManager->init(this);
668
Scroggob4490c72011-06-17 13:53:05 +0000669 // If another constructor set our dimensions, ensure that our
670 // onSizeChange gets called.
671 if (this->height() && this->width()) {
672 this->onSizeChange();
673 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000674}
675
676SampleWindow::~SampleWindow() {
677 delete fPicture;
Scroggo8ac0d542011-06-21 14:44:57 +0000678 delete fPdfCanvas;
Scroggo0f185c22011-03-24 18:35:50 +0000679 fTypeface->unref();
reed@google.com29038ed2011-07-06 17:56:47 +0000680
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000681 SkSafeUnref(fDevManager);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000682}
683
reed@android.com55e76b22009-11-23 21:46:47 +0000684static SkBitmap capture_bitmap(SkCanvas* canvas) {
685 SkBitmap bm;
686 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
687 src.copyTo(&bm, src.config());
688 return bm;
689}
690
691static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
692 SkBitmap* diff) {
693 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000694
reed@android.com55e76b22009-11-23 21:46:47 +0000695 SkAutoLockPixels alp0(src);
696 SkAutoLockPixels alp1(orig);
697 for (int y = 0; y < src.height(); y++) {
698 const void* srcP = src.getAddr(0, y);
699 const void* origP = orig.getAddr(0, y);
700 size_t bytes = src.width() * src.bytesPerPixel();
701 if (memcmp(srcP, origP, bytes)) {
702 SkDebugf("---------- difference on line %d\n", y);
703 return true;
704 }
705 }
706 return false;
707}
708
Scroggo0f185c22011-03-24 18:35:50 +0000709static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
710{
711 SkColor desiredColor = paint.getColor();
712 paint.setColor(SK_ColorWHITE);
713 const char* c_str = string.c_str();
714 size_t size = string.size();
715 SkRect bounds;
716 paint.measureText(c_str, size, &bounds);
717 bounds.offset(left, top);
718 SkScalar inset = SkIntToScalar(-2);
719 bounds.inset(inset, inset);
720 canvas->drawRect(bounds, paint);
721 if (desiredColor != SK_ColorBLACK) {
722 paint.setColor(SK_ColorBLACK);
723 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
724 }
725 paint.setColor(desiredColor);
726 canvas->drawText(c_str, size, left, top, paint);
727}
728
reed@android.com44177402009-11-23 21:07:51 +0000729#define XCLIP_N 8
730#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000731
732void SampleWindow::draw(SkCanvas* canvas) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000733 if (!fDevManager->prepareCanvas(fDeviceType, canvas, this)) {
734 return;
735 }
reed@android.com44177402009-11-23 21:07:51 +0000736 // update the animation time
bsalomon@google.come8f09102011-09-08 18:48:12 +0000737 if (!gAnimTimePrev && !gAnimTime) {
738 // first time make delta be 0
739 gAnimTime = SkTime::GetMSecs();
740 gAnimTimePrev = gAnimTime;
741 } else {
742 gAnimTimePrev = gAnimTime;
743 gAnimTime = SkTime::GetMSecs();
744 }
reed@android.com44177402009-11-23 21:07:51 +0000745
Scroggo2c8208f2011-06-15 16:49:08 +0000746 SkScalar cx = fZoomCenterX;
747 SkScalar cy = fZoomCenterY;
reed@google.com569e0432011-04-05 13:07:03 +0000748
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000749 if (fZoomLevel) {
750 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000751 SkPoint center;
752 m = canvas->getTotalMatrix();//.invert(&m);
753 m.mapXY(cx, cy, &center);
754 cx = center.fX;
755 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000756
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000757 m.setTranslate(-cx, -cy);
758 m.postScale(fZoomScale, fZoomScale);
759 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000760
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000761 canvas->concat(m);
762 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000763
reed@google.com569e0432011-04-05 13:07:03 +0000764 if (fFlipAxis) {
765 SkMatrix m;
766 m.setTranslate(cx, cy);
767 if (fFlipAxis & kFlipAxis_X) {
768 m.preScale(-SK_Scalar1, SK_Scalar1);
769 }
770 if (fFlipAxis & kFlipAxis_Y) {
771 m.preScale(SK_Scalar1, -SK_Scalar1);
772 }
773 m.preTranslate(-cx, -cy);
774 canvas->concat(m);
775 }
yangsu@google.com921091f2011-08-02 13:39:12 +0000776
reed@google.com52f57e12011-03-16 12:10:02 +0000777 // Apply any gesture matrix
778 if (true) {
779 const SkMatrix& localM = fGesture.localM();
780 if (localM.getType() & SkMatrix::kScale_Mask) {
781 canvas->setExternalMatrix(&localM);
782 }
783 canvas->concat(localM);
784 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000785
reed@google.com52f57e12011-03-16 12:10:02 +0000786 if (fGesture.isActive()) {
787 this->inval(NULL);
788 }
789 }
yangsu@google.com921091f2011-08-02 13:39:12 +0000790
reed@android.come522ca52009-11-23 20:10:41 +0000791 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000792 this->INHERITED::draw(canvas);
793 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000794
795 const SkScalar w = this->width();
796 const SkScalar h = this->height();
797 const SkScalar cw = w / XCLIP_N;
798 const SkScalar ch = h / YCLIP_N;
799 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000800 SkRect r;
801 r.fTop = y * ch;
802 r.fBottom = (y + 1) * ch;
803 if (y == YCLIP_N - 1) {
804 r.fBottom = h;
805 }
reed@android.come522ca52009-11-23 20:10:41 +0000806 for (int x = 0; x < XCLIP_N; x++) {
807 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000808 r.fLeft = x * cw;
809 r.fRight = (x + 1) * cw;
810 if (x == XCLIP_N - 1) {
811 r.fRight = w;
812 }
reed@android.come522ca52009-11-23 20:10:41 +0000813 canvas->clipRect(r);
814 this->INHERITED::draw(canvas);
815 }
816 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000817
reed@android.com55e76b22009-11-23 21:46:47 +0000818 SkBitmap diff;
819 if (bitmap_diff(canvas, orig, &diff)) {
820 }
reed@android.come522ca52009-11-23 20:10:41 +0000821 } else {
822 this->INHERITED::draw(canvas);
823 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000824 if (fShowZoomer && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +0000825 showZoomer(canvas);
826 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000827 if (fMagnify && !fSaveToPdf) {
828 magnify(canvas);
829 }
yangsu@google.com921091f2011-08-02 13:39:12 +0000830
reed@google.com29038ed2011-07-06 17:56:47 +0000831 // do this last
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000832 fDevManager->publishCanvas(fDeviceType, canvas, this);
Scroggo3272ba82011-05-25 20:50:42 +0000833}
834
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000835static float clipW = 200;
836static float clipH = 200;
837void SampleWindow::magnify(SkCanvas* canvas) {
838 SkRect r;
839 int count = canvas->save();
840
841 SkMatrix m = canvas->getTotalMatrix();
842 m.invert(&m);
843 SkPoint offset, center;
bsalomon@google.com820e80a2011-10-24 21:09:40 +0000844 SkScalar mouseX = fMouseX * SK_Scalar1;
845 SkScalar mouseY = fMouseY * SK_Scalar1;
846 m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset);
847 m.mapXY(mouseX, mouseY, &center);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000848
849 r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX());
850 r.offset(offset.fX, offset.fY);
851
852 SkPaint paint;
853 paint.setColor(0xFF66AAEE);
854 paint.setStyle(SkPaint::kStroke_Style);
bsalomon@google.com820e80a2011-10-24 21:09:40 +0000855 paint.setStrokeWidth(10.f * m.getScaleX());
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000856 //lense offset
857 //canvas->translate(0, -250);
858 canvas->drawRect(r, paint);
859 canvas->clipRect(r);
860
861 m = canvas->getTotalMatrix();
862 m.setTranslate(-center.fX, -center.fY);
bsalomon@google.com820e80a2011-10-24 21:09:40 +0000863 m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000864 m.postTranslate(center.fX, center.fY);
865 canvas->concat(m);
866
867 this->INHERITED::draw(canvas);
868
869 canvas->restoreToCount(count);
870}
871
Scroggo3272ba82011-05-25 20:50:42 +0000872void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +0000873 int count = canvas->save();
874 canvas->resetMatrix();
875 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000876 int width = SkScalarRound(this->width());
877 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000878 if (fMouseX >= width) fMouseX = width - 1;
879 else if (fMouseX < 0) fMouseX = 0;
880 if (fMouseY >= height) fMouseY = height - 1;
881 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000882
Scroggo0f185c22011-03-24 18:35:50 +0000883 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000884 bitmap.lockPixels();
885
Scroggo0f185c22011-03-24 18:35:50 +0000886 // Find the size of the zoomed in view, forced to be odd, so the examined pixel is in the middle.
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000887 int zoomedWidth = (width >> 1) | 1;
888 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000889 SkIRect src;
890 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
891 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
892 SkRect dest;
893 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
894 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
895 SkPaint paint;
896 // Clear the background behind our zoomed in view
897 paint.setColor(SK_ColorWHITE);
898 canvas->drawRect(dest, paint);
899 canvas->drawBitmapRect(bitmap, &src, dest);
900 paint.setColor(SK_ColorBLACK);
901 paint.setStyle(SkPaint::kStroke_Style);
902 // Draw a border around the pixel in the middle
903 SkRect originalPixel;
904 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
905 SkMatrix matrix;
906 SkRect scalarSrc;
907 scalarSrc.set(src);
908 SkColor color = bitmap.getColor(fMouseX, fMouseY);
909 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
910 SkRect pixel;
911 matrix.mapRect(&pixel, originalPixel);
912 // TODO Perhaps measure the values and make the outline white if it's "dark"
913 if (color == SK_ColorBLACK) {
914 paint.setColor(SK_ColorWHITE);
915 }
916 canvas->drawRect(pixel, paint);
917 }
918 paint.setColor(SK_ColorBLACK);
919 // Draw a border around the destination rectangle
920 canvas->drawRect(dest, paint);
921 paint.setStyle(SkPaint::kStrokeAndFill_Style);
922 // Identify the pixel and its color on screen
923 paint.setTypeface(fTypeface);
924 paint.setAntiAlias(true);
925 SkScalar lineHeight = paint.getFontMetrics(NULL);
926 SkString string;
927 string.appendf("(%i, %i)", fMouseX, fMouseY);
928 SkScalar left = dest.fLeft + SkIntToScalar(3);
929 SkScalar i = SK_Scalar1;
930 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
931 // Alpha
932 i += SK_Scalar1;
933 string.reset();
934 string.appendf("A: %X", SkColorGetA(color));
935 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
936 // Red
937 i += SK_Scalar1;
938 string.reset();
939 string.appendf("R: %X", SkColorGetR(color));
940 paint.setColor(SK_ColorRED);
941 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
942 // Green
943 i += SK_Scalar1;
944 string.reset();
945 string.appendf("G: %X", SkColorGetG(color));
946 paint.setColor(SK_ColorGREEN);
947 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
948 // Blue
949 i += SK_Scalar1;
950 string.reset();
951 string.appendf("B: %X", SkColorGetB(color));
952 paint.setColor(SK_ColorBLUE);
953 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
954 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +0000955}
956
reed@android.com8a1c16f2008-12-17 15:59:43 +0000957void SampleWindow::onDraw(SkCanvas* canvas) {
958 if (fRepeatDrawing) {
959 this->inval(NULL);
960 }
961}
962
963#include "SkColorPriv.h"
964
965static void reverseRedAndBlue(const SkBitmap& bm) {
966 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
967 uint8_t* p = (uint8_t*)bm.getPixels();
968 uint8_t* stop = p + bm.getSize();
969 while (p < stop) {
970 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
971 unsigned scale = SkAlpha255To256(p[3]);
972 unsigned r = p[2];
973 unsigned b = p[0];
974 p[0] = SkAlphaMul(r, scale);
975 p[1] = SkAlphaMul(p[1], scale);
976 p[2] = SkAlphaMul(b, scale);
977 p += 4;
978 }
979}
980
Scroggo8ac0d542011-06-21 14:44:57 +0000981void SampleWindow::saveToPdf()
982{
983 fSaveToPdf = true;
984 this->inval(NULL);
985}
986
reed@android.com8a1c16f2008-12-17 15:59:43 +0000987SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +0000988 if (fSaveToPdf) {
989 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
990 SkISize size = SkISize::Make(bmp.width(), bmp.height());
991 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
992 canvas->getTotalMatrix());
993 fPdfCanvas = new SkCanvas(pdfDevice);
994 pdfDevice->unref();
995 canvas = fPdfCanvas;
996 } else {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000997 switch (fDeviceType) {
998 case kRaster_DeviceType:
999 case kGPU_DeviceType:
reed@android.comf2b98d62010-12-20 18:26:13 +00001000 canvas = this->INHERITED::beforeChildren(canvas);
Scroggo8ac0d542011-06-21 14:44:57 +00001001 break;
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001002 case kPicture_DeviceType:
Scroggo8ac0d542011-06-21 14:44:57 +00001003 fPicture = new SkPicture;
1004 canvas = fPicture->beginRecording(9999, 9999);
1005 break;
reed@google.comac10a2d2010-12-22 21:39:39 +00001006 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001007 }
1008
1009 if (fUseClip) {
1010 canvas->drawColor(0xFFFF88FF);
reed@google.com045e62d2011-10-24 12:19:46 +00001011 canvas->clipPath(fClipPath, SkRegion::kIntersect_Op, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001012 }
1013
1014 return canvas;
1015}
1016
1017static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
1018 const SkRegion& rgn) {
1019 SkCanvas canvas(bm);
1020 SkRegion inval(rgn);
1021
1022 inval.translate(r.fLeft, r.fTop);
1023 canvas.clipRegion(inval);
1024 canvas.drawColor(0xFFFF8080);
1025}
yangsu@google.com501775e2011-06-24 16:04:50 +00001026#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001027void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +00001028 if (fSaveToPdf) {
1029 fSaveToPdf = false;
1030 if (fShowZoomer) {
1031 showZoomer(fPdfCanvas);
1032 }
1033 SkString name;
1034 name.printf("%s.pdf", this->getTitle());
1035 SkPDFDocument doc;
1036 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
1037 doc.appendPage(device);
1038#ifdef ANDROID
1039 name.prepend("/sdcard/");
1040#endif
yangsu@google.com501775e2011-06-24 16:04:50 +00001041
1042#ifdef SK_BUILD_FOR_IOS
1043 SkDynamicMemoryWStream mstream;
1044 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +00001045 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +00001046#endif
Scroggo8ac0d542011-06-21 14:44:57 +00001047 SkFILEWStream stream(name.c_str());
1048 if (stream.isValid()) {
1049 doc.emitPDF(&stream);
1050 const char* desc = "File saved from Skia SampleApp";
1051 this->onPDFSaved(this->getTitle(), desc, name.c_str());
1052 }
yangsu@google.com501775e2011-06-24 16:04:50 +00001053
Scroggo8ac0d542011-06-21 14:44:57 +00001054 delete fPdfCanvas;
1055 fPdfCanvas = NULL;
1056
1057 // We took over the draw calls in order to create the PDF, so we need
1058 // to redraw.
1059 this->inval(NULL);
1060 return;
1061 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001062
reed@android.comf2b98d62010-12-20 18:26:13 +00001063 if (fRequestGrabImage) {
1064 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001065
reed@google.com29038ed2011-07-06 17:56:47 +00001066 SkDevice* device = orig->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001067 SkBitmap bmp;
1068 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001069 static int gSampleGrabCounter;
1070 SkString name;
1071 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001072 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +00001073 SkImageEncoder::kPNG_Type, 100);
1074 }
1075 }
1076
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001077 if (kPicture_DeviceType == fDeviceType) {
1078 if (true) {
1079 SkPicture* pict = new SkPicture(*fPicture);
1080 fPicture->unref();
1081 orig->drawPicture(*pict);
1082 pict->unref();
1083 } else if (true) {
1084 SkDynamicMemoryWStream ostream;
1085 fPicture->serialize(&ostream);
1086 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001087
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001088 SkAutoDataUnref data(ostream.copyToData());
1089 SkMemoryStream istream(data.data(), data.size());
1090 SkPicture pict(&istream);
1091 orig->drawPicture(pict);
1092 } else {
1093 fPicture->draw(orig);
1094 fPicture->unref();
1095 }
1096 fPicture = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001097 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001098
reed@google.com17d7aec2011-04-25 14:31:44 +00001099 // Do this after presentGL and other finishing, rather than in afterChild
1100 if (fMeasureFPS && fMeasureFPS_Time) {
1101 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
1102 this->updateTitle();
Scroggo62b65b02011-06-21 16:01:26 +00001103 this->postInvalDelay();
reed@google.com17d7aec2011-04-25 14:31:44 +00001104 }
1105
1106 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +00001107 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001108 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
1109 int dx = fScrollTestX * 7;
1110 int dy = fScrollTestY * 7;
1111 SkIRect r;
1112 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001113
reed@android.com8a1c16f2008-12-17 15:59:43 +00001114 r.set(50, 50, 50+100, 50+100);
1115 bm.scrollRect(&r, dx, dy, &inval);
1116 paint_rgn(bm, r, inval);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001117 }
1118#ifdef DEBUGGER
1119 SkView* curr = curr_view(this);
1120 if (fDebugger && !is_debugger(curr) && !is_transition(curr) && !is_overview(curr)) {
1121 //Stop Pipe when fDebugger is active
1122 fUsePipe = false;
1123 (void)SampleView::SetUsePipe(curr, false);
1124 fAppMenu.getItemByID(fUsePipeMenuItemID)->setBool(fUsePipe);
1125 this->onUpdateMenu(&fAppMenu);
1126
1127 //Reset any transformations
1128 fGesture.stop();
1129 fGesture.reset();
1130
1131 this->loadView(create_debugger(gTempDataStore.begin(),
1132 gTempDataStore.count()));
1133 }
1134#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001135}
1136
reed@android.com6c5f6f22009-08-14 16:08:38 +00001137void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
1138 if (fScale) {
1139 SkScalar scale = SK_Scalar1 * 7 / 10;
1140 SkScalar cx = this->width() / 2;
1141 SkScalar cy = this->height() / 2;
1142 canvas->translate(cx, cy);
1143 canvas->scale(scale, scale);
1144 canvas->translate(-cx, -cy);
1145 }
1146 if (fRotate) {
1147 SkScalar cx = this->width() / 2;
1148 SkScalar cy = this->height() / 2;
1149 canvas->translate(cx, cy);
1150 canvas->rotate(SkIntToScalar(30));
1151 canvas->translate(-cx, -cy);
1152 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00001153 if (fPerspAnim) {
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001154 fPerspAnimTime += SampleCode::GetAnimSecondsDelta();
1155
1156 static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
1157 static const SkScalar gAnimMag = SK_Scalar1 / 1000;
1158 SkScalar t = SkScalarMod(fPerspAnimTime, gAnimPeriod);
1159 if (SkScalarFloorToInt(SkScalarDiv(fPerspAnimTime, gAnimPeriod)) & 0x1) {
1160 t = gAnimPeriod - t;
1161 }
1162 t = 2 * t - gAnimPeriod;
1163 t = SkScalarMul(SkScalarDiv(t, gAnimPeriod), gAnimMag);
1164 SkMatrix m;
1165 m.reset();
1166 m.setPerspY(t);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001167 canvas->concat(m);
1168 }
reed@google.comf0b5f682011-03-11 20:08:25 +00001169
reed@google.com09e3baa2011-05-18 12:04:31 +00001170 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
1171 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001172
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001173 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +00001174 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
1175 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
1176 fMeasureFPS_Time = SkTime::GetMSecs();
1177 }
1178 } else {
1179 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001180 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00001181 if (fPerspAnim) {
1182 this->inval(NULL);
1183 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001184 //(void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001185}
1186
1187void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +00001188 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001189}
1190
reed@android.com8a1c16f2008-12-17 15:59:43 +00001191static SkBitmap::Config gConfigCycle[] = {
1192 SkBitmap::kNo_Config, // none -> none
1193 SkBitmap::kNo_Config, // a1 -> none
1194 SkBitmap::kNo_Config, // a8 -> none
1195 SkBitmap::kNo_Config, // index8 -> none
1196 SkBitmap::kARGB_4444_Config, // 565 -> 4444
1197 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
1198 SkBitmap::kRGB_565_Config // 8888 -> 565
1199};
1200
1201static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
1202 return gConfigCycle[c];
1203}
1204
djsollen@google.come32b5832011-06-13 16:58:40 +00001205void SampleWindow::changeZoomLevel(float delta) {
1206 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001207 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001208 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
1209 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001210 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001211 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
1212 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001213 } else {
1214 fZoomScale = SK_Scalar1;
1215 }
1216
djsollen@google.come32b5832011-06-13 16:58:40 +00001217 this->updateTitle();
1218
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001219 this->inval(NULL);
1220}
1221
Scroggo2c8208f2011-06-15 16:49:08 +00001222bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +00001223 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001224 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001225 fTransitionPrev));
Scroggo2c8208f2011-06-15 16:49:08 +00001226 return true;
1227}
1228
reed@android.com8a1c16f2008-12-17 15:59:43 +00001229bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +00001230 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001231 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001232 fTransitionNext));
reed@android.com34245c72009-11-03 04:00:48 +00001233 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001234}
1235
yangsu@google.com501775e2011-06-24 16:04:50 +00001236bool SampleWindow::goToSample(int i) {
1237 fCurrIndex = (i) % fSamples.count();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001238 this->loadView(create_transition(curr_view(this),(*fSamples[fCurrIndex])(), 6));
yangsu@google.com501775e2011-06-24 16:04:50 +00001239 return true;
1240}
1241
1242SkString SampleWindow::getSampleTitle(int i) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001243 SkView* view = (*fSamples[i])();
yangsu@google.com501775e2011-06-24 16:04:50 +00001244 SkString title;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001245 SampleCode::RequestTitle(view, &title);
yangsu@google.com501775e2011-06-24 16:04:50 +00001246 view->unref();
1247 return title;
1248}
1249
1250int SampleWindow::sampleCount() {
1251 return fSamples.count();
1252}
1253
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001254void SampleWindow::showOverview() {
1255 this->loadView(create_transition(curr_view(this),
1256 create_overview(fSamples.count(), fSamples.begin()),
1257 4));
1258}
1259
Scroggo2c8208f2011-06-15 16:49:08 +00001260void SampleWindow::postAnimatingEvent() {
1261 if (fAnimating) {
reed@google.com87fac4a2011-08-04 13:50:17 +00001262 (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
Scroggo2c8208f2011-06-15 16:49:08 +00001263 }
1264}
reed@android.com8a1c16f2008-12-17 15:59:43 +00001265bool SampleWindow::onEvent(const SkEvent& evt) {
1266 if (evt.isType(ANIMATING_EVENTTYPE)) {
1267 if (fAnimating) {
1268 this->nextSample();
1269 this->postAnimatingEvent();
1270 }
1271 return true;
1272 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001273 if (evt.isType("replace-transition-view")) {
1274 this->loadView((SkView*)SkEventSink::FindSink(evt.getFast32()));
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001275 return true;
1276 }
reed@android.com34245c72009-11-03 04:00:48 +00001277 if (evt.isType("set-curr-index")) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001278 this->goToSample(evt.getFast32());
reed@android.com34245c72009-11-03 04:00:48 +00001279 return true;
1280 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001281 if (isInvalEvent(evt)) {
1282 this->inval(NULL);
1283 return true;
1284 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001285 int selected = -1;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001286 if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001287 this->setDeviceType((DeviceType)selected);
1288 return true;
1289 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001290 if (SkOSMenu::FindSwitchState(evt, "Pipe", &fUsePipe)) {
1291#ifdef PIPE_NET
1292 if (!fUsePipe)
1293 gServer.disconnectAll();
1294#endif
1295 (void)SampleView::SetUsePipe(curr_view(this), fUsePipe);
1296 this->updateTitle();
1297 this->inval(NULL);
yangsu@google.com921091f2011-08-02 13:39:12 +00001298 return true;
1299 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001300 if (SkOSMenu::FindSwitchState(evt, "Slide Show", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001301 this->toggleSlideshow();
1302 return true;
1303 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001304 if (SkOSMenu::FindTriState(evt, "AA", &fAAState) ||
1305 SkOSMenu::FindTriState(evt, "LCD", &fLCDState) ||
1306 SkOSMenu::FindTriState(evt, "Filter", &fFilterState) ||
1307 SkOSMenu::FindTriState(evt, "Hinting", &fHintingState) ||
1308 SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) ||
1309 SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
1310 SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify) ||
1311 SkOSMenu::FindListIndex(evt, "Transition-Next", &fTransitionNext) ||
1312 SkOSMenu::FindListIndex(evt, "Transition-Prev", &fTransitionPrev)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001313 this->inval(NULL);
1314 this->updateTitle();
1315 return true;
1316 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001317 if (SkOSMenu::FindSwitchState(evt, "Flip X", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001318 fFlipAxis ^= kFlipAxis_X;
1319 this->updateTitle();
1320 this->inval(NULL);
1321 return true;
1322 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001323 if (SkOSMenu::FindSwitchState(evt, "Flip Y", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001324 fFlipAxis ^= kFlipAxis_Y;
1325 this->updateTitle();
1326 this->inval(NULL);
1327 return true;
1328 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001329 if (SkOSMenu::FindAction(evt,"Save to PDF")) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001330 this->saveToPdf();
1331 return true;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001332 }
1333#ifdef DEBUGGER
1334 if (SkOSMenu::FindSwitchState(evt, "Debugger", &fDebugger)) {
1335 if (fDebugger) {
1336 fUsePipe = true;
1337 (void)SampleView::SetUsePipe(curr_view(this), true);
1338 } else {
1339 this->loadView(fSamples[fCurrIndex]());
1340 }
1341 this->inval(NULL);
1342 return true;
1343 }
1344#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001345 return this->INHERITED::onEvent(evt);
1346}
1347
reed@android.comf2b98d62010-12-20 18:26:13 +00001348bool SampleWindow::onQuery(SkEvent* query) {
1349 if (query->isType("get-slide-count")) {
1350 query->setFast32(fSamples.count());
1351 return true;
1352 }
1353 if (query->isType("get-slide-title")) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001354 SkView* view = (*fSamples[query->getFast32()])();
reed@android.comf2b98d62010-12-20 18:26:13 +00001355 SkEvent evt(gTitleEvtName);
1356 if (view->doQuery(&evt)) {
1357 query->setString("title", evt.findString(gTitleEvtName));
1358 }
1359 SkSafeUnref(view);
1360 return true;
1361 }
1362 if (query->isType("use-fast-text")) {
1363 SkEvent evt(gFastTextEvtName);
1364 return curr_view(this)->doQuery(&evt);
1365 }
reed@google.com29038ed2011-07-06 17:56:47 +00001366 if (query->isType("ignore-window-bitmap")) {
1367 query->setFast32(this->getGrContext() != NULL);
1368 return true;
1369 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001370 return this->INHERITED::onQuery(query);
1371}
1372
reed@android.com0ae6b242008-12-23 16:49:54 +00001373static void cleanup_for_filename(SkString* name) {
1374 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001375 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001376 switch (str[i]) {
1377 case ':': str[i] = '-'; break;
1378 case '/': str[i] = '-'; break;
1379 case ' ': str[i] = '_'; break;
1380 default: break;
1381 }
1382 }
1383}
reed@android.com8a1c16f2008-12-17 15:59:43 +00001384
1385bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001386 {
1387 SkView* view = curr_view(this);
1388 if (view) {
1389 SkEvent evt(gCharEvtName);
1390 evt.setFast32(uni);
1391 if (view->doQuery(&evt)) {
1392 return true;
1393 }
1394 }
1395 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001396
reed@android.com8a1c16f2008-12-17 15:59:43 +00001397 int dx = 0xFF;
1398 int dy = 0xFF;
1399
1400 switch (uni) {
1401 case '5': dx = 0; dy = 0; break;
1402 case '8': dx = 0; dy = -1; break;
1403 case '6': dx = 1; dy = 0; break;
1404 case '2': dx = 0; dy = 1; break;
1405 case '4': dx = -1; dy = 0; break;
1406 case '7': dx = -1; dy = -1; break;
1407 case '9': dx = 1; dy = -1; break;
1408 case '3': dx = 1; dy = 1; break;
1409 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001410
reed@android.com8a1c16f2008-12-17 15:59:43 +00001411 default:
1412 break;
1413 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001414
reed@android.com8a1c16f2008-12-17 15:59:43 +00001415 if (0xFF != dx && 0xFF != dy) {
1416 if ((dx | dy) == 0) {
1417 fScrollTestX = fScrollTestY = 0;
1418 } else {
1419 fScrollTestX += dx;
1420 fScrollTestY += dy;
1421 }
1422 this->inval(NULL);
1423 return true;
1424 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001425
reed@android.com0ae6b242008-12-23 16:49:54 +00001426 switch (uni) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001427 case 'd':
1428 SkGraphics::SetFontCacheUsed(0);
1429 return true;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001430 case 'f':
1431 // only
1432 fMeasureFPS = !fMeasureFPS;
1433 this->updateTitle();
1434 this->inval(NULL);
1435 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001436 case 'g':
1437 fRequestGrabImage = true;
1438 this->inval(NULL);
1439 break;
1440 case 'i':
1441 this->zoomIn();
1442 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001443 case 'o':
1444 this->zoomOut();
1445 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001446 case 'r':
1447 fRotate = !fRotate;
1448 this->inval(NULL);
1449 this->updateTitle();
1450 return true;
bsalomon@google.come8f09102011-09-08 18:48:12 +00001451 case 'k':
1452 fPerspAnim = !fPerspAnim;
1453 this->inval(NULL);
1454 this->updateTitle();
1455 return true;
bsalomon@google.com74913722011-10-27 20:44:19 +00001456 case '\\':
1457 if (fDevManager->supportsDeviceType(kNullGPU_DeviceType)) {
1458 fDeviceType= kNullGPU_DeviceType;
1459 this->inval(NULL);
1460 this->updateTitle();
1461 }
1462 return true;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001463 case 's':
1464 fScale = !fScale;
1465 this->inval(NULL);
1466 this->updateTitle();
1467 return true;
reed@android.com0ae6b242008-12-23 16:49:54 +00001468 default:
1469 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001470 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001471
1472 if (fAppMenu.handleKeyEquivalent(uni)|| fSlideMenu.handleKeyEquivalent(uni)) {
1473 this->onUpdateMenu(&fAppMenu);
1474 this->onUpdateMenu(&fSlideMenu);
1475 return true;
1476 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001477 return this->INHERITED::onHandleChar(uni);
1478}
1479
yangsu@google.com921091f2011-08-02 13:39:12 +00001480void SampleWindow::setDeviceType(DeviceType type) {
1481 if (type != fDeviceType && fDevManager->supportsDeviceType(fDeviceType))
1482 fDeviceType = type;
1483 this->updateTitle();
1484 this->inval(NULL);
1485}
1486
Scroggo2c8208f2011-06-15 16:49:08 +00001487void SampleWindow::toggleSlideshow() {
1488 fAnimating = !fAnimating;
1489 this->postAnimatingEvent();
1490 this->updateTitle();
1491}
1492
1493void SampleWindow::toggleRendering() {
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001494 DeviceType origDevType = fDeviceType;
1495 do {
1496 fDeviceType = cycle_devicetype(fDeviceType);
1497 } while (origDevType != fDeviceType &&
1498 !fDevManager->supportsDeviceType(fDeviceType));
Scroggo2c8208f2011-06-15 16:49:08 +00001499 this->updateTitle();
1500 this->inval(NULL);
1501}
1502
reed@android.com8a1c16f2008-12-17 15:59:43 +00001503#include "SkDumpCanvas.h"
1504
1505bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001506 {
1507 SkView* view = curr_view(this);
1508 if (view) {
1509 SkEvent evt(gKeyEvtName);
1510 evt.setFast32(key);
1511 if (view->doQuery(&evt)) {
1512 return true;
1513 }
1514 }
1515 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001516 switch (key) {
1517 case kRight_SkKey:
1518 if (this->nextSample()) {
1519 return true;
1520 }
1521 break;
1522 case kLeft_SkKey:
Scroggo2c8208f2011-06-15 16:49:08 +00001523 toggleRendering();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001524 return true;
1525 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001526 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001527 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001528 } else {
1529 fNClip = !fNClip;
1530 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001531 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001532 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001533 return true;
1534 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001535 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001536 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001537 } else {
1538 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001539 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001540 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001541 return true;
1542 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001543 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001544 SkDebugfDumper dumper;
1545 SkDumpCanvas dc(&dumper);
1546 this->draw(&dc);
1547 } else {
1548 fRepeatDrawing = !fRepeatDrawing;
1549 if (fRepeatDrawing) {
1550 this->inval(NULL);
1551 }
1552 }
1553 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001554 case kBack_SkKey:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001555 this->showOverview();
reed@android.com34245c72009-11-03 04:00:48 +00001556 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001557 default:
1558 break;
1559 }
1560 return this->INHERITED::onHandleKey(key);
1561}
1562
reed@google.com52f57e12011-03-16 12:10:02 +00001563///////////////////////////////////////////////////////////////////////////////
1564
1565static const char gGestureClickType[] = "GestureClickType";
1566
Scroggod3aed392011-06-22 13:26:56 +00001567bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
1568 void* owner) {
Scroggo0f185c22011-03-24 18:35:50 +00001569 if (Click::kMoved_State == state) {
1570 updatePointer(x, y);
1571 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001572 int w = SkScalarRound(this->width());
1573 int h = SkScalarRound(this->height());
1574
1575 // check for the resize-box
1576 if (w - x < 16 && h - y < 16) {
1577 return false; // let the OS handle the click
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001578 }
1579 else if (fMagnify) {
1580 //it's only necessary to update the drawing if there's a click
1581 this->inval(NULL);
1582 return false; //prevent dragging while magnify is enabled
1583 }
1584 else {
Scroggod3aed392011-06-22 13:26:56 +00001585 return this->INHERITED::onDispatchClick(x, y, state, owner);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001586 }
1587}
1588
reed@google.com52f57e12011-03-16 12:10:02 +00001589class GestureClick : public SkView::Click {
1590public:
1591 GestureClick(SkView* target) : SkView::Click(target) {
1592 this->setType(gGestureClickType);
1593 }
1594
1595 static bool IsGesture(Click* click) {
1596 return click->isType(gGestureClickType);
1597 }
1598};
1599
1600SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1601 return new GestureClick(this);
1602}
1603
1604bool SampleWindow::onClick(Click* click) {
1605 if (GestureClick::IsGesture(click)) {
1606 float x = SkScalarToFloat(click->fCurr.fX);
1607 float y = SkScalarToFloat(click->fCurr.fY);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001608
reed@google.com52f57e12011-03-16 12:10:02 +00001609 switch (click->fState) {
1610 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00001611 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001612 break;
1613 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00001614 fGesture.touchMoved(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001615 this->inval(NULL);
1616 break;
1617 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00001618 fGesture.touchEnd(click->fOwner);
reed@google.com52f57e12011-03-16 12:10:02 +00001619 this->inval(NULL);
1620 break;
1621 }
1622 return true;
1623 }
1624 return false;
1625}
1626
1627///////////////////////////////////////////////////////////////////////////////
1628
reed@android.com8a1c16f2008-12-17 15:59:43 +00001629void SampleWindow::loadView(SkView* view) {
1630 SkView::F2BIter iter(this);
1631 SkView* prev = iter.next();
1632 if (prev) {
1633 prev->detachFromParent();
1634 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001635
reed@android.com8a1c16f2008-12-17 15:59:43 +00001636 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001637 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001638 this->attachChildToFront(view)->unref();
1639 view->setSize(this->width(), this->height());
1640
yangsu@google.com921091f2011-08-02 13:39:12 +00001641 //repopulate the slide menu when a view is loaded
1642 fSlideMenu.reset();
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001643#ifdef DEBUGGER
1644 if (!is_debugger(view) && !is_overview(view) && !is_transition(view) && fDebugger) {
1645 //Force Pipe to be on if using debugger
1646 fUsePipe = true;
1647 }
1648#endif
1649 (void)SampleView::SetUsePipe(view, fUsePipe);
yangsu@google.com921091f2011-08-02 13:39:12 +00001650 if (SampleView::IsSampleView(view))
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001651 ((SampleView*)view)->requestMenu(&fSlideMenu);
yangsu@google.com921091f2011-08-02 13:39:12 +00001652 this->onUpdateMenu(&fSlideMenu);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001653 this->updateTitle();
1654}
1655
1656static const char* gConfigNames[] = {
1657 "unknown config",
1658 "A1",
1659 "A8",
1660 "Index8",
1661 "565",
1662 "4444",
1663 "8888"
1664};
1665
1666static const char* configToString(SkBitmap::Config c) {
1667 return gConfigNames[c];
1668}
1669
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001670static const char* gDeviceTypePrefix[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001671 "raster: ",
1672 "picture: ",
bsalomon@google.com74913722011-10-27 20:44:19 +00001673 "opengl: ",
1674 "null-gl: "
reed@android.com8a1c16f2008-12-17 15:59:43 +00001675};
1676
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001677static const char* trystate_str(SkOSMenu::TriState state,
reed@google.com569e0432011-04-05 13:07:03 +00001678 const char trueStr[], const char falseStr[]) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001679 if (SkOSMenu::kOnState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00001680 return trueStr;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001681 } else if (SkOSMenu::kOffState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00001682 return falseStr;
1683 }
1684 return NULL;
1685}
1686
reed@android.com8a1c16f2008-12-17 15:59:43 +00001687void SampleWindow::updateTitle() {
1688 SkString title;
1689
1690 SkView::F2BIter iter(this);
1691 SkView* view = iter.next();
1692 SkEvent evt(gTitleEvtName);
1693 if (view->doQuery(&evt)) {
1694 title.set(evt.findString(gTitleEvtName));
1695 }
1696 if (title.size() == 0) {
1697 title.set("<unknown>");
1698 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001699
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001700 title.prepend(gDeviceTypePrefix[fDeviceType]);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001701
1702 title.prepend(" ");
1703 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001704
reed@android.com8a1c16f2008-12-17 15:59:43 +00001705 if (fAnimating) {
1706 title.prepend("<A> ");
1707 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001708 if (fScale) {
1709 title.prepend("<S> ");
1710 }
1711 if (fRotate) {
1712 title.prepend("<R> ");
1713 }
reed@android.come522ca52009-11-23 20:10:41 +00001714 if (fNClip) {
1715 title.prepend("<C> ");
1716 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00001717 if (fPerspAnim) {
1718 title.prepend("<K> ");
1719 }
reed@google.com569e0432011-04-05 13:07:03 +00001720
1721 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1722 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001723 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001724 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1725 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001726
1727 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001728 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001729 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001730
1731 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001732 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001733 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001734 if (fUsePipe && SampleView::IsSampleView(view)) {
1735 title.prepend("<P> ");
1736 }
1737 if (SampleView::IsSampleView(view)) {
1738 title.prepend("! ");
1739 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001740
reed@android.com8a1c16f2008-12-17 15:59:43 +00001741 this->setTitle(title.c_str());
1742}
1743
1744void SampleWindow::onSizeChange() {
1745 this->INHERITED::onSizeChange();
reed@google.com29038ed2011-07-06 17:56:47 +00001746
reed@android.com8a1c16f2008-12-17 15:59:43 +00001747 SkView::F2BIter iter(this);
1748 SkView* view = iter.next();
1749 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001750
reed@android.com8a1c16f2008-12-17 15:59:43 +00001751 // rebuild our clippath
1752 {
1753 const SkScalar W = this->width();
1754 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001755
reed@android.com8a1c16f2008-12-17 15:59:43 +00001756 fClipPath.reset();
1757#if 0
1758 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1759 SkRect r;
1760 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1761 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1762 fClipPath.addRect(r);
1763 }
1764#else
1765 SkRect r;
1766 r.set(0, 0, W, H);
1767 fClipPath.addRect(r, SkPath::kCCW_Direction);
1768 r.set(W/4, H/4, W*3/4, H*3/4);
1769 fClipPath.addRect(r, SkPath::kCW_Direction);
1770#endif
1771 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001772
Scroggo2c8208f2011-06-15 16:49:08 +00001773 fZoomCenterX = SkScalarHalf(this->width());
1774 fZoomCenterY = SkScalarHalf(this->height());
1775
Scroggo3e7ff9f2011-06-16 15:31:26 +00001776#ifdef ANDROID
Scroggob4490c72011-06-17 13:53:05 +00001777 // FIXME: The first draw after a size change does not work on Android, so
1778 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00001779 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00001780#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001781 this->updateTitle(); // to refresh our config
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001782 fDevManager->windowSizeChanged(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001783}
1784
1785///////////////////////////////////////////////////////////////////////////////
1786
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001787static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001788static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001789static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001790
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001791bool SampleView::IsSampleView(SkView* view) {
1792 SkEvent evt(is_sample_view_tag);
1793 return view->doQuery(&evt);
1794}
1795
reed@google.comf2183392011-04-22 14:10:48 +00001796bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001797 SkEvent evt(repeat_count_tag);
1798 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001799 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001800}
1801
reed@google.com0faac1e2011-05-11 05:58:58 +00001802bool SampleView::SetUsePipe(SkView* view, bool pred) {
1803 SkEvent evt(set_use_pipe_tag);
1804 evt.setFast32(pred);
1805 return view->doEvent(evt);
1806}
1807
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001808bool SampleView::onEvent(const SkEvent& evt) {
1809 if (evt.isType(repeat_count_tag)) {
1810 fRepeatCount = evt.getFast32();
1811 return true;
1812 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001813 if (evt.isType(set_use_pipe_tag)) {
1814 fUsePipe = !!evt.getFast32();
1815 return true;
1816 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001817 return this->INHERITED::onEvent(evt);
1818}
1819
1820bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001821 if (evt->isType(is_sample_view_tag)) {
1822 return true;
1823 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001824 return this->INHERITED::onQuery(evt);
1825}
1826
reed@google.com68f456d2011-05-02 18:55:39 +00001827#ifdef TEST_GPIPE
1828 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001829
1830class SimplePC : public SkGPipeController {
1831public:
1832 SimplePC(SkCanvas* target);
1833 ~SimplePC();
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001834
1835 /**
1836 * User this method to halt/restart pipe
1837 */
1838 void setWriteToPipe(bool writeToPipe) { fWriteToPipe = writeToPipe; }
reed@google.com64e3eb22011-05-04 14:32:04 +00001839 virtual void* requestBlock(size_t minRequest, size_t* actual);
1840 virtual void notifyWritten(size_t bytes);
1841
1842private:
reed@google.com961ddb02011-05-05 14:03:48 +00001843 SkGPipeReader fReader;
1844 void* fBlock;
1845 size_t fBlockSize;
1846 size_t fBytesWritten;
1847 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001848 SkGPipeReader::Status fStatus;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001849 bool fWriteToPipe;
reed@google.com64e3eb22011-05-04 14:32:04 +00001850
1851 size_t fTotalWritten;
1852};
1853
1854SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1855 fBlock = NULL;
1856 fBlockSize = fBytesWritten = 0;
1857 fStatus = SkGPipeReader::kDone_Status;
1858 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001859 fAtomsWritten = 0;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001860 fWriteToPipe = true;
reed@google.com64e3eb22011-05-04 14:32:04 +00001861}
1862
1863SimplePC::~SimplePC() {
1864// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
reed@google.com0faac1e2011-05-11 05:58:58 +00001865 if (fTotalWritten) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001866 if (fWriteToPipe) {
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001867 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1868 fAtomsWritten, fStatus);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001869#ifdef PIPE_FILE
1870 //File is open in append mode
1871 FILE* f = fopen(FILE_PATH, "ab");
1872 SkASSERT(f != NULL);
1873 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
1874 fclose(f);
1875#endif
1876#ifdef PIPE_NET
1877 if (fAtomsWritten > 1 && fTotalWritten > 4) { //ignore done
1878 gServer.acceptConnections();
1879 gServer.writePacket(fBlock, fTotalWritten);
1880 }
1881#endif
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001882#ifdef DEBUGGER
1883 gTempDataStore.reset();
1884 gTempDataStore.append(fTotalWritten, (const char*)fBlock);
1885#endif
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001886 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001887 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001888 sk_free(fBlock);
reed@google.com64e3eb22011-05-04 14:32:04 +00001889}
1890
1891void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1892 sk_free(fBlock);
1893
1894 fBlockSize = minRequest * 4;
1895 fBlock = sk_malloc_throw(fBlockSize);
1896 fBytesWritten = 0;
1897 *actual = fBlockSize;
1898 return fBlock;
1899}
1900
1901void SimplePC::notifyWritten(size_t bytes) {
1902 SkASSERT(fBytesWritten + bytes <= fBlockSize);
reed@google.com64e3eb22011-05-04 14:32:04 +00001903 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1904 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1905 fBytesWritten += bytes;
1906 fTotalWritten += bytes;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001907
reed@google.com961ddb02011-05-05 14:03:48 +00001908 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001909}
1910
reed@google.com68f456d2011-05-02 18:55:39 +00001911#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001912
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001913void SampleView::draw(SkCanvas* canvas) {
yangsu@google.comf121b052011-08-08 16:02:51 +00001914#ifdef TEST_GPIPE
reed@google.com0faac1e2011-05-11 05:58:58 +00001915 if (fUsePipe) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001916 SkGPipeWriter writer;
1917 SimplePC controller(canvas);
reed@google.comdde09562011-05-23 12:21:05 +00001918 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
reed@google.comdde09562011-05-23 12:21:05 +00001919 canvas = writer.startRecording(&controller, flags);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001920 //Must draw before controller goes out of scope and sends data
1921 this->INHERITED::draw(canvas);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001922 //explicitly end recording to ensure writer is flushed before the memory
1923 //is freed in the deconstructor of the controller
1924 writer.endRecording();
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001925 controller.setWriteToPipe(fUsePipe);
reed@google.com0faac1e2011-05-11 05:58:58 +00001926 }
yangsu@google.comf121b052011-08-08 16:02:51 +00001927 else
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001928 this->INHERITED::draw(canvas);
yangsu@google.comf121b052011-08-08 16:02:51 +00001929#else
1930 this->INHERITED::draw(canvas);
1931#endif
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001932}
1933void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com81e3d7f2011-06-01 12:42:36 +00001934 this->onDrawBackground(canvas);
1935
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001936 for (int i = 0; i < fRepeatCount; i++) {
1937 SkAutoCanvasRestore acr(canvas, true);
1938 this->onDrawContent(canvas);
1939 }
1940}
1941
1942void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001943 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001944}
1945
1946///////////////////////////////////////////////////////////////////////////////
1947
reed@android.comf2b98d62010-12-20 18:26:13 +00001948template <typename T> void SkTBSort(T array[], int count) {
1949 for (int i = 1; i < count - 1; i++) {
1950 bool didSwap = false;
1951 for (int j = count - 1; j > i; --j) {
1952 if (array[j] < array[j-1]) {
1953 T tmp(array[j-1]);
1954 array[j-1] = array[j];
1955 array[j] = tmp;
1956 didSwap = true;
1957 }
1958 }
1959 if (!didSwap) {
1960 break;
1961 }
1962 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001963
reed@android.comf2b98d62010-12-20 18:26:13 +00001964 for (int k = 0; k < count - 1; k++) {
1965 SkASSERT(!(array[k+1] < array[k]));
1966 }
1967}
1968
1969#include "SkRandom.h"
1970
1971static void rand_rect(SkIRect* rect, SkRandom& rand) {
1972 int bits = 8;
1973 int shift = 32 - bits;
1974 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1975 rand.nextU() >> shift, rand.nextU() >> shift);
1976 rect->sort();
1977}
1978
1979static void dumpRect(const SkIRect& r) {
1980 SkDebugf(" { %d, %d, %d, %d },\n",
1981 r.fLeft, r.fTop,
1982 r.fRight, r.fBottom);
1983}
1984
1985static void test_rects(const SkIRect rect[], int count) {
1986 SkRegion rgn0, rgn1;
1987
1988 for (int i = 0; i < count; i++) {
1989 rgn0.op(rect[i], SkRegion::kUnion_Op);
1990 // dumpRect(rect[i]);
1991 }
1992 rgn1.setRects(rect, count);
1993
1994 if (rgn0 != rgn1) {
1995 SkDebugf("\n");
1996 for (int i = 0; i < count; i++) {
1997 dumpRect(rect[i]);
1998 }
1999 SkDebugf("\n");
2000 }
2001}
2002
2003static void test() {
2004 size_t i;
2005
2006 const SkIRect r0[] = {
2007 { 0, 0, 1, 1 },
2008 { 2, 2, 3, 3 },
2009 };
2010 const SkIRect r1[] = {
2011 { 0, 0, 1, 3 },
2012 { 1, 1, 2, 2 },
2013 { 2, 0, 3, 3 },
2014 };
2015 const SkIRect r2[] = {
2016 { 0, 0, 1, 2 },
2017 { 2, 1, 3, 3 },
2018 { 4, 0, 5, 1 },
2019 { 6, 0, 7, 4 },
2020 };
2021
2022 static const struct {
2023 const SkIRect* fRects;
2024 int fCount;
2025 } gRecs[] = {
2026 { r0, SK_ARRAY_COUNT(r0) },
2027 { r1, SK_ARRAY_COUNT(r1) },
2028 { r2, SK_ARRAY_COUNT(r2) },
2029 };
2030
2031 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
2032 test_rects(gRecs[i].fRects, gRecs[i].fCount);
2033 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002034
reed@android.comf2b98d62010-12-20 18:26:13 +00002035 SkRandom rand;
2036 for (i = 0; i < 10000; i++) {
2037 SkRegion rgn0, rgn1;
2038
2039 const int N = 8;
2040 SkIRect rect[N];
2041 for (int j = 0; j < N; j++) {
2042 rand_rect(&rect[j], rand);
2043 }
2044 test_rects(rect, N);
2045 }
2046}
2047
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00002048SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
reed@android.comf2b98d62010-12-20 18:26:13 +00002049// test();
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002050 return new SampleWindow(hwnd, argc, argv, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002051}
2052
2053void get_preferred_size(int* x, int* y, int* width, int* height) {
2054 *x = 10;
2055 *y = 50;
2056 *width = 640;
2057 *height = 480;
2058}
2059
2060void application_init() {
2061// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002062#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00002063 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002064#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002065 SkGraphics::Init();
2066 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002067}
2068
2069void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002070 SkEvent::Term();
2071 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002072}