blob: a5987f26c2df94616a9c00de923c0efa73a93bb6 [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
senorblanco@chromium.org78b82532011-06-28 19:44:03 +0000350SampleWindow::SampleWindow(void* hwnd, int argc, char** argv) : 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;
senorblanco@chromium.org78b82532011-06-28 19:44:03 +0000407 if (argc > 1) {
408 int i, count = fSamples.count();
409 for (i = 0; i < count; i++) {
410 SkString title = getSampleTitle(i);
411 if (title.equals(argv[1])) {
412 fCurrIndex = i;
413 break;
414 }
415 }
416 if (i == count) {
417 fprintf(stderr, "Unknown sample \"%s\"\n", argv[1]);
418 }
419 }
reed@android.come0f13ee2009-11-04 19:40:25 +0000420 this->loadView(fSamples[fCurrIndex]());
reed@google.comf0b5f682011-03-11 20:08:25 +0000421
Scroggob4490c72011-06-17 13:53:05 +0000422 // If another constructor set our dimensions, ensure that our
423 // onSizeChange gets called.
424 if (this->height() && this->width()) {
425 this->onSizeChange();
426 }
yangsu@google.com501775e2011-06-24 16:04:50 +0000427
428 fPDFData = NULL;
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000429#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +0000430 testpdf();
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000431#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000432}
433
434SampleWindow::~SampleWindow() {
435 delete fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000436 delete fGpuCanvas;
Scroggo8ac0d542011-06-21 14:44:57 +0000437 delete fPdfCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000438 if (NULL != fGrContext) {
439 fGrContext->unref();
440 }
Scroggo0f185c22011-03-24 18:35:50 +0000441 fTypeface->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000442}
443
reed@android.com55e76b22009-11-23 21:46:47 +0000444static SkBitmap capture_bitmap(SkCanvas* canvas) {
445 SkBitmap bm;
446 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
447 src.copyTo(&bm, src.config());
448 return bm;
449}
450
451static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
452 SkBitmap* diff) {
453 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000454
reed@android.com55e76b22009-11-23 21:46:47 +0000455 SkAutoLockPixels alp0(src);
456 SkAutoLockPixels alp1(orig);
457 for (int y = 0; y < src.height(); y++) {
458 const void* srcP = src.getAddr(0, y);
459 const void* origP = orig.getAddr(0, y);
460 size_t bytes = src.width() * src.bytesPerPixel();
461 if (memcmp(srcP, origP, bytes)) {
462 SkDebugf("---------- difference on line %d\n", y);
463 return true;
464 }
465 }
466 return false;
467}
468
Scroggo0f185c22011-03-24 18:35:50 +0000469static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
470{
471 SkColor desiredColor = paint.getColor();
472 paint.setColor(SK_ColorWHITE);
473 const char* c_str = string.c_str();
474 size_t size = string.size();
475 SkRect bounds;
476 paint.measureText(c_str, size, &bounds);
477 bounds.offset(left, top);
478 SkScalar inset = SkIntToScalar(-2);
479 bounds.inset(inset, inset);
480 canvas->drawRect(bounds, paint);
481 if (desiredColor != SK_ColorBLACK) {
482 paint.setColor(SK_ColorBLACK);
483 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
484 }
485 paint.setColor(desiredColor);
486 canvas->drawText(c_str, size, left, top, paint);
487}
488
reed@android.com44177402009-11-23 21:07:51 +0000489#define XCLIP_N 8
490#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000491
492void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000493 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000494 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000495 gAnimTime = SkTime::GetMSecs();
496
Scroggo2c8208f2011-06-15 16:49:08 +0000497 SkScalar cx = fZoomCenterX;
498 SkScalar cy = fZoomCenterY;
reed@google.com569e0432011-04-05 13:07:03 +0000499
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000500 if (fZoomLevel) {
501 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000502 SkPoint center;
503 m = canvas->getTotalMatrix();//.invert(&m);
504 m.mapXY(cx, cy, &center);
505 cx = center.fX;
506 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000507
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000508 m.setTranslate(-cx, -cy);
509 m.postScale(fZoomScale, fZoomScale);
510 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000511
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000512 canvas->concat(m);
513 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000514
reed@google.com569e0432011-04-05 13:07:03 +0000515 if (fFlipAxis) {
516 SkMatrix m;
517 m.setTranslate(cx, cy);
518 if (fFlipAxis & kFlipAxis_X) {
519 m.preScale(-SK_Scalar1, SK_Scalar1);
520 }
521 if (fFlipAxis & kFlipAxis_Y) {
522 m.preScale(SK_Scalar1, -SK_Scalar1);
523 }
524 m.preTranslate(-cx, -cy);
525 canvas->concat(m);
526 }
527
reed@google.com52f57e12011-03-16 12:10:02 +0000528 // Apply any gesture matrix
529 if (true) {
530 const SkMatrix& localM = fGesture.localM();
531 if (localM.getType() & SkMatrix::kScale_Mask) {
532 canvas->setExternalMatrix(&localM);
533 }
534 canvas->concat(localM);
535 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000536
reed@google.com52f57e12011-03-16 12:10:02 +0000537 if (fGesture.isActive()) {
538 this->inval(NULL);
539 }
540 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000541
reed@android.come522ca52009-11-23 20:10:41 +0000542 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000543 this->INHERITED::draw(canvas);
544 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000545
546 const SkScalar w = this->width();
547 const SkScalar h = this->height();
548 const SkScalar cw = w / XCLIP_N;
549 const SkScalar ch = h / YCLIP_N;
550 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000551 SkRect r;
552 r.fTop = y * ch;
553 r.fBottom = (y + 1) * ch;
554 if (y == YCLIP_N - 1) {
555 r.fBottom = h;
556 }
reed@android.come522ca52009-11-23 20:10:41 +0000557 for (int x = 0; x < XCLIP_N; x++) {
558 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000559 r.fLeft = x * cw;
560 r.fRight = (x + 1) * cw;
561 if (x == XCLIP_N - 1) {
562 r.fRight = w;
563 }
reed@android.come522ca52009-11-23 20:10:41 +0000564 canvas->clipRect(r);
565 this->INHERITED::draw(canvas);
566 }
567 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000568
reed@android.com55e76b22009-11-23 21:46:47 +0000569 SkBitmap diff;
570 if (bitmap_diff(canvas, orig, &diff)) {
571 }
reed@android.come522ca52009-11-23 20:10:41 +0000572 } else {
573 this->INHERITED::draw(canvas);
574 }
Scroggo8ac0d542011-06-21 14:44:57 +0000575 if (fShowZoomer && fCanvasType != kGPU_CanvasType && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +0000576 // In the GPU case, INHERITED::draw calls beforeChildren, which
577 // creates an SkGpuCanvas. All further draw calls are directed
578 // at that canvas, which is deleted in afterChildren (which is
579 // also called by draw), so we cannot show the zoomer here.
580 // Instead, we call it inside afterChildren.
581 showZoomer(canvas);
582 }
583}
584
585void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +0000586 int count = canvas->save();
587 canvas->resetMatrix();
588 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000589 int width = SkScalarRound(this->width());
590 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000591 if (fMouseX >= width) fMouseX = width - 1;
592 else if (fMouseX < 0) fMouseX = 0;
593 if (fMouseY >= height) fMouseY = height - 1;
594 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000595
Scroggo0f185c22011-03-24 18:35:50 +0000596 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000597 bitmap.lockPixels();
598
Scroggo0f185c22011-03-24 18:35:50 +0000599 // 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 +0000600 int zoomedWidth = (width >> 1) | 1;
601 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000602 SkIRect src;
603 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
604 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
605 SkRect dest;
606 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
607 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
608 SkPaint paint;
609 // Clear the background behind our zoomed in view
610 paint.setColor(SK_ColorWHITE);
611 canvas->drawRect(dest, paint);
612 canvas->drawBitmapRect(bitmap, &src, dest);
613 paint.setColor(SK_ColorBLACK);
614 paint.setStyle(SkPaint::kStroke_Style);
615 // Draw a border around the pixel in the middle
616 SkRect originalPixel;
617 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
618 SkMatrix matrix;
619 SkRect scalarSrc;
620 scalarSrc.set(src);
621 SkColor color = bitmap.getColor(fMouseX, fMouseY);
622 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
623 SkRect pixel;
624 matrix.mapRect(&pixel, originalPixel);
625 // TODO Perhaps measure the values and make the outline white if it's "dark"
626 if (color == SK_ColorBLACK) {
627 paint.setColor(SK_ColorWHITE);
628 }
629 canvas->drawRect(pixel, paint);
630 }
631 paint.setColor(SK_ColorBLACK);
632 // Draw a border around the destination rectangle
633 canvas->drawRect(dest, paint);
634 paint.setStyle(SkPaint::kStrokeAndFill_Style);
635 // Identify the pixel and its color on screen
636 paint.setTypeface(fTypeface);
637 paint.setAntiAlias(true);
638 SkScalar lineHeight = paint.getFontMetrics(NULL);
639 SkString string;
640 string.appendf("(%i, %i)", fMouseX, fMouseY);
641 SkScalar left = dest.fLeft + SkIntToScalar(3);
642 SkScalar i = SK_Scalar1;
643 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
644 // Alpha
645 i += SK_Scalar1;
646 string.reset();
647 string.appendf("A: %X", SkColorGetA(color));
648 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
649 // Red
650 i += SK_Scalar1;
651 string.reset();
652 string.appendf("R: %X", SkColorGetR(color));
653 paint.setColor(SK_ColorRED);
654 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
655 // Green
656 i += SK_Scalar1;
657 string.reset();
658 string.appendf("G: %X", SkColorGetG(color));
659 paint.setColor(SK_ColorGREEN);
660 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
661 // Blue
662 i += SK_Scalar1;
663 string.reset();
664 string.appendf("B: %X", SkColorGetB(color));
665 paint.setColor(SK_ColorBLUE);
666 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
667 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +0000668}
669
reed@android.com8a1c16f2008-12-17 15:59:43 +0000670void SampleWindow::onDraw(SkCanvas* canvas) {
671 if (fRepeatDrawing) {
672 this->inval(NULL);
673 }
674}
675
676#include "SkColorPriv.h"
677
678static void reverseRedAndBlue(const SkBitmap& bm) {
679 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
680 uint8_t* p = (uint8_t*)bm.getPixels();
681 uint8_t* stop = p + bm.getSize();
682 while (p < stop) {
683 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
684 unsigned scale = SkAlpha255To256(p[3]);
685 unsigned r = p[2];
686 unsigned b = p[0];
687 p[0] = SkAlphaMul(r, scale);
688 p[1] = SkAlphaMul(p[1], scale);
689 p[2] = SkAlphaMul(b, scale);
690 p += 4;
691 }
692}
693
Scroggo8ac0d542011-06-21 14:44:57 +0000694void SampleWindow::saveToPdf()
695{
696 fSaveToPdf = true;
697 this->inval(NULL);
698}
699
reed@android.com8a1c16f2008-12-17 15:59:43 +0000700SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000701 if (kGPU_CanvasType != fCanvasType) {
reed@android.com6efdc472008-12-19 18:24:35 +0000702#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000703 detachGL();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000704#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000705 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000706
Scroggo8ac0d542011-06-21 14:44:57 +0000707 if (fSaveToPdf) {
708 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
709 SkISize size = SkISize::Make(bmp.width(), bmp.height());
710 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
711 canvas->getTotalMatrix());
712 fPdfCanvas = new SkCanvas(pdfDevice);
713 pdfDevice->unref();
714 canvas = fPdfCanvas;
715 } else {
716 switch (fCanvasType) {
717 case kRaster_CanvasType:
reed@android.comf2b98d62010-12-20 18:26:13 +0000718 canvas = this->INHERITED::beforeChildren(canvas);
Scroggo8ac0d542011-06-21 14:44:57 +0000719 break;
720 case kPicture_CanvasType:
721 fPicture = new SkPicture;
722 canvas = fPicture->beginRecording(9999, 9999);
723 break;
724 case kGPU_CanvasType: {
725 if (make3DReady()) {
726 SkDevice* device = canvas->getDevice();
727 const SkBitmap& bitmap = device->accessBitmap(true);
728
729 GrRenderTarget* renderTarget;
730
731 GrPlatformSurfaceDesc desc;
732 desc.reset();
733 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
734 desc.fWidth = bitmap.width();
735 desc.fHeight = bitmap.height();
736 desc.fConfig = kRGBA_8888_GrPixelConfig;
737 desc.fStencilBits = 8;
738 GrGLint buffer;
739 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer);
740 desc.fPlatformRenderTarget = buffer;
741
742 renderTarget = static_cast<GrRenderTarget*>(
743 fGrContext->createPlatformSurface(desc));
744 fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
745 renderTarget->unref();
746
747 device = new SkGpuDevice(fGrContext, renderTarget);
748 fGpuCanvas->setDevice(device)->unref();
749
750 fGpuCanvas->concat(canvas->getTotalMatrix());
751 canvas = fGpuCanvas;
752
753 } else {
754 canvas = this->INHERITED::beforeChildren(canvas);
755 }
756 break;
reed@android.comf2b98d62010-12-20 18:26:13 +0000757 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000758 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000759 }
760
761 if (fUseClip) {
762 canvas->drawColor(0xFFFF88FF);
763 canvas->clipPath(fClipPath);
764 }
765
766 return canvas;
767}
768
769static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
770 const SkRegion& rgn) {
771 SkCanvas canvas(bm);
772 SkRegion inval(rgn);
773
774 inval.translate(r.fLeft, r.fTop);
775 canvas.clipRegion(inval);
776 canvas.drawColor(0xFFFF8080);
777}
yangsu@google.com501775e2011-06-24 16:04:50 +0000778#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +0000779void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +0000780 if (fSaveToPdf) {
781 fSaveToPdf = false;
782 if (fShowZoomer) {
783 showZoomer(fPdfCanvas);
784 }
785 SkString name;
786 name.printf("%s.pdf", this->getTitle());
787 SkPDFDocument doc;
788 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
789 doc.appendPage(device);
790#ifdef ANDROID
791 name.prepend("/sdcard/");
792#endif
yangsu@google.com501775e2011-06-24 16:04:50 +0000793
794#ifdef SK_BUILD_FOR_IOS
795 SkDynamicMemoryWStream mstream;
796 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +0000797 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +0000798#endif
Scroggo8ac0d542011-06-21 14:44:57 +0000799 SkFILEWStream stream(name.c_str());
800 if (stream.isValid()) {
801 doc.emitPDF(&stream);
802 const char* desc = "File saved from Skia SampleApp";
803 this->onPDFSaved(this->getTitle(), desc, name.c_str());
804 }
yangsu@google.com501775e2011-06-24 16:04:50 +0000805
Scroggo8ac0d542011-06-21 14:44:57 +0000806 delete fPdfCanvas;
807 fPdfCanvas = NULL;
808
809 // We took over the draw calls in order to create the PDF, so we need
810 // to redraw.
811 this->inval(NULL);
812 return;
813 }
814
reed@android.comf2b98d62010-12-20 18:26:13 +0000815 if (fRequestGrabImage) {
816 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000817
reed@android.comf2b98d62010-12-20 18:26:13 +0000818 SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
819 SkDevice* device = canvas->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000820 SkBitmap bmp;
821 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000822 static int gSampleGrabCounter;
823 SkString name;
824 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000825 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000826 SkImageEncoder::kPNG_Type, 100);
827 }
828 }
829
reed@android.com8a1c16f2008-12-17 15:59:43 +0000830 switch (fCanvasType) {
831 case kRaster_CanvasType:
832 break;
833 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000834 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000835 SkPicture* pict = new SkPicture(*fPicture);
836 fPicture->unref();
837 orig->drawPicture(*pict);
838 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000839 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000840 SkDynamicMemoryWStream ostream;
841 fPicture->serialize(&ostream);
842 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000843
reed@google.com8a85d0c2011-06-24 19:12:12 +0000844 SkAutoDataUnref data(ostream.copyToData());
845 SkMemoryStream istream(data.data(), data.size());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000846 SkPicture pict(&istream);
847 orig->drawPicture(pict);
848 } else {
849 fPicture->draw(orig);
850 fPicture->unref();
851 }
852 fPicture = NULL;
853 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000854#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000855 case kGPU_CanvasType:
Scroggoaed68d92011-06-08 14:26:00 +0000856 if (fShowZoomer && fGpuCanvas) {
Scroggo3272ba82011-05-25 20:50:42 +0000857 this->showZoomer(fGpuCanvas);
858 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000859 delete fGpuCanvas;
860 fGpuCanvas = NULL;
861 presentGL();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000862 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000863#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000864 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000865
reed@google.com17d7aec2011-04-25 14:31:44 +0000866 // Do this after presentGL and other finishing, rather than in afterChild
867 if (fMeasureFPS && fMeasureFPS_Time) {
868 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
869 this->updateTitle();
Scroggo62b65b02011-06-21 16:01:26 +0000870 this->postInvalDelay();
reed@google.com17d7aec2011-04-25 14:31:44 +0000871 }
872
873 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000874 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000875 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
876 int dx = fScrollTestX * 7;
877 int dy = fScrollTestY * 7;
878 SkIRect r;
879 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000880
reed@android.com8a1c16f2008-12-17 15:59:43 +0000881 r.set(50, 50, 50+100, 50+100);
882 bm.scrollRect(&r, dx, dy, &inval);
883 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000884 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000885}
886
reed@android.com6c5f6f22009-08-14 16:08:38 +0000887void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
888 if (fScale) {
889 SkScalar scale = SK_Scalar1 * 7 / 10;
890 SkScalar cx = this->width() / 2;
891 SkScalar cy = this->height() / 2;
892 canvas->translate(cx, cy);
893 canvas->scale(scale, scale);
894 canvas->translate(-cx, -cy);
895 }
896 if (fRotate) {
897 SkScalar cx = this->width() / 2;
898 SkScalar cy = this->height() / 2;
899 canvas->translate(cx, cy);
900 canvas->rotate(SkIntToScalar(30));
901 canvas->translate(-cx, -cy);
902 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000903
reed@google.com09e3baa2011-05-18 12:04:31 +0000904 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
905 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000906
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000907 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000908 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
909 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
910 fMeasureFPS_Time = SkTime::GetMSecs();
911 }
912 } else {
913 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000914 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000915 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000916}
917
918void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000919 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000920}
921
reed@android.com8a1c16f2008-12-17 15:59:43 +0000922static SkBitmap::Config gConfigCycle[] = {
923 SkBitmap::kNo_Config, // none -> none
924 SkBitmap::kNo_Config, // a1 -> none
925 SkBitmap::kNo_Config, // a8 -> none
926 SkBitmap::kNo_Config, // index8 -> none
927 SkBitmap::kARGB_4444_Config, // 565 -> 4444
928 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
929 SkBitmap::kRGB_565_Config // 8888 -> 565
930};
931
932static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
933 return gConfigCycle[c];
934}
935
djsollen@google.come32b5832011-06-13 16:58:40 +0000936void SampleWindow::changeZoomLevel(float delta) {
937 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000938 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000939 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
940 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000941 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000942 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
943 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000944 } else {
945 fZoomScale = SK_Scalar1;
946 }
947
djsollen@google.come32b5832011-06-13 16:58:40 +0000948 this->updateTitle();
949
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000950 this->inval(NULL);
951}
952
Scroggo2c8208f2011-06-15 16:49:08 +0000953bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +0000954 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
Scroggo2c8208f2011-06-15 16:49:08 +0000955 this->loadView(fSamples[fCurrIndex]());
956 return true;
957}
958
reed@android.com8a1c16f2008-12-17 15:59:43 +0000959bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000960 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
961 this->loadView(fSamples[fCurrIndex]());
962 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000963}
964
yangsu@google.com501775e2011-06-24 16:04:50 +0000965bool SampleWindow::goToSample(int i) {
966 fCurrIndex = (i) % fSamples.count();
967 this->loadView(fSamples[fCurrIndex]());
968 return true;
969}
970
971SkString SampleWindow::getSampleTitle(int i) {
972 SkView* view = fSamples[i]();
973 SkString title;
974 SkEvent evt(gTitleEvtName);
975 if (view->doQuery(&evt)) {
976 title.set(evt.findString(gTitleEvtName));
977 }
978 view->unref();
979 return title;
980}
981
982int SampleWindow::sampleCount() {
983 return fSamples.count();
984}
985
Scroggo2c8208f2011-06-15 16:49:08 +0000986void SampleWindow::postAnimatingEvent() {
987 if (fAnimating) {
988 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
989 evt->post(this->getSinkID(), ANIMATING_DELAY);
990 }
991}
992
reed@android.com8a1c16f2008-12-17 15:59:43 +0000993bool SampleWindow::onEvent(const SkEvent& evt) {
994 if (evt.isType(ANIMATING_EVENTTYPE)) {
995 if (fAnimating) {
996 this->nextSample();
997 this->postAnimatingEvent();
998 }
999 return true;
1000 }
reed@android.com34245c72009-11-03 04:00:48 +00001001 if (evt.isType("set-curr-index")) {
1002 fCurrIndex = evt.getFast32() % fSamples.count();
1003 this->loadView(fSamples[fCurrIndex]());
1004 return true;
1005 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001006 if (isInvalEvent(evt)) {
1007 this->inval(NULL);
1008 return true;
1009 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001010 return this->INHERITED::onEvent(evt);
1011}
1012
reed@android.comf2b98d62010-12-20 18:26:13 +00001013bool SampleWindow::onQuery(SkEvent* query) {
1014 if (query->isType("get-slide-count")) {
1015 query->setFast32(fSamples.count());
1016 return true;
1017 }
1018 if (query->isType("get-slide-title")) {
1019 SkView* view = fSamples[query->getFast32()]();
1020 SkEvent evt(gTitleEvtName);
1021 if (view->doQuery(&evt)) {
1022 query->setString("title", evt.findString(gTitleEvtName));
1023 }
1024 SkSafeUnref(view);
1025 return true;
1026 }
1027 if (query->isType("use-fast-text")) {
1028 SkEvent evt(gFastTextEvtName);
1029 return curr_view(this)->doQuery(&evt);
1030 }
1031 return this->INHERITED::onQuery(query);
1032}
1033
reed@android.com0ae6b242008-12-23 16:49:54 +00001034static void cleanup_for_filename(SkString* name) {
1035 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001036 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001037 switch (str[i]) {
1038 case ':': str[i] = '-'; break;
1039 case '/': str[i] = '-'; break;
1040 case ' ': str[i] = '_'; break;
1041 default: break;
1042 }
1043 }
1044}
reed@android.com8a1c16f2008-12-17 15:59:43 +00001045
1046bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001047 {
1048 SkView* view = curr_view(this);
1049 if (view) {
1050 SkEvent evt(gCharEvtName);
1051 evt.setFast32(uni);
1052 if (view->doQuery(&evt)) {
1053 return true;
1054 }
1055 }
1056 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001057
reed@android.com8a1c16f2008-12-17 15:59:43 +00001058 int dx = 0xFF;
1059 int dy = 0xFF;
1060
1061 switch (uni) {
1062 case '5': dx = 0; dy = 0; break;
1063 case '8': dx = 0; dy = -1; break;
1064 case '6': dx = 1; dy = 0; break;
1065 case '2': dx = 0; dy = 1; break;
1066 case '4': dx = -1; dy = 0; break;
1067 case '7': dx = -1; dy = -1; break;
1068 case '9': dx = 1; dy = -1; break;
1069 case '3': dx = 1; dy = 1; break;
1070 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001071
reed@android.com8a1c16f2008-12-17 15:59:43 +00001072 default:
1073 break;
1074 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001075
reed@android.com8a1c16f2008-12-17 15:59:43 +00001076 if (0xFF != dx && 0xFF != dy) {
1077 if ((dx | dy) == 0) {
1078 fScrollTestX = fScrollTestY = 0;
1079 } else {
1080 fScrollTestX += dx;
1081 fScrollTestY += dy;
1082 }
1083 this->inval(NULL);
1084 return true;
1085 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001086
reed@android.com0ae6b242008-12-23 16:49:54 +00001087 switch (uni) {
1088 case 'a':
Scroggo2c8208f2011-06-15 16:49:08 +00001089 this->toggleSlideshow();
reed@android.com0ae6b242008-12-23 16:49:54 +00001090 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001091 case 'b':
1092 fAAState = cycle_tristate(fAAState);
1093 this->updateTitle();
1094 this->inval(NULL);
1095 break;
1096 case 'c':
1097 fUseClip = !fUseClip;
1098 this->inval(NULL);
1099 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001100 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001101 case 'd':
1102 SkGraphics::SetFontCacheUsed(0);
1103 return true;
Scroggo8ac0d542011-06-21 14:44:57 +00001104 case 'e':
1105 this->saveToPdf();
1106 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001107 case 'f':
Scroggo2c8208f2011-06-15 16:49:08 +00001108 this->toggleFPS();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001109 break;
1110 case 'g':
1111 fRequestGrabImage = true;
1112 this->inval(NULL);
1113 break;
reed@google.com09e3baa2011-05-18 12:04:31 +00001114 case 'h':
1115 fHintingState = cycle_tristate(fHintingState);
1116 this->updateTitle();
1117 this->inval(NULL);
1118 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001119 case 'i':
1120 this->zoomIn();
1121 break;
1122 case 'l':
1123 fLCDState = cycle_tristate(fLCDState);
1124 this->updateTitle();
1125 this->inval(NULL);
1126 break;
reed@google.com176753a2011-05-17 15:32:04 +00001127 case 'n':
1128 fFilterState = cycle_tristate(fFilterState);
1129 this->updateTitle();
1130 this->inval(NULL);
1131 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001132 case 'o':
1133 this->zoomOut();
1134 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001135 case 'p':
1136 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001137 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001138 this->inval(NULL);
1139 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001140 case 'r':
1141 fRotate = !fRotate;
1142 this->inval(NULL);
1143 this->updateTitle();
1144 return true;
1145 case 's':
1146 fScale = !fScale;
1147 this->inval(NULL);
1148 this->updateTitle();
1149 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001150 case 'x':
1151 fFlipAxis ^= kFlipAxis_X;
1152 this->updateTitle();
1153 this->inval(NULL);
1154 break;
1155 case 'y':
1156 fFlipAxis ^= kFlipAxis_Y;
1157 this->updateTitle();
1158 this->inval(NULL);
1159 break;
scroggo08526c02011-03-22 14:03:21 +00001160 case 'z':
1161 this->toggleZoomer();
1162 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001163 default:
1164 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001165 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001166
reed@android.com8a1c16f2008-12-17 15:59:43 +00001167 return this->INHERITED::onHandleChar(uni);
1168}
1169
Scroggo2c8208f2011-06-15 16:49:08 +00001170void SampleWindow::toggleFPS() {
1171 fMeasureFPS = !fMeasureFPS;
1172 this->inval(NULL);
1173 this->updateTitle();
1174}
1175
1176void SampleWindow::toggleSlideshow() {
1177 fAnimating = !fAnimating;
1178 this->postAnimatingEvent();
1179 this->updateTitle();
1180}
1181
1182void SampleWindow::toggleRendering() {
1183 fCanvasType = cycle_canvastype(fCanvasType);
1184 this->updateTitle();
1185 this->inval(NULL);
1186}
1187
reed@android.com8a1c16f2008-12-17 15:59:43 +00001188#include "SkDumpCanvas.h"
1189
1190bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001191 {
1192 SkView* view = curr_view(this);
1193 if (view) {
1194 SkEvent evt(gKeyEvtName);
1195 evt.setFast32(key);
1196 if (view->doQuery(&evt)) {
1197 return true;
1198 }
1199 }
1200 }
1201
reed@android.com8a1c16f2008-12-17 15:59:43 +00001202 switch (key) {
1203 case kRight_SkKey:
1204 if (this->nextSample()) {
1205 return true;
1206 }
1207 break;
1208 case kLeft_SkKey:
Scroggo2c8208f2011-06-15 16:49:08 +00001209 toggleRendering();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001210 return true;
1211 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001212 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001213 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001214 } else {
1215 fNClip = !fNClip;
1216 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001217 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001218 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001219 return true;
1220 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001221 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001222 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001223 } else {
1224 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001225 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001226 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001227 return true;
1228 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001229 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001230 SkDebugfDumper dumper;
1231 SkDumpCanvas dc(&dumper);
1232 this->draw(&dc);
1233 } else {
1234 fRepeatDrawing = !fRepeatDrawing;
1235 if (fRepeatDrawing) {
1236 this->inval(NULL);
1237 }
1238 }
1239 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001240 case kBack_SkKey:
1241 this->loadView(NULL);
1242 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001243 default:
1244 break;
1245 }
1246 return this->INHERITED::onHandleKey(key);
1247}
1248
reed@google.com52f57e12011-03-16 12:10:02 +00001249///////////////////////////////////////////////////////////////////////////////
1250
1251static const char gGestureClickType[] = "GestureClickType";
1252
Scroggod3aed392011-06-22 13:26:56 +00001253bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
1254 void* owner) {
Scroggo0f185c22011-03-24 18:35:50 +00001255 if (Click::kMoved_State == state) {
1256 updatePointer(x, y);
1257 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001258 int w = SkScalarRound(this->width());
1259 int h = SkScalarRound(this->height());
1260
1261 // check for the resize-box
1262 if (w - x < 16 && h - y < 16) {
1263 return false; // let the OS handle the click
1264 } else {
Scroggod3aed392011-06-22 13:26:56 +00001265 return this->INHERITED::onDispatchClick(x, y, state, owner);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001266 }
1267}
1268
reed@google.com52f57e12011-03-16 12:10:02 +00001269class GestureClick : public SkView::Click {
1270public:
1271 GestureClick(SkView* target) : SkView::Click(target) {
1272 this->setType(gGestureClickType);
1273 }
1274
1275 static bool IsGesture(Click* click) {
1276 return click->isType(gGestureClickType);
1277 }
1278};
1279
1280SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1281 return new GestureClick(this);
1282}
1283
1284bool SampleWindow::onClick(Click* click) {
1285 if (GestureClick::IsGesture(click)) {
1286 float x = SkScalarToFloat(click->fCurr.fX);
1287 float y = SkScalarToFloat(click->fCurr.fY);
1288 switch (click->fState) {
1289 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00001290 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001291 break;
1292 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00001293 fGesture.touchMoved(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001294 this->inval(NULL);
1295 break;
1296 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00001297 fGesture.touchEnd(click->fOwner);
reed@google.com52f57e12011-03-16 12:10:02 +00001298 this->inval(NULL);
1299 break;
1300 }
1301 return true;
1302 }
1303 return false;
1304}
1305
1306///////////////////////////////////////////////////////////////////////////////
1307
reed@android.com8a1c16f2008-12-17 15:59:43 +00001308void SampleWindow::loadView(SkView* view) {
1309 SkView::F2BIter iter(this);
1310 SkView* prev = iter.next();
1311 if (prev) {
1312 prev->detachFromParent();
1313 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001314
reed@android.com34245c72009-11-03 04:00:48 +00001315 if (NULL == view) {
1316 view = create_overview(fSamples.count(), fSamples.begin());
1317 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001318 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001319 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001320 this->attachChildToFront(view)->unref();
1321 view->setSize(this->width(), this->height());
1322
1323 this->updateTitle();
1324}
1325
1326static const char* gConfigNames[] = {
1327 "unknown config",
1328 "A1",
1329 "A8",
1330 "Index8",
1331 "565",
1332 "4444",
1333 "8888"
1334};
1335
1336static const char* configToString(SkBitmap::Config c) {
1337 return gConfigNames[c];
1338}
1339
1340static const char* gCanvasTypePrefix[] = {
1341 "raster: ",
1342 "picture: ",
1343 "opengl: "
1344};
1345
reed@google.com569e0432011-04-05 13:07:03 +00001346static const char* trystate_str(SkTriState state,
1347 const char trueStr[], const char falseStr[]) {
1348 if (kTrue_SkTriState == state) {
1349 return trueStr;
1350 } else if (kFalse_SkTriState == state) {
1351 return falseStr;
1352 }
1353 return NULL;
1354}
1355
reed@android.com8a1c16f2008-12-17 15:59:43 +00001356void SampleWindow::updateTitle() {
1357 SkString title;
1358
1359 SkView::F2BIter iter(this);
1360 SkView* view = iter.next();
1361 SkEvent evt(gTitleEvtName);
1362 if (view->doQuery(&evt)) {
1363 title.set(evt.findString(gTitleEvtName));
1364 }
1365 if (title.size() == 0) {
1366 title.set("<unknown>");
1367 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001368
reed@android.com8a1c16f2008-12-17 15:59:43 +00001369 title.prepend(gCanvasTypePrefix[fCanvasType]);
1370
1371 title.prepend(" ");
1372 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001373
reed@android.com8a1c16f2008-12-17 15:59:43 +00001374 if (fAnimating) {
1375 title.prepend("<A> ");
1376 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001377 if (fScale) {
1378 title.prepend("<S> ");
1379 }
1380 if (fRotate) {
1381 title.prepend("<R> ");
1382 }
reed@android.come522ca52009-11-23 20:10:41 +00001383 if (fNClip) {
1384 title.prepend("<C> ");
1385 }
reed@google.com569e0432011-04-05 13:07:03 +00001386
1387 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1388 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001389 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001390 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1391 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001392
1393 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001394 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001395 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001396
1397 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001398 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001399 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001400 if (fUsePipe && SampleView::IsSampleView(view)) {
1401 title.prepend("<P> ");
1402 }
1403 if (SampleView::IsSampleView(view)) {
1404 title.prepend("! ");
1405 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001406
reed@android.com8a1c16f2008-12-17 15:59:43 +00001407 this->setTitle(title.c_str());
1408}
1409
1410void SampleWindow::onSizeChange() {
1411 this->INHERITED::onSizeChange();
1412
1413 SkView::F2BIter iter(this);
1414 SkView* view = iter.next();
1415 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001416
reed@android.com8a1c16f2008-12-17 15:59:43 +00001417 // rebuild our clippath
1418 {
1419 const SkScalar W = this->width();
1420 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001421
reed@android.com8a1c16f2008-12-17 15:59:43 +00001422 fClipPath.reset();
1423#if 0
1424 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1425 SkRect r;
1426 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1427 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1428 fClipPath.addRect(r);
1429 }
1430#else
1431 SkRect r;
1432 r.set(0, 0, W, H);
1433 fClipPath.addRect(r, SkPath::kCCW_Direction);
1434 r.set(W/4, H/4, W*3/4, H*3/4);
1435 fClipPath.addRect(r, SkPath::kCW_Direction);
1436#endif
1437 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001438
Scroggo2c8208f2011-06-15 16:49:08 +00001439 fZoomCenterX = SkScalarHalf(this->width());
1440 fZoomCenterY = SkScalarHalf(this->height());
1441
Scroggo3e7ff9f2011-06-16 15:31:26 +00001442#ifdef ANDROID
Scroggob4490c72011-06-17 13:53:05 +00001443 // FIXME: The first draw after a size change does not work on Android, so
1444 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00001445 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00001446#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001447 this->updateTitle(); // to refresh our config
1448}
1449
1450///////////////////////////////////////////////////////////////////////////////
1451
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001452static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001453static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001454static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001455
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001456bool SampleView::IsSampleView(SkView* view) {
1457 SkEvent evt(is_sample_view_tag);
1458 return view->doQuery(&evt);
1459}
1460
reed@google.comf2183392011-04-22 14:10:48 +00001461bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001462 SkEvent evt(repeat_count_tag);
1463 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001464 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001465}
1466
reed@google.com0faac1e2011-05-11 05:58:58 +00001467bool SampleView::SetUsePipe(SkView* view, bool pred) {
1468 SkEvent evt(set_use_pipe_tag);
1469 evt.setFast32(pred);
1470 return view->doEvent(evt);
1471}
1472
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001473bool SampleView::onEvent(const SkEvent& evt) {
1474 if (evt.isType(repeat_count_tag)) {
1475 fRepeatCount = evt.getFast32();
1476 return true;
1477 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001478 if (evt.isType(set_use_pipe_tag)) {
1479 fUsePipe = !!evt.getFast32();
1480 return true;
1481 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001482 return this->INHERITED::onEvent(evt);
1483}
1484
1485bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001486 if (evt->isType(is_sample_view_tag)) {
1487 return true;
1488 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001489 return this->INHERITED::onQuery(evt);
1490}
1491
reed@google.com68f456d2011-05-02 18:55:39 +00001492#ifdef TEST_GPIPE
1493 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001494
1495class SimplePC : public SkGPipeController {
1496public:
1497 SimplePC(SkCanvas* target);
1498 ~SimplePC();
1499
1500 virtual void* requestBlock(size_t minRequest, size_t* actual);
1501 virtual void notifyWritten(size_t bytes);
1502
1503private:
reed@google.com961ddb02011-05-05 14:03:48 +00001504 SkGPipeReader fReader;
1505 void* fBlock;
1506 size_t fBlockSize;
1507 size_t fBytesWritten;
1508 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001509 SkGPipeReader::Status fStatus;
1510
1511 size_t fTotalWritten;
1512};
1513
1514SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1515 fBlock = NULL;
1516 fBlockSize = fBytesWritten = 0;
1517 fStatus = SkGPipeReader::kDone_Status;
1518 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001519 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001520}
1521
1522SimplePC::~SimplePC() {
1523// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1524 sk_free(fBlock);
1525
reed@google.com0faac1e2011-05-11 05:58:58 +00001526 if (fTotalWritten) {
1527 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1528 fAtomsWritten, fStatus);
1529 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001530}
1531
1532void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1533 sk_free(fBlock);
1534
1535 fBlockSize = minRequest * 4;
1536 fBlock = sk_malloc_throw(fBlockSize);
1537 fBytesWritten = 0;
1538 *actual = fBlockSize;
1539 return fBlock;
1540}
1541
1542void SimplePC::notifyWritten(size_t bytes) {
1543 SkASSERT(fBytesWritten + bytes <= fBlockSize);
yangsu@google.com1f394212011-06-01 18:03:34 +00001544
1545#ifdef PIPE_FILE
1546 //File is open in append mode
1547 FILE* f = fopen(FILE_PATH, "ab");
1548 SkASSERT(f != NULL);
1549 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
1550 fclose(f);
1551#endif
1552
reed@google.com64e3eb22011-05-04 14:32:04 +00001553 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1554 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1555 fBytesWritten += bytes;
1556 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001557
1558 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001559}
1560
reed@google.com68f456d2011-05-02 18:55:39 +00001561#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001562
reed@google.com64e3eb22011-05-04 14:32:04 +00001563
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001564void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001565#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001566 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001567 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001568 if (fUsePipe) {
reed@google.comdde09562011-05-23 12:21:05 +00001569 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
1570// flags = 0;
1571 canvas = writer.startRecording(&controller, flags);
reed@google.com0faac1e2011-05-11 05:58:58 +00001572 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001573#endif
1574
reed@google.com81e3d7f2011-06-01 12:42:36 +00001575 this->onDrawBackground(canvas);
1576
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001577 for (int i = 0; i < fRepeatCount; i++) {
1578 SkAutoCanvasRestore acr(canvas, true);
1579 this->onDrawContent(canvas);
1580 }
1581}
1582
1583void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001584 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001585}
1586
1587///////////////////////////////////////////////////////////////////////////////
1588
reed@android.comf2b98d62010-12-20 18:26:13 +00001589template <typename T> void SkTBSort(T array[], int count) {
1590 for (int i = 1; i < count - 1; i++) {
1591 bool didSwap = false;
1592 for (int j = count - 1; j > i; --j) {
1593 if (array[j] < array[j-1]) {
1594 T tmp(array[j-1]);
1595 array[j-1] = array[j];
1596 array[j] = tmp;
1597 didSwap = true;
1598 }
1599 }
1600 if (!didSwap) {
1601 break;
1602 }
1603 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001604
reed@android.comf2b98d62010-12-20 18:26:13 +00001605 for (int k = 0; k < count - 1; k++) {
1606 SkASSERT(!(array[k+1] < array[k]));
1607 }
1608}
1609
1610#include "SkRandom.h"
1611
1612static void rand_rect(SkIRect* rect, SkRandom& rand) {
1613 int bits = 8;
1614 int shift = 32 - bits;
1615 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1616 rand.nextU() >> shift, rand.nextU() >> shift);
1617 rect->sort();
1618}
1619
1620static void dumpRect(const SkIRect& r) {
1621 SkDebugf(" { %d, %d, %d, %d },\n",
1622 r.fLeft, r.fTop,
1623 r.fRight, r.fBottom);
1624}
1625
1626static void test_rects(const SkIRect rect[], int count) {
1627 SkRegion rgn0, rgn1;
1628
1629 for (int i = 0; i < count; i++) {
1630 rgn0.op(rect[i], SkRegion::kUnion_Op);
1631 // dumpRect(rect[i]);
1632 }
1633 rgn1.setRects(rect, count);
1634
1635 if (rgn0 != rgn1) {
1636 SkDebugf("\n");
1637 for (int i = 0; i < count; i++) {
1638 dumpRect(rect[i]);
1639 }
1640 SkDebugf("\n");
1641 }
1642}
1643
1644static void test() {
1645 size_t i;
1646
1647 const SkIRect r0[] = {
1648 { 0, 0, 1, 1 },
1649 { 2, 2, 3, 3 },
1650 };
1651 const SkIRect r1[] = {
1652 { 0, 0, 1, 3 },
1653 { 1, 1, 2, 2 },
1654 { 2, 0, 3, 3 },
1655 };
1656 const SkIRect r2[] = {
1657 { 0, 0, 1, 2 },
1658 { 2, 1, 3, 3 },
1659 { 4, 0, 5, 1 },
1660 { 6, 0, 7, 4 },
1661 };
1662
1663 static const struct {
1664 const SkIRect* fRects;
1665 int fCount;
1666 } gRecs[] = {
1667 { r0, SK_ARRAY_COUNT(r0) },
1668 { r1, SK_ARRAY_COUNT(r1) },
1669 { r2, SK_ARRAY_COUNT(r2) },
1670 };
1671
1672 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1673 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1674 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001675
reed@android.comf2b98d62010-12-20 18:26:13 +00001676 SkRandom rand;
1677 for (i = 0; i < 10000; i++) {
1678 SkRegion rgn0, rgn1;
1679
1680 const int N = 8;
1681 SkIRect rect[N];
1682 for (int j = 0; j < N; j++) {
1683 rand_rect(&rect[j], rand);
1684 }
1685 test_rects(rect, N);
1686 }
1687}
1688
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00001689SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001690// test();
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00001691 return new SampleWindow(hwnd, argc, argv);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001692}
1693
1694void get_preferred_size(int* x, int* y, int* width, int* height) {
1695 *x = 10;
1696 *y = 50;
1697 *width = 640;
1698 *height = 480;
1699}
1700
1701void application_init() {
1702// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001703#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001704 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001705#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001706 SkGraphics::Init();
1707 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001708}
1709
1710void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001711 SkEvent::Term();
1712 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001713}