blob: 92004f9e315dc0a9d8ef45d54b9fc3cff203d3e5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
Scroggo2c8208f2011-06-15 16:49:08 +00008#include "SampleApp.h"
9
reed@google.com8a85d0c2011-06-24 19:12:12 +000010#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkCanvas.h"
12#include "SkDevice.h"
reed@google.comaf951c92011-06-16 19:10:39 +000013#include "SkGpuDevice.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkGraphics.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +000015#include "SkImageEncoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkPaint.h"
17#include "SkPicture.h"
18#include "SkStream.h"
reed@android.com44177402009-11-23 21:07:51 +000019#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkWindow.h"
21
22#include "SampleCode.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023#include "GrContext.h"
Scroggo0f185c22011-03-24 18:35:50 +000024#include "SkTypeface.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000025
Scroggo3e7ff9f2011-06-16 15:31:26 +000026#include "GrGLInterface.h"
Scroggo2c8208f2011-06-15 16:49:08 +000027
Scroggo8ac0d542011-06-21 14:44:57 +000028#include "SkPDFDevice.h"
29#include "SkPDFDocument.h"
30#include "SkStream.h"
31
yangsu@google.com1f394212011-06-01 18:03:34 +000032#define TEST_GPIPEx
33
34#ifdef TEST_GPIPE
35#define PIPE_FILE
36#define FILE_PATH "/path/to/drawing.data"
37#endif
38
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +000039#define USE_ARROWS_FOR_ZOOM true
reed@android.comf2b98d62010-12-20 18:26:13 +000040//#define DEFAULT_TO_GPU
41
reed@android.come191b162009-12-18 21:33:39 +000042extern SkView* create_overview(int, const SkViewFactory[]);
reed@android.com34245c72009-11-03 04:00:48 +000043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044#define ANIMATING_EVENTTYPE "nextSample"
45#define ANIMATING_DELAY 750
46
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000047#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +000048 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000049#else
reed@google.combad8c872011-05-18 20:10:31 +000050 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000051#endif
reed@google.combad8c872011-05-18 20:10:31 +000052#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000053
reed@google.com3cec4d72011-07-06 13:59:47 +000054static SampleWindow* gSampleWindow;
55
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000056///////////////
bsalomon@google.com098e96d2011-07-14 14:30:46 +000057class SampleWindow::DefaultDeviceManager : public SampleWindow::DeviceManager {
58public:
59
60 DefaultDeviceManager() {
61 fGrRenderTarget = NULL;
62 fGrContext = NULL;
63 }
64
65 virtual ~DefaultDeviceManager() {
66 SkSafeUnref(fGrRenderTarget);
67 SkSafeUnref(fGrContext);
68 }
69
70 virtual void init(SampleWindow* win) {
71 win->attachGL();
72 if (NULL == fGrContext) {
73 fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL);
74 }
75 if (NULL == fGrContext) {
76 SkDebugf("Failed to setup 3D");
77 win->detachGL();
78 }
79 }
80
81 virtual bool supportsDeviceType(SampleWindow::DeviceType dType) {
82 switch (dType) {
83 case kRaster_DeviceType:
84 case kPicture_DeviceType: // fallthru
85 return true;
86 case kGPU_DeviceType:
87 return NULL != fGrContext && NULL != fGrRenderTarget;
88 default:
89 return false;
90 }
91 }
92
93 virtual bool prepareCanvas(SampleWindow::DeviceType dType,
94 SkCanvas* canvas,
95 SampleWindow* win) {
96 if (kGPU_DeviceType == dType) {
97 if (fGrContext) {
98 canvas->setDevice(new SkGpuDevice(fGrContext,
99 fGrRenderTarget))->unref();
100 } else {
101 return false;
102 }
103 }
104 return true;
105 }
106
107 virtual void publishCanvas(SampleWindow::DeviceType dType,
108 SkCanvas* canvas,
109 SampleWindow* win) {
110 if (fGrContext) {
111 // in case we have queued drawing calls
112 fGrContext->flush();
113 if (dType != kGPU_DeviceType) {
114 // need to send the raster bits to the (gpu) window
115 fGrContext->setRenderTarget(fGrRenderTarget);
116 const SkBitmap& bm = win->getBitmap();
117 fGrContext->writePixels(0, 0, bm.width(), bm.height(),
118 kRGBA_8888_GrPixelConfig, bm.getPixels(),
119 bm.rowBytes());
120 }
121 }
122 win->presentGL();
123 }
124
125 virtual void windowSizeChanged(SampleWindow* win) {
126 if (fGrContext) {
127 win->attachGL();
128
129 GrPlatformSurfaceDesc desc;
130 desc.reset();
131 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
132 desc.fWidth = SkScalarRound(win->width());
133 desc.fHeight = SkScalarRound(win->height());
134 desc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000135 GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, &desc.fStencilBits);
136 GR_GL_GetIntegerv(GR_GL_SAMPLES, &desc.fSampleCnt);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000137 GrGLint buffer;
138 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer);
139 desc.fPlatformRenderTarget = buffer;
140
141 SkSafeUnref(fGrRenderTarget);
142 fGrRenderTarget = static_cast<GrRenderTarget*>(
143 fGrContext->createPlatformSurface(desc));
144 }
145 }
146
147 virtual GrContext* getGrContext() {
148 return fGrContext;
149 }
150private:
151 GrContext* fGrContext;
152 GrRenderTarget* fGrRenderTarget;
153};
154
155///////////////
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000156static const char view_inval_msg[] = "view-inval-msg";
157
Scroggo62b65b02011-06-21 16:01:26 +0000158void SampleWindow::postInvalDelay() {
reed@google.com87fac4a2011-08-04 13:50:17 +0000159 (new SkEvent(view_inval_msg, this->getSinkID()))->postDelay(1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000160}
161
162static bool isInvalEvent(const SkEvent& evt) {
163 return evt.isType(view_inval_msg);
164}
165//////////////////
166
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167SkViewRegister* SkViewRegister::gHead;
168SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) {
169 static bool gOnce;
170 if (!gOnce) {
171 gHead = NULL;
172 gOnce = true;
173 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000174
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 fChain = gHead;
176 gHead = this;
177}
178
reed@google.com29038ed2011-07-06 17:56:47 +0000179#if 0
reed@google.comf0b5f682011-03-11 20:08:25 +0000180#include <CoreFoundation/CoreFoundation.h>
181#include <CoreFoundation/CFURLAccess.h>
182
183static void testpdf() {
184 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
185 kCFStringEncodingUTF8);
186 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
187 kCFURLPOSIXPathStyle,
188 false);
189 CFRelease(path);
190 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
191 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
192 CFRelease(url);
193
194 CGContextBeginPage(cg, &box);
195 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
196 CGContextFillEllipseInRect(cg, r);
197 CGContextEndPage(cg);
198 CGContextRelease(cg);
199
200 if (false) {
201 SkBitmap bm;
202 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
203 bm.allocPixels();
204 bm.eraseColor(0);
205
206 SkCanvas canvas(bm);
207
208 }
209}
210#endif
211
212//////////////////////////////////////////////////////////////////////////////
213
reed@google.com569e0432011-04-05 13:07:03 +0000214enum FlipAxisEnum {
215 kFlipAxis_X = (1 << 0),
216 kFlipAxis_Y = (1 << 1)
217};
218
reed@google.com569e0432011-04-05 13:07:03 +0000219static SkTriState cycle_tristate(SkTriState state) {
220 static const SkTriState gCycle[] = {
221 /* kFalse_SkTriState -> */ kUnknown_SkTriState,
222 /* kTrue_SkTriState -> */ kFalse_SkTriState,
223 /* kUnknown_SkTriState -> */ kTrue_SkTriState,
224 };
225 return gCycle[state];
226}
227
reed@google.comf0b5f682011-03-11 20:08:25 +0000228#include "SkDrawFilter.h"
229
reed@google.com569e0432011-04-05 13:07:03 +0000230class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000231public:
reed@google.com09e3baa2011-05-18 12:04:31 +0000232 FlagsDrawFilter(SkTriState lcd, SkTriState aa, SkTriState filter,
233 SkTriState hinting) :
234 fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000235
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000236 virtual void filter(SkPaint* paint, Type t) {
reed@google.com569e0432011-04-05 13:07:03 +0000237 if (kText_Type == t && kUnknown_SkTriState != fLCDState) {
reed@google.com569e0432011-04-05 13:07:03 +0000238 paint->setLCDRenderText(kTrue_SkTriState == fLCDState);
239 }
240 if (kUnknown_SkTriState != fAAState) {
reed@google.com569e0432011-04-05 13:07:03 +0000241 paint->setAntiAlias(kTrue_SkTriState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000242 }
reed@google.com176753a2011-05-17 15:32:04 +0000243 if (kUnknown_SkTriState != fFilterState) {
244 paint->setFilterBitmap(kTrue_SkTriState == fFilterState);
245 }
reed@google.com09e3baa2011-05-18 12:04:31 +0000246 if (kUnknown_SkTriState != fHintingState) {
247 paint->setHinting(kTrue_SkTriState == fHintingState ?
248 SkPaint::kNormal_Hinting :
249 SkPaint::kSlight_Hinting);
250 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000251 }
252
253private:
reed@google.com569e0432011-04-05 13:07:03 +0000254 SkTriState fLCDState;
reed@google.com569e0432011-04-05 13:07:03 +0000255 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000256 SkTriState fFilterState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000257 SkTriState fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000258};
259
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260//////////////////////////////////////////////////////////////////////////////
261
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000262#define MAX_ZOOM_LEVEL 8
263#define MIN_ZOOM_LEVEL -8
264
reed@android.comf2b98d62010-12-20 18:26:13 +0000265static const char gCharEvtName[] = "SampleCode_Char_Event";
266static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000267static const char gTitleEvtName[] = "SampleCode_Title_Event";
268static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000269static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
270
271bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
272 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
273 if (outUni) {
274 *outUni = evt.getFast32();
275 }
276 return true;
277 }
278 return false;
279}
280
281bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
282 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
283 if (outKey) {
284 *outKey = (SkKey)evt.getFast32();
285 }
286 return true;
287 }
288 return false;
289}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290
291bool SampleCode::TitleQ(const SkEvent& evt) {
292 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
293}
294
295void SampleCode::TitleR(SkEvent* evt, const char title[]) {
296 SkASSERT(evt && TitleQ(*evt));
297 evt->setString(gTitleEvtName, title);
298}
299
300bool SampleCode::PrefSizeQ(const SkEvent& evt) {
301 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
302}
303
304void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
305 SkASSERT(evt && PrefSizeQ(*evt));
306 SkScalar size[2];
307 size[0] = width;
308 size[1] = height;
309 evt->setScalars(gPrefSizeEvtName, 2, size);
310}
311
reed@android.comf2b98d62010-12-20 18:26:13 +0000312bool SampleCode::FastTextQ(const SkEvent& evt) {
313 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
314}
315
316///////////////////////////////////////////////////////////////////////////////
317
reed@android.com44177402009-11-23 21:07:51 +0000318static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000319static SkMSec gAnimTimePrev;
320
reed@android.com44177402009-11-23 21:07:51 +0000321SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000322SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
323SkScalar SampleCode::GetAnimSecondsDelta() {
324 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
325}
reed@android.com44177402009-11-23 21:07:51 +0000326
327SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000328 // since gAnimTime can be up to 32 bits, we can't convert it to a float
329 // or we'll lose the low bits. Hence we use doubles for the intermediate
330 // calculations
331 double seconds = (double)gAnimTime / 1000.0;
332 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000333 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000334 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000335 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000336 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000337}
338
reed@google.com3cec4d72011-07-06 13:59:47 +0000339GrContext* SampleCode::GetGr() {
340 return gSampleWindow ? gSampleWindow->getGrContext() : NULL;
341}
342
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343//////////////////////////////////////////////////////////////////////////////
344
reed@android.comf2b98d62010-12-20 18:26:13 +0000345static SkView* curr_view(SkWindow* wind) {
346 SkView::F2BIter iter(wind);
347 return iter.next();
348}
349
Scroggo2c8208f2011-06-15 16:49:08 +0000350void SampleWindow::setZoomCenter(float x, float y)
351{
352 fZoomCenterX = SkFloatToScalar(x);
353 fZoomCenterY = SkFloatToScalar(y);
354}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000355
Scroggo0f185c22011-03-24 18:35:50 +0000356bool SampleWindow::zoomIn()
357{
358 // Arbitrarily decided
359 if (fFatBitsScale == 25) return false;
360 fFatBitsScale++;
361 this->inval(NULL);
362 return true;
363}
364
365bool SampleWindow::zoomOut()
366{
367 if (fFatBitsScale == 1) return false;
368 fFatBitsScale--;
369 this->inval(NULL);
370 return true;
371}
372
373void SampleWindow::toggleZoomer()
374{
375 fShowZoomer = !fShowZoomer;
376 this->inval(NULL);
377}
378
379void SampleWindow::updatePointer(int x, int y)
380{
381 fMouseX = x;
382 fMouseY = y;
383 if (fShowZoomer) {
384 this->inval(NULL);
385 }
386}
387
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000388static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
389 static const SampleWindow::DeviceType gCT[] = {
390 SampleWindow::kPicture_DeviceType,
391 SampleWindow::kGPU_DeviceType,
392 SampleWindow::kRaster_DeviceType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000393 };
394 return gCT[ct];
395}
396
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000397SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager) : INHERITED(hwnd) {
reed@google.com3cec4d72011-07-06 13:59:47 +0000398 gSampleWindow = this;
399
yangsu@google.com1f394212011-06-01 18:03:34 +0000400#ifdef PIPE_FILE
401 //Clear existing file or create file if it doesn't exist
402 FILE* f = fopen(FILE_PATH, "wb");
403 fclose(f);
404#endif
405
reed@android.com8a1c16f2008-12-17 15:59:43 +0000406 fPicture = NULL;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000407
reed@android.comf2b98d62010-12-20 18:26:13 +0000408#ifdef DEFAULT_TO_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000409 fDeviceType = kGPU_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000410#else
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000411 fDeviceType = kRaster_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000412#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000413 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000414 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000415 fRepeatDrawing = false;
416 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000417 fRotate = false;
418 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000419 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000420 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000421 fMeasureFPS = false;
reed@google.com569e0432011-04-05 13:07:03 +0000422 fLCDState = kUnknown_SkTriState;
423 fAAState = kUnknown_SkTriState;
reed@google.com66f22fd2011-05-17 15:33:45 +0000424 fFilterState = kUnknown_SkTriState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000425 fHintingState = kUnknown_SkTriState;
reed@google.com569e0432011-04-05 13:07:03 +0000426 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427 fScrollTestX = fScrollTestY = 0;
428
Scroggo0f185c22011-03-24 18:35:50 +0000429 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000430 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000431 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
432 fShowZoomer = false;
yangsu@google.com921091f2011-08-02 13:39:12 +0000433
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000434 fZoomLevel = 0;
435 fZoomScale = SK_Scalar1;
436
Scroggo8ac0d542011-06-21 14:44:57 +0000437 fSaveToPdf = false;
438 fPdfCanvas = NULL;
439
yangsu@google.com921091f2011-08-02 13:39:12 +0000440 int sinkID = this->getSinkID();
441 fAppMenu.setTitle("Global Settings");
442 fAppMenu.appendList("Device Type", "Device Type", sinkID, 0, "Raster", "Picture", "OpenGL", NULL);
443 fAppMenu.appendTriState("AA", "AA", sinkID, SkOSMenu::kMixedState);
444 fAppMenu.appendTriState("LCD", "LCD", sinkID, SkOSMenu::kMixedState);
445 fAppMenu.appendTriState("Filter", "Filter", sinkID, SkOSMenu::kMixedState);
446 fAppMenu.appendTriState("Hinting", "Hinting", sinkID, SkOSMenu::kMixedState);
447 fAppMenu.appendSwitch("Pipe", "Pipe" , sinkID, fUsePipe);
448 fAppMenu.appendSwitch("Slide Show", "Slide Show" , sinkID, false);
449 fAppMenu.appendSwitch("Clip", "Clip" , sinkID, fUseClip);
450 fAppMenu.appendSwitch("Measure FPS", "Measure FPS" , sinkID, fMeasureFPS);
451 fAppMenu.appendSwitch("Flip X", "Flip X" , sinkID, false);
452 fAppMenu.appendSwitch("Flip Y", "Flip Y" , sinkID, false);
453 fAppMenu.appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
454 fAppMenu.appendAction("Save to PDF", sinkID);
455
456 this->addMenu(&fAppMenu);
457 this->addMenu(&fSlideMenu);
458
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000459// this->setConfig(SkBitmap::kRGB_565_Config);
460 this->setConfig(SkBitmap::kARGB_8888_Config);
461 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000462 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000463
reed@android.com34245c72009-11-03 04:00:48 +0000464 {
465 const SkViewRegister* reg = SkViewRegister::Head();
466 while (reg) {
467 *fSamples.append() = reg->factory();
468 reg = reg->next();
469 }
470 }
471 fCurrIndex = 0;
senorblanco@chromium.org78b82532011-06-28 19:44:03 +0000472 if (argc > 1) {
473 int i, count = fSamples.count();
474 for (i = 0; i < count; i++) {
475 SkString title = getSampleTitle(i);
476 if (title.equals(argv[1])) {
477 fCurrIndex = i;
478 break;
479 }
480 }
481 if (i == count) {
482 fprintf(stderr, "Unknown sample \"%s\"\n", argv[1]);
483 }
484 }
reed@android.come0f13ee2009-11-04 19:40:25 +0000485 this->loadView(fSamples[fCurrIndex]());
bsalomon@google.com840e9f32011-07-06 21:59:09 +0000486
487 fPDFData = NULL;
reed@google.comf0b5f682011-03-11 20:08:25 +0000488
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000489 if (NULL == devManager) {
490 fDevManager = new DefaultDeviceManager();
491 } else {
492 devManager->ref();
493 fDevManager = devManager;
494 }
495 fDevManager->init(this);
496
Scroggob4490c72011-06-17 13:53:05 +0000497 // If another constructor set our dimensions, ensure that our
498 // onSizeChange gets called.
499 if (this->height() && this->width()) {
500 this->onSizeChange();
501 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000502}
503
504SampleWindow::~SampleWindow() {
505 delete fPicture;
Scroggo8ac0d542011-06-21 14:44:57 +0000506 delete fPdfCanvas;
Scroggo0f185c22011-03-24 18:35:50 +0000507 fTypeface->unref();
reed@google.com29038ed2011-07-06 17:56:47 +0000508
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000509 SkSafeUnref(fDevManager);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000510}
511
reed@android.com55e76b22009-11-23 21:46:47 +0000512static SkBitmap capture_bitmap(SkCanvas* canvas) {
513 SkBitmap bm;
514 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
515 src.copyTo(&bm, src.config());
516 return bm;
517}
518
519static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
520 SkBitmap* diff) {
521 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000522
reed@android.com55e76b22009-11-23 21:46:47 +0000523 SkAutoLockPixels alp0(src);
524 SkAutoLockPixels alp1(orig);
525 for (int y = 0; y < src.height(); y++) {
526 const void* srcP = src.getAddr(0, y);
527 const void* origP = orig.getAddr(0, y);
528 size_t bytes = src.width() * src.bytesPerPixel();
529 if (memcmp(srcP, origP, bytes)) {
530 SkDebugf("---------- difference on line %d\n", y);
531 return true;
532 }
533 }
534 return false;
535}
536
Scroggo0f185c22011-03-24 18:35:50 +0000537static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
538{
539 SkColor desiredColor = paint.getColor();
540 paint.setColor(SK_ColorWHITE);
541 const char* c_str = string.c_str();
542 size_t size = string.size();
543 SkRect bounds;
544 paint.measureText(c_str, size, &bounds);
545 bounds.offset(left, top);
546 SkScalar inset = SkIntToScalar(-2);
547 bounds.inset(inset, inset);
548 canvas->drawRect(bounds, paint);
549 if (desiredColor != SK_ColorBLACK) {
550 paint.setColor(SK_ColorBLACK);
551 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
552 }
553 paint.setColor(desiredColor);
554 canvas->drawText(c_str, size, left, top, paint);
555}
556
reed@android.com44177402009-11-23 21:07:51 +0000557#define XCLIP_N 8
558#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000559
560void SampleWindow::draw(SkCanvas* canvas) {
reed@google.com29038ed2011-07-06 17:56:47 +0000561
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000562 if (!fDevManager->prepareCanvas(fDeviceType, canvas, this)) {
563 return;
564 }
reed@android.com44177402009-11-23 21:07:51 +0000565 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000566 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000567 gAnimTime = SkTime::GetMSecs();
568
Scroggo2c8208f2011-06-15 16:49:08 +0000569 SkScalar cx = fZoomCenterX;
570 SkScalar cy = fZoomCenterY;
reed@google.com569e0432011-04-05 13:07:03 +0000571
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000572 if (fZoomLevel) {
573 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000574 SkPoint center;
575 m = canvas->getTotalMatrix();//.invert(&m);
576 m.mapXY(cx, cy, &center);
577 cx = center.fX;
578 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000579
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000580 m.setTranslate(-cx, -cy);
581 m.postScale(fZoomScale, fZoomScale);
582 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000583
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000584 canvas->concat(m);
585 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000586
reed@google.com569e0432011-04-05 13:07:03 +0000587 if (fFlipAxis) {
588 SkMatrix m;
589 m.setTranslate(cx, cy);
590 if (fFlipAxis & kFlipAxis_X) {
591 m.preScale(-SK_Scalar1, SK_Scalar1);
592 }
593 if (fFlipAxis & kFlipAxis_Y) {
594 m.preScale(SK_Scalar1, -SK_Scalar1);
595 }
596 m.preTranslate(-cx, -cy);
597 canvas->concat(m);
598 }
yangsu@google.com921091f2011-08-02 13:39:12 +0000599
reed@google.com52f57e12011-03-16 12:10:02 +0000600 // Apply any gesture matrix
601 if (true) {
602 const SkMatrix& localM = fGesture.localM();
603 if (localM.getType() & SkMatrix::kScale_Mask) {
604 canvas->setExternalMatrix(&localM);
605 }
606 canvas->concat(localM);
607 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000608
reed@google.com52f57e12011-03-16 12:10:02 +0000609 if (fGesture.isActive()) {
610 this->inval(NULL);
611 }
612 }
yangsu@google.com921091f2011-08-02 13:39:12 +0000613
reed@android.come522ca52009-11-23 20:10:41 +0000614 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000615 this->INHERITED::draw(canvas);
616 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000617
618 const SkScalar w = this->width();
619 const SkScalar h = this->height();
620 const SkScalar cw = w / XCLIP_N;
621 const SkScalar ch = h / YCLIP_N;
622 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000623 SkRect r;
624 r.fTop = y * ch;
625 r.fBottom = (y + 1) * ch;
626 if (y == YCLIP_N - 1) {
627 r.fBottom = h;
628 }
reed@android.come522ca52009-11-23 20:10:41 +0000629 for (int x = 0; x < XCLIP_N; x++) {
630 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000631 r.fLeft = x * cw;
632 r.fRight = (x + 1) * cw;
633 if (x == XCLIP_N - 1) {
634 r.fRight = w;
635 }
reed@android.come522ca52009-11-23 20:10:41 +0000636 canvas->clipRect(r);
637 this->INHERITED::draw(canvas);
638 }
639 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000640
reed@android.com55e76b22009-11-23 21:46:47 +0000641 SkBitmap diff;
642 if (bitmap_diff(canvas, orig, &diff)) {
643 }
reed@android.come522ca52009-11-23 20:10:41 +0000644 } else {
645 this->INHERITED::draw(canvas);
646 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000647 if (fShowZoomer && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +0000648 showZoomer(canvas);
649 }
yangsu@google.com921091f2011-08-02 13:39:12 +0000650
reed@google.com29038ed2011-07-06 17:56:47 +0000651 // do this last
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000652 fDevManager->publishCanvas(fDeviceType, canvas, this);
Scroggo3272ba82011-05-25 20:50:42 +0000653}
654
655void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +0000656 int count = canvas->save();
657 canvas->resetMatrix();
658 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000659 int width = SkScalarRound(this->width());
660 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000661 if (fMouseX >= width) fMouseX = width - 1;
662 else if (fMouseX < 0) fMouseX = 0;
663 if (fMouseY >= height) fMouseY = height - 1;
664 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000665
Scroggo0f185c22011-03-24 18:35:50 +0000666 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000667 bitmap.lockPixels();
668
Scroggo0f185c22011-03-24 18:35:50 +0000669 // 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 +0000670 int zoomedWidth = (width >> 1) | 1;
671 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000672 SkIRect src;
673 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
674 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
675 SkRect dest;
676 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
677 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
678 SkPaint paint;
679 // Clear the background behind our zoomed in view
680 paint.setColor(SK_ColorWHITE);
681 canvas->drawRect(dest, paint);
682 canvas->drawBitmapRect(bitmap, &src, dest);
683 paint.setColor(SK_ColorBLACK);
684 paint.setStyle(SkPaint::kStroke_Style);
685 // Draw a border around the pixel in the middle
686 SkRect originalPixel;
687 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
688 SkMatrix matrix;
689 SkRect scalarSrc;
690 scalarSrc.set(src);
691 SkColor color = bitmap.getColor(fMouseX, fMouseY);
692 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
693 SkRect pixel;
694 matrix.mapRect(&pixel, originalPixel);
695 // TODO Perhaps measure the values and make the outline white if it's "dark"
696 if (color == SK_ColorBLACK) {
697 paint.setColor(SK_ColorWHITE);
698 }
699 canvas->drawRect(pixel, paint);
700 }
701 paint.setColor(SK_ColorBLACK);
702 // Draw a border around the destination rectangle
703 canvas->drawRect(dest, paint);
704 paint.setStyle(SkPaint::kStrokeAndFill_Style);
705 // Identify the pixel and its color on screen
706 paint.setTypeface(fTypeface);
707 paint.setAntiAlias(true);
708 SkScalar lineHeight = paint.getFontMetrics(NULL);
709 SkString string;
710 string.appendf("(%i, %i)", fMouseX, fMouseY);
711 SkScalar left = dest.fLeft + SkIntToScalar(3);
712 SkScalar i = SK_Scalar1;
713 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
714 // Alpha
715 i += SK_Scalar1;
716 string.reset();
717 string.appendf("A: %X", SkColorGetA(color));
718 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
719 // Red
720 i += SK_Scalar1;
721 string.reset();
722 string.appendf("R: %X", SkColorGetR(color));
723 paint.setColor(SK_ColorRED);
724 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
725 // Green
726 i += SK_Scalar1;
727 string.reset();
728 string.appendf("G: %X", SkColorGetG(color));
729 paint.setColor(SK_ColorGREEN);
730 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
731 // Blue
732 i += SK_Scalar1;
733 string.reset();
734 string.appendf("B: %X", SkColorGetB(color));
735 paint.setColor(SK_ColorBLUE);
736 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
737 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +0000738}
739
reed@android.com8a1c16f2008-12-17 15:59:43 +0000740void SampleWindow::onDraw(SkCanvas* canvas) {
741 if (fRepeatDrawing) {
742 this->inval(NULL);
743 }
744}
745
746#include "SkColorPriv.h"
747
748static void reverseRedAndBlue(const SkBitmap& bm) {
749 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
750 uint8_t* p = (uint8_t*)bm.getPixels();
751 uint8_t* stop = p + bm.getSize();
752 while (p < stop) {
753 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
754 unsigned scale = SkAlpha255To256(p[3]);
755 unsigned r = p[2];
756 unsigned b = p[0];
757 p[0] = SkAlphaMul(r, scale);
758 p[1] = SkAlphaMul(p[1], scale);
759 p[2] = SkAlphaMul(b, scale);
760 p += 4;
761 }
762}
763
Scroggo8ac0d542011-06-21 14:44:57 +0000764void SampleWindow::saveToPdf()
765{
766 fSaveToPdf = true;
767 this->inval(NULL);
768}
769
reed@android.com8a1c16f2008-12-17 15:59:43 +0000770SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +0000771 if (fSaveToPdf) {
772 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
773 SkISize size = SkISize::Make(bmp.width(), bmp.height());
774 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
775 canvas->getTotalMatrix());
776 fPdfCanvas = new SkCanvas(pdfDevice);
777 pdfDevice->unref();
778 canvas = fPdfCanvas;
779 } else {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000780 switch (fDeviceType) {
781 case kRaster_DeviceType:
782 case kGPU_DeviceType:
reed@android.comf2b98d62010-12-20 18:26:13 +0000783 canvas = this->INHERITED::beforeChildren(canvas);
Scroggo8ac0d542011-06-21 14:44:57 +0000784 break;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000785 case kPicture_DeviceType:
Scroggo8ac0d542011-06-21 14:44:57 +0000786 fPicture = new SkPicture;
787 canvas = fPicture->beginRecording(9999, 9999);
788 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000789 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000790 }
791
792 if (fUseClip) {
793 canvas->drawColor(0xFFFF88FF);
794 canvas->clipPath(fClipPath);
795 }
796
797 return canvas;
798}
799
800static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
801 const SkRegion& rgn) {
802 SkCanvas canvas(bm);
803 SkRegion inval(rgn);
804
805 inval.translate(r.fLeft, r.fTop);
806 canvas.clipRegion(inval);
807 canvas.drawColor(0xFFFF8080);
808}
yangsu@google.com501775e2011-06-24 16:04:50 +0000809#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +0000810void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +0000811 if (fSaveToPdf) {
812 fSaveToPdf = false;
813 if (fShowZoomer) {
814 showZoomer(fPdfCanvas);
815 }
816 SkString name;
817 name.printf("%s.pdf", this->getTitle());
818 SkPDFDocument doc;
819 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
820 doc.appendPage(device);
821#ifdef ANDROID
822 name.prepend("/sdcard/");
823#endif
yangsu@google.com501775e2011-06-24 16:04:50 +0000824
825#ifdef SK_BUILD_FOR_IOS
826 SkDynamicMemoryWStream mstream;
827 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +0000828 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +0000829#endif
Scroggo8ac0d542011-06-21 14:44:57 +0000830 SkFILEWStream stream(name.c_str());
831 if (stream.isValid()) {
832 doc.emitPDF(&stream);
833 const char* desc = "File saved from Skia SampleApp";
834 this->onPDFSaved(this->getTitle(), desc, name.c_str());
835 }
yangsu@google.com501775e2011-06-24 16:04:50 +0000836
Scroggo8ac0d542011-06-21 14:44:57 +0000837 delete fPdfCanvas;
838 fPdfCanvas = NULL;
839
840 // We took over the draw calls in order to create the PDF, so we need
841 // to redraw.
842 this->inval(NULL);
843 return;
844 }
845
reed@android.comf2b98d62010-12-20 18:26:13 +0000846 if (fRequestGrabImage) {
847 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000848
reed@google.com29038ed2011-07-06 17:56:47 +0000849 SkDevice* device = orig->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000850 SkBitmap bmp;
851 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000852 static int gSampleGrabCounter;
853 SkString name;
854 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000855 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000856 SkImageEncoder::kPNG_Type, 100);
857 }
858 }
859
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000860 if (kPicture_DeviceType == fDeviceType) {
861 if (true) {
862 SkPicture* pict = new SkPicture(*fPicture);
863 fPicture->unref();
864 orig->drawPicture(*pict);
865 pict->unref();
866 } else if (true) {
867 SkDynamicMemoryWStream ostream;
868 fPicture->serialize(&ostream);
869 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000870
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000871 SkAutoDataUnref data(ostream.copyToData());
872 SkMemoryStream istream(data.data(), data.size());
873 SkPicture pict(&istream);
874 orig->drawPicture(pict);
875 } else {
876 fPicture->draw(orig);
877 fPicture->unref();
878 }
879 fPicture = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000880 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000881
reed@google.com17d7aec2011-04-25 14:31:44 +0000882 // Do this after presentGL and other finishing, rather than in afterChild
883 if (fMeasureFPS && fMeasureFPS_Time) {
884 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
885 this->updateTitle();
Scroggo62b65b02011-06-21 16:01:26 +0000886 this->postInvalDelay();
reed@google.com17d7aec2011-04-25 14:31:44 +0000887 }
888
889 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000890 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000891 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
892 int dx = fScrollTestX * 7;
893 int dy = fScrollTestY * 7;
894 SkIRect r;
895 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000896
reed@android.com8a1c16f2008-12-17 15:59:43 +0000897 r.set(50, 50, 50+100, 50+100);
898 bm.scrollRect(&r, dx, dy, &inval);
899 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000900 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000901}
902
reed@android.com6c5f6f22009-08-14 16:08:38 +0000903void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
904 if (fScale) {
905 SkScalar scale = SK_Scalar1 * 7 / 10;
906 SkScalar cx = this->width() / 2;
907 SkScalar cy = this->height() / 2;
908 canvas->translate(cx, cy);
909 canvas->scale(scale, scale);
910 canvas->translate(-cx, -cy);
911 }
912 if (fRotate) {
913 SkScalar cx = this->width() / 2;
914 SkScalar cy = this->height() / 2;
915 canvas->translate(cx, cy);
916 canvas->rotate(SkIntToScalar(30));
917 canvas->translate(-cx, -cy);
918 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000919
reed@google.com09e3baa2011-05-18 12:04:31 +0000920 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
921 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000922
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000923 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000924 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
925 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
926 fMeasureFPS_Time = SkTime::GetMSecs();
927 }
928 } else {
929 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000930 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000931 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000932}
933
934void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000935 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000936}
937
reed@android.com8a1c16f2008-12-17 15:59:43 +0000938static SkBitmap::Config gConfigCycle[] = {
939 SkBitmap::kNo_Config, // none -> none
940 SkBitmap::kNo_Config, // a1 -> none
941 SkBitmap::kNo_Config, // a8 -> none
942 SkBitmap::kNo_Config, // index8 -> none
943 SkBitmap::kARGB_4444_Config, // 565 -> 4444
944 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
945 SkBitmap::kRGB_565_Config // 8888 -> 565
946};
947
948static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
949 return gConfigCycle[c];
950}
951
djsollen@google.come32b5832011-06-13 16:58:40 +0000952void SampleWindow::changeZoomLevel(float delta) {
953 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000954 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000955 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
956 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000957 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000958 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
959 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000960 } else {
961 fZoomScale = SK_Scalar1;
962 }
963
djsollen@google.come32b5832011-06-13 16:58:40 +0000964 this->updateTitle();
965
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000966 this->inval(NULL);
967}
968
Scroggo2c8208f2011-06-15 16:49:08 +0000969bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +0000970 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
Scroggo2c8208f2011-06-15 16:49:08 +0000971 this->loadView(fSamples[fCurrIndex]());
972 return true;
973}
974
reed@android.com8a1c16f2008-12-17 15:59:43 +0000975bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000976 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
977 this->loadView(fSamples[fCurrIndex]());
978 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000979}
980
yangsu@google.com501775e2011-06-24 16:04:50 +0000981bool SampleWindow::goToSample(int i) {
982 fCurrIndex = (i) % fSamples.count();
983 this->loadView(fSamples[fCurrIndex]());
984 return true;
985}
986
987SkString SampleWindow::getSampleTitle(int i) {
988 SkView* view = fSamples[i]();
989 SkString title;
990 SkEvent evt(gTitleEvtName);
991 if (view->doQuery(&evt)) {
992 title.set(evt.findString(gTitleEvtName));
993 }
994 view->unref();
995 return title;
996}
997
998int SampleWindow::sampleCount() {
999 return fSamples.count();
1000}
1001
Scroggo2c8208f2011-06-15 16:49:08 +00001002void SampleWindow::postAnimatingEvent() {
1003 if (fAnimating) {
reed@google.com87fac4a2011-08-04 13:50:17 +00001004 (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
Scroggo2c8208f2011-06-15 16:49:08 +00001005 }
1006}
1007
reed@android.com8a1c16f2008-12-17 15:59:43 +00001008bool SampleWindow::onEvent(const SkEvent& evt) {
1009 if (evt.isType(ANIMATING_EVENTTYPE)) {
1010 if (fAnimating) {
1011 this->nextSample();
1012 this->postAnimatingEvent();
1013 }
1014 return true;
1015 }
reed@android.com34245c72009-11-03 04:00:48 +00001016 if (evt.isType("set-curr-index")) {
1017 fCurrIndex = evt.getFast32() % fSamples.count();
1018 this->loadView(fSamples[fCurrIndex]());
1019 return true;
1020 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001021 if (isInvalEvent(evt)) {
1022 this->inval(NULL);
1023 return true;
1024 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001025 int selected = -1;
1026 if (SkOSMenu::FindListIndex(&evt, "Device Type", &selected)) {
1027 this->setDeviceType((DeviceType)selected);
1028 return true;
1029 }
1030 SkOSMenu::TriState state;
1031 if (SkOSMenu::FindTriState(&evt, "AA", &state)) {
1032 fAAState = (SkTriState)state;
1033 this->updateTitle();
1034 this->inval(NULL);
1035 }
1036 if (SkOSMenu::FindTriState(&evt, "LCD", &state)) {
1037 fLCDState = (SkTriState)state;
1038 this->updateTitle();
1039 this->inval(NULL);
1040 }
1041 if (SkOSMenu::FindTriState(&evt, "Filter", &state)) {
1042 fFilterState = (SkTriState)state;
1043 this->updateTitle();
1044 this->inval(NULL);
1045 }
1046 if (SkOSMenu::FindTriState(&evt, "Hinting", &state)) {
1047 fHintingState = (SkTriState)state;
1048 this->updateTitle();
1049 this->inval(NULL);
1050 }
1051 if (SkOSMenu::FindSwitchState(&evt, "Pipe", NULL)) {
1052 this->togglePipe();
1053 return true;
1054 }
1055 if (SkOSMenu::FindSwitchState(&evt, "Slide Show", NULL)) {
1056 this->toggleSlideshow();
1057 return true;
1058 }
1059 if (SkOSMenu::FindSwitchState(&evt, "Clip", NULL)) {
1060 fUseClip = !fUseClip;
1061 this->inval(NULL);
1062 this->updateTitle();
1063 return true;
1064 }
1065 if (SkOSMenu::FindSwitchState(&evt, "Measure FPS", NULL)) {
1066 this->toggleFPS();
1067 return true;
1068 }
1069 if (SkOSMenu::FindSwitchState(&evt, "Flip X", NULL)) {
1070 fFlipAxis ^= kFlipAxis_X;
1071 this->updateTitle();
1072 this->inval(NULL);
1073 return true;
1074 }
1075 if (SkOSMenu::FindSwitchState(&evt, "Flip Y", NULL)) {
1076 fFlipAxis ^= kFlipAxis_Y;
1077 this->updateTitle();
1078 this->inval(NULL);
1079 return true;
1080 }
1081 if (SkOSMenu::FindSwitchState(&evt, "Zoomer", NULL)) {
1082 this->toggleZoomer();
1083 return true;
1084 }
1085 if (SkOSMenu::FindAction(&evt,"Save to PDF")) {
1086 this->saveToPdf();
1087 return true;
1088 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001089 return this->INHERITED::onEvent(evt);
1090}
1091
reed@android.comf2b98d62010-12-20 18:26:13 +00001092bool SampleWindow::onQuery(SkEvent* query) {
1093 if (query->isType("get-slide-count")) {
1094 query->setFast32(fSamples.count());
1095 return true;
1096 }
1097 if (query->isType("get-slide-title")) {
1098 SkView* view = fSamples[query->getFast32()]();
1099 SkEvent evt(gTitleEvtName);
1100 if (view->doQuery(&evt)) {
1101 query->setString("title", evt.findString(gTitleEvtName));
1102 }
1103 SkSafeUnref(view);
1104 return true;
1105 }
1106 if (query->isType("use-fast-text")) {
1107 SkEvent evt(gFastTextEvtName);
1108 return curr_view(this)->doQuery(&evt);
1109 }
reed@google.com29038ed2011-07-06 17:56:47 +00001110 if (query->isType("ignore-window-bitmap")) {
1111 query->setFast32(this->getGrContext() != NULL);
1112 return true;
1113 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001114 return this->INHERITED::onQuery(query);
1115}
1116
reed@android.com0ae6b242008-12-23 16:49:54 +00001117static void cleanup_for_filename(SkString* name) {
1118 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001119 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001120 switch (str[i]) {
1121 case ':': str[i] = '-'; break;
1122 case '/': str[i] = '-'; break;
1123 case ' ': str[i] = '_'; break;
1124 default: break;
1125 }
1126 }
1127}
reed@android.com8a1c16f2008-12-17 15:59:43 +00001128
1129bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001130 {
1131 SkView* view = curr_view(this);
1132 if (view) {
1133 SkEvent evt(gCharEvtName);
1134 evt.setFast32(uni);
1135 if (view->doQuery(&evt)) {
1136 return true;
1137 }
1138 }
1139 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001140
reed@android.com8a1c16f2008-12-17 15:59:43 +00001141 int dx = 0xFF;
1142 int dy = 0xFF;
1143
1144 switch (uni) {
1145 case '5': dx = 0; dy = 0; break;
1146 case '8': dx = 0; dy = -1; break;
1147 case '6': dx = 1; dy = 0; break;
1148 case '2': dx = 0; dy = 1; break;
1149 case '4': dx = -1; dy = 0; break;
1150 case '7': dx = -1; dy = -1; break;
1151 case '9': dx = 1; dy = -1; break;
1152 case '3': dx = 1; dy = 1; break;
1153 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001154
reed@android.com8a1c16f2008-12-17 15:59:43 +00001155 default:
1156 break;
1157 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001158
reed@android.com8a1c16f2008-12-17 15:59:43 +00001159 if (0xFF != dx && 0xFF != dy) {
1160 if ((dx | dy) == 0) {
1161 fScrollTestX = fScrollTestY = 0;
1162 } else {
1163 fScrollTestX += dx;
1164 fScrollTestY += dy;
1165 }
1166 this->inval(NULL);
1167 return true;
1168 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001169
reed@android.com0ae6b242008-12-23 16:49:54 +00001170 switch (uni) {
1171 case 'a':
Scroggo2c8208f2011-06-15 16:49:08 +00001172 this->toggleSlideshow();
reed@android.com0ae6b242008-12-23 16:49:54 +00001173 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001174 case 'b':
1175 fAAState = cycle_tristate(fAAState);
1176 this->updateTitle();
1177 this->inval(NULL);
1178 break;
1179 case 'c':
1180 fUseClip = !fUseClip;
1181 this->inval(NULL);
1182 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001183 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001184 case 'd':
1185 SkGraphics::SetFontCacheUsed(0);
1186 return true;
Scroggo8ac0d542011-06-21 14:44:57 +00001187 case 'e':
1188 this->saveToPdf();
1189 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001190 case 'f':
Scroggo2c8208f2011-06-15 16:49:08 +00001191 this->toggleFPS();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001192 break;
1193 case 'g':
1194 fRequestGrabImage = true;
1195 this->inval(NULL);
1196 break;
reed@google.com09e3baa2011-05-18 12:04:31 +00001197 case 'h':
1198 fHintingState = cycle_tristate(fHintingState);
1199 this->updateTitle();
1200 this->inval(NULL);
1201 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001202 case 'i':
1203 this->zoomIn();
1204 break;
1205 case 'l':
1206 fLCDState = cycle_tristate(fLCDState);
1207 this->updateTitle();
1208 this->inval(NULL);
1209 break;
reed@google.com176753a2011-05-17 15:32:04 +00001210 case 'n':
1211 fFilterState = cycle_tristate(fFilterState);
1212 this->updateTitle();
1213 this->inval(NULL);
1214 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001215 case 'o':
1216 this->zoomOut();
1217 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001218 case 'p':
1219 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001220 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001221 this->inval(NULL);
1222 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001223 case 'r':
1224 fRotate = !fRotate;
1225 this->inval(NULL);
1226 this->updateTitle();
1227 return true;
1228 case 's':
1229 fScale = !fScale;
1230 this->inval(NULL);
1231 this->updateTitle();
1232 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001233 case 'x':
1234 fFlipAxis ^= kFlipAxis_X;
1235 this->updateTitle();
1236 this->inval(NULL);
1237 break;
1238 case 'y':
1239 fFlipAxis ^= kFlipAxis_Y;
1240 this->updateTitle();
1241 this->inval(NULL);
1242 break;
scroggo08526c02011-03-22 14:03:21 +00001243 case 'z':
1244 this->toggleZoomer();
1245 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001246 default:
1247 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001248 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001249
reed@android.com8a1c16f2008-12-17 15:59:43 +00001250 return this->INHERITED::onHandleChar(uni);
1251}
1252
yangsu@google.com921091f2011-08-02 13:39:12 +00001253void SampleWindow::setDeviceType(DeviceType type) {
1254 if (type != fDeviceType && fDevManager->supportsDeviceType(fDeviceType))
1255 fDeviceType = type;
1256 this->updateTitle();
1257 this->inval(NULL);
1258}
1259
Scroggo2c8208f2011-06-15 16:49:08 +00001260void SampleWindow::toggleFPS() {
1261 fMeasureFPS = !fMeasureFPS;
1262 this->inval(NULL);
1263 this->updateTitle();
1264}
1265
1266void SampleWindow::toggleSlideshow() {
1267 fAnimating = !fAnimating;
1268 this->postAnimatingEvent();
1269 this->updateTitle();
1270}
1271
1272void SampleWindow::toggleRendering() {
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001273 DeviceType origDevType = fDeviceType;
1274 do {
1275 fDeviceType = cycle_devicetype(fDeviceType);
1276 } while (origDevType != fDeviceType &&
1277 !fDevManager->supportsDeviceType(fDeviceType));
Scroggo2c8208f2011-06-15 16:49:08 +00001278 this->updateTitle();
1279 this->inval(NULL);
1280}
1281
yangsu@google.com921091f2011-08-02 13:39:12 +00001282void SampleWindow::togglePipe() {
1283 fUsePipe = !fUsePipe;
1284 this->updateTitle();
1285}
1286
reed@android.com8a1c16f2008-12-17 15:59:43 +00001287#include "SkDumpCanvas.h"
1288
1289bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001290 {
1291 SkView* view = curr_view(this);
1292 if (view) {
1293 SkEvent evt(gKeyEvtName);
1294 evt.setFast32(key);
1295 if (view->doQuery(&evt)) {
1296 return true;
1297 }
1298 }
1299 }
1300
reed@android.com8a1c16f2008-12-17 15:59:43 +00001301 switch (key) {
1302 case kRight_SkKey:
1303 if (this->nextSample()) {
1304 return true;
1305 }
1306 break;
1307 case kLeft_SkKey:
Scroggo2c8208f2011-06-15 16:49:08 +00001308 toggleRendering();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001309 return true;
1310 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001311 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001312 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001313 } else {
1314 fNClip = !fNClip;
1315 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001316 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001317 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001318 return true;
1319 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001320 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001321 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001322 } else {
1323 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001324 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001325 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001326 return true;
1327 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001328 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001329 SkDebugfDumper dumper;
1330 SkDumpCanvas dc(&dumper);
1331 this->draw(&dc);
1332 } else {
1333 fRepeatDrawing = !fRepeatDrawing;
1334 if (fRepeatDrawing) {
1335 this->inval(NULL);
1336 }
1337 }
1338 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001339 case kBack_SkKey:
1340 this->loadView(NULL);
1341 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001342 default:
1343 break;
1344 }
1345 return this->INHERITED::onHandleKey(key);
1346}
1347
reed@google.com52f57e12011-03-16 12:10:02 +00001348///////////////////////////////////////////////////////////////////////////////
1349
1350static const char gGestureClickType[] = "GestureClickType";
1351
Scroggod3aed392011-06-22 13:26:56 +00001352bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
1353 void* owner) {
Scroggo0f185c22011-03-24 18:35:50 +00001354 if (Click::kMoved_State == state) {
1355 updatePointer(x, y);
1356 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001357 int w = SkScalarRound(this->width());
1358 int h = SkScalarRound(this->height());
1359
1360 // check for the resize-box
1361 if (w - x < 16 && h - y < 16) {
1362 return false; // let the OS handle the click
1363 } else {
Scroggod3aed392011-06-22 13:26:56 +00001364 return this->INHERITED::onDispatchClick(x, y, state, owner);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001365 }
1366}
1367
reed@google.com52f57e12011-03-16 12:10:02 +00001368class GestureClick : public SkView::Click {
1369public:
1370 GestureClick(SkView* target) : SkView::Click(target) {
1371 this->setType(gGestureClickType);
1372 }
1373
1374 static bool IsGesture(Click* click) {
1375 return click->isType(gGestureClickType);
1376 }
1377};
1378
1379SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1380 return new GestureClick(this);
1381}
1382
1383bool SampleWindow::onClick(Click* click) {
1384 if (GestureClick::IsGesture(click)) {
1385 float x = SkScalarToFloat(click->fCurr.fX);
1386 float y = SkScalarToFloat(click->fCurr.fY);
1387 switch (click->fState) {
1388 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00001389 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001390 break;
1391 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00001392 fGesture.touchMoved(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001393 this->inval(NULL);
1394 break;
1395 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00001396 fGesture.touchEnd(click->fOwner);
reed@google.com52f57e12011-03-16 12:10:02 +00001397 this->inval(NULL);
1398 break;
1399 }
1400 return true;
1401 }
1402 return false;
1403}
1404
1405///////////////////////////////////////////////////////////////////////////////
1406
reed@android.com8a1c16f2008-12-17 15:59:43 +00001407void SampleWindow::loadView(SkView* view) {
1408 SkView::F2BIter iter(this);
1409 SkView* prev = iter.next();
1410 if (prev) {
1411 prev->detachFromParent();
1412 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001413
reed@android.com34245c72009-11-03 04:00:48 +00001414 if (NULL == view) {
1415 view = create_overview(fSamples.count(), fSamples.begin());
1416 }
reed@google.com87fac4a2011-08-04 13:50:17 +00001417
reed@android.com8a1c16f2008-12-17 15:59:43 +00001418 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001419 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001420 this->attachChildToFront(view)->unref();
1421 view->setSize(this->width(), this->height());
1422
yangsu@google.com921091f2011-08-02 13:39:12 +00001423 //repopulate the slide menu when a view is loaded
1424 fSlideMenu.reset();
1425 if (SampleView::IsSampleView(view))
1426 ((SampleView*)view)->requestMenus(&fSlideMenu);
1427 this->onUpdateMenu(&fSlideMenu);
1428
reed@android.com8a1c16f2008-12-17 15:59:43 +00001429 this->updateTitle();
1430}
1431
1432static const char* gConfigNames[] = {
1433 "unknown config",
1434 "A1",
1435 "A8",
1436 "Index8",
1437 "565",
1438 "4444",
1439 "8888"
1440};
1441
1442static const char* configToString(SkBitmap::Config c) {
1443 return gConfigNames[c];
1444}
1445
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001446static const char* gDeviceTypePrefix[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001447 "raster: ",
1448 "picture: ",
1449 "opengl: "
1450};
1451
reed@google.com569e0432011-04-05 13:07:03 +00001452static const char* trystate_str(SkTriState state,
1453 const char trueStr[], const char falseStr[]) {
1454 if (kTrue_SkTriState == state) {
1455 return trueStr;
1456 } else if (kFalse_SkTriState == state) {
1457 return falseStr;
1458 }
1459 return NULL;
1460}
1461
reed@android.com8a1c16f2008-12-17 15:59:43 +00001462void SampleWindow::updateTitle() {
1463 SkString title;
1464
1465 SkView::F2BIter iter(this);
1466 SkView* view = iter.next();
1467 SkEvent evt(gTitleEvtName);
1468 if (view->doQuery(&evt)) {
1469 title.set(evt.findString(gTitleEvtName));
1470 }
1471 if (title.size() == 0) {
1472 title.set("<unknown>");
1473 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001474
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001475 title.prepend(gDeviceTypePrefix[fDeviceType]);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001476
1477 title.prepend(" ");
1478 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001479
reed@android.com8a1c16f2008-12-17 15:59:43 +00001480 if (fAnimating) {
1481 title.prepend("<A> ");
1482 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001483 if (fScale) {
1484 title.prepend("<S> ");
1485 }
1486 if (fRotate) {
1487 title.prepend("<R> ");
1488 }
reed@android.come522ca52009-11-23 20:10:41 +00001489 if (fNClip) {
1490 title.prepend("<C> ");
1491 }
reed@google.com569e0432011-04-05 13:07:03 +00001492
1493 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1494 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001495 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001496 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1497 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001498
1499 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001500 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001501 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001502
1503 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001504 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001505 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001506 if (fUsePipe && SampleView::IsSampleView(view)) {
1507 title.prepend("<P> ");
1508 }
1509 if (SampleView::IsSampleView(view)) {
1510 title.prepend("! ");
1511 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001512
reed@android.com8a1c16f2008-12-17 15:59:43 +00001513 this->setTitle(title.c_str());
1514}
1515
1516void SampleWindow::onSizeChange() {
1517 this->INHERITED::onSizeChange();
reed@google.com29038ed2011-07-06 17:56:47 +00001518
reed@android.com8a1c16f2008-12-17 15:59:43 +00001519 SkView::F2BIter iter(this);
1520 SkView* view = iter.next();
1521 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001522
reed@android.com8a1c16f2008-12-17 15:59:43 +00001523 // rebuild our clippath
1524 {
1525 const SkScalar W = this->width();
1526 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001527
reed@android.com8a1c16f2008-12-17 15:59:43 +00001528 fClipPath.reset();
1529#if 0
1530 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1531 SkRect r;
1532 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1533 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1534 fClipPath.addRect(r);
1535 }
1536#else
1537 SkRect r;
1538 r.set(0, 0, W, H);
1539 fClipPath.addRect(r, SkPath::kCCW_Direction);
1540 r.set(W/4, H/4, W*3/4, H*3/4);
1541 fClipPath.addRect(r, SkPath::kCW_Direction);
1542#endif
1543 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001544
Scroggo2c8208f2011-06-15 16:49:08 +00001545 fZoomCenterX = SkScalarHalf(this->width());
1546 fZoomCenterY = SkScalarHalf(this->height());
1547
Scroggo3e7ff9f2011-06-16 15:31:26 +00001548#ifdef ANDROID
Scroggob4490c72011-06-17 13:53:05 +00001549 // FIXME: The first draw after a size change does not work on Android, so
1550 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00001551 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00001552#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001553 this->updateTitle(); // to refresh our config
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001554 fDevManager->windowSizeChanged(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001555}
1556
1557///////////////////////////////////////////////////////////////////////////////
1558
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001559static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001560static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001561static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001562
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001563bool SampleView::IsSampleView(SkView* view) {
1564 SkEvent evt(is_sample_view_tag);
1565 return view->doQuery(&evt);
1566}
1567
reed@google.comf2183392011-04-22 14:10:48 +00001568bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001569 SkEvent evt(repeat_count_tag);
1570 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001571 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001572}
1573
reed@google.com0faac1e2011-05-11 05:58:58 +00001574bool SampleView::SetUsePipe(SkView* view, bool pred) {
1575 SkEvent evt(set_use_pipe_tag);
1576 evt.setFast32(pred);
1577 return view->doEvent(evt);
1578}
1579
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001580bool SampleView::onEvent(const SkEvent& evt) {
1581 if (evt.isType(repeat_count_tag)) {
1582 fRepeatCount = evt.getFast32();
1583 return true;
1584 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001585 if (evt.isType(set_use_pipe_tag)) {
1586 fUsePipe = !!evt.getFast32();
1587 return true;
1588 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001589 return this->INHERITED::onEvent(evt);
1590}
1591
1592bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001593 if (evt->isType(is_sample_view_tag)) {
1594 return true;
1595 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001596 return this->INHERITED::onQuery(evt);
1597}
1598
reed@google.com68f456d2011-05-02 18:55:39 +00001599#ifdef TEST_GPIPE
1600 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001601
1602class SimplePC : public SkGPipeController {
1603public:
1604 SimplePC(SkCanvas* target);
1605 ~SimplePC();
1606
1607 virtual void* requestBlock(size_t minRequest, size_t* actual);
1608 virtual void notifyWritten(size_t bytes);
1609
1610private:
reed@google.com961ddb02011-05-05 14:03:48 +00001611 SkGPipeReader fReader;
1612 void* fBlock;
1613 size_t fBlockSize;
1614 size_t fBytesWritten;
1615 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001616 SkGPipeReader::Status fStatus;
1617
1618 size_t fTotalWritten;
1619};
1620
1621SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1622 fBlock = NULL;
1623 fBlockSize = fBytesWritten = 0;
1624 fStatus = SkGPipeReader::kDone_Status;
1625 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001626 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001627}
1628
1629SimplePC::~SimplePC() {
1630// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1631 sk_free(fBlock);
1632
reed@google.com0faac1e2011-05-11 05:58:58 +00001633 if (fTotalWritten) {
1634 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1635 fAtomsWritten, fStatus);
1636 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001637}
1638
1639void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1640 sk_free(fBlock);
1641
1642 fBlockSize = minRequest * 4;
1643 fBlock = sk_malloc_throw(fBlockSize);
1644 fBytesWritten = 0;
1645 *actual = fBlockSize;
1646 return fBlock;
1647}
1648
1649void SimplePC::notifyWritten(size_t bytes) {
1650 SkASSERT(fBytesWritten + bytes <= fBlockSize);
yangsu@google.com1f394212011-06-01 18:03:34 +00001651
1652#ifdef PIPE_FILE
1653 //File is open in append mode
1654 FILE* f = fopen(FILE_PATH, "ab");
1655 SkASSERT(f != NULL);
1656 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
1657 fclose(f);
1658#endif
1659
reed@google.com64e3eb22011-05-04 14:32:04 +00001660 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1661 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1662 fBytesWritten += bytes;
1663 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001664
1665 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001666}
1667
reed@google.com68f456d2011-05-02 18:55:39 +00001668#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001669
reed@google.com64e3eb22011-05-04 14:32:04 +00001670
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001671void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001672#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001673 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001674 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001675 if (fUsePipe) {
reed@google.comdde09562011-05-23 12:21:05 +00001676 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
1677// flags = 0;
1678 canvas = writer.startRecording(&controller, flags);
reed@google.com0faac1e2011-05-11 05:58:58 +00001679 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001680#endif
1681
reed@google.com81e3d7f2011-06-01 12:42:36 +00001682 this->onDrawBackground(canvas);
1683
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001684 for (int i = 0; i < fRepeatCount; i++) {
1685 SkAutoCanvasRestore acr(canvas, true);
1686 this->onDrawContent(canvas);
1687 }
1688}
1689
1690void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001691 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001692}
1693
1694///////////////////////////////////////////////////////////////////////////////
1695
reed@android.comf2b98d62010-12-20 18:26:13 +00001696template <typename T> void SkTBSort(T array[], int count) {
1697 for (int i = 1; i < count - 1; i++) {
1698 bool didSwap = false;
1699 for (int j = count - 1; j > i; --j) {
1700 if (array[j] < array[j-1]) {
1701 T tmp(array[j-1]);
1702 array[j-1] = array[j];
1703 array[j] = tmp;
1704 didSwap = true;
1705 }
1706 }
1707 if (!didSwap) {
1708 break;
1709 }
1710 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001711
reed@android.comf2b98d62010-12-20 18:26:13 +00001712 for (int k = 0; k < count - 1; k++) {
1713 SkASSERT(!(array[k+1] < array[k]));
1714 }
1715}
1716
1717#include "SkRandom.h"
1718
1719static void rand_rect(SkIRect* rect, SkRandom& rand) {
1720 int bits = 8;
1721 int shift = 32 - bits;
1722 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1723 rand.nextU() >> shift, rand.nextU() >> shift);
1724 rect->sort();
1725}
1726
1727static void dumpRect(const SkIRect& r) {
1728 SkDebugf(" { %d, %d, %d, %d },\n",
1729 r.fLeft, r.fTop,
1730 r.fRight, r.fBottom);
1731}
1732
1733static void test_rects(const SkIRect rect[], int count) {
1734 SkRegion rgn0, rgn1;
1735
1736 for (int i = 0; i < count; i++) {
1737 rgn0.op(rect[i], SkRegion::kUnion_Op);
1738 // dumpRect(rect[i]);
1739 }
1740 rgn1.setRects(rect, count);
1741
1742 if (rgn0 != rgn1) {
1743 SkDebugf("\n");
1744 for (int i = 0; i < count; i++) {
1745 dumpRect(rect[i]);
1746 }
1747 SkDebugf("\n");
1748 }
1749}
1750
1751static void test() {
1752 size_t i;
1753
1754 const SkIRect r0[] = {
1755 { 0, 0, 1, 1 },
1756 { 2, 2, 3, 3 },
1757 };
1758 const SkIRect r1[] = {
1759 { 0, 0, 1, 3 },
1760 { 1, 1, 2, 2 },
1761 { 2, 0, 3, 3 },
1762 };
1763 const SkIRect r2[] = {
1764 { 0, 0, 1, 2 },
1765 { 2, 1, 3, 3 },
1766 { 4, 0, 5, 1 },
1767 { 6, 0, 7, 4 },
1768 };
1769
1770 static const struct {
1771 const SkIRect* fRects;
1772 int fCount;
1773 } gRecs[] = {
1774 { r0, SK_ARRAY_COUNT(r0) },
1775 { r1, SK_ARRAY_COUNT(r1) },
1776 { r2, SK_ARRAY_COUNT(r2) },
1777 };
1778
1779 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1780 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1781 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001782
reed@android.comf2b98d62010-12-20 18:26:13 +00001783 SkRandom rand;
1784 for (i = 0; i < 10000; i++) {
1785 SkRegion rgn0, rgn1;
1786
1787 const int N = 8;
1788 SkIRect rect[N];
1789 for (int j = 0; j < N; j++) {
1790 rand_rect(&rect[j], rand);
1791 }
1792 test_rects(rect, N);
1793 }
1794}
1795
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00001796SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001797// test();
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001798 return new SampleWindow(hwnd, argc, argv, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001799}
1800
1801void get_preferred_size(int* x, int* y, int* width, int* height) {
1802 *x = 10;
1803 *y = 50;
1804 *width = 640;
1805 *height = 480;
1806}
1807
1808void application_init() {
1809// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001810#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001811 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001812#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001813 SkGraphics::Init();
1814 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001815}
1816
1817void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001818 SkEvent::Term();
1819 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001820}