blob: e6c51083081356bf6081fb133b7b00487c879c67 [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.com09e3baa2011-05-18 12:04:31 +0000125 FlagsDrawFilter(SkTriState lcd, SkTriState aa, SkTriState filter,
126 SkTriState hinting) :
127 fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000128
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000129 virtual void filter(SkPaint* paint, Type t) {
reed@google.com569e0432011-04-05 13:07:03 +0000130 if (kText_Type == t && kUnknown_SkTriState != fLCDState) {
reed@google.com569e0432011-04-05 13:07:03 +0000131 paint->setLCDRenderText(kTrue_SkTriState == fLCDState);
132 }
133 if (kUnknown_SkTriState != fAAState) {
reed@google.com569e0432011-04-05 13:07:03 +0000134 paint->setAntiAlias(kTrue_SkTriState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000135 }
reed@google.com176753a2011-05-17 15:32:04 +0000136 if (kUnknown_SkTriState != fFilterState) {
137 paint->setFilterBitmap(kTrue_SkTriState == fFilterState);
138 }
reed@google.com09e3baa2011-05-18 12:04:31 +0000139 if (kUnknown_SkTriState != fHintingState) {
140 paint->setHinting(kTrue_SkTriState == fHintingState ?
141 SkPaint::kNormal_Hinting :
142 SkPaint::kSlight_Hinting);
143 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000144 }
145
146private:
reed@google.com569e0432011-04-05 13:07:03 +0000147 SkTriState fLCDState;
reed@google.com569e0432011-04-05 13:07:03 +0000148 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000149 SkTriState fFilterState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000150 SkTriState fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000151};
152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153//////////////////////////////////////////////////////////////////////////////
154
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000155#define MAX_ZOOM_LEVEL 8
156#define MIN_ZOOM_LEVEL -8
157
reed@android.comf2b98d62010-12-20 18:26:13 +0000158static const char gCharEvtName[] = "SampleCode_Char_Event";
159static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160static const char gTitleEvtName[] = "SampleCode_Title_Event";
161static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000162static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
163
164bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
165 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
166 if (outUni) {
167 *outUni = evt.getFast32();
168 }
169 return true;
170 }
171 return false;
172}
173
174bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
175 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
176 if (outKey) {
177 *outKey = (SkKey)evt.getFast32();
178 }
179 return true;
180 }
181 return false;
182}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183
184bool SampleCode::TitleQ(const SkEvent& evt) {
185 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
186}
187
188void SampleCode::TitleR(SkEvent* evt, const char title[]) {
189 SkASSERT(evt && TitleQ(*evt));
190 evt->setString(gTitleEvtName, title);
191}
192
193bool SampleCode::PrefSizeQ(const SkEvent& evt) {
194 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
195}
196
197void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
198 SkASSERT(evt && PrefSizeQ(*evt));
199 SkScalar size[2];
200 size[0] = width;
201 size[1] = height;
202 evt->setScalars(gPrefSizeEvtName, 2, size);
203}
204
reed@android.comf2b98d62010-12-20 18:26:13 +0000205bool SampleCode::FastTextQ(const SkEvent& evt) {
206 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
207}
208
209///////////////////////////////////////////////////////////////////////////////
210
reed@android.com44177402009-11-23 21:07:51 +0000211static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000212static SkMSec gAnimTimePrev;
213
reed@android.com44177402009-11-23 21:07:51 +0000214SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000215SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
216SkScalar SampleCode::GetAnimSecondsDelta() {
217 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
218}
reed@android.com44177402009-11-23 21:07:51 +0000219
220SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000221 // since gAnimTime can be up to 32 bits, we can't convert it to a float
222 // or we'll lose the low bits. Hence we use doubles for the intermediate
223 // calculations
224 double seconds = (double)gAnimTime / 1000.0;
225 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000226 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000227 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000228 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000229 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000230}
231
reed@android.com8a1c16f2008-12-17 15:59:43 +0000232//////////////////////////////////////////////////////////////////////////////
233
reed@android.comf2b98d62010-12-20 18:26:13 +0000234static SkView* curr_view(SkWindow* wind) {
235 SkView::F2BIter iter(wind);
236 return iter.next();
237}
238
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239class SampleWindow : public SkOSWindow {
reed@android.com34245c72009-11-03 04:00:48 +0000240 SkTDArray<SkViewFactory> fSamples;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000241public:
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000242 SampleWindow(void* hwnd);
243 virtual ~SampleWindow();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000244
reed@android.come522ca52009-11-23 20:10:41 +0000245 virtual void draw(SkCanvas* canvas);
246
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247protected:
248 virtual void onDraw(SkCanvas* canvas);
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000249 virtual bool onHandleKey(SkKey key);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250 virtual bool onHandleChar(SkUnichar);
251 virtual void onSizeChange();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000252
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253 virtual SkCanvas* beforeChildren(SkCanvas*);
254 virtual void afterChildren(SkCanvas*);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000255 virtual void beforeChild(SkView* child, SkCanvas* canvas);
256 virtual void afterChild(SkView* child, SkCanvas* canvas);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000257
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000258 virtual bool onEvent(const SkEvent& evt);
reed@android.comf2b98d62010-12-20 18:26:13 +0000259 virtual bool onQuery(SkEvent* evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000261 virtual bool onDispatchClick(int x, int y, Click::State);
reed@google.com52f57e12011-03-16 12:10:02 +0000262 virtual bool onClick(Click* click);
263 virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
264
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265#if 0
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000266 virtual bool handleChar(SkUnichar uni);
267 virtual bool handleEvent(const SkEvent& evt);
268 virtual bool handleKey(SkKey key);
269 virtual bool handleKeyUp(SkKey key);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270 virtual bool onHandleKeyUp(SkKey key);
271#endif
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000272
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273private:
reed@android.com34245c72009-11-03 04:00:48 +0000274 int fCurrIndex;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000275
reed@android.com8a1c16f2008-12-17 15:59:43 +0000276 SkPicture* fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000277 SkGpuCanvas* fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000278 GrContext* fGrContext;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 SkPath fClipPath;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000280
reed@google.com52f57e12011-03-16 12:10:02 +0000281 SkTouchGesture fGesture;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000282 int fZoomLevel;
283 SkScalar fZoomScale;
reed@google.com52f57e12011-03-16 12:10:02 +0000284
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 enum CanvasType {
286 kRaster_CanvasType,
287 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000288 kGPU_CanvasType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289 };
290 CanvasType fCanvasType;
291
292 bool fUseClip;
reed@android.come522ca52009-11-23 20:10:41 +0000293 bool fNClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000294 bool fRepeatDrawing;
295 bool fAnimating;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000296 bool fRotate;
297 bool fScale;
reed@android.comf2b98d62010-12-20 18:26:13 +0000298 bool fRequestGrabImage;
reed@google.com0faac1e2011-05-11 05:58:58 +0000299 bool fUsePipe;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000300 bool fMeasureFPS;
301 SkMSec fMeasureFPS_Time;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000302
Scroggo0f185c22011-03-24 18:35:50 +0000303 // The following are for the 'fatbits' drawing
304 // Latest position of the mouse.
305 int fMouseX, fMouseY;
306 int fFatBitsScale;
307 // Used by the text showing position and color values.
308 SkTypeface* fTypeface;
309 bool fShowZoomer;
310
reed@google.com569e0432011-04-05 13:07:03 +0000311 SkTriState fLCDState;
312 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000313 SkTriState fFilterState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000314 SkTriState fHintingState;
reed@google.com569e0432011-04-05 13:07:03 +0000315 unsigned fFlipAxis;
reed@google.comf0b5f682011-03-11 20:08:25 +0000316
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317 int fScrollTestX, fScrollTestY;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000318
319 bool make3DReady();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000320 void changeZoomLevel(int delta);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000321
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 void loadView(SkView*);
323 void updateTitle();
324 bool nextSample();
325
Scroggo0f185c22011-03-24 18:35:50 +0000326 void toggleZoomer();
327 bool zoomIn();
328 bool zoomOut();
329 void updatePointer(int x, int y);
330
reed@android.com8a1c16f2008-12-17 15:59:43 +0000331 void postAnimatingEvent() {
332 if (fAnimating) {
333 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
334 evt->post(this->getSinkID(), ANIMATING_DELAY);
335 }
336 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000337
338
reed@android.com8a1c16f2008-12-17 15:59:43 +0000339 static CanvasType cycle_canvastype(CanvasType);
340
341 typedef SkOSWindow INHERITED;
342};
343
Scroggo0f185c22011-03-24 18:35:50 +0000344bool SampleWindow::zoomIn()
345{
346 // Arbitrarily decided
347 if (fFatBitsScale == 25) return false;
348 fFatBitsScale++;
349 this->inval(NULL);
350 return true;
351}
352
353bool SampleWindow::zoomOut()
354{
355 if (fFatBitsScale == 1) return false;
356 fFatBitsScale--;
357 this->inval(NULL);
358 return true;
359}
360
361void SampleWindow::toggleZoomer()
362{
363 fShowZoomer = !fShowZoomer;
364 this->inval(NULL);
365}
366
367void SampleWindow::updatePointer(int x, int y)
368{
369 fMouseX = x;
370 fMouseY = y;
371 if (fShowZoomer) {
372 this->inval(NULL);
373 }
374}
375
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000376bool SampleWindow::make3DReady() {
377
378#if defined(SK_SUPPORT_GL)
bsalomon@google.com498a6232011-03-10 18:24:15 +0000379 if (attachGL()) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000380 if (NULL != fGrContext) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000381 // various gr lifecycle tests
382 #if 0
383 fGrContext->freeGpuResources();
384 #elif 0
385 // this will leak resources.
386 fGrContext->contextLost();
387 #elif 0
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000388 GrAssert(1 == fGrContext->refcnt());
389 fGrContext->unref();
390 fGrContext = NULL;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000391 #endif
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000392 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000393
bsalomon@google.com498a6232011-03-10 18:24:15 +0000394 if (NULL == fGrContext) {
395 #if defined(SK_USE_SHADERS)
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000396 fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000397 #else
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000398 fGrContext = GrContext::Create(kOpenGL_Fixed_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000399 #endif
reed@google.com569e0432011-04-05 13:07:03 +0000400 SkDebugf("---- constructor\n");
bsalomon@google.com498a6232011-03-10 18:24:15 +0000401 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000402
bsalomon@google.com498a6232011-03-10 18:24:15 +0000403 if (NULL != fGrContext) {
404 return true;
405 } else {
406 detachGL();
407 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000408 }
409#endif
410 SkDebugf("Failed to setup 3D");
411 return false;
412}
413
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) {
415 static const CanvasType gCT[] = {
416 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000417 kGPU_CanvasType,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000418 kRaster_CanvasType
419 };
420 return gCT[ct];
421}
422
423SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000424 fPicture = NULL;
reed@android.comf2b98d62010-12-20 18:26:13 +0000425 fGpuCanvas = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000427 fGrContext = NULL;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000428
reed@android.comf2b98d62010-12-20 18:26:13 +0000429#ifdef DEFAULT_TO_GPU
430 fCanvasType = kGPU_CanvasType;
431#else
reed@android.com8a1c16f2008-12-17 15:59:43 +0000432 fCanvasType = kRaster_CanvasType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000433#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000434 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000435 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000436 fRepeatDrawing = false;
437 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000438 fRotate = false;
439 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000440 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000441 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000442 fMeasureFPS = false;
reed@google.com569e0432011-04-05 13:07:03 +0000443 fLCDState = kUnknown_SkTriState;
444 fAAState = kUnknown_SkTriState;
reed@google.com66f22fd2011-05-17 15:33:45 +0000445 fFilterState = kUnknown_SkTriState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000446 fHintingState = kUnknown_SkTriState;
reed@google.com569e0432011-04-05 13:07:03 +0000447 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448 fScrollTestX = fScrollTestY = 0;
449
Scroggo0f185c22011-03-24 18:35:50 +0000450 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000451 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000452 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
453 fShowZoomer = false;
454
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000455 fZoomLevel = 0;
456 fZoomScale = SK_Scalar1;
457
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000458// this->setConfig(SkBitmap::kRGB_565_Config);
459 this->setConfig(SkBitmap::kARGB_8888_Config);
460 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000461 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000462
reed@android.com34245c72009-11-03 04:00:48 +0000463 {
464 const SkViewRegister* reg = SkViewRegister::Head();
465 while (reg) {
466 *fSamples.append() = reg->factory();
467 reg = reg->next();
468 }
469 }
470 fCurrIndex = 0;
reed@android.come0f13ee2009-11-04 19:40:25 +0000471 this->loadView(fSamples[fCurrIndex]());
reed@google.comf0b5f682011-03-11 20:08:25 +0000472
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000473#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +0000474 testpdf();
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000475#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000476}
477
478SampleWindow::~SampleWindow() {
479 delete fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000480 delete fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000481 if (NULL != fGrContext) {
482 fGrContext->unref();
483 }
Scroggo0f185c22011-03-24 18:35:50 +0000484 fTypeface->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000485}
486
reed@android.com55e76b22009-11-23 21:46:47 +0000487static SkBitmap capture_bitmap(SkCanvas* canvas) {
488 SkBitmap bm;
489 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
490 src.copyTo(&bm, src.config());
491 return bm;
492}
493
494static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
495 SkBitmap* diff) {
496 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000497
reed@android.com55e76b22009-11-23 21:46:47 +0000498 SkAutoLockPixels alp0(src);
499 SkAutoLockPixels alp1(orig);
500 for (int y = 0; y < src.height(); y++) {
501 const void* srcP = src.getAddr(0, y);
502 const void* origP = orig.getAddr(0, y);
503 size_t bytes = src.width() * src.bytesPerPixel();
504 if (memcmp(srcP, origP, bytes)) {
505 SkDebugf("---------- difference on line %d\n", y);
506 return true;
507 }
508 }
509 return false;
510}
511
Scroggo0f185c22011-03-24 18:35:50 +0000512static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
513{
514 SkColor desiredColor = paint.getColor();
515 paint.setColor(SK_ColorWHITE);
516 const char* c_str = string.c_str();
517 size_t size = string.size();
518 SkRect bounds;
519 paint.measureText(c_str, size, &bounds);
520 bounds.offset(left, top);
521 SkScalar inset = SkIntToScalar(-2);
522 bounds.inset(inset, inset);
523 canvas->drawRect(bounds, paint);
524 if (desiredColor != SK_ColorBLACK) {
525 paint.setColor(SK_ColorBLACK);
526 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
527 }
528 paint.setColor(desiredColor);
529 canvas->drawText(c_str, size, left, top, paint);
530}
531
reed@android.com44177402009-11-23 21:07:51 +0000532#define XCLIP_N 8
533#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000534
535void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000536 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000537 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000538 gAnimTime = SkTime::GetMSecs();
539
reed@google.com569e0432011-04-05 13:07:03 +0000540 SkScalar cx = SkScalarHalf(this->width());
541 SkScalar cy = SkScalarHalf(this->height());
542
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000543 if (fZoomLevel) {
544 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000545 SkPoint center;
546 m = canvas->getTotalMatrix();//.invert(&m);
547 m.mapXY(cx, cy, &center);
548 cx = center.fX;
549 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000550
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000551 m.setTranslate(-cx, -cy);
552 m.postScale(fZoomScale, fZoomScale);
553 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000554
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000555 canvas->concat(m);
556 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000557
reed@google.com569e0432011-04-05 13:07:03 +0000558 if (fFlipAxis) {
559 SkMatrix m;
560 m.setTranslate(cx, cy);
561 if (fFlipAxis & kFlipAxis_X) {
562 m.preScale(-SK_Scalar1, SK_Scalar1);
563 }
564 if (fFlipAxis & kFlipAxis_Y) {
565 m.preScale(SK_Scalar1, -SK_Scalar1);
566 }
567 m.preTranslate(-cx, -cy);
568 canvas->concat(m);
569 }
570
reed@google.com52f57e12011-03-16 12:10:02 +0000571 // Apply any gesture matrix
572 if (true) {
573 const SkMatrix& localM = fGesture.localM();
574 if (localM.getType() & SkMatrix::kScale_Mask) {
575 canvas->setExternalMatrix(&localM);
576 }
577 canvas->concat(localM);
578 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000579
reed@google.com52f57e12011-03-16 12:10:02 +0000580 if (fGesture.isActive()) {
581 this->inval(NULL);
582 }
583 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000584
reed@android.come522ca52009-11-23 20:10:41 +0000585 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000586 this->INHERITED::draw(canvas);
587 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000588
589 const SkScalar w = this->width();
590 const SkScalar h = this->height();
591 const SkScalar cw = w / XCLIP_N;
592 const SkScalar ch = h / YCLIP_N;
593 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000594 SkRect r;
595 r.fTop = y * ch;
596 r.fBottom = (y + 1) * ch;
597 if (y == YCLIP_N - 1) {
598 r.fBottom = h;
599 }
reed@android.come522ca52009-11-23 20:10:41 +0000600 for (int x = 0; x < XCLIP_N; x++) {
601 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000602 r.fLeft = x * cw;
603 r.fRight = (x + 1) * cw;
604 if (x == XCLIP_N - 1) {
605 r.fRight = w;
606 }
reed@android.come522ca52009-11-23 20:10:41 +0000607 canvas->clipRect(r);
608 this->INHERITED::draw(canvas);
609 }
610 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000611
reed@android.com55e76b22009-11-23 21:46:47 +0000612 SkBitmap diff;
613 if (bitmap_diff(canvas, orig, &diff)) {
614 }
reed@android.come522ca52009-11-23 20:10:41 +0000615 } else {
616 this->INHERITED::draw(canvas);
617 }
Scroggo0f185c22011-03-24 18:35:50 +0000618 if (fShowZoomer) {
619 int count = canvas->save();
620 canvas->resetMatrix();
621 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000622 int width = SkScalarRound(this->width());
623 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000624 if (fMouseX >= width) fMouseX = width - 1;
625 else if (fMouseX < 0) fMouseX = 0;
626 if (fMouseY >= height) fMouseY = height - 1;
627 else if (fMouseY < 0) fMouseY = 0;
628 SkBitmap bitmap = capture_bitmap(canvas);
629 // 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 +0000630 int zoomedWidth = (width >> 1) | 1;
631 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000632 SkIRect src;
633 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
634 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
635 SkRect dest;
636 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
637 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
638 SkPaint paint;
639 // Clear the background behind our zoomed in view
640 paint.setColor(SK_ColorWHITE);
641 canvas->drawRect(dest, paint);
642 canvas->drawBitmapRect(bitmap, &src, dest);
643 paint.setColor(SK_ColorBLACK);
644 paint.setStyle(SkPaint::kStroke_Style);
645 // Draw a border around the pixel in the middle
646 SkRect originalPixel;
647 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
648 SkMatrix matrix;
649 SkRect scalarSrc;
650 scalarSrc.set(src);
651 SkColor color = bitmap.getColor(fMouseX, fMouseY);
652 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
653 SkRect pixel;
654 matrix.mapRect(&pixel, originalPixel);
655 // TODO Perhaps measure the values and make the outline white if it's "dark"
656 if (color == SK_ColorBLACK) {
657 paint.setColor(SK_ColorWHITE);
658 }
659 canvas->drawRect(pixel, paint);
660 }
661 paint.setColor(SK_ColorBLACK);
662 // Draw a border around the destination rectangle
663 canvas->drawRect(dest, paint);
664 paint.setStyle(SkPaint::kStrokeAndFill_Style);
665 // Identify the pixel and its color on screen
666 paint.setTypeface(fTypeface);
667 paint.setAntiAlias(true);
668 SkScalar lineHeight = paint.getFontMetrics(NULL);
669 SkString string;
670 string.appendf("(%i, %i)", fMouseX, fMouseY);
671 SkScalar left = dest.fLeft + SkIntToScalar(3);
672 SkScalar i = SK_Scalar1;
673 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
674 // Alpha
675 i += SK_Scalar1;
676 string.reset();
677 string.appendf("A: %X", SkColorGetA(color));
678 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
679 // Red
680 i += SK_Scalar1;
681 string.reset();
682 string.appendf("R: %X", SkColorGetR(color));
683 paint.setColor(SK_ColorRED);
684 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
685 // Green
686 i += SK_Scalar1;
687 string.reset();
688 string.appendf("G: %X", SkColorGetG(color));
689 paint.setColor(SK_ColorGREEN);
690 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
691 // Blue
692 i += SK_Scalar1;
693 string.reset();
694 string.appendf("B: %X", SkColorGetB(color));
695 paint.setColor(SK_ColorBLUE);
696 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
697 canvas->restoreToCount(count);
698 }
reed@android.come522ca52009-11-23 20:10:41 +0000699}
700
reed@android.com8a1c16f2008-12-17 15:59:43 +0000701void SampleWindow::onDraw(SkCanvas* canvas) {
702 if (fRepeatDrawing) {
703 this->inval(NULL);
704 }
705}
706
707#include "SkColorPriv.h"
708
709static void reverseRedAndBlue(const SkBitmap& bm) {
710 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
711 uint8_t* p = (uint8_t*)bm.getPixels();
712 uint8_t* stop = p + bm.getSize();
713 while (p < stop) {
714 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
715 unsigned scale = SkAlpha255To256(p[3]);
716 unsigned r = p[2];
717 unsigned b = p[0];
718 p[0] = SkAlphaMul(r, scale);
719 p[1] = SkAlphaMul(p[1], scale);
720 p[2] = SkAlphaMul(b, scale);
721 p += 4;
722 }
723}
724
725SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000726 if (kGPU_CanvasType != fCanvasType) {
reed@android.com6efdc472008-12-19 18:24:35 +0000727#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000728 detachGL();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000729#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000730 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000731
reed@android.com8a1c16f2008-12-17 15:59:43 +0000732 switch (fCanvasType) {
733 case kRaster_CanvasType:
734 canvas = this->INHERITED::beforeChildren(canvas);
735 break;
736 case kPicture_CanvasType:
737 fPicture = new SkPicture;
738 canvas = fPicture->beginRecording(9999, 9999);
739 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000740 case kGPU_CanvasType: {
reed@google.com64e3eb22011-05-04 14:32:04 +0000741 if (make3DReady()) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000742 SkDevice* device = canvas->getDevice();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000743 const SkBitmap& bitmap = device->accessBitmap(true);
744
bsalomon@google.com5782d712011-01-21 21:03:59 +0000745 GrRenderTarget* renderTarget;
746 renderTarget = fGrContext->createRenderTargetFrom3DApiState();
747 fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
748 renderTarget->unref();
749
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000750 device = fGpuCanvas->createDevice(SkBitmap::kARGB_8888_Config,
751 bitmap.width(), bitmap.height(),
752 false, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000753 fGpuCanvas->setDevice(device)->unref();
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000754
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000755 fGpuCanvas->concat(canvas->getTotalMatrix());
reed@android.comf2b98d62010-12-20 18:26:13 +0000756 canvas = fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000757
reed@android.comf2b98d62010-12-20 18:26:13 +0000758 } else {
759 canvas = this->INHERITED::beforeChildren(canvas);
760 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000761 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000762 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000763 }
764
765 if (fUseClip) {
766 canvas->drawColor(0xFFFF88FF);
767 canvas->clipPath(fClipPath);
768 }
769
770 return canvas;
771}
772
773static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
774 const SkRegion& rgn) {
775 SkCanvas canvas(bm);
776 SkRegion inval(rgn);
777
778 inval.translate(r.fLeft, r.fTop);
779 canvas.clipRegion(inval);
780 canvas.drawColor(0xFFFF8080);
781}
782
783void SampleWindow::afterChildren(SkCanvas* orig) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000784 if (fRequestGrabImage) {
785 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000786
reed@android.comf2b98d62010-12-20 18:26:13 +0000787 SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
788 SkDevice* device = canvas->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000789 SkBitmap bmp;
790 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000791 static int gSampleGrabCounter;
792 SkString name;
793 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000794 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000795 SkImageEncoder::kPNG_Type, 100);
796 }
797 }
798
reed@android.com8a1c16f2008-12-17 15:59:43 +0000799 switch (fCanvasType) {
800 case kRaster_CanvasType:
801 break;
802 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000803 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000804 SkPicture* pict = new SkPicture(*fPicture);
805 fPicture->unref();
806 orig->drawPicture(*pict);
807 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000808 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000809 SkDynamicMemoryWStream ostream;
810 fPicture->serialize(&ostream);
811 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000812
reed@android.com8a1c16f2008-12-17 15:59:43 +0000813 SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
814 SkPicture pict(&istream);
815 orig->drawPicture(pict);
816 } else {
817 fPicture->draw(orig);
818 fPicture->unref();
819 }
820 fPicture = NULL;
821 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000822#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000823 case kGPU_CanvasType:
824 delete fGpuCanvas;
825 fGpuCanvas = NULL;
826 presentGL();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000827 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000828#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000829 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000830
reed@google.com17d7aec2011-04-25 14:31:44 +0000831 // Do this after presentGL and other finishing, rather than in afterChild
832 if (fMeasureFPS && fMeasureFPS_Time) {
833 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
834 this->updateTitle();
835 postInvalDelay(this->getSinkID());
836 }
837
838 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000839 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000840 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
841 int dx = fScrollTestX * 7;
842 int dy = fScrollTestY * 7;
843 SkIRect r;
844 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000845
reed@android.com8a1c16f2008-12-17 15:59:43 +0000846 r.set(50, 50, 50+100, 50+100);
847 bm.scrollRect(&r, dx, dy, &inval);
848 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000849 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000850}
851
reed@android.com6c5f6f22009-08-14 16:08:38 +0000852void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
853 if (fScale) {
854 SkScalar scale = SK_Scalar1 * 7 / 10;
855 SkScalar cx = this->width() / 2;
856 SkScalar cy = this->height() / 2;
857 canvas->translate(cx, cy);
858 canvas->scale(scale, scale);
859 canvas->translate(-cx, -cy);
860 }
861 if (fRotate) {
862 SkScalar cx = this->width() / 2;
863 SkScalar cy = this->height() / 2;
864 canvas->translate(cx, cy);
865 canvas->rotate(SkIntToScalar(30));
866 canvas->translate(-cx, -cy);
867 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000868
reed@google.com09e3baa2011-05-18 12:04:31 +0000869 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
870 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000871
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000872 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000873 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
874 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
875 fMeasureFPS_Time = SkTime::GetMSecs();
876 }
877 } else {
878 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000879 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000880 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000881}
882
883void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000884 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000885}
886
reed@android.com8a1c16f2008-12-17 15:59:43 +0000887static SkBitmap::Config gConfigCycle[] = {
888 SkBitmap::kNo_Config, // none -> none
889 SkBitmap::kNo_Config, // a1 -> none
890 SkBitmap::kNo_Config, // a8 -> none
891 SkBitmap::kNo_Config, // index8 -> none
892 SkBitmap::kARGB_4444_Config, // 565 -> 4444
893 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
894 SkBitmap::kRGB_565_Config // 8888 -> 565
895};
896
897static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
898 return gConfigCycle[c];
899}
900
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000901void SampleWindow::changeZoomLevel(int delta) {
902 fZoomLevel += delta;
903 if (fZoomLevel > 0) {
904 fZoomLevel = SkMin32(fZoomLevel, MAX_ZOOM_LEVEL);
905 fZoomScale = SkIntToScalar(fZoomLevel + 1);
906 } else if (fZoomLevel < 0) {
907 fZoomLevel = SkMax32(fZoomLevel, MIN_ZOOM_LEVEL);
908 fZoomScale = SK_Scalar1 / (1 - fZoomLevel);
909 } else {
910 fZoomScale = SK_Scalar1;
911 }
912
913 this->inval(NULL);
914}
915
reed@android.com8a1c16f2008-12-17 15:59:43 +0000916bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000917 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
918 this->loadView(fSamples[fCurrIndex]());
919 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000920}
921
922bool SampleWindow::onEvent(const SkEvent& evt) {
923 if (evt.isType(ANIMATING_EVENTTYPE)) {
924 if (fAnimating) {
925 this->nextSample();
926 this->postAnimatingEvent();
927 }
928 return true;
929 }
reed@android.com34245c72009-11-03 04:00:48 +0000930 if (evt.isType("set-curr-index")) {
931 fCurrIndex = evt.getFast32() % fSamples.count();
932 this->loadView(fSamples[fCurrIndex]());
933 return true;
934 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000935 if (isInvalEvent(evt)) {
936 this->inval(NULL);
937 return true;
938 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000939 return this->INHERITED::onEvent(evt);
940}
941
reed@android.comf2b98d62010-12-20 18:26:13 +0000942bool SampleWindow::onQuery(SkEvent* query) {
943 if (query->isType("get-slide-count")) {
944 query->setFast32(fSamples.count());
945 return true;
946 }
947 if (query->isType("get-slide-title")) {
948 SkView* view = fSamples[query->getFast32()]();
949 SkEvent evt(gTitleEvtName);
950 if (view->doQuery(&evt)) {
951 query->setString("title", evt.findString(gTitleEvtName));
952 }
953 SkSafeUnref(view);
954 return true;
955 }
956 if (query->isType("use-fast-text")) {
957 SkEvent evt(gFastTextEvtName);
958 return curr_view(this)->doQuery(&evt);
959 }
960 return this->INHERITED::onQuery(query);
961}
962
reed@android.com0ae6b242008-12-23 16:49:54 +0000963static void cleanup_for_filename(SkString* name) {
964 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +0000965 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +0000966 switch (str[i]) {
967 case ':': str[i] = '-'; break;
968 case '/': str[i] = '-'; break;
969 case ' ': str[i] = '_'; break;
970 default: break;
971 }
972 }
973}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000974
975bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000976 {
977 SkView* view = curr_view(this);
978 if (view) {
979 SkEvent evt(gCharEvtName);
980 evt.setFast32(uni);
981 if (view->doQuery(&evt)) {
982 return true;
983 }
984 }
985 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000986
reed@android.com8a1c16f2008-12-17 15:59:43 +0000987 int dx = 0xFF;
988 int dy = 0xFF;
989
990 switch (uni) {
991 case '5': dx = 0; dy = 0; break;
992 case '8': dx = 0; dy = -1; break;
993 case '6': dx = 1; dy = 0; break;
994 case '2': dx = 0; dy = 1; break;
995 case '4': dx = -1; dy = 0; break;
996 case '7': dx = -1; dy = -1; break;
997 case '9': dx = 1; dy = -1; break;
998 case '3': dx = 1; dy = 1; break;
999 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001000
reed@android.com8a1c16f2008-12-17 15:59:43 +00001001 default:
1002 break;
1003 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001004
reed@android.com8a1c16f2008-12-17 15:59:43 +00001005 if (0xFF != dx && 0xFF != dy) {
1006 if ((dx | dy) == 0) {
1007 fScrollTestX = fScrollTestY = 0;
1008 } else {
1009 fScrollTestX += dx;
1010 fScrollTestY += dy;
1011 }
1012 this->inval(NULL);
1013 return true;
1014 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001015
reed@android.com0ae6b242008-12-23 16:49:54 +00001016 switch (uni) {
1017 case 'a':
1018 fAnimating = !fAnimating;
1019 this->postAnimatingEvent();
1020 this->updateTitle();
1021 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001022 case 'b':
1023 fAAState = cycle_tristate(fAAState);
1024 this->updateTitle();
1025 this->inval(NULL);
1026 break;
1027 case 'c':
1028 fUseClip = !fUseClip;
1029 this->inval(NULL);
1030 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001031 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001032 case 'd':
1033 SkGraphics::SetFontCacheUsed(0);
1034 return true;
1035 case 'f':
1036 fMeasureFPS = !fMeasureFPS;
1037 this->inval(NULL);
1038 break;
1039 case 'g':
1040 fRequestGrabImage = true;
1041 this->inval(NULL);
1042 break;
reed@google.com09e3baa2011-05-18 12:04:31 +00001043 case 'h':
1044 fHintingState = cycle_tristate(fHintingState);
1045 this->updateTitle();
1046 this->inval(NULL);
1047 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001048 case 'i':
1049 this->zoomIn();
1050 break;
1051 case 'l':
1052 fLCDState = cycle_tristate(fLCDState);
1053 this->updateTitle();
1054 this->inval(NULL);
1055 break;
reed@google.com176753a2011-05-17 15:32:04 +00001056 case 'n':
1057 fFilterState = cycle_tristate(fFilterState);
1058 this->updateTitle();
1059 this->inval(NULL);
1060 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001061 case 'o':
1062 this->zoomOut();
1063 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001064 case 'p':
1065 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001066 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001067 this->inval(NULL);
1068 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001069 case 'r':
1070 fRotate = !fRotate;
1071 this->inval(NULL);
1072 this->updateTitle();
1073 return true;
1074 case 's':
1075 fScale = !fScale;
1076 this->inval(NULL);
1077 this->updateTitle();
1078 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001079 case 'x':
1080 fFlipAxis ^= kFlipAxis_X;
1081 this->updateTitle();
1082 this->inval(NULL);
1083 break;
1084 case 'y':
1085 fFlipAxis ^= kFlipAxis_Y;
1086 this->updateTitle();
1087 this->inval(NULL);
1088 break;
scroggo08526c02011-03-22 14:03:21 +00001089 case 'z':
1090 this->toggleZoomer();
1091 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001092 default:
1093 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001094 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001095
reed@android.com8a1c16f2008-12-17 15:59:43 +00001096 return this->INHERITED::onHandleChar(uni);
1097}
1098
1099#include "SkDumpCanvas.h"
1100
1101bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001102 {
1103 SkView* view = curr_view(this);
1104 if (view) {
1105 SkEvent evt(gKeyEvtName);
1106 evt.setFast32(key);
1107 if (view->doQuery(&evt)) {
1108 return true;
1109 }
1110 }
1111 }
1112
reed@android.com8a1c16f2008-12-17 15:59:43 +00001113 switch (key) {
1114 case kRight_SkKey:
1115 if (this->nextSample()) {
1116 return true;
1117 }
1118 break;
1119 case kLeft_SkKey:
1120 fCanvasType = cycle_canvastype(fCanvasType);
1121 this->updateTitle();
1122 this->inval(NULL);
1123 return true;
1124 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001125 if (USE_ARROWS_FOR_ZOOM) {
1126 this->changeZoomLevel(1);
1127 } else {
1128 fNClip = !fNClip;
1129 this->inval(NULL);
1130 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001131 this->updateTitle();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001132 return true;
1133 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001134 if (USE_ARROWS_FOR_ZOOM) {
1135 this->changeZoomLevel(-1);
1136 } else {
1137 this->setConfig(cycle_configs(this->getBitmap().config()));
1138 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001139 this->updateTitle();
1140 return true;
1141 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001142 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001143 SkDebugfDumper dumper;
1144 SkDumpCanvas dc(&dumper);
1145 this->draw(&dc);
1146 } else {
1147 fRepeatDrawing = !fRepeatDrawing;
1148 if (fRepeatDrawing) {
1149 this->inval(NULL);
1150 }
1151 }
1152 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001153 case kBack_SkKey:
1154 this->loadView(NULL);
1155 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001156 default:
1157 break;
1158 }
1159 return this->INHERITED::onHandleKey(key);
1160}
1161
reed@google.com52f57e12011-03-16 12:10:02 +00001162///////////////////////////////////////////////////////////////////////////////
1163
1164static const char gGestureClickType[] = "GestureClickType";
1165
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001166bool SampleWindow::onDispatchClick(int x, int y, Click::State state) {
Scroggo0f185c22011-03-24 18:35:50 +00001167 if (Click::kMoved_State == state) {
1168 updatePointer(x, y);
1169 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001170 int w = SkScalarRound(this->width());
1171 int h = SkScalarRound(this->height());
1172
1173 // check for the resize-box
1174 if (w - x < 16 && h - y < 16) {
1175 return false; // let the OS handle the click
1176 } else {
1177 return this->INHERITED::onDispatchClick(x, y, state);
1178 }
1179}
1180
reed@google.com52f57e12011-03-16 12:10:02 +00001181class GestureClick : public SkView::Click {
1182public:
1183 GestureClick(SkView* target) : SkView::Click(target) {
1184 this->setType(gGestureClickType);
1185 }
1186
1187 static bool IsGesture(Click* click) {
1188 return click->isType(gGestureClickType);
1189 }
1190};
1191
1192SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1193 return new GestureClick(this);
1194}
1195
1196bool SampleWindow::onClick(Click* click) {
1197 if (GestureClick::IsGesture(click)) {
1198 float x = SkScalarToFloat(click->fCurr.fX);
1199 float y = SkScalarToFloat(click->fCurr.fY);
1200 switch (click->fState) {
1201 case SkView::Click::kDown_State:
1202 fGesture.touchBegin(click, x, y);
1203 break;
1204 case SkView::Click::kMoved_State:
1205 fGesture.touchMoved(click, x, y);
1206 this->inval(NULL);
1207 break;
1208 case SkView::Click::kUp_State:
1209 fGesture.touchEnd(click);
1210 this->inval(NULL);
1211 break;
1212 }
1213 return true;
1214 }
1215 return false;
1216}
1217
1218///////////////////////////////////////////////////////////////////////////////
1219
reed@android.com8a1c16f2008-12-17 15:59:43 +00001220void SampleWindow::loadView(SkView* view) {
1221 SkView::F2BIter iter(this);
1222 SkView* prev = iter.next();
1223 if (prev) {
1224 prev->detachFromParent();
1225 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001226
reed@android.com34245c72009-11-03 04:00:48 +00001227 if (NULL == view) {
1228 view = create_overview(fSamples.count(), fSamples.begin());
1229 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001230 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001231 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001232 this->attachChildToFront(view)->unref();
1233 view->setSize(this->width(), this->height());
1234
1235 this->updateTitle();
1236}
1237
1238static const char* gConfigNames[] = {
1239 "unknown config",
1240 "A1",
1241 "A8",
1242 "Index8",
1243 "565",
1244 "4444",
1245 "8888"
1246};
1247
1248static const char* configToString(SkBitmap::Config c) {
1249 return gConfigNames[c];
1250}
1251
1252static const char* gCanvasTypePrefix[] = {
1253 "raster: ",
1254 "picture: ",
1255 "opengl: "
1256};
1257
reed@google.com569e0432011-04-05 13:07:03 +00001258static const char* trystate_str(SkTriState state,
1259 const char trueStr[], const char falseStr[]) {
1260 if (kTrue_SkTriState == state) {
1261 return trueStr;
1262 } else if (kFalse_SkTriState == state) {
1263 return falseStr;
1264 }
1265 return NULL;
1266}
1267
reed@android.com8a1c16f2008-12-17 15:59:43 +00001268void SampleWindow::updateTitle() {
1269 SkString title;
1270
1271 SkView::F2BIter iter(this);
1272 SkView* view = iter.next();
1273 SkEvent evt(gTitleEvtName);
1274 if (view->doQuery(&evt)) {
1275 title.set(evt.findString(gTitleEvtName));
1276 }
1277 if (title.size() == 0) {
1278 title.set("<unknown>");
1279 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001280
reed@android.com8a1c16f2008-12-17 15:59:43 +00001281 title.prepend(gCanvasTypePrefix[fCanvasType]);
1282
1283 title.prepend(" ");
1284 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001285
reed@android.com8a1c16f2008-12-17 15:59:43 +00001286 if (fAnimating) {
1287 title.prepend("<A> ");
1288 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001289 if (fScale) {
1290 title.prepend("<S> ");
1291 }
1292 if (fRotate) {
1293 title.prepend("<R> ");
1294 }
reed@android.come522ca52009-11-23 20:10:41 +00001295 if (fNClip) {
1296 title.prepend("<C> ");
1297 }
reed@google.com569e0432011-04-05 13:07:03 +00001298
1299 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1300 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001301 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001302 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1303 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001304
1305 if (fZoomLevel) {
1306 title.prependf("{%d} ", fZoomLevel);
1307 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001308
1309 if (fMeasureFPS) {
1310 title.appendf(" %4d ms", fMeasureFPS_Time);
1311 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001312 if (fUsePipe && SampleView::IsSampleView(view)) {
1313 title.prepend("<P> ");
1314 }
1315 if (SampleView::IsSampleView(view)) {
1316 title.prepend("! ");
1317 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001318
reed@android.com8a1c16f2008-12-17 15:59:43 +00001319 this->setTitle(title.c_str());
1320}
1321
1322void SampleWindow::onSizeChange() {
1323 this->INHERITED::onSizeChange();
1324
1325 SkView::F2BIter iter(this);
1326 SkView* view = iter.next();
1327 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001328
reed@android.com8a1c16f2008-12-17 15:59:43 +00001329 // rebuild our clippath
1330 {
1331 const SkScalar W = this->width();
1332 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001333
reed@android.com8a1c16f2008-12-17 15:59:43 +00001334 fClipPath.reset();
1335#if 0
1336 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1337 SkRect r;
1338 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1339 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1340 fClipPath.addRect(r);
1341 }
1342#else
1343 SkRect r;
1344 r.set(0, 0, W, H);
1345 fClipPath.addRect(r, SkPath::kCCW_Direction);
1346 r.set(W/4, H/4, W*3/4, H*3/4);
1347 fClipPath.addRect(r, SkPath::kCW_Direction);
1348#endif
1349 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001350
reed@android.com8a1c16f2008-12-17 15:59:43 +00001351 this->updateTitle(); // to refresh our config
1352}
1353
1354///////////////////////////////////////////////////////////////////////////////
1355
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001356static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001357static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001358static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001359
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001360bool SampleView::IsSampleView(SkView* view) {
1361 SkEvent evt(is_sample_view_tag);
1362 return view->doQuery(&evt);
1363}
1364
reed@google.comf2183392011-04-22 14:10:48 +00001365bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001366 SkEvent evt(repeat_count_tag);
1367 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001368 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001369}
1370
reed@google.com0faac1e2011-05-11 05:58:58 +00001371bool SampleView::SetUsePipe(SkView* view, bool pred) {
1372 SkEvent evt(set_use_pipe_tag);
1373 evt.setFast32(pred);
1374 return view->doEvent(evt);
1375}
1376
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001377bool SampleView::onEvent(const SkEvent& evt) {
1378 if (evt.isType(repeat_count_tag)) {
1379 fRepeatCount = evt.getFast32();
1380 return true;
1381 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001382 if (evt.isType(set_use_pipe_tag)) {
1383 fUsePipe = !!evt.getFast32();
1384 return true;
1385 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001386 return this->INHERITED::onEvent(evt);
1387}
1388
1389bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001390 if (evt->isType(is_sample_view_tag)) {
1391 return true;
1392 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001393 return this->INHERITED::onQuery(evt);
1394}
1395
reed@google.com4aebe4f2011-05-05 14:07:35 +00001396#define TEST_GPIPEx
reed@google.com64e3eb22011-05-04 14:32:04 +00001397
reed@google.com68f456d2011-05-02 18:55:39 +00001398#ifdef TEST_GPIPE
1399 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001400
1401class SimplePC : public SkGPipeController {
1402public:
1403 SimplePC(SkCanvas* target);
1404 ~SimplePC();
1405
1406 virtual void* requestBlock(size_t minRequest, size_t* actual);
1407 virtual void notifyWritten(size_t bytes);
1408
1409private:
reed@google.com961ddb02011-05-05 14:03:48 +00001410 SkGPipeReader fReader;
1411 void* fBlock;
1412 size_t fBlockSize;
1413 size_t fBytesWritten;
1414 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001415 SkGPipeReader::Status fStatus;
1416
1417 size_t fTotalWritten;
1418};
1419
1420SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1421 fBlock = NULL;
1422 fBlockSize = fBytesWritten = 0;
1423 fStatus = SkGPipeReader::kDone_Status;
1424 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001425 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001426}
1427
1428SimplePC::~SimplePC() {
1429// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1430 sk_free(fBlock);
1431
reed@google.com0faac1e2011-05-11 05:58:58 +00001432 if (fTotalWritten) {
1433 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1434 fAtomsWritten, fStatus);
1435 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001436}
1437
1438void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1439 sk_free(fBlock);
1440
1441 fBlockSize = minRequest * 4;
1442 fBlock = sk_malloc_throw(fBlockSize);
1443 fBytesWritten = 0;
1444 *actual = fBlockSize;
1445 return fBlock;
1446}
1447
1448void SimplePC::notifyWritten(size_t bytes) {
1449 SkASSERT(fBytesWritten + bytes <= fBlockSize);
1450
1451 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1452 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1453 fBytesWritten += bytes;
1454 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001455
1456 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001457}
1458
reed@google.com68f456d2011-05-02 18:55:39 +00001459#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001460
reed@google.com64e3eb22011-05-04 14:32:04 +00001461
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001462void SampleView::onDraw(SkCanvas* canvas) {
1463 this->onDrawBackground(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001464
1465#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001466 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001467 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001468 if (fUsePipe) {
1469 canvas = writer.startRecording(&controller);
1470 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001471#endif
1472
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001473 for (int i = 0; i < fRepeatCount; i++) {
1474 SkAutoCanvasRestore acr(canvas, true);
1475 this->onDrawContent(canvas);
1476 }
1477}
1478
1479void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001480 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001481}
1482
1483///////////////////////////////////////////////////////////////////////////////
1484
reed@android.comf2b98d62010-12-20 18:26:13 +00001485template <typename T> void SkTBSort(T array[], int count) {
1486 for (int i = 1; i < count - 1; i++) {
1487 bool didSwap = false;
1488 for (int j = count - 1; j > i; --j) {
1489 if (array[j] < array[j-1]) {
1490 T tmp(array[j-1]);
1491 array[j-1] = array[j];
1492 array[j] = tmp;
1493 didSwap = true;
1494 }
1495 }
1496 if (!didSwap) {
1497 break;
1498 }
1499 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001500
reed@android.comf2b98d62010-12-20 18:26:13 +00001501 for (int k = 0; k < count - 1; k++) {
1502 SkASSERT(!(array[k+1] < array[k]));
1503 }
1504}
1505
1506#include "SkRandom.h"
1507
1508static void rand_rect(SkIRect* rect, SkRandom& rand) {
1509 int bits = 8;
1510 int shift = 32 - bits;
1511 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1512 rand.nextU() >> shift, rand.nextU() >> shift);
1513 rect->sort();
1514}
1515
1516static void dumpRect(const SkIRect& r) {
1517 SkDebugf(" { %d, %d, %d, %d },\n",
1518 r.fLeft, r.fTop,
1519 r.fRight, r.fBottom);
1520}
1521
1522static void test_rects(const SkIRect rect[], int count) {
1523 SkRegion rgn0, rgn1;
1524
1525 for (int i = 0; i < count; i++) {
1526 rgn0.op(rect[i], SkRegion::kUnion_Op);
1527 // dumpRect(rect[i]);
1528 }
1529 rgn1.setRects(rect, count);
1530
1531 if (rgn0 != rgn1) {
1532 SkDebugf("\n");
1533 for (int i = 0; i < count; i++) {
1534 dumpRect(rect[i]);
1535 }
1536 SkDebugf("\n");
1537 }
1538}
1539
1540static void test() {
1541 size_t i;
1542
1543 const SkIRect r0[] = {
1544 { 0, 0, 1, 1 },
1545 { 2, 2, 3, 3 },
1546 };
1547 const SkIRect r1[] = {
1548 { 0, 0, 1, 3 },
1549 { 1, 1, 2, 2 },
1550 { 2, 0, 3, 3 },
1551 };
1552 const SkIRect r2[] = {
1553 { 0, 0, 1, 2 },
1554 { 2, 1, 3, 3 },
1555 { 4, 0, 5, 1 },
1556 { 6, 0, 7, 4 },
1557 };
1558
1559 static const struct {
1560 const SkIRect* fRects;
1561 int fCount;
1562 } gRecs[] = {
1563 { r0, SK_ARRAY_COUNT(r0) },
1564 { r1, SK_ARRAY_COUNT(r1) },
1565 { r2, SK_ARRAY_COUNT(r2) },
1566 };
1567
1568 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1569 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1570 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001571
reed@android.comf2b98d62010-12-20 18:26:13 +00001572 SkRandom rand;
1573 for (i = 0; i < 10000; i++) {
1574 SkRegion rgn0, rgn1;
1575
1576 const int N = 8;
1577 SkIRect rect[N];
1578 for (int j = 0; j < N; j++) {
1579 rand_rect(&rect[j], rand);
1580 }
1581 test_rects(rect, N);
1582 }
1583}
1584
reed@android.com8a1c16f2008-12-17 15:59:43 +00001585SkOSWindow* create_sk_window(void* hwnd) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001586// test();
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001587 return new SampleWindow(hwnd);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001588}
1589
1590void get_preferred_size(int* x, int* y, int* width, int* height) {
1591 *x = 10;
1592 *y = 50;
1593 *width = 640;
1594 *height = 480;
1595}
1596
1597void application_init() {
1598// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001599#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001600 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001601#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001602 SkGraphics::Init();
1603 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001604}
1605
1606void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001607 SkEvent::Term();
1608 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001609}