blob: 716998f9600dde0a76db0ff7892cc42f2549d285 [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"
reed@android.comb08eb2b2009-01-06 20:16:26 +000013#include "SkImageEncoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkPaint.h"
15#include "SkPicture.h"
16#include "SkStream.h"
reed@android.com44177402009-11-23 21:07:51 +000017#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkWindow.h"
19
20#include "SampleCode.h"
Scroggo0f185c22011-03-24 18:35:50 +000021#include "SkTypeface.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000022
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000023#if SK_SUPPORT_GPU
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000024#include "gl/GrGLInterface.h"
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000025#include "gl/GrGLUtil.h"
bsalomon@google.com583a1e32011-08-17 13:42:46 +000026#include "GrRenderTarget.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000027#include "GrContext.h"
28#include "SkGpuDevice.h"
29#else
30class GrContext;
31#endif
Scroggo2c8208f2011-06-15 16:49:08 +000032
reed@google.com1830c7a2012-06-04 12:05:43 +000033#include "SkOSFile.h"
Scroggo8ac0d542011-06-21 14:44:57 +000034#include "SkPDFDevice.h"
35#include "SkPDFDocument.h"
36#include "SkStream.h"
37
scroggo@google.comb073d922012-06-08 15:35:03 +000038#include "SkGPipe.h"
39#include "SamplePipeControllers.h"
tfarina@chromium.orgb1b7f7072012-09-18 01:52:20 +000040#include "OverView.h"
epoger@google.com6a121782012-09-14 18:42:01 +000041#include "TransitionView.h"
scroggo@google.comb073d922012-06-08 15:35:03 +000042
robertphillips@google.coma22e2112012-08-16 14:58:06 +000043SK_DEFINE_INST_COUNT(SampleWindow::DeviceManager)
44
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
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000056#define PIPE_FILEx
57#ifdef PIPE_FILE
yangsu@google.com1f394212011-06-01 18:03:34 +000058#define FILE_PATH "/path/to/drawing.data"
59#endif
60
yangsu@google.comf121b052011-08-08 16:02:51 +000061#define PIPE_NETx
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000062#ifdef PIPE_NET
63#include "SkSockets.h"
64SkTCPServer gServer;
65#endif
yangsu@google.comef7bdfa2011-08-12 14:27:47 +000066
67#define DEBUGGERx
68#ifdef DEBUGGER
69extern SkView* create_debugger(const char* data, size_t size);
70extern bool is_debugger(SkView* view);
71SkTDArray<char> gTempDataStore;
72#endif
73
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000074
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +000075#define USE_ARROWS_FOR_ZOOM true
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000076
77#if SK_ANGLE
78//#define DEFAULT_TO_ANGLE 1
79#else
80//#define DEFAULT_TO_GPU 1
81#endif
reed@android.comf2b98d62010-12-20 18:26:13 +000082
reed@android.com8a1c16f2008-12-17 15:59:43 +000083#define ANIMATING_EVENTTYPE "nextSample"
84#define ANIMATING_DELAY 750
85
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000086#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +000087 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000088#else
reed@google.combad8c872011-05-18 20:10:31 +000089 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000090#endif
reed@google.combad8c872011-05-18 20:10:31 +000091#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000092
reed@google.com3cec4d72011-07-06 13:59:47 +000093static SampleWindow* gSampleWindow;
94
reed@google.coma4f81372012-11-15 15:56:38 +000095static bool gShowGMBounds;
96
reed@google.com2072db82011-11-08 15:18:10 +000097static void postEventToSink(SkEvent* evt, SkEventSink* sink) {
98 evt->setTargetID(sink->getSinkID())->post();
99}
100
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000101///////////////////////////////////////////////////////////////////////////////
102
103static const char* skip_until(const char* str, const char* skip) {
104 if (!str) {
105 return NULL;
106 }
107 return strstr(str, skip);
108}
109
110static const char* skip_past(const char* str, const char* skip) {
111 const char* found = skip_until(str, skip);
112 if (!found) {
113 return NULL;
114 }
115 return found + strlen(skip);
116}
117
118static const char* gPrefFileName = "sampleapp_prefs.txt";
119
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000120static bool readTitleFromPrefs(SkString* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000121 SkFILEStream stream(gPrefFileName);
122 if (!stream.isValid()) {
123 return false;
124 }
125
126 int len = stream.getLength();
127 SkString data(len);
128 stream.read(data.writable_str(), len);
129 const char* s = data.c_str();
130
131 s = skip_past(s, "curr-slide-title");
132 s = skip_past(s, "=");
133 s = skip_past(s, "\"");
134 const char* stop = skip_until(s, "\"");
135 if (stop > s) {
136 title->set(s, stop - s);
137 return true;
138 }
139 return false;
140}
141
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000142static void writeTitleToPrefs(const char* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000143 SkFILEWStream stream(gPrefFileName);
144 SkString data;
145 data.printf("curr-slide-title = \"%s\"\n", title);
146 stream.write(data.c_str(), data.size());
147}
148
149///////////////////////////////////////////////////////////////////////////////
150
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000151class SampleWindow::DefaultDeviceManager : public SampleWindow::DeviceManager {
152public:
153
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000154 DefaultDeviceManager() {
155#if SK_SUPPORT_GPU
156 fCurContext = NULL;
157 fCurIntf = NULL;
158 fCurRenderTarget = NULL;
159 fMSAASampleCount = 0;
160#endif
161 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000162 }
163
164 virtual ~DefaultDeviceManager() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000165#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000166 SkSafeUnref(fCurContext);
167 SkSafeUnref(fCurIntf);
168 SkSafeUnref(fCurRenderTarget);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000169#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000170 }
171
bsalomon@google.com11959252012-04-06 20:13:38 +0000172 virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000173 SkASSERT(kNone_BackEndType == fBackend);
robertphillips@google.com53e96a12012-03-30 14:47:53 +0000174
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000175 fBackend = kNone_BackEndType;
176
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000177#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000178 switch (win->getDeviceType()) {
179 case kRaster_DeviceType:
180 // fallthrough
181 case kPicture_DeviceType:
182 // fallthrough
183 case kGPU_DeviceType:
184 // fallthrough
185 case kNullGPU_DeviceType:
186 // all these guys use the native backend
187 fBackend = kNativeGL_BackEndType;
188 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000189#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000190 case kANGLE_DeviceType:
191 // ANGLE is really the only odd man out
192 fBackend = kANGLE_BackEndType;
193 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000194#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000195 default:
196 SkASSERT(false);
197 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000198 }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000199
bsalomon@google.com11959252012-04-06 20:13:38 +0000200 bool result = win->attach(fBackend, msaaSampleCount);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000201 if (!result) {
bsalomon@google.com74913722011-10-27 20:44:19 +0000202 SkDebugf("Failed to initialize GL");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000203 return;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000204 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000205 fMSAASampleCount = msaaSampleCount;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000206
207 SkASSERT(NULL == fCurIntf);
208 switch (win->getDeviceType()) {
209 case kRaster_DeviceType:
210 // fallthrough
211 case kPicture_DeviceType:
212 // fallthrough
213 case kGPU_DeviceType:
214 // all these guys use the native interface
215 fCurIntf = GrGLCreateNativeInterface();
216 break;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000217#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000218 case kANGLE_DeviceType:
219 fCurIntf = GrGLCreateANGLEInterface();
220 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000221#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000222 case kNullGPU_DeviceType:
223 fCurIntf = GrGLCreateNullInterface();
224 break;
225 default:
226 SkASSERT(false);
227 break;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000228 }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000229
230 SkASSERT(NULL == fCurContext);
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000231 fCurContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000232
233 if (NULL == fCurContext || NULL == fCurIntf) {
234 // We need some context and interface to see results
235 SkSafeUnref(fCurContext);
236 SkSafeUnref(fCurIntf);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000237 SkDebugf("Failed to setup 3D");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000238
239 win->detach();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000240 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000241#endif // SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000242 // call windowSizeChanged to create the render target
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000243 this->windowSizeChanged(win);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000244 }
245
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000246 virtual void tearDownBackend(SampleWindow *win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000247#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000248 SkSafeUnref(fCurContext);
249 fCurContext = NULL;
250
251 SkSafeUnref(fCurIntf);
252 fCurIntf = NULL;
253
254 SkSafeUnref(fCurRenderTarget);
255 fCurRenderTarget = NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000256#endif
bsalomon@google.comddd40e52012-04-10 15:56:29 +0000257 win->detach();
258 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000259 }
260
reed@google.com5957f472012-10-01 20:31:56 +0000261 virtual SkCanvas* createCanvas(SampleWindow::DeviceType dType,
262 SampleWindow* win) {
bsalomon@google.com74913722011-10-27 20:44:19 +0000263 switch (dType) {
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000264 case kRaster_DeviceType:
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000265 // fallthrough
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000266 case kPicture_DeviceType:
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000267 // fallthrough
268#if SK_ANGLE
269 case kANGLE_DeviceType:
270#endif
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000271 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000272#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000273 case kGPU_DeviceType:
274 case kNullGPU_DeviceType:
275 if (fCurContext) {
reed@google.com5957f472012-10-01 20:31:56 +0000276 SkAutoTUnref<SkDevice> device(new SkGpuDevice(fCurContext,
277 fCurRenderTarget));
278 return new SkCanvas(device);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000279 } else {
reed@google.com5957f472012-10-01 20:31:56 +0000280 return NULL;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000281 }
282 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000283#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000284 default:
285 SkASSERT(false);
reed@google.com5957f472012-10-01 20:31:56 +0000286 return NULL;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000287 }
reed@google.com5957f472012-10-01 20:31:56 +0000288 return NULL;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000289 }
290
291 virtual void publishCanvas(SampleWindow::DeviceType dType,
292 SkCanvas* canvas,
293 SampleWindow* win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000294#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000295 if (fCurContext) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000296 // in case we have queued drawing calls
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000297 fCurContext->flush();
298
299 if (kGPU_DeviceType != dType && kNullGPU_DeviceType != dType) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000300 // need to send the raster bits to the (gpu) window
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000301 fCurContext->setRenderTarget(fCurRenderTarget);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000302 const SkBitmap& bm = win->getBitmap();
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000303 fCurRenderTarget->writePixels(0, 0, bm.width(), bm.height(),
bsalomon@google.com6f379512011-11-16 20:36:03 +0000304 kSkia8888_PM_GrPixelConfig,
305 bm.getPixels(),
306 bm.rowBytes());
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000307 }
308 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000309#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000310
311 win->present();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000312 }
313
314 virtual void windowSizeChanged(SampleWindow* win) {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000315#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000316 if (fCurContext) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000317 win->attach(fBackend, fMSAASampleCount);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000318
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000319 GrBackendRenderTargetDesc desc;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000320 desc.fWidth = SkScalarRound(win->width());
321 desc.fHeight = SkScalarRound(win->height());
bsalomon@google.comc4364992011-11-07 15:54:49 +0000322 desc.fConfig = kSkia8888_PM_GrPixelConfig;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000323 GR_GL_GetIntegerv(fCurIntf, GR_GL_SAMPLES, &desc.fSampleCnt);
324 GR_GL_GetIntegerv(fCurIntf, GR_GL_STENCIL_BITS, &desc.fStencilBits);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000325 GrGLint buffer;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000326 GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
bsalomon@google.come269f212011-11-07 13:29:52 +0000327 desc.fRenderTargetHandle = buffer;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000328
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000329 SkSafeUnref(fCurRenderTarget);
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000330 fCurRenderTarget = fCurContext->wrapBackendRenderTarget(desc);
bsalomon@google.com74913722011-10-27 20:44:19 +0000331 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000332#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000333 }
334
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000335 virtual GrContext* getGrContext() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000336#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000337 return fCurContext;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000338#else
339 return NULL;
340#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000341 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000342
343 virtual GrRenderTarget* getGrRenderTarget() SK_OVERRIDE {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000344#if SK_SUPPORT_GPU
bsalomon@google.com11959252012-04-06 20:13:38 +0000345 return fCurRenderTarget;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000346#else
347 return NULL;
348#endif
bsalomon@google.com11959252012-04-06 20:13:38 +0000349 }
350
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000351private:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000352
353#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000354 GrContext* fCurContext;
355 const GrGLInterface* fCurIntf;
356 GrRenderTarget* fCurRenderTarget;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000357 int fMSAASampleCount;
358#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000359
360 SkOSWindow::SkBackEndTypes fBackend;
361
362 typedef SampleWindow::DeviceManager INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000363};
364
365///////////////
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000366static const char view_inval_msg[] = "view-inval-msg";
367
Scroggo62b65b02011-06-21 16:01:26 +0000368void SampleWindow::postInvalDelay() {
reed@google.com87fac4a2011-08-04 13:50:17 +0000369 (new SkEvent(view_inval_msg, this->getSinkID()))->postDelay(1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000370}
371
372static bool isInvalEvent(const SkEvent& evt) {
373 return evt.isType(view_inval_msg);
374}
375//////////////////
376
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000377SkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func)
378 : fCreateFunc(func) {
379}
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000380
bsalomon@google.com8301de12011-10-31 16:47:13 +0000381SkView* SkFuncViewFactory::operator() () const {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000382 return (*fCreateFunc)();
383}
384
385#include "GMSampleView.h"
386
387SkGMSampleViewFactory::SkGMSampleViewFactory(GMFactoryFunc func)
388 : fFunc(func) {
389}
390
391SkView* SkGMSampleViewFactory::operator() () const {
392 return new GMSampleView(fFunc(NULL));
393}
394
395SkViewRegister* SkViewRegister::gHead;
396SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) {
397 fFact->ref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398 fChain = gHead;
399 gHead = this;
400}
401
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000402SkViewRegister::SkViewRegister(SkViewCreateFunc func) {
403 fFact = new SkFuncViewFactory(func);
404 fChain = gHead;
405 gHead = this;
406}
407
408SkViewRegister::SkViewRegister(GMFactoryFunc func) {
409 fFact = new SkGMSampleViewFactory(func);
410 fChain = gHead;
411 gHead = this;
412}
413
414class AutoUnrefArray {
415public:
416 AutoUnrefArray() {}
417 ~AutoUnrefArray() {
418 int count = fObjs.count();
419 for (int i = 0; i < count; ++i) {
420 fObjs[i]->unref();
421 }
422 }
423 SkRefCnt*& push_back() { return *fObjs.append(); }
chudy@google.com4605a3f2012-08-01 17:58:01 +0000424
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000425private:
426 SkTDArray<SkRefCnt*> fObjs;
427};
428
429// registers GMs as Samples
430// This can't be performed during static initialization because it could be
431// run before GMRegistry has been fully built.
caryclark@google.com02939ce2012-06-06 12:09:51 +0000432static void SkGMRegistyToSampleRegistry() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000433 static bool gOnce;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000434 static AutoUnrefArray fRegisters;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000435
436 if (!gOnce) {
437 const skiagm::GMRegistry* gmreg = skiagm::GMRegistry::Head();
438 while (gmreg) {
439 fRegisters.push_back() = new SkViewRegister(gmreg->factory());
440 gmreg = gmreg->next();
441 }
442 gOnce = true;
443 }
444}
445
reed@google.com29038ed2011-07-06 17:56:47 +0000446#if 0
reed@google.comf0b5f682011-03-11 20:08:25 +0000447#include <CoreFoundation/CoreFoundation.h>
448#include <CoreFoundation/CFURLAccess.h>
449
450static void testpdf() {
451 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
452 kCFStringEncodingUTF8);
453 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
454 kCFURLPOSIXPathStyle,
455 false);
456 CFRelease(path);
457 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
458 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
459 CFRelease(url);
460
461 CGContextBeginPage(cg, &box);
462 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
463 CGContextFillEllipseInRect(cg, r);
464 CGContextEndPage(cg);
465 CGContextRelease(cg);
466
467 if (false) {
468 SkBitmap bm;
469 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
470 bm.allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +0000471 bm.eraseColor(SK_ColorTRANSPARENT);
reed@google.comf0b5f682011-03-11 20:08:25 +0000472
473 SkCanvas canvas(bm);
474
475 }
476}
477#endif
478
479//////////////////////////////////////////////////////////////////////////////
480
reed@google.com569e0432011-04-05 13:07:03 +0000481enum FlipAxisEnum {
482 kFlipAxis_X = (1 << 0),
483 kFlipAxis_Y = (1 << 1)
484};
485
reed@google.comf0b5f682011-03-11 20:08:25 +0000486#include "SkDrawFilter.h"
487
reed@google.com569e0432011-04-05 13:07:03 +0000488class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000489public:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000490 FlagsDrawFilter(SkOSMenu::TriState lcd, SkOSMenu::TriState aa, SkOSMenu::TriState filter,
491 SkOSMenu::TriState hinting) :
reed@google.com09e3baa2011-05-18 12:04:31 +0000492 fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000493
reed@google.com971aca72012-11-26 20:26:54 +0000494 virtual bool filter(SkPaint* paint, Type t) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000495 if (kText_Type == t && SkOSMenu::kMixedState != fLCDState) {
496 paint->setLCDRenderText(SkOSMenu::kOnState == fLCDState);
reed@google.com569e0432011-04-05 13:07:03 +0000497 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000498 if (SkOSMenu::kMixedState != fAAState) {
499 paint->setAntiAlias(SkOSMenu::kOnState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000500 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000501 if (SkOSMenu::kMixedState != fFilterState) {
502 paint->setFilterBitmap(SkOSMenu::kOnState == fFilterState);
reed@google.com176753a2011-05-17 15:32:04 +0000503 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000504 if (SkOSMenu::kMixedState != fHintingState) {
505 paint->setHinting(SkOSMenu::kOnState == fHintingState ?
reed@google.com09e3baa2011-05-18 12:04:31 +0000506 SkPaint::kNormal_Hinting :
507 SkPaint::kSlight_Hinting);
508 }
reed@google.com971aca72012-11-26 20:26:54 +0000509 return true;
reed@google.comf0b5f682011-03-11 20:08:25 +0000510 }
511
512private:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000513 SkOSMenu::TriState fLCDState;
514 SkOSMenu::TriState fAAState;
515 SkOSMenu::TriState fFilterState;
516 SkOSMenu::TriState fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000517};
518
reed@android.com8a1c16f2008-12-17 15:59:43 +0000519//////////////////////////////////////////////////////////////////////////////
520
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000521#define MAX_ZOOM_LEVEL 8
522#define MIN_ZOOM_LEVEL -8
523
reed@android.comf2b98d62010-12-20 18:26:13 +0000524static const char gCharEvtName[] = "SampleCode_Char_Event";
525static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000526static const char gTitleEvtName[] = "SampleCode_Title_Event";
527static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000528static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
reed@google.com2072db82011-11-08 15:18:10 +0000529static const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle";
reed@android.comf2b98d62010-12-20 18:26:13 +0000530
531bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
532 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
533 if (outUni) {
534 *outUni = evt.getFast32();
535 }
536 return true;
537 }
538 return false;
539}
540
541bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
542 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
543 if (outKey) {
544 *outKey = (SkKey)evt.getFast32();
545 }
546 return true;
547 }
548 return false;
549}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000550
551bool SampleCode::TitleQ(const SkEvent& evt) {
552 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
553}
554
555void SampleCode::TitleR(SkEvent* evt, const char title[]) {
556 SkASSERT(evt && TitleQ(*evt));
557 evt->setString(gTitleEvtName, title);
558}
559
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000560bool SampleCode::RequestTitle(SkView* view, SkString* title) {
561 SkEvent evt(gTitleEvtName);
562 if (view->doQuery(&evt)) {
563 title->set(evt.findString(gTitleEvtName));
564 return true;
565 }
566 return false;
567}
568
reed@android.com8a1c16f2008-12-17 15:59:43 +0000569bool SampleCode::PrefSizeQ(const SkEvent& evt) {
570 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
571}
572
573void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
574 SkASSERT(evt && PrefSizeQ(*evt));
575 SkScalar size[2];
576 size[0] = width;
577 size[1] = height;
578 evt->setScalars(gPrefSizeEvtName, 2, size);
579}
580
reed@android.comf2b98d62010-12-20 18:26:13 +0000581bool SampleCode::FastTextQ(const SkEvent& evt) {
582 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
583}
584
585///////////////////////////////////////////////////////////////////////////////
586
reed@android.com44177402009-11-23 21:07:51 +0000587static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000588static SkMSec gAnimTimePrev;
589
reed@android.com44177402009-11-23 21:07:51 +0000590SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000591SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
592SkScalar SampleCode::GetAnimSecondsDelta() {
593 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
594}
reed@android.com44177402009-11-23 21:07:51 +0000595
596SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000597 // since gAnimTime can be up to 32 bits, we can't convert it to a float
598 // or we'll lose the low bits. Hence we use doubles for the intermediate
599 // calculations
600 double seconds = (double)gAnimTime / 1000.0;
601 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000602 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000603 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000604 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000605 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000606}
607
bsalomon@google.com85003222012-03-28 14:44:37 +0000608SkScalar SampleCode::GetAnimSinScalar(SkScalar amplitude,
609 SkScalar periodInSec,
610 SkScalar phaseInSec) {
611 if (!periodInSec) {
612 return 0;
613 }
614 double t = (double)gAnimTime / 1000.0 + phaseInSec;
615 t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
616 amplitude = SK_ScalarHalf * amplitude;
617 return SkScalarMul(amplitude, SkDoubleToScalar(sin(t))) + amplitude;
618}
619
reed@google.com3cec4d72011-07-06 13:59:47 +0000620GrContext* SampleCode::GetGr() {
621 return gSampleWindow ? gSampleWindow->getGrContext() : NULL;
622}
623
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000624// some GMs rely on having a skiagm::GetGr function defined
625namespace skiagm {
caryclark@google.com02939ce2012-06-06 12:09:51 +0000626 // FIXME: this should be moved into a header
627 GrContext* GetGr();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000628 GrContext* GetGr() { return SampleCode::GetGr(); }
629}
630
reed@android.com8a1c16f2008-12-17 15:59:43 +0000631//////////////////////////////////////////////////////////////////////////////
632
reed@android.comf2b98d62010-12-20 18:26:13 +0000633static SkView* curr_view(SkWindow* wind) {
634 SkView::F2BIter iter(wind);
635 return iter.next();
636}
637
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000638static bool curr_title(SkWindow* wind, SkString* title) {
639 SkView* view = curr_view(wind);
640 if (view) {
641 SkEvent evt(gTitleEvtName);
642 if (view->doQuery(&evt)) {
643 title->set(evt.findString(gTitleEvtName));
644 return true;
645 }
646 }
647 return false;
648}
649
Scroggo2c8208f2011-06-15 16:49:08 +0000650void SampleWindow::setZoomCenter(float x, float y)
651{
652 fZoomCenterX = SkFloatToScalar(x);
653 fZoomCenterY = SkFloatToScalar(y);
654}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000655
Scroggo0f185c22011-03-24 18:35:50 +0000656bool SampleWindow::zoomIn()
657{
658 // Arbitrarily decided
659 if (fFatBitsScale == 25) return false;
660 fFatBitsScale++;
661 this->inval(NULL);
662 return true;
663}
664
665bool SampleWindow::zoomOut()
666{
667 if (fFatBitsScale == 1) return false;
668 fFatBitsScale--;
669 this->inval(NULL);
670 return true;
671}
672
Scroggo0f185c22011-03-24 18:35:50 +0000673void SampleWindow::updatePointer(int x, int y)
674{
675 fMouseX = x;
676 fMouseY = y;
677 if (fShowZoomer) {
678 this->inval(NULL);
679 }
680}
681
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000682static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
683 static const SampleWindow::DeviceType gCT[] = {
684 SampleWindow::kPicture_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000685#if SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000686 SampleWindow::kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000687#if SK_ANGLE
688 SampleWindow::kANGLE_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000689#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +0000690 SampleWindow::kRaster_DeviceType, // skip the null gpu device in normal cycling
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000691#endif // SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000692 SampleWindow::kRaster_DeviceType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000693 };
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000694 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gCT) == SampleWindow::kDeviceTypeCnt, array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000695 return gCT[ct];
696}
697
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000698static void usage(const char * argv0) {
chudy@google.com4605a3f2012-08-01 17:58:01 +0000699 SkDebugf("%s [--slide sampleName] [-i resourcePath] [--msaa sampleCount] [--pictureDir dirPath] [--picture path]\n", argv0);
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000700 SkDebugf(" sampleName: sample at which to start.\n");
701 SkDebugf(" resourcePath: directory that stores image resources.\n");
bsalomon@google.com11959252012-04-06 20:13:38 +0000702 SkDebugf(" msaa: request multisampling with the given sample count.\n");
chudy@google.com4605a3f2012-08-01 17:58:01 +0000703 SkDebugf(" dirPath: path to directory skia pictures are read from\n");
704 SkDebugf(" path: path to skia picture\n");
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000705}
706
chudy@google.com4605a3f2012-08-01 17:58:01 +0000707SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager)
robertphillips@google.com4750fa52012-04-10 13:34:11 +0000708 : INHERITED(hwnd)
709 , fDevManager(NULL) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000710
reed@google.com1830c7a2012-06-04 12:05:43 +0000711 this->registerPictFileSamples(argv, argc);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000712 this->registerPictFileSample(argv, argc);
robertphillips@google.com76d40212012-03-22 14:12:15 +0000713 SkGMRegistyToSampleRegistry();
714 {
715 const SkViewRegister* reg = SkViewRegister::Head();
716 while (reg) {
717 *fSamples.append() = reg->factory();
718 reg = reg->next();
719 }
720 }
721
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000722 const char* resourcePath = NULL;
723 fCurrIndex = -1;
bsalomon@google.com11959252012-04-06 20:13:38 +0000724 fMSAASampleCount = 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000725
726 const char* const commandName = argv[0];
727 char* const* stop = argv + argc;
728 for (++argv; argv < stop; ++argv) {
729 if (strcmp(*argv, "-i") == 0) {
730 argv++;
731 if (argv < stop && **argv) {
732 resourcePath = *argv;
733 }
robertphillips@google.com76d40212012-03-22 14:12:15 +0000734 } else if (strcmp(*argv, "--slide") == 0) {
735 argv++;
736 if (argv < stop && **argv) {
737 fCurrIndex = findByTitle(*argv);
738 if (fCurrIndex < 0) {
739 fprintf(stderr, "Unknown sample \"%s\"\n", *argv);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000740 listTitles();
robertphillips@google.com76d40212012-03-22 14:12:15 +0000741 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000742 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000743 } else if (strcmp(*argv, "--msaa") == 0) {
744 ++argv;
745 if (argv < stop && **argv) {
746 fMSAASampleCount = atoi(*argv);
747 }
robertphillips@google.com7265e722012-05-03 18:22:28 +0000748 } else if (strcmp(*argv, "--list") == 0) {
749 listTitles();
bsalomon@google.com11959252012-04-06 20:13:38 +0000750 }
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000751 else {
robertphillips@google.com76d40212012-03-22 14:12:15 +0000752 usage(commandName);
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000753 }
754 }
755
756 if (fCurrIndex < 0) {
757 SkString title;
758 if (readTitleFromPrefs(&title)) {
759 fCurrIndex = findByTitle(title.c_str());
760 }
761 }
762
763 if (fCurrIndex < 0) {
764 fCurrIndex = 0;
765 }
766
reed@google.com3cec4d72011-07-06 13:59:47 +0000767 gSampleWindow = this;
768
yangsu@google.com1f394212011-06-01 18:03:34 +0000769#ifdef PIPE_FILE
770 //Clear existing file or create file if it doesn't exist
771 FILE* f = fopen(FILE_PATH, "wb");
772 fclose(f);
773#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +0000774
reed@android.com8a1c16f2008-12-17 15:59:43 +0000775 fPicture = NULL;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000776
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000777 fDeviceType = kRaster_DeviceType;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000778
779#if DEFAULT_TO_GPU
780 fDeviceType = kGPU_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000781#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000782#if SK_ANGLE && DEFAULT_TO_ANGLE
783 fDeviceType = kANGLE_DeviceType;
784#endif
785
reed@android.com8a1c16f2008-12-17 15:59:43 +0000786 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000787 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000788 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000789 fRotate = false;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000790 fPerspAnim = false;
791 fPerspAnimTime = 0;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000792 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000793 fRequestGrabImage = false;
scroggo@google.comb073d922012-06-08 15:35:03 +0000794 fPipeState = SkOSMenu::kOffState;
reed@google.com67b89ee2012-08-15 14:41:58 +0000795 fTilingState = SkOSMenu::kOffState;
scroggo@google.com85cade02012-08-17 13:29:50 +0000796 fTileCount.set(1, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000797 fMeasureFPS = false;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000798 fLCDState = SkOSMenu::kMixedState;
799 fAAState = SkOSMenu::kMixedState;
800 fFilterState = SkOSMenu::kMixedState;
801 fHintingState = SkOSMenu::kMixedState;
reed@google.com569e0432011-04-05 13:07:03 +0000802 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000803 fScrollTestX = fScrollTestY = 0;
804
Scroggo0f185c22011-03-24 18:35:50 +0000805 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000806 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000807 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
808 fShowZoomer = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000809
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000810 fZoomLevel = 0;
811 fZoomScale = SK_Scalar1;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000812
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000813 fMagnify = false;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000814 fDebugger = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000815
Scroggo8ac0d542011-06-21 14:44:57 +0000816 fSaveToPdf = false;
817 fPdfCanvas = NULL;
818
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000819 fTransitionNext = 6;
820 fTransitionPrev = 2;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000821
yangsu@google.com921091f2011-08-02 13:39:12 +0000822 int sinkID = this->getSinkID();
scroggo@google.com7dadc742012-04-18 14:07:57 +0000823 fAppMenu = new SkOSMenu;
824 fAppMenu->setTitle("Global Settings");
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000825 int itemID;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000826
827 itemID =fAppMenu->appendList("Device Type", "Device Type", sinkID, 0,
828 "Raster", "Picture", "OpenGL",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000829#if SK_ANGLE
830 "ANGLE",
831#endif
832 NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000833 fAppMenu->assignKeyEquivalentToItem(itemID, 'd');
834 itemID = fAppMenu->appendTriState("AA", "AA", sinkID, fAAState);
835 fAppMenu->assignKeyEquivalentToItem(itemID, 'b');
836 itemID = fAppMenu->appendTriState("LCD", "LCD", sinkID, fLCDState);
837 fAppMenu->assignKeyEquivalentToItem(itemID, 'l');
838 itemID = fAppMenu->appendTriState("Filter", "Filter", sinkID, fFilterState);
839 fAppMenu->assignKeyEquivalentToItem(itemID, 'n');
840 itemID = fAppMenu->appendTriState("Hinting", "Hinting", sinkID, fHintingState);
841 fAppMenu->assignKeyEquivalentToItem(itemID, 'h');
reed@google.com67b89ee2012-08-15 14:41:58 +0000842
scroggo@google.comb073d922012-06-08 15:35:03 +0000843 fUsePipeMenuItemID = fAppMenu->appendTriState("Pipe", "Pipe" , sinkID,
chudy@google.com4605a3f2012-08-01 17:58:01 +0000844 fPipeState);
scroggo@google.comb073d922012-06-08 15:35:03 +0000845 fAppMenu->assignKeyEquivalentToItem(fUsePipeMenuItemID, 'P');
reed@google.com67b89ee2012-08-15 14:41:58 +0000846
847 itemID = fAppMenu->appendTriState("Tiling", "Tiling", sinkID, fTilingState);
848 fAppMenu->assignKeyEquivalentToItem(itemID, 't');
849
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000850#ifdef DEBUGGER
scroggo@google.com7dadc742012-04-18 14:07:57 +0000851 itemID = fAppMenu->appendSwitch("Debugger", "Debugger", sinkID, fDebugger);
852 fAppMenu->assignKeyEquivalentToItem(itemID, 'q');
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000853#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +0000854 itemID = fAppMenu->appendSwitch("Slide Show", "Slide Show" , sinkID, false);
855 fAppMenu->assignKeyEquivalentToItem(itemID, 'a');
856 itemID = fAppMenu->appendSwitch("Clip", "Clip" , sinkID, fUseClip);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000857 fAppMenu->assignKeyEquivalentToItem(itemID, 'c');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000858 itemID = fAppMenu->appendSwitch("Flip X", "Flip X" , sinkID, false);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000859 fAppMenu->assignKeyEquivalentToItem(itemID, 'x');
860 itemID = fAppMenu->appendSwitch("Flip Y", "Flip Y" , sinkID, false);
861 fAppMenu->assignKeyEquivalentToItem(itemID, 'y');
862 itemID = fAppMenu->appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
863 fAppMenu->assignKeyEquivalentToItem(itemID, 'z');
864 itemID = fAppMenu->appendSwitch("Magnify", "Magnify" , sinkID, fMagnify);
865 fAppMenu->assignKeyEquivalentToItem(itemID, 'm');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000866 itemID =fAppMenu->appendList("Transition-Next", "Transition-Next", sinkID,
867 fTransitionNext, "Up", "Up and Right", "Right",
868 "Down and Right", "Down", "Down and Left",
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000869 "Left", "Up and Left", NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000870 fAppMenu->assignKeyEquivalentToItem(itemID, 'j');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000871 itemID =fAppMenu->appendList("Transition-Prev", "Transition-Prev", sinkID,
872 fTransitionPrev, "Up", "Up and Right", "Right",
873 "Down and Right", "Down", "Down and Left",
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000874 "Left", "Up and Left", NULL);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000875 fAppMenu->assignKeyEquivalentToItem(itemID, 'k');
876 itemID = fAppMenu->appendAction("Save to PDF", sinkID);
877 fAppMenu->assignKeyEquivalentToItem(itemID, 'e');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000878
scroggo@google.com7dadc742012-04-18 14:07:57 +0000879 this->addMenu(fAppMenu);
880 fSlideMenu = new SkOSMenu;
881 this->addMenu(fSlideMenu);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000882
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000883// this->setConfig(SkBitmap::kRGB_565_Config);
884 this->setConfig(SkBitmap::kARGB_8888_Config);
885 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000886 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000887
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000888 skiagm::GM::SetResourcePath(resourcePath);
889
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000890 this->loadView((*fSamples[fCurrIndex])());
chudy@google.com4605a3f2012-08-01 17:58:01 +0000891
bsalomon@google.com840e9f32011-07-06 21:59:09 +0000892 fPDFData = NULL;
reed@google.comf0b5f682011-03-11 20:08:25 +0000893
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000894 if (NULL == devManager) {
895 fDevManager = new DefaultDeviceManager();
896 } else {
897 devManager->ref();
898 fDevManager = devManager;
899 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000900 fDevManager->setUpBackend(this, fMSAASampleCount);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000901
Scroggob4490c72011-06-17 13:53:05 +0000902 // If another constructor set our dimensions, ensure that our
903 // onSizeChange gets called.
904 if (this->height() && this->width()) {
905 this->onSizeChange();
906 }
reed@google.com2072db82011-11-08 15:18:10 +0000907
908 // can't call this synchronously, since it may require a subclass to
909 // to implement, or the caller may need us to have returned from the
910 // constructor first. Hence we post an event to ourselves.
911// this->updateTitle();
912 postEventToSink(new SkEvent(gUpdateWindowTitleEvtName), this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000913}
914
915SampleWindow::~SampleWindow() {
916 delete fPicture;
Scroggo8ac0d542011-06-21 14:44:57 +0000917 delete fPdfCanvas;
Scroggo0f185c22011-03-24 18:35:50 +0000918 fTypeface->unref();
reed@google.com29038ed2011-07-06 17:56:47 +0000919
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000920 SkSafeUnref(fDevManager);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000921}
922
reed@google.com1830c7a2012-06-04 12:05:43 +0000923static void make_filepath(SkString* path, const char* dir, const SkString& name) {
924 size_t len = strlen(dir);
925 path->set(dir);
926 if (len > 0 && dir[len - 1] != '/') {
927 path->append("/");
928 }
929 path->append(name);
930}
931
chudy@google.com4605a3f2012-08-01 17:58:01 +0000932void SampleWindow::registerPictFileSample(char** argv, int argc) {
933 const char* pict = NULL;
934
935 for (int i = 0; i < argc; ++i) {
936 if (!strcmp(argv[i], "--picture")) {
937 i += 1;
938 if (i < argc) {
939 pict = argv[i];
940 break;
941 }
942 }
943 }
944 if (pict) {
945 SkString path(pict);
946 *fSamples.append() = new PictFileFactory(path);
947 }
948}
949
reed@google.com1830c7a2012-06-04 12:05:43 +0000950void SampleWindow::registerPictFileSamples(char** argv, int argc) {
951 const char* pictDir = NULL;
952
953 for (int i = 0; i < argc; ++i) {
954 if (!strcmp(argv[i], "--pictureDir")) {
955 i += 1;
956 if (i < argc) {
957 pictDir = argv[i];
958 break;
959 }
960 }
961 }
962 if (pictDir) {
963 SkOSFile::Iter iter(pictDir, "skp");
964 SkString filename;
965 while (iter.next(&filename)) {
966 SkString path;
967 make_filepath(&path, pictDir, filename);
968 *fSamples.append() = new PictFileFactory(path);
969 }
970 }
971}
972
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000973int SampleWindow::findByTitle(const char title[]) {
974 int i, count = fSamples.count();
975 for (i = 0; i < count; i++) {
976 if (getSampleTitle(i).equals(title)) {
977 return i;
978 }
979 }
mike@reedtribe.orgdd52caa2011-12-28 20:02:10 +0000980 return -1;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000981}
982
robertphillips@google.com7265e722012-05-03 18:22:28 +0000983void SampleWindow::listTitles() {
984 int count = fSamples.count();
985 SkDebugf("All Slides:\n");
986 for (int i = 0; i < count; i++) {
987 SkDebugf(" %s\n", getSampleTitle(i).c_str());
988 }
989}
990
reed@android.com55e76b22009-11-23 21:46:47 +0000991static SkBitmap capture_bitmap(SkCanvas* canvas) {
992 SkBitmap bm;
993 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
994 src.copyTo(&bm, src.config());
995 return bm;
996}
997
998static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
999 SkBitmap* diff) {
1000 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001001
reed@android.com55e76b22009-11-23 21:46:47 +00001002 SkAutoLockPixels alp0(src);
1003 SkAutoLockPixels alp1(orig);
1004 for (int y = 0; y < src.height(); y++) {
1005 const void* srcP = src.getAddr(0, y);
1006 const void* origP = orig.getAddr(0, y);
1007 size_t bytes = src.width() * src.bytesPerPixel();
1008 if (memcmp(srcP, origP, bytes)) {
1009 SkDebugf("---------- difference on line %d\n", y);
1010 return true;
1011 }
1012 }
1013 return false;
1014}
1015
Scroggo0f185c22011-03-24 18:35:50 +00001016static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
1017{
1018 SkColor desiredColor = paint.getColor();
1019 paint.setColor(SK_ColorWHITE);
1020 const char* c_str = string.c_str();
1021 size_t size = string.size();
1022 SkRect bounds;
1023 paint.measureText(c_str, size, &bounds);
1024 bounds.offset(left, top);
1025 SkScalar inset = SkIntToScalar(-2);
1026 bounds.inset(inset, inset);
1027 canvas->drawRect(bounds, paint);
1028 if (desiredColor != SK_ColorBLACK) {
1029 paint.setColor(SK_ColorBLACK);
1030 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
1031 }
1032 paint.setColor(desiredColor);
1033 canvas->drawText(c_str, size, left, top, paint);
1034}
1035
reed@android.com44177402009-11-23 21:07:51 +00001036#define XCLIP_N 8
1037#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +00001038
1039void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +00001040 // update the animation time
bsalomon@google.come8f09102011-09-08 18:48:12 +00001041 if (!gAnimTimePrev && !gAnimTime) {
1042 // first time make delta be 0
1043 gAnimTime = SkTime::GetMSecs();
1044 gAnimTimePrev = gAnimTime;
1045 } else {
1046 gAnimTimePrev = gAnimTime;
1047 gAnimTime = SkTime::GetMSecs();
1048 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001049
reed@google.comf03bb562011-11-11 21:42:12 +00001050 if (fGesture.isActive()) {
1051 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001052 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001053
djsollen@google.com796763e2012-12-10 14:12:55 +00001054 if (fMeasureFPS) {
1055 fMeasureFPS_Time = 0;
1056 }
1057
reed@android.come522ca52009-11-23 20:10:41 +00001058 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +00001059 this->INHERITED::draw(canvas);
1060 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +00001061
1062 const SkScalar w = this->width();
1063 const SkScalar h = this->height();
1064 const SkScalar cw = w / XCLIP_N;
1065 const SkScalar ch = h / YCLIP_N;
1066 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +00001067 SkRect r;
1068 r.fTop = y * ch;
1069 r.fBottom = (y + 1) * ch;
1070 if (y == YCLIP_N - 1) {
1071 r.fBottom = h;
1072 }
reed@android.come522ca52009-11-23 20:10:41 +00001073 for (int x = 0; x < XCLIP_N; x++) {
1074 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +00001075 r.fLeft = x * cw;
1076 r.fRight = (x + 1) * cw;
1077 if (x == XCLIP_N - 1) {
1078 r.fRight = w;
1079 }
reed@android.come522ca52009-11-23 20:10:41 +00001080 canvas->clipRect(r);
1081 this->INHERITED::draw(canvas);
1082 }
1083 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001084
reed@android.com55e76b22009-11-23 21:46:47 +00001085 SkBitmap diff;
1086 if (bitmap_diff(canvas, orig, &diff)) {
1087 }
reed@android.come522ca52009-11-23 20:10:41 +00001088 } else {
scroggo@google.com85cade02012-08-17 13:29:50 +00001089 const SkScalar cw = SkScalarDiv(this->width(), SkIntToScalar(fTileCount.width()));
1090 const SkScalar ch = SkScalarDiv(this->height(), SkIntToScalar(fTileCount.height()));
rmistry@google.comae933ce2012-08-23 18:19:56 +00001091
scroggo@google.com85cade02012-08-17 13:29:50 +00001092 for (int y = 0; y < fTileCount.height(); ++y) {
1093 for (int x = 0; x < fTileCount.width(); ++x) {
1094 SkAutoCanvasRestore acr(canvas, true);
1095 canvas->clipRect(SkRect::MakeXYWH(x * cw, y * ch, cw, ch));
1096 this->INHERITED::draw(canvas);
1097 }
1098 }
rmistry@google.comae933ce2012-08-23 18:19:56 +00001099
scroggo@google.com85cade02012-08-17 13:29:50 +00001100 if (!fTileCount.equals(1, 1)) {
1101 SkPaint paint;
1102 paint.setColor(0x60FF00FF);
1103 paint.setStyle(SkPaint::kStroke_Style);
1104 for (int y = 0; y < fTileCount.height(); ++y) {
1105 for (int x = 0; x < fTileCount.width(); ++x) {
1106 canvas->drawRect(SkRect::MakeXYWH(x * cw, y * ch, cw, ch), paint);
1107 }
1108 }
1109 }
reed@android.come522ca52009-11-23 20:10:41 +00001110 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001111 if (fShowZoomer && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +00001112 showZoomer(canvas);
1113 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001114 if (fMagnify && !fSaveToPdf) {
1115 magnify(canvas);
1116 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001117
djsollen@google.com796763e2012-12-10 14:12:55 +00001118 if (fMeasureFPS && fMeasureFPS_Time) {
1119 this->updateTitle();
1120 this->postInvalDelay();
1121 }
1122
reed@google.com29038ed2011-07-06 17:56:47 +00001123 // do this last
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001124 fDevManager->publishCanvas(fDeviceType, canvas, this);
Scroggo3272ba82011-05-25 20:50:42 +00001125}
1126
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001127static float clipW = 200;
1128static float clipH = 200;
1129void SampleWindow::magnify(SkCanvas* canvas) {
1130 SkRect r;
1131 int count = canvas->save();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001132
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001133 SkMatrix m = canvas->getTotalMatrix();
robertphillips@google.com07ef9112012-06-04 13:22:14 +00001134 if (!m.invert(&m)) {
1135 return;
1136 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001137 SkPoint offset, center;
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001138 SkScalar mouseX = fMouseX * SK_Scalar1;
1139 SkScalar mouseY = fMouseY * SK_Scalar1;
1140 m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset);
1141 m.mapXY(mouseX, mouseY, &center);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001142
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001143 r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX());
1144 r.offset(offset.fX, offset.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001145
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001146 SkPaint paint;
1147 paint.setColor(0xFF66AAEE);
1148 paint.setStyle(SkPaint::kStroke_Style);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001149 paint.setStrokeWidth(10.f * m.getScaleX());
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001150 //lense offset
1151 //canvas->translate(0, -250);
1152 canvas->drawRect(r, paint);
1153 canvas->clipRect(r);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001154
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001155 m = canvas->getTotalMatrix();
1156 m.setTranslate(-center.fX, -center.fY);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001157 m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001158 m.postTranslate(center.fX, center.fY);
1159 canvas->concat(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001160
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001161 this->INHERITED::draw(canvas);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001162
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001163 canvas->restoreToCount(count);
1164}
1165
Scroggo3272ba82011-05-25 20:50:42 +00001166void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +00001167 int count = canvas->save();
1168 canvas->resetMatrix();
1169 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +00001170 int width = SkScalarRound(this->width());
1171 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +00001172 if (fMouseX >= width) fMouseX = width - 1;
1173 else if (fMouseX < 0) fMouseX = 0;
1174 if (fMouseY >= height) fMouseY = height - 1;
1175 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +00001176
Scroggo0f185c22011-03-24 18:35:50 +00001177 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +00001178 bitmap.lockPixels();
1179
Scroggo0f185c22011-03-24 18:35:50 +00001180 // 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 +00001181 int zoomedWidth = (width >> 1) | 1;
1182 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +00001183 SkIRect src;
1184 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
1185 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
1186 SkRect dest;
1187 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
1188 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
1189 SkPaint paint;
1190 // Clear the background behind our zoomed in view
1191 paint.setColor(SK_ColorWHITE);
1192 canvas->drawRect(dest, paint);
1193 canvas->drawBitmapRect(bitmap, &src, dest);
1194 paint.setColor(SK_ColorBLACK);
1195 paint.setStyle(SkPaint::kStroke_Style);
1196 // Draw a border around the pixel in the middle
1197 SkRect originalPixel;
1198 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
1199 SkMatrix matrix;
1200 SkRect scalarSrc;
1201 scalarSrc.set(src);
1202 SkColor color = bitmap.getColor(fMouseX, fMouseY);
1203 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
1204 SkRect pixel;
1205 matrix.mapRect(&pixel, originalPixel);
1206 // TODO Perhaps measure the values and make the outline white if it's "dark"
1207 if (color == SK_ColorBLACK) {
1208 paint.setColor(SK_ColorWHITE);
1209 }
1210 canvas->drawRect(pixel, paint);
1211 }
1212 paint.setColor(SK_ColorBLACK);
1213 // Draw a border around the destination rectangle
1214 canvas->drawRect(dest, paint);
1215 paint.setStyle(SkPaint::kStrokeAndFill_Style);
1216 // Identify the pixel and its color on screen
1217 paint.setTypeface(fTypeface);
1218 paint.setAntiAlias(true);
1219 SkScalar lineHeight = paint.getFontMetrics(NULL);
1220 SkString string;
1221 string.appendf("(%i, %i)", fMouseX, fMouseY);
1222 SkScalar left = dest.fLeft + SkIntToScalar(3);
1223 SkScalar i = SK_Scalar1;
1224 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1225 // Alpha
1226 i += SK_Scalar1;
1227 string.reset();
1228 string.appendf("A: %X", SkColorGetA(color));
1229 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1230 // Red
1231 i += SK_Scalar1;
1232 string.reset();
1233 string.appendf("R: %X", SkColorGetR(color));
1234 paint.setColor(SK_ColorRED);
1235 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1236 // Green
1237 i += SK_Scalar1;
1238 string.reset();
1239 string.appendf("G: %X", SkColorGetG(color));
1240 paint.setColor(SK_ColorGREEN);
1241 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1242 // Blue
1243 i += SK_Scalar1;
1244 string.reset();
1245 string.appendf("B: %X", SkColorGetB(color));
1246 paint.setColor(SK_ColorBLUE);
1247 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1248 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +00001249}
1250
reed@android.com8a1c16f2008-12-17 15:59:43 +00001251void SampleWindow::onDraw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001252}
1253
1254#include "SkColorPriv.h"
1255
caryclark@google.com02939ce2012-06-06 12:09:51 +00001256#if 0 // UNUSED
reed@android.com8a1c16f2008-12-17 15:59:43 +00001257static void reverseRedAndBlue(const SkBitmap& bm) {
1258 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
1259 uint8_t* p = (uint8_t*)bm.getPixels();
1260 uint8_t* stop = p + bm.getSize();
1261 while (p < stop) {
1262 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
1263 unsigned scale = SkAlpha255To256(p[3]);
1264 unsigned r = p[2];
1265 unsigned b = p[0];
1266 p[0] = SkAlphaMul(r, scale);
1267 p[1] = SkAlphaMul(p[1], scale);
1268 p[2] = SkAlphaMul(b, scale);
1269 p += 4;
1270 }
1271}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001272#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001273
Scroggo8ac0d542011-06-21 14:44:57 +00001274void SampleWindow::saveToPdf()
1275{
1276 fSaveToPdf = true;
1277 this->inval(NULL);
1278}
1279
reed@android.com8a1c16f2008-12-17 15:59:43 +00001280SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +00001281 if (fSaveToPdf) {
1282 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
1283 SkISize size = SkISize::Make(bmp.width(), bmp.height());
1284 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
1285 canvas->getTotalMatrix());
1286 fPdfCanvas = new SkCanvas(pdfDevice);
1287 pdfDevice->unref();
1288 canvas = fPdfCanvas;
1289 } else {
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001290 switch (fDeviceType) {
1291 case kRaster_DeviceType:
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001292 // fallthrough
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001293#if SK_SUPPORT_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001294 case kGPU_DeviceType:
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001295 // fallthrough
1296#if SK_ANGLE
1297 case kANGLE_DeviceType:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001298#endif // SK_ANGLE
1299#endif // SK_SUPPORT_GPU
reed@android.comf2b98d62010-12-20 18:26:13 +00001300 canvas = this->INHERITED::beforeChildren(canvas);
Scroggo8ac0d542011-06-21 14:44:57 +00001301 break;
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001302 case kPicture_DeviceType:
Scroggo8ac0d542011-06-21 14:44:57 +00001303 fPicture = new SkPicture;
1304 canvas = fPicture->beginRecording(9999, 9999);
1305 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001306#if SK_SUPPORT_GPU
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +00001307 case kNullGPU_DeviceType:
1308 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001309#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001310 default:
1311 SkASSERT(false);
1312 break;
reed@google.comac10a2d2010-12-22 21:39:39 +00001313 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001314 }
1315
1316 if (fUseClip) {
1317 canvas->drawColor(0xFFFF88FF);
reed@google.com045e62d2011-10-24 12:19:46 +00001318 canvas->clipPath(fClipPath, SkRegion::kIntersect_Op, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001319 }
1320
1321 return canvas;
1322}
1323
1324static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
1325 const SkRegion& rgn) {
1326 SkCanvas canvas(bm);
1327 SkRegion inval(rgn);
1328
1329 inval.translate(r.fLeft, r.fTop);
1330 canvas.clipRegion(inval);
1331 canvas.drawColor(0xFFFF8080);
1332}
yangsu@google.com501775e2011-06-24 16:04:50 +00001333#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001334void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +00001335 if (fSaveToPdf) {
1336 fSaveToPdf = false;
1337 if (fShowZoomer) {
1338 showZoomer(fPdfCanvas);
1339 }
1340 SkString name;
1341 name.printf("%s.pdf", this->getTitle());
1342 SkPDFDocument doc;
1343 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
1344 doc.appendPage(device);
djsollen@google.com56c69772011-11-08 19:00:26 +00001345#ifdef SK_BUILD_FOR_ANDROID
Scroggo8ac0d542011-06-21 14:44:57 +00001346 name.prepend("/sdcard/");
1347#endif
chudy@google.com4605a3f2012-08-01 17:58:01 +00001348
yangsu@google.com501775e2011-06-24 16:04:50 +00001349#ifdef SK_BUILD_FOR_IOS
1350 SkDynamicMemoryWStream mstream;
1351 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +00001352 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +00001353#endif
Scroggo8ac0d542011-06-21 14:44:57 +00001354 SkFILEWStream stream(name.c_str());
1355 if (stream.isValid()) {
1356 doc.emitPDF(&stream);
1357 const char* desc = "File saved from Skia SampleApp";
1358 this->onPDFSaved(this->getTitle(), desc, name.c_str());
1359 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001360
Scroggo8ac0d542011-06-21 14:44:57 +00001361 delete fPdfCanvas;
1362 fPdfCanvas = NULL;
1363
1364 // We took over the draw calls in order to create the PDF, so we need
1365 // to redraw.
1366 this->inval(NULL);
1367 return;
1368 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001369
reed@android.comf2b98d62010-12-20 18:26:13 +00001370 if (fRequestGrabImage) {
1371 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001372
reed@google.com29038ed2011-07-06 17:56:47 +00001373 SkDevice* device = orig->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001374 SkBitmap bmp;
1375 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001376 static int gSampleGrabCounter;
1377 SkString name;
reed@google.com7c57e0e2012-09-08 21:20:33 +00001378 name.printf("sample_grab_%d.png", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001379 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +00001380 SkImageEncoder::kPNG_Type, 100);
1381 }
1382 }
1383
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001384 if (kPicture_DeviceType == fDeviceType) {
1385 if (true) {
1386 SkPicture* pict = new SkPicture(*fPicture);
1387 fPicture->unref();
reed@google.come23f1942011-12-08 19:36:00 +00001388 this->installDrawFilter(orig);
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001389 orig->drawPicture(*pict);
1390 pict->unref();
1391 } else if (true) {
1392 SkDynamicMemoryWStream ostream;
1393 fPicture->serialize(&ostream);
1394 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001395
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001396 SkAutoDataUnref data(ostream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +00001397 SkMemoryStream istream(data->data(), data->size());
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001398 SkPicture pict(&istream);
1399 orig->drawPicture(pict);
1400 } else {
1401 fPicture->draw(orig);
1402 fPicture->unref();
1403 }
1404 fPicture = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001405 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001406
reed@google.com17d7aec2011-04-25 14:31:44 +00001407 // Do this after presentGL and other finishing, rather than in afterChild
djsollen@google.com796763e2012-12-10 14:12:55 +00001408 if (fMeasureFPS && fMeasureFPS_StartTime) {
1409 fMeasureFPS_Time += SkTime::GetMSecs() - fMeasureFPS_StartTime;
reed@google.com17d7aec2011-04-25 14:31:44 +00001410 }
1411
1412 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +00001413 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001414 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
1415 int dx = fScrollTestX * 7;
1416 int dy = fScrollTestY * 7;
1417 SkIRect r;
1418 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001419
reed@android.com8a1c16f2008-12-17 15:59:43 +00001420 r.set(50, 50, 50+100, 50+100);
1421 bm.scrollRect(&r, dx, dy, &inval);
1422 paint_rgn(bm, r, inval);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001423 }
1424#ifdef DEBUGGER
1425 SkView* curr = curr_view(this);
1426 if (fDebugger && !is_debugger(curr) && !is_transition(curr) && !is_overview(curr)) {
1427 //Stop Pipe when fDebugger is active
scroggo@google.comb073d922012-06-08 15:35:03 +00001428 if (fPipeState != SkOSMenu::kOffState) {
1429 fPipeState = SkOSMenu::kOffState;
1430 (void)SampleView::SetUsePipe(curr, fPipeState);
1431 fAppMenu->getItemByID(fUsePipeMenuItemID)->setTriState(fPipeState);
1432 this->onUpdateMenu(fAppMenu);
1433 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001434
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001435 //Reset any transformations
1436 fGesture.stop();
1437 fGesture.reset();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001438
1439 this->loadView(create_debugger(gTempDataStore.begin(),
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001440 gTempDataStore.count()));
1441 }
1442#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001443}
1444
reed@android.com6c5f6f22009-08-14 16:08:38 +00001445void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
1446 if (fScale) {
1447 SkScalar scale = SK_Scalar1 * 7 / 10;
1448 SkScalar cx = this->width() / 2;
1449 SkScalar cy = this->height() / 2;
1450 canvas->translate(cx, cy);
1451 canvas->scale(scale, scale);
1452 canvas->translate(-cx, -cy);
1453 }
1454 if (fRotate) {
1455 SkScalar cx = this->width() / 2;
1456 SkScalar cy = this->height() / 2;
1457 canvas->translate(cx, cy);
1458 canvas->rotate(SkIntToScalar(30));
1459 canvas->translate(-cx, -cy);
1460 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00001461 if (fPerspAnim) {
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001462 fPerspAnimTime += SampleCode::GetAnimSecondsDelta();
1463
1464 static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
1465 static const SkScalar gAnimMag = SK_Scalar1 / 1000;
1466 SkScalar t = SkScalarMod(fPerspAnimTime, gAnimPeriod);
1467 if (SkScalarFloorToInt(SkScalarDiv(fPerspAnimTime, gAnimPeriod)) & 0x1) {
1468 t = gAnimPeriod - t;
1469 }
1470 t = 2 * t - gAnimPeriod;
1471 t = SkScalarMul(SkScalarDiv(t, gAnimPeriod), gAnimMag);
1472 SkMatrix m;
1473 m.reset();
1474 m.setPerspY(t);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001475 canvas->concat(m);
1476 }
reed@google.comf0b5f682011-03-11 20:08:25 +00001477
reed@google.come23f1942011-12-08 19:36:00 +00001478 this->installDrawFilter(canvas);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001479
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001480 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +00001481 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
djsollen@google.com796763e2012-12-10 14:12:55 +00001482 fMeasureFPS_StartTime = SkTime::GetMSecs();
reed@google.comf2183392011-04-22 14:10:48 +00001483 }
1484 } else {
1485 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001486 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00001487 if (fPerspAnim) {
1488 this->inval(NULL);
1489 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001490}
1491
1492void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +00001493 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001494}
1495
reed@android.com8a1c16f2008-12-17 15:59:43 +00001496static SkBitmap::Config gConfigCycle[] = {
1497 SkBitmap::kNo_Config, // none -> none
1498 SkBitmap::kNo_Config, // a1 -> none
1499 SkBitmap::kNo_Config, // a8 -> none
1500 SkBitmap::kNo_Config, // index8 -> none
1501 SkBitmap::kARGB_4444_Config, // 565 -> 4444
1502 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
1503 SkBitmap::kRGB_565_Config // 8888 -> 565
1504};
1505
1506static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
1507 return gConfigCycle[c];
1508}
1509
djsollen@google.come32b5832011-06-13 16:58:40 +00001510void SampleWindow::changeZoomLevel(float delta) {
1511 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001512 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001513 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
1514 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001515 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001516 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
1517 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001518 } else {
1519 fZoomScale = SK_Scalar1;
1520 }
reed@google.comf03bb562011-11-11 21:42:12 +00001521 this->updateMatrix();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001522}
1523
reed@google.comf03bb562011-11-11 21:42:12 +00001524void SampleWindow::updateMatrix(){
1525 SkMatrix m;
1526 m.reset();
1527 if (fZoomLevel) {
1528 SkPoint center;
1529 //m = this->getLocalMatrix();//.invert(&m);
1530 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
1531 SkScalar cx = center.fX;
1532 SkScalar cy = center.fY;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001533
reed@google.comf03bb562011-11-11 21:42:12 +00001534 m.setTranslate(-cx, -cy);
1535 m.postScale(fZoomScale, fZoomScale);
1536 m.postTranslate(cx, cy);
1537 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001538
reed@google.comf03bb562011-11-11 21:42:12 +00001539 if (fFlipAxis) {
1540 m.preTranslate(fZoomCenterX, fZoomCenterY);
1541 if (fFlipAxis & kFlipAxis_X) {
1542 m.preScale(-SK_Scalar1, SK_Scalar1);
1543 }
1544 if (fFlipAxis & kFlipAxis_Y) {
1545 m.preScale(SK_Scalar1, -SK_Scalar1);
1546 }
1547 m.preTranslate(-fZoomCenterX, -fZoomCenterY);
1548 //canvas->concat(m);
1549 }
1550 // Apply any gesture matrix
1551 m.preConcat(fGesture.localM());
1552 m.preConcat(fGesture.globalM());
chudy@google.com4605a3f2012-08-01 17:58:01 +00001553
reed@google.comf03bb562011-11-11 21:42:12 +00001554 this->setLocalMatrix(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001555
reed@google.comf03bb562011-11-11 21:42:12 +00001556 this->updateTitle();
1557 this->inval(NULL);
1558}
Scroggo2c8208f2011-06-15 16:49:08 +00001559bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +00001560 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001561 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001562 fTransitionPrev));
Scroggo2c8208f2011-06-15 16:49:08 +00001563 return true;
1564}
1565
reed@android.com8a1c16f2008-12-17 15:59:43 +00001566bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +00001567 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001568 this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001569 fTransitionNext));
reed@android.com34245c72009-11-03 04:00:48 +00001570 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001571}
1572
yangsu@google.com501775e2011-06-24 16:04:50 +00001573bool SampleWindow::goToSample(int i) {
1574 fCurrIndex = (i) % fSamples.count();
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001575 this->loadView(create_transition(curr_view(this),(*fSamples[fCurrIndex])(), 6));
yangsu@google.com501775e2011-06-24 16:04:50 +00001576 return true;
1577}
1578
1579SkString SampleWindow::getSampleTitle(int i) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001580 SkView* view = (*fSamples[i])();
yangsu@google.com501775e2011-06-24 16:04:50 +00001581 SkString title;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001582 SampleCode::RequestTitle(view, &title);
yangsu@google.com501775e2011-06-24 16:04:50 +00001583 view->unref();
1584 return title;
1585}
1586
1587int SampleWindow::sampleCount() {
1588 return fSamples.count();
1589}
1590
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001591void SampleWindow::showOverview() {
chudy@google.com4605a3f2012-08-01 17:58:01 +00001592 this->loadView(create_transition(curr_view(this),
1593 create_overview(fSamples.count(), fSamples.begin()),
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001594 4));
1595}
1596
reed@google.come23f1942011-12-08 19:36:00 +00001597void SampleWindow::installDrawFilter(SkCanvas* canvas) {
1598 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
1599 fFilterState, fHintingState))->unref();
1600}
1601
Scroggo2c8208f2011-06-15 16:49:08 +00001602void SampleWindow::postAnimatingEvent() {
1603 if (fAnimating) {
reed@google.com87fac4a2011-08-04 13:50:17 +00001604 (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
Scroggo2c8208f2011-06-15 16:49:08 +00001605 }
1606}
reed@google.come23f1942011-12-08 19:36:00 +00001607
reed@android.com8a1c16f2008-12-17 15:59:43 +00001608bool SampleWindow::onEvent(const SkEvent& evt) {
reed@google.com2072db82011-11-08 15:18:10 +00001609 if (evt.isType(gUpdateWindowTitleEvtName)) {
1610 this->updateTitle();
1611 return true;
1612 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001613 if (evt.isType(ANIMATING_EVENTTYPE)) {
1614 if (fAnimating) {
1615 this->nextSample();
1616 this->postAnimatingEvent();
1617 }
1618 return true;
1619 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001620 if (evt.isType("replace-transition-view")) {
1621 this->loadView((SkView*)SkEventSink::FindSink(evt.getFast32()));
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001622 return true;
1623 }
reed@android.com34245c72009-11-03 04:00:48 +00001624 if (evt.isType("set-curr-index")) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001625 this->goToSample(evt.getFast32());
reed@android.com34245c72009-11-03 04:00:48 +00001626 return true;
1627 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001628 if (isInvalEvent(evt)) {
1629 this->inval(NULL);
1630 return true;
1631 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001632 int selected = -1;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001633 if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001634 this->setDeviceType((DeviceType)selected);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001635 return true;
yangsu@google.com921091f2011-08-02 13:39:12 +00001636 }
scroggo@google.comb073d922012-06-08 15:35:03 +00001637 if (SkOSMenu::FindTriState(evt, "Pipe", &fPipeState)) {
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001638#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00001639 if (!fPipeState != SkOSMenu::kOnState)
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001640 gServer.disconnectAll();
1641#endif
scroggo@google.comb073d922012-06-08 15:35:03 +00001642 (void)SampleView::SetUsePipe(curr_view(this), fPipeState);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001643 this->updateTitle();
1644 this->inval(NULL);
yangsu@google.com921091f2011-08-02 13:39:12 +00001645 return true;
1646 }
reed@google.com67b89ee2012-08-15 14:41:58 +00001647 if (SkOSMenu::FindTriState(evt, "Tiling", &fTilingState)) {
1648 int nx = 1, ny = 1;
1649 switch (fTilingState) {
1650 case SkOSMenu::kOffState: nx = 1; ny = 1; break;
reed@google.com56b64a52012-08-15 20:44:36 +00001651 case SkOSMenu::kMixedState: nx = 1; ny = 16; break;
1652 case SkOSMenu::kOnState: nx = 4; ny = 4; break;
reed@google.com67b89ee2012-08-15 14:41:58 +00001653 }
scroggo@google.com85cade02012-08-17 13:29:50 +00001654 fTileCount.set(nx, ny);
reed@google.com67b89ee2012-08-15 14:41:58 +00001655 this->inval(NULL);
1656 return true;
1657 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001658 if (SkOSMenu::FindSwitchState(evt, "Slide Show", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001659 this->toggleSlideshow();
1660 return true;
1661 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001662 if (SkOSMenu::FindTriState(evt, "AA", &fAAState) ||
1663 SkOSMenu::FindTriState(evt, "LCD", &fLCDState) ||
1664 SkOSMenu::FindTriState(evt, "Filter", &fFilterState) ||
1665 SkOSMenu::FindTriState(evt, "Hinting", &fHintingState) ||
1666 SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) ||
1667 SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
1668 SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify) ||
1669 SkOSMenu::FindListIndex(evt, "Transition-Next", &fTransitionNext) ||
1670 SkOSMenu::FindListIndex(evt, "Transition-Prev", &fTransitionPrev)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001671 this->inval(NULL);
1672 this->updateTitle();
1673 return true;
1674 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001675 if (SkOSMenu::FindSwitchState(evt, "Flip X", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001676 fFlipAxis ^= kFlipAxis_X;
reed@google.comf03bb562011-11-11 21:42:12 +00001677 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001678 return true;
1679 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001680 if (SkOSMenu::FindSwitchState(evt, "Flip Y", NULL)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001681 fFlipAxis ^= kFlipAxis_Y;
reed@google.comf03bb562011-11-11 21:42:12 +00001682 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001683 return true;
1684 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001685 if (SkOSMenu::FindAction(evt,"Save to PDF")) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001686 this->saveToPdf();
1687 return true;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001688 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001689#ifdef DEBUGGER
1690 if (SkOSMenu::FindSwitchState(evt, "Debugger", &fDebugger)) {
1691 if (fDebugger) {
scroggo@google.comb073d922012-06-08 15:35:03 +00001692 fPipeState = SkOSMenu::kOnState;
1693 (void)SampleView::SetUsePipe(curr_view(this), fPipeState);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001694 } else {
scroggo@google.comb073d922012-06-08 15:35:03 +00001695 this->loadView((*fSamples[fCurrIndex])());
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001696 }
1697 this->inval(NULL);
1698 return true;
1699 }
1700#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001701 return this->INHERITED::onEvent(evt);
1702}
1703
reed@android.comf2b98d62010-12-20 18:26:13 +00001704bool SampleWindow::onQuery(SkEvent* query) {
1705 if (query->isType("get-slide-count")) {
1706 query->setFast32(fSamples.count());
1707 return true;
1708 }
1709 if (query->isType("get-slide-title")) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001710 SkView* view = (*fSamples[query->getFast32()])();
reed@android.comf2b98d62010-12-20 18:26:13 +00001711 SkEvent evt(gTitleEvtName);
1712 if (view->doQuery(&evt)) {
1713 query->setString("title", evt.findString(gTitleEvtName));
1714 }
1715 SkSafeUnref(view);
1716 return true;
1717 }
1718 if (query->isType("use-fast-text")) {
1719 SkEvent evt(gFastTextEvtName);
1720 return curr_view(this)->doQuery(&evt);
1721 }
reed@google.com29038ed2011-07-06 17:56:47 +00001722 if (query->isType("ignore-window-bitmap")) {
1723 query->setFast32(this->getGrContext() != NULL);
1724 return true;
1725 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001726 return this->INHERITED::onQuery(query);
1727}
1728
caryclark@google.com02939ce2012-06-06 12:09:51 +00001729#if 0 // UNUSED
reed@android.com0ae6b242008-12-23 16:49:54 +00001730static void cleanup_for_filename(SkString* name) {
1731 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001732 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001733 switch (str[i]) {
1734 case ':': str[i] = '-'; break;
1735 case '/': str[i] = '-'; break;
1736 case ' ': str[i] = '_'; break;
1737 default: break;
1738 }
1739 }
1740}
caryclark@google.com02939ce2012-06-06 12:09:51 +00001741#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001742
reed@google.coma4f81372012-11-15 15:56:38 +00001743//extern bool gIgnoreFastBlurRect;
1744
reed@android.com8a1c16f2008-12-17 15:59:43 +00001745bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001746 {
1747 SkView* view = curr_view(this);
1748 if (view) {
1749 SkEvent evt(gCharEvtName);
1750 evt.setFast32(uni);
1751 if (view->doQuery(&evt)) {
1752 return true;
1753 }
1754 }
1755 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001756
reed@android.com8a1c16f2008-12-17 15:59:43 +00001757 int dx = 0xFF;
1758 int dy = 0xFF;
1759
1760 switch (uni) {
1761 case '5': dx = 0; dy = 0; break;
1762 case '8': dx = 0; dy = -1; break;
1763 case '6': dx = 1; dy = 0; break;
1764 case '2': dx = 0; dy = 1; break;
1765 case '4': dx = -1; dy = 0; break;
1766 case '7': dx = -1; dy = -1; break;
1767 case '9': dx = 1; dy = -1; break;
1768 case '3': dx = 1; dy = 1; break;
1769 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001770
reed@android.com8a1c16f2008-12-17 15:59:43 +00001771 default:
1772 break;
1773 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001774
reed@android.com8a1c16f2008-12-17 15:59:43 +00001775 if (0xFF != dx && 0xFF != dy) {
1776 if ((dx | dy) == 0) {
1777 fScrollTestX = fScrollTestY = 0;
1778 } else {
1779 fScrollTestX += dx;
1780 fScrollTestY += dy;
1781 }
1782 this->inval(NULL);
1783 return true;
1784 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001785
reed@android.com0ae6b242008-12-23 16:49:54 +00001786 switch (uni) {
djsollen@google.com796763e2012-12-10 14:12:55 +00001787 case 'b':
1788 {
1789 postEventToSink(SkNEW_ARGS(SkEvent, ("PictFileView::toggleBBox")), curr_view(this));
1790 this->updateTitle();
1791 this->inval(NULL);
1792 break;
1793 }
reed@google.coma4f81372012-11-15 15:56:38 +00001794 case 'B':
1795// gIgnoreFastBlurRect = !gIgnoreFastBlurRect;
1796 this->inval(NULL);
1797 break;
1798
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001799 case 'f':
chudy@google.com4605a3f2012-08-01 17:58:01 +00001800 // only
djsollen@google.com6ff82552011-11-07 15:43:57 +00001801 toggleFPS();
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001802 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001803 case 'g':
1804 fRequestGrabImage = true;
1805 this->inval(NULL);
1806 break;
reed@google.coma4f81372012-11-15 15:56:38 +00001807 case 'G':
1808 gShowGMBounds = !gShowGMBounds;
1809 postEventToSink(GMSampleView::NewShowSizeEvt(gShowGMBounds),
1810 curr_view(this));
1811 this->inval(NULL);
1812 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001813 case 'i':
1814 this->zoomIn();
1815 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001816 case 'o':
1817 this->zoomOut();
1818 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001819 case 'r':
1820 fRotate = !fRotate;
1821 this->inval(NULL);
1822 this->updateTitle();
1823 return true;
bsalomon@google.come8f09102011-09-08 18:48:12 +00001824 case 'k':
1825 fPerspAnim = !fPerspAnim;
1826 this->inval(NULL);
1827 this->updateTitle();
1828 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001829#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00001830 case '\\':
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001831 this->setDeviceType(kNullGPU_DeviceType);
1832 this->inval(NULL);
1833 this->updateTitle();
bsalomon@google.com74913722011-10-27 20:44:19 +00001834 return true;
twiz@google.com05e70242012-01-27 19:12:00 +00001835 case 'p':
1836 {
1837 GrContext* grContext = this->getGrContext();
1838 if (grContext) {
1839 size_t cacheBytes = grContext->getGpuTextureCacheBytes();
1840 grContext->freeGpuResources();
1841 SkDebugf("Purged %d bytes from the GPU resource cache.\n",
1842 cacheBytes);
1843 }
1844 }
1845 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001846#endif
reed@android.com6c5f6f22009-08-14 16:08:38 +00001847 case 's':
1848 fScale = !fScale;
1849 this->inval(NULL);
1850 this->updateTitle();
1851 return true;
reed@android.com0ae6b242008-12-23 16:49:54 +00001852 default:
1853 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001854 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001855
scroggo@google.com7dadc742012-04-18 14:07:57 +00001856 if (fAppMenu->handleKeyEquivalent(uni)|| fSlideMenu->handleKeyEquivalent(uni)) {
1857 this->onUpdateMenu(fAppMenu);
1858 this->onUpdateMenu(fSlideMenu);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001859 return true;
1860 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001861 return this->INHERITED::onHandleChar(uni);
1862}
1863
yangsu@google.com921091f2011-08-02 13:39:12 +00001864void SampleWindow::setDeviceType(DeviceType type) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001865 if (type == fDeviceType)
1866 return;
1867
1868 fDevManager->tearDownBackend(this);
1869
1870 fDeviceType = type;
1871
bsalomon@google.com11959252012-04-06 20:13:38 +00001872 fDevManager->setUpBackend(this, fMSAASampleCount);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001873
yangsu@google.com921091f2011-08-02 13:39:12 +00001874 this->updateTitle();
1875 this->inval(NULL);
1876}
1877
Scroggo2c8208f2011-06-15 16:49:08 +00001878void SampleWindow::toggleSlideshow() {
1879 fAnimating = !fAnimating;
1880 this->postAnimatingEvent();
1881 this->updateTitle();
1882}
1883
1884void SampleWindow::toggleRendering() {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001885 this->setDeviceType(cycle_devicetype(fDeviceType));
Scroggo2c8208f2011-06-15 16:49:08 +00001886 this->updateTitle();
1887 this->inval(NULL);
1888}
1889
djsollen@google.com6ff82552011-11-07 15:43:57 +00001890void SampleWindow::toggleFPS() {
1891 fMeasureFPS = !fMeasureFPS;
1892 this->updateTitle();
1893 this->inval(NULL);
1894}
1895
reed@android.com8a1c16f2008-12-17 15:59:43 +00001896#include "SkDumpCanvas.h"
1897
1898bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001899 {
1900 SkView* view = curr_view(this);
1901 if (view) {
1902 SkEvent evt(gKeyEvtName);
1903 evt.setFast32(key);
1904 if (view->doQuery(&evt)) {
1905 return true;
1906 }
1907 }
1908 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001909 switch (key) {
1910 case kRight_SkKey:
1911 if (this->nextSample()) {
1912 return true;
1913 }
1914 break;
1915 case kLeft_SkKey:
scroggo@google.come2dd9732012-08-15 20:03:06 +00001916 if (this->previousSample()) {
1917 return true;
1918 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001919 return true;
1920 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001921 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001922 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001923 } else {
1924 fNClip = !fNClip;
1925 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001926 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001927 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001928 return true;
1929 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001930 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001931 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001932 } else {
1933 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001934 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001935 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001936 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001937 case kOK_SkKey: {
1938 SkString title;
1939 if (curr_title(this, &title)) {
1940 writeTitleToPrefs(title.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001941 }
1942 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001943 }
reed@android.com34245c72009-11-03 04:00:48 +00001944 case kBack_SkKey:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001945 this->showOverview();
reed@android.com34245c72009-11-03 04:00:48 +00001946 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001947 default:
1948 break;
1949 }
1950 return this->INHERITED::onHandleKey(key);
1951}
1952
reed@google.com52f57e12011-03-16 12:10:02 +00001953///////////////////////////////////////////////////////////////////////////////
1954
1955static const char gGestureClickType[] = "GestureClickType";
1956
Scroggod3aed392011-06-22 13:26:56 +00001957bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
1958 void* owner) {
Scroggo0f185c22011-03-24 18:35:50 +00001959 if (Click::kMoved_State == state) {
1960 updatePointer(x, y);
1961 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001962 int w = SkScalarRound(this->width());
1963 int h = SkScalarRound(this->height());
1964
1965 // check for the resize-box
1966 if (w - x < 16 && h - y < 16) {
1967 return false; // let the OS handle the click
chudy@google.com4605a3f2012-08-01 17:58:01 +00001968 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001969 else if (fMagnify) {
1970 //it's only necessary to update the drawing if there's a click
1971 this->inval(NULL);
1972 return false; //prevent dragging while magnify is enabled
1973 }
1974 else {
Scroggod3aed392011-06-22 13:26:56 +00001975 return this->INHERITED::onDispatchClick(x, y, state, owner);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001976 }
1977}
1978
reed@google.com52f57e12011-03-16 12:10:02 +00001979class GestureClick : public SkView::Click {
1980public:
1981 GestureClick(SkView* target) : SkView::Click(target) {
1982 this->setType(gGestureClickType);
1983 }
1984
1985 static bool IsGesture(Click* click) {
1986 return click->isType(gGestureClickType);
1987 }
1988};
1989
1990SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1991 return new GestureClick(this);
1992}
1993
1994bool SampleWindow::onClick(Click* click) {
1995 if (GestureClick::IsGesture(click)) {
bsalomon@google.com4d4f2812011-12-12 18:34:01 +00001996 float x = static_cast<float>(click->fICurr.fX);
1997 float y = static_cast<float>(click->fICurr.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001998
reed@google.com52f57e12011-03-16 12:10:02 +00001999 switch (click->fState) {
2000 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00002001 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00002002 break;
2003 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00002004 fGesture.touchMoved(click->fOwner, x, y);
reed@google.comf03bb562011-11-11 21:42:12 +00002005 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002006 break;
2007 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00002008 fGesture.touchEnd(click->fOwner);
reed@google.comf03bb562011-11-11 21:42:12 +00002009 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002010 break;
2011 }
2012 return true;
2013 }
2014 return false;
2015}
2016
2017///////////////////////////////////////////////////////////////////////////////
2018
reed@android.com8a1c16f2008-12-17 15:59:43 +00002019void SampleWindow::loadView(SkView* view) {
2020 SkView::F2BIter iter(this);
2021 SkView* prev = iter.next();
2022 if (prev) {
2023 prev->detachFromParent();
2024 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002025
reed@android.com8a1c16f2008-12-17 15:59:43 +00002026 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00002027 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002028 this->attachChildToFront(view)->unref();
2029 view->setSize(this->width(), this->height());
2030
yangsu@google.com921091f2011-08-02 13:39:12 +00002031 //repopulate the slide menu when a view is loaded
scroggo@google.com7dadc742012-04-18 14:07:57 +00002032 fSlideMenu->reset();
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00002033#ifdef DEBUGGER
2034 if (!is_debugger(view) && !is_overview(view) && !is_transition(view) && fDebugger) {
2035 //Force Pipe to be on if using debugger
scroggo@google.comb073d922012-06-08 15:35:03 +00002036 fPipeState = SkOSMenu::kOnState;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00002037 }
2038#endif
scroggo@google.comb073d922012-06-08 15:35:03 +00002039 (void)SampleView::SetUsePipe(view, fPipeState);
yangsu@google.com921091f2011-08-02 13:39:12 +00002040 if (SampleView::IsSampleView(view))
scroggo@google.com7dadc742012-04-18 14:07:57 +00002041 ((SampleView*)view)->requestMenu(fSlideMenu);
2042 this->onUpdateMenu(fSlideMenu);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002043 this->updateTitle();
2044}
2045
2046static const char* gConfigNames[] = {
2047 "unknown config",
2048 "A1",
2049 "A8",
2050 "Index8",
2051 "565",
2052 "4444",
2053 "8888"
2054};
2055
2056static const char* configToString(SkBitmap::Config c) {
2057 return gConfigNames[c];
2058}
2059
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002060static const char* gDeviceTypePrefix[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002061 "raster: ",
2062 "picture: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002063#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00002064 "opengl: ",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00002065#if SK_ANGLE
2066 "angle: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002067#endif // SK_ANGLE
bsalomon@google.com74913722011-10-27 20:44:19 +00002068 "null-gl: "
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002069#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +00002070};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002071SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gDeviceTypePrefix) == SampleWindow::kDeviceTypeCnt,
2072 array_size_mismatch);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002073
bsalomon@google.com11959252012-04-06 20:13:38 +00002074static const bool gDeviceTypeIsGPU[] = {
2075 false,
2076 false,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002077#if SK_SUPPORT_GPU
bsalomon@google.com11959252012-04-06 20:13:38 +00002078 true,
2079#if SK_ANGLE
2080 true,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002081#endif // SK_ANGLE
bsalomon@google.com11959252012-04-06 20:13:38 +00002082 true
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002083#endif // SK_SUPPORT_GPU
bsalomon@google.com11959252012-04-06 20:13:38 +00002084};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002085SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gDeviceTypeIsGPU) == SampleWindow::kDeviceTypeCnt,
2086 array_size_mismatch);
2087
bsalomon@google.com11959252012-04-06 20:13:38 +00002088
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002089static const char* trystate_str(SkOSMenu::TriState state,
reed@google.com569e0432011-04-05 13:07:03 +00002090 const char trueStr[], const char falseStr[]) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002091 if (SkOSMenu::kOnState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002092 return trueStr;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002093 } else if (SkOSMenu::kOffState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002094 return falseStr;
2095 }
2096 return NULL;
2097}
2098
reed@android.com8a1c16f2008-12-17 15:59:43 +00002099void SampleWindow::updateTitle() {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002100 SkView* view = curr_view(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002101
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002102 SkString title;
2103 if (!curr_title(this, &title)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002104 title.set("<unknown>");
2105 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002106
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002107 title.prepend(gDeviceTypePrefix[fDeviceType]);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002108
2109 title.prepend(" ");
2110 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002111
reed@android.com8a1c16f2008-12-17 15:59:43 +00002112 if (fAnimating) {
2113 title.prepend("<A> ");
2114 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00002115 if (fScale) {
2116 title.prepend("<S> ");
2117 }
2118 if (fRotate) {
2119 title.prepend("<R> ");
2120 }
reed@android.come522ca52009-11-23 20:10:41 +00002121 if (fNClip) {
2122 title.prepend("<C> ");
2123 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00002124 if (fPerspAnim) {
2125 title.prepend("<K> ");
2126 }
reed@google.com569e0432011-04-05 13:07:03 +00002127
2128 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
2129 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00002130 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00002131 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
2132 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002133
2134 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00002135 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002136 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002137
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002138 if (fMeasureFPS) {
reed@google.com43e10142012-09-10 11:52:52 +00002139 title.appendf(" %8.3f ms", fMeasureFPS_Time / (float)FPS_REPEAT_COUNT);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002140 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002141 if (SampleView::IsSampleView(view)) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002142 switch (fPipeState) {
2143 case SkOSMenu::kOnState:
2144 title.prepend("<Pipe> ");
2145 break;
2146 case SkOSMenu::kMixedState:
2147 title.prepend("<Tiled Pipe> ");
2148 break;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002149
scroggo@google.comb073d922012-06-08 15:35:03 +00002150 default:
2151 break;
2152 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002153 title.prepend("! ");
2154 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002155
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002156#if SK_SUPPORT_GPU
bsalomon@google.com11959252012-04-06 20:13:38 +00002157 if (gDeviceTypeIsGPU[fDeviceType] &&
robertphillips@google.com4750fa52012-04-10 13:34:11 +00002158 NULL != fDevManager &&
bsalomon@google.com11959252012-04-06 20:13:38 +00002159 fDevManager->getGrRenderTarget()->numSamples() > 0) {
2160 title.appendf(" [MSAA: %d]",
2161 fDevManager->getGrRenderTarget()->numSamples());
2162 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002163#endif
bsalomon@google.com11959252012-04-06 20:13:38 +00002164
reed@android.com8a1c16f2008-12-17 15:59:43 +00002165 this->setTitle(title.c_str());
2166}
2167
2168void SampleWindow::onSizeChange() {
2169 this->INHERITED::onSizeChange();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002170
reed@android.com8a1c16f2008-12-17 15:59:43 +00002171 SkView::F2BIter iter(this);
2172 SkView* view = iter.next();
2173 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002174
reed@android.com8a1c16f2008-12-17 15:59:43 +00002175 // rebuild our clippath
2176 {
2177 const SkScalar W = this->width();
2178 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002179
reed@android.com8a1c16f2008-12-17 15:59:43 +00002180 fClipPath.reset();
2181#if 0
2182 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
2183 SkRect r;
2184 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
2185 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
2186 fClipPath.addRect(r);
2187 }
2188#else
2189 SkRect r;
2190 r.set(0, 0, W, H);
2191 fClipPath.addRect(r, SkPath::kCCW_Direction);
2192 r.set(W/4, H/4, W*3/4, H*3/4);
2193 fClipPath.addRect(r, SkPath::kCW_Direction);
2194#endif
2195 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002196
Scroggo2c8208f2011-06-15 16:49:08 +00002197 fZoomCenterX = SkScalarHalf(this->width());
2198 fZoomCenterY = SkScalarHalf(this->height());
2199
djsollen@google.com56c69772011-11-08 19:00:26 +00002200#ifdef SK_BUILD_FOR_ANDROID
Scroggob4490c72011-06-17 13:53:05 +00002201 // FIXME: The first draw after a size change does not work on Android, so
2202 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00002203 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00002204#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00002205 this->updateTitle(); // to refresh our config
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002206 fDevManager->windowSizeChanged(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002207}
2208
2209///////////////////////////////////////////////////////////////////////////////
2210
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002211static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002212static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00002213static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002214
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002215bool SampleView::IsSampleView(SkView* view) {
2216 SkEvent evt(is_sample_view_tag);
2217 return view->doQuery(&evt);
2218}
2219
reed@google.comf2183392011-04-22 14:10:48 +00002220bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002221 SkEvent evt(repeat_count_tag);
2222 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00002223 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002224}
2225
scroggo@google.comb073d922012-06-08 15:35:03 +00002226bool SampleView::SetUsePipe(SkView* view, SkOSMenu::TriState state) {
2227 SkEvent evt;
2228 evt.setS32(set_use_pipe_tag, state);
reed@google.com0faac1e2011-05-11 05:58:58 +00002229 return view->doEvent(evt);
2230}
2231
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002232bool SampleView::onEvent(const SkEvent& evt) {
2233 if (evt.isType(repeat_count_tag)) {
2234 fRepeatCount = evt.getFast32();
2235 return true;
2236 }
scroggo@google.comb073d922012-06-08 15:35:03 +00002237 int32_t pipeHolder;
2238 if (evt.findS32(set_use_pipe_tag, &pipeHolder)) {
2239 fPipeState = static_cast<SkOSMenu::TriState>(pipeHolder);
reed@google.com0faac1e2011-05-11 05:58:58 +00002240 return true;
2241 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002242 return this->INHERITED::onEvent(evt);
2243}
2244
2245bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00002246 if (evt->isType(is_sample_view_tag)) {
2247 return true;
2248 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002249 return this->INHERITED::onQuery(evt);
2250}
2251
reed@google.com64e3eb22011-05-04 14:32:04 +00002252
2253class SimplePC : public SkGPipeController {
2254public:
2255 SimplePC(SkCanvas* target);
2256 ~SimplePC();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002257
reed@google.com64e3eb22011-05-04 14:32:04 +00002258 virtual void* requestBlock(size_t minRequest, size_t* actual);
2259 virtual void notifyWritten(size_t bytes);
2260
2261private:
reed@google.com961ddb02011-05-05 14:03:48 +00002262 SkGPipeReader fReader;
2263 void* fBlock;
2264 size_t fBlockSize;
2265 size_t fBytesWritten;
2266 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00002267 SkGPipeReader::Status fStatus;
2268
2269 size_t fTotalWritten;
2270};
2271
2272SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
2273 fBlock = NULL;
2274 fBlockSize = fBytesWritten = 0;
2275 fStatus = SkGPipeReader::kDone_Status;
2276 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00002277 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00002278}
2279
2280SimplePC::~SimplePC() {
2281// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
reed@google.com0faac1e2011-05-11 05:58:58 +00002282 if (fTotalWritten) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002283 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
2284 fAtomsWritten, fStatus);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002285#ifdef PIPE_FILE
scroggo@google.comb073d922012-06-08 15:35:03 +00002286 //File is open in append mode
2287 FILE* f = fopen(FILE_PATH, "ab");
2288 SkASSERT(f != NULL);
2289 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
2290 fclose(f);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002291#endif
2292#ifdef PIPE_NET
scroggo@google.comb073d922012-06-08 15:35:03 +00002293 if (fAtomsWritten > 1 && fTotalWritten > 4) { //ignore done
2294 gServer.acceptConnections();
2295 gServer.writePacket(fBlock, fTotalWritten);
2296 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002297#endif
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00002298#ifdef DEBUGGER
scroggo@google.comb073d922012-06-08 15:35:03 +00002299 gTempDataStore.reset();
2300 gTempDataStore.append(fTotalWritten, (const char*)fBlock);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00002301#endif
reed@google.com0faac1e2011-05-11 05:58:58 +00002302 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002303 sk_free(fBlock);
reed@google.com64e3eb22011-05-04 14:32:04 +00002304}
2305
2306void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
2307 sk_free(fBlock);
2308
2309 fBlockSize = minRequest * 4;
2310 fBlock = sk_malloc_throw(fBlockSize);
2311 fBytesWritten = 0;
2312 *actual = fBlockSize;
2313 return fBlock;
2314}
2315
2316void SimplePC::notifyWritten(size_t bytes) {
2317 SkASSERT(fBytesWritten + bytes <= fBlockSize);
reed@google.com64e3eb22011-05-04 14:32:04 +00002318 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
2319 SkASSERT(SkGPipeReader::kError_Status != fStatus);
2320 fBytesWritten += bytes;
2321 fTotalWritten += bytes;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002322
reed@google.com961ddb02011-05-05 14:03:48 +00002323 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00002324}
2325
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002326void SampleView::draw(SkCanvas* canvas) {
scroggo@google.comb073d922012-06-08 15:35:03 +00002327 if (SkOSMenu::kOffState == fPipeState) {
2328 this->INHERITED::draw(canvas);
2329 } else {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002330 SkGPipeWriter writer;
2331 SimplePC controller(canvas);
scroggo@google.comb073d922012-06-08 15:35:03 +00002332 TiledPipeController tc(canvas->getDevice()->accessBitmap(false),
2333 &canvas->getTotalMatrix());
2334 SkGPipeController* pc;
2335 if (SkOSMenu::kMixedState == fPipeState) {
2336 pc = &tc;
2337 } else {
2338 pc = &controller;
2339 }
reed@google.comdde09562011-05-23 12:21:05 +00002340 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
chudy@google.com4605a3f2012-08-01 17:58:01 +00002341
scroggo@google.comb073d922012-06-08 15:35:03 +00002342 canvas = writer.startRecording(pc, flags);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002343 //Must draw before controller goes out of scope and sends data
2344 this->INHERITED::draw(canvas);
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00002345 //explicitly end recording to ensure writer is flushed before the memory
2346 //is freed in the deconstructor of the controller
2347 writer.endRecording();
reed@google.com0faac1e2011-05-11 05:58:58 +00002348 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002349}
2350void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com81e3d7f2011-06-01 12:42:36 +00002351 this->onDrawBackground(canvas);
2352
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002353 for (int i = 0; i < fRepeatCount; i++) {
scroggo@google.com85cade02012-08-17 13:29:50 +00002354 SkAutoCanvasRestore acr(canvas, true);
2355 this->onDrawContent(canvas);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002356 }
2357}
2358
2359void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00002360 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002361}
2362
2363///////////////////////////////////////////////////////////////////////////////
2364
reed@android.comf2b98d62010-12-20 18:26:13 +00002365template <typename T> void SkTBSort(T array[], int count) {
2366 for (int i = 1; i < count - 1; i++) {
2367 bool didSwap = false;
2368 for (int j = count - 1; j > i; --j) {
2369 if (array[j] < array[j-1]) {
2370 T tmp(array[j-1]);
2371 array[j-1] = array[j];
2372 array[j] = tmp;
2373 didSwap = true;
2374 }
2375 }
2376 if (!didSwap) {
2377 break;
2378 }
2379 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002380
reed@android.comf2b98d62010-12-20 18:26:13 +00002381 for (int k = 0; k < count - 1; k++) {
2382 SkASSERT(!(array[k+1] < array[k]));
2383 }
2384}
2385
2386#include "SkRandom.h"
2387
2388static void rand_rect(SkIRect* rect, SkRandom& rand) {
2389 int bits = 8;
2390 int shift = 32 - bits;
2391 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
2392 rand.nextU() >> shift, rand.nextU() >> shift);
2393 rect->sort();
2394}
2395
2396static void dumpRect(const SkIRect& r) {
2397 SkDebugf(" { %d, %d, %d, %d },\n",
2398 r.fLeft, r.fTop,
2399 r.fRight, r.fBottom);
2400}
2401
2402static void test_rects(const SkIRect rect[], int count) {
2403 SkRegion rgn0, rgn1;
2404
2405 for (int i = 0; i < count; i++) {
2406 rgn0.op(rect[i], SkRegion::kUnion_Op);
2407 // dumpRect(rect[i]);
2408 }
2409 rgn1.setRects(rect, count);
2410
2411 if (rgn0 != rgn1) {
2412 SkDebugf("\n");
2413 for (int i = 0; i < count; i++) {
2414 dumpRect(rect[i]);
2415 }
2416 SkDebugf("\n");
2417 }
2418}
2419
2420static void test() {
2421 size_t i;
2422
2423 const SkIRect r0[] = {
2424 { 0, 0, 1, 1 },
2425 { 2, 2, 3, 3 },
2426 };
2427 const SkIRect r1[] = {
2428 { 0, 0, 1, 3 },
2429 { 1, 1, 2, 2 },
2430 { 2, 0, 3, 3 },
2431 };
2432 const SkIRect r2[] = {
2433 { 0, 0, 1, 2 },
2434 { 2, 1, 3, 3 },
2435 { 4, 0, 5, 1 },
2436 { 6, 0, 7, 4 },
2437 };
2438
2439 static const struct {
2440 const SkIRect* fRects;
2441 int fCount;
2442 } gRecs[] = {
2443 { r0, SK_ARRAY_COUNT(r0) },
2444 { r1, SK_ARRAY_COUNT(r1) },
2445 { r2, SK_ARRAY_COUNT(r2) },
2446 };
2447
2448 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
2449 test_rects(gRecs[i].fRects, gRecs[i].fCount);
2450 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002451
reed@android.comf2b98d62010-12-20 18:26:13 +00002452 SkRandom rand;
2453 for (i = 0; i < 10000; i++) {
2454 SkRegion rgn0, rgn1;
2455
2456 const int N = 8;
2457 SkIRect rect[N];
2458 for (int j = 0; j < N; j++) {
2459 rand_rect(&rect[j], rand);
2460 }
2461 test_rects(rect, N);
2462 }
2463}
2464
caryclark@google.com02939ce2012-06-06 12:09:51 +00002465// FIXME: this should be in a header
2466SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00002467SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
caryclark@google.com02939ce2012-06-06 12:09:51 +00002468 if (false) { // avoid bit rot, suppress warning
2469 test();
2470 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002471 return new SampleWindow(hwnd, argc, argv, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002472}
2473
caryclark@google.com02939ce2012-06-06 12:09:51 +00002474// FIXME: this should be in a header
2475void get_preferred_size(int* x, int* y, int* width, int* height);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002476void get_preferred_size(int* x, int* y, int* width, int* height) {
2477 *x = 10;
2478 *y = 50;
2479 *width = 640;
2480 *height = 480;
2481}
2482
caryclark@google.com5987f582012-10-02 18:33:14 +00002483#ifdef SK_BUILD_FOR_IOS
2484void save_args(int argc, char *argv[]) {
2485}
2486#endif
2487
caryclark@google.com02939ce2012-06-06 12:09:51 +00002488// FIXME: this should be in a header
2489void application_init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002490void application_init() {
2491// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002492#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00002493 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002494#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002495 SkGraphics::Init();
2496 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002497}
2498
caryclark@google.com02939ce2012-06-06 12:09:51 +00002499// FIXME: this should be in a header
2500void application_term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002501void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002502 SkEvent::Term();
2503 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002504}