blob: d561c8514836e38cf920e1a8f9f50e29bf482ac4 [file] [log] [blame]
Scroggo2c8208f2011-06-15 16:49:08 +00001#include "SampleApp.h"
2
reed@google.com8a85d0c2011-06-24 19:12:12 +00003#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00004#include "SkCanvas.h"
5#include "SkDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00006#include "SkGpuCanvas.h"
reed@google.comaf951c92011-06-16 19:10:39 +00007#include "SkGpuDevice.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkGraphics.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +00009#include "SkImageEncoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkPaint.h"
11#include "SkPicture.h"
12#include "SkStream.h"
reed@android.com44177402009-11-23 21:07:51 +000013#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkWindow.h"
15
16#include "SampleCode.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017#include "GrContext.h"
Scroggo0f185c22011-03-24 18:35:50 +000018#include "SkTypeface.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000019
Scroggo3e7ff9f2011-06-16 15:31:26 +000020#include "GrGLInterface.h"
Scroggo2c8208f2011-06-15 16:49:08 +000021
Scroggo8ac0d542011-06-21 14:44:57 +000022#include "SkPDFDevice.h"
23#include "SkPDFDocument.h"
24#include "SkStream.h"
25
yangsu@google.com1f394212011-06-01 18:03:34 +000026#define TEST_GPIPEx
27
28#ifdef TEST_GPIPE
29#define PIPE_FILE
30#define FILE_PATH "/path/to/drawing.data"
31#endif
32
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +000033#define USE_ARROWS_FOR_ZOOM true
reed@android.comf2b98d62010-12-20 18:26:13 +000034//#define DEFAULT_TO_GPU
35
reed@android.come191b162009-12-18 21:33:39 +000036extern SkView* create_overview(int, const SkViewFactory[]);
reed@android.com34245c72009-11-03 04:00:48 +000037
reed@android.comcb342352010-07-22 18:27:53 +000038#define SK_SUPPORT_GL
reed@android.com8a1c16f2008-12-17 15:59:43 +000039
40#define ANIMATING_EVENTTYPE "nextSample"
41#define ANIMATING_DELAY 750
42
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000043#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +000044 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000045#else
reed@google.combad8c872011-05-18 20:10:31 +000046 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000047#endif
reed@google.combad8c872011-05-18 20:10:31 +000048#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000049
reed@google.comac10a2d2010-12-22 21:39:39 +000050#ifdef SK_SUPPORT_GL
51 #include "GrGLConfig.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000052#endif
53
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000054///////////////
55static const char view_inval_msg[] = "view-inval-msg";
56
Scroggo62b65b02011-06-21 16:01:26 +000057void SampleWindow::postInvalDelay() {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000058 SkEvent* evt = new SkEvent(view_inval_msg);
Scroggo62b65b02011-06-21 16:01:26 +000059 evt->post(this->getSinkID(), 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000060}
61
62static bool isInvalEvent(const SkEvent& evt) {
63 return evt.isType(view_inval_msg);
64}
65//////////////////
66
reed@android.com8a1c16f2008-12-17 15:59:43 +000067SkViewRegister* SkViewRegister::gHead;
68SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) {
69 static bool gOnce;
70 if (!gOnce) {
71 gHead = NULL;
72 gOnce = true;
73 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 fChain = gHead;
76 gHead = this;
77}
78
reed@android.comf2b98d62010-12-20 18:26:13 +000079#if defined(SK_SUPPORT_GL)
80 #define SK_USE_SHADERS
81#endif
82
twiz@google.com06c3b6b2011-03-14 16:58:39 +000083#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +000084#include <CoreFoundation/CoreFoundation.h>
85#include <CoreFoundation/CFURLAccess.h>
86
87static void testpdf() {
88 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
89 kCFStringEncodingUTF8);
90 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
91 kCFURLPOSIXPathStyle,
92 false);
93 CFRelease(path);
94 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
95 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
96 CFRelease(url);
97
98 CGContextBeginPage(cg, &box);
99 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
100 CGContextFillEllipseInRect(cg, r);
101 CGContextEndPage(cg);
102 CGContextRelease(cg);
103
104 if (false) {
105 SkBitmap bm;
106 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
107 bm.allocPixels();
108 bm.eraseColor(0);
109
110 SkCanvas canvas(bm);
111
112 }
113}
114#endif
115
116//////////////////////////////////////////////////////////////////////////////
117
reed@google.com569e0432011-04-05 13:07:03 +0000118enum FlipAxisEnum {
119 kFlipAxis_X = (1 << 0),
120 kFlipAxis_Y = (1 << 1)
121};
122
reed@google.com569e0432011-04-05 13:07:03 +0000123static SkTriState cycle_tristate(SkTriState state) {
124 static const SkTriState gCycle[] = {
125 /* kFalse_SkTriState -> */ kUnknown_SkTriState,
126 /* kTrue_SkTriState -> */ kFalse_SkTriState,
127 /* kUnknown_SkTriState -> */ kTrue_SkTriState,
128 };
129 return gCycle[state];
130}
131
reed@google.comf0b5f682011-03-11 20:08:25 +0000132#include "SkDrawFilter.h"
133
reed@google.com569e0432011-04-05 13:07:03 +0000134class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000135public:
reed@google.com09e3baa2011-05-18 12:04:31 +0000136 FlagsDrawFilter(SkTriState lcd, SkTriState aa, SkTriState filter,
137 SkTriState hinting) :
138 fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000139
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000140 virtual void filter(SkPaint* paint, Type t) {
reed@google.com569e0432011-04-05 13:07:03 +0000141 if (kText_Type == t && kUnknown_SkTriState != fLCDState) {
reed@google.com569e0432011-04-05 13:07:03 +0000142 paint->setLCDRenderText(kTrue_SkTriState == fLCDState);
143 }
144 if (kUnknown_SkTriState != fAAState) {
reed@google.com569e0432011-04-05 13:07:03 +0000145 paint->setAntiAlias(kTrue_SkTriState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000146 }
reed@google.com176753a2011-05-17 15:32:04 +0000147 if (kUnknown_SkTriState != fFilterState) {
148 paint->setFilterBitmap(kTrue_SkTriState == fFilterState);
149 }
reed@google.com09e3baa2011-05-18 12:04:31 +0000150 if (kUnknown_SkTriState != fHintingState) {
151 paint->setHinting(kTrue_SkTriState == fHintingState ?
152 SkPaint::kNormal_Hinting :
153 SkPaint::kSlight_Hinting);
154 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000155 }
156
157private:
reed@google.com569e0432011-04-05 13:07:03 +0000158 SkTriState fLCDState;
reed@google.com569e0432011-04-05 13:07:03 +0000159 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000160 SkTriState fFilterState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000161 SkTriState fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000162};
163
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164//////////////////////////////////////////////////////////////////////////////
165
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000166#define MAX_ZOOM_LEVEL 8
167#define MIN_ZOOM_LEVEL -8
168
reed@android.comf2b98d62010-12-20 18:26:13 +0000169static const char gCharEvtName[] = "SampleCode_Char_Event";
170static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171static const char gTitleEvtName[] = "SampleCode_Title_Event";
172static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000173static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
174
175bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
176 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
177 if (outUni) {
178 *outUni = evt.getFast32();
179 }
180 return true;
181 }
182 return false;
183}
184
185bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
186 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
187 if (outKey) {
188 *outKey = (SkKey)evt.getFast32();
189 }
190 return true;
191 }
192 return false;
193}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194
195bool SampleCode::TitleQ(const SkEvent& evt) {
196 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
197}
198
199void SampleCode::TitleR(SkEvent* evt, const char title[]) {
200 SkASSERT(evt && TitleQ(*evt));
201 evt->setString(gTitleEvtName, title);
202}
203
204bool SampleCode::PrefSizeQ(const SkEvent& evt) {
205 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
206}
207
208void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
209 SkASSERT(evt && PrefSizeQ(*evt));
210 SkScalar size[2];
211 size[0] = width;
212 size[1] = height;
213 evt->setScalars(gPrefSizeEvtName, 2, size);
214}
215
reed@android.comf2b98d62010-12-20 18:26:13 +0000216bool SampleCode::FastTextQ(const SkEvent& evt) {
217 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
218}
219
220///////////////////////////////////////////////////////////////////////////////
221
reed@android.com44177402009-11-23 21:07:51 +0000222static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000223static SkMSec gAnimTimePrev;
224
reed@android.com44177402009-11-23 21:07:51 +0000225SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000226SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
227SkScalar SampleCode::GetAnimSecondsDelta() {
228 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
229}
reed@android.com44177402009-11-23 21:07:51 +0000230
231SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000232 // since gAnimTime can be up to 32 bits, we can't convert it to a float
233 // or we'll lose the low bits. Hence we use doubles for the intermediate
234 // calculations
235 double seconds = (double)gAnimTime / 1000.0;
236 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000237 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000238 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000239 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000240 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000241}
242
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243//////////////////////////////////////////////////////////////////////////////
244
reed@android.comf2b98d62010-12-20 18:26:13 +0000245static SkView* curr_view(SkWindow* wind) {
246 SkView::F2BIter iter(wind);
247 return iter.next();
248}
249
Scroggo2c8208f2011-06-15 16:49:08 +0000250void SampleWindow::setZoomCenter(float x, float y)
251{
252 fZoomCenterX = SkFloatToScalar(x);
253 fZoomCenterY = SkFloatToScalar(y);
254}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255
djsollen@google.come32b5832011-06-13 16:58:40 +0000256bool SampleWindow::setGrContext(GrContext* context)
257{
258 if (fGrContext) {
259 fGrContext->unref();
260 }
261 fGrContext = context;
262 fGrContext->ref();
263 return true;
264}
265
266GrContext* SampleWindow::getGrContext()
267{
268 return fGrContext;
269}
djsollen@google.come32b5832011-06-13 16:58:40 +0000270
Scroggo0f185c22011-03-24 18:35:50 +0000271bool SampleWindow::zoomIn()
272{
273 // Arbitrarily decided
274 if (fFatBitsScale == 25) return false;
275 fFatBitsScale++;
276 this->inval(NULL);
277 return true;
278}
279
280bool SampleWindow::zoomOut()
281{
282 if (fFatBitsScale == 1) return false;
283 fFatBitsScale--;
284 this->inval(NULL);
285 return true;
286}
287
288void SampleWindow::toggleZoomer()
289{
290 fShowZoomer = !fShowZoomer;
291 this->inval(NULL);
292}
293
294void SampleWindow::updatePointer(int x, int y)
295{
296 fMouseX = x;
297 fMouseY = y;
298 if (fShowZoomer) {
299 this->inval(NULL);
300 }
301}
302
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000303bool SampleWindow::make3DReady() {
304
305#if defined(SK_SUPPORT_GL)
bsalomon@google.com498a6232011-03-10 18:24:15 +0000306 if (attachGL()) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000307 if (NULL != fGrContext) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000308 // various gr lifecycle tests
309 #if 0
310 fGrContext->freeGpuResources();
311 #elif 0
312 // this will leak resources.
313 fGrContext->contextLost();
314 #elif 0
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000315 GrAssert(1 == fGrContext->refcnt());
316 fGrContext->unref();
317 fGrContext = NULL;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000318 #endif
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000319 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000320
bsalomon@google.com498a6232011-03-10 18:24:15 +0000321 if (NULL == fGrContext) {
322 #if defined(SK_USE_SHADERS)
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000323 fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000324 #else
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000325 fGrContext = GrContext::Create(kOpenGL_Fixed_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000326 #endif
reed@google.com569e0432011-04-05 13:07:03 +0000327 SkDebugf("---- constructor\n");
bsalomon@google.com498a6232011-03-10 18:24:15 +0000328 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000329
bsalomon@google.com498a6232011-03-10 18:24:15 +0000330 if (NULL != fGrContext) {
331 return true;
332 } else {
333 detachGL();
334 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000335 }
336#endif
337 SkDebugf("Failed to setup 3D");
338 return false;
339}
340
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) {
342 static const CanvasType gCT[] = {
343 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000344 kGPU_CanvasType,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000345 kRaster_CanvasType
346 };
347 return gCT[ct];
348}
349
350SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) {
yangsu@google.com1f394212011-06-01 18:03:34 +0000351#ifdef PIPE_FILE
352 //Clear existing file or create file if it doesn't exist
353 FILE* f = fopen(FILE_PATH, "wb");
354 fclose(f);
355#endif
356
reed@android.com8a1c16f2008-12-17 15:59:43 +0000357 fPicture = NULL;
reed@android.comf2b98d62010-12-20 18:26:13 +0000358 fGpuCanvas = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000359
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000360 fGrContext = NULL;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000361
reed@android.comf2b98d62010-12-20 18:26:13 +0000362#ifdef DEFAULT_TO_GPU
363 fCanvasType = kGPU_CanvasType;
364#else
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365 fCanvasType = kRaster_CanvasType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000366#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000367 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000368 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000369 fRepeatDrawing = false;
370 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000371 fRotate = false;
372 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000373 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000374 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000375 fMeasureFPS = false;
reed@google.com569e0432011-04-05 13:07:03 +0000376 fLCDState = kUnknown_SkTriState;
377 fAAState = kUnknown_SkTriState;
reed@google.com66f22fd2011-05-17 15:33:45 +0000378 fFilterState = kUnknown_SkTriState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000379 fHintingState = kUnknown_SkTriState;
reed@google.com569e0432011-04-05 13:07:03 +0000380 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000381 fScrollTestX = fScrollTestY = 0;
382
Scroggo0f185c22011-03-24 18:35:50 +0000383 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000384 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000385 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
386 fShowZoomer = false;
387
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000388 fZoomLevel = 0;
389 fZoomScale = SK_Scalar1;
390
Scroggo8ac0d542011-06-21 14:44:57 +0000391 fSaveToPdf = false;
392 fPdfCanvas = NULL;
393
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000394// this->setConfig(SkBitmap::kRGB_565_Config);
395 this->setConfig(SkBitmap::kARGB_8888_Config);
396 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000397 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398
reed@android.com34245c72009-11-03 04:00:48 +0000399 {
400 const SkViewRegister* reg = SkViewRegister::Head();
401 while (reg) {
402 *fSamples.append() = reg->factory();
403 reg = reg->next();
404 }
405 }
406 fCurrIndex = 0;
reed@android.come0f13ee2009-11-04 19:40:25 +0000407 this->loadView(fSamples[fCurrIndex]());
reed@google.comf0b5f682011-03-11 20:08:25 +0000408
Scroggob4490c72011-06-17 13:53:05 +0000409 // If another constructor set our dimensions, ensure that our
410 // onSizeChange gets called.
411 if (this->height() && this->width()) {
412 this->onSizeChange();
413 }
yangsu@google.com501775e2011-06-24 16:04:50 +0000414
415 fPDFData = NULL;
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000416#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +0000417 testpdf();
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000418#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000419}
420
421SampleWindow::~SampleWindow() {
422 delete fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000423 delete fGpuCanvas;
Scroggo8ac0d542011-06-21 14:44:57 +0000424 delete fPdfCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000425 if (NULL != fGrContext) {
426 fGrContext->unref();
427 }
Scroggo0f185c22011-03-24 18:35:50 +0000428 fTypeface->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429}
430
reed@android.com55e76b22009-11-23 21:46:47 +0000431static SkBitmap capture_bitmap(SkCanvas* canvas) {
432 SkBitmap bm;
433 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
434 src.copyTo(&bm, src.config());
435 return bm;
436}
437
438static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
439 SkBitmap* diff) {
440 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000441
reed@android.com55e76b22009-11-23 21:46:47 +0000442 SkAutoLockPixels alp0(src);
443 SkAutoLockPixels alp1(orig);
444 for (int y = 0; y < src.height(); y++) {
445 const void* srcP = src.getAddr(0, y);
446 const void* origP = orig.getAddr(0, y);
447 size_t bytes = src.width() * src.bytesPerPixel();
448 if (memcmp(srcP, origP, bytes)) {
449 SkDebugf("---------- difference on line %d\n", y);
450 return true;
451 }
452 }
453 return false;
454}
455
Scroggo0f185c22011-03-24 18:35:50 +0000456static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
457{
458 SkColor desiredColor = paint.getColor();
459 paint.setColor(SK_ColorWHITE);
460 const char* c_str = string.c_str();
461 size_t size = string.size();
462 SkRect bounds;
463 paint.measureText(c_str, size, &bounds);
464 bounds.offset(left, top);
465 SkScalar inset = SkIntToScalar(-2);
466 bounds.inset(inset, inset);
467 canvas->drawRect(bounds, paint);
468 if (desiredColor != SK_ColorBLACK) {
469 paint.setColor(SK_ColorBLACK);
470 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
471 }
472 paint.setColor(desiredColor);
473 canvas->drawText(c_str, size, left, top, paint);
474}
475
reed@android.com44177402009-11-23 21:07:51 +0000476#define XCLIP_N 8
477#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000478
479void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000480 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000481 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000482 gAnimTime = SkTime::GetMSecs();
483
Scroggo2c8208f2011-06-15 16:49:08 +0000484 SkScalar cx = fZoomCenterX;
485 SkScalar cy = fZoomCenterY;
reed@google.com569e0432011-04-05 13:07:03 +0000486
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000487 if (fZoomLevel) {
488 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000489 SkPoint center;
490 m = canvas->getTotalMatrix();//.invert(&m);
491 m.mapXY(cx, cy, &center);
492 cx = center.fX;
493 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000494
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000495 m.setTranslate(-cx, -cy);
496 m.postScale(fZoomScale, fZoomScale);
497 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000498
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000499 canvas->concat(m);
500 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000501
reed@google.com569e0432011-04-05 13:07:03 +0000502 if (fFlipAxis) {
503 SkMatrix m;
504 m.setTranslate(cx, cy);
505 if (fFlipAxis & kFlipAxis_X) {
506 m.preScale(-SK_Scalar1, SK_Scalar1);
507 }
508 if (fFlipAxis & kFlipAxis_Y) {
509 m.preScale(SK_Scalar1, -SK_Scalar1);
510 }
511 m.preTranslate(-cx, -cy);
512 canvas->concat(m);
513 }
514
reed@google.com52f57e12011-03-16 12:10:02 +0000515 // Apply any gesture matrix
516 if (true) {
517 const SkMatrix& localM = fGesture.localM();
518 if (localM.getType() & SkMatrix::kScale_Mask) {
519 canvas->setExternalMatrix(&localM);
520 }
521 canvas->concat(localM);
522 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000523
reed@google.com52f57e12011-03-16 12:10:02 +0000524 if (fGesture.isActive()) {
525 this->inval(NULL);
526 }
527 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000528
reed@android.come522ca52009-11-23 20:10:41 +0000529 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000530 this->INHERITED::draw(canvas);
531 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000532
533 const SkScalar w = this->width();
534 const SkScalar h = this->height();
535 const SkScalar cw = w / XCLIP_N;
536 const SkScalar ch = h / YCLIP_N;
537 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000538 SkRect r;
539 r.fTop = y * ch;
540 r.fBottom = (y + 1) * ch;
541 if (y == YCLIP_N - 1) {
542 r.fBottom = h;
543 }
reed@android.come522ca52009-11-23 20:10:41 +0000544 for (int x = 0; x < XCLIP_N; x++) {
545 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000546 r.fLeft = x * cw;
547 r.fRight = (x + 1) * cw;
548 if (x == XCLIP_N - 1) {
549 r.fRight = w;
550 }
reed@android.come522ca52009-11-23 20:10:41 +0000551 canvas->clipRect(r);
552 this->INHERITED::draw(canvas);
553 }
554 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000555
reed@android.com55e76b22009-11-23 21:46:47 +0000556 SkBitmap diff;
557 if (bitmap_diff(canvas, orig, &diff)) {
558 }
reed@android.come522ca52009-11-23 20:10:41 +0000559 } else {
560 this->INHERITED::draw(canvas);
561 }
Scroggo8ac0d542011-06-21 14:44:57 +0000562 if (fShowZoomer && fCanvasType != kGPU_CanvasType && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +0000563 // In the GPU case, INHERITED::draw calls beforeChildren, which
564 // creates an SkGpuCanvas. All further draw calls are directed
565 // at that canvas, which is deleted in afterChildren (which is
566 // also called by draw), so we cannot show the zoomer here.
567 // Instead, we call it inside afterChildren.
568 showZoomer(canvas);
569 }
570}
571
572void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +0000573 int count = canvas->save();
574 canvas->resetMatrix();
575 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000576 int width = SkScalarRound(this->width());
577 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000578 if (fMouseX >= width) fMouseX = width - 1;
579 else if (fMouseX < 0) fMouseX = 0;
580 if (fMouseY >= height) fMouseY = height - 1;
581 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000582
Scroggo0f185c22011-03-24 18:35:50 +0000583 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000584 bitmap.lockPixels();
585
Scroggo0f185c22011-03-24 18:35:50 +0000586 // 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 +0000587 int zoomedWidth = (width >> 1) | 1;
588 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000589 SkIRect src;
590 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
591 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
592 SkRect dest;
593 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
594 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
595 SkPaint paint;
596 // Clear the background behind our zoomed in view
597 paint.setColor(SK_ColorWHITE);
598 canvas->drawRect(dest, paint);
599 canvas->drawBitmapRect(bitmap, &src, dest);
600 paint.setColor(SK_ColorBLACK);
601 paint.setStyle(SkPaint::kStroke_Style);
602 // Draw a border around the pixel in the middle
603 SkRect originalPixel;
604 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
605 SkMatrix matrix;
606 SkRect scalarSrc;
607 scalarSrc.set(src);
608 SkColor color = bitmap.getColor(fMouseX, fMouseY);
609 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
610 SkRect pixel;
611 matrix.mapRect(&pixel, originalPixel);
612 // TODO Perhaps measure the values and make the outline white if it's "dark"
613 if (color == SK_ColorBLACK) {
614 paint.setColor(SK_ColorWHITE);
615 }
616 canvas->drawRect(pixel, paint);
617 }
618 paint.setColor(SK_ColorBLACK);
619 // Draw a border around the destination rectangle
620 canvas->drawRect(dest, paint);
621 paint.setStyle(SkPaint::kStrokeAndFill_Style);
622 // Identify the pixel and its color on screen
623 paint.setTypeface(fTypeface);
624 paint.setAntiAlias(true);
625 SkScalar lineHeight = paint.getFontMetrics(NULL);
626 SkString string;
627 string.appendf("(%i, %i)", fMouseX, fMouseY);
628 SkScalar left = dest.fLeft + SkIntToScalar(3);
629 SkScalar i = SK_Scalar1;
630 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
631 // Alpha
632 i += SK_Scalar1;
633 string.reset();
634 string.appendf("A: %X", SkColorGetA(color));
635 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
636 // Red
637 i += SK_Scalar1;
638 string.reset();
639 string.appendf("R: %X", SkColorGetR(color));
640 paint.setColor(SK_ColorRED);
641 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
642 // Green
643 i += SK_Scalar1;
644 string.reset();
645 string.appendf("G: %X", SkColorGetG(color));
646 paint.setColor(SK_ColorGREEN);
647 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
648 // Blue
649 i += SK_Scalar1;
650 string.reset();
651 string.appendf("B: %X", SkColorGetB(color));
652 paint.setColor(SK_ColorBLUE);
653 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
654 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +0000655}
656
reed@android.com8a1c16f2008-12-17 15:59:43 +0000657void SampleWindow::onDraw(SkCanvas* canvas) {
658 if (fRepeatDrawing) {
659 this->inval(NULL);
660 }
661}
662
663#include "SkColorPriv.h"
664
665static void reverseRedAndBlue(const SkBitmap& bm) {
666 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
667 uint8_t* p = (uint8_t*)bm.getPixels();
668 uint8_t* stop = p + bm.getSize();
669 while (p < stop) {
670 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
671 unsigned scale = SkAlpha255To256(p[3]);
672 unsigned r = p[2];
673 unsigned b = p[0];
674 p[0] = SkAlphaMul(r, scale);
675 p[1] = SkAlphaMul(p[1], scale);
676 p[2] = SkAlphaMul(b, scale);
677 p += 4;
678 }
679}
680
Scroggo8ac0d542011-06-21 14:44:57 +0000681void SampleWindow::saveToPdf()
682{
683 fSaveToPdf = true;
684 this->inval(NULL);
685}
686
reed@android.com8a1c16f2008-12-17 15:59:43 +0000687SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000688 if (kGPU_CanvasType != fCanvasType) {
reed@android.com6efdc472008-12-19 18:24:35 +0000689#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000690 detachGL();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000691#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000692 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000693
Scroggo8ac0d542011-06-21 14:44:57 +0000694 if (fSaveToPdf) {
695 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
696 SkISize size = SkISize::Make(bmp.width(), bmp.height());
697 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
698 canvas->getTotalMatrix());
699 fPdfCanvas = new SkCanvas(pdfDevice);
700 pdfDevice->unref();
701 canvas = fPdfCanvas;
702 } else {
703 switch (fCanvasType) {
704 case kRaster_CanvasType:
reed@android.comf2b98d62010-12-20 18:26:13 +0000705 canvas = this->INHERITED::beforeChildren(canvas);
Scroggo8ac0d542011-06-21 14:44:57 +0000706 break;
707 case kPicture_CanvasType:
708 fPicture = new SkPicture;
709 canvas = fPicture->beginRecording(9999, 9999);
710 break;
711 case kGPU_CanvasType: {
712 if (make3DReady()) {
713 SkDevice* device = canvas->getDevice();
714 const SkBitmap& bitmap = device->accessBitmap(true);
715
716 GrRenderTarget* renderTarget;
717
718 GrPlatformSurfaceDesc desc;
719 desc.reset();
720 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
721 desc.fWidth = bitmap.width();
722 desc.fHeight = bitmap.height();
723 desc.fConfig = kRGBA_8888_GrPixelConfig;
724 desc.fStencilBits = 8;
725 GrGLint buffer;
726 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer);
727 desc.fPlatformRenderTarget = buffer;
728
729 renderTarget = static_cast<GrRenderTarget*>(
730 fGrContext->createPlatformSurface(desc));
731 fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
732 renderTarget->unref();
733
734 device = new SkGpuDevice(fGrContext, renderTarget);
735 fGpuCanvas->setDevice(device)->unref();
736
737 fGpuCanvas->concat(canvas->getTotalMatrix());
738 canvas = fGpuCanvas;
739
740 } else {
741 canvas = this->INHERITED::beforeChildren(canvas);
742 }
743 break;
reed@android.comf2b98d62010-12-20 18:26:13 +0000744 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000745 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000746 }
747
748 if (fUseClip) {
749 canvas->drawColor(0xFFFF88FF);
750 canvas->clipPath(fClipPath);
751 }
752
753 return canvas;
754}
755
756static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
757 const SkRegion& rgn) {
758 SkCanvas canvas(bm);
759 SkRegion inval(rgn);
760
761 inval.translate(r.fLeft, r.fTop);
762 canvas.clipRegion(inval);
763 canvas.drawColor(0xFFFF8080);
764}
yangsu@google.com501775e2011-06-24 16:04:50 +0000765#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +0000766void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +0000767 if (fSaveToPdf) {
768 fSaveToPdf = false;
769 if (fShowZoomer) {
770 showZoomer(fPdfCanvas);
771 }
772 SkString name;
773 name.printf("%s.pdf", this->getTitle());
774 SkPDFDocument doc;
775 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
776 doc.appendPage(device);
777#ifdef ANDROID
778 name.prepend("/sdcard/");
779#endif
yangsu@google.com501775e2011-06-24 16:04:50 +0000780
781#ifdef SK_BUILD_FOR_IOS
782 SkDynamicMemoryWStream mstream;
783 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +0000784 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +0000785#endif
Scroggo8ac0d542011-06-21 14:44:57 +0000786 SkFILEWStream stream(name.c_str());
787 if (stream.isValid()) {
788 doc.emitPDF(&stream);
789 const char* desc = "File saved from Skia SampleApp";
790 this->onPDFSaved(this->getTitle(), desc, name.c_str());
791 }
yangsu@google.com501775e2011-06-24 16:04:50 +0000792
Scroggo8ac0d542011-06-21 14:44:57 +0000793 delete fPdfCanvas;
794 fPdfCanvas = NULL;
795
796 // We took over the draw calls in order to create the PDF, so we need
797 // to redraw.
798 this->inval(NULL);
799 return;
800 }
801
reed@android.comf2b98d62010-12-20 18:26:13 +0000802 if (fRequestGrabImage) {
803 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000804
reed@android.comf2b98d62010-12-20 18:26:13 +0000805 SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
806 SkDevice* device = canvas->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000807 SkBitmap bmp;
808 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000809 static int gSampleGrabCounter;
810 SkString name;
811 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000812 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000813 SkImageEncoder::kPNG_Type, 100);
814 }
815 }
816
reed@android.com8a1c16f2008-12-17 15:59:43 +0000817 switch (fCanvasType) {
818 case kRaster_CanvasType:
819 break;
820 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000821 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000822 SkPicture* pict = new SkPicture(*fPicture);
823 fPicture->unref();
824 orig->drawPicture(*pict);
825 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000826 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000827 SkDynamicMemoryWStream ostream;
828 fPicture->serialize(&ostream);
829 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000830
reed@google.com8a85d0c2011-06-24 19:12:12 +0000831 SkAutoDataUnref data(ostream.copyToData());
832 SkMemoryStream istream(data.data(), data.size());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000833 SkPicture pict(&istream);
834 orig->drawPicture(pict);
835 } else {
836 fPicture->draw(orig);
837 fPicture->unref();
838 }
839 fPicture = NULL;
840 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000841#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000842 case kGPU_CanvasType:
Scroggoaed68d92011-06-08 14:26:00 +0000843 if (fShowZoomer && fGpuCanvas) {
Scroggo3272ba82011-05-25 20:50:42 +0000844 this->showZoomer(fGpuCanvas);
845 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000846 delete fGpuCanvas;
847 fGpuCanvas = NULL;
848 presentGL();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000849 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000850#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000851 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000852
reed@google.com17d7aec2011-04-25 14:31:44 +0000853 // Do this after presentGL and other finishing, rather than in afterChild
854 if (fMeasureFPS && fMeasureFPS_Time) {
855 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
856 this->updateTitle();
Scroggo62b65b02011-06-21 16:01:26 +0000857 this->postInvalDelay();
reed@google.com17d7aec2011-04-25 14:31:44 +0000858 }
859
860 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000861 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000862 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
863 int dx = fScrollTestX * 7;
864 int dy = fScrollTestY * 7;
865 SkIRect r;
866 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000867
reed@android.com8a1c16f2008-12-17 15:59:43 +0000868 r.set(50, 50, 50+100, 50+100);
869 bm.scrollRect(&r, dx, dy, &inval);
870 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000871 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000872}
873
reed@android.com6c5f6f22009-08-14 16:08:38 +0000874void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
875 if (fScale) {
876 SkScalar scale = SK_Scalar1 * 7 / 10;
877 SkScalar cx = this->width() / 2;
878 SkScalar cy = this->height() / 2;
879 canvas->translate(cx, cy);
880 canvas->scale(scale, scale);
881 canvas->translate(-cx, -cy);
882 }
883 if (fRotate) {
884 SkScalar cx = this->width() / 2;
885 SkScalar cy = this->height() / 2;
886 canvas->translate(cx, cy);
887 canvas->rotate(SkIntToScalar(30));
888 canvas->translate(-cx, -cy);
889 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000890
reed@google.com09e3baa2011-05-18 12:04:31 +0000891 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
892 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000893
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000894 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000895 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
896 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
897 fMeasureFPS_Time = SkTime::GetMSecs();
898 }
899 } else {
900 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000901 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000902 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000903}
904
905void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000906 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000907}
908
reed@android.com8a1c16f2008-12-17 15:59:43 +0000909static SkBitmap::Config gConfigCycle[] = {
910 SkBitmap::kNo_Config, // none -> none
911 SkBitmap::kNo_Config, // a1 -> none
912 SkBitmap::kNo_Config, // a8 -> none
913 SkBitmap::kNo_Config, // index8 -> none
914 SkBitmap::kARGB_4444_Config, // 565 -> 4444
915 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
916 SkBitmap::kRGB_565_Config // 8888 -> 565
917};
918
919static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
920 return gConfigCycle[c];
921}
922
djsollen@google.come32b5832011-06-13 16:58:40 +0000923void SampleWindow::changeZoomLevel(float delta) {
924 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000925 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000926 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
927 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000928 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000929 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
930 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000931 } else {
932 fZoomScale = SK_Scalar1;
933 }
934
djsollen@google.come32b5832011-06-13 16:58:40 +0000935 this->updateTitle();
936
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000937 this->inval(NULL);
938}
939
Scroggo2c8208f2011-06-15 16:49:08 +0000940bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +0000941 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
Scroggo2c8208f2011-06-15 16:49:08 +0000942 this->loadView(fSamples[fCurrIndex]());
943 return true;
944}
945
reed@android.com8a1c16f2008-12-17 15:59:43 +0000946bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000947 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
948 this->loadView(fSamples[fCurrIndex]());
949 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000950}
951
yangsu@google.com501775e2011-06-24 16:04:50 +0000952bool SampleWindow::goToSample(int i) {
953 fCurrIndex = (i) % fSamples.count();
954 this->loadView(fSamples[fCurrIndex]());
955 return true;
956}
957
958SkString SampleWindow::getSampleTitle(int i) {
959 SkView* view = fSamples[i]();
960 SkString title;
961 SkEvent evt(gTitleEvtName);
962 if (view->doQuery(&evt)) {
963 title.set(evt.findString(gTitleEvtName));
964 }
965 view->unref();
966 return title;
967}
968
969int SampleWindow::sampleCount() {
970 return fSamples.count();
971}
972
Scroggo2c8208f2011-06-15 16:49:08 +0000973void SampleWindow::postAnimatingEvent() {
974 if (fAnimating) {
975 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
976 evt->post(this->getSinkID(), ANIMATING_DELAY);
977 }
978}
979
reed@android.com8a1c16f2008-12-17 15:59:43 +0000980bool SampleWindow::onEvent(const SkEvent& evt) {
981 if (evt.isType(ANIMATING_EVENTTYPE)) {
982 if (fAnimating) {
983 this->nextSample();
984 this->postAnimatingEvent();
985 }
986 return true;
987 }
reed@android.com34245c72009-11-03 04:00:48 +0000988 if (evt.isType("set-curr-index")) {
989 fCurrIndex = evt.getFast32() % fSamples.count();
990 this->loadView(fSamples[fCurrIndex]());
991 return true;
992 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000993 if (isInvalEvent(evt)) {
994 this->inval(NULL);
995 return true;
996 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000997 return this->INHERITED::onEvent(evt);
998}
999
reed@android.comf2b98d62010-12-20 18:26:13 +00001000bool SampleWindow::onQuery(SkEvent* query) {
1001 if (query->isType("get-slide-count")) {
1002 query->setFast32(fSamples.count());
1003 return true;
1004 }
1005 if (query->isType("get-slide-title")) {
1006 SkView* view = fSamples[query->getFast32()]();
1007 SkEvent evt(gTitleEvtName);
1008 if (view->doQuery(&evt)) {
1009 query->setString("title", evt.findString(gTitleEvtName));
1010 }
1011 SkSafeUnref(view);
1012 return true;
1013 }
1014 if (query->isType("use-fast-text")) {
1015 SkEvent evt(gFastTextEvtName);
1016 return curr_view(this)->doQuery(&evt);
1017 }
1018 return this->INHERITED::onQuery(query);
1019}
1020
reed@android.com0ae6b242008-12-23 16:49:54 +00001021static void cleanup_for_filename(SkString* name) {
1022 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001023 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001024 switch (str[i]) {
1025 case ':': str[i] = '-'; break;
1026 case '/': str[i] = '-'; break;
1027 case ' ': str[i] = '_'; break;
1028 default: break;
1029 }
1030 }
1031}
reed@android.com8a1c16f2008-12-17 15:59:43 +00001032
1033bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001034 {
1035 SkView* view = curr_view(this);
1036 if (view) {
1037 SkEvent evt(gCharEvtName);
1038 evt.setFast32(uni);
1039 if (view->doQuery(&evt)) {
1040 return true;
1041 }
1042 }
1043 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001044
reed@android.com8a1c16f2008-12-17 15:59:43 +00001045 int dx = 0xFF;
1046 int dy = 0xFF;
1047
1048 switch (uni) {
1049 case '5': dx = 0; dy = 0; break;
1050 case '8': dx = 0; dy = -1; break;
1051 case '6': dx = 1; dy = 0; break;
1052 case '2': dx = 0; dy = 1; break;
1053 case '4': dx = -1; dy = 0; break;
1054 case '7': dx = -1; dy = -1; break;
1055 case '9': dx = 1; dy = -1; break;
1056 case '3': dx = 1; dy = 1; break;
1057 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001058
reed@android.com8a1c16f2008-12-17 15:59:43 +00001059 default:
1060 break;
1061 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001062
reed@android.com8a1c16f2008-12-17 15:59:43 +00001063 if (0xFF != dx && 0xFF != dy) {
1064 if ((dx | dy) == 0) {
1065 fScrollTestX = fScrollTestY = 0;
1066 } else {
1067 fScrollTestX += dx;
1068 fScrollTestY += dy;
1069 }
1070 this->inval(NULL);
1071 return true;
1072 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001073
reed@android.com0ae6b242008-12-23 16:49:54 +00001074 switch (uni) {
1075 case 'a':
Scroggo2c8208f2011-06-15 16:49:08 +00001076 this->toggleSlideshow();
reed@android.com0ae6b242008-12-23 16:49:54 +00001077 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001078 case 'b':
1079 fAAState = cycle_tristate(fAAState);
1080 this->updateTitle();
1081 this->inval(NULL);
1082 break;
1083 case 'c':
1084 fUseClip = !fUseClip;
1085 this->inval(NULL);
1086 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001087 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001088 case 'd':
1089 SkGraphics::SetFontCacheUsed(0);
1090 return true;
Scroggo8ac0d542011-06-21 14:44:57 +00001091 case 'e':
1092 this->saveToPdf();
1093 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001094 case 'f':
Scroggo2c8208f2011-06-15 16:49:08 +00001095 this->toggleFPS();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001096 break;
1097 case 'g':
1098 fRequestGrabImage = true;
1099 this->inval(NULL);
1100 break;
reed@google.com09e3baa2011-05-18 12:04:31 +00001101 case 'h':
1102 fHintingState = cycle_tristate(fHintingState);
1103 this->updateTitle();
1104 this->inval(NULL);
1105 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001106 case 'i':
1107 this->zoomIn();
1108 break;
1109 case 'l':
1110 fLCDState = cycle_tristate(fLCDState);
1111 this->updateTitle();
1112 this->inval(NULL);
1113 break;
reed@google.com176753a2011-05-17 15:32:04 +00001114 case 'n':
1115 fFilterState = cycle_tristate(fFilterState);
1116 this->updateTitle();
1117 this->inval(NULL);
1118 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001119 case 'o':
1120 this->zoomOut();
1121 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001122 case 'p':
1123 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001124 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001125 this->inval(NULL);
1126 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001127 case 'r':
1128 fRotate = !fRotate;
1129 this->inval(NULL);
1130 this->updateTitle();
1131 return true;
1132 case 's':
1133 fScale = !fScale;
1134 this->inval(NULL);
1135 this->updateTitle();
1136 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001137 case 'x':
1138 fFlipAxis ^= kFlipAxis_X;
1139 this->updateTitle();
1140 this->inval(NULL);
1141 break;
1142 case 'y':
1143 fFlipAxis ^= kFlipAxis_Y;
1144 this->updateTitle();
1145 this->inval(NULL);
1146 break;
scroggo08526c02011-03-22 14:03:21 +00001147 case 'z':
1148 this->toggleZoomer();
1149 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001150 default:
1151 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001152 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001153
reed@android.com8a1c16f2008-12-17 15:59:43 +00001154 return this->INHERITED::onHandleChar(uni);
1155}
1156
Scroggo2c8208f2011-06-15 16:49:08 +00001157void SampleWindow::toggleFPS() {
1158 fMeasureFPS = !fMeasureFPS;
1159 this->inval(NULL);
1160 this->updateTitle();
1161}
1162
1163void SampleWindow::toggleSlideshow() {
1164 fAnimating = !fAnimating;
1165 this->postAnimatingEvent();
1166 this->updateTitle();
1167}
1168
1169void SampleWindow::toggleRendering() {
1170 fCanvasType = cycle_canvastype(fCanvasType);
1171 this->updateTitle();
1172 this->inval(NULL);
1173}
1174
reed@android.com8a1c16f2008-12-17 15:59:43 +00001175#include "SkDumpCanvas.h"
1176
1177bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001178 {
1179 SkView* view = curr_view(this);
1180 if (view) {
1181 SkEvent evt(gKeyEvtName);
1182 evt.setFast32(key);
1183 if (view->doQuery(&evt)) {
1184 return true;
1185 }
1186 }
1187 }
1188
reed@android.com8a1c16f2008-12-17 15:59:43 +00001189 switch (key) {
1190 case kRight_SkKey:
1191 if (this->nextSample()) {
1192 return true;
1193 }
1194 break;
1195 case kLeft_SkKey:
Scroggo2c8208f2011-06-15 16:49:08 +00001196 toggleRendering();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001197 return true;
1198 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001199 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001200 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001201 } else {
1202 fNClip = !fNClip;
1203 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001204 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001205 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001206 return true;
1207 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001208 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001209 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001210 } else {
1211 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001212 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001213 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001214 return true;
1215 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001216 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001217 SkDebugfDumper dumper;
1218 SkDumpCanvas dc(&dumper);
1219 this->draw(&dc);
1220 } else {
1221 fRepeatDrawing = !fRepeatDrawing;
1222 if (fRepeatDrawing) {
1223 this->inval(NULL);
1224 }
1225 }
1226 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001227 case kBack_SkKey:
1228 this->loadView(NULL);
1229 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001230 default:
1231 break;
1232 }
1233 return this->INHERITED::onHandleKey(key);
1234}
1235
reed@google.com52f57e12011-03-16 12:10:02 +00001236///////////////////////////////////////////////////////////////////////////////
1237
1238static const char gGestureClickType[] = "GestureClickType";
1239
Scroggod3aed392011-06-22 13:26:56 +00001240bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
1241 void* owner) {
Scroggo0f185c22011-03-24 18:35:50 +00001242 if (Click::kMoved_State == state) {
1243 updatePointer(x, y);
1244 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001245 int w = SkScalarRound(this->width());
1246 int h = SkScalarRound(this->height());
1247
1248 // check for the resize-box
1249 if (w - x < 16 && h - y < 16) {
1250 return false; // let the OS handle the click
1251 } else {
Scroggod3aed392011-06-22 13:26:56 +00001252 return this->INHERITED::onDispatchClick(x, y, state, owner);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001253 }
1254}
1255
reed@google.com52f57e12011-03-16 12:10:02 +00001256class GestureClick : public SkView::Click {
1257public:
1258 GestureClick(SkView* target) : SkView::Click(target) {
1259 this->setType(gGestureClickType);
1260 }
1261
1262 static bool IsGesture(Click* click) {
1263 return click->isType(gGestureClickType);
1264 }
1265};
1266
1267SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1268 return new GestureClick(this);
1269}
1270
1271bool SampleWindow::onClick(Click* click) {
1272 if (GestureClick::IsGesture(click)) {
1273 float x = SkScalarToFloat(click->fCurr.fX);
1274 float y = SkScalarToFloat(click->fCurr.fY);
1275 switch (click->fState) {
1276 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00001277 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001278 break;
1279 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00001280 fGesture.touchMoved(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001281 this->inval(NULL);
1282 break;
1283 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00001284 fGesture.touchEnd(click->fOwner);
reed@google.com52f57e12011-03-16 12:10:02 +00001285 this->inval(NULL);
1286 break;
1287 }
1288 return true;
1289 }
1290 return false;
1291}
1292
1293///////////////////////////////////////////////////////////////////////////////
1294
reed@android.com8a1c16f2008-12-17 15:59:43 +00001295void SampleWindow::loadView(SkView* view) {
1296 SkView::F2BIter iter(this);
1297 SkView* prev = iter.next();
1298 if (prev) {
1299 prev->detachFromParent();
1300 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001301
reed@android.com34245c72009-11-03 04:00:48 +00001302 if (NULL == view) {
1303 view = create_overview(fSamples.count(), fSamples.begin());
1304 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001305 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001306 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001307 this->attachChildToFront(view)->unref();
1308 view->setSize(this->width(), this->height());
1309
1310 this->updateTitle();
1311}
1312
1313static const char* gConfigNames[] = {
1314 "unknown config",
1315 "A1",
1316 "A8",
1317 "Index8",
1318 "565",
1319 "4444",
1320 "8888"
1321};
1322
1323static const char* configToString(SkBitmap::Config c) {
1324 return gConfigNames[c];
1325}
1326
1327static const char* gCanvasTypePrefix[] = {
1328 "raster: ",
1329 "picture: ",
1330 "opengl: "
1331};
1332
reed@google.com569e0432011-04-05 13:07:03 +00001333static const char* trystate_str(SkTriState state,
1334 const char trueStr[], const char falseStr[]) {
1335 if (kTrue_SkTriState == state) {
1336 return trueStr;
1337 } else if (kFalse_SkTriState == state) {
1338 return falseStr;
1339 }
1340 return NULL;
1341}
1342
reed@android.com8a1c16f2008-12-17 15:59:43 +00001343void SampleWindow::updateTitle() {
1344 SkString title;
1345
1346 SkView::F2BIter iter(this);
1347 SkView* view = iter.next();
1348 SkEvent evt(gTitleEvtName);
1349 if (view->doQuery(&evt)) {
1350 title.set(evt.findString(gTitleEvtName));
1351 }
1352 if (title.size() == 0) {
1353 title.set("<unknown>");
1354 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001355
reed@android.com8a1c16f2008-12-17 15:59:43 +00001356 title.prepend(gCanvasTypePrefix[fCanvasType]);
1357
1358 title.prepend(" ");
1359 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001360
reed@android.com8a1c16f2008-12-17 15:59:43 +00001361 if (fAnimating) {
1362 title.prepend("<A> ");
1363 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001364 if (fScale) {
1365 title.prepend("<S> ");
1366 }
1367 if (fRotate) {
1368 title.prepend("<R> ");
1369 }
reed@android.come522ca52009-11-23 20:10:41 +00001370 if (fNClip) {
1371 title.prepend("<C> ");
1372 }
reed@google.com569e0432011-04-05 13:07:03 +00001373
1374 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1375 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001376 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001377 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1378 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001379
1380 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001381 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001382 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001383
1384 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001385 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001386 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001387 if (fUsePipe && SampleView::IsSampleView(view)) {
1388 title.prepend("<P> ");
1389 }
1390 if (SampleView::IsSampleView(view)) {
1391 title.prepend("! ");
1392 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001393
reed@android.com8a1c16f2008-12-17 15:59:43 +00001394 this->setTitle(title.c_str());
1395}
1396
1397void SampleWindow::onSizeChange() {
1398 this->INHERITED::onSizeChange();
1399
1400 SkView::F2BIter iter(this);
1401 SkView* view = iter.next();
1402 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001403
reed@android.com8a1c16f2008-12-17 15:59:43 +00001404 // rebuild our clippath
1405 {
1406 const SkScalar W = this->width();
1407 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001408
reed@android.com8a1c16f2008-12-17 15:59:43 +00001409 fClipPath.reset();
1410#if 0
1411 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1412 SkRect r;
1413 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1414 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1415 fClipPath.addRect(r);
1416 }
1417#else
1418 SkRect r;
1419 r.set(0, 0, W, H);
1420 fClipPath.addRect(r, SkPath::kCCW_Direction);
1421 r.set(W/4, H/4, W*3/4, H*3/4);
1422 fClipPath.addRect(r, SkPath::kCW_Direction);
1423#endif
1424 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001425
Scroggo2c8208f2011-06-15 16:49:08 +00001426 fZoomCenterX = SkScalarHalf(this->width());
1427 fZoomCenterY = SkScalarHalf(this->height());
1428
Scroggo3e7ff9f2011-06-16 15:31:26 +00001429#ifdef ANDROID
Scroggob4490c72011-06-17 13:53:05 +00001430 // FIXME: The first draw after a size change does not work on Android, so
1431 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00001432 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00001433#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001434 this->updateTitle(); // to refresh our config
1435}
1436
1437///////////////////////////////////////////////////////////////////////////////
1438
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001439static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001440static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001441static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001442
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001443bool SampleView::IsSampleView(SkView* view) {
1444 SkEvent evt(is_sample_view_tag);
1445 return view->doQuery(&evt);
1446}
1447
reed@google.comf2183392011-04-22 14:10:48 +00001448bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001449 SkEvent evt(repeat_count_tag);
1450 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001451 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001452}
1453
reed@google.com0faac1e2011-05-11 05:58:58 +00001454bool SampleView::SetUsePipe(SkView* view, bool pred) {
1455 SkEvent evt(set_use_pipe_tag);
1456 evt.setFast32(pred);
1457 return view->doEvent(evt);
1458}
1459
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001460bool SampleView::onEvent(const SkEvent& evt) {
1461 if (evt.isType(repeat_count_tag)) {
1462 fRepeatCount = evt.getFast32();
1463 return true;
1464 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001465 if (evt.isType(set_use_pipe_tag)) {
1466 fUsePipe = !!evt.getFast32();
1467 return true;
1468 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001469 return this->INHERITED::onEvent(evt);
1470}
1471
1472bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001473 if (evt->isType(is_sample_view_tag)) {
1474 return true;
1475 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001476 return this->INHERITED::onQuery(evt);
1477}
1478
reed@google.com68f456d2011-05-02 18:55:39 +00001479#ifdef TEST_GPIPE
1480 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001481
1482class SimplePC : public SkGPipeController {
1483public:
1484 SimplePC(SkCanvas* target);
1485 ~SimplePC();
1486
1487 virtual void* requestBlock(size_t minRequest, size_t* actual);
1488 virtual void notifyWritten(size_t bytes);
1489
1490private:
reed@google.com961ddb02011-05-05 14:03:48 +00001491 SkGPipeReader fReader;
1492 void* fBlock;
1493 size_t fBlockSize;
1494 size_t fBytesWritten;
1495 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001496 SkGPipeReader::Status fStatus;
1497
1498 size_t fTotalWritten;
1499};
1500
1501SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1502 fBlock = NULL;
1503 fBlockSize = fBytesWritten = 0;
1504 fStatus = SkGPipeReader::kDone_Status;
1505 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001506 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001507}
1508
1509SimplePC::~SimplePC() {
1510// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1511 sk_free(fBlock);
1512
reed@google.com0faac1e2011-05-11 05:58:58 +00001513 if (fTotalWritten) {
1514 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1515 fAtomsWritten, fStatus);
1516 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001517}
1518
1519void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1520 sk_free(fBlock);
1521
1522 fBlockSize = minRequest * 4;
1523 fBlock = sk_malloc_throw(fBlockSize);
1524 fBytesWritten = 0;
1525 *actual = fBlockSize;
1526 return fBlock;
1527}
1528
1529void SimplePC::notifyWritten(size_t bytes) {
1530 SkASSERT(fBytesWritten + bytes <= fBlockSize);
yangsu@google.com1f394212011-06-01 18:03:34 +00001531
1532#ifdef PIPE_FILE
1533 //File is open in append mode
1534 FILE* f = fopen(FILE_PATH, "ab");
1535 SkASSERT(f != NULL);
1536 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
1537 fclose(f);
1538#endif
1539
reed@google.com64e3eb22011-05-04 14:32:04 +00001540 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1541 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1542 fBytesWritten += bytes;
1543 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001544
1545 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001546}
1547
reed@google.com68f456d2011-05-02 18:55:39 +00001548#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001549
reed@google.com64e3eb22011-05-04 14:32:04 +00001550
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001551void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001552#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001553 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001554 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001555 if (fUsePipe) {
reed@google.comdde09562011-05-23 12:21:05 +00001556 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
1557// flags = 0;
1558 canvas = writer.startRecording(&controller, flags);
reed@google.com0faac1e2011-05-11 05:58:58 +00001559 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001560#endif
1561
reed@google.com81e3d7f2011-06-01 12:42:36 +00001562 this->onDrawBackground(canvas);
1563
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001564 for (int i = 0; i < fRepeatCount; i++) {
1565 SkAutoCanvasRestore acr(canvas, true);
1566 this->onDrawContent(canvas);
1567 }
1568}
1569
1570void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001571 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001572}
1573
1574///////////////////////////////////////////////////////////////////////////////
1575
reed@android.comf2b98d62010-12-20 18:26:13 +00001576template <typename T> void SkTBSort(T array[], int count) {
1577 for (int i = 1; i < count - 1; i++) {
1578 bool didSwap = false;
1579 for (int j = count - 1; j > i; --j) {
1580 if (array[j] < array[j-1]) {
1581 T tmp(array[j-1]);
1582 array[j-1] = array[j];
1583 array[j] = tmp;
1584 didSwap = true;
1585 }
1586 }
1587 if (!didSwap) {
1588 break;
1589 }
1590 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001591
reed@android.comf2b98d62010-12-20 18:26:13 +00001592 for (int k = 0; k < count - 1; k++) {
1593 SkASSERT(!(array[k+1] < array[k]));
1594 }
1595}
1596
1597#include "SkRandom.h"
1598
1599static void rand_rect(SkIRect* rect, SkRandom& rand) {
1600 int bits = 8;
1601 int shift = 32 - bits;
1602 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1603 rand.nextU() >> shift, rand.nextU() >> shift);
1604 rect->sort();
1605}
1606
1607static void dumpRect(const SkIRect& r) {
1608 SkDebugf(" { %d, %d, %d, %d },\n",
1609 r.fLeft, r.fTop,
1610 r.fRight, r.fBottom);
1611}
1612
1613static void test_rects(const SkIRect rect[], int count) {
1614 SkRegion rgn0, rgn1;
1615
1616 for (int i = 0; i < count; i++) {
1617 rgn0.op(rect[i], SkRegion::kUnion_Op);
1618 // dumpRect(rect[i]);
1619 }
1620 rgn1.setRects(rect, count);
1621
1622 if (rgn0 != rgn1) {
1623 SkDebugf("\n");
1624 for (int i = 0; i < count; i++) {
1625 dumpRect(rect[i]);
1626 }
1627 SkDebugf("\n");
1628 }
1629}
1630
1631static void test() {
1632 size_t i;
1633
1634 const SkIRect r0[] = {
1635 { 0, 0, 1, 1 },
1636 { 2, 2, 3, 3 },
1637 };
1638 const SkIRect r1[] = {
1639 { 0, 0, 1, 3 },
1640 { 1, 1, 2, 2 },
1641 { 2, 0, 3, 3 },
1642 };
1643 const SkIRect r2[] = {
1644 { 0, 0, 1, 2 },
1645 { 2, 1, 3, 3 },
1646 { 4, 0, 5, 1 },
1647 { 6, 0, 7, 4 },
1648 };
1649
1650 static const struct {
1651 const SkIRect* fRects;
1652 int fCount;
1653 } gRecs[] = {
1654 { r0, SK_ARRAY_COUNT(r0) },
1655 { r1, SK_ARRAY_COUNT(r1) },
1656 { r2, SK_ARRAY_COUNT(r2) },
1657 };
1658
1659 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1660 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1661 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001662
reed@android.comf2b98d62010-12-20 18:26:13 +00001663 SkRandom rand;
1664 for (i = 0; i < 10000; i++) {
1665 SkRegion rgn0, rgn1;
1666
1667 const int N = 8;
1668 SkIRect rect[N];
1669 for (int j = 0; j < N; j++) {
1670 rand_rect(&rect[j], rand);
1671 }
1672 test_rects(rect, N);
1673 }
1674}
1675
reed@android.com8a1c16f2008-12-17 15:59:43 +00001676SkOSWindow* create_sk_window(void* hwnd) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001677// test();
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001678 return new SampleWindow(hwnd);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001679}
1680
1681void get_preferred_size(int* x, int* y, int* width, int* height) {
1682 *x = 10;
1683 *y = 50;
1684 *width = 640;
1685 *height = 480;
1686}
1687
1688void application_init() {
1689// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001690#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001691 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001692#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001693 SkGraphics::Init();
1694 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001695}
1696
1697void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001698 SkEvent::Term();
1699 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001700}