blob: 53c28e4d7452d0048645db20e33b0004d365497f [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"
scroggo@google.com74b7ffd2013-04-30 02:32:41 +000013#include "SkImageDecoder.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +000014#include "SkImageEncoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkPaint.h"
16#include "SkPicture.h"
17#include "SkStream.h"
edisonn@google.comf8b6b012013-07-11 14:28:04 +000018#include "SkTSort.h"
reed@android.com44177402009-11-23 21:07:51 +000019#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkWindow.h"
21
22#include "SampleCode.h"
Scroggo0f185c22011-03-24 18:35:50 +000023#include "SkTypeface.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000024
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000025#if SK_SUPPORT_GPU
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000026#include "gl/GrGLInterface.h"
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000027#include "gl/GrGLUtil.h"
bsalomon@google.com583a1e32011-08-17 13:42:46 +000028#include "GrRenderTarget.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000029#include "GrContext.h"
30#include "SkGpuDevice.h"
31#else
32class GrContext;
33#endif
Scroggo2c8208f2011-06-15 16:49:08 +000034
reed@google.com1830c7a2012-06-04 12:05:43 +000035#include "SkOSFile.h"
Scroggo8ac0d542011-06-21 14:44:57 +000036#include "SkPDFDevice.h"
37#include "SkPDFDocument.h"
38#include "SkStream.h"
39
scroggo@google.comb073d922012-06-08 15:35:03 +000040#include "SkGPipe.h"
41#include "SamplePipeControllers.h"
tfarina@chromium.orgb1b7f7072012-09-18 01:52:20 +000042#include "OverView.h"
epoger@google.com6a121782012-09-14 18:42:01 +000043#include "TransitionView.h"
scroggo@google.comb073d922012-06-08 15:35:03 +000044
robertphillips@google.coma22e2112012-08-16 14:58:06 +000045SK_DEFINE_INST_COUNT(SampleWindow::DeviceManager)
46
reed@google.com1830c7a2012-06-04 12:05:43 +000047extern SampleView* CreateSamplePictFileView(const char filename[]);
48
49class PictFileFactory : public SkViewFactory {
50 SkString fFilename;
51public:
52 PictFileFactory(const SkString& filename) : fFilename(filename) {}
53 virtual SkView* operator() () const SK_OVERRIDE {
54 return CreateSamplePictFileView(fFilename.c_str());
55 }
56};
57
edisonn@google.comf8b6b012013-07-11 14:28:04 +000058#ifdef SAMPLE_PDF_FILE_VIEWER
59extern SampleView* CreateSamplePdfFileViewer(const char filename[]);
60
61class PdfFileViewerFactory : public SkViewFactory {
62 SkString fFilename;
63public:
64 PdfFileViewerFactory(const SkString& filename) : fFilename(filename) {}
65 virtual SkView* operator() () const SK_OVERRIDE {
66 return CreateSamplePdfFileViewer(fFilename.c_str());
67 }
68};
69#endif // SAMPLE_PDF_FILE_VIEWER
70
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000071#define PIPE_FILEx
72#ifdef PIPE_FILE
yangsu@google.com1f394212011-06-01 18:03:34 +000073#define FILE_PATH "/path/to/drawing.data"
74#endif
75
yangsu@google.comf121b052011-08-08 16:02:51 +000076#define PIPE_NETx
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000077#ifdef PIPE_NET
78#include "SkSockets.h"
79SkTCPServer gServer;
80#endif
yangsu@google.comef7bdfa2011-08-12 14:27:47 +000081
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +000082#define USE_ARROWS_FOR_ZOOM true
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000083
84#if SK_ANGLE
85//#define DEFAULT_TO_ANGLE 1
86#else
87//#define DEFAULT_TO_GPU 1
88#endif
reed@android.comf2b98d62010-12-20 18:26:13 +000089
reed@android.com8a1c16f2008-12-17 15:59:43 +000090#define ANIMATING_EVENTTYPE "nextSample"
reed@google.com84cfce12013-05-29 19:22:20 +000091#define ANIMATING_DELAY 250
reed@android.com8a1c16f2008-12-17 15:59:43 +000092
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000093#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +000094 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000095#else
reed@google.combad8c872011-05-18 20:10:31 +000096 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000097#endif
reed@google.combad8c872011-05-18 20:10:31 +000098#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000099
reed@google.com3cec4d72011-07-06 13:59:47 +0000100static SampleWindow* gSampleWindow;
101
reed@google.coma4f81372012-11-15 15:56:38 +0000102static bool gShowGMBounds;
103
reed@google.com2072db82011-11-08 15:18:10 +0000104static void postEventToSink(SkEvent* evt, SkEventSink* sink) {
105 evt->setTargetID(sink->getSinkID())->post();
106}
107
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000108///////////////////////////////////////////////////////////////////////////////
109
110static const char* skip_until(const char* str, const char* skip) {
111 if (!str) {
112 return NULL;
113 }
114 return strstr(str, skip);
115}
116
117static const char* skip_past(const char* str, const char* skip) {
118 const char* found = skip_until(str, skip);
119 if (!found) {
120 return NULL;
121 }
122 return found + strlen(skip);
123}
124
125static const char* gPrefFileName = "sampleapp_prefs.txt";
126
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000127static bool readTitleFromPrefs(SkString* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000128 SkFILEStream stream(gPrefFileName);
129 if (!stream.isValid()) {
130 return false;
131 }
132
133 int len = stream.getLength();
134 SkString data(len);
135 stream.read(data.writable_str(), len);
136 const char* s = data.c_str();
137
138 s = skip_past(s, "curr-slide-title");
139 s = skip_past(s, "=");
140 s = skip_past(s, "\"");
141 const char* stop = skip_until(s, "\"");
142 if (stop > s) {
143 title->set(s, stop - s);
144 return true;
145 }
146 return false;
147}
148
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000149static void writeTitleToPrefs(const char* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000150 SkFILEWStream stream(gPrefFileName);
151 SkString data;
152 data.printf("curr-slide-title = \"%s\"\n", title);
153 stream.write(data.c_str(), data.size());
154}
155
156///////////////////////////////////////////////////////////////////////////////
157
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000158class SampleWindow::DefaultDeviceManager : public SampleWindow::DeviceManager {
159public:
160
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000161 DefaultDeviceManager() {
162#if SK_SUPPORT_GPU
163 fCurContext = NULL;
164 fCurIntf = NULL;
165 fCurRenderTarget = NULL;
166 fMSAASampleCount = 0;
167#endif
168 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000169 }
170
171 virtual ~DefaultDeviceManager() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000172#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000173 SkSafeUnref(fCurContext);
174 SkSafeUnref(fCurIntf);
175 SkSafeUnref(fCurRenderTarget);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000176#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000177 }
178
bsalomon@google.com11959252012-04-06 20:13:38 +0000179 virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000180 SkASSERT(kNone_BackEndType == fBackend);
robertphillips@google.com53e96a12012-03-30 14:47:53 +0000181
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000182 fBackend = kNone_BackEndType;
183
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000184#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000185 switch (win->getDeviceType()) {
186 case kRaster_DeviceType:
187 // fallthrough
188 case kPicture_DeviceType:
189 // fallthrough
190 case kGPU_DeviceType:
191 // fallthrough
192 case kNullGPU_DeviceType:
193 // all these guys use the native backend
194 fBackend = kNativeGL_BackEndType;
195 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000196#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000197 case kANGLE_DeviceType:
198 // ANGLE is really the only odd man out
199 fBackend = kANGLE_BackEndType;
200 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000201#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000202 default:
203 SkASSERT(false);
204 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000205 }
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000206 AttachmentInfo attachmentInfo;
207 bool result = win->attach(fBackend, msaaSampleCount, &attachmentInfo);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000208 if (!result) {
bsalomon@google.com74913722011-10-27 20:44:19 +0000209 SkDebugf("Failed to initialize GL");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000210 return;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000211 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000212 fMSAASampleCount = msaaSampleCount;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000213
214 SkASSERT(NULL == fCurIntf);
215 switch (win->getDeviceType()) {
216 case kRaster_DeviceType:
217 // fallthrough
218 case kPicture_DeviceType:
219 // fallthrough
220 case kGPU_DeviceType:
221 // all these guys use the native interface
222 fCurIntf = GrGLCreateNativeInterface();
223 break;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000224#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000225 case kANGLE_DeviceType:
226 fCurIntf = GrGLCreateANGLEInterface();
227 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000228#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000229 case kNullGPU_DeviceType:
230 fCurIntf = GrGLCreateNullInterface();
231 break;
232 default:
233 SkASSERT(false);
234 break;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000235 }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000236
237 SkASSERT(NULL == fCurContext);
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000238 fCurContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000239
240 if (NULL == fCurContext || NULL == fCurIntf) {
241 // We need some context and interface to see results
242 SkSafeUnref(fCurContext);
243 SkSafeUnref(fCurIntf);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000244 SkDebugf("Failed to setup 3D");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000245
246 win->detach();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000247 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000248#endif // SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000249 // call windowSizeChanged to create the render target
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000250 this->windowSizeChanged(win);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000251 }
252
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000253 virtual void tearDownBackend(SampleWindow *win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000254#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000255 SkSafeUnref(fCurContext);
256 fCurContext = NULL;
257
258 SkSafeUnref(fCurIntf);
259 fCurIntf = NULL;
260
261 SkSafeUnref(fCurRenderTarget);
262 fCurRenderTarget = NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000263#endif
bsalomon@google.comddd40e52012-04-10 15:56:29 +0000264 win->detach();
265 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000266 }
267
reed@google.com5957f472012-10-01 20:31:56 +0000268 virtual SkCanvas* createCanvas(SampleWindow::DeviceType dType,
269 SampleWindow* win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000270#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +0000271 if (IsGpuDeviceType(dType) && NULL != fCurContext) {
272 SkAutoTUnref<SkDevice> device(new SkGpuDevice(fCurContext, fCurRenderTarget));
273 return new SkCanvas(device);
274 } else
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000275#endif
bsalomon@google.com82502e22013-01-24 20:47:18 +0000276 {
277 return NULL;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000278 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000279 }
280
281 virtual void publishCanvas(SampleWindow::DeviceType dType,
282 SkCanvas* canvas,
283 SampleWindow* win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000284#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000285 if (fCurContext) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000286 // in case we have queued drawing calls
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000287 fCurContext->flush();
288
bsalomon@google.com82502e22013-01-24 20:47:18 +0000289 if (!IsGpuDeviceType(dType)) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000290 // need to send the raster bits to the (gpu) window
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000291 fCurContext->setRenderTarget(fCurRenderTarget);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000292 const SkBitmap& bm = win->getBitmap();
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000293 fCurRenderTarget->writePixels(0, 0, bm.width(), bm.height(),
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000294 kSkia8888_GrPixelConfig,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000295 bm.getPixels(),
296 bm.rowBytes());
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000297 }
298 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000299#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000300
301 win->present();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000302 }
303
304 virtual void windowSizeChanged(SampleWindow* win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000305#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000306 if (fCurContext) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000307 AttachmentInfo attachmentInfo;
308 win->attach(fBackend, fMSAASampleCount, &attachmentInfo);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000309
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000310 GrBackendRenderTargetDesc desc;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000311 desc.fWidth = SkScalarRound(win->width());
312 desc.fHeight = SkScalarRound(win->height());
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000313 desc.fConfig = kSkia8888_GrPixelConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000314 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000315 desc.fSampleCnt = attachmentInfo.fSampleCount;
316 desc.fStencilBits = attachmentInfo.fStencilBits;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000317 GrGLint buffer;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000318 GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
bsalomon@google.come269f212011-11-07 13:29:52 +0000319 desc.fRenderTargetHandle = buffer;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000320
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000321 SkSafeUnref(fCurRenderTarget);
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000322 fCurRenderTarget = fCurContext->wrapBackendRenderTarget(desc);
bsalomon@google.com74913722011-10-27 20:44:19 +0000323 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000324#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000325 }
326
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000327 virtual GrContext* getGrContext() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000328#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000329 return fCurContext;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000330#else
331 return NULL;
332#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000333 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000334
335 virtual GrRenderTarget* getGrRenderTarget() SK_OVERRIDE {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000336#if SK_SUPPORT_GPU
bsalomon@google.com11959252012-04-06 20:13:38 +0000337 return fCurRenderTarget;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000338#else
339 return NULL;
340#endif
bsalomon@google.com11959252012-04-06 20:13:38 +0000341 }
342
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000343private:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000344
345#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000346 GrContext* fCurContext;
347 const GrGLInterface* fCurIntf;
348 GrRenderTarget* fCurRenderTarget;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000349 int fMSAASampleCount;
350#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000351
352 SkOSWindow::SkBackEndTypes fBackend;
353
354 typedef SampleWindow::DeviceManager INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000355};
356
357///////////////
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000358static const char view_inval_msg[] = "view-inval-msg";
359
Scroggo62b65b02011-06-21 16:01:26 +0000360void SampleWindow::postInvalDelay() {
reed@google.com87fac4a2011-08-04 13:50:17 +0000361 (new SkEvent(view_inval_msg, this->getSinkID()))->postDelay(1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000362}
363
364static bool isInvalEvent(const SkEvent& evt) {
365 return evt.isType(view_inval_msg);
366}
367//////////////////
368
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000369SkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func)
370 : fCreateFunc(func) {
371}
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000372
bsalomon@google.com8301de12011-10-31 16:47:13 +0000373SkView* SkFuncViewFactory::operator() () const {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000374 return (*fCreateFunc)();
375}
376
377#include "GMSampleView.h"
378
379SkGMSampleViewFactory::SkGMSampleViewFactory(GMFactoryFunc func)
380 : fFunc(func) {
381}
382
383SkView* SkGMSampleViewFactory::operator() () const {
384 return new GMSampleView(fFunc(NULL));
385}
386
387SkViewRegister* SkViewRegister::gHead;
388SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) {
389 fFact->ref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390 fChain = gHead;
391 gHead = this;
392}
393
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000394SkViewRegister::SkViewRegister(SkViewCreateFunc func) {
395 fFact = new SkFuncViewFactory(func);
396 fChain = gHead;
397 gHead = this;
398}
399
400SkViewRegister::SkViewRegister(GMFactoryFunc func) {
401 fFact = new SkGMSampleViewFactory(func);
402 fChain = gHead;
403 gHead = this;
404}
405
406class AutoUnrefArray {
407public:
408 AutoUnrefArray() {}
409 ~AutoUnrefArray() {
410 int count = fObjs.count();
411 for (int i = 0; i < count; ++i) {
412 fObjs[i]->unref();
413 }
414 }
415 SkRefCnt*& push_back() { return *fObjs.append(); }
chudy@google.com4605a3f2012-08-01 17:58:01 +0000416
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000417private:
418 SkTDArray<SkRefCnt*> fObjs;
419};
420
421// registers GMs as Samples
422// This can't be performed during static initialization because it could be
423// run before GMRegistry has been fully built.
caryclark@google.com02939ce2012-06-06 12:09:51 +0000424static void SkGMRegistyToSampleRegistry() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000425 static bool gOnce;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000426 static AutoUnrefArray fRegisters;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000427
428 if (!gOnce) {
429 const skiagm::GMRegistry* gmreg = skiagm::GMRegistry::Head();
430 while (gmreg) {
431 fRegisters.push_back() = new SkViewRegister(gmreg->factory());
432 gmreg = gmreg->next();
433 }
434 gOnce = true;
435 }
436}
437
reed@google.com29038ed2011-07-06 17:56:47 +0000438#if 0
reed@google.comf0b5f682011-03-11 20:08:25 +0000439#include <CoreFoundation/CoreFoundation.h>
440#include <CoreFoundation/CFURLAccess.h>
441
442static void testpdf() {
443 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
444 kCFStringEncodingUTF8);
445 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
446 kCFURLPOSIXPathStyle,
447 false);
448 CFRelease(path);
449 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
450 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
451 CFRelease(url);
452
453 CGContextBeginPage(cg, &box);
454 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
455 CGContextFillEllipseInRect(cg, r);
456 CGContextEndPage(cg);
457 CGContextRelease(cg);
458
459 if (false) {
460 SkBitmap bm;
461 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
462 bm.allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +0000463 bm.eraseColor(SK_ColorTRANSPARENT);
reed@google.comf0b5f682011-03-11 20:08:25 +0000464
465 SkCanvas canvas(bm);
466
467 }
468}
469#endif
470
471//////////////////////////////////////////////////////////////////////////////
472
reed@google.com569e0432011-04-05 13:07:03 +0000473enum FlipAxisEnum {
474 kFlipAxis_X = (1 << 0),
475 kFlipAxis_Y = (1 << 1)
476};
477
reed@google.comf0b5f682011-03-11 20:08:25 +0000478#include "SkDrawFilter.h"
479
bungeman@google.com96aabc82013-06-03 21:26:34 +0000480struct HintingState {
481 SkPaint::Hinting hinting;
482 const char* name;
483 const char* label;
484};
485static HintingState gHintingStates[] = {
486 {SkPaint::kNo_Hinting, "Mixed", NULL },
487 {SkPaint::kNo_Hinting, "None", "H0 " },
488 {SkPaint::kSlight_Hinting, "Slight", "Hs " },
489 {SkPaint::kNormal_Hinting, "Normal", "Hn " },
490 {SkPaint::kFull_Hinting, "Full", "Hf " },
491};
492
reed@google.com569e0432011-04-05 13:07:03 +0000493class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000494public:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000495 FlagsDrawFilter(SkOSMenu::TriState lcd, SkOSMenu::TriState aa, SkOSMenu::TriState filter,
bungeman@google.com96aabc82013-06-03 21:26:34 +0000496 SkOSMenu::TriState subpixel, int hinting)
497 : fLCDState(lcd), fAAState(aa), fFilterState(filter), fSubpixelState(subpixel)
498 , fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000499
reed@google.com971aca72012-11-26 20:26:54 +0000500 virtual bool filter(SkPaint* paint, Type t) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000501 if (kText_Type == t && SkOSMenu::kMixedState != fLCDState) {
502 paint->setLCDRenderText(SkOSMenu::kOnState == fLCDState);
reed@google.com569e0432011-04-05 13:07:03 +0000503 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000504 if (SkOSMenu::kMixedState != fAAState) {
505 paint->setAntiAlias(SkOSMenu::kOnState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000506 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000507 if (SkOSMenu::kMixedState != fFilterState) {
508 paint->setFilterBitmap(SkOSMenu::kOnState == fFilterState);
reed@google.com176753a2011-05-17 15:32:04 +0000509 }
bungeman@google.com96aabc82013-06-03 21:26:34 +0000510 if (SkOSMenu::kMixedState != fSubpixelState) {
511 paint->setSubpixelText(SkOSMenu::kOnState == fSubpixelState);
512 }
bungeman@google.com055aa522013-06-03 21:35:03 +0000513 if (0 != fHintingState && fHintingState < (int)SK_ARRAY_COUNT(gHintingStates)) {
bungeman@google.com96aabc82013-06-03 21:26:34 +0000514 paint->setHinting(gHintingStates[fHintingState].hinting);
reed@google.com09e3baa2011-05-18 12:04:31 +0000515 }
reed@google.com971aca72012-11-26 20:26:54 +0000516 return true;
reed@google.comf0b5f682011-03-11 20:08:25 +0000517 }
518
519private:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000520 SkOSMenu::TriState fLCDState;
521 SkOSMenu::TriState fAAState;
522 SkOSMenu::TriState fFilterState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000523 SkOSMenu::TriState fSubpixelState;
524 int fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000525};
526
reed@android.com8a1c16f2008-12-17 15:59:43 +0000527//////////////////////////////////////////////////////////////////////////////
528
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000529#define MAX_ZOOM_LEVEL 8
530#define MIN_ZOOM_LEVEL -8
531
reed@android.comf2b98d62010-12-20 18:26:13 +0000532static const char gCharEvtName[] = "SampleCode_Char_Event";
533static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000534static const char gTitleEvtName[] = "SampleCode_Title_Event";
535static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000536static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
reed@google.com2072db82011-11-08 15:18:10 +0000537static const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle";
reed@android.comf2b98d62010-12-20 18:26:13 +0000538
539bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
540 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
541 if (outUni) {
542 *outUni = evt.getFast32();
543 }
544 return true;
545 }
546 return false;
547}
548
549bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
550 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
551 if (outKey) {
552 *outKey = (SkKey)evt.getFast32();
553 }
554 return true;
555 }
556 return false;
557}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000558
559bool SampleCode::TitleQ(const SkEvent& evt) {
560 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
561}
562
563void SampleCode::TitleR(SkEvent* evt, const char title[]) {
564 SkASSERT(evt && TitleQ(*evt));
565 evt->setString(gTitleEvtName, title);
566}
567
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000568bool SampleCode::RequestTitle(SkView* view, SkString* title) {
569 SkEvent evt(gTitleEvtName);
570 if (view->doQuery(&evt)) {
571 title->set(evt.findString(gTitleEvtName));
572 return true;
573 }
574 return false;
575}
576
reed@android.com8a1c16f2008-12-17 15:59:43 +0000577bool SampleCode::PrefSizeQ(const SkEvent& evt) {
578 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
579}
580
581void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
582 SkASSERT(evt && PrefSizeQ(*evt));
583 SkScalar size[2];
584 size[0] = width;
585 size[1] = height;
586 evt->setScalars(gPrefSizeEvtName, 2, size);
587}
588
reed@android.comf2b98d62010-12-20 18:26:13 +0000589bool SampleCode::FastTextQ(const SkEvent& evt) {
590 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
591}
592
593///////////////////////////////////////////////////////////////////////////////
594
reed@android.com44177402009-11-23 21:07:51 +0000595static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000596static SkMSec gAnimTimePrev;
597
reed@android.com44177402009-11-23 21:07:51 +0000598SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000599SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
600SkScalar SampleCode::GetAnimSecondsDelta() {
601 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
602}
reed@android.com44177402009-11-23 21:07:51 +0000603
604SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000605 // since gAnimTime can be up to 32 bits, we can't convert it to a float
606 // or we'll lose the low bits. Hence we use doubles for the intermediate
607 // calculations
608 double seconds = (double)gAnimTime / 1000.0;
609 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000610 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000611 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000612 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000613 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000614}
615
bsalomon@google.com85003222012-03-28 14:44:37 +0000616SkScalar SampleCode::GetAnimSinScalar(SkScalar amplitude,
617 SkScalar periodInSec,
618 SkScalar phaseInSec) {
619 if (!periodInSec) {
620 return 0;
621 }
622 double t = (double)gAnimTime / 1000.0 + phaseInSec;
623 t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
624 amplitude = SK_ScalarHalf * amplitude;
625 return SkScalarMul(amplitude, SkDoubleToScalar(sin(t))) + amplitude;
626}
627
reed@google.com3cec4d72011-07-06 13:59:47 +0000628GrContext* SampleCode::GetGr() {
629 return gSampleWindow ? gSampleWindow->getGrContext() : NULL;
630}
631
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000632// some GMs rely on having a skiagm::GetGr function defined
633namespace skiagm {
caryclark@google.com02939ce2012-06-06 12:09:51 +0000634 // FIXME: this should be moved into a header
635 GrContext* GetGr();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000636 GrContext* GetGr() { return SampleCode::GetGr(); }
637}
638
reed@android.com8a1c16f2008-12-17 15:59:43 +0000639//////////////////////////////////////////////////////////////////////////////
640
reed@android.comf2b98d62010-12-20 18:26:13 +0000641static SkView* curr_view(SkWindow* wind) {
642 SkView::F2BIter iter(wind);
643 return iter.next();
644}
645
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000646static bool curr_title(SkWindow* wind, SkString* title) {
647 SkView* view = curr_view(wind);
648 if (view) {
649 SkEvent evt(gTitleEvtName);
650 if (view->doQuery(&evt)) {
651 title->set(evt.findString(gTitleEvtName));
652 return true;
653 }
654 }
655 return false;
656}
657
Scroggo2c8208f2011-06-15 16:49:08 +0000658void SampleWindow::setZoomCenter(float x, float y)
659{
660 fZoomCenterX = SkFloatToScalar(x);
661 fZoomCenterY = SkFloatToScalar(y);
662}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000663
Scroggo0f185c22011-03-24 18:35:50 +0000664bool SampleWindow::zoomIn()
665{
666 // Arbitrarily decided
667 if (fFatBitsScale == 25) return false;
668 fFatBitsScale++;
669 this->inval(NULL);
670 return true;
671}
672
673bool SampleWindow::zoomOut()
674{
675 if (fFatBitsScale == 1) return false;
676 fFatBitsScale--;
677 this->inval(NULL);
678 return true;
679}
680
Scroggo0f185c22011-03-24 18:35:50 +0000681void SampleWindow::updatePointer(int x, int y)
682{
683 fMouseX = x;
684 fMouseY = y;
685 if (fShowZoomer) {
686 this->inval(NULL);
687 }
688}
689
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000690static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
691 static const SampleWindow::DeviceType gCT[] = {
692 SampleWindow::kPicture_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000693#if SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000694 SampleWindow::kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000695#if SK_ANGLE
696 SampleWindow::kANGLE_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000697#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +0000698 SampleWindow::kRaster_DeviceType, // skip the null gpu device in normal cycling
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000699#endif // SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000700 SampleWindow::kRaster_DeviceType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000701 };
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000702 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gCT) == SampleWindow::kDeviceTypeCnt, array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000703 return gCT[ct];
704}
705
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000706static void usage(const char * argv0) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000707 SkDebugf("%s [--slide sampleName] [-i resourcePath] [--msaa sampleCount] [--pictureDir dirPath] [--picture path] [--sort]\n", argv0);
708#ifdef SAMPLE_PDF_FILE_VIEWER
709 SkDebugf(" [--pdfDir pdfPath]\n");
710 SkDebugf(" pdfPath: path to directory pdf files are read from\n");
711#endif // SAMPLE_PDF_FILE_VIEWER
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000712 SkDebugf(" sampleName: sample at which to start.\n");
713 SkDebugf(" resourcePath: directory that stores image resources.\n");
bsalomon@google.com11959252012-04-06 20:13:38 +0000714 SkDebugf(" msaa: request multisampling with the given sample count.\n");
chudy@google.com4605a3f2012-08-01 17:58:01 +0000715 SkDebugf(" dirPath: path to directory skia pictures are read from\n");
716 SkDebugf(" path: path to skia picture\n");
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000717 SkDebugf(" --sort: sort samples by title, this would help to compare pdf rendering (P:foo.pdf) with skp rendering (P:foo.pdf)\n");
718}
719
720static SkString getSampleTitle(const SkViewFactory* sampleFactory) {
721 SkView* view = (*sampleFactory)();
722 SkString title;
723 SampleCode::RequestTitle(view, &title);
724 view->unref();
725 return title;
726}
727
edisonn@google.comde36b692013-07-11 15:40:31 +0000728static bool compareSampleTitle(const SkViewFactory* first, const SkViewFactory* second) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000729 return strcmp(getSampleTitle(first).c_str(), getSampleTitle(second).c_str()) < 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000730}
731
chudy@google.com4605a3f2012-08-01 17:58:01 +0000732SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager)
robertphillips@google.com4750fa52012-04-10 13:34:11 +0000733 : INHERITED(hwnd)
734 , fDevManager(NULL) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000735
reed@google.com7d05ac92013-06-06 13:14:13 +0000736 fCurrIndex = -1;
737
reed@google.com1830c7a2012-06-04 12:05:43 +0000738 this->registerPictFileSamples(argv, argc);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000739 this->registerPictFileSample(argv, argc);
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000740#ifdef SAMPLE_PDF_FILE_VIEWER
741 this->registerPdfFileViewerSamples(argv, argc);
742#endif // SAMPLE_PDF_FILE_VIEWER
robertphillips@google.com76d40212012-03-22 14:12:15 +0000743 SkGMRegistyToSampleRegistry();
744 {
745 const SkViewRegister* reg = SkViewRegister::Head();
746 while (reg) {
747 *fSamples.append() = reg->factory();
748 reg = reg->next();
749 }
750 }
751
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000752 bool sort = false;
753 for (int i = 0; i < argc; ++i) {
754 if (!strcmp(argv[i], "--sort")) {
755 sort = true;
756 break;
757 }
758 }
759
760 if (sort) {
761 // Sort samples, so foo.skp and foo.pdf are consecutive and we can quickly spot where
762 // skp -> pdf -> png fails.
763 SkTQSort(fSamples.begin(), fSamples.end() ? fSamples.end() - 1 : NULL, compareSampleTitle);
764 }
765
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000766 const char* resourcePath = NULL;
bsalomon@google.com11959252012-04-06 20:13:38 +0000767 fMSAASampleCount = 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000768
769 const char* const commandName = argv[0];
770 char* const* stop = argv + argc;
771 for (++argv; argv < stop; ++argv) {
772 if (strcmp(*argv, "-i") == 0) {
773 argv++;
774 if (argv < stop && **argv) {
775 resourcePath = *argv;
776 }
robertphillips@google.com76d40212012-03-22 14:12:15 +0000777 } else if (strcmp(*argv, "--slide") == 0) {
778 argv++;
779 if (argv < stop && **argv) {
780 fCurrIndex = findByTitle(*argv);
781 if (fCurrIndex < 0) {
782 fprintf(stderr, "Unknown sample \"%s\"\n", *argv);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000783 listTitles();
robertphillips@google.com76d40212012-03-22 14:12:15 +0000784 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000785 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000786 } else if (strcmp(*argv, "--msaa") == 0) {
787 ++argv;
788 if (argv < stop && **argv) {
789 fMSAASampleCount = atoi(*argv);
790 }
robertphillips@google.com7265e722012-05-03 18:22:28 +0000791 } else if (strcmp(*argv, "--list") == 0) {
792 listTitles();
bsalomon@google.com11959252012-04-06 20:13:38 +0000793 }
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000794 else {
robertphillips@google.com76d40212012-03-22 14:12:15 +0000795 usage(commandName);
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000796 }
797 }
798
799 if (fCurrIndex < 0) {
800 SkString title;
801 if (readTitleFromPrefs(&title)) {
802 fCurrIndex = findByTitle(title.c_str());
803 }
804 }
805
806 if (fCurrIndex < 0) {
807 fCurrIndex = 0;
808 }
809
reed@google.com3cec4d72011-07-06 13:59:47 +0000810 gSampleWindow = this;
811
yangsu@google.com1f394212011-06-01 18:03:34 +0000812#ifdef PIPE_FILE
813 //Clear existing file or create file if it doesn't exist
814 FILE* f = fopen(FILE_PATH, "wb");
815 fclose(f);
816#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +0000817
reed@android.com8a1c16f2008-12-17 15:59:43 +0000818 fPicture = NULL;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000819
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000820 fDeviceType = kRaster_DeviceType;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000821
822#if DEFAULT_TO_GPU
823 fDeviceType = kGPU_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000824#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000825#if SK_ANGLE && DEFAULT_TO_ANGLE
826 fDeviceType = kANGLE_DeviceType;
827#endif
828
reed@android.com8a1c16f2008-12-17 15:59:43 +0000829 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000830 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000831 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000832 fRotate = false;
bungeman@google.comb1785912013-07-09 21:58:56 +0000833 fRotateAnimTime = 0;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000834 fPerspAnim = false;
835 fPerspAnimTime = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +0000836 fRequestGrabImage = false;
scroggo@google.comb073d922012-06-08 15:35:03 +0000837 fPipeState = SkOSMenu::kOffState;
reed@google.com67b89ee2012-08-15 14:41:58 +0000838 fTilingState = SkOSMenu::kOffState;
scroggo@google.com85cade02012-08-17 13:29:50 +0000839 fTileCount.set(1, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000840 fMeasureFPS = false;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000841 fLCDState = SkOSMenu::kMixedState;
842 fAAState = SkOSMenu::kMixedState;
843 fFilterState = SkOSMenu::kMixedState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000844 fSubpixelState = SkOSMenu::kMixedState;
845 fHintingState = 0;
reed@google.com569e0432011-04-05 13:07:03 +0000846 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000847 fScrollTestX = fScrollTestY = 0;
848
Scroggo0f185c22011-03-24 18:35:50 +0000849 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000850 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000851 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
852 fShowZoomer = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000853
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000854 fZoomLevel = 0;
855 fZoomScale = SK_Scalar1;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000856
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000857 fMagnify = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000858
Scroggo8ac0d542011-06-21 14:44:57 +0000859 fSaveToPdf = false;
860 fPdfCanvas = NULL;
861
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000862 fTransitionNext = 6;
863 fTransitionPrev = 2;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000864
yangsu@google.com921091f2011-08-02 13:39:12 +0000865 int sinkID = this->getSinkID();
scroggo@google.com7dadc742012-04-18 14:07:57 +0000866 fAppMenu = new SkOSMenu;
867 fAppMenu->setTitle("Global Settings");
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000868 int itemID;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000869
870 itemID =fAppMenu->appendList("Device Type", "Device Type", sinkID, 0,
871 "Raster", "Picture", "OpenGL",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000872#if SK_ANGLE
873 "ANGLE",
874#endif
875 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000876 fAppMenu->assignKeyEquivalentToItem(itemID, 'd');
877 itemID = fAppMenu->appendTriState("AA", "AA", sinkID, fAAState);
878 fAppMenu->assignKeyEquivalentToItem(itemID, 'b');
879 itemID = fAppMenu->appendTriState("LCD", "LCD", sinkID, fLCDState);
880 fAppMenu->assignKeyEquivalentToItem(itemID, 'l');
881 itemID = fAppMenu->appendTriState("Filter", "Filter", sinkID, fFilterState);
882 fAppMenu->assignKeyEquivalentToItem(itemID, 'n');
bungeman@google.com96aabc82013-06-03 21:26:34 +0000883 itemID = fAppMenu->appendTriState("Subpixel", "Subpixel", sinkID, fSubpixelState);
884 fAppMenu->assignKeyEquivalentToItem(itemID, 's');
885 itemID = fAppMenu->appendList("Hinting", "Hinting", sinkID, fHintingState,
886 gHintingStates[0].name,
887 gHintingStates[1].name,
888 gHintingStates[2].name,
889 gHintingStates[3].name,
bungeman@google.coma9133bb2013-06-04 17:49:20 +0000890 gHintingStates[4].name,
891 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000892 fAppMenu->assignKeyEquivalentToItem(itemID, 'h');
reed@google.com67b89ee2012-08-15 14:41:58 +0000893
scroggo@google.comb073d922012-06-08 15:35:03 +0000894 fUsePipeMenuItemID = fAppMenu->appendTriState("Pipe", "Pipe" , sinkID,
chudy@google.com4605a3f2012-08-01 17:58:01 +0000895 fPipeState);
scroggo@google.comb073d922012-06-08 15:35:03 +0000896 fAppMenu->assignKeyEquivalentToItem(fUsePipeMenuItemID, 'P');
reed@google.com67b89ee2012-08-15 14:41:58 +0000897
898 itemID = fAppMenu->appendTriState("Tiling", "Tiling", sinkID, fTilingState);
899 fAppMenu->assignKeyEquivalentToItem(itemID, 't');
900
chudy@google.com4605a3f2012-08-01 17:58:01 +0000901 itemID = fAppMenu->appendSwitch("Slide Show", "Slide Show" , sinkID, false);
902 fAppMenu->assignKeyEquivalentToItem(itemID, 'a');
903 itemID = fAppMenu->appendSwitch("Clip", "Clip" , sinkID, fUseClip);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000904 fAppMenu->assignKeyEquivalentToItem(itemID, 'c');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000905 itemID = fAppMenu->appendSwitch("Flip X", "Flip X" , sinkID, false);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000906 fAppMenu->assignKeyEquivalentToItem(itemID, 'x');
907 itemID = fAppMenu->appendSwitch("Flip Y", "Flip Y" , sinkID, false);
908 fAppMenu->assignKeyEquivalentToItem(itemID, 'y');
909 itemID = fAppMenu->appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
910 fAppMenu->assignKeyEquivalentToItem(itemID, 'z');
911 itemID = fAppMenu->appendSwitch("Magnify", "Magnify" , sinkID, fMagnify);
912 fAppMenu->assignKeyEquivalentToItem(itemID, 'm');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000913 itemID =fAppMenu->appendList("Transition-Next", "Transition-Next", sinkID,
914 fTransitionNext, "Up", "Up and Right", "Right",
915 "Down and Right", "Down", "Down and Left",
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000916 "Left", "Up and Left", NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000917 fAppMenu->assignKeyEquivalentToItem(itemID, 'j');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000918 itemID =fAppMenu->appendList("Transition-Prev", "Transition-Prev", sinkID,
919 fTransitionPrev, "Up", "Up and Right", "Right",
920 "Down and Right", "Down", "Down and Left",
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000921 "Left", "Up and Left", NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000922 fAppMenu->assignKeyEquivalentToItem(itemID, 'k');
923 itemID = fAppMenu->appendAction("Save to PDF", sinkID);
924 fAppMenu->assignKeyEquivalentToItem(itemID, 'e');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000925
scroggo@google.com7dadc742012-04-18 14:07:57 +0000926 this->addMenu(fAppMenu);
927 fSlideMenu = new SkOSMenu;
928 this->addMenu(fSlideMenu);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000929
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000930// this->setConfig(SkBitmap::kRGB_565_Config);
931 this->setConfig(SkBitmap::kARGB_8888_Config);
932 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000933 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000934
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000935 skiagm::GM::SetResourcePath(resourcePath);
936
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000937 this->loadView((*fSamples[fCurrIndex])());
chudy@google.com4605a3f2012-08-01 17:58:01 +0000938
bsalomon@google.com840e9f32011-07-06 21:59:09 +0000939 fPDFData = NULL;
reed@google.comf0b5f682011-03-11 20:08:25 +0000940
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000941 if (NULL == devManager) {
942 fDevManager = new DefaultDeviceManager();
943 } else {
944 devManager->ref();
945 fDevManager = devManager;
946 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000947 fDevManager->setUpBackend(this, fMSAASampleCount);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000948
Scroggob4490c72011-06-17 13:53:05 +0000949 // If another constructor set our dimensions, ensure that our
950 // onSizeChange gets called.
951 if (this->height() && this->width()) {
952 this->onSizeChange();
953 }
reed@google.com2072db82011-11-08 15:18:10 +0000954
955 // can't call this synchronously, since it may require a subclass to
956 // to implement, or the caller may need us to have returned from the
957 // constructor first. Hence we post an event to ourselves.
958// this->updateTitle();
959 postEventToSink(new SkEvent(gUpdateWindowTitleEvtName), this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000960}
961
962SampleWindow::~SampleWindow() {
963 delete fPicture;
Scroggo8ac0d542011-06-21 14:44:57 +0000964 delete fPdfCanvas;
Scroggo0f185c22011-03-24 18:35:50 +0000965 fTypeface->unref();
reed@google.com29038ed2011-07-06 17:56:47 +0000966
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000967 SkSafeUnref(fDevManager);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000968}
969
reed@google.com1830c7a2012-06-04 12:05:43 +0000970static void make_filepath(SkString* path, const char* dir, const SkString& name) {
971 size_t len = strlen(dir);
972 path->set(dir);
973 if (len > 0 && dir[len - 1] != '/') {
974 path->append("/");
975 }
976 path->append(name);
977}
978
chudy@google.com4605a3f2012-08-01 17:58:01 +0000979void SampleWindow::registerPictFileSample(char** argv, int argc) {
980 const char* pict = NULL;
981
982 for (int i = 0; i < argc; ++i) {
983 if (!strcmp(argv[i], "--picture")) {
984 i += 1;
985 if (i < argc) {
986 pict = argv[i];
987 break;
988 }
989 }
990 }
991 if (pict) {
992 SkString path(pict);
reed@google.com7d05ac92013-06-06 13:14:13 +0000993 fCurrIndex = fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +0000994 *fSamples.append() = new PictFileFactory(path);
995 }
996}
997
reed@google.com1830c7a2012-06-04 12:05:43 +0000998void SampleWindow::registerPictFileSamples(char** argv, int argc) {
999 const char* pictDir = NULL;
1000
1001 for (int i = 0; i < argc; ++i) {
1002 if (!strcmp(argv[i], "--pictureDir")) {
1003 i += 1;
1004 if (i < argc) {
1005 pictDir = argv[i];
1006 break;
1007 }
1008 }
1009 }
1010 if (pictDir) {
1011 SkOSFile::Iter iter(pictDir, "skp");
1012 SkString filename;
1013 while (iter.next(&filename)) {
1014 SkString path;
1015 make_filepath(&path, pictDir, filename);
1016 *fSamples.append() = new PictFileFactory(path);
1017 }
1018 }
1019}
1020
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001021#ifdef SAMPLE_PDF_FILE_VIEWER
1022void SampleWindow::registerPdfFileViewerSamples(char** argv, int argc) {
1023 const char* pdfDir = NULL;
1024
1025 for (int i = 0; i < argc; ++i) {
1026 if (!strcmp(argv[i], "--pdfDir")) {
1027 i += 1;
1028 if (i < argc) {
1029 pdfDir = argv[i];
1030 break;
1031 }
1032 }
1033 }
1034 if (pdfDir) {
1035 SkOSFile::Iter iter(pdfDir, "pdf");
1036 SkString filename;
1037 while (iter.next(&filename)) {
1038 SkString path;
1039 make_filepath(&path, pdfDir, filename);
1040 *fSamples.append() = new PdfFileViewerFactory(path);
1041 }
1042 }
1043}
1044#endif // SAMPLE_PDF_FILE_VIEWER
1045
1046
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001047int SampleWindow::findByTitle(const char title[]) {
1048 int i, count = fSamples.count();
1049 for (i = 0; i < count; i++) {
1050 if (getSampleTitle(i).equals(title)) {
1051 return i;
1052 }
1053 }
mike@reedtribe.orgdd52caa2011-12-28 20:02:10 +00001054 return -1;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001055}
1056
robertphillips@google.com7265e722012-05-03 18:22:28 +00001057void SampleWindow::listTitles() {
1058 int count = fSamples.count();
1059 SkDebugf("All Slides:\n");
1060 for (int i = 0; i < count; i++) {
1061 SkDebugf(" %s\n", getSampleTitle(i).c_str());
1062 }
1063}
1064
reed@android.com55e76b22009-11-23 21:46:47 +00001065static SkBitmap capture_bitmap(SkCanvas* canvas) {
1066 SkBitmap bm;
1067 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
1068 src.copyTo(&bm, src.config());
1069 return bm;
1070}
1071
1072static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
1073 SkBitmap* diff) {
1074 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001075
reed@android.com55e76b22009-11-23 21:46:47 +00001076 SkAutoLockPixels alp0(src);
1077 SkAutoLockPixels alp1(orig);
1078 for (int y = 0; y < src.height(); y++) {
1079 const void* srcP = src.getAddr(0, y);
1080 const void* origP = orig.getAddr(0, y);
1081 size_t bytes = src.width() * src.bytesPerPixel();
1082 if (memcmp(srcP, origP, bytes)) {
1083 SkDebugf("---------- difference on line %d\n", y);
1084 return true;
1085 }
1086 }
1087 return false;
1088}
1089
Scroggo0f185c22011-03-24 18:35:50 +00001090static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
1091{
1092 SkColor desiredColor = paint.getColor();
1093 paint.setColor(SK_ColorWHITE);
1094 const char* c_str = string.c_str();
1095 size_t size = string.size();
1096 SkRect bounds;
1097 paint.measureText(c_str, size, &bounds);
1098 bounds.offset(left, top);
1099 SkScalar inset = SkIntToScalar(-2);
1100 bounds.inset(inset, inset);
1101 canvas->drawRect(bounds, paint);
1102 if (desiredColor != SK_ColorBLACK) {
1103 paint.setColor(SK_ColorBLACK);
1104 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
1105 }
1106 paint.setColor(desiredColor);
1107 canvas->drawText(c_str, size, left, top, paint);
1108}
1109
reed@android.com44177402009-11-23 21:07:51 +00001110#define XCLIP_N 8
1111#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +00001112
1113void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +00001114 // update the animation time
bsalomon@google.come8f09102011-09-08 18:48:12 +00001115 if (!gAnimTimePrev && !gAnimTime) {
1116 // first time make delta be 0
1117 gAnimTime = SkTime::GetMSecs();
1118 gAnimTimePrev = gAnimTime;
1119 } else {
1120 gAnimTimePrev = gAnimTime;
1121 gAnimTime = SkTime::GetMSecs();
1122 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001123
reed@google.comf03bb562011-11-11 21:42:12 +00001124 if (fGesture.isActive()) {
1125 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001126 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001127
djsollen@google.com796763e2012-12-10 14:12:55 +00001128 if (fMeasureFPS) {
1129 fMeasureFPS_Time = 0;
1130 }
1131
reed@android.come522ca52009-11-23 20:10:41 +00001132 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +00001133 this->INHERITED::draw(canvas);
1134 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +00001135
1136 const SkScalar w = this->width();
1137 const SkScalar h = this->height();
1138 const SkScalar cw = w / XCLIP_N;
1139 const SkScalar ch = h / YCLIP_N;
1140 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +00001141 SkRect r;
1142 r.fTop = y * ch;
1143 r.fBottom = (y + 1) * ch;
1144 if (y == YCLIP_N - 1) {
1145 r.fBottom = h;
1146 }
reed@android.come522ca52009-11-23 20:10:41 +00001147 for (int x = 0; x < XCLIP_N; x++) {
1148 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +00001149 r.fLeft = x * cw;
1150 r.fRight = (x + 1) * cw;
1151 if (x == XCLIP_N - 1) {
1152 r.fRight = w;
1153 }
reed@android.come522ca52009-11-23 20:10:41 +00001154 canvas->clipRect(r);
1155 this->INHERITED::draw(canvas);
1156 }
1157 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001158
reed@android.com55e76b22009-11-23 21:46:47 +00001159 SkBitmap diff;
1160 if (bitmap_diff(canvas, orig, &diff)) {
1161 }
reed@android.come522ca52009-11-23 20:10:41 +00001162 } else {
scroggo@google.com85cade02012-08-17 13:29:50 +00001163 const SkScalar cw = SkScalarDiv(this->width(), SkIntToScalar(fTileCount.width()));
1164 const SkScalar ch = SkScalarDiv(this->height(), SkIntToScalar(fTileCount.height()));
rmistry@google.comae933ce2012-08-23 18:19:56 +00001165
scroggo@google.com85cade02012-08-17 13:29:50 +00001166 for (int y = 0; y < fTileCount.height(); ++y) {
1167 for (int x = 0; x < fTileCount.width(); ++x) {
1168 SkAutoCanvasRestore acr(canvas, true);
1169 canvas->clipRect(SkRect::MakeXYWH(x * cw, y * ch, cw, ch));
1170 this->INHERITED::draw(canvas);
1171 }
1172 }
rmistry@google.comae933ce2012-08-23 18:19:56 +00001173
scroggo@google.com85cade02012-08-17 13:29:50 +00001174 if (!fTileCount.equals(1, 1)) {
1175 SkPaint paint;
1176 paint.setColor(0x60FF00FF);
1177 paint.setStyle(SkPaint::kStroke_Style);
1178 for (int y = 0; y < fTileCount.height(); ++y) {
1179 for (int x = 0; x < fTileCount.width(); ++x) {
1180 canvas->drawRect(SkRect::MakeXYWH(x * cw, y * ch, cw, ch), paint);
1181 }
1182 }
1183 }
reed@android.come522ca52009-11-23 20:10:41 +00001184 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001185 if (fShowZoomer && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +00001186 showZoomer(canvas);
1187 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001188 if (fMagnify && !fSaveToPdf) {
1189 magnify(canvas);
1190 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001191
djsollen@google.com796763e2012-12-10 14:12:55 +00001192 if (fMeasureFPS && fMeasureFPS_Time) {
1193 this->updateTitle();
1194 this->postInvalDelay();
1195 }
1196
reed@google.com29038ed2011-07-06 17:56:47 +00001197 // do this last
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001198 fDevManager->publishCanvas(fDeviceType, canvas, this);
Scroggo3272ba82011-05-25 20:50:42 +00001199}
1200
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001201static float clipW = 200;
1202static float clipH = 200;
1203void SampleWindow::magnify(SkCanvas* canvas) {
1204 SkRect r;
1205 int count = canvas->save();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001206
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001207 SkMatrix m = canvas->getTotalMatrix();
robertphillips@google.com07ef9112012-06-04 13:22:14 +00001208 if (!m.invert(&m)) {
1209 return;
1210 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001211 SkPoint offset, center;
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001212 SkScalar mouseX = fMouseX * SK_Scalar1;
1213 SkScalar mouseY = fMouseY * SK_Scalar1;
1214 m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset);
1215 m.mapXY(mouseX, mouseY, &center);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001216
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001217 r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX());
1218 r.offset(offset.fX, offset.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001219
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001220 SkPaint paint;
1221 paint.setColor(0xFF66AAEE);
1222 paint.setStyle(SkPaint::kStroke_Style);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001223 paint.setStrokeWidth(10.f * m.getScaleX());
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001224 //lense offset
1225 //canvas->translate(0, -250);
1226 canvas->drawRect(r, paint);
1227 canvas->clipRect(r);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001228
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001229 m = canvas->getTotalMatrix();
1230 m.setTranslate(-center.fX, -center.fY);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001231 m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001232 m.postTranslate(center.fX, center.fY);
1233 canvas->concat(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001234
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001235 this->INHERITED::draw(canvas);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001236
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001237 canvas->restoreToCount(count);
1238}
1239
Scroggo3272ba82011-05-25 20:50:42 +00001240void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +00001241 int count = canvas->save();
1242 canvas->resetMatrix();
1243 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +00001244 int width = SkScalarRound(this->width());
1245 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +00001246 if (fMouseX >= width) fMouseX = width - 1;
1247 else if (fMouseX < 0) fMouseX = 0;
1248 if (fMouseY >= height) fMouseY = height - 1;
1249 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +00001250
Scroggo0f185c22011-03-24 18:35:50 +00001251 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +00001252 bitmap.lockPixels();
1253
Scroggo0f185c22011-03-24 18:35:50 +00001254 // 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 +00001255 int zoomedWidth = (width >> 1) | 1;
1256 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +00001257 SkIRect src;
1258 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
1259 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
1260 SkRect dest;
1261 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
1262 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
1263 SkPaint paint;
1264 // Clear the background behind our zoomed in view
1265 paint.setColor(SK_ColorWHITE);
1266 canvas->drawRect(dest, paint);
1267 canvas->drawBitmapRect(bitmap, &src, dest);
1268 paint.setColor(SK_ColorBLACK);
1269 paint.setStyle(SkPaint::kStroke_Style);
1270 // Draw a border around the pixel in the middle
1271 SkRect originalPixel;
1272 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
1273 SkMatrix matrix;
1274 SkRect scalarSrc;
1275 scalarSrc.set(src);
1276 SkColor color = bitmap.getColor(fMouseX, fMouseY);
1277 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
1278 SkRect pixel;
1279 matrix.mapRect(&pixel, originalPixel);
1280 // TODO Perhaps measure the values and make the outline white if it's "dark"
1281 if (color == SK_ColorBLACK) {
1282 paint.setColor(SK_ColorWHITE);
1283 }
1284 canvas->drawRect(pixel, paint);
1285 }
1286 paint.setColor(SK_ColorBLACK);
1287 // Draw a border around the destination rectangle
1288 canvas->drawRect(dest, paint);
1289 paint.setStyle(SkPaint::kStrokeAndFill_Style);
1290 // Identify the pixel and its color on screen
1291 paint.setTypeface(fTypeface);
1292 paint.setAntiAlias(true);
1293 SkScalar lineHeight = paint.getFontMetrics(NULL);
1294 SkString string;
1295 string.appendf("(%i, %i)", fMouseX, fMouseY);
1296 SkScalar left = dest.fLeft + SkIntToScalar(3);
1297 SkScalar i = SK_Scalar1;
1298 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1299 // Alpha
1300 i += SK_Scalar1;
1301 string.reset();
1302 string.appendf("A: %X", SkColorGetA(color));
1303 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1304 // Red
1305 i += SK_Scalar1;
1306 string.reset();
1307 string.appendf("R: %X", SkColorGetR(color));
1308 paint.setColor(SK_ColorRED);
1309 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1310 // Green
1311 i += SK_Scalar1;
1312 string.reset();
1313 string.appendf("G: %X", SkColorGetG(color));
1314 paint.setColor(SK_ColorGREEN);
1315 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1316 // Blue
1317 i += SK_Scalar1;
1318 string.reset();
1319 string.appendf("B: %X", SkColorGetB(color));
1320 paint.setColor(SK_ColorBLUE);
1321 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1322 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +00001323}
1324
reed@android.com8a1c16f2008-12-17 15:59:43 +00001325void SampleWindow::onDraw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001326}
1327
1328#include "SkColorPriv.h"
1329
caryclark@google.com02939ce2012-06-06 12:09:51 +00001330#if 0 // UNUSED
reed@android.com8a1c16f2008-12-17 15:59:43 +00001331static void reverseRedAndBlue(const SkBitmap& bm) {
1332 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
1333 uint8_t* p = (uint8_t*)bm.getPixels();
1334 uint8_t* stop = p + bm.getSize();
1335 while (p < stop) {
1336 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
1337 unsigned scale = SkAlpha255To256(p[3]);
1338 unsigned r = p[2];
1339 unsigned b = p[0];
1340 p[0] = SkAlphaMul(r, scale);
1341 p[1] = SkAlphaMul(p[1], scale);
1342 p[2] = SkAlphaMul(b, scale);
1343 p += 4;
1344 }
1345}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001346#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001347
Scroggo8ac0d542011-06-21 14:44:57 +00001348void SampleWindow::saveToPdf()
1349{
1350 fSaveToPdf = true;
1351 this->inval(NULL);
1352}
1353
reed@android.com8a1c16f2008-12-17 15:59:43 +00001354SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +00001355 if (fSaveToPdf) {
1356 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
1357 SkISize size = SkISize::Make(bmp.width(), bmp.height());
1358 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
1359 canvas->getTotalMatrix());
1360 fPdfCanvas = new SkCanvas(pdfDevice);
1361 pdfDevice->unref();
1362 canvas = fPdfCanvas;
bsalomon@google.com82502e22013-01-24 20:47:18 +00001363 } else if (kPicture_DeviceType == fDeviceType) {
1364 fPicture = new SkPicture;
1365 canvas = fPicture->beginRecording(9999, 9999);
Scroggo8ac0d542011-06-21 14:44:57 +00001366 } else {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001367#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00001368 if (kNullGPU_DeviceType != fDeviceType)
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001369#endif
bsalomon@google.com82502e22013-01-24 20:47:18 +00001370 {
1371 canvas = this->INHERITED::beforeChildren(canvas);
reed@google.comac10a2d2010-12-22 21:39:39 +00001372 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001373 }
1374
1375 if (fUseClip) {
1376 canvas->drawColor(0xFFFF88FF);
reed@google.com045e62d2011-10-24 12:19:46 +00001377 canvas->clipPath(fClipPath, SkRegion::kIntersect_Op, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001378 }
1379
1380 return canvas;
1381}
1382
1383static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
1384 const SkRegion& rgn) {
1385 SkCanvas canvas(bm);
1386 SkRegion inval(rgn);
1387
1388 inval.translate(r.fLeft, r.fTop);
1389 canvas.clipRegion(inval);
1390 canvas.drawColor(0xFFFF8080);
1391}
yangsu@google.com501775e2011-06-24 16:04:50 +00001392#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001393void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +00001394 if (fSaveToPdf) {
1395 fSaveToPdf = false;
1396 if (fShowZoomer) {
1397 showZoomer(fPdfCanvas);
1398 }
1399 SkString name;
1400 name.printf("%s.pdf", this->getTitle());
1401 SkPDFDocument doc;
1402 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
1403 doc.appendPage(device);
djsollen@google.com56c69772011-11-08 19:00:26 +00001404#ifdef SK_BUILD_FOR_ANDROID
Scroggo8ac0d542011-06-21 14:44:57 +00001405 name.prepend("/sdcard/");
1406#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +00001407
yangsu@google.com501775e2011-06-24 16:04:50 +00001408#ifdef SK_BUILD_FOR_IOS
1409 SkDynamicMemoryWStream mstream;
1410 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +00001411 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +00001412#endif
Scroggo8ac0d542011-06-21 14:44:57 +00001413 SkFILEWStream stream(name.c_str());
1414 if (stream.isValid()) {
1415 doc.emitPDF(&stream);
1416 const char* desc = "File saved from Skia SampleApp";
1417 this->onPDFSaved(this->getTitle(), desc, name.c_str());
1418 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001419
Scroggo8ac0d542011-06-21 14:44:57 +00001420 delete fPdfCanvas;
1421 fPdfCanvas = NULL;
1422
1423 // We took over the draw calls in order to create the PDF, so we need
1424 // to redraw.
1425 this->inval(NULL);
1426 return;
1427 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001428
reed@android.comf2b98d62010-12-20 18:26:13 +00001429 if (fRequestGrabImage) {
1430 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001431
reed@google.com29038ed2011-07-06 17:56:47 +00001432 SkDevice* device = orig->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001433 SkBitmap bmp;
1434 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001435 static int gSampleGrabCounter;
1436 SkString name;
reed@google.com7c57e0e2012-09-08 21:20:33 +00001437 name.printf("sample_grab_%d.png", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001438 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +00001439 SkImageEncoder::kPNG_Type, 100);
1440 }
1441 }
1442
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001443 if (kPicture_DeviceType == fDeviceType) {
1444 if (true) {
1445 SkPicture* pict = new SkPicture(*fPicture);
1446 fPicture->unref();
reed@google.come23f1942011-12-08 19:36:00 +00001447 this->installDrawFilter(orig);
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001448 orig->drawPicture(*pict);
1449 pict->unref();
1450 } else if (true) {
1451 SkDynamicMemoryWStream ostream;
1452 fPicture->serialize(&ostream);
1453 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001454
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001455 SkAutoDataUnref data(ostream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +00001456 SkMemoryStream istream(data->data(), data->size());
scroggo@google.comf1754ec2013-06-28 21:32:00 +00001457 SkAutoTUnref<SkPicture> pict(SkPicture::CreateFromStream(&istream));
1458 if (pict.get() != NULL) {
1459 orig->drawPicture(*pict.get());
1460 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001461 } else {
1462 fPicture->draw(orig);
1463 fPicture->unref();
1464 }
1465 fPicture = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001466 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001467
reed@google.com17d7aec2011-04-25 14:31:44 +00001468 // Do this after presentGL and other finishing, rather than in afterChild
djsollen@google.com796763e2012-12-10 14:12:55 +00001469 if (fMeasureFPS && fMeasureFPS_StartTime) {
1470 fMeasureFPS_Time += SkTime::GetMSecs() - fMeasureFPS_StartTime;
reed@google.com17d7aec2011-04-25 14:31:44 +00001471 }
1472
1473 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +00001474 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001475 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
1476 int dx = fScrollTestX * 7;
1477 int dy = fScrollTestY * 7;
1478 SkIRect r;
1479 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001480
reed@android.com8a1c16f2008-12-17 15:59:43 +00001481 r.set(50, 50, 50+100, 50+100);
1482 bm.scrollRect(&r, dx, dy, &inval);
1483 paint_rgn(bm, r, inval);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001484 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001485}
1486
reed@android.com6c5f6f22009-08-14 16:08:38 +00001487void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
reed@android.com6c5f6f22009-08-14 16:08:38 +00001488 if (fRotate) {
bungeman@google.comb1785912013-07-09 21:58:56 +00001489 fRotateAnimTime += SampleCode::GetAnimSecondsDelta();
1490
reed@android.com6c5f6f22009-08-14 16:08:38 +00001491 SkScalar cx = this->width() / 2;
1492 SkScalar cy = this->height() / 2;
1493 canvas->translate(cx, cy);
bungeman@google.comb1785912013-07-09 21:58:56 +00001494 canvas->rotate(fRotateAnimTime * 10);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001495 canvas->translate(-cx, -cy);
1496 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001497
bsalomon@google.come8f09102011-09-08 18:48:12 +00001498 if (fPerspAnim) {
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001499 fPerspAnimTime += SampleCode::GetAnimSecondsDelta();
1500
1501 static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
1502 static const SkScalar gAnimMag = SK_Scalar1 / 1000;
1503 SkScalar t = SkScalarMod(fPerspAnimTime, gAnimPeriod);
1504 if (SkScalarFloorToInt(SkScalarDiv(fPerspAnimTime, gAnimPeriod)) & 0x1) {
1505 t = gAnimPeriod - t;
1506 }
1507 t = 2 * t - gAnimPeriod;
1508 t = SkScalarMul(SkScalarDiv(t, gAnimPeriod), gAnimMag);
1509 SkMatrix m;
1510 m.reset();
1511 m.setPerspY(t);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001512 canvas->concat(m);
1513 }
reed@google.comf0b5f682011-03-11 20:08:25 +00001514
reed@google.come23f1942011-12-08 19:36:00 +00001515 this->installDrawFilter(canvas);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001516
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001517 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +00001518 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
djsollen@google.com796763e2012-12-10 14:12:55 +00001519 fMeasureFPS_StartTime = SkTime::GetMSecs();
reed@google.comf2183392011-04-22 14:10:48 +00001520 }
1521 } else {
1522 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001523 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001524 if (fPerspAnim || fRotate) {
bsalomon@google.come8f09102011-09-08 18:48:12 +00001525 this->inval(NULL);
1526 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001527}
1528
1529void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +00001530 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001531}
1532
reed@android.com8a1c16f2008-12-17 15:59:43 +00001533static SkBitmap::Config gConfigCycle[] = {
1534 SkBitmap::kNo_Config, // none -> none
1535 SkBitmap::kNo_Config, // a1 -> none
1536 SkBitmap::kNo_Config, // a8 -> none
1537 SkBitmap::kNo_Config, // index8 -> none
1538 SkBitmap::kARGB_4444_Config, // 565 -> 4444
1539 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
1540 SkBitmap::kRGB_565_Config // 8888 -> 565
1541};
1542
1543static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
1544 return gConfigCycle[c];
1545}
1546
djsollen@google.come32b5832011-06-13 16:58:40 +00001547void SampleWindow::changeZoomLevel(float delta) {
1548 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001549 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001550 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
1551 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001552 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001553 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
1554 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001555 } else {
1556 fZoomScale = SK_Scalar1;
1557 }
reed@google.comf03bb562011-11-11 21:42:12 +00001558 this->updateMatrix();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001559}
1560
reed@google.comf03bb562011-11-11 21:42:12 +00001561void SampleWindow::updateMatrix(){
1562 SkMatrix m;
1563 m.reset();
1564 if (fZoomLevel) {
1565 SkPoint center;
1566 //m = this->getLocalMatrix();//.invert(&m);
1567 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
1568 SkScalar cx = center.fX;
1569 SkScalar cy = center.fY;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001570
reed@google.comf03bb562011-11-11 21:42:12 +00001571 m.setTranslate(-cx, -cy);
1572 m.postScale(fZoomScale, fZoomScale);
1573 m.postTranslate(cx, cy);
1574 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001575
reed@google.comf03bb562011-11-11 21:42:12 +00001576 if (fFlipAxis) {
1577 m.preTranslate(fZoomCenterX, fZoomCenterY);
1578 if (fFlipAxis & kFlipAxis_X) {
1579 m.preScale(-SK_Scalar1, SK_Scalar1);
1580 }
1581 if (fFlipAxis & kFlipAxis_Y) {
1582 m.preScale(SK_Scalar1, -SK_Scalar1);
1583 }
1584 m.preTranslate(-fZoomCenterX, -fZoomCenterY);
1585 //canvas->concat(m);
1586 }
1587 // Apply any gesture matrix
1588 m.preConcat(fGesture.localM());
1589 m.preConcat(fGesture.globalM());
chudy@google.com4605a3f2012-08-01 17:58:01 +00001590
reed@google.comf03bb562011-11-11 21:42:12 +00001591 this->setLocalMatrix(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001592
reed@google.comf03bb562011-11-11 21:42:12 +00001593 this->updateTitle();
1594 this->inval(NULL);
1595}
Scroggo2c8208f2011-06-15 16:49:08 +00001596bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +00001597 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001598 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001599 fTransitionPrev));
Scroggo2c8208f2011-06-15 16:49:08 +00001600 return true;
1601}
1602
reed@android.com8a1c16f2008-12-17 15:59:43 +00001603bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +00001604 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001605 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001606 fTransitionNext));
reed@android.com34245c72009-11-03 04:00:48 +00001607 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001608}
1609
yangsu@google.com501775e2011-06-24 16:04:50 +00001610bool SampleWindow::goToSample(int i) {
1611 fCurrIndex = (i) % fSamples.count();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001612 this->loadView(create_transition(curr_view(this),(*fSamples[fCurrIndex])(), 6));
yangsu@google.com501775e2011-06-24 16:04:50 +00001613 return true;
1614}
1615
1616SkString SampleWindow::getSampleTitle(int i) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001617 return ::getSampleTitle(fSamples[i]);
yangsu@google.com501775e2011-06-24 16:04:50 +00001618}
1619
1620int SampleWindow::sampleCount() {
1621 return fSamples.count();
1622}
1623
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001624void SampleWindow::showOverview() {
chudy@google.com4605a3f2012-08-01 17:58:01 +00001625 this->loadView(create_transition(curr_view(this),
1626 create_overview(fSamples.count(), fSamples.begin()),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001627 4));
1628}
1629
reed@google.come23f1942011-12-08 19:36:00 +00001630void SampleWindow::installDrawFilter(SkCanvas* canvas) {
bungeman@google.com96aabc82013-06-03 21:26:34 +00001631 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState, fFilterState, fSubpixelState,
1632 fHintingState))->unref();
reed@google.come23f1942011-12-08 19:36:00 +00001633}
1634
Scroggo2c8208f2011-06-15 16:49:08 +00001635void SampleWindow::postAnimatingEvent() {
1636 if (fAnimating) {
reed@google.com87fac4a2011-08-04 13:50:17 +00001637 (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
Scroggo2c8208f2011-06-15 16:49:08 +00001638 }
1639}
reed@google.come23f1942011-12-08 19:36:00 +00001640
reed@android.com8a1c16f2008-12-17 15:59:43 +00001641bool SampleWindow::onEvent(const SkEvent& evt) {
reed@google.com2072db82011-11-08 15:18:10 +00001642 if (evt.isType(gUpdateWindowTitleEvtName)) {
1643 this->updateTitle();
1644 return true;
1645 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001646 if (evt.isType(ANIMATING_EVENTTYPE)) {
1647 if (fAnimating) {
1648 this->nextSample();
1649 this->postAnimatingEvent();
1650 }
1651 return true;
1652 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001653 if (evt.isType("replace-transition-view")) {
1654 this->loadView((SkView*)SkEventSink::FindSink(evt.getFast32()));
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001655 return true;
1656 }
reed@android.com34245c72009-11-03 04:00:48 +00001657 if (evt.isType("set-curr-index")) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001658 this->goToSample(evt.getFast32());
reed@android.com34245c72009-11-03 04:00:48 +00001659 return true;
1660 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001661 if (isInvalEvent(evt)) {
1662 this->inval(NULL);
1663 return true;
1664 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001665 int selected = -1;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001666 if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001667 this->setDeviceType((DeviceType)selected);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001668 return true;
yangsu@google.com921091f2011-08-02 13:39:12 +00001669 }
scroggo@google.comb073d922012-06-08 15:35:03 +00001670 if (SkOSMenu::FindTriState(evt, "Pipe", &fPipeState)) {
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001671#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00001672 if (!fPipeState != SkOSMenu::kOnState)
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001673 gServer.disconnectAll();
1674#endif
scroggo@google.comb073d922012-06-08 15:35:03 +00001675 (void)SampleView::SetUsePipe(curr_view(this), fPipeState);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001676 this->updateTitle();
1677 this->inval(NULL);
yangsu@google.com921091f2011-08-02 13:39:12 +00001678 return true;
1679 }
reed@google.com67b89ee2012-08-15 14:41:58 +00001680 if (SkOSMenu::FindTriState(evt, "Tiling", &fTilingState)) {
1681 int nx = 1, ny = 1;
1682 switch (fTilingState) {
1683 case SkOSMenu::kOffState: nx = 1; ny = 1; break;
reed@google.com56b64a52012-08-15 20:44:36 +00001684 case SkOSMenu::kMixedState: nx = 1; ny = 16; break;
1685 case SkOSMenu::kOnState: nx = 4; ny = 4; break;
reed@google.com67b89ee2012-08-15 14:41:58 +00001686 }
scroggo@google.com85cade02012-08-17 13:29:50 +00001687 fTileCount.set(nx, ny);
reed@google.com67b89ee2012-08-15 14:41:58 +00001688 this->inval(NULL);
1689 return true;
1690 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001691 if (SkOSMenu::FindSwitchState(evt, "Slide Show", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001692 this->toggleSlideshow();
1693 return true;
1694 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001695 if (SkOSMenu::FindTriState(evt, "AA", &fAAState) ||
1696 SkOSMenu::FindTriState(evt, "LCD", &fLCDState) ||
1697 SkOSMenu::FindTriState(evt, "Filter", &fFilterState) ||
bungeman@google.com96aabc82013-06-03 21:26:34 +00001698 SkOSMenu::FindTriState(evt, "Subpixel", &fSubpixelState) ||
1699 SkOSMenu::FindListIndex(evt, "Hinting", &fHintingState) ||
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001700 SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) ||
1701 SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
1702 SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify) ||
1703 SkOSMenu::FindListIndex(evt, "Transition-Next", &fTransitionNext) ||
1704 SkOSMenu::FindListIndex(evt, "Transition-Prev", &fTransitionPrev)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001705 this->inval(NULL);
1706 this->updateTitle();
1707 return true;
1708 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001709 if (SkOSMenu::FindSwitchState(evt, "Flip X", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001710 fFlipAxis ^= kFlipAxis_X;
reed@google.comf03bb562011-11-11 21:42:12 +00001711 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001712 return true;
1713 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001714 if (SkOSMenu::FindSwitchState(evt, "Flip Y", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001715 fFlipAxis ^= kFlipAxis_Y;
reed@google.comf03bb562011-11-11 21:42:12 +00001716 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001717 return true;
1718 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001719 if (SkOSMenu::FindAction(evt,"Save to PDF")) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001720 this->saveToPdf();
1721 return true;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001722 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001723 return this->INHERITED::onEvent(evt);
1724}
1725
reed@android.comf2b98d62010-12-20 18:26:13 +00001726bool SampleWindow::onQuery(SkEvent* query) {
1727 if (query->isType("get-slide-count")) {
1728 query->setFast32(fSamples.count());
1729 return true;
1730 }
1731 if (query->isType("get-slide-title")) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001732 SkView* view = (*fSamples[query->getFast32()])();
reed@android.comf2b98d62010-12-20 18:26:13 +00001733 SkEvent evt(gTitleEvtName);
1734 if (view->doQuery(&evt)) {
1735 query->setString("title", evt.findString(gTitleEvtName));
1736 }
1737 SkSafeUnref(view);
1738 return true;
1739 }
1740 if (query->isType("use-fast-text")) {
1741 SkEvent evt(gFastTextEvtName);
1742 return curr_view(this)->doQuery(&evt);
1743 }
reed@google.com29038ed2011-07-06 17:56:47 +00001744 if (query->isType("ignore-window-bitmap")) {
1745 query->setFast32(this->getGrContext() != NULL);
1746 return true;
1747 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001748 return this->INHERITED::onQuery(query);
1749}
1750
caryclark@google.com02939ce2012-06-06 12:09:51 +00001751#if 0 // UNUSED
reed@android.com0ae6b242008-12-23 16:49:54 +00001752static void cleanup_for_filename(SkString* name) {
1753 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001754 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001755 switch (str[i]) {
1756 case ':': str[i] = '-'; break;
1757 case '/': str[i] = '-'; break;
1758 case ' ': str[i] = '_'; break;
1759 default: break;
1760 }
1761 }
1762}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001763#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001764
reed@google.coma4f81372012-11-15 15:56:38 +00001765//extern bool gIgnoreFastBlurRect;
1766
reed@android.com8a1c16f2008-12-17 15:59:43 +00001767bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001768 {
1769 SkView* view = curr_view(this);
1770 if (view) {
1771 SkEvent evt(gCharEvtName);
1772 evt.setFast32(uni);
1773 if (view->doQuery(&evt)) {
1774 return true;
1775 }
1776 }
1777 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001778
reed@android.com8a1c16f2008-12-17 15:59:43 +00001779 int dx = 0xFF;
1780 int dy = 0xFF;
1781
1782 switch (uni) {
1783 case '5': dx = 0; dy = 0; break;
1784 case '8': dx = 0; dy = -1; break;
1785 case '6': dx = 1; dy = 0; break;
1786 case '2': dx = 0; dy = 1; break;
1787 case '4': dx = -1; dy = 0; break;
1788 case '7': dx = -1; dy = -1; break;
1789 case '9': dx = 1; dy = -1; break;
1790 case '3': dx = 1; dy = 1; break;
1791 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001792
reed@android.com8a1c16f2008-12-17 15:59:43 +00001793 default:
1794 break;
1795 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001796
reed@android.com8a1c16f2008-12-17 15:59:43 +00001797 if (0xFF != dx && 0xFF != dy) {
1798 if ((dx | dy) == 0) {
1799 fScrollTestX = fScrollTestY = 0;
1800 } else {
1801 fScrollTestX += dx;
1802 fScrollTestY += dy;
1803 }
1804 this->inval(NULL);
1805 return true;
1806 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001807
reed@android.com0ae6b242008-12-23 16:49:54 +00001808 switch (uni) {
djsollen@google.com796763e2012-12-10 14:12:55 +00001809 case 'b':
1810 {
1811 postEventToSink(SkNEW_ARGS(SkEvent, ("PictFileView::toggleBBox")), curr_view(this));
1812 this->updateTitle();
1813 this->inval(NULL);
1814 break;
1815 }
reed@google.coma4f81372012-11-15 15:56:38 +00001816 case 'B':
1817// gIgnoreFastBlurRect = !gIgnoreFastBlurRect;
1818 this->inval(NULL);
1819 break;
1820
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001821 case 'f':
chudy@google.com4605a3f2012-08-01 17:58:01 +00001822 // only
djsollen@google.com6ff82552011-11-07 15:43:57 +00001823 toggleFPS();
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001824 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001825 case 'g':
1826 fRequestGrabImage = true;
1827 this->inval(NULL);
1828 break;
reed@google.coma4f81372012-11-15 15:56:38 +00001829 case 'G':
1830 gShowGMBounds = !gShowGMBounds;
1831 postEventToSink(GMSampleView::NewShowSizeEvt(gShowGMBounds),
1832 curr_view(this));
1833 this->inval(NULL);
1834 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001835 case 'i':
1836 this->zoomIn();
1837 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001838 case 'o':
1839 this->zoomOut();
1840 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001841 case 'r':
1842 fRotate = !fRotate;
bungeman@google.comb1785912013-07-09 21:58:56 +00001843 fRotateAnimTime = 0;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001844 this->inval(NULL);
1845 this->updateTitle();
1846 return true;
bsalomon@google.come8f09102011-09-08 18:48:12 +00001847 case 'k':
1848 fPerspAnim = !fPerspAnim;
1849 this->inval(NULL);
1850 this->updateTitle();
1851 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001852#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00001853 case '\\':
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001854 this->setDeviceType(kNullGPU_DeviceType);
1855 this->inval(NULL);
1856 this->updateTitle();
bsalomon@google.com74913722011-10-27 20:44:19 +00001857 return true;
twiz@google.com05e70242012-01-27 19:12:00 +00001858 case 'p':
1859 {
1860 GrContext* grContext = this->getGrContext();
1861 if (grContext) {
1862 size_t cacheBytes = grContext->getGpuTextureCacheBytes();
1863 grContext->freeGpuResources();
1864 SkDebugf("Purged %d bytes from the GPU resource cache.\n",
1865 cacheBytes);
1866 }
1867 }
1868 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001869#endif
reed@android.com0ae6b242008-12-23 16:49:54 +00001870 default:
1871 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001872 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001873
scroggo@google.com7dadc742012-04-18 14:07:57 +00001874 if (fAppMenu->handleKeyEquivalent(uni)|| fSlideMenu->handleKeyEquivalent(uni)) {
1875 this->onUpdateMenu(fAppMenu);
1876 this->onUpdateMenu(fSlideMenu);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001877 return true;
1878 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001879 return this->INHERITED::onHandleChar(uni);
1880}
1881
yangsu@google.com921091f2011-08-02 13:39:12 +00001882void SampleWindow::setDeviceType(DeviceType type) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001883 if (type == fDeviceType)
1884 return;
1885
1886 fDevManager->tearDownBackend(this);
1887
1888 fDeviceType = type;
1889
bsalomon@google.com11959252012-04-06 20:13:38 +00001890 fDevManager->setUpBackend(this, fMSAASampleCount);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001891
yangsu@google.com921091f2011-08-02 13:39:12 +00001892 this->updateTitle();
1893 this->inval(NULL);
1894}
1895
Scroggo2c8208f2011-06-15 16:49:08 +00001896void SampleWindow::toggleSlideshow() {
1897 fAnimating = !fAnimating;
1898 this->postAnimatingEvent();
1899 this->updateTitle();
1900}
1901
1902void SampleWindow::toggleRendering() {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001903 this->setDeviceType(cycle_devicetype(fDeviceType));
Scroggo2c8208f2011-06-15 16:49:08 +00001904 this->updateTitle();
1905 this->inval(NULL);
1906}
1907
djsollen@google.com6ff82552011-11-07 15:43:57 +00001908void SampleWindow::toggleFPS() {
1909 fMeasureFPS = !fMeasureFPS;
1910 this->updateTitle();
1911 this->inval(NULL);
1912}
1913
reed@android.com8a1c16f2008-12-17 15:59:43 +00001914#include "SkDumpCanvas.h"
1915
1916bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001917 {
1918 SkView* view = curr_view(this);
1919 if (view) {
1920 SkEvent evt(gKeyEvtName);
1921 evt.setFast32(key);
1922 if (view->doQuery(&evt)) {
1923 return true;
1924 }
1925 }
1926 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001927 switch (key) {
1928 case kRight_SkKey:
1929 if (this->nextSample()) {
1930 return true;
1931 }
1932 break;
1933 case kLeft_SkKey:
scroggo@google.come2dd9732012-08-15 20:03:06 +00001934 if (this->previousSample()) {
1935 return true;
1936 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001937 return true;
1938 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001939 if (USE_ARROWS_FOR_ZOOM) {
bungeman@google.comcefd9812013-05-15 15:07:32 +00001940 this->changeZoomLevel(1.f / 32.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001941 } else {
1942 fNClip = !fNClip;
1943 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001944 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001945 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001946 return true;
1947 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001948 if (USE_ARROWS_FOR_ZOOM) {
bungeman@google.comcefd9812013-05-15 15:07:32 +00001949 this->changeZoomLevel(-1.f / 32.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001950 } else {
1951 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001952 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001953 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001954 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001955 case kOK_SkKey: {
1956 SkString title;
1957 if (curr_title(this, &title)) {
1958 writeTitleToPrefs(title.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001959 }
1960 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001961 }
reed@android.com34245c72009-11-03 04:00:48 +00001962 case kBack_SkKey:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001963 this->showOverview();
reed@android.com34245c72009-11-03 04:00:48 +00001964 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001965 default:
1966 break;
1967 }
1968 return this->INHERITED::onHandleKey(key);
1969}
1970
reed@google.com52f57e12011-03-16 12:10:02 +00001971///////////////////////////////////////////////////////////////////////////////
1972
1973static const char gGestureClickType[] = "GestureClickType";
1974
Scroggod3aed392011-06-22 13:26:56 +00001975bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
reed@google.com4d5c26d2013-01-08 16:17:50 +00001976 void* owner, unsigned modi) {
Scroggo0f185c22011-03-24 18:35:50 +00001977 if (Click::kMoved_State == state) {
1978 updatePointer(x, y);
1979 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001980 int w = SkScalarRound(this->width());
1981 int h = SkScalarRound(this->height());
1982
1983 // check for the resize-box
1984 if (w - x < 16 && h - y < 16) {
1985 return false; // let the OS handle the click
chudy@google.com4605a3f2012-08-01 17:58:01 +00001986 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001987 else if (fMagnify) {
1988 //it's only necessary to update the drawing if there's a click
1989 this->inval(NULL);
1990 return false; //prevent dragging while magnify is enabled
reed@google.com4d5c26d2013-01-08 16:17:50 +00001991 } else {
1992 // capture control+option, and trigger debugger
1993 if ((modi & kControl_SkModifierKey) && (modi & kOption_SkModifierKey)) {
1994 if (Click::kDown_State == state) {
1995 SkEvent evt("debug-hit-test");
1996 evt.setS32("debug-hit-test-x", x);
1997 evt.setS32("debug-hit-test-y", y);
1998 curr_view(this)->doEvent(evt);
1999 }
2000 return true;
2001 } else {
2002 return this->INHERITED::onDispatchClick(x, y, state, owner, modi);
2003 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002004 }
2005}
2006
reed@google.com52f57e12011-03-16 12:10:02 +00002007class GestureClick : public SkView::Click {
2008public:
2009 GestureClick(SkView* target) : SkView::Click(target) {
2010 this->setType(gGestureClickType);
2011 }
2012
2013 static bool IsGesture(Click* click) {
2014 return click->isType(gGestureClickType);
2015 }
2016};
2017
reed@google.com4d5c26d2013-01-08 16:17:50 +00002018SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y,
2019 unsigned modi) {
reed@google.com52f57e12011-03-16 12:10:02 +00002020 return new GestureClick(this);
2021}
2022
2023bool SampleWindow::onClick(Click* click) {
2024 if (GestureClick::IsGesture(click)) {
bsalomon@google.com4d4f2812011-12-12 18:34:01 +00002025 float x = static_cast<float>(click->fICurr.fX);
2026 float y = static_cast<float>(click->fICurr.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00002027
reed@google.com52f57e12011-03-16 12:10:02 +00002028 switch (click->fState) {
2029 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00002030 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00002031 break;
2032 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00002033 fGesture.touchMoved(click->fOwner, x, y);
reed@google.comf03bb562011-11-11 21:42:12 +00002034 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002035 break;
2036 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00002037 fGesture.touchEnd(click->fOwner);
reed@google.comf03bb562011-11-11 21:42:12 +00002038 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002039 break;
2040 }
2041 return true;
2042 }
2043 return false;
2044}
2045
2046///////////////////////////////////////////////////////////////////////////////
2047
reed@android.com8a1c16f2008-12-17 15:59:43 +00002048void SampleWindow::loadView(SkView* view) {
2049 SkView::F2BIter iter(this);
2050 SkView* prev = iter.next();
2051 if (prev) {
2052 prev->detachFromParent();
2053 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002054
reed@android.com8a1c16f2008-12-17 15:59:43 +00002055 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00002056 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002057 this->attachChildToFront(view)->unref();
2058 view->setSize(this->width(), this->height());
2059
yangsu@google.com921091f2011-08-02 13:39:12 +00002060 //repopulate the slide menu when a view is loaded
scroggo@google.com7dadc742012-04-18 14:07:57 +00002061 fSlideMenu->reset();
djsollen@google.com388974f2013-01-17 13:20:01 +00002062
scroggo@google.comb073d922012-06-08 15:35:03 +00002063 (void)SampleView::SetUsePipe(view, fPipeState);
yangsu@google.com921091f2011-08-02 13:39:12 +00002064 if (SampleView::IsSampleView(view))
scroggo@google.com7dadc742012-04-18 14:07:57 +00002065 ((SampleView*)view)->requestMenu(fSlideMenu);
2066 this->onUpdateMenu(fSlideMenu);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002067 this->updateTitle();
2068}
2069
2070static const char* gConfigNames[] = {
2071 "unknown config",
2072 "A1",
2073 "A8",
2074 "Index8",
2075 "565",
2076 "4444",
2077 "8888"
2078};
2079
2080static const char* configToString(SkBitmap::Config c) {
2081 return gConfigNames[c];
2082}
2083
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002084static const char* gDeviceTypePrefix[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002085 "raster: ",
2086 "picture: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002087#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00002088 "opengl: ",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00002089#if SK_ANGLE
2090 "angle: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002091#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +00002092 "null-gl: "
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002093#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +00002094};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002095SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gDeviceTypePrefix) == SampleWindow::kDeviceTypeCnt,
2096 array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002097
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002098static const char* trystate_str(SkOSMenu::TriState state,
reed@google.com569e0432011-04-05 13:07:03 +00002099 const char trueStr[], const char falseStr[]) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002100 if (SkOSMenu::kOnState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002101 return trueStr;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002102 } else if (SkOSMenu::kOffState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002103 return falseStr;
2104 }
2105 return NULL;
2106}
2107
reed@android.com8a1c16f2008-12-17 15:59:43 +00002108void SampleWindow::updateTitle() {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002109 SkView* view = curr_view(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002110
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002111 SkString title;
2112 if (!curr_title(this, &title)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002113 title.set("<unknown>");
2114 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002115
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002116 title.prepend(gDeviceTypePrefix[fDeviceType]);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002117
2118 title.prepend(" ");
2119 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002120
reed@android.com8a1c16f2008-12-17 15:59:43 +00002121 if (fAnimating) {
2122 title.prepend("<A> ");
2123 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00002124 if (fRotate) {
2125 title.prepend("<R> ");
2126 }
reed@android.come522ca52009-11-23 20:10:41 +00002127 if (fNClip) {
2128 title.prepend("<C> ");
2129 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00002130 if (fPerspAnim) {
2131 title.prepend("<K> ");
2132 }
reed@google.com569e0432011-04-05 13:07:03 +00002133
2134 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
2135 title.prepend(trystate_str(fAAState, "AA ", "aa "));
bungeman@google.com96aabc82013-06-03 21:26:34 +00002136 title.prepend(trystate_str(fFilterState, "N ", "n "));
2137 title.prepend(trystate_str(fSubpixelState, "S ", "s "));
reed@google.com569e0432011-04-05 13:07:03 +00002138 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
2139 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002140 title.prepend(gHintingStates[fHintingState].label);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002141
2142 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00002143 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002144 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002145
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002146 if (fMeasureFPS) {
reed@google.com43e10142012-09-10 11:52:52 +00002147 title.appendf(" %8.3f ms", fMeasureFPS_Time / (float)FPS_REPEAT_COUNT);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002148 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002149 if (SampleView::IsSampleView(view)) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002150 switch (fPipeState) {
2151 case SkOSMenu::kOnState:
2152 title.prepend("<Pipe> ");
2153 break;
2154 case SkOSMenu::kMixedState:
2155 title.prepend("<Tiled Pipe> ");
2156 break;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002157
scroggo@google.comb073d922012-06-08 15:35:03 +00002158 default:
2159 break;
2160 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002161 title.prepend("! ");
2162 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002163
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002164#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00002165 if (IsGpuDeviceType(fDeviceType) &&
robertphillips@google.com4750fa52012-04-10 13:34:11 +00002166 NULL != fDevManager &&
bsalomon@google.com82502e22013-01-24 20:47:18 +00002167 fDevManager->getGrRenderTarget() &&
bsalomon@google.com11959252012-04-06 20:13:38 +00002168 fDevManager->getGrRenderTarget()->numSamples() > 0) {
2169 title.appendf(" [MSAA: %d]",
2170 fDevManager->getGrRenderTarget()->numSamples());
2171 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002172#endif
bsalomon@google.com11959252012-04-06 20:13:38 +00002173
reed@android.com8a1c16f2008-12-17 15:59:43 +00002174 this->setTitle(title.c_str());
2175}
2176
2177void SampleWindow::onSizeChange() {
2178 this->INHERITED::onSizeChange();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002179
reed@android.com8a1c16f2008-12-17 15:59:43 +00002180 SkView::F2BIter iter(this);
2181 SkView* view = iter.next();
2182 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002183
reed@android.com8a1c16f2008-12-17 15:59:43 +00002184 // rebuild our clippath
2185 {
2186 const SkScalar W = this->width();
2187 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002188
reed@android.com8a1c16f2008-12-17 15:59:43 +00002189 fClipPath.reset();
2190#if 0
2191 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
2192 SkRect r;
2193 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
2194 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
2195 fClipPath.addRect(r);
2196 }
2197#else
2198 SkRect r;
2199 r.set(0, 0, W, H);
2200 fClipPath.addRect(r, SkPath::kCCW_Direction);
2201 r.set(W/4, H/4, W*3/4, H*3/4);
2202 fClipPath.addRect(r, SkPath::kCW_Direction);
2203#endif
2204 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002205
Scroggo2c8208f2011-06-15 16:49:08 +00002206 fZoomCenterX = SkScalarHalf(this->width());
2207 fZoomCenterY = SkScalarHalf(this->height());
2208
djsollen@google.com56c69772011-11-08 19:00:26 +00002209#ifdef SK_BUILD_FOR_ANDROID
Scroggob4490c72011-06-17 13:53:05 +00002210 // FIXME: The first draw after a size change does not work on Android, so
2211 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00002212 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00002213#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00002214 this->updateTitle(); // to refresh our config
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002215 fDevManager->windowSizeChanged(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002216}
2217
2218///////////////////////////////////////////////////////////////////////////////
2219
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002220static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002221static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00002222static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002223
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002224bool SampleView::IsSampleView(SkView* view) {
2225 SkEvent evt(is_sample_view_tag);
2226 return view->doQuery(&evt);
2227}
2228
reed@google.comf2183392011-04-22 14:10:48 +00002229bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002230 SkEvent evt(repeat_count_tag);
2231 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00002232 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002233}
2234
scroggo@google.comb073d922012-06-08 15:35:03 +00002235bool SampleView::SetUsePipe(SkView* view, SkOSMenu::TriState state) {
2236 SkEvent evt;
2237 evt.setS32(set_use_pipe_tag, state);
reed@google.com0faac1e2011-05-11 05:58:58 +00002238 return view->doEvent(evt);
2239}
2240
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002241bool SampleView::onEvent(const SkEvent& evt) {
2242 if (evt.isType(repeat_count_tag)) {
2243 fRepeatCount = evt.getFast32();
2244 return true;
2245 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002246
scroggo@google.comb073d922012-06-08 15:35:03 +00002247 int32_t pipeHolder;
2248 if (evt.findS32(set_use_pipe_tag, &pipeHolder)) {
2249 fPipeState = static_cast<SkOSMenu::TriState>(pipeHolder);
reed@google.com0faac1e2011-05-11 05:58:58 +00002250 return true;
2251 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002252
2253 if (evt.isType("debug-hit-test")) {
2254 fDebugHitTest = true;
2255 evt.findS32("debug-hit-test-x", &fDebugHitTestLoc.fX);
2256 evt.findS32("debug-hit-test-y", &fDebugHitTestLoc.fY);
2257 this->inval(NULL);
2258 return true;
2259 }
2260
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002261 return this->INHERITED::onEvent(evt);
2262}
2263
2264bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002265 if (evt->isType(is_sample_view_tag)) {
2266 return true;
2267 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002268 return this->INHERITED::onQuery(evt);
2269}
2270
reed@google.com64e3eb22011-05-04 14:32:04 +00002271
2272class SimplePC : public SkGPipeController {
2273public:
2274 SimplePC(SkCanvas* target);
2275 ~SimplePC();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002276
reed@google.com64e3eb22011-05-04 14:32:04 +00002277 virtual void* requestBlock(size_t minRequest, size_t* actual);
2278 virtual void notifyWritten(size_t bytes);
2279
2280private:
reed@google.com961ddb02011-05-05 14:03:48 +00002281 SkGPipeReader fReader;
2282 void* fBlock;
2283 size_t fBlockSize;
2284 size_t fBytesWritten;
2285 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00002286 SkGPipeReader::Status fStatus;
2287
2288 size_t fTotalWritten;
2289};
2290
2291SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
2292 fBlock = NULL;
2293 fBlockSize = fBytesWritten = 0;
2294 fStatus = SkGPipeReader::kDone_Status;
2295 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00002296 fAtomsWritten = 0;
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00002297 fReader.setBitmapDecoder(&SkImageDecoder::DecodeMemory);
reed@google.com64e3eb22011-05-04 14:32:04 +00002298}
2299
2300SimplePC::~SimplePC() {
2301// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
reed@google.com0faac1e2011-05-11 05:58:58 +00002302 if (fTotalWritten) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002303 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
2304 fAtomsWritten, fStatus);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002305#ifdef PIPE_FILE
scroggo@google.comb073d922012-06-08 15:35:03 +00002306 //File is open in append mode
2307 FILE* f = fopen(FILE_PATH, "ab");
2308 SkASSERT(f != NULL);
2309 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
2310 fclose(f);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002311#endif
2312#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00002313 if (fAtomsWritten > 1 && fTotalWritten > 4) { //ignore done
2314 gServer.acceptConnections();
2315 gServer.writePacket(fBlock, fTotalWritten);
2316 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002317#endif
reed@google.com0faac1e2011-05-11 05:58:58 +00002318 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002319 sk_free(fBlock);
reed@google.com64e3eb22011-05-04 14:32:04 +00002320}
2321
2322void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
2323 sk_free(fBlock);
2324
2325 fBlockSize = minRequest * 4;
2326 fBlock = sk_malloc_throw(fBlockSize);
2327 fBytesWritten = 0;
2328 *actual = fBlockSize;
2329 return fBlock;
2330}
2331
2332void SimplePC::notifyWritten(size_t bytes) {
2333 SkASSERT(fBytesWritten + bytes <= fBlockSize);
reed@google.com64e3eb22011-05-04 14:32:04 +00002334 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
2335 SkASSERT(SkGPipeReader::kError_Status != fStatus);
2336 fBytesWritten += bytes;
2337 fTotalWritten += bytes;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002338
reed@google.com961ddb02011-05-05 14:03:48 +00002339 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00002340}
2341
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002342void SampleView::draw(SkCanvas* canvas) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002343 if (SkOSMenu::kOffState == fPipeState) {
2344 this->INHERITED::draw(canvas);
2345 } else {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002346 SkGPipeWriter writer;
2347 SimplePC controller(canvas);
scroggo@google.comb073d922012-06-08 15:35:03 +00002348 TiledPipeController tc(canvas->getDevice()->accessBitmap(false),
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00002349 &SkImageDecoder::DecodeMemory,
scroggo@google.comb073d922012-06-08 15:35:03 +00002350 &canvas->getTotalMatrix());
2351 SkGPipeController* pc;
2352 if (SkOSMenu::kMixedState == fPipeState) {
2353 pc = &tc;
2354 } else {
2355 pc = &controller;
2356 }
reed@google.comdde09562011-05-23 12:21:05 +00002357 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002358
scroggo@google.comb073d922012-06-08 15:35:03 +00002359 canvas = writer.startRecording(pc, flags);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002360 //Must draw before controller goes out of scope and sends data
2361 this->INHERITED::draw(canvas);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00002362 //explicitly end recording to ensure writer is flushed before the memory
2363 //is freed in the deconstructor of the controller
2364 writer.endRecording();
reed@google.com0faac1e2011-05-11 05:58:58 +00002365 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002366}
reed@google.com4d5c26d2013-01-08 16:17:50 +00002367
2368#include "SkBounder.h"
2369
2370class DebugHitTestBounder : public SkBounder {
2371public:
2372 DebugHitTestBounder(int x, int y) {
2373 fLoc.set(x, y);
2374 }
2375
2376 virtual bool onIRect(const SkIRect& bounds) SK_OVERRIDE {
2377 if (bounds.contains(fLoc.x(), fLoc.y())) {
2378 //
2379 // Set a break-point here to see what was being drawn under
2380 // the click point (just needed a line of code to stop the debugger)
2381 //
2382 bounds.centerX();
2383 }
2384 return true;
2385 }
2386
2387private:
2388 SkIPoint fLoc;
2389 typedef SkBounder INHERITED;
2390};
2391
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002392void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com81e3d7f2011-06-01 12:42:36 +00002393 this->onDrawBackground(canvas);
2394
reed@google.com4d5c26d2013-01-08 16:17:50 +00002395 DebugHitTestBounder bounder(fDebugHitTestLoc.x(), fDebugHitTestLoc.y());
2396 if (fDebugHitTest) {
2397 canvas->setBounder(&bounder);
2398 }
2399
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002400 for (int i = 0; i < fRepeatCount; i++) {
scroggo@google.com85cade02012-08-17 13:29:50 +00002401 SkAutoCanvasRestore acr(canvas, true);
2402 this->onDrawContent(canvas);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002403 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002404
2405 fDebugHitTest = false;
2406 canvas->setBounder(NULL);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002407}
2408
2409void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00002410 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002411}
2412
2413///////////////////////////////////////////////////////////////////////////////
2414
reed@android.comf2b98d62010-12-20 18:26:13 +00002415template <typename T> void SkTBSort(T array[], int count) {
2416 for (int i = 1; i < count - 1; i++) {
2417 bool didSwap = false;
2418 for (int j = count - 1; j > i; --j) {
2419 if (array[j] < array[j-1]) {
2420 T tmp(array[j-1]);
2421 array[j-1] = array[j];
2422 array[j] = tmp;
2423 didSwap = true;
2424 }
2425 }
2426 if (!didSwap) {
2427 break;
2428 }
2429 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002430
reed@android.comf2b98d62010-12-20 18:26:13 +00002431 for (int k = 0; k < count - 1; k++) {
2432 SkASSERT(!(array[k+1] < array[k]));
2433 }
2434}
2435
2436#include "SkRandom.h"
2437
2438static void rand_rect(SkIRect* rect, SkRandom& rand) {
2439 int bits = 8;
2440 int shift = 32 - bits;
2441 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
2442 rand.nextU() >> shift, rand.nextU() >> shift);
2443 rect->sort();
2444}
2445
2446static void dumpRect(const SkIRect& r) {
2447 SkDebugf(" { %d, %d, %d, %d },\n",
2448 r.fLeft, r.fTop,
2449 r.fRight, r.fBottom);
2450}
2451
2452static void test_rects(const SkIRect rect[], int count) {
2453 SkRegion rgn0, rgn1;
2454
2455 for (int i = 0; i < count; i++) {
2456 rgn0.op(rect[i], SkRegion::kUnion_Op);
2457 // dumpRect(rect[i]);
2458 }
2459 rgn1.setRects(rect, count);
2460
2461 if (rgn0 != rgn1) {
2462 SkDebugf("\n");
2463 for (int i = 0; i < count; i++) {
2464 dumpRect(rect[i]);
2465 }
2466 SkDebugf("\n");
2467 }
2468}
2469
2470static void test() {
2471 size_t i;
2472
2473 const SkIRect r0[] = {
2474 { 0, 0, 1, 1 },
2475 { 2, 2, 3, 3 },
2476 };
2477 const SkIRect r1[] = {
2478 { 0, 0, 1, 3 },
2479 { 1, 1, 2, 2 },
2480 { 2, 0, 3, 3 },
2481 };
2482 const SkIRect r2[] = {
2483 { 0, 0, 1, 2 },
2484 { 2, 1, 3, 3 },
2485 { 4, 0, 5, 1 },
2486 { 6, 0, 7, 4 },
2487 };
2488
2489 static const struct {
2490 const SkIRect* fRects;
2491 int fCount;
2492 } gRecs[] = {
2493 { r0, SK_ARRAY_COUNT(r0) },
2494 { r1, SK_ARRAY_COUNT(r1) },
2495 { r2, SK_ARRAY_COUNT(r2) },
2496 };
2497
2498 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
2499 test_rects(gRecs[i].fRects, gRecs[i].fCount);
2500 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002501
reed@android.comf2b98d62010-12-20 18:26:13 +00002502 SkRandom rand;
2503 for (i = 0; i < 10000; i++) {
2504 SkRegion rgn0, rgn1;
2505
2506 const int N = 8;
2507 SkIRect rect[N];
2508 for (int j = 0; j < N; j++) {
2509 rand_rect(&rect[j], rand);
2510 }
2511 test_rects(rect, N);
2512 }
2513}
2514
caryclark@google.com02939ce2012-06-06 12:09:51 +00002515// FIXME: this should be in a header
2516SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00002517SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
caryclark@google.com02939ce2012-06-06 12:09:51 +00002518 if (false) { // avoid bit rot, suppress warning
2519 test();
2520 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002521 return new SampleWindow(hwnd, argc, argv, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002522}
2523
caryclark@google.com02939ce2012-06-06 12:09:51 +00002524// FIXME: this should be in a header
2525void get_preferred_size(int* x, int* y, int* width, int* height);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002526void get_preferred_size(int* x, int* y, int* width, int* height) {
2527 *x = 10;
2528 *y = 50;
2529 *width = 640;
2530 *height = 480;
2531}
2532
caryclark@google.com5987f582012-10-02 18:33:14 +00002533#ifdef SK_BUILD_FOR_IOS
2534void save_args(int argc, char *argv[]) {
2535}
2536#endif
2537
caryclark@google.com02939ce2012-06-06 12:09:51 +00002538// FIXME: this should be in a header
2539void application_init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002540void application_init() {
2541// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002542#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00002543 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002544#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002545 SkGraphics::Init();
2546 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002547}
2548
caryclark@google.com02939ce2012-06-06 12:09:51 +00002549// FIXME: this should be in a header
2550void application_term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002551void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002552 SkEvent::Term();
2553 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002554}