blob: 5b37662dcd55175a37473a0dc5d475cfc773709f [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 {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000389 skiagm::GM* gm = fFunc(NULL);
390 gm->setMode(skiagm::GM::kSample_Mode);
391 return new GMSampleView(gm);
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000392}
393
394SkViewRegister* SkViewRegister::gHead;
395SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) {
396 fFact->ref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000397 fChain = gHead;
398 gHead = this;
399}
400
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000401SkViewRegister::SkViewRegister(SkViewCreateFunc func) {
402 fFact = new SkFuncViewFactory(func);
403 fChain = gHead;
404 gHead = this;
405}
406
407SkViewRegister::SkViewRegister(GMFactoryFunc func) {
408 fFact = new SkGMSampleViewFactory(func);
409 fChain = gHead;
410 gHead = this;
411}
412
413class AutoUnrefArray {
414public:
415 AutoUnrefArray() {}
416 ~AutoUnrefArray() {
417 int count = fObjs.count();
418 for (int i = 0; i < count; ++i) {
419 fObjs[i]->unref();
420 }
421 }
422 SkRefCnt*& push_back() { return *fObjs.append(); }
chudy@google.com4605a3f2012-08-01 17:58:01 +0000423
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000424private:
425 SkTDArray<SkRefCnt*> fObjs;
426};
427
428// registers GMs as Samples
429// This can't be performed during static initialization because it could be
430// run before GMRegistry has been fully built.
caryclark@google.com02939ce2012-06-06 12:09:51 +0000431static void SkGMRegistyToSampleRegistry() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000432 static bool gOnce;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000433 static AutoUnrefArray fRegisters;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000434
435 if (!gOnce) {
436 const skiagm::GMRegistry* gmreg = skiagm::GMRegistry::Head();
437 while (gmreg) {
438 fRegisters.push_back() = new SkViewRegister(gmreg->factory());
439 gmreg = gmreg->next();
440 }
441 gOnce = true;
442 }
443}
444
reed@google.comf0b5f682011-03-11 20:08:25 +0000445//////////////////////////////////////////////////////////////////////////////
446
reed@google.com569e0432011-04-05 13:07:03 +0000447enum FlipAxisEnum {
448 kFlipAxis_X = (1 << 0),
449 kFlipAxis_Y = (1 << 1)
450};
451
reed@google.comf0b5f682011-03-11 20:08:25 +0000452#include "SkDrawFilter.h"
453
bungeman@google.com96aabc82013-06-03 21:26:34 +0000454struct HintingState {
455 SkPaint::Hinting hinting;
456 const char* name;
457 const char* label;
458};
459static HintingState gHintingStates[] = {
460 {SkPaint::kNo_Hinting, "Mixed", NULL },
461 {SkPaint::kNo_Hinting, "None", "H0 " },
462 {SkPaint::kSlight_Hinting, "Slight", "Hs " },
463 {SkPaint::kNormal_Hinting, "Normal", "Hn " },
464 {SkPaint::kFull_Hinting, "Full", "Hf " },
465};
466
reed@google.com15bc13d2013-12-10 16:53:13 +0000467struct FilterLevelState {
468 SkPaint::FilterLevel fLevel;
469 const char* fName;
470 const char* fLabel;
471};
472static FilterLevelState gFilterLevelStates[] = {
473 { SkPaint::kNone_FilterLevel, "Mixed", NULL },
474 { SkPaint::kNone_FilterLevel, "None", "F0 " },
475 { SkPaint::kLow_FilterLevel, "Low", "F1 " },
476 { SkPaint::kMedium_FilterLevel, "Medium", "F2 " },
477 { SkPaint::kHigh_FilterLevel, "High", "F3 " },
478};
479
reed@google.com569e0432011-04-05 13:07:03 +0000480class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000481public:
reed@google.com15bc13d2013-12-10 16:53:13 +0000482 FlagsDrawFilter(SkOSMenu::TriState lcd, SkOSMenu::TriState aa,
483 SkOSMenu::TriState subpixel, int hinting, int filterlevel)
484 : fLCDState(lcd)
485 , fAAState(aa)
486 , fSubpixelState(subpixel)
487 , fHintingState(hinting)
488 , fFilterLevelIndex(filterlevel)
489 {
490 SkASSERT((unsigned)filterlevel < SK_ARRAY_COUNT(gFilterLevelStates));
491 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000492
reed@google.com971aca72012-11-26 20:26:54 +0000493 virtual bool filter(SkPaint* paint, Type t) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000494 if (kText_Type == t && SkOSMenu::kMixedState != fLCDState) {
495 paint->setLCDRenderText(SkOSMenu::kOnState == fLCDState);
reed@google.com569e0432011-04-05 13:07:03 +0000496 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000497 if (SkOSMenu::kMixedState != fAAState) {
498 paint->setAntiAlias(SkOSMenu::kOnState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000499 }
reed@google.com15bc13d2013-12-10 16:53:13 +0000500 if (0 != fFilterLevelIndex) {
501 paint->setFilterLevel(gFilterLevelStates[fFilterLevelIndex].fLevel);
reed@google.com176753a2011-05-17 15:32:04 +0000502 }
bungeman@google.com96aabc82013-06-03 21:26:34 +0000503 if (SkOSMenu::kMixedState != fSubpixelState) {
504 paint->setSubpixelText(SkOSMenu::kOnState == fSubpixelState);
505 }
bungeman@google.com055aa522013-06-03 21:35:03 +0000506 if (0 != fHintingState && fHintingState < (int)SK_ARRAY_COUNT(gHintingStates)) {
bungeman@google.com96aabc82013-06-03 21:26:34 +0000507 paint->setHinting(gHintingStates[fHintingState].hinting);
reed@google.com09e3baa2011-05-18 12:04:31 +0000508 }
reed@google.com971aca72012-11-26 20:26:54 +0000509 return true;
reed@google.comf0b5f682011-03-11 20:08:25 +0000510 }
511
512private:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000513 SkOSMenu::TriState fLCDState;
514 SkOSMenu::TriState fAAState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000515 SkOSMenu::TriState fSubpixelState;
516 int fHintingState;
reed@google.com15bc13d2013-12-10 16:53:13 +0000517 int fFilterLevelIndex;
reed@google.comf0b5f682011-03-11 20:08:25 +0000518};
519
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520//////////////////////////////////////////////////////////////////////////////
521
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000522#define MAX_ZOOM_LEVEL 8
523#define MIN_ZOOM_LEVEL -8
524
reed@android.comf2b98d62010-12-20 18:26:13 +0000525static const char gCharEvtName[] = "SampleCode_Char_Event";
526static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000527static const char gTitleEvtName[] = "SampleCode_Title_Event";
528static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000529static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
reed@google.com2072db82011-11-08 15:18:10 +0000530static const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle";
reed@android.comf2b98d62010-12-20 18:26:13 +0000531
532bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
533 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
534 if (outUni) {
535 *outUni = evt.getFast32();
536 }
537 return true;
538 }
539 return false;
540}
541
542bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
543 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
544 if (outKey) {
545 *outKey = (SkKey)evt.getFast32();
546 }
547 return true;
548 }
549 return false;
550}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000551
552bool SampleCode::TitleQ(const SkEvent& evt) {
553 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
554}
555
556void SampleCode::TitleR(SkEvent* evt, const char title[]) {
557 SkASSERT(evt && TitleQ(*evt));
558 evt->setString(gTitleEvtName, title);
559}
560
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000561bool SampleCode::RequestTitle(SkView* view, SkString* title) {
562 SkEvent evt(gTitleEvtName);
563 if (view->doQuery(&evt)) {
564 title->set(evt.findString(gTitleEvtName));
565 return true;
566 }
567 return false;
568}
569
reed@android.com8a1c16f2008-12-17 15:59:43 +0000570bool SampleCode::PrefSizeQ(const SkEvent& evt) {
571 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
572}
573
574void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
575 SkASSERT(evt && PrefSizeQ(*evt));
576 SkScalar size[2];
577 size[0] = width;
578 size[1] = height;
579 evt->setScalars(gPrefSizeEvtName, 2, size);
580}
581
reed@android.comf2b98d62010-12-20 18:26:13 +0000582bool SampleCode::FastTextQ(const SkEvent& evt) {
583 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
584}
585
586///////////////////////////////////////////////////////////////////////////////
587
reed@android.com44177402009-11-23 21:07:51 +0000588static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000589static SkMSec gAnimTimePrev;
590
reed@android.com44177402009-11-23 21:07:51 +0000591SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000592SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
593SkScalar SampleCode::GetAnimSecondsDelta() {
594 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
595}
reed@android.com44177402009-11-23 21:07:51 +0000596
597SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000598 // since gAnimTime can be up to 32 bits, we can't convert it to a float
599 // or we'll lose the low bits. Hence we use doubles for the intermediate
600 // calculations
601 double seconds = (double)gAnimTime / 1000.0;
602 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000603 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000604 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000605 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000606 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000607}
608
bsalomon@google.com85003222012-03-28 14:44:37 +0000609SkScalar SampleCode::GetAnimSinScalar(SkScalar amplitude,
610 SkScalar periodInSec,
611 SkScalar phaseInSec) {
612 if (!periodInSec) {
613 return 0;
614 }
615 double t = (double)gAnimTime / 1000.0 + phaseInSec;
616 t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
617 amplitude = SK_ScalarHalf * amplitude;
618 return SkScalarMul(amplitude, SkDoubleToScalar(sin(t))) + amplitude;
619}
620
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000621enum TilingMode {
622 kNo_Tiling,
623 kAbs_128x128_Tiling,
624 kAbs_256x256_Tiling,
625 kRel_4x4_Tiling,
626 kRel_1x16_Tiling,
627 kRel_16x1_Tiling,
628
629 kLast_TilingMode_Enum
630};
631
632struct TilingInfo {
633 const char* label;
634 SkScalar w, h;
635};
636
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000637static const struct TilingInfo gTilingInfo[] = {
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000638 { "No tiling", SK_Scalar1 , SK_Scalar1 }, // kNo_Tiling
639 { "128x128" , SkIntToScalar(128), SkIntToScalar(128) }, // kAbs_128x128_Tiling
640 { "256x256" , SkIntToScalar(256), SkIntToScalar(256) }, // kAbs_256x256_Tiling
641 { "1/4x1/4" , SK_Scalar1 / 4 , SK_Scalar1 / 4 }, // kRel_4x4_Tiling
642 { "1/1x1/16" , SK_Scalar1 , SK_Scalar1 / 16 }, // kRel_1x16_Tiling
643 { "1/16x1/1" , SK_Scalar1 / 16 , SK_Scalar1 }, // kRel_16x1_Tiling
644};
645SK_COMPILE_ASSERT((SK_ARRAY_COUNT(gTilingInfo) == kLast_TilingMode_Enum),
646 Incomplete_tiling_labels);
647
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000648SkSize SampleWindow::tileSize() const {
649 SkASSERT((TilingMode)fTilingMode < kLast_TilingMode_Enum);
650 const struct TilingInfo* info = gTilingInfo + fTilingMode;
651 return SkSize::Make(info->w > SK_Scalar1 ? info->w : this->width() * info->w,
652 info->h > SK_Scalar1 ? info->h : this->height() * info->h);
653}
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000654//////////////////////////////////////////////////////////////////////////////
655
reed@android.comf2b98d62010-12-20 18:26:13 +0000656static SkView* curr_view(SkWindow* wind) {
657 SkView::F2BIter iter(wind);
658 return iter.next();
659}
660
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000661static bool curr_title(SkWindow* wind, SkString* title) {
662 SkView* view = curr_view(wind);
663 if (view) {
664 SkEvent evt(gTitleEvtName);
665 if (view->doQuery(&evt)) {
666 title->set(evt.findString(gTitleEvtName));
667 return true;
668 }
669 }
670 return false;
671}
672
Scroggo2c8208f2011-06-15 16:49:08 +0000673void SampleWindow::setZoomCenter(float x, float y)
674{
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000675 fZoomCenterX = x;
676 fZoomCenterY = y;
Scroggo2c8208f2011-06-15 16:49:08 +0000677}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000678
Scroggo0f185c22011-03-24 18:35:50 +0000679bool SampleWindow::zoomIn()
680{
681 // Arbitrarily decided
682 if (fFatBitsScale == 25) return false;
683 fFatBitsScale++;
684 this->inval(NULL);
685 return true;
686}
687
688bool SampleWindow::zoomOut()
689{
690 if (fFatBitsScale == 1) return false;
691 fFatBitsScale--;
692 this->inval(NULL);
693 return true;
694}
695
Scroggo0f185c22011-03-24 18:35:50 +0000696void SampleWindow::updatePointer(int x, int y)
697{
698 fMouseX = x;
699 fMouseY = y;
700 if (fShowZoomer) {
701 this->inval(NULL);
702 }
703}
704
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000705static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
706 static const SampleWindow::DeviceType gCT[] = {
707 SampleWindow::kPicture_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000708#if SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000709 SampleWindow::kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000710#if SK_ANGLE
711 SampleWindow::kANGLE_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000712#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +0000713 SampleWindow::kRaster_DeviceType, // skip the null gpu device in normal cycling
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000714#endif // SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000715 SampleWindow::kRaster_DeviceType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000716 };
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000717 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gCT) == SampleWindow::kDeviceTypeCnt, array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000718 return gCT[ct];
719}
720
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000721static void usage(const char * argv0) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000722 SkDebugf("%s [--slide sampleName] [-i resourcePath] [--msaa sampleCount] [--pictureDir dirPath] [--picture path] [--sort]\n", argv0);
723#ifdef SAMPLE_PDF_FILE_VIEWER
724 SkDebugf(" [--pdfDir pdfPath]\n");
725 SkDebugf(" pdfPath: path to directory pdf files are read from\n");
726#endif // SAMPLE_PDF_FILE_VIEWER
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000727 SkDebugf(" sampleName: sample at which to start.\n");
728 SkDebugf(" resourcePath: directory that stores image resources.\n");
bsalomon@google.com11959252012-04-06 20:13:38 +0000729 SkDebugf(" msaa: request multisampling with the given sample count.\n");
chudy@google.com4605a3f2012-08-01 17:58:01 +0000730 SkDebugf(" dirPath: path to directory skia pictures are read from\n");
731 SkDebugf(" path: path to skia picture\n");
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000732 SkDebugf(" --sort: sort samples by title, this would help to compare pdf rendering (P:foo.pdf) with skp rendering (P:foo.pdf)\n");
733}
734
735static SkString getSampleTitle(const SkViewFactory* sampleFactory) {
736 SkView* view = (*sampleFactory)();
737 SkString title;
738 SampleCode::RequestTitle(view, &title);
739 view->unref();
740 return title;
741}
742
edisonn@google.comde36b692013-07-11 15:40:31 +0000743static bool compareSampleTitle(const SkViewFactory* first, const SkViewFactory* second) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000744 return strcmp(getSampleTitle(first).c_str(), getSampleTitle(second).c_str()) < 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000745}
746
chudy@google.com4605a3f2012-08-01 17:58:01 +0000747SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager)
robertphillips@google.com4750fa52012-04-10 13:34:11 +0000748 : INHERITED(hwnd)
749 , fDevManager(NULL) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000750
reed@google.com7d05ac92013-06-06 13:14:13 +0000751 fCurrIndex = -1;
752
reed@google.com1830c7a2012-06-04 12:05:43 +0000753 this->registerPictFileSamples(argv, argc);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000754 this->registerPictFileSample(argv, argc);
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000755#ifdef SAMPLE_PDF_FILE_VIEWER
756 this->registerPdfFileViewerSamples(argv, argc);
757#endif // SAMPLE_PDF_FILE_VIEWER
robertphillips@google.com76d40212012-03-22 14:12:15 +0000758 SkGMRegistyToSampleRegistry();
759 {
760 const SkViewRegister* reg = SkViewRegister::Head();
761 while (reg) {
762 *fSamples.append() = reg->factory();
763 reg = reg->next();
764 }
765 }
766
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000767 bool sort = false;
768 for (int i = 0; i < argc; ++i) {
769 if (!strcmp(argv[i], "--sort")) {
770 sort = true;
771 break;
772 }
773 }
774
775 if (sort) {
776 // Sort samples, so foo.skp and foo.pdf are consecutive and we can quickly spot where
777 // skp -> pdf -> png fails.
778 SkTQSort(fSamples.begin(), fSamples.end() ? fSamples.end() - 1 : NULL, compareSampleTitle);
779 }
780
halcanary@google.com9acb8cd2014-01-10 14:53:49 +0000781 const char* resourcePath = "resources"; // same default as tests
bsalomon@google.com11959252012-04-06 20:13:38 +0000782 fMSAASampleCount = 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000783
784 const char* const commandName = argv[0];
785 char* const* stop = argv + argc;
786 for (++argv; argv < stop; ++argv) {
reed@google.com55ebe8e2013-09-10 19:12:07 +0000787 if (!strcmp(*argv, "-i") || !strcmp(*argv, "--resourcePath")) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000788 argv++;
789 if (argv < stop && **argv) {
790 resourcePath = *argv;
791 }
robertphillips@google.com76d40212012-03-22 14:12:15 +0000792 } else if (strcmp(*argv, "--slide") == 0) {
793 argv++;
794 if (argv < stop && **argv) {
795 fCurrIndex = findByTitle(*argv);
796 if (fCurrIndex < 0) {
797 fprintf(stderr, "Unknown sample \"%s\"\n", *argv);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000798 listTitles();
robertphillips@google.com76d40212012-03-22 14:12:15 +0000799 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000800 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000801 } else if (strcmp(*argv, "--msaa") == 0) {
802 ++argv;
803 if (argv < stop && **argv) {
804 fMSAASampleCount = atoi(*argv);
805 }
robertphillips@google.com7265e722012-05-03 18:22:28 +0000806 } else if (strcmp(*argv, "--list") == 0) {
807 listTitles();
halcanary@google.com805ca192013-10-14 12:47:37 +0000808 } else if (strcmp(*argv, "--pictureDir") == 0) {
809 ++argv; // This case is dealt with in registerPictFileSamples().
810 } else if (strcmp(*argv, "--picture") == 0) {
811 ++argv; // This case is dealt with in registerPictFileSample().
bsalomon@google.com11959252012-04-06 20:13:38 +0000812 }
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000813 else {
robertphillips@google.com76d40212012-03-22 14:12:15 +0000814 usage(commandName);
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000815 }
816 }
817
818 if (fCurrIndex < 0) {
819 SkString title;
820 if (readTitleFromPrefs(&title)) {
821 fCurrIndex = findByTitle(title.c_str());
822 }
823 }
824
825 if (fCurrIndex < 0) {
826 fCurrIndex = 0;
827 }
828
reed@google.com3cec4d72011-07-06 13:59:47 +0000829 gSampleWindow = this;
830
yangsu@google.com1f394212011-06-01 18:03:34 +0000831#ifdef PIPE_FILE
832 //Clear existing file or create file if it doesn't exist
833 FILE* f = fopen(FILE_PATH, "wb");
834 fclose(f);
835#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +0000836
reed@android.com8a1c16f2008-12-17 15:59:43 +0000837 fPicture = NULL;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000838
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000839 fDeviceType = kRaster_DeviceType;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000840
841#if DEFAULT_TO_GPU
842 fDeviceType = kGPU_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000843#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000844#if SK_ANGLE && DEFAULT_TO_ANGLE
845 fDeviceType = kANGLE_DeviceType;
846#endif
847
reed@android.com8a1c16f2008-12-17 15:59:43 +0000848 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000849 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000850 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000851 fRotate = false;
bungeman@google.comb1785912013-07-09 21:58:56 +0000852 fRotateAnimTime = 0;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000853 fPerspAnim = false;
854 fPerspAnimTime = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +0000855 fRequestGrabImage = false;
scroggo@google.comb073d922012-06-08 15:35:03 +0000856 fPipeState = SkOSMenu::kOffState;
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000857 fTilingMode = kNo_Tiling;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000858 fMeasureFPS = false;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000859 fLCDState = SkOSMenu::kMixedState;
860 fAAState = SkOSMenu::kMixedState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000861 fSubpixelState = SkOSMenu::kMixedState;
862 fHintingState = 0;
reed@google.com15bc13d2013-12-10 16:53:13 +0000863 fFilterLevelIndex = 0;
reed@google.com569e0432011-04-05 13:07:03 +0000864 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000865 fScrollTestX = fScrollTestY = 0;
866
Scroggo0f185c22011-03-24 18:35:50 +0000867 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000868 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000869 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
870 fShowZoomer = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000871
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000872 fZoomLevel = 0;
873 fZoomScale = SK_Scalar1;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000874
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000875 fMagnify = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000876
Scroggo8ac0d542011-06-21 14:44:57 +0000877 fSaveToPdf = false;
878 fPdfCanvas = NULL;
879
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000880 fTransitionNext = 6;
881 fTransitionPrev = 2;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000882
yangsu@google.com921091f2011-08-02 13:39:12 +0000883 int sinkID = this->getSinkID();
scroggo@google.com7dadc742012-04-18 14:07:57 +0000884 fAppMenu = new SkOSMenu;
885 fAppMenu->setTitle("Global Settings");
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000886 int itemID;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000887
888 itemID =fAppMenu->appendList("Device Type", "Device Type", sinkID, 0,
889 "Raster", "Picture", "OpenGL",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000890#if SK_ANGLE
891 "ANGLE",
892#endif
893 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000894 fAppMenu->assignKeyEquivalentToItem(itemID, 'd');
895 itemID = fAppMenu->appendTriState("AA", "AA", sinkID, fAAState);
896 fAppMenu->assignKeyEquivalentToItem(itemID, 'b');
897 itemID = fAppMenu->appendTriState("LCD", "LCD", sinkID, fLCDState);
898 fAppMenu->assignKeyEquivalentToItem(itemID, 'l');
reed@google.com15bc13d2013-12-10 16:53:13 +0000899 itemID = fAppMenu->appendList("FilterLevel", "FilterLevel", sinkID, fFilterLevelIndex,
900 gFilterLevelStates[0].fName,
901 gFilterLevelStates[1].fName,
902 gFilterLevelStates[2].fName,
903 gFilterLevelStates[3].fName,
904 gFilterLevelStates[4].fName,
905 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000906 fAppMenu->assignKeyEquivalentToItem(itemID, 'n');
bungeman@google.com96aabc82013-06-03 21:26:34 +0000907 itemID = fAppMenu->appendTriState("Subpixel", "Subpixel", sinkID, fSubpixelState);
908 fAppMenu->assignKeyEquivalentToItem(itemID, 's');
909 itemID = fAppMenu->appendList("Hinting", "Hinting", sinkID, fHintingState,
910 gHintingStates[0].name,
911 gHintingStates[1].name,
912 gHintingStates[2].name,
913 gHintingStates[3].name,
bungeman@google.coma9133bb2013-06-04 17:49:20 +0000914 gHintingStates[4].name,
915 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000916 fAppMenu->assignKeyEquivalentToItem(itemID, 'h');
reed@google.com67b89ee2012-08-15 14:41:58 +0000917
scroggo@google.comb073d922012-06-08 15:35:03 +0000918 fUsePipeMenuItemID = fAppMenu->appendTriState("Pipe", "Pipe" , sinkID,
chudy@google.com4605a3f2012-08-01 17:58:01 +0000919 fPipeState);
scroggo@google.comb073d922012-06-08 15:35:03 +0000920 fAppMenu->assignKeyEquivalentToItem(fUsePipeMenuItemID, 'P');
reed@google.com67b89ee2012-08-15 14:41:58 +0000921
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000922 itemID =fAppMenu->appendList("Tiling", "Tiling", sinkID, fTilingMode,
923 gTilingInfo[kNo_Tiling].label,
924 gTilingInfo[kAbs_128x128_Tiling].label,
925 gTilingInfo[kAbs_256x256_Tiling].label,
926 gTilingInfo[kRel_4x4_Tiling].label,
927 gTilingInfo[kRel_1x16_Tiling].label,
928 gTilingInfo[kRel_16x1_Tiling].label,
929 NULL);
reed@google.com67b89ee2012-08-15 14:41:58 +0000930 fAppMenu->assignKeyEquivalentToItem(itemID, 't');
931
chudy@google.com4605a3f2012-08-01 17:58:01 +0000932 itemID = fAppMenu->appendSwitch("Slide Show", "Slide Show" , sinkID, false);
933 fAppMenu->assignKeyEquivalentToItem(itemID, 'a');
934 itemID = fAppMenu->appendSwitch("Clip", "Clip" , sinkID, fUseClip);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000935 fAppMenu->assignKeyEquivalentToItem(itemID, 'c');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000936 itemID = fAppMenu->appendSwitch("Flip X", "Flip X" , sinkID, false);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000937 fAppMenu->assignKeyEquivalentToItem(itemID, 'x');
938 itemID = fAppMenu->appendSwitch("Flip Y", "Flip Y" , sinkID, false);
939 fAppMenu->assignKeyEquivalentToItem(itemID, 'y');
940 itemID = fAppMenu->appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
941 fAppMenu->assignKeyEquivalentToItem(itemID, 'z');
942 itemID = fAppMenu->appendSwitch("Magnify", "Magnify" , sinkID, fMagnify);
943 fAppMenu->assignKeyEquivalentToItem(itemID, 'm');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000944 itemID =fAppMenu->appendList("Transition-Next", "Transition-Next", sinkID,
945 fTransitionNext, "Up", "Up and Right", "Right",
946 "Down and Right", "Down", "Down and Left",
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000947 "Left", "Up and Left", NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000948 fAppMenu->assignKeyEquivalentToItem(itemID, 'j');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000949 itemID =fAppMenu->appendList("Transition-Prev", "Transition-Prev", sinkID,
950 fTransitionPrev, "Up", "Up and Right", "Right",
951 "Down and Right", "Down", "Down and Left",
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000952 "Left", "Up and Left", NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000953 fAppMenu->assignKeyEquivalentToItem(itemID, 'k');
954 itemID = fAppMenu->appendAction("Save to PDF", sinkID);
955 fAppMenu->assignKeyEquivalentToItem(itemID, 'e');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000956
scroggo@google.com7dadc742012-04-18 14:07:57 +0000957 this->addMenu(fAppMenu);
958 fSlideMenu = new SkOSMenu;
959 this->addMenu(fSlideMenu);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000960
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +0000961 this->setColorType(kPMColor_SkColorType);
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000962 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000963 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000964
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000965 skiagm::GM::SetResourcePath(resourcePath);
966
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000967 this->loadView((*fSamples[fCurrIndex])());
chudy@google.com4605a3f2012-08-01 17:58:01 +0000968
bsalomon@google.com840e9f32011-07-06 21:59:09 +0000969 fPDFData = NULL;
reed@google.comf0b5f682011-03-11 20:08:25 +0000970
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000971 if (NULL == devManager) {
972 fDevManager = new DefaultDeviceManager();
973 } else {
974 devManager->ref();
975 fDevManager = devManager;
976 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000977 fDevManager->setUpBackend(this, fMSAASampleCount);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000978
Scroggob4490c72011-06-17 13:53:05 +0000979 // If another constructor set our dimensions, ensure that our
980 // onSizeChange gets called.
981 if (this->height() && this->width()) {
982 this->onSizeChange();
983 }
reed@google.com2072db82011-11-08 15:18:10 +0000984
985 // can't call this synchronously, since it may require a subclass to
986 // to implement, or the caller may need us to have returned from the
987 // constructor first. Hence we post an event to ourselves.
988// this->updateTitle();
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +0000989 post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000990}
991
992SampleWindow::~SampleWindow() {
993 delete fPicture;
Scroggo8ac0d542011-06-21 14:44:57 +0000994 delete fPdfCanvas;
Scroggo0f185c22011-03-24 18:35:50 +0000995 fTypeface->unref();
reed@google.com29038ed2011-07-06 17:56:47 +0000996
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000997 SkSafeUnref(fDevManager);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000998}
999
reed@google.com1830c7a2012-06-04 12:05:43 +00001000static void make_filepath(SkString* path, const char* dir, const SkString& name) {
1001 size_t len = strlen(dir);
1002 path->set(dir);
1003 if (len > 0 && dir[len - 1] != '/') {
1004 path->append("/");
1005 }
1006 path->append(name);
1007}
1008
chudy@google.com4605a3f2012-08-01 17:58:01 +00001009void SampleWindow::registerPictFileSample(char** argv, int argc) {
1010 const char* pict = NULL;
1011
1012 for (int i = 0; i < argc; ++i) {
1013 if (!strcmp(argv[i], "--picture")) {
1014 i += 1;
1015 if (i < argc) {
1016 pict = argv[i];
1017 break;
1018 }
1019 }
1020 }
1021 if (pict) {
1022 SkString path(pict);
reed@google.com7d05ac92013-06-06 13:14:13 +00001023 fCurrIndex = fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001024 *fSamples.append() = new PictFileFactory(path);
1025 }
1026}
1027
reed@google.com1830c7a2012-06-04 12:05:43 +00001028void SampleWindow::registerPictFileSamples(char** argv, int argc) {
1029 const char* pictDir = NULL;
1030
1031 for (int i = 0; i < argc; ++i) {
1032 if (!strcmp(argv[i], "--pictureDir")) {
1033 i += 1;
1034 if (i < argc) {
1035 pictDir = argv[i];
1036 break;
1037 }
1038 }
1039 }
1040 if (pictDir) {
1041 SkOSFile::Iter iter(pictDir, "skp");
1042 SkString filename;
1043 while (iter.next(&filename)) {
1044 SkString path;
1045 make_filepath(&path, pictDir, filename);
1046 *fSamples.append() = new PictFileFactory(path);
1047 }
1048 }
1049}
1050
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001051#ifdef SAMPLE_PDF_FILE_VIEWER
1052void SampleWindow::registerPdfFileViewerSamples(char** argv, int argc) {
1053 const char* pdfDir = NULL;
1054
1055 for (int i = 0; i < argc; ++i) {
1056 if (!strcmp(argv[i], "--pdfDir")) {
1057 i += 1;
1058 if (i < argc) {
1059 pdfDir = argv[i];
1060 break;
1061 }
1062 }
1063 }
1064 if (pdfDir) {
1065 SkOSFile::Iter iter(pdfDir, "pdf");
1066 SkString filename;
1067 while (iter.next(&filename)) {
1068 SkString path;
1069 make_filepath(&path, pdfDir, filename);
1070 *fSamples.append() = new PdfFileViewerFactory(path);
1071 }
1072 }
1073}
1074#endif // SAMPLE_PDF_FILE_VIEWER
1075
1076
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001077int SampleWindow::findByTitle(const char title[]) {
1078 int i, count = fSamples.count();
1079 for (i = 0; i < count; i++) {
1080 if (getSampleTitle(i).equals(title)) {
1081 return i;
1082 }
1083 }
mike@reedtribe.orgdd52caa2011-12-28 20:02:10 +00001084 return -1;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001085}
1086
robertphillips@google.com7265e722012-05-03 18:22:28 +00001087void SampleWindow::listTitles() {
1088 int count = fSamples.count();
1089 SkDebugf("All Slides:\n");
1090 for (int i = 0; i < count; i++) {
1091 SkDebugf(" %s\n", getSampleTitle(i).c_str());
1092 }
1093}
1094
reed@android.com55e76b22009-11-23 21:46:47 +00001095static SkBitmap capture_bitmap(SkCanvas* canvas) {
1096 SkBitmap bm;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001097 if (bm.allocPixels(canvas->imageInfo())) {
1098 canvas->readPixels(&bm, 0, 0);
1099 }
reed@android.com55e76b22009-11-23 21:46:47 +00001100 return bm;
1101}
1102
1103static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
1104 SkBitmap* diff) {
1105 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001106
reed@android.com55e76b22009-11-23 21:46:47 +00001107 SkAutoLockPixels alp0(src);
1108 SkAutoLockPixels alp1(orig);
1109 for (int y = 0; y < src.height(); y++) {
1110 const void* srcP = src.getAddr(0, y);
1111 const void* origP = orig.getAddr(0, y);
1112 size_t bytes = src.width() * src.bytesPerPixel();
1113 if (memcmp(srcP, origP, bytes)) {
1114 SkDebugf("---------- difference on line %d\n", y);
1115 return true;
1116 }
1117 }
1118 return false;
1119}
1120
Scroggo0f185c22011-03-24 18:35:50 +00001121static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
1122{
1123 SkColor desiredColor = paint.getColor();
1124 paint.setColor(SK_ColorWHITE);
1125 const char* c_str = string.c_str();
1126 size_t size = string.size();
1127 SkRect bounds;
1128 paint.measureText(c_str, size, &bounds);
1129 bounds.offset(left, top);
1130 SkScalar inset = SkIntToScalar(-2);
1131 bounds.inset(inset, inset);
1132 canvas->drawRect(bounds, paint);
1133 if (desiredColor != SK_ColorBLACK) {
1134 paint.setColor(SK_ColorBLACK);
1135 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
1136 }
1137 paint.setColor(desiredColor);
1138 canvas->drawText(c_str, size, left, top, paint);
1139}
1140
reed@android.com44177402009-11-23 21:07:51 +00001141#define XCLIP_N 8
1142#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +00001143
1144void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +00001145 // update the animation time
bsalomon@google.come8f09102011-09-08 18:48:12 +00001146 if (!gAnimTimePrev && !gAnimTime) {
1147 // first time make delta be 0
1148 gAnimTime = SkTime::GetMSecs();
1149 gAnimTimePrev = gAnimTime;
1150 } else {
1151 gAnimTimePrev = gAnimTime;
1152 gAnimTime = SkTime::GetMSecs();
1153 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001154
reed@google.comf03bb562011-11-11 21:42:12 +00001155 if (fGesture.isActive()) {
1156 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001157 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001158
djsollen@google.com796763e2012-12-10 14:12:55 +00001159 if (fMeasureFPS) {
1160 fMeasureFPS_Time = 0;
1161 }
1162
reed@android.come522ca52009-11-23 20:10:41 +00001163 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +00001164 this->INHERITED::draw(canvas);
1165 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +00001166
1167 const SkScalar w = this->width();
1168 const SkScalar h = this->height();
1169 const SkScalar cw = w / XCLIP_N;
1170 const SkScalar ch = h / YCLIP_N;
1171 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +00001172 SkRect r;
1173 r.fTop = y * ch;
1174 r.fBottom = (y + 1) * ch;
1175 if (y == YCLIP_N - 1) {
1176 r.fBottom = h;
1177 }
reed@android.come522ca52009-11-23 20:10:41 +00001178 for (int x = 0; x < XCLIP_N; x++) {
1179 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +00001180 r.fLeft = x * cw;
1181 r.fRight = (x + 1) * cw;
1182 if (x == XCLIP_N - 1) {
1183 r.fRight = w;
1184 }
reed@android.come522ca52009-11-23 20:10:41 +00001185 canvas->clipRect(r);
1186 this->INHERITED::draw(canvas);
1187 }
1188 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001189
reed@android.com55e76b22009-11-23 21:46:47 +00001190 SkBitmap diff;
1191 if (bitmap_diff(canvas, orig, &diff)) {
1192 }
reed@android.come522ca52009-11-23 20:10:41 +00001193 } else {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00001194 SkSize tile = this->tileSize();
rmistry@google.comae933ce2012-08-23 18:19:56 +00001195
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001196 for (SkScalar y = 0; y < height(); y += tile.height()) {
1197 for (SkScalar x = 0; x < width(); x += tile.width()) {
scroggo@google.com85cade02012-08-17 13:29:50 +00001198 SkAutoCanvasRestore acr(canvas, true);
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001199 canvas->clipRect(SkRect::MakeXYWH(x, y,
1200 tile.width(),
1201 tile.height()));
scroggo@google.com85cade02012-08-17 13:29:50 +00001202 this->INHERITED::draw(canvas);
1203 }
1204 }
rmistry@google.comae933ce2012-08-23 18:19:56 +00001205
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001206 if (fTilingMode != kNo_Tiling) {
scroggo@google.com85cade02012-08-17 13:29:50 +00001207 SkPaint paint;
1208 paint.setColor(0x60FF00FF);
1209 paint.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001210
1211 for (SkScalar y = 0; y < height(); y += tile.height()) {
1212 for (SkScalar x = 0; x < width(); x += tile.width()) {
1213 canvas->drawRect(SkRect::MakeXYWH(x, y,
1214 tile.width(),
1215 tile.height()),
1216 paint);
scroggo@google.com85cade02012-08-17 13:29:50 +00001217 }
1218 }
1219 }
reed@android.come522ca52009-11-23 20:10:41 +00001220 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001221 if (fShowZoomer && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +00001222 showZoomer(canvas);
1223 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001224 if (fMagnify && !fSaveToPdf) {
1225 magnify(canvas);
1226 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001227
djsollen@google.com796763e2012-12-10 14:12:55 +00001228 if (fMeasureFPS && fMeasureFPS_Time) {
1229 this->updateTitle();
1230 this->postInvalDelay();
1231 }
1232
reed@google.com29038ed2011-07-06 17:56:47 +00001233 // do this last
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001234 fDevManager->publishCanvas(fDeviceType, canvas, this);
Scroggo3272ba82011-05-25 20:50:42 +00001235}
1236
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001237static float clipW = 200;
1238static float clipH = 200;
1239void SampleWindow::magnify(SkCanvas* canvas) {
1240 SkRect r;
1241 int count = canvas->save();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001242
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001243 SkMatrix m = canvas->getTotalMatrix();
robertphillips@google.com07ef9112012-06-04 13:22:14 +00001244 if (!m.invert(&m)) {
1245 return;
1246 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001247 SkPoint offset, center;
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001248 SkScalar mouseX = fMouseX * SK_Scalar1;
1249 SkScalar mouseY = fMouseY * SK_Scalar1;
1250 m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset);
1251 m.mapXY(mouseX, mouseY, &center);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001252
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001253 r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX());
1254 r.offset(offset.fX, offset.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001255
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001256 SkPaint paint;
1257 paint.setColor(0xFF66AAEE);
1258 paint.setStyle(SkPaint::kStroke_Style);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001259 paint.setStrokeWidth(10.f * m.getScaleX());
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001260 //lense offset
1261 //canvas->translate(0, -250);
1262 canvas->drawRect(r, paint);
1263 canvas->clipRect(r);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001264
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001265 m = canvas->getTotalMatrix();
1266 m.setTranslate(-center.fX, -center.fY);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001267 m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001268 m.postTranslate(center.fX, center.fY);
1269 canvas->concat(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001270
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001271 this->INHERITED::draw(canvas);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001272
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001273 canvas->restoreToCount(count);
1274}
1275
Scroggo3272ba82011-05-25 20:50:42 +00001276void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +00001277 int count = canvas->save();
1278 canvas->resetMatrix();
1279 // Ensure the mouse position is on screen.
reed@google.come1ca7052013-12-17 19:22:07 +00001280 int width = SkScalarRoundToInt(this->width());
1281 int height = SkScalarRoundToInt(this->height());
Scroggo0f185c22011-03-24 18:35:50 +00001282 if (fMouseX >= width) fMouseX = width - 1;
1283 else if (fMouseX < 0) fMouseX = 0;
1284 if (fMouseY >= height) fMouseY = height - 1;
1285 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +00001286
Scroggo0f185c22011-03-24 18:35:50 +00001287 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +00001288 bitmap.lockPixels();
1289
Scroggo0f185c22011-03-24 18:35:50 +00001290 // 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 +00001291 int zoomedWidth = (width >> 1) | 1;
1292 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +00001293 SkIRect src;
1294 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
1295 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
1296 SkRect dest;
1297 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
1298 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
1299 SkPaint paint;
1300 // Clear the background behind our zoomed in view
1301 paint.setColor(SK_ColorWHITE);
1302 canvas->drawRect(dest, paint);
1303 canvas->drawBitmapRect(bitmap, &src, dest);
1304 paint.setColor(SK_ColorBLACK);
1305 paint.setStyle(SkPaint::kStroke_Style);
1306 // Draw a border around the pixel in the middle
1307 SkRect originalPixel;
1308 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
1309 SkMatrix matrix;
1310 SkRect scalarSrc;
1311 scalarSrc.set(src);
1312 SkColor color = bitmap.getColor(fMouseX, fMouseY);
1313 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
1314 SkRect pixel;
1315 matrix.mapRect(&pixel, originalPixel);
1316 // TODO Perhaps measure the values and make the outline white if it's "dark"
1317 if (color == SK_ColorBLACK) {
1318 paint.setColor(SK_ColorWHITE);
1319 }
1320 canvas->drawRect(pixel, paint);
1321 }
1322 paint.setColor(SK_ColorBLACK);
1323 // Draw a border around the destination rectangle
1324 canvas->drawRect(dest, paint);
1325 paint.setStyle(SkPaint::kStrokeAndFill_Style);
1326 // Identify the pixel and its color on screen
1327 paint.setTypeface(fTypeface);
1328 paint.setAntiAlias(true);
1329 SkScalar lineHeight = paint.getFontMetrics(NULL);
1330 SkString string;
1331 string.appendf("(%i, %i)", fMouseX, fMouseY);
1332 SkScalar left = dest.fLeft + SkIntToScalar(3);
1333 SkScalar i = SK_Scalar1;
1334 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1335 // Alpha
1336 i += SK_Scalar1;
1337 string.reset();
1338 string.appendf("A: %X", SkColorGetA(color));
1339 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1340 // Red
1341 i += SK_Scalar1;
1342 string.reset();
1343 string.appendf("R: %X", SkColorGetR(color));
1344 paint.setColor(SK_ColorRED);
1345 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1346 // Green
1347 i += SK_Scalar1;
1348 string.reset();
1349 string.appendf("G: %X", SkColorGetG(color));
1350 paint.setColor(SK_ColorGREEN);
1351 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1352 // Blue
1353 i += SK_Scalar1;
1354 string.reset();
1355 string.appendf("B: %X", SkColorGetB(color));
1356 paint.setColor(SK_ColorBLUE);
1357 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1358 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +00001359}
1360
reed@android.com8a1c16f2008-12-17 15:59:43 +00001361void SampleWindow::onDraw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001362}
1363
1364#include "SkColorPriv.h"
1365
caryclark@google.com02939ce2012-06-06 12:09:51 +00001366#if 0 // UNUSED
reed@android.com8a1c16f2008-12-17 15:59:43 +00001367static void reverseRedAndBlue(const SkBitmap& bm) {
1368 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
1369 uint8_t* p = (uint8_t*)bm.getPixels();
1370 uint8_t* stop = p + bm.getSize();
1371 while (p < stop) {
1372 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
1373 unsigned scale = SkAlpha255To256(p[3]);
1374 unsigned r = p[2];
1375 unsigned b = p[0];
1376 p[0] = SkAlphaMul(r, scale);
1377 p[1] = SkAlphaMul(p[1], scale);
1378 p[2] = SkAlphaMul(b, scale);
1379 p += 4;
1380 }
1381}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001382#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001383
Scroggo8ac0d542011-06-21 14:44:57 +00001384void SampleWindow::saveToPdf()
1385{
1386 fSaveToPdf = true;
1387 this->inval(NULL);
1388}
1389
reed@android.com8a1c16f2008-12-17 15:59:43 +00001390SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +00001391 if (fSaveToPdf) {
1392 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
1393 SkISize size = SkISize::Make(bmp.width(), bmp.height());
1394 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
1395 canvas->getTotalMatrix());
1396 fPdfCanvas = new SkCanvas(pdfDevice);
1397 pdfDevice->unref();
1398 canvas = fPdfCanvas;
bsalomon@google.com82502e22013-01-24 20:47:18 +00001399 } else if (kPicture_DeviceType == fDeviceType) {
1400 fPicture = new SkPicture;
1401 canvas = fPicture->beginRecording(9999, 9999);
Scroggo8ac0d542011-06-21 14:44:57 +00001402 } else {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001403#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00001404 if (kNullGPU_DeviceType != fDeviceType)
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001405#endif
bsalomon@google.com82502e22013-01-24 20:47:18 +00001406 {
1407 canvas = this->INHERITED::beforeChildren(canvas);
reed@google.comac10a2d2010-12-22 21:39:39 +00001408 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001409 }
1410
1411 if (fUseClip) {
1412 canvas->drawColor(0xFFFF88FF);
reed@google.com045e62d2011-10-24 12:19:46 +00001413 canvas->clipPath(fClipPath, SkRegion::kIntersect_Op, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001414 }
1415
1416 return canvas;
1417}
1418
1419static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
1420 const SkRegion& rgn) {
1421 SkCanvas canvas(bm);
1422 SkRegion inval(rgn);
1423
1424 inval.translate(r.fLeft, r.fTop);
1425 canvas.clipRegion(inval);
1426 canvas.drawColor(0xFFFF8080);
1427}
yangsu@google.com501775e2011-06-24 16:04:50 +00001428#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001429void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +00001430 if (fSaveToPdf) {
1431 fSaveToPdf = false;
1432 if (fShowZoomer) {
1433 showZoomer(fPdfCanvas);
1434 }
1435 SkString name;
1436 name.printf("%s.pdf", this->getTitle());
1437 SkPDFDocument doc;
1438 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
1439 doc.appendPage(device);
djsollen@google.com56c69772011-11-08 19:00:26 +00001440#ifdef SK_BUILD_FOR_ANDROID
Scroggo8ac0d542011-06-21 14:44:57 +00001441 name.prepend("/sdcard/");
1442#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +00001443
yangsu@google.com501775e2011-06-24 16:04:50 +00001444#ifdef SK_BUILD_FOR_IOS
1445 SkDynamicMemoryWStream mstream;
1446 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +00001447 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +00001448#endif
Scroggo8ac0d542011-06-21 14:44:57 +00001449 SkFILEWStream stream(name.c_str());
1450 if (stream.isValid()) {
1451 doc.emitPDF(&stream);
1452 const char* desc = "File saved from Skia SampleApp";
1453 this->onPDFSaved(this->getTitle(), desc, name.c_str());
1454 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001455
Scroggo8ac0d542011-06-21 14:44:57 +00001456 delete fPdfCanvas;
1457 fPdfCanvas = NULL;
1458
1459 // We took over the draw calls in order to create the PDF, so we need
1460 // to redraw.
1461 this->inval(NULL);
1462 return;
1463 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001464
reed@android.comf2b98d62010-12-20 18:26:13 +00001465 if (fRequestGrabImage) {
1466 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001467
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001468 SkBaseDevice* device = orig->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001469 SkBitmap bmp;
1470 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001471 static int gSampleGrabCounter;
1472 SkString name;
reed@google.com7c57e0e2012-09-08 21:20:33 +00001473 name.printf("sample_grab_%d.png", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001474 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +00001475 SkImageEncoder::kPNG_Type, 100);
1476 }
1477 }
1478
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001479 if (kPicture_DeviceType == fDeviceType) {
1480 if (true) {
1481 SkPicture* pict = new SkPicture(*fPicture);
1482 fPicture->unref();
reed@google.come23f1942011-12-08 19:36:00 +00001483 this->installDrawFilter(orig);
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001484 orig->drawPicture(*pict);
1485 pict->unref();
1486 } else if (true) {
1487 SkDynamicMemoryWStream ostream;
1488 fPicture->serialize(&ostream);
1489 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001490
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001491 SkAutoDataUnref data(ostream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +00001492 SkMemoryStream istream(data->data(), data->size());
scroggo@google.comf1754ec2013-06-28 21:32:00 +00001493 SkAutoTUnref<SkPicture> pict(SkPicture::CreateFromStream(&istream));
1494 if (pict.get() != NULL) {
1495 orig->drawPicture(*pict.get());
1496 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001497 } else {
1498 fPicture->draw(orig);
1499 fPicture->unref();
1500 }
1501 fPicture = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001502 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001503
reed@google.com17d7aec2011-04-25 14:31:44 +00001504 // Do this after presentGL and other finishing, rather than in afterChild
djsollen@google.com796763e2012-12-10 14:12:55 +00001505 if (fMeasureFPS && fMeasureFPS_StartTime) {
1506 fMeasureFPS_Time += SkTime::GetMSecs() - fMeasureFPS_StartTime;
reed@google.com17d7aec2011-04-25 14:31:44 +00001507 }
1508
1509 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +00001510 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001511 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
1512 int dx = fScrollTestX * 7;
1513 int dy = fScrollTestY * 7;
1514 SkIRect r;
1515 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001516
reed@android.com8a1c16f2008-12-17 15:59:43 +00001517 r.set(50, 50, 50+100, 50+100);
1518 bm.scrollRect(&r, dx, dy, &inval);
1519 paint_rgn(bm, r, inval);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001520 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001521}
1522
reed@android.com6c5f6f22009-08-14 16:08:38 +00001523void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
reed@android.com6c5f6f22009-08-14 16:08:38 +00001524 if (fRotate) {
bungeman@google.comb1785912013-07-09 21:58:56 +00001525 fRotateAnimTime += SampleCode::GetAnimSecondsDelta();
1526
reed@android.com6c5f6f22009-08-14 16:08:38 +00001527 SkScalar cx = this->width() / 2;
1528 SkScalar cy = this->height() / 2;
1529 canvas->translate(cx, cy);
bungeman@google.comb1785912013-07-09 21:58:56 +00001530 canvas->rotate(fRotateAnimTime * 10);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001531 canvas->translate(-cx, -cy);
1532 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001533
bsalomon@google.come8f09102011-09-08 18:48:12 +00001534 if (fPerspAnim) {
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001535 fPerspAnimTime += SampleCode::GetAnimSecondsDelta();
1536
1537 static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
1538 static const SkScalar gAnimMag = SK_Scalar1 / 1000;
1539 SkScalar t = SkScalarMod(fPerspAnimTime, gAnimPeriod);
1540 if (SkScalarFloorToInt(SkScalarDiv(fPerspAnimTime, gAnimPeriod)) & 0x1) {
1541 t = gAnimPeriod - t;
1542 }
1543 t = 2 * t - gAnimPeriod;
1544 t = SkScalarMul(SkScalarDiv(t, gAnimPeriod), gAnimMag);
1545 SkMatrix m;
1546 m.reset();
1547 m.setPerspY(t);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001548 canvas->concat(m);
1549 }
reed@google.comf0b5f682011-03-11 20:08:25 +00001550
reed@google.come23f1942011-12-08 19:36:00 +00001551 this->installDrawFilter(canvas);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001552
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001553 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +00001554 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
djsollen@google.com796763e2012-12-10 14:12:55 +00001555 fMeasureFPS_StartTime = SkTime::GetMSecs();
reed@google.comf2183392011-04-22 14:10:48 +00001556 }
1557 } else {
1558 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001559 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001560 if (fPerspAnim || fRotate) {
bsalomon@google.come8f09102011-09-08 18:48:12 +00001561 this->inval(NULL);
1562 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001563}
1564
1565void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +00001566 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001567}
1568
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +00001569static SkColorType gColorTypeCycle[] = {
1570 kUnknown_SkColorType, // none -> none
1571 kUnknown_SkColorType, // a8 -> none
1572 kARGB_4444_SkColorType, // 565 -> 4444
1573 kPMColor_SkColorType, // 4444 -> 8888
1574 kRGB_565_SkColorType, // 8888 -> 565
1575 kRGB_565_SkColorType, // 8888 -> 565
1576 kUnknown_SkColorType, // index8 -> none
reed@android.com8a1c16f2008-12-17 15:59:43 +00001577};
1578
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +00001579static SkColorType cycle_configs(SkColorType c) {
1580 return gColorTypeCycle[c];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001581}
1582
djsollen@google.come32b5832011-06-13 16:58:40 +00001583void SampleWindow::changeZoomLevel(float delta) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001584 fZoomLevel += delta;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001585 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001586 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
1587 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001588 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001589 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
1590 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001591 } else {
1592 fZoomScale = SK_Scalar1;
1593 }
reed@google.comf03bb562011-11-11 21:42:12 +00001594 this->updateMatrix();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001595}
1596
reed@google.comf03bb562011-11-11 21:42:12 +00001597void SampleWindow::updateMatrix(){
1598 SkMatrix m;
1599 m.reset();
1600 if (fZoomLevel) {
1601 SkPoint center;
1602 //m = this->getLocalMatrix();//.invert(&m);
1603 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
1604 SkScalar cx = center.fX;
1605 SkScalar cy = center.fY;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001606
reed@google.comf03bb562011-11-11 21:42:12 +00001607 m.setTranslate(-cx, -cy);
1608 m.postScale(fZoomScale, fZoomScale);
1609 m.postTranslate(cx, cy);
1610 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001611
reed@google.comf03bb562011-11-11 21:42:12 +00001612 if (fFlipAxis) {
1613 m.preTranslate(fZoomCenterX, fZoomCenterY);
1614 if (fFlipAxis & kFlipAxis_X) {
1615 m.preScale(-SK_Scalar1, SK_Scalar1);
1616 }
1617 if (fFlipAxis & kFlipAxis_Y) {
1618 m.preScale(SK_Scalar1, -SK_Scalar1);
1619 }
1620 m.preTranslate(-fZoomCenterX, -fZoomCenterY);
1621 //canvas->concat(m);
1622 }
1623 // Apply any gesture matrix
1624 m.preConcat(fGesture.localM());
1625 m.preConcat(fGesture.globalM());
chudy@google.com4605a3f2012-08-01 17:58:01 +00001626
reed@google.comf03bb562011-11-11 21:42:12 +00001627 this->setLocalMatrix(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001628
reed@google.comf03bb562011-11-11 21:42:12 +00001629 this->updateTitle();
1630 this->inval(NULL);
1631}
Scroggo2c8208f2011-06-15 16:49:08 +00001632bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +00001633 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001634 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001635 fTransitionPrev));
Scroggo2c8208f2011-06-15 16:49:08 +00001636 return true;
1637}
1638
reed@android.com8a1c16f2008-12-17 15:59:43 +00001639bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +00001640 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001641 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001642 fTransitionNext));
reed@android.com34245c72009-11-03 04:00:48 +00001643 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001644}
1645
yangsu@google.com501775e2011-06-24 16:04:50 +00001646bool SampleWindow::goToSample(int i) {
1647 fCurrIndex = (i) % fSamples.count();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001648 this->loadView(create_transition(curr_view(this),(*fSamples[fCurrIndex])(), 6));
yangsu@google.com501775e2011-06-24 16:04:50 +00001649 return true;
1650}
1651
1652SkString SampleWindow::getSampleTitle(int i) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001653 return ::getSampleTitle(fSamples[i]);
yangsu@google.com501775e2011-06-24 16:04:50 +00001654}
1655
1656int SampleWindow::sampleCount() {
1657 return fSamples.count();
1658}
1659
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001660void SampleWindow::showOverview() {
chudy@google.com4605a3f2012-08-01 17:58:01 +00001661 this->loadView(create_transition(curr_view(this),
1662 create_overview(fSamples.count(), fSamples.begin()),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001663 4));
1664}
1665
reed@google.come23f1942011-12-08 19:36:00 +00001666void SampleWindow::installDrawFilter(SkCanvas* canvas) {
reed@google.com15bc13d2013-12-10 16:53:13 +00001667 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState, fSubpixelState,
1668 fHintingState, fFilterLevelIndex))->unref();
reed@google.come23f1942011-12-08 19:36:00 +00001669}
1670
Scroggo2c8208f2011-06-15 16:49:08 +00001671void SampleWindow::postAnimatingEvent() {
1672 if (fAnimating) {
reed@google.com87fac4a2011-08-04 13:50:17 +00001673 (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
Scroggo2c8208f2011-06-15 16:49:08 +00001674 }
1675}
reed@google.come23f1942011-12-08 19:36:00 +00001676
reed@android.com8a1c16f2008-12-17 15:59:43 +00001677bool SampleWindow::onEvent(const SkEvent& evt) {
reed@google.com2072db82011-11-08 15:18:10 +00001678 if (evt.isType(gUpdateWindowTitleEvtName)) {
1679 this->updateTitle();
1680 return true;
1681 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001682 if (evt.isType(ANIMATING_EVENTTYPE)) {
1683 if (fAnimating) {
1684 this->nextSample();
1685 this->postAnimatingEvent();
1686 }
1687 return true;
1688 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001689 if (evt.isType("replace-transition-view")) {
1690 this->loadView((SkView*)SkEventSink::FindSink(evt.getFast32()));
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001691 return true;
1692 }
reed@android.com34245c72009-11-03 04:00:48 +00001693 if (evt.isType("set-curr-index")) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001694 this->goToSample(evt.getFast32());
reed@android.com34245c72009-11-03 04:00:48 +00001695 return true;
1696 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001697 if (isInvalEvent(evt)) {
1698 this->inval(NULL);
1699 return true;
1700 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001701 int selected = -1;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001702 if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001703 this->setDeviceType((DeviceType)selected);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001704 return true;
yangsu@google.com921091f2011-08-02 13:39:12 +00001705 }
scroggo@google.comb073d922012-06-08 15:35:03 +00001706 if (SkOSMenu::FindTriState(evt, "Pipe", &fPipeState)) {
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001707#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00001708 if (!fPipeState != SkOSMenu::kOnState)
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001709 gServer.disconnectAll();
1710#endif
scroggo@google.comb073d922012-06-08 15:35:03 +00001711 (void)SampleView::SetUsePipe(curr_view(this), fPipeState);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001712 this->updateTitle();
1713 this->inval(NULL);
yangsu@google.com921091f2011-08-02 13:39:12 +00001714 return true;
1715 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001716 if (SkOSMenu::FindSwitchState(evt, "Slide Show", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001717 this->toggleSlideshow();
1718 return true;
1719 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001720 if (SkOSMenu::FindTriState(evt, "AA", &fAAState) ||
1721 SkOSMenu::FindTriState(evt, "LCD", &fLCDState) ||
reed@google.com15bc13d2013-12-10 16:53:13 +00001722 SkOSMenu::FindListIndex(evt, "FilterLevel", &fFilterLevelIndex) ||
bungeman@google.com96aabc82013-06-03 21:26:34 +00001723 SkOSMenu::FindTriState(evt, "Subpixel", &fSubpixelState) ||
1724 SkOSMenu::FindListIndex(evt, "Hinting", &fHintingState) ||
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001725 SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) ||
1726 SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
1727 SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify) ||
1728 SkOSMenu::FindListIndex(evt, "Transition-Next", &fTransitionNext) ||
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00001729 SkOSMenu::FindListIndex(evt, "Transition-Prev", &fTransitionPrev)) {
1730 this->inval(NULL);
1731 this->updateTitle();
1732 return true;
1733 }
1734 if (SkOSMenu::FindListIndex(evt, "Tiling", &fTilingMode)) {
1735 if (SampleView::IsSampleView(curr_view(this))) {
1736 ((SampleView*)curr_view(this))->onTileSizeChanged(this->tileSize());
1737 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001738 this->inval(NULL);
1739 this->updateTitle();
1740 return true;
1741 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001742 if (SkOSMenu::FindSwitchState(evt, "Flip X", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001743 fFlipAxis ^= kFlipAxis_X;
reed@google.comf03bb562011-11-11 21:42:12 +00001744 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001745 return true;
1746 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001747 if (SkOSMenu::FindSwitchState(evt, "Flip Y", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001748 fFlipAxis ^= kFlipAxis_Y;
reed@google.comf03bb562011-11-11 21:42:12 +00001749 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001750 return true;
1751 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001752 if (SkOSMenu::FindAction(evt,"Save to PDF")) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001753 this->saveToPdf();
1754 return true;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001755 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001756 return this->INHERITED::onEvent(evt);
1757}
1758
reed@android.comf2b98d62010-12-20 18:26:13 +00001759bool SampleWindow::onQuery(SkEvent* query) {
1760 if (query->isType("get-slide-count")) {
1761 query->setFast32(fSamples.count());
1762 return true;
1763 }
1764 if (query->isType("get-slide-title")) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001765 SkView* view = (*fSamples[query->getFast32()])();
reed@android.comf2b98d62010-12-20 18:26:13 +00001766 SkEvent evt(gTitleEvtName);
1767 if (view->doQuery(&evt)) {
1768 query->setString("title", evt.findString(gTitleEvtName));
1769 }
1770 SkSafeUnref(view);
1771 return true;
1772 }
1773 if (query->isType("use-fast-text")) {
1774 SkEvent evt(gFastTextEvtName);
1775 return curr_view(this)->doQuery(&evt);
1776 }
reed@google.com29038ed2011-07-06 17:56:47 +00001777 if (query->isType("ignore-window-bitmap")) {
1778 query->setFast32(this->getGrContext() != NULL);
1779 return true;
1780 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001781 return this->INHERITED::onQuery(query);
1782}
1783
caryclark@google.com02939ce2012-06-06 12:09:51 +00001784#if 0 // UNUSED
reed@android.com0ae6b242008-12-23 16:49:54 +00001785static void cleanup_for_filename(SkString* name) {
1786 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001787 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001788 switch (str[i]) {
1789 case ':': str[i] = '-'; break;
1790 case '/': str[i] = '-'; break;
1791 case ' ': str[i] = '_'; break;
1792 default: break;
1793 }
1794 }
1795}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001796#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001797
1798bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001799 {
1800 SkView* view = curr_view(this);
1801 if (view) {
1802 SkEvent evt(gCharEvtName);
1803 evt.setFast32(uni);
1804 if (view->doQuery(&evt)) {
1805 return true;
1806 }
1807 }
1808 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001809
reed@android.com8a1c16f2008-12-17 15:59:43 +00001810 int dx = 0xFF;
1811 int dy = 0xFF;
1812
1813 switch (uni) {
1814 case '5': dx = 0; dy = 0; break;
1815 case '8': dx = 0; dy = -1; break;
1816 case '6': dx = 1; dy = 0; break;
1817 case '2': dx = 0; dy = 1; break;
1818 case '4': dx = -1; dy = 0; break;
1819 case '7': dx = -1; dy = -1; break;
1820 case '9': dx = 1; dy = -1; break;
1821 case '3': dx = 1; dy = 1; break;
1822 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001823
reed@android.com8a1c16f2008-12-17 15:59:43 +00001824 default:
1825 break;
1826 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001827
reed@android.com8a1c16f2008-12-17 15:59:43 +00001828 if (0xFF != dx && 0xFF != dy) {
1829 if ((dx | dy) == 0) {
1830 fScrollTestX = fScrollTestY = 0;
1831 } else {
1832 fScrollTestX += dx;
1833 fScrollTestY += dy;
1834 }
1835 this->inval(NULL);
1836 return true;
1837 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001838
reed@android.com0ae6b242008-12-23 16:49:54 +00001839 switch (uni) {
reed@google.coma4f81372012-11-15 15:56:38 +00001840 case 'B':
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001841 post_event_to_sink(SkNEW_ARGS(SkEvent, ("PictFileView::toggleBBox")), curr_view(this));
1842 // Cannot call updateTitle() synchronously, because the toggleBBox event is still in
1843 // the queue.
1844 post_event_to_sink(SkNEW_ARGS(SkEvent, (gUpdateWindowTitleEvtName)), this);
reed@google.coma4f81372012-11-15 15:56:38 +00001845 this->inval(NULL);
1846 break;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001847 case 'f':
chudy@google.com4605a3f2012-08-01 17:58:01 +00001848 // only
djsollen@google.com6ff82552011-11-07 15:43:57 +00001849 toggleFPS();
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001850 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001851 case 'g':
1852 fRequestGrabImage = true;
1853 this->inval(NULL);
1854 break;
reed@google.coma4f81372012-11-15 15:56:38 +00001855 case 'G':
1856 gShowGMBounds = !gShowGMBounds;
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001857 post_event_to_sink(GMSampleView::NewShowSizeEvt(gShowGMBounds),
reed@google.coma4f81372012-11-15 15:56:38 +00001858 curr_view(this));
1859 this->inval(NULL);
1860 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001861 case 'i':
1862 this->zoomIn();
1863 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001864 case 'o':
1865 this->zoomOut();
1866 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001867 case 'r':
1868 fRotate = !fRotate;
bungeman@google.comb1785912013-07-09 21:58:56 +00001869 fRotateAnimTime = 0;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001870 this->inval(NULL);
1871 this->updateTitle();
1872 return true;
bsalomon@google.come8f09102011-09-08 18:48:12 +00001873 case 'k':
1874 fPerspAnim = !fPerspAnim;
1875 this->inval(NULL);
1876 this->updateTitle();
1877 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001878#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00001879 case '\\':
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001880 this->setDeviceType(kNullGPU_DeviceType);
1881 this->inval(NULL);
1882 this->updateTitle();
bsalomon@google.com74913722011-10-27 20:44:19 +00001883 return true;
twiz@google.com05e70242012-01-27 19:12:00 +00001884 case 'p':
1885 {
1886 GrContext* grContext = this->getGrContext();
1887 if (grContext) {
1888 size_t cacheBytes = grContext->getGpuTextureCacheBytes();
1889 grContext->freeGpuResources();
1890 SkDebugf("Purged %d bytes from the GPU resource cache.\n",
1891 cacheBytes);
1892 }
1893 }
1894 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001895#endif
reed@android.com0ae6b242008-12-23 16:49:54 +00001896 default:
1897 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001898 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001899
scroggo@google.com7dadc742012-04-18 14:07:57 +00001900 if (fAppMenu->handleKeyEquivalent(uni)|| fSlideMenu->handleKeyEquivalent(uni)) {
1901 this->onUpdateMenu(fAppMenu);
1902 this->onUpdateMenu(fSlideMenu);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001903 return true;
1904 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001905 return this->INHERITED::onHandleChar(uni);
1906}
1907
yangsu@google.com921091f2011-08-02 13:39:12 +00001908void SampleWindow::setDeviceType(DeviceType type) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001909 if (type == fDeviceType)
1910 return;
1911
1912 fDevManager->tearDownBackend(this);
1913
1914 fDeviceType = type;
1915
bsalomon@google.com11959252012-04-06 20:13:38 +00001916 fDevManager->setUpBackend(this, fMSAASampleCount);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001917
yangsu@google.com921091f2011-08-02 13:39:12 +00001918 this->updateTitle();
1919 this->inval(NULL);
1920}
1921
Scroggo2c8208f2011-06-15 16:49:08 +00001922void SampleWindow::toggleSlideshow() {
1923 fAnimating = !fAnimating;
1924 this->postAnimatingEvent();
1925 this->updateTitle();
1926}
1927
1928void SampleWindow::toggleRendering() {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001929 this->setDeviceType(cycle_devicetype(fDeviceType));
Scroggo2c8208f2011-06-15 16:49:08 +00001930 this->updateTitle();
1931 this->inval(NULL);
1932}
1933
djsollen@google.com6ff82552011-11-07 15:43:57 +00001934void SampleWindow::toggleFPS() {
1935 fMeasureFPS = !fMeasureFPS;
1936 this->updateTitle();
1937 this->inval(NULL);
1938}
1939
reed@android.com8a1c16f2008-12-17 15:59:43 +00001940#include "SkDumpCanvas.h"
1941
1942bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001943 {
1944 SkView* view = curr_view(this);
1945 if (view) {
1946 SkEvent evt(gKeyEvtName);
1947 evt.setFast32(key);
1948 if (view->doQuery(&evt)) {
1949 return true;
1950 }
1951 }
1952 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001953 switch (key) {
1954 case kRight_SkKey:
1955 if (this->nextSample()) {
1956 return true;
1957 }
1958 break;
1959 case kLeft_SkKey:
scroggo@google.come2dd9732012-08-15 20:03:06 +00001960 if (this->previousSample()) {
1961 return true;
1962 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001963 return true;
1964 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001965 if (USE_ARROWS_FOR_ZOOM) {
bungeman@google.comcefd9812013-05-15 15:07:32 +00001966 this->changeZoomLevel(1.f / 32.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001967 } else {
1968 fNClip = !fNClip;
1969 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001970 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001971 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001972 return true;
1973 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001974 if (USE_ARROWS_FOR_ZOOM) {
bungeman@google.comcefd9812013-05-15 15:07:32 +00001975 this->changeZoomLevel(-1.f / 32.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001976 } else {
commit-bot@chromium.orge24ad232014-02-16 22:03:38 +00001977 this->setColorType(cycle_configs(this->getBitmap().colorType()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001978 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001979 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001980 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001981 case kOK_SkKey: {
1982 SkString title;
1983 if (curr_title(this, &title)) {
1984 writeTitleToPrefs(title.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001985 }
1986 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001987 }
reed@android.com34245c72009-11-03 04:00:48 +00001988 case kBack_SkKey:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001989 this->showOverview();
reed@android.com34245c72009-11-03 04:00:48 +00001990 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001991 default:
1992 break;
1993 }
1994 return this->INHERITED::onHandleKey(key);
1995}
1996
reed@google.com52f57e12011-03-16 12:10:02 +00001997///////////////////////////////////////////////////////////////////////////////
1998
1999static const char gGestureClickType[] = "GestureClickType";
2000
Scroggod3aed392011-06-22 13:26:56 +00002001bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
reed@google.com4d5c26d2013-01-08 16:17:50 +00002002 void* owner, unsigned modi) {
Scroggo0f185c22011-03-24 18:35:50 +00002003 if (Click::kMoved_State == state) {
2004 updatePointer(x, y);
2005 }
reed@google.come1ca7052013-12-17 19:22:07 +00002006 int w = SkScalarRoundToInt(this->width());
2007 int h = SkScalarRoundToInt(this->height());
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002008
2009 // check for the resize-box
2010 if (w - x < 16 && h - y < 16) {
2011 return false; // let the OS handle the click
chudy@google.com4605a3f2012-08-01 17:58:01 +00002012 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002013 else if (fMagnify) {
2014 //it's only necessary to update the drawing if there's a click
2015 this->inval(NULL);
2016 return false; //prevent dragging while magnify is enabled
reed@google.com4d5c26d2013-01-08 16:17:50 +00002017 } else {
2018 // capture control+option, and trigger debugger
2019 if ((modi & kControl_SkModifierKey) && (modi & kOption_SkModifierKey)) {
2020 if (Click::kDown_State == state) {
2021 SkEvent evt("debug-hit-test");
2022 evt.setS32("debug-hit-test-x", x);
2023 evt.setS32("debug-hit-test-y", y);
2024 curr_view(this)->doEvent(evt);
2025 }
2026 return true;
2027 } else {
2028 return this->INHERITED::onDispatchClick(x, y, state, owner, modi);
2029 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002030 }
2031}
2032
reed@google.com52f57e12011-03-16 12:10:02 +00002033class GestureClick : public SkView::Click {
2034public:
2035 GestureClick(SkView* target) : SkView::Click(target) {
2036 this->setType(gGestureClickType);
2037 }
2038
2039 static bool IsGesture(Click* click) {
2040 return click->isType(gGestureClickType);
2041 }
2042};
2043
reed@google.com4d5c26d2013-01-08 16:17:50 +00002044SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y,
2045 unsigned modi) {
reed@google.com52f57e12011-03-16 12:10:02 +00002046 return new GestureClick(this);
2047}
2048
2049bool SampleWindow::onClick(Click* click) {
2050 if (GestureClick::IsGesture(click)) {
bsalomon@google.com4d4f2812011-12-12 18:34:01 +00002051 float x = static_cast<float>(click->fICurr.fX);
2052 float y = static_cast<float>(click->fICurr.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00002053
reed@google.com52f57e12011-03-16 12:10:02 +00002054 switch (click->fState) {
2055 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00002056 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00002057 break;
2058 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00002059 fGesture.touchMoved(click->fOwner, x, y);
reed@google.comf03bb562011-11-11 21:42:12 +00002060 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002061 break;
2062 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00002063 fGesture.touchEnd(click->fOwner);
reed@google.comf03bb562011-11-11 21:42:12 +00002064 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002065 break;
2066 }
2067 return true;
2068 }
2069 return false;
2070}
2071
2072///////////////////////////////////////////////////////////////////////////////
2073
reed@android.com8a1c16f2008-12-17 15:59:43 +00002074void SampleWindow::loadView(SkView* view) {
2075 SkView::F2BIter iter(this);
2076 SkView* prev = iter.next();
2077 if (prev) {
2078 prev->detachFromParent();
2079 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002080
reed@android.com8a1c16f2008-12-17 15:59:43 +00002081 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00002082 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002083 this->attachChildToFront(view)->unref();
2084 view->setSize(this->width(), this->height());
2085
yangsu@google.com921091f2011-08-02 13:39:12 +00002086 //repopulate the slide menu when a view is loaded
scroggo@google.com7dadc742012-04-18 14:07:57 +00002087 fSlideMenu->reset();
djsollen@google.com388974f2013-01-17 13:20:01 +00002088
scroggo@google.comb073d922012-06-08 15:35:03 +00002089 (void)SampleView::SetUsePipe(view, fPipeState);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00002090 if (SampleView::IsSampleView(view)) {
2091 SampleView* sampleView = (SampleView*)view;
2092 sampleView->requestMenu(fSlideMenu);
2093 sampleView->onTileSizeChanged(this->tileSize());
2094 }
scroggo@google.com7dadc742012-04-18 14:07:57 +00002095 this->onUpdateMenu(fSlideMenu);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002096 this->updateTitle();
2097}
2098
2099static const char* gConfigNames[] = {
2100 "unknown config",
reed@android.com8a1c16f2008-12-17 15:59:43 +00002101 "A8",
2102 "Index8",
2103 "565",
2104 "4444",
2105 "8888"
2106};
2107
2108static const char* configToString(SkBitmap::Config c) {
2109 return gConfigNames[c];
2110}
2111
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002112static const char* gDeviceTypePrefix[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002113 "raster: ",
2114 "picture: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002115#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00002116 "opengl: ",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00002117#if SK_ANGLE
2118 "angle: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002119#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +00002120 "null-gl: "
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002121#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +00002122};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002123SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gDeviceTypePrefix) == SampleWindow::kDeviceTypeCnt,
2124 array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002125
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002126static const char* trystate_str(SkOSMenu::TriState state,
reed@google.com569e0432011-04-05 13:07:03 +00002127 const char trueStr[], const char falseStr[]) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002128 if (SkOSMenu::kOnState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002129 return trueStr;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002130 } else if (SkOSMenu::kOffState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002131 return falseStr;
2132 }
2133 return NULL;
2134}
2135
reed@android.com8a1c16f2008-12-17 15:59:43 +00002136void SampleWindow::updateTitle() {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002137 SkView* view = curr_view(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002138
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002139 SkString title;
2140 if (!curr_title(this, &title)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002141 title.set("<unknown>");
2142 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002143
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002144 title.prepend(gDeviceTypePrefix[fDeviceType]);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002145
2146 title.prepend(" ");
2147 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002148
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00002149 if (fTilingMode != kNo_Tiling) {
2150 title.prependf("<T: %s> ", gTilingInfo[fTilingMode].label);
2151 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002152 if (fAnimating) {
2153 title.prepend("<A> ");
2154 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00002155 if (fRotate) {
2156 title.prepend("<R> ");
2157 }
reed@android.come522ca52009-11-23 20:10:41 +00002158 if (fNClip) {
2159 title.prepend("<C> ");
2160 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00002161 if (fPerspAnim) {
2162 title.prepend("<K> ");
2163 }
reed@google.com569e0432011-04-05 13:07:03 +00002164
2165 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
2166 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com15bc13d2013-12-10 16:53:13 +00002167 title.prepend(gFilterLevelStates[fFilterLevelIndex].fLabel);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002168 title.prepend(trystate_str(fSubpixelState, "S ", "s "));
reed@google.com569e0432011-04-05 13:07:03 +00002169 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
2170 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002171 title.prepend(gHintingStates[fHintingState].label);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002172
2173 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00002174 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002175 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002176
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002177 if (fMeasureFPS) {
reed@google.com43e10142012-09-10 11:52:52 +00002178 title.appendf(" %8.3f ms", fMeasureFPS_Time / (float)FPS_REPEAT_COUNT);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002179 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002180 if (SampleView::IsSampleView(view)) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002181 switch (fPipeState) {
2182 case SkOSMenu::kOnState:
2183 title.prepend("<Pipe> ");
2184 break;
2185 case SkOSMenu::kMixedState:
2186 title.prepend("<Tiled Pipe> ");
2187 break;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002188
scroggo@google.comb073d922012-06-08 15:35:03 +00002189 default:
2190 break;
2191 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002192 title.prepend("! ");
2193 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002194
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002195#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00002196 if (IsGpuDeviceType(fDeviceType) &&
robertphillips@google.com4750fa52012-04-10 13:34:11 +00002197 NULL != fDevManager &&
bsalomon@google.com82502e22013-01-24 20:47:18 +00002198 fDevManager->getGrRenderTarget() &&
bsalomon@google.com11959252012-04-06 20:13:38 +00002199 fDevManager->getGrRenderTarget()->numSamples() > 0) {
2200 title.appendf(" [MSAA: %d]",
2201 fDevManager->getGrRenderTarget()->numSamples());
2202 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002203#endif
bsalomon@google.com11959252012-04-06 20:13:38 +00002204
reed@android.com8a1c16f2008-12-17 15:59:43 +00002205 this->setTitle(title.c_str());
2206}
2207
2208void SampleWindow::onSizeChange() {
2209 this->INHERITED::onSizeChange();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002210
reed@android.com8a1c16f2008-12-17 15:59:43 +00002211 SkView::F2BIter iter(this);
2212 SkView* view = iter.next();
2213 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002214
reed@android.com8a1c16f2008-12-17 15:59:43 +00002215 // rebuild our clippath
2216 {
2217 const SkScalar W = this->width();
2218 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002219
reed@android.com8a1c16f2008-12-17 15:59:43 +00002220 fClipPath.reset();
2221#if 0
2222 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
2223 SkRect r;
2224 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
2225 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
2226 fClipPath.addRect(r);
2227 }
2228#else
2229 SkRect r;
2230 r.set(0, 0, W, H);
2231 fClipPath.addRect(r, SkPath::kCCW_Direction);
2232 r.set(W/4, H/4, W*3/4, H*3/4);
2233 fClipPath.addRect(r, SkPath::kCW_Direction);
2234#endif
2235 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002236
Scroggo2c8208f2011-06-15 16:49:08 +00002237 fZoomCenterX = SkScalarHalf(this->width());
2238 fZoomCenterY = SkScalarHalf(this->height());
2239
djsollen@google.com56c69772011-11-08 19:00:26 +00002240#ifdef SK_BUILD_FOR_ANDROID
Scroggob4490c72011-06-17 13:53:05 +00002241 // FIXME: The first draw after a size change does not work on Android, so
2242 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00002243 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00002244#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00002245 this->updateTitle(); // to refresh our config
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002246 fDevManager->windowSizeChanged(this);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00002247
2248 if (fTilingMode != kNo_Tiling && SampleView::IsSampleView(view)) {
2249 ((SampleView*)view)->onTileSizeChanged(this->tileSize());
2250 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002251}
2252
2253///////////////////////////////////////////////////////////////////////////////
2254
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002255static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002256static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00002257static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002258
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002259bool SampleView::IsSampleView(SkView* view) {
2260 SkEvent evt(is_sample_view_tag);
2261 return view->doQuery(&evt);
2262}
2263
reed@google.comf2183392011-04-22 14:10:48 +00002264bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002265 SkEvent evt(repeat_count_tag);
2266 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00002267 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002268}
2269
scroggo@google.comb073d922012-06-08 15:35:03 +00002270bool SampleView::SetUsePipe(SkView* view, SkOSMenu::TriState state) {
2271 SkEvent evt;
2272 evt.setS32(set_use_pipe_tag, state);
reed@google.com0faac1e2011-05-11 05:58:58 +00002273 return view->doEvent(evt);
2274}
2275
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002276bool SampleView::onEvent(const SkEvent& evt) {
2277 if (evt.isType(repeat_count_tag)) {
2278 fRepeatCount = evt.getFast32();
2279 return true;
2280 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002281
scroggo@google.comb073d922012-06-08 15:35:03 +00002282 int32_t pipeHolder;
2283 if (evt.findS32(set_use_pipe_tag, &pipeHolder)) {
2284 fPipeState = static_cast<SkOSMenu::TriState>(pipeHolder);
reed@google.com0faac1e2011-05-11 05:58:58 +00002285 return true;
2286 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002287
2288 if (evt.isType("debug-hit-test")) {
2289 fDebugHitTest = true;
2290 evt.findS32("debug-hit-test-x", &fDebugHitTestLoc.fX);
2291 evt.findS32("debug-hit-test-y", &fDebugHitTestLoc.fY);
2292 this->inval(NULL);
2293 return true;
2294 }
2295
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002296 return this->INHERITED::onEvent(evt);
2297}
2298
2299bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002300 if (evt->isType(is_sample_view_tag)) {
2301 return true;
2302 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002303 return this->INHERITED::onQuery(evt);
2304}
2305
reed@google.com64e3eb22011-05-04 14:32:04 +00002306
2307class SimplePC : public SkGPipeController {
2308public:
2309 SimplePC(SkCanvas* target);
2310 ~SimplePC();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002311
reed@google.com64e3eb22011-05-04 14:32:04 +00002312 virtual void* requestBlock(size_t minRequest, size_t* actual);
2313 virtual void notifyWritten(size_t bytes);
2314
2315private:
reed@google.com961ddb02011-05-05 14:03:48 +00002316 SkGPipeReader fReader;
2317 void* fBlock;
2318 size_t fBlockSize;
2319 size_t fBytesWritten;
2320 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00002321 SkGPipeReader::Status fStatus;
2322
2323 size_t fTotalWritten;
2324};
2325
2326SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
2327 fBlock = NULL;
2328 fBlockSize = fBytesWritten = 0;
2329 fStatus = SkGPipeReader::kDone_Status;
2330 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00002331 fAtomsWritten = 0;
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00002332 fReader.setBitmapDecoder(&SkImageDecoder::DecodeMemory);
reed@google.com64e3eb22011-05-04 14:32:04 +00002333}
2334
2335SimplePC::~SimplePC() {
2336// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
reed@google.com0faac1e2011-05-11 05:58:58 +00002337 if (fTotalWritten) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002338 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
2339 fAtomsWritten, fStatus);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002340#ifdef PIPE_FILE
scroggo@google.comb073d922012-06-08 15:35:03 +00002341 //File is open in append mode
2342 FILE* f = fopen(FILE_PATH, "ab");
2343 SkASSERT(f != NULL);
2344 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
2345 fclose(f);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002346#endif
2347#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00002348 if (fAtomsWritten > 1 && fTotalWritten > 4) { //ignore done
2349 gServer.acceptConnections();
2350 gServer.writePacket(fBlock, fTotalWritten);
2351 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002352#endif
reed@google.com0faac1e2011-05-11 05:58:58 +00002353 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002354 sk_free(fBlock);
reed@google.com64e3eb22011-05-04 14:32:04 +00002355}
2356
2357void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
2358 sk_free(fBlock);
2359
2360 fBlockSize = minRequest * 4;
2361 fBlock = sk_malloc_throw(fBlockSize);
2362 fBytesWritten = 0;
2363 *actual = fBlockSize;
2364 return fBlock;
2365}
2366
2367void SimplePC::notifyWritten(size_t bytes) {
2368 SkASSERT(fBytesWritten + bytes <= fBlockSize);
reed@google.com64e3eb22011-05-04 14:32:04 +00002369 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
2370 SkASSERT(SkGPipeReader::kError_Status != fStatus);
2371 fBytesWritten += bytes;
2372 fTotalWritten += bytes;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002373
reed@google.com961ddb02011-05-05 14:03:48 +00002374 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00002375}
2376
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002377void SampleView::draw(SkCanvas* canvas) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002378 if (SkOSMenu::kOffState == fPipeState) {
2379 this->INHERITED::draw(canvas);
2380 } else {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002381 SkGPipeWriter writer;
2382 SimplePC controller(canvas);
scroggo@google.comb073d922012-06-08 15:35:03 +00002383 TiledPipeController tc(canvas->getDevice()->accessBitmap(false),
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00002384 &SkImageDecoder::DecodeMemory,
scroggo@google.comb073d922012-06-08 15:35:03 +00002385 &canvas->getTotalMatrix());
2386 SkGPipeController* pc;
2387 if (SkOSMenu::kMixedState == fPipeState) {
2388 pc = &tc;
2389 } else {
2390 pc = &controller;
2391 }
reed@google.comdde09562011-05-23 12:21:05 +00002392 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002393
scroggo@google.comb073d922012-06-08 15:35:03 +00002394 canvas = writer.startRecording(pc, flags);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002395 //Must draw before controller goes out of scope and sends data
2396 this->INHERITED::draw(canvas);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00002397 //explicitly end recording to ensure writer is flushed before the memory
2398 //is freed in the deconstructor of the controller
2399 writer.endRecording();
reed@google.com0faac1e2011-05-11 05:58:58 +00002400 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002401}
reed@google.com4d5c26d2013-01-08 16:17:50 +00002402
2403#include "SkBounder.h"
2404
2405class DebugHitTestBounder : public SkBounder {
2406public:
2407 DebugHitTestBounder(int x, int y) {
2408 fLoc.set(x, y);
2409 }
2410
2411 virtual bool onIRect(const SkIRect& bounds) SK_OVERRIDE {
2412 if (bounds.contains(fLoc.x(), fLoc.y())) {
2413 //
2414 // Set a break-point here to see what was being drawn under
2415 // the click point (just needed a line of code to stop the debugger)
2416 //
2417 bounds.centerX();
2418 }
2419 return true;
2420 }
2421
2422private:
2423 SkIPoint fLoc;
2424 typedef SkBounder INHERITED;
2425};
2426
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002427void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com81e3d7f2011-06-01 12:42:36 +00002428 this->onDrawBackground(canvas);
2429
reed@google.com4d5c26d2013-01-08 16:17:50 +00002430 DebugHitTestBounder bounder(fDebugHitTestLoc.x(), fDebugHitTestLoc.y());
2431 if (fDebugHitTest) {
2432 canvas->setBounder(&bounder);
2433 }
2434
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002435 for (int i = 0; i < fRepeatCount; i++) {
scroggo@google.com85cade02012-08-17 13:29:50 +00002436 SkAutoCanvasRestore acr(canvas, true);
2437 this->onDrawContent(canvas);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002438 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002439
2440 fDebugHitTest = false;
2441 canvas->setBounder(NULL);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002442}
2443
2444void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00002445 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002446}
2447
2448///////////////////////////////////////////////////////////////////////////////
2449
reed@android.comf2b98d62010-12-20 18:26:13 +00002450template <typename T> void SkTBSort(T array[], int count) {
2451 for (int i = 1; i < count - 1; i++) {
2452 bool didSwap = false;
2453 for (int j = count - 1; j > i; --j) {
2454 if (array[j] < array[j-1]) {
2455 T tmp(array[j-1]);
2456 array[j-1] = array[j];
2457 array[j] = tmp;
2458 didSwap = true;
2459 }
2460 }
2461 if (!didSwap) {
2462 break;
2463 }
2464 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002465
reed@android.comf2b98d62010-12-20 18:26:13 +00002466 for (int k = 0; k < count - 1; k++) {
2467 SkASSERT(!(array[k+1] < array[k]));
2468 }
2469}
2470
2471#include "SkRandom.h"
2472
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002473static void rand_rect(SkIRect* rect, SkRandom& rand) {
reed@android.comf2b98d62010-12-20 18:26:13 +00002474 int bits = 8;
2475 int shift = 32 - bits;
2476 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
2477 rand.nextU() >> shift, rand.nextU() >> shift);
2478 rect->sort();
2479}
2480
2481static void dumpRect(const SkIRect& r) {
2482 SkDebugf(" { %d, %d, %d, %d },\n",
2483 r.fLeft, r.fTop,
2484 r.fRight, r.fBottom);
2485}
2486
2487static void test_rects(const SkIRect rect[], int count) {
2488 SkRegion rgn0, rgn1;
2489
2490 for (int i = 0; i < count; i++) {
2491 rgn0.op(rect[i], SkRegion::kUnion_Op);
2492 // dumpRect(rect[i]);
2493 }
2494 rgn1.setRects(rect, count);
2495
2496 if (rgn0 != rgn1) {
2497 SkDebugf("\n");
2498 for (int i = 0; i < count; i++) {
2499 dumpRect(rect[i]);
2500 }
2501 SkDebugf("\n");
2502 }
2503}
2504
2505static void test() {
2506 size_t i;
2507
2508 const SkIRect r0[] = {
2509 { 0, 0, 1, 1 },
2510 { 2, 2, 3, 3 },
2511 };
2512 const SkIRect r1[] = {
2513 { 0, 0, 1, 3 },
2514 { 1, 1, 2, 2 },
2515 { 2, 0, 3, 3 },
2516 };
2517 const SkIRect r2[] = {
2518 { 0, 0, 1, 2 },
2519 { 2, 1, 3, 3 },
2520 { 4, 0, 5, 1 },
2521 { 6, 0, 7, 4 },
2522 };
2523
2524 static const struct {
2525 const SkIRect* fRects;
2526 int fCount;
2527 } gRecs[] = {
2528 { r0, SK_ARRAY_COUNT(r0) },
2529 { r1, SK_ARRAY_COUNT(r1) },
2530 { r2, SK_ARRAY_COUNT(r2) },
2531 };
2532
2533 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
2534 test_rects(gRecs[i].fRects, gRecs[i].fCount);
2535 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002536
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002537 SkRandom rand;
reed@android.comf2b98d62010-12-20 18:26:13 +00002538 for (i = 0; i < 10000; i++) {
2539 SkRegion rgn0, rgn1;
2540
2541 const int N = 8;
2542 SkIRect rect[N];
2543 for (int j = 0; j < N; j++) {
2544 rand_rect(&rect[j], rand);
2545 }
2546 test_rects(rect, N);
2547 }
2548}
2549
caryclark@google.com02939ce2012-06-06 12:09:51 +00002550// FIXME: this should be in a header
2551SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00002552SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
caryclark@google.com02939ce2012-06-06 12:09:51 +00002553 if (false) { // avoid bit rot, suppress warning
2554 test();
2555 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002556 return new SampleWindow(hwnd, argc, argv, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002557}
2558
caryclark@google.com02939ce2012-06-06 12:09:51 +00002559// FIXME: this should be in a header
2560void get_preferred_size(int* x, int* y, int* width, int* height);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002561void get_preferred_size(int* x, int* y, int* width, int* height) {
2562 *x = 10;
2563 *y = 50;
2564 *width = 640;
2565 *height = 480;
2566}
2567
caryclark@google.com5987f582012-10-02 18:33:14 +00002568#ifdef SK_BUILD_FOR_IOS
2569void save_args(int argc, char *argv[]) {
2570}
2571#endif
2572
caryclark@google.com02939ce2012-06-06 12:09:51 +00002573// FIXME: this should be in a header
2574void application_init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002575void application_init() {
2576// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002577#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00002578 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002579#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002580 SkGraphics::Init();
2581 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002582}
2583
caryclark@google.com02939ce2012-06-06 12:09:51 +00002584// FIXME: this should be in a header
2585void application_term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002586void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002587 SkEvent::Term();
2588 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002589}