blob: bb22685e249a119d514402e615a96bb34e916b0e [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.orgb1b7f702012-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
bsalomon@google.com82d12232013-09-09 15:36:26 +000087#define DEFAULT_TO_GPU 0 // if 1 default rendering is on GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000088#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) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000272 SkAutoTUnref<SkBaseDevice> device(new SkGpuDevice(fCurContext, fCurRenderTarget));
bsalomon@google.com82502e22013-01-24 20:47:18 +0000273 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) {
reed@google.com44699382013-10-31 17:28:30 +0000508 paint->setFilterLevel(SkOSMenu::kOnState == fFilterState ?
509 SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
reed@google.com176753a2011-05-17 15:32:04 +0000510 }
bungeman@google.com96aabc82013-06-03 21:26:34 +0000511 if (SkOSMenu::kMixedState != fSubpixelState) {
512 paint->setSubpixelText(SkOSMenu::kOnState == fSubpixelState);
513 }
bungeman@google.com055aa522013-06-03 21:35:03 +0000514 if (0 != fHintingState && fHintingState < (int)SK_ARRAY_COUNT(gHintingStates)) {
bungeman@google.com96aabc82013-06-03 21:26:34 +0000515 paint->setHinting(gHintingStates[fHintingState].hinting);
reed@google.com09e3baa2011-05-18 12:04:31 +0000516 }
reed@google.com971aca72012-11-26 20:26:54 +0000517 return true;
reed@google.comf0b5f682011-03-11 20:08:25 +0000518 }
519
520private:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000521 SkOSMenu::TriState fLCDState;
522 SkOSMenu::TriState fAAState;
523 SkOSMenu::TriState fFilterState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000524 SkOSMenu::TriState fSubpixelState;
525 int fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000526};
527
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528//////////////////////////////////////////////////////////////////////////////
529
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000530#define MAX_ZOOM_LEVEL 8
531#define MIN_ZOOM_LEVEL -8
532
reed@android.comf2b98d62010-12-20 18:26:13 +0000533static const char gCharEvtName[] = "SampleCode_Char_Event";
534static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000535static const char gTitleEvtName[] = "SampleCode_Title_Event";
536static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000537static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
reed@google.com2072db82011-11-08 15:18:10 +0000538static const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle";
reed@android.comf2b98d62010-12-20 18:26:13 +0000539
540bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
541 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
542 if (outUni) {
543 *outUni = evt.getFast32();
544 }
545 return true;
546 }
547 return false;
548}
549
550bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
551 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
552 if (outKey) {
553 *outKey = (SkKey)evt.getFast32();
554 }
555 return true;
556 }
557 return false;
558}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000559
560bool SampleCode::TitleQ(const SkEvent& evt) {
561 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
562}
563
564void SampleCode::TitleR(SkEvent* evt, const char title[]) {
565 SkASSERT(evt && TitleQ(*evt));
566 evt->setString(gTitleEvtName, title);
567}
568
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000569bool SampleCode::RequestTitle(SkView* view, SkString* title) {
570 SkEvent evt(gTitleEvtName);
571 if (view->doQuery(&evt)) {
572 title->set(evt.findString(gTitleEvtName));
573 return true;
574 }
575 return false;
576}
577
reed@android.com8a1c16f2008-12-17 15:59:43 +0000578bool SampleCode::PrefSizeQ(const SkEvent& evt) {
579 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
580}
581
582void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
583 SkASSERT(evt && PrefSizeQ(*evt));
584 SkScalar size[2];
585 size[0] = width;
586 size[1] = height;
587 evt->setScalars(gPrefSizeEvtName, 2, size);
588}
589
reed@android.comf2b98d62010-12-20 18:26:13 +0000590bool SampleCode::FastTextQ(const SkEvent& evt) {
591 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
592}
593
594///////////////////////////////////////////////////////////////////////////////
595
reed@android.com44177402009-11-23 21:07:51 +0000596static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000597static SkMSec gAnimTimePrev;
598
reed@android.com44177402009-11-23 21:07:51 +0000599SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000600SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
601SkScalar SampleCode::GetAnimSecondsDelta() {
602 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
603}
reed@android.com44177402009-11-23 21:07:51 +0000604
605SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000606 // since gAnimTime can be up to 32 bits, we can't convert it to a float
607 // or we'll lose the low bits. Hence we use doubles for the intermediate
608 // calculations
609 double seconds = (double)gAnimTime / 1000.0;
610 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000611 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000612 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000613 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000614 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000615}
616
bsalomon@google.com85003222012-03-28 14:44:37 +0000617SkScalar SampleCode::GetAnimSinScalar(SkScalar amplitude,
618 SkScalar periodInSec,
619 SkScalar phaseInSec) {
620 if (!periodInSec) {
621 return 0;
622 }
623 double t = (double)gAnimTime / 1000.0 + phaseInSec;
624 t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
625 amplitude = SK_ScalarHalf * amplitude;
626 return SkScalarMul(amplitude, SkDoubleToScalar(sin(t))) + amplitude;
627}
628
reed@google.com3cec4d72011-07-06 13:59:47 +0000629GrContext* SampleCode::GetGr() {
630 return gSampleWindow ? gSampleWindow->getGrContext() : NULL;
631}
632
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000633// some GMs rely on having a skiagm::GetGr function defined
634namespace skiagm {
caryclark@google.com02939ce2012-06-06 12:09:51 +0000635 // FIXME: this should be moved into a header
636 GrContext* GetGr();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000637 GrContext* GetGr() { return SampleCode::GetGr(); }
638}
639
reed@android.com8a1c16f2008-12-17 15:59:43 +0000640//////////////////////////////////////////////////////////////////////////////
641
reed@android.comf2b98d62010-12-20 18:26:13 +0000642static SkView* curr_view(SkWindow* wind) {
643 SkView::F2BIter iter(wind);
644 return iter.next();
645}
646
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000647static bool curr_title(SkWindow* wind, SkString* title) {
648 SkView* view = curr_view(wind);
649 if (view) {
650 SkEvent evt(gTitleEvtName);
651 if (view->doQuery(&evt)) {
652 title->set(evt.findString(gTitleEvtName));
653 return true;
654 }
655 }
656 return false;
657}
658
Scroggo2c8208f2011-06-15 16:49:08 +0000659void SampleWindow::setZoomCenter(float x, float y)
660{
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000661 fZoomCenterX = x;
662 fZoomCenterY = y;
Scroggo2c8208f2011-06-15 16:49:08 +0000663}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000664
Scroggo0f185c22011-03-24 18:35:50 +0000665bool SampleWindow::zoomIn()
666{
667 // Arbitrarily decided
668 if (fFatBitsScale == 25) return false;
669 fFatBitsScale++;
670 this->inval(NULL);
671 return true;
672}
673
674bool SampleWindow::zoomOut()
675{
676 if (fFatBitsScale == 1) return false;
677 fFatBitsScale--;
678 this->inval(NULL);
679 return true;
680}
681
Scroggo0f185c22011-03-24 18:35:50 +0000682void SampleWindow::updatePointer(int x, int y)
683{
684 fMouseX = x;
685 fMouseY = y;
686 if (fShowZoomer) {
687 this->inval(NULL);
688 }
689}
690
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000691static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
692 static const SampleWindow::DeviceType gCT[] = {
693 SampleWindow::kPicture_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000694#if SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000695 SampleWindow::kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000696#if SK_ANGLE
697 SampleWindow::kANGLE_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000698#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +0000699 SampleWindow::kRaster_DeviceType, // skip the null gpu device in normal cycling
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000700#endif // SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000701 SampleWindow::kRaster_DeviceType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000702 };
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000703 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gCT) == SampleWindow::kDeviceTypeCnt, array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000704 return gCT[ct];
705}
706
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000707static void usage(const char * argv0) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000708 SkDebugf("%s [--slide sampleName] [-i resourcePath] [--msaa sampleCount] [--pictureDir dirPath] [--picture path] [--sort]\n", argv0);
709#ifdef SAMPLE_PDF_FILE_VIEWER
710 SkDebugf(" [--pdfDir pdfPath]\n");
711 SkDebugf(" pdfPath: path to directory pdf files are read from\n");
712#endif // SAMPLE_PDF_FILE_VIEWER
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000713 SkDebugf(" sampleName: sample at which to start.\n");
714 SkDebugf(" resourcePath: directory that stores image resources.\n");
bsalomon@google.com11959252012-04-06 20:13:38 +0000715 SkDebugf(" msaa: request multisampling with the given sample count.\n");
chudy@google.com4605a3f2012-08-01 17:58:01 +0000716 SkDebugf(" dirPath: path to directory skia pictures are read from\n");
717 SkDebugf(" path: path to skia picture\n");
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000718 SkDebugf(" --sort: sort samples by title, this would help to compare pdf rendering (P:foo.pdf) with skp rendering (P:foo.pdf)\n");
719}
720
721static SkString getSampleTitle(const SkViewFactory* sampleFactory) {
722 SkView* view = (*sampleFactory)();
723 SkString title;
724 SampleCode::RequestTitle(view, &title);
725 view->unref();
726 return title;
727}
728
edisonn@google.comde36b692013-07-11 15:40:31 +0000729static bool compareSampleTitle(const SkViewFactory* first, const SkViewFactory* second) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000730 return strcmp(getSampleTitle(first).c_str(), getSampleTitle(second).c_str()) < 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000731}
732
chudy@google.com4605a3f2012-08-01 17:58:01 +0000733SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager)
robertphillips@google.com4750fa52012-04-10 13:34:11 +0000734 : INHERITED(hwnd)
735 , fDevManager(NULL) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000736
reed@google.com7d05ac92013-06-06 13:14:13 +0000737 fCurrIndex = -1;
738
reed@google.com1830c7a2012-06-04 12:05:43 +0000739 this->registerPictFileSamples(argv, argc);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000740 this->registerPictFileSample(argv, argc);
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000741#ifdef SAMPLE_PDF_FILE_VIEWER
742 this->registerPdfFileViewerSamples(argv, argc);
743#endif // SAMPLE_PDF_FILE_VIEWER
robertphillips@google.com76d40212012-03-22 14:12:15 +0000744 SkGMRegistyToSampleRegistry();
745 {
746 const SkViewRegister* reg = SkViewRegister::Head();
747 while (reg) {
748 *fSamples.append() = reg->factory();
749 reg = reg->next();
750 }
751 }
752
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000753 bool sort = false;
754 for (int i = 0; i < argc; ++i) {
755 if (!strcmp(argv[i], "--sort")) {
756 sort = true;
757 break;
758 }
759 }
760
761 if (sort) {
762 // Sort samples, so foo.skp and foo.pdf are consecutive and we can quickly spot where
763 // skp -> pdf -> png fails.
764 SkTQSort(fSamples.begin(), fSamples.end() ? fSamples.end() - 1 : NULL, compareSampleTitle);
765 }
766
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000767 const char* resourcePath = NULL;
bsalomon@google.com11959252012-04-06 20:13:38 +0000768 fMSAASampleCount = 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000769
770 const char* const commandName = argv[0];
771 char* const* stop = argv + argc;
772 for (++argv; argv < stop; ++argv) {
reed@google.com55ebe8e2013-09-10 19:12:07 +0000773 if (!strcmp(*argv, "-i") || !strcmp(*argv, "--resourcePath")) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000774 argv++;
775 if (argv < stop && **argv) {
776 resourcePath = *argv;
777 }
robertphillips@google.com76d40212012-03-22 14:12:15 +0000778 } else if (strcmp(*argv, "--slide") == 0) {
779 argv++;
780 if (argv < stop && **argv) {
781 fCurrIndex = findByTitle(*argv);
782 if (fCurrIndex < 0) {
783 fprintf(stderr, "Unknown sample \"%s\"\n", *argv);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000784 listTitles();
robertphillips@google.com76d40212012-03-22 14:12:15 +0000785 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000786 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000787 } else if (strcmp(*argv, "--msaa") == 0) {
788 ++argv;
789 if (argv < stop && **argv) {
790 fMSAASampleCount = atoi(*argv);
791 }
robertphillips@google.com7265e722012-05-03 18:22:28 +0000792 } else if (strcmp(*argv, "--list") == 0) {
793 listTitles();
halcanary@google.com805ca192013-10-14 12:47:37 +0000794 } else if (strcmp(*argv, "--pictureDir") == 0) {
795 ++argv; // This case is dealt with in registerPictFileSamples().
796 } else if (strcmp(*argv, "--picture") == 0) {
797 ++argv; // This case is dealt with in registerPictFileSample().
bsalomon@google.com11959252012-04-06 20:13:38 +0000798 }
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000799 else {
robertphillips@google.com76d40212012-03-22 14:12:15 +0000800 usage(commandName);
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000801 }
802 }
803
804 if (fCurrIndex < 0) {
805 SkString title;
806 if (readTitleFromPrefs(&title)) {
807 fCurrIndex = findByTitle(title.c_str());
808 }
809 }
810
811 if (fCurrIndex < 0) {
812 fCurrIndex = 0;
813 }
814
reed@google.com3cec4d72011-07-06 13:59:47 +0000815 gSampleWindow = this;
816
yangsu@google.com1f394212011-06-01 18:03:34 +0000817#ifdef PIPE_FILE
818 //Clear existing file or create file if it doesn't exist
819 FILE* f = fopen(FILE_PATH, "wb");
820 fclose(f);
821#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +0000822
reed@android.com8a1c16f2008-12-17 15:59:43 +0000823 fPicture = NULL;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000824
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000825 fDeviceType = kRaster_DeviceType;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000826
827#if DEFAULT_TO_GPU
828 fDeviceType = kGPU_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000829#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000830#if SK_ANGLE && DEFAULT_TO_ANGLE
831 fDeviceType = kANGLE_DeviceType;
832#endif
833
reed@android.com8a1c16f2008-12-17 15:59:43 +0000834 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000835 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000836 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000837 fRotate = false;
bungeman@google.comb1785912013-07-09 21:58:56 +0000838 fRotateAnimTime = 0;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000839 fPerspAnim = false;
840 fPerspAnimTime = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +0000841 fRequestGrabImage = false;
scroggo@google.comb073d922012-06-08 15:35:03 +0000842 fPipeState = SkOSMenu::kOffState;
reed@google.com67b89ee2012-08-15 14:41:58 +0000843 fTilingState = SkOSMenu::kOffState;
scroggo@google.com85cade02012-08-17 13:29:50 +0000844 fTileCount.set(1, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000845 fMeasureFPS = false;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000846 fLCDState = SkOSMenu::kMixedState;
847 fAAState = SkOSMenu::kMixedState;
848 fFilterState = SkOSMenu::kMixedState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000849 fSubpixelState = SkOSMenu::kMixedState;
850 fHintingState = 0;
reed@google.com569e0432011-04-05 13:07:03 +0000851 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000852 fScrollTestX = fScrollTestY = 0;
853
Scroggo0f185c22011-03-24 18:35:50 +0000854 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000855 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000856 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
857 fShowZoomer = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000858
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000859 fZoomLevel = 0;
860 fZoomScale = SK_Scalar1;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000861
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000862 fMagnify = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000863
Scroggo8ac0d542011-06-21 14:44:57 +0000864 fSaveToPdf = false;
865 fPdfCanvas = NULL;
866
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000867 fTransitionNext = 6;
868 fTransitionPrev = 2;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000869
yangsu@google.com921091f2011-08-02 13:39:12 +0000870 int sinkID = this->getSinkID();
scroggo@google.com7dadc742012-04-18 14:07:57 +0000871 fAppMenu = new SkOSMenu;
872 fAppMenu->setTitle("Global Settings");
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000873 int itemID;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000874
875 itemID =fAppMenu->appendList("Device Type", "Device Type", sinkID, 0,
876 "Raster", "Picture", "OpenGL",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000877#if SK_ANGLE
878 "ANGLE",
879#endif
880 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000881 fAppMenu->assignKeyEquivalentToItem(itemID, 'd');
882 itemID = fAppMenu->appendTriState("AA", "AA", sinkID, fAAState);
883 fAppMenu->assignKeyEquivalentToItem(itemID, 'b');
884 itemID = fAppMenu->appendTriState("LCD", "LCD", sinkID, fLCDState);
885 fAppMenu->assignKeyEquivalentToItem(itemID, 'l');
886 itemID = fAppMenu->appendTriState("Filter", "Filter", sinkID, fFilterState);
887 fAppMenu->assignKeyEquivalentToItem(itemID, 'n');
bungeman@google.com96aabc82013-06-03 21:26:34 +0000888 itemID = fAppMenu->appendTriState("Subpixel", "Subpixel", sinkID, fSubpixelState);
889 fAppMenu->assignKeyEquivalentToItem(itemID, 's');
890 itemID = fAppMenu->appendList("Hinting", "Hinting", sinkID, fHintingState,
891 gHintingStates[0].name,
892 gHintingStates[1].name,
893 gHintingStates[2].name,
894 gHintingStates[3].name,
bungeman@google.coma9133bb2013-06-04 17:49:20 +0000895 gHintingStates[4].name,
896 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000897 fAppMenu->assignKeyEquivalentToItem(itemID, 'h');
reed@google.com67b89ee2012-08-15 14:41:58 +0000898
scroggo@google.comb073d922012-06-08 15:35:03 +0000899 fUsePipeMenuItemID = fAppMenu->appendTriState("Pipe", "Pipe" , sinkID,
chudy@google.com4605a3f2012-08-01 17:58:01 +0000900 fPipeState);
scroggo@google.comb073d922012-06-08 15:35:03 +0000901 fAppMenu->assignKeyEquivalentToItem(fUsePipeMenuItemID, 'P');
reed@google.com67b89ee2012-08-15 14:41:58 +0000902
903 itemID = fAppMenu->appendTriState("Tiling", "Tiling", sinkID, fTilingState);
904 fAppMenu->assignKeyEquivalentToItem(itemID, 't');
905
chudy@google.com4605a3f2012-08-01 17:58:01 +0000906 itemID = fAppMenu->appendSwitch("Slide Show", "Slide Show" , sinkID, false);
907 fAppMenu->assignKeyEquivalentToItem(itemID, 'a');
908 itemID = fAppMenu->appendSwitch("Clip", "Clip" , sinkID, fUseClip);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000909 fAppMenu->assignKeyEquivalentToItem(itemID, 'c');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000910 itemID = fAppMenu->appendSwitch("Flip X", "Flip X" , sinkID, false);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000911 fAppMenu->assignKeyEquivalentToItem(itemID, 'x');
912 itemID = fAppMenu->appendSwitch("Flip Y", "Flip Y" , sinkID, false);
913 fAppMenu->assignKeyEquivalentToItem(itemID, 'y');
914 itemID = fAppMenu->appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
915 fAppMenu->assignKeyEquivalentToItem(itemID, 'z');
916 itemID = fAppMenu->appendSwitch("Magnify", "Magnify" , sinkID, fMagnify);
917 fAppMenu->assignKeyEquivalentToItem(itemID, 'm');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000918 itemID =fAppMenu->appendList("Transition-Next", "Transition-Next", sinkID,
919 fTransitionNext, "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, 'j');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000923 itemID =fAppMenu->appendList("Transition-Prev", "Transition-Prev", sinkID,
924 fTransitionPrev, "Up", "Up and Right", "Right",
925 "Down and Right", "Down", "Down and Left",
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000926 "Left", "Up and Left", NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000927 fAppMenu->assignKeyEquivalentToItem(itemID, 'k');
928 itemID = fAppMenu->appendAction("Save to PDF", sinkID);
929 fAppMenu->assignKeyEquivalentToItem(itemID, 'e');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000930
scroggo@google.com7dadc742012-04-18 14:07:57 +0000931 this->addMenu(fAppMenu);
932 fSlideMenu = new SkOSMenu;
933 this->addMenu(fSlideMenu);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000934
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000935// this->setConfig(SkBitmap::kRGB_565_Config);
936 this->setConfig(SkBitmap::kARGB_8888_Config);
937 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000938 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000939
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000940 skiagm::GM::SetResourcePath(resourcePath);
941
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000942 this->loadView((*fSamples[fCurrIndex])());
chudy@google.com4605a3f2012-08-01 17:58:01 +0000943
bsalomon@google.com840e9f32011-07-06 21:59:09 +0000944 fPDFData = NULL;
reed@google.comf0b5f682011-03-11 20:08:25 +0000945
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000946 if (NULL == devManager) {
947 fDevManager = new DefaultDeviceManager();
948 } else {
949 devManager->ref();
950 fDevManager = devManager;
951 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000952 fDevManager->setUpBackend(this, fMSAASampleCount);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000953
Scroggob4490c72011-06-17 13:53:05 +0000954 // If another constructor set our dimensions, ensure that our
955 // onSizeChange gets called.
956 if (this->height() && this->width()) {
957 this->onSizeChange();
958 }
reed@google.com2072db82011-11-08 15:18:10 +0000959
960 // can't call this synchronously, since it may require a subclass to
961 // to implement, or the caller may need us to have returned from the
962 // constructor first. Hence we post an event to ourselves.
963// this->updateTitle();
964 postEventToSink(new SkEvent(gUpdateWindowTitleEvtName), this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000965}
966
967SampleWindow::~SampleWindow() {
968 delete fPicture;
Scroggo8ac0d542011-06-21 14:44:57 +0000969 delete fPdfCanvas;
Scroggo0f185c22011-03-24 18:35:50 +0000970 fTypeface->unref();
reed@google.com29038ed2011-07-06 17:56:47 +0000971
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000972 SkSafeUnref(fDevManager);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000973}
974
reed@google.com1830c7a2012-06-04 12:05:43 +0000975static void make_filepath(SkString* path, const char* dir, const SkString& name) {
976 size_t len = strlen(dir);
977 path->set(dir);
978 if (len > 0 && dir[len - 1] != '/') {
979 path->append("/");
980 }
981 path->append(name);
982}
983
chudy@google.com4605a3f2012-08-01 17:58:01 +0000984void SampleWindow::registerPictFileSample(char** argv, int argc) {
985 const char* pict = NULL;
986
987 for (int i = 0; i < argc; ++i) {
988 if (!strcmp(argv[i], "--picture")) {
989 i += 1;
990 if (i < argc) {
991 pict = argv[i];
992 break;
993 }
994 }
995 }
996 if (pict) {
997 SkString path(pict);
reed@google.com7d05ac92013-06-06 13:14:13 +0000998 fCurrIndex = fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +0000999 *fSamples.append() = new PictFileFactory(path);
1000 }
1001}
1002
reed@google.com1830c7a2012-06-04 12:05:43 +00001003void SampleWindow::registerPictFileSamples(char** argv, int argc) {
1004 const char* pictDir = NULL;
1005
1006 for (int i = 0; i < argc; ++i) {
1007 if (!strcmp(argv[i], "--pictureDir")) {
1008 i += 1;
1009 if (i < argc) {
1010 pictDir = argv[i];
1011 break;
1012 }
1013 }
1014 }
1015 if (pictDir) {
1016 SkOSFile::Iter iter(pictDir, "skp");
1017 SkString filename;
1018 while (iter.next(&filename)) {
1019 SkString path;
1020 make_filepath(&path, pictDir, filename);
1021 *fSamples.append() = new PictFileFactory(path);
1022 }
1023 }
1024}
1025
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001026#ifdef SAMPLE_PDF_FILE_VIEWER
1027void SampleWindow::registerPdfFileViewerSamples(char** argv, int argc) {
1028 const char* pdfDir = NULL;
1029
1030 for (int i = 0; i < argc; ++i) {
1031 if (!strcmp(argv[i], "--pdfDir")) {
1032 i += 1;
1033 if (i < argc) {
1034 pdfDir = argv[i];
1035 break;
1036 }
1037 }
1038 }
1039 if (pdfDir) {
1040 SkOSFile::Iter iter(pdfDir, "pdf");
1041 SkString filename;
1042 while (iter.next(&filename)) {
1043 SkString path;
1044 make_filepath(&path, pdfDir, filename);
1045 *fSamples.append() = new PdfFileViewerFactory(path);
1046 }
1047 }
1048}
1049#endif // SAMPLE_PDF_FILE_VIEWER
1050
1051
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001052int SampleWindow::findByTitle(const char title[]) {
1053 int i, count = fSamples.count();
1054 for (i = 0; i < count; i++) {
1055 if (getSampleTitle(i).equals(title)) {
1056 return i;
1057 }
1058 }
mike@reedtribe.orgdd52caa2011-12-28 20:02:10 +00001059 return -1;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001060}
1061
robertphillips@google.com7265e722012-05-03 18:22:28 +00001062void SampleWindow::listTitles() {
1063 int count = fSamples.count();
1064 SkDebugf("All Slides:\n");
1065 for (int i = 0; i < count; i++) {
1066 SkDebugf(" %s\n", getSampleTitle(i).c_str());
1067 }
1068}
1069
reed@android.com55e76b22009-11-23 21:46:47 +00001070static SkBitmap capture_bitmap(SkCanvas* canvas) {
1071 SkBitmap bm;
1072 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
1073 src.copyTo(&bm, src.config());
1074 return bm;
1075}
1076
1077static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
1078 SkBitmap* diff) {
1079 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001080
reed@android.com55e76b22009-11-23 21:46:47 +00001081 SkAutoLockPixels alp0(src);
1082 SkAutoLockPixels alp1(orig);
1083 for (int y = 0; y < src.height(); y++) {
1084 const void* srcP = src.getAddr(0, y);
1085 const void* origP = orig.getAddr(0, y);
1086 size_t bytes = src.width() * src.bytesPerPixel();
1087 if (memcmp(srcP, origP, bytes)) {
1088 SkDebugf("---------- difference on line %d\n", y);
1089 return true;
1090 }
1091 }
1092 return false;
1093}
1094
Scroggo0f185c22011-03-24 18:35:50 +00001095static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
1096{
1097 SkColor desiredColor = paint.getColor();
1098 paint.setColor(SK_ColorWHITE);
1099 const char* c_str = string.c_str();
1100 size_t size = string.size();
1101 SkRect bounds;
1102 paint.measureText(c_str, size, &bounds);
1103 bounds.offset(left, top);
1104 SkScalar inset = SkIntToScalar(-2);
1105 bounds.inset(inset, inset);
1106 canvas->drawRect(bounds, paint);
1107 if (desiredColor != SK_ColorBLACK) {
1108 paint.setColor(SK_ColorBLACK);
1109 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
1110 }
1111 paint.setColor(desiredColor);
1112 canvas->drawText(c_str, size, left, top, paint);
1113}
1114
reed@android.com44177402009-11-23 21:07:51 +00001115#define XCLIP_N 8
1116#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +00001117
1118void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +00001119 // update the animation time
bsalomon@google.come8f09102011-09-08 18:48:12 +00001120 if (!gAnimTimePrev && !gAnimTime) {
1121 // first time make delta be 0
1122 gAnimTime = SkTime::GetMSecs();
1123 gAnimTimePrev = gAnimTime;
1124 } else {
1125 gAnimTimePrev = gAnimTime;
1126 gAnimTime = SkTime::GetMSecs();
1127 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001128
reed@google.comf03bb562011-11-11 21:42:12 +00001129 if (fGesture.isActive()) {
1130 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001131 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001132
djsollen@google.com796763e2012-12-10 14:12:55 +00001133 if (fMeasureFPS) {
1134 fMeasureFPS_Time = 0;
1135 }
1136
reed@android.come522ca52009-11-23 20:10:41 +00001137 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +00001138 this->INHERITED::draw(canvas);
1139 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +00001140
1141 const SkScalar w = this->width();
1142 const SkScalar h = this->height();
1143 const SkScalar cw = w / XCLIP_N;
1144 const SkScalar ch = h / YCLIP_N;
1145 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +00001146 SkRect r;
1147 r.fTop = y * ch;
1148 r.fBottom = (y + 1) * ch;
1149 if (y == YCLIP_N - 1) {
1150 r.fBottom = h;
1151 }
reed@android.come522ca52009-11-23 20:10:41 +00001152 for (int x = 0; x < XCLIP_N; x++) {
1153 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +00001154 r.fLeft = x * cw;
1155 r.fRight = (x + 1) * cw;
1156 if (x == XCLIP_N - 1) {
1157 r.fRight = w;
1158 }
reed@android.come522ca52009-11-23 20:10:41 +00001159 canvas->clipRect(r);
1160 this->INHERITED::draw(canvas);
1161 }
1162 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001163
reed@android.com55e76b22009-11-23 21:46:47 +00001164 SkBitmap diff;
1165 if (bitmap_diff(canvas, orig, &diff)) {
1166 }
reed@android.come522ca52009-11-23 20:10:41 +00001167 } else {
scroggo@google.com85cade02012-08-17 13:29:50 +00001168 const SkScalar cw = SkScalarDiv(this->width(), SkIntToScalar(fTileCount.width()));
1169 const SkScalar ch = SkScalarDiv(this->height(), SkIntToScalar(fTileCount.height()));
rmistry@google.comae933ce2012-08-23 18:19:56 +00001170
scroggo@google.com85cade02012-08-17 13:29:50 +00001171 for (int y = 0; y < fTileCount.height(); ++y) {
1172 for (int x = 0; x < fTileCount.width(); ++x) {
1173 SkAutoCanvasRestore acr(canvas, true);
1174 canvas->clipRect(SkRect::MakeXYWH(x * cw, y * ch, cw, ch));
1175 this->INHERITED::draw(canvas);
1176 }
1177 }
rmistry@google.comae933ce2012-08-23 18:19:56 +00001178
scroggo@google.com85cade02012-08-17 13:29:50 +00001179 if (!fTileCount.equals(1, 1)) {
1180 SkPaint paint;
1181 paint.setColor(0x60FF00FF);
1182 paint.setStyle(SkPaint::kStroke_Style);
1183 for (int y = 0; y < fTileCount.height(); ++y) {
1184 for (int x = 0; x < fTileCount.width(); ++x) {
1185 canvas->drawRect(SkRect::MakeXYWH(x * cw, y * ch, cw, ch), paint);
1186 }
1187 }
1188 }
reed@android.come522ca52009-11-23 20:10:41 +00001189 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001190 if (fShowZoomer && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +00001191 showZoomer(canvas);
1192 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001193 if (fMagnify && !fSaveToPdf) {
1194 magnify(canvas);
1195 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001196
djsollen@google.com796763e2012-12-10 14:12:55 +00001197 if (fMeasureFPS && fMeasureFPS_Time) {
1198 this->updateTitle();
1199 this->postInvalDelay();
1200 }
1201
reed@google.com29038ed2011-07-06 17:56:47 +00001202 // do this last
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001203 fDevManager->publishCanvas(fDeviceType, canvas, this);
Scroggo3272ba82011-05-25 20:50:42 +00001204}
1205
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001206static float clipW = 200;
1207static float clipH = 200;
1208void SampleWindow::magnify(SkCanvas* canvas) {
1209 SkRect r;
1210 int count = canvas->save();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001211
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001212 SkMatrix m = canvas->getTotalMatrix();
robertphillips@google.com07ef9112012-06-04 13:22:14 +00001213 if (!m.invert(&m)) {
1214 return;
1215 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001216 SkPoint offset, center;
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001217 SkScalar mouseX = fMouseX * SK_Scalar1;
1218 SkScalar mouseY = fMouseY * SK_Scalar1;
1219 m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset);
1220 m.mapXY(mouseX, mouseY, &center);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001221
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001222 r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX());
1223 r.offset(offset.fX, offset.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001224
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001225 SkPaint paint;
1226 paint.setColor(0xFF66AAEE);
1227 paint.setStyle(SkPaint::kStroke_Style);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001228 paint.setStrokeWidth(10.f * m.getScaleX());
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001229 //lense offset
1230 //canvas->translate(0, -250);
1231 canvas->drawRect(r, paint);
1232 canvas->clipRect(r);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001233
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001234 m = canvas->getTotalMatrix();
1235 m.setTranslate(-center.fX, -center.fY);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001236 m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001237 m.postTranslate(center.fX, center.fY);
1238 canvas->concat(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001239
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001240 this->INHERITED::draw(canvas);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001241
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001242 canvas->restoreToCount(count);
1243}
1244
Scroggo3272ba82011-05-25 20:50:42 +00001245void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +00001246 int count = canvas->save();
1247 canvas->resetMatrix();
1248 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +00001249 int width = SkScalarRound(this->width());
1250 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +00001251 if (fMouseX >= width) fMouseX = width - 1;
1252 else if (fMouseX < 0) fMouseX = 0;
1253 if (fMouseY >= height) fMouseY = height - 1;
1254 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +00001255
Scroggo0f185c22011-03-24 18:35:50 +00001256 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +00001257 bitmap.lockPixels();
1258
Scroggo0f185c22011-03-24 18:35:50 +00001259 // 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 +00001260 int zoomedWidth = (width >> 1) | 1;
1261 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +00001262 SkIRect src;
1263 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
1264 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
1265 SkRect dest;
1266 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
1267 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
1268 SkPaint paint;
1269 // Clear the background behind our zoomed in view
1270 paint.setColor(SK_ColorWHITE);
1271 canvas->drawRect(dest, paint);
1272 canvas->drawBitmapRect(bitmap, &src, dest);
1273 paint.setColor(SK_ColorBLACK);
1274 paint.setStyle(SkPaint::kStroke_Style);
1275 // Draw a border around the pixel in the middle
1276 SkRect originalPixel;
1277 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
1278 SkMatrix matrix;
1279 SkRect scalarSrc;
1280 scalarSrc.set(src);
1281 SkColor color = bitmap.getColor(fMouseX, fMouseY);
1282 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
1283 SkRect pixel;
1284 matrix.mapRect(&pixel, originalPixel);
1285 // TODO Perhaps measure the values and make the outline white if it's "dark"
1286 if (color == SK_ColorBLACK) {
1287 paint.setColor(SK_ColorWHITE);
1288 }
1289 canvas->drawRect(pixel, paint);
1290 }
1291 paint.setColor(SK_ColorBLACK);
1292 // Draw a border around the destination rectangle
1293 canvas->drawRect(dest, paint);
1294 paint.setStyle(SkPaint::kStrokeAndFill_Style);
1295 // Identify the pixel and its color on screen
1296 paint.setTypeface(fTypeface);
1297 paint.setAntiAlias(true);
1298 SkScalar lineHeight = paint.getFontMetrics(NULL);
1299 SkString string;
1300 string.appendf("(%i, %i)", fMouseX, fMouseY);
1301 SkScalar left = dest.fLeft + SkIntToScalar(3);
1302 SkScalar i = SK_Scalar1;
1303 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1304 // Alpha
1305 i += SK_Scalar1;
1306 string.reset();
1307 string.appendf("A: %X", SkColorGetA(color));
1308 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1309 // Red
1310 i += SK_Scalar1;
1311 string.reset();
1312 string.appendf("R: %X", SkColorGetR(color));
1313 paint.setColor(SK_ColorRED);
1314 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1315 // Green
1316 i += SK_Scalar1;
1317 string.reset();
1318 string.appendf("G: %X", SkColorGetG(color));
1319 paint.setColor(SK_ColorGREEN);
1320 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1321 // Blue
1322 i += SK_Scalar1;
1323 string.reset();
1324 string.appendf("B: %X", SkColorGetB(color));
1325 paint.setColor(SK_ColorBLUE);
1326 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1327 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +00001328}
1329
reed@android.com8a1c16f2008-12-17 15:59:43 +00001330void SampleWindow::onDraw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001331}
1332
1333#include "SkColorPriv.h"
1334
caryclark@google.com02939ce2012-06-06 12:09:51 +00001335#if 0 // UNUSED
reed@android.com8a1c16f2008-12-17 15:59:43 +00001336static void reverseRedAndBlue(const SkBitmap& bm) {
1337 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
1338 uint8_t* p = (uint8_t*)bm.getPixels();
1339 uint8_t* stop = p + bm.getSize();
1340 while (p < stop) {
1341 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
1342 unsigned scale = SkAlpha255To256(p[3]);
1343 unsigned r = p[2];
1344 unsigned b = p[0];
1345 p[0] = SkAlphaMul(r, scale);
1346 p[1] = SkAlphaMul(p[1], scale);
1347 p[2] = SkAlphaMul(b, scale);
1348 p += 4;
1349 }
1350}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001351#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001352
Scroggo8ac0d542011-06-21 14:44:57 +00001353void SampleWindow::saveToPdf()
1354{
1355 fSaveToPdf = true;
1356 this->inval(NULL);
1357}
1358
reed@android.com8a1c16f2008-12-17 15:59:43 +00001359SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +00001360 if (fSaveToPdf) {
1361 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
1362 SkISize size = SkISize::Make(bmp.width(), bmp.height());
1363 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
1364 canvas->getTotalMatrix());
1365 fPdfCanvas = new SkCanvas(pdfDevice);
1366 pdfDevice->unref();
1367 canvas = fPdfCanvas;
bsalomon@google.com82502e22013-01-24 20:47:18 +00001368 } else if (kPicture_DeviceType == fDeviceType) {
1369 fPicture = new SkPicture;
1370 canvas = fPicture->beginRecording(9999, 9999);
Scroggo8ac0d542011-06-21 14:44:57 +00001371 } else {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001372#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00001373 if (kNullGPU_DeviceType != fDeviceType)
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001374#endif
bsalomon@google.com82502e22013-01-24 20:47:18 +00001375 {
1376 canvas = this->INHERITED::beforeChildren(canvas);
reed@google.comac10a2d2010-12-22 21:39:39 +00001377 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001378 }
1379
1380 if (fUseClip) {
1381 canvas->drawColor(0xFFFF88FF);
reed@google.com045e62d2011-10-24 12:19:46 +00001382 canvas->clipPath(fClipPath, SkRegion::kIntersect_Op, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001383 }
1384
1385 return canvas;
1386}
1387
1388static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
1389 const SkRegion& rgn) {
1390 SkCanvas canvas(bm);
1391 SkRegion inval(rgn);
1392
1393 inval.translate(r.fLeft, r.fTop);
1394 canvas.clipRegion(inval);
1395 canvas.drawColor(0xFFFF8080);
1396}
yangsu@google.com501775e2011-06-24 16:04:50 +00001397#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001398void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +00001399 if (fSaveToPdf) {
1400 fSaveToPdf = false;
1401 if (fShowZoomer) {
1402 showZoomer(fPdfCanvas);
1403 }
1404 SkString name;
1405 name.printf("%s.pdf", this->getTitle());
1406 SkPDFDocument doc;
1407 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
1408 doc.appendPage(device);
djsollen@google.com56c69772011-11-08 19:00:26 +00001409#ifdef SK_BUILD_FOR_ANDROID
Scroggo8ac0d542011-06-21 14:44:57 +00001410 name.prepend("/sdcard/");
1411#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +00001412
yangsu@google.com501775e2011-06-24 16:04:50 +00001413#ifdef SK_BUILD_FOR_IOS
1414 SkDynamicMemoryWStream mstream;
1415 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +00001416 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +00001417#endif
Scroggo8ac0d542011-06-21 14:44:57 +00001418 SkFILEWStream stream(name.c_str());
1419 if (stream.isValid()) {
1420 doc.emitPDF(&stream);
1421 const char* desc = "File saved from Skia SampleApp";
1422 this->onPDFSaved(this->getTitle(), desc, name.c_str());
1423 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001424
Scroggo8ac0d542011-06-21 14:44:57 +00001425 delete fPdfCanvas;
1426 fPdfCanvas = NULL;
1427
1428 // We took over the draw calls in order to create the PDF, so we need
1429 // to redraw.
1430 this->inval(NULL);
1431 return;
1432 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001433
reed@android.comf2b98d62010-12-20 18:26:13 +00001434 if (fRequestGrabImage) {
1435 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001436
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001437 SkBaseDevice* device = orig->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001438 SkBitmap bmp;
1439 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001440 static int gSampleGrabCounter;
1441 SkString name;
reed@google.com7c57e0e2012-09-08 21:20:33 +00001442 name.printf("sample_grab_%d.png", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001443 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +00001444 SkImageEncoder::kPNG_Type, 100);
1445 }
1446 }
1447
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001448 if (kPicture_DeviceType == fDeviceType) {
1449 if (true) {
1450 SkPicture* pict = new SkPicture(*fPicture);
1451 fPicture->unref();
reed@google.come23f1942011-12-08 19:36:00 +00001452 this->installDrawFilter(orig);
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001453 orig->drawPicture(*pict);
1454 pict->unref();
1455 } else if (true) {
1456 SkDynamicMemoryWStream ostream;
1457 fPicture->serialize(&ostream);
1458 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001459
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001460 SkAutoDataUnref data(ostream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +00001461 SkMemoryStream istream(data->data(), data->size());
scroggo@google.comf1754ec2013-06-28 21:32:00 +00001462 SkAutoTUnref<SkPicture> pict(SkPicture::CreateFromStream(&istream));
1463 if (pict.get() != NULL) {
1464 orig->drawPicture(*pict.get());
1465 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001466 } else {
1467 fPicture->draw(orig);
1468 fPicture->unref();
1469 }
1470 fPicture = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001471 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001472
reed@google.com17d7aec2011-04-25 14:31:44 +00001473 // Do this after presentGL and other finishing, rather than in afterChild
djsollen@google.com796763e2012-12-10 14:12:55 +00001474 if (fMeasureFPS && fMeasureFPS_StartTime) {
1475 fMeasureFPS_Time += SkTime::GetMSecs() - fMeasureFPS_StartTime;
reed@google.com17d7aec2011-04-25 14:31:44 +00001476 }
1477
1478 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +00001479 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001480 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
1481 int dx = fScrollTestX * 7;
1482 int dy = fScrollTestY * 7;
1483 SkIRect r;
1484 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001485
reed@android.com8a1c16f2008-12-17 15:59:43 +00001486 r.set(50, 50, 50+100, 50+100);
1487 bm.scrollRect(&r, dx, dy, &inval);
1488 paint_rgn(bm, r, inval);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001489 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001490}
1491
reed@android.com6c5f6f22009-08-14 16:08:38 +00001492void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
reed@android.com6c5f6f22009-08-14 16:08:38 +00001493 if (fRotate) {
bungeman@google.comb1785912013-07-09 21:58:56 +00001494 fRotateAnimTime += SampleCode::GetAnimSecondsDelta();
1495
reed@android.com6c5f6f22009-08-14 16:08:38 +00001496 SkScalar cx = this->width() / 2;
1497 SkScalar cy = this->height() / 2;
1498 canvas->translate(cx, cy);
bungeman@google.comb1785912013-07-09 21:58:56 +00001499 canvas->rotate(fRotateAnimTime * 10);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001500 canvas->translate(-cx, -cy);
1501 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001502
bsalomon@google.come8f09102011-09-08 18:48:12 +00001503 if (fPerspAnim) {
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001504 fPerspAnimTime += SampleCode::GetAnimSecondsDelta();
1505
1506 static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
1507 static const SkScalar gAnimMag = SK_Scalar1 / 1000;
1508 SkScalar t = SkScalarMod(fPerspAnimTime, gAnimPeriod);
1509 if (SkScalarFloorToInt(SkScalarDiv(fPerspAnimTime, gAnimPeriod)) & 0x1) {
1510 t = gAnimPeriod - t;
1511 }
1512 t = 2 * t - gAnimPeriod;
1513 t = SkScalarMul(SkScalarDiv(t, gAnimPeriod), gAnimMag);
1514 SkMatrix m;
1515 m.reset();
1516 m.setPerspY(t);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001517 canvas->concat(m);
1518 }
reed@google.comf0b5f682011-03-11 20:08:25 +00001519
reed@google.come23f1942011-12-08 19:36:00 +00001520 this->installDrawFilter(canvas);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001521
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001522 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +00001523 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
djsollen@google.com796763e2012-12-10 14:12:55 +00001524 fMeasureFPS_StartTime = SkTime::GetMSecs();
reed@google.comf2183392011-04-22 14:10:48 +00001525 }
1526 } else {
1527 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001528 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001529 if (fPerspAnim || fRotate) {
bsalomon@google.come8f09102011-09-08 18:48:12 +00001530 this->inval(NULL);
1531 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001532}
1533
1534void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +00001535 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001536}
1537
reed@android.com8a1c16f2008-12-17 15:59:43 +00001538static SkBitmap::Config gConfigCycle[] = {
1539 SkBitmap::kNo_Config, // none -> none
reed@google.com72e78082013-11-25 20:54:56 +00001540 SkBitmap::kNo_Config, // a1 -> none
reed@android.com8a1c16f2008-12-17 15:59:43 +00001541 SkBitmap::kNo_Config, // a8 -> none
1542 SkBitmap::kNo_Config, // index8 -> none
1543 SkBitmap::kARGB_4444_Config, // 565 -> 4444
1544 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
1545 SkBitmap::kRGB_565_Config // 8888 -> 565
1546};
1547
1548static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
1549 return gConfigCycle[c];
1550}
1551
djsollen@google.come32b5832011-06-13 16:58:40 +00001552void SampleWindow::changeZoomLevel(float delta) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001553 fZoomLevel += delta;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001554 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001555 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
1556 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001557 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001558 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
1559 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001560 } else {
1561 fZoomScale = SK_Scalar1;
1562 }
reed@google.comf03bb562011-11-11 21:42:12 +00001563 this->updateMatrix();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001564}
1565
reed@google.comf03bb562011-11-11 21:42:12 +00001566void SampleWindow::updateMatrix(){
1567 SkMatrix m;
1568 m.reset();
1569 if (fZoomLevel) {
1570 SkPoint center;
1571 //m = this->getLocalMatrix();//.invert(&m);
1572 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
1573 SkScalar cx = center.fX;
1574 SkScalar cy = center.fY;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001575
reed@google.comf03bb562011-11-11 21:42:12 +00001576 m.setTranslate(-cx, -cy);
1577 m.postScale(fZoomScale, fZoomScale);
1578 m.postTranslate(cx, cy);
1579 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001580
reed@google.comf03bb562011-11-11 21:42:12 +00001581 if (fFlipAxis) {
1582 m.preTranslate(fZoomCenterX, fZoomCenterY);
1583 if (fFlipAxis & kFlipAxis_X) {
1584 m.preScale(-SK_Scalar1, SK_Scalar1);
1585 }
1586 if (fFlipAxis & kFlipAxis_Y) {
1587 m.preScale(SK_Scalar1, -SK_Scalar1);
1588 }
1589 m.preTranslate(-fZoomCenterX, -fZoomCenterY);
1590 //canvas->concat(m);
1591 }
1592 // Apply any gesture matrix
1593 m.preConcat(fGesture.localM());
1594 m.preConcat(fGesture.globalM());
chudy@google.com4605a3f2012-08-01 17:58:01 +00001595
reed@google.comf03bb562011-11-11 21:42:12 +00001596 this->setLocalMatrix(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001597
reed@google.comf03bb562011-11-11 21:42:12 +00001598 this->updateTitle();
1599 this->inval(NULL);
1600}
Scroggo2c8208f2011-06-15 16:49:08 +00001601bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +00001602 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001603 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001604 fTransitionPrev));
Scroggo2c8208f2011-06-15 16:49:08 +00001605 return true;
1606}
1607
reed@android.com8a1c16f2008-12-17 15:59:43 +00001608bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +00001609 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001610 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001611 fTransitionNext));
reed@android.com34245c72009-11-03 04:00:48 +00001612 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001613}
1614
yangsu@google.com501775e2011-06-24 16:04:50 +00001615bool SampleWindow::goToSample(int i) {
1616 fCurrIndex = (i) % fSamples.count();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001617 this->loadView(create_transition(curr_view(this),(*fSamples[fCurrIndex])(), 6));
yangsu@google.com501775e2011-06-24 16:04:50 +00001618 return true;
1619}
1620
1621SkString SampleWindow::getSampleTitle(int i) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001622 return ::getSampleTitle(fSamples[i]);
yangsu@google.com501775e2011-06-24 16:04:50 +00001623}
1624
1625int SampleWindow::sampleCount() {
1626 return fSamples.count();
1627}
1628
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001629void SampleWindow::showOverview() {
chudy@google.com4605a3f2012-08-01 17:58:01 +00001630 this->loadView(create_transition(curr_view(this),
1631 create_overview(fSamples.count(), fSamples.begin()),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001632 4));
1633}
1634
reed@google.come23f1942011-12-08 19:36:00 +00001635void SampleWindow::installDrawFilter(SkCanvas* canvas) {
bungeman@google.com96aabc82013-06-03 21:26:34 +00001636 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState, fFilterState, fSubpixelState,
1637 fHintingState))->unref();
reed@google.come23f1942011-12-08 19:36:00 +00001638}
1639
Scroggo2c8208f2011-06-15 16:49:08 +00001640void SampleWindow::postAnimatingEvent() {
1641 if (fAnimating) {
reed@google.com87fac4a2011-08-04 13:50:17 +00001642 (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
Scroggo2c8208f2011-06-15 16:49:08 +00001643 }
1644}
reed@google.come23f1942011-12-08 19:36:00 +00001645
reed@android.com8a1c16f2008-12-17 15:59:43 +00001646bool SampleWindow::onEvent(const SkEvent& evt) {
reed@google.com2072db82011-11-08 15:18:10 +00001647 if (evt.isType(gUpdateWindowTitleEvtName)) {
1648 this->updateTitle();
1649 return true;
1650 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001651 if (evt.isType(ANIMATING_EVENTTYPE)) {
1652 if (fAnimating) {
1653 this->nextSample();
1654 this->postAnimatingEvent();
1655 }
1656 return true;
1657 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001658 if (evt.isType("replace-transition-view")) {
1659 this->loadView((SkView*)SkEventSink::FindSink(evt.getFast32()));
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001660 return true;
1661 }
reed@android.com34245c72009-11-03 04:00:48 +00001662 if (evt.isType("set-curr-index")) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001663 this->goToSample(evt.getFast32());
reed@android.com34245c72009-11-03 04:00:48 +00001664 return true;
1665 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001666 if (isInvalEvent(evt)) {
1667 this->inval(NULL);
1668 return true;
1669 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001670 int selected = -1;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001671 if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001672 this->setDeviceType((DeviceType)selected);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001673 return true;
yangsu@google.com921091f2011-08-02 13:39:12 +00001674 }
scroggo@google.comb073d922012-06-08 15:35:03 +00001675 if (SkOSMenu::FindTriState(evt, "Pipe", &fPipeState)) {
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001676#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00001677 if (!fPipeState != SkOSMenu::kOnState)
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001678 gServer.disconnectAll();
1679#endif
scroggo@google.comb073d922012-06-08 15:35:03 +00001680 (void)SampleView::SetUsePipe(curr_view(this), fPipeState);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001681 this->updateTitle();
1682 this->inval(NULL);
yangsu@google.com921091f2011-08-02 13:39:12 +00001683 return true;
1684 }
reed@google.com67b89ee2012-08-15 14:41:58 +00001685 if (SkOSMenu::FindTriState(evt, "Tiling", &fTilingState)) {
1686 int nx = 1, ny = 1;
1687 switch (fTilingState) {
1688 case SkOSMenu::kOffState: nx = 1; ny = 1; break;
reed@google.com56b64a52012-08-15 20:44:36 +00001689 case SkOSMenu::kMixedState: nx = 1; ny = 16; break;
1690 case SkOSMenu::kOnState: nx = 4; ny = 4; break;
reed@google.com67b89ee2012-08-15 14:41:58 +00001691 }
scroggo@google.com85cade02012-08-17 13:29:50 +00001692 fTileCount.set(nx, ny);
reed@google.com67b89ee2012-08-15 14:41:58 +00001693 this->inval(NULL);
1694 return true;
1695 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001696 if (SkOSMenu::FindSwitchState(evt, "Slide Show", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001697 this->toggleSlideshow();
1698 return true;
1699 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001700 if (SkOSMenu::FindTriState(evt, "AA", &fAAState) ||
1701 SkOSMenu::FindTriState(evt, "LCD", &fLCDState) ||
1702 SkOSMenu::FindTriState(evt, "Filter", &fFilterState) ||
bungeman@google.com96aabc82013-06-03 21:26:34 +00001703 SkOSMenu::FindTriState(evt, "Subpixel", &fSubpixelState) ||
1704 SkOSMenu::FindListIndex(evt, "Hinting", &fHintingState) ||
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001705 SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) ||
1706 SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
1707 SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify) ||
1708 SkOSMenu::FindListIndex(evt, "Transition-Next", &fTransitionNext) ||
1709 SkOSMenu::FindListIndex(evt, "Transition-Prev", &fTransitionPrev)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001710 this->inval(NULL);
1711 this->updateTitle();
1712 return true;
1713 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001714 if (SkOSMenu::FindSwitchState(evt, "Flip X", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001715 fFlipAxis ^= kFlipAxis_X;
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::FindSwitchState(evt, "Flip Y", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001720 fFlipAxis ^= kFlipAxis_Y;
reed@google.comf03bb562011-11-11 21:42:12 +00001721 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001722 return true;
1723 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001724 if (SkOSMenu::FindAction(evt,"Save to PDF")) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001725 this->saveToPdf();
1726 return true;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001727 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001728 return this->INHERITED::onEvent(evt);
1729}
1730
reed@android.comf2b98d62010-12-20 18:26:13 +00001731bool SampleWindow::onQuery(SkEvent* query) {
1732 if (query->isType("get-slide-count")) {
1733 query->setFast32(fSamples.count());
1734 return true;
1735 }
1736 if (query->isType("get-slide-title")) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001737 SkView* view = (*fSamples[query->getFast32()])();
reed@android.comf2b98d62010-12-20 18:26:13 +00001738 SkEvent evt(gTitleEvtName);
1739 if (view->doQuery(&evt)) {
1740 query->setString("title", evt.findString(gTitleEvtName));
1741 }
1742 SkSafeUnref(view);
1743 return true;
1744 }
1745 if (query->isType("use-fast-text")) {
1746 SkEvent evt(gFastTextEvtName);
1747 return curr_view(this)->doQuery(&evt);
1748 }
reed@google.com29038ed2011-07-06 17:56:47 +00001749 if (query->isType("ignore-window-bitmap")) {
1750 query->setFast32(this->getGrContext() != NULL);
1751 return true;
1752 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001753 return this->INHERITED::onQuery(query);
1754}
1755
caryclark@google.com02939ce2012-06-06 12:09:51 +00001756#if 0 // UNUSED
reed@android.com0ae6b242008-12-23 16:49:54 +00001757static void cleanup_for_filename(SkString* name) {
1758 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001759 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001760 switch (str[i]) {
1761 case ':': str[i] = '-'; break;
1762 case '/': str[i] = '-'; break;
1763 case ' ': str[i] = '_'; break;
1764 default: break;
1765 }
1766 }
1767}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001768#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001769
reed@google.coma4f81372012-11-15 15:56:38 +00001770//extern bool gIgnoreFastBlurRect;
1771
reed@android.com8a1c16f2008-12-17 15:59:43 +00001772bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001773 {
1774 SkView* view = curr_view(this);
1775 if (view) {
1776 SkEvent evt(gCharEvtName);
1777 evt.setFast32(uni);
1778 if (view->doQuery(&evt)) {
1779 return true;
1780 }
1781 }
1782 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001783
reed@android.com8a1c16f2008-12-17 15:59:43 +00001784 int dx = 0xFF;
1785 int dy = 0xFF;
1786
1787 switch (uni) {
1788 case '5': dx = 0; dy = 0; break;
1789 case '8': dx = 0; dy = -1; break;
1790 case '6': dx = 1; dy = 0; break;
1791 case '2': dx = 0; dy = 1; break;
1792 case '4': dx = -1; dy = 0; break;
1793 case '7': dx = -1; dy = -1; break;
1794 case '9': dx = 1; dy = -1; break;
1795 case '3': dx = 1; dy = 1; break;
1796 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001797
reed@android.com8a1c16f2008-12-17 15:59:43 +00001798 default:
1799 break;
1800 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001801
reed@android.com8a1c16f2008-12-17 15:59:43 +00001802 if (0xFF != dx && 0xFF != dy) {
1803 if ((dx | dy) == 0) {
1804 fScrollTestX = fScrollTestY = 0;
1805 } else {
1806 fScrollTestX += dx;
1807 fScrollTestY += dy;
1808 }
1809 this->inval(NULL);
1810 return true;
1811 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001812
reed@android.com0ae6b242008-12-23 16:49:54 +00001813 switch (uni) {
djsollen@google.com796763e2012-12-10 14:12:55 +00001814 case 'b':
1815 {
1816 postEventToSink(SkNEW_ARGS(SkEvent, ("PictFileView::toggleBBox")), curr_view(this));
1817 this->updateTitle();
1818 this->inval(NULL);
1819 break;
1820 }
reed@google.coma4f81372012-11-15 15:56:38 +00001821 case 'B':
1822// gIgnoreFastBlurRect = !gIgnoreFastBlurRect;
1823 this->inval(NULL);
1824 break;
1825
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001826 case 'f':
chudy@google.com4605a3f2012-08-01 17:58:01 +00001827 // only
djsollen@google.com6ff82552011-11-07 15:43:57 +00001828 toggleFPS();
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001829 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001830 case 'g':
1831 fRequestGrabImage = true;
1832 this->inval(NULL);
1833 break;
reed@google.coma4f81372012-11-15 15:56:38 +00001834 case 'G':
1835 gShowGMBounds = !gShowGMBounds;
1836 postEventToSink(GMSampleView::NewShowSizeEvt(gShowGMBounds),
1837 curr_view(this));
1838 this->inval(NULL);
1839 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001840 case 'i':
1841 this->zoomIn();
1842 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001843 case 'o':
1844 this->zoomOut();
1845 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001846 case 'r':
1847 fRotate = !fRotate;
bungeman@google.comb1785912013-07-09 21:58:56 +00001848 fRotateAnimTime = 0;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001849 this->inval(NULL);
1850 this->updateTitle();
1851 return true;
bsalomon@google.come8f09102011-09-08 18:48:12 +00001852 case 'k':
1853 fPerspAnim = !fPerspAnim;
1854 this->inval(NULL);
1855 this->updateTitle();
1856 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001857#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00001858 case '\\':
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001859 this->setDeviceType(kNullGPU_DeviceType);
1860 this->inval(NULL);
1861 this->updateTitle();
bsalomon@google.com74913722011-10-27 20:44:19 +00001862 return true;
twiz@google.com05e70242012-01-27 19:12:00 +00001863 case 'p':
1864 {
1865 GrContext* grContext = this->getGrContext();
1866 if (grContext) {
1867 size_t cacheBytes = grContext->getGpuTextureCacheBytes();
1868 grContext->freeGpuResources();
1869 SkDebugf("Purged %d bytes from the GPU resource cache.\n",
1870 cacheBytes);
1871 }
1872 }
1873 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001874#endif
reed@android.com0ae6b242008-12-23 16:49:54 +00001875 default:
1876 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001877 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001878
scroggo@google.com7dadc742012-04-18 14:07:57 +00001879 if (fAppMenu->handleKeyEquivalent(uni)|| fSlideMenu->handleKeyEquivalent(uni)) {
1880 this->onUpdateMenu(fAppMenu);
1881 this->onUpdateMenu(fSlideMenu);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001882 return true;
1883 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001884 return this->INHERITED::onHandleChar(uni);
1885}
1886
yangsu@google.com921091f2011-08-02 13:39:12 +00001887void SampleWindow::setDeviceType(DeviceType type) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001888 if (type == fDeviceType)
1889 return;
1890
1891 fDevManager->tearDownBackend(this);
1892
1893 fDeviceType = type;
1894
bsalomon@google.com11959252012-04-06 20:13:38 +00001895 fDevManager->setUpBackend(this, fMSAASampleCount);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001896
yangsu@google.com921091f2011-08-02 13:39:12 +00001897 this->updateTitle();
1898 this->inval(NULL);
1899}
1900
Scroggo2c8208f2011-06-15 16:49:08 +00001901void SampleWindow::toggleSlideshow() {
1902 fAnimating = !fAnimating;
1903 this->postAnimatingEvent();
1904 this->updateTitle();
1905}
1906
1907void SampleWindow::toggleRendering() {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001908 this->setDeviceType(cycle_devicetype(fDeviceType));
Scroggo2c8208f2011-06-15 16:49:08 +00001909 this->updateTitle();
1910 this->inval(NULL);
1911}
1912
djsollen@google.com6ff82552011-11-07 15:43:57 +00001913void SampleWindow::toggleFPS() {
1914 fMeasureFPS = !fMeasureFPS;
1915 this->updateTitle();
1916 this->inval(NULL);
1917}
1918
reed@android.com8a1c16f2008-12-17 15:59:43 +00001919#include "SkDumpCanvas.h"
1920
1921bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001922 {
1923 SkView* view = curr_view(this);
1924 if (view) {
1925 SkEvent evt(gKeyEvtName);
1926 evt.setFast32(key);
1927 if (view->doQuery(&evt)) {
1928 return true;
1929 }
1930 }
1931 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001932 switch (key) {
1933 case kRight_SkKey:
1934 if (this->nextSample()) {
1935 return true;
1936 }
1937 break;
1938 case kLeft_SkKey:
scroggo@google.come2dd9732012-08-15 20:03:06 +00001939 if (this->previousSample()) {
1940 return true;
1941 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001942 return true;
1943 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001944 if (USE_ARROWS_FOR_ZOOM) {
bungeman@google.comcefd9812013-05-15 15:07:32 +00001945 this->changeZoomLevel(1.f / 32.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001946 } else {
1947 fNClip = !fNClip;
1948 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001949 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001950 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001951 return true;
1952 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001953 if (USE_ARROWS_FOR_ZOOM) {
bungeman@google.comcefd9812013-05-15 15:07:32 +00001954 this->changeZoomLevel(-1.f / 32.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001955 } else {
1956 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001957 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001958 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001959 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001960 case kOK_SkKey: {
1961 SkString title;
1962 if (curr_title(this, &title)) {
1963 writeTitleToPrefs(title.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001964 }
1965 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001966 }
reed@android.com34245c72009-11-03 04:00:48 +00001967 case kBack_SkKey:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001968 this->showOverview();
reed@android.com34245c72009-11-03 04:00:48 +00001969 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001970 default:
1971 break;
1972 }
1973 return this->INHERITED::onHandleKey(key);
1974}
1975
reed@google.com52f57e12011-03-16 12:10:02 +00001976///////////////////////////////////////////////////////////////////////////////
1977
1978static const char gGestureClickType[] = "GestureClickType";
1979
Scroggod3aed392011-06-22 13:26:56 +00001980bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
reed@google.com4d5c26d2013-01-08 16:17:50 +00001981 void* owner, unsigned modi) {
Scroggo0f185c22011-03-24 18:35:50 +00001982 if (Click::kMoved_State == state) {
1983 updatePointer(x, y);
1984 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001985 int w = SkScalarRound(this->width());
1986 int h = SkScalarRound(this->height());
1987
1988 // check for the resize-box
1989 if (w - x < 16 && h - y < 16) {
1990 return false; // let the OS handle the click
chudy@google.com4605a3f2012-08-01 17:58:01 +00001991 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001992 else if (fMagnify) {
1993 //it's only necessary to update the drawing if there's a click
1994 this->inval(NULL);
1995 return false; //prevent dragging while magnify is enabled
reed@google.com4d5c26d2013-01-08 16:17:50 +00001996 } else {
1997 // capture control+option, and trigger debugger
1998 if ((modi & kControl_SkModifierKey) && (modi & kOption_SkModifierKey)) {
1999 if (Click::kDown_State == state) {
2000 SkEvent evt("debug-hit-test");
2001 evt.setS32("debug-hit-test-x", x);
2002 evt.setS32("debug-hit-test-y", y);
2003 curr_view(this)->doEvent(evt);
2004 }
2005 return true;
2006 } else {
2007 return this->INHERITED::onDispatchClick(x, y, state, owner, modi);
2008 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002009 }
2010}
2011
reed@google.com52f57e12011-03-16 12:10:02 +00002012class GestureClick : public SkView::Click {
2013public:
2014 GestureClick(SkView* target) : SkView::Click(target) {
2015 this->setType(gGestureClickType);
2016 }
2017
2018 static bool IsGesture(Click* click) {
2019 return click->isType(gGestureClickType);
2020 }
2021};
2022
reed@google.com4d5c26d2013-01-08 16:17:50 +00002023SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y,
2024 unsigned modi) {
reed@google.com52f57e12011-03-16 12:10:02 +00002025 return new GestureClick(this);
2026}
2027
2028bool SampleWindow::onClick(Click* click) {
2029 if (GestureClick::IsGesture(click)) {
bsalomon@google.com4d4f2812011-12-12 18:34:01 +00002030 float x = static_cast<float>(click->fICurr.fX);
2031 float y = static_cast<float>(click->fICurr.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00002032
reed@google.com52f57e12011-03-16 12:10:02 +00002033 switch (click->fState) {
2034 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00002035 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00002036 break;
2037 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00002038 fGesture.touchMoved(click->fOwner, x, y);
reed@google.comf03bb562011-11-11 21:42:12 +00002039 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002040 break;
2041 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00002042 fGesture.touchEnd(click->fOwner);
reed@google.comf03bb562011-11-11 21:42:12 +00002043 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002044 break;
2045 }
2046 return true;
2047 }
2048 return false;
2049}
2050
2051///////////////////////////////////////////////////////////////////////////////
2052
reed@android.com8a1c16f2008-12-17 15:59:43 +00002053void SampleWindow::loadView(SkView* view) {
2054 SkView::F2BIter iter(this);
2055 SkView* prev = iter.next();
2056 if (prev) {
2057 prev->detachFromParent();
2058 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002059
reed@android.com8a1c16f2008-12-17 15:59:43 +00002060 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00002061 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002062 this->attachChildToFront(view)->unref();
2063 view->setSize(this->width(), this->height());
2064
yangsu@google.com921091f2011-08-02 13:39:12 +00002065 //repopulate the slide menu when a view is loaded
scroggo@google.com7dadc742012-04-18 14:07:57 +00002066 fSlideMenu->reset();
djsollen@google.com388974f2013-01-17 13:20:01 +00002067
scroggo@google.comb073d922012-06-08 15:35:03 +00002068 (void)SampleView::SetUsePipe(view, fPipeState);
yangsu@google.com921091f2011-08-02 13:39:12 +00002069 if (SampleView::IsSampleView(view))
scroggo@google.com7dadc742012-04-18 14:07:57 +00002070 ((SampleView*)view)->requestMenu(fSlideMenu);
2071 this->onUpdateMenu(fSlideMenu);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002072 this->updateTitle();
2073}
2074
2075static const char* gConfigNames[] = {
2076 "unknown config",
reed@google.com72e78082013-11-25 20:54:56 +00002077 "A1",
reed@android.com8a1c16f2008-12-17 15:59:43 +00002078 "A8",
2079 "Index8",
2080 "565",
2081 "4444",
2082 "8888"
2083};
2084
2085static const char* configToString(SkBitmap::Config c) {
2086 return gConfigNames[c];
2087}
2088
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002089static const char* gDeviceTypePrefix[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002090 "raster: ",
2091 "picture: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002092#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00002093 "opengl: ",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00002094#if SK_ANGLE
2095 "angle: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002096#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +00002097 "null-gl: "
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002098#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +00002099};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002100SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gDeviceTypePrefix) == SampleWindow::kDeviceTypeCnt,
2101 array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002102
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002103static const char* trystate_str(SkOSMenu::TriState state,
reed@google.com569e0432011-04-05 13:07:03 +00002104 const char trueStr[], const char falseStr[]) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002105 if (SkOSMenu::kOnState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002106 return trueStr;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002107 } else if (SkOSMenu::kOffState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002108 return falseStr;
2109 }
2110 return NULL;
2111}
2112
reed@android.com8a1c16f2008-12-17 15:59:43 +00002113void SampleWindow::updateTitle() {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002114 SkView* view = curr_view(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002115
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002116 SkString title;
2117 if (!curr_title(this, &title)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002118 title.set("<unknown>");
2119 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002120
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002121 title.prepend(gDeviceTypePrefix[fDeviceType]);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002122
2123 title.prepend(" ");
2124 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002125
reed@android.com8a1c16f2008-12-17 15:59:43 +00002126 if (fAnimating) {
2127 title.prepend("<A> ");
2128 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00002129 if (fRotate) {
2130 title.prepend("<R> ");
2131 }
reed@android.come522ca52009-11-23 20:10:41 +00002132 if (fNClip) {
2133 title.prepend("<C> ");
2134 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00002135 if (fPerspAnim) {
2136 title.prepend("<K> ");
2137 }
reed@google.com569e0432011-04-05 13:07:03 +00002138
2139 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
2140 title.prepend(trystate_str(fAAState, "AA ", "aa "));
bungeman@google.com96aabc82013-06-03 21:26:34 +00002141 title.prepend(trystate_str(fFilterState, "N ", "n "));
2142 title.prepend(trystate_str(fSubpixelState, "S ", "s "));
reed@google.com569e0432011-04-05 13:07:03 +00002143 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
2144 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002145 title.prepend(gHintingStates[fHintingState].label);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002146
2147 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00002148 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002149 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002150
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002151 if (fMeasureFPS) {
reed@google.com43e10142012-09-10 11:52:52 +00002152 title.appendf(" %8.3f ms", fMeasureFPS_Time / (float)FPS_REPEAT_COUNT);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002153 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002154 if (SampleView::IsSampleView(view)) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002155 switch (fPipeState) {
2156 case SkOSMenu::kOnState:
2157 title.prepend("<Pipe> ");
2158 break;
2159 case SkOSMenu::kMixedState:
2160 title.prepend("<Tiled Pipe> ");
2161 break;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002162
scroggo@google.comb073d922012-06-08 15:35:03 +00002163 default:
2164 break;
2165 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002166 title.prepend("! ");
2167 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002168
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002169#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00002170 if (IsGpuDeviceType(fDeviceType) &&
robertphillips@google.com4750fa52012-04-10 13:34:11 +00002171 NULL != fDevManager &&
bsalomon@google.com82502e22013-01-24 20:47:18 +00002172 fDevManager->getGrRenderTarget() &&
bsalomon@google.com11959252012-04-06 20:13:38 +00002173 fDevManager->getGrRenderTarget()->numSamples() > 0) {
2174 title.appendf(" [MSAA: %d]",
2175 fDevManager->getGrRenderTarget()->numSamples());
2176 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002177#endif
bsalomon@google.com11959252012-04-06 20:13:38 +00002178
reed@android.com8a1c16f2008-12-17 15:59:43 +00002179 this->setTitle(title.c_str());
2180}
2181
2182void SampleWindow::onSizeChange() {
2183 this->INHERITED::onSizeChange();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002184
reed@android.com8a1c16f2008-12-17 15:59:43 +00002185 SkView::F2BIter iter(this);
2186 SkView* view = iter.next();
2187 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002188
reed@android.com8a1c16f2008-12-17 15:59:43 +00002189 // rebuild our clippath
2190 {
2191 const SkScalar W = this->width();
2192 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002193
reed@android.com8a1c16f2008-12-17 15:59:43 +00002194 fClipPath.reset();
2195#if 0
2196 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
2197 SkRect r;
2198 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
2199 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
2200 fClipPath.addRect(r);
2201 }
2202#else
2203 SkRect r;
2204 r.set(0, 0, W, H);
2205 fClipPath.addRect(r, SkPath::kCCW_Direction);
2206 r.set(W/4, H/4, W*3/4, H*3/4);
2207 fClipPath.addRect(r, SkPath::kCW_Direction);
2208#endif
2209 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002210
Scroggo2c8208f2011-06-15 16:49:08 +00002211 fZoomCenterX = SkScalarHalf(this->width());
2212 fZoomCenterY = SkScalarHalf(this->height());
2213
djsollen@google.com56c69772011-11-08 19:00:26 +00002214#ifdef SK_BUILD_FOR_ANDROID
Scroggob4490c72011-06-17 13:53:05 +00002215 // FIXME: The first draw after a size change does not work on Android, so
2216 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00002217 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00002218#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00002219 this->updateTitle(); // to refresh our config
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002220 fDevManager->windowSizeChanged(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002221}
2222
2223///////////////////////////////////////////////////////////////////////////////
2224
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002225static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002226static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00002227static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002228
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002229bool SampleView::IsSampleView(SkView* view) {
2230 SkEvent evt(is_sample_view_tag);
2231 return view->doQuery(&evt);
2232}
2233
reed@google.comf2183392011-04-22 14:10:48 +00002234bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002235 SkEvent evt(repeat_count_tag);
2236 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00002237 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002238}
2239
scroggo@google.comb073d922012-06-08 15:35:03 +00002240bool SampleView::SetUsePipe(SkView* view, SkOSMenu::TriState state) {
2241 SkEvent evt;
2242 evt.setS32(set_use_pipe_tag, state);
reed@google.com0faac1e2011-05-11 05:58:58 +00002243 return view->doEvent(evt);
2244}
2245
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002246bool SampleView::onEvent(const SkEvent& evt) {
2247 if (evt.isType(repeat_count_tag)) {
2248 fRepeatCount = evt.getFast32();
2249 return true;
2250 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002251
scroggo@google.comb073d922012-06-08 15:35:03 +00002252 int32_t pipeHolder;
2253 if (evt.findS32(set_use_pipe_tag, &pipeHolder)) {
2254 fPipeState = static_cast<SkOSMenu::TriState>(pipeHolder);
reed@google.com0faac1e2011-05-11 05:58:58 +00002255 return true;
2256 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002257
2258 if (evt.isType("debug-hit-test")) {
2259 fDebugHitTest = true;
2260 evt.findS32("debug-hit-test-x", &fDebugHitTestLoc.fX);
2261 evt.findS32("debug-hit-test-y", &fDebugHitTestLoc.fY);
2262 this->inval(NULL);
2263 return true;
2264 }
2265
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002266 return this->INHERITED::onEvent(evt);
2267}
2268
2269bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002270 if (evt->isType(is_sample_view_tag)) {
2271 return true;
2272 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002273 return this->INHERITED::onQuery(evt);
2274}
2275
reed@google.com64e3eb22011-05-04 14:32:04 +00002276
2277class SimplePC : public SkGPipeController {
2278public:
2279 SimplePC(SkCanvas* target);
2280 ~SimplePC();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002281
reed@google.com64e3eb22011-05-04 14:32:04 +00002282 virtual void* requestBlock(size_t minRequest, size_t* actual);
2283 virtual void notifyWritten(size_t bytes);
2284
2285private:
reed@google.com961ddb02011-05-05 14:03:48 +00002286 SkGPipeReader fReader;
2287 void* fBlock;
2288 size_t fBlockSize;
2289 size_t fBytesWritten;
2290 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00002291 SkGPipeReader::Status fStatus;
2292
2293 size_t fTotalWritten;
2294};
2295
2296SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
2297 fBlock = NULL;
2298 fBlockSize = fBytesWritten = 0;
2299 fStatus = SkGPipeReader::kDone_Status;
2300 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00002301 fAtomsWritten = 0;
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00002302 fReader.setBitmapDecoder(&SkImageDecoder::DecodeMemory);
reed@google.com64e3eb22011-05-04 14:32:04 +00002303}
2304
2305SimplePC::~SimplePC() {
2306// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
reed@google.com0faac1e2011-05-11 05:58:58 +00002307 if (fTotalWritten) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002308 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
2309 fAtomsWritten, fStatus);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002310#ifdef PIPE_FILE
scroggo@google.comb073d922012-06-08 15:35:03 +00002311 //File is open in append mode
2312 FILE* f = fopen(FILE_PATH, "ab");
2313 SkASSERT(f != NULL);
2314 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
2315 fclose(f);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002316#endif
2317#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00002318 if (fAtomsWritten > 1 && fTotalWritten > 4) { //ignore done
2319 gServer.acceptConnections();
2320 gServer.writePacket(fBlock, fTotalWritten);
2321 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002322#endif
reed@google.com0faac1e2011-05-11 05:58:58 +00002323 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002324 sk_free(fBlock);
reed@google.com64e3eb22011-05-04 14:32:04 +00002325}
2326
2327void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
2328 sk_free(fBlock);
2329
2330 fBlockSize = minRequest * 4;
2331 fBlock = sk_malloc_throw(fBlockSize);
2332 fBytesWritten = 0;
2333 *actual = fBlockSize;
2334 return fBlock;
2335}
2336
2337void SimplePC::notifyWritten(size_t bytes) {
2338 SkASSERT(fBytesWritten + bytes <= fBlockSize);
reed@google.com64e3eb22011-05-04 14:32:04 +00002339 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
2340 SkASSERT(SkGPipeReader::kError_Status != fStatus);
2341 fBytesWritten += bytes;
2342 fTotalWritten += bytes;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002343
reed@google.com961ddb02011-05-05 14:03:48 +00002344 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00002345}
2346
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002347void SampleView::draw(SkCanvas* canvas) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002348 if (SkOSMenu::kOffState == fPipeState) {
2349 this->INHERITED::draw(canvas);
2350 } else {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002351 SkGPipeWriter writer;
2352 SimplePC controller(canvas);
scroggo@google.comb073d922012-06-08 15:35:03 +00002353 TiledPipeController tc(canvas->getDevice()->accessBitmap(false),
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00002354 &SkImageDecoder::DecodeMemory,
scroggo@google.comb073d922012-06-08 15:35:03 +00002355 &canvas->getTotalMatrix());
2356 SkGPipeController* pc;
2357 if (SkOSMenu::kMixedState == fPipeState) {
2358 pc = &tc;
2359 } else {
2360 pc = &controller;
2361 }
reed@google.comdde09562011-05-23 12:21:05 +00002362 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002363
scroggo@google.comb073d922012-06-08 15:35:03 +00002364 canvas = writer.startRecording(pc, flags);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002365 //Must draw before controller goes out of scope and sends data
2366 this->INHERITED::draw(canvas);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00002367 //explicitly end recording to ensure writer is flushed before the memory
2368 //is freed in the deconstructor of the controller
2369 writer.endRecording();
reed@google.com0faac1e2011-05-11 05:58:58 +00002370 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002371}
reed@google.com4d5c26d2013-01-08 16:17:50 +00002372
2373#include "SkBounder.h"
2374
2375class DebugHitTestBounder : public SkBounder {
2376public:
2377 DebugHitTestBounder(int x, int y) {
2378 fLoc.set(x, y);
2379 }
2380
2381 virtual bool onIRect(const SkIRect& bounds) SK_OVERRIDE {
2382 if (bounds.contains(fLoc.x(), fLoc.y())) {
2383 //
2384 // Set a break-point here to see what was being drawn under
2385 // the click point (just needed a line of code to stop the debugger)
2386 //
2387 bounds.centerX();
2388 }
2389 return true;
2390 }
2391
2392private:
2393 SkIPoint fLoc;
2394 typedef SkBounder INHERITED;
2395};
2396
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002397void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com81e3d7f2011-06-01 12:42:36 +00002398 this->onDrawBackground(canvas);
2399
reed@google.com4d5c26d2013-01-08 16:17:50 +00002400 DebugHitTestBounder bounder(fDebugHitTestLoc.x(), fDebugHitTestLoc.y());
2401 if (fDebugHitTest) {
2402 canvas->setBounder(&bounder);
2403 }
2404
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002405 for (int i = 0; i < fRepeatCount; i++) {
scroggo@google.com85cade02012-08-17 13:29:50 +00002406 SkAutoCanvasRestore acr(canvas, true);
2407 this->onDrawContent(canvas);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002408 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002409
2410 fDebugHitTest = false;
2411 canvas->setBounder(NULL);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002412}
2413
2414void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00002415 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002416}
2417
2418///////////////////////////////////////////////////////////////////////////////
2419
reed@android.comf2b98d62010-12-20 18:26:13 +00002420template <typename T> void SkTBSort(T array[], int count) {
2421 for (int i = 1; i < count - 1; i++) {
2422 bool didSwap = false;
2423 for (int j = count - 1; j > i; --j) {
2424 if (array[j] < array[j-1]) {
2425 T tmp(array[j-1]);
2426 array[j-1] = array[j];
2427 array[j] = tmp;
2428 didSwap = true;
2429 }
2430 }
2431 if (!didSwap) {
2432 break;
2433 }
2434 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002435
reed@android.comf2b98d62010-12-20 18:26:13 +00002436 for (int k = 0; k < count - 1; k++) {
2437 SkASSERT(!(array[k+1] < array[k]));
2438 }
2439}
2440
2441#include "SkRandom.h"
2442
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002443static void rand_rect(SkIRect* rect, SkRandom& rand) {
reed@android.comf2b98d62010-12-20 18:26:13 +00002444 int bits = 8;
2445 int shift = 32 - bits;
2446 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
2447 rand.nextU() >> shift, rand.nextU() >> shift);
2448 rect->sort();
2449}
2450
2451static void dumpRect(const SkIRect& r) {
2452 SkDebugf(" { %d, %d, %d, %d },\n",
2453 r.fLeft, r.fTop,
2454 r.fRight, r.fBottom);
2455}
2456
2457static void test_rects(const SkIRect rect[], int count) {
2458 SkRegion rgn0, rgn1;
2459
2460 for (int i = 0; i < count; i++) {
2461 rgn0.op(rect[i], SkRegion::kUnion_Op);
2462 // dumpRect(rect[i]);
2463 }
2464 rgn1.setRects(rect, count);
2465
2466 if (rgn0 != rgn1) {
2467 SkDebugf("\n");
2468 for (int i = 0; i < count; i++) {
2469 dumpRect(rect[i]);
2470 }
2471 SkDebugf("\n");
2472 }
2473}
2474
2475static void test() {
2476 size_t i;
2477
2478 const SkIRect r0[] = {
2479 { 0, 0, 1, 1 },
2480 { 2, 2, 3, 3 },
2481 };
2482 const SkIRect r1[] = {
2483 { 0, 0, 1, 3 },
2484 { 1, 1, 2, 2 },
2485 { 2, 0, 3, 3 },
2486 };
2487 const SkIRect r2[] = {
2488 { 0, 0, 1, 2 },
2489 { 2, 1, 3, 3 },
2490 { 4, 0, 5, 1 },
2491 { 6, 0, 7, 4 },
2492 };
2493
2494 static const struct {
2495 const SkIRect* fRects;
2496 int fCount;
2497 } gRecs[] = {
2498 { r0, SK_ARRAY_COUNT(r0) },
2499 { r1, SK_ARRAY_COUNT(r1) },
2500 { r2, SK_ARRAY_COUNT(r2) },
2501 };
2502
2503 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
2504 test_rects(gRecs[i].fRects, gRecs[i].fCount);
2505 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002506
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002507 SkRandom rand;
reed@android.comf2b98d62010-12-20 18:26:13 +00002508 for (i = 0; i < 10000; i++) {
2509 SkRegion rgn0, rgn1;
2510
2511 const int N = 8;
2512 SkIRect rect[N];
2513 for (int j = 0; j < N; j++) {
2514 rand_rect(&rect[j], rand);
2515 }
2516 test_rects(rect, N);
2517 }
2518}
2519
caryclark@google.com02939ce2012-06-06 12:09:51 +00002520// FIXME: this should be in a header
2521SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00002522SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
caryclark@google.com02939ce2012-06-06 12:09:51 +00002523 if (false) { // avoid bit rot, suppress warning
2524 test();
2525 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002526 return new SampleWindow(hwnd, argc, argv, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002527}
2528
caryclark@google.com02939ce2012-06-06 12:09:51 +00002529// FIXME: this should be in a header
2530void get_preferred_size(int* x, int* y, int* width, int* height);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002531void get_preferred_size(int* x, int* y, int* width, int* height) {
2532 *x = 10;
2533 *y = 50;
2534 *width = 640;
2535 *height = 480;
2536}
2537
caryclark@google.com5987f582012-10-02 18:33:14 +00002538#ifdef SK_BUILD_FOR_IOS
2539void save_args(int argc, char *argv[]) {
2540}
2541#endif
2542
caryclark@google.com02939ce2012-06-06 12:09:51 +00002543// FIXME: this should be in a header
2544void application_init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002545void application_init() {
2546// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002547#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00002548 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002549#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002550 SkGraphics::Init();
2551 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002552}
2553
caryclark@google.com02939ce2012-06-06 12:09:51 +00002554// FIXME: this should be in a header
2555void application_term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002556void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002557 SkEvent::Term();
2558 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002559}