blob: a10185300e03a92407295dbfd62352ac8a441172 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Scroggo2c8208f2011-06-15 16:49:08 +00007#include "SampleApp.h"
8
reed@google.com8a85d0c2011-06-24 19:12:12 +00009#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkCanvas.h"
11#include "SkDevice.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkGraphics.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +000013#include "SkImageEncoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkPaint.h"
15#include "SkPicture.h"
16#include "SkStream.h"
reed@android.com44177402009-11-23 21:07:51 +000017#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkWindow.h"
19
20#include "SampleCode.h"
Scroggo0f185c22011-03-24 18:35:50 +000021#include "SkTypeface.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000022
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000023#if SK_SUPPORT_GPU
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000024#include "gl/GrGLInterface.h"
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000025#include "gl/GrGLUtil.h"
bsalomon@google.com583a1e32011-08-17 13:42:46 +000026#include "GrRenderTarget.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000027#include "GrContext.h"
28#include "SkGpuDevice.h"
29#else
30class GrContext;
31#endif
Scroggo2c8208f2011-06-15 16:49:08 +000032
reed@google.com1830c7a2012-06-04 12:05:43 +000033#include "SkOSFile.h"
Scroggo8ac0d542011-06-21 14:44:57 +000034#include "SkPDFDevice.h"
35#include "SkPDFDocument.h"
36#include "SkStream.h"
37
scroggo@google.comb073d922012-06-08 15:35:03 +000038#include "SkGPipe.h"
39#include "SamplePipeControllers.h"
40
reed@google.com1830c7a2012-06-04 12:05:43 +000041extern SampleView* CreateSamplePictFileView(const char filename[]);
42
43class PictFileFactory : public SkViewFactory {
44 SkString fFilename;
45public:
46 PictFileFactory(const SkString& filename) : fFilename(filename) {}
47 virtual SkView* operator() () const SK_OVERRIDE {
48 return CreateSamplePictFileView(fFilename.c_str());
49 }
50};
51
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000052#define PIPE_FILEx
53#ifdef PIPE_FILE
yangsu@google.com1f394212011-06-01 18:03:34 +000054#define FILE_PATH "/path/to/drawing.data"
55#endif
56
yangsu@google.comf121b052011-08-08 16:02:51 +000057#define PIPE_NETx
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000058#ifdef PIPE_NET
59#include "SkSockets.h"
60SkTCPServer gServer;
61#endif
yangsu@google.comef7bdfa2011-08-12 14:27:47 +000062
63#define DEBUGGERx
64#ifdef DEBUGGER
65extern SkView* create_debugger(const char* data, size_t size);
66extern bool is_debugger(SkView* view);
67SkTDArray<char> gTempDataStore;
68#endif
69
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000070
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +000071#define USE_ARROWS_FOR_ZOOM true
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000072
73#if SK_ANGLE
74//#define DEFAULT_TO_ANGLE 1
75#else
76//#define DEFAULT_TO_GPU 1
77#endif
reed@android.comf2b98d62010-12-20 18:26:13 +000078
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000079extern SkView* create_overview(int, const SkViewFactory*[]);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +000080extern bool is_overview(SkView* view);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000081extern SkView* create_transition(SkView*, SkView*, int);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +000082extern bool is_transition(SkView* view);
83
reed@android.com34245c72009-11-03 04:00:48 +000084
reed@android.com8a1c16f2008-12-17 15:59:43 +000085#define ANIMATING_EVENTTYPE "nextSample"
86#define ANIMATING_DELAY 750
87
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000088#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +000089 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000090#else
reed@google.combad8c872011-05-18 20:10:31 +000091 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000092#endif
reed@google.combad8c872011-05-18 20:10:31 +000093#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000094
reed@google.com3cec4d72011-07-06 13:59:47 +000095static SampleWindow* gSampleWindow;
96
reed@google.com2072db82011-11-08 15:18:10 +000097static void postEventToSink(SkEvent* evt, SkEventSink* sink) {
98 evt->setTargetID(sink->getSinkID())->post();
99}
100
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000101///////////////////////////////////////////////////////////////////////////////
102
103static const char* skip_until(const char* str, const char* skip) {
104 if (!str) {
105 return NULL;
106 }
107 return strstr(str, skip);
108}
109
110static const char* skip_past(const char* str, const char* skip) {
111 const char* found = skip_until(str, skip);
112 if (!found) {
113 return NULL;
114 }
115 return found + strlen(skip);
116}
117
118static const char* gPrefFileName = "sampleapp_prefs.txt";
119
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000120static bool readTitleFromPrefs(SkString* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000121 SkFILEStream stream(gPrefFileName);
122 if (!stream.isValid()) {
123 return false;
124 }
125
126 int len = stream.getLength();
127 SkString data(len);
128 stream.read(data.writable_str(), len);
129 const char* s = data.c_str();
130
131 s = skip_past(s, "curr-slide-title");
132 s = skip_past(s, "=");
133 s = skip_past(s, "\"");
134 const char* stop = skip_until(s, "\"");
135 if (stop > s) {
136 title->set(s, stop - s);
137 return true;
138 }
139 return false;
140}
141
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000142static void writeTitleToPrefs(const char* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000143 SkFILEWStream stream(gPrefFileName);
144 SkString data;
145 data.printf("curr-slide-title = \"%s\"\n", title);
146 stream.write(data.c_str(), data.size());
147}
148
149///////////////////////////////////////////////////////////////////////////////
150
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000151class SampleWindow::DefaultDeviceManager : public SampleWindow::DeviceManager {
152public:
153
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000154 DefaultDeviceManager() {
155#if SK_SUPPORT_GPU
156 fCurContext = NULL;
157 fCurIntf = NULL;
158 fCurRenderTarget = NULL;
159 fMSAASampleCount = 0;
160#endif
161 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000162 }
163
164 virtual ~DefaultDeviceManager() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000165#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000166 SkSafeUnref(fCurContext);
167 SkSafeUnref(fCurIntf);
168 SkSafeUnref(fCurRenderTarget);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000169#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000170 }
171
bsalomon@google.com11959252012-04-06 20:13:38 +0000172 virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000173 SkASSERT(kNone_BackEndType == fBackend);
robertphillips@google.com53e96a12012-03-30 14:47:53 +0000174
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000175 fBackend = kNone_BackEndType;
176
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000177#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000178 switch (win->getDeviceType()) {
179 case kRaster_DeviceType:
180 // fallthrough
181 case kPicture_DeviceType:
182 // fallthrough
183 case kGPU_DeviceType:
184 // fallthrough
185 case kNullGPU_DeviceType:
186 // all these guys use the native backend
187 fBackend = kNativeGL_BackEndType;
188 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000189#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000190 case kANGLE_DeviceType:
191 // ANGLE is really the only odd man out
192 fBackend = kANGLE_BackEndType;
193 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000194#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000195 default:
196 SkASSERT(false);
197 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000198 }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000199
bsalomon@google.com11959252012-04-06 20:13:38 +0000200 bool result = win->attach(fBackend, msaaSampleCount);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000201 if (!result) {
bsalomon@google.com74913722011-10-27 20:44:19 +0000202 SkDebugf("Failed to initialize GL");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000203 return;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000204 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000205 fMSAASampleCount = msaaSampleCount;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000206
207 SkASSERT(NULL == fCurIntf);
208 switch (win->getDeviceType()) {
209 case kRaster_DeviceType:
210 // fallthrough
211 case kPicture_DeviceType:
212 // fallthrough
213 case kGPU_DeviceType:
214 // all these guys use the native interface
215 fCurIntf = GrGLCreateNativeInterface();
216 break;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000217#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000218 case kANGLE_DeviceType:
219 fCurIntf = GrGLCreateANGLEInterface();
220 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000221#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000222 case kNullGPU_DeviceType:
223 fCurIntf = GrGLCreateNullInterface();
224 break;
225 default:
226 SkASSERT(false);
227 break;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000228 }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000229
230 SkASSERT(NULL == fCurContext);
231 fCurContext = GrContext::Create(kOpenGL_Shaders_GrEngine,
232 (GrPlatform3DContext) fCurIntf);
233
234 if (NULL == fCurContext || NULL == fCurIntf) {
235 // We need some context and interface to see results
236 SkSafeUnref(fCurContext);
237 SkSafeUnref(fCurIntf);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000238 SkDebugf("Failed to setup 3D");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000239
240 win->detach();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000241 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000242#endif // SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000243 // call windowSizeChanged to create the render target
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000244 this->windowSizeChanged(win);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000245 }
246
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000247 virtual void tearDownBackend(SampleWindow *win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000248#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000249 SkSafeUnref(fCurContext);
250 fCurContext = NULL;
251
252 SkSafeUnref(fCurIntf);
253 fCurIntf = NULL;
254
255 SkSafeUnref(fCurRenderTarget);
256 fCurRenderTarget = NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000257#endif
bsalomon@google.comddd40e52012-04-10 15:56:29 +0000258 win->detach();
259 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000260 }
261
262 virtual bool prepareCanvas(SampleWindow::DeviceType dType,
263 SkCanvas* canvas,
264 SampleWindow* win) {
bsalomon@google.com74913722011-10-27 20:44:19 +0000265 switch (dType) {
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000266 case kRaster_DeviceType:
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000267 // fallthrough
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000268 case kPicture_DeviceType:
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000269 // fallthrough
270#if SK_ANGLE
271 case kANGLE_DeviceType:
272#endif
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000273 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000274#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000275 case kGPU_DeviceType:
276 case kNullGPU_DeviceType:
277 if (fCurContext) {
278 canvas->setDevice(new SkGpuDevice(fCurContext,
279 fCurRenderTarget))->unref();
280 } else {
281 return false;
282 }
283 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000284#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000285 default:
286 SkASSERT(false);
287 return false;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000288 }
289 return true;
290 }
291
292 virtual void publishCanvas(SampleWindow::DeviceType dType,
293 SkCanvas* canvas,
294 SampleWindow* win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000295#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000296 if (fCurContext) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000297 // in case we have queued drawing calls
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000298 fCurContext->flush();
299
300 if (kGPU_DeviceType != dType && kNullGPU_DeviceType != dType) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000301 // need to send the raster bits to the (gpu) window
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000302 fCurContext->setRenderTarget(fCurRenderTarget);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000303 const SkBitmap& bm = win->getBitmap();
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000304 fCurRenderTarget->writePixels(0, 0, bm.width(), bm.height(),
bsalomon@google.com6f379512011-11-16 20:36:03 +0000305 kSkia8888_PM_GrPixelConfig,
306 bm.getPixels(),
307 bm.rowBytes());
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000308 }
309 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000310#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000311
312 win->present();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000313 }
314
315 virtual void windowSizeChanged(SampleWindow* win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000316#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000317 if (fCurContext) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000318 win->attach(fBackend, fMSAASampleCount);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000319
bsalomon@google.come269f212011-11-07 13:29:52 +0000320 GrPlatformRenderTargetDesc desc;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000321 desc.fWidth = SkScalarRound(win->width());
322 desc.fHeight = SkScalarRound(win->height());
bsalomon@google.comc4364992011-11-07 15:54:49 +0000323 desc.fConfig = kSkia8888_PM_GrPixelConfig;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000324 GR_GL_GetIntegerv(fCurIntf, GR_GL_SAMPLES, &desc.fSampleCnt);
325 GR_GL_GetIntegerv(fCurIntf, GR_GL_STENCIL_BITS, &desc.fStencilBits);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000326 GrGLint buffer;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000327 GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
bsalomon@google.come269f212011-11-07 13:29:52 +0000328 desc.fRenderTargetHandle = buffer;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000329
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000330 SkSafeUnref(fCurRenderTarget);
331 fCurRenderTarget = fCurContext->createPlatformRenderTarget(desc);
bsalomon@google.com74913722011-10-27 20:44:19 +0000332 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000333#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000334 }
335
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000336 virtual GrContext* getGrContext() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000337#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000338 return fCurContext;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000339#else
340 return NULL;
341#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000342 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000343
344 virtual GrRenderTarget* getGrRenderTarget() SK_OVERRIDE {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000345#if SK_SUPPORT_GPU
bsalomon@google.com11959252012-04-06 20:13:38 +0000346 return fCurRenderTarget;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000347#else
348 return NULL;
349#endif
bsalomon@google.com11959252012-04-06 20:13:38 +0000350 }
351
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000352private:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000353
354#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000355 GrContext* fCurContext;
356 const GrGLInterface* fCurIntf;
357 GrRenderTarget* fCurRenderTarget;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000358 int fMSAASampleCount;
359#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000360
361 SkOSWindow::SkBackEndTypes fBackend;
362
363 typedef SampleWindow::DeviceManager INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000364};
365
366///////////////
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000367static const char view_inval_msg[] = "view-inval-msg";
368
Scroggo62b65b02011-06-21 16:01:26 +0000369void SampleWindow::postInvalDelay() {
reed@google.com87fac4a2011-08-04 13:50:17 +0000370 (new SkEvent(view_inval_msg, this->getSinkID()))->postDelay(1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000371}
372
373static bool isInvalEvent(const SkEvent& evt) {
374 return evt.isType(view_inval_msg);
375}
376//////////////////
377
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000378SkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func)
379 : fCreateFunc(func) {
380}
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000381
bsalomon@google.com8301de12011-10-31 16:47:13 +0000382SkView* SkFuncViewFactory::operator() () const {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000383 return (*fCreateFunc)();
384}
385
386#include "GMSampleView.h"
387
388SkGMSampleViewFactory::SkGMSampleViewFactory(GMFactoryFunc func)
389 : fFunc(func) {
390}
391
392SkView* SkGMSampleViewFactory::operator() () const {
393 return new GMSampleView(fFunc(NULL));
394}
395
396SkViewRegister* SkViewRegister::gHead;
397SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) {
398 fFact->ref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000399 fChain = gHead;
400 gHead = this;
401}
402
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000403SkViewRegister::SkViewRegister(SkViewCreateFunc func) {
404 fFact = new SkFuncViewFactory(func);
405 fChain = gHead;
406 gHead = this;
407}
408
409SkViewRegister::SkViewRegister(GMFactoryFunc func) {
410 fFact = new SkGMSampleViewFactory(func);
411 fChain = gHead;
412 gHead = this;
413}
414
415class AutoUnrefArray {
416public:
417 AutoUnrefArray() {}
418 ~AutoUnrefArray() {
419 int count = fObjs.count();
420 for (int i = 0; i < count; ++i) {
421 fObjs[i]->unref();
422 }
423 }
424 SkRefCnt*& push_back() { return *fObjs.append(); }
chudy@google.com4605a3f2012-08-01 17:58:01 +0000425
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000426private:
427 SkTDArray<SkRefCnt*> fObjs;
428};
429
430// registers GMs as Samples
431// This can't be performed during static initialization because it could be
432// run before GMRegistry has been fully built.
caryclark@google.com02939ce2012-06-06 12:09:51 +0000433static void SkGMRegistyToSampleRegistry() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000434 static bool gOnce;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000435 static AutoUnrefArray fRegisters;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000436
437 if (!gOnce) {
438 const skiagm::GMRegistry* gmreg = skiagm::GMRegistry::Head();
439 while (gmreg) {
440 fRegisters.push_back() = new SkViewRegister(gmreg->factory());
441 gmreg = gmreg->next();
442 }
443 gOnce = true;
444 }
445}
446
reed@google.com29038ed2011-07-06 17:56:47 +0000447#if 0
reed@google.comf0b5f682011-03-11 20:08:25 +0000448#include <CoreFoundation/CoreFoundation.h>
449#include <CoreFoundation/CFURLAccess.h>
450
451static void testpdf() {
452 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
453 kCFStringEncodingUTF8);
454 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
455 kCFURLPOSIXPathStyle,
456 false);
457 CFRelease(path);
458 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
459 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
460 CFRelease(url);
461
462 CGContextBeginPage(cg, &box);
463 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
464 CGContextFillEllipseInRect(cg, r);
465 CGContextEndPage(cg);
466 CGContextRelease(cg);
467
468 if (false) {
469 SkBitmap bm;
470 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
471 bm.allocPixels();
472 bm.eraseColor(0);
473
474 SkCanvas canvas(bm);
475
476 }
477}
478#endif
479
480//////////////////////////////////////////////////////////////////////////////
481
reed@google.com569e0432011-04-05 13:07:03 +0000482enum FlipAxisEnum {
483 kFlipAxis_X = (1 << 0),
484 kFlipAxis_Y = (1 << 1)
485};
486
reed@google.comf0b5f682011-03-11 20:08:25 +0000487#include "SkDrawFilter.h"
488
reed@google.com569e0432011-04-05 13:07:03 +0000489class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000490public:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000491 FlagsDrawFilter(SkOSMenu::TriState lcd, SkOSMenu::TriState aa, SkOSMenu::TriState filter,
492 SkOSMenu::TriState hinting) :
reed@google.com09e3baa2011-05-18 12:04:31 +0000493 fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000494
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000495 virtual void filter(SkPaint* paint, Type t) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000496 if (kText_Type == t && SkOSMenu::kMixedState != fLCDState) {
497 paint->setLCDRenderText(SkOSMenu::kOnState == fLCDState);
reed@google.com569e0432011-04-05 13:07:03 +0000498 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000499 if (SkOSMenu::kMixedState != fAAState) {
500 paint->setAntiAlias(SkOSMenu::kOnState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000501 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000502 if (SkOSMenu::kMixedState != fFilterState) {
503 paint->setFilterBitmap(SkOSMenu::kOnState == fFilterState);
reed@google.com176753a2011-05-17 15:32:04 +0000504 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000505 if (SkOSMenu::kMixedState != fHintingState) {
506 paint->setHinting(SkOSMenu::kOnState == fHintingState ?
reed@google.com09e3baa2011-05-18 12:04:31 +0000507 SkPaint::kNormal_Hinting :
508 SkPaint::kSlight_Hinting);
509 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000510 }
511
512private:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000513 SkOSMenu::TriState fLCDState;
514 SkOSMenu::TriState fAAState;
515 SkOSMenu::TriState fFilterState;
516 SkOSMenu::TriState fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000517};
518
reed@android.com8a1c16f2008-12-17 15:59:43 +0000519//////////////////////////////////////////////////////////////////////////////
520
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000521#define MAX_ZOOM_LEVEL 8
522#define MIN_ZOOM_LEVEL -8
523
reed@android.comf2b98d62010-12-20 18:26:13 +0000524static const char gCharEvtName[] = "SampleCode_Char_Event";
525static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000526static const char gTitleEvtName[] = "SampleCode_Title_Event";
527static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000528static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
reed@google.com2072db82011-11-08 15:18:10 +0000529static const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle";
reed@android.comf2b98d62010-12-20 18:26:13 +0000530
531bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
532 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
533 if (outUni) {
534 *outUni = evt.getFast32();
535 }
536 return true;
537 }
538 return false;
539}
540
541bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
542 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
543 if (outKey) {
544 *outKey = (SkKey)evt.getFast32();
545 }
546 return true;
547 }
548 return false;
549}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000550
551bool SampleCode::TitleQ(const SkEvent& evt) {
552 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
553}
554
555void SampleCode::TitleR(SkEvent* evt, const char title[]) {
556 SkASSERT(evt && TitleQ(*evt));
557 evt->setString(gTitleEvtName, title);
558}
559
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000560bool SampleCode::RequestTitle(SkView* view, SkString* title) {
561 SkEvent evt(gTitleEvtName);
562 if (view->doQuery(&evt)) {
563 title->set(evt.findString(gTitleEvtName));
564 return true;
565 }
566 return false;
567}
568
reed@android.com8a1c16f2008-12-17 15:59:43 +0000569bool SampleCode::PrefSizeQ(const SkEvent& evt) {
570 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
571}
572
573void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
574 SkASSERT(evt && PrefSizeQ(*evt));
575 SkScalar size[2];
576 size[0] = width;
577 size[1] = height;
578 evt->setScalars(gPrefSizeEvtName, 2, size);
579}
580
reed@android.comf2b98d62010-12-20 18:26:13 +0000581bool SampleCode::FastTextQ(const SkEvent& evt) {
582 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
583}
584
585///////////////////////////////////////////////////////////////////////////////
586
reed@android.com44177402009-11-23 21:07:51 +0000587static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000588static SkMSec gAnimTimePrev;
589
reed@android.com44177402009-11-23 21:07:51 +0000590SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000591SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
592SkScalar SampleCode::GetAnimSecondsDelta() {
593 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
594}
reed@android.com44177402009-11-23 21:07:51 +0000595
596SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000597 // since gAnimTime can be up to 32 bits, we can't convert it to a float
598 // or we'll lose the low bits. Hence we use doubles for the intermediate
599 // calculations
600 double seconds = (double)gAnimTime / 1000.0;
601 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000602 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000603 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000604 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000605 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000606}
607
bsalomon@google.com85003222012-03-28 14:44:37 +0000608SkScalar SampleCode::GetAnimSinScalar(SkScalar amplitude,
609 SkScalar periodInSec,
610 SkScalar phaseInSec) {
611 if (!periodInSec) {
612 return 0;
613 }
614 double t = (double)gAnimTime / 1000.0 + phaseInSec;
615 t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
616 amplitude = SK_ScalarHalf * amplitude;
617 return SkScalarMul(amplitude, SkDoubleToScalar(sin(t))) + amplitude;
618}
619
reed@google.com3cec4d72011-07-06 13:59:47 +0000620GrContext* SampleCode::GetGr() {
621 return gSampleWindow ? gSampleWindow->getGrContext() : NULL;
622}
623
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000624// some GMs rely on having a skiagm::GetGr function defined
625namespace skiagm {
caryclark@google.com02939ce2012-06-06 12:09:51 +0000626 // FIXME: this should be moved into a header
627 GrContext* GetGr();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000628 GrContext* GetGr() { return SampleCode::GetGr(); }
629}
630
reed@android.com8a1c16f2008-12-17 15:59:43 +0000631//////////////////////////////////////////////////////////////////////////////
632
reed@android.comf2b98d62010-12-20 18:26:13 +0000633static SkView* curr_view(SkWindow* wind) {
634 SkView::F2BIter iter(wind);
635 return iter.next();
636}
637
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000638static bool curr_title(SkWindow* wind, SkString* title) {
639 SkView* view = curr_view(wind);
640 if (view) {
641 SkEvent evt(gTitleEvtName);
642 if (view->doQuery(&evt)) {
643 title->set(evt.findString(gTitleEvtName));
644 return true;
645 }
646 }
647 return false;
648}
649
Scroggo2c8208f2011-06-15 16:49:08 +0000650void SampleWindow::setZoomCenter(float x, float y)
651{
652 fZoomCenterX = SkFloatToScalar(x);
653 fZoomCenterY = SkFloatToScalar(y);
654}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000655
Scroggo0f185c22011-03-24 18:35:50 +0000656bool SampleWindow::zoomIn()
657{
658 // Arbitrarily decided
659 if (fFatBitsScale == 25) return false;
660 fFatBitsScale++;
661 this->inval(NULL);
662 return true;
663}
664
665bool SampleWindow::zoomOut()
666{
667 if (fFatBitsScale == 1) return false;
668 fFatBitsScale--;
669 this->inval(NULL);
670 return true;
671}
672
Scroggo0f185c22011-03-24 18:35:50 +0000673void SampleWindow::updatePointer(int x, int y)
674{
675 fMouseX = x;
676 fMouseY = y;
677 if (fShowZoomer) {
678 this->inval(NULL);
679 }
680}
681
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000682static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
683 static const SampleWindow::DeviceType gCT[] = {
684 SampleWindow::kPicture_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000685#if SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000686 SampleWindow::kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000687#if SK_ANGLE
688 SampleWindow::kANGLE_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000689#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +0000690 SampleWindow::kRaster_DeviceType, // skip the null gpu device in normal cycling
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000691#endif // SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000692 SampleWindow::kRaster_DeviceType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000693 };
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000694 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gCT) == SampleWindow::kDeviceTypeCnt, array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000695 return gCT[ct];
696}
697
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000698static void usage(const char * argv0) {
chudy@google.com4605a3f2012-08-01 17:58:01 +0000699 SkDebugf("%s [--slide sampleName] [-i resourcePath] [--msaa sampleCount] [--pictureDir dirPath] [--picture path]\n", argv0);
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000700 SkDebugf(" sampleName: sample at which to start.\n");
701 SkDebugf(" resourcePath: directory that stores image resources.\n");
bsalomon@google.com11959252012-04-06 20:13:38 +0000702 SkDebugf(" msaa: request multisampling with the given sample count.\n");
chudy@google.com4605a3f2012-08-01 17:58:01 +0000703 SkDebugf(" dirPath: path to directory skia pictures are read from\n");
704 SkDebugf(" path: path to skia picture\n");
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000705}
706
chudy@google.com4605a3f2012-08-01 17:58:01 +0000707SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager)
robertphillips@google.com4750fa52012-04-10 13:34:11 +0000708 : INHERITED(hwnd)
709 , fDevManager(NULL) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000710
reed@google.com1830c7a2012-06-04 12:05:43 +0000711 this->registerPictFileSamples(argv, argc);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000712 this->registerPictFileSample(argv, argc);
robertphillips@google.com76d40212012-03-22 14:12:15 +0000713 SkGMRegistyToSampleRegistry();
714 {
715 const SkViewRegister* reg = SkViewRegister::Head();
716 while (reg) {
717 *fSamples.append() = reg->factory();
718 reg = reg->next();
719 }
720 }
721
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000722 const char* resourcePath = NULL;
723 fCurrIndex = -1;
bsalomon@google.com11959252012-04-06 20:13:38 +0000724 fMSAASampleCount = 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000725
726 const char* const commandName = argv[0];
727 char* const* stop = argv + argc;
728 for (++argv; argv < stop; ++argv) {
729 if (strcmp(*argv, "-i") == 0) {
730 argv++;
731 if (argv < stop && **argv) {
732 resourcePath = *argv;
733 }
robertphillips@google.com76d40212012-03-22 14:12:15 +0000734 } else if (strcmp(*argv, "--slide") == 0) {
735 argv++;
736 if (argv < stop && **argv) {
737 fCurrIndex = findByTitle(*argv);
738 if (fCurrIndex < 0) {
739 fprintf(stderr, "Unknown sample \"%s\"\n", *argv);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000740 listTitles();
robertphillips@google.com76d40212012-03-22 14:12:15 +0000741 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000742 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000743 } else if (strcmp(*argv, "--msaa") == 0) {
744 ++argv;
745 if (argv < stop && **argv) {
746 fMSAASampleCount = atoi(*argv);
747 }
robertphillips@google.com7265e722012-05-03 18:22:28 +0000748 } else if (strcmp(*argv, "--list") == 0) {
749 listTitles();
bsalomon@google.com11959252012-04-06 20:13:38 +0000750 }
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000751 else {
robertphillips@google.com76d40212012-03-22 14:12:15 +0000752 usage(commandName);
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000753 }
754 }
755
756 if (fCurrIndex < 0) {
757 SkString title;
758 if (readTitleFromPrefs(&title)) {
759 fCurrIndex = findByTitle(title.c_str());
760 }
761 }
762
763 if (fCurrIndex < 0) {
764 fCurrIndex = 0;
765 }
766
reed@google.com3cec4d72011-07-06 13:59:47 +0000767 gSampleWindow = this;
768
yangsu@google.com1f394212011-06-01 18:03:34 +0000769#ifdef PIPE_FILE
770 //Clear existing file or create file if it doesn't exist
771 FILE* f = fopen(FILE_PATH, "wb");
772 fclose(f);
773#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +0000774
reed@android.com8a1c16f2008-12-17 15:59:43 +0000775 fPicture = NULL;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000776
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000777 fDeviceType = kRaster_DeviceType;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000778
779#if DEFAULT_TO_GPU
780 fDeviceType = kGPU_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000781#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000782#if SK_ANGLE && DEFAULT_TO_ANGLE
783 fDeviceType = kANGLE_DeviceType;
784#endif
785
reed@android.com8a1c16f2008-12-17 15:59:43 +0000786 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000787 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000788 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000789 fRotate = false;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000790 fPerspAnim = false;
791 fPerspAnimTime = 0;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000792 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000793 fRequestGrabImage = false;
scroggo@google.comb073d922012-06-08 15:35:03 +0000794 fPipeState = SkOSMenu::kOffState;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000795 fMeasureFPS = false;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000796 fLCDState = SkOSMenu::kMixedState;
797 fAAState = SkOSMenu::kMixedState;
798 fFilterState = SkOSMenu::kMixedState;
799 fHintingState = SkOSMenu::kMixedState;
reed@google.com569e0432011-04-05 13:07:03 +0000800 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000801 fScrollTestX = fScrollTestY = 0;
802
Scroggo0f185c22011-03-24 18:35:50 +0000803 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000804 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000805 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
806 fShowZoomer = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000807
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000808 fZoomLevel = 0;
809 fZoomScale = SK_Scalar1;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000810
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000811 fMagnify = false;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000812 fDebugger = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000813
Scroggo8ac0d542011-06-21 14:44:57 +0000814 fSaveToPdf = false;
815 fPdfCanvas = NULL;
816
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000817 fTransitionNext = 6;
818 fTransitionPrev = 2;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000819
yangsu@google.com921091f2011-08-02 13:39:12 +0000820 int sinkID = this->getSinkID();
scroggo@google.com7dadc742012-04-18 14:07:57 +0000821 fAppMenu = new SkOSMenu;
822 fAppMenu->setTitle("Global Settings");
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000823 int itemID;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000824
825 itemID =fAppMenu->appendList("Device Type", "Device Type", sinkID, 0,
826 "Raster", "Picture", "OpenGL",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000827#if SK_ANGLE
828 "ANGLE",
829#endif
830 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000831 fAppMenu->assignKeyEquivalentToItem(itemID, 'd');
832 itemID = fAppMenu->appendTriState("AA", "AA", sinkID, fAAState);
833 fAppMenu->assignKeyEquivalentToItem(itemID, 'b');
834 itemID = fAppMenu->appendTriState("LCD", "LCD", sinkID, fLCDState);
835 fAppMenu->assignKeyEquivalentToItem(itemID, 'l');
836 itemID = fAppMenu->appendTriState("Filter", "Filter", sinkID, fFilterState);
837 fAppMenu->assignKeyEquivalentToItem(itemID, 'n');
838 itemID = fAppMenu->appendTriState("Hinting", "Hinting", sinkID, fHintingState);
839 fAppMenu->assignKeyEquivalentToItem(itemID, 'h');
scroggo@google.comb073d922012-06-08 15:35:03 +0000840 fUsePipeMenuItemID = fAppMenu->appendTriState("Pipe", "Pipe" , sinkID,
chudy@google.com4605a3f2012-08-01 17:58:01 +0000841 fPipeState);
scroggo@google.comb073d922012-06-08 15:35:03 +0000842 fAppMenu->assignKeyEquivalentToItem(fUsePipeMenuItemID, 'P');
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000843#ifdef DEBUGGER
scroggo@google.com7dadc742012-04-18 14:07:57 +0000844 itemID = fAppMenu->appendSwitch("Debugger", "Debugger", sinkID, fDebugger);
845 fAppMenu->assignKeyEquivalentToItem(itemID, 'q');
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000846#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +0000847 itemID = fAppMenu->appendSwitch("Slide Show", "Slide Show" , sinkID, false);
848 fAppMenu->assignKeyEquivalentToItem(itemID, 'a');
849 itemID = fAppMenu->appendSwitch("Clip", "Clip" , sinkID, fUseClip);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000850 fAppMenu->assignKeyEquivalentToItem(itemID, 'c');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000851 itemID = fAppMenu->appendSwitch("Flip X", "Flip X" , sinkID, false);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000852 fAppMenu->assignKeyEquivalentToItem(itemID, 'x');
853 itemID = fAppMenu->appendSwitch("Flip Y", "Flip Y" , sinkID, false);
854 fAppMenu->assignKeyEquivalentToItem(itemID, 'y');
855 itemID = fAppMenu->appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
856 fAppMenu->assignKeyEquivalentToItem(itemID, 'z');
857 itemID = fAppMenu->appendSwitch("Magnify", "Magnify" , sinkID, fMagnify);
858 fAppMenu->assignKeyEquivalentToItem(itemID, 'm');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000859 itemID =fAppMenu->appendList("Transition-Next", "Transition-Next", sinkID,
860 fTransitionNext, "Up", "Up and Right", "Right",
861 "Down and Right", "Down", "Down and Left",
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000862 "Left", "Up and Left", NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000863 fAppMenu->assignKeyEquivalentToItem(itemID, 'j');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000864 itemID =fAppMenu->appendList("Transition-Prev", "Transition-Prev", sinkID,
865 fTransitionPrev, "Up", "Up and Right", "Right",
866 "Down and Right", "Down", "Down and Left",
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000867 "Left", "Up and Left", NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000868 fAppMenu->assignKeyEquivalentToItem(itemID, 'k');
869 itemID = fAppMenu->appendAction("Save to PDF", sinkID);
870 fAppMenu->assignKeyEquivalentToItem(itemID, 'e');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000871
scroggo@google.com7dadc742012-04-18 14:07:57 +0000872 this->addMenu(fAppMenu);
873 fSlideMenu = new SkOSMenu;
874 this->addMenu(fSlideMenu);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000875
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000876// this->setConfig(SkBitmap::kRGB_565_Config);
877 this->setConfig(SkBitmap::kARGB_8888_Config);
878 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000879 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000880
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000881 skiagm::GM::SetResourcePath(resourcePath);
882
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000883 this->loadView((*fSamples[fCurrIndex])());
chudy@google.com4605a3f2012-08-01 17:58:01 +0000884
bsalomon@google.com840e9f32011-07-06 21:59:09 +0000885 fPDFData = NULL;
reed@google.comf0b5f682011-03-11 20:08:25 +0000886
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000887 if (NULL == devManager) {
888 fDevManager = new DefaultDeviceManager();
889 } else {
890 devManager->ref();
891 fDevManager = devManager;
892 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000893 fDevManager->setUpBackend(this, fMSAASampleCount);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000894
Scroggob4490c72011-06-17 13:53:05 +0000895 // If another constructor set our dimensions, ensure that our
896 // onSizeChange gets called.
897 if (this->height() && this->width()) {
898 this->onSizeChange();
899 }
reed@google.com2072db82011-11-08 15:18:10 +0000900
901 // can't call this synchronously, since it may require a subclass to
902 // to implement, or the caller may need us to have returned from the
903 // constructor first. Hence we post an event to ourselves.
904// this->updateTitle();
905 postEventToSink(new SkEvent(gUpdateWindowTitleEvtName), this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000906}
907
908SampleWindow::~SampleWindow() {
909 delete fPicture;
Scroggo8ac0d542011-06-21 14:44:57 +0000910 delete fPdfCanvas;
Scroggo0f185c22011-03-24 18:35:50 +0000911 fTypeface->unref();
reed@google.com29038ed2011-07-06 17:56:47 +0000912
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000913 SkSafeUnref(fDevManager);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000914}
915
reed@google.com1830c7a2012-06-04 12:05:43 +0000916static void make_filepath(SkString* path, const char* dir, const SkString& name) {
917 size_t len = strlen(dir);
918 path->set(dir);
919 if (len > 0 && dir[len - 1] != '/') {
920 path->append("/");
921 }
922 path->append(name);
923}
924
chudy@google.com4605a3f2012-08-01 17:58:01 +0000925void SampleWindow::registerPictFileSample(char** argv, int argc) {
926 const char* pict = NULL;
927
928 for (int i = 0; i < argc; ++i) {
929 if (!strcmp(argv[i], "--picture")) {
930 i += 1;
931 if (i < argc) {
932 pict = argv[i];
933 break;
934 }
935 }
936 }
937 if (pict) {
938 SkString path(pict);
939 *fSamples.append() = new PictFileFactory(path);
940 }
941}
942
reed@google.com1830c7a2012-06-04 12:05:43 +0000943void SampleWindow::registerPictFileSamples(char** argv, int argc) {
944 const char* pictDir = NULL;
945
946 for (int i = 0; i < argc; ++i) {
947 if (!strcmp(argv[i], "--pictureDir")) {
948 i += 1;
949 if (i < argc) {
950 pictDir = argv[i];
951 break;
952 }
953 }
954 }
955 if (pictDir) {
956 SkOSFile::Iter iter(pictDir, "skp");
957 SkString filename;
958 while (iter.next(&filename)) {
959 SkString path;
960 make_filepath(&path, pictDir, filename);
961 *fSamples.append() = new PictFileFactory(path);
962 }
963 }
964}
965
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000966int SampleWindow::findByTitle(const char title[]) {
967 int i, count = fSamples.count();
968 for (i = 0; i < count; i++) {
969 if (getSampleTitle(i).equals(title)) {
970 return i;
971 }
972 }
mike@reedtribe.orgdd52caa2011-12-28 20:02:10 +0000973 return -1;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000974}
975
robertphillips@google.com7265e722012-05-03 18:22:28 +0000976void SampleWindow::listTitles() {
977 int count = fSamples.count();
978 SkDebugf("All Slides:\n");
979 for (int i = 0; i < count; i++) {
980 SkDebugf(" %s\n", getSampleTitle(i).c_str());
981 }
982}
983
reed@android.com55e76b22009-11-23 21:46:47 +0000984static SkBitmap capture_bitmap(SkCanvas* canvas) {
985 SkBitmap bm;
986 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
987 src.copyTo(&bm, src.config());
988 return bm;
989}
990
991static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
992 SkBitmap* diff) {
993 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000994
reed@android.com55e76b22009-11-23 21:46:47 +0000995 SkAutoLockPixels alp0(src);
996 SkAutoLockPixels alp1(orig);
997 for (int y = 0; y < src.height(); y++) {
998 const void* srcP = src.getAddr(0, y);
999 const void* origP = orig.getAddr(0, y);
1000 size_t bytes = src.width() * src.bytesPerPixel();
1001 if (memcmp(srcP, origP, bytes)) {
1002 SkDebugf("---------- difference on line %d\n", y);
1003 return true;
1004 }
1005 }
1006 return false;
1007}
1008
Scroggo0f185c22011-03-24 18:35:50 +00001009static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
1010{
1011 SkColor desiredColor = paint.getColor();
1012 paint.setColor(SK_ColorWHITE);
1013 const char* c_str = string.c_str();
1014 size_t size = string.size();
1015 SkRect bounds;
1016 paint.measureText(c_str, size, &bounds);
1017 bounds.offset(left, top);
1018 SkScalar inset = SkIntToScalar(-2);
1019 bounds.inset(inset, inset);
1020 canvas->drawRect(bounds, paint);
1021 if (desiredColor != SK_ColorBLACK) {
1022 paint.setColor(SK_ColorBLACK);
1023 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
1024 }
1025 paint.setColor(desiredColor);
1026 canvas->drawText(c_str, size, left, top, paint);
1027}
1028
reed@android.com44177402009-11-23 21:07:51 +00001029#define XCLIP_N 8
1030#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +00001031
1032void SampleWindow::draw(SkCanvas* canvas) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001033 if (!fDevManager->prepareCanvas(fDeviceType, canvas, this)) {
1034 return;
1035 }
reed@android.com44177402009-11-23 21:07:51 +00001036 // update the animation time
bsalomon@google.come8f09102011-09-08 18:48:12 +00001037 if (!gAnimTimePrev && !gAnimTime) {
1038 // first time make delta be 0
1039 gAnimTime = SkTime::GetMSecs();
1040 gAnimTimePrev = gAnimTime;
1041 } else {
1042 gAnimTimePrev = gAnimTime;
1043 gAnimTime = SkTime::GetMSecs();
1044 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001045
reed@google.comf03bb562011-11-11 21:42:12 +00001046 const SkMatrix& localM = fGesture.localM();
1047 if (localM.getType() & SkMatrix::kScale_Mask) {
1048 canvas->setExternalMatrix(&localM);
1049 }
1050 if (fGesture.isActive()) {
1051 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001052 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001053
reed@android.come522ca52009-11-23 20:10:41 +00001054 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +00001055 this->INHERITED::draw(canvas);
1056 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +00001057
1058 const SkScalar w = this->width();
1059 const SkScalar h = this->height();
1060 const SkScalar cw = w / XCLIP_N;
1061 const SkScalar ch = h / YCLIP_N;
1062 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +00001063 SkRect r;
1064 r.fTop = y * ch;
1065 r.fBottom = (y + 1) * ch;
1066 if (y == YCLIP_N - 1) {
1067 r.fBottom = h;
1068 }
reed@android.come522ca52009-11-23 20:10:41 +00001069 for (int x = 0; x < XCLIP_N; x++) {
1070 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +00001071 r.fLeft = x * cw;
1072 r.fRight = (x + 1) * cw;
1073 if (x == XCLIP_N - 1) {
1074 r.fRight = w;
1075 }
reed@android.come522ca52009-11-23 20:10:41 +00001076 canvas->clipRect(r);
1077 this->INHERITED::draw(canvas);
1078 }
1079 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001080
reed@android.com55e76b22009-11-23 21:46:47 +00001081 SkBitmap diff;
1082 if (bitmap_diff(canvas, orig, &diff)) {
1083 }
reed@android.come522ca52009-11-23 20:10:41 +00001084 } else {
1085 this->INHERITED::draw(canvas);
1086 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001087 if (fShowZoomer && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +00001088 showZoomer(canvas);
1089 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001090 if (fMagnify && !fSaveToPdf) {
1091 magnify(canvas);
1092 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001093
reed@google.com29038ed2011-07-06 17:56:47 +00001094 // do this last
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001095 fDevManager->publishCanvas(fDeviceType, canvas, this);
Scroggo3272ba82011-05-25 20:50:42 +00001096}
1097
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001098static float clipW = 200;
1099static float clipH = 200;
1100void SampleWindow::magnify(SkCanvas* canvas) {
1101 SkRect r;
1102 int count = canvas->save();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001103
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001104 SkMatrix m = canvas->getTotalMatrix();
robertphillips@google.com07ef9112012-06-04 13:22:14 +00001105 if (!m.invert(&m)) {
1106 return;
1107 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001108 SkPoint offset, center;
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001109 SkScalar mouseX = fMouseX * SK_Scalar1;
1110 SkScalar mouseY = fMouseY * SK_Scalar1;
1111 m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset);
1112 m.mapXY(mouseX, mouseY, &center);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001113
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001114 r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX());
1115 r.offset(offset.fX, offset.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001116
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001117 SkPaint paint;
1118 paint.setColor(0xFF66AAEE);
1119 paint.setStyle(SkPaint::kStroke_Style);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001120 paint.setStrokeWidth(10.f * m.getScaleX());
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001121 //lense offset
1122 //canvas->translate(0, -250);
1123 canvas->drawRect(r, paint);
1124 canvas->clipRect(r);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001125
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001126 m = canvas->getTotalMatrix();
1127 m.setTranslate(-center.fX, -center.fY);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001128 m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001129 m.postTranslate(center.fX, center.fY);
1130 canvas->concat(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001131
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001132 this->INHERITED::draw(canvas);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001133
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001134 canvas->restoreToCount(count);
1135}
1136
Scroggo3272ba82011-05-25 20:50:42 +00001137void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +00001138 int count = canvas->save();
1139 canvas->resetMatrix();
1140 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +00001141 int width = SkScalarRound(this->width());
1142 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +00001143 if (fMouseX >= width) fMouseX = width - 1;
1144 else if (fMouseX < 0) fMouseX = 0;
1145 if (fMouseY >= height) fMouseY = height - 1;
1146 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +00001147
Scroggo0f185c22011-03-24 18:35:50 +00001148 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +00001149 bitmap.lockPixels();
1150
Scroggo0f185c22011-03-24 18:35:50 +00001151 // 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 +00001152 int zoomedWidth = (width >> 1) | 1;
1153 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +00001154 SkIRect src;
1155 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
1156 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
1157 SkRect dest;
1158 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
1159 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
1160 SkPaint paint;
1161 // Clear the background behind our zoomed in view
1162 paint.setColor(SK_ColorWHITE);
1163 canvas->drawRect(dest, paint);
1164 canvas->drawBitmapRect(bitmap, &src, dest);
1165 paint.setColor(SK_ColorBLACK);
1166 paint.setStyle(SkPaint::kStroke_Style);
1167 // Draw a border around the pixel in the middle
1168 SkRect originalPixel;
1169 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
1170 SkMatrix matrix;
1171 SkRect scalarSrc;
1172 scalarSrc.set(src);
1173 SkColor color = bitmap.getColor(fMouseX, fMouseY);
1174 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
1175 SkRect pixel;
1176 matrix.mapRect(&pixel, originalPixel);
1177 // TODO Perhaps measure the values and make the outline white if it's "dark"
1178 if (color == SK_ColorBLACK) {
1179 paint.setColor(SK_ColorWHITE);
1180 }
1181 canvas->drawRect(pixel, paint);
1182 }
1183 paint.setColor(SK_ColorBLACK);
1184 // Draw a border around the destination rectangle
1185 canvas->drawRect(dest, paint);
1186 paint.setStyle(SkPaint::kStrokeAndFill_Style);
1187 // Identify the pixel and its color on screen
1188 paint.setTypeface(fTypeface);
1189 paint.setAntiAlias(true);
1190 SkScalar lineHeight = paint.getFontMetrics(NULL);
1191 SkString string;
1192 string.appendf("(%i, %i)", fMouseX, fMouseY);
1193 SkScalar left = dest.fLeft + SkIntToScalar(3);
1194 SkScalar i = SK_Scalar1;
1195 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1196 // Alpha
1197 i += SK_Scalar1;
1198 string.reset();
1199 string.appendf("A: %X", SkColorGetA(color));
1200 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1201 // Red
1202 i += SK_Scalar1;
1203 string.reset();
1204 string.appendf("R: %X", SkColorGetR(color));
1205 paint.setColor(SK_ColorRED);
1206 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1207 // Green
1208 i += SK_Scalar1;
1209 string.reset();
1210 string.appendf("G: %X", SkColorGetG(color));
1211 paint.setColor(SK_ColorGREEN);
1212 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1213 // Blue
1214 i += SK_Scalar1;
1215 string.reset();
1216 string.appendf("B: %X", SkColorGetB(color));
1217 paint.setColor(SK_ColorBLUE);
1218 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1219 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +00001220}
1221
reed@android.com8a1c16f2008-12-17 15:59:43 +00001222void SampleWindow::onDraw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001223}
1224
1225#include "SkColorPriv.h"
1226
caryclark@google.com02939ce2012-06-06 12:09:51 +00001227#if 0 // UNUSED
reed@android.com8a1c16f2008-12-17 15:59:43 +00001228static void reverseRedAndBlue(const SkBitmap& bm) {
1229 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
1230 uint8_t* p = (uint8_t*)bm.getPixels();
1231 uint8_t* stop = p + bm.getSize();
1232 while (p < stop) {
1233 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
1234 unsigned scale = SkAlpha255To256(p[3]);
1235 unsigned r = p[2];
1236 unsigned b = p[0];
1237 p[0] = SkAlphaMul(r, scale);
1238 p[1] = SkAlphaMul(p[1], scale);
1239 p[2] = SkAlphaMul(b, scale);
1240 p += 4;
1241 }
1242}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001243#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001244
Scroggo8ac0d542011-06-21 14:44:57 +00001245void SampleWindow::saveToPdf()
1246{
1247 fSaveToPdf = true;
1248 this->inval(NULL);
1249}
1250
reed@android.com8a1c16f2008-12-17 15:59:43 +00001251SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +00001252 if (fSaveToPdf) {
1253 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
1254 SkISize size = SkISize::Make(bmp.width(), bmp.height());
1255 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
1256 canvas->getTotalMatrix());
1257 fPdfCanvas = new SkCanvas(pdfDevice);
1258 pdfDevice->unref();
1259 canvas = fPdfCanvas;
1260 } else {
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001261 switch (fDeviceType) {
1262 case kRaster_DeviceType:
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001263 // fallthrough
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001264#if SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001265 case kGPU_DeviceType:
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001266 // fallthrough
1267#if SK_ANGLE
1268 case kANGLE_DeviceType:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001269#endif // SK_ANGLE
1270#endif // SK_SUPPORT_GPU
reed@android.comf2b98d62010-12-20 18:26:13 +00001271 canvas = this->INHERITED::beforeChildren(canvas);
Scroggo8ac0d542011-06-21 14:44:57 +00001272 break;
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001273 case kPicture_DeviceType:
Scroggo8ac0d542011-06-21 14:44:57 +00001274 fPicture = new SkPicture;
1275 canvas = fPicture->beginRecording(9999, 9999);
1276 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001277#if SK_SUPPORT_GPU
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +00001278 case kNullGPU_DeviceType:
1279 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001280#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001281 default:
1282 SkASSERT(false);
1283 break;
reed@google.comac10a2d2010-12-22 21:39:39 +00001284 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001285 }
1286
1287 if (fUseClip) {
1288 canvas->drawColor(0xFFFF88FF);
reed@google.com045e62d2011-10-24 12:19:46 +00001289 canvas->clipPath(fClipPath, SkRegion::kIntersect_Op, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001290 }
1291
1292 return canvas;
1293}
1294
1295static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
1296 const SkRegion& rgn) {
1297 SkCanvas canvas(bm);
1298 SkRegion inval(rgn);
1299
1300 inval.translate(r.fLeft, r.fTop);
1301 canvas.clipRegion(inval);
1302 canvas.drawColor(0xFFFF8080);
1303}
yangsu@google.com501775e2011-06-24 16:04:50 +00001304#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001305void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +00001306 if (fSaveToPdf) {
1307 fSaveToPdf = false;
1308 if (fShowZoomer) {
1309 showZoomer(fPdfCanvas);
1310 }
1311 SkString name;
1312 name.printf("%s.pdf", this->getTitle());
1313 SkPDFDocument doc;
1314 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
1315 doc.appendPage(device);
djsollen@google.com56c69772011-11-08 19:00:26 +00001316#ifdef SK_BUILD_FOR_ANDROID
Scroggo8ac0d542011-06-21 14:44:57 +00001317 name.prepend("/sdcard/");
1318#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +00001319
yangsu@google.com501775e2011-06-24 16:04:50 +00001320#ifdef SK_BUILD_FOR_IOS
1321 SkDynamicMemoryWStream mstream;
1322 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +00001323 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +00001324#endif
Scroggo8ac0d542011-06-21 14:44:57 +00001325 SkFILEWStream stream(name.c_str());
1326 if (stream.isValid()) {
1327 doc.emitPDF(&stream);
1328 const char* desc = "File saved from Skia SampleApp";
1329 this->onPDFSaved(this->getTitle(), desc, name.c_str());
1330 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001331
Scroggo8ac0d542011-06-21 14:44:57 +00001332 delete fPdfCanvas;
1333 fPdfCanvas = NULL;
1334
1335 // We took over the draw calls in order to create the PDF, so we need
1336 // to redraw.
1337 this->inval(NULL);
1338 return;
1339 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001340
reed@android.comf2b98d62010-12-20 18:26:13 +00001341 if (fRequestGrabImage) {
1342 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001343
reed@google.com29038ed2011-07-06 17:56:47 +00001344 SkDevice* device = orig->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001345 SkBitmap bmp;
1346 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001347 static int gSampleGrabCounter;
1348 SkString name;
1349 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001350 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +00001351 SkImageEncoder::kPNG_Type, 100);
1352 }
1353 }
1354
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001355 if (kPicture_DeviceType == fDeviceType) {
1356 if (true) {
1357 SkPicture* pict = new SkPicture(*fPicture);
1358 fPicture->unref();
reed@google.come23f1942011-12-08 19:36:00 +00001359 this->installDrawFilter(orig);
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001360 orig->drawPicture(*pict);
1361 pict->unref();
1362 } else if (true) {
1363 SkDynamicMemoryWStream ostream;
1364 fPicture->serialize(&ostream);
1365 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001366
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001367 SkAutoDataUnref data(ostream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +00001368 SkMemoryStream istream(data->data(), data->size());
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001369 SkPicture pict(&istream);
1370 orig->drawPicture(pict);
1371 } else {
1372 fPicture->draw(orig);
1373 fPicture->unref();
1374 }
1375 fPicture = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001376 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001377
reed@google.com17d7aec2011-04-25 14:31:44 +00001378 // Do this after presentGL and other finishing, rather than in afterChild
1379 if (fMeasureFPS && fMeasureFPS_Time) {
1380 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
1381 this->updateTitle();
Scroggo62b65b02011-06-21 16:01:26 +00001382 this->postInvalDelay();
reed@google.com17d7aec2011-04-25 14:31:44 +00001383 }
1384
1385 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +00001386 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001387 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
1388 int dx = fScrollTestX * 7;
1389 int dy = fScrollTestY * 7;
1390 SkIRect r;
1391 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001392
reed@android.com8a1c16f2008-12-17 15:59:43 +00001393 r.set(50, 50, 50+100, 50+100);
1394 bm.scrollRect(&r, dx, dy, &inval);
1395 paint_rgn(bm, r, inval);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001396 }
1397#ifdef DEBUGGER
1398 SkView* curr = curr_view(this);
1399 if (fDebugger && !is_debugger(curr) && !is_transition(curr) && !is_overview(curr)) {
1400 //Stop Pipe when fDebugger is active
scroggo@google.comb073d922012-06-08 15:35:03 +00001401 if (fPipeState != SkOSMenu::kOffState) {
1402 fPipeState = SkOSMenu::kOffState;
1403 (void)SampleView::SetUsePipe(curr, fPipeState);
1404 fAppMenu->getItemByID(fUsePipeMenuItemID)->setTriState(fPipeState);
1405 this->onUpdateMenu(fAppMenu);
1406 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001407
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001408 //Reset any transformations
1409 fGesture.stop();
1410 fGesture.reset();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001411
1412 this->loadView(create_debugger(gTempDataStore.begin(),
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001413 gTempDataStore.count()));
1414 }
1415#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001416}
1417
reed@android.com6c5f6f22009-08-14 16:08:38 +00001418void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
1419 if (fScale) {
1420 SkScalar scale = SK_Scalar1 * 7 / 10;
1421 SkScalar cx = this->width() / 2;
1422 SkScalar cy = this->height() / 2;
1423 canvas->translate(cx, cy);
1424 canvas->scale(scale, scale);
1425 canvas->translate(-cx, -cy);
1426 }
1427 if (fRotate) {
1428 SkScalar cx = this->width() / 2;
1429 SkScalar cy = this->height() / 2;
1430 canvas->translate(cx, cy);
1431 canvas->rotate(SkIntToScalar(30));
1432 canvas->translate(-cx, -cy);
1433 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00001434 if (fPerspAnim) {
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001435 fPerspAnimTime += SampleCode::GetAnimSecondsDelta();
1436
1437 static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
1438 static const SkScalar gAnimMag = SK_Scalar1 / 1000;
1439 SkScalar t = SkScalarMod(fPerspAnimTime, gAnimPeriod);
1440 if (SkScalarFloorToInt(SkScalarDiv(fPerspAnimTime, gAnimPeriod)) & 0x1) {
1441 t = gAnimPeriod - t;
1442 }
1443 t = 2 * t - gAnimPeriod;
1444 t = SkScalarMul(SkScalarDiv(t, gAnimPeriod), gAnimMag);
1445 SkMatrix m;
1446 m.reset();
1447 m.setPerspY(t);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001448 canvas->concat(m);
1449 }
reed@google.comf0b5f682011-03-11 20:08:25 +00001450
reed@google.come23f1942011-12-08 19:36:00 +00001451 this->installDrawFilter(canvas);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001452
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001453 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +00001454 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
1455 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
1456 fMeasureFPS_Time = SkTime::GetMSecs();
1457 }
1458 } else {
1459 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001460 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00001461 if (fPerspAnim) {
1462 this->inval(NULL);
1463 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001464}
1465
1466void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +00001467 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001468}
1469
reed@android.com8a1c16f2008-12-17 15:59:43 +00001470static SkBitmap::Config gConfigCycle[] = {
1471 SkBitmap::kNo_Config, // none -> none
1472 SkBitmap::kNo_Config, // a1 -> none
1473 SkBitmap::kNo_Config, // a8 -> none
1474 SkBitmap::kNo_Config, // index8 -> none
1475 SkBitmap::kARGB_4444_Config, // 565 -> 4444
1476 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
1477 SkBitmap::kRGB_565_Config // 8888 -> 565
1478};
1479
1480static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
1481 return gConfigCycle[c];
1482}
1483
djsollen@google.come32b5832011-06-13 16:58:40 +00001484void SampleWindow::changeZoomLevel(float delta) {
1485 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001486 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001487 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
1488 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001489 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001490 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
1491 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001492 } else {
1493 fZoomScale = SK_Scalar1;
1494 }
reed@google.comf03bb562011-11-11 21:42:12 +00001495 this->updateMatrix();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001496}
1497
reed@google.comf03bb562011-11-11 21:42:12 +00001498void SampleWindow::updateMatrix(){
1499 SkMatrix m;
1500 m.reset();
1501 if (fZoomLevel) {
1502 SkPoint center;
1503 //m = this->getLocalMatrix();//.invert(&m);
1504 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
1505 SkScalar cx = center.fX;
1506 SkScalar cy = center.fY;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001507
reed@google.comf03bb562011-11-11 21:42:12 +00001508 m.setTranslate(-cx, -cy);
1509 m.postScale(fZoomScale, fZoomScale);
1510 m.postTranslate(cx, cy);
1511 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001512
reed@google.comf03bb562011-11-11 21:42:12 +00001513 if (fFlipAxis) {
1514 m.preTranslate(fZoomCenterX, fZoomCenterY);
1515 if (fFlipAxis & kFlipAxis_X) {
1516 m.preScale(-SK_Scalar1, SK_Scalar1);
1517 }
1518 if (fFlipAxis & kFlipAxis_Y) {
1519 m.preScale(SK_Scalar1, -SK_Scalar1);
1520 }
1521 m.preTranslate(-fZoomCenterX, -fZoomCenterY);
1522 //canvas->concat(m);
1523 }
1524 // Apply any gesture matrix
1525 m.preConcat(fGesture.localM());
1526 m.preConcat(fGesture.globalM());
chudy@google.com4605a3f2012-08-01 17:58:01 +00001527
reed@google.comf03bb562011-11-11 21:42:12 +00001528 this->setLocalMatrix(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001529
reed@google.comf03bb562011-11-11 21:42:12 +00001530 this->updateTitle();
1531 this->inval(NULL);
1532}
Scroggo2c8208f2011-06-15 16:49:08 +00001533bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +00001534 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001535 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001536 fTransitionPrev));
Scroggo2c8208f2011-06-15 16:49:08 +00001537 return true;
1538}
1539
reed@android.com8a1c16f2008-12-17 15:59:43 +00001540bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +00001541 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001542 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001543 fTransitionNext));
reed@android.com34245c72009-11-03 04:00:48 +00001544 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001545}
1546
yangsu@google.com501775e2011-06-24 16:04:50 +00001547bool SampleWindow::goToSample(int i) {
1548 fCurrIndex = (i) % fSamples.count();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001549 this->loadView(create_transition(curr_view(this),(*fSamples[fCurrIndex])(), 6));
yangsu@google.com501775e2011-06-24 16:04:50 +00001550 return true;
1551}
1552
1553SkString SampleWindow::getSampleTitle(int i) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001554 SkView* view = (*fSamples[i])();
yangsu@google.com501775e2011-06-24 16:04:50 +00001555 SkString title;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001556 SampleCode::RequestTitle(view, &title);
yangsu@google.com501775e2011-06-24 16:04:50 +00001557 view->unref();
1558 return title;
1559}
1560
1561int SampleWindow::sampleCount() {
1562 return fSamples.count();
1563}
1564
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001565void SampleWindow::showOverview() {
chudy@google.com4605a3f2012-08-01 17:58:01 +00001566 this->loadView(create_transition(curr_view(this),
1567 create_overview(fSamples.count(), fSamples.begin()),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001568 4));
1569}
1570
reed@google.come23f1942011-12-08 19:36:00 +00001571void SampleWindow::installDrawFilter(SkCanvas* canvas) {
1572 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
1573 fFilterState, fHintingState))->unref();
1574}
1575
Scroggo2c8208f2011-06-15 16:49:08 +00001576void SampleWindow::postAnimatingEvent() {
1577 if (fAnimating) {
reed@google.com87fac4a2011-08-04 13:50:17 +00001578 (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
Scroggo2c8208f2011-06-15 16:49:08 +00001579 }
1580}
reed@google.come23f1942011-12-08 19:36:00 +00001581
reed@android.com8a1c16f2008-12-17 15:59:43 +00001582bool SampleWindow::onEvent(const SkEvent& evt) {
reed@google.com2072db82011-11-08 15:18:10 +00001583 if (evt.isType(gUpdateWindowTitleEvtName)) {
1584 this->updateTitle();
1585 return true;
1586 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001587 if (evt.isType(ANIMATING_EVENTTYPE)) {
1588 if (fAnimating) {
1589 this->nextSample();
1590 this->postAnimatingEvent();
1591 }
1592 return true;
1593 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001594 if (evt.isType("replace-transition-view")) {
1595 this->loadView((SkView*)SkEventSink::FindSink(evt.getFast32()));
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001596 return true;
1597 }
reed@android.com34245c72009-11-03 04:00:48 +00001598 if (evt.isType("set-curr-index")) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001599 this->goToSample(evt.getFast32());
reed@android.com34245c72009-11-03 04:00:48 +00001600 return true;
1601 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001602 if (isInvalEvent(evt)) {
1603 this->inval(NULL);
1604 return true;
1605 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001606 int selected = -1;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001607 if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001608 this->setDeviceType((DeviceType)selected);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001609 return true;
yangsu@google.com921091f2011-08-02 13:39:12 +00001610 }
scroggo@google.comb073d922012-06-08 15:35:03 +00001611 if (SkOSMenu::FindTriState(evt, "Pipe", &fPipeState)) {
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001612#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00001613 if (!fPipeState != SkOSMenu::kOnState)
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001614 gServer.disconnectAll();
1615#endif
scroggo@google.comb073d922012-06-08 15:35:03 +00001616 (void)SampleView::SetUsePipe(curr_view(this), fPipeState);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001617 this->updateTitle();
1618 this->inval(NULL);
yangsu@google.com921091f2011-08-02 13:39:12 +00001619 return true;
1620 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001621 if (SkOSMenu::FindSwitchState(evt, "Slide Show", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001622 this->toggleSlideshow();
1623 return true;
1624 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001625 if (SkOSMenu::FindTriState(evt, "AA", &fAAState) ||
1626 SkOSMenu::FindTriState(evt, "LCD", &fLCDState) ||
1627 SkOSMenu::FindTriState(evt, "Filter", &fFilterState) ||
1628 SkOSMenu::FindTriState(evt, "Hinting", &fHintingState) ||
1629 SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) ||
1630 SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
1631 SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify) ||
1632 SkOSMenu::FindListIndex(evt, "Transition-Next", &fTransitionNext) ||
1633 SkOSMenu::FindListIndex(evt, "Transition-Prev", &fTransitionPrev)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001634 this->inval(NULL);
1635 this->updateTitle();
1636 return true;
1637 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001638 if (SkOSMenu::FindSwitchState(evt, "Flip X", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001639 fFlipAxis ^= kFlipAxis_X;
reed@google.comf03bb562011-11-11 21:42:12 +00001640 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001641 return true;
1642 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001643 if (SkOSMenu::FindSwitchState(evt, "Flip Y", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001644 fFlipAxis ^= kFlipAxis_Y;
reed@google.comf03bb562011-11-11 21:42:12 +00001645 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001646 return true;
1647 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001648 if (SkOSMenu::FindAction(evt,"Save to PDF")) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001649 this->saveToPdf();
1650 return true;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001651 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001652#ifdef DEBUGGER
1653 if (SkOSMenu::FindSwitchState(evt, "Debugger", &fDebugger)) {
1654 if (fDebugger) {
scroggo@google.comb073d922012-06-08 15:35:03 +00001655 fPipeState = SkOSMenu::kOnState;
1656 (void)SampleView::SetUsePipe(curr_view(this), fPipeState);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001657 } else {
scroggo@google.comb073d922012-06-08 15:35:03 +00001658 this->loadView((*fSamples[fCurrIndex])());
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001659 }
1660 this->inval(NULL);
1661 return true;
1662 }
1663#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001664 return this->INHERITED::onEvent(evt);
1665}
1666
reed@android.comf2b98d62010-12-20 18:26:13 +00001667bool SampleWindow::onQuery(SkEvent* query) {
1668 if (query->isType("get-slide-count")) {
1669 query->setFast32(fSamples.count());
1670 return true;
1671 }
1672 if (query->isType("get-slide-title")) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001673 SkView* view = (*fSamples[query->getFast32()])();
reed@android.comf2b98d62010-12-20 18:26:13 +00001674 SkEvent evt(gTitleEvtName);
1675 if (view->doQuery(&evt)) {
1676 query->setString("title", evt.findString(gTitleEvtName));
1677 }
1678 SkSafeUnref(view);
1679 return true;
1680 }
1681 if (query->isType("use-fast-text")) {
1682 SkEvent evt(gFastTextEvtName);
1683 return curr_view(this)->doQuery(&evt);
1684 }
reed@google.com29038ed2011-07-06 17:56:47 +00001685 if (query->isType("ignore-window-bitmap")) {
1686 query->setFast32(this->getGrContext() != NULL);
1687 return true;
1688 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001689 return this->INHERITED::onQuery(query);
1690}
1691
caryclark@google.com02939ce2012-06-06 12:09:51 +00001692#if 0 // UNUSED
reed@android.com0ae6b242008-12-23 16:49:54 +00001693static void cleanup_for_filename(SkString* name) {
1694 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001695 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001696 switch (str[i]) {
1697 case ':': str[i] = '-'; break;
1698 case '/': str[i] = '-'; break;
1699 case ' ': str[i] = '_'; break;
1700 default: break;
1701 }
1702 }
1703}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001704#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001705
1706bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001707 {
1708 SkView* view = curr_view(this);
1709 if (view) {
1710 SkEvent evt(gCharEvtName);
1711 evt.setFast32(uni);
1712 if (view->doQuery(&evt)) {
1713 return true;
1714 }
1715 }
1716 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001717
reed@android.com8a1c16f2008-12-17 15:59:43 +00001718 int dx = 0xFF;
1719 int dy = 0xFF;
1720
1721 switch (uni) {
1722 case '5': dx = 0; dy = 0; break;
1723 case '8': dx = 0; dy = -1; break;
1724 case '6': dx = 1; dy = 0; break;
1725 case '2': dx = 0; dy = 1; break;
1726 case '4': dx = -1; dy = 0; break;
1727 case '7': dx = -1; dy = -1; break;
1728 case '9': dx = 1; dy = -1; break;
1729 case '3': dx = 1; dy = 1; break;
1730 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001731
reed@android.com8a1c16f2008-12-17 15:59:43 +00001732 default:
1733 break;
1734 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001735
reed@android.com8a1c16f2008-12-17 15:59:43 +00001736 if (0xFF != dx && 0xFF != dy) {
1737 if ((dx | dy) == 0) {
1738 fScrollTestX = fScrollTestY = 0;
1739 } else {
1740 fScrollTestX += dx;
1741 fScrollTestY += dy;
1742 }
1743 this->inval(NULL);
1744 return true;
1745 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001746
reed@android.com0ae6b242008-12-23 16:49:54 +00001747 switch (uni) {
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001748 case 'f':
chudy@google.com4605a3f2012-08-01 17:58:01 +00001749 // only
djsollen@google.com6ff82552011-11-07 15:43:57 +00001750 toggleFPS();
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001751 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001752 case 'g':
1753 fRequestGrabImage = true;
1754 this->inval(NULL);
1755 break;
1756 case 'i':
1757 this->zoomIn();
1758 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001759 case 'o':
1760 this->zoomOut();
1761 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001762 case 'r':
1763 fRotate = !fRotate;
1764 this->inval(NULL);
1765 this->updateTitle();
1766 return true;
bsalomon@google.come8f09102011-09-08 18:48:12 +00001767 case 'k':
1768 fPerspAnim = !fPerspAnim;
1769 this->inval(NULL);
1770 this->updateTitle();
1771 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001772#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00001773 case '\\':
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001774 this->setDeviceType(kNullGPU_DeviceType);
1775 this->inval(NULL);
1776 this->updateTitle();
bsalomon@google.com74913722011-10-27 20:44:19 +00001777 return true;
twiz@google.com05e70242012-01-27 19:12:00 +00001778 case 'p':
1779 {
1780 GrContext* grContext = this->getGrContext();
1781 if (grContext) {
1782 size_t cacheBytes = grContext->getGpuTextureCacheBytes();
1783 grContext->freeGpuResources();
1784 SkDebugf("Purged %d bytes from the GPU resource cache.\n",
1785 cacheBytes);
1786 }
1787 }
1788 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001789#endif
reed@android.com6c5f6f22009-08-14 16:08:38 +00001790 case 's':
1791 fScale = !fScale;
1792 this->inval(NULL);
1793 this->updateTitle();
1794 return true;
reed@android.com0ae6b242008-12-23 16:49:54 +00001795 default:
1796 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001797 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001798
scroggo@google.com7dadc742012-04-18 14:07:57 +00001799 if (fAppMenu->handleKeyEquivalent(uni)|| fSlideMenu->handleKeyEquivalent(uni)) {
1800 this->onUpdateMenu(fAppMenu);
1801 this->onUpdateMenu(fSlideMenu);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001802 return true;
1803 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001804 return this->INHERITED::onHandleChar(uni);
1805}
1806
yangsu@google.com921091f2011-08-02 13:39:12 +00001807void SampleWindow::setDeviceType(DeviceType type) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001808 if (type == fDeviceType)
1809 return;
1810
1811 fDevManager->tearDownBackend(this);
1812
1813 fDeviceType = type;
1814
bsalomon@google.com11959252012-04-06 20:13:38 +00001815 fDevManager->setUpBackend(this, fMSAASampleCount);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001816
yangsu@google.com921091f2011-08-02 13:39:12 +00001817 this->updateTitle();
1818 this->inval(NULL);
1819}
1820
Scroggo2c8208f2011-06-15 16:49:08 +00001821void SampleWindow::toggleSlideshow() {
1822 fAnimating = !fAnimating;
1823 this->postAnimatingEvent();
1824 this->updateTitle();
1825}
1826
1827void SampleWindow::toggleRendering() {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001828 this->setDeviceType(cycle_devicetype(fDeviceType));
Scroggo2c8208f2011-06-15 16:49:08 +00001829 this->updateTitle();
1830 this->inval(NULL);
1831}
1832
djsollen@google.com6ff82552011-11-07 15:43:57 +00001833void SampleWindow::toggleFPS() {
1834 fMeasureFPS = !fMeasureFPS;
1835 this->updateTitle();
1836 this->inval(NULL);
1837}
1838
reed@android.com8a1c16f2008-12-17 15:59:43 +00001839#include "SkDumpCanvas.h"
1840
1841bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001842 {
1843 SkView* view = curr_view(this);
1844 if (view) {
1845 SkEvent evt(gKeyEvtName);
1846 evt.setFast32(key);
1847 if (view->doQuery(&evt)) {
1848 return true;
1849 }
1850 }
1851 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001852 switch (key) {
1853 case kRight_SkKey:
1854 if (this->nextSample()) {
1855 return true;
1856 }
1857 break;
1858 case kLeft_SkKey:
Scroggo2c8208f2011-06-15 16:49:08 +00001859 toggleRendering();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001860 return true;
1861 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001862 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001863 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001864 } else {
1865 fNClip = !fNClip;
1866 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001867 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001868 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001869 return true;
1870 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001871 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001872 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001873 } else {
1874 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001875 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001876 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001877 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001878 case kOK_SkKey: {
1879 SkString title;
1880 if (curr_title(this, &title)) {
1881 writeTitleToPrefs(title.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001882 }
1883 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001884 }
reed@android.com34245c72009-11-03 04:00:48 +00001885 case kBack_SkKey:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001886 this->showOverview();
reed@android.com34245c72009-11-03 04:00:48 +00001887 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001888 default:
1889 break;
1890 }
1891 return this->INHERITED::onHandleKey(key);
1892}
1893
reed@google.com52f57e12011-03-16 12:10:02 +00001894///////////////////////////////////////////////////////////////////////////////
1895
1896static const char gGestureClickType[] = "GestureClickType";
1897
Scroggod3aed392011-06-22 13:26:56 +00001898bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
1899 void* owner) {
Scroggo0f185c22011-03-24 18:35:50 +00001900 if (Click::kMoved_State == state) {
1901 updatePointer(x, y);
1902 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001903 int w = SkScalarRound(this->width());
1904 int h = SkScalarRound(this->height());
1905
1906 // check for the resize-box
1907 if (w - x < 16 && h - y < 16) {
1908 return false; // let the OS handle the click
chudy@google.com4605a3f2012-08-01 17:58:01 +00001909 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001910 else if (fMagnify) {
1911 //it's only necessary to update the drawing if there's a click
1912 this->inval(NULL);
1913 return false; //prevent dragging while magnify is enabled
1914 }
1915 else {
Scroggod3aed392011-06-22 13:26:56 +00001916 return this->INHERITED::onDispatchClick(x, y, state, owner);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001917 }
1918}
1919
reed@google.com52f57e12011-03-16 12:10:02 +00001920class GestureClick : public SkView::Click {
1921public:
1922 GestureClick(SkView* target) : SkView::Click(target) {
1923 this->setType(gGestureClickType);
1924 }
1925
1926 static bool IsGesture(Click* click) {
1927 return click->isType(gGestureClickType);
1928 }
1929};
1930
1931SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1932 return new GestureClick(this);
1933}
1934
1935bool SampleWindow::onClick(Click* click) {
1936 if (GestureClick::IsGesture(click)) {
bsalomon@google.com4d4f2812011-12-12 18:34:01 +00001937 float x = static_cast<float>(click->fICurr.fX);
1938 float y = static_cast<float>(click->fICurr.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001939
reed@google.com52f57e12011-03-16 12:10:02 +00001940 switch (click->fState) {
1941 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00001942 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001943 break;
1944 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00001945 fGesture.touchMoved(click->fOwner, x, y);
reed@google.comf03bb562011-11-11 21:42:12 +00001946 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001947 break;
1948 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00001949 fGesture.touchEnd(click->fOwner);
reed@google.comf03bb562011-11-11 21:42:12 +00001950 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001951 break;
1952 }
1953 return true;
1954 }
1955 return false;
1956}
1957
1958///////////////////////////////////////////////////////////////////////////////
1959
reed@android.com8a1c16f2008-12-17 15:59:43 +00001960void SampleWindow::loadView(SkView* view) {
1961 SkView::F2BIter iter(this);
1962 SkView* prev = iter.next();
1963 if (prev) {
1964 prev->detachFromParent();
1965 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001966
reed@android.com8a1c16f2008-12-17 15:59:43 +00001967 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001968 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001969 this->attachChildToFront(view)->unref();
1970 view->setSize(this->width(), this->height());
1971
yangsu@google.com921091f2011-08-02 13:39:12 +00001972 //repopulate the slide menu when a view is loaded
scroggo@google.com7dadc742012-04-18 14:07:57 +00001973 fSlideMenu->reset();
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001974#ifdef DEBUGGER
1975 if (!is_debugger(view) && !is_overview(view) && !is_transition(view) && fDebugger) {
1976 //Force Pipe to be on if using debugger
scroggo@google.comb073d922012-06-08 15:35:03 +00001977 fPipeState = SkOSMenu::kOnState;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001978 }
1979#endif
scroggo@google.comb073d922012-06-08 15:35:03 +00001980 (void)SampleView::SetUsePipe(view, fPipeState);
yangsu@google.com921091f2011-08-02 13:39:12 +00001981 if (SampleView::IsSampleView(view))
scroggo@google.com7dadc742012-04-18 14:07:57 +00001982 ((SampleView*)view)->requestMenu(fSlideMenu);
1983 this->onUpdateMenu(fSlideMenu);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001984 this->updateTitle();
1985}
1986
1987static const char* gConfigNames[] = {
1988 "unknown config",
1989 "A1",
1990 "A8",
1991 "Index8",
1992 "565",
1993 "4444",
1994 "8888"
1995};
1996
1997static const char* configToString(SkBitmap::Config c) {
1998 return gConfigNames[c];
1999}
2000
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002001static const char* gDeviceTypePrefix[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002002 "raster: ",
2003 "picture: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002004#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00002005 "opengl: ",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00002006#if SK_ANGLE
2007 "angle: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002008#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +00002009 "null-gl: "
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002010#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +00002011};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002012SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gDeviceTypePrefix) == SampleWindow::kDeviceTypeCnt,
2013 array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002014
bsalomon@google.com11959252012-04-06 20:13:38 +00002015static const bool gDeviceTypeIsGPU[] = {
2016 false,
2017 false,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002018#if SK_SUPPORT_GPU
bsalomon@google.com11959252012-04-06 20:13:38 +00002019 true,
2020#if SK_ANGLE
2021 true,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002022#endif // SK_ANGLE
bsalomon@google.com11959252012-04-06 20:13:38 +00002023 true
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002024#endif // SK_SUPPORT_GPU
bsalomon@google.com11959252012-04-06 20:13:38 +00002025};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002026SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gDeviceTypeIsGPU) == SampleWindow::kDeviceTypeCnt,
2027 array_size_mismatch);
2028
bsalomon@google.com11959252012-04-06 20:13:38 +00002029
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002030static const char* trystate_str(SkOSMenu::TriState state,
reed@google.com569e0432011-04-05 13:07:03 +00002031 const char trueStr[], const char falseStr[]) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002032 if (SkOSMenu::kOnState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002033 return trueStr;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002034 } else if (SkOSMenu::kOffState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002035 return falseStr;
2036 }
2037 return NULL;
2038}
2039
reed@android.com8a1c16f2008-12-17 15:59:43 +00002040void SampleWindow::updateTitle() {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002041 SkView* view = curr_view(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002042
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002043 SkString title;
2044 if (!curr_title(this, &title)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002045 title.set("<unknown>");
2046 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002047
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002048 title.prepend(gDeviceTypePrefix[fDeviceType]);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002049
2050 title.prepend(" ");
2051 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002052
reed@android.com8a1c16f2008-12-17 15:59:43 +00002053 if (fAnimating) {
2054 title.prepend("<A> ");
2055 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00002056 if (fScale) {
2057 title.prepend("<S> ");
2058 }
2059 if (fRotate) {
2060 title.prepend("<R> ");
2061 }
reed@android.come522ca52009-11-23 20:10:41 +00002062 if (fNClip) {
2063 title.prepend("<C> ");
2064 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00002065 if (fPerspAnim) {
2066 title.prepend("<K> ");
2067 }
reed@google.com569e0432011-04-05 13:07:03 +00002068
2069 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
2070 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00002071 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00002072 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
2073 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002074
2075 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00002076 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002077 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002078
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002079 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00002080 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002081 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002082 if (SampleView::IsSampleView(view)) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002083 switch (fPipeState) {
2084 case SkOSMenu::kOnState:
2085 title.prepend("<Pipe> ");
2086 break;
2087 case SkOSMenu::kMixedState:
2088 title.prepend("<Tiled Pipe> ");
2089 break;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002090
scroggo@google.comb073d922012-06-08 15:35:03 +00002091 default:
2092 break;
2093 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002094 title.prepend("! ");
2095 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002096
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002097#if SK_SUPPORT_GPU
bsalomon@google.com11959252012-04-06 20:13:38 +00002098 if (gDeviceTypeIsGPU[fDeviceType] &&
robertphillips@google.com4750fa52012-04-10 13:34:11 +00002099 NULL != fDevManager &&
bsalomon@google.com11959252012-04-06 20:13:38 +00002100 fDevManager->getGrRenderTarget()->numSamples() > 0) {
2101 title.appendf(" [MSAA: %d]",
2102 fDevManager->getGrRenderTarget()->numSamples());
2103 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002104#endif
bsalomon@google.com11959252012-04-06 20:13:38 +00002105
reed@android.com8a1c16f2008-12-17 15:59:43 +00002106 this->setTitle(title.c_str());
2107}
2108
2109void SampleWindow::onSizeChange() {
2110 this->INHERITED::onSizeChange();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002111
reed@android.com8a1c16f2008-12-17 15:59:43 +00002112 SkView::F2BIter iter(this);
2113 SkView* view = iter.next();
2114 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002115
reed@android.com8a1c16f2008-12-17 15:59:43 +00002116 // rebuild our clippath
2117 {
2118 const SkScalar W = this->width();
2119 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002120
reed@android.com8a1c16f2008-12-17 15:59:43 +00002121 fClipPath.reset();
2122#if 0
2123 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
2124 SkRect r;
2125 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
2126 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
2127 fClipPath.addRect(r);
2128 }
2129#else
2130 SkRect r;
2131 r.set(0, 0, W, H);
2132 fClipPath.addRect(r, SkPath::kCCW_Direction);
2133 r.set(W/4, H/4, W*3/4, H*3/4);
2134 fClipPath.addRect(r, SkPath::kCW_Direction);
2135#endif
2136 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002137
Scroggo2c8208f2011-06-15 16:49:08 +00002138 fZoomCenterX = SkScalarHalf(this->width());
2139 fZoomCenterY = SkScalarHalf(this->height());
2140
djsollen@google.com56c69772011-11-08 19:00:26 +00002141#ifdef SK_BUILD_FOR_ANDROID
Scroggob4490c72011-06-17 13:53:05 +00002142 // FIXME: The first draw after a size change does not work on Android, so
2143 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00002144 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00002145#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00002146 this->updateTitle(); // to refresh our config
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002147 fDevManager->windowSizeChanged(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002148}
2149
2150///////////////////////////////////////////////////////////////////////////////
2151
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002152static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002153static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00002154static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002155
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002156bool SampleView::IsSampleView(SkView* view) {
2157 SkEvent evt(is_sample_view_tag);
2158 return view->doQuery(&evt);
2159}
2160
reed@google.comf2183392011-04-22 14:10:48 +00002161bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002162 SkEvent evt(repeat_count_tag);
2163 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00002164 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002165}
2166
scroggo@google.comb073d922012-06-08 15:35:03 +00002167bool SampleView::SetUsePipe(SkView* view, SkOSMenu::TriState state) {
2168 SkEvent evt;
2169 evt.setS32(set_use_pipe_tag, state);
reed@google.com0faac1e2011-05-11 05:58:58 +00002170 return view->doEvent(evt);
2171}
2172
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002173bool SampleView::onEvent(const SkEvent& evt) {
2174 if (evt.isType(repeat_count_tag)) {
2175 fRepeatCount = evt.getFast32();
2176 return true;
2177 }
scroggo@google.comb073d922012-06-08 15:35:03 +00002178 int32_t pipeHolder;
2179 if (evt.findS32(set_use_pipe_tag, &pipeHolder)) {
2180 fPipeState = static_cast<SkOSMenu::TriState>(pipeHolder);
reed@google.com0faac1e2011-05-11 05:58:58 +00002181 return true;
2182 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002183 return this->INHERITED::onEvent(evt);
2184}
2185
2186bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002187 if (evt->isType(is_sample_view_tag)) {
2188 return true;
2189 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002190 return this->INHERITED::onQuery(evt);
2191}
2192
reed@google.com64e3eb22011-05-04 14:32:04 +00002193
2194class SimplePC : public SkGPipeController {
2195public:
2196 SimplePC(SkCanvas* target);
2197 ~SimplePC();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002198
reed@google.com64e3eb22011-05-04 14:32:04 +00002199 virtual void* requestBlock(size_t minRequest, size_t* actual);
2200 virtual void notifyWritten(size_t bytes);
2201
2202private:
reed@google.com961ddb02011-05-05 14:03:48 +00002203 SkGPipeReader fReader;
2204 void* fBlock;
2205 size_t fBlockSize;
2206 size_t fBytesWritten;
2207 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00002208 SkGPipeReader::Status fStatus;
2209
2210 size_t fTotalWritten;
2211};
2212
2213SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
2214 fBlock = NULL;
2215 fBlockSize = fBytesWritten = 0;
2216 fStatus = SkGPipeReader::kDone_Status;
2217 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00002218 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00002219}
2220
2221SimplePC::~SimplePC() {
2222// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
reed@google.com0faac1e2011-05-11 05:58:58 +00002223 if (fTotalWritten) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002224 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
2225 fAtomsWritten, fStatus);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002226#ifdef PIPE_FILE
scroggo@google.comb073d922012-06-08 15:35:03 +00002227 //File is open in append mode
2228 FILE* f = fopen(FILE_PATH, "ab");
2229 SkASSERT(f != NULL);
2230 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
2231 fclose(f);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002232#endif
2233#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00002234 if (fAtomsWritten > 1 && fTotalWritten > 4) { //ignore done
2235 gServer.acceptConnections();
2236 gServer.writePacket(fBlock, fTotalWritten);
2237 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002238#endif
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00002239#ifdef DEBUGGER
scroggo@google.comb073d922012-06-08 15:35:03 +00002240 gTempDataStore.reset();
2241 gTempDataStore.append(fTotalWritten, (const char*)fBlock);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00002242#endif
reed@google.com0faac1e2011-05-11 05:58:58 +00002243 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002244 sk_free(fBlock);
reed@google.com64e3eb22011-05-04 14:32:04 +00002245}
2246
2247void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
2248 sk_free(fBlock);
2249
2250 fBlockSize = minRequest * 4;
2251 fBlock = sk_malloc_throw(fBlockSize);
2252 fBytesWritten = 0;
2253 *actual = fBlockSize;
2254 return fBlock;
2255}
2256
2257void SimplePC::notifyWritten(size_t bytes) {
2258 SkASSERT(fBytesWritten + bytes <= fBlockSize);
reed@google.com64e3eb22011-05-04 14:32:04 +00002259 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
2260 SkASSERT(SkGPipeReader::kError_Status != fStatus);
2261 fBytesWritten += bytes;
2262 fTotalWritten += bytes;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002263
reed@google.com961ddb02011-05-05 14:03:48 +00002264 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00002265}
2266
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002267void SampleView::draw(SkCanvas* canvas) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002268 if (SkOSMenu::kOffState == fPipeState) {
2269 this->INHERITED::draw(canvas);
2270 } else {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002271 SkGPipeWriter writer;
2272 SimplePC controller(canvas);
scroggo@google.comb073d922012-06-08 15:35:03 +00002273 TiledPipeController tc(canvas->getDevice()->accessBitmap(false),
2274 &canvas->getTotalMatrix());
2275 SkGPipeController* pc;
2276 if (SkOSMenu::kMixedState == fPipeState) {
2277 pc = &tc;
2278 } else {
2279 pc = &controller;
2280 }
reed@google.comdde09562011-05-23 12:21:05 +00002281 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002282
scroggo@google.comb073d922012-06-08 15:35:03 +00002283 canvas = writer.startRecording(pc, flags);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002284 //Must draw before controller goes out of scope and sends data
2285 this->INHERITED::draw(canvas);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00002286 //explicitly end recording to ensure writer is flushed before the memory
2287 //is freed in the deconstructor of the controller
2288 writer.endRecording();
reed@google.com0faac1e2011-05-11 05:58:58 +00002289 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002290}
2291void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com81e3d7f2011-06-01 12:42:36 +00002292 this->onDrawBackground(canvas);
2293
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002294 for (int i = 0; i < fRepeatCount; i++) {
2295 SkAutoCanvasRestore acr(canvas, true);
2296 this->onDrawContent(canvas);
2297 }
2298}
2299
2300void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00002301 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002302}
2303
2304///////////////////////////////////////////////////////////////////////////////
2305
reed@android.comf2b98d62010-12-20 18:26:13 +00002306template <typename T> void SkTBSort(T array[], int count) {
2307 for (int i = 1; i < count - 1; i++) {
2308 bool didSwap = false;
2309 for (int j = count - 1; j > i; --j) {
2310 if (array[j] < array[j-1]) {
2311 T tmp(array[j-1]);
2312 array[j-1] = array[j];
2313 array[j] = tmp;
2314 didSwap = true;
2315 }
2316 }
2317 if (!didSwap) {
2318 break;
2319 }
2320 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002321
reed@android.comf2b98d62010-12-20 18:26:13 +00002322 for (int k = 0; k < count - 1; k++) {
2323 SkASSERT(!(array[k+1] < array[k]));
2324 }
2325}
2326
2327#include "SkRandom.h"
2328
2329static void rand_rect(SkIRect* rect, SkRandom& rand) {
2330 int bits = 8;
2331 int shift = 32 - bits;
2332 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
2333 rand.nextU() >> shift, rand.nextU() >> shift);
2334 rect->sort();
2335}
2336
2337static void dumpRect(const SkIRect& r) {
2338 SkDebugf(" { %d, %d, %d, %d },\n",
2339 r.fLeft, r.fTop,
2340 r.fRight, r.fBottom);
2341}
2342
2343static void test_rects(const SkIRect rect[], int count) {
2344 SkRegion rgn0, rgn1;
2345
2346 for (int i = 0; i < count; i++) {
2347 rgn0.op(rect[i], SkRegion::kUnion_Op);
2348 // dumpRect(rect[i]);
2349 }
2350 rgn1.setRects(rect, count);
2351
2352 if (rgn0 != rgn1) {
2353 SkDebugf("\n");
2354 for (int i = 0; i < count; i++) {
2355 dumpRect(rect[i]);
2356 }
2357 SkDebugf("\n");
2358 }
2359}
2360
2361static void test() {
2362 size_t i;
2363
2364 const SkIRect r0[] = {
2365 { 0, 0, 1, 1 },
2366 { 2, 2, 3, 3 },
2367 };
2368 const SkIRect r1[] = {
2369 { 0, 0, 1, 3 },
2370 { 1, 1, 2, 2 },
2371 { 2, 0, 3, 3 },
2372 };
2373 const SkIRect r2[] = {
2374 { 0, 0, 1, 2 },
2375 { 2, 1, 3, 3 },
2376 { 4, 0, 5, 1 },
2377 { 6, 0, 7, 4 },
2378 };
2379
2380 static const struct {
2381 const SkIRect* fRects;
2382 int fCount;
2383 } gRecs[] = {
2384 { r0, SK_ARRAY_COUNT(r0) },
2385 { r1, SK_ARRAY_COUNT(r1) },
2386 { r2, SK_ARRAY_COUNT(r2) },
2387 };
2388
2389 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
2390 test_rects(gRecs[i].fRects, gRecs[i].fCount);
2391 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002392
reed@android.comf2b98d62010-12-20 18:26:13 +00002393 SkRandom rand;
2394 for (i = 0; i < 10000; i++) {
2395 SkRegion rgn0, rgn1;
2396
2397 const int N = 8;
2398 SkIRect rect[N];
2399 for (int j = 0; j < N; j++) {
2400 rand_rect(&rect[j], rand);
2401 }
2402 test_rects(rect, N);
2403 }
2404}
2405
caryclark@google.com02939ce2012-06-06 12:09:51 +00002406// FIXME: this should be in a header
2407SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00002408SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
caryclark@google.com02939ce2012-06-06 12:09:51 +00002409 if (false) { // avoid bit rot, suppress warning
2410 test();
2411 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002412 return new SampleWindow(hwnd, argc, argv, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002413}
2414
caryclark@google.com02939ce2012-06-06 12:09:51 +00002415// FIXME: this should be in a header
2416void get_preferred_size(int* x, int* y, int* width, int* height);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002417void get_preferred_size(int* x, int* y, int* width, int* height) {
2418 *x = 10;
2419 *y = 50;
2420 *width = 640;
2421 *height = 480;
2422}
2423
caryclark@google.com02939ce2012-06-06 12:09:51 +00002424// FIXME: this should be in a header
2425void application_init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002426void application_init() {
2427// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002428#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00002429 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002430#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002431 SkGraphics::Init();
2432 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002433}
2434
caryclark@google.com02939ce2012-06-06 12:09:51 +00002435// FIXME: this should be in a header
2436void application_term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002437void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002438 SkEvent::Term();
2439 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002440}