blob: 34b23f1476d193dde45d16f9bc08f07b863607b0 [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);
Scroggo3272ba82011-05-25 20:50:42 +0000331 void showZoomer(SkCanvas* canvas);
Scroggo0f185c22011-03-24 18:35:50 +0000332
reed@android.com8a1c16f2008-12-17 15:59:43 +0000333 void postAnimatingEvent() {
334 if (fAnimating) {
335 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
336 evt->post(this->getSinkID(), ANIMATING_DELAY);
337 }
338 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000339
340
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 static CanvasType cycle_canvastype(CanvasType);
342
343 typedef SkOSWindow INHERITED;
344};
345
Scroggo0f185c22011-03-24 18:35:50 +0000346bool SampleWindow::zoomIn()
347{
348 // Arbitrarily decided
349 if (fFatBitsScale == 25) return false;
350 fFatBitsScale++;
351 this->inval(NULL);
352 return true;
353}
354
355bool SampleWindow::zoomOut()
356{
357 if (fFatBitsScale == 1) return false;
358 fFatBitsScale--;
359 this->inval(NULL);
360 return true;
361}
362
363void SampleWindow::toggleZoomer()
364{
365 fShowZoomer = !fShowZoomer;
366 this->inval(NULL);
367}
368
369void SampleWindow::updatePointer(int x, int y)
370{
371 fMouseX = x;
372 fMouseY = y;
373 if (fShowZoomer) {
374 this->inval(NULL);
375 }
376}
377
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000378bool SampleWindow::make3DReady() {
379
380#if defined(SK_SUPPORT_GL)
bsalomon@google.com498a6232011-03-10 18:24:15 +0000381 if (attachGL()) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000382 if (NULL != fGrContext) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000383 // various gr lifecycle tests
384 #if 0
385 fGrContext->freeGpuResources();
386 #elif 0
387 // this will leak resources.
388 fGrContext->contextLost();
389 #elif 0
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000390 GrAssert(1 == fGrContext->refcnt());
391 fGrContext->unref();
392 fGrContext = NULL;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000393 #endif
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000394 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000395
bsalomon@google.com498a6232011-03-10 18:24:15 +0000396 if (NULL == fGrContext) {
397 #if defined(SK_USE_SHADERS)
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000398 fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000399 #else
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000400 fGrContext = GrContext::Create(kOpenGL_Fixed_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000401 #endif
reed@google.com569e0432011-04-05 13:07:03 +0000402 SkDebugf("---- constructor\n");
bsalomon@google.com498a6232011-03-10 18:24:15 +0000403 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000404
bsalomon@google.com498a6232011-03-10 18:24:15 +0000405 if (NULL != fGrContext) {
406 return true;
407 } else {
408 detachGL();
409 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000410 }
411#endif
412 SkDebugf("Failed to setup 3D");
413 return false;
414}
415
reed@android.com8a1c16f2008-12-17 15:59:43 +0000416SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) {
417 static const CanvasType gCT[] = {
418 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000419 kGPU_CanvasType,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000420 kRaster_CanvasType
421 };
422 return gCT[ct];
423}
424
425SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426 fPicture = NULL;
reed@android.comf2b98d62010-12-20 18:26:13 +0000427 fGpuCanvas = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000429 fGrContext = NULL;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000430
reed@android.comf2b98d62010-12-20 18:26:13 +0000431#ifdef DEFAULT_TO_GPU
432 fCanvasType = kGPU_CanvasType;
433#else
reed@android.com8a1c16f2008-12-17 15:59:43 +0000434 fCanvasType = kRaster_CanvasType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000435#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000436 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000437 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000438 fRepeatDrawing = false;
439 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000440 fRotate = false;
441 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000442 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000443 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000444 fMeasureFPS = false;
reed@google.com569e0432011-04-05 13:07:03 +0000445 fLCDState = kUnknown_SkTriState;
446 fAAState = kUnknown_SkTriState;
reed@google.com66f22fd2011-05-17 15:33:45 +0000447 fFilterState = kUnknown_SkTriState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000448 fHintingState = kUnknown_SkTriState;
reed@google.com569e0432011-04-05 13:07:03 +0000449 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000450 fScrollTestX = fScrollTestY = 0;
451
Scroggo0f185c22011-03-24 18:35:50 +0000452 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000453 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000454 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
455 fShowZoomer = false;
456
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000457 fZoomLevel = 0;
458 fZoomScale = SK_Scalar1;
459
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000460// this->setConfig(SkBitmap::kRGB_565_Config);
461 this->setConfig(SkBitmap::kARGB_8888_Config);
462 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000463 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000464
reed@android.com34245c72009-11-03 04:00:48 +0000465 {
466 const SkViewRegister* reg = SkViewRegister::Head();
467 while (reg) {
468 *fSamples.append() = reg->factory();
469 reg = reg->next();
470 }
471 }
472 fCurrIndex = 0;
reed@android.come0f13ee2009-11-04 19:40:25 +0000473 this->loadView(fSamples[fCurrIndex]());
reed@google.comf0b5f682011-03-11 20:08:25 +0000474
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000475#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +0000476 testpdf();
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000477#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000478}
479
480SampleWindow::~SampleWindow() {
481 delete fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000482 delete fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000483 if (NULL != fGrContext) {
484 fGrContext->unref();
485 }
Scroggo0f185c22011-03-24 18:35:50 +0000486 fTypeface->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000487}
488
reed@android.com55e76b22009-11-23 21:46:47 +0000489static SkBitmap capture_bitmap(SkCanvas* canvas) {
490 SkBitmap bm;
491 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
492 src.copyTo(&bm, src.config());
493 return bm;
494}
495
496static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
497 SkBitmap* diff) {
498 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000499
reed@android.com55e76b22009-11-23 21:46:47 +0000500 SkAutoLockPixels alp0(src);
501 SkAutoLockPixels alp1(orig);
502 for (int y = 0; y < src.height(); y++) {
503 const void* srcP = src.getAddr(0, y);
504 const void* origP = orig.getAddr(0, y);
505 size_t bytes = src.width() * src.bytesPerPixel();
506 if (memcmp(srcP, origP, bytes)) {
507 SkDebugf("---------- difference on line %d\n", y);
508 return true;
509 }
510 }
511 return false;
512}
513
Scroggo0f185c22011-03-24 18:35:50 +0000514static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
515{
516 SkColor desiredColor = paint.getColor();
517 paint.setColor(SK_ColorWHITE);
518 const char* c_str = string.c_str();
519 size_t size = string.size();
520 SkRect bounds;
521 paint.measureText(c_str, size, &bounds);
522 bounds.offset(left, top);
523 SkScalar inset = SkIntToScalar(-2);
524 bounds.inset(inset, inset);
525 canvas->drawRect(bounds, paint);
526 if (desiredColor != SK_ColorBLACK) {
527 paint.setColor(SK_ColorBLACK);
528 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
529 }
530 paint.setColor(desiredColor);
531 canvas->drawText(c_str, size, left, top, paint);
532}
533
reed@android.com44177402009-11-23 21:07:51 +0000534#define XCLIP_N 8
535#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000536
537void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000538 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000539 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000540 gAnimTime = SkTime::GetMSecs();
541
reed@google.com569e0432011-04-05 13:07:03 +0000542 SkScalar cx = SkScalarHalf(this->width());
543 SkScalar cy = SkScalarHalf(this->height());
544
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000545 if (fZoomLevel) {
546 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000547 SkPoint center;
548 m = canvas->getTotalMatrix();//.invert(&m);
549 m.mapXY(cx, cy, &center);
550 cx = center.fX;
551 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000552
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000553 m.setTranslate(-cx, -cy);
554 m.postScale(fZoomScale, fZoomScale);
555 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000556
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000557 canvas->concat(m);
558 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000559
reed@google.com569e0432011-04-05 13:07:03 +0000560 if (fFlipAxis) {
561 SkMatrix m;
562 m.setTranslate(cx, cy);
563 if (fFlipAxis & kFlipAxis_X) {
564 m.preScale(-SK_Scalar1, SK_Scalar1);
565 }
566 if (fFlipAxis & kFlipAxis_Y) {
567 m.preScale(SK_Scalar1, -SK_Scalar1);
568 }
569 m.preTranslate(-cx, -cy);
570 canvas->concat(m);
571 }
572
reed@google.com52f57e12011-03-16 12:10:02 +0000573 // Apply any gesture matrix
574 if (true) {
575 const SkMatrix& localM = fGesture.localM();
576 if (localM.getType() & SkMatrix::kScale_Mask) {
577 canvas->setExternalMatrix(&localM);
578 }
579 canvas->concat(localM);
580 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000581
reed@google.com52f57e12011-03-16 12:10:02 +0000582 if (fGesture.isActive()) {
583 this->inval(NULL);
584 }
585 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000586
reed@android.come522ca52009-11-23 20:10:41 +0000587 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000588 this->INHERITED::draw(canvas);
589 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000590
591 const SkScalar w = this->width();
592 const SkScalar h = this->height();
593 const SkScalar cw = w / XCLIP_N;
594 const SkScalar ch = h / YCLIP_N;
595 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000596 SkRect r;
597 r.fTop = y * ch;
598 r.fBottom = (y + 1) * ch;
599 if (y == YCLIP_N - 1) {
600 r.fBottom = h;
601 }
reed@android.come522ca52009-11-23 20:10:41 +0000602 for (int x = 0; x < XCLIP_N; x++) {
603 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000604 r.fLeft = x * cw;
605 r.fRight = (x + 1) * cw;
606 if (x == XCLIP_N - 1) {
607 r.fRight = w;
608 }
reed@android.come522ca52009-11-23 20:10:41 +0000609 canvas->clipRect(r);
610 this->INHERITED::draw(canvas);
611 }
612 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000613
reed@android.com55e76b22009-11-23 21:46:47 +0000614 SkBitmap diff;
615 if (bitmap_diff(canvas, orig, &diff)) {
616 }
reed@android.come522ca52009-11-23 20:10:41 +0000617 } else {
618 this->INHERITED::draw(canvas);
619 }
Scroggo3272ba82011-05-25 20:50:42 +0000620 if (fShowZoomer && fCanvasType != kGPU_CanvasType) {
621 // In the GPU case, INHERITED::draw calls beforeChildren, which
622 // creates an SkGpuCanvas. All further draw calls are directed
623 // at that canvas, which is deleted in afterChildren (which is
624 // also called by draw), so we cannot show the zoomer here.
625 // Instead, we call it inside afterChildren.
626 showZoomer(canvas);
627 }
628}
629
630void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +0000631 int count = canvas->save();
632 canvas->resetMatrix();
633 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000634 int width = SkScalarRound(this->width());
635 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000636 if (fMouseX >= width) fMouseX = width - 1;
637 else if (fMouseX < 0) fMouseX = 0;
638 if (fMouseY >= height) fMouseY = height - 1;
639 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000640
Scroggo0f185c22011-03-24 18:35:50 +0000641 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000642 bitmap.lockPixels();
643
Scroggo0f185c22011-03-24 18:35:50 +0000644 // 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 +0000645 int zoomedWidth = (width >> 1) | 1;
646 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000647 SkIRect src;
648 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
649 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
650 SkRect dest;
651 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
652 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
653 SkPaint paint;
654 // Clear the background behind our zoomed in view
655 paint.setColor(SK_ColorWHITE);
656 canvas->drawRect(dest, paint);
657 canvas->drawBitmapRect(bitmap, &src, dest);
658 paint.setColor(SK_ColorBLACK);
659 paint.setStyle(SkPaint::kStroke_Style);
660 // Draw a border around the pixel in the middle
661 SkRect originalPixel;
662 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
663 SkMatrix matrix;
664 SkRect scalarSrc;
665 scalarSrc.set(src);
666 SkColor color = bitmap.getColor(fMouseX, fMouseY);
667 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
668 SkRect pixel;
669 matrix.mapRect(&pixel, originalPixel);
670 // TODO Perhaps measure the values and make the outline white if it's "dark"
671 if (color == SK_ColorBLACK) {
672 paint.setColor(SK_ColorWHITE);
673 }
674 canvas->drawRect(pixel, paint);
675 }
676 paint.setColor(SK_ColorBLACK);
677 // Draw a border around the destination rectangle
678 canvas->drawRect(dest, paint);
679 paint.setStyle(SkPaint::kStrokeAndFill_Style);
680 // Identify the pixel and its color on screen
681 paint.setTypeface(fTypeface);
682 paint.setAntiAlias(true);
683 SkScalar lineHeight = paint.getFontMetrics(NULL);
684 SkString string;
685 string.appendf("(%i, %i)", fMouseX, fMouseY);
686 SkScalar left = dest.fLeft + SkIntToScalar(3);
687 SkScalar i = SK_Scalar1;
688 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
689 // Alpha
690 i += SK_Scalar1;
691 string.reset();
692 string.appendf("A: %X", SkColorGetA(color));
693 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
694 // Red
695 i += SK_Scalar1;
696 string.reset();
697 string.appendf("R: %X", SkColorGetR(color));
698 paint.setColor(SK_ColorRED);
699 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
700 // Green
701 i += SK_Scalar1;
702 string.reset();
703 string.appendf("G: %X", SkColorGetG(color));
704 paint.setColor(SK_ColorGREEN);
705 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
706 // Blue
707 i += SK_Scalar1;
708 string.reset();
709 string.appendf("B: %X", SkColorGetB(color));
710 paint.setColor(SK_ColorBLUE);
711 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
712 canvas->restoreToCount(count);
Scroggo3272ba82011-05-25 20:50:42 +0000713 bitmap.unlockPixels();
reed@android.come522ca52009-11-23 20:10:41 +0000714}
715
reed@android.com8a1c16f2008-12-17 15:59:43 +0000716void SampleWindow::onDraw(SkCanvas* canvas) {
717 if (fRepeatDrawing) {
718 this->inval(NULL);
719 }
720}
721
722#include "SkColorPriv.h"
723
724static void reverseRedAndBlue(const SkBitmap& bm) {
725 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
726 uint8_t* p = (uint8_t*)bm.getPixels();
727 uint8_t* stop = p + bm.getSize();
728 while (p < stop) {
729 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
730 unsigned scale = SkAlpha255To256(p[3]);
731 unsigned r = p[2];
732 unsigned b = p[0];
733 p[0] = SkAlphaMul(r, scale);
734 p[1] = SkAlphaMul(p[1], scale);
735 p[2] = SkAlphaMul(b, scale);
736 p += 4;
737 }
738}
739
740SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000741 if (kGPU_CanvasType != fCanvasType) {
reed@android.com6efdc472008-12-19 18:24:35 +0000742#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000743 detachGL();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000744#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000745 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000746
reed@android.com8a1c16f2008-12-17 15:59:43 +0000747 switch (fCanvasType) {
748 case kRaster_CanvasType:
749 canvas = this->INHERITED::beforeChildren(canvas);
750 break;
751 case kPicture_CanvasType:
752 fPicture = new SkPicture;
753 canvas = fPicture->beginRecording(9999, 9999);
754 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000755 case kGPU_CanvasType: {
reed@google.com64e3eb22011-05-04 14:32:04 +0000756 if (make3DReady()) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000757 SkDevice* device = canvas->getDevice();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000758 const SkBitmap& bitmap = device->accessBitmap(true);
759
bsalomon@google.com5782d712011-01-21 21:03:59 +0000760 GrRenderTarget* renderTarget;
761 renderTarget = fGrContext->createRenderTargetFrom3DApiState();
762 fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
763 renderTarget->unref();
764
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000765 device = fGpuCanvas->createDevice(SkBitmap::kARGB_8888_Config,
766 bitmap.width(), bitmap.height(),
767 false, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000768 fGpuCanvas->setDevice(device)->unref();
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000769
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000770 fGpuCanvas->concat(canvas->getTotalMatrix());
reed@android.comf2b98d62010-12-20 18:26:13 +0000771 canvas = fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000772
reed@android.comf2b98d62010-12-20 18:26:13 +0000773 } else {
774 canvas = this->INHERITED::beforeChildren(canvas);
775 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000776 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000777 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000778 }
779
780 if (fUseClip) {
781 canvas->drawColor(0xFFFF88FF);
782 canvas->clipPath(fClipPath);
783 }
784
785 return canvas;
786}
787
788static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
789 const SkRegion& rgn) {
790 SkCanvas canvas(bm);
791 SkRegion inval(rgn);
792
793 inval.translate(r.fLeft, r.fTop);
794 canvas.clipRegion(inval);
795 canvas.drawColor(0xFFFF8080);
796}
797
798void SampleWindow::afterChildren(SkCanvas* orig) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000799 if (fRequestGrabImage) {
800 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000801
reed@android.comf2b98d62010-12-20 18:26:13 +0000802 SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
803 SkDevice* device = canvas->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000804 SkBitmap bmp;
805 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000806 static int gSampleGrabCounter;
807 SkString name;
808 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000809 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000810 SkImageEncoder::kPNG_Type, 100);
811 }
812 }
813
reed@android.com8a1c16f2008-12-17 15:59:43 +0000814 switch (fCanvasType) {
815 case kRaster_CanvasType:
816 break;
817 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000818 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000819 SkPicture* pict = new SkPicture(*fPicture);
820 fPicture->unref();
821 orig->drawPicture(*pict);
822 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000823 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000824 SkDynamicMemoryWStream ostream;
825 fPicture->serialize(&ostream);
826 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000827
reed@android.com8a1c16f2008-12-17 15:59:43 +0000828 SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
829 SkPicture pict(&istream);
830 orig->drawPicture(pict);
831 } else {
832 fPicture->draw(orig);
833 fPicture->unref();
834 }
835 fPicture = NULL;
836 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000837#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000838 case kGPU_CanvasType:
Scroggo3272ba82011-05-25 20:50:42 +0000839 if (fShowZoomer) {
840 this->showZoomer(fGpuCanvas);
841 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000842 delete fGpuCanvas;
843 fGpuCanvas = NULL;
844 presentGL();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000845 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000846#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000847 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000848
reed@google.com17d7aec2011-04-25 14:31:44 +0000849 // Do this after presentGL and other finishing, rather than in afterChild
850 if (fMeasureFPS && fMeasureFPS_Time) {
851 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
852 this->updateTitle();
853 postInvalDelay(this->getSinkID());
854 }
855
856 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000857 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000858 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
859 int dx = fScrollTestX * 7;
860 int dy = fScrollTestY * 7;
861 SkIRect r;
862 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000863
reed@android.com8a1c16f2008-12-17 15:59:43 +0000864 r.set(50, 50, 50+100, 50+100);
865 bm.scrollRect(&r, dx, dy, &inval);
866 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000867 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000868}
869
reed@android.com6c5f6f22009-08-14 16:08:38 +0000870void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
871 if (fScale) {
872 SkScalar scale = SK_Scalar1 * 7 / 10;
873 SkScalar cx = this->width() / 2;
874 SkScalar cy = this->height() / 2;
875 canvas->translate(cx, cy);
876 canvas->scale(scale, scale);
877 canvas->translate(-cx, -cy);
878 }
879 if (fRotate) {
880 SkScalar cx = this->width() / 2;
881 SkScalar cy = this->height() / 2;
882 canvas->translate(cx, cy);
883 canvas->rotate(SkIntToScalar(30));
884 canvas->translate(-cx, -cy);
885 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000886
reed@google.com09e3baa2011-05-18 12:04:31 +0000887 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
888 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000889
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000890 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000891 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
892 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
893 fMeasureFPS_Time = SkTime::GetMSecs();
894 }
895 } else {
896 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000897 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000898 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000899}
900
901void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000902 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000903}
904
reed@android.com8a1c16f2008-12-17 15:59:43 +0000905static SkBitmap::Config gConfigCycle[] = {
906 SkBitmap::kNo_Config, // none -> none
907 SkBitmap::kNo_Config, // a1 -> none
908 SkBitmap::kNo_Config, // a8 -> none
909 SkBitmap::kNo_Config, // index8 -> none
910 SkBitmap::kARGB_4444_Config, // 565 -> 4444
911 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
912 SkBitmap::kRGB_565_Config // 8888 -> 565
913};
914
915static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
916 return gConfigCycle[c];
917}
918
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000919void SampleWindow::changeZoomLevel(int delta) {
920 fZoomLevel += delta;
921 if (fZoomLevel > 0) {
922 fZoomLevel = SkMin32(fZoomLevel, MAX_ZOOM_LEVEL);
923 fZoomScale = SkIntToScalar(fZoomLevel + 1);
924 } else if (fZoomLevel < 0) {
925 fZoomLevel = SkMax32(fZoomLevel, MIN_ZOOM_LEVEL);
926 fZoomScale = SK_Scalar1 / (1 - fZoomLevel);
927 } else {
928 fZoomScale = SK_Scalar1;
929 }
930
931 this->inval(NULL);
932}
933
reed@android.com8a1c16f2008-12-17 15:59:43 +0000934bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000935 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
936 this->loadView(fSamples[fCurrIndex]());
937 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000938}
939
940bool SampleWindow::onEvent(const SkEvent& evt) {
941 if (evt.isType(ANIMATING_EVENTTYPE)) {
942 if (fAnimating) {
943 this->nextSample();
944 this->postAnimatingEvent();
945 }
946 return true;
947 }
reed@android.com34245c72009-11-03 04:00:48 +0000948 if (evt.isType("set-curr-index")) {
949 fCurrIndex = evt.getFast32() % fSamples.count();
950 this->loadView(fSamples[fCurrIndex]());
951 return true;
952 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000953 if (isInvalEvent(evt)) {
954 this->inval(NULL);
955 return true;
956 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000957 return this->INHERITED::onEvent(evt);
958}
959
reed@android.comf2b98d62010-12-20 18:26:13 +0000960bool SampleWindow::onQuery(SkEvent* query) {
961 if (query->isType("get-slide-count")) {
962 query->setFast32(fSamples.count());
963 return true;
964 }
965 if (query->isType("get-slide-title")) {
966 SkView* view = fSamples[query->getFast32()]();
967 SkEvent evt(gTitleEvtName);
968 if (view->doQuery(&evt)) {
969 query->setString("title", evt.findString(gTitleEvtName));
970 }
971 SkSafeUnref(view);
972 return true;
973 }
974 if (query->isType("use-fast-text")) {
975 SkEvent evt(gFastTextEvtName);
976 return curr_view(this)->doQuery(&evt);
977 }
978 return this->INHERITED::onQuery(query);
979}
980
reed@android.com0ae6b242008-12-23 16:49:54 +0000981static void cleanup_for_filename(SkString* name) {
982 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +0000983 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +0000984 switch (str[i]) {
985 case ':': str[i] = '-'; break;
986 case '/': str[i] = '-'; break;
987 case ' ': str[i] = '_'; break;
988 default: break;
989 }
990 }
991}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000992
993bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000994 {
995 SkView* view = curr_view(this);
996 if (view) {
997 SkEvent evt(gCharEvtName);
998 evt.setFast32(uni);
999 if (view->doQuery(&evt)) {
1000 return true;
1001 }
1002 }
1003 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001004
reed@android.com8a1c16f2008-12-17 15:59:43 +00001005 int dx = 0xFF;
1006 int dy = 0xFF;
1007
1008 switch (uni) {
1009 case '5': dx = 0; dy = 0; break;
1010 case '8': dx = 0; dy = -1; break;
1011 case '6': dx = 1; dy = 0; break;
1012 case '2': dx = 0; dy = 1; break;
1013 case '4': dx = -1; dy = 0; break;
1014 case '7': dx = -1; dy = -1; break;
1015 case '9': dx = 1; dy = -1; break;
1016 case '3': dx = 1; dy = 1; break;
1017 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001018
reed@android.com8a1c16f2008-12-17 15:59:43 +00001019 default:
1020 break;
1021 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001022
reed@android.com8a1c16f2008-12-17 15:59:43 +00001023 if (0xFF != dx && 0xFF != dy) {
1024 if ((dx | dy) == 0) {
1025 fScrollTestX = fScrollTestY = 0;
1026 } else {
1027 fScrollTestX += dx;
1028 fScrollTestY += dy;
1029 }
1030 this->inval(NULL);
1031 return true;
1032 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001033
reed@android.com0ae6b242008-12-23 16:49:54 +00001034 switch (uni) {
1035 case 'a':
1036 fAnimating = !fAnimating;
1037 this->postAnimatingEvent();
1038 this->updateTitle();
1039 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001040 case 'b':
1041 fAAState = cycle_tristate(fAAState);
1042 this->updateTitle();
1043 this->inval(NULL);
1044 break;
1045 case 'c':
1046 fUseClip = !fUseClip;
1047 this->inval(NULL);
1048 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001049 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001050 case 'd':
1051 SkGraphics::SetFontCacheUsed(0);
1052 return true;
1053 case 'f':
1054 fMeasureFPS = !fMeasureFPS;
1055 this->inval(NULL);
1056 break;
1057 case 'g':
1058 fRequestGrabImage = true;
1059 this->inval(NULL);
1060 break;
reed@google.com09e3baa2011-05-18 12:04:31 +00001061 case 'h':
1062 fHintingState = cycle_tristate(fHintingState);
1063 this->updateTitle();
1064 this->inval(NULL);
1065 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001066 case 'i':
1067 this->zoomIn();
1068 break;
1069 case 'l':
1070 fLCDState = cycle_tristate(fLCDState);
1071 this->updateTitle();
1072 this->inval(NULL);
1073 break;
reed@google.com176753a2011-05-17 15:32:04 +00001074 case 'n':
1075 fFilterState = cycle_tristate(fFilterState);
1076 this->updateTitle();
1077 this->inval(NULL);
1078 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001079 case 'o':
1080 this->zoomOut();
1081 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001082 case 'p':
1083 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001084 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001085 this->inval(NULL);
1086 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001087 case 'r':
1088 fRotate = !fRotate;
1089 this->inval(NULL);
1090 this->updateTitle();
1091 return true;
1092 case 's':
1093 fScale = !fScale;
1094 this->inval(NULL);
1095 this->updateTitle();
1096 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001097 case 'x':
1098 fFlipAxis ^= kFlipAxis_X;
1099 this->updateTitle();
1100 this->inval(NULL);
1101 break;
1102 case 'y':
1103 fFlipAxis ^= kFlipAxis_Y;
1104 this->updateTitle();
1105 this->inval(NULL);
1106 break;
scroggo08526c02011-03-22 14:03:21 +00001107 case 'z':
1108 this->toggleZoomer();
1109 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001110 default:
1111 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001112 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001113
reed@android.com8a1c16f2008-12-17 15:59:43 +00001114 return this->INHERITED::onHandleChar(uni);
1115}
1116
1117#include "SkDumpCanvas.h"
1118
1119bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001120 {
1121 SkView* view = curr_view(this);
1122 if (view) {
1123 SkEvent evt(gKeyEvtName);
1124 evt.setFast32(key);
1125 if (view->doQuery(&evt)) {
1126 return true;
1127 }
1128 }
1129 }
1130
reed@android.com8a1c16f2008-12-17 15:59:43 +00001131 switch (key) {
1132 case kRight_SkKey:
1133 if (this->nextSample()) {
1134 return true;
1135 }
1136 break;
1137 case kLeft_SkKey:
1138 fCanvasType = cycle_canvastype(fCanvasType);
1139 this->updateTitle();
1140 this->inval(NULL);
1141 return true;
1142 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001143 if (USE_ARROWS_FOR_ZOOM) {
1144 this->changeZoomLevel(1);
1145 } else {
1146 fNClip = !fNClip;
1147 this->inval(NULL);
1148 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001149 this->updateTitle();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001150 return true;
1151 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001152 if (USE_ARROWS_FOR_ZOOM) {
1153 this->changeZoomLevel(-1);
1154 } else {
1155 this->setConfig(cycle_configs(this->getBitmap().config()));
1156 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001157 this->updateTitle();
1158 return true;
1159 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001160 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001161 SkDebugfDumper dumper;
1162 SkDumpCanvas dc(&dumper);
1163 this->draw(&dc);
1164 } else {
1165 fRepeatDrawing = !fRepeatDrawing;
1166 if (fRepeatDrawing) {
1167 this->inval(NULL);
1168 }
1169 }
1170 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001171 case kBack_SkKey:
1172 this->loadView(NULL);
1173 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001174 default:
1175 break;
1176 }
1177 return this->INHERITED::onHandleKey(key);
1178}
1179
reed@google.com52f57e12011-03-16 12:10:02 +00001180///////////////////////////////////////////////////////////////////////////////
1181
1182static const char gGestureClickType[] = "GestureClickType";
1183
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001184bool SampleWindow::onDispatchClick(int x, int y, Click::State state) {
Scroggo0f185c22011-03-24 18:35:50 +00001185 if (Click::kMoved_State == state) {
1186 updatePointer(x, y);
1187 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001188 int w = SkScalarRound(this->width());
1189 int h = SkScalarRound(this->height());
1190
1191 // check for the resize-box
1192 if (w - x < 16 && h - y < 16) {
1193 return false; // let the OS handle the click
1194 } else {
1195 return this->INHERITED::onDispatchClick(x, y, state);
1196 }
1197}
1198
reed@google.com52f57e12011-03-16 12:10:02 +00001199class GestureClick : public SkView::Click {
1200public:
1201 GestureClick(SkView* target) : SkView::Click(target) {
1202 this->setType(gGestureClickType);
1203 }
1204
1205 static bool IsGesture(Click* click) {
1206 return click->isType(gGestureClickType);
1207 }
1208};
1209
1210SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1211 return new GestureClick(this);
1212}
1213
1214bool SampleWindow::onClick(Click* click) {
1215 if (GestureClick::IsGesture(click)) {
1216 float x = SkScalarToFloat(click->fCurr.fX);
1217 float y = SkScalarToFloat(click->fCurr.fY);
1218 switch (click->fState) {
1219 case SkView::Click::kDown_State:
1220 fGesture.touchBegin(click, x, y);
1221 break;
1222 case SkView::Click::kMoved_State:
1223 fGesture.touchMoved(click, x, y);
1224 this->inval(NULL);
1225 break;
1226 case SkView::Click::kUp_State:
1227 fGesture.touchEnd(click);
1228 this->inval(NULL);
1229 break;
1230 }
1231 return true;
1232 }
1233 return false;
1234}
1235
1236///////////////////////////////////////////////////////////////////////////////
1237
reed@android.com8a1c16f2008-12-17 15:59:43 +00001238void SampleWindow::loadView(SkView* view) {
1239 SkView::F2BIter iter(this);
1240 SkView* prev = iter.next();
1241 if (prev) {
1242 prev->detachFromParent();
1243 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001244
reed@android.com34245c72009-11-03 04:00:48 +00001245 if (NULL == view) {
1246 view = create_overview(fSamples.count(), fSamples.begin());
1247 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001248 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001249 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001250 this->attachChildToFront(view)->unref();
1251 view->setSize(this->width(), this->height());
1252
1253 this->updateTitle();
1254}
1255
1256static const char* gConfigNames[] = {
1257 "unknown config",
1258 "A1",
1259 "A8",
1260 "Index8",
1261 "565",
1262 "4444",
1263 "8888"
1264};
1265
1266static const char* configToString(SkBitmap::Config c) {
1267 return gConfigNames[c];
1268}
1269
1270static const char* gCanvasTypePrefix[] = {
1271 "raster: ",
1272 "picture: ",
1273 "opengl: "
1274};
1275
reed@google.com569e0432011-04-05 13:07:03 +00001276static const char* trystate_str(SkTriState state,
1277 const char trueStr[], const char falseStr[]) {
1278 if (kTrue_SkTriState == state) {
1279 return trueStr;
1280 } else if (kFalse_SkTriState == state) {
1281 return falseStr;
1282 }
1283 return NULL;
1284}
1285
reed@android.com8a1c16f2008-12-17 15:59:43 +00001286void SampleWindow::updateTitle() {
1287 SkString title;
1288
1289 SkView::F2BIter iter(this);
1290 SkView* view = iter.next();
1291 SkEvent evt(gTitleEvtName);
1292 if (view->doQuery(&evt)) {
1293 title.set(evt.findString(gTitleEvtName));
1294 }
1295 if (title.size() == 0) {
1296 title.set("<unknown>");
1297 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001298
reed@android.com8a1c16f2008-12-17 15:59:43 +00001299 title.prepend(gCanvasTypePrefix[fCanvasType]);
1300
1301 title.prepend(" ");
1302 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001303
reed@android.com8a1c16f2008-12-17 15:59:43 +00001304 if (fAnimating) {
1305 title.prepend("<A> ");
1306 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001307 if (fScale) {
1308 title.prepend("<S> ");
1309 }
1310 if (fRotate) {
1311 title.prepend("<R> ");
1312 }
reed@android.come522ca52009-11-23 20:10:41 +00001313 if (fNClip) {
1314 title.prepend("<C> ");
1315 }
reed@google.com569e0432011-04-05 13:07:03 +00001316
1317 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1318 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001319 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001320 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1321 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001322
1323 if (fZoomLevel) {
1324 title.prependf("{%d} ", fZoomLevel);
1325 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001326
1327 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001328 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001329 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001330 if (fUsePipe && SampleView::IsSampleView(view)) {
1331 title.prepend("<P> ");
1332 }
1333 if (SampleView::IsSampleView(view)) {
1334 title.prepend("! ");
1335 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001336
reed@android.com8a1c16f2008-12-17 15:59:43 +00001337 this->setTitle(title.c_str());
1338}
1339
1340void SampleWindow::onSizeChange() {
1341 this->INHERITED::onSizeChange();
1342
1343 SkView::F2BIter iter(this);
1344 SkView* view = iter.next();
1345 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001346
reed@android.com8a1c16f2008-12-17 15:59:43 +00001347 // rebuild our clippath
1348 {
1349 const SkScalar W = this->width();
1350 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001351
reed@android.com8a1c16f2008-12-17 15:59:43 +00001352 fClipPath.reset();
1353#if 0
1354 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1355 SkRect r;
1356 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1357 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1358 fClipPath.addRect(r);
1359 }
1360#else
1361 SkRect r;
1362 r.set(0, 0, W, H);
1363 fClipPath.addRect(r, SkPath::kCCW_Direction);
1364 r.set(W/4, H/4, W*3/4, H*3/4);
1365 fClipPath.addRect(r, SkPath::kCW_Direction);
1366#endif
1367 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001368
reed@android.com8a1c16f2008-12-17 15:59:43 +00001369 this->updateTitle(); // to refresh our config
1370}
1371
1372///////////////////////////////////////////////////////////////////////////////
1373
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001374static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001375static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001376static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001377
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001378bool SampleView::IsSampleView(SkView* view) {
1379 SkEvent evt(is_sample_view_tag);
1380 return view->doQuery(&evt);
1381}
1382
reed@google.comf2183392011-04-22 14:10:48 +00001383bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001384 SkEvent evt(repeat_count_tag);
1385 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001386 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001387}
1388
reed@google.com0faac1e2011-05-11 05:58:58 +00001389bool SampleView::SetUsePipe(SkView* view, bool pred) {
1390 SkEvent evt(set_use_pipe_tag);
1391 evt.setFast32(pred);
1392 return view->doEvent(evt);
1393}
1394
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001395bool SampleView::onEvent(const SkEvent& evt) {
1396 if (evt.isType(repeat_count_tag)) {
1397 fRepeatCount = evt.getFast32();
1398 return true;
1399 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001400 if (evt.isType(set_use_pipe_tag)) {
1401 fUsePipe = !!evt.getFast32();
1402 return true;
1403 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001404 return this->INHERITED::onEvent(evt);
1405}
1406
1407bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001408 if (evt->isType(is_sample_view_tag)) {
1409 return true;
1410 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001411 return this->INHERITED::onQuery(evt);
1412}
1413
reed@google.com4aebe4f2011-05-05 14:07:35 +00001414#define TEST_GPIPEx
reed@google.com64e3eb22011-05-04 14:32:04 +00001415
reed@google.com68f456d2011-05-02 18:55:39 +00001416#ifdef TEST_GPIPE
1417 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001418
1419class SimplePC : public SkGPipeController {
1420public:
1421 SimplePC(SkCanvas* target);
1422 ~SimplePC();
1423
1424 virtual void* requestBlock(size_t minRequest, size_t* actual);
1425 virtual void notifyWritten(size_t bytes);
1426
1427private:
reed@google.com961ddb02011-05-05 14:03:48 +00001428 SkGPipeReader fReader;
1429 void* fBlock;
1430 size_t fBlockSize;
1431 size_t fBytesWritten;
1432 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001433 SkGPipeReader::Status fStatus;
1434
1435 size_t fTotalWritten;
1436};
1437
1438SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1439 fBlock = NULL;
1440 fBlockSize = fBytesWritten = 0;
1441 fStatus = SkGPipeReader::kDone_Status;
1442 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001443 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001444}
1445
1446SimplePC::~SimplePC() {
1447// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1448 sk_free(fBlock);
1449
reed@google.com0faac1e2011-05-11 05:58:58 +00001450 if (fTotalWritten) {
1451 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1452 fAtomsWritten, fStatus);
1453 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001454}
1455
1456void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1457 sk_free(fBlock);
1458
1459 fBlockSize = minRequest * 4;
1460 fBlock = sk_malloc_throw(fBlockSize);
1461 fBytesWritten = 0;
1462 *actual = fBlockSize;
1463 return fBlock;
1464}
1465
1466void SimplePC::notifyWritten(size_t bytes) {
1467 SkASSERT(fBytesWritten + bytes <= fBlockSize);
1468
1469 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1470 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1471 fBytesWritten += bytes;
1472 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001473
1474 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001475}
1476
reed@google.com68f456d2011-05-02 18:55:39 +00001477#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001478
reed@google.com64e3eb22011-05-04 14:32:04 +00001479
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001480void SampleView::onDraw(SkCanvas* canvas) {
1481 this->onDrawBackground(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001482
1483#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001484 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001485 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001486 if (fUsePipe) {
reed@google.comdde09562011-05-23 12:21:05 +00001487 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
1488// flags = 0;
1489 canvas = writer.startRecording(&controller, flags);
reed@google.com0faac1e2011-05-11 05:58:58 +00001490 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001491#endif
1492
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001493 for (int i = 0; i < fRepeatCount; i++) {
1494 SkAutoCanvasRestore acr(canvas, true);
1495 this->onDrawContent(canvas);
1496 }
1497}
1498
1499void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001500 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001501}
1502
1503///////////////////////////////////////////////////////////////////////////////
1504
reed@android.comf2b98d62010-12-20 18:26:13 +00001505template <typename T> void SkTBSort(T array[], int count) {
1506 for (int i = 1; i < count - 1; i++) {
1507 bool didSwap = false;
1508 for (int j = count - 1; j > i; --j) {
1509 if (array[j] < array[j-1]) {
1510 T tmp(array[j-1]);
1511 array[j-1] = array[j];
1512 array[j] = tmp;
1513 didSwap = true;
1514 }
1515 }
1516 if (!didSwap) {
1517 break;
1518 }
1519 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001520
reed@android.comf2b98d62010-12-20 18:26:13 +00001521 for (int k = 0; k < count - 1; k++) {
1522 SkASSERT(!(array[k+1] < array[k]));
1523 }
1524}
1525
1526#include "SkRandom.h"
1527
1528static void rand_rect(SkIRect* rect, SkRandom& rand) {
1529 int bits = 8;
1530 int shift = 32 - bits;
1531 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1532 rand.nextU() >> shift, rand.nextU() >> shift);
1533 rect->sort();
1534}
1535
1536static void dumpRect(const SkIRect& r) {
1537 SkDebugf(" { %d, %d, %d, %d },\n",
1538 r.fLeft, r.fTop,
1539 r.fRight, r.fBottom);
1540}
1541
1542static void test_rects(const SkIRect rect[], int count) {
1543 SkRegion rgn0, rgn1;
1544
1545 for (int i = 0; i < count; i++) {
1546 rgn0.op(rect[i], SkRegion::kUnion_Op);
1547 // dumpRect(rect[i]);
1548 }
1549 rgn1.setRects(rect, count);
1550
1551 if (rgn0 != rgn1) {
1552 SkDebugf("\n");
1553 for (int i = 0; i < count; i++) {
1554 dumpRect(rect[i]);
1555 }
1556 SkDebugf("\n");
1557 }
1558}
1559
1560static void test() {
1561 size_t i;
1562
1563 const SkIRect r0[] = {
1564 { 0, 0, 1, 1 },
1565 { 2, 2, 3, 3 },
1566 };
1567 const SkIRect r1[] = {
1568 { 0, 0, 1, 3 },
1569 { 1, 1, 2, 2 },
1570 { 2, 0, 3, 3 },
1571 };
1572 const SkIRect r2[] = {
1573 { 0, 0, 1, 2 },
1574 { 2, 1, 3, 3 },
1575 { 4, 0, 5, 1 },
1576 { 6, 0, 7, 4 },
1577 };
1578
1579 static const struct {
1580 const SkIRect* fRects;
1581 int fCount;
1582 } gRecs[] = {
1583 { r0, SK_ARRAY_COUNT(r0) },
1584 { r1, SK_ARRAY_COUNT(r1) },
1585 { r2, SK_ARRAY_COUNT(r2) },
1586 };
1587
1588 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1589 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1590 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001591
reed@android.comf2b98d62010-12-20 18:26:13 +00001592 SkRandom rand;
1593 for (i = 0; i < 10000; i++) {
1594 SkRegion rgn0, rgn1;
1595
1596 const int N = 8;
1597 SkIRect rect[N];
1598 for (int j = 0; j < N; j++) {
1599 rand_rect(&rect[j], rand);
1600 }
1601 test_rects(rect, N);
1602 }
1603}
1604
reed@android.com8a1c16f2008-12-17 15:59:43 +00001605SkOSWindow* create_sk_window(void* hwnd) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001606// test();
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001607 return new SampleWindow(hwnd);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001608}
1609
1610void get_preferred_size(int* x, int* y, int* width, int* height) {
1611 *x = 10;
1612 *y = 50;
1613 *width = 640;
1614 *height = 480;
1615}
1616
1617void application_init() {
1618// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001619#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001620 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001621#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001622 SkGraphics::Init();
1623 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001624}
1625
1626void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001627 SkEvent::Term();
1628 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001629}