blob: 0d70f660df697c6e322aa1b48aee6387fce0802f [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() {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000159 SkEvent* evt = new SkEvent(view_inval_msg);
Scroggo62b65b02011-06-21 16:01:26 +0000160 evt->post(this->getSinkID(), 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000161}
162
163static bool isInvalEvent(const SkEvent& evt) {
164 return evt.isType(view_inval_msg);
165}
166//////////////////
167
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168SkViewRegister* SkViewRegister::gHead;
169SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) {
170 static bool gOnce;
171 if (!gOnce) {
172 gHead = NULL;
173 gOnce = true;
174 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000175
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 fChain = gHead;
177 gHead = this;
178}
179
reed@google.com29038ed2011-07-06 17:56:47 +0000180#if 0
reed@google.comf0b5f682011-03-11 20:08:25 +0000181#include <CoreFoundation/CoreFoundation.h>
182#include <CoreFoundation/CFURLAccess.h>
183
184static void testpdf() {
185 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
186 kCFStringEncodingUTF8);
187 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
188 kCFURLPOSIXPathStyle,
189 false);
190 CFRelease(path);
191 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
192 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
193 CFRelease(url);
194
195 CGContextBeginPage(cg, &box);
196 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
197 CGContextFillEllipseInRect(cg, r);
198 CGContextEndPage(cg);
199 CGContextRelease(cg);
200
201 if (false) {
202 SkBitmap bm;
203 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
204 bm.allocPixels();
205 bm.eraseColor(0);
206
207 SkCanvas canvas(bm);
208
209 }
210}
211#endif
212
213//////////////////////////////////////////////////////////////////////////////
214
reed@google.com569e0432011-04-05 13:07:03 +0000215enum FlipAxisEnum {
216 kFlipAxis_X = (1 << 0),
217 kFlipAxis_Y = (1 << 1)
218};
219
reed@google.com569e0432011-04-05 13:07:03 +0000220static SkTriState cycle_tristate(SkTriState state) {
221 static const SkTriState gCycle[] = {
222 /* kFalse_SkTriState -> */ kUnknown_SkTriState,
223 /* kTrue_SkTriState -> */ kFalse_SkTriState,
224 /* kUnknown_SkTriState -> */ kTrue_SkTriState,
225 };
226 return gCycle[state];
227}
228
reed@google.comf0b5f682011-03-11 20:08:25 +0000229#include "SkDrawFilter.h"
230
reed@google.com569e0432011-04-05 13:07:03 +0000231class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000232public:
reed@google.com09e3baa2011-05-18 12:04:31 +0000233 FlagsDrawFilter(SkTriState lcd, SkTriState aa, SkTriState filter,
234 SkTriState hinting) :
235 fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000236
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000237 virtual void filter(SkPaint* paint, Type t) {
reed@google.com569e0432011-04-05 13:07:03 +0000238 if (kText_Type == t && kUnknown_SkTriState != fLCDState) {
reed@google.com569e0432011-04-05 13:07:03 +0000239 paint->setLCDRenderText(kTrue_SkTriState == fLCDState);
240 }
241 if (kUnknown_SkTriState != fAAState) {
reed@google.com569e0432011-04-05 13:07:03 +0000242 paint->setAntiAlias(kTrue_SkTriState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000243 }
reed@google.com176753a2011-05-17 15:32:04 +0000244 if (kUnknown_SkTriState != fFilterState) {
245 paint->setFilterBitmap(kTrue_SkTriState == fFilterState);
246 }
reed@google.com09e3baa2011-05-18 12:04:31 +0000247 if (kUnknown_SkTriState != fHintingState) {
248 paint->setHinting(kTrue_SkTriState == fHintingState ?
249 SkPaint::kNormal_Hinting :
250 SkPaint::kSlight_Hinting);
251 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000252 }
253
254private:
reed@google.com569e0432011-04-05 13:07:03 +0000255 SkTriState fLCDState;
reed@google.com569e0432011-04-05 13:07:03 +0000256 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000257 SkTriState fFilterState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000258 SkTriState fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000259};
260
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261//////////////////////////////////////////////////////////////////////////////
262
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000263#define MAX_ZOOM_LEVEL 8
264#define MIN_ZOOM_LEVEL -8
265
reed@android.comf2b98d62010-12-20 18:26:13 +0000266static const char gCharEvtName[] = "SampleCode_Char_Event";
267static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268static const char gTitleEvtName[] = "SampleCode_Title_Event";
269static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000270static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
271
272bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
273 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
274 if (outUni) {
275 *outUni = evt.getFast32();
276 }
277 return true;
278 }
279 return false;
280}
281
282bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
283 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
284 if (outKey) {
285 *outKey = (SkKey)evt.getFast32();
286 }
287 return true;
288 }
289 return false;
290}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291
292bool SampleCode::TitleQ(const SkEvent& evt) {
293 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
294}
295
296void SampleCode::TitleR(SkEvent* evt, const char title[]) {
297 SkASSERT(evt && TitleQ(*evt));
298 evt->setString(gTitleEvtName, title);
299}
300
301bool SampleCode::PrefSizeQ(const SkEvent& evt) {
302 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
303}
304
305void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
306 SkASSERT(evt && PrefSizeQ(*evt));
307 SkScalar size[2];
308 size[0] = width;
309 size[1] = height;
310 evt->setScalars(gPrefSizeEvtName, 2, size);
311}
312
reed@android.comf2b98d62010-12-20 18:26:13 +0000313bool SampleCode::FastTextQ(const SkEvent& evt) {
314 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
315}
316
317///////////////////////////////////////////////////////////////////////////////
318
reed@android.com44177402009-11-23 21:07:51 +0000319static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000320static SkMSec gAnimTimePrev;
321
reed@android.com44177402009-11-23 21:07:51 +0000322SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000323SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
324SkScalar SampleCode::GetAnimSecondsDelta() {
325 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
326}
reed@android.com44177402009-11-23 21:07:51 +0000327
328SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000329 // since gAnimTime can be up to 32 bits, we can't convert it to a float
330 // or we'll lose the low bits. Hence we use doubles for the intermediate
331 // calculations
332 double seconds = (double)gAnimTime / 1000.0;
333 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000334 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000335 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000336 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000337 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000338}
339
reed@google.com3cec4d72011-07-06 13:59:47 +0000340GrContext* SampleCode::GetGr() {
341 return gSampleWindow ? gSampleWindow->getGrContext() : NULL;
342}
343
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344//////////////////////////////////////////////////////////////////////////////
345
reed@android.comf2b98d62010-12-20 18:26:13 +0000346static SkView* curr_view(SkWindow* wind) {
347 SkView::F2BIter iter(wind);
348 return iter.next();
349}
350
Scroggo2c8208f2011-06-15 16:49:08 +0000351void SampleWindow::setZoomCenter(float x, float y)
352{
353 fZoomCenterX = SkFloatToScalar(x);
354 fZoomCenterY = SkFloatToScalar(y);
355}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000356
Scroggo0f185c22011-03-24 18:35:50 +0000357bool SampleWindow::zoomIn()
358{
359 // Arbitrarily decided
360 if (fFatBitsScale == 25) return false;
361 fFatBitsScale++;
362 this->inval(NULL);
363 return true;
364}
365
366bool SampleWindow::zoomOut()
367{
368 if (fFatBitsScale == 1) return false;
369 fFatBitsScale--;
370 this->inval(NULL);
371 return true;
372}
373
374void SampleWindow::toggleZoomer()
375{
376 fShowZoomer = !fShowZoomer;
377 this->inval(NULL);
378}
379
380void SampleWindow::updatePointer(int x, int y)
381{
382 fMouseX = x;
383 fMouseY = y;
384 if (fShowZoomer) {
385 this->inval(NULL);
386 }
387}
388
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000389static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
390 static const SampleWindow::DeviceType gCT[] = {
391 SampleWindow::kPicture_DeviceType,
392 SampleWindow::kGPU_DeviceType,
393 SampleWindow::kRaster_DeviceType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000394 };
395 return gCT[ct];
396}
397
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000398SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager) : INHERITED(hwnd) {
reed@google.com3cec4d72011-07-06 13:59:47 +0000399 gSampleWindow = this;
400
yangsu@google.com1f394212011-06-01 18:03:34 +0000401#ifdef PIPE_FILE
402 //Clear existing file or create file if it doesn't exist
403 FILE* f = fopen(FILE_PATH, "wb");
404 fclose(f);
405#endif
406
reed@android.com8a1c16f2008-12-17 15:59:43 +0000407 fPicture = NULL;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000408
reed@android.comf2b98d62010-12-20 18:26:13 +0000409#ifdef DEFAULT_TO_GPU
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000410 fDeviceType = kGPU_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000411#else
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000412 fDeviceType = kRaster_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000413#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000415 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000416 fRepeatDrawing = false;
417 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000418 fRotate = false;
419 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000420 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000421 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000422 fMeasureFPS = false;
reed@google.com569e0432011-04-05 13:07:03 +0000423 fLCDState = kUnknown_SkTriState;
424 fAAState = kUnknown_SkTriState;
reed@google.com66f22fd2011-05-17 15:33:45 +0000425 fFilterState = kUnknown_SkTriState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000426 fHintingState = kUnknown_SkTriState;
reed@google.com569e0432011-04-05 13:07:03 +0000427 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428 fScrollTestX = fScrollTestY = 0;
429
Scroggo0f185c22011-03-24 18:35:50 +0000430 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000431 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000432 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
433 fShowZoomer = false;
yangsu@google.com921091f2011-08-02 13:39:12 +0000434
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000435 fZoomLevel = 0;
436 fZoomScale = SK_Scalar1;
437
Scroggo8ac0d542011-06-21 14:44:57 +0000438 fSaveToPdf = false;
439 fPdfCanvas = NULL;
440
yangsu@google.com921091f2011-08-02 13:39:12 +0000441 int sinkID = this->getSinkID();
442 fAppMenu.setTitle("Global Settings");
443 fAppMenu.appendList("Device Type", "Device Type", sinkID, 0, "Raster", "Picture", "OpenGL", NULL);
444 fAppMenu.appendTriState("AA", "AA", sinkID, SkOSMenu::kMixedState);
445 fAppMenu.appendTriState("LCD", "LCD", sinkID, SkOSMenu::kMixedState);
446 fAppMenu.appendTriState("Filter", "Filter", sinkID, SkOSMenu::kMixedState);
447 fAppMenu.appendTriState("Hinting", "Hinting", sinkID, SkOSMenu::kMixedState);
448 fAppMenu.appendSwitch("Pipe", "Pipe" , sinkID, fUsePipe);
449 fAppMenu.appendSwitch("Slide Show", "Slide Show" , sinkID, false);
450 fAppMenu.appendSwitch("Clip", "Clip" , sinkID, fUseClip);
451 fAppMenu.appendSwitch("Measure FPS", "Measure FPS" , sinkID, fMeasureFPS);
452 fAppMenu.appendSwitch("Flip X", "Flip X" , sinkID, false);
453 fAppMenu.appendSwitch("Flip Y", "Flip Y" , sinkID, false);
454 fAppMenu.appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
455 fAppMenu.appendAction("Save to PDF", sinkID);
456
457 this->addMenu(&fAppMenu);
458 this->addMenu(&fSlideMenu);
459
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000460// this->setConfig(SkBitmap::kRGB_565_Config);
461 this->setConfig(SkBitmap::kARGB_8888_Config);
462 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000463 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000464
reed@android.com34245c72009-11-03 04:00:48 +0000465 {
466 const SkViewRegister* reg = SkViewRegister::Head();
467 while (reg) {
468 *fSamples.append() = reg->factory();
469 reg = reg->next();
470 }
471 }
472 fCurrIndex = 0;
senorblanco@chromium.org78b82532011-06-28 19:44:03 +0000473 if (argc > 1) {
474 int i, count = fSamples.count();
475 for (i = 0; i < count; i++) {
476 SkString title = getSampleTitle(i);
477 if (title.equals(argv[1])) {
478 fCurrIndex = i;
479 break;
480 }
481 }
482 if (i == count) {
483 fprintf(stderr, "Unknown sample \"%s\"\n", argv[1]);
484 }
485 }
reed@android.come0f13ee2009-11-04 19:40:25 +0000486 this->loadView(fSamples[fCurrIndex]());
bsalomon@google.com840e9f32011-07-06 21:59:09 +0000487
488 fPDFData = NULL;
reed@google.comf0b5f682011-03-11 20:08:25 +0000489
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000490 if (NULL == devManager) {
491 fDevManager = new DefaultDeviceManager();
492 } else {
493 devManager->ref();
494 fDevManager = devManager;
495 }
496 fDevManager->init(this);
497
Scroggob4490c72011-06-17 13:53:05 +0000498 // If another constructor set our dimensions, ensure that our
499 // onSizeChange gets called.
500 if (this->height() && this->width()) {
501 this->onSizeChange();
502 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000503}
504
505SampleWindow::~SampleWindow() {
506 delete fPicture;
Scroggo8ac0d542011-06-21 14:44:57 +0000507 delete fPdfCanvas;
Scroggo0f185c22011-03-24 18:35:50 +0000508 fTypeface->unref();
reed@google.com29038ed2011-07-06 17:56:47 +0000509
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000510 SkSafeUnref(fDevManager);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000511}
512
reed@android.com55e76b22009-11-23 21:46:47 +0000513static SkBitmap capture_bitmap(SkCanvas* canvas) {
514 SkBitmap bm;
515 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
516 src.copyTo(&bm, src.config());
517 return bm;
518}
519
520static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
521 SkBitmap* diff) {
522 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000523
reed@android.com55e76b22009-11-23 21:46:47 +0000524 SkAutoLockPixels alp0(src);
525 SkAutoLockPixels alp1(orig);
526 for (int y = 0; y < src.height(); y++) {
527 const void* srcP = src.getAddr(0, y);
528 const void* origP = orig.getAddr(0, y);
529 size_t bytes = src.width() * src.bytesPerPixel();
530 if (memcmp(srcP, origP, bytes)) {
531 SkDebugf("---------- difference on line %d\n", y);
532 return true;
533 }
534 }
535 return false;
536}
537
Scroggo0f185c22011-03-24 18:35:50 +0000538static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
539{
540 SkColor desiredColor = paint.getColor();
541 paint.setColor(SK_ColorWHITE);
542 const char* c_str = string.c_str();
543 size_t size = string.size();
544 SkRect bounds;
545 paint.measureText(c_str, size, &bounds);
546 bounds.offset(left, top);
547 SkScalar inset = SkIntToScalar(-2);
548 bounds.inset(inset, inset);
549 canvas->drawRect(bounds, paint);
550 if (desiredColor != SK_ColorBLACK) {
551 paint.setColor(SK_ColorBLACK);
552 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
553 }
554 paint.setColor(desiredColor);
555 canvas->drawText(c_str, size, left, top, paint);
556}
557
reed@android.com44177402009-11-23 21:07:51 +0000558#define XCLIP_N 8
559#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000560
561void SampleWindow::draw(SkCanvas* canvas) {
reed@google.com29038ed2011-07-06 17:56:47 +0000562
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000563 if (!fDevManager->prepareCanvas(fDeviceType, canvas, this)) {
564 return;
565 }
reed@android.com44177402009-11-23 21:07:51 +0000566 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000567 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000568 gAnimTime = SkTime::GetMSecs();
569
Scroggo2c8208f2011-06-15 16:49:08 +0000570 SkScalar cx = fZoomCenterX;
571 SkScalar cy = fZoomCenterY;
reed@google.com569e0432011-04-05 13:07:03 +0000572
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000573 if (fZoomLevel) {
574 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000575 SkPoint center;
576 m = canvas->getTotalMatrix();//.invert(&m);
577 m.mapXY(cx, cy, &center);
578 cx = center.fX;
579 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000580
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000581 m.setTranslate(-cx, -cy);
582 m.postScale(fZoomScale, fZoomScale);
583 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000584
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000585 canvas->concat(m);
586 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000587
reed@google.com569e0432011-04-05 13:07:03 +0000588 if (fFlipAxis) {
589 SkMatrix m;
590 m.setTranslate(cx, cy);
591 if (fFlipAxis & kFlipAxis_X) {
592 m.preScale(-SK_Scalar1, SK_Scalar1);
593 }
594 if (fFlipAxis & kFlipAxis_Y) {
595 m.preScale(SK_Scalar1, -SK_Scalar1);
596 }
597 m.preTranslate(-cx, -cy);
598 canvas->concat(m);
599 }
yangsu@google.com921091f2011-08-02 13:39:12 +0000600
reed@google.com52f57e12011-03-16 12:10:02 +0000601 // Apply any gesture matrix
602 if (true) {
603 const SkMatrix& localM = fGesture.localM();
604 if (localM.getType() & SkMatrix::kScale_Mask) {
605 canvas->setExternalMatrix(&localM);
606 }
607 canvas->concat(localM);
608 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000609
reed@google.com52f57e12011-03-16 12:10:02 +0000610 if (fGesture.isActive()) {
611 this->inval(NULL);
612 }
613 }
yangsu@google.com921091f2011-08-02 13:39:12 +0000614
reed@android.come522ca52009-11-23 20:10:41 +0000615 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000616 this->INHERITED::draw(canvas);
617 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000618
619 const SkScalar w = this->width();
620 const SkScalar h = this->height();
621 const SkScalar cw = w / XCLIP_N;
622 const SkScalar ch = h / YCLIP_N;
623 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000624 SkRect r;
625 r.fTop = y * ch;
626 r.fBottom = (y + 1) * ch;
627 if (y == YCLIP_N - 1) {
628 r.fBottom = h;
629 }
reed@android.come522ca52009-11-23 20:10:41 +0000630 for (int x = 0; x < XCLIP_N; x++) {
631 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000632 r.fLeft = x * cw;
633 r.fRight = (x + 1) * cw;
634 if (x == XCLIP_N - 1) {
635 r.fRight = w;
636 }
reed@android.come522ca52009-11-23 20:10:41 +0000637 canvas->clipRect(r);
638 this->INHERITED::draw(canvas);
639 }
640 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000641
reed@android.com55e76b22009-11-23 21:46:47 +0000642 SkBitmap diff;
643 if (bitmap_diff(canvas, orig, &diff)) {
644 }
reed@android.come522ca52009-11-23 20:10:41 +0000645 } else {
646 this->INHERITED::draw(canvas);
647 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000648 if (fShowZoomer && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +0000649 showZoomer(canvas);
650 }
yangsu@google.com921091f2011-08-02 13:39:12 +0000651
reed@google.com29038ed2011-07-06 17:56:47 +0000652 // do this last
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000653 fDevManager->publishCanvas(fDeviceType, canvas, this);
Scroggo3272ba82011-05-25 20:50:42 +0000654}
655
656void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +0000657 int count = canvas->save();
658 canvas->resetMatrix();
659 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000660 int width = SkScalarRound(this->width());
661 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000662 if (fMouseX >= width) fMouseX = width - 1;
663 else if (fMouseX < 0) fMouseX = 0;
664 if (fMouseY >= height) fMouseY = height - 1;
665 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000666
Scroggo0f185c22011-03-24 18:35:50 +0000667 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000668 bitmap.lockPixels();
669
Scroggo0f185c22011-03-24 18:35:50 +0000670 // 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 +0000671 int zoomedWidth = (width >> 1) | 1;
672 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000673 SkIRect src;
674 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
675 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
676 SkRect dest;
677 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
678 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
679 SkPaint paint;
680 // Clear the background behind our zoomed in view
681 paint.setColor(SK_ColorWHITE);
682 canvas->drawRect(dest, paint);
683 canvas->drawBitmapRect(bitmap, &src, dest);
684 paint.setColor(SK_ColorBLACK);
685 paint.setStyle(SkPaint::kStroke_Style);
686 // Draw a border around the pixel in the middle
687 SkRect originalPixel;
688 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
689 SkMatrix matrix;
690 SkRect scalarSrc;
691 scalarSrc.set(src);
692 SkColor color = bitmap.getColor(fMouseX, fMouseY);
693 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
694 SkRect pixel;
695 matrix.mapRect(&pixel, originalPixel);
696 // TODO Perhaps measure the values and make the outline white if it's "dark"
697 if (color == SK_ColorBLACK) {
698 paint.setColor(SK_ColorWHITE);
699 }
700 canvas->drawRect(pixel, paint);
701 }
702 paint.setColor(SK_ColorBLACK);
703 // Draw a border around the destination rectangle
704 canvas->drawRect(dest, paint);
705 paint.setStyle(SkPaint::kStrokeAndFill_Style);
706 // Identify the pixel and its color on screen
707 paint.setTypeface(fTypeface);
708 paint.setAntiAlias(true);
709 SkScalar lineHeight = paint.getFontMetrics(NULL);
710 SkString string;
711 string.appendf("(%i, %i)", fMouseX, fMouseY);
712 SkScalar left = dest.fLeft + SkIntToScalar(3);
713 SkScalar i = SK_Scalar1;
714 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
715 // Alpha
716 i += SK_Scalar1;
717 string.reset();
718 string.appendf("A: %X", SkColorGetA(color));
719 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
720 // Red
721 i += SK_Scalar1;
722 string.reset();
723 string.appendf("R: %X", SkColorGetR(color));
724 paint.setColor(SK_ColorRED);
725 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
726 // Green
727 i += SK_Scalar1;
728 string.reset();
729 string.appendf("G: %X", SkColorGetG(color));
730 paint.setColor(SK_ColorGREEN);
731 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
732 // Blue
733 i += SK_Scalar1;
734 string.reset();
735 string.appendf("B: %X", SkColorGetB(color));
736 paint.setColor(SK_ColorBLUE);
737 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
738 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +0000739}
740
reed@android.com8a1c16f2008-12-17 15:59:43 +0000741void SampleWindow::onDraw(SkCanvas* canvas) {
742 if (fRepeatDrawing) {
743 this->inval(NULL);
744 }
745}
746
747#include "SkColorPriv.h"
748
749static void reverseRedAndBlue(const SkBitmap& bm) {
750 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
751 uint8_t* p = (uint8_t*)bm.getPixels();
752 uint8_t* stop = p + bm.getSize();
753 while (p < stop) {
754 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
755 unsigned scale = SkAlpha255To256(p[3]);
756 unsigned r = p[2];
757 unsigned b = p[0];
758 p[0] = SkAlphaMul(r, scale);
759 p[1] = SkAlphaMul(p[1], scale);
760 p[2] = SkAlphaMul(b, scale);
761 p += 4;
762 }
763}
764
Scroggo8ac0d542011-06-21 14:44:57 +0000765void SampleWindow::saveToPdf()
766{
767 fSaveToPdf = true;
768 this->inval(NULL);
769}
770
reed@android.com8a1c16f2008-12-17 15:59:43 +0000771SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +0000772 if (fSaveToPdf) {
773 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
774 SkISize size = SkISize::Make(bmp.width(), bmp.height());
775 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
776 canvas->getTotalMatrix());
777 fPdfCanvas = new SkCanvas(pdfDevice);
778 pdfDevice->unref();
779 canvas = fPdfCanvas;
780 } else {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000781 switch (fDeviceType) {
782 case kRaster_DeviceType:
783 case kGPU_DeviceType:
reed@android.comf2b98d62010-12-20 18:26:13 +0000784 canvas = this->INHERITED::beforeChildren(canvas);
Scroggo8ac0d542011-06-21 14:44:57 +0000785 break;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000786 case kPicture_DeviceType:
Scroggo8ac0d542011-06-21 14:44:57 +0000787 fPicture = new SkPicture;
788 canvas = fPicture->beginRecording(9999, 9999);
789 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000790 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000791 }
792
793 if (fUseClip) {
794 canvas->drawColor(0xFFFF88FF);
795 canvas->clipPath(fClipPath);
796 }
797
798 return canvas;
799}
800
801static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
802 const SkRegion& rgn) {
803 SkCanvas canvas(bm);
804 SkRegion inval(rgn);
805
806 inval.translate(r.fLeft, r.fTop);
807 canvas.clipRegion(inval);
808 canvas.drawColor(0xFFFF8080);
809}
yangsu@google.com501775e2011-06-24 16:04:50 +0000810#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +0000811void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +0000812 if (fSaveToPdf) {
813 fSaveToPdf = false;
814 if (fShowZoomer) {
815 showZoomer(fPdfCanvas);
816 }
817 SkString name;
818 name.printf("%s.pdf", this->getTitle());
819 SkPDFDocument doc;
820 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
821 doc.appendPage(device);
822#ifdef ANDROID
823 name.prepend("/sdcard/");
824#endif
yangsu@google.com501775e2011-06-24 16:04:50 +0000825
826#ifdef SK_BUILD_FOR_IOS
827 SkDynamicMemoryWStream mstream;
828 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +0000829 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +0000830#endif
Scroggo8ac0d542011-06-21 14:44:57 +0000831 SkFILEWStream stream(name.c_str());
832 if (stream.isValid()) {
833 doc.emitPDF(&stream);
834 const char* desc = "File saved from Skia SampleApp";
835 this->onPDFSaved(this->getTitle(), desc, name.c_str());
836 }
yangsu@google.com501775e2011-06-24 16:04:50 +0000837
Scroggo8ac0d542011-06-21 14:44:57 +0000838 delete fPdfCanvas;
839 fPdfCanvas = NULL;
840
841 // We took over the draw calls in order to create the PDF, so we need
842 // to redraw.
843 this->inval(NULL);
844 return;
845 }
846
reed@android.comf2b98d62010-12-20 18:26:13 +0000847 if (fRequestGrabImage) {
848 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000849
reed@google.com29038ed2011-07-06 17:56:47 +0000850 SkDevice* device = orig->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000851 SkBitmap bmp;
852 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000853 static int gSampleGrabCounter;
854 SkString name;
855 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000856 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000857 SkImageEncoder::kPNG_Type, 100);
858 }
859 }
860
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000861 if (kPicture_DeviceType == fDeviceType) {
862 if (true) {
863 SkPicture* pict = new SkPicture(*fPicture);
864 fPicture->unref();
865 orig->drawPicture(*pict);
866 pict->unref();
867 } else if (true) {
868 SkDynamicMemoryWStream ostream;
869 fPicture->serialize(&ostream);
870 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000871
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000872 SkAutoDataUnref data(ostream.copyToData());
873 SkMemoryStream istream(data.data(), data.size());
874 SkPicture pict(&istream);
875 orig->drawPicture(pict);
876 } else {
877 fPicture->draw(orig);
878 fPicture->unref();
879 }
880 fPicture = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000881 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000882
reed@google.com17d7aec2011-04-25 14:31:44 +0000883 // Do this after presentGL and other finishing, rather than in afterChild
884 if (fMeasureFPS && fMeasureFPS_Time) {
885 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
886 this->updateTitle();
Scroggo62b65b02011-06-21 16:01:26 +0000887 this->postInvalDelay();
reed@google.com17d7aec2011-04-25 14:31:44 +0000888 }
889
890 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000891 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000892 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
893 int dx = fScrollTestX * 7;
894 int dy = fScrollTestY * 7;
895 SkIRect r;
896 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000897
reed@android.com8a1c16f2008-12-17 15:59:43 +0000898 r.set(50, 50, 50+100, 50+100);
899 bm.scrollRect(&r, dx, dy, &inval);
900 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000901 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000902}
903
reed@android.com6c5f6f22009-08-14 16:08:38 +0000904void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
905 if (fScale) {
906 SkScalar scale = SK_Scalar1 * 7 / 10;
907 SkScalar cx = this->width() / 2;
908 SkScalar cy = this->height() / 2;
909 canvas->translate(cx, cy);
910 canvas->scale(scale, scale);
911 canvas->translate(-cx, -cy);
912 }
913 if (fRotate) {
914 SkScalar cx = this->width() / 2;
915 SkScalar cy = this->height() / 2;
916 canvas->translate(cx, cy);
917 canvas->rotate(SkIntToScalar(30));
918 canvas->translate(-cx, -cy);
919 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000920
reed@google.com09e3baa2011-05-18 12:04:31 +0000921 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
922 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000923
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000924 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000925 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
926 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
927 fMeasureFPS_Time = SkTime::GetMSecs();
928 }
929 } else {
930 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000931 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000932 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000933}
934
935void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000936 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000937}
938
reed@android.com8a1c16f2008-12-17 15:59:43 +0000939static SkBitmap::Config gConfigCycle[] = {
940 SkBitmap::kNo_Config, // none -> none
941 SkBitmap::kNo_Config, // a1 -> none
942 SkBitmap::kNo_Config, // a8 -> none
943 SkBitmap::kNo_Config, // index8 -> none
944 SkBitmap::kARGB_4444_Config, // 565 -> 4444
945 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
946 SkBitmap::kRGB_565_Config // 8888 -> 565
947};
948
949static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
950 return gConfigCycle[c];
951}
952
djsollen@google.come32b5832011-06-13 16:58:40 +0000953void SampleWindow::changeZoomLevel(float delta) {
954 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000955 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000956 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
957 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000958 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000959 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
960 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000961 } else {
962 fZoomScale = SK_Scalar1;
963 }
964
djsollen@google.come32b5832011-06-13 16:58:40 +0000965 this->updateTitle();
966
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000967 this->inval(NULL);
968}
969
Scroggo2c8208f2011-06-15 16:49:08 +0000970bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +0000971 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
Scroggo2c8208f2011-06-15 16:49:08 +0000972 this->loadView(fSamples[fCurrIndex]());
973 return true;
974}
975
reed@android.com8a1c16f2008-12-17 15:59:43 +0000976bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000977 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
978 this->loadView(fSamples[fCurrIndex]());
979 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000980}
981
yangsu@google.com501775e2011-06-24 16:04:50 +0000982bool SampleWindow::goToSample(int i) {
983 fCurrIndex = (i) % fSamples.count();
984 this->loadView(fSamples[fCurrIndex]());
985 return true;
986}
987
988SkString SampleWindow::getSampleTitle(int i) {
989 SkView* view = fSamples[i]();
990 SkString title;
991 SkEvent evt(gTitleEvtName);
992 if (view->doQuery(&evt)) {
993 title.set(evt.findString(gTitleEvtName));
994 }
995 view->unref();
996 return title;
997}
998
999int SampleWindow::sampleCount() {
1000 return fSamples.count();
1001}
1002
Scroggo2c8208f2011-06-15 16:49:08 +00001003void SampleWindow::postAnimatingEvent() {
1004 if (fAnimating) {
1005 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
1006 evt->post(this->getSinkID(), ANIMATING_DELAY);
1007 }
1008}
1009
reed@android.com8a1c16f2008-12-17 15:59:43 +00001010bool SampleWindow::onEvent(const SkEvent& evt) {
1011 if (evt.isType(ANIMATING_EVENTTYPE)) {
1012 if (fAnimating) {
1013 this->nextSample();
1014 this->postAnimatingEvent();
1015 }
1016 return true;
1017 }
reed@android.com34245c72009-11-03 04:00:48 +00001018 if (evt.isType("set-curr-index")) {
1019 fCurrIndex = evt.getFast32() % fSamples.count();
1020 this->loadView(fSamples[fCurrIndex]());
1021 return true;
1022 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001023 if (isInvalEvent(evt)) {
1024 this->inval(NULL);
1025 return true;
1026 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001027 int selected = -1;
1028 if (SkOSMenu::FindListIndex(&evt, "Device Type", &selected)) {
1029 this->setDeviceType((DeviceType)selected);
1030 return true;
1031 }
1032 SkOSMenu::TriState state;
1033 if (SkOSMenu::FindTriState(&evt, "AA", &state)) {
1034 fAAState = (SkTriState)state;
1035 this->updateTitle();
1036 this->inval(NULL);
1037 }
1038 if (SkOSMenu::FindTriState(&evt, "LCD", &state)) {
1039 fLCDState = (SkTriState)state;
1040 this->updateTitle();
1041 this->inval(NULL);
1042 }
1043 if (SkOSMenu::FindTriState(&evt, "Filter", &state)) {
1044 fFilterState = (SkTriState)state;
1045 this->updateTitle();
1046 this->inval(NULL);
1047 }
1048 if (SkOSMenu::FindTriState(&evt, "Hinting", &state)) {
1049 fHintingState = (SkTriState)state;
1050 this->updateTitle();
1051 this->inval(NULL);
1052 }
1053 if (SkOSMenu::FindSwitchState(&evt, "Pipe", NULL)) {
1054 this->togglePipe();
1055 return true;
1056 }
1057 if (SkOSMenu::FindSwitchState(&evt, "Slide Show", NULL)) {
1058 this->toggleSlideshow();
1059 return true;
1060 }
1061 if (SkOSMenu::FindSwitchState(&evt, "Clip", NULL)) {
1062 fUseClip = !fUseClip;
1063 this->inval(NULL);
1064 this->updateTitle();
1065 return true;
1066 }
1067 if (SkOSMenu::FindSwitchState(&evt, "Measure FPS", NULL)) {
1068 this->toggleFPS();
1069 return true;
1070 }
1071 if (SkOSMenu::FindSwitchState(&evt, "Flip X", NULL)) {
1072 fFlipAxis ^= kFlipAxis_X;
1073 this->updateTitle();
1074 this->inval(NULL);
1075 return true;
1076 }
1077 if (SkOSMenu::FindSwitchState(&evt, "Flip Y", NULL)) {
1078 fFlipAxis ^= kFlipAxis_Y;
1079 this->updateTitle();
1080 this->inval(NULL);
1081 return true;
1082 }
1083 if (SkOSMenu::FindSwitchState(&evt, "Zoomer", NULL)) {
1084 this->toggleZoomer();
1085 return true;
1086 }
1087 if (SkOSMenu::FindAction(&evt,"Save to PDF")) {
1088 this->saveToPdf();
1089 return true;
1090 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001091 return this->INHERITED::onEvent(evt);
1092}
1093
reed@android.comf2b98d62010-12-20 18:26:13 +00001094bool SampleWindow::onQuery(SkEvent* query) {
1095 if (query->isType("get-slide-count")) {
1096 query->setFast32(fSamples.count());
1097 return true;
1098 }
1099 if (query->isType("get-slide-title")) {
1100 SkView* view = fSamples[query->getFast32()]();
1101 SkEvent evt(gTitleEvtName);
1102 if (view->doQuery(&evt)) {
1103 query->setString("title", evt.findString(gTitleEvtName));
1104 }
1105 SkSafeUnref(view);
1106 return true;
1107 }
1108 if (query->isType("use-fast-text")) {
1109 SkEvent evt(gFastTextEvtName);
1110 return curr_view(this)->doQuery(&evt);
1111 }
reed@google.com29038ed2011-07-06 17:56:47 +00001112 if (query->isType("ignore-window-bitmap")) {
1113 query->setFast32(this->getGrContext() != NULL);
1114 return true;
1115 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001116 return this->INHERITED::onQuery(query);
1117}
1118
reed@android.com0ae6b242008-12-23 16:49:54 +00001119static void cleanup_for_filename(SkString* name) {
1120 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001121 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001122 switch (str[i]) {
1123 case ':': str[i] = '-'; break;
1124 case '/': str[i] = '-'; break;
1125 case ' ': str[i] = '_'; break;
1126 default: break;
1127 }
1128 }
1129}
reed@android.com8a1c16f2008-12-17 15:59:43 +00001130
1131bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001132 {
1133 SkView* view = curr_view(this);
1134 if (view) {
1135 SkEvent evt(gCharEvtName);
1136 evt.setFast32(uni);
1137 if (view->doQuery(&evt)) {
1138 return true;
1139 }
1140 }
1141 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001142
reed@android.com8a1c16f2008-12-17 15:59:43 +00001143 int dx = 0xFF;
1144 int dy = 0xFF;
1145
1146 switch (uni) {
1147 case '5': dx = 0; dy = 0; break;
1148 case '8': dx = 0; dy = -1; break;
1149 case '6': dx = 1; dy = 0; break;
1150 case '2': dx = 0; dy = 1; break;
1151 case '4': dx = -1; dy = 0; break;
1152 case '7': dx = -1; dy = -1; break;
1153 case '9': dx = 1; dy = -1; break;
1154 case '3': dx = 1; dy = 1; break;
1155 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001156
reed@android.com8a1c16f2008-12-17 15:59:43 +00001157 default:
1158 break;
1159 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001160
reed@android.com8a1c16f2008-12-17 15:59:43 +00001161 if (0xFF != dx && 0xFF != dy) {
1162 if ((dx | dy) == 0) {
1163 fScrollTestX = fScrollTestY = 0;
1164 } else {
1165 fScrollTestX += dx;
1166 fScrollTestY += dy;
1167 }
1168 this->inval(NULL);
1169 return true;
1170 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001171
reed@android.com0ae6b242008-12-23 16:49:54 +00001172 switch (uni) {
1173 case 'a':
Scroggo2c8208f2011-06-15 16:49:08 +00001174 this->toggleSlideshow();
reed@android.com0ae6b242008-12-23 16:49:54 +00001175 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001176 case 'b':
1177 fAAState = cycle_tristate(fAAState);
1178 this->updateTitle();
1179 this->inval(NULL);
1180 break;
1181 case 'c':
1182 fUseClip = !fUseClip;
1183 this->inval(NULL);
1184 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001185 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001186 case 'd':
1187 SkGraphics::SetFontCacheUsed(0);
1188 return true;
Scroggo8ac0d542011-06-21 14:44:57 +00001189 case 'e':
1190 this->saveToPdf();
1191 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001192 case 'f':
Scroggo2c8208f2011-06-15 16:49:08 +00001193 this->toggleFPS();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001194 break;
1195 case 'g':
1196 fRequestGrabImage = true;
1197 this->inval(NULL);
1198 break;
reed@google.com09e3baa2011-05-18 12:04:31 +00001199 case 'h':
1200 fHintingState = cycle_tristate(fHintingState);
1201 this->updateTitle();
1202 this->inval(NULL);
1203 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001204 case 'i':
1205 this->zoomIn();
1206 break;
1207 case 'l':
1208 fLCDState = cycle_tristate(fLCDState);
1209 this->updateTitle();
1210 this->inval(NULL);
1211 break;
reed@google.com176753a2011-05-17 15:32:04 +00001212 case 'n':
1213 fFilterState = cycle_tristate(fFilterState);
1214 this->updateTitle();
1215 this->inval(NULL);
1216 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001217 case 'o':
1218 this->zoomOut();
1219 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001220 case 'p':
1221 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001222 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001223 this->inval(NULL);
1224 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001225 case 'r':
1226 fRotate = !fRotate;
1227 this->inval(NULL);
1228 this->updateTitle();
1229 return true;
1230 case 's':
1231 fScale = !fScale;
1232 this->inval(NULL);
1233 this->updateTitle();
1234 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001235 case 'x':
1236 fFlipAxis ^= kFlipAxis_X;
1237 this->updateTitle();
1238 this->inval(NULL);
1239 break;
1240 case 'y':
1241 fFlipAxis ^= kFlipAxis_Y;
1242 this->updateTitle();
1243 this->inval(NULL);
1244 break;
scroggo08526c02011-03-22 14:03:21 +00001245 case 'z':
1246 this->toggleZoomer();
1247 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001248 default:
1249 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001250 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001251
reed@android.com8a1c16f2008-12-17 15:59:43 +00001252 return this->INHERITED::onHandleChar(uni);
1253}
1254
yangsu@google.com921091f2011-08-02 13:39:12 +00001255void SampleWindow::setDeviceType(DeviceType type) {
1256 if (type != fDeviceType && fDevManager->supportsDeviceType(fDeviceType))
1257 fDeviceType = type;
1258 this->updateTitle();
1259 this->inval(NULL);
1260}
1261
Scroggo2c8208f2011-06-15 16:49:08 +00001262void SampleWindow::toggleFPS() {
1263 fMeasureFPS = !fMeasureFPS;
1264 this->inval(NULL);
1265 this->updateTitle();
1266}
1267
1268void SampleWindow::toggleSlideshow() {
1269 fAnimating = !fAnimating;
1270 this->postAnimatingEvent();
1271 this->updateTitle();
1272}
1273
1274void SampleWindow::toggleRendering() {
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001275 DeviceType origDevType = fDeviceType;
1276 do {
1277 fDeviceType = cycle_devicetype(fDeviceType);
1278 } while (origDevType != fDeviceType &&
1279 !fDevManager->supportsDeviceType(fDeviceType));
Scroggo2c8208f2011-06-15 16:49:08 +00001280 this->updateTitle();
1281 this->inval(NULL);
1282}
1283
yangsu@google.com921091f2011-08-02 13:39:12 +00001284void SampleWindow::togglePipe() {
1285 fUsePipe = !fUsePipe;
1286 this->updateTitle();
1287}
1288
reed@android.com8a1c16f2008-12-17 15:59:43 +00001289#include "SkDumpCanvas.h"
1290
1291bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001292 {
1293 SkView* view = curr_view(this);
1294 if (view) {
1295 SkEvent evt(gKeyEvtName);
1296 evt.setFast32(key);
1297 if (view->doQuery(&evt)) {
1298 return true;
1299 }
1300 }
1301 }
1302
reed@android.com8a1c16f2008-12-17 15:59:43 +00001303 switch (key) {
1304 case kRight_SkKey:
1305 if (this->nextSample()) {
1306 return true;
1307 }
1308 break;
1309 case kLeft_SkKey:
Scroggo2c8208f2011-06-15 16:49:08 +00001310 toggleRendering();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001311 return true;
1312 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001313 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001314 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001315 } else {
1316 fNClip = !fNClip;
1317 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001318 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001319 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001320 return true;
1321 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001322 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001323 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001324 } else {
1325 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001326 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001327 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001328 return true;
1329 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001330 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001331 SkDebugfDumper dumper;
1332 SkDumpCanvas dc(&dumper);
1333 this->draw(&dc);
1334 } else {
1335 fRepeatDrawing = !fRepeatDrawing;
1336 if (fRepeatDrawing) {
1337 this->inval(NULL);
1338 }
1339 }
1340 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001341 case kBack_SkKey:
1342 this->loadView(NULL);
1343 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001344 default:
1345 break;
1346 }
1347 return this->INHERITED::onHandleKey(key);
1348}
1349
reed@google.com52f57e12011-03-16 12:10:02 +00001350///////////////////////////////////////////////////////////////////////////////
1351
1352static const char gGestureClickType[] = "GestureClickType";
1353
Scroggod3aed392011-06-22 13:26:56 +00001354bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
1355 void* owner) {
Scroggo0f185c22011-03-24 18:35:50 +00001356 if (Click::kMoved_State == state) {
1357 updatePointer(x, y);
1358 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001359 int w = SkScalarRound(this->width());
1360 int h = SkScalarRound(this->height());
1361
1362 // check for the resize-box
1363 if (w - x < 16 && h - y < 16) {
1364 return false; // let the OS handle the click
1365 } else {
Scroggod3aed392011-06-22 13:26:56 +00001366 return this->INHERITED::onDispatchClick(x, y, state, owner);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001367 }
1368}
1369
reed@google.com52f57e12011-03-16 12:10:02 +00001370class GestureClick : public SkView::Click {
1371public:
1372 GestureClick(SkView* target) : SkView::Click(target) {
1373 this->setType(gGestureClickType);
1374 }
1375
1376 static bool IsGesture(Click* click) {
1377 return click->isType(gGestureClickType);
1378 }
1379};
1380
1381SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1382 return new GestureClick(this);
1383}
1384
1385bool SampleWindow::onClick(Click* click) {
1386 if (GestureClick::IsGesture(click)) {
1387 float x = SkScalarToFloat(click->fCurr.fX);
1388 float y = SkScalarToFloat(click->fCurr.fY);
1389 switch (click->fState) {
1390 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00001391 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001392 break;
1393 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00001394 fGesture.touchMoved(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001395 this->inval(NULL);
1396 break;
1397 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00001398 fGesture.touchEnd(click->fOwner);
reed@google.com52f57e12011-03-16 12:10:02 +00001399 this->inval(NULL);
1400 break;
1401 }
1402 return true;
1403 }
1404 return false;
1405}
1406
1407///////////////////////////////////////////////////////////////////////////////
1408
reed@android.com8a1c16f2008-12-17 15:59:43 +00001409void SampleWindow::loadView(SkView* view) {
1410 SkView::F2BIter iter(this);
1411 SkView* prev = iter.next();
1412 if (prev) {
1413 prev->detachFromParent();
1414 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001415
reed@android.com34245c72009-11-03 04:00:48 +00001416 if (NULL == view) {
1417 view = create_overview(fSamples.count(), fSamples.begin());
1418 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001419 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001420 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001421 this->attachChildToFront(view)->unref();
1422 view->setSize(this->width(), this->height());
1423
yangsu@google.com921091f2011-08-02 13:39:12 +00001424 //repopulate the slide menu when a view is loaded
1425 fSlideMenu.reset();
1426 if (SampleView::IsSampleView(view))
1427 ((SampleView*)view)->requestMenus(&fSlideMenu);
1428 this->onUpdateMenu(&fSlideMenu);
1429
reed@android.com8a1c16f2008-12-17 15:59:43 +00001430 this->updateTitle();
1431}
1432
1433static const char* gConfigNames[] = {
1434 "unknown config",
1435 "A1",
1436 "A8",
1437 "Index8",
1438 "565",
1439 "4444",
1440 "8888"
1441};
1442
1443static const char* configToString(SkBitmap::Config c) {
1444 return gConfigNames[c];
1445}
1446
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001447static const char* gDeviceTypePrefix[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001448 "raster: ",
1449 "picture: ",
1450 "opengl: "
1451};
1452
reed@google.com569e0432011-04-05 13:07:03 +00001453static const char* trystate_str(SkTriState state,
1454 const char trueStr[], const char falseStr[]) {
1455 if (kTrue_SkTriState == state) {
1456 return trueStr;
1457 } else if (kFalse_SkTriState == state) {
1458 return falseStr;
1459 }
1460 return NULL;
1461}
1462
reed@android.com8a1c16f2008-12-17 15:59:43 +00001463void SampleWindow::updateTitle() {
1464 SkString title;
1465
1466 SkView::F2BIter iter(this);
1467 SkView* view = iter.next();
1468 SkEvent evt(gTitleEvtName);
1469 if (view->doQuery(&evt)) {
1470 title.set(evt.findString(gTitleEvtName));
1471 }
1472 if (title.size() == 0) {
1473 title.set("<unknown>");
1474 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001475
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001476 title.prepend(gDeviceTypePrefix[fDeviceType]);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001477
1478 title.prepend(" ");
1479 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001480
reed@android.com8a1c16f2008-12-17 15:59:43 +00001481 if (fAnimating) {
1482 title.prepend("<A> ");
1483 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001484 if (fScale) {
1485 title.prepend("<S> ");
1486 }
1487 if (fRotate) {
1488 title.prepend("<R> ");
1489 }
reed@android.come522ca52009-11-23 20:10:41 +00001490 if (fNClip) {
1491 title.prepend("<C> ");
1492 }
reed@google.com569e0432011-04-05 13:07:03 +00001493
1494 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1495 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001496 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001497 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1498 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001499
1500 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001501 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001502 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001503
1504 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001505 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001506 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001507 if (fUsePipe && SampleView::IsSampleView(view)) {
1508 title.prepend("<P> ");
1509 }
1510 if (SampleView::IsSampleView(view)) {
1511 title.prepend("! ");
1512 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001513
reed@android.com8a1c16f2008-12-17 15:59:43 +00001514 this->setTitle(title.c_str());
1515}
1516
1517void SampleWindow::onSizeChange() {
1518 this->INHERITED::onSizeChange();
reed@google.com29038ed2011-07-06 17:56:47 +00001519
reed@android.com8a1c16f2008-12-17 15:59:43 +00001520 SkView::F2BIter iter(this);
1521 SkView* view = iter.next();
1522 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001523
reed@android.com8a1c16f2008-12-17 15:59:43 +00001524 // rebuild our clippath
1525 {
1526 const SkScalar W = this->width();
1527 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001528
reed@android.com8a1c16f2008-12-17 15:59:43 +00001529 fClipPath.reset();
1530#if 0
1531 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1532 SkRect r;
1533 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1534 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1535 fClipPath.addRect(r);
1536 }
1537#else
1538 SkRect r;
1539 r.set(0, 0, W, H);
1540 fClipPath.addRect(r, SkPath::kCCW_Direction);
1541 r.set(W/4, H/4, W*3/4, H*3/4);
1542 fClipPath.addRect(r, SkPath::kCW_Direction);
1543#endif
1544 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001545
Scroggo2c8208f2011-06-15 16:49:08 +00001546 fZoomCenterX = SkScalarHalf(this->width());
1547 fZoomCenterY = SkScalarHalf(this->height());
1548
Scroggo3e7ff9f2011-06-16 15:31:26 +00001549#ifdef ANDROID
Scroggob4490c72011-06-17 13:53:05 +00001550 // FIXME: The first draw after a size change does not work on Android, so
1551 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00001552 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00001553#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001554 this->updateTitle(); // to refresh our config
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001555 fDevManager->windowSizeChanged(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001556}
1557
1558///////////////////////////////////////////////////////////////////////////////
1559
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001560static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001561static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001562static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001563
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001564bool SampleView::IsSampleView(SkView* view) {
1565 SkEvent evt(is_sample_view_tag);
1566 return view->doQuery(&evt);
1567}
1568
reed@google.comf2183392011-04-22 14:10:48 +00001569bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001570 SkEvent evt(repeat_count_tag);
1571 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001572 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001573}
1574
reed@google.com0faac1e2011-05-11 05:58:58 +00001575bool SampleView::SetUsePipe(SkView* view, bool pred) {
1576 SkEvent evt(set_use_pipe_tag);
1577 evt.setFast32(pred);
1578 return view->doEvent(evt);
1579}
1580
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001581bool SampleView::onEvent(const SkEvent& evt) {
1582 if (evt.isType(repeat_count_tag)) {
1583 fRepeatCount = evt.getFast32();
1584 return true;
1585 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001586 if (evt.isType(set_use_pipe_tag)) {
1587 fUsePipe = !!evt.getFast32();
1588 return true;
1589 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001590 return this->INHERITED::onEvent(evt);
1591}
1592
1593bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001594 if (evt->isType(is_sample_view_tag)) {
1595 return true;
1596 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001597 return this->INHERITED::onQuery(evt);
1598}
1599
reed@google.com68f456d2011-05-02 18:55:39 +00001600#ifdef TEST_GPIPE
1601 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001602
1603class SimplePC : public SkGPipeController {
1604public:
1605 SimplePC(SkCanvas* target);
1606 ~SimplePC();
1607
1608 virtual void* requestBlock(size_t minRequest, size_t* actual);
1609 virtual void notifyWritten(size_t bytes);
1610
1611private:
reed@google.com961ddb02011-05-05 14:03:48 +00001612 SkGPipeReader fReader;
1613 void* fBlock;
1614 size_t fBlockSize;
1615 size_t fBytesWritten;
1616 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001617 SkGPipeReader::Status fStatus;
1618
1619 size_t fTotalWritten;
1620};
1621
1622SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1623 fBlock = NULL;
1624 fBlockSize = fBytesWritten = 0;
1625 fStatus = SkGPipeReader::kDone_Status;
1626 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001627 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001628}
1629
1630SimplePC::~SimplePC() {
1631// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1632 sk_free(fBlock);
1633
reed@google.com0faac1e2011-05-11 05:58:58 +00001634 if (fTotalWritten) {
1635 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1636 fAtomsWritten, fStatus);
1637 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001638}
1639
1640void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1641 sk_free(fBlock);
1642
1643 fBlockSize = minRequest * 4;
1644 fBlock = sk_malloc_throw(fBlockSize);
1645 fBytesWritten = 0;
1646 *actual = fBlockSize;
1647 return fBlock;
1648}
1649
1650void SimplePC::notifyWritten(size_t bytes) {
1651 SkASSERT(fBytesWritten + bytes <= fBlockSize);
yangsu@google.com1f394212011-06-01 18:03:34 +00001652
1653#ifdef PIPE_FILE
1654 //File is open in append mode
1655 FILE* f = fopen(FILE_PATH, "ab");
1656 SkASSERT(f != NULL);
1657 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
1658 fclose(f);
1659#endif
1660
reed@google.com64e3eb22011-05-04 14:32:04 +00001661 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1662 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1663 fBytesWritten += bytes;
1664 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001665
1666 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001667}
1668
reed@google.com68f456d2011-05-02 18:55:39 +00001669#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001670
reed@google.com64e3eb22011-05-04 14:32:04 +00001671
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001672void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001673#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001674 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001675 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001676 if (fUsePipe) {
reed@google.comdde09562011-05-23 12:21:05 +00001677 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
1678// flags = 0;
1679 canvas = writer.startRecording(&controller, flags);
reed@google.com0faac1e2011-05-11 05:58:58 +00001680 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001681#endif
1682
reed@google.com81e3d7f2011-06-01 12:42:36 +00001683 this->onDrawBackground(canvas);
1684
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001685 for (int i = 0; i < fRepeatCount; i++) {
1686 SkAutoCanvasRestore acr(canvas, true);
1687 this->onDrawContent(canvas);
1688 }
1689}
1690
1691void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001692 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001693}
1694
1695///////////////////////////////////////////////////////////////////////////////
1696
reed@android.comf2b98d62010-12-20 18:26:13 +00001697template <typename T> void SkTBSort(T array[], int count) {
1698 for (int i = 1; i < count - 1; i++) {
1699 bool didSwap = false;
1700 for (int j = count - 1; j > i; --j) {
1701 if (array[j] < array[j-1]) {
1702 T tmp(array[j-1]);
1703 array[j-1] = array[j];
1704 array[j] = tmp;
1705 didSwap = true;
1706 }
1707 }
1708 if (!didSwap) {
1709 break;
1710 }
1711 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001712
reed@android.comf2b98d62010-12-20 18:26:13 +00001713 for (int k = 0; k < count - 1; k++) {
1714 SkASSERT(!(array[k+1] < array[k]));
1715 }
1716}
1717
1718#include "SkRandom.h"
1719
1720static void rand_rect(SkIRect* rect, SkRandom& rand) {
1721 int bits = 8;
1722 int shift = 32 - bits;
1723 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1724 rand.nextU() >> shift, rand.nextU() >> shift);
1725 rect->sort();
1726}
1727
1728static void dumpRect(const SkIRect& r) {
1729 SkDebugf(" { %d, %d, %d, %d },\n",
1730 r.fLeft, r.fTop,
1731 r.fRight, r.fBottom);
1732}
1733
1734static void test_rects(const SkIRect rect[], int count) {
1735 SkRegion rgn0, rgn1;
1736
1737 for (int i = 0; i < count; i++) {
1738 rgn0.op(rect[i], SkRegion::kUnion_Op);
1739 // dumpRect(rect[i]);
1740 }
1741 rgn1.setRects(rect, count);
1742
1743 if (rgn0 != rgn1) {
1744 SkDebugf("\n");
1745 for (int i = 0; i < count; i++) {
1746 dumpRect(rect[i]);
1747 }
1748 SkDebugf("\n");
1749 }
1750}
1751
1752static void test() {
1753 size_t i;
1754
1755 const SkIRect r0[] = {
1756 { 0, 0, 1, 1 },
1757 { 2, 2, 3, 3 },
1758 };
1759 const SkIRect r1[] = {
1760 { 0, 0, 1, 3 },
1761 { 1, 1, 2, 2 },
1762 { 2, 0, 3, 3 },
1763 };
1764 const SkIRect r2[] = {
1765 { 0, 0, 1, 2 },
1766 { 2, 1, 3, 3 },
1767 { 4, 0, 5, 1 },
1768 { 6, 0, 7, 4 },
1769 };
1770
1771 static const struct {
1772 const SkIRect* fRects;
1773 int fCount;
1774 } gRecs[] = {
1775 { r0, SK_ARRAY_COUNT(r0) },
1776 { r1, SK_ARRAY_COUNT(r1) },
1777 { r2, SK_ARRAY_COUNT(r2) },
1778 };
1779
1780 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1781 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1782 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001783
reed@android.comf2b98d62010-12-20 18:26:13 +00001784 SkRandom rand;
1785 for (i = 0; i < 10000; i++) {
1786 SkRegion rgn0, rgn1;
1787
1788 const int N = 8;
1789 SkIRect rect[N];
1790 for (int j = 0; j < N; j++) {
1791 rand_rect(&rect[j], rand);
1792 }
1793 test_rects(rect, N);
1794 }
1795}
1796
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00001797SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001798// test();
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001799 return new SampleWindow(hwnd, argc, argv, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001800}
1801
1802void get_preferred_size(int* x, int* y, int* width, int* height) {
1803 *x = 10;
1804 *y = 50;
1805 *width = 640;
1806 *height = 480;
1807}
1808
1809void application_init() {
1810// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001811#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001812 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001813#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001814 SkGraphics::Init();
1815 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001816}
1817
1818void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001819 SkEvent::Term();
1820 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001821}