blob: c457ef68a8d644875f3c5f39baed932d919c1b64 [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
Scroggo2c8208f2011-06-15 16:49:08 +000018#ifdef ANDROID
19 #include "gl2.h"
20#endif
21
yangsu@google.com1f394212011-06-01 18:03:34 +000022#define TEST_GPIPEx
23
24#ifdef TEST_GPIPE
25#define PIPE_FILE
26#define FILE_PATH "/path/to/drawing.data"
27#endif
28
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +000029#define USE_ARROWS_FOR_ZOOM true
reed@android.comf2b98d62010-12-20 18:26:13 +000030//#define DEFAULT_TO_GPU
31
reed@android.come191b162009-12-18 21:33:39 +000032extern SkView* create_overview(int, const SkViewFactory[]);
reed@android.com34245c72009-11-03 04:00:48 +000033
reed@android.comcb342352010-07-22 18:27:53 +000034#define SK_SUPPORT_GL
reed@android.com8a1c16f2008-12-17 15:59:43 +000035
36#define ANIMATING_EVENTTYPE "nextSample"
37#define ANIMATING_DELAY 750
38
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000039#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +000040 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000041#else
reed@google.combad8c872011-05-18 20:10:31 +000042 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000043#endif
reed@google.combad8c872011-05-18 20:10:31 +000044#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000045
reed@google.comac10a2d2010-12-22 21:39:39 +000046#ifdef SK_SUPPORT_GL
47 #include "GrGLConfig.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000048#endif
49
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000050///////////////
51static const char view_inval_msg[] = "view-inval-msg";
52
53static void postInvalDelay(SkEventSinkID sinkID) {
54 SkEvent* evt = new SkEvent(view_inval_msg);
reed@google.comf2183392011-04-22 14:10:48 +000055 evt->post(sinkID, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000056}
57
58static bool isInvalEvent(const SkEvent& evt) {
59 return evt.isType(view_inval_msg);
60}
61//////////////////
62
reed@android.com8a1c16f2008-12-17 15:59:43 +000063SkViewRegister* SkViewRegister::gHead;
64SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) {
65 static bool gOnce;
66 if (!gOnce) {
67 gHead = NULL;
68 gOnce = true;
69 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000070
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 fChain = gHead;
72 gHead = this;
73}
74
reed@android.comf2b98d62010-12-20 18:26:13 +000075#if defined(SK_SUPPORT_GL)
76 #define SK_USE_SHADERS
77#endif
78
twiz@google.com06c3b6b2011-03-14 16:58:39 +000079#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +000080#include <CoreFoundation/CoreFoundation.h>
81#include <CoreFoundation/CFURLAccess.h>
82
83static void testpdf() {
84 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
85 kCFStringEncodingUTF8);
86 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
87 kCFURLPOSIXPathStyle,
88 false);
89 CFRelease(path);
90 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
91 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
92 CFRelease(url);
93
94 CGContextBeginPage(cg, &box);
95 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
96 CGContextFillEllipseInRect(cg, r);
97 CGContextEndPage(cg);
98 CGContextRelease(cg);
99
100 if (false) {
101 SkBitmap bm;
102 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
103 bm.allocPixels();
104 bm.eraseColor(0);
105
106 SkCanvas canvas(bm);
107
108 }
109}
110#endif
111
112//////////////////////////////////////////////////////////////////////////////
113
reed@google.com569e0432011-04-05 13:07:03 +0000114enum FlipAxisEnum {
115 kFlipAxis_X = (1 << 0),
116 kFlipAxis_Y = (1 << 1)
117};
118
reed@google.com569e0432011-04-05 13:07:03 +0000119static SkTriState cycle_tristate(SkTriState state) {
120 static const SkTriState gCycle[] = {
121 /* kFalse_SkTriState -> */ kUnknown_SkTriState,
122 /* kTrue_SkTriState -> */ kFalse_SkTriState,
123 /* kUnknown_SkTriState -> */ kTrue_SkTriState,
124 };
125 return gCycle[state];
126}
127
reed@google.comf0b5f682011-03-11 20:08:25 +0000128#include "SkDrawFilter.h"
129
reed@google.com569e0432011-04-05 13:07:03 +0000130class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000131public:
reed@google.com09e3baa2011-05-18 12:04:31 +0000132 FlagsDrawFilter(SkTriState lcd, SkTriState aa, SkTriState filter,
133 SkTriState hinting) :
134 fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000135
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000136 virtual void filter(SkPaint* paint, Type t) {
reed@google.com569e0432011-04-05 13:07:03 +0000137 if (kText_Type == t && kUnknown_SkTriState != fLCDState) {
reed@google.com569e0432011-04-05 13:07:03 +0000138 paint->setLCDRenderText(kTrue_SkTriState == fLCDState);
139 }
140 if (kUnknown_SkTriState != fAAState) {
reed@google.com569e0432011-04-05 13:07:03 +0000141 paint->setAntiAlias(kTrue_SkTriState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000142 }
reed@google.com176753a2011-05-17 15:32:04 +0000143 if (kUnknown_SkTriState != fFilterState) {
144 paint->setFilterBitmap(kTrue_SkTriState == fFilterState);
145 }
reed@google.com09e3baa2011-05-18 12:04:31 +0000146 if (kUnknown_SkTriState != fHintingState) {
147 paint->setHinting(kTrue_SkTriState == fHintingState ?
148 SkPaint::kNormal_Hinting :
149 SkPaint::kSlight_Hinting);
150 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000151 }
152
153private:
reed@google.com569e0432011-04-05 13:07:03 +0000154 SkTriState fLCDState;
reed@google.com569e0432011-04-05 13:07:03 +0000155 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000156 SkTriState fFilterState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000157 SkTriState fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000158};
159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160//////////////////////////////////////////////////////////////////////////////
161
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000162#define MAX_ZOOM_LEVEL 8
163#define MIN_ZOOM_LEVEL -8
164
reed@android.comf2b98d62010-12-20 18:26:13 +0000165static const char gCharEvtName[] = "SampleCode_Char_Event";
166static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167static const char gTitleEvtName[] = "SampleCode_Title_Event";
168static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000169static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
170
171bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
172 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
173 if (outUni) {
174 *outUni = evt.getFast32();
175 }
176 return true;
177 }
178 return false;
179}
180
181bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
182 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
183 if (outKey) {
184 *outKey = (SkKey)evt.getFast32();
185 }
186 return true;
187 }
188 return false;
189}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190
191bool SampleCode::TitleQ(const SkEvent& evt) {
192 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
193}
194
195void SampleCode::TitleR(SkEvent* evt, const char title[]) {
196 SkASSERT(evt && TitleQ(*evt));
197 evt->setString(gTitleEvtName, title);
198}
199
200bool SampleCode::PrefSizeQ(const SkEvent& evt) {
201 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
202}
203
204void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
205 SkASSERT(evt && PrefSizeQ(*evt));
206 SkScalar size[2];
207 size[0] = width;
208 size[1] = height;
209 evt->setScalars(gPrefSizeEvtName, 2, size);
210}
211
reed@android.comf2b98d62010-12-20 18:26:13 +0000212bool SampleCode::FastTextQ(const SkEvent& evt) {
213 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
214}
215
216///////////////////////////////////////////////////////////////////////////////
217
reed@android.com44177402009-11-23 21:07:51 +0000218static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000219static SkMSec gAnimTimePrev;
220
reed@android.com44177402009-11-23 21:07:51 +0000221SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000222SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
223SkScalar SampleCode::GetAnimSecondsDelta() {
224 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
225}
reed@android.com44177402009-11-23 21:07:51 +0000226
227SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000228 // since gAnimTime can be up to 32 bits, we can't convert it to a float
229 // or we'll lose the low bits. Hence we use doubles for the intermediate
230 // calculations
231 double seconds = (double)gAnimTime / 1000.0;
232 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000233 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000234 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000235 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000236 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000237}
238
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239//////////////////////////////////////////////////////////////////////////////
240
reed@android.comf2b98d62010-12-20 18:26:13 +0000241static SkView* curr_view(SkWindow* wind) {
242 SkView::F2BIter iter(wind);
243 return iter.next();
244}
245
Scroggo2c8208f2011-06-15 16:49:08 +0000246void SampleWindow::setZoomCenter(float x, float y)
247{
248 fZoomCenterX = SkFloatToScalar(x);
249 fZoomCenterY = SkFloatToScalar(y);
250}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251
djsollen@google.come32b5832011-06-13 16:58:40 +0000252bool SampleWindow::setGrContext(GrContext* context)
253{
254 if (fGrContext) {
255 fGrContext->unref();
256 }
257 fGrContext = context;
258 fGrContext->ref();
259 return true;
260}
261
262GrContext* SampleWindow::getGrContext()
263{
264 return fGrContext;
265}
djsollen@google.come32b5832011-06-13 16:58:40 +0000266
Scroggo0f185c22011-03-24 18:35:50 +0000267bool SampleWindow::zoomIn()
268{
269 // Arbitrarily decided
270 if (fFatBitsScale == 25) return false;
271 fFatBitsScale++;
272 this->inval(NULL);
273 return true;
274}
275
276bool SampleWindow::zoomOut()
277{
278 if (fFatBitsScale == 1) return false;
279 fFatBitsScale--;
280 this->inval(NULL);
281 return true;
282}
283
284void SampleWindow::toggleZoomer()
285{
286 fShowZoomer = !fShowZoomer;
287 this->inval(NULL);
288}
289
290void SampleWindow::updatePointer(int x, int y)
291{
292 fMouseX = x;
293 fMouseY = y;
294 if (fShowZoomer) {
295 this->inval(NULL);
296 }
297}
298
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000299bool SampleWindow::make3DReady() {
300
301#if defined(SK_SUPPORT_GL)
bsalomon@google.com498a6232011-03-10 18:24:15 +0000302 if (attachGL()) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000303 if (NULL != fGrContext) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000304 // various gr lifecycle tests
305 #if 0
306 fGrContext->freeGpuResources();
307 #elif 0
308 // this will leak resources.
309 fGrContext->contextLost();
310 #elif 0
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000311 GrAssert(1 == fGrContext->refcnt());
312 fGrContext->unref();
313 fGrContext = NULL;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000314 #endif
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000315 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000316
bsalomon@google.com498a6232011-03-10 18:24:15 +0000317 if (NULL == fGrContext) {
318 #if defined(SK_USE_SHADERS)
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000319 fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000320 #else
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000321 fGrContext = GrContext::Create(kOpenGL_Fixed_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000322 #endif
reed@google.com569e0432011-04-05 13:07:03 +0000323 SkDebugf("---- constructor\n");
bsalomon@google.com498a6232011-03-10 18:24:15 +0000324 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000325
bsalomon@google.com498a6232011-03-10 18:24:15 +0000326 if (NULL != fGrContext) {
327 return true;
328 } else {
329 detachGL();
330 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000331 }
332#endif
333 SkDebugf("Failed to setup 3D");
334 return false;
335}
336
reed@android.com8a1c16f2008-12-17 15:59:43 +0000337SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) {
338 static const CanvasType gCT[] = {
339 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000340 kGPU_CanvasType,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 kRaster_CanvasType
342 };
343 return gCT[ct];
344}
345
346SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) {
yangsu@google.com1f394212011-06-01 18:03:34 +0000347#ifdef PIPE_FILE
348 //Clear existing file or create file if it doesn't exist
349 FILE* f = fopen(FILE_PATH, "wb");
350 fclose(f);
351#endif
352
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353 fPicture = NULL;
reed@android.comf2b98d62010-12-20 18:26:13 +0000354 fGpuCanvas = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000355
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000356 fGrContext = NULL;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000357
reed@android.comf2b98d62010-12-20 18:26:13 +0000358#ifdef DEFAULT_TO_GPU
359 fCanvasType = kGPU_CanvasType;
360#else
reed@android.com8a1c16f2008-12-17 15:59:43 +0000361 fCanvasType = kRaster_CanvasType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000362#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000363 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000364 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365 fRepeatDrawing = false;
366 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000367 fRotate = false;
368 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000369 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000370 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000371 fMeasureFPS = false;
reed@google.com569e0432011-04-05 13:07:03 +0000372 fLCDState = kUnknown_SkTriState;
373 fAAState = kUnknown_SkTriState;
reed@google.com66f22fd2011-05-17 15:33:45 +0000374 fFilterState = kUnknown_SkTriState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000375 fHintingState = kUnknown_SkTriState;
reed@google.com569e0432011-04-05 13:07:03 +0000376 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000377 fScrollTestX = fScrollTestY = 0;
378
Scroggo0f185c22011-03-24 18:35:50 +0000379 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000380 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000381 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
382 fShowZoomer = false;
383
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000384 fZoomLevel = 0;
385 fZoomScale = SK_Scalar1;
386
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000387// this->setConfig(SkBitmap::kRGB_565_Config);
388 this->setConfig(SkBitmap::kARGB_8888_Config);
389 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000390 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000391
reed@android.com34245c72009-11-03 04:00:48 +0000392 {
393 const SkViewRegister* reg = SkViewRegister::Head();
394 while (reg) {
395 *fSamples.append() = reg->factory();
396 reg = reg->next();
397 }
398 }
399 fCurrIndex = 0;
reed@android.come0f13ee2009-11-04 19:40:25 +0000400 this->loadView(fSamples[fCurrIndex]());
reed@google.comf0b5f682011-03-11 20:08:25 +0000401
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000402#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +0000403 testpdf();
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000404#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000405}
406
407SampleWindow::~SampleWindow() {
408 delete fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000409 delete fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000410 if (NULL != fGrContext) {
411 fGrContext->unref();
412 }
Scroggo0f185c22011-03-24 18:35:50 +0000413 fTypeface->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414}
415
reed@android.com55e76b22009-11-23 21:46:47 +0000416static SkBitmap capture_bitmap(SkCanvas* canvas) {
417 SkBitmap bm;
418 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
419 src.copyTo(&bm, src.config());
420 return bm;
421}
422
423static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
424 SkBitmap* diff) {
425 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000426
reed@android.com55e76b22009-11-23 21:46:47 +0000427 SkAutoLockPixels alp0(src);
428 SkAutoLockPixels alp1(orig);
429 for (int y = 0; y < src.height(); y++) {
430 const void* srcP = src.getAddr(0, y);
431 const void* origP = orig.getAddr(0, y);
432 size_t bytes = src.width() * src.bytesPerPixel();
433 if (memcmp(srcP, origP, bytes)) {
434 SkDebugf("---------- difference on line %d\n", y);
435 return true;
436 }
437 }
438 return false;
439}
440
Scroggo0f185c22011-03-24 18:35:50 +0000441static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
442{
443 SkColor desiredColor = paint.getColor();
444 paint.setColor(SK_ColorWHITE);
445 const char* c_str = string.c_str();
446 size_t size = string.size();
447 SkRect bounds;
448 paint.measureText(c_str, size, &bounds);
449 bounds.offset(left, top);
450 SkScalar inset = SkIntToScalar(-2);
451 bounds.inset(inset, inset);
452 canvas->drawRect(bounds, paint);
453 if (desiredColor != SK_ColorBLACK) {
454 paint.setColor(SK_ColorBLACK);
455 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
456 }
457 paint.setColor(desiredColor);
458 canvas->drawText(c_str, size, left, top, paint);
459}
460
reed@android.com44177402009-11-23 21:07:51 +0000461#define XCLIP_N 8
462#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000463
464void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000465 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000466 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000467 gAnimTime = SkTime::GetMSecs();
468
Scroggo2c8208f2011-06-15 16:49:08 +0000469 SkScalar cx = fZoomCenterX;
470 SkScalar cy = fZoomCenterY;
reed@google.com569e0432011-04-05 13:07:03 +0000471
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000472 if (fZoomLevel) {
473 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000474 SkPoint center;
475 m = canvas->getTotalMatrix();//.invert(&m);
476 m.mapXY(cx, cy, &center);
477 cx = center.fX;
478 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000479
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000480 m.setTranslate(-cx, -cy);
481 m.postScale(fZoomScale, fZoomScale);
482 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000483
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000484 canvas->concat(m);
485 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000486
reed@google.com569e0432011-04-05 13:07:03 +0000487 if (fFlipAxis) {
488 SkMatrix m;
489 m.setTranslate(cx, cy);
490 if (fFlipAxis & kFlipAxis_X) {
491 m.preScale(-SK_Scalar1, SK_Scalar1);
492 }
493 if (fFlipAxis & kFlipAxis_Y) {
494 m.preScale(SK_Scalar1, -SK_Scalar1);
495 }
496 m.preTranslate(-cx, -cy);
497 canvas->concat(m);
498 }
499
reed@google.com52f57e12011-03-16 12:10:02 +0000500 // Apply any gesture matrix
501 if (true) {
502 const SkMatrix& localM = fGesture.localM();
503 if (localM.getType() & SkMatrix::kScale_Mask) {
504 canvas->setExternalMatrix(&localM);
505 }
506 canvas->concat(localM);
507 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000508
reed@google.com52f57e12011-03-16 12:10:02 +0000509 if (fGesture.isActive()) {
510 this->inval(NULL);
511 }
512 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000513
reed@android.come522ca52009-11-23 20:10:41 +0000514 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000515 this->INHERITED::draw(canvas);
516 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000517
518 const SkScalar w = this->width();
519 const SkScalar h = this->height();
520 const SkScalar cw = w / XCLIP_N;
521 const SkScalar ch = h / YCLIP_N;
522 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000523 SkRect r;
524 r.fTop = y * ch;
525 r.fBottom = (y + 1) * ch;
526 if (y == YCLIP_N - 1) {
527 r.fBottom = h;
528 }
reed@android.come522ca52009-11-23 20:10:41 +0000529 for (int x = 0; x < XCLIP_N; x++) {
530 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000531 r.fLeft = x * cw;
532 r.fRight = (x + 1) * cw;
533 if (x == XCLIP_N - 1) {
534 r.fRight = w;
535 }
reed@android.come522ca52009-11-23 20:10:41 +0000536 canvas->clipRect(r);
537 this->INHERITED::draw(canvas);
538 }
539 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000540
reed@android.com55e76b22009-11-23 21:46:47 +0000541 SkBitmap diff;
542 if (bitmap_diff(canvas, orig, &diff)) {
543 }
reed@android.come522ca52009-11-23 20:10:41 +0000544 } else {
545 this->INHERITED::draw(canvas);
546 }
Scroggo3272ba82011-05-25 20:50:42 +0000547 if (fShowZoomer && fCanvasType != kGPU_CanvasType) {
548 // In the GPU case, INHERITED::draw calls beforeChildren, which
549 // creates an SkGpuCanvas. All further draw calls are directed
550 // at that canvas, which is deleted in afterChildren (which is
551 // also called by draw), so we cannot show the zoomer here.
552 // Instead, we call it inside afterChildren.
553 showZoomer(canvas);
554 }
555}
556
557void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +0000558 int count = canvas->save();
559 canvas->resetMatrix();
560 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000561 int width = SkScalarRound(this->width());
562 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000563 if (fMouseX >= width) fMouseX = width - 1;
564 else if (fMouseX < 0) fMouseX = 0;
565 if (fMouseY >= height) fMouseY = height - 1;
566 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000567
Scroggo0f185c22011-03-24 18:35:50 +0000568 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000569 bitmap.lockPixels();
570
Scroggo0f185c22011-03-24 18:35:50 +0000571 // 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 +0000572 int zoomedWidth = (width >> 1) | 1;
573 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000574 SkIRect src;
575 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
576 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
577 SkRect dest;
578 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
579 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
580 SkPaint paint;
581 // Clear the background behind our zoomed in view
582 paint.setColor(SK_ColorWHITE);
583 canvas->drawRect(dest, paint);
584 canvas->drawBitmapRect(bitmap, &src, dest);
585 paint.setColor(SK_ColorBLACK);
586 paint.setStyle(SkPaint::kStroke_Style);
587 // Draw a border around the pixel in the middle
588 SkRect originalPixel;
589 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
590 SkMatrix matrix;
591 SkRect scalarSrc;
592 scalarSrc.set(src);
593 SkColor color = bitmap.getColor(fMouseX, fMouseY);
594 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
595 SkRect pixel;
596 matrix.mapRect(&pixel, originalPixel);
597 // TODO Perhaps measure the values and make the outline white if it's "dark"
598 if (color == SK_ColorBLACK) {
599 paint.setColor(SK_ColorWHITE);
600 }
601 canvas->drawRect(pixel, paint);
602 }
603 paint.setColor(SK_ColorBLACK);
604 // Draw a border around the destination rectangle
605 canvas->drawRect(dest, paint);
606 paint.setStyle(SkPaint::kStrokeAndFill_Style);
607 // Identify the pixel and its color on screen
608 paint.setTypeface(fTypeface);
609 paint.setAntiAlias(true);
610 SkScalar lineHeight = paint.getFontMetrics(NULL);
611 SkString string;
612 string.appendf("(%i, %i)", fMouseX, fMouseY);
613 SkScalar left = dest.fLeft + SkIntToScalar(3);
614 SkScalar i = SK_Scalar1;
615 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
616 // Alpha
617 i += SK_Scalar1;
618 string.reset();
619 string.appendf("A: %X", SkColorGetA(color));
620 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
621 // Red
622 i += SK_Scalar1;
623 string.reset();
624 string.appendf("R: %X", SkColorGetR(color));
625 paint.setColor(SK_ColorRED);
626 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
627 // Green
628 i += SK_Scalar1;
629 string.reset();
630 string.appendf("G: %X", SkColorGetG(color));
631 paint.setColor(SK_ColorGREEN);
632 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
633 // Blue
634 i += SK_Scalar1;
635 string.reset();
636 string.appendf("B: %X", SkColorGetB(color));
637 paint.setColor(SK_ColorBLUE);
638 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
639 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +0000640}
641
reed@android.com8a1c16f2008-12-17 15:59:43 +0000642void SampleWindow::onDraw(SkCanvas* canvas) {
643 if (fRepeatDrawing) {
644 this->inval(NULL);
645 }
646}
647
648#include "SkColorPriv.h"
649
650static void reverseRedAndBlue(const SkBitmap& bm) {
651 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
652 uint8_t* p = (uint8_t*)bm.getPixels();
653 uint8_t* stop = p + bm.getSize();
654 while (p < stop) {
655 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
656 unsigned scale = SkAlpha255To256(p[3]);
657 unsigned r = p[2];
658 unsigned b = p[0];
659 p[0] = SkAlphaMul(r, scale);
660 p[1] = SkAlphaMul(p[1], scale);
661 p[2] = SkAlphaMul(b, scale);
662 p += 4;
663 }
664}
665
666SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000667 if (kGPU_CanvasType != fCanvasType) {
reed@android.com6efdc472008-12-19 18:24:35 +0000668#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000669 detachGL();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000670#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000671 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000672
reed@android.com8a1c16f2008-12-17 15:59:43 +0000673 switch (fCanvasType) {
674 case kRaster_CanvasType:
675 canvas = this->INHERITED::beforeChildren(canvas);
676 break;
677 case kPicture_CanvasType:
678 fPicture = new SkPicture;
679 canvas = fPicture->beginRecording(9999, 9999);
680 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000681 case kGPU_CanvasType: {
reed@google.com64e3eb22011-05-04 14:32:04 +0000682 if (make3DReady()) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000683 SkDevice* device = canvas->getDevice();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000684 const SkBitmap& bitmap = device->accessBitmap(true);
685
bsalomon@google.com5782d712011-01-21 21:03:59 +0000686 GrRenderTarget* renderTarget;
687 renderTarget = fGrContext->createRenderTargetFrom3DApiState();
688 fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
689 renderTarget->unref();
690
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000691 device = fGpuCanvas->createDevice(SkBitmap::kARGB_8888_Config,
692 bitmap.width(), bitmap.height(),
693 false, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000694 fGpuCanvas->setDevice(device)->unref();
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000695
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000696 fGpuCanvas->concat(canvas->getTotalMatrix());
reed@android.comf2b98d62010-12-20 18:26:13 +0000697 canvas = fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000698
reed@android.comf2b98d62010-12-20 18:26:13 +0000699 } else {
700 canvas = this->INHERITED::beforeChildren(canvas);
701 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000702 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000703 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000704 }
705
706 if (fUseClip) {
707 canvas->drawColor(0xFFFF88FF);
708 canvas->clipPath(fClipPath);
709 }
710
711 return canvas;
712}
713
714static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
715 const SkRegion& rgn) {
716 SkCanvas canvas(bm);
717 SkRegion inval(rgn);
718
719 inval.translate(r.fLeft, r.fTop);
720 canvas.clipRegion(inval);
721 canvas.drawColor(0xFFFF8080);
722}
723
724void SampleWindow::afterChildren(SkCanvas* orig) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000725 if (fRequestGrabImage) {
726 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000727
reed@android.comf2b98d62010-12-20 18:26:13 +0000728 SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
729 SkDevice* device = canvas->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000730 SkBitmap bmp;
731 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000732 static int gSampleGrabCounter;
733 SkString name;
734 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000735 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000736 SkImageEncoder::kPNG_Type, 100);
737 }
738 }
739
reed@android.com8a1c16f2008-12-17 15:59:43 +0000740 switch (fCanvasType) {
741 case kRaster_CanvasType:
742 break;
743 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000744 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000745 SkPicture* pict = new SkPicture(*fPicture);
746 fPicture->unref();
747 orig->drawPicture(*pict);
748 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000749 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000750 SkDynamicMemoryWStream ostream;
751 fPicture->serialize(&ostream);
752 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000753
reed@android.com8a1c16f2008-12-17 15:59:43 +0000754 SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
755 SkPicture pict(&istream);
756 orig->drawPicture(pict);
757 } else {
758 fPicture->draw(orig);
759 fPicture->unref();
760 }
761 fPicture = NULL;
762 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000763#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000764 case kGPU_CanvasType:
Scroggoaed68d92011-06-08 14:26:00 +0000765 if (fShowZoomer && fGpuCanvas) {
Scroggo3272ba82011-05-25 20:50:42 +0000766 this->showZoomer(fGpuCanvas);
767 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000768 delete fGpuCanvas;
769 fGpuCanvas = NULL;
770 presentGL();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000771 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000772#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000773 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000774
reed@google.com17d7aec2011-04-25 14:31:44 +0000775 // Do this after presentGL and other finishing, rather than in afterChild
776 if (fMeasureFPS && fMeasureFPS_Time) {
777 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
778 this->updateTitle();
779 postInvalDelay(this->getSinkID());
780 }
781
782 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000783 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000784 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
785 int dx = fScrollTestX * 7;
786 int dy = fScrollTestY * 7;
787 SkIRect r;
788 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000789
reed@android.com8a1c16f2008-12-17 15:59:43 +0000790 r.set(50, 50, 50+100, 50+100);
791 bm.scrollRect(&r, dx, dy, &inval);
792 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000793 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000794}
795
reed@android.com6c5f6f22009-08-14 16:08:38 +0000796void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
797 if (fScale) {
798 SkScalar scale = SK_Scalar1 * 7 / 10;
799 SkScalar cx = this->width() / 2;
800 SkScalar cy = this->height() / 2;
801 canvas->translate(cx, cy);
802 canvas->scale(scale, scale);
803 canvas->translate(-cx, -cy);
804 }
805 if (fRotate) {
806 SkScalar cx = this->width() / 2;
807 SkScalar cy = this->height() / 2;
808 canvas->translate(cx, cy);
809 canvas->rotate(SkIntToScalar(30));
810 canvas->translate(-cx, -cy);
811 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000812
reed@google.com09e3baa2011-05-18 12:04:31 +0000813 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
814 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000815
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000816 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000817 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
818 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
819 fMeasureFPS_Time = SkTime::GetMSecs();
820 }
821 } else {
822 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000823 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000824 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000825}
826
827void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000828 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000829}
830
reed@android.com8a1c16f2008-12-17 15:59:43 +0000831static SkBitmap::Config gConfigCycle[] = {
832 SkBitmap::kNo_Config, // none -> none
833 SkBitmap::kNo_Config, // a1 -> none
834 SkBitmap::kNo_Config, // a8 -> none
835 SkBitmap::kNo_Config, // index8 -> none
836 SkBitmap::kARGB_4444_Config, // 565 -> 4444
837 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
838 SkBitmap::kRGB_565_Config // 8888 -> 565
839};
840
841static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
842 return gConfigCycle[c];
843}
844
djsollen@google.come32b5832011-06-13 16:58:40 +0000845void SampleWindow::changeZoomLevel(float delta) {
846 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000847 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000848 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
849 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000850 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000851 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
852 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000853 } else {
854 fZoomScale = SK_Scalar1;
855 }
856
djsollen@google.come32b5832011-06-13 16:58:40 +0000857 this->updateTitle();
858
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000859 this->inval(NULL);
860}
861
Scroggo2c8208f2011-06-15 16:49:08 +0000862bool SampleWindow::previousSample() {
863 fCurrIndex = (fCurrIndex - 1) % fSamples.count();
864 this->loadView(fSamples[fCurrIndex]());
865 return true;
866}
867
reed@android.com8a1c16f2008-12-17 15:59:43 +0000868bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000869 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
870 this->loadView(fSamples[fCurrIndex]());
871 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000872}
873
Scroggo2c8208f2011-06-15 16:49:08 +0000874void SampleWindow::postAnimatingEvent() {
875 if (fAnimating) {
876 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
877 evt->post(this->getSinkID(), ANIMATING_DELAY);
878 }
879}
880
reed@android.com8a1c16f2008-12-17 15:59:43 +0000881bool SampleWindow::onEvent(const SkEvent& evt) {
882 if (evt.isType(ANIMATING_EVENTTYPE)) {
883 if (fAnimating) {
884 this->nextSample();
885 this->postAnimatingEvent();
886 }
887 return true;
888 }
reed@android.com34245c72009-11-03 04:00:48 +0000889 if (evt.isType("set-curr-index")) {
890 fCurrIndex = evt.getFast32() % fSamples.count();
891 this->loadView(fSamples[fCurrIndex]());
892 return true;
893 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000894 if (isInvalEvent(evt)) {
895 this->inval(NULL);
896 return true;
897 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000898 return this->INHERITED::onEvent(evt);
899}
900
reed@android.comf2b98d62010-12-20 18:26:13 +0000901bool SampleWindow::onQuery(SkEvent* query) {
902 if (query->isType("get-slide-count")) {
903 query->setFast32(fSamples.count());
904 return true;
905 }
906 if (query->isType("get-slide-title")) {
907 SkView* view = fSamples[query->getFast32()]();
908 SkEvent evt(gTitleEvtName);
909 if (view->doQuery(&evt)) {
910 query->setString("title", evt.findString(gTitleEvtName));
911 }
912 SkSafeUnref(view);
913 return true;
914 }
915 if (query->isType("use-fast-text")) {
916 SkEvent evt(gFastTextEvtName);
917 return curr_view(this)->doQuery(&evt);
918 }
919 return this->INHERITED::onQuery(query);
920}
921
reed@android.com0ae6b242008-12-23 16:49:54 +0000922static void cleanup_for_filename(SkString* name) {
923 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +0000924 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +0000925 switch (str[i]) {
926 case ':': str[i] = '-'; break;
927 case '/': str[i] = '-'; break;
928 case ' ': str[i] = '_'; break;
929 default: break;
930 }
931 }
932}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000933
934bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000935 {
936 SkView* view = curr_view(this);
937 if (view) {
938 SkEvent evt(gCharEvtName);
939 evt.setFast32(uni);
940 if (view->doQuery(&evt)) {
941 return true;
942 }
943 }
944 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000945
reed@android.com8a1c16f2008-12-17 15:59:43 +0000946 int dx = 0xFF;
947 int dy = 0xFF;
948
949 switch (uni) {
950 case '5': dx = 0; dy = 0; break;
951 case '8': dx = 0; dy = -1; break;
952 case '6': dx = 1; dy = 0; break;
953 case '2': dx = 0; dy = 1; break;
954 case '4': dx = -1; dy = 0; break;
955 case '7': dx = -1; dy = -1; break;
956 case '9': dx = 1; dy = -1; break;
957 case '3': dx = 1; dy = 1; break;
958 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000959
reed@android.com8a1c16f2008-12-17 15:59:43 +0000960 default:
961 break;
962 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000963
reed@android.com8a1c16f2008-12-17 15:59:43 +0000964 if (0xFF != dx && 0xFF != dy) {
965 if ((dx | dy) == 0) {
966 fScrollTestX = fScrollTestY = 0;
967 } else {
968 fScrollTestX += dx;
969 fScrollTestY += dy;
970 }
971 this->inval(NULL);
972 return true;
973 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000974
reed@android.com0ae6b242008-12-23 16:49:54 +0000975 switch (uni) {
976 case 'a':
Scroggo2c8208f2011-06-15 16:49:08 +0000977 this->toggleSlideshow();
reed@android.com0ae6b242008-12-23 16:49:54 +0000978 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000979 case 'b':
980 fAAState = cycle_tristate(fAAState);
981 this->updateTitle();
982 this->inval(NULL);
983 break;
984 case 'c':
985 fUseClip = !fUseClip;
986 this->inval(NULL);
987 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +0000988 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000989 case 'd':
990 SkGraphics::SetFontCacheUsed(0);
991 return true;
992 case 'f':
Scroggo2c8208f2011-06-15 16:49:08 +0000993 this->toggleFPS();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000994 break;
995 case 'g':
996 fRequestGrabImage = true;
997 this->inval(NULL);
998 break;
reed@google.com09e3baa2011-05-18 12:04:31 +0000999 case 'h':
1000 fHintingState = cycle_tristate(fHintingState);
1001 this->updateTitle();
1002 this->inval(NULL);
1003 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001004 case 'i':
1005 this->zoomIn();
1006 break;
1007 case 'l':
1008 fLCDState = cycle_tristate(fLCDState);
1009 this->updateTitle();
1010 this->inval(NULL);
1011 break;
reed@google.com176753a2011-05-17 15:32:04 +00001012 case 'n':
1013 fFilterState = cycle_tristate(fFilterState);
1014 this->updateTitle();
1015 this->inval(NULL);
1016 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001017 case 'o':
1018 this->zoomOut();
1019 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001020 case 'p':
1021 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001022 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001023 this->inval(NULL);
1024 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001025 case 'r':
1026 fRotate = !fRotate;
1027 this->inval(NULL);
1028 this->updateTitle();
1029 return true;
1030 case 's':
1031 fScale = !fScale;
1032 this->inval(NULL);
1033 this->updateTitle();
1034 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001035 case 'x':
1036 fFlipAxis ^= kFlipAxis_X;
1037 this->updateTitle();
1038 this->inval(NULL);
1039 break;
1040 case 'y':
1041 fFlipAxis ^= kFlipAxis_Y;
1042 this->updateTitle();
1043 this->inval(NULL);
1044 break;
scroggo08526c02011-03-22 14:03:21 +00001045 case 'z':
1046 this->toggleZoomer();
1047 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001048 default:
1049 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001050 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001051
reed@android.com8a1c16f2008-12-17 15:59:43 +00001052 return this->INHERITED::onHandleChar(uni);
1053}
1054
Scroggo2c8208f2011-06-15 16:49:08 +00001055void SampleWindow::toggleFPS() {
1056 fMeasureFPS = !fMeasureFPS;
1057 this->inval(NULL);
1058 this->updateTitle();
1059}
1060
1061void SampleWindow::toggleSlideshow() {
1062 fAnimating = !fAnimating;
1063 this->postAnimatingEvent();
1064 this->updateTitle();
1065}
1066
1067void SampleWindow::toggleRendering() {
1068 fCanvasType = cycle_canvastype(fCanvasType);
1069 this->updateTitle();
1070 this->inval(NULL);
1071}
1072
reed@android.com8a1c16f2008-12-17 15:59:43 +00001073#include "SkDumpCanvas.h"
1074
1075bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001076 {
1077 SkView* view = curr_view(this);
1078 if (view) {
1079 SkEvent evt(gKeyEvtName);
1080 evt.setFast32(key);
1081 if (view->doQuery(&evt)) {
1082 return true;
1083 }
1084 }
1085 }
1086
reed@android.com8a1c16f2008-12-17 15:59:43 +00001087 switch (key) {
1088 case kRight_SkKey:
1089 if (this->nextSample()) {
1090 return true;
1091 }
1092 break;
1093 case kLeft_SkKey:
Scroggo2c8208f2011-06-15 16:49:08 +00001094 toggleRendering();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001095 return true;
1096 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001097 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001098 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001099 } else {
1100 fNClip = !fNClip;
1101 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001102 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001103 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001104 return true;
1105 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001106 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001107 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001108 } else {
1109 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001110 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001111 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001112 return true;
1113 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001114 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001115 SkDebugfDumper dumper;
1116 SkDumpCanvas dc(&dumper);
1117 this->draw(&dc);
1118 } else {
1119 fRepeatDrawing = !fRepeatDrawing;
1120 if (fRepeatDrawing) {
1121 this->inval(NULL);
1122 }
1123 }
1124 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001125 case kBack_SkKey:
1126 this->loadView(NULL);
1127 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001128 default:
1129 break;
1130 }
1131 return this->INHERITED::onHandleKey(key);
1132}
1133
reed@google.com52f57e12011-03-16 12:10:02 +00001134///////////////////////////////////////////////////////////////////////////////
1135
1136static const char gGestureClickType[] = "GestureClickType";
1137
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001138bool SampleWindow::onDispatchClick(int x, int y, Click::State state) {
Scroggo0f185c22011-03-24 18:35:50 +00001139 if (Click::kMoved_State == state) {
1140 updatePointer(x, y);
1141 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001142 int w = SkScalarRound(this->width());
1143 int h = SkScalarRound(this->height());
1144
1145 // check for the resize-box
1146 if (w - x < 16 && h - y < 16) {
1147 return false; // let the OS handle the click
1148 } else {
1149 return this->INHERITED::onDispatchClick(x, y, state);
1150 }
1151}
1152
reed@google.com52f57e12011-03-16 12:10:02 +00001153class GestureClick : public SkView::Click {
1154public:
1155 GestureClick(SkView* target) : SkView::Click(target) {
1156 this->setType(gGestureClickType);
1157 }
1158
1159 static bool IsGesture(Click* click) {
1160 return click->isType(gGestureClickType);
1161 }
1162};
1163
1164SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1165 return new GestureClick(this);
1166}
1167
1168bool SampleWindow::onClick(Click* click) {
1169 if (GestureClick::IsGesture(click)) {
1170 float x = SkScalarToFloat(click->fCurr.fX);
1171 float y = SkScalarToFloat(click->fCurr.fY);
1172 switch (click->fState) {
1173 case SkView::Click::kDown_State:
1174 fGesture.touchBegin(click, x, y);
1175 break;
1176 case SkView::Click::kMoved_State:
1177 fGesture.touchMoved(click, x, y);
1178 this->inval(NULL);
1179 break;
1180 case SkView::Click::kUp_State:
1181 fGesture.touchEnd(click);
1182 this->inval(NULL);
1183 break;
1184 }
1185 return true;
1186 }
1187 return false;
1188}
1189
1190///////////////////////////////////////////////////////////////////////////////
1191
reed@android.com8a1c16f2008-12-17 15:59:43 +00001192void SampleWindow::loadView(SkView* view) {
1193 SkView::F2BIter iter(this);
1194 SkView* prev = iter.next();
1195 if (prev) {
1196 prev->detachFromParent();
1197 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001198
reed@android.com34245c72009-11-03 04:00:48 +00001199 if (NULL == view) {
1200 view = create_overview(fSamples.count(), fSamples.begin());
1201 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001202 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001203 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001204 this->attachChildToFront(view)->unref();
1205 view->setSize(this->width(), this->height());
1206
1207 this->updateTitle();
1208}
1209
1210static const char* gConfigNames[] = {
1211 "unknown config",
1212 "A1",
1213 "A8",
1214 "Index8",
1215 "565",
1216 "4444",
1217 "8888"
1218};
1219
1220static const char* configToString(SkBitmap::Config c) {
1221 return gConfigNames[c];
1222}
1223
1224static const char* gCanvasTypePrefix[] = {
1225 "raster: ",
1226 "picture: ",
1227 "opengl: "
1228};
1229
reed@google.com569e0432011-04-05 13:07:03 +00001230static const char* trystate_str(SkTriState state,
1231 const char trueStr[], const char falseStr[]) {
1232 if (kTrue_SkTriState == state) {
1233 return trueStr;
1234 } else if (kFalse_SkTriState == state) {
1235 return falseStr;
1236 }
1237 return NULL;
1238}
1239
reed@android.com8a1c16f2008-12-17 15:59:43 +00001240void SampleWindow::updateTitle() {
1241 SkString title;
1242
1243 SkView::F2BIter iter(this);
1244 SkView* view = iter.next();
1245 SkEvent evt(gTitleEvtName);
1246 if (view->doQuery(&evt)) {
1247 title.set(evt.findString(gTitleEvtName));
1248 }
1249 if (title.size() == 0) {
1250 title.set("<unknown>");
1251 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001252
reed@android.com8a1c16f2008-12-17 15:59:43 +00001253 title.prepend(gCanvasTypePrefix[fCanvasType]);
1254
1255 title.prepend(" ");
1256 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001257
reed@android.com8a1c16f2008-12-17 15:59:43 +00001258 if (fAnimating) {
1259 title.prepend("<A> ");
1260 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001261 if (fScale) {
1262 title.prepend("<S> ");
1263 }
1264 if (fRotate) {
1265 title.prepend("<R> ");
1266 }
reed@android.come522ca52009-11-23 20:10:41 +00001267 if (fNClip) {
1268 title.prepend("<C> ");
1269 }
reed@google.com569e0432011-04-05 13:07:03 +00001270
1271 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1272 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001273 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001274 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1275 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001276
1277 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001278 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001279 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001280
1281 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001282 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001283 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001284 if (fUsePipe && SampleView::IsSampleView(view)) {
1285 title.prepend("<P> ");
1286 }
1287 if (SampleView::IsSampleView(view)) {
1288 title.prepend("! ");
1289 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001290
reed@android.com8a1c16f2008-12-17 15:59:43 +00001291 this->setTitle(title.c_str());
1292}
1293
1294void SampleWindow::onSizeChange() {
1295 this->INHERITED::onSizeChange();
1296
1297 SkView::F2BIter iter(this);
1298 SkView* view = iter.next();
1299 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001300
reed@android.com8a1c16f2008-12-17 15:59:43 +00001301 // rebuild our clippath
1302 {
1303 const SkScalar W = this->width();
1304 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001305
reed@android.com8a1c16f2008-12-17 15:59:43 +00001306 fClipPath.reset();
1307#if 0
1308 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1309 SkRect r;
1310 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1311 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1312 fClipPath.addRect(r);
1313 }
1314#else
1315 SkRect r;
1316 r.set(0, 0, W, H);
1317 fClipPath.addRect(r, SkPath::kCCW_Direction);
1318 r.set(W/4, H/4, W*3/4, H*3/4);
1319 fClipPath.addRect(r, SkPath::kCW_Direction);
1320#endif
1321 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001322
Scroggo2c8208f2011-06-15 16:49:08 +00001323 fZoomCenterX = SkScalarHalf(this->width());
1324 fZoomCenterY = SkScalarHalf(this->height());
1325
1326 if (fGrContext) {
1327 glViewport(0, 0, SkScalarRound(this->width()),
1328 SkScalarRound(this->height()));
1329 fGrContext->resetContext();
1330 }
1331
reed@android.com8a1c16f2008-12-17 15:59:43 +00001332 this->updateTitle(); // to refresh our config
1333}
1334
1335///////////////////////////////////////////////////////////////////////////////
1336
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001337static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001338static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001339static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001340
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001341bool SampleView::IsSampleView(SkView* view) {
1342 SkEvent evt(is_sample_view_tag);
1343 return view->doQuery(&evt);
1344}
1345
reed@google.comf2183392011-04-22 14:10:48 +00001346bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001347 SkEvent evt(repeat_count_tag);
1348 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001349 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001350}
1351
reed@google.com0faac1e2011-05-11 05:58:58 +00001352bool SampleView::SetUsePipe(SkView* view, bool pred) {
1353 SkEvent evt(set_use_pipe_tag);
1354 evt.setFast32(pred);
1355 return view->doEvent(evt);
1356}
1357
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001358bool SampleView::onEvent(const SkEvent& evt) {
1359 if (evt.isType(repeat_count_tag)) {
1360 fRepeatCount = evt.getFast32();
1361 return true;
1362 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001363 if (evt.isType(set_use_pipe_tag)) {
1364 fUsePipe = !!evt.getFast32();
1365 return true;
1366 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001367 return this->INHERITED::onEvent(evt);
1368}
1369
1370bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001371 if (evt->isType(is_sample_view_tag)) {
1372 return true;
1373 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001374 return this->INHERITED::onQuery(evt);
1375}
1376
reed@google.com68f456d2011-05-02 18:55:39 +00001377#ifdef TEST_GPIPE
1378 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001379
1380class SimplePC : public SkGPipeController {
1381public:
1382 SimplePC(SkCanvas* target);
1383 ~SimplePC();
1384
1385 virtual void* requestBlock(size_t minRequest, size_t* actual);
1386 virtual void notifyWritten(size_t bytes);
1387
1388private:
reed@google.com961ddb02011-05-05 14:03:48 +00001389 SkGPipeReader fReader;
1390 void* fBlock;
1391 size_t fBlockSize;
1392 size_t fBytesWritten;
1393 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001394 SkGPipeReader::Status fStatus;
1395
1396 size_t fTotalWritten;
1397};
1398
1399SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1400 fBlock = NULL;
1401 fBlockSize = fBytesWritten = 0;
1402 fStatus = SkGPipeReader::kDone_Status;
1403 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001404 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001405}
1406
1407SimplePC::~SimplePC() {
1408// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1409 sk_free(fBlock);
1410
reed@google.com0faac1e2011-05-11 05:58:58 +00001411 if (fTotalWritten) {
1412 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1413 fAtomsWritten, fStatus);
1414 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001415}
1416
1417void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1418 sk_free(fBlock);
1419
1420 fBlockSize = minRequest * 4;
1421 fBlock = sk_malloc_throw(fBlockSize);
1422 fBytesWritten = 0;
1423 *actual = fBlockSize;
1424 return fBlock;
1425}
1426
1427void SimplePC::notifyWritten(size_t bytes) {
1428 SkASSERT(fBytesWritten + bytes <= fBlockSize);
yangsu@google.com1f394212011-06-01 18:03:34 +00001429
1430#ifdef PIPE_FILE
1431 //File is open in append mode
1432 FILE* f = fopen(FILE_PATH, "ab");
1433 SkASSERT(f != NULL);
1434 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
1435 fclose(f);
1436#endif
1437
reed@google.com64e3eb22011-05-04 14:32:04 +00001438 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1439 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1440 fBytesWritten += bytes;
1441 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001442
1443 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001444}
1445
reed@google.com68f456d2011-05-02 18:55:39 +00001446#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001447
reed@google.com64e3eb22011-05-04 14:32:04 +00001448
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001449void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001450#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001451 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001452 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001453 if (fUsePipe) {
reed@google.comdde09562011-05-23 12:21:05 +00001454 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
1455// flags = 0;
1456 canvas = writer.startRecording(&controller, flags);
reed@google.com0faac1e2011-05-11 05:58:58 +00001457 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001458#endif
1459
reed@google.com81e3d7f2011-06-01 12:42:36 +00001460 this->onDrawBackground(canvas);
1461
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001462 for (int i = 0; i < fRepeatCount; i++) {
1463 SkAutoCanvasRestore acr(canvas, true);
1464 this->onDrawContent(canvas);
1465 }
1466}
1467
1468void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001469 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001470}
1471
1472///////////////////////////////////////////////////////////////////////////////
1473
reed@android.comf2b98d62010-12-20 18:26:13 +00001474template <typename T> void SkTBSort(T array[], int count) {
1475 for (int i = 1; i < count - 1; i++) {
1476 bool didSwap = false;
1477 for (int j = count - 1; j > i; --j) {
1478 if (array[j] < array[j-1]) {
1479 T tmp(array[j-1]);
1480 array[j-1] = array[j];
1481 array[j] = tmp;
1482 didSwap = true;
1483 }
1484 }
1485 if (!didSwap) {
1486 break;
1487 }
1488 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001489
reed@android.comf2b98d62010-12-20 18:26:13 +00001490 for (int k = 0; k < count - 1; k++) {
1491 SkASSERT(!(array[k+1] < array[k]));
1492 }
1493}
1494
1495#include "SkRandom.h"
1496
1497static void rand_rect(SkIRect* rect, SkRandom& rand) {
1498 int bits = 8;
1499 int shift = 32 - bits;
1500 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1501 rand.nextU() >> shift, rand.nextU() >> shift);
1502 rect->sort();
1503}
1504
1505static void dumpRect(const SkIRect& r) {
1506 SkDebugf(" { %d, %d, %d, %d },\n",
1507 r.fLeft, r.fTop,
1508 r.fRight, r.fBottom);
1509}
1510
1511static void test_rects(const SkIRect rect[], int count) {
1512 SkRegion rgn0, rgn1;
1513
1514 for (int i = 0; i < count; i++) {
1515 rgn0.op(rect[i], SkRegion::kUnion_Op);
1516 // dumpRect(rect[i]);
1517 }
1518 rgn1.setRects(rect, count);
1519
1520 if (rgn0 != rgn1) {
1521 SkDebugf("\n");
1522 for (int i = 0; i < count; i++) {
1523 dumpRect(rect[i]);
1524 }
1525 SkDebugf("\n");
1526 }
1527}
1528
1529static void test() {
1530 size_t i;
1531
1532 const SkIRect r0[] = {
1533 { 0, 0, 1, 1 },
1534 { 2, 2, 3, 3 },
1535 };
1536 const SkIRect r1[] = {
1537 { 0, 0, 1, 3 },
1538 { 1, 1, 2, 2 },
1539 { 2, 0, 3, 3 },
1540 };
1541 const SkIRect r2[] = {
1542 { 0, 0, 1, 2 },
1543 { 2, 1, 3, 3 },
1544 { 4, 0, 5, 1 },
1545 { 6, 0, 7, 4 },
1546 };
1547
1548 static const struct {
1549 const SkIRect* fRects;
1550 int fCount;
1551 } gRecs[] = {
1552 { r0, SK_ARRAY_COUNT(r0) },
1553 { r1, SK_ARRAY_COUNT(r1) },
1554 { r2, SK_ARRAY_COUNT(r2) },
1555 };
1556
1557 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1558 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1559 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001560
reed@android.comf2b98d62010-12-20 18:26:13 +00001561 SkRandom rand;
1562 for (i = 0; i < 10000; i++) {
1563 SkRegion rgn0, rgn1;
1564
1565 const int N = 8;
1566 SkIRect rect[N];
1567 for (int j = 0; j < N; j++) {
1568 rand_rect(&rect[j], rand);
1569 }
1570 test_rects(rect, N);
1571 }
1572}
1573
reed@android.com8a1c16f2008-12-17 15:59:43 +00001574SkOSWindow* create_sk_window(void* hwnd) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001575// test();
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001576 return new SampleWindow(hwnd);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001577}
1578
1579void get_preferred_size(int* x, int* y, int* width, int* height) {
1580 *x = 10;
1581 *y = 50;
1582 *width = 640;
1583 *height = 480;
1584}
1585
1586void application_init() {
1587// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001588#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001589 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001590#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001591 SkGraphics::Init();
1592 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001593}
1594
1595void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001596 SkEvent::Term();
1597 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001598}