blob: 9b35be81b9f344686073cc0363f8165e0bb6be7a [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.com569e0432011-04-05 13:07:03 +0000125 FlagsDrawFilter(SkTriState lcd, SkTriState aa) : fLCDState(lcd), fAAState(aa) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000126
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000127 virtual void filter(SkPaint* paint, Type t) {
reed@google.com569e0432011-04-05 13:07:03 +0000128 if (kText_Type == t && kUnknown_SkTriState != fLCDState) {
reed@google.com569e0432011-04-05 13:07:03 +0000129 paint->setLCDRenderText(kTrue_SkTriState == fLCDState);
130 }
131 if (kUnknown_SkTriState != fAAState) {
reed@google.com569e0432011-04-05 13:07:03 +0000132 paint->setAntiAlias(kTrue_SkTriState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000133 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000134 }
135
136private:
reed@google.com569e0432011-04-05 13:07:03 +0000137 SkTriState fLCDState;
reed@google.com569e0432011-04-05 13:07:03 +0000138 SkTriState fAAState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000139};
140
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141//////////////////////////////////////////////////////////////////////////////
142
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000143#define MAX_ZOOM_LEVEL 8
144#define MIN_ZOOM_LEVEL -8
145
reed@android.comf2b98d62010-12-20 18:26:13 +0000146static const char gCharEvtName[] = "SampleCode_Char_Event";
147static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148static const char gTitleEvtName[] = "SampleCode_Title_Event";
149static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000150static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
151
152bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
153 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
154 if (outUni) {
155 *outUni = evt.getFast32();
156 }
157 return true;
158 }
159 return false;
160}
161
162bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
163 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
164 if (outKey) {
165 *outKey = (SkKey)evt.getFast32();
166 }
167 return true;
168 }
169 return false;
170}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171
172bool SampleCode::TitleQ(const SkEvent& evt) {
173 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
174}
175
176void SampleCode::TitleR(SkEvent* evt, const char title[]) {
177 SkASSERT(evt && TitleQ(*evt));
178 evt->setString(gTitleEvtName, title);
179}
180
181bool SampleCode::PrefSizeQ(const SkEvent& evt) {
182 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
183}
184
185void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
186 SkASSERT(evt && PrefSizeQ(*evt));
187 SkScalar size[2];
188 size[0] = width;
189 size[1] = height;
190 evt->setScalars(gPrefSizeEvtName, 2, size);
191}
192
reed@android.comf2b98d62010-12-20 18:26:13 +0000193bool SampleCode::FastTextQ(const SkEvent& evt) {
194 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
195}
196
197///////////////////////////////////////////////////////////////////////////////
198
reed@android.com44177402009-11-23 21:07:51 +0000199static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000200static SkMSec gAnimTimePrev;
201
reed@android.com44177402009-11-23 21:07:51 +0000202SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000203SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
204SkScalar SampleCode::GetAnimSecondsDelta() {
205 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
206}
reed@android.com44177402009-11-23 21:07:51 +0000207
208SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000209 // since gAnimTime can be up to 32 bits, we can't convert it to a float
210 // or we'll lose the low bits. Hence we use doubles for the intermediate
211 // calculations
212 double seconds = (double)gAnimTime / 1000.0;
213 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000214 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000215 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000216 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000217 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000218}
219
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220//////////////////////////////////////////////////////////////////////////////
221
reed@android.comf2b98d62010-12-20 18:26:13 +0000222static SkView* curr_view(SkWindow* wind) {
223 SkView::F2BIter iter(wind);
224 return iter.next();
225}
226
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227class SampleWindow : public SkOSWindow {
reed@android.com34245c72009-11-03 04:00:48 +0000228 SkTDArray<SkViewFactory> fSamples;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229public:
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000230 SampleWindow(void* hwnd);
231 virtual ~SampleWindow();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000232
reed@android.come522ca52009-11-23 20:10:41 +0000233 virtual void draw(SkCanvas* canvas);
234
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235protected:
236 virtual void onDraw(SkCanvas* canvas);
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000237 virtual bool onHandleKey(SkKey key);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 virtual bool onHandleChar(SkUnichar);
239 virtual void onSizeChange();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000240
reed@android.com8a1c16f2008-12-17 15:59:43 +0000241 virtual SkCanvas* beforeChildren(SkCanvas*);
242 virtual void afterChildren(SkCanvas*);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000243 virtual void beforeChild(SkView* child, SkCanvas* canvas);
244 virtual void afterChild(SkView* child, SkCanvas* canvas);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000245
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000246 virtual bool onEvent(const SkEvent& evt);
reed@android.comf2b98d62010-12-20 18:26:13 +0000247 virtual bool onQuery(SkEvent* evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000249 virtual bool onDispatchClick(int x, int y, Click::State);
reed@google.com52f57e12011-03-16 12:10:02 +0000250 virtual bool onClick(Click* click);
251 virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
252
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253#if 0
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000254 virtual bool handleChar(SkUnichar uni);
255 virtual bool handleEvent(const SkEvent& evt);
256 virtual bool handleKey(SkKey key);
257 virtual bool handleKeyUp(SkKey key);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258 virtual bool onHandleKeyUp(SkKey key);
259#endif
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000260
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261private:
reed@android.com34245c72009-11-03 04:00:48 +0000262 int fCurrIndex;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000263
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264 SkPicture* fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000265 SkGpuCanvas* fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000266 GrContext* fGrContext;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000267 SkPath fClipPath;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000268
reed@google.com52f57e12011-03-16 12:10:02 +0000269 SkTouchGesture fGesture;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000270 int fZoomLevel;
271 SkScalar fZoomScale;
reed@google.com52f57e12011-03-16 12:10:02 +0000272
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273 enum CanvasType {
274 kRaster_CanvasType,
275 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000276 kGPU_CanvasType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277 };
278 CanvasType fCanvasType;
279
280 bool fUseClip;
reed@android.come522ca52009-11-23 20:10:41 +0000281 bool fNClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282 bool fRepeatDrawing;
283 bool fAnimating;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000284 bool fRotate;
285 bool fScale;
reed@android.comf2b98d62010-12-20 18:26:13 +0000286 bool fRequestGrabImage;
reed@google.com0faac1e2011-05-11 05:58:58 +0000287 bool fUsePipe;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000288 bool fMeasureFPS;
289 SkMSec fMeasureFPS_Time;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000290
Scroggo0f185c22011-03-24 18:35:50 +0000291 // The following are for the 'fatbits' drawing
292 // Latest position of the mouse.
293 int fMouseX, fMouseY;
294 int fFatBitsScale;
295 // Used by the text showing position and color values.
296 SkTypeface* fTypeface;
297 bool fShowZoomer;
298
reed@google.com569e0432011-04-05 13:07:03 +0000299 SkTriState fLCDState;
300 SkTriState fAAState;
301 unsigned fFlipAxis;
reed@google.comf0b5f682011-03-11 20:08:25 +0000302
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303 int fScrollTestX, fScrollTestY;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000304
305 bool make3DReady();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000306 void changeZoomLevel(int delta);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000307
reed@android.com8a1c16f2008-12-17 15:59:43 +0000308 void loadView(SkView*);
309 void updateTitle();
310 bool nextSample();
311
Scroggo0f185c22011-03-24 18:35:50 +0000312 void toggleZoomer();
313 bool zoomIn();
314 bool zoomOut();
315 void updatePointer(int x, int y);
316
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317 void postAnimatingEvent() {
318 if (fAnimating) {
319 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
320 evt->post(this->getSinkID(), ANIMATING_DELAY);
321 }
322 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000323
324
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325 static CanvasType cycle_canvastype(CanvasType);
326
327 typedef SkOSWindow INHERITED;
328};
329
Scroggo0f185c22011-03-24 18:35:50 +0000330bool SampleWindow::zoomIn()
331{
332 // Arbitrarily decided
333 if (fFatBitsScale == 25) return false;
334 fFatBitsScale++;
335 this->inval(NULL);
336 return true;
337}
338
339bool SampleWindow::zoomOut()
340{
341 if (fFatBitsScale == 1) return false;
342 fFatBitsScale--;
343 this->inval(NULL);
344 return true;
345}
346
347void SampleWindow::toggleZoomer()
348{
349 fShowZoomer = !fShowZoomer;
350 this->inval(NULL);
351}
352
353void SampleWindow::updatePointer(int x, int y)
354{
355 fMouseX = x;
356 fMouseY = y;
357 if (fShowZoomer) {
358 this->inval(NULL);
359 }
360}
361
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000362bool SampleWindow::make3DReady() {
363
364#if defined(SK_SUPPORT_GL)
bsalomon@google.com498a6232011-03-10 18:24:15 +0000365 if (attachGL()) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000366 if (NULL != fGrContext) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000367 // various gr lifecycle tests
368 #if 0
369 fGrContext->freeGpuResources();
370 #elif 0
371 // this will leak resources.
372 fGrContext->contextLost();
373 #elif 0
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000374 GrAssert(1 == fGrContext->refcnt());
375 fGrContext->unref();
376 fGrContext = NULL;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000377 #endif
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000378 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000379
bsalomon@google.com498a6232011-03-10 18:24:15 +0000380 if (NULL == fGrContext) {
381 #if defined(SK_USE_SHADERS)
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000382 fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000383 #else
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000384 fGrContext = GrContext::Create(kOpenGL_Fixed_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000385 #endif
reed@google.com569e0432011-04-05 13:07:03 +0000386 SkDebugf("---- constructor\n");
bsalomon@google.com498a6232011-03-10 18:24:15 +0000387 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000388
bsalomon@google.com498a6232011-03-10 18:24:15 +0000389 if (NULL != fGrContext) {
390 return true;
391 } else {
392 detachGL();
393 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000394 }
395#endif
396 SkDebugf("Failed to setup 3D");
397 return false;
398}
399
reed@android.com8a1c16f2008-12-17 15:59:43 +0000400SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) {
401 static const CanvasType gCT[] = {
402 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000403 kGPU_CanvasType,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000404 kRaster_CanvasType
405 };
406 return gCT[ct];
407}
408
409SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410 fPicture = NULL;
reed@android.comf2b98d62010-12-20 18:26:13 +0000411 fGpuCanvas = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000413 fGrContext = NULL;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000414
reed@android.comf2b98d62010-12-20 18:26:13 +0000415#ifdef DEFAULT_TO_GPU
416 fCanvasType = kGPU_CanvasType;
417#else
reed@android.com8a1c16f2008-12-17 15:59:43 +0000418 fCanvasType = kRaster_CanvasType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000419#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000420 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000421 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000422 fRepeatDrawing = false;
423 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000424 fRotate = false;
425 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000426 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000427 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000428 fMeasureFPS = false;
reed@google.com569e0432011-04-05 13:07:03 +0000429 fLCDState = kUnknown_SkTriState;
430 fAAState = kUnknown_SkTriState;
431 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000432 fScrollTestX = fScrollTestY = 0;
433
Scroggo0f185c22011-03-24 18:35:50 +0000434 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000435 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000436 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
437 fShowZoomer = false;
438
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000439 fZoomLevel = 0;
440 fZoomScale = SK_Scalar1;
441
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000442// this->setConfig(SkBitmap::kRGB_565_Config);
443 this->setConfig(SkBitmap::kARGB_8888_Config);
444 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000445 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000446
reed@android.com34245c72009-11-03 04:00:48 +0000447 {
448 const SkViewRegister* reg = SkViewRegister::Head();
449 while (reg) {
450 *fSamples.append() = reg->factory();
451 reg = reg->next();
452 }
453 }
454 fCurrIndex = 0;
reed@android.come0f13ee2009-11-04 19:40:25 +0000455 this->loadView(fSamples[fCurrIndex]());
reed@google.comf0b5f682011-03-11 20:08:25 +0000456
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000457#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +0000458 testpdf();
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000459#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000460}
461
462SampleWindow::~SampleWindow() {
463 delete fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000464 delete fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000465 if (NULL != fGrContext) {
466 fGrContext->unref();
467 }
Scroggo0f185c22011-03-24 18:35:50 +0000468 fTypeface->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000469}
470
reed@android.com55e76b22009-11-23 21:46:47 +0000471static SkBitmap capture_bitmap(SkCanvas* canvas) {
472 SkBitmap bm;
473 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
474 src.copyTo(&bm, src.config());
475 return bm;
476}
477
478static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
479 SkBitmap* diff) {
480 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000481
reed@android.com55e76b22009-11-23 21:46:47 +0000482 SkAutoLockPixels alp0(src);
483 SkAutoLockPixels alp1(orig);
484 for (int y = 0; y < src.height(); y++) {
485 const void* srcP = src.getAddr(0, y);
486 const void* origP = orig.getAddr(0, y);
487 size_t bytes = src.width() * src.bytesPerPixel();
488 if (memcmp(srcP, origP, bytes)) {
489 SkDebugf("---------- difference on line %d\n", y);
490 return true;
491 }
492 }
493 return false;
494}
495
Scroggo0f185c22011-03-24 18:35:50 +0000496static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
497{
498 SkColor desiredColor = paint.getColor();
499 paint.setColor(SK_ColorWHITE);
500 const char* c_str = string.c_str();
501 size_t size = string.size();
502 SkRect bounds;
503 paint.measureText(c_str, size, &bounds);
504 bounds.offset(left, top);
505 SkScalar inset = SkIntToScalar(-2);
506 bounds.inset(inset, inset);
507 canvas->drawRect(bounds, paint);
508 if (desiredColor != SK_ColorBLACK) {
509 paint.setColor(SK_ColorBLACK);
510 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
511 }
512 paint.setColor(desiredColor);
513 canvas->drawText(c_str, size, left, top, paint);
514}
515
reed@android.com44177402009-11-23 21:07:51 +0000516#define XCLIP_N 8
517#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000518
519void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000520 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000521 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000522 gAnimTime = SkTime::GetMSecs();
523
reed@google.com569e0432011-04-05 13:07:03 +0000524 SkScalar cx = SkScalarHalf(this->width());
525 SkScalar cy = SkScalarHalf(this->height());
526
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000527 if (fZoomLevel) {
528 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000529 SkPoint center;
530 m = canvas->getTotalMatrix();//.invert(&m);
531 m.mapXY(cx, cy, &center);
532 cx = center.fX;
533 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000534
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000535 m.setTranslate(-cx, -cy);
536 m.postScale(fZoomScale, fZoomScale);
537 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000538
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000539 canvas->concat(m);
540 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000541
reed@google.com569e0432011-04-05 13:07:03 +0000542 if (fFlipAxis) {
543 SkMatrix m;
544 m.setTranslate(cx, cy);
545 if (fFlipAxis & kFlipAxis_X) {
546 m.preScale(-SK_Scalar1, SK_Scalar1);
547 }
548 if (fFlipAxis & kFlipAxis_Y) {
549 m.preScale(SK_Scalar1, -SK_Scalar1);
550 }
551 m.preTranslate(-cx, -cy);
552 canvas->concat(m);
553 }
554
reed@google.com52f57e12011-03-16 12:10:02 +0000555 // Apply any gesture matrix
556 if (true) {
557 const SkMatrix& localM = fGesture.localM();
558 if (localM.getType() & SkMatrix::kScale_Mask) {
559 canvas->setExternalMatrix(&localM);
560 }
561 canvas->concat(localM);
562 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000563
reed@google.com52f57e12011-03-16 12:10:02 +0000564 if (fGesture.isActive()) {
565 this->inval(NULL);
566 }
567 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000568
reed@android.come522ca52009-11-23 20:10:41 +0000569 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000570 this->INHERITED::draw(canvas);
571 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000572
573 const SkScalar w = this->width();
574 const SkScalar h = this->height();
575 const SkScalar cw = w / XCLIP_N;
576 const SkScalar ch = h / YCLIP_N;
577 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000578 SkRect r;
579 r.fTop = y * ch;
580 r.fBottom = (y + 1) * ch;
581 if (y == YCLIP_N - 1) {
582 r.fBottom = h;
583 }
reed@android.come522ca52009-11-23 20:10:41 +0000584 for (int x = 0; x < XCLIP_N; x++) {
585 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000586 r.fLeft = x * cw;
587 r.fRight = (x + 1) * cw;
588 if (x == XCLIP_N - 1) {
589 r.fRight = w;
590 }
reed@android.come522ca52009-11-23 20:10:41 +0000591 canvas->clipRect(r);
592 this->INHERITED::draw(canvas);
593 }
594 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000595
reed@android.com55e76b22009-11-23 21:46:47 +0000596 SkBitmap diff;
597 if (bitmap_diff(canvas, orig, &diff)) {
598 }
reed@android.come522ca52009-11-23 20:10:41 +0000599 } else {
600 this->INHERITED::draw(canvas);
601 }
Scroggo0f185c22011-03-24 18:35:50 +0000602 if (fShowZoomer) {
603 int count = canvas->save();
604 canvas->resetMatrix();
605 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000606 int width = SkScalarRound(this->width());
607 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000608 if (fMouseX >= width) fMouseX = width - 1;
609 else if (fMouseX < 0) fMouseX = 0;
610 if (fMouseY >= height) fMouseY = height - 1;
611 else if (fMouseY < 0) fMouseY = 0;
612 SkBitmap bitmap = capture_bitmap(canvas);
613 // 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 +0000614 int zoomedWidth = (width >> 1) | 1;
615 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000616 SkIRect src;
617 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
618 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
619 SkRect dest;
620 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
621 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
622 SkPaint paint;
623 // Clear the background behind our zoomed in view
624 paint.setColor(SK_ColorWHITE);
625 canvas->drawRect(dest, paint);
626 canvas->drawBitmapRect(bitmap, &src, dest);
627 paint.setColor(SK_ColorBLACK);
628 paint.setStyle(SkPaint::kStroke_Style);
629 // Draw a border around the pixel in the middle
630 SkRect originalPixel;
631 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
632 SkMatrix matrix;
633 SkRect scalarSrc;
634 scalarSrc.set(src);
635 SkColor color = bitmap.getColor(fMouseX, fMouseY);
636 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
637 SkRect pixel;
638 matrix.mapRect(&pixel, originalPixel);
639 // TODO Perhaps measure the values and make the outline white if it's "dark"
640 if (color == SK_ColorBLACK) {
641 paint.setColor(SK_ColorWHITE);
642 }
643 canvas->drawRect(pixel, paint);
644 }
645 paint.setColor(SK_ColorBLACK);
646 // Draw a border around the destination rectangle
647 canvas->drawRect(dest, paint);
648 paint.setStyle(SkPaint::kStrokeAndFill_Style);
649 // Identify the pixel and its color on screen
650 paint.setTypeface(fTypeface);
651 paint.setAntiAlias(true);
652 SkScalar lineHeight = paint.getFontMetrics(NULL);
653 SkString string;
654 string.appendf("(%i, %i)", fMouseX, fMouseY);
655 SkScalar left = dest.fLeft + SkIntToScalar(3);
656 SkScalar i = SK_Scalar1;
657 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
658 // Alpha
659 i += SK_Scalar1;
660 string.reset();
661 string.appendf("A: %X", SkColorGetA(color));
662 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
663 // Red
664 i += SK_Scalar1;
665 string.reset();
666 string.appendf("R: %X", SkColorGetR(color));
667 paint.setColor(SK_ColorRED);
668 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
669 // Green
670 i += SK_Scalar1;
671 string.reset();
672 string.appendf("G: %X", SkColorGetG(color));
673 paint.setColor(SK_ColorGREEN);
674 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
675 // Blue
676 i += SK_Scalar1;
677 string.reset();
678 string.appendf("B: %X", SkColorGetB(color));
679 paint.setColor(SK_ColorBLUE);
680 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
681 canvas->restoreToCount(count);
682 }
reed@android.come522ca52009-11-23 20:10:41 +0000683}
684
reed@android.com8a1c16f2008-12-17 15:59:43 +0000685void SampleWindow::onDraw(SkCanvas* canvas) {
686 if (fRepeatDrawing) {
687 this->inval(NULL);
688 }
689}
690
691#include "SkColorPriv.h"
692
693static void reverseRedAndBlue(const SkBitmap& bm) {
694 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
695 uint8_t* p = (uint8_t*)bm.getPixels();
696 uint8_t* stop = p + bm.getSize();
697 while (p < stop) {
698 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
699 unsigned scale = SkAlpha255To256(p[3]);
700 unsigned r = p[2];
701 unsigned b = p[0];
702 p[0] = SkAlphaMul(r, scale);
703 p[1] = SkAlphaMul(p[1], scale);
704 p[2] = SkAlphaMul(b, scale);
705 p += 4;
706 }
707}
708
709SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000710 if (kGPU_CanvasType != fCanvasType) {
reed@android.com6efdc472008-12-19 18:24:35 +0000711#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000712 detachGL();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000713#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000714 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000715
reed@android.com8a1c16f2008-12-17 15:59:43 +0000716 switch (fCanvasType) {
717 case kRaster_CanvasType:
718 canvas = this->INHERITED::beforeChildren(canvas);
719 break;
720 case kPicture_CanvasType:
721 fPicture = new SkPicture;
722 canvas = fPicture->beginRecording(9999, 9999);
723 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000724 case kGPU_CanvasType: {
reed@google.com64e3eb22011-05-04 14:32:04 +0000725 if (make3DReady()) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000726 SkDevice* device = canvas->getDevice();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000727 const SkBitmap& bitmap = device->accessBitmap(true);
728
bsalomon@google.com5782d712011-01-21 21:03:59 +0000729 GrRenderTarget* renderTarget;
730 renderTarget = fGrContext->createRenderTargetFrom3DApiState();
731 fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
732 renderTarget->unref();
733
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000734 device = fGpuCanvas->createDevice(SkBitmap::kARGB_8888_Config,
735 bitmap.width(), bitmap.height(),
736 false, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000737 fGpuCanvas->setDevice(device)->unref();
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000738
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000739 fGpuCanvas->concat(canvas->getTotalMatrix());
reed@android.comf2b98d62010-12-20 18:26:13 +0000740 canvas = fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000741
reed@android.comf2b98d62010-12-20 18:26:13 +0000742 } else {
743 canvas = this->INHERITED::beforeChildren(canvas);
744 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000745 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000746 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000747 }
748
749 if (fUseClip) {
750 canvas->drawColor(0xFFFF88FF);
751 canvas->clipPath(fClipPath);
752 }
753
754 return canvas;
755}
756
757static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
758 const SkRegion& rgn) {
759 SkCanvas canvas(bm);
760 SkRegion inval(rgn);
761
762 inval.translate(r.fLeft, r.fTop);
763 canvas.clipRegion(inval);
764 canvas.drawColor(0xFFFF8080);
765}
766
767void SampleWindow::afterChildren(SkCanvas* orig) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000768 if (fRequestGrabImage) {
769 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000770
reed@android.comf2b98d62010-12-20 18:26:13 +0000771 SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
772 SkDevice* device = canvas->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000773 SkBitmap bmp;
774 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000775 static int gSampleGrabCounter;
776 SkString name;
777 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000778 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000779 SkImageEncoder::kPNG_Type, 100);
780 }
781 }
782
reed@android.com8a1c16f2008-12-17 15:59:43 +0000783 switch (fCanvasType) {
784 case kRaster_CanvasType:
785 break;
786 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000787 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000788 SkPicture* pict = new SkPicture(*fPicture);
789 fPicture->unref();
790 orig->drawPicture(*pict);
791 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000792 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000793 SkDynamicMemoryWStream ostream;
794 fPicture->serialize(&ostream);
795 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000796
reed@android.com8a1c16f2008-12-17 15:59:43 +0000797 SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
798 SkPicture pict(&istream);
799 orig->drawPicture(pict);
800 } else {
801 fPicture->draw(orig);
802 fPicture->unref();
803 }
804 fPicture = NULL;
805 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000806#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000807 case kGPU_CanvasType:
808 delete fGpuCanvas;
809 fGpuCanvas = NULL;
810 presentGL();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000811 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000812#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000813 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000814
reed@google.com17d7aec2011-04-25 14:31:44 +0000815 // Do this after presentGL and other finishing, rather than in afterChild
816 if (fMeasureFPS && fMeasureFPS_Time) {
817 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
818 this->updateTitle();
819 postInvalDelay(this->getSinkID());
820 }
821
822 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000823 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000824 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
825 int dx = fScrollTestX * 7;
826 int dy = fScrollTestY * 7;
827 SkIRect r;
828 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000829
reed@android.com8a1c16f2008-12-17 15:59:43 +0000830 r.set(50, 50, 50+100, 50+100);
831 bm.scrollRect(&r, dx, dy, &inval);
832 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000833 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000834}
835
reed@android.com6c5f6f22009-08-14 16:08:38 +0000836void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
837 if (fScale) {
838 SkScalar scale = SK_Scalar1 * 7 / 10;
839 SkScalar cx = this->width() / 2;
840 SkScalar cy = this->height() / 2;
841 canvas->translate(cx, cy);
842 canvas->scale(scale, scale);
843 canvas->translate(-cx, -cy);
844 }
845 if (fRotate) {
846 SkScalar cx = this->width() / 2;
847 SkScalar cy = this->height() / 2;
848 canvas->translate(cx, cy);
849 canvas->rotate(SkIntToScalar(30));
850 canvas->translate(-cx, -cy);
851 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000852
reed@google.com569e0432011-04-05 13:07:03 +0000853 if (kUnknown_SkTriState != fLCDState ||
854 kUnknown_SkTriState != fAAState) {
855 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState))->unref();
reed@google.comf0b5f682011-03-11 20:08:25 +0000856 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000857
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000858 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000859 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
860 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
861 fMeasureFPS_Time = SkTime::GetMSecs();
862 }
863 } else {
864 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000865 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000866 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000867}
868
869void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000870 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000871}
872
reed@android.com8a1c16f2008-12-17 15:59:43 +0000873static SkBitmap::Config gConfigCycle[] = {
874 SkBitmap::kNo_Config, // none -> none
875 SkBitmap::kNo_Config, // a1 -> none
876 SkBitmap::kNo_Config, // a8 -> none
877 SkBitmap::kNo_Config, // index8 -> none
878 SkBitmap::kARGB_4444_Config, // 565 -> 4444
879 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
880 SkBitmap::kRGB_565_Config // 8888 -> 565
881};
882
883static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
884 return gConfigCycle[c];
885}
886
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000887void SampleWindow::changeZoomLevel(int delta) {
888 fZoomLevel += delta;
889 if (fZoomLevel > 0) {
890 fZoomLevel = SkMin32(fZoomLevel, MAX_ZOOM_LEVEL);
891 fZoomScale = SkIntToScalar(fZoomLevel + 1);
892 } else if (fZoomLevel < 0) {
893 fZoomLevel = SkMax32(fZoomLevel, MIN_ZOOM_LEVEL);
894 fZoomScale = SK_Scalar1 / (1 - fZoomLevel);
895 } else {
896 fZoomScale = SK_Scalar1;
897 }
898
899 this->inval(NULL);
900}
901
reed@android.com8a1c16f2008-12-17 15:59:43 +0000902bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000903 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
904 this->loadView(fSamples[fCurrIndex]());
905 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000906}
907
908bool SampleWindow::onEvent(const SkEvent& evt) {
909 if (evt.isType(ANIMATING_EVENTTYPE)) {
910 if (fAnimating) {
911 this->nextSample();
912 this->postAnimatingEvent();
913 }
914 return true;
915 }
reed@android.com34245c72009-11-03 04:00:48 +0000916 if (evt.isType("set-curr-index")) {
917 fCurrIndex = evt.getFast32() % fSamples.count();
918 this->loadView(fSamples[fCurrIndex]());
919 return true;
920 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000921 if (isInvalEvent(evt)) {
922 this->inval(NULL);
923 return true;
924 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000925 return this->INHERITED::onEvent(evt);
926}
927
reed@android.comf2b98d62010-12-20 18:26:13 +0000928bool SampleWindow::onQuery(SkEvent* query) {
929 if (query->isType("get-slide-count")) {
930 query->setFast32(fSamples.count());
931 return true;
932 }
933 if (query->isType("get-slide-title")) {
934 SkView* view = fSamples[query->getFast32()]();
935 SkEvent evt(gTitleEvtName);
936 if (view->doQuery(&evt)) {
937 query->setString("title", evt.findString(gTitleEvtName));
938 }
939 SkSafeUnref(view);
940 return true;
941 }
942 if (query->isType("use-fast-text")) {
943 SkEvent evt(gFastTextEvtName);
944 return curr_view(this)->doQuery(&evt);
945 }
946 return this->INHERITED::onQuery(query);
947}
948
reed@android.com0ae6b242008-12-23 16:49:54 +0000949static void cleanup_for_filename(SkString* name) {
950 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +0000951 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +0000952 switch (str[i]) {
953 case ':': str[i] = '-'; break;
954 case '/': str[i] = '-'; break;
955 case ' ': str[i] = '_'; break;
956 default: break;
957 }
958 }
959}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000960
961bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000962 {
963 SkView* view = curr_view(this);
964 if (view) {
965 SkEvent evt(gCharEvtName);
966 evt.setFast32(uni);
967 if (view->doQuery(&evt)) {
968 return true;
969 }
970 }
971 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000972
reed@android.com8a1c16f2008-12-17 15:59:43 +0000973 int dx = 0xFF;
974 int dy = 0xFF;
975
976 switch (uni) {
977 case '5': dx = 0; dy = 0; break;
978 case '8': dx = 0; dy = -1; break;
979 case '6': dx = 1; dy = 0; break;
980 case '2': dx = 0; dy = 1; break;
981 case '4': dx = -1; dy = 0; break;
982 case '7': dx = -1; dy = -1; break;
983 case '9': dx = 1; dy = -1; break;
984 case '3': dx = 1; dy = 1; break;
985 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000986
reed@android.com8a1c16f2008-12-17 15:59:43 +0000987 default:
988 break;
989 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000990
reed@android.com8a1c16f2008-12-17 15:59:43 +0000991 if (0xFF != dx && 0xFF != dy) {
992 if ((dx | dy) == 0) {
993 fScrollTestX = fScrollTestY = 0;
994 } else {
995 fScrollTestX += dx;
996 fScrollTestY += dy;
997 }
998 this->inval(NULL);
999 return true;
1000 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001001
reed@android.com0ae6b242008-12-23 16:49:54 +00001002 switch (uni) {
1003 case 'a':
1004 fAnimating = !fAnimating;
1005 this->postAnimatingEvent();
1006 this->updateTitle();
1007 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001008 case 'b':
1009 fAAState = cycle_tristate(fAAState);
1010 this->updateTitle();
1011 this->inval(NULL);
1012 break;
1013 case 'c':
1014 fUseClip = !fUseClip;
1015 this->inval(NULL);
1016 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001017 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001018 case 'd':
1019 SkGraphics::SetFontCacheUsed(0);
1020 return true;
1021 case 'f':
1022 fMeasureFPS = !fMeasureFPS;
1023 this->inval(NULL);
1024 break;
1025 case 'g':
1026 fRequestGrabImage = true;
1027 this->inval(NULL);
1028 break;
1029 case 'i':
1030 this->zoomIn();
1031 break;
1032 case 'l':
1033 fLCDState = cycle_tristate(fLCDState);
1034 this->updateTitle();
1035 this->inval(NULL);
1036 break;
1037 case 'o':
1038 this->zoomOut();
1039 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001040 case 'p':
1041 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001042 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001043 this->inval(NULL);
1044 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001045 case 'r':
1046 fRotate = !fRotate;
1047 this->inval(NULL);
1048 this->updateTitle();
1049 return true;
1050 case 's':
1051 fScale = !fScale;
1052 this->inval(NULL);
1053 this->updateTitle();
1054 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001055 case 'x':
1056 fFlipAxis ^= kFlipAxis_X;
1057 this->updateTitle();
1058 this->inval(NULL);
1059 break;
1060 case 'y':
1061 fFlipAxis ^= kFlipAxis_Y;
1062 this->updateTitle();
1063 this->inval(NULL);
1064 break;
scroggo08526c02011-03-22 14:03:21 +00001065 case 'z':
1066 this->toggleZoomer();
1067 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001068 default:
1069 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001070 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001071
reed@android.com8a1c16f2008-12-17 15:59:43 +00001072 return this->INHERITED::onHandleChar(uni);
1073}
1074
1075#include "SkDumpCanvas.h"
1076
1077bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001078 {
1079 SkView* view = curr_view(this);
1080 if (view) {
1081 SkEvent evt(gKeyEvtName);
1082 evt.setFast32(key);
1083 if (view->doQuery(&evt)) {
1084 return true;
1085 }
1086 }
1087 }
1088
reed@android.com8a1c16f2008-12-17 15:59:43 +00001089 switch (key) {
1090 case kRight_SkKey:
1091 if (this->nextSample()) {
1092 return true;
1093 }
1094 break;
1095 case kLeft_SkKey:
1096 fCanvasType = cycle_canvastype(fCanvasType);
1097 this->updateTitle();
1098 this->inval(NULL);
1099 return true;
1100 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001101 if (USE_ARROWS_FOR_ZOOM) {
1102 this->changeZoomLevel(1);
1103 } else {
1104 fNClip = !fNClip;
1105 this->inval(NULL);
1106 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001107 this->updateTitle();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001108 return true;
1109 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001110 if (USE_ARROWS_FOR_ZOOM) {
1111 this->changeZoomLevel(-1);
1112 } else {
1113 this->setConfig(cycle_configs(this->getBitmap().config()));
1114 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001115 this->updateTitle();
1116 return true;
1117 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001118 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001119 SkDebugfDumper dumper;
1120 SkDumpCanvas dc(&dumper);
1121 this->draw(&dc);
1122 } else {
1123 fRepeatDrawing = !fRepeatDrawing;
1124 if (fRepeatDrawing) {
1125 this->inval(NULL);
1126 }
1127 }
1128 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001129 case kBack_SkKey:
1130 this->loadView(NULL);
1131 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001132 default:
1133 break;
1134 }
1135 return this->INHERITED::onHandleKey(key);
1136}
1137
reed@google.com52f57e12011-03-16 12:10:02 +00001138///////////////////////////////////////////////////////////////////////////////
1139
1140static const char gGestureClickType[] = "GestureClickType";
1141
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001142bool SampleWindow::onDispatchClick(int x, int y, Click::State state) {
Scroggo0f185c22011-03-24 18:35:50 +00001143 if (Click::kMoved_State == state) {
1144 updatePointer(x, y);
1145 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001146 int w = SkScalarRound(this->width());
1147 int h = SkScalarRound(this->height());
1148
1149 // check for the resize-box
1150 if (w - x < 16 && h - y < 16) {
1151 return false; // let the OS handle the click
1152 } else {
1153 return this->INHERITED::onDispatchClick(x, y, state);
1154 }
1155}
1156
reed@google.com52f57e12011-03-16 12:10:02 +00001157class GestureClick : public SkView::Click {
1158public:
1159 GestureClick(SkView* target) : SkView::Click(target) {
1160 this->setType(gGestureClickType);
1161 }
1162
1163 static bool IsGesture(Click* click) {
1164 return click->isType(gGestureClickType);
1165 }
1166};
1167
1168SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1169 return new GestureClick(this);
1170}
1171
1172bool SampleWindow::onClick(Click* click) {
1173 if (GestureClick::IsGesture(click)) {
1174 float x = SkScalarToFloat(click->fCurr.fX);
1175 float y = SkScalarToFloat(click->fCurr.fY);
1176 switch (click->fState) {
1177 case SkView::Click::kDown_State:
1178 fGesture.touchBegin(click, x, y);
1179 break;
1180 case SkView::Click::kMoved_State:
1181 fGesture.touchMoved(click, x, y);
1182 this->inval(NULL);
1183 break;
1184 case SkView::Click::kUp_State:
1185 fGesture.touchEnd(click);
1186 this->inval(NULL);
1187 break;
1188 }
1189 return true;
1190 }
1191 return false;
1192}
1193
1194///////////////////////////////////////////////////////////////////////////////
1195
reed@android.com8a1c16f2008-12-17 15:59:43 +00001196void SampleWindow::loadView(SkView* view) {
1197 SkView::F2BIter iter(this);
1198 SkView* prev = iter.next();
1199 if (prev) {
1200 prev->detachFromParent();
1201 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001202
reed@android.com34245c72009-11-03 04:00:48 +00001203 if (NULL == view) {
1204 view = create_overview(fSamples.count(), fSamples.begin());
1205 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001206 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001207 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001208 this->attachChildToFront(view)->unref();
1209 view->setSize(this->width(), this->height());
1210
1211 this->updateTitle();
1212}
1213
1214static const char* gConfigNames[] = {
1215 "unknown config",
1216 "A1",
1217 "A8",
1218 "Index8",
1219 "565",
1220 "4444",
1221 "8888"
1222};
1223
1224static const char* configToString(SkBitmap::Config c) {
1225 return gConfigNames[c];
1226}
1227
1228static const char* gCanvasTypePrefix[] = {
1229 "raster: ",
1230 "picture: ",
1231 "opengl: "
1232};
1233
reed@google.com569e0432011-04-05 13:07:03 +00001234static const char* trystate_str(SkTriState state,
1235 const char trueStr[], const char falseStr[]) {
1236 if (kTrue_SkTriState == state) {
1237 return trueStr;
1238 } else if (kFalse_SkTriState == state) {
1239 return falseStr;
1240 }
1241 return NULL;
1242}
1243
reed@android.com8a1c16f2008-12-17 15:59:43 +00001244void SampleWindow::updateTitle() {
1245 SkString title;
1246
1247 SkView::F2BIter iter(this);
1248 SkView* view = iter.next();
1249 SkEvent evt(gTitleEvtName);
1250 if (view->doQuery(&evt)) {
1251 title.set(evt.findString(gTitleEvtName));
1252 }
1253 if (title.size() == 0) {
1254 title.set("<unknown>");
1255 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001256
reed@android.com8a1c16f2008-12-17 15:59:43 +00001257 title.prepend(gCanvasTypePrefix[fCanvasType]);
1258
1259 title.prepend(" ");
1260 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001261
reed@android.com8a1c16f2008-12-17 15:59:43 +00001262 if (fAnimating) {
1263 title.prepend("<A> ");
1264 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001265 if (fScale) {
1266 title.prepend("<S> ");
1267 }
1268 if (fRotate) {
1269 title.prepend("<R> ");
1270 }
reed@android.come522ca52009-11-23 20:10:41 +00001271 if (fNClip) {
1272 title.prepend("<C> ");
1273 }
reed@google.com569e0432011-04-05 13:07:03 +00001274
1275 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1276 title.prepend(trystate_str(fAAState, "AA ", "aa "));
1277 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1278 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001279
1280 if (fZoomLevel) {
1281 title.prependf("{%d} ", fZoomLevel);
1282 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001283
1284 if (fMeasureFPS) {
1285 title.appendf(" %4d ms", fMeasureFPS_Time);
1286 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001287 if (fUsePipe && SampleView::IsSampleView(view)) {
1288 title.prepend("<P> ");
1289 }
1290 if (SampleView::IsSampleView(view)) {
1291 title.prepend("! ");
1292 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001293
reed@android.com8a1c16f2008-12-17 15:59:43 +00001294 this->setTitle(title.c_str());
1295}
1296
1297void SampleWindow::onSizeChange() {
1298 this->INHERITED::onSizeChange();
1299
1300 SkView::F2BIter iter(this);
1301 SkView* view = iter.next();
1302 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001303
reed@android.com8a1c16f2008-12-17 15:59:43 +00001304 // rebuild our clippath
1305 {
1306 const SkScalar W = this->width();
1307 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001308
reed@android.com8a1c16f2008-12-17 15:59:43 +00001309 fClipPath.reset();
1310#if 0
1311 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1312 SkRect r;
1313 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1314 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1315 fClipPath.addRect(r);
1316 }
1317#else
1318 SkRect r;
1319 r.set(0, 0, W, H);
1320 fClipPath.addRect(r, SkPath::kCCW_Direction);
1321 r.set(W/4, H/4, W*3/4, H*3/4);
1322 fClipPath.addRect(r, SkPath::kCW_Direction);
1323#endif
1324 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001325
reed@android.com8a1c16f2008-12-17 15:59:43 +00001326 this->updateTitle(); // to refresh our config
1327}
1328
1329///////////////////////////////////////////////////////////////////////////////
1330
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001331static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001332static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001333static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001334
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001335bool SampleView::IsSampleView(SkView* view) {
1336 SkEvent evt(is_sample_view_tag);
1337 return view->doQuery(&evt);
1338}
1339
reed@google.comf2183392011-04-22 14:10:48 +00001340bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001341 SkEvent evt(repeat_count_tag);
1342 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001343 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001344}
1345
reed@google.com0faac1e2011-05-11 05:58:58 +00001346bool SampleView::SetUsePipe(SkView* view, bool pred) {
1347 SkEvent evt(set_use_pipe_tag);
1348 evt.setFast32(pred);
1349 return view->doEvent(evt);
1350}
1351
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001352bool SampleView::onEvent(const SkEvent& evt) {
1353 if (evt.isType(repeat_count_tag)) {
1354 fRepeatCount = evt.getFast32();
1355 return true;
1356 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001357 if (evt.isType(set_use_pipe_tag)) {
1358 fUsePipe = !!evt.getFast32();
1359 return true;
1360 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001361 return this->INHERITED::onEvent(evt);
1362}
1363
1364bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001365 if (evt->isType(is_sample_view_tag)) {
1366 return true;
1367 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001368 return this->INHERITED::onQuery(evt);
1369}
1370
reed@google.com4aebe4f2011-05-05 14:07:35 +00001371#define TEST_GPIPEx
reed@google.com64e3eb22011-05-04 14:32:04 +00001372
reed@google.com68f456d2011-05-02 18:55:39 +00001373#ifdef TEST_GPIPE
1374 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001375
1376class SimplePC : public SkGPipeController {
1377public:
1378 SimplePC(SkCanvas* target);
1379 ~SimplePC();
1380
1381 virtual void* requestBlock(size_t minRequest, size_t* actual);
1382 virtual void notifyWritten(size_t bytes);
1383
1384private:
reed@google.com961ddb02011-05-05 14:03:48 +00001385 SkGPipeReader fReader;
1386 void* fBlock;
1387 size_t fBlockSize;
1388 size_t fBytesWritten;
1389 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001390 SkGPipeReader::Status fStatus;
1391
1392 size_t fTotalWritten;
1393};
1394
1395SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1396 fBlock = NULL;
1397 fBlockSize = fBytesWritten = 0;
1398 fStatus = SkGPipeReader::kDone_Status;
1399 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001400 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001401}
1402
1403SimplePC::~SimplePC() {
1404// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1405 sk_free(fBlock);
1406
reed@google.com0faac1e2011-05-11 05:58:58 +00001407 if (fTotalWritten) {
1408 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1409 fAtomsWritten, fStatus);
1410 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001411}
1412
1413void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1414 sk_free(fBlock);
1415
1416 fBlockSize = minRequest * 4;
1417 fBlock = sk_malloc_throw(fBlockSize);
1418 fBytesWritten = 0;
1419 *actual = fBlockSize;
1420 return fBlock;
1421}
1422
1423void SimplePC::notifyWritten(size_t bytes) {
1424 SkASSERT(fBytesWritten + bytes <= fBlockSize);
1425
1426 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1427 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1428 fBytesWritten += bytes;
1429 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001430
1431 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001432}
1433
reed@google.com68f456d2011-05-02 18:55:39 +00001434#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001435
reed@google.com64e3eb22011-05-04 14:32:04 +00001436
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001437void SampleView::onDraw(SkCanvas* canvas) {
1438 this->onDrawBackground(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001439
1440#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001441 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001442 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001443 if (fUsePipe) {
1444 canvas = writer.startRecording(&controller);
1445 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001446#endif
1447
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001448 for (int i = 0; i < fRepeatCount; i++) {
1449 SkAutoCanvasRestore acr(canvas, true);
1450 this->onDrawContent(canvas);
1451 }
1452}
1453
1454void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001455 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001456}
1457
1458///////////////////////////////////////////////////////////////////////////////
1459
reed@android.comf2b98d62010-12-20 18:26:13 +00001460template <typename T> void SkTBSort(T array[], int count) {
1461 for (int i = 1; i < count - 1; i++) {
1462 bool didSwap = false;
1463 for (int j = count - 1; j > i; --j) {
1464 if (array[j] < array[j-1]) {
1465 T tmp(array[j-1]);
1466 array[j-1] = array[j];
1467 array[j] = tmp;
1468 didSwap = true;
1469 }
1470 }
1471 if (!didSwap) {
1472 break;
1473 }
1474 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001475
reed@android.comf2b98d62010-12-20 18:26:13 +00001476 for (int k = 0; k < count - 1; k++) {
1477 SkASSERT(!(array[k+1] < array[k]));
1478 }
1479}
1480
1481#include "SkRandom.h"
1482
1483static void rand_rect(SkIRect* rect, SkRandom& rand) {
1484 int bits = 8;
1485 int shift = 32 - bits;
1486 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1487 rand.nextU() >> shift, rand.nextU() >> shift);
1488 rect->sort();
1489}
1490
1491static void dumpRect(const SkIRect& r) {
1492 SkDebugf(" { %d, %d, %d, %d },\n",
1493 r.fLeft, r.fTop,
1494 r.fRight, r.fBottom);
1495}
1496
1497static void test_rects(const SkIRect rect[], int count) {
1498 SkRegion rgn0, rgn1;
1499
1500 for (int i = 0; i < count; i++) {
1501 rgn0.op(rect[i], SkRegion::kUnion_Op);
1502 // dumpRect(rect[i]);
1503 }
1504 rgn1.setRects(rect, count);
1505
1506 if (rgn0 != rgn1) {
1507 SkDebugf("\n");
1508 for (int i = 0; i < count; i++) {
1509 dumpRect(rect[i]);
1510 }
1511 SkDebugf("\n");
1512 }
1513}
1514
1515static void test() {
1516 size_t i;
1517
1518 const SkIRect r0[] = {
1519 { 0, 0, 1, 1 },
1520 { 2, 2, 3, 3 },
1521 };
1522 const SkIRect r1[] = {
1523 { 0, 0, 1, 3 },
1524 { 1, 1, 2, 2 },
1525 { 2, 0, 3, 3 },
1526 };
1527 const SkIRect r2[] = {
1528 { 0, 0, 1, 2 },
1529 { 2, 1, 3, 3 },
1530 { 4, 0, 5, 1 },
1531 { 6, 0, 7, 4 },
1532 };
1533
1534 static const struct {
1535 const SkIRect* fRects;
1536 int fCount;
1537 } gRecs[] = {
1538 { r0, SK_ARRAY_COUNT(r0) },
1539 { r1, SK_ARRAY_COUNT(r1) },
1540 { r2, SK_ARRAY_COUNT(r2) },
1541 };
1542
1543 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1544 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1545 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001546
reed@android.comf2b98d62010-12-20 18:26:13 +00001547 SkRandom rand;
1548 for (i = 0; i < 10000; i++) {
1549 SkRegion rgn0, rgn1;
1550
1551 const int N = 8;
1552 SkIRect rect[N];
1553 for (int j = 0; j < N; j++) {
1554 rand_rect(&rect[j], rand);
1555 }
1556 test_rects(rect, N);
1557 }
1558}
1559
reed@android.com8a1c16f2008-12-17 15:59:43 +00001560SkOSWindow* create_sk_window(void* hwnd) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001561// test();
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001562 return new SampleWindow(hwnd);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001563}
1564
1565void get_preferred_size(int* x, int* y, int* width, int* height) {
1566 *x = 10;
1567 *y = 50;
1568 *width = 640;
1569 *height = 480;
1570}
1571
1572void application_init() {
1573// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001574#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001575 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001576#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001577 SkGraphics::Init();
1578 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001579}
1580
1581void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001582 SkEvent::Term();
1583 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001584}