blob: 74a07080eebf3d264384c55b4ef41eeb5ce49c32 [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
Scroggo8ac0d542011-06-21 14:44:57 +000021#include "SkPDFDevice.h"
22#include "SkPDFDocument.h"
23#include "SkStream.h"
24
yangsu@google.com1f394212011-06-01 18:03:34 +000025#define TEST_GPIPEx
26
27#ifdef TEST_GPIPE
28#define PIPE_FILE
29#define FILE_PATH "/path/to/drawing.data"
30#endif
31
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +000032#define USE_ARROWS_FOR_ZOOM true
reed@android.comf2b98d62010-12-20 18:26:13 +000033//#define DEFAULT_TO_GPU
34
reed@android.come191b162009-12-18 21:33:39 +000035extern SkView* create_overview(int, const SkViewFactory[]);
reed@android.com34245c72009-11-03 04:00:48 +000036
reed@android.comcb342352010-07-22 18:27:53 +000037#define SK_SUPPORT_GL
reed@android.com8a1c16f2008-12-17 15:59:43 +000038
39#define ANIMATING_EVENTTYPE "nextSample"
40#define ANIMATING_DELAY 750
41
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000042#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +000043 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000044#else
reed@google.combad8c872011-05-18 20:10:31 +000045 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000046#endif
reed@google.combad8c872011-05-18 20:10:31 +000047#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000048
reed@google.comac10a2d2010-12-22 21:39:39 +000049#ifdef SK_SUPPORT_GL
50 #include "GrGLConfig.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000051#endif
52
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000053///////////////
54static const char view_inval_msg[] = "view-inval-msg";
55
Scroggo62b65b02011-06-21 16:01:26 +000056void SampleWindow::postInvalDelay() {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000057 SkEvent* evt = new SkEvent(view_inval_msg);
Scroggo62b65b02011-06-21 16:01:26 +000058 evt->post(this->getSinkID(), 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000059}
60
61static bool isInvalEvent(const SkEvent& evt) {
62 return evt.isType(view_inval_msg);
63}
64//////////////////
65
reed@android.com8a1c16f2008-12-17 15:59:43 +000066SkViewRegister* SkViewRegister::gHead;
67SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) {
68 static bool gOnce;
69 if (!gOnce) {
70 gHead = NULL;
71 gOnce = true;
72 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000073
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 fChain = gHead;
75 gHead = this;
76}
77
reed@android.comf2b98d62010-12-20 18:26:13 +000078#if defined(SK_SUPPORT_GL)
79 #define SK_USE_SHADERS
80#endif
81
twiz@google.com06c3b6b2011-03-14 16:58:39 +000082#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +000083#include <CoreFoundation/CoreFoundation.h>
84#include <CoreFoundation/CFURLAccess.h>
85
86static void testpdf() {
87 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
88 kCFStringEncodingUTF8);
89 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
90 kCFURLPOSIXPathStyle,
91 false);
92 CFRelease(path);
93 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
94 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
95 CFRelease(url);
96
97 CGContextBeginPage(cg, &box);
98 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
99 CGContextFillEllipseInRect(cg, r);
100 CGContextEndPage(cg);
101 CGContextRelease(cg);
102
103 if (false) {
104 SkBitmap bm;
105 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
106 bm.allocPixels();
107 bm.eraseColor(0);
108
109 SkCanvas canvas(bm);
110
111 }
112}
113#endif
114
115//////////////////////////////////////////////////////////////////////////////
116
reed@google.com569e0432011-04-05 13:07:03 +0000117enum FlipAxisEnum {
118 kFlipAxis_X = (1 << 0),
119 kFlipAxis_Y = (1 << 1)
120};
121
reed@google.com569e0432011-04-05 13:07:03 +0000122static SkTriState cycle_tristate(SkTriState state) {
123 static const SkTriState gCycle[] = {
124 /* kFalse_SkTriState -> */ kUnknown_SkTriState,
125 /* kTrue_SkTriState -> */ kFalse_SkTriState,
126 /* kUnknown_SkTriState -> */ kTrue_SkTriState,
127 };
128 return gCycle[state];
129}
130
reed@google.comf0b5f682011-03-11 20:08:25 +0000131#include "SkDrawFilter.h"
132
reed@google.com569e0432011-04-05 13:07:03 +0000133class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000134public:
reed@google.com09e3baa2011-05-18 12:04:31 +0000135 FlagsDrawFilter(SkTriState lcd, SkTriState aa, SkTriState filter,
136 SkTriState hinting) :
137 fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000138
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000139 virtual void filter(SkPaint* paint, Type t) {
reed@google.com569e0432011-04-05 13:07:03 +0000140 if (kText_Type == t && kUnknown_SkTriState != fLCDState) {
reed@google.com569e0432011-04-05 13:07:03 +0000141 paint->setLCDRenderText(kTrue_SkTriState == fLCDState);
142 }
143 if (kUnknown_SkTriState != fAAState) {
reed@google.com569e0432011-04-05 13:07:03 +0000144 paint->setAntiAlias(kTrue_SkTriState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000145 }
reed@google.com176753a2011-05-17 15:32:04 +0000146 if (kUnknown_SkTriState != fFilterState) {
147 paint->setFilterBitmap(kTrue_SkTriState == fFilterState);
148 }
reed@google.com09e3baa2011-05-18 12:04:31 +0000149 if (kUnknown_SkTriState != fHintingState) {
150 paint->setHinting(kTrue_SkTriState == fHintingState ?
151 SkPaint::kNormal_Hinting :
152 SkPaint::kSlight_Hinting);
153 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000154 }
155
156private:
reed@google.com569e0432011-04-05 13:07:03 +0000157 SkTriState fLCDState;
reed@google.com569e0432011-04-05 13:07:03 +0000158 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000159 SkTriState fFilterState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000160 SkTriState fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000161};
162
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163//////////////////////////////////////////////////////////////////////////////
164
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000165#define MAX_ZOOM_LEVEL 8
166#define MIN_ZOOM_LEVEL -8
167
reed@android.comf2b98d62010-12-20 18:26:13 +0000168static const char gCharEvtName[] = "SampleCode_Char_Event";
169static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170static const char gTitleEvtName[] = "SampleCode_Title_Event";
171static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000172static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
173
174bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
175 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
176 if (outUni) {
177 *outUni = evt.getFast32();
178 }
179 return true;
180 }
181 return false;
182}
183
184bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
185 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
186 if (outKey) {
187 *outKey = (SkKey)evt.getFast32();
188 }
189 return true;
190 }
191 return false;
192}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193
194bool SampleCode::TitleQ(const SkEvent& evt) {
195 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
196}
197
198void SampleCode::TitleR(SkEvent* evt, const char title[]) {
199 SkASSERT(evt && TitleQ(*evt));
200 evt->setString(gTitleEvtName, title);
201}
202
203bool SampleCode::PrefSizeQ(const SkEvent& evt) {
204 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
205}
206
207void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
208 SkASSERT(evt && PrefSizeQ(*evt));
209 SkScalar size[2];
210 size[0] = width;
211 size[1] = height;
212 evt->setScalars(gPrefSizeEvtName, 2, size);
213}
214
reed@android.comf2b98d62010-12-20 18:26:13 +0000215bool SampleCode::FastTextQ(const SkEvent& evt) {
216 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
217}
218
219///////////////////////////////////////////////////////////////////////////////
220
reed@android.com44177402009-11-23 21:07:51 +0000221static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000222static SkMSec gAnimTimePrev;
223
reed@android.com44177402009-11-23 21:07:51 +0000224SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000225SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
226SkScalar SampleCode::GetAnimSecondsDelta() {
227 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
228}
reed@android.com44177402009-11-23 21:07:51 +0000229
230SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000231 // since gAnimTime can be up to 32 bits, we can't convert it to a float
232 // or we'll lose the low bits. Hence we use doubles for the intermediate
233 // calculations
234 double seconds = (double)gAnimTime / 1000.0;
235 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000236 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000237 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000238 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000239 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000240}
241
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242//////////////////////////////////////////////////////////////////////////////
243
reed@android.comf2b98d62010-12-20 18:26:13 +0000244static SkView* curr_view(SkWindow* wind) {
245 SkView::F2BIter iter(wind);
246 return iter.next();
247}
248
Scroggo2c8208f2011-06-15 16:49:08 +0000249void SampleWindow::setZoomCenter(float x, float y)
250{
251 fZoomCenterX = SkFloatToScalar(x);
252 fZoomCenterY = SkFloatToScalar(y);
253}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254
djsollen@google.come32b5832011-06-13 16:58:40 +0000255bool SampleWindow::setGrContext(GrContext* context)
256{
257 if (fGrContext) {
258 fGrContext->unref();
259 }
260 fGrContext = context;
261 fGrContext->ref();
262 return true;
263}
264
265GrContext* SampleWindow::getGrContext()
266{
267 return fGrContext;
268}
djsollen@google.come32b5832011-06-13 16:58:40 +0000269
Scroggo0f185c22011-03-24 18:35:50 +0000270bool SampleWindow::zoomIn()
271{
272 // Arbitrarily decided
273 if (fFatBitsScale == 25) return false;
274 fFatBitsScale++;
275 this->inval(NULL);
276 return true;
277}
278
279bool SampleWindow::zoomOut()
280{
281 if (fFatBitsScale == 1) return false;
282 fFatBitsScale--;
283 this->inval(NULL);
284 return true;
285}
286
287void SampleWindow::toggleZoomer()
288{
289 fShowZoomer = !fShowZoomer;
290 this->inval(NULL);
291}
292
293void SampleWindow::updatePointer(int x, int y)
294{
295 fMouseX = x;
296 fMouseY = y;
297 if (fShowZoomer) {
298 this->inval(NULL);
299 }
300}
301
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000302bool SampleWindow::make3DReady() {
303
304#if defined(SK_SUPPORT_GL)
bsalomon@google.com498a6232011-03-10 18:24:15 +0000305 if (attachGL()) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000306 if (NULL != fGrContext) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000307 // various gr lifecycle tests
308 #if 0
309 fGrContext->freeGpuResources();
310 #elif 0
311 // this will leak resources.
312 fGrContext->contextLost();
313 #elif 0
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000314 GrAssert(1 == fGrContext->refcnt());
315 fGrContext->unref();
316 fGrContext = NULL;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000317 #endif
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000318 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000319
bsalomon@google.com498a6232011-03-10 18:24:15 +0000320 if (NULL == fGrContext) {
321 #if defined(SK_USE_SHADERS)
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000322 fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000323 #else
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000324 fGrContext = GrContext::Create(kOpenGL_Fixed_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000325 #endif
reed@google.com569e0432011-04-05 13:07:03 +0000326 SkDebugf("---- constructor\n");
bsalomon@google.com498a6232011-03-10 18:24:15 +0000327 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000328
bsalomon@google.com498a6232011-03-10 18:24:15 +0000329 if (NULL != fGrContext) {
330 return true;
331 } else {
332 detachGL();
333 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000334 }
335#endif
336 SkDebugf("Failed to setup 3D");
337 return false;
338}
339
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) {
341 static const CanvasType gCT[] = {
342 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000343 kGPU_CanvasType,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344 kRaster_CanvasType
345 };
346 return gCT[ct];
347}
348
349SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) {
yangsu@google.com1f394212011-06-01 18:03:34 +0000350#ifdef PIPE_FILE
351 //Clear existing file or create file if it doesn't exist
352 FILE* f = fopen(FILE_PATH, "wb");
353 fclose(f);
354#endif
355
reed@android.com8a1c16f2008-12-17 15:59:43 +0000356 fPicture = NULL;
reed@android.comf2b98d62010-12-20 18:26:13 +0000357 fGpuCanvas = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000358
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000359 fGrContext = NULL;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000360
reed@android.comf2b98d62010-12-20 18:26:13 +0000361#ifdef DEFAULT_TO_GPU
362 fCanvasType = kGPU_CanvasType;
363#else
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364 fCanvasType = kRaster_CanvasType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000365#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000367 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000368 fRepeatDrawing = false;
369 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000370 fRotate = false;
371 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000372 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000373 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000374 fMeasureFPS = false;
reed@google.com569e0432011-04-05 13:07:03 +0000375 fLCDState = kUnknown_SkTriState;
376 fAAState = kUnknown_SkTriState;
reed@google.com66f22fd2011-05-17 15:33:45 +0000377 fFilterState = kUnknown_SkTriState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000378 fHintingState = kUnknown_SkTriState;
reed@google.com569e0432011-04-05 13:07:03 +0000379 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000380 fScrollTestX = fScrollTestY = 0;
381
Scroggo0f185c22011-03-24 18:35:50 +0000382 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000383 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000384 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
385 fShowZoomer = false;
386
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000387 fZoomLevel = 0;
388 fZoomScale = SK_Scalar1;
389
Scroggo8ac0d542011-06-21 14:44:57 +0000390 fSaveToPdf = false;
391 fPdfCanvas = NULL;
392
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000393// this->setConfig(SkBitmap::kRGB_565_Config);
394 this->setConfig(SkBitmap::kARGB_8888_Config);
395 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000396 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000397
reed@android.com34245c72009-11-03 04:00:48 +0000398 {
399 const SkViewRegister* reg = SkViewRegister::Head();
400 while (reg) {
401 *fSamples.append() = reg->factory();
402 reg = reg->next();
403 }
404 }
405 fCurrIndex = 0;
reed@android.come0f13ee2009-11-04 19:40:25 +0000406 this->loadView(fSamples[fCurrIndex]());
reed@google.comf0b5f682011-03-11 20:08:25 +0000407
Scroggob4490c72011-06-17 13:53:05 +0000408 // If another constructor set our dimensions, ensure that our
409 // onSizeChange gets called.
410 if (this->height() && this->width()) {
411 this->onSizeChange();
412 }
yangsu@google.com501775e2011-06-24 16:04:50 +0000413
414 fPDFData = NULL;
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000415#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +0000416 testpdf();
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000417#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000418}
419
420SampleWindow::~SampleWindow() {
421 delete fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000422 delete fGpuCanvas;
Scroggo8ac0d542011-06-21 14:44:57 +0000423 delete fPdfCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000424 if (NULL != fGrContext) {
425 fGrContext->unref();
426 }
Scroggo0f185c22011-03-24 18:35:50 +0000427 fTypeface->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428}
429
reed@android.com55e76b22009-11-23 21:46:47 +0000430static SkBitmap capture_bitmap(SkCanvas* canvas) {
431 SkBitmap bm;
432 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
433 src.copyTo(&bm, src.config());
434 return bm;
435}
436
437static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
438 SkBitmap* diff) {
439 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000440
reed@android.com55e76b22009-11-23 21:46:47 +0000441 SkAutoLockPixels alp0(src);
442 SkAutoLockPixels alp1(orig);
443 for (int y = 0; y < src.height(); y++) {
444 const void* srcP = src.getAddr(0, y);
445 const void* origP = orig.getAddr(0, y);
446 size_t bytes = src.width() * src.bytesPerPixel();
447 if (memcmp(srcP, origP, bytes)) {
448 SkDebugf("---------- difference on line %d\n", y);
449 return true;
450 }
451 }
452 return false;
453}
454
Scroggo0f185c22011-03-24 18:35:50 +0000455static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
456{
457 SkColor desiredColor = paint.getColor();
458 paint.setColor(SK_ColorWHITE);
459 const char* c_str = string.c_str();
460 size_t size = string.size();
461 SkRect bounds;
462 paint.measureText(c_str, size, &bounds);
463 bounds.offset(left, top);
464 SkScalar inset = SkIntToScalar(-2);
465 bounds.inset(inset, inset);
466 canvas->drawRect(bounds, paint);
467 if (desiredColor != SK_ColorBLACK) {
468 paint.setColor(SK_ColorBLACK);
469 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
470 }
471 paint.setColor(desiredColor);
472 canvas->drawText(c_str, size, left, top, paint);
473}
474
reed@android.com44177402009-11-23 21:07:51 +0000475#define XCLIP_N 8
476#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000477
478void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000479 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000480 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000481 gAnimTime = SkTime::GetMSecs();
482
Scroggo2c8208f2011-06-15 16:49:08 +0000483 SkScalar cx = fZoomCenterX;
484 SkScalar cy = fZoomCenterY;
reed@google.com569e0432011-04-05 13:07:03 +0000485
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000486 if (fZoomLevel) {
487 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000488 SkPoint center;
489 m = canvas->getTotalMatrix();//.invert(&m);
490 m.mapXY(cx, cy, &center);
491 cx = center.fX;
492 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000493
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000494 m.setTranslate(-cx, -cy);
495 m.postScale(fZoomScale, fZoomScale);
496 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000497
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000498 canvas->concat(m);
499 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000500
reed@google.com569e0432011-04-05 13:07:03 +0000501 if (fFlipAxis) {
502 SkMatrix m;
503 m.setTranslate(cx, cy);
504 if (fFlipAxis & kFlipAxis_X) {
505 m.preScale(-SK_Scalar1, SK_Scalar1);
506 }
507 if (fFlipAxis & kFlipAxis_Y) {
508 m.preScale(SK_Scalar1, -SK_Scalar1);
509 }
510 m.preTranslate(-cx, -cy);
511 canvas->concat(m);
512 }
513
reed@google.com52f57e12011-03-16 12:10:02 +0000514 // Apply any gesture matrix
515 if (true) {
516 const SkMatrix& localM = fGesture.localM();
517 if (localM.getType() & SkMatrix::kScale_Mask) {
518 canvas->setExternalMatrix(&localM);
519 }
520 canvas->concat(localM);
521 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000522
reed@google.com52f57e12011-03-16 12:10:02 +0000523 if (fGesture.isActive()) {
524 this->inval(NULL);
525 }
526 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000527
reed@android.come522ca52009-11-23 20:10:41 +0000528 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000529 this->INHERITED::draw(canvas);
530 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000531
532 const SkScalar w = this->width();
533 const SkScalar h = this->height();
534 const SkScalar cw = w / XCLIP_N;
535 const SkScalar ch = h / YCLIP_N;
536 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000537 SkRect r;
538 r.fTop = y * ch;
539 r.fBottom = (y + 1) * ch;
540 if (y == YCLIP_N - 1) {
541 r.fBottom = h;
542 }
reed@android.come522ca52009-11-23 20:10:41 +0000543 for (int x = 0; x < XCLIP_N; x++) {
544 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000545 r.fLeft = x * cw;
546 r.fRight = (x + 1) * cw;
547 if (x == XCLIP_N - 1) {
548 r.fRight = w;
549 }
reed@android.come522ca52009-11-23 20:10:41 +0000550 canvas->clipRect(r);
551 this->INHERITED::draw(canvas);
552 }
553 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000554
reed@android.com55e76b22009-11-23 21:46:47 +0000555 SkBitmap diff;
556 if (bitmap_diff(canvas, orig, &diff)) {
557 }
reed@android.come522ca52009-11-23 20:10:41 +0000558 } else {
559 this->INHERITED::draw(canvas);
560 }
Scroggo8ac0d542011-06-21 14:44:57 +0000561 if (fShowZoomer && fCanvasType != kGPU_CanvasType && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +0000562 // In the GPU case, INHERITED::draw calls beforeChildren, which
563 // creates an SkGpuCanvas. All further draw calls are directed
564 // at that canvas, which is deleted in afterChildren (which is
565 // also called by draw), so we cannot show the zoomer here.
566 // Instead, we call it inside afterChildren.
567 showZoomer(canvas);
568 }
569}
570
571void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +0000572 int count = canvas->save();
573 canvas->resetMatrix();
574 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000575 int width = SkScalarRound(this->width());
576 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000577 if (fMouseX >= width) fMouseX = width - 1;
578 else if (fMouseX < 0) fMouseX = 0;
579 if (fMouseY >= height) fMouseY = height - 1;
580 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000581
Scroggo0f185c22011-03-24 18:35:50 +0000582 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000583 bitmap.lockPixels();
584
Scroggo0f185c22011-03-24 18:35:50 +0000585 // 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 +0000586 int zoomedWidth = (width >> 1) | 1;
587 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000588 SkIRect src;
589 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
590 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
591 SkRect dest;
592 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
593 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
594 SkPaint paint;
595 // Clear the background behind our zoomed in view
596 paint.setColor(SK_ColorWHITE);
597 canvas->drawRect(dest, paint);
598 canvas->drawBitmapRect(bitmap, &src, dest);
599 paint.setColor(SK_ColorBLACK);
600 paint.setStyle(SkPaint::kStroke_Style);
601 // Draw a border around the pixel in the middle
602 SkRect originalPixel;
603 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
604 SkMatrix matrix;
605 SkRect scalarSrc;
606 scalarSrc.set(src);
607 SkColor color = bitmap.getColor(fMouseX, fMouseY);
608 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
609 SkRect pixel;
610 matrix.mapRect(&pixel, originalPixel);
611 // TODO Perhaps measure the values and make the outline white if it's "dark"
612 if (color == SK_ColorBLACK) {
613 paint.setColor(SK_ColorWHITE);
614 }
615 canvas->drawRect(pixel, paint);
616 }
617 paint.setColor(SK_ColorBLACK);
618 // Draw a border around the destination rectangle
619 canvas->drawRect(dest, paint);
620 paint.setStyle(SkPaint::kStrokeAndFill_Style);
621 // Identify the pixel and its color on screen
622 paint.setTypeface(fTypeface);
623 paint.setAntiAlias(true);
624 SkScalar lineHeight = paint.getFontMetrics(NULL);
625 SkString string;
626 string.appendf("(%i, %i)", fMouseX, fMouseY);
627 SkScalar left = dest.fLeft + SkIntToScalar(3);
628 SkScalar i = SK_Scalar1;
629 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
630 // Alpha
631 i += SK_Scalar1;
632 string.reset();
633 string.appendf("A: %X", SkColorGetA(color));
634 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
635 // Red
636 i += SK_Scalar1;
637 string.reset();
638 string.appendf("R: %X", SkColorGetR(color));
639 paint.setColor(SK_ColorRED);
640 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
641 // Green
642 i += SK_Scalar1;
643 string.reset();
644 string.appendf("G: %X", SkColorGetG(color));
645 paint.setColor(SK_ColorGREEN);
646 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
647 // Blue
648 i += SK_Scalar1;
649 string.reset();
650 string.appendf("B: %X", SkColorGetB(color));
651 paint.setColor(SK_ColorBLUE);
652 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
653 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +0000654}
655
reed@android.com8a1c16f2008-12-17 15:59:43 +0000656void SampleWindow::onDraw(SkCanvas* canvas) {
657 if (fRepeatDrawing) {
658 this->inval(NULL);
659 }
660}
661
662#include "SkColorPriv.h"
663
664static void reverseRedAndBlue(const SkBitmap& bm) {
665 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
666 uint8_t* p = (uint8_t*)bm.getPixels();
667 uint8_t* stop = p + bm.getSize();
668 while (p < stop) {
669 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
670 unsigned scale = SkAlpha255To256(p[3]);
671 unsigned r = p[2];
672 unsigned b = p[0];
673 p[0] = SkAlphaMul(r, scale);
674 p[1] = SkAlphaMul(p[1], scale);
675 p[2] = SkAlphaMul(b, scale);
676 p += 4;
677 }
678}
679
Scroggo8ac0d542011-06-21 14:44:57 +0000680void SampleWindow::saveToPdf()
681{
682 fSaveToPdf = true;
683 this->inval(NULL);
684}
685
reed@android.com8a1c16f2008-12-17 15:59:43 +0000686SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000687 if (kGPU_CanvasType != fCanvasType) {
reed@android.com6efdc472008-12-19 18:24:35 +0000688#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000689 detachGL();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000690#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000691 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000692
Scroggo8ac0d542011-06-21 14:44:57 +0000693 if (fSaveToPdf) {
694 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
695 SkISize size = SkISize::Make(bmp.width(), bmp.height());
696 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
697 canvas->getTotalMatrix());
698 fPdfCanvas = new SkCanvas(pdfDevice);
699 pdfDevice->unref();
700 canvas = fPdfCanvas;
701 } else {
702 switch (fCanvasType) {
703 case kRaster_CanvasType:
reed@android.comf2b98d62010-12-20 18:26:13 +0000704 canvas = this->INHERITED::beforeChildren(canvas);
Scroggo8ac0d542011-06-21 14:44:57 +0000705 break;
706 case kPicture_CanvasType:
707 fPicture = new SkPicture;
708 canvas = fPicture->beginRecording(9999, 9999);
709 break;
710 case kGPU_CanvasType: {
711 if (make3DReady()) {
712 SkDevice* device = canvas->getDevice();
713 const SkBitmap& bitmap = device->accessBitmap(true);
714
715 GrRenderTarget* renderTarget;
716
717 GrPlatformSurfaceDesc desc;
718 desc.reset();
719 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
720 desc.fWidth = bitmap.width();
721 desc.fHeight = bitmap.height();
722 desc.fConfig = kRGBA_8888_GrPixelConfig;
723 desc.fStencilBits = 8;
724 GrGLint buffer;
725 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer);
726 desc.fPlatformRenderTarget = buffer;
727
728 renderTarget = static_cast<GrRenderTarget*>(
729 fGrContext->createPlatformSurface(desc));
730 fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
731 renderTarget->unref();
732
733 device = new SkGpuDevice(fGrContext, renderTarget);
734 fGpuCanvas->setDevice(device)->unref();
735
736 fGpuCanvas->concat(canvas->getTotalMatrix());
737 canvas = fGpuCanvas;
738
739 } else {
740 canvas = this->INHERITED::beforeChildren(canvas);
741 }
742 break;
reed@android.comf2b98d62010-12-20 18:26:13 +0000743 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000744 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000745 }
746
747 if (fUseClip) {
748 canvas->drawColor(0xFFFF88FF);
749 canvas->clipPath(fClipPath);
750 }
751
752 return canvas;
753}
754
755static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
756 const SkRegion& rgn) {
757 SkCanvas canvas(bm);
758 SkRegion inval(rgn);
759
760 inval.translate(r.fLeft, r.fTop);
761 canvas.clipRegion(inval);
762 canvas.drawColor(0xFFFF8080);
763}
yangsu@google.com501775e2011-06-24 16:04:50 +0000764#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +0000765void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +0000766 if (fSaveToPdf) {
767 fSaveToPdf = false;
768 if (fShowZoomer) {
769 showZoomer(fPdfCanvas);
770 }
771 SkString name;
772 name.printf("%s.pdf", this->getTitle());
773 SkPDFDocument doc;
774 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
775 doc.appendPage(device);
776#ifdef ANDROID
777 name.prepend("/sdcard/");
778#endif
yangsu@google.com501775e2011-06-24 16:04:50 +0000779
780#ifdef SK_BUILD_FOR_IOS
781 SkDynamicMemoryWStream mstream;
782 doc.emitPDF(&mstream);
783 fPDFData = SkData::NewWithCopy(mstream.getStream(),mstream.getOffset());
784#endif
Scroggo8ac0d542011-06-21 14:44:57 +0000785 SkFILEWStream stream(name.c_str());
786 if (stream.isValid()) {
787 doc.emitPDF(&stream);
788 const char* desc = "File saved from Skia SampleApp";
789 this->onPDFSaved(this->getTitle(), desc, name.c_str());
790 }
yangsu@google.com501775e2011-06-24 16:04:50 +0000791
Scroggo8ac0d542011-06-21 14:44:57 +0000792 delete fPdfCanvas;
793 fPdfCanvas = NULL;
794
795 // We took over the draw calls in order to create the PDF, so we need
796 // to redraw.
797 this->inval(NULL);
798 return;
799 }
800
reed@android.comf2b98d62010-12-20 18:26:13 +0000801 if (fRequestGrabImage) {
802 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000803
reed@android.comf2b98d62010-12-20 18:26:13 +0000804 SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
805 SkDevice* device = canvas->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000806 SkBitmap bmp;
807 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000808 static int gSampleGrabCounter;
809 SkString name;
810 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000811 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000812 SkImageEncoder::kPNG_Type, 100);
813 }
814 }
815
reed@android.com8a1c16f2008-12-17 15:59:43 +0000816 switch (fCanvasType) {
817 case kRaster_CanvasType:
818 break;
819 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000820 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000821 SkPicture* pict = new SkPicture(*fPicture);
822 fPicture->unref();
823 orig->drawPicture(*pict);
824 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000825 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000826 SkDynamicMemoryWStream ostream;
827 fPicture->serialize(&ostream);
828 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000829
reed@android.com8a1c16f2008-12-17 15:59:43 +0000830 SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
831 SkPicture pict(&istream);
832 orig->drawPicture(pict);
833 } else {
834 fPicture->draw(orig);
835 fPicture->unref();
836 }
837 fPicture = NULL;
838 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000839#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000840 case kGPU_CanvasType:
Scroggoaed68d92011-06-08 14:26:00 +0000841 if (fShowZoomer && fGpuCanvas) {
Scroggo3272ba82011-05-25 20:50:42 +0000842 this->showZoomer(fGpuCanvas);
843 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000844 delete fGpuCanvas;
845 fGpuCanvas = NULL;
846 presentGL();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000847 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000848#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000849 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000850
reed@google.com17d7aec2011-04-25 14:31:44 +0000851 // Do this after presentGL and other finishing, rather than in afterChild
852 if (fMeasureFPS && fMeasureFPS_Time) {
853 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
854 this->updateTitle();
Scroggo62b65b02011-06-21 16:01:26 +0000855 this->postInvalDelay();
reed@google.com17d7aec2011-04-25 14:31:44 +0000856 }
857
858 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000859 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000860 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
861 int dx = fScrollTestX * 7;
862 int dy = fScrollTestY * 7;
863 SkIRect r;
864 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000865
reed@android.com8a1c16f2008-12-17 15:59:43 +0000866 r.set(50, 50, 50+100, 50+100);
867 bm.scrollRect(&r, dx, dy, &inval);
868 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000869 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000870}
871
reed@android.com6c5f6f22009-08-14 16:08:38 +0000872void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
873 if (fScale) {
874 SkScalar scale = SK_Scalar1 * 7 / 10;
875 SkScalar cx = this->width() / 2;
876 SkScalar cy = this->height() / 2;
877 canvas->translate(cx, cy);
878 canvas->scale(scale, scale);
879 canvas->translate(-cx, -cy);
880 }
881 if (fRotate) {
882 SkScalar cx = this->width() / 2;
883 SkScalar cy = this->height() / 2;
884 canvas->translate(cx, cy);
885 canvas->rotate(SkIntToScalar(30));
886 canvas->translate(-cx, -cy);
887 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000888
reed@google.com09e3baa2011-05-18 12:04:31 +0000889 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
890 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000891
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000892 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000893 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
894 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
895 fMeasureFPS_Time = SkTime::GetMSecs();
896 }
897 } else {
898 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000899 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000900 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000901}
902
903void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000904 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000905}
906
reed@android.com8a1c16f2008-12-17 15:59:43 +0000907static SkBitmap::Config gConfigCycle[] = {
908 SkBitmap::kNo_Config, // none -> none
909 SkBitmap::kNo_Config, // a1 -> none
910 SkBitmap::kNo_Config, // a8 -> none
911 SkBitmap::kNo_Config, // index8 -> none
912 SkBitmap::kARGB_4444_Config, // 565 -> 4444
913 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
914 SkBitmap::kRGB_565_Config // 8888 -> 565
915};
916
917static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
918 return gConfigCycle[c];
919}
920
djsollen@google.come32b5832011-06-13 16:58:40 +0000921void SampleWindow::changeZoomLevel(float delta) {
922 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000923 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000924 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
925 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000926 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000927 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
928 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000929 } else {
930 fZoomScale = SK_Scalar1;
931 }
932
djsollen@google.come32b5832011-06-13 16:58:40 +0000933 this->updateTitle();
934
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000935 this->inval(NULL);
936}
937
Scroggo2c8208f2011-06-15 16:49:08 +0000938bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +0000939 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
Scroggo2c8208f2011-06-15 16:49:08 +0000940 this->loadView(fSamples[fCurrIndex]());
941 return true;
942}
943
reed@android.com8a1c16f2008-12-17 15:59:43 +0000944bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000945 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
946 this->loadView(fSamples[fCurrIndex]());
947 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000948}
949
yangsu@google.com501775e2011-06-24 16:04:50 +0000950bool SampleWindow::goToSample(int i) {
951 fCurrIndex = (i) % fSamples.count();
952 this->loadView(fSamples[fCurrIndex]());
953 return true;
954}
955
956SkString SampleWindow::getSampleTitle(int i) {
957 SkView* view = fSamples[i]();
958 SkString title;
959 SkEvent evt(gTitleEvtName);
960 if (view->doQuery(&evt)) {
961 title.set(evt.findString(gTitleEvtName));
962 }
963 view->unref();
964 return title;
965}
966
967int SampleWindow::sampleCount() {
968 return fSamples.count();
969}
970
Scroggo2c8208f2011-06-15 16:49:08 +0000971void SampleWindow::postAnimatingEvent() {
972 if (fAnimating) {
973 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
974 evt->post(this->getSinkID(), ANIMATING_DELAY);
975 }
976}
977
reed@android.com8a1c16f2008-12-17 15:59:43 +0000978bool SampleWindow::onEvent(const SkEvent& evt) {
979 if (evt.isType(ANIMATING_EVENTTYPE)) {
980 if (fAnimating) {
981 this->nextSample();
982 this->postAnimatingEvent();
983 }
984 return true;
985 }
reed@android.com34245c72009-11-03 04:00:48 +0000986 if (evt.isType("set-curr-index")) {
987 fCurrIndex = evt.getFast32() % fSamples.count();
988 this->loadView(fSamples[fCurrIndex]());
989 return true;
990 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000991 if (isInvalEvent(evt)) {
992 this->inval(NULL);
993 return true;
994 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000995 return this->INHERITED::onEvent(evt);
996}
997
reed@android.comf2b98d62010-12-20 18:26:13 +0000998bool SampleWindow::onQuery(SkEvent* query) {
999 if (query->isType("get-slide-count")) {
1000 query->setFast32(fSamples.count());
1001 return true;
1002 }
1003 if (query->isType("get-slide-title")) {
1004 SkView* view = fSamples[query->getFast32()]();
1005 SkEvent evt(gTitleEvtName);
1006 if (view->doQuery(&evt)) {
1007 query->setString("title", evt.findString(gTitleEvtName));
1008 }
1009 SkSafeUnref(view);
1010 return true;
1011 }
1012 if (query->isType("use-fast-text")) {
1013 SkEvent evt(gFastTextEvtName);
1014 return curr_view(this)->doQuery(&evt);
1015 }
1016 return this->INHERITED::onQuery(query);
1017}
1018
reed@android.com0ae6b242008-12-23 16:49:54 +00001019static void cleanup_for_filename(SkString* name) {
1020 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001021 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001022 switch (str[i]) {
1023 case ':': str[i] = '-'; break;
1024 case '/': str[i] = '-'; break;
1025 case ' ': str[i] = '_'; break;
1026 default: break;
1027 }
1028 }
1029}
reed@android.com8a1c16f2008-12-17 15:59:43 +00001030
1031bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001032 {
1033 SkView* view = curr_view(this);
1034 if (view) {
1035 SkEvent evt(gCharEvtName);
1036 evt.setFast32(uni);
1037 if (view->doQuery(&evt)) {
1038 return true;
1039 }
1040 }
1041 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001042
reed@android.com8a1c16f2008-12-17 15:59:43 +00001043 int dx = 0xFF;
1044 int dy = 0xFF;
1045
1046 switch (uni) {
1047 case '5': dx = 0; dy = 0; break;
1048 case '8': dx = 0; dy = -1; break;
1049 case '6': dx = 1; dy = 0; break;
1050 case '2': dx = 0; dy = 1; break;
1051 case '4': dx = -1; dy = 0; break;
1052 case '7': dx = -1; dy = -1; break;
1053 case '9': dx = 1; dy = -1; break;
1054 case '3': dx = 1; dy = 1; break;
1055 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001056
reed@android.com8a1c16f2008-12-17 15:59:43 +00001057 default:
1058 break;
1059 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001060
reed@android.com8a1c16f2008-12-17 15:59:43 +00001061 if (0xFF != dx && 0xFF != dy) {
1062 if ((dx | dy) == 0) {
1063 fScrollTestX = fScrollTestY = 0;
1064 } else {
1065 fScrollTestX += dx;
1066 fScrollTestY += dy;
1067 }
1068 this->inval(NULL);
1069 return true;
1070 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001071
reed@android.com0ae6b242008-12-23 16:49:54 +00001072 switch (uni) {
1073 case 'a':
Scroggo2c8208f2011-06-15 16:49:08 +00001074 this->toggleSlideshow();
reed@android.com0ae6b242008-12-23 16:49:54 +00001075 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001076 case 'b':
1077 fAAState = cycle_tristate(fAAState);
1078 this->updateTitle();
1079 this->inval(NULL);
1080 break;
1081 case 'c':
1082 fUseClip = !fUseClip;
1083 this->inval(NULL);
1084 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001085 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001086 case 'd':
1087 SkGraphics::SetFontCacheUsed(0);
1088 return true;
Scroggo8ac0d542011-06-21 14:44:57 +00001089 case 'e':
1090 this->saveToPdf();
1091 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001092 case 'f':
Scroggo2c8208f2011-06-15 16:49:08 +00001093 this->toggleFPS();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001094 break;
1095 case 'g':
1096 fRequestGrabImage = true;
1097 this->inval(NULL);
1098 break;
reed@google.com09e3baa2011-05-18 12:04:31 +00001099 case 'h':
1100 fHintingState = cycle_tristate(fHintingState);
1101 this->updateTitle();
1102 this->inval(NULL);
1103 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001104 case 'i':
1105 this->zoomIn();
1106 break;
1107 case 'l':
1108 fLCDState = cycle_tristate(fLCDState);
1109 this->updateTitle();
1110 this->inval(NULL);
1111 break;
reed@google.com176753a2011-05-17 15:32:04 +00001112 case 'n':
1113 fFilterState = cycle_tristate(fFilterState);
1114 this->updateTitle();
1115 this->inval(NULL);
1116 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001117 case 'o':
1118 this->zoomOut();
1119 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001120 case 'p':
1121 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001122 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001123 this->inval(NULL);
1124 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001125 case 'r':
1126 fRotate = !fRotate;
1127 this->inval(NULL);
1128 this->updateTitle();
1129 return true;
1130 case 's':
1131 fScale = !fScale;
1132 this->inval(NULL);
1133 this->updateTitle();
1134 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001135 case 'x':
1136 fFlipAxis ^= kFlipAxis_X;
1137 this->updateTitle();
1138 this->inval(NULL);
1139 break;
1140 case 'y':
1141 fFlipAxis ^= kFlipAxis_Y;
1142 this->updateTitle();
1143 this->inval(NULL);
1144 break;
scroggo08526c02011-03-22 14:03:21 +00001145 case 'z':
1146 this->toggleZoomer();
1147 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001148 default:
1149 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001150 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001151
reed@android.com8a1c16f2008-12-17 15:59:43 +00001152 return this->INHERITED::onHandleChar(uni);
1153}
1154
Scroggo2c8208f2011-06-15 16:49:08 +00001155void SampleWindow::toggleFPS() {
1156 fMeasureFPS = !fMeasureFPS;
1157 this->inval(NULL);
1158 this->updateTitle();
1159}
1160
1161void SampleWindow::toggleSlideshow() {
1162 fAnimating = !fAnimating;
1163 this->postAnimatingEvent();
1164 this->updateTitle();
1165}
1166
1167void SampleWindow::toggleRendering() {
1168 fCanvasType = cycle_canvastype(fCanvasType);
1169 this->updateTitle();
1170 this->inval(NULL);
1171}
1172
reed@android.com8a1c16f2008-12-17 15:59:43 +00001173#include "SkDumpCanvas.h"
1174
1175bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001176 {
1177 SkView* view = curr_view(this);
1178 if (view) {
1179 SkEvent evt(gKeyEvtName);
1180 evt.setFast32(key);
1181 if (view->doQuery(&evt)) {
1182 return true;
1183 }
1184 }
1185 }
1186
reed@android.com8a1c16f2008-12-17 15:59:43 +00001187 switch (key) {
1188 case kRight_SkKey:
1189 if (this->nextSample()) {
1190 return true;
1191 }
1192 break;
1193 case kLeft_SkKey:
Scroggo2c8208f2011-06-15 16:49:08 +00001194 toggleRendering();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001195 return true;
1196 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001197 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001198 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001199 } else {
1200 fNClip = !fNClip;
1201 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001202 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001203 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001204 return true;
1205 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001206 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001207 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001208 } else {
1209 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001210 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001211 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001212 return true;
1213 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001214 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001215 SkDebugfDumper dumper;
1216 SkDumpCanvas dc(&dumper);
1217 this->draw(&dc);
1218 } else {
1219 fRepeatDrawing = !fRepeatDrawing;
1220 if (fRepeatDrawing) {
1221 this->inval(NULL);
1222 }
1223 }
1224 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001225 case kBack_SkKey:
1226 this->loadView(NULL);
1227 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001228 default:
1229 break;
1230 }
1231 return this->INHERITED::onHandleKey(key);
1232}
1233
reed@google.com52f57e12011-03-16 12:10:02 +00001234///////////////////////////////////////////////////////////////////////////////
1235
1236static const char gGestureClickType[] = "GestureClickType";
1237
Scroggod3aed392011-06-22 13:26:56 +00001238bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
1239 void* owner) {
Scroggo0f185c22011-03-24 18:35:50 +00001240 if (Click::kMoved_State == state) {
1241 updatePointer(x, y);
1242 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001243 int w = SkScalarRound(this->width());
1244 int h = SkScalarRound(this->height());
1245
1246 // check for the resize-box
1247 if (w - x < 16 && h - y < 16) {
1248 return false; // let the OS handle the click
1249 } else {
Scroggod3aed392011-06-22 13:26:56 +00001250 return this->INHERITED::onDispatchClick(x, y, state, owner);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001251 }
1252}
1253
reed@google.com52f57e12011-03-16 12:10:02 +00001254class GestureClick : public SkView::Click {
1255public:
1256 GestureClick(SkView* target) : SkView::Click(target) {
1257 this->setType(gGestureClickType);
1258 }
1259
1260 static bool IsGesture(Click* click) {
1261 return click->isType(gGestureClickType);
1262 }
1263};
1264
1265SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1266 return new GestureClick(this);
1267}
1268
1269bool SampleWindow::onClick(Click* click) {
1270 if (GestureClick::IsGesture(click)) {
1271 float x = SkScalarToFloat(click->fCurr.fX);
1272 float y = SkScalarToFloat(click->fCurr.fY);
1273 switch (click->fState) {
1274 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00001275 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001276 break;
1277 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00001278 fGesture.touchMoved(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001279 this->inval(NULL);
1280 break;
1281 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00001282 fGesture.touchEnd(click->fOwner);
reed@google.com52f57e12011-03-16 12:10:02 +00001283 this->inval(NULL);
1284 break;
1285 }
1286 return true;
1287 }
1288 return false;
1289}
1290
1291///////////////////////////////////////////////////////////////////////////////
1292
reed@android.com8a1c16f2008-12-17 15:59:43 +00001293void SampleWindow::loadView(SkView* view) {
1294 SkView::F2BIter iter(this);
1295 SkView* prev = iter.next();
1296 if (prev) {
1297 prev->detachFromParent();
1298 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001299
reed@android.com34245c72009-11-03 04:00:48 +00001300 if (NULL == view) {
1301 view = create_overview(fSamples.count(), fSamples.begin());
1302 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001303 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001304 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001305 this->attachChildToFront(view)->unref();
1306 view->setSize(this->width(), this->height());
1307
1308 this->updateTitle();
1309}
1310
1311static const char* gConfigNames[] = {
1312 "unknown config",
1313 "A1",
1314 "A8",
1315 "Index8",
1316 "565",
1317 "4444",
1318 "8888"
1319};
1320
1321static const char* configToString(SkBitmap::Config c) {
1322 return gConfigNames[c];
1323}
1324
1325static const char* gCanvasTypePrefix[] = {
1326 "raster: ",
1327 "picture: ",
1328 "opengl: "
1329};
1330
reed@google.com569e0432011-04-05 13:07:03 +00001331static const char* trystate_str(SkTriState state,
1332 const char trueStr[], const char falseStr[]) {
1333 if (kTrue_SkTriState == state) {
1334 return trueStr;
1335 } else if (kFalse_SkTriState == state) {
1336 return falseStr;
1337 }
1338 return NULL;
1339}
1340
reed@android.com8a1c16f2008-12-17 15:59:43 +00001341void SampleWindow::updateTitle() {
1342 SkString title;
1343
1344 SkView::F2BIter iter(this);
1345 SkView* view = iter.next();
1346 SkEvent evt(gTitleEvtName);
1347 if (view->doQuery(&evt)) {
1348 title.set(evt.findString(gTitleEvtName));
1349 }
1350 if (title.size() == 0) {
1351 title.set("<unknown>");
1352 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001353
reed@android.com8a1c16f2008-12-17 15:59:43 +00001354 title.prepend(gCanvasTypePrefix[fCanvasType]);
1355
1356 title.prepend(" ");
1357 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001358
reed@android.com8a1c16f2008-12-17 15:59:43 +00001359 if (fAnimating) {
1360 title.prepend("<A> ");
1361 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001362 if (fScale) {
1363 title.prepend("<S> ");
1364 }
1365 if (fRotate) {
1366 title.prepend("<R> ");
1367 }
reed@android.come522ca52009-11-23 20:10:41 +00001368 if (fNClip) {
1369 title.prepend("<C> ");
1370 }
reed@google.com569e0432011-04-05 13:07:03 +00001371
1372 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1373 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001374 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001375 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1376 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001377
1378 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001379 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001380 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001381
1382 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001383 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001384 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001385 if (fUsePipe && SampleView::IsSampleView(view)) {
1386 title.prepend("<P> ");
1387 }
1388 if (SampleView::IsSampleView(view)) {
1389 title.prepend("! ");
1390 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001391
reed@android.com8a1c16f2008-12-17 15:59:43 +00001392 this->setTitle(title.c_str());
1393}
1394
1395void SampleWindow::onSizeChange() {
1396 this->INHERITED::onSizeChange();
1397
1398 SkView::F2BIter iter(this);
1399 SkView* view = iter.next();
1400 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001401
reed@android.com8a1c16f2008-12-17 15:59:43 +00001402 // rebuild our clippath
1403 {
1404 const SkScalar W = this->width();
1405 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001406
reed@android.com8a1c16f2008-12-17 15:59:43 +00001407 fClipPath.reset();
1408#if 0
1409 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1410 SkRect r;
1411 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1412 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1413 fClipPath.addRect(r);
1414 }
1415#else
1416 SkRect r;
1417 r.set(0, 0, W, H);
1418 fClipPath.addRect(r, SkPath::kCCW_Direction);
1419 r.set(W/4, H/4, W*3/4, H*3/4);
1420 fClipPath.addRect(r, SkPath::kCW_Direction);
1421#endif
1422 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001423
Scroggo2c8208f2011-06-15 16:49:08 +00001424 fZoomCenterX = SkScalarHalf(this->width());
1425 fZoomCenterY = SkScalarHalf(this->height());
1426
Scroggo3e7ff9f2011-06-16 15:31:26 +00001427#ifdef ANDROID
Scroggob4490c72011-06-17 13:53:05 +00001428 // FIXME: The first draw after a size change does not work on Android, so
1429 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00001430 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00001431#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001432 this->updateTitle(); // to refresh our config
1433}
1434
1435///////////////////////////////////////////////////////////////////////////////
1436
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001437static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001438static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001439static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001440
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001441bool SampleView::IsSampleView(SkView* view) {
1442 SkEvent evt(is_sample_view_tag);
1443 return view->doQuery(&evt);
1444}
1445
reed@google.comf2183392011-04-22 14:10:48 +00001446bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001447 SkEvent evt(repeat_count_tag);
1448 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001449 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001450}
1451
reed@google.com0faac1e2011-05-11 05:58:58 +00001452bool SampleView::SetUsePipe(SkView* view, bool pred) {
1453 SkEvent evt(set_use_pipe_tag);
1454 evt.setFast32(pred);
1455 return view->doEvent(evt);
1456}
1457
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001458bool SampleView::onEvent(const SkEvent& evt) {
1459 if (evt.isType(repeat_count_tag)) {
1460 fRepeatCount = evt.getFast32();
1461 return true;
1462 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001463 if (evt.isType(set_use_pipe_tag)) {
1464 fUsePipe = !!evt.getFast32();
1465 return true;
1466 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001467 return this->INHERITED::onEvent(evt);
1468}
1469
1470bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001471 if (evt->isType(is_sample_view_tag)) {
1472 return true;
1473 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001474 return this->INHERITED::onQuery(evt);
1475}
1476
reed@google.com68f456d2011-05-02 18:55:39 +00001477#ifdef TEST_GPIPE
1478 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001479
1480class SimplePC : public SkGPipeController {
1481public:
1482 SimplePC(SkCanvas* target);
1483 ~SimplePC();
1484
1485 virtual void* requestBlock(size_t minRequest, size_t* actual);
1486 virtual void notifyWritten(size_t bytes);
1487
1488private:
reed@google.com961ddb02011-05-05 14:03:48 +00001489 SkGPipeReader fReader;
1490 void* fBlock;
1491 size_t fBlockSize;
1492 size_t fBytesWritten;
1493 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001494 SkGPipeReader::Status fStatus;
1495
1496 size_t fTotalWritten;
1497};
1498
1499SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1500 fBlock = NULL;
1501 fBlockSize = fBytesWritten = 0;
1502 fStatus = SkGPipeReader::kDone_Status;
1503 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001504 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001505}
1506
1507SimplePC::~SimplePC() {
1508// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1509 sk_free(fBlock);
1510
reed@google.com0faac1e2011-05-11 05:58:58 +00001511 if (fTotalWritten) {
1512 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1513 fAtomsWritten, fStatus);
1514 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001515}
1516
1517void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1518 sk_free(fBlock);
1519
1520 fBlockSize = minRequest * 4;
1521 fBlock = sk_malloc_throw(fBlockSize);
1522 fBytesWritten = 0;
1523 *actual = fBlockSize;
1524 return fBlock;
1525}
1526
1527void SimplePC::notifyWritten(size_t bytes) {
1528 SkASSERT(fBytesWritten + bytes <= fBlockSize);
yangsu@google.com1f394212011-06-01 18:03:34 +00001529
1530#ifdef PIPE_FILE
1531 //File is open in append mode
1532 FILE* f = fopen(FILE_PATH, "ab");
1533 SkASSERT(f != NULL);
1534 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
1535 fclose(f);
1536#endif
1537
reed@google.com64e3eb22011-05-04 14:32:04 +00001538 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1539 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1540 fBytesWritten += bytes;
1541 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001542
1543 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001544}
1545
reed@google.com68f456d2011-05-02 18:55:39 +00001546#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001547
reed@google.com64e3eb22011-05-04 14:32:04 +00001548
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001549void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001550#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001551 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001552 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001553 if (fUsePipe) {
reed@google.comdde09562011-05-23 12:21:05 +00001554 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
1555// flags = 0;
1556 canvas = writer.startRecording(&controller, flags);
reed@google.com0faac1e2011-05-11 05:58:58 +00001557 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001558#endif
1559
reed@google.com81e3d7f2011-06-01 12:42:36 +00001560 this->onDrawBackground(canvas);
1561
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001562 for (int i = 0; i < fRepeatCount; i++) {
1563 SkAutoCanvasRestore acr(canvas, true);
1564 this->onDrawContent(canvas);
1565 }
1566}
1567
1568void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001569 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001570}
1571
1572///////////////////////////////////////////////////////////////////////////////
1573
reed@android.comf2b98d62010-12-20 18:26:13 +00001574template <typename T> void SkTBSort(T array[], int count) {
1575 for (int i = 1; i < count - 1; i++) {
1576 bool didSwap = false;
1577 for (int j = count - 1; j > i; --j) {
1578 if (array[j] < array[j-1]) {
1579 T tmp(array[j-1]);
1580 array[j-1] = array[j];
1581 array[j] = tmp;
1582 didSwap = true;
1583 }
1584 }
1585 if (!didSwap) {
1586 break;
1587 }
1588 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001589
reed@android.comf2b98d62010-12-20 18:26:13 +00001590 for (int k = 0; k < count - 1; k++) {
1591 SkASSERT(!(array[k+1] < array[k]));
1592 }
1593}
1594
1595#include "SkRandom.h"
1596
1597static void rand_rect(SkIRect* rect, SkRandom& rand) {
1598 int bits = 8;
1599 int shift = 32 - bits;
1600 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1601 rand.nextU() >> shift, rand.nextU() >> shift);
1602 rect->sort();
1603}
1604
1605static void dumpRect(const SkIRect& r) {
1606 SkDebugf(" { %d, %d, %d, %d },\n",
1607 r.fLeft, r.fTop,
1608 r.fRight, r.fBottom);
1609}
1610
1611static void test_rects(const SkIRect rect[], int count) {
1612 SkRegion rgn0, rgn1;
1613
1614 for (int i = 0; i < count; i++) {
1615 rgn0.op(rect[i], SkRegion::kUnion_Op);
1616 // dumpRect(rect[i]);
1617 }
1618 rgn1.setRects(rect, count);
1619
1620 if (rgn0 != rgn1) {
1621 SkDebugf("\n");
1622 for (int i = 0; i < count; i++) {
1623 dumpRect(rect[i]);
1624 }
1625 SkDebugf("\n");
1626 }
1627}
1628
1629static void test() {
1630 size_t i;
1631
1632 const SkIRect r0[] = {
1633 { 0, 0, 1, 1 },
1634 { 2, 2, 3, 3 },
1635 };
1636 const SkIRect r1[] = {
1637 { 0, 0, 1, 3 },
1638 { 1, 1, 2, 2 },
1639 { 2, 0, 3, 3 },
1640 };
1641 const SkIRect r2[] = {
1642 { 0, 0, 1, 2 },
1643 { 2, 1, 3, 3 },
1644 { 4, 0, 5, 1 },
1645 { 6, 0, 7, 4 },
1646 };
1647
1648 static const struct {
1649 const SkIRect* fRects;
1650 int fCount;
1651 } gRecs[] = {
1652 { r0, SK_ARRAY_COUNT(r0) },
1653 { r1, SK_ARRAY_COUNT(r1) },
1654 { r2, SK_ARRAY_COUNT(r2) },
1655 };
1656
1657 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1658 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1659 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001660
reed@android.comf2b98d62010-12-20 18:26:13 +00001661 SkRandom rand;
1662 for (i = 0; i < 10000; i++) {
1663 SkRegion rgn0, rgn1;
1664
1665 const int N = 8;
1666 SkIRect rect[N];
1667 for (int j = 0; j < N; j++) {
1668 rand_rect(&rect[j], rand);
1669 }
1670 test_rects(rect, N);
1671 }
1672}
1673
reed@android.com8a1c16f2008-12-17 15:59:43 +00001674SkOSWindow* create_sk_window(void* hwnd) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001675// test();
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001676 return new SampleWindow(hwnd);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001677}
1678
1679void get_preferred_size(int* x, int* y, int* width, int* height) {
1680 *x = 10;
1681 *y = 50;
1682 *width = 640;
1683 *height = 480;
1684}
1685
1686void application_init() {
1687// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001688#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001689 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001690#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001691 SkGraphics::Init();
1692 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001693}
1694
1695void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001696 SkEvent::Term();
1697 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001698}