blob: 287639ad88b2523f393316f3ad2a0f22b5598380 [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
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +0000102static void post_event_to_sink(SkEvent* evt, SkEventSink* sink) {
reed@google.com2072db82011-11-08 15:18:10 +0000103 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
reed@google.com7fa2a652014-01-27 13:42:58 +0000131 size_t len = stream.getLength();
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000132 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);
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000213 SkAutoTUnref<const GrGLInterface> glInterface;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000214 switch (win->getDeviceType()) {
215 case kRaster_DeviceType:
216 // fallthrough
217 case kPicture_DeviceType:
218 // fallthrough
219 case kGPU_DeviceType:
220 // all these guys use the native interface
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000221 glInterface.reset(GrGLCreateNativeInterface());
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000222 break;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000223#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000224 case kANGLE_DeviceType:
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000225 glInterface.reset(GrGLCreateANGLEInterface());
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000226 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000227#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000228 case kNullGPU_DeviceType:
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000229 glInterface.reset(GrGLCreateNullInterface());
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000230 break;
231 default:
232 SkASSERT(false);
233 break;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000234 }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000235
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000236 // Currently SampleApp does not use NVPR. TODO: Provide an NVPR device type that is skipped
237 // when the driver doesn't support NVPR.
238 fCurIntf = GrGLInterfaceRemoveNVPR(glInterface.get());
239
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000240 SkASSERT(NULL == fCurContext);
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000241 fCurContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000242
243 if (NULL == fCurContext || NULL == fCurIntf) {
244 // We need some context and interface to see results
245 SkSafeUnref(fCurContext);
246 SkSafeUnref(fCurIntf);
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000247 fCurContext = NULL;
248 fCurIntf = NULL;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000249 SkDebugf("Failed to setup 3D");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000250
251 win->detach();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000252 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000253#endif // SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000254 // call windowSizeChanged to create the render target
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000255 this->windowSizeChanged(win);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000256 }
257
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000258 virtual void tearDownBackend(SampleWindow *win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000259#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000260 SkSafeUnref(fCurContext);
261 fCurContext = NULL;
262
263 SkSafeUnref(fCurIntf);
264 fCurIntf = NULL;
265
266 SkSafeUnref(fCurRenderTarget);
267 fCurRenderTarget = NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000268#endif
bsalomon@google.comddd40e52012-04-10 15:56:29 +0000269 win->detach();
270 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000271 }
272
reed@google.com5957f472012-10-01 20:31:56 +0000273 virtual SkCanvas* createCanvas(SampleWindow::DeviceType dType,
274 SampleWindow* win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000275#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +0000276 if (IsGpuDeviceType(dType) && NULL != fCurContext) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000277 SkAutoTUnref<SkBaseDevice> device(new SkGpuDevice(fCurContext, fCurRenderTarget));
bsalomon@google.com82502e22013-01-24 20:47:18 +0000278 return new SkCanvas(device);
279 } else
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000280#endif
bsalomon@google.com82502e22013-01-24 20:47:18 +0000281 {
282 return NULL;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000283 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000284 }
285
286 virtual void publishCanvas(SampleWindow::DeviceType dType,
287 SkCanvas* canvas,
288 SampleWindow* win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000289#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000290 if (fCurContext) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000291 // in case we have queued drawing calls
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000292 fCurContext->flush();
293
bsalomon@google.com82502e22013-01-24 20:47:18 +0000294 if (!IsGpuDeviceType(dType)) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000295 // need to send the raster bits to the (gpu) window
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000296 fCurContext->setRenderTarget(fCurRenderTarget);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000297 const SkBitmap& bm = win->getBitmap();
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000298 fCurRenderTarget->writePixels(0, 0, bm.width(), bm.height(),
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000299 kSkia8888_GrPixelConfig,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000300 bm.getPixels(),
301 bm.rowBytes());
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000302 }
303 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000304#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000305
306 win->present();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000307 }
308
309 virtual void windowSizeChanged(SampleWindow* win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000310#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000311 if (fCurContext) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000312 AttachmentInfo attachmentInfo;
313 win->attach(fBackend, fMSAASampleCount, &attachmentInfo);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000314
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000315 GrBackendRenderTargetDesc desc;
reed@google.come1ca7052013-12-17 19:22:07 +0000316 desc.fWidth = SkScalarRoundToInt(win->width());
317 desc.fHeight = SkScalarRoundToInt(win->height());
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000318 desc.fConfig = kSkia8888_GrPixelConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000319 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000320 desc.fSampleCnt = attachmentInfo.fSampleCount;
321 desc.fStencilBits = attachmentInfo.fStencilBits;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000322 GrGLint buffer;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000323 GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
bsalomon@google.come269f212011-11-07 13:29:52 +0000324 desc.fRenderTargetHandle = buffer;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000325
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000326 SkSafeUnref(fCurRenderTarget);
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000327 fCurRenderTarget = fCurContext->wrapBackendRenderTarget(desc);
bsalomon@google.com74913722011-10-27 20:44:19 +0000328 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000329#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000330 }
331
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000332 virtual GrContext* getGrContext() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000333#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000334 return fCurContext;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000335#else
336 return NULL;
337#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000338 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000339
340 virtual GrRenderTarget* getGrRenderTarget() SK_OVERRIDE {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000341#if SK_SUPPORT_GPU
bsalomon@google.com11959252012-04-06 20:13:38 +0000342 return fCurRenderTarget;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000343#else
344 return NULL;
345#endif
bsalomon@google.com11959252012-04-06 20:13:38 +0000346 }
347
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000348private:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000349
350#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000351 GrContext* fCurContext;
352 const GrGLInterface* fCurIntf;
353 GrRenderTarget* fCurRenderTarget;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000354 int fMSAASampleCount;
355#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000356
357 SkOSWindow::SkBackEndTypes fBackend;
358
359 typedef SampleWindow::DeviceManager INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000360};
361
362///////////////
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000363static const char view_inval_msg[] = "view-inval-msg";
364
Scroggo62b65b02011-06-21 16:01:26 +0000365void SampleWindow::postInvalDelay() {
reed@google.com87fac4a2011-08-04 13:50:17 +0000366 (new SkEvent(view_inval_msg, this->getSinkID()))->postDelay(1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000367}
368
369static bool isInvalEvent(const SkEvent& evt) {
370 return evt.isType(view_inval_msg);
371}
372//////////////////
373
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000374SkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func)
375 : fCreateFunc(func) {
376}
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000377
bsalomon@google.com8301de12011-10-31 16:47:13 +0000378SkView* SkFuncViewFactory::operator() () const {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000379 return (*fCreateFunc)();
380}
381
382#include "GMSampleView.h"
383
384SkGMSampleViewFactory::SkGMSampleViewFactory(GMFactoryFunc func)
385 : fFunc(func) {
386}
387
388SkView* SkGMSampleViewFactory::operator() () const {
389 return new GMSampleView(fFunc(NULL));
390}
391
392SkViewRegister* SkViewRegister::gHead;
393SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) {
394 fFact->ref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000395 fChain = gHead;
396 gHead = this;
397}
398
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000399SkViewRegister::SkViewRegister(SkViewCreateFunc func) {
400 fFact = new SkFuncViewFactory(func);
401 fChain = gHead;
402 gHead = this;
403}
404
405SkViewRegister::SkViewRegister(GMFactoryFunc func) {
406 fFact = new SkGMSampleViewFactory(func);
407 fChain = gHead;
408 gHead = this;
409}
410
411class AutoUnrefArray {
412public:
413 AutoUnrefArray() {}
414 ~AutoUnrefArray() {
415 int count = fObjs.count();
416 for (int i = 0; i < count; ++i) {
417 fObjs[i]->unref();
418 }
419 }
420 SkRefCnt*& push_back() { return *fObjs.append(); }
chudy@google.com4605a3f2012-08-01 17:58:01 +0000421
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000422private:
423 SkTDArray<SkRefCnt*> fObjs;
424};
425
426// registers GMs as Samples
427// This can't be performed during static initialization because it could be
428// run before GMRegistry has been fully built.
caryclark@google.com02939ce2012-06-06 12:09:51 +0000429static void SkGMRegistyToSampleRegistry() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000430 static bool gOnce;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000431 static AutoUnrefArray fRegisters;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000432
433 if (!gOnce) {
434 const skiagm::GMRegistry* gmreg = skiagm::GMRegistry::Head();
435 while (gmreg) {
436 fRegisters.push_back() = new SkViewRegister(gmreg->factory());
437 gmreg = gmreg->next();
438 }
439 gOnce = true;
440 }
441}
442
reed@google.com29038ed2011-07-06 17:56:47 +0000443#if 0
reed@google.comf0b5f682011-03-11 20:08:25 +0000444#include <CoreFoundation/CoreFoundation.h>
445#include <CoreFoundation/CFURLAccess.h>
446
447static void testpdf() {
448 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
449 kCFStringEncodingUTF8);
450 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
451 kCFURLPOSIXPathStyle,
452 false);
453 CFRelease(path);
454 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
455 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
456 CFRelease(url);
457
458 CGContextBeginPage(cg, &box);
459 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
460 CGContextFillEllipseInRect(cg, r);
461 CGContextEndPage(cg);
462 CGContextRelease(cg);
463
464 if (false) {
465 SkBitmap bm;
466 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
467 bm.allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +0000468 bm.eraseColor(SK_ColorTRANSPARENT);
reed@google.comf0b5f682011-03-11 20:08:25 +0000469
470 SkCanvas canvas(bm);
471
472 }
473}
474#endif
475
476//////////////////////////////////////////////////////////////////////////////
477
reed@google.com569e0432011-04-05 13:07:03 +0000478enum FlipAxisEnum {
479 kFlipAxis_X = (1 << 0),
480 kFlipAxis_Y = (1 << 1)
481};
482
reed@google.comf0b5f682011-03-11 20:08:25 +0000483#include "SkDrawFilter.h"
484
bungeman@google.com96aabc82013-06-03 21:26:34 +0000485struct HintingState {
486 SkPaint::Hinting hinting;
487 const char* name;
488 const char* label;
489};
490static HintingState gHintingStates[] = {
491 {SkPaint::kNo_Hinting, "Mixed", NULL },
492 {SkPaint::kNo_Hinting, "None", "H0 " },
493 {SkPaint::kSlight_Hinting, "Slight", "Hs " },
494 {SkPaint::kNormal_Hinting, "Normal", "Hn " },
495 {SkPaint::kFull_Hinting, "Full", "Hf " },
496};
497
reed@google.com15bc13d2013-12-10 16:53:13 +0000498struct FilterLevelState {
499 SkPaint::FilterLevel fLevel;
500 const char* fName;
501 const char* fLabel;
502};
503static FilterLevelState gFilterLevelStates[] = {
504 { SkPaint::kNone_FilterLevel, "Mixed", NULL },
505 { SkPaint::kNone_FilterLevel, "None", "F0 " },
506 { SkPaint::kLow_FilterLevel, "Low", "F1 " },
507 { SkPaint::kMedium_FilterLevel, "Medium", "F2 " },
508 { SkPaint::kHigh_FilterLevel, "High", "F3 " },
509};
510
reed@google.com569e0432011-04-05 13:07:03 +0000511class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000512public:
reed@google.com15bc13d2013-12-10 16:53:13 +0000513 FlagsDrawFilter(SkOSMenu::TriState lcd, SkOSMenu::TriState aa,
514 SkOSMenu::TriState subpixel, int hinting, int filterlevel)
515 : fLCDState(lcd)
516 , fAAState(aa)
517 , fSubpixelState(subpixel)
518 , fHintingState(hinting)
519 , fFilterLevelIndex(filterlevel)
520 {
521 SkASSERT((unsigned)filterlevel < SK_ARRAY_COUNT(gFilterLevelStates));
522 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000523
reed@google.com971aca72012-11-26 20:26:54 +0000524 virtual bool filter(SkPaint* paint, Type t) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000525 if (kText_Type == t && SkOSMenu::kMixedState != fLCDState) {
526 paint->setLCDRenderText(SkOSMenu::kOnState == fLCDState);
reed@google.com569e0432011-04-05 13:07:03 +0000527 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000528 if (SkOSMenu::kMixedState != fAAState) {
529 paint->setAntiAlias(SkOSMenu::kOnState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000530 }
reed@google.com15bc13d2013-12-10 16:53:13 +0000531 if (0 != fFilterLevelIndex) {
532 paint->setFilterLevel(gFilterLevelStates[fFilterLevelIndex].fLevel);
reed@google.com176753a2011-05-17 15:32:04 +0000533 }
bungeman@google.com96aabc82013-06-03 21:26:34 +0000534 if (SkOSMenu::kMixedState != fSubpixelState) {
535 paint->setSubpixelText(SkOSMenu::kOnState == fSubpixelState);
536 }
bungeman@google.com055aa522013-06-03 21:35:03 +0000537 if (0 != fHintingState && fHintingState < (int)SK_ARRAY_COUNT(gHintingStates)) {
bungeman@google.com96aabc82013-06-03 21:26:34 +0000538 paint->setHinting(gHintingStates[fHintingState].hinting);
reed@google.com09e3baa2011-05-18 12:04:31 +0000539 }
reed@google.com971aca72012-11-26 20:26:54 +0000540 return true;
reed@google.comf0b5f682011-03-11 20:08:25 +0000541 }
542
543private:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000544 SkOSMenu::TriState fLCDState;
545 SkOSMenu::TriState fAAState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000546 SkOSMenu::TriState fSubpixelState;
547 int fHintingState;
reed@google.com15bc13d2013-12-10 16:53:13 +0000548 int fFilterLevelIndex;
reed@google.comf0b5f682011-03-11 20:08:25 +0000549};
550
reed@android.com8a1c16f2008-12-17 15:59:43 +0000551//////////////////////////////////////////////////////////////////////////////
552
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000553#define MAX_ZOOM_LEVEL 8
554#define MIN_ZOOM_LEVEL -8
555
reed@android.comf2b98d62010-12-20 18:26:13 +0000556static const char gCharEvtName[] = "SampleCode_Char_Event";
557static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000558static const char gTitleEvtName[] = "SampleCode_Title_Event";
559static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000560static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
reed@google.com2072db82011-11-08 15:18:10 +0000561static const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle";
reed@android.comf2b98d62010-12-20 18:26:13 +0000562
563bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
564 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
565 if (outUni) {
566 *outUni = evt.getFast32();
567 }
568 return true;
569 }
570 return false;
571}
572
573bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
574 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
575 if (outKey) {
576 *outKey = (SkKey)evt.getFast32();
577 }
578 return true;
579 }
580 return false;
581}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000582
583bool SampleCode::TitleQ(const SkEvent& evt) {
584 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
585}
586
587void SampleCode::TitleR(SkEvent* evt, const char title[]) {
588 SkASSERT(evt && TitleQ(*evt));
589 evt->setString(gTitleEvtName, title);
590}
591
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000592bool SampleCode::RequestTitle(SkView* view, SkString* title) {
593 SkEvent evt(gTitleEvtName);
594 if (view->doQuery(&evt)) {
595 title->set(evt.findString(gTitleEvtName));
596 return true;
597 }
598 return false;
599}
600
reed@android.com8a1c16f2008-12-17 15:59:43 +0000601bool SampleCode::PrefSizeQ(const SkEvent& evt) {
602 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
603}
604
605void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
606 SkASSERT(evt && PrefSizeQ(*evt));
607 SkScalar size[2];
608 size[0] = width;
609 size[1] = height;
610 evt->setScalars(gPrefSizeEvtName, 2, size);
611}
612
reed@android.comf2b98d62010-12-20 18:26:13 +0000613bool SampleCode::FastTextQ(const SkEvent& evt) {
614 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
615}
616
617///////////////////////////////////////////////////////////////////////////////
618
reed@android.com44177402009-11-23 21:07:51 +0000619static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000620static SkMSec gAnimTimePrev;
621
reed@android.com44177402009-11-23 21:07:51 +0000622SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000623SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
624SkScalar SampleCode::GetAnimSecondsDelta() {
625 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
626}
reed@android.com44177402009-11-23 21:07:51 +0000627
628SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000629 // since gAnimTime can be up to 32 bits, we can't convert it to a float
630 // or we'll lose the low bits. Hence we use doubles for the intermediate
631 // calculations
632 double seconds = (double)gAnimTime / 1000.0;
633 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000634 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000635 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000636 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000637 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000638}
639
bsalomon@google.com85003222012-03-28 14:44:37 +0000640SkScalar SampleCode::GetAnimSinScalar(SkScalar amplitude,
641 SkScalar periodInSec,
642 SkScalar phaseInSec) {
643 if (!periodInSec) {
644 return 0;
645 }
646 double t = (double)gAnimTime / 1000.0 + phaseInSec;
647 t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
648 amplitude = SK_ScalarHalf * amplitude;
649 return SkScalarMul(amplitude, SkDoubleToScalar(sin(t))) + amplitude;
650}
651
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000652enum TilingMode {
653 kNo_Tiling,
654 kAbs_128x128_Tiling,
655 kAbs_256x256_Tiling,
656 kRel_4x4_Tiling,
657 kRel_1x16_Tiling,
658 kRel_16x1_Tiling,
659
660 kLast_TilingMode_Enum
661};
662
663struct TilingInfo {
664 const char* label;
665 SkScalar w, h;
666};
667
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000668static const struct TilingInfo gTilingInfo[] = {
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000669 { "No tiling", SK_Scalar1 , SK_Scalar1 }, // kNo_Tiling
670 { "128x128" , SkIntToScalar(128), SkIntToScalar(128) }, // kAbs_128x128_Tiling
671 { "256x256" , SkIntToScalar(256), SkIntToScalar(256) }, // kAbs_256x256_Tiling
672 { "1/4x1/4" , SK_Scalar1 / 4 , SK_Scalar1 / 4 }, // kRel_4x4_Tiling
673 { "1/1x1/16" , SK_Scalar1 , SK_Scalar1 / 16 }, // kRel_1x16_Tiling
674 { "1/16x1/1" , SK_Scalar1 / 16 , SK_Scalar1 }, // kRel_16x1_Tiling
675};
676SK_COMPILE_ASSERT((SK_ARRAY_COUNT(gTilingInfo) == kLast_TilingMode_Enum),
677 Incomplete_tiling_labels);
678
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000679SkSize SampleWindow::tileSize() const {
680 SkASSERT((TilingMode)fTilingMode < kLast_TilingMode_Enum);
681 const struct TilingInfo* info = gTilingInfo + fTilingMode;
682 return SkSize::Make(info->w > SK_Scalar1 ? info->w : this->width() * info->w,
683 info->h > SK_Scalar1 ? info->h : this->height() * info->h);
684}
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000685//////////////////////////////////////////////////////////////////////////////
686
reed@android.comf2b98d62010-12-20 18:26:13 +0000687static SkView* curr_view(SkWindow* wind) {
688 SkView::F2BIter iter(wind);
689 return iter.next();
690}
691
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000692static bool curr_title(SkWindow* wind, SkString* title) {
693 SkView* view = curr_view(wind);
694 if (view) {
695 SkEvent evt(gTitleEvtName);
696 if (view->doQuery(&evt)) {
697 title->set(evt.findString(gTitleEvtName));
698 return true;
699 }
700 }
701 return false;
702}
703
Scroggo2c8208f2011-06-15 16:49:08 +0000704void SampleWindow::setZoomCenter(float x, float y)
705{
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000706 fZoomCenterX = x;
707 fZoomCenterY = y;
Scroggo2c8208f2011-06-15 16:49:08 +0000708}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000709
Scroggo0f185c22011-03-24 18:35:50 +0000710bool SampleWindow::zoomIn()
711{
712 // Arbitrarily decided
713 if (fFatBitsScale == 25) return false;
714 fFatBitsScale++;
715 this->inval(NULL);
716 return true;
717}
718
719bool SampleWindow::zoomOut()
720{
721 if (fFatBitsScale == 1) return false;
722 fFatBitsScale--;
723 this->inval(NULL);
724 return true;
725}
726
Scroggo0f185c22011-03-24 18:35:50 +0000727void SampleWindow::updatePointer(int x, int y)
728{
729 fMouseX = x;
730 fMouseY = y;
731 if (fShowZoomer) {
732 this->inval(NULL);
733 }
734}
735
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000736static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
737 static const SampleWindow::DeviceType gCT[] = {
738 SampleWindow::kPicture_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000739#if SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000740 SampleWindow::kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000741#if SK_ANGLE
742 SampleWindow::kANGLE_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000743#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +0000744 SampleWindow::kRaster_DeviceType, // skip the null gpu device in normal cycling
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000745#endif // SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000746 SampleWindow::kRaster_DeviceType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000747 };
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000748 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gCT) == SampleWindow::kDeviceTypeCnt, array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000749 return gCT[ct];
750}
751
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000752static void usage(const char * argv0) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000753 SkDebugf("%s [--slide sampleName] [-i resourcePath] [--msaa sampleCount] [--pictureDir dirPath] [--picture path] [--sort]\n", argv0);
754#ifdef SAMPLE_PDF_FILE_VIEWER
755 SkDebugf(" [--pdfDir pdfPath]\n");
756 SkDebugf(" pdfPath: path to directory pdf files are read from\n");
757#endif // SAMPLE_PDF_FILE_VIEWER
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000758 SkDebugf(" sampleName: sample at which to start.\n");
759 SkDebugf(" resourcePath: directory that stores image resources.\n");
bsalomon@google.com11959252012-04-06 20:13:38 +0000760 SkDebugf(" msaa: request multisampling with the given sample count.\n");
chudy@google.com4605a3f2012-08-01 17:58:01 +0000761 SkDebugf(" dirPath: path to directory skia pictures are read from\n");
762 SkDebugf(" path: path to skia picture\n");
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000763 SkDebugf(" --sort: sort samples by title, this would help to compare pdf rendering (P:foo.pdf) with skp rendering (P:foo.pdf)\n");
764}
765
766static SkString getSampleTitle(const SkViewFactory* sampleFactory) {
767 SkView* view = (*sampleFactory)();
768 SkString title;
769 SampleCode::RequestTitle(view, &title);
770 view->unref();
771 return title;
772}
773
edisonn@google.comde36b692013-07-11 15:40:31 +0000774static bool compareSampleTitle(const SkViewFactory* first, const SkViewFactory* second) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000775 return strcmp(getSampleTitle(first).c_str(), getSampleTitle(second).c_str()) < 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000776}
777
chudy@google.com4605a3f2012-08-01 17:58:01 +0000778SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager)
robertphillips@google.com4750fa52012-04-10 13:34:11 +0000779 : INHERITED(hwnd)
780 , fDevManager(NULL) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000781
reed@google.com7d05ac92013-06-06 13:14:13 +0000782 fCurrIndex = -1;
783
reed@google.com1830c7a2012-06-04 12:05:43 +0000784 this->registerPictFileSamples(argv, argc);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000785 this->registerPictFileSample(argv, argc);
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000786#ifdef SAMPLE_PDF_FILE_VIEWER
787 this->registerPdfFileViewerSamples(argv, argc);
788#endif // SAMPLE_PDF_FILE_VIEWER
robertphillips@google.com76d40212012-03-22 14:12:15 +0000789 SkGMRegistyToSampleRegistry();
790 {
791 const SkViewRegister* reg = SkViewRegister::Head();
792 while (reg) {
793 *fSamples.append() = reg->factory();
794 reg = reg->next();
795 }
796 }
797
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000798 bool sort = false;
799 for (int i = 0; i < argc; ++i) {
800 if (!strcmp(argv[i], "--sort")) {
801 sort = true;
802 break;
803 }
804 }
805
806 if (sort) {
807 // Sort samples, so foo.skp and foo.pdf are consecutive and we can quickly spot where
808 // skp -> pdf -> png fails.
809 SkTQSort(fSamples.begin(), fSamples.end() ? fSamples.end() - 1 : NULL, compareSampleTitle);
810 }
811
halcanary@google.com9acb8cd2014-01-10 14:53:49 +0000812 const char* resourcePath = "resources"; // same default as tests
bsalomon@google.com11959252012-04-06 20:13:38 +0000813 fMSAASampleCount = 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000814
815 const char* const commandName = argv[0];
816 char* const* stop = argv + argc;
817 for (++argv; argv < stop; ++argv) {
reed@google.com55ebe8e2013-09-10 19:12:07 +0000818 if (!strcmp(*argv, "-i") || !strcmp(*argv, "--resourcePath")) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000819 argv++;
820 if (argv < stop && **argv) {
821 resourcePath = *argv;
822 }
robertphillips@google.com76d40212012-03-22 14:12:15 +0000823 } else if (strcmp(*argv, "--slide") == 0) {
824 argv++;
825 if (argv < stop && **argv) {
826 fCurrIndex = findByTitle(*argv);
827 if (fCurrIndex < 0) {
828 fprintf(stderr, "Unknown sample \"%s\"\n", *argv);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000829 listTitles();
robertphillips@google.com76d40212012-03-22 14:12:15 +0000830 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000831 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000832 } else if (strcmp(*argv, "--msaa") == 0) {
833 ++argv;
834 if (argv < stop && **argv) {
835 fMSAASampleCount = atoi(*argv);
836 }
robertphillips@google.com7265e722012-05-03 18:22:28 +0000837 } else if (strcmp(*argv, "--list") == 0) {
838 listTitles();
halcanary@google.com805ca192013-10-14 12:47:37 +0000839 } else if (strcmp(*argv, "--pictureDir") == 0) {
840 ++argv; // This case is dealt with in registerPictFileSamples().
841 } else if (strcmp(*argv, "--picture") == 0) {
842 ++argv; // This case is dealt with in registerPictFileSample().
bsalomon@google.com11959252012-04-06 20:13:38 +0000843 }
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000844 else {
robertphillips@google.com76d40212012-03-22 14:12:15 +0000845 usage(commandName);
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000846 }
847 }
848
849 if (fCurrIndex < 0) {
850 SkString title;
851 if (readTitleFromPrefs(&title)) {
852 fCurrIndex = findByTitle(title.c_str());
853 }
854 }
855
856 if (fCurrIndex < 0) {
857 fCurrIndex = 0;
858 }
859
reed@google.com3cec4d72011-07-06 13:59:47 +0000860 gSampleWindow = this;
861
yangsu@google.com1f394212011-06-01 18:03:34 +0000862#ifdef PIPE_FILE
863 //Clear existing file or create file if it doesn't exist
864 FILE* f = fopen(FILE_PATH, "wb");
865 fclose(f);
866#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +0000867
reed@android.com8a1c16f2008-12-17 15:59:43 +0000868 fPicture = NULL;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000869
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000870 fDeviceType = kRaster_DeviceType;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000871
872#if DEFAULT_TO_GPU
873 fDeviceType = kGPU_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000874#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000875#if SK_ANGLE && DEFAULT_TO_ANGLE
876 fDeviceType = kANGLE_DeviceType;
877#endif
878
reed@android.com8a1c16f2008-12-17 15:59:43 +0000879 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000880 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000881 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000882 fRotate = false;
bungeman@google.comb1785912013-07-09 21:58:56 +0000883 fRotateAnimTime = 0;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000884 fPerspAnim = false;
885 fPerspAnimTime = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +0000886 fRequestGrabImage = false;
scroggo@google.comb073d922012-06-08 15:35:03 +0000887 fPipeState = SkOSMenu::kOffState;
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000888 fTilingMode = kNo_Tiling;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000889 fMeasureFPS = false;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000890 fLCDState = SkOSMenu::kMixedState;
891 fAAState = SkOSMenu::kMixedState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000892 fSubpixelState = SkOSMenu::kMixedState;
893 fHintingState = 0;
reed@google.com15bc13d2013-12-10 16:53:13 +0000894 fFilterLevelIndex = 0;
reed@google.com569e0432011-04-05 13:07:03 +0000895 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000896 fScrollTestX = fScrollTestY = 0;
897
Scroggo0f185c22011-03-24 18:35:50 +0000898 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000899 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000900 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
901 fShowZoomer = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000902
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000903 fZoomLevel = 0;
904 fZoomScale = SK_Scalar1;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000905
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000906 fMagnify = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000907
Scroggo8ac0d542011-06-21 14:44:57 +0000908 fSaveToPdf = false;
909 fPdfCanvas = NULL;
910
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000911 fTransitionNext = 6;
912 fTransitionPrev = 2;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000913
yangsu@google.com921091f2011-08-02 13:39:12 +0000914 int sinkID = this->getSinkID();
scroggo@google.com7dadc742012-04-18 14:07:57 +0000915 fAppMenu = new SkOSMenu;
916 fAppMenu->setTitle("Global Settings");
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000917 int itemID;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000918
919 itemID =fAppMenu->appendList("Device Type", "Device Type", sinkID, 0,
920 "Raster", "Picture", "OpenGL",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000921#if SK_ANGLE
922 "ANGLE",
923#endif
924 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000925 fAppMenu->assignKeyEquivalentToItem(itemID, 'd');
926 itemID = fAppMenu->appendTriState("AA", "AA", sinkID, fAAState);
927 fAppMenu->assignKeyEquivalentToItem(itemID, 'b');
928 itemID = fAppMenu->appendTriState("LCD", "LCD", sinkID, fLCDState);
929 fAppMenu->assignKeyEquivalentToItem(itemID, 'l');
reed@google.com15bc13d2013-12-10 16:53:13 +0000930 itemID = fAppMenu->appendList("FilterLevel", "FilterLevel", sinkID, fFilterLevelIndex,
931 gFilterLevelStates[0].fName,
932 gFilterLevelStates[1].fName,
933 gFilterLevelStates[2].fName,
934 gFilterLevelStates[3].fName,
935 gFilterLevelStates[4].fName,
936 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000937 fAppMenu->assignKeyEquivalentToItem(itemID, 'n');
bungeman@google.com96aabc82013-06-03 21:26:34 +0000938 itemID = fAppMenu->appendTriState("Subpixel", "Subpixel", sinkID, fSubpixelState);
939 fAppMenu->assignKeyEquivalentToItem(itemID, 's');
940 itemID = fAppMenu->appendList("Hinting", "Hinting", sinkID, fHintingState,
941 gHintingStates[0].name,
942 gHintingStates[1].name,
943 gHintingStates[2].name,
944 gHintingStates[3].name,
bungeman@google.coma9133bb2013-06-04 17:49:20 +0000945 gHintingStates[4].name,
946 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000947 fAppMenu->assignKeyEquivalentToItem(itemID, 'h');
reed@google.com67b89ee2012-08-15 14:41:58 +0000948
scroggo@google.comb073d922012-06-08 15:35:03 +0000949 fUsePipeMenuItemID = fAppMenu->appendTriState("Pipe", "Pipe" , sinkID,
chudy@google.com4605a3f2012-08-01 17:58:01 +0000950 fPipeState);
scroggo@google.comb073d922012-06-08 15:35:03 +0000951 fAppMenu->assignKeyEquivalentToItem(fUsePipeMenuItemID, 'P');
reed@google.com67b89ee2012-08-15 14:41:58 +0000952
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000953 itemID =fAppMenu->appendList("Tiling", "Tiling", sinkID, fTilingMode,
954 gTilingInfo[kNo_Tiling].label,
955 gTilingInfo[kAbs_128x128_Tiling].label,
956 gTilingInfo[kAbs_256x256_Tiling].label,
957 gTilingInfo[kRel_4x4_Tiling].label,
958 gTilingInfo[kRel_1x16_Tiling].label,
959 gTilingInfo[kRel_16x1_Tiling].label,
960 NULL);
reed@google.com67b89ee2012-08-15 14:41:58 +0000961 fAppMenu->assignKeyEquivalentToItem(itemID, 't');
962
chudy@google.com4605a3f2012-08-01 17:58:01 +0000963 itemID = fAppMenu->appendSwitch("Slide Show", "Slide Show" , sinkID, false);
964 fAppMenu->assignKeyEquivalentToItem(itemID, 'a');
965 itemID = fAppMenu->appendSwitch("Clip", "Clip" , sinkID, fUseClip);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000966 fAppMenu->assignKeyEquivalentToItem(itemID, 'c');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000967 itemID = fAppMenu->appendSwitch("Flip X", "Flip X" , sinkID, false);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000968 fAppMenu->assignKeyEquivalentToItem(itemID, 'x');
969 itemID = fAppMenu->appendSwitch("Flip Y", "Flip Y" , sinkID, false);
970 fAppMenu->assignKeyEquivalentToItem(itemID, 'y');
971 itemID = fAppMenu->appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
972 fAppMenu->assignKeyEquivalentToItem(itemID, 'z');
973 itemID = fAppMenu->appendSwitch("Magnify", "Magnify" , sinkID, fMagnify);
974 fAppMenu->assignKeyEquivalentToItem(itemID, 'm');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000975 itemID =fAppMenu->appendList("Transition-Next", "Transition-Next", sinkID,
976 fTransitionNext, "Up", "Up and Right", "Right",
977 "Down and Right", "Down", "Down and Left",
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000978 "Left", "Up and Left", NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000979 fAppMenu->assignKeyEquivalentToItem(itemID, 'j');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000980 itemID =fAppMenu->appendList("Transition-Prev", "Transition-Prev", sinkID,
981 fTransitionPrev, "Up", "Up and Right", "Right",
982 "Down and Right", "Down", "Down and Left",
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000983 "Left", "Up and Left", NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000984 fAppMenu->assignKeyEquivalentToItem(itemID, 'k');
985 itemID = fAppMenu->appendAction("Save to PDF", sinkID);
986 fAppMenu->assignKeyEquivalentToItem(itemID, 'e');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000987
scroggo@google.com7dadc742012-04-18 14:07:57 +0000988 this->addMenu(fAppMenu);
989 fSlideMenu = new SkOSMenu;
990 this->addMenu(fSlideMenu);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000991
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000992// this->setConfig(SkBitmap::kRGB_565_Config);
993 this->setConfig(SkBitmap::kARGB_8888_Config);
994 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000995 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000996
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000997 skiagm::GM::SetResourcePath(resourcePath);
998
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000999 this->loadView((*fSamples[fCurrIndex])());
chudy@google.com4605a3f2012-08-01 17:58:01 +00001000
bsalomon@google.com840e9f32011-07-06 21:59:09 +00001001 fPDFData = NULL;
reed@google.comf0b5f682011-03-11 20:08:25 +00001002
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001003 if (NULL == devManager) {
1004 fDevManager = new DefaultDeviceManager();
1005 } else {
1006 devManager->ref();
1007 fDevManager = devManager;
1008 }
bsalomon@google.com11959252012-04-06 20:13:38 +00001009 fDevManager->setUpBackend(this, fMSAASampleCount);
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001010
Scroggob4490c72011-06-17 13:53:05 +00001011 // If another constructor set our dimensions, ensure that our
1012 // onSizeChange gets called.
1013 if (this->height() && this->width()) {
1014 this->onSizeChange();
1015 }
reed@google.com2072db82011-11-08 15:18:10 +00001016
1017 // can't call this synchronously, since it may require a subclass to
1018 // to implement, or the caller may need us to have returned from the
1019 // constructor first. Hence we post an event to ourselves.
1020// this->updateTitle();
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001021 post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001022}
1023
1024SampleWindow::~SampleWindow() {
1025 delete fPicture;
Scroggo8ac0d542011-06-21 14:44:57 +00001026 delete fPdfCanvas;
Scroggo0f185c22011-03-24 18:35:50 +00001027 fTypeface->unref();
reed@google.com29038ed2011-07-06 17:56:47 +00001028
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001029 SkSafeUnref(fDevManager);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001030}
1031
reed@google.com1830c7a2012-06-04 12:05:43 +00001032static void make_filepath(SkString* path, const char* dir, const SkString& name) {
1033 size_t len = strlen(dir);
1034 path->set(dir);
1035 if (len > 0 && dir[len - 1] != '/') {
1036 path->append("/");
1037 }
1038 path->append(name);
1039}
1040
chudy@google.com4605a3f2012-08-01 17:58:01 +00001041void SampleWindow::registerPictFileSample(char** argv, int argc) {
1042 const char* pict = NULL;
1043
1044 for (int i = 0; i < argc; ++i) {
1045 if (!strcmp(argv[i], "--picture")) {
1046 i += 1;
1047 if (i < argc) {
1048 pict = argv[i];
1049 break;
1050 }
1051 }
1052 }
1053 if (pict) {
1054 SkString path(pict);
reed@google.com7d05ac92013-06-06 13:14:13 +00001055 fCurrIndex = fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001056 *fSamples.append() = new PictFileFactory(path);
1057 }
1058}
1059
reed@google.com1830c7a2012-06-04 12:05:43 +00001060void SampleWindow::registerPictFileSamples(char** argv, int argc) {
1061 const char* pictDir = NULL;
1062
1063 for (int i = 0; i < argc; ++i) {
1064 if (!strcmp(argv[i], "--pictureDir")) {
1065 i += 1;
1066 if (i < argc) {
1067 pictDir = argv[i];
1068 break;
1069 }
1070 }
1071 }
1072 if (pictDir) {
1073 SkOSFile::Iter iter(pictDir, "skp");
1074 SkString filename;
1075 while (iter.next(&filename)) {
1076 SkString path;
1077 make_filepath(&path, pictDir, filename);
1078 *fSamples.append() = new PictFileFactory(path);
1079 }
1080 }
1081}
1082
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001083#ifdef SAMPLE_PDF_FILE_VIEWER
1084void SampleWindow::registerPdfFileViewerSamples(char** argv, int argc) {
1085 const char* pdfDir = NULL;
1086
1087 for (int i = 0; i < argc; ++i) {
1088 if (!strcmp(argv[i], "--pdfDir")) {
1089 i += 1;
1090 if (i < argc) {
1091 pdfDir = argv[i];
1092 break;
1093 }
1094 }
1095 }
1096 if (pdfDir) {
1097 SkOSFile::Iter iter(pdfDir, "pdf");
1098 SkString filename;
1099 while (iter.next(&filename)) {
1100 SkString path;
1101 make_filepath(&path, pdfDir, filename);
1102 *fSamples.append() = new PdfFileViewerFactory(path);
1103 }
1104 }
1105}
1106#endif // SAMPLE_PDF_FILE_VIEWER
1107
1108
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001109int SampleWindow::findByTitle(const char title[]) {
1110 int i, count = fSamples.count();
1111 for (i = 0; i < count; i++) {
1112 if (getSampleTitle(i).equals(title)) {
1113 return i;
1114 }
1115 }
mike@reedtribe.orgdd52caa2011-12-28 20:02:10 +00001116 return -1;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001117}
1118
robertphillips@google.com7265e722012-05-03 18:22:28 +00001119void SampleWindow::listTitles() {
1120 int count = fSamples.count();
1121 SkDebugf("All Slides:\n");
1122 for (int i = 0; i < count; i++) {
1123 SkDebugf(" %s\n", getSampleTitle(i).c_str());
1124 }
1125}
1126
reed@android.com55e76b22009-11-23 21:46:47 +00001127static SkBitmap capture_bitmap(SkCanvas* canvas) {
1128 SkBitmap bm;
1129 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
1130 src.copyTo(&bm, src.config());
1131 return bm;
1132}
1133
1134static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
1135 SkBitmap* diff) {
1136 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001137
reed@android.com55e76b22009-11-23 21:46:47 +00001138 SkAutoLockPixels alp0(src);
1139 SkAutoLockPixels alp1(orig);
1140 for (int y = 0; y < src.height(); y++) {
1141 const void* srcP = src.getAddr(0, y);
1142 const void* origP = orig.getAddr(0, y);
1143 size_t bytes = src.width() * src.bytesPerPixel();
1144 if (memcmp(srcP, origP, bytes)) {
1145 SkDebugf("---------- difference on line %d\n", y);
1146 return true;
1147 }
1148 }
1149 return false;
1150}
1151
Scroggo0f185c22011-03-24 18:35:50 +00001152static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
1153{
1154 SkColor desiredColor = paint.getColor();
1155 paint.setColor(SK_ColorWHITE);
1156 const char* c_str = string.c_str();
1157 size_t size = string.size();
1158 SkRect bounds;
1159 paint.measureText(c_str, size, &bounds);
1160 bounds.offset(left, top);
1161 SkScalar inset = SkIntToScalar(-2);
1162 bounds.inset(inset, inset);
1163 canvas->drawRect(bounds, paint);
1164 if (desiredColor != SK_ColorBLACK) {
1165 paint.setColor(SK_ColorBLACK);
1166 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
1167 }
1168 paint.setColor(desiredColor);
1169 canvas->drawText(c_str, size, left, top, paint);
1170}
1171
reed@android.com44177402009-11-23 21:07:51 +00001172#define XCLIP_N 8
1173#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +00001174
1175void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +00001176 // update the animation time
bsalomon@google.come8f09102011-09-08 18:48:12 +00001177 if (!gAnimTimePrev && !gAnimTime) {
1178 // first time make delta be 0
1179 gAnimTime = SkTime::GetMSecs();
1180 gAnimTimePrev = gAnimTime;
1181 } else {
1182 gAnimTimePrev = gAnimTime;
1183 gAnimTime = SkTime::GetMSecs();
1184 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001185
reed@google.comf03bb562011-11-11 21:42:12 +00001186 if (fGesture.isActive()) {
1187 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001188 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001189
djsollen@google.com796763e2012-12-10 14:12:55 +00001190 if (fMeasureFPS) {
1191 fMeasureFPS_Time = 0;
1192 }
1193
reed@android.come522ca52009-11-23 20:10:41 +00001194 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +00001195 this->INHERITED::draw(canvas);
1196 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +00001197
1198 const SkScalar w = this->width();
1199 const SkScalar h = this->height();
1200 const SkScalar cw = w / XCLIP_N;
1201 const SkScalar ch = h / YCLIP_N;
1202 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +00001203 SkRect r;
1204 r.fTop = y * ch;
1205 r.fBottom = (y + 1) * ch;
1206 if (y == YCLIP_N - 1) {
1207 r.fBottom = h;
1208 }
reed@android.come522ca52009-11-23 20:10:41 +00001209 for (int x = 0; x < XCLIP_N; x++) {
1210 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +00001211 r.fLeft = x * cw;
1212 r.fRight = (x + 1) * cw;
1213 if (x == XCLIP_N - 1) {
1214 r.fRight = w;
1215 }
reed@android.come522ca52009-11-23 20:10:41 +00001216 canvas->clipRect(r);
1217 this->INHERITED::draw(canvas);
1218 }
1219 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001220
reed@android.com55e76b22009-11-23 21:46:47 +00001221 SkBitmap diff;
1222 if (bitmap_diff(canvas, orig, &diff)) {
1223 }
reed@android.come522ca52009-11-23 20:10:41 +00001224 } else {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00001225 SkSize tile = this->tileSize();
rmistry@google.comae933ce2012-08-23 18:19:56 +00001226
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001227 for (SkScalar y = 0; y < height(); y += tile.height()) {
1228 for (SkScalar x = 0; x < width(); x += tile.width()) {
scroggo@google.com85cade02012-08-17 13:29:50 +00001229 SkAutoCanvasRestore acr(canvas, true);
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001230 canvas->clipRect(SkRect::MakeXYWH(x, y,
1231 tile.width(),
1232 tile.height()));
scroggo@google.com85cade02012-08-17 13:29:50 +00001233 this->INHERITED::draw(canvas);
1234 }
1235 }
rmistry@google.comae933ce2012-08-23 18:19:56 +00001236
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001237 if (fTilingMode != kNo_Tiling) {
scroggo@google.com85cade02012-08-17 13:29:50 +00001238 SkPaint paint;
1239 paint.setColor(0x60FF00FF);
1240 paint.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001241
1242 for (SkScalar y = 0; y < height(); y += tile.height()) {
1243 for (SkScalar x = 0; x < width(); x += tile.width()) {
1244 canvas->drawRect(SkRect::MakeXYWH(x, y,
1245 tile.width(),
1246 tile.height()),
1247 paint);
scroggo@google.com85cade02012-08-17 13:29:50 +00001248 }
1249 }
1250 }
reed@android.come522ca52009-11-23 20:10:41 +00001251 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001252 if (fShowZoomer && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +00001253 showZoomer(canvas);
1254 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001255 if (fMagnify && !fSaveToPdf) {
1256 magnify(canvas);
1257 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001258
djsollen@google.com796763e2012-12-10 14:12:55 +00001259 if (fMeasureFPS && fMeasureFPS_Time) {
1260 this->updateTitle();
1261 this->postInvalDelay();
1262 }
1263
reed@google.com29038ed2011-07-06 17:56:47 +00001264 // do this last
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001265 fDevManager->publishCanvas(fDeviceType, canvas, this);
Scroggo3272ba82011-05-25 20:50:42 +00001266}
1267
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001268static float clipW = 200;
1269static float clipH = 200;
1270void SampleWindow::magnify(SkCanvas* canvas) {
1271 SkRect r;
1272 int count = canvas->save();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001273
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001274 SkMatrix m = canvas->getTotalMatrix();
robertphillips@google.com07ef9112012-06-04 13:22:14 +00001275 if (!m.invert(&m)) {
1276 return;
1277 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001278 SkPoint offset, center;
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001279 SkScalar mouseX = fMouseX * SK_Scalar1;
1280 SkScalar mouseY = fMouseY * SK_Scalar1;
1281 m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset);
1282 m.mapXY(mouseX, mouseY, &center);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001283
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001284 r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX());
1285 r.offset(offset.fX, offset.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001286
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001287 SkPaint paint;
1288 paint.setColor(0xFF66AAEE);
1289 paint.setStyle(SkPaint::kStroke_Style);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001290 paint.setStrokeWidth(10.f * m.getScaleX());
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001291 //lense offset
1292 //canvas->translate(0, -250);
1293 canvas->drawRect(r, paint);
1294 canvas->clipRect(r);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001295
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001296 m = canvas->getTotalMatrix();
1297 m.setTranslate(-center.fX, -center.fY);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001298 m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001299 m.postTranslate(center.fX, center.fY);
1300 canvas->concat(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001301
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001302 this->INHERITED::draw(canvas);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001303
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001304 canvas->restoreToCount(count);
1305}
1306
Scroggo3272ba82011-05-25 20:50:42 +00001307void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +00001308 int count = canvas->save();
1309 canvas->resetMatrix();
1310 // Ensure the mouse position is on screen.
reed@google.come1ca7052013-12-17 19:22:07 +00001311 int width = SkScalarRoundToInt(this->width());
1312 int height = SkScalarRoundToInt(this->height());
Scroggo0f185c22011-03-24 18:35:50 +00001313 if (fMouseX >= width) fMouseX = width - 1;
1314 else if (fMouseX < 0) fMouseX = 0;
1315 if (fMouseY >= height) fMouseY = height - 1;
1316 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +00001317
Scroggo0f185c22011-03-24 18:35:50 +00001318 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +00001319 bitmap.lockPixels();
1320
Scroggo0f185c22011-03-24 18:35:50 +00001321 // 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 +00001322 int zoomedWidth = (width >> 1) | 1;
1323 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +00001324 SkIRect src;
1325 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
1326 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
1327 SkRect dest;
1328 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
1329 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
1330 SkPaint paint;
1331 // Clear the background behind our zoomed in view
1332 paint.setColor(SK_ColorWHITE);
1333 canvas->drawRect(dest, paint);
1334 canvas->drawBitmapRect(bitmap, &src, dest);
1335 paint.setColor(SK_ColorBLACK);
1336 paint.setStyle(SkPaint::kStroke_Style);
1337 // Draw a border around the pixel in the middle
1338 SkRect originalPixel;
1339 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
1340 SkMatrix matrix;
1341 SkRect scalarSrc;
1342 scalarSrc.set(src);
1343 SkColor color = bitmap.getColor(fMouseX, fMouseY);
1344 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
1345 SkRect pixel;
1346 matrix.mapRect(&pixel, originalPixel);
1347 // TODO Perhaps measure the values and make the outline white if it's "dark"
1348 if (color == SK_ColorBLACK) {
1349 paint.setColor(SK_ColorWHITE);
1350 }
1351 canvas->drawRect(pixel, paint);
1352 }
1353 paint.setColor(SK_ColorBLACK);
1354 // Draw a border around the destination rectangle
1355 canvas->drawRect(dest, paint);
1356 paint.setStyle(SkPaint::kStrokeAndFill_Style);
1357 // Identify the pixel and its color on screen
1358 paint.setTypeface(fTypeface);
1359 paint.setAntiAlias(true);
1360 SkScalar lineHeight = paint.getFontMetrics(NULL);
1361 SkString string;
1362 string.appendf("(%i, %i)", fMouseX, fMouseY);
1363 SkScalar left = dest.fLeft + SkIntToScalar(3);
1364 SkScalar i = SK_Scalar1;
1365 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1366 // Alpha
1367 i += SK_Scalar1;
1368 string.reset();
1369 string.appendf("A: %X", SkColorGetA(color));
1370 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1371 // Red
1372 i += SK_Scalar1;
1373 string.reset();
1374 string.appendf("R: %X", SkColorGetR(color));
1375 paint.setColor(SK_ColorRED);
1376 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1377 // Green
1378 i += SK_Scalar1;
1379 string.reset();
1380 string.appendf("G: %X", SkColorGetG(color));
1381 paint.setColor(SK_ColorGREEN);
1382 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1383 // Blue
1384 i += SK_Scalar1;
1385 string.reset();
1386 string.appendf("B: %X", SkColorGetB(color));
1387 paint.setColor(SK_ColorBLUE);
1388 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1389 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +00001390}
1391
reed@android.com8a1c16f2008-12-17 15:59:43 +00001392void SampleWindow::onDraw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001393}
1394
1395#include "SkColorPriv.h"
1396
caryclark@google.com02939ce2012-06-06 12:09:51 +00001397#if 0 // UNUSED
reed@android.com8a1c16f2008-12-17 15:59:43 +00001398static void reverseRedAndBlue(const SkBitmap& bm) {
1399 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
1400 uint8_t* p = (uint8_t*)bm.getPixels();
1401 uint8_t* stop = p + bm.getSize();
1402 while (p < stop) {
1403 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
1404 unsigned scale = SkAlpha255To256(p[3]);
1405 unsigned r = p[2];
1406 unsigned b = p[0];
1407 p[0] = SkAlphaMul(r, scale);
1408 p[1] = SkAlphaMul(p[1], scale);
1409 p[2] = SkAlphaMul(b, scale);
1410 p += 4;
1411 }
1412}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001413#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001414
Scroggo8ac0d542011-06-21 14:44:57 +00001415void SampleWindow::saveToPdf()
1416{
1417 fSaveToPdf = true;
1418 this->inval(NULL);
1419}
1420
reed@android.com8a1c16f2008-12-17 15:59:43 +00001421SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +00001422 if (fSaveToPdf) {
1423 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
1424 SkISize size = SkISize::Make(bmp.width(), bmp.height());
1425 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
1426 canvas->getTotalMatrix());
1427 fPdfCanvas = new SkCanvas(pdfDevice);
1428 pdfDevice->unref();
1429 canvas = fPdfCanvas;
bsalomon@google.com82502e22013-01-24 20:47:18 +00001430 } else if (kPicture_DeviceType == fDeviceType) {
1431 fPicture = new SkPicture;
1432 canvas = fPicture->beginRecording(9999, 9999);
Scroggo8ac0d542011-06-21 14:44:57 +00001433 } else {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001434#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00001435 if (kNullGPU_DeviceType != fDeviceType)
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001436#endif
bsalomon@google.com82502e22013-01-24 20:47:18 +00001437 {
1438 canvas = this->INHERITED::beforeChildren(canvas);
reed@google.comac10a2d2010-12-22 21:39:39 +00001439 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001440 }
1441
1442 if (fUseClip) {
1443 canvas->drawColor(0xFFFF88FF);
reed@google.com045e62d2011-10-24 12:19:46 +00001444 canvas->clipPath(fClipPath, SkRegion::kIntersect_Op, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001445 }
1446
1447 return canvas;
1448}
1449
1450static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
1451 const SkRegion& rgn) {
1452 SkCanvas canvas(bm);
1453 SkRegion inval(rgn);
1454
1455 inval.translate(r.fLeft, r.fTop);
1456 canvas.clipRegion(inval);
1457 canvas.drawColor(0xFFFF8080);
1458}
yangsu@google.com501775e2011-06-24 16:04:50 +00001459#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001460void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +00001461 if (fSaveToPdf) {
1462 fSaveToPdf = false;
1463 if (fShowZoomer) {
1464 showZoomer(fPdfCanvas);
1465 }
1466 SkString name;
1467 name.printf("%s.pdf", this->getTitle());
1468 SkPDFDocument doc;
1469 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
1470 doc.appendPage(device);
djsollen@google.com56c69772011-11-08 19:00:26 +00001471#ifdef SK_BUILD_FOR_ANDROID
Scroggo8ac0d542011-06-21 14:44:57 +00001472 name.prepend("/sdcard/");
1473#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +00001474
yangsu@google.com501775e2011-06-24 16:04:50 +00001475#ifdef SK_BUILD_FOR_IOS
1476 SkDynamicMemoryWStream mstream;
1477 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +00001478 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +00001479#endif
Scroggo8ac0d542011-06-21 14:44:57 +00001480 SkFILEWStream stream(name.c_str());
1481 if (stream.isValid()) {
1482 doc.emitPDF(&stream);
1483 const char* desc = "File saved from Skia SampleApp";
1484 this->onPDFSaved(this->getTitle(), desc, name.c_str());
1485 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001486
Scroggo8ac0d542011-06-21 14:44:57 +00001487 delete fPdfCanvas;
1488 fPdfCanvas = NULL;
1489
1490 // We took over the draw calls in order to create the PDF, so we need
1491 // to redraw.
1492 this->inval(NULL);
1493 return;
1494 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001495
reed@android.comf2b98d62010-12-20 18:26:13 +00001496 if (fRequestGrabImage) {
1497 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001498
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001499 SkBaseDevice* device = orig->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001500 SkBitmap bmp;
1501 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001502 static int gSampleGrabCounter;
1503 SkString name;
reed@google.com7c57e0e2012-09-08 21:20:33 +00001504 name.printf("sample_grab_%d.png", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001505 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +00001506 SkImageEncoder::kPNG_Type, 100);
1507 }
1508 }
1509
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001510 if (kPicture_DeviceType == fDeviceType) {
1511 if (true) {
1512 SkPicture* pict = new SkPicture(*fPicture);
1513 fPicture->unref();
reed@google.come23f1942011-12-08 19:36:00 +00001514 this->installDrawFilter(orig);
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001515 orig->drawPicture(*pict);
1516 pict->unref();
1517 } else if (true) {
1518 SkDynamicMemoryWStream ostream;
1519 fPicture->serialize(&ostream);
1520 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001521
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001522 SkAutoDataUnref data(ostream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +00001523 SkMemoryStream istream(data->data(), data->size());
scroggo@google.comf1754ec2013-06-28 21:32:00 +00001524 SkAutoTUnref<SkPicture> pict(SkPicture::CreateFromStream(&istream));
1525 if (pict.get() != NULL) {
1526 orig->drawPicture(*pict.get());
1527 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001528 } else {
1529 fPicture->draw(orig);
1530 fPicture->unref();
1531 }
1532 fPicture = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001533 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001534
reed@google.com17d7aec2011-04-25 14:31:44 +00001535 // Do this after presentGL and other finishing, rather than in afterChild
djsollen@google.com796763e2012-12-10 14:12:55 +00001536 if (fMeasureFPS && fMeasureFPS_StartTime) {
1537 fMeasureFPS_Time += SkTime::GetMSecs() - fMeasureFPS_StartTime;
reed@google.com17d7aec2011-04-25 14:31:44 +00001538 }
1539
1540 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +00001541 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001542 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
1543 int dx = fScrollTestX * 7;
1544 int dy = fScrollTestY * 7;
1545 SkIRect r;
1546 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001547
reed@android.com8a1c16f2008-12-17 15:59:43 +00001548 r.set(50, 50, 50+100, 50+100);
1549 bm.scrollRect(&r, dx, dy, &inval);
1550 paint_rgn(bm, r, inval);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001551 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001552}
1553
reed@android.com6c5f6f22009-08-14 16:08:38 +00001554void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
reed@android.com6c5f6f22009-08-14 16:08:38 +00001555 if (fRotate) {
bungeman@google.comb1785912013-07-09 21:58:56 +00001556 fRotateAnimTime += SampleCode::GetAnimSecondsDelta();
1557
reed@android.com6c5f6f22009-08-14 16:08:38 +00001558 SkScalar cx = this->width() / 2;
1559 SkScalar cy = this->height() / 2;
1560 canvas->translate(cx, cy);
bungeman@google.comb1785912013-07-09 21:58:56 +00001561 canvas->rotate(fRotateAnimTime * 10);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001562 canvas->translate(-cx, -cy);
1563 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001564
bsalomon@google.come8f09102011-09-08 18:48:12 +00001565 if (fPerspAnim) {
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001566 fPerspAnimTime += SampleCode::GetAnimSecondsDelta();
1567
1568 static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
1569 static const SkScalar gAnimMag = SK_Scalar1 / 1000;
1570 SkScalar t = SkScalarMod(fPerspAnimTime, gAnimPeriod);
1571 if (SkScalarFloorToInt(SkScalarDiv(fPerspAnimTime, gAnimPeriod)) & 0x1) {
1572 t = gAnimPeriod - t;
1573 }
1574 t = 2 * t - gAnimPeriod;
1575 t = SkScalarMul(SkScalarDiv(t, gAnimPeriod), gAnimMag);
1576 SkMatrix m;
1577 m.reset();
1578 m.setPerspY(t);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001579 canvas->concat(m);
1580 }
reed@google.comf0b5f682011-03-11 20:08:25 +00001581
reed@google.come23f1942011-12-08 19:36:00 +00001582 this->installDrawFilter(canvas);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001583
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001584 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +00001585 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
djsollen@google.com796763e2012-12-10 14:12:55 +00001586 fMeasureFPS_StartTime = SkTime::GetMSecs();
reed@google.comf2183392011-04-22 14:10:48 +00001587 }
1588 } else {
1589 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001590 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001591 if (fPerspAnim || fRotate) {
bsalomon@google.come8f09102011-09-08 18:48:12 +00001592 this->inval(NULL);
1593 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001594}
1595
1596void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +00001597 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001598}
1599
reed@android.com8a1c16f2008-12-17 15:59:43 +00001600static SkBitmap::Config gConfigCycle[] = {
1601 SkBitmap::kNo_Config, // none -> none
reed@android.com8a1c16f2008-12-17 15:59:43 +00001602 SkBitmap::kNo_Config, // a8 -> none
1603 SkBitmap::kNo_Config, // index8 -> none
1604 SkBitmap::kARGB_4444_Config, // 565 -> 4444
1605 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
1606 SkBitmap::kRGB_565_Config // 8888 -> 565
1607};
1608
1609static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
1610 return gConfigCycle[c];
1611}
1612
djsollen@google.come32b5832011-06-13 16:58:40 +00001613void SampleWindow::changeZoomLevel(float delta) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001614 fZoomLevel += delta;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001615 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001616 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
1617 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001618 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001619 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
1620 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001621 } else {
1622 fZoomScale = SK_Scalar1;
1623 }
reed@google.comf03bb562011-11-11 21:42:12 +00001624 this->updateMatrix();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001625}
1626
reed@google.comf03bb562011-11-11 21:42:12 +00001627void SampleWindow::updateMatrix(){
1628 SkMatrix m;
1629 m.reset();
1630 if (fZoomLevel) {
1631 SkPoint center;
1632 //m = this->getLocalMatrix();//.invert(&m);
1633 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
1634 SkScalar cx = center.fX;
1635 SkScalar cy = center.fY;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001636
reed@google.comf03bb562011-11-11 21:42:12 +00001637 m.setTranslate(-cx, -cy);
1638 m.postScale(fZoomScale, fZoomScale);
1639 m.postTranslate(cx, cy);
1640 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001641
reed@google.comf03bb562011-11-11 21:42:12 +00001642 if (fFlipAxis) {
1643 m.preTranslate(fZoomCenterX, fZoomCenterY);
1644 if (fFlipAxis & kFlipAxis_X) {
1645 m.preScale(-SK_Scalar1, SK_Scalar1);
1646 }
1647 if (fFlipAxis & kFlipAxis_Y) {
1648 m.preScale(SK_Scalar1, -SK_Scalar1);
1649 }
1650 m.preTranslate(-fZoomCenterX, -fZoomCenterY);
1651 //canvas->concat(m);
1652 }
1653 // Apply any gesture matrix
1654 m.preConcat(fGesture.localM());
1655 m.preConcat(fGesture.globalM());
chudy@google.com4605a3f2012-08-01 17:58:01 +00001656
reed@google.comf03bb562011-11-11 21:42:12 +00001657 this->setLocalMatrix(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001658
reed@google.comf03bb562011-11-11 21:42:12 +00001659 this->updateTitle();
1660 this->inval(NULL);
1661}
Scroggo2c8208f2011-06-15 16:49:08 +00001662bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +00001663 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001664 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001665 fTransitionPrev));
Scroggo2c8208f2011-06-15 16:49:08 +00001666 return true;
1667}
1668
reed@android.com8a1c16f2008-12-17 15:59:43 +00001669bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +00001670 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001671 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001672 fTransitionNext));
reed@android.com34245c72009-11-03 04:00:48 +00001673 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001674}
1675
yangsu@google.com501775e2011-06-24 16:04:50 +00001676bool SampleWindow::goToSample(int i) {
1677 fCurrIndex = (i) % fSamples.count();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001678 this->loadView(create_transition(curr_view(this),(*fSamples[fCurrIndex])(), 6));
yangsu@google.com501775e2011-06-24 16:04:50 +00001679 return true;
1680}
1681
1682SkString SampleWindow::getSampleTitle(int i) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001683 return ::getSampleTitle(fSamples[i]);
yangsu@google.com501775e2011-06-24 16:04:50 +00001684}
1685
1686int SampleWindow::sampleCount() {
1687 return fSamples.count();
1688}
1689
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001690void SampleWindow::showOverview() {
chudy@google.com4605a3f2012-08-01 17:58:01 +00001691 this->loadView(create_transition(curr_view(this),
1692 create_overview(fSamples.count(), fSamples.begin()),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001693 4));
1694}
1695
reed@google.come23f1942011-12-08 19:36:00 +00001696void SampleWindow::installDrawFilter(SkCanvas* canvas) {
reed@google.com15bc13d2013-12-10 16:53:13 +00001697 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState, fSubpixelState,
1698 fHintingState, fFilterLevelIndex))->unref();
reed@google.come23f1942011-12-08 19:36:00 +00001699}
1700
Scroggo2c8208f2011-06-15 16:49:08 +00001701void SampleWindow::postAnimatingEvent() {
1702 if (fAnimating) {
reed@google.com87fac4a2011-08-04 13:50:17 +00001703 (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
Scroggo2c8208f2011-06-15 16:49:08 +00001704 }
1705}
reed@google.come23f1942011-12-08 19:36:00 +00001706
reed@android.com8a1c16f2008-12-17 15:59:43 +00001707bool SampleWindow::onEvent(const SkEvent& evt) {
reed@google.com2072db82011-11-08 15:18:10 +00001708 if (evt.isType(gUpdateWindowTitleEvtName)) {
1709 this->updateTitle();
1710 return true;
1711 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001712 if (evt.isType(ANIMATING_EVENTTYPE)) {
1713 if (fAnimating) {
1714 this->nextSample();
1715 this->postAnimatingEvent();
1716 }
1717 return true;
1718 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001719 if (evt.isType("replace-transition-view")) {
1720 this->loadView((SkView*)SkEventSink::FindSink(evt.getFast32()));
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001721 return true;
1722 }
reed@android.com34245c72009-11-03 04:00:48 +00001723 if (evt.isType("set-curr-index")) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001724 this->goToSample(evt.getFast32());
reed@android.com34245c72009-11-03 04:00:48 +00001725 return true;
1726 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001727 if (isInvalEvent(evt)) {
1728 this->inval(NULL);
1729 return true;
1730 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001731 int selected = -1;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001732 if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001733 this->setDeviceType((DeviceType)selected);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001734 return true;
yangsu@google.com921091f2011-08-02 13:39:12 +00001735 }
scroggo@google.comb073d922012-06-08 15:35:03 +00001736 if (SkOSMenu::FindTriState(evt, "Pipe", &fPipeState)) {
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001737#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00001738 if (!fPipeState != SkOSMenu::kOnState)
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001739 gServer.disconnectAll();
1740#endif
scroggo@google.comb073d922012-06-08 15:35:03 +00001741 (void)SampleView::SetUsePipe(curr_view(this), fPipeState);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001742 this->updateTitle();
1743 this->inval(NULL);
yangsu@google.com921091f2011-08-02 13:39:12 +00001744 return true;
1745 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001746 if (SkOSMenu::FindSwitchState(evt, "Slide Show", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001747 this->toggleSlideshow();
1748 return true;
1749 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001750 if (SkOSMenu::FindTriState(evt, "AA", &fAAState) ||
1751 SkOSMenu::FindTriState(evt, "LCD", &fLCDState) ||
reed@google.com15bc13d2013-12-10 16:53:13 +00001752 SkOSMenu::FindListIndex(evt, "FilterLevel", &fFilterLevelIndex) ||
bungeman@google.com96aabc82013-06-03 21:26:34 +00001753 SkOSMenu::FindTriState(evt, "Subpixel", &fSubpixelState) ||
1754 SkOSMenu::FindListIndex(evt, "Hinting", &fHintingState) ||
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001755 SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) ||
1756 SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
1757 SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify) ||
1758 SkOSMenu::FindListIndex(evt, "Transition-Next", &fTransitionNext) ||
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00001759 SkOSMenu::FindListIndex(evt, "Transition-Prev", &fTransitionPrev)) {
1760 this->inval(NULL);
1761 this->updateTitle();
1762 return true;
1763 }
1764 if (SkOSMenu::FindListIndex(evt, "Tiling", &fTilingMode)) {
1765 if (SampleView::IsSampleView(curr_view(this))) {
1766 ((SampleView*)curr_view(this))->onTileSizeChanged(this->tileSize());
1767 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001768 this->inval(NULL);
1769 this->updateTitle();
1770 return true;
1771 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001772 if (SkOSMenu::FindSwitchState(evt, "Flip X", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001773 fFlipAxis ^= kFlipAxis_X;
reed@google.comf03bb562011-11-11 21:42:12 +00001774 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001775 return true;
1776 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001777 if (SkOSMenu::FindSwitchState(evt, "Flip Y", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001778 fFlipAxis ^= kFlipAxis_Y;
reed@google.comf03bb562011-11-11 21:42:12 +00001779 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001780 return true;
1781 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001782 if (SkOSMenu::FindAction(evt,"Save to PDF")) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001783 this->saveToPdf();
1784 return true;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001785 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001786 return this->INHERITED::onEvent(evt);
1787}
1788
reed@android.comf2b98d62010-12-20 18:26:13 +00001789bool SampleWindow::onQuery(SkEvent* query) {
1790 if (query->isType("get-slide-count")) {
1791 query->setFast32(fSamples.count());
1792 return true;
1793 }
1794 if (query->isType("get-slide-title")) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001795 SkView* view = (*fSamples[query->getFast32()])();
reed@android.comf2b98d62010-12-20 18:26:13 +00001796 SkEvent evt(gTitleEvtName);
1797 if (view->doQuery(&evt)) {
1798 query->setString("title", evt.findString(gTitleEvtName));
1799 }
1800 SkSafeUnref(view);
1801 return true;
1802 }
1803 if (query->isType("use-fast-text")) {
1804 SkEvent evt(gFastTextEvtName);
1805 return curr_view(this)->doQuery(&evt);
1806 }
reed@google.com29038ed2011-07-06 17:56:47 +00001807 if (query->isType("ignore-window-bitmap")) {
1808 query->setFast32(this->getGrContext() != NULL);
1809 return true;
1810 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001811 return this->INHERITED::onQuery(query);
1812}
1813
caryclark@google.com02939ce2012-06-06 12:09:51 +00001814#if 0 // UNUSED
reed@android.com0ae6b242008-12-23 16:49:54 +00001815static void cleanup_for_filename(SkString* name) {
1816 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001817 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001818 switch (str[i]) {
1819 case ':': str[i] = '-'; break;
1820 case '/': str[i] = '-'; break;
1821 case ' ': str[i] = '_'; break;
1822 default: break;
1823 }
1824 }
1825}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001826#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001827
1828bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001829 {
1830 SkView* view = curr_view(this);
1831 if (view) {
1832 SkEvent evt(gCharEvtName);
1833 evt.setFast32(uni);
1834 if (view->doQuery(&evt)) {
1835 return true;
1836 }
1837 }
1838 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001839
reed@android.com8a1c16f2008-12-17 15:59:43 +00001840 int dx = 0xFF;
1841 int dy = 0xFF;
1842
1843 switch (uni) {
1844 case '5': dx = 0; dy = 0; break;
1845 case '8': dx = 0; dy = -1; break;
1846 case '6': dx = 1; dy = 0; break;
1847 case '2': dx = 0; dy = 1; break;
1848 case '4': dx = -1; dy = 0; break;
1849 case '7': dx = -1; dy = -1; break;
1850 case '9': dx = 1; dy = -1; break;
1851 case '3': dx = 1; dy = 1; break;
1852 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001853
reed@android.com8a1c16f2008-12-17 15:59:43 +00001854 default:
1855 break;
1856 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001857
reed@android.com8a1c16f2008-12-17 15:59:43 +00001858 if (0xFF != dx && 0xFF != dy) {
1859 if ((dx | dy) == 0) {
1860 fScrollTestX = fScrollTestY = 0;
1861 } else {
1862 fScrollTestX += dx;
1863 fScrollTestY += dy;
1864 }
1865 this->inval(NULL);
1866 return true;
1867 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001868
reed@android.com0ae6b242008-12-23 16:49:54 +00001869 switch (uni) {
reed@google.coma4f81372012-11-15 15:56:38 +00001870 case 'B':
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001871 post_event_to_sink(SkNEW_ARGS(SkEvent, ("PictFileView::toggleBBox")), curr_view(this));
1872 // Cannot call updateTitle() synchronously, because the toggleBBox event is still in
1873 // the queue.
1874 post_event_to_sink(SkNEW_ARGS(SkEvent, (gUpdateWindowTitleEvtName)), this);
reed@google.coma4f81372012-11-15 15:56:38 +00001875 this->inval(NULL);
1876 break;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001877 case 'f':
chudy@google.com4605a3f2012-08-01 17:58:01 +00001878 // only
djsollen@google.com6ff82552011-11-07 15:43:57 +00001879 toggleFPS();
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001880 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001881 case 'g':
1882 fRequestGrabImage = true;
1883 this->inval(NULL);
1884 break;
reed@google.coma4f81372012-11-15 15:56:38 +00001885 case 'G':
1886 gShowGMBounds = !gShowGMBounds;
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001887 post_event_to_sink(GMSampleView::NewShowSizeEvt(gShowGMBounds),
reed@google.coma4f81372012-11-15 15:56:38 +00001888 curr_view(this));
1889 this->inval(NULL);
1890 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001891 case 'i':
1892 this->zoomIn();
1893 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001894 case 'o':
1895 this->zoomOut();
1896 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001897 case 'r':
1898 fRotate = !fRotate;
bungeman@google.comb1785912013-07-09 21:58:56 +00001899 fRotateAnimTime = 0;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001900 this->inval(NULL);
1901 this->updateTitle();
1902 return true;
bsalomon@google.come8f09102011-09-08 18:48:12 +00001903 case 'k':
1904 fPerspAnim = !fPerspAnim;
1905 this->inval(NULL);
1906 this->updateTitle();
1907 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001908#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00001909 case '\\':
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001910 this->setDeviceType(kNullGPU_DeviceType);
1911 this->inval(NULL);
1912 this->updateTitle();
bsalomon@google.com74913722011-10-27 20:44:19 +00001913 return true;
twiz@google.com05e70242012-01-27 19:12:00 +00001914 case 'p':
1915 {
1916 GrContext* grContext = this->getGrContext();
1917 if (grContext) {
1918 size_t cacheBytes = grContext->getGpuTextureCacheBytes();
1919 grContext->freeGpuResources();
1920 SkDebugf("Purged %d bytes from the GPU resource cache.\n",
1921 cacheBytes);
1922 }
1923 }
1924 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001925#endif
reed@android.com0ae6b242008-12-23 16:49:54 +00001926 default:
1927 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001928 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001929
scroggo@google.com7dadc742012-04-18 14:07:57 +00001930 if (fAppMenu->handleKeyEquivalent(uni)|| fSlideMenu->handleKeyEquivalent(uni)) {
1931 this->onUpdateMenu(fAppMenu);
1932 this->onUpdateMenu(fSlideMenu);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001933 return true;
1934 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001935 return this->INHERITED::onHandleChar(uni);
1936}
1937
yangsu@google.com921091f2011-08-02 13:39:12 +00001938void SampleWindow::setDeviceType(DeviceType type) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001939 if (type == fDeviceType)
1940 return;
1941
1942 fDevManager->tearDownBackend(this);
1943
1944 fDeviceType = type;
1945
bsalomon@google.com11959252012-04-06 20:13:38 +00001946 fDevManager->setUpBackend(this, fMSAASampleCount);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001947
yangsu@google.com921091f2011-08-02 13:39:12 +00001948 this->updateTitle();
1949 this->inval(NULL);
1950}
1951
Scroggo2c8208f2011-06-15 16:49:08 +00001952void SampleWindow::toggleSlideshow() {
1953 fAnimating = !fAnimating;
1954 this->postAnimatingEvent();
1955 this->updateTitle();
1956}
1957
1958void SampleWindow::toggleRendering() {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001959 this->setDeviceType(cycle_devicetype(fDeviceType));
Scroggo2c8208f2011-06-15 16:49:08 +00001960 this->updateTitle();
1961 this->inval(NULL);
1962}
1963
djsollen@google.com6ff82552011-11-07 15:43:57 +00001964void SampleWindow::toggleFPS() {
1965 fMeasureFPS = !fMeasureFPS;
1966 this->updateTitle();
1967 this->inval(NULL);
1968}
1969
reed@android.com8a1c16f2008-12-17 15:59:43 +00001970#include "SkDumpCanvas.h"
1971
1972bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001973 {
1974 SkView* view = curr_view(this);
1975 if (view) {
1976 SkEvent evt(gKeyEvtName);
1977 evt.setFast32(key);
1978 if (view->doQuery(&evt)) {
1979 return true;
1980 }
1981 }
1982 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001983 switch (key) {
1984 case kRight_SkKey:
1985 if (this->nextSample()) {
1986 return true;
1987 }
1988 break;
1989 case kLeft_SkKey:
scroggo@google.come2dd9732012-08-15 20:03:06 +00001990 if (this->previousSample()) {
1991 return true;
1992 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001993 return true;
1994 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001995 if (USE_ARROWS_FOR_ZOOM) {
bungeman@google.comcefd9812013-05-15 15:07:32 +00001996 this->changeZoomLevel(1.f / 32.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001997 } else {
1998 fNClip = !fNClip;
1999 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00002000 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002001 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002002 return true;
2003 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002004 if (USE_ARROWS_FOR_ZOOM) {
bungeman@google.comcefd9812013-05-15 15:07:32 +00002005 this->changeZoomLevel(-1.f / 32.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002006 } else {
2007 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00002008 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002009 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002010 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002011 case kOK_SkKey: {
2012 SkString title;
2013 if (curr_title(this, &title)) {
2014 writeTitleToPrefs(title.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002015 }
2016 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002017 }
reed@android.com34245c72009-11-03 04:00:48 +00002018 case kBack_SkKey:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002019 this->showOverview();
reed@android.com34245c72009-11-03 04:00:48 +00002020 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002021 default:
2022 break;
2023 }
2024 return this->INHERITED::onHandleKey(key);
2025}
2026
reed@google.com52f57e12011-03-16 12:10:02 +00002027///////////////////////////////////////////////////////////////////////////////
2028
2029static const char gGestureClickType[] = "GestureClickType";
2030
Scroggod3aed392011-06-22 13:26:56 +00002031bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
reed@google.com4d5c26d2013-01-08 16:17:50 +00002032 void* owner, unsigned modi) {
Scroggo0f185c22011-03-24 18:35:50 +00002033 if (Click::kMoved_State == state) {
2034 updatePointer(x, y);
2035 }
reed@google.come1ca7052013-12-17 19:22:07 +00002036 int w = SkScalarRoundToInt(this->width());
2037 int h = SkScalarRoundToInt(this->height());
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002038
2039 // check for the resize-box
2040 if (w - x < 16 && h - y < 16) {
2041 return false; // let the OS handle the click
chudy@google.com4605a3f2012-08-01 17:58:01 +00002042 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002043 else if (fMagnify) {
2044 //it's only necessary to update the drawing if there's a click
2045 this->inval(NULL);
2046 return false; //prevent dragging while magnify is enabled
reed@google.com4d5c26d2013-01-08 16:17:50 +00002047 } else {
2048 // capture control+option, and trigger debugger
2049 if ((modi & kControl_SkModifierKey) && (modi & kOption_SkModifierKey)) {
2050 if (Click::kDown_State == state) {
2051 SkEvent evt("debug-hit-test");
2052 evt.setS32("debug-hit-test-x", x);
2053 evt.setS32("debug-hit-test-y", y);
2054 curr_view(this)->doEvent(evt);
2055 }
2056 return true;
2057 } else {
2058 return this->INHERITED::onDispatchClick(x, y, state, owner, modi);
2059 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002060 }
2061}
2062
reed@google.com52f57e12011-03-16 12:10:02 +00002063class GestureClick : public SkView::Click {
2064public:
2065 GestureClick(SkView* target) : SkView::Click(target) {
2066 this->setType(gGestureClickType);
2067 }
2068
2069 static bool IsGesture(Click* click) {
2070 return click->isType(gGestureClickType);
2071 }
2072};
2073
reed@google.com4d5c26d2013-01-08 16:17:50 +00002074SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y,
2075 unsigned modi) {
reed@google.com52f57e12011-03-16 12:10:02 +00002076 return new GestureClick(this);
2077}
2078
2079bool SampleWindow::onClick(Click* click) {
2080 if (GestureClick::IsGesture(click)) {
bsalomon@google.com4d4f2812011-12-12 18:34:01 +00002081 float x = static_cast<float>(click->fICurr.fX);
2082 float y = static_cast<float>(click->fICurr.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00002083
reed@google.com52f57e12011-03-16 12:10:02 +00002084 switch (click->fState) {
2085 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00002086 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00002087 break;
2088 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00002089 fGesture.touchMoved(click->fOwner, x, y);
reed@google.comf03bb562011-11-11 21:42:12 +00002090 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002091 break;
2092 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00002093 fGesture.touchEnd(click->fOwner);
reed@google.comf03bb562011-11-11 21:42:12 +00002094 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002095 break;
2096 }
2097 return true;
2098 }
2099 return false;
2100}
2101
2102///////////////////////////////////////////////////////////////////////////////
2103
reed@android.com8a1c16f2008-12-17 15:59:43 +00002104void SampleWindow::loadView(SkView* view) {
2105 SkView::F2BIter iter(this);
2106 SkView* prev = iter.next();
2107 if (prev) {
2108 prev->detachFromParent();
2109 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002110
reed@android.com8a1c16f2008-12-17 15:59:43 +00002111 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00002112 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002113 this->attachChildToFront(view)->unref();
2114 view->setSize(this->width(), this->height());
2115
yangsu@google.com921091f2011-08-02 13:39:12 +00002116 //repopulate the slide menu when a view is loaded
scroggo@google.com7dadc742012-04-18 14:07:57 +00002117 fSlideMenu->reset();
djsollen@google.com388974f2013-01-17 13:20:01 +00002118
scroggo@google.comb073d922012-06-08 15:35:03 +00002119 (void)SampleView::SetUsePipe(view, fPipeState);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00002120 if (SampleView::IsSampleView(view)) {
2121 SampleView* sampleView = (SampleView*)view;
2122 sampleView->requestMenu(fSlideMenu);
2123 sampleView->onTileSizeChanged(this->tileSize());
2124 }
scroggo@google.com7dadc742012-04-18 14:07:57 +00002125 this->onUpdateMenu(fSlideMenu);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002126 this->updateTitle();
2127}
2128
2129static const char* gConfigNames[] = {
2130 "unknown config",
reed@android.com8a1c16f2008-12-17 15:59:43 +00002131 "A8",
2132 "Index8",
2133 "565",
2134 "4444",
2135 "8888"
2136};
2137
2138static const char* configToString(SkBitmap::Config c) {
2139 return gConfigNames[c];
2140}
2141
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002142static const char* gDeviceTypePrefix[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002143 "raster: ",
2144 "picture: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002145#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00002146 "opengl: ",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00002147#if SK_ANGLE
2148 "angle: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002149#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +00002150 "null-gl: "
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002151#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +00002152};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002153SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gDeviceTypePrefix) == SampleWindow::kDeviceTypeCnt,
2154 array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002155
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002156static const char* trystate_str(SkOSMenu::TriState state,
reed@google.com569e0432011-04-05 13:07:03 +00002157 const char trueStr[], const char falseStr[]) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002158 if (SkOSMenu::kOnState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002159 return trueStr;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002160 } else if (SkOSMenu::kOffState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002161 return falseStr;
2162 }
2163 return NULL;
2164}
2165
reed@android.com8a1c16f2008-12-17 15:59:43 +00002166void SampleWindow::updateTitle() {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002167 SkView* view = curr_view(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002168
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002169 SkString title;
2170 if (!curr_title(this, &title)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002171 title.set("<unknown>");
2172 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002173
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002174 title.prepend(gDeviceTypePrefix[fDeviceType]);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002175
2176 title.prepend(" ");
2177 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002178
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00002179 if (fTilingMode != kNo_Tiling) {
2180 title.prependf("<T: %s> ", gTilingInfo[fTilingMode].label);
2181 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002182 if (fAnimating) {
2183 title.prepend("<A> ");
2184 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00002185 if (fRotate) {
2186 title.prepend("<R> ");
2187 }
reed@android.come522ca52009-11-23 20:10:41 +00002188 if (fNClip) {
2189 title.prepend("<C> ");
2190 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00002191 if (fPerspAnim) {
2192 title.prepend("<K> ");
2193 }
reed@google.com569e0432011-04-05 13:07:03 +00002194
2195 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
2196 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com15bc13d2013-12-10 16:53:13 +00002197 title.prepend(gFilterLevelStates[fFilterLevelIndex].fLabel);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002198 title.prepend(trystate_str(fSubpixelState, "S ", "s "));
reed@google.com569e0432011-04-05 13:07:03 +00002199 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
2200 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002201 title.prepend(gHintingStates[fHintingState].label);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002202
2203 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00002204 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002205 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002206
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002207 if (fMeasureFPS) {
reed@google.com43e10142012-09-10 11:52:52 +00002208 title.appendf(" %8.3f ms", fMeasureFPS_Time / (float)FPS_REPEAT_COUNT);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002209 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002210 if (SampleView::IsSampleView(view)) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002211 switch (fPipeState) {
2212 case SkOSMenu::kOnState:
2213 title.prepend("<Pipe> ");
2214 break;
2215 case SkOSMenu::kMixedState:
2216 title.prepend("<Tiled Pipe> ");
2217 break;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002218
scroggo@google.comb073d922012-06-08 15:35:03 +00002219 default:
2220 break;
2221 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002222 title.prepend("! ");
2223 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002224
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002225#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00002226 if (IsGpuDeviceType(fDeviceType) &&
robertphillips@google.com4750fa52012-04-10 13:34:11 +00002227 NULL != fDevManager &&
bsalomon@google.com82502e22013-01-24 20:47:18 +00002228 fDevManager->getGrRenderTarget() &&
bsalomon@google.com11959252012-04-06 20:13:38 +00002229 fDevManager->getGrRenderTarget()->numSamples() > 0) {
2230 title.appendf(" [MSAA: %d]",
2231 fDevManager->getGrRenderTarget()->numSamples());
2232 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002233#endif
bsalomon@google.com11959252012-04-06 20:13:38 +00002234
reed@android.com8a1c16f2008-12-17 15:59:43 +00002235 this->setTitle(title.c_str());
2236}
2237
2238void SampleWindow::onSizeChange() {
2239 this->INHERITED::onSizeChange();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002240
reed@android.com8a1c16f2008-12-17 15:59:43 +00002241 SkView::F2BIter iter(this);
2242 SkView* view = iter.next();
2243 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002244
reed@android.com8a1c16f2008-12-17 15:59:43 +00002245 // rebuild our clippath
2246 {
2247 const SkScalar W = this->width();
2248 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002249
reed@android.com8a1c16f2008-12-17 15:59:43 +00002250 fClipPath.reset();
2251#if 0
2252 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
2253 SkRect r;
2254 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
2255 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
2256 fClipPath.addRect(r);
2257 }
2258#else
2259 SkRect r;
2260 r.set(0, 0, W, H);
2261 fClipPath.addRect(r, SkPath::kCCW_Direction);
2262 r.set(W/4, H/4, W*3/4, H*3/4);
2263 fClipPath.addRect(r, SkPath::kCW_Direction);
2264#endif
2265 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002266
Scroggo2c8208f2011-06-15 16:49:08 +00002267 fZoomCenterX = SkScalarHalf(this->width());
2268 fZoomCenterY = SkScalarHalf(this->height());
2269
djsollen@google.com56c69772011-11-08 19:00:26 +00002270#ifdef SK_BUILD_FOR_ANDROID
Scroggob4490c72011-06-17 13:53:05 +00002271 // FIXME: The first draw after a size change does not work on Android, so
2272 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00002273 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00002274#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00002275 this->updateTitle(); // to refresh our config
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002276 fDevManager->windowSizeChanged(this);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00002277
2278 if (fTilingMode != kNo_Tiling && SampleView::IsSampleView(view)) {
2279 ((SampleView*)view)->onTileSizeChanged(this->tileSize());
2280 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002281}
2282
2283///////////////////////////////////////////////////////////////////////////////
2284
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002285static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002286static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00002287static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002288
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002289bool SampleView::IsSampleView(SkView* view) {
2290 SkEvent evt(is_sample_view_tag);
2291 return view->doQuery(&evt);
2292}
2293
reed@google.comf2183392011-04-22 14:10:48 +00002294bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002295 SkEvent evt(repeat_count_tag);
2296 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00002297 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002298}
2299
scroggo@google.comb073d922012-06-08 15:35:03 +00002300bool SampleView::SetUsePipe(SkView* view, SkOSMenu::TriState state) {
2301 SkEvent evt;
2302 evt.setS32(set_use_pipe_tag, state);
reed@google.com0faac1e2011-05-11 05:58:58 +00002303 return view->doEvent(evt);
2304}
2305
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002306bool SampleView::onEvent(const SkEvent& evt) {
2307 if (evt.isType(repeat_count_tag)) {
2308 fRepeatCount = evt.getFast32();
2309 return true;
2310 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002311
scroggo@google.comb073d922012-06-08 15:35:03 +00002312 int32_t pipeHolder;
2313 if (evt.findS32(set_use_pipe_tag, &pipeHolder)) {
2314 fPipeState = static_cast<SkOSMenu::TriState>(pipeHolder);
reed@google.com0faac1e2011-05-11 05:58:58 +00002315 return true;
2316 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002317
2318 if (evt.isType("debug-hit-test")) {
2319 fDebugHitTest = true;
2320 evt.findS32("debug-hit-test-x", &fDebugHitTestLoc.fX);
2321 evt.findS32("debug-hit-test-y", &fDebugHitTestLoc.fY);
2322 this->inval(NULL);
2323 return true;
2324 }
2325
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002326 return this->INHERITED::onEvent(evt);
2327}
2328
2329bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002330 if (evt->isType(is_sample_view_tag)) {
2331 return true;
2332 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002333 return this->INHERITED::onQuery(evt);
2334}
2335
reed@google.com64e3eb22011-05-04 14:32:04 +00002336
2337class SimplePC : public SkGPipeController {
2338public:
2339 SimplePC(SkCanvas* target);
2340 ~SimplePC();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002341
reed@google.com64e3eb22011-05-04 14:32:04 +00002342 virtual void* requestBlock(size_t minRequest, size_t* actual);
2343 virtual void notifyWritten(size_t bytes);
2344
2345private:
reed@google.com961ddb02011-05-05 14:03:48 +00002346 SkGPipeReader fReader;
2347 void* fBlock;
2348 size_t fBlockSize;
2349 size_t fBytesWritten;
2350 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00002351 SkGPipeReader::Status fStatus;
2352
2353 size_t fTotalWritten;
2354};
2355
2356SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
2357 fBlock = NULL;
2358 fBlockSize = fBytesWritten = 0;
2359 fStatus = SkGPipeReader::kDone_Status;
2360 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00002361 fAtomsWritten = 0;
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00002362 fReader.setBitmapDecoder(&SkImageDecoder::DecodeMemory);
reed@google.com64e3eb22011-05-04 14:32:04 +00002363}
2364
2365SimplePC::~SimplePC() {
2366// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
reed@google.com0faac1e2011-05-11 05:58:58 +00002367 if (fTotalWritten) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002368 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
2369 fAtomsWritten, fStatus);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002370#ifdef PIPE_FILE
scroggo@google.comb073d922012-06-08 15:35:03 +00002371 //File is open in append mode
2372 FILE* f = fopen(FILE_PATH, "ab");
2373 SkASSERT(f != NULL);
2374 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
2375 fclose(f);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002376#endif
2377#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00002378 if (fAtomsWritten > 1 && fTotalWritten > 4) { //ignore done
2379 gServer.acceptConnections();
2380 gServer.writePacket(fBlock, fTotalWritten);
2381 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002382#endif
reed@google.com0faac1e2011-05-11 05:58:58 +00002383 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002384 sk_free(fBlock);
reed@google.com64e3eb22011-05-04 14:32:04 +00002385}
2386
2387void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
2388 sk_free(fBlock);
2389
2390 fBlockSize = minRequest * 4;
2391 fBlock = sk_malloc_throw(fBlockSize);
2392 fBytesWritten = 0;
2393 *actual = fBlockSize;
2394 return fBlock;
2395}
2396
2397void SimplePC::notifyWritten(size_t bytes) {
2398 SkASSERT(fBytesWritten + bytes <= fBlockSize);
reed@google.com64e3eb22011-05-04 14:32:04 +00002399 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
2400 SkASSERT(SkGPipeReader::kError_Status != fStatus);
2401 fBytesWritten += bytes;
2402 fTotalWritten += bytes;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002403
reed@google.com961ddb02011-05-05 14:03:48 +00002404 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00002405}
2406
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002407void SampleView::draw(SkCanvas* canvas) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002408 if (SkOSMenu::kOffState == fPipeState) {
2409 this->INHERITED::draw(canvas);
2410 } else {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002411 SkGPipeWriter writer;
2412 SimplePC controller(canvas);
scroggo@google.comb073d922012-06-08 15:35:03 +00002413 TiledPipeController tc(canvas->getDevice()->accessBitmap(false),
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00002414 &SkImageDecoder::DecodeMemory,
scroggo@google.comb073d922012-06-08 15:35:03 +00002415 &canvas->getTotalMatrix());
2416 SkGPipeController* pc;
2417 if (SkOSMenu::kMixedState == fPipeState) {
2418 pc = &tc;
2419 } else {
2420 pc = &controller;
2421 }
reed@google.comdde09562011-05-23 12:21:05 +00002422 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002423
scroggo@google.comb073d922012-06-08 15:35:03 +00002424 canvas = writer.startRecording(pc, flags);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002425 //Must draw before controller goes out of scope and sends data
2426 this->INHERITED::draw(canvas);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00002427 //explicitly end recording to ensure writer is flushed before the memory
2428 //is freed in the deconstructor of the controller
2429 writer.endRecording();
reed@google.com0faac1e2011-05-11 05:58:58 +00002430 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002431}
reed@google.com4d5c26d2013-01-08 16:17:50 +00002432
2433#include "SkBounder.h"
2434
2435class DebugHitTestBounder : public SkBounder {
2436public:
2437 DebugHitTestBounder(int x, int y) {
2438 fLoc.set(x, y);
2439 }
2440
2441 virtual bool onIRect(const SkIRect& bounds) SK_OVERRIDE {
2442 if (bounds.contains(fLoc.x(), fLoc.y())) {
2443 //
2444 // Set a break-point here to see what was being drawn under
2445 // the click point (just needed a line of code to stop the debugger)
2446 //
2447 bounds.centerX();
2448 }
2449 return true;
2450 }
2451
2452private:
2453 SkIPoint fLoc;
2454 typedef SkBounder INHERITED;
2455};
2456
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002457void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com81e3d7f2011-06-01 12:42:36 +00002458 this->onDrawBackground(canvas);
2459
reed@google.com4d5c26d2013-01-08 16:17:50 +00002460 DebugHitTestBounder bounder(fDebugHitTestLoc.x(), fDebugHitTestLoc.y());
2461 if (fDebugHitTest) {
2462 canvas->setBounder(&bounder);
2463 }
2464
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002465 for (int i = 0; i < fRepeatCount; i++) {
scroggo@google.com85cade02012-08-17 13:29:50 +00002466 SkAutoCanvasRestore acr(canvas, true);
2467 this->onDrawContent(canvas);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002468 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002469
2470 fDebugHitTest = false;
2471 canvas->setBounder(NULL);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002472}
2473
2474void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00002475 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002476}
2477
2478///////////////////////////////////////////////////////////////////////////////
2479
reed@android.comf2b98d62010-12-20 18:26:13 +00002480template <typename T> void SkTBSort(T array[], int count) {
2481 for (int i = 1; i < count - 1; i++) {
2482 bool didSwap = false;
2483 for (int j = count - 1; j > i; --j) {
2484 if (array[j] < array[j-1]) {
2485 T tmp(array[j-1]);
2486 array[j-1] = array[j];
2487 array[j] = tmp;
2488 didSwap = true;
2489 }
2490 }
2491 if (!didSwap) {
2492 break;
2493 }
2494 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002495
reed@android.comf2b98d62010-12-20 18:26:13 +00002496 for (int k = 0; k < count - 1; k++) {
2497 SkASSERT(!(array[k+1] < array[k]));
2498 }
2499}
2500
2501#include "SkRandom.h"
2502
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002503static void rand_rect(SkIRect* rect, SkRandom& rand) {
reed@android.comf2b98d62010-12-20 18:26:13 +00002504 int bits = 8;
2505 int shift = 32 - bits;
2506 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
2507 rand.nextU() >> shift, rand.nextU() >> shift);
2508 rect->sort();
2509}
2510
2511static void dumpRect(const SkIRect& r) {
2512 SkDebugf(" { %d, %d, %d, %d },\n",
2513 r.fLeft, r.fTop,
2514 r.fRight, r.fBottom);
2515}
2516
2517static void test_rects(const SkIRect rect[], int count) {
2518 SkRegion rgn0, rgn1;
2519
2520 for (int i = 0; i < count; i++) {
2521 rgn0.op(rect[i], SkRegion::kUnion_Op);
2522 // dumpRect(rect[i]);
2523 }
2524 rgn1.setRects(rect, count);
2525
2526 if (rgn0 != rgn1) {
2527 SkDebugf("\n");
2528 for (int i = 0; i < count; i++) {
2529 dumpRect(rect[i]);
2530 }
2531 SkDebugf("\n");
2532 }
2533}
2534
2535static void test() {
2536 size_t i;
2537
2538 const SkIRect r0[] = {
2539 { 0, 0, 1, 1 },
2540 { 2, 2, 3, 3 },
2541 };
2542 const SkIRect r1[] = {
2543 { 0, 0, 1, 3 },
2544 { 1, 1, 2, 2 },
2545 { 2, 0, 3, 3 },
2546 };
2547 const SkIRect r2[] = {
2548 { 0, 0, 1, 2 },
2549 { 2, 1, 3, 3 },
2550 { 4, 0, 5, 1 },
2551 { 6, 0, 7, 4 },
2552 };
2553
2554 static const struct {
2555 const SkIRect* fRects;
2556 int fCount;
2557 } gRecs[] = {
2558 { r0, SK_ARRAY_COUNT(r0) },
2559 { r1, SK_ARRAY_COUNT(r1) },
2560 { r2, SK_ARRAY_COUNT(r2) },
2561 };
2562
2563 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
2564 test_rects(gRecs[i].fRects, gRecs[i].fCount);
2565 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002566
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002567 SkRandom rand;
reed@android.comf2b98d62010-12-20 18:26:13 +00002568 for (i = 0; i < 10000; i++) {
2569 SkRegion rgn0, rgn1;
2570
2571 const int N = 8;
2572 SkIRect rect[N];
2573 for (int j = 0; j < N; j++) {
2574 rand_rect(&rect[j], rand);
2575 }
2576 test_rects(rect, N);
2577 }
2578}
2579
caryclark@google.com02939ce2012-06-06 12:09:51 +00002580// FIXME: this should be in a header
2581SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00002582SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
caryclark@google.com02939ce2012-06-06 12:09:51 +00002583 if (false) { // avoid bit rot, suppress warning
2584 test();
2585 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002586 return new SampleWindow(hwnd, argc, argv, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002587}
2588
caryclark@google.com02939ce2012-06-06 12:09:51 +00002589// FIXME: this should be in a header
2590void get_preferred_size(int* x, int* y, int* width, int* height);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002591void get_preferred_size(int* x, int* y, int* width, int* height) {
2592 *x = 10;
2593 *y = 50;
2594 *width = 640;
2595 *height = 480;
2596}
2597
caryclark@google.com5987f582012-10-02 18:33:14 +00002598#ifdef SK_BUILD_FOR_IOS
2599void save_args(int argc, char *argv[]) {
2600}
2601#endif
2602
caryclark@google.com02939ce2012-06-06 12:09:51 +00002603// FIXME: this should be in a header
2604void application_init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002605void application_init() {
2606// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002607#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00002608 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002609#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002610 SkGraphics::Init();
2611 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002612}
2613
caryclark@google.com02939ce2012-06-06 12:09:51 +00002614// FIXME: this should be in a header
2615void application_term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002616void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002617 SkEvent::Term();
2618 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002619}