blob: 80a702bac27add823ec4399a1a5e04acddd8e2eb [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;
reed@google.com66f22fd2011-05-17 15:33:45 +0000437 fFilterState = kUnknown_SkTriState;
reed@google.com569e0432011-04-05 13:07:03 +0000438 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000439 fScrollTestX = fScrollTestY = 0;
440
Scroggo0f185c22011-03-24 18:35:50 +0000441 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000442 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000443 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
444 fShowZoomer = false;
445
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000446 fZoomLevel = 0;
447 fZoomScale = SK_Scalar1;
448
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000449// this->setConfig(SkBitmap::kRGB_565_Config);
450 this->setConfig(SkBitmap::kARGB_8888_Config);
451 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000452 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000453
reed@android.com34245c72009-11-03 04:00:48 +0000454 {
455 const SkViewRegister* reg = SkViewRegister::Head();
456 while (reg) {
457 *fSamples.append() = reg->factory();
458 reg = reg->next();
459 }
460 }
461 fCurrIndex = 0;
reed@android.come0f13ee2009-11-04 19:40:25 +0000462 this->loadView(fSamples[fCurrIndex]());
reed@google.comf0b5f682011-03-11 20:08:25 +0000463
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000464#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +0000465 testpdf();
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000466#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000467}
468
469SampleWindow::~SampleWindow() {
470 delete fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000471 delete fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000472 if (NULL != fGrContext) {
473 fGrContext->unref();
474 }
Scroggo0f185c22011-03-24 18:35:50 +0000475 fTypeface->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000476}
477
reed@android.com55e76b22009-11-23 21:46:47 +0000478static SkBitmap capture_bitmap(SkCanvas* canvas) {
479 SkBitmap bm;
480 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
481 src.copyTo(&bm, src.config());
482 return bm;
483}
484
485static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
486 SkBitmap* diff) {
487 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000488
reed@android.com55e76b22009-11-23 21:46:47 +0000489 SkAutoLockPixels alp0(src);
490 SkAutoLockPixels alp1(orig);
491 for (int y = 0; y < src.height(); y++) {
492 const void* srcP = src.getAddr(0, y);
493 const void* origP = orig.getAddr(0, y);
494 size_t bytes = src.width() * src.bytesPerPixel();
495 if (memcmp(srcP, origP, bytes)) {
496 SkDebugf("---------- difference on line %d\n", y);
497 return true;
498 }
499 }
500 return false;
501}
502
Scroggo0f185c22011-03-24 18:35:50 +0000503static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
504{
505 SkColor desiredColor = paint.getColor();
506 paint.setColor(SK_ColorWHITE);
507 const char* c_str = string.c_str();
508 size_t size = string.size();
509 SkRect bounds;
510 paint.measureText(c_str, size, &bounds);
511 bounds.offset(left, top);
512 SkScalar inset = SkIntToScalar(-2);
513 bounds.inset(inset, inset);
514 canvas->drawRect(bounds, paint);
515 if (desiredColor != SK_ColorBLACK) {
516 paint.setColor(SK_ColorBLACK);
517 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
518 }
519 paint.setColor(desiredColor);
520 canvas->drawText(c_str, size, left, top, paint);
521}
522
reed@android.com44177402009-11-23 21:07:51 +0000523#define XCLIP_N 8
524#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000525
526void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000527 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000528 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000529 gAnimTime = SkTime::GetMSecs();
530
reed@google.com569e0432011-04-05 13:07:03 +0000531 SkScalar cx = SkScalarHalf(this->width());
532 SkScalar cy = SkScalarHalf(this->height());
533
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000534 if (fZoomLevel) {
535 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000536 SkPoint center;
537 m = canvas->getTotalMatrix();//.invert(&m);
538 m.mapXY(cx, cy, &center);
539 cx = center.fX;
540 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000541
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000542 m.setTranslate(-cx, -cy);
543 m.postScale(fZoomScale, fZoomScale);
544 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000545
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000546 canvas->concat(m);
547 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000548
reed@google.com569e0432011-04-05 13:07:03 +0000549 if (fFlipAxis) {
550 SkMatrix m;
551 m.setTranslate(cx, cy);
552 if (fFlipAxis & kFlipAxis_X) {
553 m.preScale(-SK_Scalar1, SK_Scalar1);
554 }
555 if (fFlipAxis & kFlipAxis_Y) {
556 m.preScale(SK_Scalar1, -SK_Scalar1);
557 }
558 m.preTranslate(-cx, -cy);
559 canvas->concat(m);
560 }
561
reed@google.com52f57e12011-03-16 12:10:02 +0000562 // Apply any gesture matrix
563 if (true) {
564 const SkMatrix& localM = fGesture.localM();
565 if (localM.getType() & SkMatrix::kScale_Mask) {
566 canvas->setExternalMatrix(&localM);
567 }
568 canvas->concat(localM);
569 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000570
reed@google.com52f57e12011-03-16 12:10:02 +0000571 if (fGesture.isActive()) {
572 this->inval(NULL);
573 }
574 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000575
reed@android.come522ca52009-11-23 20:10:41 +0000576 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000577 this->INHERITED::draw(canvas);
578 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000579
580 const SkScalar w = this->width();
581 const SkScalar h = this->height();
582 const SkScalar cw = w / XCLIP_N;
583 const SkScalar ch = h / YCLIP_N;
584 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000585 SkRect r;
586 r.fTop = y * ch;
587 r.fBottom = (y + 1) * ch;
588 if (y == YCLIP_N - 1) {
589 r.fBottom = h;
590 }
reed@android.come522ca52009-11-23 20:10:41 +0000591 for (int x = 0; x < XCLIP_N; x++) {
592 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000593 r.fLeft = x * cw;
594 r.fRight = (x + 1) * cw;
595 if (x == XCLIP_N - 1) {
596 r.fRight = w;
597 }
reed@android.come522ca52009-11-23 20:10:41 +0000598 canvas->clipRect(r);
599 this->INHERITED::draw(canvas);
600 }
601 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000602
reed@android.com55e76b22009-11-23 21:46:47 +0000603 SkBitmap diff;
604 if (bitmap_diff(canvas, orig, &diff)) {
605 }
reed@android.come522ca52009-11-23 20:10:41 +0000606 } else {
607 this->INHERITED::draw(canvas);
608 }
Scroggo0f185c22011-03-24 18:35:50 +0000609 if (fShowZoomer) {
610 int count = canvas->save();
611 canvas->resetMatrix();
612 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000613 int width = SkScalarRound(this->width());
614 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000615 if (fMouseX >= width) fMouseX = width - 1;
616 else if (fMouseX < 0) fMouseX = 0;
617 if (fMouseY >= height) fMouseY = height - 1;
618 else if (fMouseY < 0) fMouseY = 0;
619 SkBitmap bitmap = capture_bitmap(canvas);
620 // 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 +0000621 int zoomedWidth = (width >> 1) | 1;
622 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000623 SkIRect src;
624 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
625 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
626 SkRect dest;
627 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
628 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
629 SkPaint paint;
630 // Clear the background behind our zoomed in view
631 paint.setColor(SK_ColorWHITE);
632 canvas->drawRect(dest, paint);
633 canvas->drawBitmapRect(bitmap, &src, dest);
634 paint.setColor(SK_ColorBLACK);
635 paint.setStyle(SkPaint::kStroke_Style);
636 // Draw a border around the pixel in the middle
637 SkRect originalPixel;
638 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
639 SkMatrix matrix;
640 SkRect scalarSrc;
641 scalarSrc.set(src);
642 SkColor color = bitmap.getColor(fMouseX, fMouseY);
643 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
644 SkRect pixel;
645 matrix.mapRect(&pixel, originalPixel);
646 // TODO Perhaps measure the values and make the outline white if it's "dark"
647 if (color == SK_ColorBLACK) {
648 paint.setColor(SK_ColorWHITE);
649 }
650 canvas->drawRect(pixel, paint);
651 }
652 paint.setColor(SK_ColorBLACK);
653 // Draw a border around the destination rectangle
654 canvas->drawRect(dest, paint);
655 paint.setStyle(SkPaint::kStrokeAndFill_Style);
656 // Identify the pixel and its color on screen
657 paint.setTypeface(fTypeface);
658 paint.setAntiAlias(true);
659 SkScalar lineHeight = paint.getFontMetrics(NULL);
660 SkString string;
661 string.appendf("(%i, %i)", fMouseX, fMouseY);
662 SkScalar left = dest.fLeft + SkIntToScalar(3);
663 SkScalar i = SK_Scalar1;
664 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
665 // Alpha
666 i += SK_Scalar1;
667 string.reset();
668 string.appendf("A: %X", SkColorGetA(color));
669 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
670 // Red
671 i += SK_Scalar1;
672 string.reset();
673 string.appendf("R: %X", SkColorGetR(color));
674 paint.setColor(SK_ColorRED);
675 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
676 // Green
677 i += SK_Scalar1;
678 string.reset();
679 string.appendf("G: %X", SkColorGetG(color));
680 paint.setColor(SK_ColorGREEN);
681 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
682 // Blue
683 i += SK_Scalar1;
684 string.reset();
685 string.appendf("B: %X", SkColorGetB(color));
686 paint.setColor(SK_ColorBLUE);
687 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
688 canvas->restoreToCount(count);
689 }
reed@android.come522ca52009-11-23 20:10:41 +0000690}
691
reed@android.com8a1c16f2008-12-17 15:59:43 +0000692void SampleWindow::onDraw(SkCanvas* canvas) {
693 if (fRepeatDrawing) {
694 this->inval(NULL);
695 }
696}
697
698#include "SkColorPriv.h"
699
700static void reverseRedAndBlue(const SkBitmap& bm) {
701 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
702 uint8_t* p = (uint8_t*)bm.getPixels();
703 uint8_t* stop = p + bm.getSize();
704 while (p < stop) {
705 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
706 unsigned scale = SkAlpha255To256(p[3]);
707 unsigned r = p[2];
708 unsigned b = p[0];
709 p[0] = SkAlphaMul(r, scale);
710 p[1] = SkAlphaMul(p[1], scale);
711 p[2] = SkAlphaMul(b, scale);
712 p += 4;
713 }
714}
715
716SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000717 if (kGPU_CanvasType != fCanvasType) {
reed@android.com6efdc472008-12-19 18:24:35 +0000718#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000719 detachGL();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000720#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000721 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000722
reed@android.com8a1c16f2008-12-17 15:59:43 +0000723 switch (fCanvasType) {
724 case kRaster_CanvasType:
725 canvas = this->INHERITED::beforeChildren(canvas);
726 break;
727 case kPicture_CanvasType:
728 fPicture = new SkPicture;
729 canvas = fPicture->beginRecording(9999, 9999);
730 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000731 case kGPU_CanvasType: {
reed@google.com64e3eb22011-05-04 14:32:04 +0000732 if (make3DReady()) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000733 SkDevice* device = canvas->getDevice();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000734 const SkBitmap& bitmap = device->accessBitmap(true);
735
bsalomon@google.com5782d712011-01-21 21:03:59 +0000736 GrRenderTarget* renderTarget;
737 renderTarget = fGrContext->createRenderTargetFrom3DApiState();
738 fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
739 renderTarget->unref();
740
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000741 device = fGpuCanvas->createDevice(SkBitmap::kARGB_8888_Config,
742 bitmap.width(), bitmap.height(),
743 false, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000744 fGpuCanvas->setDevice(device)->unref();
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000745
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000746 fGpuCanvas->concat(canvas->getTotalMatrix());
reed@android.comf2b98d62010-12-20 18:26:13 +0000747 canvas = fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000748
reed@android.comf2b98d62010-12-20 18:26:13 +0000749 } else {
750 canvas = this->INHERITED::beforeChildren(canvas);
751 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000752 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000753 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000754 }
755
756 if (fUseClip) {
757 canvas->drawColor(0xFFFF88FF);
758 canvas->clipPath(fClipPath);
759 }
760
761 return canvas;
762}
763
764static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
765 const SkRegion& rgn) {
766 SkCanvas canvas(bm);
767 SkRegion inval(rgn);
768
769 inval.translate(r.fLeft, r.fTop);
770 canvas.clipRegion(inval);
771 canvas.drawColor(0xFFFF8080);
772}
773
774void SampleWindow::afterChildren(SkCanvas* orig) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000775 if (fRequestGrabImage) {
776 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000777
reed@android.comf2b98d62010-12-20 18:26:13 +0000778 SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
779 SkDevice* device = canvas->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000780 SkBitmap bmp;
781 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000782 static int gSampleGrabCounter;
783 SkString name;
784 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000785 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000786 SkImageEncoder::kPNG_Type, 100);
787 }
788 }
789
reed@android.com8a1c16f2008-12-17 15:59:43 +0000790 switch (fCanvasType) {
791 case kRaster_CanvasType:
792 break;
793 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000794 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000795 SkPicture* pict = new SkPicture(*fPicture);
796 fPicture->unref();
797 orig->drawPicture(*pict);
798 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000799 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000800 SkDynamicMemoryWStream ostream;
801 fPicture->serialize(&ostream);
802 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000803
reed@android.com8a1c16f2008-12-17 15:59:43 +0000804 SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
805 SkPicture pict(&istream);
806 orig->drawPicture(pict);
807 } else {
808 fPicture->draw(orig);
809 fPicture->unref();
810 }
811 fPicture = NULL;
812 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000813#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000814 case kGPU_CanvasType:
815 delete fGpuCanvas;
816 fGpuCanvas = NULL;
817 presentGL();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000818 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000819#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000820 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000821
reed@google.com17d7aec2011-04-25 14:31:44 +0000822 // Do this after presentGL and other finishing, rather than in afterChild
823 if (fMeasureFPS && fMeasureFPS_Time) {
824 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
825 this->updateTitle();
826 postInvalDelay(this->getSinkID());
827 }
828
829 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000830 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000831 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
832 int dx = fScrollTestX * 7;
833 int dy = fScrollTestY * 7;
834 SkIRect r;
835 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000836
reed@android.com8a1c16f2008-12-17 15:59:43 +0000837 r.set(50, 50, 50+100, 50+100);
838 bm.scrollRect(&r, dx, dy, &inval);
839 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000840 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000841}
842
reed@android.com6c5f6f22009-08-14 16:08:38 +0000843void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
844 if (fScale) {
845 SkScalar scale = SK_Scalar1 * 7 / 10;
846 SkScalar cx = this->width() / 2;
847 SkScalar cy = this->height() / 2;
848 canvas->translate(cx, cy);
849 canvas->scale(scale, scale);
850 canvas->translate(-cx, -cy);
851 }
852 if (fRotate) {
853 SkScalar cx = this->width() / 2;
854 SkScalar cy = this->height() / 2;
855 canvas->translate(cx, cy);
856 canvas->rotate(SkIntToScalar(30));
857 canvas->translate(-cx, -cy);
858 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000859
reed@google.com176753a2011-05-17 15:32:04 +0000860 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState, fFilterState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000861
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000862 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000863 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
864 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
865 fMeasureFPS_Time = SkTime::GetMSecs();
866 }
867 } else {
868 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000869 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000870 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000871}
872
873void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000874 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000875}
876
reed@android.com8a1c16f2008-12-17 15:59:43 +0000877static SkBitmap::Config gConfigCycle[] = {
878 SkBitmap::kNo_Config, // none -> none
879 SkBitmap::kNo_Config, // a1 -> none
880 SkBitmap::kNo_Config, // a8 -> none
881 SkBitmap::kNo_Config, // index8 -> none
882 SkBitmap::kARGB_4444_Config, // 565 -> 4444
883 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
884 SkBitmap::kRGB_565_Config // 8888 -> 565
885};
886
887static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
888 return gConfigCycle[c];
889}
890
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000891void SampleWindow::changeZoomLevel(int delta) {
892 fZoomLevel += delta;
893 if (fZoomLevel > 0) {
894 fZoomLevel = SkMin32(fZoomLevel, MAX_ZOOM_LEVEL);
895 fZoomScale = SkIntToScalar(fZoomLevel + 1);
896 } else if (fZoomLevel < 0) {
897 fZoomLevel = SkMax32(fZoomLevel, MIN_ZOOM_LEVEL);
898 fZoomScale = SK_Scalar1 / (1 - fZoomLevel);
899 } else {
900 fZoomScale = SK_Scalar1;
901 }
902
903 this->inval(NULL);
904}
905
reed@android.com8a1c16f2008-12-17 15:59:43 +0000906bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000907 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
908 this->loadView(fSamples[fCurrIndex]());
909 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000910}
911
912bool SampleWindow::onEvent(const SkEvent& evt) {
913 if (evt.isType(ANIMATING_EVENTTYPE)) {
914 if (fAnimating) {
915 this->nextSample();
916 this->postAnimatingEvent();
917 }
918 return true;
919 }
reed@android.com34245c72009-11-03 04:00:48 +0000920 if (evt.isType("set-curr-index")) {
921 fCurrIndex = evt.getFast32() % fSamples.count();
922 this->loadView(fSamples[fCurrIndex]());
923 return true;
924 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000925 if (isInvalEvent(evt)) {
926 this->inval(NULL);
927 return true;
928 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000929 return this->INHERITED::onEvent(evt);
930}
931
reed@android.comf2b98d62010-12-20 18:26:13 +0000932bool SampleWindow::onQuery(SkEvent* query) {
933 if (query->isType("get-slide-count")) {
934 query->setFast32(fSamples.count());
935 return true;
936 }
937 if (query->isType("get-slide-title")) {
938 SkView* view = fSamples[query->getFast32()]();
939 SkEvent evt(gTitleEvtName);
940 if (view->doQuery(&evt)) {
941 query->setString("title", evt.findString(gTitleEvtName));
942 }
943 SkSafeUnref(view);
944 return true;
945 }
946 if (query->isType("use-fast-text")) {
947 SkEvent evt(gFastTextEvtName);
948 return curr_view(this)->doQuery(&evt);
949 }
950 return this->INHERITED::onQuery(query);
951}
952
reed@android.com0ae6b242008-12-23 16:49:54 +0000953static void cleanup_for_filename(SkString* name) {
954 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +0000955 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +0000956 switch (str[i]) {
957 case ':': str[i] = '-'; break;
958 case '/': str[i] = '-'; break;
959 case ' ': str[i] = '_'; break;
960 default: break;
961 }
962 }
963}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000964
965bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000966 {
967 SkView* view = curr_view(this);
968 if (view) {
969 SkEvent evt(gCharEvtName);
970 evt.setFast32(uni);
971 if (view->doQuery(&evt)) {
972 return true;
973 }
974 }
975 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000976
reed@android.com8a1c16f2008-12-17 15:59:43 +0000977 int dx = 0xFF;
978 int dy = 0xFF;
979
980 switch (uni) {
981 case '5': dx = 0; dy = 0; break;
982 case '8': dx = 0; dy = -1; break;
983 case '6': dx = 1; dy = 0; break;
984 case '2': dx = 0; dy = 1; break;
985 case '4': dx = -1; dy = 0; break;
986 case '7': dx = -1; dy = -1; break;
987 case '9': dx = 1; dy = -1; break;
988 case '3': dx = 1; dy = 1; break;
989 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000990
reed@android.com8a1c16f2008-12-17 15:59:43 +0000991 default:
992 break;
993 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000994
reed@android.com8a1c16f2008-12-17 15:59:43 +0000995 if (0xFF != dx && 0xFF != dy) {
996 if ((dx | dy) == 0) {
997 fScrollTestX = fScrollTestY = 0;
998 } else {
999 fScrollTestX += dx;
1000 fScrollTestY += dy;
1001 }
1002 this->inval(NULL);
1003 return true;
1004 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001005
reed@android.com0ae6b242008-12-23 16:49:54 +00001006 switch (uni) {
1007 case 'a':
1008 fAnimating = !fAnimating;
1009 this->postAnimatingEvent();
1010 this->updateTitle();
1011 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001012 case 'b':
1013 fAAState = cycle_tristate(fAAState);
1014 this->updateTitle();
1015 this->inval(NULL);
1016 break;
1017 case 'c':
1018 fUseClip = !fUseClip;
1019 this->inval(NULL);
1020 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001021 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001022 case 'd':
1023 SkGraphics::SetFontCacheUsed(0);
1024 return true;
1025 case 'f':
1026 fMeasureFPS = !fMeasureFPS;
1027 this->inval(NULL);
1028 break;
1029 case 'g':
1030 fRequestGrabImage = true;
1031 this->inval(NULL);
1032 break;
1033 case 'i':
1034 this->zoomIn();
1035 break;
1036 case 'l':
1037 fLCDState = cycle_tristate(fLCDState);
1038 this->updateTitle();
1039 this->inval(NULL);
1040 break;
reed@google.com176753a2011-05-17 15:32:04 +00001041 case 'n':
1042 fFilterState = cycle_tristate(fFilterState);
1043 this->updateTitle();
1044 this->inval(NULL);
1045 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001046 case 'o':
1047 this->zoomOut();
1048 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001049 case 'p':
1050 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001051 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001052 this->inval(NULL);
1053 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001054 case 'r':
1055 fRotate = !fRotate;
1056 this->inval(NULL);
1057 this->updateTitle();
1058 return true;
1059 case 's':
1060 fScale = !fScale;
1061 this->inval(NULL);
1062 this->updateTitle();
1063 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001064 case 'x':
1065 fFlipAxis ^= kFlipAxis_X;
1066 this->updateTitle();
1067 this->inval(NULL);
1068 break;
1069 case 'y':
1070 fFlipAxis ^= kFlipAxis_Y;
1071 this->updateTitle();
1072 this->inval(NULL);
1073 break;
scroggo08526c02011-03-22 14:03:21 +00001074 case 'z':
1075 this->toggleZoomer();
1076 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001077 default:
1078 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001079 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001080
reed@android.com8a1c16f2008-12-17 15:59:43 +00001081 return this->INHERITED::onHandleChar(uni);
1082}
1083
1084#include "SkDumpCanvas.h"
1085
1086bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001087 {
1088 SkView* view = curr_view(this);
1089 if (view) {
1090 SkEvent evt(gKeyEvtName);
1091 evt.setFast32(key);
1092 if (view->doQuery(&evt)) {
1093 return true;
1094 }
1095 }
1096 }
1097
reed@android.com8a1c16f2008-12-17 15:59:43 +00001098 switch (key) {
1099 case kRight_SkKey:
1100 if (this->nextSample()) {
1101 return true;
1102 }
1103 break;
1104 case kLeft_SkKey:
1105 fCanvasType = cycle_canvastype(fCanvasType);
1106 this->updateTitle();
1107 this->inval(NULL);
1108 return true;
1109 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001110 if (USE_ARROWS_FOR_ZOOM) {
1111 this->changeZoomLevel(1);
1112 } else {
1113 fNClip = !fNClip;
1114 this->inval(NULL);
1115 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001116 this->updateTitle();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001117 return true;
1118 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001119 if (USE_ARROWS_FOR_ZOOM) {
1120 this->changeZoomLevel(-1);
1121 } else {
1122 this->setConfig(cycle_configs(this->getBitmap().config()));
1123 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001124 this->updateTitle();
1125 return true;
1126 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001127 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001128 SkDebugfDumper dumper;
1129 SkDumpCanvas dc(&dumper);
1130 this->draw(&dc);
1131 } else {
1132 fRepeatDrawing = !fRepeatDrawing;
1133 if (fRepeatDrawing) {
1134 this->inval(NULL);
1135 }
1136 }
1137 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001138 case kBack_SkKey:
1139 this->loadView(NULL);
1140 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001141 default:
1142 break;
1143 }
1144 return this->INHERITED::onHandleKey(key);
1145}
1146
reed@google.com52f57e12011-03-16 12:10:02 +00001147///////////////////////////////////////////////////////////////////////////////
1148
1149static const char gGestureClickType[] = "GestureClickType";
1150
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001151bool SampleWindow::onDispatchClick(int x, int y, Click::State state) {
Scroggo0f185c22011-03-24 18:35:50 +00001152 if (Click::kMoved_State == state) {
1153 updatePointer(x, y);
1154 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001155 int w = SkScalarRound(this->width());
1156 int h = SkScalarRound(this->height());
1157
1158 // check for the resize-box
1159 if (w - x < 16 && h - y < 16) {
1160 return false; // let the OS handle the click
1161 } else {
1162 return this->INHERITED::onDispatchClick(x, y, state);
1163 }
1164}
1165
reed@google.com52f57e12011-03-16 12:10:02 +00001166class GestureClick : public SkView::Click {
1167public:
1168 GestureClick(SkView* target) : SkView::Click(target) {
1169 this->setType(gGestureClickType);
1170 }
1171
1172 static bool IsGesture(Click* click) {
1173 return click->isType(gGestureClickType);
1174 }
1175};
1176
1177SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1178 return new GestureClick(this);
1179}
1180
1181bool SampleWindow::onClick(Click* click) {
1182 if (GestureClick::IsGesture(click)) {
1183 float x = SkScalarToFloat(click->fCurr.fX);
1184 float y = SkScalarToFloat(click->fCurr.fY);
1185 switch (click->fState) {
1186 case SkView::Click::kDown_State:
1187 fGesture.touchBegin(click, x, y);
1188 break;
1189 case SkView::Click::kMoved_State:
1190 fGesture.touchMoved(click, x, y);
1191 this->inval(NULL);
1192 break;
1193 case SkView::Click::kUp_State:
1194 fGesture.touchEnd(click);
1195 this->inval(NULL);
1196 break;
1197 }
1198 return true;
1199 }
1200 return false;
1201}
1202
1203///////////////////////////////////////////////////////////////////////////////
1204
reed@android.com8a1c16f2008-12-17 15:59:43 +00001205void SampleWindow::loadView(SkView* view) {
1206 SkView::F2BIter iter(this);
1207 SkView* prev = iter.next();
1208 if (prev) {
1209 prev->detachFromParent();
1210 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001211
reed@android.com34245c72009-11-03 04:00:48 +00001212 if (NULL == view) {
1213 view = create_overview(fSamples.count(), fSamples.begin());
1214 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001215 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001216 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001217 this->attachChildToFront(view)->unref();
1218 view->setSize(this->width(), this->height());
1219
1220 this->updateTitle();
1221}
1222
1223static const char* gConfigNames[] = {
1224 "unknown config",
1225 "A1",
1226 "A8",
1227 "Index8",
1228 "565",
1229 "4444",
1230 "8888"
1231};
1232
1233static const char* configToString(SkBitmap::Config c) {
1234 return gConfigNames[c];
1235}
1236
1237static const char* gCanvasTypePrefix[] = {
1238 "raster: ",
1239 "picture: ",
1240 "opengl: "
1241};
1242
reed@google.com569e0432011-04-05 13:07:03 +00001243static const char* trystate_str(SkTriState state,
1244 const char trueStr[], const char falseStr[]) {
1245 if (kTrue_SkTriState == state) {
1246 return trueStr;
1247 } else if (kFalse_SkTriState == state) {
1248 return falseStr;
1249 }
1250 return NULL;
1251}
1252
reed@android.com8a1c16f2008-12-17 15:59:43 +00001253void SampleWindow::updateTitle() {
1254 SkString title;
1255
1256 SkView::F2BIter iter(this);
1257 SkView* view = iter.next();
1258 SkEvent evt(gTitleEvtName);
1259 if (view->doQuery(&evt)) {
1260 title.set(evt.findString(gTitleEvtName));
1261 }
1262 if (title.size() == 0) {
1263 title.set("<unknown>");
1264 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001265
reed@android.com8a1c16f2008-12-17 15:59:43 +00001266 title.prepend(gCanvasTypePrefix[fCanvasType]);
1267
1268 title.prepend(" ");
1269 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001270
reed@android.com8a1c16f2008-12-17 15:59:43 +00001271 if (fAnimating) {
1272 title.prepend("<A> ");
1273 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001274 if (fScale) {
1275 title.prepend("<S> ");
1276 }
1277 if (fRotate) {
1278 title.prepend("<R> ");
1279 }
reed@android.come522ca52009-11-23 20:10:41 +00001280 if (fNClip) {
1281 title.prepend("<C> ");
1282 }
reed@google.com569e0432011-04-05 13:07:03 +00001283
1284 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1285 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com176753a2011-05-17 15:32:04 +00001286 title.prepend(trystate_str(fFilterState, "LERP ", "lerp "));
reed@google.com569e0432011-04-05 13:07:03 +00001287 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1288 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001289
1290 if (fZoomLevel) {
1291 title.prependf("{%d} ", fZoomLevel);
1292 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001293
1294 if (fMeasureFPS) {
1295 title.appendf(" %4d ms", fMeasureFPS_Time);
1296 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001297 if (fUsePipe && SampleView::IsSampleView(view)) {
1298 title.prepend("<P> ");
1299 }
1300 if (SampleView::IsSampleView(view)) {
1301 title.prepend("! ");
1302 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001303
reed@android.com8a1c16f2008-12-17 15:59:43 +00001304 this->setTitle(title.c_str());
1305}
1306
1307void SampleWindow::onSizeChange() {
1308 this->INHERITED::onSizeChange();
1309
1310 SkView::F2BIter iter(this);
1311 SkView* view = iter.next();
1312 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001313
reed@android.com8a1c16f2008-12-17 15:59:43 +00001314 // rebuild our clippath
1315 {
1316 const SkScalar W = this->width();
1317 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001318
reed@android.com8a1c16f2008-12-17 15:59:43 +00001319 fClipPath.reset();
1320#if 0
1321 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1322 SkRect r;
1323 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1324 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1325 fClipPath.addRect(r);
1326 }
1327#else
1328 SkRect r;
1329 r.set(0, 0, W, H);
1330 fClipPath.addRect(r, SkPath::kCCW_Direction);
1331 r.set(W/4, H/4, W*3/4, H*3/4);
1332 fClipPath.addRect(r, SkPath::kCW_Direction);
1333#endif
1334 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001335
reed@android.com8a1c16f2008-12-17 15:59:43 +00001336 this->updateTitle(); // to refresh our config
1337}
1338
1339///////////////////////////////////////////////////////////////////////////////
1340
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001341static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001342static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001343static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001344
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001345bool SampleView::IsSampleView(SkView* view) {
1346 SkEvent evt(is_sample_view_tag);
1347 return view->doQuery(&evt);
1348}
1349
reed@google.comf2183392011-04-22 14:10:48 +00001350bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001351 SkEvent evt(repeat_count_tag);
1352 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001353 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001354}
1355
reed@google.com0faac1e2011-05-11 05:58:58 +00001356bool SampleView::SetUsePipe(SkView* view, bool pred) {
1357 SkEvent evt(set_use_pipe_tag);
1358 evt.setFast32(pred);
1359 return view->doEvent(evt);
1360}
1361
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001362bool SampleView::onEvent(const SkEvent& evt) {
1363 if (evt.isType(repeat_count_tag)) {
1364 fRepeatCount = evt.getFast32();
1365 return true;
1366 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001367 if (evt.isType(set_use_pipe_tag)) {
1368 fUsePipe = !!evt.getFast32();
1369 return true;
1370 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001371 return this->INHERITED::onEvent(evt);
1372}
1373
1374bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001375 if (evt->isType(is_sample_view_tag)) {
1376 return true;
1377 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001378 return this->INHERITED::onQuery(evt);
1379}
1380
reed@google.com4aebe4f2011-05-05 14:07:35 +00001381#define TEST_GPIPEx
reed@google.com64e3eb22011-05-04 14:32:04 +00001382
reed@google.com68f456d2011-05-02 18:55:39 +00001383#ifdef TEST_GPIPE
1384 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001385
1386class SimplePC : public SkGPipeController {
1387public:
1388 SimplePC(SkCanvas* target);
1389 ~SimplePC();
1390
1391 virtual void* requestBlock(size_t minRequest, size_t* actual);
1392 virtual void notifyWritten(size_t bytes);
1393
1394private:
reed@google.com961ddb02011-05-05 14:03:48 +00001395 SkGPipeReader fReader;
1396 void* fBlock;
1397 size_t fBlockSize;
1398 size_t fBytesWritten;
1399 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001400 SkGPipeReader::Status fStatus;
1401
1402 size_t fTotalWritten;
1403};
1404
1405SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1406 fBlock = NULL;
1407 fBlockSize = fBytesWritten = 0;
1408 fStatus = SkGPipeReader::kDone_Status;
1409 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001410 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001411}
1412
1413SimplePC::~SimplePC() {
1414// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1415 sk_free(fBlock);
1416
reed@google.com0faac1e2011-05-11 05:58:58 +00001417 if (fTotalWritten) {
1418 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1419 fAtomsWritten, fStatus);
1420 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001421}
1422
1423void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1424 sk_free(fBlock);
1425
1426 fBlockSize = minRequest * 4;
1427 fBlock = sk_malloc_throw(fBlockSize);
1428 fBytesWritten = 0;
1429 *actual = fBlockSize;
1430 return fBlock;
1431}
1432
1433void SimplePC::notifyWritten(size_t bytes) {
1434 SkASSERT(fBytesWritten + bytes <= fBlockSize);
1435
1436 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1437 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1438 fBytesWritten += bytes;
1439 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001440
1441 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001442}
1443
reed@google.com68f456d2011-05-02 18:55:39 +00001444#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001445
reed@google.com64e3eb22011-05-04 14:32:04 +00001446
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001447void SampleView::onDraw(SkCanvas* canvas) {
1448 this->onDrawBackground(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001449
1450#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001451 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001452 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001453 if (fUsePipe) {
1454 canvas = writer.startRecording(&controller);
1455 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001456#endif
1457
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001458 for (int i = 0; i < fRepeatCount; i++) {
1459 SkAutoCanvasRestore acr(canvas, true);
1460 this->onDrawContent(canvas);
1461 }
1462}
1463
1464void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001465 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001466}
1467
1468///////////////////////////////////////////////////////////////////////////////
1469
reed@android.comf2b98d62010-12-20 18:26:13 +00001470template <typename T> void SkTBSort(T array[], int count) {
1471 for (int i = 1; i < count - 1; i++) {
1472 bool didSwap = false;
1473 for (int j = count - 1; j > i; --j) {
1474 if (array[j] < array[j-1]) {
1475 T tmp(array[j-1]);
1476 array[j-1] = array[j];
1477 array[j] = tmp;
1478 didSwap = true;
1479 }
1480 }
1481 if (!didSwap) {
1482 break;
1483 }
1484 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001485
reed@android.comf2b98d62010-12-20 18:26:13 +00001486 for (int k = 0; k < count - 1; k++) {
1487 SkASSERT(!(array[k+1] < array[k]));
1488 }
1489}
1490
1491#include "SkRandom.h"
1492
1493static void rand_rect(SkIRect* rect, SkRandom& rand) {
1494 int bits = 8;
1495 int shift = 32 - bits;
1496 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1497 rand.nextU() >> shift, rand.nextU() >> shift);
1498 rect->sort();
1499}
1500
1501static void dumpRect(const SkIRect& r) {
1502 SkDebugf(" { %d, %d, %d, %d },\n",
1503 r.fLeft, r.fTop,
1504 r.fRight, r.fBottom);
1505}
1506
1507static void test_rects(const SkIRect rect[], int count) {
1508 SkRegion rgn0, rgn1;
1509
1510 for (int i = 0; i < count; i++) {
1511 rgn0.op(rect[i], SkRegion::kUnion_Op);
1512 // dumpRect(rect[i]);
1513 }
1514 rgn1.setRects(rect, count);
1515
1516 if (rgn0 != rgn1) {
1517 SkDebugf("\n");
1518 for (int i = 0; i < count; i++) {
1519 dumpRect(rect[i]);
1520 }
1521 SkDebugf("\n");
1522 }
1523}
1524
1525static void test() {
1526 size_t i;
1527
1528 const SkIRect r0[] = {
1529 { 0, 0, 1, 1 },
1530 { 2, 2, 3, 3 },
1531 };
1532 const SkIRect r1[] = {
1533 { 0, 0, 1, 3 },
1534 { 1, 1, 2, 2 },
1535 { 2, 0, 3, 3 },
1536 };
1537 const SkIRect r2[] = {
1538 { 0, 0, 1, 2 },
1539 { 2, 1, 3, 3 },
1540 { 4, 0, 5, 1 },
1541 { 6, 0, 7, 4 },
1542 };
1543
1544 static const struct {
1545 const SkIRect* fRects;
1546 int fCount;
1547 } gRecs[] = {
1548 { r0, SK_ARRAY_COUNT(r0) },
1549 { r1, SK_ARRAY_COUNT(r1) },
1550 { r2, SK_ARRAY_COUNT(r2) },
1551 };
1552
1553 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1554 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1555 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001556
reed@android.comf2b98d62010-12-20 18:26:13 +00001557 SkRandom rand;
1558 for (i = 0; i < 10000; i++) {
1559 SkRegion rgn0, rgn1;
1560
1561 const int N = 8;
1562 SkIRect rect[N];
1563 for (int j = 0; j < N; j++) {
1564 rand_rect(&rect[j], rand);
1565 }
1566 test_rects(rect, N);
1567 }
1568}
1569
reed@android.com8a1c16f2008-12-17 15:59:43 +00001570SkOSWindow* create_sk_window(void* hwnd) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001571// test();
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001572 return new SampleWindow(hwnd);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001573}
1574
1575void get_preferred_size(int* x, int* y, int* width, int* height) {
1576 *x = 10;
1577 *y = 50;
1578 *width = 640;
1579 *height = 480;
1580}
1581
1582void application_init() {
1583// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001584#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001585 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001586#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001587 SkGraphics::Init();
1588 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001589}
1590
1591void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001592 SkEvent::Term();
1593 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001594}