blob: 455559c745bc92e0cfe9823a21ad60ca8a0e4d9b [file] [log] [blame]
Scroggo2c8208f2011-06-15 16:49:08 +00001#include "SampleApp.h"
2
reed@android.com8a1c16f2008-12-17 15:59:43 +00003#include "SkCanvas.h"
4#include "SkDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00005#include "SkGpuCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00006#include "SkGraphics.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +00007#include "SkImageEncoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkPaint.h"
9#include "SkPicture.h"
10#include "SkStream.h"
reed@android.com44177402009-11-23 21:07:51 +000011#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkWindow.h"
13
14#include "SampleCode.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "GrContext.h"
Scroggo0f185c22011-03-24 18:35:50 +000016#include "SkTypeface.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000017
Scroggo3e7ff9f2011-06-16 15:31:26 +000018#include "GrGLInterface.h"
Scroggo2c8208f2011-06-15 16:49:08 +000019
yangsu@google.com1f394212011-06-01 18:03:34 +000020#define TEST_GPIPEx
21
22#ifdef TEST_GPIPE
23#define PIPE_FILE
24#define FILE_PATH "/path/to/drawing.data"
25#endif
26
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +000027#define USE_ARROWS_FOR_ZOOM true
reed@android.comf2b98d62010-12-20 18:26:13 +000028//#define DEFAULT_TO_GPU
29
reed@android.come191b162009-12-18 21:33:39 +000030extern SkView* create_overview(int, const SkViewFactory[]);
reed@android.com34245c72009-11-03 04:00:48 +000031
reed@android.comcb342352010-07-22 18:27:53 +000032#define SK_SUPPORT_GL
reed@android.com8a1c16f2008-12-17 15:59:43 +000033
34#define ANIMATING_EVENTTYPE "nextSample"
35#define ANIMATING_DELAY 750
36
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000037#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +000038 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000039#else
reed@google.combad8c872011-05-18 20:10:31 +000040 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000041#endif
reed@google.combad8c872011-05-18 20:10:31 +000042#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000043
reed@google.comac10a2d2010-12-22 21:39:39 +000044#ifdef SK_SUPPORT_GL
45 #include "GrGLConfig.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000046#endif
47
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000048///////////////
49static const char view_inval_msg[] = "view-inval-msg";
50
51static void postInvalDelay(SkEventSinkID sinkID) {
52 SkEvent* evt = new SkEvent(view_inval_msg);
reed@google.comf2183392011-04-22 14:10:48 +000053 evt->post(sinkID, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000054}
55
56static bool isInvalEvent(const SkEvent& evt) {
57 return evt.isType(view_inval_msg);
58}
59//////////////////
60
reed@android.com8a1c16f2008-12-17 15:59:43 +000061SkViewRegister* SkViewRegister::gHead;
62SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) {
63 static bool gOnce;
64 if (!gOnce) {
65 gHead = NULL;
66 gOnce = true;
67 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000068
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 fChain = gHead;
70 gHead = this;
71}
72
reed@android.comf2b98d62010-12-20 18:26:13 +000073#if defined(SK_SUPPORT_GL)
74 #define SK_USE_SHADERS
75#endif
76
twiz@google.com06c3b6b2011-03-14 16:58:39 +000077#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +000078#include <CoreFoundation/CoreFoundation.h>
79#include <CoreFoundation/CFURLAccess.h>
80
81static void testpdf() {
82 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
83 kCFStringEncodingUTF8);
84 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
85 kCFURLPOSIXPathStyle,
86 false);
87 CFRelease(path);
88 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
89 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
90 CFRelease(url);
91
92 CGContextBeginPage(cg, &box);
93 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
94 CGContextFillEllipseInRect(cg, r);
95 CGContextEndPage(cg);
96 CGContextRelease(cg);
97
98 if (false) {
99 SkBitmap bm;
100 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
101 bm.allocPixels();
102 bm.eraseColor(0);
103
104 SkCanvas canvas(bm);
105
106 }
107}
108#endif
109
110//////////////////////////////////////////////////////////////////////////////
111
reed@google.com569e0432011-04-05 13:07:03 +0000112enum FlipAxisEnum {
113 kFlipAxis_X = (1 << 0),
114 kFlipAxis_Y = (1 << 1)
115};
116
reed@google.com569e0432011-04-05 13:07:03 +0000117static SkTriState cycle_tristate(SkTriState state) {
118 static const SkTriState gCycle[] = {
119 /* kFalse_SkTriState -> */ kUnknown_SkTriState,
120 /* kTrue_SkTriState -> */ kFalse_SkTriState,
121 /* kUnknown_SkTriState -> */ kTrue_SkTriState,
122 };
123 return gCycle[state];
124}
125
reed@google.comf0b5f682011-03-11 20:08:25 +0000126#include "SkDrawFilter.h"
127
reed@google.com569e0432011-04-05 13:07:03 +0000128class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000129public:
reed@google.com09e3baa2011-05-18 12:04:31 +0000130 FlagsDrawFilter(SkTriState lcd, SkTriState aa, SkTriState filter,
131 SkTriState hinting) :
132 fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000133
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000134 virtual void filter(SkPaint* paint, Type t) {
reed@google.com569e0432011-04-05 13:07:03 +0000135 if (kText_Type == t && kUnknown_SkTriState != fLCDState) {
reed@google.com569e0432011-04-05 13:07:03 +0000136 paint->setLCDRenderText(kTrue_SkTriState == fLCDState);
137 }
138 if (kUnknown_SkTriState != fAAState) {
reed@google.com569e0432011-04-05 13:07:03 +0000139 paint->setAntiAlias(kTrue_SkTriState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000140 }
reed@google.com176753a2011-05-17 15:32:04 +0000141 if (kUnknown_SkTriState != fFilterState) {
142 paint->setFilterBitmap(kTrue_SkTriState == fFilterState);
143 }
reed@google.com09e3baa2011-05-18 12:04:31 +0000144 if (kUnknown_SkTriState != fHintingState) {
145 paint->setHinting(kTrue_SkTriState == fHintingState ?
146 SkPaint::kNormal_Hinting :
147 SkPaint::kSlight_Hinting);
148 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000149 }
150
151private:
reed@google.com569e0432011-04-05 13:07:03 +0000152 SkTriState fLCDState;
reed@google.com569e0432011-04-05 13:07:03 +0000153 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000154 SkTriState fFilterState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000155 SkTriState fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000156};
157
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158//////////////////////////////////////////////////////////////////////////////
159
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000160#define MAX_ZOOM_LEVEL 8
161#define MIN_ZOOM_LEVEL -8
162
reed@android.comf2b98d62010-12-20 18:26:13 +0000163static const char gCharEvtName[] = "SampleCode_Char_Event";
164static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165static const char gTitleEvtName[] = "SampleCode_Title_Event";
166static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000167static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
168
169bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
170 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
171 if (outUni) {
172 *outUni = evt.getFast32();
173 }
174 return true;
175 }
176 return false;
177}
178
179bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
180 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
181 if (outKey) {
182 *outKey = (SkKey)evt.getFast32();
183 }
184 return true;
185 }
186 return false;
187}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188
189bool SampleCode::TitleQ(const SkEvent& evt) {
190 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
191}
192
193void SampleCode::TitleR(SkEvent* evt, const char title[]) {
194 SkASSERT(evt && TitleQ(*evt));
195 evt->setString(gTitleEvtName, title);
196}
197
198bool SampleCode::PrefSizeQ(const SkEvent& evt) {
199 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
200}
201
202void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
203 SkASSERT(evt && PrefSizeQ(*evt));
204 SkScalar size[2];
205 size[0] = width;
206 size[1] = height;
207 evt->setScalars(gPrefSizeEvtName, 2, size);
208}
209
reed@android.comf2b98d62010-12-20 18:26:13 +0000210bool SampleCode::FastTextQ(const SkEvent& evt) {
211 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
212}
213
214///////////////////////////////////////////////////////////////////////////////
215
reed@android.com44177402009-11-23 21:07:51 +0000216static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000217static SkMSec gAnimTimePrev;
218
reed@android.com44177402009-11-23 21:07:51 +0000219SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000220SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
221SkScalar SampleCode::GetAnimSecondsDelta() {
222 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
223}
reed@android.com44177402009-11-23 21:07:51 +0000224
225SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000226 // since gAnimTime can be up to 32 bits, we can't convert it to a float
227 // or we'll lose the low bits. Hence we use doubles for the intermediate
228 // calculations
229 double seconds = (double)gAnimTime / 1000.0;
230 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000231 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000232 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000233 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000234 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000235}
236
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237//////////////////////////////////////////////////////////////////////////////
238
reed@android.comf2b98d62010-12-20 18:26:13 +0000239static SkView* curr_view(SkWindow* wind) {
240 SkView::F2BIter iter(wind);
241 return iter.next();
242}
243
Scroggo2c8208f2011-06-15 16:49:08 +0000244void SampleWindow::setZoomCenter(float x, float y)
245{
246 fZoomCenterX = SkFloatToScalar(x);
247 fZoomCenterY = SkFloatToScalar(y);
248}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249
djsollen@google.come32b5832011-06-13 16:58:40 +0000250bool SampleWindow::setGrContext(GrContext* context)
251{
252 if (fGrContext) {
253 fGrContext->unref();
254 }
255 fGrContext = context;
256 fGrContext->ref();
257 return true;
258}
259
260GrContext* SampleWindow::getGrContext()
261{
262 return fGrContext;
263}
djsollen@google.come32b5832011-06-13 16:58:40 +0000264
Scroggo0f185c22011-03-24 18:35:50 +0000265bool SampleWindow::zoomIn()
266{
267 // Arbitrarily decided
268 if (fFatBitsScale == 25) return false;
269 fFatBitsScale++;
270 this->inval(NULL);
271 return true;
272}
273
274bool SampleWindow::zoomOut()
275{
276 if (fFatBitsScale == 1) return false;
277 fFatBitsScale--;
278 this->inval(NULL);
279 return true;
280}
281
282void SampleWindow::toggleZoomer()
283{
284 fShowZoomer = !fShowZoomer;
285 this->inval(NULL);
286}
287
288void SampleWindow::updatePointer(int x, int y)
289{
290 fMouseX = x;
291 fMouseY = y;
292 if (fShowZoomer) {
293 this->inval(NULL);
294 }
295}
296
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000297bool SampleWindow::make3DReady() {
298
299#if defined(SK_SUPPORT_GL)
bsalomon@google.com498a6232011-03-10 18:24:15 +0000300 if (attachGL()) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000301 if (NULL != fGrContext) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000302 // various gr lifecycle tests
303 #if 0
304 fGrContext->freeGpuResources();
305 #elif 0
306 // this will leak resources.
307 fGrContext->contextLost();
308 #elif 0
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000309 GrAssert(1 == fGrContext->refcnt());
310 fGrContext->unref();
311 fGrContext = NULL;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000312 #endif
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000313 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000314
bsalomon@google.com498a6232011-03-10 18:24:15 +0000315 if (NULL == fGrContext) {
316 #if defined(SK_USE_SHADERS)
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000317 fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000318 #else
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000319 fGrContext = GrContext::Create(kOpenGL_Fixed_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000320 #endif
reed@google.com569e0432011-04-05 13:07:03 +0000321 SkDebugf("---- constructor\n");
bsalomon@google.com498a6232011-03-10 18:24:15 +0000322 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000323
bsalomon@google.com498a6232011-03-10 18:24:15 +0000324 if (NULL != fGrContext) {
325 return true;
326 } else {
327 detachGL();
328 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000329 }
330#endif
331 SkDebugf("Failed to setup 3D");
332 return false;
333}
334
reed@android.com8a1c16f2008-12-17 15:59:43 +0000335SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) {
336 static const CanvasType gCT[] = {
337 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000338 kGPU_CanvasType,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000339 kRaster_CanvasType
340 };
341 return gCT[ct];
342}
343
344SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) {
yangsu@google.com1f394212011-06-01 18:03:34 +0000345#ifdef PIPE_FILE
346 //Clear existing file or create file if it doesn't exist
347 FILE* f = fopen(FILE_PATH, "wb");
348 fclose(f);
349#endif
350
reed@android.com8a1c16f2008-12-17 15:59:43 +0000351 fPicture = NULL;
reed@android.comf2b98d62010-12-20 18:26:13 +0000352 fGpuCanvas = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000354 fGrContext = NULL;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000355
reed@android.comf2b98d62010-12-20 18:26:13 +0000356#ifdef DEFAULT_TO_GPU
357 fCanvasType = kGPU_CanvasType;
358#else
reed@android.com8a1c16f2008-12-17 15:59:43 +0000359 fCanvasType = kRaster_CanvasType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000360#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000361 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000362 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000363 fRepeatDrawing = false;
364 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000365 fRotate = false;
366 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000367 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000368 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000369 fMeasureFPS = false;
reed@google.com569e0432011-04-05 13:07:03 +0000370 fLCDState = kUnknown_SkTriState;
371 fAAState = kUnknown_SkTriState;
reed@google.com66f22fd2011-05-17 15:33:45 +0000372 fFilterState = kUnknown_SkTriState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000373 fHintingState = kUnknown_SkTriState;
reed@google.com569e0432011-04-05 13:07:03 +0000374 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000375 fScrollTestX = fScrollTestY = 0;
376
Scroggo0f185c22011-03-24 18:35:50 +0000377 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000378 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000379 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
380 fShowZoomer = false;
381
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000382 fZoomLevel = 0;
383 fZoomScale = SK_Scalar1;
384
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000385// this->setConfig(SkBitmap::kRGB_565_Config);
386 this->setConfig(SkBitmap::kARGB_8888_Config);
387 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000388 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000389
reed@android.com34245c72009-11-03 04:00:48 +0000390 {
391 const SkViewRegister* reg = SkViewRegister::Head();
392 while (reg) {
393 *fSamples.append() = reg->factory();
394 reg = reg->next();
395 }
396 }
397 fCurrIndex = 0;
reed@android.come0f13ee2009-11-04 19:40:25 +0000398 this->loadView(fSamples[fCurrIndex]());
reed@google.comf0b5f682011-03-11 20:08:25 +0000399
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000400#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +0000401 testpdf();
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000402#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000403}
404
405SampleWindow::~SampleWindow() {
406 delete fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000407 delete fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000408 if (NULL != fGrContext) {
409 fGrContext->unref();
410 }
Scroggo0f185c22011-03-24 18:35:50 +0000411 fTypeface->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412}
413
reed@android.com55e76b22009-11-23 21:46:47 +0000414static SkBitmap capture_bitmap(SkCanvas* canvas) {
415 SkBitmap bm;
416 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
417 src.copyTo(&bm, src.config());
418 return bm;
419}
420
421static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
422 SkBitmap* diff) {
423 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000424
reed@android.com55e76b22009-11-23 21:46:47 +0000425 SkAutoLockPixels alp0(src);
426 SkAutoLockPixels alp1(orig);
427 for (int y = 0; y < src.height(); y++) {
428 const void* srcP = src.getAddr(0, y);
429 const void* origP = orig.getAddr(0, y);
430 size_t bytes = src.width() * src.bytesPerPixel();
431 if (memcmp(srcP, origP, bytes)) {
432 SkDebugf("---------- difference on line %d\n", y);
433 return true;
434 }
435 }
436 return false;
437}
438
Scroggo0f185c22011-03-24 18:35:50 +0000439static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
440{
441 SkColor desiredColor = paint.getColor();
442 paint.setColor(SK_ColorWHITE);
443 const char* c_str = string.c_str();
444 size_t size = string.size();
445 SkRect bounds;
446 paint.measureText(c_str, size, &bounds);
447 bounds.offset(left, top);
448 SkScalar inset = SkIntToScalar(-2);
449 bounds.inset(inset, inset);
450 canvas->drawRect(bounds, paint);
451 if (desiredColor != SK_ColorBLACK) {
452 paint.setColor(SK_ColorBLACK);
453 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
454 }
455 paint.setColor(desiredColor);
456 canvas->drawText(c_str, size, left, top, paint);
457}
458
reed@android.com44177402009-11-23 21:07:51 +0000459#define XCLIP_N 8
460#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000461
462void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000463 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000464 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000465 gAnimTime = SkTime::GetMSecs();
466
Scroggo2c8208f2011-06-15 16:49:08 +0000467 SkScalar cx = fZoomCenterX;
468 SkScalar cy = fZoomCenterY;
reed@google.com569e0432011-04-05 13:07:03 +0000469
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000470 if (fZoomLevel) {
471 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000472 SkPoint center;
473 m = canvas->getTotalMatrix();//.invert(&m);
474 m.mapXY(cx, cy, &center);
475 cx = center.fX;
476 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000477
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000478 m.setTranslate(-cx, -cy);
479 m.postScale(fZoomScale, fZoomScale);
480 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000481
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000482 canvas->concat(m);
483 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000484
reed@google.com569e0432011-04-05 13:07:03 +0000485 if (fFlipAxis) {
486 SkMatrix m;
487 m.setTranslate(cx, cy);
488 if (fFlipAxis & kFlipAxis_X) {
489 m.preScale(-SK_Scalar1, SK_Scalar1);
490 }
491 if (fFlipAxis & kFlipAxis_Y) {
492 m.preScale(SK_Scalar1, -SK_Scalar1);
493 }
494 m.preTranslate(-cx, -cy);
495 canvas->concat(m);
496 }
497
reed@google.com52f57e12011-03-16 12:10:02 +0000498 // Apply any gesture matrix
499 if (true) {
500 const SkMatrix& localM = fGesture.localM();
501 if (localM.getType() & SkMatrix::kScale_Mask) {
502 canvas->setExternalMatrix(&localM);
503 }
504 canvas->concat(localM);
505 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000506
reed@google.com52f57e12011-03-16 12:10:02 +0000507 if (fGesture.isActive()) {
508 this->inval(NULL);
509 }
510 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000511
reed@android.come522ca52009-11-23 20:10:41 +0000512 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000513 this->INHERITED::draw(canvas);
514 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000515
516 const SkScalar w = this->width();
517 const SkScalar h = this->height();
518 const SkScalar cw = w / XCLIP_N;
519 const SkScalar ch = h / YCLIP_N;
520 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000521 SkRect r;
522 r.fTop = y * ch;
523 r.fBottom = (y + 1) * ch;
524 if (y == YCLIP_N - 1) {
525 r.fBottom = h;
526 }
reed@android.come522ca52009-11-23 20:10:41 +0000527 for (int x = 0; x < XCLIP_N; x++) {
528 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000529 r.fLeft = x * cw;
530 r.fRight = (x + 1) * cw;
531 if (x == XCLIP_N - 1) {
532 r.fRight = w;
533 }
reed@android.come522ca52009-11-23 20:10:41 +0000534 canvas->clipRect(r);
535 this->INHERITED::draw(canvas);
536 }
537 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000538
reed@android.com55e76b22009-11-23 21:46:47 +0000539 SkBitmap diff;
540 if (bitmap_diff(canvas, orig, &diff)) {
541 }
reed@android.come522ca52009-11-23 20:10:41 +0000542 } else {
543 this->INHERITED::draw(canvas);
544 }
Scroggo3272ba82011-05-25 20:50:42 +0000545 if (fShowZoomer && fCanvasType != kGPU_CanvasType) {
546 // In the GPU case, INHERITED::draw calls beforeChildren, which
547 // creates an SkGpuCanvas. All further draw calls are directed
548 // at that canvas, which is deleted in afterChildren (which is
549 // also called by draw), so we cannot show the zoomer here.
550 // Instead, we call it inside afterChildren.
551 showZoomer(canvas);
552 }
553}
554
555void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +0000556 int count = canvas->save();
557 canvas->resetMatrix();
558 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000559 int width = SkScalarRound(this->width());
560 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000561 if (fMouseX >= width) fMouseX = width - 1;
562 else if (fMouseX < 0) fMouseX = 0;
563 if (fMouseY >= height) fMouseY = height - 1;
564 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000565
Scroggo0f185c22011-03-24 18:35:50 +0000566 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000567 bitmap.lockPixels();
568
Scroggo0f185c22011-03-24 18:35:50 +0000569 // 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 +0000570 int zoomedWidth = (width >> 1) | 1;
571 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000572 SkIRect src;
573 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
574 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
575 SkRect dest;
576 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
577 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
578 SkPaint paint;
579 // Clear the background behind our zoomed in view
580 paint.setColor(SK_ColorWHITE);
581 canvas->drawRect(dest, paint);
582 canvas->drawBitmapRect(bitmap, &src, dest);
583 paint.setColor(SK_ColorBLACK);
584 paint.setStyle(SkPaint::kStroke_Style);
585 // Draw a border around the pixel in the middle
586 SkRect originalPixel;
587 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
588 SkMatrix matrix;
589 SkRect scalarSrc;
590 scalarSrc.set(src);
591 SkColor color = bitmap.getColor(fMouseX, fMouseY);
592 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
593 SkRect pixel;
594 matrix.mapRect(&pixel, originalPixel);
595 // TODO Perhaps measure the values and make the outline white if it's "dark"
596 if (color == SK_ColorBLACK) {
597 paint.setColor(SK_ColorWHITE);
598 }
599 canvas->drawRect(pixel, paint);
600 }
601 paint.setColor(SK_ColorBLACK);
602 // Draw a border around the destination rectangle
603 canvas->drawRect(dest, paint);
604 paint.setStyle(SkPaint::kStrokeAndFill_Style);
605 // Identify the pixel and its color on screen
606 paint.setTypeface(fTypeface);
607 paint.setAntiAlias(true);
608 SkScalar lineHeight = paint.getFontMetrics(NULL);
609 SkString string;
610 string.appendf("(%i, %i)", fMouseX, fMouseY);
611 SkScalar left = dest.fLeft + SkIntToScalar(3);
612 SkScalar i = SK_Scalar1;
613 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
614 // Alpha
615 i += SK_Scalar1;
616 string.reset();
617 string.appendf("A: %X", SkColorGetA(color));
618 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
619 // Red
620 i += SK_Scalar1;
621 string.reset();
622 string.appendf("R: %X", SkColorGetR(color));
623 paint.setColor(SK_ColorRED);
624 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
625 // Green
626 i += SK_Scalar1;
627 string.reset();
628 string.appendf("G: %X", SkColorGetG(color));
629 paint.setColor(SK_ColorGREEN);
630 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
631 // Blue
632 i += SK_Scalar1;
633 string.reset();
634 string.appendf("B: %X", SkColorGetB(color));
635 paint.setColor(SK_ColorBLUE);
636 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
637 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +0000638}
639
reed@android.com8a1c16f2008-12-17 15:59:43 +0000640void SampleWindow::onDraw(SkCanvas* canvas) {
641 if (fRepeatDrawing) {
642 this->inval(NULL);
643 }
644}
645
646#include "SkColorPriv.h"
647
648static void reverseRedAndBlue(const SkBitmap& bm) {
649 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
650 uint8_t* p = (uint8_t*)bm.getPixels();
651 uint8_t* stop = p + bm.getSize();
652 while (p < stop) {
653 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
654 unsigned scale = SkAlpha255To256(p[3]);
655 unsigned r = p[2];
656 unsigned b = p[0];
657 p[0] = SkAlphaMul(r, scale);
658 p[1] = SkAlphaMul(p[1], scale);
659 p[2] = SkAlphaMul(b, scale);
660 p += 4;
661 }
662}
663
664SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000665 if (kGPU_CanvasType != fCanvasType) {
reed@android.com6efdc472008-12-19 18:24:35 +0000666#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000667 detachGL();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000668#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000669 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000670
reed@android.com8a1c16f2008-12-17 15:59:43 +0000671 switch (fCanvasType) {
672 case kRaster_CanvasType:
673 canvas = this->INHERITED::beforeChildren(canvas);
674 break;
675 case kPicture_CanvasType:
676 fPicture = new SkPicture;
677 canvas = fPicture->beginRecording(9999, 9999);
678 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000679 case kGPU_CanvasType: {
reed@google.com64e3eb22011-05-04 14:32:04 +0000680 if (make3DReady()) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000681 SkDevice* device = canvas->getDevice();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000682 const SkBitmap& bitmap = device->accessBitmap(true);
683
bsalomon@google.com5782d712011-01-21 21:03:59 +0000684 GrRenderTarget* renderTarget;
Scroggo3e7ff9f2011-06-16 15:31:26 +0000685
686 GrPlatformSurfaceDesc desc;
687 desc.reset();
688 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
689 desc.fWidth = bitmap.width();
690 desc.fHeight = bitmap.height();
691 desc.fConfig = kRGBA_8888_GrPixelConfig;
692 desc.fStencilBits = 8;
693 GrGLint buffer;
694 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer);
695 desc.fPlatformRenderTarget = buffer;
696
697 renderTarget = static_cast<GrRenderTarget*>(
698 fGrContext->createPlatformSurface(desc));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000699 fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
700 renderTarget->unref();
701
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000702 device = fGpuCanvas->createDevice(SkBitmap::kARGB_8888_Config,
703 bitmap.width(), bitmap.height(),
704 false, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000705 fGpuCanvas->setDevice(device)->unref();
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000706
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000707 fGpuCanvas->concat(canvas->getTotalMatrix());
reed@android.comf2b98d62010-12-20 18:26:13 +0000708 canvas = fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000709
reed@android.comf2b98d62010-12-20 18:26:13 +0000710 } else {
711 canvas = this->INHERITED::beforeChildren(canvas);
712 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000713 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000714 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000715 }
716
717 if (fUseClip) {
718 canvas->drawColor(0xFFFF88FF);
719 canvas->clipPath(fClipPath);
720 }
721
722 return canvas;
723}
724
725static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
726 const SkRegion& rgn) {
727 SkCanvas canvas(bm);
728 SkRegion inval(rgn);
729
730 inval.translate(r.fLeft, r.fTop);
731 canvas.clipRegion(inval);
732 canvas.drawColor(0xFFFF8080);
733}
734
735void SampleWindow::afterChildren(SkCanvas* orig) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000736 if (fRequestGrabImage) {
737 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000738
reed@android.comf2b98d62010-12-20 18:26:13 +0000739 SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
740 SkDevice* device = canvas->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000741 SkBitmap bmp;
742 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000743 static int gSampleGrabCounter;
744 SkString name;
745 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000746 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000747 SkImageEncoder::kPNG_Type, 100);
748 }
749 }
750
reed@android.com8a1c16f2008-12-17 15:59:43 +0000751 switch (fCanvasType) {
752 case kRaster_CanvasType:
753 break;
754 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000755 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000756 SkPicture* pict = new SkPicture(*fPicture);
757 fPicture->unref();
758 orig->drawPicture(*pict);
759 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000760 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000761 SkDynamicMemoryWStream ostream;
762 fPicture->serialize(&ostream);
763 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000764
reed@android.com8a1c16f2008-12-17 15:59:43 +0000765 SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
766 SkPicture pict(&istream);
767 orig->drawPicture(pict);
768 } else {
769 fPicture->draw(orig);
770 fPicture->unref();
771 }
772 fPicture = NULL;
773 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000774#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000775 case kGPU_CanvasType:
Scroggoaed68d92011-06-08 14:26:00 +0000776 if (fShowZoomer && fGpuCanvas) {
Scroggo3272ba82011-05-25 20:50:42 +0000777 this->showZoomer(fGpuCanvas);
778 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000779 delete fGpuCanvas;
780 fGpuCanvas = NULL;
781 presentGL();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000782 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000783#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000784 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000785
reed@google.com17d7aec2011-04-25 14:31:44 +0000786 // Do this after presentGL and other finishing, rather than in afterChild
787 if (fMeasureFPS && fMeasureFPS_Time) {
788 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
789 this->updateTitle();
790 postInvalDelay(this->getSinkID());
791 }
792
793 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000794 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000795 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
796 int dx = fScrollTestX * 7;
797 int dy = fScrollTestY * 7;
798 SkIRect r;
799 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000800
reed@android.com8a1c16f2008-12-17 15:59:43 +0000801 r.set(50, 50, 50+100, 50+100);
802 bm.scrollRect(&r, dx, dy, &inval);
803 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000804 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000805}
806
reed@android.com6c5f6f22009-08-14 16:08:38 +0000807void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
808 if (fScale) {
809 SkScalar scale = SK_Scalar1 * 7 / 10;
810 SkScalar cx = this->width() / 2;
811 SkScalar cy = this->height() / 2;
812 canvas->translate(cx, cy);
813 canvas->scale(scale, scale);
814 canvas->translate(-cx, -cy);
815 }
816 if (fRotate) {
817 SkScalar cx = this->width() / 2;
818 SkScalar cy = this->height() / 2;
819 canvas->translate(cx, cy);
820 canvas->rotate(SkIntToScalar(30));
821 canvas->translate(-cx, -cy);
822 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000823
reed@google.com09e3baa2011-05-18 12:04:31 +0000824 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
825 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000826
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000827 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000828 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
829 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
830 fMeasureFPS_Time = SkTime::GetMSecs();
831 }
832 } else {
833 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000834 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000835 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000836}
837
838void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000839 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000840}
841
reed@android.com8a1c16f2008-12-17 15:59:43 +0000842static SkBitmap::Config gConfigCycle[] = {
843 SkBitmap::kNo_Config, // none -> none
844 SkBitmap::kNo_Config, // a1 -> none
845 SkBitmap::kNo_Config, // a8 -> none
846 SkBitmap::kNo_Config, // index8 -> none
847 SkBitmap::kARGB_4444_Config, // 565 -> 4444
848 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
849 SkBitmap::kRGB_565_Config // 8888 -> 565
850};
851
852static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
853 return gConfigCycle[c];
854}
855
djsollen@google.come32b5832011-06-13 16:58:40 +0000856void SampleWindow::changeZoomLevel(float delta) {
857 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000858 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000859 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
860 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000861 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000862 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
863 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000864 } else {
865 fZoomScale = SK_Scalar1;
866 }
867
djsollen@google.come32b5832011-06-13 16:58:40 +0000868 this->updateTitle();
869
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000870 this->inval(NULL);
871}
872
Scroggo2c8208f2011-06-15 16:49:08 +0000873bool SampleWindow::previousSample() {
874 fCurrIndex = (fCurrIndex - 1) % fSamples.count();
875 this->loadView(fSamples[fCurrIndex]());
876 return true;
877}
878
reed@android.com8a1c16f2008-12-17 15:59:43 +0000879bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000880 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
881 this->loadView(fSamples[fCurrIndex]());
882 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000883}
884
Scroggo2c8208f2011-06-15 16:49:08 +0000885void SampleWindow::postAnimatingEvent() {
886 if (fAnimating) {
887 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
888 evt->post(this->getSinkID(), ANIMATING_DELAY);
889 }
890}
891
reed@android.com8a1c16f2008-12-17 15:59:43 +0000892bool SampleWindow::onEvent(const SkEvent& evt) {
893 if (evt.isType(ANIMATING_EVENTTYPE)) {
894 if (fAnimating) {
895 this->nextSample();
896 this->postAnimatingEvent();
897 }
898 return true;
899 }
reed@android.com34245c72009-11-03 04:00:48 +0000900 if (evt.isType("set-curr-index")) {
901 fCurrIndex = evt.getFast32() % fSamples.count();
902 this->loadView(fSamples[fCurrIndex]());
903 return true;
904 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000905 if (isInvalEvent(evt)) {
906 this->inval(NULL);
907 return true;
908 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000909 return this->INHERITED::onEvent(evt);
910}
911
reed@android.comf2b98d62010-12-20 18:26:13 +0000912bool SampleWindow::onQuery(SkEvent* query) {
913 if (query->isType("get-slide-count")) {
914 query->setFast32(fSamples.count());
915 return true;
916 }
917 if (query->isType("get-slide-title")) {
918 SkView* view = fSamples[query->getFast32()]();
919 SkEvent evt(gTitleEvtName);
920 if (view->doQuery(&evt)) {
921 query->setString("title", evt.findString(gTitleEvtName));
922 }
923 SkSafeUnref(view);
924 return true;
925 }
926 if (query->isType("use-fast-text")) {
927 SkEvent evt(gFastTextEvtName);
928 return curr_view(this)->doQuery(&evt);
929 }
930 return this->INHERITED::onQuery(query);
931}
932
reed@android.com0ae6b242008-12-23 16:49:54 +0000933static void cleanup_for_filename(SkString* name) {
934 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +0000935 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +0000936 switch (str[i]) {
937 case ':': str[i] = '-'; break;
938 case '/': str[i] = '-'; break;
939 case ' ': str[i] = '_'; break;
940 default: break;
941 }
942 }
943}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000944
945bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000946 {
947 SkView* view = curr_view(this);
948 if (view) {
949 SkEvent evt(gCharEvtName);
950 evt.setFast32(uni);
951 if (view->doQuery(&evt)) {
952 return true;
953 }
954 }
955 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000956
reed@android.com8a1c16f2008-12-17 15:59:43 +0000957 int dx = 0xFF;
958 int dy = 0xFF;
959
960 switch (uni) {
961 case '5': dx = 0; dy = 0; break;
962 case '8': dx = 0; dy = -1; break;
963 case '6': dx = 1; dy = 0; break;
964 case '2': dx = 0; dy = 1; break;
965 case '4': dx = -1; dy = 0; break;
966 case '7': dx = -1; dy = -1; break;
967 case '9': dx = 1; dy = -1; break;
968 case '3': dx = 1; dy = 1; break;
969 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000970
reed@android.com8a1c16f2008-12-17 15:59:43 +0000971 default:
972 break;
973 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000974
reed@android.com8a1c16f2008-12-17 15:59:43 +0000975 if (0xFF != dx && 0xFF != dy) {
976 if ((dx | dy) == 0) {
977 fScrollTestX = fScrollTestY = 0;
978 } else {
979 fScrollTestX += dx;
980 fScrollTestY += dy;
981 }
982 this->inval(NULL);
983 return true;
984 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000985
reed@android.com0ae6b242008-12-23 16:49:54 +0000986 switch (uni) {
987 case 'a':
Scroggo2c8208f2011-06-15 16:49:08 +0000988 this->toggleSlideshow();
reed@android.com0ae6b242008-12-23 16:49:54 +0000989 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000990 case 'b':
991 fAAState = cycle_tristate(fAAState);
992 this->updateTitle();
993 this->inval(NULL);
994 break;
995 case 'c':
996 fUseClip = !fUseClip;
997 this->inval(NULL);
998 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +0000999 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001000 case 'd':
1001 SkGraphics::SetFontCacheUsed(0);
1002 return true;
1003 case 'f':
Scroggo2c8208f2011-06-15 16:49:08 +00001004 this->toggleFPS();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001005 break;
1006 case 'g':
1007 fRequestGrabImage = true;
1008 this->inval(NULL);
1009 break;
reed@google.com09e3baa2011-05-18 12:04:31 +00001010 case 'h':
1011 fHintingState = cycle_tristate(fHintingState);
1012 this->updateTitle();
1013 this->inval(NULL);
1014 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001015 case 'i':
1016 this->zoomIn();
1017 break;
1018 case 'l':
1019 fLCDState = cycle_tristate(fLCDState);
1020 this->updateTitle();
1021 this->inval(NULL);
1022 break;
reed@google.com176753a2011-05-17 15:32:04 +00001023 case 'n':
1024 fFilterState = cycle_tristate(fFilterState);
1025 this->updateTitle();
1026 this->inval(NULL);
1027 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001028 case 'o':
1029 this->zoomOut();
1030 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001031 case 'p':
1032 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001033 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001034 this->inval(NULL);
1035 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001036 case 'r':
1037 fRotate = !fRotate;
1038 this->inval(NULL);
1039 this->updateTitle();
1040 return true;
1041 case 's':
1042 fScale = !fScale;
1043 this->inval(NULL);
1044 this->updateTitle();
1045 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001046 case 'x':
1047 fFlipAxis ^= kFlipAxis_X;
1048 this->updateTitle();
1049 this->inval(NULL);
1050 break;
1051 case 'y':
1052 fFlipAxis ^= kFlipAxis_Y;
1053 this->updateTitle();
1054 this->inval(NULL);
1055 break;
scroggo08526c02011-03-22 14:03:21 +00001056 case 'z':
1057 this->toggleZoomer();
1058 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001059 default:
1060 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001061 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001062
reed@android.com8a1c16f2008-12-17 15:59:43 +00001063 return this->INHERITED::onHandleChar(uni);
1064}
1065
Scroggo2c8208f2011-06-15 16:49:08 +00001066void SampleWindow::toggleFPS() {
1067 fMeasureFPS = !fMeasureFPS;
1068 this->inval(NULL);
1069 this->updateTitle();
1070}
1071
1072void SampleWindow::toggleSlideshow() {
1073 fAnimating = !fAnimating;
1074 this->postAnimatingEvent();
1075 this->updateTitle();
1076}
1077
1078void SampleWindow::toggleRendering() {
1079 fCanvasType = cycle_canvastype(fCanvasType);
1080 this->updateTitle();
1081 this->inval(NULL);
1082}
1083
reed@android.com8a1c16f2008-12-17 15:59:43 +00001084#include "SkDumpCanvas.h"
1085
1086bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001087 {
1088 SkView* view = curr_view(this);
1089 if (view) {
1090 SkEvent evt(gKeyEvtName);
1091 evt.setFast32(key);
1092 if (view->doQuery(&evt)) {
1093 return true;
1094 }
1095 }
1096 }
1097
reed@android.com8a1c16f2008-12-17 15:59:43 +00001098 switch (key) {
1099 case kRight_SkKey:
1100 if (this->nextSample()) {
1101 return true;
1102 }
1103 break;
1104 case kLeft_SkKey:
Scroggo2c8208f2011-06-15 16:49:08 +00001105 toggleRendering();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001106 return true;
1107 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001108 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001109 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001110 } else {
1111 fNClip = !fNClip;
1112 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001113 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001114 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001115 return true;
1116 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001117 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001118 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001119 } else {
1120 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001121 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001122 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001123 return true;
1124 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001125 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001126 SkDebugfDumper dumper;
1127 SkDumpCanvas dc(&dumper);
1128 this->draw(&dc);
1129 } else {
1130 fRepeatDrawing = !fRepeatDrawing;
1131 if (fRepeatDrawing) {
1132 this->inval(NULL);
1133 }
1134 }
1135 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001136 case kBack_SkKey:
1137 this->loadView(NULL);
1138 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001139 default:
1140 break;
1141 }
1142 return this->INHERITED::onHandleKey(key);
1143}
1144
reed@google.com52f57e12011-03-16 12:10:02 +00001145///////////////////////////////////////////////////////////////////////////////
1146
1147static const char gGestureClickType[] = "GestureClickType";
1148
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001149bool SampleWindow::onDispatchClick(int x, int y, Click::State state) {
Scroggo0f185c22011-03-24 18:35:50 +00001150 if (Click::kMoved_State == state) {
1151 updatePointer(x, y);
1152 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001153 int w = SkScalarRound(this->width());
1154 int h = SkScalarRound(this->height());
1155
1156 // check for the resize-box
1157 if (w - x < 16 && h - y < 16) {
1158 return false; // let the OS handle the click
1159 } else {
1160 return this->INHERITED::onDispatchClick(x, y, state);
1161 }
1162}
1163
reed@google.com52f57e12011-03-16 12:10:02 +00001164class GestureClick : public SkView::Click {
1165public:
1166 GestureClick(SkView* target) : SkView::Click(target) {
1167 this->setType(gGestureClickType);
1168 }
1169
1170 static bool IsGesture(Click* click) {
1171 return click->isType(gGestureClickType);
1172 }
1173};
1174
1175SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1176 return new GestureClick(this);
1177}
1178
1179bool SampleWindow::onClick(Click* click) {
1180 if (GestureClick::IsGesture(click)) {
1181 float x = SkScalarToFloat(click->fCurr.fX);
1182 float y = SkScalarToFloat(click->fCurr.fY);
1183 switch (click->fState) {
1184 case SkView::Click::kDown_State:
1185 fGesture.touchBegin(click, x, y);
1186 break;
1187 case SkView::Click::kMoved_State:
1188 fGesture.touchMoved(click, x, y);
1189 this->inval(NULL);
1190 break;
1191 case SkView::Click::kUp_State:
1192 fGesture.touchEnd(click);
1193 this->inval(NULL);
1194 break;
1195 }
1196 return true;
1197 }
1198 return false;
1199}
1200
1201///////////////////////////////////////////////////////////////////////////////
1202
reed@android.com8a1c16f2008-12-17 15:59:43 +00001203void SampleWindow::loadView(SkView* view) {
1204 SkView::F2BIter iter(this);
1205 SkView* prev = iter.next();
1206 if (prev) {
1207 prev->detachFromParent();
1208 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001209
reed@android.com34245c72009-11-03 04:00:48 +00001210 if (NULL == view) {
1211 view = create_overview(fSamples.count(), fSamples.begin());
1212 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001213 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001214 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001215 this->attachChildToFront(view)->unref();
1216 view->setSize(this->width(), this->height());
1217
1218 this->updateTitle();
1219}
1220
1221static const char* gConfigNames[] = {
1222 "unknown config",
1223 "A1",
1224 "A8",
1225 "Index8",
1226 "565",
1227 "4444",
1228 "8888"
1229};
1230
1231static const char* configToString(SkBitmap::Config c) {
1232 return gConfigNames[c];
1233}
1234
1235static const char* gCanvasTypePrefix[] = {
1236 "raster: ",
1237 "picture: ",
1238 "opengl: "
1239};
1240
reed@google.com569e0432011-04-05 13:07:03 +00001241static const char* trystate_str(SkTriState state,
1242 const char trueStr[], const char falseStr[]) {
1243 if (kTrue_SkTriState == state) {
1244 return trueStr;
1245 } else if (kFalse_SkTriState == state) {
1246 return falseStr;
1247 }
1248 return NULL;
1249}
1250
reed@android.com8a1c16f2008-12-17 15:59:43 +00001251void SampleWindow::updateTitle() {
1252 SkString title;
1253
1254 SkView::F2BIter iter(this);
1255 SkView* view = iter.next();
1256 SkEvent evt(gTitleEvtName);
1257 if (view->doQuery(&evt)) {
1258 title.set(evt.findString(gTitleEvtName));
1259 }
1260 if (title.size() == 0) {
1261 title.set("<unknown>");
1262 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001263
reed@android.com8a1c16f2008-12-17 15:59:43 +00001264 title.prepend(gCanvasTypePrefix[fCanvasType]);
1265
1266 title.prepend(" ");
1267 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001268
reed@android.com8a1c16f2008-12-17 15:59:43 +00001269 if (fAnimating) {
1270 title.prepend("<A> ");
1271 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001272 if (fScale) {
1273 title.prepend("<S> ");
1274 }
1275 if (fRotate) {
1276 title.prepend("<R> ");
1277 }
reed@android.come522ca52009-11-23 20:10:41 +00001278 if (fNClip) {
1279 title.prepend("<C> ");
1280 }
reed@google.com569e0432011-04-05 13:07:03 +00001281
1282 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1283 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001284 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001285 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1286 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001287
1288 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001289 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001290 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001291
1292 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001293 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001294 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001295 if (fUsePipe && SampleView::IsSampleView(view)) {
1296 title.prepend("<P> ");
1297 }
1298 if (SampleView::IsSampleView(view)) {
1299 title.prepend("! ");
1300 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001301
reed@android.com8a1c16f2008-12-17 15:59:43 +00001302 this->setTitle(title.c_str());
1303}
1304
1305void SampleWindow::onSizeChange() {
1306 this->INHERITED::onSizeChange();
1307
1308 SkView::F2BIter iter(this);
1309 SkView* view = iter.next();
1310 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001311
reed@android.com8a1c16f2008-12-17 15:59:43 +00001312 // rebuild our clippath
1313 {
1314 const SkScalar W = this->width();
1315 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001316
reed@android.com8a1c16f2008-12-17 15:59:43 +00001317 fClipPath.reset();
1318#if 0
1319 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1320 SkRect r;
1321 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1322 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1323 fClipPath.addRect(r);
1324 }
1325#else
1326 SkRect r;
1327 r.set(0, 0, W, H);
1328 fClipPath.addRect(r, SkPath::kCCW_Direction);
1329 r.set(W/4, H/4, W*3/4, H*3/4);
1330 fClipPath.addRect(r, SkPath::kCW_Direction);
1331#endif
1332 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001333
Scroggo2c8208f2011-06-15 16:49:08 +00001334 fZoomCenterX = SkScalarHalf(this->width());
1335 fZoomCenterY = SkScalarHalf(this->height());
1336
Scroggo3e7ff9f2011-06-16 15:31:26 +00001337#ifdef ANDROID
1338 postInvalDelay(this->getSinkID());
Scroggo716a0382011-06-16 14:00:15 +00001339#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001340 this->updateTitle(); // to refresh our config
1341}
1342
1343///////////////////////////////////////////////////////////////////////////////
1344
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001345static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001346static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001347static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001348
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001349bool SampleView::IsSampleView(SkView* view) {
1350 SkEvent evt(is_sample_view_tag);
1351 return view->doQuery(&evt);
1352}
1353
reed@google.comf2183392011-04-22 14:10:48 +00001354bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001355 SkEvent evt(repeat_count_tag);
1356 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001357 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001358}
1359
reed@google.com0faac1e2011-05-11 05:58:58 +00001360bool SampleView::SetUsePipe(SkView* view, bool pred) {
1361 SkEvent evt(set_use_pipe_tag);
1362 evt.setFast32(pred);
1363 return view->doEvent(evt);
1364}
1365
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001366bool SampleView::onEvent(const SkEvent& evt) {
1367 if (evt.isType(repeat_count_tag)) {
1368 fRepeatCount = evt.getFast32();
1369 return true;
1370 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001371 if (evt.isType(set_use_pipe_tag)) {
1372 fUsePipe = !!evt.getFast32();
1373 return true;
1374 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001375 return this->INHERITED::onEvent(evt);
1376}
1377
1378bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001379 if (evt->isType(is_sample_view_tag)) {
1380 return true;
1381 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001382 return this->INHERITED::onQuery(evt);
1383}
1384
reed@google.com68f456d2011-05-02 18:55:39 +00001385#ifdef TEST_GPIPE
1386 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001387
1388class SimplePC : public SkGPipeController {
1389public:
1390 SimplePC(SkCanvas* target);
1391 ~SimplePC();
1392
1393 virtual void* requestBlock(size_t minRequest, size_t* actual);
1394 virtual void notifyWritten(size_t bytes);
1395
1396private:
reed@google.com961ddb02011-05-05 14:03:48 +00001397 SkGPipeReader fReader;
1398 void* fBlock;
1399 size_t fBlockSize;
1400 size_t fBytesWritten;
1401 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001402 SkGPipeReader::Status fStatus;
1403
1404 size_t fTotalWritten;
1405};
1406
1407SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1408 fBlock = NULL;
1409 fBlockSize = fBytesWritten = 0;
1410 fStatus = SkGPipeReader::kDone_Status;
1411 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001412 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001413}
1414
1415SimplePC::~SimplePC() {
1416// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1417 sk_free(fBlock);
1418
reed@google.com0faac1e2011-05-11 05:58:58 +00001419 if (fTotalWritten) {
1420 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1421 fAtomsWritten, fStatus);
1422 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001423}
1424
1425void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1426 sk_free(fBlock);
1427
1428 fBlockSize = minRequest * 4;
1429 fBlock = sk_malloc_throw(fBlockSize);
1430 fBytesWritten = 0;
1431 *actual = fBlockSize;
1432 return fBlock;
1433}
1434
1435void SimplePC::notifyWritten(size_t bytes) {
1436 SkASSERT(fBytesWritten + bytes <= fBlockSize);
yangsu@google.com1f394212011-06-01 18:03:34 +00001437
1438#ifdef PIPE_FILE
1439 //File is open in append mode
1440 FILE* f = fopen(FILE_PATH, "ab");
1441 SkASSERT(f != NULL);
1442 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
1443 fclose(f);
1444#endif
1445
reed@google.com64e3eb22011-05-04 14:32:04 +00001446 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1447 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1448 fBytesWritten += bytes;
1449 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001450
1451 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001452}
1453
reed@google.com68f456d2011-05-02 18:55:39 +00001454#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001455
reed@google.com64e3eb22011-05-04 14:32:04 +00001456
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001457void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001458#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001459 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001460 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001461 if (fUsePipe) {
reed@google.comdde09562011-05-23 12:21:05 +00001462 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
1463// flags = 0;
1464 canvas = writer.startRecording(&controller, flags);
reed@google.com0faac1e2011-05-11 05:58:58 +00001465 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001466#endif
1467
reed@google.com81e3d7f2011-06-01 12:42:36 +00001468 this->onDrawBackground(canvas);
1469
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001470 for (int i = 0; i < fRepeatCount; i++) {
1471 SkAutoCanvasRestore acr(canvas, true);
1472 this->onDrawContent(canvas);
1473 }
1474}
1475
1476void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001477 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001478}
1479
1480///////////////////////////////////////////////////////////////////////////////
1481
reed@android.comf2b98d62010-12-20 18:26:13 +00001482template <typename T> void SkTBSort(T array[], int count) {
1483 for (int i = 1; i < count - 1; i++) {
1484 bool didSwap = false;
1485 for (int j = count - 1; j > i; --j) {
1486 if (array[j] < array[j-1]) {
1487 T tmp(array[j-1]);
1488 array[j-1] = array[j];
1489 array[j] = tmp;
1490 didSwap = true;
1491 }
1492 }
1493 if (!didSwap) {
1494 break;
1495 }
1496 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001497
reed@android.comf2b98d62010-12-20 18:26:13 +00001498 for (int k = 0; k < count - 1; k++) {
1499 SkASSERT(!(array[k+1] < array[k]));
1500 }
1501}
1502
1503#include "SkRandom.h"
1504
1505static void rand_rect(SkIRect* rect, SkRandom& rand) {
1506 int bits = 8;
1507 int shift = 32 - bits;
1508 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1509 rand.nextU() >> shift, rand.nextU() >> shift);
1510 rect->sort();
1511}
1512
1513static void dumpRect(const SkIRect& r) {
1514 SkDebugf(" { %d, %d, %d, %d },\n",
1515 r.fLeft, r.fTop,
1516 r.fRight, r.fBottom);
1517}
1518
1519static void test_rects(const SkIRect rect[], int count) {
1520 SkRegion rgn0, rgn1;
1521
1522 for (int i = 0; i < count; i++) {
1523 rgn0.op(rect[i], SkRegion::kUnion_Op);
1524 // dumpRect(rect[i]);
1525 }
1526 rgn1.setRects(rect, count);
1527
1528 if (rgn0 != rgn1) {
1529 SkDebugf("\n");
1530 for (int i = 0; i < count; i++) {
1531 dumpRect(rect[i]);
1532 }
1533 SkDebugf("\n");
1534 }
1535}
1536
1537static void test() {
1538 size_t i;
1539
1540 const SkIRect r0[] = {
1541 { 0, 0, 1, 1 },
1542 { 2, 2, 3, 3 },
1543 };
1544 const SkIRect r1[] = {
1545 { 0, 0, 1, 3 },
1546 { 1, 1, 2, 2 },
1547 { 2, 0, 3, 3 },
1548 };
1549 const SkIRect r2[] = {
1550 { 0, 0, 1, 2 },
1551 { 2, 1, 3, 3 },
1552 { 4, 0, 5, 1 },
1553 { 6, 0, 7, 4 },
1554 };
1555
1556 static const struct {
1557 const SkIRect* fRects;
1558 int fCount;
1559 } gRecs[] = {
1560 { r0, SK_ARRAY_COUNT(r0) },
1561 { r1, SK_ARRAY_COUNT(r1) },
1562 { r2, SK_ARRAY_COUNT(r2) },
1563 };
1564
1565 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1566 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1567 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001568
reed@android.comf2b98d62010-12-20 18:26:13 +00001569 SkRandom rand;
1570 for (i = 0; i < 10000; i++) {
1571 SkRegion rgn0, rgn1;
1572
1573 const int N = 8;
1574 SkIRect rect[N];
1575 for (int j = 0; j < N; j++) {
1576 rand_rect(&rect[j], rand);
1577 }
1578 test_rects(rect, N);
1579 }
1580}
1581
reed@android.com8a1c16f2008-12-17 15:59:43 +00001582SkOSWindow* create_sk_window(void* hwnd) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001583// test();
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001584 return new SampleWindow(hwnd);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001585}
1586
1587void get_preferred_size(int* x, int* y, int* width, int* height) {
1588 *x = 10;
1589 *y = 50;
1590 *width = 640;
1591 *height = 480;
1592}
1593
1594void application_init() {
1595// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001596#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001597 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001598#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001599 SkGraphics::Init();
1600 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001601}
1602
1603void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001604 SkEvent::Term();
1605 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001606}