blob: 9c2b7d13446758332fa3c2c8e8b53414d4570206 [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 }
413
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000414#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +0000415 testpdf();
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000416#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000417}
418
419SampleWindow::~SampleWindow() {
420 delete fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000421 delete fGpuCanvas;
Scroggo8ac0d542011-06-21 14:44:57 +0000422 delete fPdfCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000423 if (NULL != fGrContext) {
424 fGrContext->unref();
425 }
Scroggo0f185c22011-03-24 18:35:50 +0000426 fTypeface->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427}
428
reed@android.com55e76b22009-11-23 21:46:47 +0000429static SkBitmap capture_bitmap(SkCanvas* canvas) {
430 SkBitmap bm;
431 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
432 src.copyTo(&bm, src.config());
433 return bm;
434}
435
436static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
437 SkBitmap* diff) {
438 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000439
reed@android.com55e76b22009-11-23 21:46:47 +0000440 SkAutoLockPixels alp0(src);
441 SkAutoLockPixels alp1(orig);
442 for (int y = 0; y < src.height(); y++) {
443 const void* srcP = src.getAddr(0, y);
444 const void* origP = orig.getAddr(0, y);
445 size_t bytes = src.width() * src.bytesPerPixel();
446 if (memcmp(srcP, origP, bytes)) {
447 SkDebugf("---------- difference on line %d\n", y);
448 return true;
449 }
450 }
451 return false;
452}
453
Scroggo0f185c22011-03-24 18:35:50 +0000454static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
455{
456 SkColor desiredColor = paint.getColor();
457 paint.setColor(SK_ColorWHITE);
458 const char* c_str = string.c_str();
459 size_t size = string.size();
460 SkRect bounds;
461 paint.measureText(c_str, size, &bounds);
462 bounds.offset(left, top);
463 SkScalar inset = SkIntToScalar(-2);
464 bounds.inset(inset, inset);
465 canvas->drawRect(bounds, paint);
466 if (desiredColor != SK_ColorBLACK) {
467 paint.setColor(SK_ColorBLACK);
468 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
469 }
470 paint.setColor(desiredColor);
471 canvas->drawText(c_str, size, left, top, paint);
472}
473
reed@android.com44177402009-11-23 21:07:51 +0000474#define XCLIP_N 8
475#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000476
477void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000478 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000479 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000480 gAnimTime = SkTime::GetMSecs();
481
Scroggo2c8208f2011-06-15 16:49:08 +0000482 SkScalar cx = fZoomCenterX;
483 SkScalar cy = fZoomCenterY;
reed@google.com569e0432011-04-05 13:07:03 +0000484
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000485 if (fZoomLevel) {
486 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000487 SkPoint center;
488 m = canvas->getTotalMatrix();//.invert(&m);
489 m.mapXY(cx, cy, &center);
490 cx = center.fX;
491 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000492
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000493 m.setTranslate(-cx, -cy);
494 m.postScale(fZoomScale, fZoomScale);
495 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000496
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000497 canvas->concat(m);
498 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000499
reed@google.com569e0432011-04-05 13:07:03 +0000500 if (fFlipAxis) {
501 SkMatrix m;
502 m.setTranslate(cx, cy);
503 if (fFlipAxis & kFlipAxis_X) {
504 m.preScale(-SK_Scalar1, SK_Scalar1);
505 }
506 if (fFlipAxis & kFlipAxis_Y) {
507 m.preScale(SK_Scalar1, -SK_Scalar1);
508 }
509 m.preTranslate(-cx, -cy);
510 canvas->concat(m);
511 }
512
reed@google.com52f57e12011-03-16 12:10:02 +0000513 // Apply any gesture matrix
514 if (true) {
515 const SkMatrix& localM = fGesture.localM();
516 if (localM.getType() & SkMatrix::kScale_Mask) {
517 canvas->setExternalMatrix(&localM);
518 }
519 canvas->concat(localM);
520 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000521
reed@google.com52f57e12011-03-16 12:10:02 +0000522 if (fGesture.isActive()) {
523 this->inval(NULL);
524 }
525 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000526
reed@android.come522ca52009-11-23 20:10:41 +0000527 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000528 this->INHERITED::draw(canvas);
529 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000530
531 const SkScalar w = this->width();
532 const SkScalar h = this->height();
533 const SkScalar cw = w / XCLIP_N;
534 const SkScalar ch = h / YCLIP_N;
535 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000536 SkRect r;
537 r.fTop = y * ch;
538 r.fBottom = (y + 1) * ch;
539 if (y == YCLIP_N - 1) {
540 r.fBottom = h;
541 }
reed@android.come522ca52009-11-23 20:10:41 +0000542 for (int x = 0; x < XCLIP_N; x++) {
543 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000544 r.fLeft = x * cw;
545 r.fRight = (x + 1) * cw;
546 if (x == XCLIP_N - 1) {
547 r.fRight = w;
548 }
reed@android.come522ca52009-11-23 20:10:41 +0000549 canvas->clipRect(r);
550 this->INHERITED::draw(canvas);
551 }
552 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000553
reed@android.com55e76b22009-11-23 21:46:47 +0000554 SkBitmap diff;
555 if (bitmap_diff(canvas, orig, &diff)) {
556 }
reed@android.come522ca52009-11-23 20:10:41 +0000557 } else {
558 this->INHERITED::draw(canvas);
559 }
Scroggo8ac0d542011-06-21 14:44:57 +0000560 if (fShowZoomer && fCanvasType != kGPU_CanvasType && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +0000561 // In the GPU case, INHERITED::draw calls beforeChildren, which
562 // creates an SkGpuCanvas. All further draw calls are directed
563 // at that canvas, which is deleted in afterChildren (which is
564 // also called by draw), so we cannot show the zoomer here.
565 // Instead, we call it inside afterChildren.
566 showZoomer(canvas);
567 }
568}
569
570void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +0000571 int count = canvas->save();
572 canvas->resetMatrix();
573 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000574 int width = SkScalarRound(this->width());
575 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000576 if (fMouseX >= width) fMouseX = width - 1;
577 else if (fMouseX < 0) fMouseX = 0;
578 if (fMouseY >= height) fMouseY = height - 1;
579 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000580
Scroggo0f185c22011-03-24 18:35:50 +0000581 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000582 bitmap.lockPixels();
583
Scroggo0f185c22011-03-24 18:35:50 +0000584 // 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 +0000585 int zoomedWidth = (width >> 1) | 1;
586 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000587 SkIRect src;
588 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
589 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
590 SkRect dest;
591 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
592 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
593 SkPaint paint;
594 // Clear the background behind our zoomed in view
595 paint.setColor(SK_ColorWHITE);
596 canvas->drawRect(dest, paint);
597 canvas->drawBitmapRect(bitmap, &src, dest);
598 paint.setColor(SK_ColorBLACK);
599 paint.setStyle(SkPaint::kStroke_Style);
600 // Draw a border around the pixel in the middle
601 SkRect originalPixel;
602 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
603 SkMatrix matrix;
604 SkRect scalarSrc;
605 scalarSrc.set(src);
606 SkColor color = bitmap.getColor(fMouseX, fMouseY);
607 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
608 SkRect pixel;
609 matrix.mapRect(&pixel, originalPixel);
610 // TODO Perhaps measure the values and make the outline white if it's "dark"
611 if (color == SK_ColorBLACK) {
612 paint.setColor(SK_ColorWHITE);
613 }
614 canvas->drawRect(pixel, paint);
615 }
616 paint.setColor(SK_ColorBLACK);
617 // Draw a border around the destination rectangle
618 canvas->drawRect(dest, paint);
619 paint.setStyle(SkPaint::kStrokeAndFill_Style);
620 // Identify the pixel and its color on screen
621 paint.setTypeface(fTypeface);
622 paint.setAntiAlias(true);
623 SkScalar lineHeight = paint.getFontMetrics(NULL);
624 SkString string;
625 string.appendf("(%i, %i)", fMouseX, fMouseY);
626 SkScalar left = dest.fLeft + SkIntToScalar(3);
627 SkScalar i = SK_Scalar1;
628 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
629 // Alpha
630 i += SK_Scalar1;
631 string.reset();
632 string.appendf("A: %X", SkColorGetA(color));
633 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
634 // Red
635 i += SK_Scalar1;
636 string.reset();
637 string.appendf("R: %X", SkColorGetR(color));
638 paint.setColor(SK_ColorRED);
639 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
640 // Green
641 i += SK_Scalar1;
642 string.reset();
643 string.appendf("G: %X", SkColorGetG(color));
644 paint.setColor(SK_ColorGREEN);
645 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
646 // Blue
647 i += SK_Scalar1;
648 string.reset();
649 string.appendf("B: %X", SkColorGetB(color));
650 paint.setColor(SK_ColorBLUE);
651 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
652 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +0000653}
654
reed@android.com8a1c16f2008-12-17 15:59:43 +0000655void SampleWindow::onDraw(SkCanvas* canvas) {
656 if (fRepeatDrawing) {
657 this->inval(NULL);
658 }
659}
660
661#include "SkColorPriv.h"
662
663static void reverseRedAndBlue(const SkBitmap& bm) {
664 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
665 uint8_t* p = (uint8_t*)bm.getPixels();
666 uint8_t* stop = p + bm.getSize();
667 while (p < stop) {
668 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
669 unsigned scale = SkAlpha255To256(p[3]);
670 unsigned r = p[2];
671 unsigned b = p[0];
672 p[0] = SkAlphaMul(r, scale);
673 p[1] = SkAlphaMul(p[1], scale);
674 p[2] = SkAlphaMul(b, scale);
675 p += 4;
676 }
677}
678
Scroggo8ac0d542011-06-21 14:44:57 +0000679void SampleWindow::saveToPdf()
680{
681 fSaveToPdf = true;
682 this->inval(NULL);
683}
684
reed@android.com8a1c16f2008-12-17 15:59:43 +0000685SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000686 if (kGPU_CanvasType != fCanvasType) {
reed@android.com6efdc472008-12-19 18:24:35 +0000687#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000688 detachGL();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000689#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000690 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000691
Scroggo8ac0d542011-06-21 14:44:57 +0000692 if (fSaveToPdf) {
693 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
694 SkISize size = SkISize::Make(bmp.width(), bmp.height());
695 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
696 canvas->getTotalMatrix());
697 fPdfCanvas = new SkCanvas(pdfDevice);
698 pdfDevice->unref();
699 canvas = fPdfCanvas;
700 } else {
701 switch (fCanvasType) {
702 case kRaster_CanvasType:
reed@android.comf2b98d62010-12-20 18:26:13 +0000703 canvas = this->INHERITED::beforeChildren(canvas);
Scroggo8ac0d542011-06-21 14:44:57 +0000704 break;
705 case kPicture_CanvasType:
706 fPicture = new SkPicture;
707 canvas = fPicture->beginRecording(9999, 9999);
708 break;
709 case kGPU_CanvasType: {
710 if (make3DReady()) {
711 SkDevice* device = canvas->getDevice();
712 const SkBitmap& bitmap = device->accessBitmap(true);
713
714 GrRenderTarget* renderTarget;
715
716 GrPlatformSurfaceDesc desc;
717 desc.reset();
718 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
719 desc.fWidth = bitmap.width();
720 desc.fHeight = bitmap.height();
721 desc.fConfig = kRGBA_8888_GrPixelConfig;
722 desc.fStencilBits = 8;
723 GrGLint buffer;
724 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer);
725 desc.fPlatformRenderTarget = buffer;
726
727 renderTarget = static_cast<GrRenderTarget*>(
728 fGrContext->createPlatformSurface(desc));
729 fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
730 renderTarget->unref();
731
732 device = new SkGpuDevice(fGrContext, renderTarget);
733 fGpuCanvas->setDevice(device)->unref();
734
735 fGpuCanvas->concat(canvas->getTotalMatrix());
736 canvas = fGpuCanvas;
737
738 } else {
739 canvas = this->INHERITED::beforeChildren(canvas);
740 }
741 break;
reed@android.comf2b98d62010-12-20 18:26:13 +0000742 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000743 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000744 }
745
746 if (fUseClip) {
747 canvas->drawColor(0xFFFF88FF);
748 canvas->clipPath(fClipPath);
749 }
750
751 return canvas;
752}
753
754static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
755 const SkRegion& rgn) {
756 SkCanvas canvas(bm);
757 SkRegion inval(rgn);
758
759 inval.translate(r.fLeft, r.fTop);
760 canvas.clipRegion(inval);
761 canvas.drawColor(0xFFFF8080);
762}
763
764void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +0000765 if (fSaveToPdf) {
766 fSaveToPdf = false;
767 if (fShowZoomer) {
768 showZoomer(fPdfCanvas);
769 }
770 SkString name;
771 name.printf("%s.pdf", this->getTitle());
772 SkPDFDocument doc;
773 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
774 doc.appendPage(device);
775#ifdef ANDROID
776 name.prepend("/sdcard/");
777#endif
778 SkFILEWStream stream(name.c_str());
779 if (stream.isValid()) {
780 doc.emitPDF(&stream);
781 const char* desc = "File saved from Skia SampleApp";
782 this->onPDFSaved(this->getTitle(), desc, name.c_str());
783 }
784 delete fPdfCanvas;
785 fPdfCanvas = NULL;
786
787 // We took over the draw calls in order to create the PDF, so we need
788 // to redraw.
789 this->inval(NULL);
790 return;
791 }
792
reed@android.comf2b98d62010-12-20 18:26:13 +0000793 if (fRequestGrabImage) {
794 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000795
reed@android.comf2b98d62010-12-20 18:26:13 +0000796 SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
797 SkDevice* device = canvas->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000798 SkBitmap bmp;
799 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000800 static int gSampleGrabCounter;
801 SkString name;
802 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000803 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000804 SkImageEncoder::kPNG_Type, 100);
805 }
806 }
807
reed@android.com8a1c16f2008-12-17 15:59:43 +0000808 switch (fCanvasType) {
809 case kRaster_CanvasType:
810 break;
811 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000812 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000813 SkPicture* pict = new SkPicture(*fPicture);
814 fPicture->unref();
815 orig->drawPicture(*pict);
816 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000817 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000818 SkDynamicMemoryWStream ostream;
819 fPicture->serialize(&ostream);
820 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000821
reed@android.com8a1c16f2008-12-17 15:59:43 +0000822 SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
823 SkPicture pict(&istream);
824 orig->drawPicture(pict);
825 } else {
826 fPicture->draw(orig);
827 fPicture->unref();
828 }
829 fPicture = NULL;
830 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000831#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000832 case kGPU_CanvasType:
Scroggoaed68d92011-06-08 14:26:00 +0000833 if (fShowZoomer && fGpuCanvas) {
Scroggo3272ba82011-05-25 20:50:42 +0000834 this->showZoomer(fGpuCanvas);
835 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000836 delete fGpuCanvas;
837 fGpuCanvas = NULL;
838 presentGL();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000839 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000840#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000841 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000842
reed@google.com17d7aec2011-04-25 14:31:44 +0000843 // Do this after presentGL and other finishing, rather than in afterChild
844 if (fMeasureFPS && fMeasureFPS_Time) {
845 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
846 this->updateTitle();
Scroggo62b65b02011-06-21 16:01:26 +0000847 this->postInvalDelay();
reed@google.com17d7aec2011-04-25 14:31:44 +0000848 }
849
850 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000851 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000852 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
853 int dx = fScrollTestX * 7;
854 int dy = fScrollTestY * 7;
855 SkIRect r;
856 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000857
reed@android.com8a1c16f2008-12-17 15:59:43 +0000858 r.set(50, 50, 50+100, 50+100);
859 bm.scrollRect(&r, dx, dy, &inval);
860 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000861 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000862}
863
reed@android.com6c5f6f22009-08-14 16:08:38 +0000864void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
865 if (fScale) {
866 SkScalar scale = SK_Scalar1 * 7 / 10;
867 SkScalar cx = this->width() / 2;
868 SkScalar cy = this->height() / 2;
869 canvas->translate(cx, cy);
870 canvas->scale(scale, scale);
871 canvas->translate(-cx, -cy);
872 }
873 if (fRotate) {
874 SkScalar cx = this->width() / 2;
875 SkScalar cy = this->height() / 2;
876 canvas->translate(cx, cy);
877 canvas->rotate(SkIntToScalar(30));
878 canvas->translate(-cx, -cy);
879 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000880
reed@google.com09e3baa2011-05-18 12:04:31 +0000881 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
882 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000883
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000884 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000885 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
886 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
887 fMeasureFPS_Time = SkTime::GetMSecs();
888 }
889 } else {
890 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000891 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000892 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000893}
894
895void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000896 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000897}
898
reed@android.com8a1c16f2008-12-17 15:59:43 +0000899static SkBitmap::Config gConfigCycle[] = {
900 SkBitmap::kNo_Config, // none -> none
901 SkBitmap::kNo_Config, // a1 -> none
902 SkBitmap::kNo_Config, // a8 -> none
903 SkBitmap::kNo_Config, // index8 -> none
904 SkBitmap::kARGB_4444_Config, // 565 -> 4444
905 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
906 SkBitmap::kRGB_565_Config // 8888 -> 565
907};
908
909static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
910 return gConfigCycle[c];
911}
912
djsollen@google.come32b5832011-06-13 16:58:40 +0000913void SampleWindow::changeZoomLevel(float delta) {
914 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000915 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000916 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
917 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000918 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000919 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
920 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000921 } else {
922 fZoomScale = SK_Scalar1;
923 }
924
djsollen@google.come32b5832011-06-13 16:58:40 +0000925 this->updateTitle();
926
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000927 this->inval(NULL);
928}
929
Scroggo2c8208f2011-06-15 16:49:08 +0000930bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +0000931 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
Scroggo2c8208f2011-06-15 16:49:08 +0000932 this->loadView(fSamples[fCurrIndex]());
933 return true;
934}
935
reed@android.com8a1c16f2008-12-17 15:59:43 +0000936bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000937 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
938 this->loadView(fSamples[fCurrIndex]());
939 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000940}
941
Scroggo2c8208f2011-06-15 16:49:08 +0000942void SampleWindow::postAnimatingEvent() {
943 if (fAnimating) {
944 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
945 evt->post(this->getSinkID(), ANIMATING_DELAY);
946 }
947}
948
reed@android.com8a1c16f2008-12-17 15:59:43 +0000949bool SampleWindow::onEvent(const SkEvent& evt) {
950 if (evt.isType(ANIMATING_EVENTTYPE)) {
951 if (fAnimating) {
952 this->nextSample();
953 this->postAnimatingEvent();
954 }
955 return true;
956 }
reed@android.com34245c72009-11-03 04:00:48 +0000957 if (evt.isType("set-curr-index")) {
958 fCurrIndex = evt.getFast32() % fSamples.count();
959 this->loadView(fSamples[fCurrIndex]());
960 return true;
961 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000962 if (isInvalEvent(evt)) {
963 this->inval(NULL);
964 return true;
965 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000966 return this->INHERITED::onEvent(evt);
967}
968
reed@android.comf2b98d62010-12-20 18:26:13 +0000969bool SampleWindow::onQuery(SkEvent* query) {
970 if (query->isType("get-slide-count")) {
971 query->setFast32(fSamples.count());
972 return true;
973 }
974 if (query->isType("get-slide-title")) {
975 SkView* view = fSamples[query->getFast32()]();
976 SkEvent evt(gTitleEvtName);
977 if (view->doQuery(&evt)) {
978 query->setString("title", evt.findString(gTitleEvtName));
979 }
980 SkSafeUnref(view);
981 return true;
982 }
983 if (query->isType("use-fast-text")) {
984 SkEvent evt(gFastTextEvtName);
985 return curr_view(this)->doQuery(&evt);
986 }
987 return this->INHERITED::onQuery(query);
988}
989
reed@android.com0ae6b242008-12-23 16:49:54 +0000990static void cleanup_for_filename(SkString* name) {
991 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +0000992 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +0000993 switch (str[i]) {
994 case ':': str[i] = '-'; break;
995 case '/': str[i] = '-'; break;
996 case ' ': str[i] = '_'; break;
997 default: break;
998 }
999 }
1000}
reed@android.com8a1c16f2008-12-17 15:59:43 +00001001
1002bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001003 {
1004 SkView* view = curr_view(this);
1005 if (view) {
1006 SkEvent evt(gCharEvtName);
1007 evt.setFast32(uni);
1008 if (view->doQuery(&evt)) {
1009 return true;
1010 }
1011 }
1012 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001013
reed@android.com8a1c16f2008-12-17 15:59:43 +00001014 int dx = 0xFF;
1015 int dy = 0xFF;
1016
1017 switch (uni) {
1018 case '5': dx = 0; dy = 0; break;
1019 case '8': dx = 0; dy = -1; break;
1020 case '6': dx = 1; dy = 0; break;
1021 case '2': dx = 0; dy = 1; break;
1022 case '4': dx = -1; dy = 0; break;
1023 case '7': dx = -1; dy = -1; break;
1024 case '9': dx = 1; dy = -1; break;
1025 case '3': dx = 1; dy = 1; break;
1026 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001027
reed@android.com8a1c16f2008-12-17 15:59:43 +00001028 default:
1029 break;
1030 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001031
reed@android.com8a1c16f2008-12-17 15:59:43 +00001032 if (0xFF != dx && 0xFF != dy) {
1033 if ((dx | dy) == 0) {
1034 fScrollTestX = fScrollTestY = 0;
1035 } else {
1036 fScrollTestX += dx;
1037 fScrollTestY += dy;
1038 }
1039 this->inval(NULL);
1040 return true;
1041 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001042
reed@android.com0ae6b242008-12-23 16:49:54 +00001043 switch (uni) {
1044 case 'a':
Scroggo2c8208f2011-06-15 16:49:08 +00001045 this->toggleSlideshow();
reed@android.com0ae6b242008-12-23 16:49:54 +00001046 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001047 case 'b':
1048 fAAState = cycle_tristate(fAAState);
1049 this->updateTitle();
1050 this->inval(NULL);
1051 break;
1052 case 'c':
1053 fUseClip = !fUseClip;
1054 this->inval(NULL);
1055 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001056 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001057 case 'd':
1058 SkGraphics::SetFontCacheUsed(0);
1059 return true;
Scroggo8ac0d542011-06-21 14:44:57 +00001060 case 'e':
1061 this->saveToPdf();
1062 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001063 case 'f':
Scroggo2c8208f2011-06-15 16:49:08 +00001064 this->toggleFPS();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001065 break;
1066 case 'g':
1067 fRequestGrabImage = true;
1068 this->inval(NULL);
1069 break;
reed@google.com09e3baa2011-05-18 12:04:31 +00001070 case 'h':
1071 fHintingState = cycle_tristate(fHintingState);
1072 this->updateTitle();
1073 this->inval(NULL);
1074 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001075 case 'i':
1076 this->zoomIn();
1077 break;
1078 case 'l':
1079 fLCDState = cycle_tristate(fLCDState);
1080 this->updateTitle();
1081 this->inval(NULL);
1082 break;
reed@google.com176753a2011-05-17 15:32:04 +00001083 case 'n':
1084 fFilterState = cycle_tristate(fFilterState);
1085 this->updateTitle();
1086 this->inval(NULL);
1087 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001088 case 'o':
1089 this->zoomOut();
1090 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001091 case 'p':
1092 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001093 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001094 this->inval(NULL);
1095 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001096 case 'r':
1097 fRotate = !fRotate;
1098 this->inval(NULL);
1099 this->updateTitle();
1100 return true;
1101 case 's':
1102 fScale = !fScale;
1103 this->inval(NULL);
1104 this->updateTitle();
1105 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001106 case 'x':
1107 fFlipAxis ^= kFlipAxis_X;
1108 this->updateTitle();
1109 this->inval(NULL);
1110 break;
1111 case 'y':
1112 fFlipAxis ^= kFlipAxis_Y;
1113 this->updateTitle();
1114 this->inval(NULL);
1115 break;
scroggo08526c02011-03-22 14:03:21 +00001116 case 'z':
1117 this->toggleZoomer();
1118 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001119 default:
1120 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001121 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001122
reed@android.com8a1c16f2008-12-17 15:59:43 +00001123 return this->INHERITED::onHandleChar(uni);
1124}
1125
Scroggo2c8208f2011-06-15 16:49:08 +00001126void SampleWindow::toggleFPS() {
1127 fMeasureFPS = !fMeasureFPS;
1128 this->inval(NULL);
1129 this->updateTitle();
1130}
1131
1132void SampleWindow::toggleSlideshow() {
1133 fAnimating = !fAnimating;
1134 this->postAnimatingEvent();
1135 this->updateTitle();
1136}
1137
1138void SampleWindow::toggleRendering() {
1139 fCanvasType = cycle_canvastype(fCanvasType);
1140 this->updateTitle();
1141 this->inval(NULL);
1142}
1143
reed@android.com8a1c16f2008-12-17 15:59:43 +00001144#include "SkDumpCanvas.h"
1145
1146bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001147 {
1148 SkView* view = curr_view(this);
1149 if (view) {
1150 SkEvent evt(gKeyEvtName);
1151 evt.setFast32(key);
1152 if (view->doQuery(&evt)) {
1153 return true;
1154 }
1155 }
1156 }
1157
reed@android.com8a1c16f2008-12-17 15:59:43 +00001158 switch (key) {
1159 case kRight_SkKey:
1160 if (this->nextSample()) {
1161 return true;
1162 }
1163 break;
1164 case kLeft_SkKey:
Scroggo2c8208f2011-06-15 16:49:08 +00001165 toggleRendering();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001166 return true;
1167 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001168 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001169 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001170 } else {
1171 fNClip = !fNClip;
1172 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001173 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001174 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001175 return true;
1176 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001177 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001178 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001179 } else {
1180 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001181 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001182 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001183 return true;
1184 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001185 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001186 SkDebugfDumper dumper;
1187 SkDumpCanvas dc(&dumper);
1188 this->draw(&dc);
1189 } else {
1190 fRepeatDrawing = !fRepeatDrawing;
1191 if (fRepeatDrawing) {
1192 this->inval(NULL);
1193 }
1194 }
1195 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001196 case kBack_SkKey:
1197 this->loadView(NULL);
1198 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001199 default:
1200 break;
1201 }
1202 return this->INHERITED::onHandleKey(key);
1203}
1204
reed@google.com52f57e12011-03-16 12:10:02 +00001205///////////////////////////////////////////////////////////////////////////////
1206
1207static const char gGestureClickType[] = "GestureClickType";
1208
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001209bool SampleWindow::onDispatchClick(int x, int y, Click::State state) {
Scroggo0f185c22011-03-24 18:35:50 +00001210 if (Click::kMoved_State == state) {
1211 updatePointer(x, y);
1212 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001213 int w = SkScalarRound(this->width());
1214 int h = SkScalarRound(this->height());
1215
1216 // check for the resize-box
1217 if (w - x < 16 && h - y < 16) {
1218 return false; // let the OS handle the click
1219 } else {
1220 return this->INHERITED::onDispatchClick(x, y, state);
1221 }
1222}
1223
reed@google.com52f57e12011-03-16 12:10:02 +00001224class GestureClick : public SkView::Click {
1225public:
1226 GestureClick(SkView* target) : SkView::Click(target) {
1227 this->setType(gGestureClickType);
1228 }
1229
1230 static bool IsGesture(Click* click) {
1231 return click->isType(gGestureClickType);
1232 }
1233};
1234
1235SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1236 return new GestureClick(this);
1237}
1238
Scroggoa54e2f62011-06-17 12:46:17 +00001239union IntPtr {
1240 int fInt;
1241 void* fPtr;
1242};
1243
1244static void* int2ptr(int n) {
1245 IntPtr data;
1246 data.fInt = n;
1247 return data.fPtr;
1248}
1249
1250bool SampleWindow::handleTouch(int ownerId, float x, float y, SkView::Click::State state) {
1251 void* click = int2ptr(ownerId);
1252 switch(state) {
1253 case SkView::Click::kDown_State:
1254 fGesture.touchBegin(click, x, y);
1255 break;
1256 case SkView::Click::kMoved_State:
1257 fGesture.touchMoved(click, x, y);
1258 this->inval(NULL);
1259 break;
1260 case SkView::Click::kUp_State:
1261 fGesture.touchEnd(click);
1262 this->inval(NULL);
1263 break;
1264 default:
1265 return false;
1266 }
1267 return true;
1268}
1269
reed@google.com52f57e12011-03-16 12:10:02 +00001270bool SampleWindow::onClick(Click* click) {
1271 if (GestureClick::IsGesture(click)) {
1272 float x = SkScalarToFloat(click->fCurr.fX);
1273 float y = SkScalarToFloat(click->fCurr.fY);
1274 switch (click->fState) {
1275 case SkView::Click::kDown_State:
1276 fGesture.touchBegin(click, x, y);
1277 break;
1278 case SkView::Click::kMoved_State:
1279 fGesture.touchMoved(click, x, y);
1280 this->inval(NULL);
1281 break;
1282 case SkView::Click::kUp_State:
1283 fGesture.touchEnd(click);
1284 this->inval(NULL);
1285 break;
1286 }
1287 return true;
1288 }
1289 return false;
1290}
1291
1292///////////////////////////////////////////////////////////////////////////////
1293
reed@android.com8a1c16f2008-12-17 15:59:43 +00001294void SampleWindow::loadView(SkView* view) {
1295 SkView::F2BIter iter(this);
1296 SkView* prev = iter.next();
1297 if (prev) {
1298 prev->detachFromParent();
1299 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001300
reed@android.com34245c72009-11-03 04:00:48 +00001301 if (NULL == view) {
1302 view = create_overview(fSamples.count(), fSamples.begin());
1303 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001304 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001305 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001306 this->attachChildToFront(view)->unref();
1307 view->setSize(this->width(), this->height());
1308
1309 this->updateTitle();
1310}
1311
1312static const char* gConfigNames[] = {
1313 "unknown config",
1314 "A1",
1315 "A8",
1316 "Index8",
1317 "565",
1318 "4444",
1319 "8888"
1320};
1321
1322static const char* configToString(SkBitmap::Config c) {
1323 return gConfigNames[c];
1324}
1325
1326static const char* gCanvasTypePrefix[] = {
1327 "raster: ",
1328 "picture: ",
1329 "opengl: "
1330};
1331
reed@google.com569e0432011-04-05 13:07:03 +00001332static const char* trystate_str(SkTriState state,
1333 const char trueStr[], const char falseStr[]) {
1334 if (kTrue_SkTriState == state) {
1335 return trueStr;
1336 } else if (kFalse_SkTriState == state) {
1337 return falseStr;
1338 }
1339 return NULL;
1340}
1341
reed@android.com8a1c16f2008-12-17 15:59:43 +00001342void SampleWindow::updateTitle() {
1343 SkString title;
1344
1345 SkView::F2BIter iter(this);
1346 SkView* view = iter.next();
1347 SkEvent evt(gTitleEvtName);
1348 if (view->doQuery(&evt)) {
1349 title.set(evt.findString(gTitleEvtName));
1350 }
1351 if (title.size() == 0) {
1352 title.set("<unknown>");
1353 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001354
reed@android.com8a1c16f2008-12-17 15:59:43 +00001355 title.prepend(gCanvasTypePrefix[fCanvasType]);
1356
1357 title.prepend(" ");
1358 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001359
reed@android.com8a1c16f2008-12-17 15:59:43 +00001360 if (fAnimating) {
1361 title.prepend("<A> ");
1362 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001363 if (fScale) {
1364 title.prepend("<S> ");
1365 }
1366 if (fRotate) {
1367 title.prepend("<R> ");
1368 }
reed@android.come522ca52009-11-23 20:10:41 +00001369 if (fNClip) {
1370 title.prepend("<C> ");
1371 }
reed@google.com569e0432011-04-05 13:07:03 +00001372
1373 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1374 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001375 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001376 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1377 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001378
1379 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001380 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001381 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001382
1383 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001384 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001385 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001386 if (fUsePipe && SampleView::IsSampleView(view)) {
1387 title.prepend("<P> ");
1388 }
1389 if (SampleView::IsSampleView(view)) {
1390 title.prepend("! ");
1391 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001392
reed@android.com8a1c16f2008-12-17 15:59:43 +00001393 this->setTitle(title.c_str());
1394}
1395
1396void SampleWindow::onSizeChange() {
1397 this->INHERITED::onSizeChange();
1398
1399 SkView::F2BIter iter(this);
1400 SkView* view = iter.next();
1401 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001402
reed@android.com8a1c16f2008-12-17 15:59:43 +00001403 // rebuild our clippath
1404 {
1405 const SkScalar W = this->width();
1406 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001407
reed@android.com8a1c16f2008-12-17 15:59:43 +00001408 fClipPath.reset();
1409#if 0
1410 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1411 SkRect r;
1412 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1413 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1414 fClipPath.addRect(r);
1415 }
1416#else
1417 SkRect r;
1418 r.set(0, 0, W, H);
1419 fClipPath.addRect(r, SkPath::kCCW_Direction);
1420 r.set(W/4, H/4, W*3/4, H*3/4);
1421 fClipPath.addRect(r, SkPath::kCW_Direction);
1422#endif
1423 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001424
Scroggo2c8208f2011-06-15 16:49:08 +00001425 fZoomCenterX = SkScalarHalf(this->width());
1426 fZoomCenterY = SkScalarHalf(this->height());
1427
Scroggo3e7ff9f2011-06-16 15:31:26 +00001428#ifdef ANDROID
Scroggob4490c72011-06-17 13:53:05 +00001429 // FIXME: The first draw after a size change does not work on Android, so
1430 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00001431 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00001432#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001433 this->updateTitle(); // to refresh our config
1434}
1435
1436///////////////////////////////////////////////////////////////////////////////
1437
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001438static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001439static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001440static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001441
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001442bool SampleView::IsSampleView(SkView* view) {
1443 SkEvent evt(is_sample_view_tag);
1444 return view->doQuery(&evt);
1445}
1446
reed@google.comf2183392011-04-22 14:10:48 +00001447bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001448 SkEvent evt(repeat_count_tag);
1449 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001450 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001451}
1452
reed@google.com0faac1e2011-05-11 05:58:58 +00001453bool SampleView::SetUsePipe(SkView* view, bool pred) {
1454 SkEvent evt(set_use_pipe_tag);
1455 evt.setFast32(pred);
1456 return view->doEvent(evt);
1457}
1458
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001459bool SampleView::onEvent(const SkEvent& evt) {
1460 if (evt.isType(repeat_count_tag)) {
1461 fRepeatCount = evt.getFast32();
1462 return true;
1463 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001464 if (evt.isType(set_use_pipe_tag)) {
1465 fUsePipe = !!evt.getFast32();
1466 return true;
1467 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001468 return this->INHERITED::onEvent(evt);
1469}
1470
1471bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001472 if (evt->isType(is_sample_view_tag)) {
1473 return true;
1474 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001475 return this->INHERITED::onQuery(evt);
1476}
1477
reed@google.com68f456d2011-05-02 18:55:39 +00001478#ifdef TEST_GPIPE
1479 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001480
1481class SimplePC : public SkGPipeController {
1482public:
1483 SimplePC(SkCanvas* target);
1484 ~SimplePC();
1485
1486 virtual void* requestBlock(size_t minRequest, size_t* actual);
1487 virtual void notifyWritten(size_t bytes);
1488
1489private:
reed@google.com961ddb02011-05-05 14:03:48 +00001490 SkGPipeReader fReader;
1491 void* fBlock;
1492 size_t fBlockSize;
1493 size_t fBytesWritten;
1494 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001495 SkGPipeReader::Status fStatus;
1496
1497 size_t fTotalWritten;
1498};
1499
1500SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1501 fBlock = NULL;
1502 fBlockSize = fBytesWritten = 0;
1503 fStatus = SkGPipeReader::kDone_Status;
1504 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001505 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001506}
1507
1508SimplePC::~SimplePC() {
1509// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1510 sk_free(fBlock);
1511
reed@google.com0faac1e2011-05-11 05:58:58 +00001512 if (fTotalWritten) {
1513 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1514 fAtomsWritten, fStatus);
1515 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001516}
1517
1518void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1519 sk_free(fBlock);
1520
1521 fBlockSize = minRequest * 4;
1522 fBlock = sk_malloc_throw(fBlockSize);
1523 fBytesWritten = 0;
1524 *actual = fBlockSize;
1525 return fBlock;
1526}
1527
1528void SimplePC::notifyWritten(size_t bytes) {
1529 SkASSERT(fBytesWritten + bytes <= fBlockSize);
yangsu@google.com1f394212011-06-01 18:03:34 +00001530
1531#ifdef PIPE_FILE
1532 //File is open in append mode
1533 FILE* f = fopen(FILE_PATH, "ab");
1534 SkASSERT(f != NULL);
1535 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
1536 fclose(f);
1537#endif
1538
reed@google.com64e3eb22011-05-04 14:32:04 +00001539 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1540 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1541 fBytesWritten += bytes;
1542 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001543
1544 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001545}
1546
reed@google.com68f456d2011-05-02 18:55:39 +00001547#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001548
reed@google.com64e3eb22011-05-04 14:32:04 +00001549
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001550void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001551#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001552 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001553 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001554 if (fUsePipe) {
reed@google.comdde09562011-05-23 12:21:05 +00001555 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
1556// flags = 0;
1557 canvas = writer.startRecording(&controller, flags);
reed@google.com0faac1e2011-05-11 05:58:58 +00001558 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001559#endif
1560
reed@google.com81e3d7f2011-06-01 12:42:36 +00001561 this->onDrawBackground(canvas);
1562
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001563 for (int i = 0; i < fRepeatCount; i++) {
1564 SkAutoCanvasRestore acr(canvas, true);
1565 this->onDrawContent(canvas);
1566 }
1567}
1568
1569void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001570 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001571}
1572
1573///////////////////////////////////////////////////////////////////////////////
1574
reed@android.comf2b98d62010-12-20 18:26:13 +00001575template <typename T> void SkTBSort(T array[], int count) {
1576 for (int i = 1; i < count - 1; i++) {
1577 bool didSwap = false;
1578 for (int j = count - 1; j > i; --j) {
1579 if (array[j] < array[j-1]) {
1580 T tmp(array[j-1]);
1581 array[j-1] = array[j];
1582 array[j] = tmp;
1583 didSwap = true;
1584 }
1585 }
1586 if (!didSwap) {
1587 break;
1588 }
1589 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001590
reed@android.comf2b98d62010-12-20 18:26:13 +00001591 for (int k = 0; k < count - 1; k++) {
1592 SkASSERT(!(array[k+1] < array[k]));
1593 }
1594}
1595
1596#include "SkRandom.h"
1597
1598static void rand_rect(SkIRect* rect, SkRandom& rand) {
1599 int bits = 8;
1600 int shift = 32 - bits;
1601 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1602 rand.nextU() >> shift, rand.nextU() >> shift);
1603 rect->sort();
1604}
1605
1606static void dumpRect(const SkIRect& r) {
1607 SkDebugf(" { %d, %d, %d, %d },\n",
1608 r.fLeft, r.fTop,
1609 r.fRight, r.fBottom);
1610}
1611
1612static void test_rects(const SkIRect rect[], int count) {
1613 SkRegion rgn0, rgn1;
1614
1615 for (int i = 0; i < count; i++) {
1616 rgn0.op(rect[i], SkRegion::kUnion_Op);
1617 // dumpRect(rect[i]);
1618 }
1619 rgn1.setRects(rect, count);
1620
1621 if (rgn0 != rgn1) {
1622 SkDebugf("\n");
1623 for (int i = 0; i < count; i++) {
1624 dumpRect(rect[i]);
1625 }
1626 SkDebugf("\n");
1627 }
1628}
1629
1630static void test() {
1631 size_t i;
1632
1633 const SkIRect r0[] = {
1634 { 0, 0, 1, 1 },
1635 { 2, 2, 3, 3 },
1636 };
1637 const SkIRect r1[] = {
1638 { 0, 0, 1, 3 },
1639 { 1, 1, 2, 2 },
1640 { 2, 0, 3, 3 },
1641 };
1642 const SkIRect r2[] = {
1643 { 0, 0, 1, 2 },
1644 { 2, 1, 3, 3 },
1645 { 4, 0, 5, 1 },
1646 { 6, 0, 7, 4 },
1647 };
1648
1649 static const struct {
1650 const SkIRect* fRects;
1651 int fCount;
1652 } gRecs[] = {
1653 { r0, SK_ARRAY_COUNT(r0) },
1654 { r1, SK_ARRAY_COUNT(r1) },
1655 { r2, SK_ARRAY_COUNT(r2) },
1656 };
1657
1658 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1659 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1660 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001661
reed@android.comf2b98d62010-12-20 18:26:13 +00001662 SkRandom rand;
1663 for (i = 0; i < 10000; i++) {
1664 SkRegion rgn0, rgn1;
1665
1666 const int N = 8;
1667 SkIRect rect[N];
1668 for (int j = 0; j < N; j++) {
1669 rand_rect(&rect[j], rand);
1670 }
1671 test_rects(rect, N);
1672 }
1673}
1674
reed@android.com8a1c16f2008-12-17 15:59:43 +00001675SkOSWindow* create_sk_window(void* hwnd) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001676// test();
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001677 return new SampleWindow(hwnd);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001678}
1679
1680void get_preferred_size(int* x, int* y, int* width, int* height) {
1681 *x = 10;
1682 *y = 50;
1683 *width = 640;
1684 *height = 480;
1685}
1686
1687void application_init() {
1688// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001689#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001690 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001691#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001692 SkGraphics::Init();
1693 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001694}
1695
1696void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001697 SkEvent::Term();
1698 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001699}