blob: 795f8021da9a6870e2de37e35de684afdcbff66f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Scroggo2c8208f2011-06-15 16:49:08 +00007#include "SampleApp.h"
8
reed@google.com8a85d0c2011-06-24 19:12:12 +00009#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkCanvas.h"
11#include "SkDevice.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkGraphics.h"
scroggo@google.com74b7ffd2013-04-30 02:32:41 +000013#include "SkImageDecoder.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +000014#include "SkImageEncoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkPaint.h"
16#include "SkPicture.h"
17#include "SkStream.h"
edisonn@google.comf8b6b012013-07-11 14:28:04 +000018#include "SkTSort.h"
reed@android.com44177402009-11-23 21:07:51 +000019#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkWindow.h"
21
22#include "SampleCode.h"
Scroggo0f185c22011-03-24 18:35:50 +000023#include "SkTypeface.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000024
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000025#if SK_SUPPORT_GPU
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000026#include "gl/GrGLInterface.h"
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000027#include "gl/GrGLUtil.h"
bsalomon@google.com583a1e32011-08-17 13:42:46 +000028#include "GrRenderTarget.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000029#include "GrContext.h"
30#include "SkGpuDevice.h"
31#else
32class GrContext;
33#endif
Scroggo2c8208f2011-06-15 16:49:08 +000034
reed@google.com1830c7a2012-06-04 12:05:43 +000035#include "SkOSFile.h"
Scroggo8ac0d542011-06-21 14:44:57 +000036#include "SkPDFDevice.h"
37#include "SkPDFDocument.h"
38#include "SkStream.h"
39
scroggo@google.comb073d922012-06-08 15:35:03 +000040#include "SkGPipe.h"
41#include "SamplePipeControllers.h"
tfarina@chromium.orgb1b7f7072012-09-18 01:52:20 +000042#include "OverView.h"
epoger@google.com6a121782012-09-14 18:42:01 +000043#include "TransitionView.h"
scroggo@google.comb073d922012-06-08 15:35:03 +000044
reed@google.com1830c7a2012-06-04 12:05:43 +000045extern SampleView* CreateSamplePictFileView(const char filename[]);
46
47class PictFileFactory : public SkViewFactory {
48 SkString fFilename;
49public:
50 PictFileFactory(const SkString& filename) : fFilename(filename) {}
51 virtual SkView* operator() () const SK_OVERRIDE {
52 return CreateSamplePictFileView(fFilename.c_str());
53 }
54};
55
edisonn@google.comf8b6b012013-07-11 14:28:04 +000056#ifdef SAMPLE_PDF_FILE_VIEWER
57extern SampleView* CreateSamplePdfFileViewer(const char filename[]);
58
59class PdfFileViewerFactory : public SkViewFactory {
60 SkString fFilename;
61public:
62 PdfFileViewerFactory(const SkString& filename) : fFilename(filename) {}
63 virtual SkView* operator() () const SK_OVERRIDE {
64 return CreateSamplePdfFileViewer(fFilename.c_str());
65 }
66};
67#endif // SAMPLE_PDF_FILE_VIEWER
68
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000069#define PIPE_FILEx
70#ifdef PIPE_FILE
yangsu@google.com1f394212011-06-01 18:03:34 +000071#define FILE_PATH "/path/to/drawing.data"
72#endif
73
yangsu@google.comf121b052011-08-08 16:02:51 +000074#define PIPE_NETx
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000075#ifdef PIPE_NET
76#include "SkSockets.h"
77SkTCPServer gServer;
78#endif
yangsu@google.comef7bdfa2011-08-12 14:27:47 +000079
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +000080#define USE_ARROWS_FOR_ZOOM true
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000081
82#if SK_ANGLE
83//#define DEFAULT_TO_ANGLE 1
84#else
bsalomon@google.com82d12232013-09-09 15:36:26 +000085#define DEFAULT_TO_GPU 0 // if 1 default rendering is on GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000086#endif
reed@android.comf2b98d62010-12-20 18:26:13 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088#define ANIMATING_EVENTTYPE "nextSample"
reed@google.com84cfce12013-05-29 19:22:20 +000089#define ANIMATING_DELAY 250
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000091#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +000092 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000093#else
reed@google.combad8c872011-05-18 20:10:31 +000094 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000095#endif
reed@google.combad8c872011-05-18 20:10:31 +000096#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000097
reed@google.com3cec4d72011-07-06 13:59:47 +000098static SampleWindow* gSampleWindow;
99
reed@google.coma4f81372012-11-15 15:56:38 +0000100static bool gShowGMBounds;
101
reed@google.com2072db82011-11-08 15:18:10 +0000102static void postEventToSink(SkEvent* evt, SkEventSink* sink) {
103 evt->setTargetID(sink->getSinkID())->post();
104}
105
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000106///////////////////////////////////////////////////////////////////////////////
107
108static const char* skip_until(const char* str, const char* skip) {
109 if (!str) {
110 return NULL;
111 }
112 return strstr(str, skip);
113}
114
115static const char* skip_past(const char* str, const char* skip) {
116 const char* found = skip_until(str, skip);
117 if (!found) {
118 return NULL;
119 }
120 return found + strlen(skip);
121}
122
123static const char* gPrefFileName = "sampleapp_prefs.txt";
124
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000125static bool readTitleFromPrefs(SkString* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000126 SkFILEStream stream(gPrefFileName);
127 if (!stream.isValid()) {
128 return false;
129 }
130
131 int len = stream.getLength();
132 SkString data(len);
133 stream.read(data.writable_str(), len);
134 const char* s = data.c_str();
135
136 s = skip_past(s, "curr-slide-title");
137 s = skip_past(s, "=");
138 s = skip_past(s, "\"");
139 const char* stop = skip_until(s, "\"");
140 if (stop > s) {
141 title->set(s, stop - s);
142 return true;
143 }
144 return false;
145}
146
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000147static void writeTitleToPrefs(const char* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000148 SkFILEWStream stream(gPrefFileName);
149 SkString data;
150 data.printf("curr-slide-title = \"%s\"\n", title);
151 stream.write(data.c_str(), data.size());
152}
153
154///////////////////////////////////////////////////////////////////////////////
155
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000156class SampleWindow::DefaultDeviceManager : public SampleWindow::DeviceManager {
157public:
158
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000159 DefaultDeviceManager() {
160#if SK_SUPPORT_GPU
161 fCurContext = NULL;
162 fCurIntf = NULL;
163 fCurRenderTarget = NULL;
164 fMSAASampleCount = 0;
165#endif
166 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000167 }
168
169 virtual ~DefaultDeviceManager() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000170#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000171 SkSafeUnref(fCurContext);
172 SkSafeUnref(fCurIntf);
173 SkSafeUnref(fCurRenderTarget);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000174#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000175 }
176
bsalomon@google.com11959252012-04-06 20:13:38 +0000177 virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000178 SkASSERT(kNone_BackEndType == fBackend);
robertphillips@google.com53e96a12012-03-30 14:47:53 +0000179
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000180 fBackend = kNone_BackEndType;
181
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000182#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000183 switch (win->getDeviceType()) {
184 case kRaster_DeviceType:
185 // fallthrough
186 case kPicture_DeviceType:
187 // fallthrough
188 case kGPU_DeviceType:
189 // fallthrough
190 case kNullGPU_DeviceType:
191 // all these guys use the native backend
192 fBackend = kNativeGL_BackEndType;
193 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000194#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000195 case kANGLE_DeviceType:
196 // ANGLE is really the only odd man out
197 fBackend = kANGLE_BackEndType;
198 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000199#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000200 default:
201 SkASSERT(false);
202 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000203 }
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000204 AttachmentInfo attachmentInfo;
205 bool result = win->attach(fBackend, msaaSampleCount, &attachmentInfo);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000206 if (!result) {
bsalomon@google.com74913722011-10-27 20:44:19 +0000207 SkDebugf("Failed to initialize GL");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000208 return;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000209 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000210 fMSAASampleCount = msaaSampleCount;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000211
212 SkASSERT(NULL == fCurIntf);
213 switch (win->getDeviceType()) {
214 case kRaster_DeviceType:
215 // fallthrough
216 case kPicture_DeviceType:
217 // fallthrough
218 case kGPU_DeviceType:
219 // all these guys use the native interface
220 fCurIntf = GrGLCreateNativeInterface();
221 break;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000222#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000223 case kANGLE_DeviceType:
224 fCurIntf = GrGLCreateANGLEInterface();
225 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000226#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000227 case kNullGPU_DeviceType:
228 fCurIntf = GrGLCreateNullInterface();
229 break;
230 default:
231 SkASSERT(false);
232 break;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000233 }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000234
235 SkASSERT(NULL == fCurContext);
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000236 fCurContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000237
238 if (NULL == fCurContext || NULL == fCurIntf) {
239 // We need some context and interface to see results
240 SkSafeUnref(fCurContext);
241 SkSafeUnref(fCurIntf);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000242 SkDebugf("Failed to setup 3D");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000243
244 win->detach();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000245 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000246#endif // SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000247 // call windowSizeChanged to create the render target
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000248 this->windowSizeChanged(win);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000249 }
250
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000251 virtual void tearDownBackend(SampleWindow *win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000252#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000253 SkSafeUnref(fCurContext);
254 fCurContext = NULL;
255
256 SkSafeUnref(fCurIntf);
257 fCurIntf = NULL;
258
259 SkSafeUnref(fCurRenderTarget);
260 fCurRenderTarget = NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000261#endif
bsalomon@google.comddd40e52012-04-10 15:56:29 +0000262 win->detach();
263 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000264 }
265
reed@google.com5957f472012-10-01 20:31:56 +0000266 virtual SkCanvas* createCanvas(SampleWindow::DeviceType dType,
267 SampleWindow* win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000268#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +0000269 if (IsGpuDeviceType(dType) && NULL != fCurContext) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000270 SkAutoTUnref<SkBaseDevice> device(new SkGpuDevice(fCurContext, fCurRenderTarget));
bsalomon@google.com82502e22013-01-24 20:47:18 +0000271 return new SkCanvas(device);
272 } else
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000273#endif
bsalomon@google.com82502e22013-01-24 20:47:18 +0000274 {
275 return NULL;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000276 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000277 }
278
279 virtual void publishCanvas(SampleWindow::DeviceType dType,
280 SkCanvas* canvas,
281 SampleWindow* win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000282#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000283 if (fCurContext) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000284 // in case we have queued drawing calls
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000285 fCurContext->flush();
286
bsalomon@google.com82502e22013-01-24 20:47:18 +0000287 if (!IsGpuDeviceType(dType)) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000288 // need to send the raster bits to the (gpu) window
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000289 fCurContext->setRenderTarget(fCurRenderTarget);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000290 const SkBitmap& bm = win->getBitmap();
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000291 fCurRenderTarget->writePixels(0, 0, bm.width(), bm.height(),
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000292 kSkia8888_GrPixelConfig,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000293 bm.getPixels(),
294 bm.rowBytes());
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000295 }
296 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000297#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000298
299 win->present();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000300 }
301
302 virtual void windowSizeChanged(SampleWindow* win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000303#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000304 if (fCurContext) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000305 AttachmentInfo attachmentInfo;
306 win->attach(fBackend, fMSAASampleCount, &attachmentInfo);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000307
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000308 GrBackendRenderTargetDesc desc;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000309 desc.fWidth = SkScalarRound(win->width());
310 desc.fHeight = SkScalarRound(win->height());
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000311 desc.fConfig = kSkia8888_GrPixelConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000312 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000313 desc.fSampleCnt = attachmentInfo.fSampleCount;
314 desc.fStencilBits = attachmentInfo.fStencilBits;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000315 GrGLint buffer;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000316 GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
bsalomon@google.come269f212011-11-07 13:29:52 +0000317 desc.fRenderTargetHandle = buffer;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000318
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000319 SkSafeUnref(fCurRenderTarget);
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000320 fCurRenderTarget = fCurContext->wrapBackendRenderTarget(desc);
bsalomon@google.com74913722011-10-27 20:44:19 +0000321 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000322#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000323 }
324
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000325 virtual GrContext* getGrContext() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000326#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000327 return fCurContext;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000328#else
329 return NULL;
330#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000331 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000332
333 virtual GrRenderTarget* getGrRenderTarget() SK_OVERRIDE {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000334#if SK_SUPPORT_GPU
bsalomon@google.com11959252012-04-06 20:13:38 +0000335 return fCurRenderTarget;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000336#else
337 return NULL;
338#endif
bsalomon@google.com11959252012-04-06 20:13:38 +0000339 }
340
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000341private:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000342
343#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000344 GrContext* fCurContext;
345 const GrGLInterface* fCurIntf;
346 GrRenderTarget* fCurRenderTarget;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000347 int fMSAASampleCount;
348#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000349
350 SkOSWindow::SkBackEndTypes fBackend;
351
352 typedef SampleWindow::DeviceManager INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000353};
354
355///////////////
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000356static const char view_inval_msg[] = "view-inval-msg";
357
Scroggo62b65b02011-06-21 16:01:26 +0000358void SampleWindow::postInvalDelay() {
reed@google.com87fac4a2011-08-04 13:50:17 +0000359 (new SkEvent(view_inval_msg, this->getSinkID()))->postDelay(1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000360}
361
362static bool isInvalEvent(const SkEvent& evt) {
363 return evt.isType(view_inval_msg);
364}
365//////////////////
366
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000367SkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func)
368 : fCreateFunc(func) {
369}
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000370
bsalomon@google.com8301de12011-10-31 16:47:13 +0000371SkView* SkFuncViewFactory::operator() () const {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000372 return (*fCreateFunc)();
373}
374
375#include "GMSampleView.h"
376
377SkGMSampleViewFactory::SkGMSampleViewFactory(GMFactoryFunc func)
378 : fFunc(func) {
379}
380
381SkView* SkGMSampleViewFactory::operator() () const {
382 return new GMSampleView(fFunc(NULL));
383}
384
385SkViewRegister* SkViewRegister::gHead;
386SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) {
387 fFact->ref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000388 fChain = gHead;
389 gHead = this;
390}
391
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000392SkViewRegister::SkViewRegister(SkViewCreateFunc func) {
393 fFact = new SkFuncViewFactory(func);
394 fChain = gHead;
395 gHead = this;
396}
397
398SkViewRegister::SkViewRegister(GMFactoryFunc func) {
399 fFact = new SkGMSampleViewFactory(func);
400 fChain = gHead;
401 gHead = this;
402}
403
404class AutoUnrefArray {
405public:
406 AutoUnrefArray() {}
407 ~AutoUnrefArray() {
408 int count = fObjs.count();
409 for (int i = 0; i < count; ++i) {
410 fObjs[i]->unref();
411 }
412 }
413 SkRefCnt*& push_back() { return *fObjs.append(); }
chudy@google.com4605a3f2012-08-01 17:58:01 +0000414
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000415private:
416 SkTDArray<SkRefCnt*> fObjs;
417};
418
419// registers GMs as Samples
420// This can't be performed during static initialization because it could be
421// run before GMRegistry has been fully built.
caryclark@google.com02939ce2012-06-06 12:09:51 +0000422static void SkGMRegistyToSampleRegistry() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000423 static bool gOnce;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000424 static AutoUnrefArray fRegisters;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000425
426 if (!gOnce) {
427 const skiagm::GMRegistry* gmreg = skiagm::GMRegistry::Head();
428 while (gmreg) {
429 fRegisters.push_back() = new SkViewRegister(gmreg->factory());
430 gmreg = gmreg->next();
431 }
432 gOnce = true;
433 }
434}
435
reed@google.com29038ed2011-07-06 17:56:47 +0000436#if 0
reed@google.comf0b5f682011-03-11 20:08:25 +0000437#include <CoreFoundation/CoreFoundation.h>
438#include <CoreFoundation/CFURLAccess.h>
439
440static void testpdf() {
441 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
442 kCFStringEncodingUTF8);
443 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
444 kCFURLPOSIXPathStyle,
445 false);
446 CFRelease(path);
447 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
448 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
449 CFRelease(url);
450
451 CGContextBeginPage(cg, &box);
452 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
453 CGContextFillEllipseInRect(cg, r);
454 CGContextEndPage(cg);
455 CGContextRelease(cg);
456
457 if (false) {
458 SkBitmap bm;
459 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
460 bm.allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +0000461 bm.eraseColor(SK_ColorTRANSPARENT);
reed@google.comf0b5f682011-03-11 20:08:25 +0000462
463 SkCanvas canvas(bm);
464
465 }
466}
467#endif
468
469//////////////////////////////////////////////////////////////////////////////
470
reed@google.com569e0432011-04-05 13:07:03 +0000471enum FlipAxisEnum {
472 kFlipAxis_X = (1 << 0),
473 kFlipAxis_Y = (1 << 1)
474};
475
reed@google.comf0b5f682011-03-11 20:08:25 +0000476#include "SkDrawFilter.h"
477
bungeman@google.com96aabc82013-06-03 21:26:34 +0000478struct HintingState {
479 SkPaint::Hinting hinting;
480 const char* name;
481 const char* label;
482};
483static HintingState gHintingStates[] = {
484 {SkPaint::kNo_Hinting, "Mixed", NULL },
485 {SkPaint::kNo_Hinting, "None", "H0 " },
486 {SkPaint::kSlight_Hinting, "Slight", "Hs " },
487 {SkPaint::kNormal_Hinting, "Normal", "Hn " },
488 {SkPaint::kFull_Hinting, "Full", "Hf " },
489};
490
reed@google.com569e0432011-04-05 13:07:03 +0000491class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000492public:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000493 FlagsDrawFilter(SkOSMenu::TriState lcd, SkOSMenu::TriState aa, SkOSMenu::TriState filter,
bungeman@google.com96aabc82013-06-03 21:26:34 +0000494 SkOSMenu::TriState subpixel, int hinting)
495 : fLCDState(lcd), fAAState(aa), fFilterState(filter), fSubpixelState(subpixel)
496 , fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000497
reed@google.com971aca72012-11-26 20:26:54 +0000498 virtual bool filter(SkPaint* paint, Type t) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000499 if (kText_Type == t && SkOSMenu::kMixedState != fLCDState) {
500 paint->setLCDRenderText(SkOSMenu::kOnState == fLCDState);
reed@google.com569e0432011-04-05 13:07:03 +0000501 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000502 if (SkOSMenu::kMixedState != fAAState) {
503 paint->setAntiAlias(SkOSMenu::kOnState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000504 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000505 if (SkOSMenu::kMixedState != fFilterState) {
reed@google.com44699382013-10-31 17:28:30 +0000506 paint->setFilterLevel(SkOSMenu::kOnState == fFilterState ?
507 SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
reed@google.com176753a2011-05-17 15:32:04 +0000508 }
bungeman@google.com96aabc82013-06-03 21:26:34 +0000509 if (SkOSMenu::kMixedState != fSubpixelState) {
510 paint->setSubpixelText(SkOSMenu::kOnState == fSubpixelState);
511 }
bungeman@google.com055aa522013-06-03 21:35:03 +0000512 if (0 != fHintingState && fHintingState < (int)SK_ARRAY_COUNT(gHintingStates)) {
bungeman@google.com96aabc82013-06-03 21:26:34 +0000513 paint->setHinting(gHintingStates[fHintingState].hinting);
reed@google.com09e3baa2011-05-18 12:04:31 +0000514 }
reed@google.com971aca72012-11-26 20:26:54 +0000515 return true;
reed@google.comf0b5f682011-03-11 20:08:25 +0000516 }
517
518private:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000519 SkOSMenu::TriState fLCDState;
520 SkOSMenu::TriState fAAState;
521 SkOSMenu::TriState fFilterState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000522 SkOSMenu::TriState fSubpixelState;
523 int fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000524};
525
reed@android.com8a1c16f2008-12-17 15:59:43 +0000526//////////////////////////////////////////////////////////////////////////////
527
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000528#define MAX_ZOOM_LEVEL 8
529#define MIN_ZOOM_LEVEL -8
530
reed@android.comf2b98d62010-12-20 18:26:13 +0000531static const char gCharEvtName[] = "SampleCode_Char_Event";
532static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000533static const char gTitleEvtName[] = "SampleCode_Title_Event";
534static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000535static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
reed@google.com2072db82011-11-08 15:18:10 +0000536static const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle";
reed@android.comf2b98d62010-12-20 18:26:13 +0000537
538bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
539 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
540 if (outUni) {
541 *outUni = evt.getFast32();
542 }
543 return true;
544 }
545 return false;
546}
547
548bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
549 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
550 if (outKey) {
551 *outKey = (SkKey)evt.getFast32();
552 }
553 return true;
554 }
555 return false;
556}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000557
558bool SampleCode::TitleQ(const SkEvent& evt) {
559 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
560}
561
562void SampleCode::TitleR(SkEvent* evt, const char title[]) {
563 SkASSERT(evt && TitleQ(*evt));
564 evt->setString(gTitleEvtName, title);
565}
566
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000567bool SampleCode::RequestTitle(SkView* view, SkString* title) {
568 SkEvent evt(gTitleEvtName);
569 if (view->doQuery(&evt)) {
570 title->set(evt.findString(gTitleEvtName));
571 return true;
572 }
573 return false;
574}
575
reed@android.com8a1c16f2008-12-17 15:59:43 +0000576bool SampleCode::PrefSizeQ(const SkEvent& evt) {
577 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
578}
579
580void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
581 SkASSERT(evt && PrefSizeQ(*evt));
582 SkScalar size[2];
583 size[0] = width;
584 size[1] = height;
585 evt->setScalars(gPrefSizeEvtName, 2, size);
586}
587
reed@android.comf2b98d62010-12-20 18:26:13 +0000588bool SampleCode::FastTextQ(const SkEvent& evt) {
589 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
590}
591
592///////////////////////////////////////////////////////////////////////////////
593
reed@android.com44177402009-11-23 21:07:51 +0000594static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000595static SkMSec gAnimTimePrev;
596
reed@android.com44177402009-11-23 21:07:51 +0000597SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000598SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
599SkScalar SampleCode::GetAnimSecondsDelta() {
600 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
601}
reed@android.com44177402009-11-23 21:07:51 +0000602
603SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000604 // since gAnimTime can be up to 32 bits, we can't convert it to a float
605 // or we'll lose the low bits. Hence we use doubles for the intermediate
606 // calculations
607 double seconds = (double)gAnimTime / 1000.0;
608 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000609 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000610 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000611 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000612 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000613}
614
bsalomon@google.com85003222012-03-28 14:44:37 +0000615SkScalar SampleCode::GetAnimSinScalar(SkScalar amplitude,
616 SkScalar periodInSec,
617 SkScalar phaseInSec) {
618 if (!periodInSec) {
619 return 0;
620 }
621 double t = (double)gAnimTime / 1000.0 + phaseInSec;
622 t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
623 amplitude = SK_ScalarHalf * amplitude;
624 return SkScalarMul(amplitude, SkDoubleToScalar(sin(t))) + amplitude;
625}
626
reed@google.com3cec4d72011-07-06 13:59:47 +0000627GrContext* SampleCode::GetGr() {
628 return gSampleWindow ? gSampleWindow->getGrContext() : NULL;
629}
630
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000631// some GMs rely on having a skiagm::GetGr function defined
632namespace skiagm {
caryclark@google.com02939ce2012-06-06 12:09:51 +0000633 // FIXME: this should be moved into a header
634 GrContext* GetGr();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000635 GrContext* GetGr() { return SampleCode::GetGr(); }
636}
637
reed@android.com8a1c16f2008-12-17 15:59:43 +0000638//////////////////////////////////////////////////////////////////////////////
639
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000640enum TilingMode {
641 kNo_Tiling,
642 kAbs_128x128_Tiling,
643 kAbs_256x256_Tiling,
644 kRel_4x4_Tiling,
645 kRel_1x16_Tiling,
646 kRel_16x1_Tiling,
647
648 kLast_TilingMode_Enum
649};
650
651struct TilingInfo {
652 const char* label;
653 SkScalar w, h;
654};
655
656static struct TilingInfo gTilingInfo[] = {
657 { "No tiling", SK_Scalar1 , SK_Scalar1 }, // kNo_Tiling
658 { "128x128" , SkIntToScalar(128), SkIntToScalar(128) }, // kAbs_128x128_Tiling
659 { "256x256" , SkIntToScalar(256), SkIntToScalar(256) }, // kAbs_256x256_Tiling
660 { "1/4x1/4" , SK_Scalar1 / 4 , SK_Scalar1 / 4 }, // kRel_4x4_Tiling
661 { "1/1x1/16" , SK_Scalar1 , SK_Scalar1 / 16 }, // kRel_1x16_Tiling
662 { "1/16x1/1" , SK_Scalar1 / 16 , SK_Scalar1 }, // kRel_16x1_Tiling
663};
664SK_COMPILE_ASSERT((SK_ARRAY_COUNT(gTilingInfo) == kLast_TilingMode_Enum),
665 Incomplete_tiling_labels);
666
667//////////////////////////////////////////////////////////////////////////////
668
reed@android.comf2b98d62010-12-20 18:26:13 +0000669static SkView* curr_view(SkWindow* wind) {
670 SkView::F2BIter iter(wind);
671 return iter.next();
672}
673
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000674static bool curr_title(SkWindow* wind, SkString* title) {
675 SkView* view = curr_view(wind);
676 if (view) {
677 SkEvent evt(gTitleEvtName);
678 if (view->doQuery(&evt)) {
679 title->set(evt.findString(gTitleEvtName));
680 return true;
681 }
682 }
683 return false;
684}
685
Scroggo2c8208f2011-06-15 16:49:08 +0000686void SampleWindow::setZoomCenter(float x, float y)
687{
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000688 fZoomCenterX = x;
689 fZoomCenterY = y;
Scroggo2c8208f2011-06-15 16:49:08 +0000690}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000691
Scroggo0f185c22011-03-24 18:35:50 +0000692bool SampleWindow::zoomIn()
693{
694 // Arbitrarily decided
695 if (fFatBitsScale == 25) return false;
696 fFatBitsScale++;
697 this->inval(NULL);
698 return true;
699}
700
701bool SampleWindow::zoomOut()
702{
703 if (fFatBitsScale == 1) return false;
704 fFatBitsScale--;
705 this->inval(NULL);
706 return true;
707}
708
Scroggo0f185c22011-03-24 18:35:50 +0000709void SampleWindow::updatePointer(int x, int y)
710{
711 fMouseX = x;
712 fMouseY = y;
713 if (fShowZoomer) {
714 this->inval(NULL);
715 }
716}
717
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000718static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
719 static const SampleWindow::DeviceType gCT[] = {
720 SampleWindow::kPicture_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000721#if SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000722 SampleWindow::kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000723#if SK_ANGLE
724 SampleWindow::kANGLE_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000725#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +0000726 SampleWindow::kRaster_DeviceType, // skip the null gpu device in normal cycling
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000727#endif // SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000728 SampleWindow::kRaster_DeviceType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000729 };
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000730 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gCT) == SampleWindow::kDeviceTypeCnt, array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000731 return gCT[ct];
732}
733
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000734static void usage(const char * argv0) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000735 SkDebugf("%s [--slide sampleName] [-i resourcePath] [--msaa sampleCount] [--pictureDir dirPath] [--picture path] [--sort]\n", argv0);
736#ifdef SAMPLE_PDF_FILE_VIEWER
737 SkDebugf(" [--pdfDir pdfPath]\n");
738 SkDebugf(" pdfPath: path to directory pdf files are read from\n");
739#endif // SAMPLE_PDF_FILE_VIEWER
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000740 SkDebugf(" sampleName: sample at which to start.\n");
741 SkDebugf(" resourcePath: directory that stores image resources.\n");
bsalomon@google.com11959252012-04-06 20:13:38 +0000742 SkDebugf(" msaa: request multisampling with the given sample count.\n");
chudy@google.com4605a3f2012-08-01 17:58:01 +0000743 SkDebugf(" dirPath: path to directory skia pictures are read from\n");
744 SkDebugf(" path: path to skia picture\n");
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000745 SkDebugf(" --sort: sort samples by title, this would help to compare pdf rendering (P:foo.pdf) with skp rendering (P:foo.pdf)\n");
746}
747
748static SkString getSampleTitle(const SkViewFactory* sampleFactory) {
749 SkView* view = (*sampleFactory)();
750 SkString title;
751 SampleCode::RequestTitle(view, &title);
752 view->unref();
753 return title;
754}
755
edisonn@google.comde36b692013-07-11 15:40:31 +0000756static bool compareSampleTitle(const SkViewFactory* first, const SkViewFactory* second) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000757 return strcmp(getSampleTitle(first).c_str(), getSampleTitle(second).c_str()) < 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000758}
759
chudy@google.com4605a3f2012-08-01 17:58:01 +0000760SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager)
robertphillips@google.com4750fa52012-04-10 13:34:11 +0000761 : INHERITED(hwnd)
762 , fDevManager(NULL) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000763
reed@google.com7d05ac92013-06-06 13:14:13 +0000764 fCurrIndex = -1;
765
reed@google.com1830c7a2012-06-04 12:05:43 +0000766 this->registerPictFileSamples(argv, argc);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000767 this->registerPictFileSample(argv, argc);
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000768#ifdef SAMPLE_PDF_FILE_VIEWER
769 this->registerPdfFileViewerSamples(argv, argc);
770#endif // SAMPLE_PDF_FILE_VIEWER
robertphillips@google.com76d40212012-03-22 14:12:15 +0000771 SkGMRegistyToSampleRegistry();
772 {
773 const SkViewRegister* reg = SkViewRegister::Head();
774 while (reg) {
775 *fSamples.append() = reg->factory();
776 reg = reg->next();
777 }
778 }
779
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000780 bool sort = false;
781 for (int i = 0; i < argc; ++i) {
782 if (!strcmp(argv[i], "--sort")) {
783 sort = true;
784 break;
785 }
786 }
787
788 if (sort) {
789 // Sort samples, so foo.skp and foo.pdf are consecutive and we can quickly spot where
790 // skp -> pdf -> png fails.
791 SkTQSort(fSamples.begin(), fSamples.end() ? fSamples.end() - 1 : NULL, compareSampleTitle);
792 }
793
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000794 const char* resourcePath = NULL;
bsalomon@google.com11959252012-04-06 20:13:38 +0000795 fMSAASampleCount = 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000796
797 const char* const commandName = argv[0];
798 char* const* stop = argv + argc;
799 for (++argv; argv < stop; ++argv) {
reed@google.com55ebe8e2013-09-10 19:12:07 +0000800 if (!strcmp(*argv, "-i") || !strcmp(*argv, "--resourcePath")) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000801 argv++;
802 if (argv < stop && **argv) {
803 resourcePath = *argv;
804 }
robertphillips@google.com76d40212012-03-22 14:12:15 +0000805 } else if (strcmp(*argv, "--slide") == 0) {
806 argv++;
807 if (argv < stop && **argv) {
808 fCurrIndex = findByTitle(*argv);
809 if (fCurrIndex < 0) {
810 fprintf(stderr, "Unknown sample \"%s\"\n", *argv);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000811 listTitles();
robertphillips@google.com76d40212012-03-22 14:12:15 +0000812 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000813 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000814 } else if (strcmp(*argv, "--msaa") == 0) {
815 ++argv;
816 if (argv < stop && **argv) {
817 fMSAASampleCount = atoi(*argv);
818 }
robertphillips@google.com7265e722012-05-03 18:22:28 +0000819 } else if (strcmp(*argv, "--list") == 0) {
820 listTitles();
halcanary@google.com805ca192013-10-14 12:47:37 +0000821 } else if (strcmp(*argv, "--pictureDir") == 0) {
822 ++argv; // This case is dealt with in registerPictFileSamples().
823 } else if (strcmp(*argv, "--picture") == 0) {
824 ++argv; // This case is dealt with in registerPictFileSample().
bsalomon@google.com11959252012-04-06 20:13:38 +0000825 }
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000826 else {
robertphillips@google.com76d40212012-03-22 14:12:15 +0000827 usage(commandName);
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000828 }
829 }
830
831 if (fCurrIndex < 0) {
832 SkString title;
833 if (readTitleFromPrefs(&title)) {
834 fCurrIndex = findByTitle(title.c_str());
835 }
836 }
837
838 if (fCurrIndex < 0) {
839 fCurrIndex = 0;
840 }
841
reed@google.com3cec4d72011-07-06 13:59:47 +0000842 gSampleWindow = this;
843
yangsu@google.com1f394212011-06-01 18:03:34 +0000844#ifdef PIPE_FILE
845 //Clear existing file or create file if it doesn't exist
846 FILE* f = fopen(FILE_PATH, "wb");
847 fclose(f);
848#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +0000849
reed@android.com8a1c16f2008-12-17 15:59:43 +0000850 fPicture = NULL;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000851
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000852 fDeviceType = kRaster_DeviceType;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000853
854#if DEFAULT_TO_GPU
855 fDeviceType = kGPU_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000856#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000857#if SK_ANGLE && DEFAULT_TO_ANGLE
858 fDeviceType = kANGLE_DeviceType;
859#endif
860
reed@android.com8a1c16f2008-12-17 15:59:43 +0000861 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000862 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000863 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000864 fRotate = false;
bungeman@google.comb1785912013-07-09 21:58:56 +0000865 fRotateAnimTime = 0;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000866 fPerspAnim = false;
867 fPerspAnimTime = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +0000868 fRequestGrabImage = false;
scroggo@google.comb073d922012-06-08 15:35:03 +0000869 fPipeState = SkOSMenu::kOffState;
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000870 fTilingMode = kNo_Tiling;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000871 fMeasureFPS = false;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000872 fLCDState = SkOSMenu::kMixedState;
873 fAAState = SkOSMenu::kMixedState;
874 fFilterState = SkOSMenu::kMixedState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000875 fSubpixelState = SkOSMenu::kMixedState;
876 fHintingState = 0;
reed@google.com569e0432011-04-05 13:07:03 +0000877 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000878 fScrollTestX = fScrollTestY = 0;
879
Scroggo0f185c22011-03-24 18:35:50 +0000880 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000881 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000882 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
883 fShowZoomer = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000884
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000885 fZoomLevel = 0;
886 fZoomScale = SK_Scalar1;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000887
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000888 fMagnify = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000889
Scroggo8ac0d542011-06-21 14:44:57 +0000890 fSaveToPdf = false;
891 fPdfCanvas = NULL;
892
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000893 fTransitionNext = 6;
894 fTransitionPrev = 2;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000895
yangsu@google.com921091f2011-08-02 13:39:12 +0000896 int sinkID = this->getSinkID();
scroggo@google.com7dadc742012-04-18 14:07:57 +0000897 fAppMenu = new SkOSMenu;
898 fAppMenu->setTitle("Global Settings");
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000899 int itemID;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000900
901 itemID =fAppMenu->appendList("Device Type", "Device Type", sinkID, 0,
902 "Raster", "Picture", "OpenGL",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000903#if SK_ANGLE
904 "ANGLE",
905#endif
906 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000907 fAppMenu->assignKeyEquivalentToItem(itemID, 'd');
908 itemID = fAppMenu->appendTriState("AA", "AA", sinkID, fAAState);
909 fAppMenu->assignKeyEquivalentToItem(itemID, 'b');
910 itemID = fAppMenu->appendTriState("LCD", "LCD", sinkID, fLCDState);
911 fAppMenu->assignKeyEquivalentToItem(itemID, 'l');
912 itemID = fAppMenu->appendTriState("Filter", "Filter", sinkID, fFilterState);
913 fAppMenu->assignKeyEquivalentToItem(itemID, 'n');
bungeman@google.com96aabc82013-06-03 21:26:34 +0000914 itemID = fAppMenu->appendTriState("Subpixel", "Subpixel", sinkID, fSubpixelState);
915 fAppMenu->assignKeyEquivalentToItem(itemID, 's');
916 itemID = fAppMenu->appendList("Hinting", "Hinting", sinkID, fHintingState,
917 gHintingStates[0].name,
918 gHintingStates[1].name,
919 gHintingStates[2].name,
920 gHintingStates[3].name,
bungeman@google.coma9133bb2013-06-04 17:49:20 +0000921 gHintingStates[4].name,
922 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000923 fAppMenu->assignKeyEquivalentToItem(itemID, 'h');
reed@google.com67b89ee2012-08-15 14:41:58 +0000924
scroggo@google.comb073d922012-06-08 15:35:03 +0000925 fUsePipeMenuItemID = fAppMenu->appendTriState("Pipe", "Pipe" , sinkID,
chudy@google.com4605a3f2012-08-01 17:58:01 +0000926 fPipeState);
scroggo@google.comb073d922012-06-08 15:35:03 +0000927 fAppMenu->assignKeyEquivalentToItem(fUsePipeMenuItemID, 'P');
reed@google.com67b89ee2012-08-15 14:41:58 +0000928
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000929 itemID =fAppMenu->appendList("Tiling", "Tiling", sinkID, fTilingMode,
930 gTilingInfo[kNo_Tiling].label,
931 gTilingInfo[kAbs_128x128_Tiling].label,
932 gTilingInfo[kAbs_256x256_Tiling].label,
933 gTilingInfo[kRel_4x4_Tiling].label,
934 gTilingInfo[kRel_1x16_Tiling].label,
935 gTilingInfo[kRel_16x1_Tiling].label,
936 NULL);
reed@google.com67b89ee2012-08-15 14:41:58 +0000937 fAppMenu->assignKeyEquivalentToItem(itemID, 't');
938
chudy@google.com4605a3f2012-08-01 17:58:01 +0000939 itemID = fAppMenu->appendSwitch("Slide Show", "Slide Show" , sinkID, false);
940 fAppMenu->assignKeyEquivalentToItem(itemID, 'a');
941 itemID = fAppMenu->appendSwitch("Clip", "Clip" , sinkID, fUseClip);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000942 fAppMenu->assignKeyEquivalentToItem(itemID, 'c');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000943 itemID = fAppMenu->appendSwitch("Flip X", "Flip X" , sinkID, false);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000944 fAppMenu->assignKeyEquivalentToItem(itemID, 'x');
945 itemID = fAppMenu->appendSwitch("Flip Y", "Flip Y" , sinkID, false);
946 fAppMenu->assignKeyEquivalentToItem(itemID, 'y');
947 itemID = fAppMenu->appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
948 fAppMenu->assignKeyEquivalentToItem(itemID, 'z');
949 itemID = fAppMenu->appendSwitch("Magnify", "Magnify" , sinkID, fMagnify);
950 fAppMenu->assignKeyEquivalentToItem(itemID, 'm');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000951 itemID =fAppMenu->appendList("Transition-Next", "Transition-Next", sinkID,
952 fTransitionNext, "Up", "Up and Right", "Right",
953 "Down and Right", "Down", "Down and Left",
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000954 "Left", "Up and Left", NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000955 fAppMenu->assignKeyEquivalentToItem(itemID, 'j');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000956 itemID =fAppMenu->appendList("Transition-Prev", "Transition-Prev", sinkID,
957 fTransitionPrev, "Up", "Up and Right", "Right",
958 "Down and Right", "Down", "Down and Left",
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000959 "Left", "Up and Left", NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000960 fAppMenu->assignKeyEquivalentToItem(itemID, 'k');
961 itemID = fAppMenu->appendAction("Save to PDF", sinkID);
962 fAppMenu->assignKeyEquivalentToItem(itemID, 'e');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000963
scroggo@google.com7dadc742012-04-18 14:07:57 +0000964 this->addMenu(fAppMenu);
965 fSlideMenu = new SkOSMenu;
966 this->addMenu(fSlideMenu);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000967
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000968// this->setConfig(SkBitmap::kRGB_565_Config);
969 this->setConfig(SkBitmap::kARGB_8888_Config);
970 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000971 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000972
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000973 skiagm::GM::SetResourcePath(resourcePath);
974
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000975 this->loadView((*fSamples[fCurrIndex])());
chudy@google.com4605a3f2012-08-01 17:58:01 +0000976
bsalomon@google.com840e9f32011-07-06 21:59:09 +0000977 fPDFData = NULL;
reed@google.comf0b5f682011-03-11 20:08:25 +0000978
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000979 if (NULL == devManager) {
980 fDevManager = new DefaultDeviceManager();
981 } else {
982 devManager->ref();
983 fDevManager = devManager;
984 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000985 fDevManager->setUpBackend(this, fMSAASampleCount);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000986
Scroggob4490c72011-06-17 13:53:05 +0000987 // If another constructor set our dimensions, ensure that our
988 // onSizeChange gets called.
989 if (this->height() && this->width()) {
990 this->onSizeChange();
991 }
reed@google.com2072db82011-11-08 15:18:10 +0000992
993 // can't call this synchronously, since it may require a subclass to
994 // to implement, or the caller may need us to have returned from the
995 // constructor first. Hence we post an event to ourselves.
996// this->updateTitle();
997 postEventToSink(new SkEvent(gUpdateWindowTitleEvtName), this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000998}
999
1000SampleWindow::~SampleWindow() {
1001 delete fPicture;
Scroggo8ac0d542011-06-21 14:44:57 +00001002 delete fPdfCanvas;
Scroggo0f185c22011-03-24 18:35:50 +00001003 fTypeface->unref();
reed@google.com29038ed2011-07-06 17:56:47 +00001004
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001005 SkSafeUnref(fDevManager);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001006}
1007
reed@google.com1830c7a2012-06-04 12:05:43 +00001008static void make_filepath(SkString* path, const char* dir, const SkString& name) {
1009 size_t len = strlen(dir);
1010 path->set(dir);
1011 if (len > 0 && dir[len - 1] != '/') {
1012 path->append("/");
1013 }
1014 path->append(name);
1015}
1016
chudy@google.com4605a3f2012-08-01 17:58:01 +00001017void SampleWindow::registerPictFileSample(char** argv, int argc) {
1018 const char* pict = NULL;
1019
1020 for (int i = 0; i < argc; ++i) {
1021 if (!strcmp(argv[i], "--picture")) {
1022 i += 1;
1023 if (i < argc) {
1024 pict = argv[i];
1025 break;
1026 }
1027 }
1028 }
1029 if (pict) {
1030 SkString path(pict);
reed@google.com7d05ac92013-06-06 13:14:13 +00001031 fCurrIndex = fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001032 *fSamples.append() = new PictFileFactory(path);
1033 }
1034}
1035
reed@google.com1830c7a2012-06-04 12:05:43 +00001036void SampleWindow::registerPictFileSamples(char** argv, int argc) {
1037 const char* pictDir = NULL;
1038
1039 for (int i = 0; i < argc; ++i) {
1040 if (!strcmp(argv[i], "--pictureDir")) {
1041 i += 1;
1042 if (i < argc) {
1043 pictDir = argv[i];
1044 break;
1045 }
1046 }
1047 }
1048 if (pictDir) {
1049 SkOSFile::Iter iter(pictDir, "skp");
1050 SkString filename;
1051 while (iter.next(&filename)) {
1052 SkString path;
1053 make_filepath(&path, pictDir, filename);
1054 *fSamples.append() = new PictFileFactory(path);
1055 }
1056 }
1057}
1058
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001059#ifdef SAMPLE_PDF_FILE_VIEWER
1060void SampleWindow::registerPdfFileViewerSamples(char** argv, int argc) {
1061 const char* pdfDir = NULL;
1062
1063 for (int i = 0; i < argc; ++i) {
1064 if (!strcmp(argv[i], "--pdfDir")) {
1065 i += 1;
1066 if (i < argc) {
1067 pdfDir = argv[i];
1068 break;
1069 }
1070 }
1071 }
1072 if (pdfDir) {
1073 SkOSFile::Iter iter(pdfDir, "pdf");
1074 SkString filename;
1075 while (iter.next(&filename)) {
1076 SkString path;
1077 make_filepath(&path, pdfDir, filename);
1078 *fSamples.append() = new PdfFileViewerFactory(path);
1079 }
1080 }
1081}
1082#endif // SAMPLE_PDF_FILE_VIEWER
1083
1084
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001085int SampleWindow::findByTitle(const char title[]) {
1086 int i, count = fSamples.count();
1087 for (i = 0; i < count; i++) {
1088 if (getSampleTitle(i).equals(title)) {
1089 return i;
1090 }
1091 }
mike@reedtribe.orgdd52caa2011-12-28 20:02:10 +00001092 return -1;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001093}
1094
robertphillips@google.com7265e722012-05-03 18:22:28 +00001095void SampleWindow::listTitles() {
1096 int count = fSamples.count();
1097 SkDebugf("All Slides:\n");
1098 for (int i = 0; i < count; i++) {
1099 SkDebugf(" %s\n", getSampleTitle(i).c_str());
1100 }
1101}
1102
reed@android.com55e76b22009-11-23 21:46:47 +00001103static SkBitmap capture_bitmap(SkCanvas* canvas) {
1104 SkBitmap bm;
1105 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
1106 src.copyTo(&bm, src.config());
1107 return bm;
1108}
1109
1110static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
1111 SkBitmap* diff) {
1112 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001113
reed@android.com55e76b22009-11-23 21:46:47 +00001114 SkAutoLockPixels alp0(src);
1115 SkAutoLockPixels alp1(orig);
1116 for (int y = 0; y < src.height(); y++) {
1117 const void* srcP = src.getAddr(0, y);
1118 const void* origP = orig.getAddr(0, y);
1119 size_t bytes = src.width() * src.bytesPerPixel();
1120 if (memcmp(srcP, origP, bytes)) {
1121 SkDebugf("---------- difference on line %d\n", y);
1122 return true;
1123 }
1124 }
1125 return false;
1126}
1127
Scroggo0f185c22011-03-24 18:35:50 +00001128static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
1129{
1130 SkColor desiredColor = paint.getColor();
1131 paint.setColor(SK_ColorWHITE);
1132 const char* c_str = string.c_str();
1133 size_t size = string.size();
1134 SkRect bounds;
1135 paint.measureText(c_str, size, &bounds);
1136 bounds.offset(left, top);
1137 SkScalar inset = SkIntToScalar(-2);
1138 bounds.inset(inset, inset);
1139 canvas->drawRect(bounds, paint);
1140 if (desiredColor != SK_ColorBLACK) {
1141 paint.setColor(SK_ColorBLACK);
1142 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
1143 }
1144 paint.setColor(desiredColor);
1145 canvas->drawText(c_str, size, left, top, paint);
1146}
1147
reed@android.com44177402009-11-23 21:07:51 +00001148#define XCLIP_N 8
1149#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +00001150
1151void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +00001152 // update the animation time
bsalomon@google.come8f09102011-09-08 18:48:12 +00001153 if (!gAnimTimePrev && !gAnimTime) {
1154 // first time make delta be 0
1155 gAnimTime = SkTime::GetMSecs();
1156 gAnimTimePrev = gAnimTime;
1157 } else {
1158 gAnimTimePrev = gAnimTime;
1159 gAnimTime = SkTime::GetMSecs();
1160 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001161
reed@google.comf03bb562011-11-11 21:42:12 +00001162 if (fGesture.isActive()) {
1163 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001164 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001165
djsollen@google.com796763e2012-12-10 14:12:55 +00001166 if (fMeasureFPS) {
1167 fMeasureFPS_Time = 0;
1168 }
1169
reed@android.come522ca52009-11-23 20:10:41 +00001170 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +00001171 this->INHERITED::draw(canvas);
1172 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +00001173
1174 const SkScalar w = this->width();
1175 const SkScalar h = this->height();
1176 const SkScalar cw = w / XCLIP_N;
1177 const SkScalar ch = h / YCLIP_N;
1178 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +00001179 SkRect r;
1180 r.fTop = y * ch;
1181 r.fBottom = (y + 1) * ch;
1182 if (y == YCLIP_N - 1) {
1183 r.fBottom = h;
1184 }
reed@android.come522ca52009-11-23 20:10:41 +00001185 for (int x = 0; x < XCLIP_N; x++) {
1186 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +00001187 r.fLeft = x * cw;
1188 r.fRight = (x + 1) * cw;
1189 if (x == XCLIP_N - 1) {
1190 r.fRight = w;
1191 }
reed@android.come522ca52009-11-23 20:10:41 +00001192 canvas->clipRect(r);
1193 this->INHERITED::draw(canvas);
1194 }
1195 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001196
reed@android.com55e76b22009-11-23 21:46:47 +00001197 SkBitmap diff;
1198 if (bitmap_diff(canvas, orig, &diff)) {
1199 }
reed@android.come522ca52009-11-23 20:10:41 +00001200 } else {
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001201 SkSize tile;
1202 SkASSERT((TilingMode)fTilingMode < kLast_TilingMode_Enum);
1203 struct TilingInfo* info = gTilingInfo + fTilingMode;
1204 tile.set(info->w > SK_Scalar1 ? info->w : width() * info->w,
1205 info->h > SK_Scalar1 ? info->h : height() * info->h);
rmistry@google.comae933ce2012-08-23 18:19:56 +00001206
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001207 for (SkScalar y = 0; y < height(); y += tile.height()) {
1208 for (SkScalar x = 0; x < width(); x += tile.width()) {
scroggo@google.com85cade02012-08-17 13:29:50 +00001209 SkAutoCanvasRestore acr(canvas, true);
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001210 canvas->clipRect(SkRect::MakeXYWH(x, y,
1211 tile.width(),
1212 tile.height()));
scroggo@google.com85cade02012-08-17 13:29:50 +00001213 this->INHERITED::draw(canvas);
1214 }
1215 }
rmistry@google.comae933ce2012-08-23 18:19:56 +00001216
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001217 if (fTilingMode != kNo_Tiling) {
scroggo@google.com85cade02012-08-17 13:29:50 +00001218 SkPaint paint;
1219 paint.setColor(0x60FF00FF);
1220 paint.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001221
1222 for (SkScalar y = 0; y < height(); y += tile.height()) {
1223 for (SkScalar x = 0; x < width(); x += tile.width()) {
1224 canvas->drawRect(SkRect::MakeXYWH(x, y,
1225 tile.width(),
1226 tile.height()),
1227 paint);
scroggo@google.com85cade02012-08-17 13:29:50 +00001228 }
1229 }
1230 }
reed@android.come522ca52009-11-23 20:10:41 +00001231 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001232 if (fShowZoomer && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +00001233 showZoomer(canvas);
1234 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001235 if (fMagnify && !fSaveToPdf) {
1236 magnify(canvas);
1237 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001238
djsollen@google.com796763e2012-12-10 14:12:55 +00001239 if (fMeasureFPS && fMeasureFPS_Time) {
1240 this->updateTitle();
1241 this->postInvalDelay();
1242 }
1243
reed@google.com29038ed2011-07-06 17:56:47 +00001244 // do this last
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001245 fDevManager->publishCanvas(fDeviceType, canvas, this);
Scroggo3272ba82011-05-25 20:50:42 +00001246}
1247
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001248static float clipW = 200;
1249static float clipH = 200;
1250void SampleWindow::magnify(SkCanvas* canvas) {
1251 SkRect r;
1252 int count = canvas->save();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001253
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001254 SkMatrix m = canvas->getTotalMatrix();
robertphillips@google.com07ef9112012-06-04 13:22:14 +00001255 if (!m.invert(&m)) {
1256 return;
1257 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001258 SkPoint offset, center;
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001259 SkScalar mouseX = fMouseX * SK_Scalar1;
1260 SkScalar mouseY = fMouseY * SK_Scalar1;
1261 m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset);
1262 m.mapXY(mouseX, mouseY, &center);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001263
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001264 r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX());
1265 r.offset(offset.fX, offset.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001266
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001267 SkPaint paint;
1268 paint.setColor(0xFF66AAEE);
1269 paint.setStyle(SkPaint::kStroke_Style);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001270 paint.setStrokeWidth(10.f * m.getScaleX());
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001271 //lense offset
1272 //canvas->translate(0, -250);
1273 canvas->drawRect(r, paint);
1274 canvas->clipRect(r);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001275
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001276 m = canvas->getTotalMatrix();
1277 m.setTranslate(-center.fX, -center.fY);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001278 m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001279 m.postTranslate(center.fX, center.fY);
1280 canvas->concat(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001281
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001282 this->INHERITED::draw(canvas);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001283
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001284 canvas->restoreToCount(count);
1285}
1286
Scroggo3272ba82011-05-25 20:50:42 +00001287void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +00001288 int count = canvas->save();
1289 canvas->resetMatrix();
1290 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +00001291 int width = SkScalarRound(this->width());
1292 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +00001293 if (fMouseX >= width) fMouseX = width - 1;
1294 else if (fMouseX < 0) fMouseX = 0;
1295 if (fMouseY >= height) fMouseY = height - 1;
1296 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +00001297
Scroggo0f185c22011-03-24 18:35:50 +00001298 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +00001299 bitmap.lockPixels();
1300
Scroggo0f185c22011-03-24 18:35:50 +00001301 // 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 +00001302 int zoomedWidth = (width >> 1) | 1;
1303 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +00001304 SkIRect src;
1305 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
1306 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
1307 SkRect dest;
1308 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
1309 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
1310 SkPaint paint;
1311 // Clear the background behind our zoomed in view
1312 paint.setColor(SK_ColorWHITE);
1313 canvas->drawRect(dest, paint);
1314 canvas->drawBitmapRect(bitmap, &src, dest);
1315 paint.setColor(SK_ColorBLACK);
1316 paint.setStyle(SkPaint::kStroke_Style);
1317 // Draw a border around the pixel in the middle
1318 SkRect originalPixel;
1319 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
1320 SkMatrix matrix;
1321 SkRect scalarSrc;
1322 scalarSrc.set(src);
1323 SkColor color = bitmap.getColor(fMouseX, fMouseY);
1324 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
1325 SkRect pixel;
1326 matrix.mapRect(&pixel, originalPixel);
1327 // TODO Perhaps measure the values and make the outline white if it's "dark"
1328 if (color == SK_ColorBLACK) {
1329 paint.setColor(SK_ColorWHITE);
1330 }
1331 canvas->drawRect(pixel, paint);
1332 }
1333 paint.setColor(SK_ColorBLACK);
1334 // Draw a border around the destination rectangle
1335 canvas->drawRect(dest, paint);
1336 paint.setStyle(SkPaint::kStrokeAndFill_Style);
1337 // Identify the pixel and its color on screen
1338 paint.setTypeface(fTypeface);
1339 paint.setAntiAlias(true);
1340 SkScalar lineHeight = paint.getFontMetrics(NULL);
1341 SkString string;
1342 string.appendf("(%i, %i)", fMouseX, fMouseY);
1343 SkScalar left = dest.fLeft + SkIntToScalar(3);
1344 SkScalar i = SK_Scalar1;
1345 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1346 // Alpha
1347 i += SK_Scalar1;
1348 string.reset();
1349 string.appendf("A: %X", SkColorGetA(color));
1350 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1351 // Red
1352 i += SK_Scalar1;
1353 string.reset();
1354 string.appendf("R: %X", SkColorGetR(color));
1355 paint.setColor(SK_ColorRED);
1356 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1357 // Green
1358 i += SK_Scalar1;
1359 string.reset();
1360 string.appendf("G: %X", SkColorGetG(color));
1361 paint.setColor(SK_ColorGREEN);
1362 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1363 // Blue
1364 i += SK_Scalar1;
1365 string.reset();
1366 string.appendf("B: %X", SkColorGetB(color));
1367 paint.setColor(SK_ColorBLUE);
1368 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1369 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +00001370}
1371
reed@android.com8a1c16f2008-12-17 15:59:43 +00001372void SampleWindow::onDraw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001373}
1374
1375#include "SkColorPriv.h"
1376
caryclark@google.com02939ce2012-06-06 12:09:51 +00001377#if 0 // UNUSED
reed@android.com8a1c16f2008-12-17 15:59:43 +00001378static void reverseRedAndBlue(const SkBitmap& bm) {
1379 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
1380 uint8_t* p = (uint8_t*)bm.getPixels();
1381 uint8_t* stop = p + bm.getSize();
1382 while (p < stop) {
1383 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
1384 unsigned scale = SkAlpha255To256(p[3]);
1385 unsigned r = p[2];
1386 unsigned b = p[0];
1387 p[0] = SkAlphaMul(r, scale);
1388 p[1] = SkAlphaMul(p[1], scale);
1389 p[2] = SkAlphaMul(b, scale);
1390 p += 4;
1391 }
1392}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001393#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001394
Scroggo8ac0d542011-06-21 14:44:57 +00001395void SampleWindow::saveToPdf()
1396{
1397 fSaveToPdf = true;
1398 this->inval(NULL);
1399}
1400
reed@android.com8a1c16f2008-12-17 15:59:43 +00001401SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +00001402 if (fSaveToPdf) {
1403 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
1404 SkISize size = SkISize::Make(bmp.width(), bmp.height());
1405 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
1406 canvas->getTotalMatrix());
1407 fPdfCanvas = new SkCanvas(pdfDevice);
1408 pdfDevice->unref();
1409 canvas = fPdfCanvas;
bsalomon@google.com82502e22013-01-24 20:47:18 +00001410 } else if (kPicture_DeviceType == fDeviceType) {
1411 fPicture = new SkPicture;
1412 canvas = fPicture->beginRecording(9999, 9999);
Scroggo8ac0d542011-06-21 14:44:57 +00001413 } else {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001414#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00001415 if (kNullGPU_DeviceType != fDeviceType)
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001416#endif
bsalomon@google.com82502e22013-01-24 20:47:18 +00001417 {
1418 canvas = this->INHERITED::beforeChildren(canvas);
reed@google.comac10a2d2010-12-22 21:39:39 +00001419 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001420 }
1421
1422 if (fUseClip) {
1423 canvas->drawColor(0xFFFF88FF);
reed@google.com045e62d2011-10-24 12:19:46 +00001424 canvas->clipPath(fClipPath, SkRegion::kIntersect_Op, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001425 }
1426
1427 return canvas;
1428}
1429
1430static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
1431 const SkRegion& rgn) {
1432 SkCanvas canvas(bm);
1433 SkRegion inval(rgn);
1434
1435 inval.translate(r.fLeft, r.fTop);
1436 canvas.clipRegion(inval);
1437 canvas.drawColor(0xFFFF8080);
1438}
yangsu@google.com501775e2011-06-24 16:04:50 +00001439#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001440void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +00001441 if (fSaveToPdf) {
1442 fSaveToPdf = false;
1443 if (fShowZoomer) {
1444 showZoomer(fPdfCanvas);
1445 }
1446 SkString name;
1447 name.printf("%s.pdf", this->getTitle());
1448 SkPDFDocument doc;
1449 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
1450 doc.appendPage(device);
djsollen@google.com56c69772011-11-08 19:00:26 +00001451#ifdef SK_BUILD_FOR_ANDROID
Scroggo8ac0d542011-06-21 14:44:57 +00001452 name.prepend("/sdcard/");
1453#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +00001454
yangsu@google.com501775e2011-06-24 16:04:50 +00001455#ifdef SK_BUILD_FOR_IOS
1456 SkDynamicMemoryWStream mstream;
1457 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +00001458 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +00001459#endif
Scroggo8ac0d542011-06-21 14:44:57 +00001460 SkFILEWStream stream(name.c_str());
1461 if (stream.isValid()) {
1462 doc.emitPDF(&stream);
1463 const char* desc = "File saved from Skia SampleApp";
1464 this->onPDFSaved(this->getTitle(), desc, name.c_str());
1465 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001466
Scroggo8ac0d542011-06-21 14:44:57 +00001467 delete fPdfCanvas;
1468 fPdfCanvas = NULL;
1469
1470 // We took over the draw calls in order to create the PDF, so we need
1471 // to redraw.
1472 this->inval(NULL);
1473 return;
1474 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001475
reed@android.comf2b98d62010-12-20 18:26:13 +00001476 if (fRequestGrabImage) {
1477 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001478
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001479 SkBaseDevice* device = orig->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001480 SkBitmap bmp;
1481 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001482 static int gSampleGrabCounter;
1483 SkString name;
reed@google.com7c57e0e2012-09-08 21:20:33 +00001484 name.printf("sample_grab_%d.png", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001485 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +00001486 SkImageEncoder::kPNG_Type, 100);
1487 }
1488 }
1489
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001490 if (kPicture_DeviceType == fDeviceType) {
1491 if (true) {
1492 SkPicture* pict = new SkPicture(*fPicture);
1493 fPicture->unref();
reed@google.come23f1942011-12-08 19:36:00 +00001494 this->installDrawFilter(orig);
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001495 orig->drawPicture(*pict);
1496 pict->unref();
1497 } else if (true) {
1498 SkDynamicMemoryWStream ostream;
1499 fPicture->serialize(&ostream);
1500 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001501
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001502 SkAutoDataUnref data(ostream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +00001503 SkMemoryStream istream(data->data(), data->size());
scroggo@google.comf1754ec2013-06-28 21:32:00 +00001504 SkAutoTUnref<SkPicture> pict(SkPicture::CreateFromStream(&istream));
1505 if (pict.get() != NULL) {
1506 orig->drawPicture(*pict.get());
1507 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001508 } else {
1509 fPicture->draw(orig);
1510 fPicture->unref();
1511 }
1512 fPicture = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001513 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001514
reed@google.com17d7aec2011-04-25 14:31:44 +00001515 // Do this after presentGL and other finishing, rather than in afterChild
djsollen@google.com796763e2012-12-10 14:12:55 +00001516 if (fMeasureFPS && fMeasureFPS_StartTime) {
1517 fMeasureFPS_Time += SkTime::GetMSecs() - fMeasureFPS_StartTime;
reed@google.com17d7aec2011-04-25 14:31:44 +00001518 }
1519
1520 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +00001521 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001522 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
1523 int dx = fScrollTestX * 7;
1524 int dy = fScrollTestY * 7;
1525 SkIRect r;
1526 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001527
reed@android.com8a1c16f2008-12-17 15:59:43 +00001528 r.set(50, 50, 50+100, 50+100);
1529 bm.scrollRect(&r, dx, dy, &inval);
1530 paint_rgn(bm, r, inval);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001531 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001532}
1533
reed@android.com6c5f6f22009-08-14 16:08:38 +00001534void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
reed@android.com6c5f6f22009-08-14 16:08:38 +00001535 if (fRotate) {
bungeman@google.comb1785912013-07-09 21:58:56 +00001536 fRotateAnimTime += SampleCode::GetAnimSecondsDelta();
1537
reed@android.com6c5f6f22009-08-14 16:08:38 +00001538 SkScalar cx = this->width() / 2;
1539 SkScalar cy = this->height() / 2;
1540 canvas->translate(cx, cy);
bungeman@google.comb1785912013-07-09 21:58:56 +00001541 canvas->rotate(fRotateAnimTime * 10);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001542 canvas->translate(-cx, -cy);
1543 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001544
bsalomon@google.come8f09102011-09-08 18:48:12 +00001545 if (fPerspAnim) {
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001546 fPerspAnimTime += SampleCode::GetAnimSecondsDelta();
1547
1548 static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
1549 static const SkScalar gAnimMag = SK_Scalar1 / 1000;
1550 SkScalar t = SkScalarMod(fPerspAnimTime, gAnimPeriod);
1551 if (SkScalarFloorToInt(SkScalarDiv(fPerspAnimTime, gAnimPeriod)) & 0x1) {
1552 t = gAnimPeriod - t;
1553 }
1554 t = 2 * t - gAnimPeriod;
1555 t = SkScalarMul(SkScalarDiv(t, gAnimPeriod), gAnimMag);
1556 SkMatrix m;
1557 m.reset();
1558 m.setPerspY(t);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001559 canvas->concat(m);
1560 }
reed@google.comf0b5f682011-03-11 20:08:25 +00001561
reed@google.come23f1942011-12-08 19:36:00 +00001562 this->installDrawFilter(canvas);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001563
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001564 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +00001565 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
djsollen@google.com796763e2012-12-10 14:12:55 +00001566 fMeasureFPS_StartTime = SkTime::GetMSecs();
reed@google.comf2183392011-04-22 14:10:48 +00001567 }
1568 } else {
1569 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001570 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001571 if (fPerspAnim || fRotate) {
bsalomon@google.come8f09102011-09-08 18:48:12 +00001572 this->inval(NULL);
1573 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001574}
1575
1576void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +00001577 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001578}
1579
reed@android.com8a1c16f2008-12-17 15:59:43 +00001580static SkBitmap::Config gConfigCycle[] = {
1581 SkBitmap::kNo_Config, // none -> none
reed@android.com8a1c16f2008-12-17 15:59:43 +00001582 SkBitmap::kNo_Config, // a8 -> none
1583 SkBitmap::kNo_Config, // index8 -> none
1584 SkBitmap::kARGB_4444_Config, // 565 -> 4444
1585 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
1586 SkBitmap::kRGB_565_Config // 8888 -> 565
1587};
1588
1589static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
1590 return gConfigCycle[c];
1591}
1592
djsollen@google.come32b5832011-06-13 16:58:40 +00001593void SampleWindow::changeZoomLevel(float delta) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001594 fZoomLevel += delta;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001595 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001596 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
1597 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001598 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001599 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
1600 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001601 } else {
1602 fZoomScale = SK_Scalar1;
1603 }
reed@google.comf03bb562011-11-11 21:42:12 +00001604 this->updateMatrix();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001605}
1606
reed@google.comf03bb562011-11-11 21:42:12 +00001607void SampleWindow::updateMatrix(){
1608 SkMatrix m;
1609 m.reset();
1610 if (fZoomLevel) {
1611 SkPoint center;
1612 //m = this->getLocalMatrix();//.invert(&m);
1613 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
1614 SkScalar cx = center.fX;
1615 SkScalar cy = center.fY;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001616
reed@google.comf03bb562011-11-11 21:42:12 +00001617 m.setTranslate(-cx, -cy);
1618 m.postScale(fZoomScale, fZoomScale);
1619 m.postTranslate(cx, cy);
1620 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001621
reed@google.comf03bb562011-11-11 21:42:12 +00001622 if (fFlipAxis) {
1623 m.preTranslate(fZoomCenterX, fZoomCenterY);
1624 if (fFlipAxis & kFlipAxis_X) {
1625 m.preScale(-SK_Scalar1, SK_Scalar1);
1626 }
1627 if (fFlipAxis & kFlipAxis_Y) {
1628 m.preScale(SK_Scalar1, -SK_Scalar1);
1629 }
1630 m.preTranslate(-fZoomCenterX, -fZoomCenterY);
1631 //canvas->concat(m);
1632 }
1633 // Apply any gesture matrix
1634 m.preConcat(fGesture.localM());
1635 m.preConcat(fGesture.globalM());
chudy@google.com4605a3f2012-08-01 17:58:01 +00001636
reed@google.comf03bb562011-11-11 21:42:12 +00001637 this->setLocalMatrix(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001638
reed@google.comf03bb562011-11-11 21:42:12 +00001639 this->updateTitle();
1640 this->inval(NULL);
1641}
Scroggo2c8208f2011-06-15 16:49:08 +00001642bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +00001643 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001644 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001645 fTransitionPrev));
Scroggo2c8208f2011-06-15 16:49:08 +00001646 return true;
1647}
1648
reed@android.com8a1c16f2008-12-17 15:59:43 +00001649bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +00001650 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001651 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001652 fTransitionNext));
reed@android.com34245c72009-11-03 04:00:48 +00001653 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001654}
1655
yangsu@google.com501775e2011-06-24 16:04:50 +00001656bool SampleWindow::goToSample(int i) {
1657 fCurrIndex = (i) % fSamples.count();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001658 this->loadView(create_transition(curr_view(this),(*fSamples[fCurrIndex])(), 6));
yangsu@google.com501775e2011-06-24 16:04:50 +00001659 return true;
1660}
1661
1662SkString SampleWindow::getSampleTitle(int i) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001663 return ::getSampleTitle(fSamples[i]);
yangsu@google.com501775e2011-06-24 16:04:50 +00001664}
1665
1666int SampleWindow::sampleCount() {
1667 return fSamples.count();
1668}
1669
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001670void SampleWindow::showOverview() {
chudy@google.com4605a3f2012-08-01 17:58:01 +00001671 this->loadView(create_transition(curr_view(this),
1672 create_overview(fSamples.count(), fSamples.begin()),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001673 4));
1674}
1675
reed@google.come23f1942011-12-08 19:36:00 +00001676void SampleWindow::installDrawFilter(SkCanvas* canvas) {
bungeman@google.com96aabc82013-06-03 21:26:34 +00001677 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState, fFilterState, fSubpixelState,
1678 fHintingState))->unref();
reed@google.come23f1942011-12-08 19:36:00 +00001679}
1680
Scroggo2c8208f2011-06-15 16:49:08 +00001681void SampleWindow::postAnimatingEvent() {
1682 if (fAnimating) {
reed@google.com87fac4a2011-08-04 13:50:17 +00001683 (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
Scroggo2c8208f2011-06-15 16:49:08 +00001684 }
1685}
reed@google.come23f1942011-12-08 19:36:00 +00001686
reed@android.com8a1c16f2008-12-17 15:59:43 +00001687bool SampleWindow::onEvent(const SkEvent& evt) {
reed@google.com2072db82011-11-08 15:18:10 +00001688 if (evt.isType(gUpdateWindowTitleEvtName)) {
1689 this->updateTitle();
1690 return true;
1691 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001692 if (evt.isType(ANIMATING_EVENTTYPE)) {
1693 if (fAnimating) {
1694 this->nextSample();
1695 this->postAnimatingEvent();
1696 }
1697 return true;
1698 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001699 if (evt.isType("replace-transition-view")) {
1700 this->loadView((SkView*)SkEventSink::FindSink(evt.getFast32()));
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001701 return true;
1702 }
reed@android.com34245c72009-11-03 04:00:48 +00001703 if (evt.isType("set-curr-index")) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001704 this->goToSample(evt.getFast32());
reed@android.com34245c72009-11-03 04:00:48 +00001705 return true;
1706 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001707 if (isInvalEvent(evt)) {
1708 this->inval(NULL);
1709 return true;
1710 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001711 int selected = -1;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001712 if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001713 this->setDeviceType((DeviceType)selected);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001714 return true;
yangsu@google.com921091f2011-08-02 13:39:12 +00001715 }
scroggo@google.comb073d922012-06-08 15:35:03 +00001716 if (SkOSMenu::FindTriState(evt, "Pipe", &fPipeState)) {
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001717#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00001718 if (!fPipeState != SkOSMenu::kOnState)
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001719 gServer.disconnectAll();
1720#endif
scroggo@google.comb073d922012-06-08 15:35:03 +00001721 (void)SampleView::SetUsePipe(curr_view(this), fPipeState);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001722 this->updateTitle();
1723 this->inval(NULL);
yangsu@google.com921091f2011-08-02 13:39:12 +00001724 return true;
1725 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001726 if (SkOSMenu::FindSwitchState(evt, "Slide Show", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001727 this->toggleSlideshow();
1728 return true;
1729 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001730 if (SkOSMenu::FindTriState(evt, "AA", &fAAState) ||
1731 SkOSMenu::FindTriState(evt, "LCD", &fLCDState) ||
1732 SkOSMenu::FindTriState(evt, "Filter", &fFilterState) ||
bungeman@google.com96aabc82013-06-03 21:26:34 +00001733 SkOSMenu::FindTriState(evt, "Subpixel", &fSubpixelState) ||
1734 SkOSMenu::FindListIndex(evt, "Hinting", &fHintingState) ||
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001735 SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) ||
1736 SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
1737 SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify) ||
1738 SkOSMenu::FindListIndex(evt, "Transition-Next", &fTransitionNext) ||
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001739 SkOSMenu::FindListIndex(evt, "Transition-Prev", &fTransitionPrev) ||
1740 SkOSMenu::FindListIndex(evt, "Tiling", &fTilingMode)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001741 this->inval(NULL);
1742 this->updateTitle();
1743 return true;
1744 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001745 if (SkOSMenu::FindSwitchState(evt, "Flip X", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001746 fFlipAxis ^= kFlipAxis_X;
reed@google.comf03bb562011-11-11 21:42:12 +00001747 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001748 return true;
1749 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001750 if (SkOSMenu::FindSwitchState(evt, "Flip Y", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001751 fFlipAxis ^= kFlipAxis_Y;
reed@google.comf03bb562011-11-11 21:42:12 +00001752 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001753 return true;
1754 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001755 if (SkOSMenu::FindAction(evt,"Save to PDF")) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001756 this->saveToPdf();
1757 return true;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001758 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001759 return this->INHERITED::onEvent(evt);
1760}
1761
reed@android.comf2b98d62010-12-20 18:26:13 +00001762bool SampleWindow::onQuery(SkEvent* query) {
1763 if (query->isType("get-slide-count")) {
1764 query->setFast32(fSamples.count());
1765 return true;
1766 }
1767 if (query->isType("get-slide-title")) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001768 SkView* view = (*fSamples[query->getFast32()])();
reed@android.comf2b98d62010-12-20 18:26:13 +00001769 SkEvent evt(gTitleEvtName);
1770 if (view->doQuery(&evt)) {
1771 query->setString("title", evt.findString(gTitleEvtName));
1772 }
1773 SkSafeUnref(view);
1774 return true;
1775 }
1776 if (query->isType("use-fast-text")) {
1777 SkEvent evt(gFastTextEvtName);
1778 return curr_view(this)->doQuery(&evt);
1779 }
reed@google.com29038ed2011-07-06 17:56:47 +00001780 if (query->isType("ignore-window-bitmap")) {
1781 query->setFast32(this->getGrContext() != NULL);
1782 return true;
1783 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001784 return this->INHERITED::onQuery(query);
1785}
1786
caryclark@google.com02939ce2012-06-06 12:09:51 +00001787#if 0 // UNUSED
reed@android.com0ae6b242008-12-23 16:49:54 +00001788static void cleanup_for_filename(SkString* name) {
1789 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001790 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001791 switch (str[i]) {
1792 case ':': str[i] = '-'; break;
1793 case '/': str[i] = '-'; break;
1794 case ' ': str[i] = '_'; break;
1795 default: break;
1796 }
1797 }
1798}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001799#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001800
reed@google.coma4f81372012-11-15 15:56:38 +00001801//extern bool gIgnoreFastBlurRect;
1802
reed@android.com8a1c16f2008-12-17 15:59:43 +00001803bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001804 {
1805 SkView* view = curr_view(this);
1806 if (view) {
1807 SkEvent evt(gCharEvtName);
1808 evt.setFast32(uni);
1809 if (view->doQuery(&evt)) {
1810 return true;
1811 }
1812 }
1813 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001814
reed@android.com8a1c16f2008-12-17 15:59:43 +00001815 int dx = 0xFF;
1816 int dy = 0xFF;
1817
1818 switch (uni) {
1819 case '5': dx = 0; dy = 0; break;
1820 case '8': dx = 0; dy = -1; break;
1821 case '6': dx = 1; dy = 0; break;
1822 case '2': dx = 0; dy = 1; break;
1823 case '4': dx = -1; dy = 0; break;
1824 case '7': dx = -1; dy = -1; break;
1825 case '9': dx = 1; dy = -1; break;
1826 case '3': dx = 1; dy = 1; break;
1827 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001828
reed@android.com8a1c16f2008-12-17 15:59:43 +00001829 default:
1830 break;
1831 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001832
reed@android.com8a1c16f2008-12-17 15:59:43 +00001833 if (0xFF != dx && 0xFF != dy) {
1834 if ((dx | dy) == 0) {
1835 fScrollTestX = fScrollTestY = 0;
1836 } else {
1837 fScrollTestX += dx;
1838 fScrollTestY += dy;
1839 }
1840 this->inval(NULL);
1841 return true;
1842 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001843
reed@android.com0ae6b242008-12-23 16:49:54 +00001844 switch (uni) {
djsollen@google.com796763e2012-12-10 14:12:55 +00001845 case 'b':
1846 {
1847 postEventToSink(SkNEW_ARGS(SkEvent, ("PictFileView::toggleBBox")), curr_view(this));
1848 this->updateTitle();
1849 this->inval(NULL);
1850 break;
1851 }
reed@google.coma4f81372012-11-15 15:56:38 +00001852 case 'B':
1853// gIgnoreFastBlurRect = !gIgnoreFastBlurRect;
1854 this->inval(NULL);
1855 break;
1856
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001857 case 'f':
chudy@google.com4605a3f2012-08-01 17:58:01 +00001858 // only
djsollen@google.com6ff82552011-11-07 15:43:57 +00001859 toggleFPS();
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001860 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001861 case 'g':
1862 fRequestGrabImage = true;
1863 this->inval(NULL);
1864 break;
reed@google.coma4f81372012-11-15 15:56:38 +00001865 case 'G':
1866 gShowGMBounds = !gShowGMBounds;
1867 postEventToSink(GMSampleView::NewShowSizeEvt(gShowGMBounds),
1868 curr_view(this));
1869 this->inval(NULL);
1870 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001871 case 'i':
1872 this->zoomIn();
1873 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001874 case 'o':
1875 this->zoomOut();
1876 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001877 case 'r':
1878 fRotate = !fRotate;
bungeman@google.comb1785912013-07-09 21:58:56 +00001879 fRotateAnimTime = 0;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001880 this->inval(NULL);
1881 this->updateTitle();
1882 return true;
bsalomon@google.come8f09102011-09-08 18:48:12 +00001883 case 'k':
1884 fPerspAnim = !fPerspAnim;
1885 this->inval(NULL);
1886 this->updateTitle();
1887 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001888#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00001889 case '\\':
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001890 this->setDeviceType(kNullGPU_DeviceType);
1891 this->inval(NULL);
1892 this->updateTitle();
bsalomon@google.com74913722011-10-27 20:44:19 +00001893 return true;
twiz@google.com05e70242012-01-27 19:12:00 +00001894 case 'p':
1895 {
1896 GrContext* grContext = this->getGrContext();
1897 if (grContext) {
1898 size_t cacheBytes = grContext->getGpuTextureCacheBytes();
1899 grContext->freeGpuResources();
1900 SkDebugf("Purged %d bytes from the GPU resource cache.\n",
1901 cacheBytes);
1902 }
1903 }
1904 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001905#endif
reed@android.com0ae6b242008-12-23 16:49:54 +00001906 default:
1907 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001908 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001909
scroggo@google.com7dadc742012-04-18 14:07:57 +00001910 if (fAppMenu->handleKeyEquivalent(uni)|| fSlideMenu->handleKeyEquivalent(uni)) {
1911 this->onUpdateMenu(fAppMenu);
1912 this->onUpdateMenu(fSlideMenu);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001913 return true;
1914 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001915 return this->INHERITED::onHandleChar(uni);
1916}
1917
yangsu@google.com921091f2011-08-02 13:39:12 +00001918void SampleWindow::setDeviceType(DeviceType type) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001919 if (type == fDeviceType)
1920 return;
1921
1922 fDevManager->tearDownBackend(this);
1923
1924 fDeviceType = type;
1925
bsalomon@google.com11959252012-04-06 20:13:38 +00001926 fDevManager->setUpBackend(this, fMSAASampleCount);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001927
yangsu@google.com921091f2011-08-02 13:39:12 +00001928 this->updateTitle();
1929 this->inval(NULL);
1930}
1931
Scroggo2c8208f2011-06-15 16:49:08 +00001932void SampleWindow::toggleSlideshow() {
1933 fAnimating = !fAnimating;
1934 this->postAnimatingEvent();
1935 this->updateTitle();
1936}
1937
1938void SampleWindow::toggleRendering() {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001939 this->setDeviceType(cycle_devicetype(fDeviceType));
Scroggo2c8208f2011-06-15 16:49:08 +00001940 this->updateTitle();
1941 this->inval(NULL);
1942}
1943
djsollen@google.com6ff82552011-11-07 15:43:57 +00001944void SampleWindow::toggleFPS() {
1945 fMeasureFPS = !fMeasureFPS;
1946 this->updateTitle();
1947 this->inval(NULL);
1948}
1949
reed@android.com8a1c16f2008-12-17 15:59:43 +00001950#include "SkDumpCanvas.h"
1951
1952bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001953 {
1954 SkView* view = curr_view(this);
1955 if (view) {
1956 SkEvent evt(gKeyEvtName);
1957 evt.setFast32(key);
1958 if (view->doQuery(&evt)) {
1959 return true;
1960 }
1961 }
1962 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001963 switch (key) {
1964 case kRight_SkKey:
1965 if (this->nextSample()) {
1966 return true;
1967 }
1968 break;
1969 case kLeft_SkKey:
scroggo@google.come2dd9732012-08-15 20:03:06 +00001970 if (this->previousSample()) {
1971 return true;
1972 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001973 return true;
1974 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001975 if (USE_ARROWS_FOR_ZOOM) {
bungeman@google.comcefd9812013-05-15 15:07:32 +00001976 this->changeZoomLevel(1.f / 32.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001977 } else {
1978 fNClip = !fNClip;
1979 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001980 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001981 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001982 return true;
1983 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001984 if (USE_ARROWS_FOR_ZOOM) {
bungeman@google.comcefd9812013-05-15 15:07:32 +00001985 this->changeZoomLevel(-1.f / 32.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001986 } else {
1987 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001988 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001989 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001990 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001991 case kOK_SkKey: {
1992 SkString title;
1993 if (curr_title(this, &title)) {
1994 writeTitleToPrefs(title.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001995 }
1996 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001997 }
reed@android.com34245c72009-11-03 04:00:48 +00001998 case kBack_SkKey:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001999 this->showOverview();
reed@android.com34245c72009-11-03 04:00:48 +00002000 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002001 default:
2002 break;
2003 }
2004 return this->INHERITED::onHandleKey(key);
2005}
2006
reed@google.com52f57e12011-03-16 12:10:02 +00002007///////////////////////////////////////////////////////////////////////////////
2008
2009static const char gGestureClickType[] = "GestureClickType";
2010
Scroggod3aed392011-06-22 13:26:56 +00002011bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
reed@google.com4d5c26d2013-01-08 16:17:50 +00002012 void* owner, unsigned modi) {
Scroggo0f185c22011-03-24 18:35:50 +00002013 if (Click::kMoved_State == state) {
2014 updatePointer(x, y);
2015 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002016 int w = SkScalarRound(this->width());
2017 int h = SkScalarRound(this->height());
2018
2019 // check for the resize-box
2020 if (w - x < 16 && h - y < 16) {
2021 return false; // let the OS handle the click
chudy@google.com4605a3f2012-08-01 17:58:01 +00002022 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002023 else if (fMagnify) {
2024 //it's only necessary to update the drawing if there's a click
2025 this->inval(NULL);
2026 return false; //prevent dragging while magnify is enabled
reed@google.com4d5c26d2013-01-08 16:17:50 +00002027 } else {
2028 // capture control+option, and trigger debugger
2029 if ((modi & kControl_SkModifierKey) && (modi & kOption_SkModifierKey)) {
2030 if (Click::kDown_State == state) {
2031 SkEvent evt("debug-hit-test");
2032 evt.setS32("debug-hit-test-x", x);
2033 evt.setS32("debug-hit-test-y", y);
2034 curr_view(this)->doEvent(evt);
2035 }
2036 return true;
2037 } else {
2038 return this->INHERITED::onDispatchClick(x, y, state, owner, modi);
2039 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002040 }
2041}
2042
reed@google.com52f57e12011-03-16 12:10:02 +00002043class GestureClick : public SkView::Click {
2044public:
2045 GestureClick(SkView* target) : SkView::Click(target) {
2046 this->setType(gGestureClickType);
2047 }
2048
2049 static bool IsGesture(Click* click) {
2050 return click->isType(gGestureClickType);
2051 }
2052};
2053
reed@google.com4d5c26d2013-01-08 16:17:50 +00002054SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y,
2055 unsigned modi) {
reed@google.com52f57e12011-03-16 12:10:02 +00002056 return new GestureClick(this);
2057}
2058
2059bool SampleWindow::onClick(Click* click) {
2060 if (GestureClick::IsGesture(click)) {
bsalomon@google.com4d4f2812011-12-12 18:34:01 +00002061 float x = static_cast<float>(click->fICurr.fX);
2062 float y = static_cast<float>(click->fICurr.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00002063
reed@google.com52f57e12011-03-16 12:10:02 +00002064 switch (click->fState) {
2065 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00002066 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00002067 break;
2068 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00002069 fGesture.touchMoved(click->fOwner, x, y);
reed@google.comf03bb562011-11-11 21:42:12 +00002070 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002071 break;
2072 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00002073 fGesture.touchEnd(click->fOwner);
reed@google.comf03bb562011-11-11 21:42:12 +00002074 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002075 break;
2076 }
2077 return true;
2078 }
2079 return false;
2080}
2081
2082///////////////////////////////////////////////////////////////////////////////
2083
reed@android.com8a1c16f2008-12-17 15:59:43 +00002084void SampleWindow::loadView(SkView* view) {
2085 SkView::F2BIter iter(this);
2086 SkView* prev = iter.next();
2087 if (prev) {
2088 prev->detachFromParent();
2089 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002090
reed@android.com8a1c16f2008-12-17 15:59:43 +00002091 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00002092 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002093 this->attachChildToFront(view)->unref();
2094 view->setSize(this->width(), this->height());
2095
yangsu@google.com921091f2011-08-02 13:39:12 +00002096 //repopulate the slide menu when a view is loaded
scroggo@google.com7dadc742012-04-18 14:07:57 +00002097 fSlideMenu->reset();
djsollen@google.com388974f2013-01-17 13:20:01 +00002098
scroggo@google.comb073d922012-06-08 15:35:03 +00002099 (void)SampleView::SetUsePipe(view, fPipeState);
yangsu@google.com921091f2011-08-02 13:39:12 +00002100 if (SampleView::IsSampleView(view))
scroggo@google.com7dadc742012-04-18 14:07:57 +00002101 ((SampleView*)view)->requestMenu(fSlideMenu);
2102 this->onUpdateMenu(fSlideMenu);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002103 this->updateTitle();
2104}
2105
2106static const char* gConfigNames[] = {
2107 "unknown config",
reed@android.com8a1c16f2008-12-17 15:59:43 +00002108 "A8",
2109 "Index8",
2110 "565",
2111 "4444",
2112 "8888"
2113};
2114
2115static const char* configToString(SkBitmap::Config c) {
2116 return gConfigNames[c];
2117}
2118
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002119static const char* gDeviceTypePrefix[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002120 "raster: ",
2121 "picture: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002122#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00002123 "opengl: ",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00002124#if SK_ANGLE
2125 "angle: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002126#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +00002127 "null-gl: "
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002128#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +00002129};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002130SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gDeviceTypePrefix) == SampleWindow::kDeviceTypeCnt,
2131 array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002132
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002133static const char* trystate_str(SkOSMenu::TriState state,
reed@google.com569e0432011-04-05 13:07:03 +00002134 const char trueStr[], const char falseStr[]) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002135 if (SkOSMenu::kOnState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002136 return trueStr;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002137 } else if (SkOSMenu::kOffState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002138 return falseStr;
2139 }
2140 return NULL;
2141}
2142
reed@android.com8a1c16f2008-12-17 15:59:43 +00002143void SampleWindow::updateTitle() {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002144 SkView* view = curr_view(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002145
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002146 SkString title;
2147 if (!curr_title(this, &title)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002148 title.set("<unknown>");
2149 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002150
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002151 title.prepend(gDeviceTypePrefix[fDeviceType]);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002152
2153 title.prepend(" ");
2154 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002155
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00002156 if (fTilingMode != kNo_Tiling) {
2157 title.prependf("<T: %s> ", gTilingInfo[fTilingMode].label);
2158 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002159 if (fAnimating) {
2160 title.prepend("<A> ");
2161 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00002162 if (fRotate) {
2163 title.prepend("<R> ");
2164 }
reed@android.come522ca52009-11-23 20:10:41 +00002165 if (fNClip) {
2166 title.prepend("<C> ");
2167 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00002168 if (fPerspAnim) {
2169 title.prepend("<K> ");
2170 }
reed@google.com569e0432011-04-05 13:07:03 +00002171
2172 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
2173 title.prepend(trystate_str(fAAState, "AA ", "aa "));
bungeman@google.com96aabc82013-06-03 21:26:34 +00002174 title.prepend(trystate_str(fFilterState, "N ", "n "));
2175 title.prepend(trystate_str(fSubpixelState, "S ", "s "));
reed@google.com569e0432011-04-05 13:07:03 +00002176 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
2177 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002178 title.prepend(gHintingStates[fHintingState].label);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002179
2180 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00002181 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002182 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002183
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002184 if (fMeasureFPS) {
reed@google.com43e10142012-09-10 11:52:52 +00002185 title.appendf(" %8.3f ms", fMeasureFPS_Time / (float)FPS_REPEAT_COUNT);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002186 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002187 if (SampleView::IsSampleView(view)) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002188 switch (fPipeState) {
2189 case SkOSMenu::kOnState:
2190 title.prepend("<Pipe> ");
2191 break;
2192 case SkOSMenu::kMixedState:
2193 title.prepend("<Tiled Pipe> ");
2194 break;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002195
scroggo@google.comb073d922012-06-08 15:35:03 +00002196 default:
2197 break;
2198 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002199 title.prepend("! ");
2200 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002201
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002202#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00002203 if (IsGpuDeviceType(fDeviceType) &&
robertphillips@google.com4750fa52012-04-10 13:34:11 +00002204 NULL != fDevManager &&
bsalomon@google.com82502e22013-01-24 20:47:18 +00002205 fDevManager->getGrRenderTarget() &&
bsalomon@google.com11959252012-04-06 20:13:38 +00002206 fDevManager->getGrRenderTarget()->numSamples() > 0) {
2207 title.appendf(" [MSAA: %d]",
2208 fDevManager->getGrRenderTarget()->numSamples());
2209 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002210#endif
bsalomon@google.com11959252012-04-06 20:13:38 +00002211
reed@android.com8a1c16f2008-12-17 15:59:43 +00002212 this->setTitle(title.c_str());
2213}
2214
2215void SampleWindow::onSizeChange() {
2216 this->INHERITED::onSizeChange();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002217
reed@android.com8a1c16f2008-12-17 15:59:43 +00002218 SkView::F2BIter iter(this);
2219 SkView* view = iter.next();
2220 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002221
reed@android.com8a1c16f2008-12-17 15:59:43 +00002222 // rebuild our clippath
2223 {
2224 const SkScalar W = this->width();
2225 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002226
reed@android.com8a1c16f2008-12-17 15:59:43 +00002227 fClipPath.reset();
2228#if 0
2229 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
2230 SkRect r;
2231 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
2232 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
2233 fClipPath.addRect(r);
2234 }
2235#else
2236 SkRect r;
2237 r.set(0, 0, W, H);
2238 fClipPath.addRect(r, SkPath::kCCW_Direction);
2239 r.set(W/4, H/4, W*3/4, H*3/4);
2240 fClipPath.addRect(r, SkPath::kCW_Direction);
2241#endif
2242 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002243
Scroggo2c8208f2011-06-15 16:49:08 +00002244 fZoomCenterX = SkScalarHalf(this->width());
2245 fZoomCenterY = SkScalarHalf(this->height());
2246
djsollen@google.com56c69772011-11-08 19:00:26 +00002247#ifdef SK_BUILD_FOR_ANDROID
Scroggob4490c72011-06-17 13:53:05 +00002248 // FIXME: The first draw after a size change does not work on Android, so
2249 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00002250 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00002251#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00002252 this->updateTitle(); // to refresh our config
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002253 fDevManager->windowSizeChanged(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002254}
2255
2256///////////////////////////////////////////////////////////////////////////////
2257
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002258static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002259static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00002260static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002261
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002262bool SampleView::IsSampleView(SkView* view) {
2263 SkEvent evt(is_sample_view_tag);
2264 return view->doQuery(&evt);
2265}
2266
reed@google.comf2183392011-04-22 14:10:48 +00002267bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002268 SkEvent evt(repeat_count_tag);
2269 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00002270 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002271}
2272
scroggo@google.comb073d922012-06-08 15:35:03 +00002273bool SampleView::SetUsePipe(SkView* view, SkOSMenu::TriState state) {
2274 SkEvent evt;
2275 evt.setS32(set_use_pipe_tag, state);
reed@google.com0faac1e2011-05-11 05:58:58 +00002276 return view->doEvent(evt);
2277}
2278
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002279bool SampleView::onEvent(const SkEvent& evt) {
2280 if (evt.isType(repeat_count_tag)) {
2281 fRepeatCount = evt.getFast32();
2282 return true;
2283 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002284
scroggo@google.comb073d922012-06-08 15:35:03 +00002285 int32_t pipeHolder;
2286 if (evt.findS32(set_use_pipe_tag, &pipeHolder)) {
2287 fPipeState = static_cast<SkOSMenu::TriState>(pipeHolder);
reed@google.com0faac1e2011-05-11 05:58:58 +00002288 return true;
2289 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002290
2291 if (evt.isType("debug-hit-test")) {
2292 fDebugHitTest = true;
2293 evt.findS32("debug-hit-test-x", &fDebugHitTestLoc.fX);
2294 evt.findS32("debug-hit-test-y", &fDebugHitTestLoc.fY);
2295 this->inval(NULL);
2296 return true;
2297 }
2298
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002299 return this->INHERITED::onEvent(evt);
2300}
2301
2302bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002303 if (evt->isType(is_sample_view_tag)) {
2304 return true;
2305 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002306 return this->INHERITED::onQuery(evt);
2307}
2308
reed@google.com64e3eb22011-05-04 14:32:04 +00002309
2310class SimplePC : public SkGPipeController {
2311public:
2312 SimplePC(SkCanvas* target);
2313 ~SimplePC();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002314
reed@google.com64e3eb22011-05-04 14:32:04 +00002315 virtual void* requestBlock(size_t minRequest, size_t* actual);
2316 virtual void notifyWritten(size_t bytes);
2317
2318private:
reed@google.com961ddb02011-05-05 14:03:48 +00002319 SkGPipeReader fReader;
2320 void* fBlock;
2321 size_t fBlockSize;
2322 size_t fBytesWritten;
2323 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00002324 SkGPipeReader::Status fStatus;
2325
2326 size_t fTotalWritten;
2327};
2328
2329SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
2330 fBlock = NULL;
2331 fBlockSize = fBytesWritten = 0;
2332 fStatus = SkGPipeReader::kDone_Status;
2333 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00002334 fAtomsWritten = 0;
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00002335 fReader.setBitmapDecoder(&SkImageDecoder::DecodeMemory);
reed@google.com64e3eb22011-05-04 14:32:04 +00002336}
2337
2338SimplePC::~SimplePC() {
2339// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
reed@google.com0faac1e2011-05-11 05:58:58 +00002340 if (fTotalWritten) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002341 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
2342 fAtomsWritten, fStatus);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002343#ifdef PIPE_FILE
scroggo@google.comb073d922012-06-08 15:35:03 +00002344 //File is open in append mode
2345 FILE* f = fopen(FILE_PATH, "ab");
2346 SkASSERT(f != NULL);
2347 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
2348 fclose(f);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002349#endif
2350#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00002351 if (fAtomsWritten > 1 && fTotalWritten > 4) { //ignore done
2352 gServer.acceptConnections();
2353 gServer.writePacket(fBlock, fTotalWritten);
2354 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002355#endif
reed@google.com0faac1e2011-05-11 05:58:58 +00002356 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002357 sk_free(fBlock);
reed@google.com64e3eb22011-05-04 14:32:04 +00002358}
2359
2360void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
2361 sk_free(fBlock);
2362
2363 fBlockSize = minRequest * 4;
2364 fBlock = sk_malloc_throw(fBlockSize);
2365 fBytesWritten = 0;
2366 *actual = fBlockSize;
2367 return fBlock;
2368}
2369
2370void SimplePC::notifyWritten(size_t bytes) {
2371 SkASSERT(fBytesWritten + bytes <= fBlockSize);
reed@google.com64e3eb22011-05-04 14:32:04 +00002372 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
2373 SkASSERT(SkGPipeReader::kError_Status != fStatus);
2374 fBytesWritten += bytes;
2375 fTotalWritten += bytes;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002376
reed@google.com961ddb02011-05-05 14:03:48 +00002377 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00002378}
2379
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002380void SampleView::draw(SkCanvas* canvas) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002381 if (SkOSMenu::kOffState == fPipeState) {
2382 this->INHERITED::draw(canvas);
2383 } else {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002384 SkGPipeWriter writer;
2385 SimplePC controller(canvas);
scroggo@google.comb073d922012-06-08 15:35:03 +00002386 TiledPipeController tc(canvas->getDevice()->accessBitmap(false),
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00002387 &SkImageDecoder::DecodeMemory,
scroggo@google.comb073d922012-06-08 15:35:03 +00002388 &canvas->getTotalMatrix());
2389 SkGPipeController* pc;
2390 if (SkOSMenu::kMixedState == fPipeState) {
2391 pc = &tc;
2392 } else {
2393 pc = &controller;
2394 }
reed@google.comdde09562011-05-23 12:21:05 +00002395 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002396
scroggo@google.comb073d922012-06-08 15:35:03 +00002397 canvas = writer.startRecording(pc, flags);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002398 //Must draw before controller goes out of scope and sends data
2399 this->INHERITED::draw(canvas);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00002400 //explicitly end recording to ensure writer is flushed before the memory
2401 //is freed in the deconstructor of the controller
2402 writer.endRecording();
reed@google.com0faac1e2011-05-11 05:58:58 +00002403 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002404}
reed@google.com4d5c26d2013-01-08 16:17:50 +00002405
2406#include "SkBounder.h"
2407
2408class DebugHitTestBounder : public SkBounder {
2409public:
2410 DebugHitTestBounder(int x, int y) {
2411 fLoc.set(x, y);
2412 }
2413
2414 virtual bool onIRect(const SkIRect& bounds) SK_OVERRIDE {
2415 if (bounds.contains(fLoc.x(), fLoc.y())) {
2416 //
2417 // Set a break-point here to see what was being drawn under
2418 // the click point (just needed a line of code to stop the debugger)
2419 //
2420 bounds.centerX();
2421 }
2422 return true;
2423 }
2424
2425private:
2426 SkIPoint fLoc;
2427 typedef SkBounder INHERITED;
2428};
2429
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002430void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com81e3d7f2011-06-01 12:42:36 +00002431 this->onDrawBackground(canvas);
2432
reed@google.com4d5c26d2013-01-08 16:17:50 +00002433 DebugHitTestBounder bounder(fDebugHitTestLoc.x(), fDebugHitTestLoc.y());
2434 if (fDebugHitTest) {
2435 canvas->setBounder(&bounder);
2436 }
2437
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002438 for (int i = 0; i < fRepeatCount; i++) {
scroggo@google.com85cade02012-08-17 13:29:50 +00002439 SkAutoCanvasRestore acr(canvas, true);
2440 this->onDrawContent(canvas);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002441 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002442
2443 fDebugHitTest = false;
2444 canvas->setBounder(NULL);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002445}
2446
2447void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00002448 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002449}
2450
2451///////////////////////////////////////////////////////////////////////////////
2452
reed@android.comf2b98d62010-12-20 18:26:13 +00002453template <typename T> void SkTBSort(T array[], int count) {
2454 for (int i = 1; i < count - 1; i++) {
2455 bool didSwap = false;
2456 for (int j = count - 1; j > i; --j) {
2457 if (array[j] < array[j-1]) {
2458 T tmp(array[j-1]);
2459 array[j-1] = array[j];
2460 array[j] = tmp;
2461 didSwap = true;
2462 }
2463 }
2464 if (!didSwap) {
2465 break;
2466 }
2467 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002468
reed@android.comf2b98d62010-12-20 18:26:13 +00002469 for (int k = 0; k < count - 1; k++) {
2470 SkASSERT(!(array[k+1] < array[k]));
2471 }
2472}
2473
2474#include "SkRandom.h"
2475
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002476static void rand_rect(SkIRect* rect, SkRandom& rand) {
reed@android.comf2b98d62010-12-20 18:26:13 +00002477 int bits = 8;
2478 int shift = 32 - bits;
2479 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
2480 rand.nextU() >> shift, rand.nextU() >> shift);
2481 rect->sort();
2482}
2483
2484static void dumpRect(const SkIRect& r) {
2485 SkDebugf(" { %d, %d, %d, %d },\n",
2486 r.fLeft, r.fTop,
2487 r.fRight, r.fBottom);
2488}
2489
2490static void test_rects(const SkIRect rect[], int count) {
2491 SkRegion rgn0, rgn1;
2492
2493 for (int i = 0; i < count; i++) {
2494 rgn0.op(rect[i], SkRegion::kUnion_Op);
2495 // dumpRect(rect[i]);
2496 }
2497 rgn1.setRects(rect, count);
2498
2499 if (rgn0 != rgn1) {
2500 SkDebugf("\n");
2501 for (int i = 0; i < count; i++) {
2502 dumpRect(rect[i]);
2503 }
2504 SkDebugf("\n");
2505 }
2506}
2507
2508static void test() {
2509 size_t i;
2510
2511 const SkIRect r0[] = {
2512 { 0, 0, 1, 1 },
2513 { 2, 2, 3, 3 },
2514 };
2515 const SkIRect r1[] = {
2516 { 0, 0, 1, 3 },
2517 { 1, 1, 2, 2 },
2518 { 2, 0, 3, 3 },
2519 };
2520 const SkIRect r2[] = {
2521 { 0, 0, 1, 2 },
2522 { 2, 1, 3, 3 },
2523 { 4, 0, 5, 1 },
2524 { 6, 0, 7, 4 },
2525 };
2526
2527 static const struct {
2528 const SkIRect* fRects;
2529 int fCount;
2530 } gRecs[] = {
2531 { r0, SK_ARRAY_COUNT(r0) },
2532 { r1, SK_ARRAY_COUNT(r1) },
2533 { r2, SK_ARRAY_COUNT(r2) },
2534 };
2535
2536 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
2537 test_rects(gRecs[i].fRects, gRecs[i].fCount);
2538 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002539
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002540 SkRandom rand;
reed@android.comf2b98d62010-12-20 18:26:13 +00002541 for (i = 0; i < 10000; i++) {
2542 SkRegion rgn0, rgn1;
2543
2544 const int N = 8;
2545 SkIRect rect[N];
2546 for (int j = 0; j < N; j++) {
2547 rand_rect(&rect[j], rand);
2548 }
2549 test_rects(rect, N);
2550 }
2551}
2552
caryclark@google.com02939ce2012-06-06 12:09:51 +00002553// FIXME: this should be in a header
2554SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00002555SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
caryclark@google.com02939ce2012-06-06 12:09:51 +00002556 if (false) { // avoid bit rot, suppress warning
2557 test();
2558 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002559 return new SampleWindow(hwnd, argc, argv, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002560}
2561
caryclark@google.com02939ce2012-06-06 12:09:51 +00002562// FIXME: this should be in a header
2563void get_preferred_size(int* x, int* y, int* width, int* height);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002564void get_preferred_size(int* x, int* y, int* width, int* height) {
2565 *x = 10;
2566 *y = 50;
2567 *width = 640;
2568 *height = 480;
2569}
2570
caryclark@google.com5987f582012-10-02 18:33:14 +00002571#ifdef SK_BUILD_FOR_IOS
2572void save_args(int argc, char *argv[]) {
2573}
2574#endif
2575
caryclark@google.com02939ce2012-06-06 12:09:51 +00002576// FIXME: this should be in a header
2577void application_init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002578void application_init() {
2579// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002580#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00002581 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002582#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002583 SkGraphics::Init();
2584 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002585}
2586
caryclark@google.com02939ce2012-06-06 12:09:51 +00002587// FIXME: this should be in a header
2588void application_term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002589void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002590 SkEvent::Term();
2591 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002592}