blob: d1cbeeffc17bdf1e5ae251b6169062df1217f461 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#include "SkCanvas.h"
2#include "SkDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00003#include "SkGpuCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00004#include "SkGraphics.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +00005#include "SkImageEncoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00006#include "SkPaint.h"
7#include "SkPicture.h"
8#include "SkStream.h"
reed@android.com44177402009-11-23 21:07:51 +00009#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkWindow.h"
11
12#include "SampleCode.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrContext.h"
reed@google.com52f57e12011-03-16 12:10:02 +000014#include "SkTouchGesture.h"
Scroggo0f185c22011-03-24 18:35:50 +000015#include "SkTypeface.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000016
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +000017#define USE_ARROWS_FOR_ZOOM true
reed@android.comf2b98d62010-12-20 18:26:13 +000018//#define DEFAULT_TO_GPU
19
reed@android.come191b162009-12-18 21:33:39 +000020extern SkView* create_overview(int, const SkViewFactory[]);
reed@android.com34245c72009-11-03 04:00:48 +000021
reed@android.comcb342352010-07-22 18:27:53 +000022#define SK_SUPPORT_GL
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
24#define ANIMATING_EVENTTYPE "nextSample"
25#define ANIMATING_DELAY 750
26
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000027#ifdef SK_DEBUG
28 #define FPS_REPEAT_COUNT 10
29#else
30 #define FPS_REPEAT_COUNT 100
31#endif
32
reed@google.comac10a2d2010-12-22 21:39:39 +000033#ifdef SK_SUPPORT_GL
34 #include "GrGLConfig.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000035#endif
36
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000037///////////////
38static const char view_inval_msg[] = "view-inval-msg";
39
40static void postInvalDelay(SkEventSinkID sinkID) {
41 SkEvent* evt = new SkEvent(view_inval_msg);
reed@google.comf2183392011-04-22 14:10:48 +000042 evt->post(sinkID, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000043}
44
45static bool isInvalEvent(const SkEvent& evt) {
46 return evt.isType(view_inval_msg);
47}
48//////////////////
49
reed@android.com8a1c16f2008-12-17 15:59:43 +000050SkViewRegister* SkViewRegister::gHead;
51SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) {
52 static bool gOnce;
53 if (!gOnce) {
54 gHead = NULL;
55 gOnce = true;
56 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000057
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 fChain = gHead;
59 gHead = this;
60}
61
reed@android.comf2b98d62010-12-20 18:26:13 +000062#if defined(SK_SUPPORT_GL)
63 #define SK_USE_SHADERS
64#endif
65
twiz@google.com06c3b6b2011-03-14 16:58:39 +000066#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +000067#include <CoreFoundation/CoreFoundation.h>
68#include <CoreFoundation/CFURLAccess.h>
69
70static void testpdf() {
71 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
72 kCFStringEncodingUTF8);
73 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
74 kCFURLPOSIXPathStyle,
75 false);
76 CFRelease(path);
77 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
78 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
79 CFRelease(url);
80
81 CGContextBeginPage(cg, &box);
82 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
83 CGContextFillEllipseInRect(cg, r);
84 CGContextEndPage(cg);
85 CGContextRelease(cg);
86
87 if (false) {
88 SkBitmap bm;
89 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
90 bm.allocPixels();
91 bm.eraseColor(0);
92
93 SkCanvas canvas(bm);
94
95 }
96}
97#endif
98
99//////////////////////////////////////////////////////////////////////////////
100
reed@google.com569e0432011-04-05 13:07:03 +0000101enum FlipAxisEnum {
102 kFlipAxis_X = (1 << 0),
103 kFlipAxis_Y = (1 << 1)
104};
105
106enum SkTriState {
107 kFalse_SkTriState,
108 kTrue_SkTriState,
109 kUnknown_SkTriState,
110};
111
112static SkTriState cycle_tristate(SkTriState state) {
113 static const SkTriState gCycle[] = {
114 /* kFalse_SkTriState -> */ kUnknown_SkTriState,
115 /* kTrue_SkTriState -> */ kFalse_SkTriState,
116 /* kUnknown_SkTriState -> */ kTrue_SkTriState,
117 };
118 return gCycle[state];
119}
120
reed@google.comf0b5f682011-03-11 20:08:25 +0000121#include "SkDrawFilter.h"
122
reed@google.com569e0432011-04-05 13:07:03 +0000123class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000124public:
reed@google.com176753a2011-05-17 15:32:04 +0000125 FlagsDrawFilter(SkTriState lcd, SkTriState aa, SkTriState filter) :
126 fLCDState(lcd), fAAState(aa), fFilterState(filter) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000127
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000128 virtual void filter(SkPaint* paint, Type t) {
reed@google.com569e0432011-04-05 13:07:03 +0000129 if (kText_Type == t && kUnknown_SkTriState != fLCDState) {
reed@google.com569e0432011-04-05 13:07:03 +0000130 paint->setLCDRenderText(kTrue_SkTriState == fLCDState);
131 }
132 if (kUnknown_SkTriState != fAAState) {
reed@google.com569e0432011-04-05 13:07:03 +0000133 paint->setAntiAlias(kTrue_SkTriState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000134 }
reed@google.com176753a2011-05-17 15:32:04 +0000135 if (kUnknown_SkTriState != fFilterState) {
136 paint->setFilterBitmap(kTrue_SkTriState == fFilterState);
137 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000138 }
139
140private:
reed@google.com569e0432011-04-05 13:07:03 +0000141 SkTriState fLCDState;
reed@google.com569e0432011-04-05 13:07:03 +0000142 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000143 SkTriState fFilterState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000144};
145
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146//////////////////////////////////////////////////////////////////////////////
147
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000148#define MAX_ZOOM_LEVEL 8
149#define MIN_ZOOM_LEVEL -8
150
reed@android.comf2b98d62010-12-20 18:26:13 +0000151static const char gCharEvtName[] = "SampleCode_Char_Event";
152static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153static const char gTitleEvtName[] = "SampleCode_Title_Event";
154static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000155static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
156
157bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
158 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
159 if (outUni) {
160 *outUni = evt.getFast32();
161 }
162 return true;
163 }
164 return false;
165}
166
167bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
168 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
169 if (outKey) {
170 *outKey = (SkKey)evt.getFast32();
171 }
172 return true;
173 }
174 return false;
175}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176
177bool SampleCode::TitleQ(const SkEvent& evt) {
178 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
179}
180
181void SampleCode::TitleR(SkEvent* evt, const char title[]) {
182 SkASSERT(evt && TitleQ(*evt));
183 evt->setString(gTitleEvtName, title);
184}
185
186bool SampleCode::PrefSizeQ(const SkEvent& evt) {
187 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
188}
189
190void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
191 SkASSERT(evt && PrefSizeQ(*evt));
192 SkScalar size[2];
193 size[0] = width;
194 size[1] = height;
195 evt->setScalars(gPrefSizeEvtName, 2, size);
196}
197
reed@android.comf2b98d62010-12-20 18:26:13 +0000198bool SampleCode::FastTextQ(const SkEvent& evt) {
199 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
200}
201
202///////////////////////////////////////////////////////////////////////////////
203
reed@android.com44177402009-11-23 21:07:51 +0000204static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000205static SkMSec gAnimTimePrev;
206
reed@android.com44177402009-11-23 21:07:51 +0000207SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000208SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
209SkScalar SampleCode::GetAnimSecondsDelta() {
210 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
211}
reed@android.com44177402009-11-23 21:07:51 +0000212
213SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000214 // since gAnimTime can be up to 32 bits, we can't convert it to a float
215 // or we'll lose the low bits. Hence we use doubles for the intermediate
216 // calculations
217 double seconds = (double)gAnimTime / 1000.0;
218 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000219 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000220 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000221 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000222 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000223}
224
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225//////////////////////////////////////////////////////////////////////////////
226
reed@android.comf2b98d62010-12-20 18:26:13 +0000227static SkView* curr_view(SkWindow* wind) {
228 SkView::F2BIter iter(wind);
229 return iter.next();
230}
231
reed@android.com8a1c16f2008-12-17 15:59:43 +0000232class SampleWindow : public SkOSWindow {
reed@android.com34245c72009-11-03 04:00:48 +0000233 SkTDArray<SkViewFactory> fSamples;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234public:
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000235 SampleWindow(void* hwnd);
236 virtual ~SampleWindow();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237
reed@android.come522ca52009-11-23 20:10:41 +0000238 virtual void draw(SkCanvas* canvas);
239
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240protected:
241 virtual void onDraw(SkCanvas* canvas);
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000242 virtual bool onHandleKey(SkKey key);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 virtual bool onHandleChar(SkUnichar);
244 virtual void onSizeChange();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000245
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246 virtual SkCanvas* beforeChildren(SkCanvas*);
247 virtual void afterChildren(SkCanvas*);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000248 virtual void beforeChild(SkView* child, SkCanvas* canvas);
249 virtual void afterChild(SkView* child, SkCanvas* canvas);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000250
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000251 virtual bool onEvent(const SkEvent& evt);
reed@android.comf2b98d62010-12-20 18:26:13 +0000252 virtual bool onQuery(SkEvent* evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000254 virtual bool onDispatchClick(int x, int y, Click::State);
reed@google.com52f57e12011-03-16 12:10:02 +0000255 virtual bool onClick(Click* click);
256 virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
257
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258#if 0
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000259 virtual bool handleChar(SkUnichar uni);
260 virtual bool handleEvent(const SkEvent& evt);
261 virtual bool handleKey(SkKey key);
262 virtual bool handleKeyUp(SkKey key);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 virtual bool onHandleKeyUp(SkKey key);
264#endif
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000265
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266private:
reed@android.com34245c72009-11-03 04:00:48 +0000267 int fCurrIndex;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000268
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269 SkPicture* fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000270 SkGpuCanvas* fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000271 GrContext* fGrContext;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272 SkPath fClipPath;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000273
reed@google.com52f57e12011-03-16 12:10:02 +0000274 SkTouchGesture fGesture;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000275 int fZoomLevel;
276 SkScalar fZoomScale;
reed@google.com52f57e12011-03-16 12:10:02 +0000277
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278 enum CanvasType {
279 kRaster_CanvasType,
280 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000281 kGPU_CanvasType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282 };
283 CanvasType fCanvasType;
284
285 bool fUseClip;
reed@android.come522ca52009-11-23 20:10:41 +0000286 bool fNClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287 bool fRepeatDrawing;
288 bool fAnimating;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000289 bool fRotate;
290 bool fScale;
reed@android.comf2b98d62010-12-20 18:26:13 +0000291 bool fRequestGrabImage;
reed@google.com0faac1e2011-05-11 05:58:58 +0000292 bool fUsePipe;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000293 bool fMeasureFPS;
294 SkMSec fMeasureFPS_Time;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000295
Scroggo0f185c22011-03-24 18:35:50 +0000296 // The following are for the 'fatbits' drawing
297 // Latest position of the mouse.
298 int fMouseX, fMouseY;
299 int fFatBitsScale;
300 // Used by the text showing position and color values.
301 SkTypeface* fTypeface;
302 bool fShowZoomer;
303
reed@google.com569e0432011-04-05 13:07:03 +0000304 SkTriState fLCDState;
305 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000306 SkTriState fFilterState;
reed@google.com569e0432011-04-05 13:07:03 +0000307 unsigned fFlipAxis;
reed@google.comf0b5f682011-03-11 20:08:25 +0000308
reed@android.com8a1c16f2008-12-17 15:59:43 +0000309 int fScrollTestX, fScrollTestY;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000310
311 bool make3DReady();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000312 void changeZoomLevel(int delta);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000313
reed@android.com8a1c16f2008-12-17 15:59:43 +0000314 void loadView(SkView*);
315 void updateTitle();
316 bool nextSample();
317
Scroggo0f185c22011-03-24 18:35:50 +0000318 void toggleZoomer();
319 bool zoomIn();
320 bool zoomOut();
321 void updatePointer(int x, int y);
322
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323 void postAnimatingEvent() {
324 if (fAnimating) {
325 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
326 evt->post(this->getSinkID(), ANIMATING_DELAY);
327 }
328 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000329
330
reed@android.com8a1c16f2008-12-17 15:59:43 +0000331 static CanvasType cycle_canvastype(CanvasType);
332
333 typedef SkOSWindow INHERITED;
334};
335
Scroggo0f185c22011-03-24 18:35:50 +0000336bool SampleWindow::zoomIn()
337{
338 // Arbitrarily decided
339 if (fFatBitsScale == 25) return false;
340 fFatBitsScale++;
341 this->inval(NULL);
342 return true;
343}
344
345bool SampleWindow::zoomOut()
346{
347 if (fFatBitsScale == 1) return false;
348 fFatBitsScale--;
349 this->inval(NULL);
350 return true;
351}
352
353void SampleWindow::toggleZoomer()
354{
355 fShowZoomer = !fShowZoomer;
356 this->inval(NULL);
357}
358
359void SampleWindow::updatePointer(int x, int y)
360{
361 fMouseX = x;
362 fMouseY = y;
363 if (fShowZoomer) {
364 this->inval(NULL);
365 }
366}
367
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000368bool SampleWindow::make3DReady() {
369
370#if defined(SK_SUPPORT_GL)
bsalomon@google.com498a6232011-03-10 18:24:15 +0000371 if (attachGL()) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000372 if (NULL != fGrContext) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000373 // various gr lifecycle tests
374 #if 0
375 fGrContext->freeGpuResources();
376 #elif 0
377 // this will leak resources.
378 fGrContext->contextLost();
379 #elif 0
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000380 GrAssert(1 == fGrContext->refcnt());
381 fGrContext->unref();
382 fGrContext = NULL;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000383 #endif
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000384 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000385
bsalomon@google.com498a6232011-03-10 18:24:15 +0000386 if (NULL == fGrContext) {
387 #if defined(SK_USE_SHADERS)
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000388 fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000389 #else
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000390 fGrContext = GrContext::Create(kOpenGL_Fixed_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000391 #endif
reed@google.com569e0432011-04-05 13:07:03 +0000392 SkDebugf("---- constructor\n");
bsalomon@google.com498a6232011-03-10 18:24:15 +0000393 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000394
bsalomon@google.com498a6232011-03-10 18:24:15 +0000395 if (NULL != fGrContext) {
396 return true;
397 } else {
398 detachGL();
399 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000400 }
401#endif
402 SkDebugf("Failed to setup 3D");
403 return false;
404}
405
reed@android.com8a1c16f2008-12-17 15:59:43 +0000406SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) {
407 static const CanvasType gCT[] = {
408 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000409 kGPU_CanvasType,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410 kRaster_CanvasType
411 };
412 return gCT[ct];
413}
414
415SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000416 fPicture = NULL;
reed@android.comf2b98d62010-12-20 18:26:13 +0000417 fGpuCanvas = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000418
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000419 fGrContext = NULL;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000420
reed@android.comf2b98d62010-12-20 18:26:13 +0000421#ifdef DEFAULT_TO_GPU
422 fCanvasType = kGPU_CanvasType;
423#else
reed@android.com8a1c16f2008-12-17 15:59:43 +0000424 fCanvasType = kRaster_CanvasType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000425#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000427 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428 fRepeatDrawing = false;
429 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000430 fRotate = false;
431 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000432 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000433 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000434 fMeasureFPS = false;
reed@google.com569e0432011-04-05 13:07:03 +0000435 fLCDState = kUnknown_SkTriState;
436 fAAState = kUnknown_SkTriState;
437 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000438 fScrollTestX = fScrollTestY = 0;
439
Scroggo0f185c22011-03-24 18:35:50 +0000440 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000441 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000442 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
443 fShowZoomer = false;
444
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000445 fZoomLevel = 0;
446 fZoomScale = SK_Scalar1;
447
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000448// this->setConfig(SkBitmap::kRGB_565_Config);
449 this->setConfig(SkBitmap::kARGB_8888_Config);
450 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000451 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000452
reed@android.com34245c72009-11-03 04:00:48 +0000453 {
454 const SkViewRegister* reg = SkViewRegister::Head();
455 while (reg) {
456 *fSamples.append() = reg->factory();
457 reg = reg->next();
458 }
459 }
460 fCurrIndex = 0;
reed@android.come0f13ee2009-11-04 19:40:25 +0000461 this->loadView(fSamples[fCurrIndex]());
reed@google.comf0b5f682011-03-11 20:08:25 +0000462
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000463#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +0000464 testpdf();
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000465#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000466}
467
468SampleWindow::~SampleWindow() {
469 delete fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000470 delete fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000471 if (NULL != fGrContext) {
472 fGrContext->unref();
473 }
Scroggo0f185c22011-03-24 18:35:50 +0000474 fTypeface->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000475}
476
reed@android.com55e76b22009-11-23 21:46:47 +0000477static SkBitmap capture_bitmap(SkCanvas* canvas) {
478 SkBitmap bm;
479 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
480 src.copyTo(&bm, src.config());
481 return bm;
482}
483
484static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
485 SkBitmap* diff) {
486 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000487
reed@android.com55e76b22009-11-23 21:46:47 +0000488 SkAutoLockPixels alp0(src);
489 SkAutoLockPixels alp1(orig);
490 for (int y = 0; y < src.height(); y++) {
491 const void* srcP = src.getAddr(0, y);
492 const void* origP = orig.getAddr(0, y);
493 size_t bytes = src.width() * src.bytesPerPixel();
494 if (memcmp(srcP, origP, bytes)) {
495 SkDebugf("---------- difference on line %d\n", y);
496 return true;
497 }
498 }
499 return false;
500}
501
Scroggo0f185c22011-03-24 18:35:50 +0000502static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
503{
504 SkColor desiredColor = paint.getColor();
505 paint.setColor(SK_ColorWHITE);
506 const char* c_str = string.c_str();
507 size_t size = string.size();
508 SkRect bounds;
509 paint.measureText(c_str, size, &bounds);
510 bounds.offset(left, top);
511 SkScalar inset = SkIntToScalar(-2);
512 bounds.inset(inset, inset);
513 canvas->drawRect(bounds, paint);
514 if (desiredColor != SK_ColorBLACK) {
515 paint.setColor(SK_ColorBLACK);
516 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
517 }
518 paint.setColor(desiredColor);
519 canvas->drawText(c_str, size, left, top, paint);
520}
521
reed@android.com44177402009-11-23 21:07:51 +0000522#define XCLIP_N 8
523#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000524
525void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000526 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000527 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000528 gAnimTime = SkTime::GetMSecs();
529
reed@google.com569e0432011-04-05 13:07:03 +0000530 SkScalar cx = SkScalarHalf(this->width());
531 SkScalar cy = SkScalarHalf(this->height());
532
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000533 if (fZoomLevel) {
534 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000535 SkPoint center;
536 m = canvas->getTotalMatrix();//.invert(&m);
537 m.mapXY(cx, cy, &center);
538 cx = center.fX;
539 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000540
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000541 m.setTranslate(-cx, -cy);
542 m.postScale(fZoomScale, fZoomScale);
543 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000544
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000545 canvas->concat(m);
546 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000547
reed@google.com569e0432011-04-05 13:07:03 +0000548 if (fFlipAxis) {
549 SkMatrix m;
550 m.setTranslate(cx, cy);
551 if (fFlipAxis & kFlipAxis_X) {
552 m.preScale(-SK_Scalar1, SK_Scalar1);
553 }
554 if (fFlipAxis & kFlipAxis_Y) {
555 m.preScale(SK_Scalar1, -SK_Scalar1);
556 }
557 m.preTranslate(-cx, -cy);
558 canvas->concat(m);
559 }
560
reed@google.com52f57e12011-03-16 12:10:02 +0000561 // Apply any gesture matrix
562 if (true) {
563 const SkMatrix& localM = fGesture.localM();
564 if (localM.getType() & SkMatrix::kScale_Mask) {
565 canvas->setExternalMatrix(&localM);
566 }
567 canvas->concat(localM);
568 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000569
reed@google.com52f57e12011-03-16 12:10:02 +0000570 if (fGesture.isActive()) {
571 this->inval(NULL);
572 }
573 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000574
reed@android.come522ca52009-11-23 20:10:41 +0000575 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000576 this->INHERITED::draw(canvas);
577 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000578
579 const SkScalar w = this->width();
580 const SkScalar h = this->height();
581 const SkScalar cw = w / XCLIP_N;
582 const SkScalar ch = h / YCLIP_N;
583 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000584 SkRect r;
585 r.fTop = y * ch;
586 r.fBottom = (y + 1) * ch;
587 if (y == YCLIP_N - 1) {
588 r.fBottom = h;
589 }
reed@android.come522ca52009-11-23 20:10:41 +0000590 for (int x = 0; x < XCLIP_N; x++) {
591 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000592 r.fLeft = x * cw;
593 r.fRight = (x + 1) * cw;
594 if (x == XCLIP_N - 1) {
595 r.fRight = w;
596 }
reed@android.come522ca52009-11-23 20:10:41 +0000597 canvas->clipRect(r);
598 this->INHERITED::draw(canvas);
599 }
600 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000601
reed@android.com55e76b22009-11-23 21:46:47 +0000602 SkBitmap diff;
603 if (bitmap_diff(canvas, orig, &diff)) {
604 }
reed@android.come522ca52009-11-23 20:10:41 +0000605 } else {
606 this->INHERITED::draw(canvas);
607 }
Scroggo0f185c22011-03-24 18:35:50 +0000608 if (fShowZoomer) {
609 int count = canvas->save();
610 canvas->resetMatrix();
611 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000612 int width = SkScalarRound(this->width());
613 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000614 if (fMouseX >= width) fMouseX = width - 1;
615 else if (fMouseX < 0) fMouseX = 0;
616 if (fMouseY >= height) fMouseY = height - 1;
617 else if (fMouseY < 0) fMouseY = 0;
618 SkBitmap bitmap = capture_bitmap(canvas);
619 // 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 +0000620 int zoomedWidth = (width >> 1) | 1;
621 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000622 SkIRect src;
623 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
624 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
625 SkRect dest;
626 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
627 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
628 SkPaint paint;
629 // Clear the background behind our zoomed in view
630 paint.setColor(SK_ColorWHITE);
631 canvas->drawRect(dest, paint);
632 canvas->drawBitmapRect(bitmap, &src, dest);
633 paint.setColor(SK_ColorBLACK);
634 paint.setStyle(SkPaint::kStroke_Style);
635 // Draw a border around the pixel in the middle
636 SkRect originalPixel;
637 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
638 SkMatrix matrix;
639 SkRect scalarSrc;
640 scalarSrc.set(src);
641 SkColor color = bitmap.getColor(fMouseX, fMouseY);
642 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
643 SkRect pixel;
644 matrix.mapRect(&pixel, originalPixel);
645 // TODO Perhaps measure the values and make the outline white if it's "dark"
646 if (color == SK_ColorBLACK) {
647 paint.setColor(SK_ColorWHITE);
648 }
649 canvas->drawRect(pixel, paint);
650 }
651 paint.setColor(SK_ColorBLACK);
652 // Draw a border around the destination rectangle
653 canvas->drawRect(dest, paint);
654 paint.setStyle(SkPaint::kStrokeAndFill_Style);
655 // Identify the pixel and its color on screen
656 paint.setTypeface(fTypeface);
657 paint.setAntiAlias(true);
658 SkScalar lineHeight = paint.getFontMetrics(NULL);
659 SkString string;
660 string.appendf("(%i, %i)", fMouseX, fMouseY);
661 SkScalar left = dest.fLeft + SkIntToScalar(3);
662 SkScalar i = SK_Scalar1;
663 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
664 // Alpha
665 i += SK_Scalar1;
666 string.reset();
667 string.appendf("A: %X", SkColorGetA(color));
668 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
669 // Red
670 i += SK_Scalar1;
671 string.reset();
672 string.appendf("R: %X", SkColorGetR(color));
673 paint.setColor(SK_ColorRED);
674 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
675 // Green
676 i += SK_Scalar1;
677 string.reset();
678 string.appendf("G: %X", SkColorGetG(color));
679 paint.setColor(SK_ColorGREEN);
680 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
681 // Blue
682 i += SK_Scalar1;
683 string.reset();
684 string.appendf("B: %X", SkColorGetB(color));
685 paint.setColor(SK_ColorBLUE);
686 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
687 canvas->restoreToCount(count);
688 }
reed@android.come522ca52009-11-23 20:10:41 +0000689}
690
reed@android.com8a1c16f2008-12-17 15:59:43 +0000691void SampleWindow::onDraw(SkCanvas* canvas) {
692 if (fRepeatDrawing) {
693 this->inval(NULL);
694 }
695}
696
697#include "SkColorPriv.h"
698
699static void reverseRedAndBlue(const SkBitmap& bm) {
700 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
701 uint8_t* p = (uint8_t*)bm.getPixels();
702 uint8_t* stop = p + bm.getSize();
703 while (p < stop) {
704 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
705 unsigned scale = SkAlpha255To256(p[3]);
706 unsigned r = p[2];
707 unsigned b = p[0];
708 p[0] = SkAlphaMul(r, scale);
709 p[1] = SkAlphaMul(p[1], scale);
710 p[2] = SkAlphaMul(b, scale);
711 p += 4;
712 }
713}
714
715SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000716 if (kGPU_CanvasType != fCanvasType) {
reed@android.com6efdc472008-12-19 18:24:35 +0000717#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000718 detachGL();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000719#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000720 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000721
reed@android.com8a1c16f2008-12-17 15:59:43 +0000722 switch (fCanvasType) {
723 case kRaster_CanvasType:
724 canvas = this->INHERITED::beforeChildren(canvas);
725 break;
726 case kPicture_CanvasType:
727 fPicture = new SkPicture;
728 canvas = fPicture->beginRecording(9999, 9999);
729 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000730 case kGPU_CanvasType: {
reed@google.com64e3eb22011-05-04 14:32:04 +0000731 if (make3DReady()) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000732 SkDevice* device = canvas->getDevice();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000733 const SkBitmap& bitmap = device->accessBitmap(true);
734
bsalomon@google.com5782d712011-01-21 21:03:59 +0000735 GrRenderTarget* renderTarget;
736 renderTarget = fGrContext->createRenderTargetFrom3DApiState();
737 fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
738 renderTarget->unref();
739
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000740 device = fGpuCanvas->createDevice(SkBitmap::kARGB_8888_Config,
741 bitmap.width(), bitmap.height(),
742 false, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000743 fGpuCanvas->setDevice(device)->unref();
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000744
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000745 fGpuCanvas->concat(canvas->getTotalMatrix());
reed@android.comf2b98d62010-12-20 18:26:13 +0000746 canvas = fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000747
reed@android.comf2b98d62010-12-20 18:26:13 +0000748 } else {
749 canvas = this->INHERITED::beforeChildren(canvas);
750 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000751 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000752 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000753 }
754
755 if (fUseClip) {
756 canvas->drawColor(0xFFFF88FF);
757 canvas->clipPath(fClipPath);
758 }
759
760 return canvas;
761}
762
763static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
764 const SkRegion& rgn) {
765 SkCanvas canvas(bm);
766 SkRegion inval(rgn);
767
768 inval.translate(r.fLeft, r.fTop);
769 canvas.clipRegion(inval);
770 canvas.drawColor(0xFFFF8080);
771}
772
773void SampleWindow::afterChildren(SkCanvas* orig) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000774 if (fRequestGrabImage) {
775 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000776
reed@android.comf2b98d62010-12-20 18:26:13 +0000777 SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
778 SkDevice* device = canvas->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000779 SkBitmap bmp;
780 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000781 static int gSampleGrabCounter;
782 SkString name;
783 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000784 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000785 SkImageEncoder::kPNG_Type, 100);
786 }
787 }
788
reed@android.com8a1c16f2008-12-17 15:59:43 +0000789 switch (fCanvasType) {
790 case kRaster_CanvasType:
791 break;
792 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000793 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000794 SkPicture* pict = new SkPicture(*fPicture);
795 fPicture->unref();
796 orig->drawPicture(*pict);
797 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000798 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000799 SkDynamicMemoryWStream ostream;
800 fPicture->serialize(&ostream);
801 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000802
reed@android.com8a1c16f2008-12-17 15:59:43 +0000803 SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
804 SkPicture pict(&istream);
805 orig->drawPicture(pict);
806 } else {
807 fPicture->draw(orig);
808 fPicture->unref();
809 }
810 fPicture = NULL;
811 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000812#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000813 case kGPU_CanvasType:
814 delete fGpuCanvas;
815 fGpuCanvas = NULL;
816 presentGL();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000817 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000818#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000819 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000820
reed@google.com17d7aec2011-04-25 14:31:44 +0000821 // Do this after presentGL and other finishing, rather than in afterChild
822 if (fMeasureFPS && fMeasureFPS_Time) {
823 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
824 this->updateTitle();
825 postInvalDelay(this->getSinkID());
826 }
827
828 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000829 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000830 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
831 int dx = fScrollTestX * 7;
832 int dy = fScrollTestY * 7;
833 SkIRect r;
834 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000835
reed@android.com8a1c16f2008-12-17 15:59:43 +0000836 r.set(50, 50, 50+100, 50+100);
837 bm.scrollRect(&r, dx, dy, &inval);
838 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000839 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000840}
841
reed@android.com6c5f6f22009-08-14 16:08:38 +0000842void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
843 if (fScale) {
844 SkScalar scale = SK_Scalar1 * 7 / 10;
845 SkScalar cx = this->width() / 2;
846 SkScalar cy = this->height() / 2;
847 canvas->translate(cx, cy);
848 canvas->scale(scale, scale);
849 canvas->translate(-cx, -cy);
850 }
851 if (fRotate) {
852 SkScalar cx = this->width() / 2;
853 SkScalar cy = this->height() / 2;
854 canvas->translate(cx, cy);
855 canvas->rotate(SkIntToScalar(30));
856 canvas->translate(-cx, -cy);
857 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000858
reed@google.com176753a2011-05-17 15:32:04 +0000859 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState, fFilterState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000860
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000861 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000862 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
863 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
864 fMeasureFPS_Time = SkTime::GetMSecs();
865 }
866 } else {
867 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000868 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000869 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000870}
871
872void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000873 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000874}
875
reed@android.com8a1c16f2008-12-17 15:59:43 +0000876static SkBitmap::Config gConfigCycle[] = {
877 SkBitmap::kNo_Config, // none -> none
878 SkBitmap::kNo_Config, // a1 -> none
879 SkBitmap::kNo_Config, // a8 -> none
880 SkBitmap::kNo_Config, // index8 -> none
881 SkBitmap::kARGB_4444_Config, // 565 -> 4444
882 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
883 SkBitmap::kRGB_565_Config // 8888 -> 565
884};
885
886static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
887 return gConfigCycle[c];
888}
889
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000890void SampleWindow::changeZoomLevel(int delta) {
891 fZoomLevel += delta;
892 if (fZoomLevel > 0) {
893 fZoomLevel = SkMin32(fZoomLevel, MAX_ZOOM_LEVEL);
894 fZoomScale = SkIntToScalar(fZoomLevel + 1);
895 } else if (fZoomLevel < 0) {
896 fZoomLevel = SkMax32(fZoomLevel, MIN_ZOOM_LEVEL);
897 fZoomScale = SK_Scalar1 / (1 - fZoomLevel);
898 } else {
899 fZoomScale = SK_Scalar1;
900 }
901
902 this->inval(NULL);
903}
904
reed@android.com8a1c16f2008-12-17 15:59:43 +0000905bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000906 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
907 this->loadView(fSamples[fCurrIndex]());
908 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000909}
910
911bool SampleWindow::onEvent(const SkEvent& evt) {
912 if (evt.isType(ANIMATING_EVENTTYPE)) {
913 if (fAnimating) {
914 this->nextSample();
915 this->postAnimatingEvent();
916 }
917 return true;
918 }
reed@android.com34245c72009-11-03 04:00:48 +0000919 if (evt.isType("set-curr-index")) {
920 fCurrIndex = evt.getFast32() % fSamples.count();
921 this->loadView(fSamples[fCurrIndex]());
922 return true;
923 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000924 if (isInvalEvent(evt)) {
925 this->inval(NULL);
926 return true;
927 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000928 return this->INHERITED::onEvent(evt);
929}
930
reed@android.comf2b98d62010-12-20 18:26:13 +0000931bool SampleWindow::onQuery(SkEvent* query) {
932 if (query->isType("get-slide-count")) {
933 query->setFast32(fSamples.count());
934 return true;
935 }
936 if (query->isType("get-slide-title")) {
937 SkView* view = fSamples[query->getFast32()]();
938 SkEvent evt(gTitleEvtName);
939 if (view->doQuery(&evt)) {
940 query->setString("title", evt.findString(gTitleEvtName));
941 }
942 SkSafeUnref(view);
943 return true;
944 }
945 if (query->isType("use-fast-text")) {
946 SkEvent evt(gFastTextEvtName);
947 return curr_view(this)->doQuery(&evt);
948 }
949 return this->INHERITED::onQuery(query);
950}
951
reed@android.com0ae6b242008-12-23 16:49:54 +0000952static void cleanup_for_filename(SkString* name) {
953 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +0000954 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +0000955 switch (str[i]) {
956 case ':': str[i] = '-'; break;
957 case '/': str[i] = '-'; break;
958 case ' ': str[i] = '_'; break;
959 default: break;
960 }
961 }
962}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000963
964bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000965 {
966 SkView* view = curr_view(this);
967 if (view) {
968 SkEvent evt(gCharEvtName);
969 evt.setFast32(uni);
970 if (view->doQuery(&evt)) {
971 return true;
972 }
973 }
974 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000975
reed@android.com8a1c16f2008-12-17 15:59:43 +0000976 int dx = 0xFF;
977 int dy = 0xFF;
978
979 switch (uni) {
980 case '5': dx = 0; dy = 0; break;
981 case '8': dx = 0; dy = -1; break;
982 case '6': dx = 1; dy = 0; break;
983 case '2': dx = 0; dy = 1; break;
984 case '4': dx = -1; dy = 0; break;
985 case '7': dx = -1; dy = -1; break;
986 case '9': dx = 1; dy = -1; break;
987 case '3': dx = 1; dy = 1; break;
988 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000989
reed@android.com8a1c16f2008-12-17 15:59:43 +0000990 default:
991 break;
992 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000993
reed@android.com8a1c16f2008-12-17 15:59:43 +0000994 if (0xFF != dx && 0xFF != dy) {
995 if ((dx | dy) == 0) {
996 fScrollTestX = fScrollTestY = 0;
997 } else {
998 fScrollTestX += dx;
999 fScrollTestY += dy;
1000 }
1001 this->inval(NULL);
1002 return true;
1003 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001004
reed@android.com0ae6b242008-12-23 16:49:54 +00001005 switch (uni) {
1006 case 'a':
1007 fAnimating = !fAnimating;
1008 this->postAnimatingEvent();
1009 this->updateTitle();
1010 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001011 case 'b':
1012 fAAState = cycle_tristate(fAAState);
1013 this->updateTitle();
1014 this->inval(NULL);
1015 break;
1016 case 'c':
1017 fUseClip = !fUseClip;
1018 this->inval(NULL);
1019 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001020 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001021 case 'd':
1022 SkGraphics::SetFontCacheUsed(0);
1023 return true;
1024 case 'f':
1025 fMeasureFPS = !fMeasureFPS;
1026 this->inval(NULL);
1027 break;
1028 case 'g':
1029 fRequestGrabImage = true;
1030 this->inval(NULL);
1031 break;
1032 case 'i':
1033 this->zoomIn();
1034 break;
1035 case 'l':
1036 fLCDState = cycle_tristate(fLCDState);
1037 this->updateTitle();
1038 this->inval(NULL);
1039 break;
reed@google.com176753a2011-05-17 15:32:04 +00001040 case 'n':
1041 fFilterState = cycle_tristate(fFilterState);
1042 this->updateTitle();
1043 this->inval(NULL);
1044 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001045 case 'o':
1046 this->zoomOut();
1047 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001048 case 'p':
1049 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001050 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001051 this->inval(NULL);
1052 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001053 case 'r':
1054 fRotate = !fRotate;
1055 this->inval(NULL);
1056 this->updateTitle();
1057 return true;
1058 case 's':
1059 fScale = !fScale;
1060 this->inval(NULL);
1061 this->updateTitle();
1062 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001063 case 'x':
1064 fFlipAxis ^= kFlipAxis_X;
1065 this->updateTitle();
1066 this->inval(NULL);
1067 break;
1068 case 'y':
1069 fFlipAxis ^= kFlipAxis_Y;
1070 this->updateTitle();
1071 this->inval(NULL);
1072 break;
scroggo08526c02011-03-22 14:03:21 +00001073 case 'z':
1074 this->toggleZoomer();
1075 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001076 default:
1077 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001078 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001079
reed@android.com8a1c16f2008-12-17 15:59:43 +00001080 return this->INHERITED::onHandleChar(uni);
1081}
1082
1083#include "SkDumpCanvas.h"
1084
1085bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001086 {
1087 SkView* view = curr_view(this);
1088 if (view) {
1089 SkEvent evt(gKeyEvtName);
1090 evt.setFast32(key);
1091 if (view->doQuery(&evt)) {
1092 return true;
1093 }
1094 }
1095 }
1096
reed@android.com8a1c16f2008-12-17 15:59:43 +00001097 switch (key) {
1098 case kRight_SkKey:
1099 if (this->nextSample()) {
1100 return true;
1101 }
1102 break;
1103 case kLeft_SkKey:
1104 fCanvasType = cycle_canvastype(fCanvasType);
1105 this->updateTitle();
1106 this->inval(NULL);
1107 return true;
1108 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001109 if (USE_ARROWS_FOR_ZOOM) {
1110 this->changeZoomLevel(1);
1111 } else {
1112 fNClip = !fNClip;
1113 this->inval(NULL);
1114 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001115 this->updateTitle();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001116 return true;
1117 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001118 if (USE_ARROWS_FOR_ZOOM) {
1119 this->changeZoomLevel(-1);
1120 } else {
1121 this->setConfig(cycle_configs(this->getBitmap().config()));
1122 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001123 this->updateTitle();
1124 return true;
1125 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001126 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001127 SkDebugfDumper dumper;
1128 SkDumpCanvas dc(&dumper);
1129 this->draw(&dc);
1130 } else {
1131 fRepeatDrawing = !fRepeatDrawing;
1132 if (fRepeatDrawing) {
1133 this->inval(NULL);
1134 }
1135 }
1136 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001137 case kBack_SkKey:
1138 this->loadView(NULL);
1139 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001140 default:
1141 break;
1142 }
1143 return this->INHERITED::onHandleKey(key);
1144}
1145
reed@google.com52f57e12011-03-16 12:10:02 +00001146///////////////////////////////////////////////////////////////////////////////
1147
1148static const char gGestureClickType[] = "GestureClickType";
1149
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001150bool SampleWindow::onDispatchClick(int x, int y, Click::State state) {
Scroggo0f185c22011-03-24 18:35:50 +00001151 if (Click::kMoved_State == state) {
1152 updatePointer(x, y);
1153 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001154 int w = SkScalarRound(this->width());
1155 int h = SkScalarRound(this->height());
1156
1157 // check for the resize-box
1158 if (w - x < 16 && h - y < 16) {
1159 return false; // let the OS handle the click
1160 } else {
1161 return this->INHERITED::onDispatchClick(x, y, state);
1162 }
1163}
1164
reed@google.com52f57e12011-03-16 12:10:02 +00001165class GestureClick : public SkView::Click {
1166public:
1167 GestureClick(SkView* target) : SkView::Click(target) {
1168 this->setType(gGestureClickType);
1169 }
1170
1171 static bool IsGesture(Click* click) {
1172 return click->isType(gGestureClickType);
1173 }
1174};
1175
1176SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1177 return new GestureClick(this);
1178}
1179
1180bool SampleWindow::onClick(Click* click) {
1181 if (GestureClick::IsGesture(click)) {
1182 float x = SkScalarToFloat(click->fCurr.fX);
1183 float y = SkScalarToFloat(click->fCurr.fY);
1184 switch (click->fState) {
1185 case SkView::Click::kDown_State:
1186 fGesture.touchBegin(click, x, y);
1187 break;
1188 case SkView::Click::kMoved_State:
1189 fGesture.touchMoved(click, x, y);
1190 this->inval(NULL);
1191 break;
1192 case SkView::Click::kUp_State:
1193 fGesture.touchEnd(click);
1194 this->inval(NULL);
1195 break;
1196 }
1197 return true;
1198 }
1199 return false;
1200}
1201
1202///////////////////////////////////////////////////////////////////////////////
1203
reed@android.com8a1c16f2008-12-17 15:59:43 +00001204void SampleWindow::loadView(SkView* view) {
1205 SkView::F2BIter iter(this);
1206 SkView* prev = iter.next();
1207 if (prev) {
1208 prev->detachFromParent();
1209 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001210
reed@android.com34245c72009-11-03 04:00:48 +00001211 if (NULL == view) {
1212 view = create_overview(fSamples.count(), fSamples.begin());
1213 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001214 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001215 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001216 this->attachChildToFront(view)->unref();
1217 view->setSize(this->width(), this->height());
1218
1219 this->updateTitle();
1220}
1221
1222static const char* gConfigNames[] = {
1223 "unknown config",
1224 "A1",
1225 "A8",
1226 "Index8",
1227 "565",
1228 "4444",
1229 "8888"
1230};
1231
1232static const char* configToString(SkBitmap::Config c) {
1233 return gConfigNames[c];
1234}
1235
1236static const char* gCanvasTypePrefix[] = {
1237 "raster: ",
1238 "picture: ",
1239 "opengl: "
1240};
1241
reed@google.com569e0432011-04-05 13:07:03 +00001242static const char* trystate_str(SkTriState state,
1243 const char trueStr[], const char falseStr[]) {
1244 if (kTrue_SkTriState == state) {
1245 return trueStr;
1246 } else if (kFalse_SkTriState == state) {
1247 return falseStr;
1248 }
1249 return NULL;
1250}
1251
reed@android.com8a1c16f2008-12-17 15:59:43 +00001252void SampleWindow::updateTitle() {
1253 SkString title;
1254
1255 SkView::F2BIter iter(this);
1256 SkView* view = iter.next();
1257 SkEvent evt(gTitleEvtName);
1258 if (view->doQuery(&evt)) {
1259 title.set(evt.findString(gTitleEvtName));
1260 }
1261 if (title.size() == 0) {
1262 title.set("<unknown>");
1263 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001264
reed@android.com8a1c16f2008-12-17 15:59:43 +00001265 title.prepend(gCanvasTypePrefix[fCanvasType]);
1266
1267 title.prepend(" ");
1268 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001269
reed@android.com8a1c16f2008-12-17 15:59:43 +00001270 if (fAnimating) {
1271 title.prepend("<A> ");
1272 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001273 if (fScale) {
1274 title.prepend("<S> ");
1275 }
1276 if (fRotate) {
1277 title.prepend("<R> ");
1278 }
reed@android.come522ca52009-11-23 20:10:41 +00001279 if (fNClip) {
1280 title.prepend("<C> ");
1281 }
reed@google.com569e0432011-04-05 13:07:03 +00001282
1283 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1284 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com176753a2011-05-17 15:32:04 +00001285 title.prepend(trystate_str(fFilterState, "LERP ", "lerp "));
reed@google.com569e0432011-04-05 13:07:03 +00001286 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1287 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001288
1289 if (fZoomLevel) {
1290 title.prependf("{%d} ", fZoomLevel);
1291 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001292
1293 if (fMeasureFPS) {
1294 title.appendf(" %4d ms", fMeasureFPS_Time);
1295 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001296 if (fUsePipe && SampleView::IsSampleView(view)) {
1297 title.prepend("<P> ");
1298 }
1299 if (SampleView::IsSampleView(view)) {
1300 title.prepend("! ");
1301 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001302
reed@android.com8a1c16f2008-12-17 15:59:43 +00001303 this->setTitle(title.c_str());
1304}
1305
1306void SampleWindow::onSizeChange() {
1307 this->INHERITED::onSizeChange();
1308
1309 SkView::F2BIter iter(this);
1310 SkView* view = iter.next();
1311 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001312
reed@android.com8a1c16f2008-12-17 15:59:43 +00001313 // rebuild our clippath
1314 {
1315 const SkScalar W = this->width();
1316 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001317
reed@android.com8a1c16f2008-12-17 15:59:43 +00001318 fClipPath.reset();
1319#if 0
1320 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1321 SkRect r;
1322 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1323 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1324 fClipPath.addRect(r);
1325 }
1326#else
1327 SkRect r;
1328 r.set(0, 0, W, H);
1329 fClipPath.addRect(r, SkPath::kCCW_Direction);
1330 r.set(W/4, H/4, W*3/4, H*3/4);
1331 fClipPath.addRect(r, SkPath::kCW_Direction);
1332#endif
1333 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001334
reed@android.com8a1c16f2008-12-17 15:59:43 +00001335 this->updateTitle(); // to refresh our config
1336}
1337
1338///////////////////////////////////////////////////////////////////////////////
1339
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001340static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001341static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001342static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001343
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001344bool SampleView::IsSampleView(SkView* view) {
1345 SkEvent evt(is_sample_view_tag);
1346 return view->doQuery(&evt);
1347}
1348
reed@google.comf2183392011-04-22 14:10:48 +00001349bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001350 SkEvent evt(repeat_count_tag);
1351 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001352 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001353}
1354
reed@google.com0faac1e2011-05-11 05:58:58 +00001355bool SampleView::SetUsePipe(SkView* view, bool pred) {
1356 SkEvent evt(set_use_pipe_tag);
1357 evt.setFast32(pred);
1358 return view->doEvent(evt);
1359}
1360
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001361bool SampleView::onEvent(const SkEvent& evt) {
1362 if (evt.isType(repeat_count_tag)) {
1363 fRepeatCount = evt.getFast32();
1364 return true;
1365 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001366 if (evt.isType(set_use_pipe_tag)) {
1367 fUsePipe = !!evt.getFast32();
1368 return true;
1369 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001370 return this->INHERITED::onEvent(evt);
1371}
1372
1373bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001374 if (evt->isType(is_sample_view_tag)) {
1375 return true;
1376 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001377 return this->INHERITED::onQuery(evt);
1378}
1379
reed@google.com4aebe4f2011-05-05 14:07:35 +00001380#define TEST_GPIPEx
reed@google.com64e3eb22011-05-04 14:32:04 +00001381
reed@google.com68f456d2011-05-02 18:55:39 +00001382#ifdef TEST_GPIPE
1383 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001384
1385class SimplePC : public SkGPipeController {
1386public:
1387 SimplePC(SkCanvas* target);
1388 ~SimplePC();
1389
1390 virtual void* requestBlock(size_t minRequest, size_t* actual);
1391 virtual void notifyWritten(size_t bytes);
1392
1393private:
reed@google.com961ddb02011-05-05 14:03:48 +00001394 SkGPipeReader fReader;
1395 void* fBlock;
1396 size_t fBlockSize;
1397 size_t fBytesWritten;
1398 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001399 SkGPipeReader::Status fStatus;
1400
1401 size_t fTotalWritten;
1402};
1403
1404SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1405 fBlock = NULL;
1406 fBlockSize = fBytesWritten = 0;
1407 fStatus = SkGPipeReader::kDone_Status;
1408 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001409 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001410}
1411
1412SimplePC::~SimplePC() {
1413// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1414 sk_free(fBlock);
1415
reed@google.com0faac1e2011-05-11 05:58:58 +00001416 if (fTotalWritten) {
1417 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1418 fAtomsWritten, fStatus);
1419 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001420}
1421
1422void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1423 sk_free(fBlock);
1424
1425 fBlockSize = minRequest * 4;
1426 fBlock = sk_malloc_throw(fBlockSize);
1427 fBytesWritten = 0;
1428 *actual = fBlockSize;
1429 return fBlock;
1430}
1431
1432void SimplePC::notifyWritten(size_t bytes) {
1433 SkASSERT(fBytesWritten + bytes <= fBlockSize);
1434
1435 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1436 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1437 fBytesWritten += bytes;
1438 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001439
1440 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001441}
1442
reed@google.com68f456d2011-05-02 18:55:39 +00001443#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001444
reed@google.com64e3eb22011-05-04 14:32:04 +00001445
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001446void SampleView::onDraw(SkCanvas* canvas) {
1447 this->onDrawBackground(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001448
1449#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001450 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001451 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001452 if (fUsePipe) {
1453 canvas = writer.startRecording(&controller);
1454 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001455#endif
1456
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001457 for (int i = 0; i < fRepeatCount; i++) {
1458 SkAutoCanvasRestore acr(canvas, true);
1459 this->onDrawContent(canvas);
1460 }
1461}
1462
1463void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001464 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001465}
1466
1467///////////////////////////////////////////////////////////////////////////////
1468
reed@android.comf2b98d62010-12-20 18:26:13 +00001469template <typename T> void SkTBSort(T array[], int count) {
1470 for (int i = 1; i < count - 1; i++) {
1471 bool didSwap = false;
1472 for (int j = count - 1; j > i; --j) {
1473 if (array[j] < array[j-1]) {
1474 T tmp(array[j-1]);
1475 array[j-1] = array[j];
1476 array[j] = tmp;
1477 didSwap = true;
1478 }
1479 }
1480 if (!didSwap) {
1481 break;
1482 }
1483 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001484
reed@android.comf2b98d62010-12-20 18:26:13 +00001485 for (int k = 0; k < count - 1; k++) {
1486 SkASSERT(!(array[k+1] < array[k]));
1487 }
1488}
1489
1490#include "SkRandom.h"
1491
1492static void rand_rect(SkIRect* rect, SkRandom& rand) {
1493 int bits = 8;
1494 int shift = 32 - bits;
1495 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1496 rand.nextU() >> shift, rand.nextU() >> shift);
1497 rect->sort();
1498}
1499
1500static void dumpRect(const SkIRect& r) {
1501 SkDebugf(" { %d, %d, %d, %d },\n",
1502 r.fLeft, r.fTop,
1503 r.fRight, r.fBottom);
1504}
1505
1506static void test_rects(const SkIRect rect[], int count) {
1507 SkRegion rgn0, rgn1;
1508
1509 for (int i = 0; i < count; i++) {
1510 rgn0.op(rect[i], SkRegion::kUnion_Op);
1511 // dumpRect(rect[i]);
1512 }
1513 rgn1.setRects(rect, count);
1514
1515 if (rgn0 != rgn1) {
1516 SkDebugf("\n");
1517 for (int i = 0; i < count; i++) {
1518 dumpRect(rect[i]);
1519 }
1520 SkDebugf("\n");
1521 }
1522}
1523
1524static void test() {
1525 size_t i;
1526
1527 const SkIRect r0[] = {
1528 { 0, 0, 1, 1 },
1529 { 2, 2, 3, 3 },
1530 };
1531 const SkIRect r1[] = {
1532 { 0, 0, 1, 3 },
1533 { 1, 1, 2, 2 },
1534 { 2, 0, 3, 3 },
1535 };
1536 const SkIRect r2[] = {
1537 { 0, 0, 1, 2 },
1538 { 2, 1, 3, 3 },
1539 { 4, 0, 5, 1 },
1540 { 6, 0, 7, 4 },
1541 };
1542
1543 static const struct {
1544 const SkIRect* fRects;
1545 int fCount;
1546 } gRecs[] = {
1547 { r0, SK_ARRAY_COUNT(r0) },
1548 { r1, SK_ARRAY_COUNT(r1) },
1549 { r2, SK_ARRAY_COUNT(r2) },
1550 };
1551
1552 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1553 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1554 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001555
reed@android.comf2b98d62010-12-20 18:26:13 +00001556 SkRandom rand;
1557 for (i = 0; i < 10000; i++) {
1558 SkRegion rgn0, rgn1;
1559
1560 const int N = 8;
1561 SkIRect rect[N];
1562 for (int j = 0; j < N; j++) {
1563 rand_rect(&rect[j], rand);
1564 }
1565 test_rects(rect, N);
1566 }
1567}
1568
reed@android.com8a1c16f2008-12-17 15:59:43 +00001569SkOSWindow* create_sk_window(void* hwnd) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001570// test();
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001571 return new SampleWindow(hwnd);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001572}
1573
1574void get_preferred_size(int* x, int* y, int* width, int* height) {
1575 *x = 10;
1576 *y = 50;
1577 *width = 640;
1578 *height = 480;
1579}
1580
1581void application_init() {
1582// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001583#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001584 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001585#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001586 SkGraphics::Init();
1587 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001588}
1589
1590void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001591 SkEvent::Term();
1592 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001593}