blob: 53e15a5fdfd2ce40518767185fba9b00e13ce05d [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.com29038ed2011-07-06 17:56:47 +0000445#if 0
reed@google.comf0b5f682011-03-11 20:08:25 +0000446#include <CoreFoundation/CoreFoundation.h>
447#include <CoreFoundation/CFURLAccess.h>
448
449static void testpdf() {
450 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
451 kCFStringEncodingUTF8);
452 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
453 kCFURLPOSIXPathStyle,
454 false);
455 CFRelease(path);
456 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
457 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
458 CFRelease(url);
459
460 CGContextBeginPage(cg, &box);
461 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
462 CGContextFillEllipseInRect(cg, r);
463 CGContextEndPage(cg);
464 CGContextRelease(cg);
465
466 if (false) {
467 SkBitmap bm;
468 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
469 bm.allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +0000470 bm.eraseColor(SK_ColorTRANSPARENT);
reed@google.comf0b5f682011-03-11 20:08:25 +0000471
472 SkCanvas canvas(bm);
473
474 }
475}
476#endif
477
478//////////////////////////////////////////////////////////////////////////////
479
reed@google.com569e0432011-04-05 13:07:03 +0000480enum FlipAxisEnum {
481 kFlipAxis_X = (1 << 0),
482 kFlipAxis_Y = (1 << 1)
483};
484
reed@google.comf0b5f682011-03-11 20:08:25 +0000485#include "SkDrawFilter.h"
486
bungeman@google.com96aabc82013-06-03 21:26:34 +0000487struct HintingState {
488 SkPaint::Hinting hinting;
489 const char* name;
490 const char* label;
491};
492static HintingState gHintingStates[] = {
493 {SkPaint::kNo_Hinting, "Mixed", NULL },
494 {SkPaint::kNo_Hinting, "None", "H0 " },
495 {SkPaint::kSlight_Hinting, "Slight", "Hs " },
496 {SkPaint::kNormal_Hinting, "Normal", "Hn " },
497 {SkPaint::kFull_Hinting, "Full", "Hf " },
498};
499
reed@google.com15bc13d2013-12-10 16:53:13 +0000500struct FilterLevelState {
501 SkPaint::FilterLevel fLevel;
502 const char* fName;
503 const char* fLabel;
504};
505static FilterLevelState gFilterLevelStates[] = {
506 { SkPaint::kNone_FilterLevel, "Mixed", NULL },
507 { SkPaint::kNone_FilterLevel, "None", "F0 " },
508 { SkPaint::kLow_FilterLevel, "Low", "F1 " },
509 { SkPaint::kMedium_FilterLevel, "Medium", "F2 " },
510 { SkPaint::kHigh_FilterLevel, "High", "F3 " },
511};
512
reed@google.com569e0432011-04-05 13:07:03 +0000513class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000514public:
reed@google.com15bc13d2013-12-10 16:53:13 +0000515 FlagsDrawFilter(SkOSMenu::TriState lcd, SkOSMenu::TriState aa,
516 SkOSMenu::TriState subpixel, int hinting, int filterlevel)
517 : fLCDState(lcd)
518 , fAAState(aa)
519 , fSubpixelState(subpixel)
520 , fHintingState(hinting)
521 , fFilterLevelIndex(filterlevel)
522 {
523 SkASSERT((unsigned)filterlevel < SK_ARRAY_COUNT(gFilterLevelStates));
524 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000525
reed@google.com971aca72012-11-26 20:26:54 +0000526 virtual bool filter(SkPaint* paint, Type t) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000527 if (kText_Type == t && SkOSMenu::kMixedState != fLCDState) {
528 paint->setLCDRenderText(SkOSMenu::kOnState == fLCDState);
reed@google.com569e0432011-04-05 13:07:03 +0000529 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000530 if (SkOSMenu::kMixedState != fAAState) {
531 paint->setAntiAlias(SkOSMenu::kOnState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000532 }
reed@google.com15bc13d2013-12-10 16:53:13 +0000533 if (0 != fFilterLevelIndex) {
534 paint->setFilterLevel(gFilterLevelStates[fFilterLevelIndex].fLevel);
reed@google.com176753a2011-05-17 15:32:04 +0000535 }
bungeman@google.com96aabc82013-06-03 21:26:34 +0000536 if (SkOSMenu::kMixedState != fSubpixelState) {
537 paint->setSubpixelText(SkOSMenu::kOnState == fSubpixelState);
538 }
bungeman@google.com055aa522013-06-03 21:35:03 +0000539 if (0 != fHintingState && fHintingState < (int)SK_ARRAY_COUNT(gHintingStates)) {
bungeman@google.com96aabc82013-06-03 21:26:34 +0000540 paint->setHinting(gHintingStates[fHintingState].hinting);
reed@google.com09e3baa2011-05-18 12:04:31 +0000541 }
reed@google.com971aca72012-11-26 20:26:54 +0000542 return true;
reed@google.comf0b5f682011-03-11 20:08:25 +0000543 }
544
545private:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000546 SkOSMenu::TriState fLCDState;
547 SkOSMenu::TriState fAAState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000548 SkOSMenu::TriState fSubpixelState;
549 int fHintingState;
reed@google.com15bc13d2013-12-10 16:53:13 +0000550 int fFilterLevelIndex;
reed@google.comf0b5f682011-03-11 20:08:25 +0000551};
552
reed@android.com8a1c16f2008-12-17 15:59:43 +0000553//////////////////////////////////////////////////////////////////////////////
554
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000555#define MAX_ZOOM_LEVEL 8
556#define MIN_ZOOM_LEVEL -8
557
reed@android.comf2b98d62010-12-20 18:26:13 +0000558static const char gCharEvtName[] = "SampleCode_Char_Event";
559static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000560static const char gTitleEvtName[] = "SampleCode_Title_Event";
561static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000562static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
reed@google.com2072db82011-11-08 15:18:10 +0000563static const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle";
reed@android.comf2b98d62010-12-20 18:26:13 +0000564
565bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
566 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
567 if (outUni) {
568 *outUni = evt.getFast32();
569 }
570 return true;
571 }
572 return false;
573}
574
575bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
576 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
577 if (outKey) {
578 *outKey = (SkKey)evt.getFast32();
579 }
580 return true;
581 }
582 return false;
583}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000584
585bool SampleCode::TitleQ(const SkEvent& evt) {
586 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
587}
588
589void SampleCode::TitleR(SkEvent* evt, const char title[]) {
590 SkASSERT(evt && TitleQ(*evt));
591 evt->setString(gTitleEvtName, title);
592}
593
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000594bool SampleCode::RequestTitle(SkView* view, SkString* title) {
595 SkEvent evt(gTitleEvtName);
596 if (view->doQuery(&evt)) {
597 title->set(evt.findString(gTitleEvtName));
598 return true;
599 }
600 return false;
601}
602
reed@android.com8a1c16f2008-12-17 15:59:43 +0000603bool SampleCode::PrefSizeQ(const SkEvent& evt) {
604 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
605}
606
607void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
608 SkASSERT(evt && PrefSizeQ(*evt));
609 SkScalar size[2];
610 size[0] = width;
611 size[1] = height;
612 evt->setScalars(gPrefSizeEvtName, 2, size);
613}
614
reed@android.comf2b98d62010-12-20 18:26:13 +0000615bool SampleCode::FastTextQ(const SkEvent& evt) {
616 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
617}
618
619///////////////////////////////////////////////////////////////////////////////
620
reed@android.com44177402009-11-23 21:07:51 +0000621static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000622static SkMSec gAnimTimePrev;
623
reed@android.com44177402009-11-23 21:07:51 +0000624SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000625SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
626SkScalar SampleCode::GetAnimSecondsDelta() {
627 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
628}
reed@android.com44177402009-11-23 21:07:51 +0000629
630SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000631 // since gAnimTime can be up to 32 bits, we can't convert it to a float
632 // or we'll lose the low bits. Hence we use doubles for the intermediate
633 // calculations
634 double seconds = (double)gAnimTime / 1000.0;
635 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000636 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000637 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000638 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000639 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000640}
641
bsalomon@google.com85003222012-03-28 14:44:37 +0000642SkScalar SampleCode::GetAnimSinScalar(SkScalar amplitude,
643 SkScalar periodInSec,
644 SkScalar phaseInSec) {
645 if (!periodInSec) {
646 return 0;
647 }
648 double t = (double)gAnimTime / 1000.0 + phaseInSec;
649 t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
650 amplitude = SK_ScalarHalf * amplitude;
651 return SkScalarMul(amplitude, SkDoubleToScalar(sin(t))) + amplitude;
652}
653
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000654enum TilingMode {
655 kNo_Tiling,
656 kAbs_128x128_Tiling,
657 kAbs_256x256_Tiling,
658 kRel_4x4_Tiling,
659 kRel_1x16_Tiling,
660 kRel_16x1_Tiling,
661
662 kLast_TilingMode_Enum
663};
664
665struct TilingInfo {
666 const char* label;
667 SkScalar w, h;
668};
669
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000670static const struct TilingInfo gTilingInfo[] = {
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000671 { "No tiling", SK_Scalar1 , SK_Scalar1 }, // kNo_Tiling
672 { "128x128" , SkIntToScalar(128), SkIntToScalar(128) }, // kAbs_128x128_Tiling
673 { "256x256" , SkIntToScalar(256), SkIntToScalar(256) }, // kAbs_256x256_Tiling
674 { "1/4x1/4" , SK_Scalar1 / 4 , SK_Scalar1 / 4 }, // kRel_4x4_Tiling
675 { "1/1x1/16" , SK_Scalar1 , SK_Scalar1 / 16 }, // kRel_1x16_Tiling
676 { "1/16x1/1" , SK_Scalar1 / 16 , SK_Scalar1 }, // kRel_16x1_Tiling
677};
678SK_COMPILE_ASSERT((SK_ARRAY_COUNT(gTilingInfo) == kLast_TilingMode_Enum),
679 Incomplete_tiling_labels);
680
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000681SkSize SampleWindow::tileSize() const {
682 SkASSERT((TilingMode)fTilingMode < kLast_TilingMode_Enum);
683 const struct TilingInfo* info = gTilingInfo + fTilingMode;
684 return SkSize::Make(info->w > SK_Scalar1 ? info->w : this->width() * info->w,
685 info->h > SK_Scalar1 ? info->h : this->height() * info->h);
686}
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000687//////////////////////////////////////////////////////////////////////////////
688
reed@android.comf2b98d62010-12-20 18:26:13 +0000689static SkView* curr_view(SkWindow* wind) {
690 SkView::F2BIter iter(wind);
691 return iter.next();
692}
693
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000694static bool curr_title(SkWindow* wind, SkString* title) {
695 SkView* view = curr_view(wind);
696 if (view) {
697 SkEvent evt(gTitleEvtName);
698 if (view->doQuery(&evt)) {
699 title->set(evt.findString(gTitleEvtName));
700 return true;
701 }
702 }
703 return false;
704}
705
Scroggo2c8208f2011-06-15 16:49:08 +0000706void SampleWindow::setZoomCenter(float x, float y)
707{
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000708 fZoomCenterX = x;
709 fZoomCenterY = y;
Scroggo2c8208f2011-06-15 16:49:08 +0000710}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000711
Scroggo0f185c22011-03-24 18:35:50 +0000712bool SampleWindow::zoomIn()
713{
714 // Arbitrarily decided
715 if (fFatBitsScale == 25) return false;
716 fFatBitsScale++;
717 this->inval(NULL);
718 return true;
719}
720
721bool SampleWindow::zoomOut()
722{
723 if (fFatBitsScale == 1) return false;
724 fFatBitsScale--;
725 this->inval(NULL);
726 return true;
727}
728
Scroggo0f185c22011-03-24 18:35:50 +0000729void SampleWindow::updatePointer(int x, int y)
730{
731 fMouseX = x;
732 fMouseY = y;
733 if (fShowZoomer) {
734 this->inval(NULL);
735 }
736}
737
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000738static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
739 static const SampleWindow::DeviceType gCT[] = {
740 SampleWindow::kPicture_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000741#if SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000742 SampleWindow::kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000743#if SK_ANGLE
744 SampleWindow::kANGLE_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000745#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +0000746 SampleWindow::kRaster_DeviceType, // skip the null gpu device in normal cycling
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000747#endif // SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000748 SampleWindow::kRaster_DeviceType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000749 };
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000750 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gCT) == SampleWindow::kDeviceTypeCnt, array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000751 return gCT[ct];
752}
753
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000754static void usage(const char * argv0) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000755 SkDebugf("%s [--slide sampleName] [-i resourcePath] [--msaa sampleCount] [--pictureDir dirPath] [--picture path] [--sort]\n", argv0);
756#ifdef SAMPLE_PDF_FILE_VIEWER
757 SkDebugf(" [--pdfDir pdfPath]\n");
758 SkDebugf(" pdfPath: path to directory pdf files are read from\n");
759#endif // SAMPLE_PDF_FILE_VIEWER
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000760 SkDebugf(" sampleName: sample at which to start.\n");
761 SkDebugf(" resourcePath: directory that stores image resources.\n");
bsalomon@google.com11959252012-04-06 20:13:38 +0000762 SkDebugf(" msaa: request multisampling with the given sample count.\n");
chudy@google.com4605a3f2012-08-01 17:58:01 +0000763 SkDebugf(" dirPath: path to directory skia pictures are read from\n");
764 SkDebugf(" path: path to skia picture\n");
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000765 SkDebugf(" --sort: sort samples by title, this would help to compare pdf rendering (P:foo.pdf) with skp rendering (P:foo.pdf)\n");
766}
767
768static SkString getSampleTitle(const SkViewFactory* sampleFactory) {
769 SkView* view = (*sampleFactory)();
770 SkString title;
771 SampleCode::RequestTitle(view, &title);
772 view->unref();
773 return title;
774}
775
edisonn@google.comde36b692013-07-11 15:40:31 +0000776static bool compareSampleTitle(const SkViewFactory* first, const SkViewFactory* second) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000777 return strcmp(getSampleTitle(first).c_str(), getSampleTitle(second).c_str()) < 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000778}
779
chudy@google.com4605a3f2012-08-01 17:58:01 +0000780SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager)
robertphillips@google.com4750fa52012-04-10 13:34:11 +0000781 : INHERITED(hwnd)
782 , fDevManager(NULL) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000783
reed@google.com7d05ac92013-06-06 13:14:13 +0000784 fCurrIndex = -1;
785
reed@google.com1830c7a2012-06-04 12:05:43 +0000786 this->registerPictFileSamples(argv, argc);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000787 this->registerPictFileSample(argv, argc);
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000788#ifdef SAMPLE_PDF_FILE_VIEWER
789 this->registerPdfFileViewerSamples(argv, argc);
790#endif // SAMPLE_PDF_FILE_VIEWER
robertphillips@google.com76d40212012-03-22 14:12:15 +0000791 SkGMRegistyToSampleRegistry();
792 {
793 const SkViewRegister* reg = SkViewRegister::Head();
794 while (reg) {
795 *fSamples.append() = reg->factory();
796 reg = reg->next();
797 }
798 }
799
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000800 bool sort = false;
801 for (int i = 0; i < argc; ++i) {
802 if (!strcmp(argv[i], "--sort")) {
803 sort = true;
804 break;
805 }
806 }
807
808 if (sort) {
809 // Sort samples, so foo.skp and foo.pdf are consecutive and we can quickly spot where
810 // skp -> pdf -> png fails.
811 SkTQSort(fSamples.begin(), fSamples.end() ? fSamples.end() - 1 : NULL, compareSampleTitle);
812 }
813
halcanary@google.com9acb8cd2014-01-10 14:53:49 +0000814 const char* resourcePath = "resources"; // same default as tests
bsalomon@google.com11959252012-04-06 20:13:38 +0000815 fMSAASampleCount = 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000816
817 const char* const commandName = argv[0];
818 char* const* stop = argv + argc;
819 for (++argv; argv < stop; ++argv) {
reed@google.com55ebe8e2013-09-10 19:12:07 +0000820 if (!strcmp(*argv, "-i") || !strcmp(*argv, "--resourcePath")) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000821 argv++;
822 if (argv < stop && **argv) {
823 resourcePath = *argv;
824 }
robertphillips@google.com76d40212012-03-22 14:12:15 +0000825 } else if (strcmp(*argv, "--slide") == 0) {
826 argv++;
827 if (argv < stop && **argv) {
828 fCurrIndex = findByTitle(*argv);
829 if (fCurrIndex < 0) {
830 fprintf(stderr, "Unknown sample \"%s\"\n", *argv);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000831 listTitles();
robertphillips@google.com76d40212012-03-22 14:12:15 +0000832 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000833 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000834 } else if (strcmp(*argv, "--msaa") == 0) {
835 ++argv;
836 if (argv < stop && **argv) {
837 fMSAASampleCount = atoi(*argv);
838 }
robertphillips@google.com7265e722012-05-03 18:22:28 +0000839 } else if (strcmp(*argv, "--list") == 0) {
840 listTitles();
halcanary@google.com805ca192013-10-14 12:47:37 +0000841 } else if (strcmp(*argv, "--pictureDir") == 0) {
842 ++argv; // This case is dealt with in registerPictFileSamples().
843 } else if (strcmp(*argv, "--picture") == 0) {
844 ++argv; // This case is dealt with in registerPictFileSample().
bsalomon@google.com11959252012-04-06 20:13:38 +0000845 }
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000846 else {
robertphillips@google.com76d40212012-03-22 14:12:15 +0000847 usage(commandName);
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000848 }
849 }
850
851 if (fCurrIndex < 0) {
852 SkString title;
853 if (readTitleFromPrefs(&title)) {
854 fCurrIndex = findByTitle(title.c_str());
855 }
856 }
857
858 if (fCurrIndex < 0) {
859 fCurrIndex = 0;
860 }
861
reed@google.com3cec4d72011-07-06 13:59:47 +0000862 gSampleWindow = this;
863
yangsu@google.com1f394212011-06-01 18:03:34 +0000864#ifdef PIPE_FILE
865 //Clear existing file or create file if it doesn't exist
866 FILE* f = fopen(FILE_PATH, "wb");
867 fclose(f);
868#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +0000869
reed@android.com8a1c16f2008-12-17 15:59:43 +0000870 fPicture = NULL;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000871
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000872 fDeviceType = kRaster_DeviceType;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000873
874#if DEFAULT_TO_GPU
875 fDeviceType = kGPU_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000876#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000877#if SK_ANGLE && DEFAULT_TO_ANGLE
878 fDeviceType = kANGLE_DeviceType;
879#endif
880
reed@android.com8a1c16f2008-12-17 15:59:43 +0000881 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000882 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000883 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000884 fRotate = false;
bungeman@google.comb1785912013-07-09 21:58:56 +0000885 fRotateAnimTime = 0;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000886 fPerspAnim = false;
887 fPerspAnimTime = 0;
reed@android.comf2b98d62010-12-20 18:26:13 +0000888 fRequestGrabImage = false;
scroggo@google.comb073d922012-06-08 15:35:03 +0000889 fPipeState = SkOSMenu::kOffState;
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000890 fTilingMode = kNo_Tiling;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000891 fMeasureFPS = false;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000892 fLCDState = SkOSMenu::kMixedState;
893 fAAState = SkOSMenu::kMixedState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000894 fSubpixelState = SkOSMenu::kMixedState;
895 fHintingState = 0;
reed@google.com15bc13d2013-12-10 16:53:13 +0000896 fFilterLevelIndex = 0;
reed@google.com569e0432011-04-05 13:07:03 +0000897 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000898 fScrollTestX = fScrollTestY = 0;
899
Scroggo0f185c22011-03-24 18:35:50 +0000900 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000901 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000902 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
903 fShowZoomer = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000904
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000905 fZoomLevel = 0;
906 fZoomScale = SK_Scalar1;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000907
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000908 fMagnify = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000909
Scroggo8ac0d542011-06-21 14:44:57 +0000910 fSaveToPdf = false;
911 fPdfCanvas = NULL;
912
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000913 fTransitionNext = 6;
914 fTransitionPrev = 2;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000915
yangsu@google.com921091f2011-08-02 13:39:12 +0000916 int sinkID = this->getSinkID();
scroggo@google.com7dadc742012-04-18 14:07:57 +0000917 fAppMenu = new SkOSMenu;
918 fAppMenu->setTitle("Global Settings");
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000919 int itemID;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000920
921 itemID =fAppMenu->appendList("Device Type", "Device Type", sinkID, 0,
922 "Raster", "Picture", "OpenGL",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000923#if SK_ANGLE
924 "ANGLE",
925#endif
926 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000927 fAppMenu->assignKeyEquivalentToItem(itemID, 'd');
928 itemID = fAppMenu->appendTriState("AA", "AA", sinkID, fAAState);
929 fAppMenu->assignKeyEquivalentToItem(itemID, 'b');
930 itemID = fAppMenu->appendTriState("LCD", "LCD", sinkID, fLCDState);
931 fAppMenu->assignKeyEquivalentToItem(itemID, 'l');
reed@google.com15bc13d2013-12-10 16:53:13 +0000932 itemID = fAppMenu->appendList("FilterLevel", "FilterLevel", sinkID, fFilterLevelIndex,
933 gFilterLevelStates[0].fName,
934 gFilterLevelStates[1].fName,
935 gFilterLevelStates[2].fName,
936 gFilterLevelStates[3].fName,
937 gFilterLevelStates[4].fName,
938 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000939 fAppMenu->assignKeyEquivalentToItem(itemID, 'n');
bungeman@google.com96aabc82013-06-03 21:26:34 +0000940 itemID = fAppMenu->appendTriState("Subpixel", "Subpixel", sinkID, fSubpixelState);
941 fAppMenu->assignKeyEquivalentToItem(itemID, 's');
942 itemID = fAppMenu->appendList("Hinting", "Hinting", sinkID, fHintingState,
943 gHintingStates[0].name,
944 gHintingStates[1].name,
945 gHintingStates[2].name,
946 gHintingStates[3].name,
bungeman@google.coma9133bb2013-06-04 17:49:20 +0000947 gHintingStates[4].name,
948 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000949 fAppMenu->assignKeyEquivalentToItem(itemID, 'h');
reed@google.com67b89ee2012-08-15 14:41:58 +0000950
scroggo@google.comb073d922012-06-08 15:35:03 +0000951 fUsePipeMenuItemID = fAppMenu->appendTriState("Pipe", "Pipe" , sinkID,
chudy@google.com4605a3f2012-08-01 17:58:01 +0000952 fPipeState);
scroggo@google.comb073d922012-06-08 15:35:03 +0000953 fAppMenu->assignKeyEquivalentToItem(fUsePipeMenuItemID, 'P');
reed@google.com67b89ee2012-08-15 14:41:58 +0000954
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000955 itemID =fAppMenu->appendList("Tiling", "Tiling", sinkID, fTilingMode,
956 gTilingInfo[kNo_Tiling].label,
957 gTilingInfo[kAbs_128x128_Tiling].label,
958 gTilingInfo[kAbs_256x256_Tiling].label,
959 gTilingInfo[kRel_4x4_Tiling].label,
960 gTilingInfo[kRel_1x16_Tiling].label,
961 gTilingInfo[kRel_16x1_Tiling].label,
962 NULL);
reed@google.com67b89ee2012-08-15 14:41:58 +0000963 fAppMenu->assignKeyEquivalentToItem(itemID, 't');
964
chudy@google.com4605a3f2012-08-01 17:58:01 +0000965 itemID = fAppMenu->appendSwitch("Slide Show", "Slide Show" , sinkID, false);
966 fAppMenu->assignKeyEquivalentToItem(itemID, 'a');
967 itemID = fAppMenu->appendSwitch("Clip", "Clip" , sinkID, fUseClip);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000968 fAppMenu->assignKeyEquivalentToItem(itemID, 'c');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000969 itemID = fAppMenu->appendSwitch("Flip X", "Flip X" , sinkID, false);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000970 fAppMenu->assignKeyEquivalentToItem(itemID, 'x');
971 itemID = fAppMenu->appendSwitch("Flip Y", "Flip Y" , sinkID, false);
972 fAppMenu->assignKeyEquivalentToItem(itemID, 'y');
973 itemID = fAppMenu->appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
974 fAppMenu->assignKeyEquivalentToItem(itemID, 'z');
975 itemID = fAppMenu->appendSwitch("Magnify", "Magnify" , sinkID, fMagnify);
976 fAppMenu->assignKeyEquivalentToItem(itemID, 'm');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000977 itemID =fAppMenu->appendList("Transition-Next", "Transition-Next", sinkID,
978 fTransitionNext, "Up", "Up and Right", "Right",
979 "Down and Right", "Down", "Down and Left",
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000980 "Left", "Up and Left", NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000981 fAppMenu->assignKeyEquivalentToItem(itemID, 'j');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000982 itemID =fAppMenu->appendList("Transition-Prev", "Transition-Prev", sinkID,
983 fTransitionPrev, "Up", "Up and Right", "Right",
984 "Down and Right", "Down", "Down and Left",
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000985 "Left", "Up and Left", NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000986 fAppMenu->assignKeyEquivalentToItem(itemID, 'k');
987 itemID = fAppMenu->appendAction("Save to PDF", sinkID);
988 fAppMenu->assignKeyEquivalentToItem(itemID, 'e');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000989
scroggo@google.com7dadc742012-04-18 14:07:57 +0000990 this->addMenu(fAppMenu);
991 fSlideMenu = new SkOSMenu;
992 this->addMenu(fSlideMenu);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000993
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000994// this->setConfig(SkBitmap::kRGB_565_Config);
995 this->setConfig(SkBitmap::kARGB_8888_Config);
996 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000997 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000998
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000999 skiagm::GM::SetResourcePath(resourcePath);
1000
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001001 this->loadView((*fSamples[fCurrIndex])());
chudy@google.com4605a3f2012-08-01 17:58:01 +00001002
bsalomon@google.com840e9f32011-07-06 21:59:09 +00001003 fPDFData = NULL;
reed@google.comf0b5f682011-03-11 20:08:25 +00001004
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001005 if (NULL == devManager) {
1006 fDevManager = new DefaultDeviceManager();
1007 } else {
1008 devManager->ref();
1009 fDevManager = devManager;
1010 }
bsalomon@google.com11959252012-04-06 20:13:38 +00001011 fDevManager->setUpBackend(this, fMSAASampleCount);
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001012
Scroggob4490c72011-06-17 13:53:05 +00001013 // If another constructor set our dimensions, ensure that our
1014 // onSizeChange gets called.
1015 if (this->height() && this->width()) {
1016 this->onSizeChange();
1017 }
reed@google.com2072db82011-11-08 15:18:10 +00001018
1019 // can't call this synchronously, since it may require a subclass to
1020 // to implement, or the caller may need us to have returned from the
1021 // constructor first. Hence we post an event to ourselves.
1022// this->updateTitle();
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001023 post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001024}
1025
1026SampleWindow::~SampleWindow() {
1027 delete fPicture;
Scroggo8ac0d542011-06-21 14:44:57 +00001028 delete fPdfCanvas;
Scroggo0f185c22011-03-24 18:35:50 +00001029 fTypeface->unref();
reed@google.com29038ed2011-07-06 17:56:47 +00001030
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001031 SkSafeUnref(fDevManager);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001032}
1033
reed@google.com1830c7a2012-06-04 12:05:43 +00001034static void make_filepath(SkString* path, const char* dir, const SkString& name) {
1035 size_t len = strlen(dir);
1036 path->set(dir);
1037 if (len > 0 && dir[len - 1] != '/') {
1038 path->append("/");
1039 }
1040 path->append(name);
1041}
1042
chudy@google.com4605a3f2012-08-01 17:58:01 +00001043void SampleWindow::registerPictFileSample(char** argv, int argc) {
1044 const char* pict = NULL;
1045
1046 for (int i = 0; i < argc; ++i) {
1047 if (!strcmp(argv[i], "--picture")) {
1048 i += 1;
1049 if (i < argc) {
1050 pict = argv[i];
1051 break;
1052 }
1053 }
1054 }
1055 if (pict) {
1056 SkString path(pict);
reed@google.com7d05ac92013-06-06 13:14:13 +00001057 fCurrIndex = fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001058 *fSamples.append() = new PictFileFactory(path);
1059 }
1060}
1061
reed@google.com1830c7a2012-06-04 12:05:43 +00001062void SampleWindow::registerPictFileSamples(char** argv, int argc) {
1063 const char* pictDir = NULL;
1064
1065 for (int i = 0; i < argc; ++i) {
1066 if (!strcmp(argv[i], "--pictureDir")) {
1067 i += 1;
1068 if (i < argc) {
1069 pictDir = argv[i];
1070 break;
1071 }
1072 }
1073 }
1074 if (pictDir) {
1075 SkOSFile::Iter iter(pictDir, "skp");
1076 SkString filename;
1077 while (iter.next(&filename)) {
1078 SkString path;
1079 make_filepath(&path, pictDir, filename);
1080 *fSamples.append() = new PictFileFactory(path);
1081 }
1082 }
1083}
1084
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001085#ifdef SAMPLE_PDF_FILE_VIEWER
1086void SampleWindow::registerPdfFileViewerSamples(char** argv, int argc) {
1087 const char* pdfDir = NULL;
1088
1089 for (int i = 0; i < argc; ++i) {
1090 if (!strcmp(argv[i], "--pdfDir")) {
1091 i += 1;
1092 if (i < argc) {
1093 pdfDir = argv[i];
1094 break;
1095 }
1096 }
1097 }
1098 if (pdfDir) {
1099 SkOSFile::Iter iter(pdfDir, "pdf");
1100 SkString filename;
1101 while (iter.next(&filename)) {
1102 SkString path;
1103 make_filepath(&path, pdfDir, filename);
1104 *fSamples.append() = new PdfFileViewerFactory(path);
1105 }
1106 }
1107}
1108#endif // SAMPLE_PDF_FILE_VIEWER
1109
1110
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001111int SampleWindow::findByTitle(const char title[]) {
1112 int i, count = fSamples.count();
1113 for (i = 0; i < count; i++) {
1114 if (getSampleTitle(i).equals(title)) {
1115 return i;
1116 }
1117 }
mike@reedtribe.orgdd52caa2011-12-28 20:02:10 +00001118 return -1;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001119}
1120
robertphillips@google.com7265e722012-05-03 18:22:28 +00001121void SampleWindow::listTitles() {
1122 int count = fSamples.count();
1123 SkDebugf("All Slides:\n");
1124 for (int i = 0; i < count; i++) {
1125 SkDebugf(" %s\n", getSampleTitle(i).c_str());
1126 }
1127}
1128
reed@android.com55e76b22009-11-23 21:46:47 +00001129static SkBitmap capture_bitmap(SkCanvas* canvas) {
1130 SkBitmap bm;
1131 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
1132 src.copyTo(&bm, src.config());
1133 return bm;
1134}
1135
1136static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
1137 SkBitmap* diff) {
1138 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001139
reed@android.com55e76b22009-11-23 21:46:47 +00001140 SkAutoLockPixels alp0(src);
1141 SkAutoLockPixels alp1(orig);
1142 for (int y = 0; y < src.height(); y++) {
1143 const void* srcP = src.getAddr(0, y);
1144 const void* origP = orig.getAddr(0, y);
1145 size_t bytes = src.width() * src.bytesPerPixel();
1146 if (memcmp(srcP, origP, bytes)) {
1147 SkDebugf("---------- difference on line %d\n", y);
1148 return true;
1149 }
1150 }
1151 return false;
1152}
1153
Scroggo0f185c22011-03-24 18:35:50 +00001154static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
1155{
1156 SkColor desiredColor = paint.getColor();
1157 paint.setColor(SK_ColorWHITE);
1158 const char* c_str = string.c_str();
1159 size_t size = string.size();
1160 SkRect bounds;
1161 paint.measureText(c_str, size, &bounds);
1162 bounds.offset(left, top);
1163 SkScalar inset = SkIntToScalar(-2);
1164 bounds.inset(inset, inset);
1165 canvas->drawRect(bounds, paint);
1166 if (desiredColor != SK_ColorBLACK) {
1167 paint.setColor(SK_ColorBLACK);
1168 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
1169 }
1170 paint.setColor(desiredColor);
1171 canvas->drawText(c_str, size, left, top, paint);
1172}
1173
reed@android.com44177402009-11-23 21:07:51 +00001174#define XCLIP_N 8
1175#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +00001176
1177void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +00001178 // update the animation time
bsalomon@google.come8f09102011-09-08 18:48:12 +00001179 if (!gAnimTimePrev && !gAnimTime) {
1180 // first time make delta be 0
1181 gAnimTime = SkTime::GetMSecs();
1182 gAnimTimePrev = gAnimTime;
1183 } else {
1184 gAnimTimePrev = gAnimTime;
1185 gAnimTime = SkTime::GetMSecs();
1186 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001187
reed@google.comf03bb562011-11-11 21:42:12 +00001188 if (fGesture.isActive()) {
1189 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001190 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001191
djsollen@google.com796763e2012-12-10 14:12:55 +00001192 if (fMeasureFPS) {
1193 fMeasureFPS_Time = 0;
1194 }
1195
reed@android.come522ca52009-11-23 20:10:41 +00001196 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +00001197 this->INHERITED::draw(canvas);
1198 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +00001199
1200 const SkScalar w = this->width();
1201 const SkScalar h = this->height();
1202 const SkScalar cw = w / XCLIP_N;
1203 const SkScalar ch = h / YCLIP_N;
1204 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +00001205 SkRect r;
1206 r.fTop = y * ch;
1207 r.fBottom = (y + 1) * ch;
1208 if (y == YCLIP_N - 1) {
1209 r.fBottom = h;
1210 }
reed@android.come522ca52009-11-23 20:10:41 +00001211 for (int x = 0; x < XCLIP_N; x++) {
1212 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +00001213 r.fLeft = x * cw;
1214 r.fRight = (x + 1) * cw;
1215 if (x == XCLIP_N - 1) {
1216 r.fRight = w;
1217 }
reed@android.come522ca52009-11-23 20:10:41 +00001218 canvas->clipRect(r);
1219 this->INHERITED::draw(canvas);
1220 }
1221 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001222
reed@android.com55e76b22009-11-23 21:46:47 +00001223 SkBitmap diff;
1224 if (bitmap_diff(canvas, orig, &diff)) {
1225 }
reed@android.come522ca52009-11-23 20:10:41 +00001226 } else {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00001227 SkSize tile = this->tileSize();
rmistry@google.comae933ce2012-08-23 18:19:56 +00001228
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001229 for (SkScalar y = 0; y < height(); y += tile.height()) {
1230 for (SkScalar x = 0; x < width(); x += tile.width()) {
scroggo@google.com85cade02012-08-17 13:29:50 +00001231 SkAutoCanvasRestore acr(canvas, true);
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001232 canvas->clipRect(SkRect::MakeXYWH(x, y,
1233 tile.width(),
1234 tile.height()));
scroggo@google.com85cade02012-08-17 13:29:50 +00001235 this->INHERITED::draw(canvas);
1236 }
1237 }
rmistry@google.comae933ce2012-08-23 18:19:56 +00001238
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001239 if (fTilingMode != kNo_Tiling) {
scroggo@google.com85cade02012-08-17 13:29:50 +00001240 SkPaint paint;
1241 paint.setColor(0x60FF00FF);
1242 paint.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001243
1244 for (SkScalar y = 0; y < height(); y += tile.height()) {
1245 for (SkScalar x = 0; x < width(); x += tile.width()) {
1246 canvas->drawRect(SkRect::MakeXYWH(x, y,
1247 tile.width(),
1248 tile.height()),
1249 paint);
scroggo@google.com85cade02012-08-17 13:29:50 +00001250 }
1251 }
1252 }
reed@android.come522ca52009-11-23 20:10:41 +00001253 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001254 if (fShowZoomer && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +00001255 showZoomer(canvas);
1256 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001257 if (fMagnify && !fSaveToPdf) {
1258 magnify(canvas);
1259 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001260
djsollen@google.com796763e2012-12-10 14:12:55 +00001261 if (fMeasureFPS && fMeasureFPS_Time) {
1262 this->updateTitle();
1263 this->postInvalDelay();
1264 }
1265
reed@google.com29038ed2011-07-06 17:56:47 +00001266 // do this last
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001267 fDevManager->publishCanvas(fDeviceType, canvas, this);
Scroggo3272ba82011-05-25 20:50:42 +00001268}
1269
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001270static float clipW = 200;
1271static float clipH = 200;
1272void SampleWindow::magnify(SkCanvas* canvas) {
1273 SkRect r;
1274 int count = canvas->save();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001275
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001276 SkMatrix m = canvas->getTotalMatrix();
robertphillips@google.com07ef9112012-06-04 13:22:14 +00001277 if (!m.invert(&m)) {
1278 return;
1279 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001280 SkPoint offset, center;
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001281 SkScalar mouseX = fMouseX * SK_Scalar1;
1282 SkScalar mouseY = fMouseY * SK_Scalar1;
1283 m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset);
1284 m.mapXY(mouseX, mouseY, &center);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001285
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001286 r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX());
1287 r.offset(offset.fX, offset.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001288
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001289 SkPaint paint;
1290 paint.setColor(0xFF66AAEE);
1291 paint.setStyle(SkPaint::kStroke_Style);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001292 paint.setStrokeWidth(10.f * m.getScaleX());
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001293 //lense offset
1294 //canvas->translate(0, -250);
1295 canvas->drawRect(r, paint);
1296 canvas->clipRect(r);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001297
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001298 m = canvas->getTotalMatrix();
1299 m.setTranslate(-center.fX, -center.fY);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001300 m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001301 m.postTranslate(center.fX, center.fY);
1302 canvas->concat(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001303
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001304 this->INHERITED::draw(canvas);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001305
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001306 canvas->restoreToCount(count);
1307}
1308
Scroggo3272ba82011-05-25 20:50:42 +00001309void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +00001310 int count = canvas->save();
1311 canvas->resetMatrix();
1312 // Ensure the mouse position is on screen.
reed@google.come1ca7052013-12-17 19:22:07 +00001313 int width = SkScalarRoundToInt(this->width());
1314 int height = SkScalarRoundToInt(this->height());
Scroggo0f185c22011-03-24 18:35:50 +00001315 if (fMouseX >= width) fMouseX = width - 1;
1316 else if (fMouseX < 0) fMouseX = 0;
1317 if (fMouseY >= height) fMouseY = height - 1;
1318 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +00001319
Scroggo0f185c22011-03-24 18:35:50 +00001320 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +00001321 bitmap.lockPixels();
1322
Scroggo0f185c22011-03-24 18:35:50 +00001323 // 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 +00001324 int zoomedWidth = (width >> 1) | 1;
1325 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +00001326 SkIRect src;
1327 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
1328 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
1329 SkRect dest;
1330 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
1331 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
1332 SkPaint paint;
1333 // Clear the background behind our zoomed in view
1334 paint.setColor(SK_ColorWHITE);
1335 canvas->drawRect(dest, paint);
1336 canvas->drawBitmapRect(bitmap, &src, dest);
1337 paint.setColor(SK_ColorBLACK);
1338 paint.setStyle(SkPaint::kStroke_Style);
1339 // Draw a border around the pixel in the middle
1340 SkRect originalPixel;
1341 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
1342 SkMatrix matrix;
1343 SkRect scalarSrc;
1344 scalarSrc.set(src);
1345 SkColor color = bitmap.getColor(fMouseX, fMouseY);
1346 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
1347 SkRect pixel;
1348 matrix.mapRect(&pixel, originalPixel);
1349 // TODO Perhaps measure the values and make the outline white if it's "dark"
1350 if (color == SK_ColorBLACK) {
1351 paint.setColor(SK_ColorWHITE);
1352 }
1353 canvas->drawRect(pixel, paint);
1354 }
1355 paint.setColor(SK_ColorBLACK);
1356 // Draw a border around the destination rectangle
1357 canvas->drawRect(dest, paint);
1358 paint.setStyle(SkPaint::kStrokeAndFill_Style);
1359 // Identify the pixel and its color on screen
1360 paint.setTypeface(fTypeface);
1361 paint.setAntiAlias(true);
1362 SkScalar lineHeight = paint.getFontMetrics(NULL);
1363 SkString string;
1364 string.appendf("(%i, %i)", fMouseX, fMouseY);
1365 SkScalar left = dest.fLeft + SkIntToScalar(3);
1366 SkScalar i = SK_Scalar1;
1367 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1368 // Alpha
1369 i += SK_Scalar1;
1370 string.reset();
1371 string.appendf("A: %X", SkColorGetA(color));
1372 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1373 // Red
1374 i += SK_Scalar1;
1375 string.reset();
1376 string.appendf("R: %X", SkColorGetR(color));
1377 paint.setColor(SK_ColorRED);
1378 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1379 // Green
1380 i += SK_Scalar1;
1381 string.reset();
1382 string.appendf("G: %X", SkColorGetG(color));
1383 paint.setColor(SK_ColorGREEN);
1384 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1385 // Blue
1386 i += SK_Scalar1;
1387 string.reset();
1388 string.appendf("B: %X", SkColorGetB(color));
1389 paint.setColor(SK_ColorBLUE);
1390 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1391 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +00001392}
1393
reed@android.com8a1c16f2008-12-17 15:59:43 +00001394void SampleWindow::onDraw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001395}
1396
1397#include "SkColorPriv.h"
1398
caryclark@google.com02939ce2012-06-06 12:09:51 +00001399#if 0 // UNUSED
reed@android.com8a1c16f2008-12-17 15:59:43 +00001400static void reverseRedAndBlue(const SkBitmap& bm) {
1401 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
1402 uint8_t* p = (uint8_t*)bm.getPixels();
1403 uint8_t* stop = p + bm.getSize();
1404 while (p < stop) {
1405 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
1406 unsigned scale = SkAlpha255To256(p[3]);
1407 unsigned r = p[2];
1408 unsigned b = p[0];
1409 p[0] = SkAlphaMul(r, scale);
1410 p[1] = SkAlphaMul(p[1], scale);
1411 p[2] = SkAlphaMul(b, scale);
1412 p += 4;
1413 }
1414}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001415#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001416
Scroggo8ac0d542011-06-21 14:44:57 +00001417void SampleWindow::saveToPdf()
1418{
1419 fSaveToPdf = true;
1420 this->inval(NULL);
1421}
1422
reed@android.com8a1c16f2008-12-17 15:59:43 +00001423SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +00001424 if (fSaveToPdf) {
1425 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
1426 SkISize size = SkISize::Make(bmp.width(), bmp.height());
1427 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
1428 canvas->getTotalMatrix());
1429 fPdfCanvas = new SkCanvas(pdfDevice);
1430 pdfDevice->unref();
1431 canvas = fPdfCanvas;
bsalomon@google.com82502e22013-01-24 20:47:18 +00001432 } else if (kPicture_DeviceType == fDeviceType) {
1433 fPicture = new SkPicture;
1434 canvas = fPicture->beginRecording(9999, 9999);
Scroggo8ac0d542011-06-21 14:44:57 +00001435 } else {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001436#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00001437 if (kNullGPU_DeviceType != fDeviceType)
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001438#endif
bsalomon@google.com82502e22013-01-24 20:47:18 +00001439 {
1440 canvas = this->INHERITED::beforeChildren(canvas);
reed@google.comac10a2d2010-12-22 21:39:39 +00001441 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001442 }
1443
1444 if (fUseClip) {
1445 canvas->drawColor(0xFFFF88FF);
reed@google.com045e62d2011-10-24 12:19:46 +00001446 canvas->clipPath(fClipPath, SkRegion::kIntersect_Op, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001447 }
1448
1449 return canvas;
1450}
1451
1452static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
1453 const SkRegion& rgn) {
1454 SkCanvas canvas(bm);
1455 SkRegion inval(rgn);
1456
1457 inval.translate(r.fLeft, r.fTop);
1458 canvas.clipRegion(inval);
1459 canvas.drawColor(0xFFFF8080);
1460}
yangsu@google.com501775e2011-06-24 16:04:50 +00001461#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001462void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +00001463 if (fSaveToPdf) {
1464 fSaveToPdf = false;
1465 if (fShowZoomer) {
1466 showZoomer(fPdfCanvas);
1467 }
1468 SkString name;
1469 name.printf("%s.pdf", this->getTitle());
1470 SkPDFDocument doc;
1471 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
1472 doc.appendPage(device);
djsollen@google.com56c69772011-11-08 19:00:26 +00001473#ifdef SK_BUILD_FOR_ANDROID
Scroggo8ac0d542011-06-21 14:44:57 +00001474 name.prepend("/sdcard/");
1475#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +00001476
yangsu@google.com501775e2011-06-24 16:04:50 +00001477#ifdef SK_BUILD_FOR_IOS
1478 SkDynamicMemoryWStream mstream;
1479 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +00001480 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +00001481#endif
Scroggo8ac0d542011-06-21 14:44:57 +00001482 SkFILEWStream stream(name.c_str());
1483 if (stream.isValid()) {
1484 doc.emitPDF(&stream);
1485 const char* desc = "File saved from Skia SampleApp";
1486 this->onPDFSaved(this->getTitle(), desc, name.c_str());
1487 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001488
Scroggo8ac0d542011-06-21 14:44:57 +00001489 delete fPdfCanvas;
1490 fPdfCanvas = NULL;
1491
1492 // We took over the draw calls in order to create the PDF, so we need
1493 // to redraw.
1494 this->inval(NULL);
1495 return;
1496 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001497
reed@android.comf2b98d62010-12-20 18:26:13 +00001498 if (fRequestGrabImage) {
1499 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001500
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001501 SkBaseDevice* device = orig->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001502 SkBitmap bmp;
1503 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001504 static int gSampleGrabCounter;
1505 SkString name;
reed@google.com7c57e0e2012-09-08 21:20:33 +00001506 name.printf("sample_grab_%d.png", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001507 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +00001508 SkImageEncoder::kPNG_Type, 100);
1509 }
1510 }
1511
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001512 if (kPicture_DeviceType == fDeviceType) {
1513 if (true) {
1514 SkPicture* pict = new SkPicture(*fPicture);
1515 fPicture->unref();
reed@google.come23f1942011-12-08 19:36:00 +00001516 this->installDrawFilter(orig);
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001517 orig->drawPicture(*pict);
1518 pict->unref();
1519 } else if (true) {
1520 SkDynamicMemoryWStream ostream;
1521 fPicture->serialize(&ostream);
1522 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001523
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001524 SkAutoDataUnref data(ostream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +00001525 SkMemoryStream istream(data->data(), data->size());
scroggo@google.comf1754ec2013-06-28 21:32:00 +00001526 SkAutoTUnref<SkPicture> pict(SkPicture::CreateFromStream(&istream));
1527 if (pict.get() != NULL) {
1528 orig->drawPicture(*pict.get());
1529 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001530 } else {
1531 fPicture->draw(orig);
1532 fPicture->unref();
1533 }
1534 fPicture = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001535 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001536
reed@google.com17d7aec2011-04-25 14:31:44 +00001537 // Do this after presentGL and other finishing, rather than in afterChild
djsollen@google.com796763e2012-12-10 14:12:55 +00001538 if (fMeasureFPS && fMeasureFPS_StartTime) {
1539 fMeasureFPS_Time += SkTime::GetMSecs() - fMeasureFPS_StartTime;
reed@google.com17d7aec2011-04-25 14:31:44 +00001540 }
1541
1542 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +00001543 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001544 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
1545 int dx = fScrollTestX * 7;
1546 int dy = fScrollTestY * 7;
1547 SkIRect r;
1548 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001549
reed@android.com8a1c16f2008-12-17 15:59:43 +00001550 r.set(50, 50, 50+100, 50+100);
1551 bm.scrollRect(&r, dx, dy, &inval);
1552 paint_rgn(bm, r, inval);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001553 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001554}
1555
reed@android.com6c5f6f22009-08-14 16:08:38 +00001556void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
reed@android.com6c5f6f22009-08-14 16:08:38 +00001557 if (fRotate) {
bungeman@google.comb1785912013-07-09 21:58:56 +00001558 fRotateAnimTime += SampleCode::GetAnimSecondsDelta();
1559
reed@android.com6c5f6f22009-08-14 16:08:38 +00001560 SkScalar cx = this->width() / 2;
1561 SkScalar cy = this->height() / 2;
1562 canvas->translate(cx, cy);
bungeman@google.comb1785912013-07-09 21:58:56 +00001563 canvas->rotate(fRotateAnimTime * 10);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001564 canvas->translate(-cx, -cy);
1565 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001566
bsalomon@google.come8f09102011-09-08 18:48:12 +00001567 if (fPerspAnim) {
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001568 fPerspAnimTime += SampleCode::GetAnimSecondsDelta();
1569
1570 static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
1571 static const SkScalar gAnimMag = SK_Scalar1 / 1000;
1572 SkScalar t = SkScalarMod(fPerspAnimTime, gAnimPeriod);
1573 if (SkScalarFloorToInt(SkScalarDiv(fPerspAnimTime, gAnimPeriod)) & 0x1) {
1574 t = gAnimPeriod - t;
1575 }
1576 t = 2 * t - gAnimPeriod;
1577 t = SkScalarMul(SkScalarDiv(t, gAnimPeriod), gAnimMag);
1578 SkMatrix m;
1579 m.reset();
1580 m.setPerspY(t);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001581 canvas->concat(m);
1582 }
reed@google.comf0b5f682011-03-11 20:08:25 +00001583
reed@google.come23f1942011-12-08 19:36:00 +00001584 this->installDrawFilter(canvas);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001585
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001586 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +00001587 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
djsollen@google.com796763e2012-12-10 14:12:55 +00001588 fMeasureFPS_StartTime = SkTime::GetMSecs();
reed@google.comf2183392011-04-22 14:10:48 +00001589 }
1590 } else {
1591 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001592 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001593 if (fPerspAnim || fRotate) {
bsalomon@google.come8f09102011-09-08 18:48:12 +00001594 this->inval(NULL);
1595 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001596}
1597
1598void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +00001599 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001600}
1601
reed@android.com8a1c16f2008-12-17 15:59:43 +00001602static SkBitmap::Config gConfigCycle[] = {
1603 SkBitmap::kNo_Config, // none -> none
reed@android.com8a1c16f2008-12-17 15:59:43 +00001604 SkBitmap::kNo_Config, // a8 -> none
1605 SkBitmap::kNo_Config, // index8 -> none
1606 SkBitmap::kARGB_4444_Config, // 565 -> 4444
1607 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
1608 SkBitmap::kRGB_565_Config // 8888 -> 565
1609};
1610
1611static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
1612 return gConfigCycle[c];
1613}
1614
djsollen@google.come32b5832011-06-13 16:58:40 +00001615void SampleWindow::changeZoomLevel(float delta) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001616 fZoomLevel += delta;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001617 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001618 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
1619 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001620 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001621 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
1622 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001623 } else {
1624 fZoomScale = SK_Scalar1;
1625 }
reed@google.comf03bb562011-11-11 21:42:12 +00001626 this->updateMatrix();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001627}
1628
reed@google.comf03bb562011-11-11 21:42:12 +00001629void SampleWindow::updateMatrix(){
1630 SkMatrix m;
1631 m.reset();
1632 if (fZoomLevel) {
1633 SkPoint center;
1634 //m = this->getLocalMatrix();//.invert(&m);
1635 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
1636 SkScalar cx = center.fX;
1637 SkScalar cy = center.fY;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001638
reed@google.comf03bb562011-11-11 21:42:12 +00001639 m.setTranslate(-cx, -cy);
1640 m.postScale(fZoomScale, fZoomScale);
1641 m.postTranslate(cx, cy);
1642 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001643
reed@google.comf03bb562011-11-11 21:42:12 +00001644 if (fFlipAxis) {
1645 m.preTranslate(fZoomCenterX, fZoomCenterY);
1646 if (fFlipAxis & kFlipAxis_X) {
1647 m.preScale(-SK_Scalar1, SK_Scalar1);
1648 }
1649 if (fFlipAxis & kFlipAxis_Y) {
1650 m.preScale(SK_Scalar1, -SK_Scalar1);
1651 }
1652 m.preTranslate(-fZoomCenterX, -fZoomCenterY);
1653 //canvas->concat(m);
1654 }
1655 // Apply any gesture matrix
1656 m.preConcat(fGesture.localM());
1657 m.preConcat(fGesture.globalM());
chudy@google.com4605a3f2012-08-01 17:58:01 +00001658
reed@google.comf03bb562011-11-11 21:42:12 +00001659 this->setLocalMatrix(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001660
reed@google.comf03bb562011-11-11 21:42:12 +00001661 this->updateTitle();
1662 this->inval(NULL);
1663}
Scroggo2c8208f2011-06-15 16:49:08 +00001664bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +00001665 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001666 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001667 fTransitionPrev));
Scroggo2c8208f2011-06-15 16:49:08 +00001668 return true;
1669}
1670
reed@android.com8a1c16f2008-12-17 15:59:43 +00001671bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +00001672 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001673 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001674 fTransitionNext));
reed@android.com34245c72009-11-03 04:00:48 +00001675 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001676}
1677
yangsu@google.com501775e2011-06-24 16:04:50 +00001678bool SampleWindow::goToSample(int i) {
1679 fCurrIndex = (i) % fSamples.count();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001680 this->loadView(create_transition(curr_view(this),(*fSamples[fCurrIndex])(), 6));
yangsu@google.com501775e2011-06-24 16:04:50 +00001681 return true;
1682}
1683
1684SkString SampleWindow::getSampleTitle(int i) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001685 return ::getSampleTitle(fSamples[i]);
yangsu@google.com501775e2011-06-24 16:04:50 +00001686}
1687
1688int SampleWindow::sampleCount() {
1689 return fSamples.count();
1690}
1691
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001692void SampleWindow::showOverview() {
chudy@google.com4605a3f2012-08-01 17:58:01 +00001693 this->loadView(create_transition(curr_view(this),
1694 create_overview(fSamples.count(), fSamples.begin()),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001695 4));
1696}
1697
reed@google.come23f1942011-12-08 19:36:00 +00001698void SampleWindow::installDrawFilter(SkCanvas* canvas) {
reed@google.com15bc13d2013-12-10 16:53:13 +00001699 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState, fSubpixelState,
1700 fHintingState, fFilterLevelIndex))->unref();
reed@google.come23f1942011-12-08 19:36:00 +00001701}
1702
Scroggo2c8208f2011-06-15 16:49:08 +00001703void SampleWindow::postAnimatingEvent() {
1704 if (fAnimating) {
reed@google.com87fac4a2011-08-04 13:50:17 +00001705 (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
Scroggo2c8208f2011-06-15 16:49:08 +00001706 }
1707}
reed@google.come23f1942011-12-08 19:36:00 +00001708
reed@android.com8a1c16f2008-12-17 15:59:43 +00001709bool SampleWindow::onEvent(const SkEvent& evt) {
reed@google.com2072db82011-11-08 15:18:10 +00001710 if (evt.isType(gUpdateWindowTitleEvtName)) {
1711 this->updateTitle();
1712 return true;
1713 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001714 if (evt.isType(ANIMATING_EVENTTYPE)) {
1715 if (fAnimating) {
1716 this->nextSample();
1717 this->postAnimatingEvent();
1718 }
1719 return true;
1720 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001721 if (evt.isType("replace-transition-view")) {
1722 this->loadView((SkView*)SkEventSink::FindSink(evt.getFast32()));
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001723 return true;
1724 }
reed@android.com34245c72009-11-03 04:00:48 +00001725 if (evt.isType("set-curr-index")) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001726 this->goToSample(evt.getFast32());
reed@android.com34245c72009-11-03 04:00:48 +00001727 return true;
1728 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001729 if (isInvalEvent(evt)) {
1730 this->inval(NULL);
1731 return true;
1732 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001733 int selected = -1;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001734 if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001735 this->setDeviceType((DeviceType)selected);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001736 return true;
yangsu@google.com921091f2011-08-02 13:39:12 +00001737 }
scroggo@google.comb073d922012-06-08 15:35:03 +00001738 if (SkOSMenu::FindTriState(evt, "Pipe", &fPipeState)) {
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001739#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00001740 if (!fPipeState != SkOSMenu::kOnState)
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001741 gServer.disconnectAll();
1742#endif
scroggo@google.comb073d922012-06-08 15:35:03 +00001743 (void)SampleView::SetUsePipe(curr_view(this), fPipeState);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001744 this->updateTitle();
1745 this->inval(NULL);
yangsu@google.com921091f2011-08-02 13:39:12 +00001746 return true;
1747 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001748 if (SkOSMenu::FindSwitchState(evt, "Slide Show", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001749 this->toggleSlideshow();
1750 return true;
1751 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001752 if (SkOSMenu::FindTriState(evt, "AA", &fAAState) ||
1753 SkOSMenu::FindTriState(evt, "LCD", &fLCDState) ||
reed@google.com15bc13d2013-12-10 16:53:13 +00001754 SkOSMenu::FindListIndex(evt, "FilterLevel", &fFilterLevelIndex) ||
bungeman@google.com96aabc82013-06-03 21:26:34 +00001755 SkOSMenu::FindTriState(evt, "Subpixel", &fSubpixelState) ||
1756 SkOSMenu::FindListIndex(evt, "Hinting", &fHintingState) ||
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001757 SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) ||
1758 SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
1759 SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify) ||
1760 SkOSMenu::FindListIndex(evt, "Transition-Next", &fTransitionNext) ||
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00001761 SkOSMenu::FindListIndex(evt, "Transition-Prev", &fTransitionPrev)) {
1762 this->inval(NULL);
1763 this->updateTitle();
1764 return true;
1765 }
1766 if (SkOSMenu::FindListIndex(evt, "Tiling", &fTilingMode)) {
1767 if (SampleView::IsSampleView(curr_view(this))) {
1768 ((SampleView*)curr_view(this))->onTileSizeChanged(this->tileSize());
1769 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001770 this->inval(NULL);
1771 this->updateTitle();
1772 return true;
1773 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001774 if (SkOSMenu::FindSwitchState(evt, "Flip X", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001775 fFlipAxis ^= kFlipAxis_X;
reed@google.comf03bb562011-11-11 21:42:12 +00001776 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001777 return true;
1778 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001779 if (SkOSMenu::FindSwitchState(evt, "Flip Y", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001780 fFlipAxis ^= kFlipAxis_Y;
reed@google.comf03bb562011-11-11 21:42:12 +00001781 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001782 return true;
1783 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001784 if (SkOSMenu::FindAction(evt,"Save to PDF")) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001785 this->saveToPdf();
1786 return true;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001787 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001788 return this->INHERITED::onEvent(evt);
1789}
1790
reed@android.comf2b98d62010-12-20 18:26:13 +00001791bool SampleWindow::onQuery(SkEvent* query) {
1792 if (query->isType("get-slide-count")) {
1793 query->setFast32(fSamples.count());
1794 return true;
1795 }
1796 if (query->isType("get-slide-title")) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001797 SkView* view = (*fSamples[query->getFast32()])();
reed@android.comf2b98d62010-12-20 18:26:13 +00001798 SkEvent evt(gTitleEvtName);
1799 if (view->doQuery(&evt)) {
1800 query->setString("title", evt.findString(gTitleEvtName));
1801 }
1802 SkSafeUnref(view);
1803 return true;
1804 }
1805 if (query->isType("use-fast-text")) {
1806 SkEvent evt(gFastTextEvtName);
1807 return curr_view(this)->doQuery(&evt);
1808 }
reed@google.com29038ed2011-07-06 17:56:47 +00001809 if (query->isType("ignore-window-bitmap")) {
1810 query->setFast32(this->getGrContext() != NULL);
1811 return true;
1812 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001813 return this->INHERITED::onQuery(query);
1814}
1815
caryclark@google.com02939ce2012-06-06 12:09:51 +00001816#if 0 // UNUSED
reed@android.com0ae6b242008-12-23 16:49:54 +00001817static void cleanup_for_filename(SkString* name) {
1818 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001819 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001820 switch (str[i]) {
1821 case ':': str[i] = '-'; break;
1822 case '/': str[i] = '-'; break;
1823 case ' ': str[i] = '_'; break;
1824 default: break;
1825 }
1826 }
1827}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001828#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001829
1830bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001831 {
1832 SkView* view = curr_view(this);
1833 if (view) {
1834 SkEvent evt(gCharEvtName);
1835 evt.setFast32(uni);
1836 if (view->doQuery(&evt)) {
1837 return true;
1838 }
1839 }
1840 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001841
reed@android.com8a1c16f2008-12-17 15:59:43 +00001842 int dx = 0xFF;
1843 int dy = 0xFF;
1844
1845 switch (uni) {
1846 case '5': dx = 0; dy = 0; break;
1847 case '8': dx = 0; dy = -1; break;
1848 case '6': dx = 1; dy = 0; break;
1849 case '2': dx = 0; dy = 1; break;
1850 case '4': dx = -1; dy = 0; break;
1851 case '7': dx = -1; dy = -1; break;
1852 case '9': dx = 1; dy = -1; break;
1853 case '3': dx = 1; dy = 1; break;
1854 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001855
reed@android.com8a1c16f2008-12-17 15:59:43 +00001856 default:
1857 break;
1858 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001859
reed@android.com8a1c16f2008-12-17 15:59:43 +00001860 if (0xFF != dx && 0xFF != dy) {
1861 if ((dx | dy) == 0) {
1862 fScrollTestX = fScrollTestY = 0;
1863 } else {
1864 fScrollTestX += dx;
1865 fScrollTestY += dy;
1866 }
1867 this->inval(NULL);
1868 return true;
1869 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001870
reed@android.com0ae6b242008-12-23 16:49:54 +00001871 switch (uni) {
reed@google.coma4f81372012-11-15 15:56:38 +00001872 case 'B':
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001873 post_event_to_sink(SkNEW_ARGS(SkEvent, ("PictFileView::toggleBBox")), curr_view(this));
1874 // Cannot call updateTitle() synchronously, because the toggleBBox event is still in
1875 // the queue.
1876 post_event_to_sink(SkNEW_ARGS(SkEvent, (gUpdateWindowTitleEvtName)), this);
reed@google.coma4f81372012-11-15 15:56:38 +00001877 this->inval(NULL);
1878 break;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001879 case 'f':
chudy@google.com4605a3f2012-08-01 17:58:01 +00001880 // only
djsollen@google.com6ff82552011-11-07 15:43:57 +00001881 toggleFPS();
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001882 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001883 case 'g':
1884 fRequestGrabImage = true;
1885 this->inval(NULL);
1886 break;
reed@google.coma4f81372012-11-15 15:56:38 +00001887 case 'G':
1888 gShowGMBounds = !gShowGMBounds;
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001889 post_event_to_sink(GMSampleView::NewShowSizeEvt(gShowGMBounds),
reed@google.coma4f81372012-11-15 15:56:38 +00001890 curr_view(this));
1891 this->inval(NULL);
1892 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001893 case 'i':
1894 this->zoomIn();
1895 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001896 case 'o':
1897 this->zoomOut();
1898 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001899 case 'r':
1900 fRotate = !fRotate;
bungeman@google.comb1785912013-07-09 21:58:56 +00001901 fRotateAnimTime = 0;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001902 this->inval(NULL);
1903 this->updateTitle();
1904 return true;
bsalomon@google.come8f09102011-09-08 18:48:12 +00001905 case 'k':
1906 fPerspAnim = !fPerspAnim;
1907 this->inval(NULL);
1908 this->updateTitle();
1909 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001910#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00001911 case '\\':
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001912 this->setDeviceType(kNullGPU_DeviceType);
1913 this->inval(NULL);
1914 this->updateTitle();
bsalomon@google.com74913722011-10-27 20:44:19 +00001915 return true;
twiz@google.com05e70242012-01-27 19:12:00 +00001916 case 'p':
1917 {
1918 GrContext* grContext = this->getGrContext();
1919 if (grContext) {
1920 size_t cacheBytes = grContext->getGpuTextureCacheBytes();
1921 grContext->freeGpuResources();
1922 SkDebugf("Purged %d bytes from the GPU resource cache.\n",
1923 cacheBytes);
1924 }
1925 }
1926 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001927#endif
reed@android.com0ae6b242008-12-23 16:49:54 +00001928 default:
1929 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001930 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001931
scroggo@google.com7dadc742012-04-18 14:07:57 +00001932 if (fAppMenu->handleKeyEquivalent(uni)|| fSlideMenu->handleKeyEquivalent(uni)) {
1933 this->onUpdateMenu(fAppMenu);
1934 this->onUpdateMenu(fSlideMenu);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001935 return true;
1936 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001937 return this->INHERITED::onHandleChar(uni);
1938}
1939
yangsu@google.com921091f2011-08-02 13:39:12 +00001940void SampleWindow::setDeviceType(DeviceType type) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001941 if (type == fDeviceType)
1942 return;
1943
1944 fDevManager->tearDownBackend(this);
1945
1946 fDeviceType = type;
1947
bsalomon@google.com11959252012-04-06 20:13:38 +00001948 fDevManager->setUpBackend(this, fMSAASampleCount);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001949
yangsu@google.com921091f2011-08-02 13:39:12 +00001950 this->updateTitle();
1951 this->inval(NULL);
1952}
1953
Scroggo2c8208f2011-06-15 16:49:08 +00001954void SampleWindow::toggleSlideshow() {
1955 fAnimating = !fAnimating;
1956 this->postAnimatingEvent();
1957 this->updateTitle();
1958}
1959
1960void SampleWindow::toggleRendering() {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001961 this->setDeviceType(cycle_devicetype(fDeviceType));
Scroggo2c8208f2011-06-15 16:49:08 +00001962 this->updateTitle();
1963 this->inval(NULL);
1964}
1965
djsollen@google.com6ff82552011-11-07 15:43:57 +00001966void SampleWindow::toggleFPS() {
1967 fMeasureFPS = !fMeasureFPS;
1968 this->updateTitle();
1969 this->inval(NULL);
1970}
1971
reed@android.com8a1c16f2008-12-17 15:59:43 +00001972#include "SkDumpCanvas.h"
1973
1974bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001975 {
1976 SkView* view = curr_view(this);
1977 if (view) {
1978 SkEvent evt(gKeyEvtName);
1979 evt.setFast32(key);
1980 if (view->doQuery(&evt)) {
1981 return true;
1982 }
1983 }
1984 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001985 switch (key) {
1986 case kRight_SkKey:
1987 if (this->nextSample()) {
1988 return true;
1989 }
1990 break;
1991 case kLeft_SkKey:
scroggo@google.come2dd9732012-08-15 20:03:06 +00001992 if (this->previousSample()) {
1993 return true;
1994 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001995 return true;
1996 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001997 if (USE_ARROWS_FOR_ZOOM) {
bungeman@google.comcefd9812013-05-15 15:07:32 +00001998 this->changeZoomLevel(1.f / 32.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001999 } else {
2000 fNClip = !fNClip;
2001 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00002002 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002003 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002004 return true;
2005 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002006 if (USE_ARROWS_FOR_ZOOM) {
bungeman@google.comcefd9812013-05-15 15:07:32 +00002007 this->changeZoomLevel(-1.f / 32.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002008 } else {
2009 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00002010 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002011 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002012 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002013 case kOK_SkKey: {
2014 SkString title;
2015 if (curr_title(this, &title)) {
2016 writeTitleToPrefs(title.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002017 }
2018 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002019 }
reed@android.com34245c72009-11-03 04:00:48 +00002020 case kBack_SkKey:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002021 this->showOverview();
reed@android.com34245c72009-11-03 04:00:48 +00002022 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00002023 default:
2024 break;
2025 }
2026 return this->INHERITED::onHandleKey(key);
2027}
2028
reed@google.com52f57e12011-03-16 12:10:02 +00002029///////////////////////////////////////////////////////////////////////////////
2030
2031static const char gGestureClickType[] = "GestureClickType";
2032
Scroggod3aed392011-06-22 13:26:56 +00002033bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
reed@google.com4d5c26d2013-01-08 16:17:50 +00002034 void* owner, unsigned modi) {
Scroggo0f185c22011-03-24 18:35:50 +00002035 if (Click::kMoved_State == state) {
2036 updatePointer(x, y);
2037 }
reed@google.come1ca7052013-12-17 19:22:07 +00002038 int w = SkScalarRoundToInt(this->width());
2039 int h = SkScalarRoundToInt(this->height());
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002040
2041 // check for the resize-box
2042 if (w - x < 16 && h - y < 16) {
2043 return false; // let the OS handle the click
chudy@google.com4605a3f2012-08-01 17:58:01 +00002044 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002045 else if (fMagnify) {
2046 //it's only necessary to update the drawing if there's a click
2047 this->inval(NULL);
2048 return false; //prevent dragging while magnify is enabled
reed@google.com4d5c26d2013-01-08 16:17:50 +00002049 } else {
2050 // capture control+option, and trigger debugger
2051 if ((modi & kControl_SkModifierKey) && (modi & kOption_SkModifierKey)) {
2052 if (Click::kDown_State == state) {
2053 SkEvent evt("debug-hit-test");
2054 evt.setS32("debug-hit-test-x", x);
2055 evt.setS32("debug-hit-test-y", y);
2056 curr_view(this)->doEvent(evt);
2057 }
2058 return true;
2059 } else {
2060 return this->INHERITED::onDispatchClick(x, y, state, owner, modi);
2061 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002062 }
2063}
2064
reed@google.com52f57e12011-03-16 12:10:02 +00002065class GestureClick : public SkView::Click {
2066public:
2067 GestureClick(SkView* target) : SkView::Click(target) {
2068 this->setType(gGestureClickType);
2069 }
2070
2071 static bool IsGesture(Click* click) {
2072 return click->isType(gGestureClickType);
2073 }
2074};
2075
reed@google.com4d5c26d2013-01-08 16:17:50 +00002076SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y,
2077 unsigned modi) {
reed@google.com52f57e12011-03-16 12:10:02 +00002078 return new GestureClick(this);
2079}
2080
2081bool SampleWindow::onClick(Click* click) {
2082 if (GestureClick::IsGesture(click)) {
bsalomon@google.com4d4f2812011-12-12 18:34:01 +00002083 float x = static_cast<float>(click->fICurr.fX);
2084 float y = static_cast<float>(click->fICurr.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00002085
reed@google.com52f57e12011-03-16 12:10:02 +00002086 switch (click->fState) {
2087 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00002088 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00002089 break;
2090 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00002091 fGesture.touchMoved(click->fOwner, x, y);
reed@google.comf03bb562011-11-11 21:42:12 +00002092 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002093 break;
2094 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00002095 fGesture.touchEnd(click->fOwner);
reed@google.comf03bb562011-11-11 21:42:12 +00002096 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002097 break;
2098 }
2099 return true;
2100 }
2101 return false;
2102}
2103
2104///////////////////////////////////////////////////////////////////////////////
2105
reed@android.com8a1c16f2008-12-17 15:59:43 +00002106void SampleWindow::loadView(SkView* view) {
2107 SkView::F2BIter iter(this);
2108 SkView* prev = iter.next();
2109 if (prev) {
2110 prev->detachFromParent();
2111 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002112
reed@android.com8a1c16f2008-12-17 15:59:43 +00002113 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00002114 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002115 this->attachChildToFront(view)->unref();
2116 view->setSize(this->width(), this->height());
2117
yangsu@google.com921091f2011-08-02 13:39:12 +00002118 //repopulate the slide menu when a view is loaded
scroggo@google.com7dadc742012-04-18 14:07:57 +00002119 fSlideMenu->reset();
djsollen@google.com388974f2013-01-17 13:20:01 +00002120
scroggo@google.comb073d922012-06-08 15:35:03 +00002121 (void)SampleView::SetUsePipe(view, fPipeState);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00002122 if (SampleView::IsSampleView(view)) {
2123 SampleView* sampleView = (SampleView*)view;
2124 sampleView->requestMenu(fSlideMenu);
2125 sampleView->onTileSizeChanged(this->tileSize());
2126 }
scroggo@google.com7dadc742012-04-18 14:07:57 +00002127 this->onUpdateMenu(fSlideMenu);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002128 this->updateTitle();
2129}
2130
2131static const char* gConfigNames[] = {
2132 "unknown config",
reed@android.com8a1c16f2008-12-17 15:59:43 +00002133 "A8",
2134 "Index8",
2135 "565",
2136 "4444",
2137 "8888"
2138};
2139
2140static const char* configToString(SkBitmap::Config c) {
2141 return gConfigNames[c];
2142}
2143
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002144static const char* gDeviceTypePrefix[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002145 "raster: ",
2146 "picture: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002147#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00002148 "opengl: ",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00002149#if SK_ANGLE
2150 "angle: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002151#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +00002152 "null-gl: "
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002153#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +00002154};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002155SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gDeviceTypePrefix) == SampleWindow::kDeviceTypeCnt,
2156 array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002157
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002158static const char* trystate_str(SkOSMenu::TriState state,
reed@google.com569e0432011-04-05 13:07:03 +00002159 const char trueStr[], const char falseStr[]) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002160 if (SkOSMenu::kOnState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002161 return trueStr;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002162 } else if (SkOSMenu::kOffState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002163 return falseStr;
2164 }
2165 return NULL;
2166}
2167
reed@android.com8a1c16f2008-12-17 15:59:43 +00002168void SampleWindow::updateTitle() {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002169 SkView* view = curr_view(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002170
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002171 SkString title;
2172 if (!curr_title(this, &title)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002173 title.set("<unknown>");
2174 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002175
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002176 title.prepend(gDeviceTypePrefix[fDeviceType]);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002177
2178 title.prepend(" ");
2179 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002180
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00002181 if (fTilingMode != kNo_Tiling) {
2182 title.prependf("<T: %s> ", gTilingInfo[fTilingMode].label);
2183 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002184 if (fAnimating) {
2185 title.prepend("<A> ");
2186 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00002187 if (fRotate) {
2188 title.prepend("<R> ");
2189 }
reed@android.come522ca52009-11-23 20:10:41 +00002190 if (fNClip) {
2191 title.prepend("<C> ");
2192 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00002193 if (fPerspAnim) {
2194 title.prepend("<K> ");
2195 }
reed@google.com569e0432011-04-05 13:07:03 +00002196
2197 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
2198 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com15bc13d2013-12-10 16:53:13 +00002199 title.prepend(gFilterLevelStates[fFilterLevelIndex].fLabel);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002200 title.prepend(trystate_str(fSubpixelState, "S ", "s "));
reed@google.com569e0432011-04-05 13:07:03 +00002201 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
2202 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002203 title.prepend(gHintingStates[fHintingState].label);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002204
2205 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00002206 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002207 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002208
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002209 if (fMeasureFPS) {
reed@google.com43e10142012-09-10 11:52:52 +00002210 title.appendf(" %8.3f ms", fMeasureFPS_Time / (float)FPS_REPEAT_COUNT);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002211 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002212 if (SampleView::IsSampleView(view)) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002213 switch (fPipeState) {
2214 case SkOSMenu::kOnState:
2215 title.prepend("<Pipe> ");
2216 break;
2217 case SkOSMenu::kMixedState:
2218 title.prepend("<Tiled Pipe> ");
2219 break;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002220
scroggo@google.comb073d922012-06-08 15:35:03 +00002221 default:
2222 break;
2223 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002224 title.prepend("! ");
2225 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002226
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002227#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00002228 if (IsGpuDeviceType(fDeviceType) &&
robertphillips@google.com4750fa52012-04-10 13:34:11 +00002229 NULL != fDevManager &&
bsalomon@google.com82502e22013-01-24 20:47:18 +00002230 fDevManager->getGrRenderTarget() &&
bsalomon@google.com11959252012-04-06 20:13:38 +00002231 fDevManager->getGrRenderTarget()->numSamples() > 0) {
2232 title.appendf(" [MSAA: %d]",
2233 fDevManager->getGrRenderTarget()->numSamples());
2234 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002235#endif
bsalomon@google.com11959252012-04-06 20:13:38 +00002236
reed@android.com8a1c16f2008-12-17 15:59:43 +00002237 this->setTitle(title.c_str());
2238}
2239
2240void SampleWindow::onSizeChange() {
2241 this->INHERITED::onSizeChange();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002242
reed@android.com8a1c16f2008-12-17 15:59:43 +00002243 SkView::F2BIter iter(this);
2244 SkView* view = iter.next();
2245 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002246
reed@android.com8a1c16f2008-12-17 15:59:43 +00002247 // rebuild our clippath
2248 {
2249 const SkScalar W = this->width();
2250 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002251
reed@android.com8a1c16f2008-12-17 15:59:43 +00002252 fClipPath.reset();
2253#if 0
2254 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
2255 SkRect r;
2256 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
2257 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
2258 fClipPath.addRect(r);
2259 }
2260#else
2261 SkRect r;
2262 r.set(0, 0, W, H);
2263 fClipPath.addRect(r, SkPath::kCCW_Direction);
2264 r.set(W/4, H/4, W*3/4, H*3/4);
2265 fClipPath.addRect(r, SkPath::kCW_Direction);
2266#endif
2267 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002268
Scroggo2c8208f2011-06-15 16:49:08 +00002269 fZoomCenterX = SkScalarHalf(this->width());
2270 fZoomCenterY = SkScalarHalf(this->height());
2271
djsollen@google.com56c69772011-11-08 19:00:26 +00002272#ifdef SK_BUILD_FOR_ANDROID
Scroggob4490c72011-06-17 13:53:05 +00002273 // FIXME: The first draw after a size change does not work on Android, so
2274 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00002275 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00002276#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00002277 this->updateTitle(); // to refresh our config
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002278 fDevManager->windowSizeChanged(this);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00002279
2280 if (fTilingMode != kNo_Tiling && SampleView::IsSampleView(view)) {
2281 ((SampleView*)view)->onTileSizeChanged(this->tileSize());
2282 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002283}
2284
2285///////////////////////////////////////////////////////////////////////////////
2286
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002287static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002288static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00002289static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002290
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002291bool SampleView::IsSampleView(SkView* view) {
2292 SkEvent evt(is_sample_view_tag);
2293 return view->doQuery(&evt);
2294}
2295
reed@google.comf2183392011-04-22 14:10:48 +00002296bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002297 SkEvent evt(repeat_count_tag);
2298 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00002299 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002300}
2301
scroggo@google.comb073d922012-06-08 15:35:03 +00002302bool SampleView::SetUsePipe(SkView* view, SkOSMenu::TriState state) {
2303 SkEvent evt;
2304 evt.setS32(set_use_pipe_tag, state);
reed@google.com0faac1e2011-05-11 05:58:58 +00002305 return view->doEvent(evt);
2306}
2307
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002308bool SampleView::onEvent(const SkEvent& evt) {
2309 if (evt.isType(repeat_count_tag)) {
2310 fRepeatCount = evt.getFast32();
2311 return true;
2312 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002313
scroggo@google.comb073d922012-06-08 15:35:03 +00002314 int32_t pipeHolder;
2315 if (evt.findS32(set_use_pipe_tag, &pipeHolder)) {
2316 fPipeState = static_cast<SkOSMenu::TriState>(pipeHolder);
reed@google.com0faac1e2011-05-11 05:58:58 +00002317 return true;
2318 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002319
2320 if (evt.isType("debug-hit-test")) {
2321 fDebugHitTest = true;
2322 evt.findS32("debug-hit-test-x", &fDebugHitTestLoc.fX);
2323 evt.findS32("debug-hit-test-y", &fDebugHitTestLoc.fY);
2324 this->inval(NULL);
2325 return true;
2326 }
2327
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002328 return this->INHERITED::onEvent(evt);
2329}
2330
2331bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002332 if (evt->isType(is_sample_view_tag)) {
2333 return true;
2334 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002335 return this->INHERITED::onQuery(evt);
2336}
2337
reed@google.com64e3eb22011-05-04 14:32:04 +00002338
2339class SimplePC : public SkGPipeController {
2340public:
2341 SimplePC(SkCanvas* target);
2342 ~SimplePC();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002343
reed@google.com64e3eb22011-05-04 14:32:04 +00002344 virtual void* requestBlock(size_t minRequest, size_t* actual);
2345 virtual void notifyWritten(size_t bytes);
2346
2347private:
reed@google.com961ddb02011-05-05 14:03:48 +00002348 SkGPipeReader fReader;
2349 void* fBlock;
2350 size_t fBlockSize;
2351 size_t fBytesWritten;
2352 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00002353 SkGPipeReader::Status fStatus;
2354
2355 size_t fTotalWritten;
2356};
2357
2358SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
2359 fBlock = NULL;
2360 fBlockSize = fBytesWritten = 0;
2361 fStatus = SkGPipeReader::kDone_Status;
2362 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00002363 fAtomsWritten = 0;
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00002364 fReader.setBitmapDecoder(&SkImageDecoder::DecodeMemory);
reed@google.com64e3eb22011-05-04 14:32:04 +00002365}
2366
2367SimplePC::~SimplePC() {
2368// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
reed@google.com0faac1e2011-05-11 05:58:58 +00002369 if (fTotalWritten) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002370 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
2371 fAtomsWritten, fStatus);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002372#ifdef PIPE_FILE
scroggo@google.comb073d922012-06-08 15:35:03 +00002373 //File is open in append mode
2374 FILE* f = fopen(FILE_PATH, "ab");
2375 SkASSERT(f != NULL);
2376 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
2377 fclose(f);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002378#endif
2379#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00002380 if (fAtomsWritten > 1 && fTotalWritten > 4) { //ignore done
2381 gServer.acceptConnections();
2382 gServer.writePacket(fBlock, fTotalWritten);
2383 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002384#endif
reed@google.com0faac1e2011-05-11 05:58:58 +00002385 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002386 sk_free(fBlock);
reed@google.com64e3eb22011-05-04 14:32:04 +00002387}
2388
2389void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
2390 sk_free(fBlock);
2391
2392 fBlockSize = minRequest * 4;
2393 fBlock = sk_malloc_throw(fBlockSize);
2394 fBytesWritten = 0;
2395 *actual = fBlockSize;
2396 return fBlock;
2397}
2398
2399void SimplePC::notifyWritten(size_t bytes) {
2400 SkASSERT(fBytesWritten + bytes <= fBlockSize);
reed@google.com64e3eb22011-05-04 14:32:04 +00002401 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
2402 SkASSERT(SkGPipeReader::kError_Status != fStatus);
2403 fBytesWritten += bytes;
2404 fTotalWritten += bytes;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002405
reed@google.com961ddb02011-05-05 14:03:48 +00002406 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00002407}
2408
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002409void SampleView::draw(SkCanvas* canvas) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002410 if (SkOSMenu::kOffState == fPipeState) {
2411 this->INHERITED::draw(canvas);
2412 } else {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002413 SkGPipeWriter writer;
2414 SimplePC controller(canvas);
scroggo@google.comb073d922012-06-08 15:35:03 +00002415 TiledPipeController tc(canvas->getDevice()->accessBitmap(false),
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00002416 &SkImageDecoder::DecodeMemory,
scroggo@google.comb073d922012-06-08 15:35:03 +00002417 &canvas->getTotalMatrix());
2418 SkGPipeController* pc;
2419 if (SkOSMenu::kMixedState == fPipeState) {
2420 pc = &tc;
2421 } else {
2422 pc = &controller;
2423 }
reed@google.comdde09562011-05-23 12:21:05 +00002424 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002425
scroggo@google.comb073d922012-06-08 15:35:03 +00002426 canvas = writer.startRecording(pc, flags);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002427 //Must draw before controller goes out of scope and sends data
2428 this->INHERITED::draw(canvas);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00002429 //explicitly end recording to ensure writer is flushed before the memory
2430 //is freed in the deconstructor of the controller
2431 writer.endRecording();
reed@google.com0faac1e2011-05-11 05:58:58 +00002432 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002433}
reed@google.com4d5c26d2013-01-08 16:17:50 +00002434
2435#include "SkBounder.h"
2436
2437class DebugHitTestBounder : public SkBounder {
2438public:
2439 DebugHitTestBounder(int x, int y) {
2440 fLoc.set(x, y);
2441 }
2442
2443 virtual bool onIRect(const SkIRect& bounds) SK_OVERRIDE {
2444 if (bounds.contains(fLoc.x(), fLoc.y())) {
2445 //
2446 // Set a break-point here to see what was being drawn under
2447 // the click point (just needed a line of code to stop the debugger)
2448 //
2449 bounds.centerX();
2450 }
2451 return true;
2452 }
2453
2454private:
2455 SkIPoint fLoc;
2456 typedef SkBounder INHERITED;
2457};
2458
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002459void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com81e3d7f2011-06-01 12:42:36 +00002460 this->onDrawBackground(canvas);
2461
reed@google.com4d5c26d2013-01-08 16:17:50 +00002462 DebugHitTestBounder bounder(fDebugHitTestLoc.x(), fDebugHitTestLoc.y());
2463 if (fDebugHitTest) {
2464 canvas->setBounder(&bounder);
2465 }
2466
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002467 for (int i = 0; i < fRepeatCount; i++) {
scroggo@google.com85cade02012-08-17 13:29:50 +00002468 SkAutoCanvasRestore acr(canvas, true);
2469 this->onDrawContent(canvas);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002470 }
reed@google.com4d5c26d2013-01-08 16:17:50 +00002471
2472 fDebugHitTest = false;
2473 canvas->setBounder(NULL);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002474}
2475
2476void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00002477 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002478}
2479
2480///////////////////////////////////////////////////////////////////////////////
2481
reed@android.comf2b98d62010-12-20 18:26:13 +00002482template <typename T> void SkTBSort(T array[], int count) {
2483 for (int i = 1; i < count - 1; i++) {
2484 bool didSwap = false;
2485 for (int j = count - 1; j > i; --j) {
2486 if (array[j] < array[j-1]) {
2487 T tmp(array[j-1]);
2488 array[j-1] = array[j];
2489 array[j] = tmp;
2490 didSwap = true;
2491 }
2492 }
2493 if (!didSwap) {
2494 break;
2495 }
2496 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002497
reed@android.comf2b98d62010-12-20 18:26:13 +00002498 for (int k = 0; k < count - 1; k++) {
2499 SkASSERT(!(array[k+1] < array[k]));
2500 }
2501}
2502
2503#include "SkRandom.h"
2504
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002505static void rand_rect(SkIRect* rect, SkRandom& rand) {
reed@android.comf2b98d62010-12-20 18:26:13 +00002506 int bits = 8;
2507 int shift = 32 - bits;
2508 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
2509 rand.nextU() >> shift, rand.nextU() >> shift);
2510 rect->sort();
2511}
2512
2513static void dumpRect(const SkIRect& r) {
2514 SkDebugf(" { %d, %d, %d, %d },\n",
2515 r.fLeft, r.fTop,
2516 r.fRight, r.fBottom);
2517}
2518
2519static void test_rects(const SkIRect rect[], int count) {
2520 SkRegion rgn0, rgn1;
2521
2522 for (int i = 0; i < count; i++) {
2523 rgn0.op(rect[i], SkRegion::kUnion_Op);
2524 // dumpRect(rect[i]);
2525 }
2526 rgn1.setRects(rect, count);
2527
2528 if (rgn0 != rgn1) {
2529 SkDebugf("\n");
2530 for (int i = 0; i < count; i++) {
2531 dumpRect(rect[i]);
2532 }
2533 SkDebugf("\n");
2534 }
2535}
2536
2537static void test() {
2538 size_t i;
2539
2540 const SkIRect r0[] = {
2541 { 0, 0, 1, 1 },
2542 { 2, 2, 3, 3 },
2543 };
2544 const SkIRect r1[] = {
2545 { 0, 0, 1, 3 },
2546 { 1, 1, 2, 2 },
2547 { 2, 0, 3, 3 },
2548 };
2549 const SkIRect r2[] = {
2550 { 0, 0, 1, 2 },
2551 { 2, 1, 3, 3 },
2552 { 4, 0, 5, 1 },
2553 { 6, 0, 7, 4 },
2554 };
2555
2556 static const struct {
2557 const SkIRect* fRects;
2558 int fCount;
2559 } gRecs[] = {
2560 { r0, SK_ARRAY_COUNT(r0) },
2561 { r1, SK_ARRAY_COUNT(r1) },
2562 { r2, SK_ARRAY_COUNT(r2) },
2563 };
2564
2565 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
2566 test_rects(gRecs[i].fRects, gRecs[i].fCount);
2567 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002568
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002569 SkRandom rand;
reed@android.comf2b98d62010-12-20 18:26:13 +00002570 for (i = 0; i < 10000; i++) {
2571 SkRegion rgn0, rgn1;
2572
2573 const int N = 8;
2574 SkIRect rect[N];
2575 for (int j = 0; j < N; j++) {
2576 rand_rect(&rect[j], rand);
2577 }
2578 test_rects(rect, N);
2579 }
2580}
2581
caryclark@google.com02939ce2012-06-06 12:09:51 +00002582// FIXME: this should be in a header
2583SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00002584SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
caryclark@google.com02939ce2012-06-06 12:09:51 +00002585 if (false) { // avoid bit rot, suppress warning
2586 test();
2587 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002588 return new SampleWindow(hwnd, argc, argv, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002589}
2590
caryclark@google.com02939ce2012-06-06 12:09:51 +00002591// FIXME: this should be in a header
2592void get_preferred_size(int* x, int* y, int* width, int* height);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002593void get_preferred_size(int* x, int* y, int* width, int* height) {
2594 *x = 10;
2595 *y = 50;
2596 *width = 640;
2597 *height = 480;
2598}
2599
caryclark@google.com5987f582012-10-02 18:33:14 +00002600#ifdef SK_BUILD_FOR_IOS
2601void save_args(int argc, char *argv[]) {
2602}
2603#endif
2604
caryclark@google.com02939ce2012-06-06 12:09:51 +00002605// FIXME: this should be in a header
2606void application_init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002607void application_init() {
2608// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002609#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00002610 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002611#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002612 SkGraphics::Init();
2613 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002614}
2615
caryclark@google.com02939ce2012-06-06 12:09:51 +00002616// FIXME: this should be in a header
2617void application_term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002618void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002619 SkEvent::Term();
2620 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002621}