blob: 2fb73b7e034aafcea4ecec5724a6e2d51e846de8 [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@google.comaf951c92011-06-16 19:10:39 +00006#include "SkGpuDevice.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00007#include "SkGraphics.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +00008#include "SkImageEncoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SkPaint.h"
10#include "SkPicture.h"
11#include "SkStream.h"
reed@android.com44177402009-11-23 21:07:51 +000012#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkWindow.h"
14
15#include "SampleCode.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016#include "GrContext.h"
Scroggo0f185c22011-03-24 18:35:50 +000017#include "SkTypeface.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000018
Scroggo3e7ff9f2011-06-16 15:31:26 +000019#include "GrGLInterface.h"
Scroggo2c8208f2011-06-15 16:49:08 +000020
yangsu@google.com1f394212011-06-01 18:03:34 +000021#define TEST_GPIPEx
22
23#ifdef TEST_GPIPE
24#define PIPE_FILE
25#define FILE_PATH "/path/to/drawing.data"
26#endif
27
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +000028#define USE_ARROWS_FOR_ZOOM true
reed@android.comf2b98d62010-12-20 18:26:13 +000029//#define DEFAULT_TO_GPU
30
reed@android.come191b162009-12-18 21:33:39 +000031extern SkView* create_overview(int, const SkViewFactory[]);
reed@android.com34245c72009-11-03 04:00:48 +000032
reed@android.comcb342352010-07-22 18:27:53 +000033#define SK_SUPPORT_GL
reed@android.com8a1c16f2008-12-17 15:59:43 +000034
35#define ANIMATING_EVENTTYPE "nextSample"
36#define ANIMATING_DELAY 750
37
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000038#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +000039 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000040#else
reed@google.combad8c872011-05-18 20:10:31 +000041 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000042#endif
reed@google.combad8c872011-05-18 20:10:31 +000043#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000044
reed@google.comac10a2d2010-12-22 21:39:39 +000045#ifdef SK_SUPPORT_GL
46 #include "GrGLConfig.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000047#endif
48
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000049///////////////
50static const char view_inval_msg[] = "view-inval-msg";
51
52static void postInvalDelay(SkEventSinkID sinkID) {
53 SkEvent* evt = new SkEvent(view_inval_msg);
reed@google.comf2183392011-04-22 14:10:48 +000054 evt->post(sinkID, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000055}
56
57static bool isInvalEvent(const SkEvent& evt) {
58 return evt.isType(view_inval_msg);
59}
60//////////////////
61
reed@android.com8a1c16f2008-12-17 15:59:43 +000062SkViewRegister* SkViewRegister::gHead;
63SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) {
64 static bool gOnce;
65 if (!gOnce) {
66 gHead = NULL;
67 gOnce = true;
68 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000069
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 fChain = gHead;
71 gHead = this;
72}
73
reed@android.comf2b98d62010-12-20 18:26:13 +000074#if defined(SK_SUPPORT_GL)
75 #define SK_USE_SHADERS
76#endif
77
twiz@google.com06c3b6b2011-03-14 16:58:39 +000078#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +000079#include <CoreFoundation/CoreFoundation.h>
80#include <CoreFoundation/CFURLAccess.h>
81
82static void testpdf() {
83 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
84 kCFStringEncodingUTF8);
85 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
86 kCFURLPOSIXPathStyle,
87 false);
88 CFRelease(path);
89 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
90 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
91 CFRelease(url);
92
93 CGContextBeginPage(cg, &box);
94 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
95 CGContextFillEllipseInRect(cg, r);
96 CGContextEndPage(cg);
97 CGContextRelease(cg);
98
99 if (false) {
100 SkBitmap bm;
101 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
102 bm.allocPixels();
103 bm.eraseColor(0);
104
105 SkCanvas canvas(bm);
106
107 }
108}
109#endif
110
111//////////////////////////////////////////////////////////////////////////////
112
reed@google.com569e0432011-04-05 13:07:03 +0000113enum FlipAxisEnum {
114 kFlipAxis_X = (1 << 0),
115 kFlipAxis_Y = (1 << 1)
116};
117
reed@google.com569e0432011-04-05 13:07:03 +0000118static SkTriState cycle_tristate(SkTriState state) {
119 static const SkTriState gCycle[] = {
120 /* kFalse_SkTriState -> */ kUnknown_SkTriState,
121 /* kTrue_SkTriState -> */ kFalse_SkTriState,
122 /* kUnknown_SkTriState -> */ kTrue_SkTriState,
123 };
124 return gCycle[state];
125}
126
reed@google.comf0b5f682011-03-11 20:08:25 +0000127#include "SkDrawFilter.h"
128
reed@google.com569e0432011-04-05 13:07:03 +0000129class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000130public:
reed@google.com09e3baa2011-05-18 12:04:31 +0000131 FlagsDrawFilter(SkTriState lcd, SkTriState aa, SkTriState filter,
132 SkTriState hinting) :
133 fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000134
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000135 virtual void filter(SkPaint* paint, Type t) {
reed@google.com569e0432011-04-05 13:07:03 +0000136 if (kText_Type == t && kUnknown_SkTriState != fLCDState) {
reed@google.com569e0432011-04-05 13:07:03 +0000137 paint->setLCDRenderText(kTrue_SkTriState == fLCDState);
138 }
139 if (kUnknown_SkTriState != fAAState) {
reed@google.com569e0432011-04-05 13:07:03 +0000140 paint->setAntiAlias(kTrue_SkTriState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000141 }
reed@google.com176753a2011-05-17 15:32:04 +0000142 if (kUnknown_SkTriState != fFilterState) {
143 paint->setFilterBitmap(kTrue_SkTriState == fFilterState);
144 }
reed@google.com09e3baa2011-05-18 12:04:31 +0000145 if (kUnknown_SkTriState != fHintingState) {
146 paint->setHinting(kTrue_SkTriState == fHintingState ?
147 SkPaint::kNormal_Hinting :
148 SkPaint::kSlight_Hinting);
149 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000150 }
151
152private:
reed@google.com569e0432011-04-05 13:07:03 +0000153 SkTriState fLCDState;
reed@google.com569e0432011-04-05 13:07:03 +0000154 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000155 SkTriState fFilterState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000156 SkTriState fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000157};
158
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159//////////////////////////////////////////////////////////////////////////////
160
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000161#define MAX_ZOOM_LEVEL 8
162#define MIN_ZOOM_LEVEL -8
163
reed@android.comf2b98d62010-12-20 18:26:13 +0000164static const char gCharEvtName[] = "SampleCode_Char_Event";
165static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166static const char gTitleEvtName[] = "SampleCode_Title_Event";
167static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000168static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
169
170bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
171 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
172 if (outUni) {
173 *outUni = evt.getFast32();
174 }
175 return true;
176 }
177 return false;
178}
179
180bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
181 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
182 if (outKey) {
183 *outKey = (SkKey)evt.getFast32();
184 }
185 return true;
186 }
187 return false;
188}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189
190bool SampleCode::TitleQ(const SkEvent& evt) {
191 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
192}
193
194void SampleCode::TitleR(SkEvent* evt, const char title[]) {
195 SkASSERT(evt && TitleQ(*evt));
196 evt->setString(gTitleEvtName, title);
197}
198
199bool SampleCode::PrefSizeQ(const SkEvent& evt) {
200 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
201}
202
203void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
204 SkASSERT(evt && PrefSizeQ(*evt));
205 SkScalar size[2];
206 size[0] = width;
207 size[1] = height;
208 evt->setScalars(gPrefSizeEvtName, 2, size);
209}
210
reed@android.comf2b98d62010-12-20 18:26:13 +0000211bool SampleCode::FastTextQ(const SkEvent& evt) {
212 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
213}
214
215///////////////////////////////////////////////////////////////////////////////
216
reed@android.com44177402009-11-23 21:07:51 +0000217static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000218static SkMSec gAnimTimePrev;
219
reed@android.com44177402009-11-23 21:07:51 +0000220SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000221SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
222SkScalar SampleCode::GetAnimSecondsDelta() {
223 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
224}
reed@android.com44177402009-11-23 21:07:51 +0000225
226SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000227 // since gAnimTime can be up to 32 bits, we can't convert it to a float
228 // or we'll lose the low bits. Hence we use doubles for the intermediate
229 // calculations
230 double seconds = (double)gAnimTime / 1000.0;
231 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000232 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000233 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000234 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000235 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000236}
237
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238//////////////////////////////////////////////////////////////////////////////
239
reed@android.comf2b98d62010-12-20 18:26:13 +0000240static SkView* curr_view(SkWindow* wind) {
241 SkView::F2BIter iter(wind);
242 return iter.next();
243}
244
Scroggo2c8208f2011-06-15 16:49:08 +0000245void SampleWindow::setZoomCenter(float x, float y)
246{
247 fZoomCenterX = SkFloatToScalar(x);
248 fZoomCenterY = SkFloatToScalar(y);
249}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250
djsollen@google.come32b5832011-06-13 16:58:40 +0000251bool SampleWindow::setGrContext(GrContext* context)
252{
253 if (fGrContext) {
254 fGrContext->unref();
255 }
256 fGrContext = context;
257 fGrContext->ref();
258 return true;
259}
260
261GrContext* SampleWindow::getGrContext()
262{
263 return fGrContext;
264}
djsollen@google.come32b5832011-06-13 16:58:40 +0000265
Scroggo0f185c22011-03-24 18:35:50 +0000266bool SampleWindow::zoomIn()
267{
268 // Arbitrarily decided
269 if (fFatBitsScale == 25) return false;
270 fFatBitsScale++;
271 this->inval(NULL);
272 return true;
273}
274
275bool SampleWindow::zoomOut()
276{
277 if (fFatBitsScale == 1) return false;
278 fFatBitsScale--;
279 this->inval(NULL);
280 return true;
281}
282
283void SampleWindow::toggleZoomer()
284{
285 fShowZoomer = !fShowZoomer;
286 this->inval(NULL);
287}
288
289void SampleWindow::updatePointer(int x, int y)
290{
291 fMouseX = x;
292 fMouseY = y;
293 if (fShowZoomer) {
294 this->inval(NULL);
295 }
296}
297
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000298bool SampleWindow::make3DReady() {
299
300#if defined(SK_SUPPORT_GL)
bsalomon@google.com498a6232011-03-10 18:24:15 +0000301 if (attachGL()) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000302 if (NULL != fGrContext) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000303 // various gr lifecycle tests
304 #if 0
305 fGrContext->freeGpuResources();
306 #elif 0
307 // this will leak resources.
308 fGrContext->contextLost();
309 #elif 0
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000310 GrAssert(1 == fGrContext->refcnt());
311 fGrContext->unref();
312 fGrContext = NULL;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000313 #endif
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000314 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000315
bsalomon@google.com498a6232011-03-10 18:24:15 +0000316 if (NULL == fGrContext) {
317 #if defined(SK_USE_SHADERS)
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000318 fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000319 #else
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000320 fGrContext = GrContext::Create(kOpenGL_Fixed_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000321 #endif
reed@google.com569e0432011-04-05 13:07:03 +0000322 SkDebugf("---- constructor\n");
bsalomon@google.com498a6232011-03-10 18:24:15 +0000323 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000324
bsalomon@google.com498a6232011-03-10 18:24:15 +0000325 if (NULL != fGrContext) {
326 return true;
327 } else {
328 detachGL();
329 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000330 }
331#endif
332 SkDebugf("Failed to setup 3D");
333 return false;
334}
335
reed@android.com8a1c16f2008-12-17 15:59:43 +0000336SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) {
337 static const CanvasType gCT[] = {
338 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000339 kGPU_CanvasType,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340 kRaster_CanvasType
341 };
342 return gCT[ct];
343}
344
345SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) {
yangsu@google.com1f394212011-06-01 18:03:34 +0000346#ifdef PIPE_FILE
347 //Clear existing file or create file if it doesn't exist
348 FILE* f = fopen(FILE_PATH, "wb");
349 fclose(f);
350#endif
351
reed@android.com8a1c16f2008-12-17 15:59:43 +0000352 fPicture = NULL;
reed@android.comf2b98d62010-12-20 18:26:13 +0000353 fGpuCanvas = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000354
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000355 fGrContext = NULL;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000356
reed@android.comf2b98d62010-12-20 18:26:13 +0000357#ifdef DEFAULT_TO_GPU
358 fCanvasType = kGPU_CanvasType;
359#else
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360 fCanvasType = kRaster_CanvasType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000361#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000362 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000363 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364 fRepeatDrawing = false;
365 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000366 fRotate = false;
367 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000368 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000369 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000370 fMeasureFPS = false;
reed@google.com569e0432011-04-05 13:07:03 +0000371 fLCDState = kUnknown_SkTriState;
372 fAAState = kUnknown_SkTriState;
reed@google.com66f22fd2011-05-17 15:33:45 +0000373 fFilterState = kUnknown_SkTriState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000374 fHintingState = kUnknown_SkTriState;
reed@google.com569e0432011-04-05 13:07:03 +0000375 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000376 fScrollTestX = fScrollTestY = 0;
377
Scroggo0f185c22011-03-24 18:35:50 +0000378 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000379 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000380 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
381 fShowZoomer = false;
382
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000383 fZoomLevel = 0;
384 fZoomScale = SK_Scalar1;
385
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000386// this->setConfig(SkBitmap::kRGB_565_Config);
387 this->setConfig(SkBitmap::kARGB_8888_Config);
388 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000389 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390
reed@android.com34245c72009-11-03 04:00:48 +0000391 {
392 const SkViewRegister* reg = SkViewRegister::Head();
393 while (reg) {
394 *fSamples.append() = reg->factory();
395 reg = reg->next();
396 }
397 }
398 fCurrIndex = 0;
reed@android.come0f13ee2009-11-04 19:40:25 +0000399 this->loadView(fSamples[fCurrIndex]());
reed@google.comf0b5f682011-03-11 20:08:25 +0000400
Scroggob4490c72011-06-17 13:53:05 +0000401 // If another constructor set our dimensions, ensure that our
402 // onSizeChange gets called.
403 if (this->height() && this->width()) {
404 this->onSizeChange();
405 }
406
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000407#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +0000408 testpdf();
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000409#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410}
411
412SampleWindow::~SampleWindow() {
413 delete fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000414 delete fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000415 if (NULL != fGrContext) {
416 fGrContext->unref();
417 }
Scroggo0f185c22011-03-24 18:35:50 +0000418 fTypeface->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000419}
420
reed@android.com55e76b22009-11-23 21:46:47 +0000421static SkBitmap capture_bitmap(SkCanvas* canvas) {
422 SkBitmap bm;
423 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
424 src.copyTo(&bm, src.config());
425 return bm;
426}
427
428static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
429 SkBitmap* diff) {
430 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000431
reed@android.com55e76b22009-11-23 21:46:47 +0000432 SkAutoLockPixels alp0(src);
433 SkAutoLockPixels alp1(orig);
434 for (int y = 0; y < src.height(); y++) {
435 const void* srcP = src.getAddr(0, y);
436 const void* origP = orig.getAddr(0, y);
437 size_t bytes = src.width() * src.bytesPerPixel();
438 if (memcmp(srcP, origP, bytes)) {
439 SkDebugf("---------- difference on line %d\n", y);
440 return true;
441 }
442 }
443 return false;
444}
445
Scroggo0f185c22011-03-24 18:35:50 +0000446static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
447{
448 SkColor desiredColor = paint.getColor();
449 paint.setColor(SK_ColorWHITE);
450 const char* c_str = string.c_str();
451 size_t size = string.size();
452 SkRect bounds;
453 paint.measureText(c_str, size, &bounds);
454 bounds.offset(left, top);
455 SkScalar inset = SkIntToScalar(-2);
456 bounds.inset(inset, inset);
457 canvas->drawRect(bounds, paint);
458 if (desiredColor != SK_ColorBLACK) {
459 paint.setColor(SK_ColorBLACK);
460 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
461 }
462 paint.setColor(desiredColor);
463 canvas->drawText(c_str, size, left, top, paint);
464}
465
reed@android.com44177402009-11-23 21:07:51 +0000466#define XCLIP_N 8
467#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000468
469void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000470 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000471 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000472 gAnimTime = SkTime::GetMSecs();
473
Scroggo2c8208f2011-06-15 16:49:08 +0000474 SkScalar cx = fZoomCenterX;
475 SkScalar cy = fZoomCenterY;
reed@google.com569e0432011-04-05 13:07:03 +0000476
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000477 if (fZoomLevel) {
478 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000479 SkPoint center;
480 m = canvas->getTotalMatrix();//.invert(&m);
481 m.mapXY(cx, cy, &center);
482 cx = center.fX;
483 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000484
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000485 m.setTranslate(-cx, -cy);
486 m.postScale(fZoomScale, fZoomScale);
487 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000488
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000489 canvas->concat(m);
490 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000491
reed@google.com569e0432011-04-05 13:07:03 +0000492 if (fFlipAxis) {
493 SkMatrix m;
494 m.setTranslate(cx, cy);
495 if (fFlipAxis & kFlipAxis_X) {
496 m.preScale(-SK_Scalar1, SK_Scalar1);
497 }
498 if (fFlipAxis & kFlipAxis_Y) {
499 m.preScale(SK_Scalar1, -SK_Scalar1);
500 }
501 m.preTranslate(-cx, -cy);
502 canvas->concat(m);
503 }
504
reed@google.com52f57e12011-03-16 12:10:02 +0000505 // Apply any gesture matrix
506 if (true) {
507 const SkMatrix& localM = fGesture.localM();
508 if (localM.getType() & SkMatrix::kScale_Mask) {
509 canvas->setExternalMatrix(&localM);
510 }
511 canvas->concat(localM);
512 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000513
reed@google.com52f57e12011-03-16 12:10:02 +0000514 if (fGesture.isActive()) {
515 this->inval(NULL);
516 }
517 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000518
reed@android.come522ca52009-11-23 20:10:41 +0000519 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000520 this->INHERITED::draw(canvas);
521 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000522
523 const SkScalar w = this->width();
524 const SkScalar h = this->height();
525 const SkScalar cw = w / XCLIP_N;
526 const SkScalar ch = h / YCLIP_N;
527 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000528 SkRect r;
529 r.fTop = y * ch;
530 r.fBottom = (y + 1) * ch;
531 if (y == YCLIP_N - 1) {
532 r.fBottom = h;
533 }
reed@android.come522ca52009-11-23 20:10:41 +0000534 for (int x = 0; x < XCLIP_N; x++) {
535 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000536 r.fLeft = x * cw;
537 r.fRight = (x + 1) * cw;
538 if (x == XCLIP_N - 1) {
539 r.fRight = w;
540 }
reed@android.come522ca52009-11-23 20:10:41 +0000541 canvas->clipRect(r);
542 this->INHERITED::draw(canvas);
543 }
544 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000545
reed@android.com55e76b22009-11-23 21:46:47 +0000546 SkBitmap diff;
547 if (bitmap_diff(canvas, orig, &diff)) {
548 }
reed@android.come522ca52009-11-23 20:10:41 +0000549 } else {
550 this->INHERITED::draw(canvas);
551 }
Scroggo3272ba82011-05-25 20:50:42 +0000552 if (fShowZoomer && fCanvasType != kGPU_CanvasType) {
553 // In the GPU case, INHERITED::draw calls beforeChildren, which
554 // creates an SkGpuCanvas. All further draw calls are directed
555 // at that canvas, which is deleted in afterChildren (which is
556 // also called by draw), so we cannot show the zoomer here.
557 // Instead, we call it inside afterChildren.
558 showZoomer(canvas);
559 }
560}
561
562void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +0000563 int count = canvas->save();
564 canvas->resetMatrix();
565 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000566 int width = SkScalarRound(this->width());
567 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000568 if (fMouseX >= width) fMouseX = width - 1;
569 else if (fMouseX < 0) fMouseX = 0;
570 if (fMouseY >= height) fMouseY = height - 1;
571 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000572
Scroggo0f185c22011-03-24 18:35:50 +0000573 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000574 bitmap.lockPixels();
575
Scroggo0f185c22011-03-24 18:35:50 +0000576 // 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 +0000577 int zoomedWidth = (width >> 1) | 1;
578 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000579 SkIRect src;
580 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
581 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
582 SkRect dest;
583 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
584 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
585 SkPaint paint;
586 // Clear the background behind our zoomed in view
587 paint.setColor(SK_ColorWHITE);
588 canvas->drawRect(dest, paint);
589 canvas->drawBitmapRect(bitmap, &src, dest);
590 paint.setColor(SK_ColorBLACK);
591 paint.setStyle(SkPaint::kStroke_Style);
592 // Draw a border around the pixel in the middle
593 SkRect originalPixel;
594 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
595 SkMatrix matrix;
596 SkRect scalarSrc;
597 scalarSrc.set(src);
598 SkColor color = bitmap.getColor(fMouseX, fMouseY);
599 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
600 SkRect pixel;
601 matrix.mapRect(&pixel, originalPixel);
602 // TODO Perhaps measure the values and make the outline white if it's "dark"
603 if (color == SK_ColorBLACK) {
604 paint.setColor(SK_ColorWHITE);
605 }
606 canvas->drawRect(pixel, paint);
607 }
608 paint.setColor(SK_ColorBLACK);
609 // Draw a border around the destination rectangle
610 canvas->drawRect(dest, paint);
611 paint.setStyle(SkPaint::kStrokeAndFill_Style);
612 // Identify the pixel and its color on screen
613 paint.setTypeface(fTypeface);
614 paint.setAntiAlias(true);
615 SkScalar lineHeight = paint.getFontMetrics(NULL);
616 SkString string;
617 string.appendf("(%i, %i)", fMouseX, fMouseY);
618 SkScalar left = dest.fLeft + SkIntToScalar(3);
619 SkScalar i = SK_Scalar1;
620 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
621 // Alpha
622 i += SK_Scalar1;
623 string.reset();
624 string.appendf("A: %X", SkColorGetA(color));
625 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
626 // Red
627 i += SK_Scalar1;
628 string.reset();
629 string.appendf("R: %X", SkColorGetR(color));
630 paint.setColor(SK_ColorRED);
631 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
632 // Green
633 i += SK_Scalar1;
634 string.reset();
635 string.appendf("G: %X", SkColorGetG(color));
636 paint.setColor(SK_ColorGREEN);
637 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
638 // Blue
639 i += SK_Scalar1;
640 string.reset();
641 string.appendf("B: %X", SkColorGetB(color));
642 paint.setColor(SK_ColorBLUE);
643 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
644 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +0000645}
646
reed@android.com8a1c16f2008-12-17 15:59:43 +0000647void SampleWindow::onDraw(SkCanvas* canvas) {
648 if (fRepeatDrawing) {
649 this->inval(NULL);
650 }
651}
652
653#include "SkColorPriv.h"
654
655static void reverseRedAndBlue(const SkBitmap& bm) {
656 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
657 uint8_t* p = (uint8_t*)bm.getPixels();
658 uint8_t* stop = p + bm.getSize();
659 while (p < stop) {
660 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
661 unsigned scale = SkAlpha255To256(p[3]);
662 unsigned r = p[2];
663 unsigned b = p[0];
664 p[0] = SkAlphaMul(r, scale);
665 p[1] = SkAlphaMul(p[1], scale);
666 p[2] = SkAlphaMul(b, scale);
667 p += 4;
668 }
669}
670
671SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000672 if (kGPU_CanvasType != fCanvasType) {
reed@android.com6efdc472008-12-19 18:24:35 +0000673#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000674 detachGL();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000675#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000676 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000677
reed@android.com8a1c16f2008-12-17 15:59:43 +0000678 switch (fCanvasType) {
679 case kRaster_CanvasType:
680 canvas = this->INHERITED::beforeChildren(canvas);
681 break;
682 case kPicture_CanvasType:
683 fPicture = new SkPicture;
684 canvas = fPicture->beginRecording(9999, 9999);
685 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000686 case kGPU_CanvasType: {
reed@google.com64e3eb22011-05-04 14:32:04 +0000687 if (make3DReady()) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000688 SkDevice* device = canvas->getDevice();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000689 const SkBitmap& bitmap = device->accessBitmap(true);
690
bsalomon@google.com5782d712011-01-21 21:03:59 +0000691 GrRenderTarget* renderTarget;
Scroggo3e7ff9f2011-06-16 15:31:26 +0000692
693 GrPlatformSurfaceDesc desc;
694 desc.reset();
695 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
696 desc.fWidth = bitmap.width();
697 desc.fHeight = bitmap.height();
698 desc.fConfig = kRGBA_8888_GrPixelConfig;
699 desc.fStencilBits = 8;
700 GrGLint buffer;
701 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer);
702 desc.fPlatformRenderTarget = buffer;
703
704 renderTarget = static_cast<GrRenderTarget*>(
705 fGrContext->createPlatformSurface(desc));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000706 fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
707 renderTarget->unref();
708
reed@google.comaf951c92011-06-16 19:10:39 +0000709 device = new SkGpuDevice(fGrContext, renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000710 fGpuCanvas->setDevice(device)->unref();
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000711
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000712 fGpuCanvas->concat(canvas->getTotalMatrix());
reed@android.comf2b98d62010-12-20 18:26:13 +0000713 canvas = fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000714
reed@android.comf2b98d62010-12-20 18:26:13 +0000715 } else {
716 canvas = this->INHERITED::beforeChildren(canvas);
717 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000718 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000719 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000720 }
721
722 if (fUseClip) {
723 canvas->drawColor(0xFFFF88FF);
724 canvas->clipPath(fClipPath);
725 }
726
727 return canvas;
728}
729
730static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
731 const SkRegion& rgn) {
732 SkCanvas canvas(bm);
733 SkRegion inval(rgn);
734
735 inval.translate(r.fLeft, r.fTop);
736 canvas.clipRegion(inval);
737 canvas.drawColor(0xFFFF8080);
738}
739
740void SampleWindow::afterChildren(SkCanvas* orig) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000741 if (fRequestGrabImage) {
742 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000743
reed@android.comf2b98d62010-12-20 18:26:13 +0000744 SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
745 SkDevice* device = canvas->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000746 SkBitmap bmp;
747 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000748 static int gSampleGrabCounter;
749 SkString name;
750 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000751 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000752 SkImageEncoder::kPNG_Type, 100);
753 }
754 }
755
reed@android.com8a1c16f2008-12-17 15:59:43 +0000756 switch (fCanvasType) {
757 case kRaster_CanvasType:
758 break;
759 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000760 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000761 SkPicture* pict = new SkPicture(*fPicture);
762 fPicture->unref();
763 orig->drawPicture(*pict);
764 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000765 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000766 SkDynamicMemoryWStream ostream;
767 fPicture->serialize(&ostream);
768 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000769
reed@android.com8a1c16f2008-12-17 15:59:43 +0000770 SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
771 SkPicture pict(&istream);
772 orig->drawPicture(pict);
773 } else {
774 fPicture->draw(orig);
775 fPicture->unref();
776 }
777 fPicture = NULL;
778 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000779#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000780 case kGPU_CanvasType:
Scroggoaed68d92011-06-08 14:26:00 +0000781 if (fShowZoomer && fGpuCanvas) {
Scroggo3272ba82011-05-25 20:50:42 +0000782 this->showZoomer(fGpuCanvas);
783 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000784 delete fGpuCanvas;
785 fGpuCanvas = NULL;
786 presentGL();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000787 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000788#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000789 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000790
reed@google.com17d7aec2011-04-25 14:31:44 +0000791 // Do this after presentGL and other finishing, rather than in afterChild
792 if (fMeasureFPS && fMeasureFPS_Time) {
793 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
794 this->updateTitle();
795 postInvalDelay(this->getSinkID());
796 }
797
798 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000799 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000800 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
801 int dx = fScrollTestX * 7;
802 int dy = fScrollTestY * 7;
803 SkIRect r;
804 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000805
reed@android.com8a1c16f2008-12-17 15:59:43 +0000806 r.set(50, 50, 50+100, 50+100);
807 bm.scrollRect(&r, dx, dy, &inval);
808 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000809 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000810}
811
reed@android.com6c5f6f22009-08-14 16:08:38 +0000812void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
813 if (fScale) {
814 SkScalar scale = SK_Scalar1 * 7 / 10;
815 SkScalar cx = this->width() / 2;
816 SkScalar cy = this->height() / 2;
817 canvas->translate(cx, cy);
818 canvas->scale(scale, scale);
819 canvas->translate(-cx, -cy);
820 }
821 if (fRotate) {
822 SkScalar cx = this->width() / 2;
823 SkScalar cy = this->height() / 2;
824 canvas->translate(cx, cy);
825 canvas->rotate(SkIntToScalar(30));
826 canvas->translate(-cx, -cy);
827 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000828
reed@google.com09e3baa2011-05-18 12:04:31 +0000829 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
830 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000831
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000832 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000833 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
834 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
835 fMeasureFPS_Time = SkTime::GetMSecs();
836 }
837 } else {
838 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000839 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000840 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000841}
842
843void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000844 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000845}
846
reed@android.com8a1c16f2008-12-17 15:59:43 +0000847static SkBitmap::Config gConfigCycle[] = {
848 SkBitmap::kNo_Config, // none -> none
849 SkBitmap::kNo_Config, // a1 -> none
850 SkBitmap::kNo_Config, // a8 -> none
851 SkBitmap::kNo_Config, // index8 -> none
852 SkBitmap::kARGB_4444_Config, // 565 -> 4444
853 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
854 SkBitmap::kRGB_565_Config // 8888 -> 565
855};
856
857static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
858 return gConfigCycle[c];
859}
860
djsollen@google.come32b5832011-06-13 16:58:40 +0000861void SampleWindow::changeZoomLevel(float delta) {
862 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000863 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000864 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
865 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000866 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000867 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
868 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000869 } else {
870 fZoomScale = SK_Scalar1;
871 }
872
djsollen@google.come32b5832011-06-13 16:58:40 +0000873 this->updateTitle();
874
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000875 this->inval(NULL);
876}
877
Scroggo2c8208f2011-06-15 16:49:08 +0000878bool SampleWindow::previousSample() {
879 fCurrIndex = (fCurrIndex - 1) % fSamples.count();
880 this->loadView(fSamples[fCurrIndex]());
881 return true;
882}
883
reed@android.com8a1c16f2008-12-17 15:59:43 +0000884bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000885 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
886 this->loadView(fSamples[fCurrIndex]());
887 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000888}
889
Scroggo2c8208f2011-06-15 16:49:08 +0000890void SampleWindow::postAnimatingEvent() {
891 if (fAnimating) {
892 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
893 evt->post(this->getSinkID(), ANIMATING_DELAY);
894 }
895}
896
reed@android.com8a1c16f2008-12-17 15:59:43 +0000897bool SampleWindow::onEvent(const SkEvent& evt) {
898 if (evt.isType(ANIMATING_EVENTTYPE)) {
899 if (fAnimating) {
900 this->nextSample();
901 this->postAnimatingEvent();
902 }
903 return true;
904 }
reed@android.com34245c72009-11-03 04:00:48 +0000905 if (evt.isType("set-curr-index")) {
906 fCurrIndex = evt.getFast32() % fSamples.count();
907 this->loadView(fSamples[fCurrIndex]());
908 return true;
909 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000910 if (isInvalEvent(evt)) {
911 this->inval(NULL);
912 return true;
913 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000914 return this->INHERITED::onEvent(evt);
915}
916
reed@android.comf2b98d62010-12-20 18:26:13 +0000917bool SampleWindow::onQuery(SkEvent* query) {
918 if (query->isType("get-slide-count")) {
919 query->setFast32(fSamples.count());
920 return true;
921 }
922 if (query->isType("get-slide-title")) {
923 SkView* view = fSamples[query->getFast32()]();
924 SkEvent evt(gTitleEvtName);
925 if (view->doQuery(&evt)) {
926 query->setString("title", evt.findString(gTitleEvtName));
927 }
928 SkSafeUnref(view);
929 return true;
930 }
931 if (query->isType("use-fast-text")) {
932 SkEvent evt(gFastTextEvtName);
933 return curr_view(this)->doQuery(&evt);
934 }
935 return this->INHERITED::onQuery(query);
936}
937
reed@android.com0ae6b242008-12-23 16:49:54 +0000938static void cleanup_for_filename(SkString* name) {
939 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +0000940 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +0000941 switch (str[i]) {
942 case ':': str[i] = '-'; break;
943 case '/': str[i] = '-'; break;
944 case ' ': str[i] = '_'; break;
945 default: break;
946 }
947 }
948}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000949
950bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000951 {
952 SkView* view = curr_view(this);
953 if (view) {
954 SkEvent evt(gCharEvtName);
955 evt.setFast32(uni);
956 if (view->doQuery(&evt)) {
957 return true;
958 }
959 }
960 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000961
reed@android.com8a1c16f2008-12-17 15:59:43 +0000962 int dx = 0xFF;
963 int dy = 0xFF;
964
965 switch (uni) {
966 case '5': dx = 0; dy = 0; break;
967 case '8': dx = 0; dy = -1; break;
968 case '6': dx = 1; dy = 0; break;
969 case '2': dx = 0; dy = 1; break;
970 case '4': dx = -1; dy = 0; break;
971 case '7': dx = -1; dy = -1; break;
972 case '9': dx = 1; dy = -1; break;
973 case '3': dx = 1; dy = 1; break;
974 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000975
reed@android.com8a1c16f2008-12-17 15:59:43 +0000976 default:
977 break;
978 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000979
reed@android.com8a1c16f2008-12-17 15:59:43 +0000980 if (0xFF != dx && 0xFF != dy) {
981 if ((dx | dy) == 0) {
982 fScrollTestX = fScrollTestY = 0;
983 } else {
984 fScrollTestX += dx;
985 fScrollTestY += dy;
986 }
987 this->inval(NULL);
988 return true;
989 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000990
reed@android.com0ae6b242008-12-23 16:49:54 +0000991 switch (uni) {
992 case 'a':
Scroggo2c8208f2011-06-15 16:49:08 +0000993 this->toggleSlideshow();
reed@android.com0ae6b242008-12-23 16:49:54 +0000994 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000995 case 'b':
996 fAAState = cycle_tristate(fAAState);
997 this->updateTitle();
998 this->inval(NULL);
999 break;
1000 case 'c':
1001 fUseClip = !fUseClip;
1002 this->inval(NULL);
1003 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001004 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001005 case 'd':
1006 SkGraphics::SetFontCacheUsed(0);
1007 return true;
1008 case 'f':
Scroggo2c8208f2011-06-15 16:49:08 +00001009 this->toggleFPS();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001010 break;
1011 case 'g':
1012 fRequestGrabImage = true;
1013 this->inval(NULL);
1014 break;
reed@google.com09e3baa2011-05-18 12:04:31 +00001015 case 'h':
1016 fHintingState = cycle_tristate(fHintingState);
1017 this->updateTitle();
1018 this->inval(NULL);
1019 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001020 case 'i':
1021 this->zoomIn();
1022 break;
1023 case 'l':
1024 fLCDState = cycle_tristate(fLCDState);
1025 this->updateTitle();
1026 this->inval(NULL);
1027 break;
reed@google.com176753a2011-05-17 15:32:04 +00001028 case 'n':
1029 fFilterState = cycle_tristate(fFilterState);
1030 this->updateTitle();
1031 this->inval(NULL);
1032 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001033 case 'o':
1034 this->zoomOut();
1035 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001036 case 'p':
1037 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001038 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001039 this->inval(NULL);
1040 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001041 case 'r':
1042 fRotate = !fRotate;
1043 this->inval(NULL);
1044 this->updateTitle();
1045 return true;
1046 case 's':
1047 fScale = !fScale;
1048 this->inval(NULL);
1049 this->updateTitle();
1050 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001051 case 'x':
1052 fFlipAxis ^= kFlipAxis_X;
1053 this->updateTitle();
1054 this->inval(NULL);
1055 break;
1056 case 'y':
1057 fFlipAxis ^= kFlipAxis_Y;
1058 this->updateTitle();
1059 this->inval(NULL);
1060 break;
scroggo08526c02011-03-22 14:03:21 +00001061 case 'z':
1062 this->toggleZoomer();
1063 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001064 default:
1065 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001066 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001067
reed@android.com8a1c16f2008-12-17 15:59:43 +00001068 return this->INHERITED::onHandleChar(uni);
1069}
1070
Scroggo2c8208f2011-06-15 16:49:08 +00001071void SampleWindow::toggleFPS() {
1072 fMeasureFPS = !fMeasureFPS;
1073 this->inval(NULL);
1074 this->updateTitle();
1075}
1076
1077void SampleWindow::toggleSlideshow() {
1078 fAnimating = !fAnimating;
1079 this->postAnimatingEvent();
1080 this->updateTitle();
1081}
1082
1083void SampleWindow::toggleRendering() {
1084 fCanvasType = cycle_canvastype(fCanvasType);
1085 this->updateTitle();
1086 this->inval(NULL);
1087}
1088
reed@android.com8a1c16f2008-12-17 15:59:43 +00001089#include "SkDumpCanvas.h"
1090
1091bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001092 {
1093 SkView* view = curr_view(this);
1094 if (view) {
1095 SkEvent evt(gKeyEvtName);
1096 evt.setFast32(key);
1097 if (view->doQuery(&evt)) {
1098 return true;
1099 }
1100 }
1101 }
1102
reed@android.com8a1c16f2008-12-17 15:59:43 +00001103 switch (key) {
1104 case kRight_SkKey:
1105 if (this->nextSample()) {
1106 return true;
1107 }
1108 break;
1109 case kLeft_SkKey:
Scroggo2c8208f2011-06-15 16:49:08 +00001110 toggleRendering();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001111 return true;
1112 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001113 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001114 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001115 } else {
1116 fNClip = !fNClip;
1117 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001118 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001119 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001120 return true;
1121 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001122 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001123 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001124 } else {
1125 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001126 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001127 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001128 return true;
1129 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001130 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001131 SkDebugfDumper dumper;
1132 SkDumpCanvas dc(&dumper);
1133 this->draw(&dc);
1134 } else {
1135 fRepeatDrawing = !fRepeatDrawing;
1136 if (fRepeatDrawing) {
1137 this->inval(NULL);
1138 }
1139 }
1140 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001141 case kBack_SkKey:
1142 this->loadView(NULL);
1143 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001144 default:
1145 break;
1146 }
1147 return this->INHERITED::onHandleKey(key);
1148}
1149
reed@google.com52f57e12011-03-16 12:10:02 +00001150///////////////////////////////////////////////////////////////////////////////
1151
1152static const char gGestureClickType[] = "GestureClickType";
1153
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001154bool SampleWindow::onDispatchClick(int x, int y, Click::State state) {
Scroggo0f185c22011-03-24 18:35:50 +00001155 if (Click::kMoved_State == state) {
1156 updatePointer(x, y);
1157 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001158 int w = SkScalarRound(this->width());
1159 int h = SkScalarRound(this->height());
1160
1161 // check for the resize-box
1162 if (w - x < 16 && h - y < 16) {
1163 return false; // let the OS handle the click
1164 } else {
1165 return this->INHERITED::onDispatchClick(x, y, state);
1166 }
1167}
1168
reed@google.com52f57e12011-03-16 12:10:02 +00001169class GestureClick : public SkView::Click {
1170public:
1171 GestureClick(SkView* target) : SkView::Click(target) {
1172 this->setType(gGestureClickType);
1173 }
1174
1175 static bool IsGesture(Click* click) {
1176 return click->isType(gGestureClickType);
1177 }
1178};
1179
1180SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1181 return new GestureClick(this);
1182}
1183
Scroggoa54e2f62011-06-17 12:46:17 +00001184union IntPtr {
1185 int fInt;
1186 void* fPtr;
1187};
1188
1189static void* int2ptr(int n) {
1190 IntPtr data;
1191 data.fInt = n;
1192 return data.fPtr;
1193}
1194
1195bool SampleWindow::handleTouch(int ownerId, float x, float y, SkView::Click::State state) {
1196 void* click = int2ptr(ownerId);
1197 switch(state) {
1198 case SkView::Click::kDown_State:
1199 fGesture.touchBegin(click, x, y);
1200 break;
1201 case SkView::Click::kMoved_State:
1202 fGesture.touchMoved(click, x, y);
1203 this->inval(NULL);
1204 break;
1205 case SkView::Click::kUp_State:
1206 fGesture.touchEnd(click);
1207 this->inval(NULL);
1208 break;
1209 default:
1210 return false;
1211 }
1212 return true;
1213}
1214
reed@google.com52f57e12011-03-16 12:10:02 +00001215bool SampleWindow::onClick(Click* click) {
1216 if (GestureClick::IsGesture(click)) {
1217 float x = SkScalarToFloat(click->fCurr.fX);
1218 float y = SkScalarToFloat(click->fCurr.fY);
1219 switch (click->fState) {
1220 case SkView::Click::kDown_State:
1221 fGesture.touchBegin(click, x, y);
1222 break;
1223 case SkView::Click::kMoved_State:
1224 fGesture.touchMoved(click, x, y);
1225 this->inval(NULL);
1226 break;
1227 case SkView::Click::kUp_State:
1228 fGesture.touchEnd(click);
1229 this->inval(NULL);
1230 break;
1231 }
1232 return true;
1233 }
1234 return false;
1235}
1236
1237///////////////////////////////////////////////////////////////////////////////
1238
reed@android.com8a1c16f2008-12-17 15:59:43 +00001239void SampleWindow::loadView(SkView* view) {
1240 SkView::F2BIter iter(this);
1241 SkView* prev = iter.next();
1242 if (prev) {
1243 prev->detachFromParent();
1244 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001245
reed@android.com34245c72009-11-03 04:00:48 +00001246 if (NULL == view) {
1247 view = create_overview(fSamples.count(), fSamples.begin());
1248 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001249 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001250 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001251 this->attachChildToFront(view)->unref();
1252 view->setSize(this->width(), this->height());
1253
1254 this->updateTitle();
1255}
1256
1257static const char* gConfigNames[] = {
1258 "unknown config",
1259 "A1",
1260 "A8",
1261 "Index8",
1262 "565",
1263 "4444",
1264 "8888"
1265};
1266
1267static const char* configToString(SkBitmap::Config c) {
1268 return gConfigNames[c];
1269}
1270
1271static const char* gCanvasTypePrefix[] = {
1272 "raster: ",
1273 "picture: ",
1274 "opengl: "
1275};
1276
reed@google.com569e0432011-04-05 13:07:03 +00001277static const char* trystate_str(SkTriState state,
1278 const char trueStr[], const char falseStr[]) {
1279 if (kTrue_SkTriState == state) {
1280 return trueStr;
1281 } else if (kFalse_SkTriState == state) {
1282 return falseStr;
1283 }
1284 return NULL;
1285}
1286
reed@android.com8a1c16f2008-12-17 15:59:43 +00001287void SampleWindow::updateTitle() {
1288 SkString title;
1289
1290 SkView::F2BIter iter(this);
1291 SkView* view = iter.next();
1292 SkEvent evt(gTitleEvtName);
1293 if (view->doQuery(&evt)) {
1294 title.set(evt.findString(gTitleEvtName));
1295 }
1296 if (title.size() == 0) {
1297 title.set("<unknown>");
1298 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001299
reed@android.com8a1c16f2008-12-17 15:59:43 +00001300 title.prepend(gCanvasTypePrefix[fCanvasType]);
1301
1302 title.prepend(" ");
1303 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001304
reed@android.com8a1c16f2008-12-17 15:59:43 +00001305 if (fAnimating) {
1306 title.prepend("<A> ");
1307 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001308 if (fScale) {
1309 title.prepend("<S> ");
1310 }
1311 if (fRotate) {
1312 title.prepend("<R> ");
1313 }
reed@android.come522ca52009-11-23 20:10:41 +00001314 if (fNClip) {
1315 title.prepend("<C> ");
1316 }
reed@google.com569e0432011-04-05 13:07:03 +00001317
1318 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1319 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001320 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001321 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1322 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001323
1324 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001325 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001326 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001327
1328 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001329 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001330 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001331 if (fUsePipe && SampleView::IsSampleView(view)) {
1332 title.prepend("<P> ");
1333 }
1334 if (SampleView::IsSampleView(view)) {
1335 title.prepend("! ");
1336 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001337
reed@android.com8a1c16f2008-12-17 15:59:43 +00001338 this->setTitle(title.c_str());
1339}
1340
1341void SampleWindow::onSizeChange() {
1342 this->INHERITED::onSizeChange();
1343
1344 SkView::F2BIter iter(this);
1345 SkView* view = iter.next();
1346 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001347
reed@android.com8a1c16f2008-12-17 15:59:43 +00001348 // rebuild our clippath
1349 {
1350 const SkScalar W = this->width();
1351 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001352
reed@android.com8a1c16f2008-12-17 15:59:43 +00001353 fClipPath.reset();
1354#if 0
1355 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1356 SkRect r;
1357 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1358 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1359 fClipPath.addRect(r);
1360 }
1361#else
1362 SkRect r;
1363 r.set(0, 0, W, H);
1364 fClipPath.addRect(r, SkPath::kCCW_Direction);
1365 r.set(W/4, H/4, W*3/4, H*3/4);
1366 fClipPath.addRect(r, SkPath::kCW_Direction);
1367#endif
1368 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001369
Scroggo2c8208f2011-06-15 16:49:08 +00001370 fZoomCenterX = SkScalarHalf(this->width());
1371 fZoomCenterY = SkScalarHalf(this->height());
1372
Scroggo3e7ff9f2011-06-16 15:31:26 +00001373#ifdef ANDROID
Scroggob4490c72011-06-17 13:53:05 +00001374 // FIXME: The first draw after a size change does not work on Android, so
1375 // we post an invalidate.
Scroggo3e7ff9f2011-06-16 15:31:26 +00001376 postInvalDelay(this->getSinkID());
Scroggo716a0382011-06-16 14:00:15 +00001377#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001378 this->updateTitle(); // to refresh our config
1379}
1380
1381///////////////////////////////////////////////////////////////////////////////
1382
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001383static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001384static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001385static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001386
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001387bool SampleView::IsSampleView(SkView* view) {
1388 SkEvent evt(is_sample_view_tag);
1389 return view->doQuery(&evt);
1390}
1391
reed@google.comf2183392011-04-22 14:10:48 +00001392bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001393 SkEvent evt(repeat_count_tag);
1394 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001395 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001396}
1397
reed@google.com0faac1e2011-05-11 05:58:58 +00001398bool SampleView::SetUsePipe(SkView* view, bool pred) {
1399 SkEvent evt(set_use_pipe_tag);
1400 evt.setFast32(pred);
1401 return view->doEvent(evt);
1402}
1403
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001404bool SampleView::onEvent(const SkEvent& evt) {
1405 if (evt.isType(repeat_count_tag)) {
1406 fRepeatCount = evt.getFast32();
1407 return true;
1408 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001409 if (evt.isType(set_use_pipe_tag)) {
1410 fUsePipe = !!evt.getFast32();
1411 return true;
1412 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001413 return this->INHERITED::onEvent(evt);
1414}
1415
1416bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001417 if (evt->isType(is_sample_view_tag)) {
1418 return true;
1419 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001420 return this->INHERITED::onQuery(evt);
1421}
1422
reed@google.com68f456d2011-05-02 18:55:39 +00001423#ifdef TEST_GPIPE
1424 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001425
1426class SimplePC : public SkGPipeController {
1427public:
1428 SimplePC(SkCanvas* target);
1429 ~SimplePC();
1430
1431 virtual void* requestBlock(size_t minRequest, size_t* actual);
1432 virtual void notifyWritten(size_t bytes);
1433
1434private:
reed@google.com961ddb02011-05-05 14:03:48 +00001435 SkGPipeReader fReader;
1436 void* fBlock;
1437 size_t fBlockSize;
1438 size_t fBytesWritten;
1439 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001440 SkGPipeReader::Status fStatus;
1441
1442 size_t fTotalWritten;
1443};
1444
1445SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1446 fBlock = NULL;
1447 fBlockSize = fBytesWritten = 0;
1448 fStatus = SkGPipeReader::kDone_Status;
1449 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001450 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001451}
1452
1453SimplePC::~SimplePC() {
1454// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1455 sk_free(fBlock);
1456
reed@google.com0faac1e2011-05-11 05:58:58 +00001457 if (fTotalWritten) {
1458 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1459 fAtomsWritten, fStatus);
1460 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001461}
1462
1463void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1464 sk_free(fBlock);
1465
1466 fBlockSize = minRequest * 4;
1467 fBlock = sk_malloc_throw(fBlockSize);
1468 fBytesWritten = 0;
1469 *actual = fBlockSize;
1470 return fBlock;
1471}
1472
1473void SimplePC::notifyWritten(size_t bytes) {
1474 SkASSERT(fBytesWritten + bytes <= fBlockSize);
yangsu@google.com1f394212011-06-01 18:03:34 +00001475
1476#ifdef PIPE_FILE
1477 //File is open in append mode
1478 FILE* f = fopen(FILE_PATH, "ab");
1479 SkASSERT(f != NULL);
1480 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
1481 fclose(f);
1482#endif
1483
reed@google.com64e3eb22011-05-04 14:32:04 +00001484 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1485 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1486 fBytesWritten += bytes;
1487 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001488
1489 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001490}
1491
reed@google.com68f456d2011-05-02 18:55:39 +00001492#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001493
reed@google.com64e3eb22011-05-04 14:32:04 +00001494
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001495void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001496#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001497 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001498 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001499 if (fUsePipe) {
reed@google.comdde09562011-05-23 12:21:05 +00001500 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
1501// flags = 0;
1502 canvas = writer.startRecording(&controller, flags);
reed@google.com0faac1e2011-05-11 05:58:58 +00001503 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001504#endif
1505
reed@google.com81e3d7f2011-06-01 12:42:36 +00001506 this->onDrawBackground(canvas);
1507
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001508 for (int i = 0; i < fRepeatCount; i++) {
1509 SkAutoCanvasRestore acr(canvas, true);
1510 this->onDrawContent(canvas);
1511 }
1512}
1513
1514void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001515 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001516}
1517
1518///////////////////////////////////////////////////////////////////////////////
1519
reed@android.comf2b98d62010-12-20 18:26:13 +00001520template <typename T> void SkTBSort(T array[], int count) {
1521 for (int i = 1; i < count - 1; i++) {
1522 bool didSwap = false;
1523 for (int j = count - 1; j > i; --j) {
1524 if (array[j] < array[j-1]) {
1525 T tmp(array[j-1]);
1526 array[j-1] = array[j];
1527 array[j] = tmp;
1528 didSwap = true;
1529 }
1530 }
1531 if (!didSwap) {
1532 break;
1533 }
1534 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001535
reed@android.comf2b98d62010-12-20 18:26:13 +00001536 for (int k = 0; k < count - 1; k++) {
1537 SkASSERT(!(array[k+1] < array[k]));
1538 }
1539}
1540
1541#include "SkRandom.h"
1542
1543static void rand_rect(SkIRect* rect, SkRandom& rand) {
1544 int bits = 8;
1545 int shift = 32 - bits;
1546 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1547 rand.nextU() >> shift, rand.nextU() >> shift);
1548 rect->sort();
1549}
1550
1551static void dumpRect(const SkIRect& r) {
1552 SkDebugf(" { %d, %d, %d, %d },\n",
1553 r.fLeft, r.fTop,
1554 r.fRight, r.fBottom);
1555}
1556
1557static void test_rects(const SkIRect rect[], int count) {
1558 SkRegion rgn0, rgn1;
1559
1560 for (int i = 0; i < count; i++) {
1561 rgn0.op(rect[i], SkRegion::kUnion_Op);
1562 // dumpRect(rect[i]);
1563 }
1564 rgn1.setRects(rect, count);
1565
1566 if (rgn0 != rgn1) {
1567 SkDebugf("\n");
1568 for (int i = 0; i < count; i++) {
1569 dumpRect(rect[i]);
1570 }
1571 SkDebugf("\n");
1572 }
1573}
1574
1575static void test() {
1576 size_t i;
1577
1578 const SkIRect r0[] = {
1579 { 0, 0, 1, 1 },
1580 { 2, 2, 3, 3 },
1581 };
1582 const SkIRect r1[] = {
1583 { 0, 0, 1, 3 },
1584 { 1, 1, 2, 2 },
1585 { 2, 0, 3, 3 },
1586 };
1587 const SkIRect r2[] = {
1588 { 0, 0, 1, 2 },
1589 { 2, 1, 3, 3 },
1590 { 4, 0, 5, 1 },
1591 { 6, 0, 7, 4 },
1592 };
1593
1594 static const struct {
1595 const SkIRect* fRects;
1596 int fCount;
1597 } gRecs[] = {
1598 { r0, SK_ARRAY_COUNT(r0) },
1599 { r1, SK_ARRAY_COUNT(r1) },
1600 { r2, SK_ARRAY_COUNT(r2) },
1601 };
1602
1603 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1604 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1605 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001606
reed@android.comf2b98d62010-12-20 18:26:13 +00001607 SkRandom rand;
1608 for (i = 0; i < 10000; i++) {
1609 SkRegion rgn0, rgn1;
1610
1611 const int N = 8;
1612 SkIRect rect[N];
1613 for (int j = 0; j < N; j++) {
1614 rand_rect(&rect[j], rand);
1615 }
1616 test_rects(rect, N);
1617 }
1618}
1619
reed@android.com8a1c16f2008-12-17 15:59:43 +00001620SkOSWindow* create_sk_window(void* hwnd) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001621// test();
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001622 return new SampleWindow(hwnd);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001623}
1624
1625void get_preferred_size(int* x, int* y, int* width, int* height) {
1626 *x = 10;
1627 *y = 50;
1628 *width = 640;
1629 *height = 480;
1630}
1631
1632void application_init() {
1633// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001634#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001635 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001636#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001637 SkGraphics::Init();
1638 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001639}
1640
1641void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001642 SkEvent::Term();
1643 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001644}