blob: 76f4da1e2662a362719f44df74b7da1d2d2e9079 [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
reed@google.combad8c872011-05-18 20:10:31 +000028 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000029#else
reed@google.combad8c872011-05-18 20:10:31 +000030 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000031#endif
reed@google.combad8c872011-05-18 20:10:31 +000032#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000033
reed@google.comac10a2d2010-12-22 21:39:39 +000034#ifdef SK_SUPPORT_GL
35 #include "GrGLConfig.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000036#endif
37
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000038///////////////
39static const char view_inval_msg[] = "view-inval-msg";
40
41static void postInvalDelay(SkEventSinkID sinkID) {
42 SkEvent* evt = new SkEvent(view_inval_msg);
reed@google.comf2183392011-04-22 14:10:48 +000043 evt->post(sinkID, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000044}
45
46static bool isInvalEvent(const SkEvent& evt) {
47 return evt.isType(view_inval_msg);
48}
49//////////////////
50
reed@android.com8a1c16f2008-12-17 15:59:43 +000051SkViewRegister* SkViewRegister::gHead;
52SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) {
53 static bool gOnce;
54 if (!gOnce) {
55 gHead = NULL;
56 gOnce = true;
57 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000058
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 fChain = gHead;
60 gHead = this;
61}
62
reed@android.comf2b98d62010-12-20 18:26:13 +000063#if defined(SK_SUPPORT_GL)
64 #define SK_USE_SHADERS
65#endif
66
twiz@google.com06c3b6b2011-03-14 16:58:39 +000067#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +000068#include <CoreFoundation/CoreFoundation.h>
69#include <CoreFoundation/CFURLAccess.h>
70
71static void testpdf() {
72 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
73 kCFStringEncodingUTF8);
74 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
75 kCFURLPOSIXPathStyle,
76 false);
77 CFRelease(path);
78 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
79 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
80 CFRelease(url);
81
82 CGContextBeginPage(cg, &box);
83 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
84 CGContextFillEllipseInRect(cg, r);
85 CGContextEndPage(cg);
86 CGContextRelease(cg);
87
88 if (false) {
89 SkBitmap bm;
90 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
91 bm.allocPixels();
92 bm.eraseColor(0);
93
94 SkCanvas canvas(bm);
95
96 }
97}
98#endif
99
100//////////////////////////////////////////////////////////////////////////////
101
reed@google.com569e0432011-04-05 13:07:03 +0000102enum FlipAxisEnum {
103 kFlipAxis_X = (1 << 0),
104 kFlipAxis_Y = (1 << 1)
105};
106
107enum SkTriState {
108 kFalse_SkTriState,
109 kTrue_SkTriState,
110 kUnknown_SkTriState,
111};
112
113static SkTriState cycle_tristate(SkTriState state) {
114 static const SkTriState gCycle[] = {
115 /* kFalse_SkTriState -> */ kUnknown_SkTriState,
116 /* kTrue_SkTriState -> */ kFalse_SkTriState,
117 /* kUnknown_SkTriState -> */ kTrue_SkTriState,
118 };
119 return gCycle[state];
120}
121
reed@google.comf0b5f682011-03-11 20:08:25 +0000122#include "SkDrawFilter.h"
123
reed@google.com569e0432011-04-05 13:07:03 +0000124class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000125public:
reed@google.com09e3baa2011-05-18 12:04:31 +0000126 FlagsDrawFilter(SkTriState lcd, SkTriState aa, SkTriState filter,
127 SkTriState hinting) :
128 fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000129
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000130 virtual void filter(SkPaint* paint, Type t) {
reed@google.com569e0432011-04-05 13:07:03 +0000131 if (kText_Type == t && kUnknown_SkTriState != fLCDState) {
reed@google.com569e0432011-04-05 13:07:03 +0000132 paint->setLCDRenderText(kTrue_SkTriState == fLCDState);
133 }
134 if (kUnknown_SkTriState != fAAState) {
reed@google.com569e0432011-04-05 13:07:03 +0000135 paint->setAntiAlias(kTrue_SkTriState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000136 }
reed@google.com176753a2011-05-17 15:32:04 +0000137 if (kUnknown_SkTriState != fFilterState) {
138 paint->setFilterBitmap(kTrue_SkTriState == fFilterState);
139 }
reed@google.com09e3baa2011-05-18 12:04:31 +0000140 if (kUnknown_SkTriState != fHintingState) {
141 paint->setHinting(kTrue_SkTriState == fHintingState ?
142 SkPaint::kNormal_Hinting :
143 SkPaint::kSlight_Hinting);
144 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000145 }
146
147private:
reed@google.com569e0432011-04-05 13:07:03 +0000148 SkTriState fLCDState;
reed@google.com569e0432011-04-05 13:07:03 +0000149 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000150 SkTriState fFilterState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000151 SkTriState fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000152};
153
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154//////////////////////////////////////////////////////////////////////////////
155
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000156#define MAX_ZOOM_LEVEL 8
157#define MIN_ZOOM_LEVEL -8
158
reed@android.comf2b98d62010-12-20 18:26:13 +0000159static const char gCharEvtName[] = "SampleCode_Char_Event";
160static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161static const char gTitleEvtName[] = "SampleCode_Title_Event";
162static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000163static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
164
165bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
166 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
167 if (outUni) {
168 *outUni = evt.getFast32();
169 }
170 return true;
171 }
172 return false;
173}
174
175bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
176 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
177 if (outKey) {
178 *outKey = (SkKey)evt.getFast32();
179 }
180 return true;
181 }
182 return false;
183}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184
185bool SampleCode::TitleQ(const SkEvent& evt) {
186 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
187}
188
189void SampleCode::TitleR(SkEvent* evt, const char title[]) {
190 SkASSERT(evt && TitleQ(*evt));
191 evt->setString(gTitleEvtName, title);
192}
193
194bool SampleCode::PrefSizeQ(const SkEvent& evt) {
195 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
196}
197
198void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
199 SkASSERT(evt && PrefSizeQ(*evt));
200 SkScalar size[2];
201 size[0] = width;
202 size[1] = height;
203 evt->setScalars(gPrefSizeEvtName, 2, size);
204}
205
reed@android.comf2b98d62010-12-20 18:26:13 +0000206bool SampleCode::FastTextQ(const SkEvent& evt) {
207 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
208}
209
210///////////////////////////////////////////////////////////////////////////////
211
reed@android.com44177402009-11-23 21:07:51 +0000212static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000213static SkMSec gAnimTimePrev;
214
reed@android.com44177402009-11-23 21:07:51 +0000215SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000216SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
217SkScalar SampleCode::GetAnimSecondsDelta() {
218 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
219}
reed@android.com44177402009-11-23 21:07:51 +0000220
221SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000222 // since gAnimTime can be up to 32 bits, we can't convert it to a float
223 // or we'll lose the low bits. Hence we use doubles for the intermediate
224 // calculations
225 double seconds = (double)gAnimTime / 1000.0;
226 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000227 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000228 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000229 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000230 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000231}
232
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233//////////////////////////////////////////////////////////////////////////////
234
reed@android.comf2b98d62010-12-20 18:26:13 +0000235static SkView* curr_view(SkWindow* wind) {
236 SkView::F2BIter iter(wind);
237 return iter.next();
238}
239
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240class SampleWindow : public SkOSWindow {
reed@android.com34245c72009-11-03 04:00:48 +0000241 SkTDArray<SkViewFactory> fSamples;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242public:
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000243 SampleWindow(void* hwnd);
244 virtual ~SampleWindow();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245
reed@android.come522ca52009-11-23 20:10:41 +0000246 virtual void draw(SkCanvas* canvas);
247
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248protected:
249 virtual void onDraw(SkCanvas* canvas);
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000250 virtual bool onHandleKey(SkKey key);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251 virtual bool onHandleChar(SkUnichar);
252 virtual void onSizeChange();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000253
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254 virtual SkCanvas* beforeChildren(SkCanvas*);
255 virtual void afterChildren(SkCanvas*);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000256 virtual void beforeChild(SkView* child, SkCanvas* canvas);
257 virtual void afterChild(SkView* child, SkCanvas* canvas);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000258
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000259 virtual bool onEvent(const SkEvent& evt);
reed@android.comf2b98d62010-12-20 18:26:13 +0000260 virtual bool onQuery(SkEvent* evt);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000262 virtual bool onDispatchClick(int x, int y, Click::State);
reed@google.com52f57e12011-03-16 12:10:02 +0000263 virtual bool onClick(Click* click);
264 virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
265
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266#if 0
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000267 virtual bool handleChar(SkUnichar uni);
268 virtual bool handleEvent(const SkEvent& evt);
269 virtual bool handleKey(SkKey key);
270 virtual bool handleKeyUp(SkKey key);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271 virtual bool onHandleKeyUp(SkKey key);
272#endif
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000273
reed@android.com8a1c16f2008-12-17 15:59:43 +0000274private:
reed@android.com34245c72009-11-03 04:00:48 +0000275 int fCurrIndex;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000276
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277 SkPicture* fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000278 SkGpuCanvas* fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000279 GrContext* fGrContext;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280 SkPath fClipPath;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000281
reed@google.com52f57e12011-03-16 12:10:02 +0000282 SkTouchGesture fGesture;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000283 int fZoomLevel;
284 SkScalar fZoomScale;
reed@google.com52f57e12011-03-16 12:10:02 +0000285
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 enum CanvasType {
287 kRaster_CanvasType,
288 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000289 kGPU_CanvasType
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290 };
291 CanvasType fCanvasType;
292
293 bool fUseClip;
reed@android.come522ca52009-11-23 20:10:41 +0000294 bool fNClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295 bool fRepeatDrawing;
296 bool fAnimating;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000297 bool fRotate;
298 bool fScale;
reed@android.comf2b98d62010-12-20 18:26:13 +0000299 bool fRequestGrabImage;
reed@google.com0faac1e2011-05-11 05:58:58 +0000300 bool fUsePipe;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000301 bool fMeasureFPS;
302 SkMSec fMeasureFPS_Time;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000303
Scroggo0f185c22011-03-24 18:35:50 +0000304 // The following are for the 'fatbits' drawing
305 // Latest position of the mouse.
306 int fMouseX, fMouseY;
307 int fFatBitsScale;
308 // Used by the text showing position and color values.
309 SkTypeface* fTypeface;
310 bool fShowZoomer;
311
reed@google.com569e0432011-04-05 13:07:03 +0000312 SkTriState fLCDState;
313 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000314 SkTriState fFilterState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000315 SkTriState fHintingState;
reed@google.com569e0432011-04-05 13:07:03 +0000316 unsigned fFlipAxis;
reed@google.comf0b5f682011-03-11 20:08:25 +0000317
reed@android.com8a1c16f2008-12-17 15:59:43 +0000318 int fScrollTestX, fScrollTestY;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000319
320 bool make3DReady();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000321 void changeZoomLevel(int delta);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000322
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323 void loadView(SkView*);
324 void updateTitle();
325 bool nextSample();
326
Scroggo0f185c22011-03-24 18:35:50 +0000327 void toggleZoomer();
328 bool zoomIn();
329 bool zoomOut();
330 void updatePointer(int x, int y);
331
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332 void postAnimatingEvent() {
333 if (fAnimating) {
334 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
335 evt->post(this->getSinkID(), ANIMATING_DELAY);
336 }
337 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000338
339
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340 static CanvasType cycle_canvastype(CanvasType);
341
342 typedef SkOSWindow INHERITED;
343};
344
Scroggo0f185c22011-03-24 18:35:50 +0000345bool SampleWindow::zoomIn()
346{
347 // Arbitrarily decided
348 if (fFatBitsScale == 25) return false;
349 fFatBitsScale++;
350 this->inval(NULL);
351 return true;
352}
353
354bool SampleWindow::zoomOut()
355{
356 if (fFatBitsScale == 1) return false;
357 fFatBitsScale--;
358 this->inval(NULL);
359 return true;
360}
361
362void SampleWindow::toggleZoomer()
363{
364 fShowZoomer = !fShowZoomer;
365 this->inval(NULL);
366}
367
368void SampleWindow::updatePointer(int x, int y)
369{
370 fMouseX = x;
371 fMouseY = y;
372 if (fShowZoomer) {
373 this->inval(NULL);
374 }
375}
376
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000377bool SampleWindow::make3DReady() {
378
379#if defined(SK_SUPPORT_GL)
bsalomon@google.com498a6232011-03-10 18:24:15 +0000380 if (attachGL()) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000381 if (NULL != fGrContext) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000382 // various gr lifecycle tests
383 #if 0
384 fGrContext->freeGpuResources();
385 #elif 0
386 // this will leak resources.
387 fGrContext->contextLost();
388 #elif 0
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000389 GrAssert(1 == fGrContext->refcnt());
390 fGrContext->unref();
391 fGrContext = NULL;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000392 #endif
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000393 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000394
bsalomon@google.com498a6232011-03-10 18:24:15 +0000395 if (NULL == fGrContext) {
396 #if defined(SK_USE_SHADERS)
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000397 fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000398 #else
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000399 fGrContext = GrContext::Create(kOpenGL_Fixed_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000400 #endif
reed@google.com569e0432011-04-05 13:07:03 +0000401 SkDebugf("---- constructor\n");
bsalomon@google.com498a6232011-03-10 18:24:15 +0000402 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000403
bsalomon@google.com498a6232011-03-10 18:24:15 +0000404 if (NULL != fGrContext) {
405 return true;
406 } else {
407 detachGL();
408 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000409 }
410#endif
411 SkDebugf("Failed to setup 3D");
412 return false;
413}
414
reed@android.com8a1c16f2008-12-17 15:59:43 +0000415SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) {
416 static const CanvasType gCT[] = {
417 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000418 kGPU_CanvasType,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000419 kRaster_CanvasType
420 };
421 return gCT[ct];
422}
423
424SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000425 fPicture = NULL;
reed@android.comf2b98d62010-12-20 18:26:13 +0000426 fGpuCanvas = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000428 fGrContext = NULL;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000429
reed@android.comf2b98d62010-12-20 18:26:13 +0000430#ifdef DEFAULT_TO_GPU
431 fCanvasType = kGPU_CanvasType;
432#else
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433 fCanvasType = kRaster_CanvasType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000434#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000435 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000436 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000437 fRepeatDrawing = false;
438 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000439 fRotate = false;
440 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000441 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000442 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000443 fMeasureFPS = false;
reed@google.com569e0432011-04-05 13:07:03 +0000444 fLCDState = kUnknown_SkTriState;
445 fAAState = kUnknown_SkTriState;
reed@google.com66f22fd2011-05-17 15:33:45 +0000446 fFilterState = kUnknown_SkTriState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000447 fHintingState = kUnknown_SkTriState;
reed@google.com569e0432011-04-05 13:07:03 +0000448 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000449 fScrollTestX = fScrollTestY = 0;
450
Scroggo0f185c22011-03-24 18:35:50 +0000451 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000452 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000453 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
454 fShowZoomer = false;
455
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000456 fZoomLevel = 0;
457 fZoomScale = SK_Scalar1;
458
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000459// this->setConfig(SkBitmap::kRGB_565_Config);
460 this->setConfig(SkBitmap::kARGB_8888_Config);
461 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000462 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000463
reed@android.com34245c72009-11-03 04:00:48 +0000464 {
465 const SkViewRegister* reg = SkViewRegister::Head();
466 while (reg) {
467 *fSamples.append() = reg->factory();
468 reg = reg->next();
469 }
470 }
471 fCurrIndex = 0;
reed@android.come0f13ee2009-11-04 19:40:25 +0000472 this->loadView(fSamples[fCurrIndex]());
reed@google.comf0b5f682011-03-11 20:08:25 +0000473
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000474#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +0000475 testpdf();
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000476#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000477}
478
479SampleWindow::~SampleWindow() {
480 delete fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000481 delete fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000482 if (NULL != fGrContext) {
483 fGrContext->unref();
484 }
Scroggo0f185c22011-03-24 18:35:50 +0000485 fTypeface->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000486}
487
reed@android.com55e76b22009-11-23 21:46:47 +0000488static SkBitmap capture_bitmap(SkCanvas* canvas) {
489 SkBitmap bm;
490 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
491 src.copyTo(&bm, src.config());
492 return bm;
493}
494
495static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
496 SkBitmap* diff) {
497 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000498
reed@android.com55e76b22009-11-23 21:46:47 +0000499 SkAutoLockPixels alp0(src);
500 SkAutoLockPixels alp1(orig);
501 for (int y = 0; y < src.height(); y++) {
502 const void* srcP = src.getAddr(0, y);
503 const void* origP = orig.getAddr(0, y);
504 size_t bytes = src.width() * src.bytesPerPixel();
505 if (memcmp(srcP, origP, bytes)) {
506 SkDebugf("---------- difference on line %d\n", y);
507 return true;
508 }
509 }
510 return false;
511}
512
Scroggo0f185c22011-03-24 18:35:50 +0000513static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
514{
515 SkColor desiredColor = paint.getColor();
516 paint.setColor(SK_ColorWHITE);
517 const char* c_str = string.c_str();
518 size_t size = string.size();
519 SkRect bounds;
520 paint.measureText(c_str, size, &bounds);
521 bounds.offset(left, top);
522 SkScalar inset = SkIntToScalar(-2);
523 bounds.inset(inset, inset);
524 canvas->drawRect(bounds, paint);
525 if (desiredColor != SK_ColorBLACK) {
526 paint.setColor(SK_ColorBLACK);
527 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
528 }
529 paint.setColor(desiredColor);
530 canvas->drawText(c_str, size, left, top, paint);
531}
532
reed@android.com44177402009-11-23 21:07:51 +0000533#define XCLIP_N 8
534#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000535
536void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000537 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000538 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000539 gAnimTime = SkTime::GetMSecs();
540
reed@google.com569e0432011-04-05 13:07:03 +0000541 SkScalar cx = SkScalarHalf(this->width());
542 SkScalar cy = SkScalarHalf(this->height());
543
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000544 if (fZoomLevel) {
545 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000546 SkPoint center;
547 m = canvas->getTotalMatrix();//.invert(&m);
548 m.mapXY(cx, cy, &center);
549 cx = center.fX;
550 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000551
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000552 m.setTranslate(-cx, -cy);
553 m.postScale(fZoomScale, fZoomScale);
554 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000555
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000556 canvas->concat(m);
557 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000558
reed@google.com569e0432011-04-05 13:07:03 +0000559 if (fFlipAxis) {
560 SkMatrix m;
561 m.setTranslate(cx, cy);
562 if (fFlipAxis & kFlipAxis_X) {
563 m.preScale(-SK_Scalar1, SK_Scalar1);
564 }
565 if (fFlipAxis & kFlipAxis_Y) {
566 m.preScale(SK_Scalar1, -SK_Scalar1);
567 }
568 m.preTranslate(-cx, -cy);
569 canvas->concat(m);
570 }
571
reed@google.com52f57e12011-03-16 12:10:02 +0000572 // Apply any gesture matrix
573 if (true) {
574 const SkMatrix& localM = fGesture.localM();
575 if (localM.getType() & SkMatrix::kScale_Mask) {
576 canvas->setExternalMatrix(&localM);
577 }
578 canvas->concat(localM);
579 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000580
reed@google.com52f57e12011-03-16 12:10:02 +0000581 if (fGesture.isActive()) {
582 this->inval(NULL);
583 }
584 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000585
reed@android.come522ca52009-11-23 20:10:41 +0000586 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000587 this->INHERITED::draw(canvas);
588 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000589
590 const SkScalar w = this->width();
591 const SkScalar h = this->height();
592 const SkScalar cw = w / XCLIP_N;
593 const SkScalar ch = h / YCLIP_N;
594 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000595 SkRect r;
596 r.fTop = y * ch;
597 r.fBottom = (y + 1) * ch;
598 if (y == YCLIP_N - 1) {
599 r.fBottom = h;
600 }
reed@android.come522ca52009-11-23 20:10:41 +0000601 for (int x = 0; x < XCLIP_N; x++) {
602 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000603 r.fLeft = x * cw;
604 r.fRight = (x + 1) * cw;
605 if (x == XCLIP_N - 1) {
606 r.fRight = w;
607 }
reed@android.come522ca52009-11-23 20:10:41 +0000608 canvas->clipRect(r);
609 this->INHERITED::draw(canvas);
610 }
611 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000612
reed@android.com55e76b22009-11-23 21:46:47 +0000613 SkBitmap diff;
614 if (bitmap_diff(canvas, orig, &diff)) {
615 }
reed@android.come522ca52009-11-23 20:10:41 +0000616 } else {
617 this->INHERITED::draw(canvas);
618 }
Scroggo0f185c22011-03-24 18:35:50 +0000619 if (fShowZoomer) {
620 int count = canvas->save();
621 canvas->resetMatrix();
622 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000623 int width = SkScalarRound(this->width());
624 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000625 if (fMouseX >= width) fMouseX = width - 1;
626 else if (fMouseX < 0) fMouseX = 0;
627 if (fMouseY >= height) fMouseY = height - 1;
628 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000629
Scroggo0f185c22011-03-24 18:35:50 +0000630 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000631 bitmap.lockPixels();
632
Scroggo0f185c22011-03-24 18:35:50 +0000633 // 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 +0000634 int zoomedWidth = (width >> 1) | 1;
635 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000636 SkIRect src;
637 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
638 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
639 SkRect dest;
640 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
641 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
642 SkPaint paint;
643 // Clear the background behind our zoomed in view
644 paint.setColor(SK_ColorWHITE);
645 canvas->drawRect(dest, paint);
646 canvas->drawBitmapRect(bitmap, &src, dest);
647 paint.setColor(SK_ColorBLACK);
648 paint.setStyle(SkPaint::kStroke_Style);
649 // Draw a border around the pixel in the middle
650 SkRect originalPixel;
651 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
652 SkMatrix matrix;
653 SkRect scalarSrc;
654 scalarSrc.set(src);
655 SkColor color = bitmap.getColor(fMouseX, fMouseY);
656 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
657 SkRect pixel;
658 matrix.mapRect(&pixel, originalPixel);
659 // TODO Perhaps measure the values and make the outline white if it's "dark"
660 if (color == SK_ColorBLACK) {
661 paint.setColor(SK_ColorWHITE);
662 }
663 canvas->drawRect(pixel, paint);
664 }
665 paint.setColor(SK_ColorBLACK);
666 // Draw a border around the destination rectangle
667 canvas->drawRect(dest, paint);
668 paint.setStyle(SkPaint::kStrokeAndFill_Style);
669 // Identify the pixel and its color on screen
670 paint.setTypeface(fTypeface);
671 paint.setAntiAlias(true);
672 SkScalar lineHeight = paint.getFontMetrics(NULL);
673 SkString string;
674 string.appendf("(%i, %i)", fMouseX, fMouseY);
675 SkScalar left = dest.fLeft + SkIntToScalar(3);
676 SkScalar i = SK_Scalar1;
677 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
678 // Alpha
679 i += SK_Scalar1;
680 string.reset();
681 string.appendf("A: %X", SkColorGetA(color));
682 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
683 // Red
684 i += SK_Scalar1;
685 string.reset();
686 string.appendf("R: %X", SkColorGetR(color));
687 paint.setColor(SK_ColorRED);
688 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
689 // Green
690 i += SK_Scalar1;
691 string.reset();
692 string.appendf("G: %X", SkColorGetG(color));
693 paint.setColor(SK_ColorGREEN);
694 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
695 // Blue
696 i += SK_Scalar1;
697 string.reset();
698 string.appendf("B: %X", SkColorGetB(color));
699 paint.setColor(SK_ColorBLUE);
700 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
701 canvas->restoreToCount(count);
702 }
reed@android.come522ca52009-11-23 20:10:41 +0000703}
704
reed@android.com8a1c16f2008-12-17 15:59:43 +0000705void SampleWindow::onDraw(SkCanvas* canvas) {
706 if (fRepeatDrawing) {
707 this->inval(NULL);
708 }
709}
710
711#include "SkColorPriv.h"
712
713static void reverseRedAndBlue(const SkBitmap& bm) {
714 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
715 uint8_t* p = (uint8_t*)bm.getPixels();
716 uint8_t* stop = p + bm.getSize();
717 while (p < stop) {
718 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
719 unsigned scale = SkAlpha255To256(p[3]);
720 unsigned r = p[2];
721 unsigned b = p[0];
722 p[0] = SkAlphaMul(r, scale);
723 p[1] = SkAlphaMul(p[1], scale);
724 p[2] = SkAlphaMul(b, scale);
725 p += 4;
726 }
727}
728
729SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000730 if (kGPU_CanvasType != fCanvasType) {
reed@android.com6efdc472008-12-19 18:24:35 +0000731#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000732 detachGL();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000733#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000734 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000735
reed@android.com8a1c16f2008-12-17 15:59:43 +0000736 switch (fCanvasType) {
737 case kRaster_CanvasType:
738 canvas = this->INHERITED::beforeChildren(canvas);
739 break;
740 case kPicture_CanvasType:
741 fPicture = new SkPicture;
742 canvas = fPicture->beginRecording(9999, 9999);
743 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000744 case kGPU_CanvasType: {
reed@google.com64e3eb22011-05-04 14:32:04 +0000745 if (make3DReady()) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000746 SkDevice* device = canvas->getDevice();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000747 const SkBitmap& bitmap = device->accessBitmap(true);
748
bsalomon@google.com5782d712011-01-21 21:03:59 +0000749 GrRenderTarget* renderTarget;
750 renderTarget = fGrContext->createRenderTargetFrom3DApiState();
751 fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
752 renderTarget->unref();
753
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000754 device = fGpuCanvas->createDevice(SkBitmap::kARGB_8888_Config,
755 bitmap.width(), bitmap.height(),
756 false, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000757 fGpuCanvas->setDevice(device)->unref();
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000758
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000759 fGpuCanvas->concat(canvas->getTotalMatrix());
reed@android.comf2b98d62010-12-20 18:26:13 +0000760 canvas = fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000761
reed@android.comf2b98d62010-12-20 18:26:13 +0000762 } else {
763 canvas = this->INHERITED::beforeChildren(canvas);
764 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000765 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000766 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000767 }
768
769 if (fUseClip) {
770 canvas->drawColor(0xFFFF88FF);
771 canvas->clipPath(fClipPath);
772 }
773
774 return canvas;
775}
776
777static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
778 const SkRegion& rgn) {
779 SkCanvas canvas(bm);
780 SkRegion inval(rgn);
781
782 inval.translate(r.fLeft, r.fTop);
783 canvas.clipRegion(inval);
784 canvas.drawColor(0xFFFF8080);
785}
786
787void SampleWindow::afterChildren(SkCanvas* orig) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000788 if (fRequestGrabImage) {
789 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000790
reed@android.comf2b98d62010-12-20 18:26:13 +0000791 SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
792 SkDevice* device = canvas->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000793 SkBitmap bmp;
794 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000795 static int gSampleGrabCounter;
796 SkString name;
797 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000798 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000799 SkImageEncoder::kPNG_Type, 100);
800 }
801 }
802
reed@android.com8a1c16f2008-12-17 15:59:43 +0000803 switch (fCanvasType) {
804 case kRaster_CanvasType:
805 break;
806 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000807 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000808 SkPicture* pict = new SkPicture(*fPicture);
809 fPicture->unref();
810 orig->drawPicture(*pict);
811 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000812 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000813 SkDynamicMemoryWStream ostream;
814 fPicture->serialize(&ostream);
815 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000816
reed@android.com8a1c16f2008-12-17 15:59:43 +0000817 SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
818 SkPicture pict(&istream);
819 orig->drawPicture(pict);
820 } else {
821 fPicture->draw(orig);
822 fPicture->unref();
823 }
824 fPicture = NULL;
825 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000826#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000827 case kGPU_CanvasType:
828 delete fGpuCanvas;
829 fGpuCanvas = NULL;
830 presentGL();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000831 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000832#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000833 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000834
reed@google.com17d7aec2011-04-25 14:31:44 +0000835 // Do this after presentGL and other finishing, rather than in afterChild
836 if (fMeasureFPS && fMeasureFPS_Time) {
837 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
838 this->updateTitle();
839 postInvalDelay(this->getSinkID());
840 }
841
842 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000843 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000844 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
845 int dx = fScrollTestX * 7;
846 int dy = fScrollTestY * 7;
847 SkIRect r;
848 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000849
reed@android.com8a1c16f2008-12-17 15:59:43 +0000850 r.set(50, 50, 50+100, 50+100);
851 bm.scrollRect(&r, dx, dy, &inval);
852 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000853 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000854}
855
reed@android.com6c5f6f22009-08-14 16:08:38 +0000856void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
857 if (fScale) {
858 SkScalar scale = SK_Scalar1 * 7 / 10;
859 SkScalar cx = this->width() / 2;
860 SkScalar cy = this->height() / 2;
861 canvas->translate(cx, cy);
862 canvas->scale(scale, scale);
863 canvas->translate(-cx, -cy);
864 }
865 if (fRotate) {
866 SkScalar cx = this->width() / 2;
867 SkScalar cy = this->height() / 2;
868 canvas->translate(cx, cy);
869 canvas->rotate(SkIntToScalar(30));
870 canvas->translate(-cx, -cy);
871 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000872
reed@google.com09e3baa2011-05-18 12:04:31 +0000873 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
874 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000875
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000876 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000877 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
878 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
879 fMeasureFPS_Time = SkTime::GetMSecs();
880 }
881 } else {
882 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000883 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000884 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000885}
886
887void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000888 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000889}
890
reed@android.com8a1c16f2008-12-17 15:59:43 +0000891static SkBitmap::Config gConfigCycle[] = {
892 SkBitmap::kNo_Config, // none -> none
893 SkBitmap::kNo_Config, // a1 -> none
894 SkBitmap::kNo_Config, // a8 -> none
895 SkBitmap::kNo_Config, // index8 -> none
896 SkBitmap::kARGB_4444_Config, // 565 -> 4444
897 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
898 SkBitmap::kRGB_565_Config // 8888 -> 565
899};
900
901static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
902 return gConfigCycle[c];
903}
904
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000905void SampleWindow::changeZoomLevel(int delta) {
906 fZoomLevel += delta;
907 if (fZoomLevel > 0) {
908 fZoomLevel = SkMin32(fZoomLevel, MAX_ZOOM_LEVEL);
909 fZoomScale = SkIntToScalar(fZoomLevel + 1);
910 } else if (fZoomLevel < 0) {
911 fZoomLevel = SkMax32(fZoomLevel, MIN_ZOOM_LEVEL);
912 fZoomScale = SK_Scalar1 / (1 - fZoomLevel);
913 } else {
914 fZoomScale = SK_Scalar1;
915 }
916
917 this->inval(NULL);
918}
919
reed@android.com8a1c16f2008-12-17 15:59:43 +0000920bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000921 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
922 this->loadView(fSamples[fCurrIndex]());
923 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000924}
925
926bool SampleWindow::onEvent(const SkEvent& evt) {
927 if (evt.isType(ANIMATING_EVENTTYPE)) {
928 if (fAnimating) {
929 this->nextSample();
930 this->postAnimatingEvent();
931 }
932 return true;
933 }
reed@android.com34245c72009-11-03 04:00:48 +0000934 if (evt.isType("set-curr-index")) {
935 fCurrIndex = evt.getFast32() % fSamples.count();
936 this->loadView(fSamples[fCurrIndex]());
937 return true;
938 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000939 if (isInvalEvent(evt)) {
940 this->inval(NULL);
941 return true;
942 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000943 return this->INHERITED::onEvent(evt);
944}
945
reed@android.comf2b98d62010-12-20 18:26:13 +0000946bool SampleWindow::onQuery(SkEvent* query) {
947 if (query->isType("get-slide-count")) {
948 query->setFast32(fSamples.count());
949 return true;
950 }
951 if (query->isType("get-slide-title")) {
952 SkView* view = fSamples[query->getFast32()]();
953 SkEvent evt(gTitleEvtName);
954 if (view->doQuery(&evt)) {
955 query->setString("title", evt.findString(gTitleEvtName));
956 }
957 SkSafeUnref(view);
958 return true;
959 }
960 if (query->isType("use-fast-text")) {
961 SkEvent evt(gFastTextEvtName);
962 return curr_view(this)->doQuery(&evt);
963 }
964 return this->INHERITED::onQuery(query);
965}
966
reed@android.com0ae6b242008-12-23 16:49:54 +0000967static void cleanup_for_filename(SkString* name) {
968 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +0000969 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +0000970 switch (str[i]) {
971 case ':': str[i] = '-'; break;
972 case '/': str[i] = '-'; break;
973 case ' ': str[i] = '_'; break;
974 default: break;
975 }
976 }
977}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000978
979bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000980 {
981 SkView* view = curr_view(this);
982 if (view) {
983 SkEvent evt(gCharEvtName);
984 evt.setFast32(uni);
985 if (view->doQuery(&evt)) {
986 return true;
987 }
988 }
989 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000990
reed@android.com8a1c16f2008-12-17 15:59:43 +0000991 int dx = 0xFF;
992 int dy = 0xFF;
993
994 switch (uni) {
995 case '5': dx = 0; dy = 0; break;
996 case '8': dx = 0; dy = -1; break;
997 case '6': dx = 1; dy = 0; break;
998 case '2': dx = 0; dy = 1; break;
999 case '4': dx = -1; dy = 0; break;
1000 case '7': dx = -1; dy = -1; break;
1001 case '9': dx = 1; dy = -1; break;
1002 case '3': dx = 1; dy = 1; break;
1003 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001004
reed@android.com8a1c16f2008-12-17 15:59:43 +00001005 default:
1006 break;
1007 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001008
reed@android.com8a1c16f2008-12-17 15:59:43 +00001009 if (0xFF != dx && 0xFF != dy) {
1010 if ((dx | dy) == 0) {
1011 fScrollTestX = fScrollTestY = 0;
1012 } else {
1013 fScrollTestX += dx;
1014 fScrollTestY += dy;
1015 }
1016 this->inval(NULL);
1017 return true;
1018 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001019
reed@android.com0ae6b242008-12-23 16:49:54 +00001020 switch (uni) {
1021 case 'a':
1022 fAnimating = !fAnimating;
1023 this->postAnimatingEvent();
1024 this->updateTitle();
1025 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001026 case 'b':
1027 fAAState = cycle_tristate(fAAState);
1028 this->updateTitle();
1029 this->inval(NULL);
1030 break;
1031 case 'c':
1032 fUseClip = !fUseClip;
1033 this->inval(NULL);
1034 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001035 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001036 case 'd':
1037 SkGraphics::SetFontCacheUsed(0);
1038 return true;
1039 case 'f':
1040 fMeasureFPS = !fMeasureFPS;
1041 this->inval(NULL);
1042 break;
1043 case 'g':
1044 fRequestGrabImage = true;
1045 this->inval(NULL);
1046 break;
reed@google.com09e3baa2011-05-18 12:04:31 +00001047 case 'h':
1048 fHintingState = cycle_tristate(fHintingState);
1049 this->updateTitle();
1050 this->inval(NULL);
1051 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001052 case 'i':
1053 this->zoomIn();
1054 break;
1055 case 'l':
1056 fLCDState = cycle_tristate(fLCDState);
1057 this->updateTitle();
1058 this->inval(NULL);
1059 break;
reed@google.com176753a2011-05-17 15:32:04 +00001060 case 'n':
1061 fFilterState = cycle_tristate(fFilterState);
1062 this->updateTitle();
1063 this->inval(NULL);
1064 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001065 case 'o':
1066 this->zoomOut();
1067 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001068 case 'p':
1069 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001070 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001071 this->inval(NULL);
1072 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001073 case 'r':
1074 fRotate = !fRotate;
1075 this->inval(NULL);
1076 this->updateTitle();
1077 return true;
1078 case 's':
1079 fScale = !fScale;
1080 this->inval(NULL);
1081 this->updateTitle();
1082 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001083 case 'x':
1084 fFlipAxis ^= kFlipAxis_X;
1085 this->updateTitle();
1086 this->inval(NULL);
1087 break;
1088 case 'y':
1089 fFlipAxis ^= kFlipAxis_Y;
1090 this->updateTitle();
1091 this->inval(NULL);
1092 break;
scroggo08526c02011-03-22 14:03:21 +00001093 case 'z':
1094 this->toggleZoomer();
1095 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001096 default:
1097 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001098 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001099
reed@android.com8a1c16f2008-12-17 15:59:43 +00001100 return this->INHERITED::onHandleChar(uni);
1101}
1102
1103#include "SkDumpCanvas.h"
1104
1105bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001106 {
1107 SkView* view = curr_view(this);
1108 if (view) {
1109 SkEvent evt(gKeyEvtName);
1110 evt.setFast32(key);
1111 if (view->doQuery(&evt)) {
1112 return true;
1113 }
1114 }
1115 }
1116
reed@android.com8a1c16f2008-12-17 15:59:43 +00001117 switch (key) {
1118 case kRight_SkKey:
1119 if (this->nextSample()) {
1120 return true;
1121 }
1122 break;
1123 case kLeft_SkKey:
1124 fCanvasType = cycle_canvastype(fCanvasType);
1125 this->updateTitle();
1126 this->inval(NULL);
1127 return true;
1128 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001129 if (USE_ARROWS_FOR_ZOOM) {
1130 this->changeZoomLevel(1);
1131 } else {
1132 fNClip = !fNClip;
1133 this->inval(NULL);
1134 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001135 this->updateTitle();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001136 return true;
1137 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001138 if (USE_ARROWS_FOR_ZOOM) {
1139 this->changeZoomLevel(-1);
1140 } else {
1141 this->setConfig(cycle_configs(this->getBitmap().config()));
1142 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001143 this->updateTitle();
1144 return true;
1145 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001146 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001147 SkDebugfDumper dumper;
1148 SkDumpCanvas dc(&dumper);
1149 this->draw(&dc);
1150 } else {
1151 fRepeatDrawing = !fRepeatDrawing;
1152 if (fRepeatDrawing) {
1153 this->inval(NULL);
1154 }
1155 }
1156 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001157 case kBack_SkKey:
1158 this->loadView(NULL);
1159 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001160 default:
1161 break;
1162 }
1163 return this->INHERITED::onHandleKey(key);
1164}
1165
reed@google.com52f57e12011-03-16 12:10:02 +00001166///////////////////////////////////////////////////////////////////////////////
1167
1168static const char gGestureClickType[] = "GestureClickType";
1169
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001170bool SampleWindow::onDispatchClick(int x, int y, Click::State state) {
Scroggo0f185c22011-03-24 18:35:50 +00001171 if (Click::kMoved_State == state) {
1172 updatePointer(x, y);
1173 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001174 int w = SkScalarRound(this->width());
1175 int h = SkScalarRound(this->height());
1176
1177 // check for the resize-box
1178 if (w - x < 16 && h - y < 16) {
1179 return false; // let the OS handle the click
1180 } else {
1181 return this->INHERITED::onDispatchClick(x, y, state);
1182 }
1183}
1184
reed@google.com52f57e12011-03-16 12:10:02 +00001185class GestureClick : public SkView::Click {
1186public:
1187 GestureClick(SkView* target) : SkView::Click(target) {
1188 this->setType(gGestureClickType);
1189 }
1190
1191 static bool IsGesture(Click* click) {
1192 return click->isType(gGestureClickType);
1193 }
1194};
1195
1196SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1197 return new GestureClick(this);
1198}
1199
1200bool SampleWindow::onClick(Click* click) {
1201 if (GestureClick::IsGesture(click)) {
1202 float x = SkScalarToFloat(click->fCurr.fX);
1203 float y = SkScalarToFloat(click->fCurr.fY);
1204 switch (click->fState) {
1205 case SkView::Click::kDown_State:
1206 fGesture.touchBegin(click, x, y);
1207 break;
1208 case SkView::Click::kMoved_State:
1209 fGesture.touchMoved(click, x, y);
1210 this->inval(NULL);
1211 break;
1212 case SkView::Click::kUp_State:
1213 fGesture.touchEnd(click);
1214 this->inval(NULL);
1215 break;
1216 }
1217 return true;
1218 }
1219 return false;
1220}
1221
1222///////////////////////////////////////////////////////////////////////////////
1223
reed@android.com8a1c16f2008-12-17 15:59:43 +00001224void SampleWindow::loadView(SkView* view) {
1225 SkView::F2BIter iter(this);
1226 SkView* prev = iter.next();
1227 if (prev) {
1228 prev->detachFromParent();
1229 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001230
reed@android.com34245c72009-11-03 04:00:48 +00001231 if (NULL == view) {
1232 view = create_overview(fSamples.count(), fSamples.begin());
1233 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001234 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001235 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001236 this->attachChildToFront(view)->unref();
1237 view->setSize(this->width(), this->height());
1238
1239 this->updateTitle();
1240}
1241
1242static const char* gConfigNames[] = {
1243 "unknown config",
1244 "A1",
1245 "A8",
1246 "Index8",
1247 "565",
1248 "4444",
1249 "8888"
1250};
1251
1252static const char* configToString(SkBitmap::Config c) {
1253 return gConfigNames[c];
1254}
1255
1256static const char* gCanvasTypePrefix[] = {
1257 "raster: ",
1258 "picture: ",
1259 "opengl: "
1260};
1261
reed@google.com569e0432011-04-05 13:07:03 +00001262static const char* trystate_str(SkTriState state,
1263 const char trueStr[], const char falseStr[]) {
1264 if (kTrue_SkTriState == state) {
1265 return trueStr;
1266 } else if (kFalse_SkTriState == state) {
1267 return falseStr;
1268 }
1269 return NULL;
1270}
1271
reed@android.com8a1c16f2008-12-17 15:59:43 +00001272void SampleWindow::updateTitle() {
1273 SkString title;
1274
1275 SkView::F2BIter iter(this);
1276 SkView* view = iter.next();
1277 SkEvent evt(gTitleEvtName);
1278 if (view->doQuery(&evt)) {
1279 title.set(evt.findString(gTitleEvtName));
1280 }
1281 if (title.size() == 0) {
1282 title.set("<unknown>");
1283 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001284
reed@android.com8a1c16f2008-12-17 15:59:43 +00001285 title.prepend(gCanvasTypePrefix[fCanvasType]);
1286
1287 title.prepend(" ");
1288 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001289
reed@android.com8a1c16f2008-12-17 15:59:43 +00001290 if (fAnimating) {
1291 title.prepend("<A> ");
1292 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001293 if (fScale) {
1294 title.prepend("<S> ");
1295 }
1296 if (fRotate) {
1297 title.prepend("<R> ");
1298 }
reed@android.come522ca52009-11-23 20:10:41 +00001299 if (fNClip) {
1300 title.prepend("<C> ");
1301 }
reed@google.com569e0432011-04-05 13:07:03 +00001302
1303 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1304 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001305 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001306 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1307 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001308
1309 if (fZoomLevel) {
1310 title.prependf("{%d} ", fZoomLevel);
1311 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001312
1313 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001314 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001315 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001316 if (fUsePipe && SampleView::IsSampleView(view)) {
1317 title.prepend("<P> ");
1318 }
1319 if (SampleView::IsSampleView(view)) {
1320 title.prepend("! ");
1321 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001322
reed@android.com8a1c16f2008-12-17 15:59:43 +00001323 this->setTitle(title.c_str());
1324}
1325
1326void SampleWindow::onSizeChange() {
1327 this->INHERITED::onSizeChange();
1328
1329 SkView::F2BIter iter(this);
1330 SkView* view = iter.next();
1331 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001332
reed@android.com8a1c16f2008-12-17 15:59:43 +00001333 // rebuild our clippath
1334 {
1335 const SkScalar W = this->width();
1336 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001337
reed@android.com8a1c16f2008-12-17 15:59:43 +00001338 fClipPath.reset();
1339#if 0
1340 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1341 SkRect r;
1342 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1343 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1344 fClipPath.addRect(r);
1345 }
1346#else
1347 SkRect r;
1348 r.set(0, 0, W, H);
1349 fClipPath.addRect(r, SkPath::kCCW_Direction);
1350 r.set(W/4, H/4, W*3/4, H*3/4);
1351 fClipPath.addRect(r, SkPath::kCW_Direction);
1352#endif
1353 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001354
reed@android.com8a1c16f2008-12-17 15:59:43 +00001355 this->updateTitle(); // to refresh our config
1356}
1357
1358///////////////////////////////////////////////////////////////////////////////
1359
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001360static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001361static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001362static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001363
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001364bool SampleView::IsSampleView(SkView* view) {
1365 SkEvent evt(is_sample_view_tag);
1366 return view->doQuery(&evt);
1367}
1368
reed@google.comf2183392011-04-22 14:10:48 +00001369bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001370 SkEvent evt(repeat_count_tag);
1371 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001372 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001373}
1374
reed@google.com0faac1e2011-05-11 05:58:58 +00001375bool SampleView::SetUsePipe(SkView* view, bool pred) {
1376 SkEvent evt(set_use_pipe_tag);
1377 evt.setFast32(pred);
1378 return view->doEvent(evt);
1379}
1380
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001381bool SampleView::onEvent(const SkEvent& evt) {
1382 if (evt.isType(repeat_count_tag)) {
1383 fRepeatCount = evt.getFast32();
1384 return true;
1385 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001386 if (evt.isType(set_use_pipe_tag)) {
1387 fUsePipe = !!evt.getFast32();
1388 return true;
1389 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001390 return this->INHERITED::onEvent(evt);
1391}
1392
1393bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001394 if (evt->isType(is_sample_view_tag)) {
1395 return true;
1396 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001397 return this->INHERITED::onQuery(evt);
1398}
1399
reed@google.com4aebe4f2011-05-05 14:07:35 +00001400#define TEST_GPIPEx
reed@google.com64e3eb22011-05-04 14:32:04 +00001401
reed@google.com68f456d2011-05-02 18:55:39 +00001402#ifdef TEST_GPIPE
1403 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001404
1405class SimplePC : public SkGPipeController {
1406public:
1407 SimplePC(SkCanvas* target);
1408 ~SimplePC();
1409
1410 virtual void* requestBlock(size_t minRequest, size_t* actual);
1411 virtual void notifyWritten(size_t bytes);
1412
1413private:
reed@google.com961ddb02011-05-05 14:03:48 +00001414 SkGPipeReader fReader;
1415 void* fBlock;
1416 size_t fBlockSize;
1417 size_t fBytesWritten;
1418 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001419 SkGPipeReader::Status fStatus;
1420
1421 size_t fTotalWritten;
1422};
1423
1424SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1425 fBlock = NULL;
1426 fBlockSize = fBytesWritten = 0;
1427 fStatus = SkGPipeReader::kDone_Status;
1428 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001429 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001430}
1431
1432SimplePC::~SimplePC() {
1433// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1434 sk_free(fBlock);
1435
reed@google.com0faac1e2011-05-11 05:58:58 +00001436 if (fTotalWritten) {
1437 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1438 fAtomsWritten, fStatus);
1439 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001440}
1441
1442void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1443 sk_free(fBlock);
1444
1445 fBlockSize = minRequest * 4;
1446 fBlock = sk_malloc_throw(fBlockSize);
1447 fBytesWritten = 0;
1448 *actual = fBlockSize;
1449 return fBlock;
1450}
1451
1452void SimplePC::notifyWritten(size_t bytes) {
1453 SkASSERT(fBytesWritten + bytes <= fBlockSize);
1454
1455 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1456 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1457 fBytesWritten += bytes;
1458 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001459
1460 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001461}
1462
reed@google.com68f456d2011-05-02 18:55:39 +00001463#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001464
reed@google.com64e3eb22011-05-04 14:32:04 +00001465
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001466void SampleView::onDraw(SkCanvas* canvas) {
1467 this->onDrawBackground(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001468
1469#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001470 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001471 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001472 if (fUsePipe) {
1473 canvas = writer.startRecording(&controller);
1474 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001475#endif
1476
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001477 for (int i = 0; i < fRepeatCount; i++) {
1478 SkAutoCanvasRestore acr(canvas, true);
1479 this->onDrawContent(canvas);
1480 }
1481}
1482
1483void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001484 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001485}
1486
1487///////////////////////////////////////////////////////////////////////////////
1488
reed@android.comf2b98d62010-12-20 18:26:13 +00001489template <typename T> void SkTBSort(T array[], int count) {
1490 for (int i = 1; i < count - 1; i++) {
1491 bool didSwap = false;
1492 for (int j = count - 1; j > i; --j) {
1493 if (array[j] < array[j-1]) {
1494 T tmp(array[j-1]);
1495 array[j-1] = array[j];
1496 array[j] = tmp;
1497 didSwap = true;
1498 }
1499 }
1500 if (!didSwap) {
1501 break;
1502 }
1503 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001504
reed@android.comf2b98d62010-12-20 18:26:13 +00001505 for (int k = 0; k < count - 1; k++) {
1506 SkASSERT(!(array[k+1] < array[k]));
1507 }
1508}
1509
1510#include "SkRandom.h"
1511
1512static void rand_rect(SkIRect* rect, SkRandom& rand) {
1513 int bits = 8;
1514 int shift = 32 - bits;
1515 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1516 rand.nextU() >> shift, rand.nextU() >> shift);
1517 rect->sort();
1518}
1519
1520static void dumpRect(const SkIRect& r) {
1521 SkDebugf(" { %d, %d, %d, %d },\n",
1522 r.fLeft, r.fTop,
1523 r.fRight, r.fBottom);
1524}
1525
1526static void test_rects(const SkIRect rect[], int count) {
1527 SkRegion rgn0, rgn1;
1528
1529 for (int i = 0; i < count; i++) {
1530 rgn0.op(rect[i], SkRegion::kUnion_Op);
1531 // dumpRect(rect[i]);
1532 }
1533 rgn1.setRects(rect, count);
1534
1535 if (rgn0 != rgn1) {
1536 SkDebugf("\n");
1537 for (int i = 0; i < count; i++) {
1538 dumpRect(rect[i]);
1539 }
1540 SkDebugf("\n");
1541 }
1542}
1543
1544static void test() {
1545 size_t i;
1546
1547 const SkIRect r0[] = {
1548 { 0, 0, 1, 1 },
1549 { 2, 2, 3, 3 },
1550 };
1551 const SkIRect r1[] = {
1552 { 0, 0, 1, 3 },
1553 { 1, 1, 2, 2 },
1554 { 2, 0, 3, 3 },
1555 };
1556 const SkIRect r2[] = {
1557 { 0, 0, 1, 2 },
1558 { 2, 1, 3, 3 },
1559 { 4, 0, 5, 1 },
1560 { 6, 0, 7, 4 },
1561 };
1562
1563 static const struct {
1564 const SkIRect* fRects;
1565 int fCount;
1566 } gRecs[] = {
1567 { r0, SK_ARRAY_COUNT(r0) },
1568 { r1, SK_ARRAY_COUNT(r1) },
1569 { r2, SK_ARRAY_COUNT(r2) },
1570 };
1571
1572 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1573 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1574 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001575
reed@android.comf2b98d62010-12-20 18:26:13 +00001576 SkRandom rand;
1577 for (i = 0; i < 10000; i++) {
1578 SkRegion rgn0, rgn1;
1579
1580 const int N = 8;
1581 SkIRect rect[N];
1582 for (int j = 0; j < N; j++) {
1583 rand_rect(&rect[j], rand);
1584 }
1585 test_rects(rect, N);
1586 }
1587}
1588
reed@android.com8a1c16f2008-12-17 15:59:43 +00001589SkOSWindow* create_sk_window(void* hwnd) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001590// test();
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001591 return new SampleWindow(hwnd);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001592}
1593
1594void get_preferred_size(int* x, int* y, int* width, int* height) {
1595 *x = 10;
1596 *y = 50;
1597 *width = 640;
1598 *height = 480;
1599}
1600
1601void application_init() {
1602// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001603#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001604 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001605#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001606 SkGraphics::Init();
1607 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001608}
1609
1610void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001611 SkEvent::Term();
1612 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001613}