blob: 258da06e327e884692d66527427460b43d7e4038 [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.comaf951c92011-06-16 19:10:39 +00006#include "SkGpuDevice.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00007#include "SkGraphics.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +00008#include "SkImageEncoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SkPaint.h"
10#include "SkPicture.h"
11#include "SkStream.h"
reed@android.com44177402009-11-23 21:07:51 +000012#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkWindow.h"
14
15#include "SampleCode.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016#include "GrContext.h"
Scroggo0f185c22011-03-24 18:35:50 +000017#include "SkTypeface.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000018
Scroggo3e7ff9f2011-06-16 15:31:26 +000019#include "GrGLInterface.h"
Scroggo2c8208f2011-06-15 16:49:08 +000020
Scroggo8ac0d542011-06-21 14:44:57 +000021#include "SkPDFDevice.h"
22#include "SkPDFDocument.h"
23#include "SkStream.h"
24
yangsu@google.com1f394212011-06-01 18:03:34 +000025#define TEST_GPIPEx
26
27#ifdef TEST_GPIPE
28#define PIPE_FILE
29#define FILE_PATH "/path/to/drawing.data"
30#endif
31
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +000032#define USE_ARROWS_FOR_ZOOM true
reed@android.comf2b98d62010-12-20 18:26:13 +000033//#define DEFAULT_TO_GPU
34
reed@android.come191b162009-12-18 21:33:39 +000035extern SkView* create_overview(int, const SkViewFactory[]);
reed@android.com34245c72009-11-03 04:00:48 +000036
reed@android.comcb342352010-07-22 18:27:53 +000037#define SK_SUPPORT_GL
reed@android.com8a1c16f2008-12-17 15:59:43 +000038
39#define ANIMATING_EVENTTYPE "nextSample"
40#define ANIMATING_DELAY 750
41
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000042#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +000043 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000044#else
reed@google.combad8c872011-05-18 20:10:31 +000045 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000046#endif
reed@google.combad8c872011-05-18 20:10:31 +000047#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000048
reed@google.comac10a2d2010-12-22 21:39:39 +000049#ifdef SK_SUPPORT_GL
50 #include "GrGLConfig.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000051#endif
52
reed@google.com3cec4d72011-07-06 13:59:47 +000053static SampleWindow* gSampleWindow;
54
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000055///////////////
56static const char view_inval_msg[] = "view-inval-msg";
57
Scroggo62b65b02011-06-21 16:01:26 +000058void SampleWindow::postInvalDelay() {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000059 SkEvent* evt = new SkEvent(view_inval_msg);
Scroggo62b65b02011-06-21 16:01:26 +000060 evt->post(this->getSinkID(), 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000061}
62
63static bool isInvalEvent(const SkEvent& evt) {
64 return evt.isType(view_inval_msg);
65}
66//////////////////
67
reed@android.com8a1c16f2008-12-17 15:59:43 +000068SkViewRegister* SkViewRegister::gHead;
69SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) {
70 static bool gOnce;
71 if (!gOnce) {
72 gHead = NULL;
73 gOnce = true;
74 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000075
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 fChain = gHead;
77 gHead = this;
78}
79
reed@android.comf2b98d62010-12-20 18:26:13 +000080#if defined(SK_SUPPORT_GL)
81 #define SK_USE_SHADERS
82#endif
83
reed@google.com29038ed2011-07-06 17:56:47 +000084#if 0
reed@google.comf0b5f682011-03-11 20:08:25 +000085#include <CoreFoundation/CoreFoundation.h>
86#include <CoreFoundation/CFURLAccess.h>
87
88static void testpdf() {
89 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
90 kCFStringEncodingUTF8);
91 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
92 kCFURLPOSIXPathStyle,
93 false);
94 CFRelease(path);
95 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
96 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
97 CFRelease(url);
98
99 CGContextBeginPage(cg, &box);
100 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
101 CGContextFillEllipseInRect(cg, r);
102 CGContextEndPage(cg);
103 CGContextRelease(cg);
104
105 if (false) {
106 SkBitmap bm;
107 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
108 bm.allocPixels();
109 bm.eraseColor(0);
110
111 SkCanvas canvas(bm);
112
113 }
114}
115#endif
116
117//////////////////////////////////////////////////////////////////////////////
118
reed@google.com569e0432011-04-05 13:07:03 +0000119enum FlipAxisEnum {
120 kFlipAxis_X = (1 << 0),
121 kFlipAxis_Y = (1 << 1)
122};
123
reed@google.com569e0432011-04-05 13:07:03 +0000124static SkTriState cycle_tristate(SkTriState state) {
125 static const SkTriState gCycle[] = {
126 /* kFalse_SkTriState -> */ kUnknown_SkTriState,
127 /* kTrue_SkTriState -> */ kFalse_SkTriState,
128 /* kUnknown_SkTriState -> */ kTrue_SkTriState,
129 };
130 return gCycle[state];
131}
132
reed@google.comf0b5f682011-03-11 20:08:25 +0000133#include "SkDrawFilter.h"
134
reed@google.com569e0432011-04-05 13:07:03 +0000135class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000136public:
reed@google.com09e3baa2011-05-18 12:04:31 +0000137 FlagsDrawFilter(SkTriState lcd, SkTriState aa, SkTriState filter,
138 SkTriState hinting) :
139 fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000140
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000141 virtual void filter(SkPaint* paint, Type t) {
reed@google.com569e0432011-04-05 13:07:03 +0000142 if (kText_Type == t && kUnknown_SkTriState != fLCDState) {
reed@google.com569e0432011-04-05 13:07:03 +0000143 paint->setLCDRenderText(kTrue_SkTriState == fLCDState);
144 }
145 if (kUnknown_SkTriState != fAAState) {
reed@google.com569e0432011-04-05 13:07:03 +0000146 paint->setAntiAlias(kTrue_SkTriState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000147 }
reed@google.com176753a2011-05-17 15:32:04 +0000148 if (kUnknown_SkTriState != fFilterState) {
149 paint->setFilterBitmap(kTrue_SkTriState == fFilterState);
150 }
reed@google.com09e3baa2011-05-18 12:04:31 +0000151 if (kUnknown_SkTriState != fHintingState) {
152 paint->setHinting(kTrue_SkTriState == fHintingState ?
153 SkPaint::kNormal_Hinting :
154 SkPaint::kSlight_Hinting);
155 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000156 }
157
158private:
reed@google.com569e0432011-04-05 13:07:03 +0000159 SkTriState fLCDState;
reed@google.com569e0432011-04-05 13:07:03 +0000160 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000161 SkTriState fFilterState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000162 SkTriState fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000163};
164
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165//////////////////////////////////////////////////////////////////////////////
166
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000167#define MAX_ZOOM_LEVEL 8
168#define MIN_ZOOM_LEVEL -8
169
reed@android.comf2b98d62010-12-20 18:26:13 +0000170static const char gCharEvtName[] = "SampleCode_Char_Event";
171static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172static const char gTitleEvtName[] = "SampleCode_Title_Event";
173static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000174static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
175
176bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
177 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
178 if (outUni) {
179 *outUni = evt.getFast32();
180 }
181 return true;
182 }
183 return false;
184}
185
186bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
187 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
188 if (outKey) {
189 *outKey = (SkKey)evt.getFast32();
190 }
191 return true;
192 }
193 return false;
194}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195
196bool SampleCode::TitleQ(const SkEvent& evt) {
197 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
198}
199
200void SampleCode::TitleR(SkEvent* evt, const char title[]) {
201 SkASSERT(evt && TitleQ(*evt));
202 evt->setString(gTitleEvtName, title);
203}
204
205bool SampleCode::PrefSizeQ(const SkEvent& evt) {
206 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
207}
208
209void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
210 SkASSERT(evt && PrefSizeQ(*evt));
211 SkScalar size[2];
212 size[0] = width;
213 size[1] = height;
214 evt->setScalars(gPrefSizeEvtName, 2, size);
215}
216
reed@android.comf2b98d62010-12-20 18:26:13 +0000217bool SampleCode::FastTextQ(const SkEvent& evt) {
218 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
219}
220
221///////////////////////////////////////////////////////////////////////////////
222
reed@android.com44177402009-11-23 21:07:51 +0000223static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000224static SkMSec gAnimTimePrev;
225
reed@android.com44177402009-11-23 21:07:51 +0000226SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000227SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
228SkScalar SampleCode::GetAnimSecondsDelta() {
229 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
230}
reed@android.com44177402009-11-23 21:07:51 +0000231
232SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000233 // since gAnimTime can be up to 32 bits, we can't convert it to a float
234 // or we'll lose the low bits. Hence we use doubles for the intermediate
235 // calculations
236 double seconds = (double)gAnimTime / 1000.0;
237 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000238 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000239 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000240 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000241 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000242}
243
reed@google.com3cec4d72011-07-06 13:59:47 +0000244GrContext* SampleCode::GetGr() {
245 return gSampleWindow ? gSampleWindow->getGrContext() : NULL;
246}
247
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248//////////////////////////////////////////////////////////////////////////////
249
reed@android.comf2b98d62010-12-20 18:26:13 +0000250static SkView* curr_view(SkWindow* wind) {
251 SkView::F2BIter iter(wind);
252 return iter.next();
253}
254
Scroggo2c8208f2011-06-15 16:49:08 +0000255void SampleWindow::setZoomCenter(float x, float y)
256{
257 fZoomCenterX = SkFloatToScalar(x);
258 fZoomCenterY = SkFloatToScalar(y);
259}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260
djsollen@google.come32b5832011-06-13 16:58:40 +0000261bool SampleWindow::setGrContext(GrContext* context)
262{
263 if (fGrContext) {
264 fGrContext->unref();
265 }
266 fGrContext = context;
267 fGrContext->ref();
268 return true;
269}
270
271GrContext* SampleWindow::getGrContext()
272{
273 return fGrContext;
274}
djsollen@google.come32b5832011-06-13 16:58:40 +0000275
Scroggo0f185c22011-03-24 18:35:50 +0000276bool SampleWindow::zoomIn()
277{
278 // Arbitrarily decided
279 if (fFatBitsScale == 25) return false;
280 fFatBitsScale++;
281 this->inval(NULL);
282 return true;
283}
284
285bool SampleWindow::zoomOut()
286{
287 if (fFatBitsScale == 1) return false;
288 fFatBitsScale--;
289 this->inval(NULL);
290 return true;
291}
292
293void SampleWindow::toggleZoomer()
294{
295 fShowZoomer = !fShowZoomer;
296 this->inval(NULL);
297}
298
299void SampleWindow::updatePointer(int x, int y)
300{
301 fMouseX = x;
302 fMouseY = y;
303 if (fShowZoomer) {
304 this->inval(NULL);
305 }
306}
307
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000308bool SampleWindow::make3DReady() {
309
310#if defined(SK_SUPPORT_GL)
bsalomon@google.com498a6232011-03-10 18:24:15 +0000311 if (attachGL()) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000312 if (NULL != fGrContext) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000313 // various gr lifecycle tests
314 #if 0
315 fGrContext->freeGpuResources();
316 #elif 0
317 // this will leak resources.
318 fGrContext->contextLost();
319 #elif 0
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000320 GrAssert(1 == fGrContext->refcnt());
321 fGrContext->unref();
322 fGrContext = NULL;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000323 #endif
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000324 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000325
bsalomon@google.com498a6232011-03-10 18:24:15 +0000326 if (NULL == fGrContext) {
327 #if defined(SK_USE_SHADERS)
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000328 fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000329 #else
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000330 fGrContext = GrContext::Create(kOpenGL_Fixed_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000331 #endif
reed@google.com569e0432011-04-05 13:07:03 +0000332 SkDebugf("---- constructor\n");
bsalomon@google.com498a6232011-03-10 18:24:15 +0000333 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000334
bsalomon@google.com498a6232011-03-10 18:24:15 +0000335 if (NULL != fGrContext) {
336 return true;
337 } else {
338 detachGL();
339 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000340 }
341#endif
342 SkDebugf("Failed to setup 3D");
343 return false;
344}
345
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) {
347 static const CanvasType gCT[] = {
348 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000349 kGPU_CanvasType,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000350 kRaster_CanvasType
351 };
352 return gCT[ct];
353}
354
senorblanco@chromium.org78b82532011-06-28 19:44:03 +0000355SampleWindow::SampleWindow(void* hwnd, int argc, char** argv) : INHERITED(hwnd) {
reed@google.com3cec4d72011-07-06 13:59:47 +0000356 gSampleWindow = this;
357
yangsu@google.com1f394212011-06-01 18:03:34 +0000358#ifdef PIPE_FILE
359 //Clear existing file or create file if it doesn't exist
360 FILE* f = fopen(FILE_PATH, "wb");
361 fclose(f);
362#endif
363
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364 fPicture = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000366 fGrContext = NULL;
reed@google.com29038ed2011-07-06 17:56:47 +0000367 fGrRenderTarget = NULL;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000368
reed@android.comf2b98d62010-12-20 18:26:13 +0000369#ifdef DEFAULT_TO_GPU
370 fCanvasType = kGPU_CanvasType;
371#else
reed@android.com8a1c16f2008-12-17 15:59:43 +0000372 fCanvasType = kRaster_CanvasType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000373#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000374 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000375 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000376 fRepeatDrawing = false;
377 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000378 fRotate = false;
379 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000380 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000381 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000382 fMeasureFPS = false;
reed@google.com569e0432011-04-05 13:07:03 +0000383 fLCDState = kUnknown_SkTriState;
384 fAAState = kUnknown_SkTriState;
reed@google.com66f22fd2011-05-17 15:33:45 +0000385 fFilterState = kUnknown_SkTriState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000386 fHintingState = kUnknown_SkTriState;
reed@google.com569e0432011-04-05 13:07:03 +0000387 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000388 fScrollTestX = fScrollTestY = 0;
389
Scroggo0f185c22011-03-24 18:35:50 +0000390 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000391 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000392 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
393 fShowZoomer = false;
394
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000395 fZoomLevel = 0;
396 fZoomScale = SK_Scalar1;
397
Scroggo8ac0d542011-06-21 14:44:57 +0000398 fSaveToPdf = false;
399 fPdfCanvas = NULL;
400
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000401// this->setConfig(SkBitmap::kRGB_565_Config);
402 this->setConfig(SkBitmap::kARGB_8888_Config);
403 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000404 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000405
reed@android.com34245c72009-11-03 04:00:48 +0000406 {
407 const SkViewRegister* reg = SkViewRegister::Head();
408 while (reg) {
409 *fSamples.append() = reg->factory();
410 reg = reg->next();
411 }
412 }
413 fCurrIndex = 0;
senorblanco@chromium.org78b82532011-06-28 19:44:03 +0000414 if (argc > 1) {
415 int i, count = fSamples.count();
416 for (i = 0; i < count; i++) {
417 SkString title = getSampleTitle(i);
418 if (title.equals(argv[1])) {
419 fCurrIndex = i;
420 break;
421 }
422 }
423 if (i == count) {
424 fprintf(stderr, "Unknown sample \"%s\"\n", argv[1]);
425 }
426 }
reed@android.come0f13ee2009-11-04 19:40:25 +0000427 this->loadView(fSamples[fCurrIndex]());
bsalomon@google.com840e9f32011-07-06 21:59:09 +0000428
429 fPDFData = NULL;
reed@google.comf0b5f682011-03-11 20:08:25 +0000430
bsalomon@google.com840e9f32011-07-06 21:59:09 +0000431 this->make3DReady();
Scroggob4490c72011-06-17 13:53:05 +0000432 // If another constructor set our dimensions, ensure that our
433 // onSizeChange gets called.
434 if (this->height() && this->width()) {
435 this->onSizeChange();
436 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000437}
438
439SampleWindow::~SampleWindow() {
440 delete fPicture;
Scroggo8ac0d542011-06-21 14:44:57 +0000441 delete fPdfCanvas;
Scroggo0f185c22011-03-24 18:35:50 +0000442 fTypeface->unref();
reed@google.com29038ed2011-07-06 17:56:47 +0000443
444 SkSafeUnref(fGrRenderTarget);
445 SkSafeUnref(fGrContext);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000446}
447
reed@android.com55e76b22009-11-23 21:46:47 +0000448static SkBitmap capture_bitmap(SkCanvas* canvas) {
449 SkBitmap bm;
450 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
451 src.copyTo(&bm, src.config());
452 return bm;
453}
454
455static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
456 SkBitmap* diff) {
457 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000458
reed@android.com55e76b22009-11-23 21:46:47 +0000459 SkAutoLockPixels alp0(src);
460 SkAutoLockPixels alp1(orig);
461 for (int y = 0; y < src.height(); y++) {
462 const void* srcP = src.getAddr(0, y);
463 const void* origP = orig.getAddr(0, y);
464 size_t bytes = src.width() * src.bytesPerPixel();
465 if (memcmp(srcP, origP, bytes)) {
466 SkDebugf("---------- difference on line %d\n", y);
467 return true;
468 }
469 }
470 return false;
471}
472
Scroggo0f185c22011-03-24 18:35:50 +0000473static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
474{
475 SkColor desiredColor = paint.getColor();
476 paint.setColor(SK_ColorWHITE);
477 const char* c_str = string.c_str();
478 size_t size = string.size();
479 SkRect bounds;
480 paint.measureText(c_str, size, &bounds);
481 bounds.offset(left, top);
482 SkScalar inset = SkIntToScalar(-2);
483 bounds.inset(inset, inset);
484 canvas->drawRect(bounds, paint);
485 if (desiredColor != SK_ColorBLACK) {
486 paint.setColor(SK_ColorBLACK);
487 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
488 }
489 paint.setColor(desiredColor);
490 canvas->drawText(c_str, size, left, top, paint);
491}
492
reed@android.com44177402009-11-23 21:07:51 +0000493#define XCLIP_N 8
494#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000495
496void SampleWindow::draw(SkCanvas* canvas) {
reed@google.com29038ed2011-07-06 17:56:47 +0000497 if (fGrContext && (kGPU_CanvasType == fCanvasType)) {
498 canvas->setDevice(new SkGpuDevice(fGrContext,
499 fGrRenderTarget))->unref();
500 }
501
reed@android.com44177402009-11-23 21:07:51 +0000502 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000503 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000504 gAnimTime = SkTime::GetMSecs();
505
Scroggo2c8208f2011-06-15 16:49:08 +0000506 SkScalar cx = fZoomCenterX;
507 SkScalar cy = fZoomCenterY;
reed@google.com569e0432011-04-05 13:07:03 +0000508
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000509 if (fZoomLevel) {
510 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000511 SkPoint center;
512 m = canvas->getTotalMatrix();//.invert(&m);
513 m.mapXY(cx, cy, &center);
514 cx = center.fX;
515 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000516
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000517 m.setTranslate(-cx, -cy);
518 m.postScale(fZoomScale, fZoomScale);
519 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000520
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000521 canvas->concat(m);
522 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000523
reed@google.com569e0432011-04-05 13:07:03 +0000524 if (fFlipAxis) {
525 SkMatrix m;
526 m.setTranslate(cx, cy);
527 if (fFlipAxis & kFlipAxis_X) {
528 m.preScale(-SK_Scalar1, SK_Scalar1);
529 }
530 if (fFlipAxis & kFlipAxis_Y) {
531 m.preScale(SK_Scalar1, -SK_Scalar1);
532 }
533 m.preTranslate(-cx, -cy);
534 canvas->concat(m);
535 }
536
reed@google.com52f57e12011-03-16 12:10:02 +0000537 // Apply any gesture matrix
538 if (true) {
539 const SkMatrix& localM = fGesture.localM();
540 if (localM.getType() & SkMatrix::kScale_Mask) {
541 canvas->setExternalMatrix(&localM);
542 }
543 canvas->concat(localM);
544 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000545
reed@google.com52f57e12011-03-16 12:10:02 +0000546 if (fGesture.isActive()) {
547 this->inval(NULL);
548 }
549 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000550
reed@android.come522ca52009-11-23 20:10:41 +0000551 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000552 this->INHERITED::draw(canvas);
553 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000554
555 const SkScalar w = this->width();
556 const SkScalar h = this->height();
557 const SkScalar cw = w / XCLIP_N;
558 const SkScalar ch = h / YCLIP_N;
559 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000560 SkRect r;
561 r.fTop = y * ch;
562 r.fBottom = (y + 1) * ch;
563 if (y == YCLIP_N - 1) {
564 r.fBottom = h;
565 }
reed@android.come522ca52009-11-23 20:10:41 +0000566 for (int x = 0; x < XCLIP_N; x++) {
567 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000568 r.fLeft = x * cw;
569 r.fRight = (x + 1) * cw;
570 if (x == XCLIP_N - 1) {
571 r.fRight = w;
572 }
reed@android.come522ca52009-11-23 20:10:41 +0000573 canvas->clipRect(r);
574 this->INHERITED::draw(canvas);
575 }
576 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000577
reed@android.com55e76b22009-11-23 21:46:47 +0000578 SkBitmap diff;
579 if (bitmap_diff(canvas, orig, &diff)) {
580 }
reed@android.come522ca52009-11-23 20:10:41 +0000581 } else {
582 this->INHERITED::draw(canvas);
583 }
Scroggo8ac0d542011-06-21 14:44:57 +0000584 if (fShowZoomer && fCanvasType != kGPU_CanvasType && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +0000585 // In the GPU case, INHERITED::draw calls beforeChildren, which
586 // creates an SkGpuCanvas. All further draw calls are directed
587 // at that canvas, which is deleted in afterChildren (which is
588 // also called by draw), so we cannot show the zoomer here.
589 // Instead, we call it inside afterChildren.
590 showZoomer(canvas);
591 }
reed@google.com29038ed2011-07-06 17:56:47 +0000592
593 // do this last
mike@reedtribe.org6757a1a2011-07-07 01:10:34 +0000594 if (fGrContext) {
595 // in case we have queued drawing calls
596 fGrContext->flush();
597 if (fCanvasType != kGPU_CanvasType) {
598 // need to send the raster bits to the (gpu) window
599 fGrContext->setRenderTarget(fGrRenderTarget);
600 const SkBitmap& bm = this->getBitmap();
601 fGrContext->writePixels(0, 0, bm.width(), bm.height(),
602 kRGBA_8888_GrPixelConfig, bm.getPixels(),
603 bm.rowBytes());
604 }
reed@google.com29038ed2011-07-06 17:56:47 +0000605 }
606 presentGL();
Scroggo3272ba82011-05-25 20:50:42 +0000607}
608
609void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +0000610 int count = canvas->save();
611 canvas->resetMatrix();
612 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000613 int width = SkScalarRound(this->width());
614 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000615 if (fMouseX >= width) fMouseX = width - 1;
616 else if (fMouseX < 0) fMouseX = 0;
617 if (fMouseY >= height) fMouseY = height - 1;
618 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000619
Scroggo0f185c22011-03-24 18:35:50 +0000620 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000621 bitmap.lockPixels();
622
Scroggo0f185c22011-03-24 18:35:50 +0000623 // 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 +0000624 int zoomedWidth = (width >> 1) | 1;
625 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000626 SkIRect src;
627 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
628 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
629 SkRect dest;
630 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
631 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
632 SkPaint paint;
633 // Clear the background behind our zoomed in view
634 paint.setColor(SK_ColorWHITE);
635 canvas->drawRect(dest, paint);
636 canvas->drawBitmapRect(bitmap, &src, dest);
637 paint.setColor(SK_ColorBLACK);
638 paint.setStyle(SkPaint::kStroke_Style);
639 // Draw a border around the pixel in the middle
640 SkRect originalPixel;
641 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
642 SkMatrix matrix;
643 SkRect scalarSrc;
644 scalarSrc.set(src);
645 SkColor color = bitmap.getColor(fMouseX, fMouseY);
646 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
647 SkRect pixel;
648 matrix.mapRect(&pixel, originalPixel);
649 // TODO Perhaps measure the values and make the outline white if it's "dark"
650 if (color == SK_ColorBLACK) {
651 paint.setColor(SK_ColorWHITE);
652 }
653 canvas->drawRect(pixel, paint);
654 }
655 paint.setColor(SK_ColorBLACK);
656 // Draw a border around the destination rectangle
657 canvas->drawRect(dest, paint);
658 paint.setStyle(SkPaint::kStrokeAndFill_Style);
659 // Identify the pixel and its color on screen
660 paint.setTypeface(fTypeface);
661 paint.setAntiAlias(true);
662 SkScalar lineHeight = paint.getFontMetrics(NULL);
663 SkString string;
664 string.appendf("(%i, %i)", fMouseX, fMouseY);
665 SkScalar left = dest.fLeft + SkIntToScalar(3);
666 SkScalar i = SK_Scalar1;
667 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
668 // Alpha
669 i += SK_Scalar1;
670 string.reset();
671 string.appendf("A: %X", SkColorGetA(color));
672 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
673 // Red
674 i += SK_Scalar1;
675 string.reset();
676 string.appendf("R: %X", SkColorGetR(color));
677 paint.setColor(SK_ColorRED);
678 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
679 // Green
680 i += SK_Scalar1;
681 string.reset();
682 string.appendf("G: %X", SkColorGetG(color));
683 paint.setColor(SK_ColorGREEN);
684 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
685 // Blue
686 i += SK_Scalar1;
687 string.reset();
688 string.appendf("B: %X", SkColorGetB(color));
689 paint.setColor(SK_ColorBLUE);
690 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
691 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +0000692}
693
reed@android.com8a1c16f2008-12-17 15:59:43 +0000694void SampleWindow::onDraw(SkCanvas* canvas) {
695 if (fRepeatDrawing) {
696 this->inval(NULL);
697 }
698}
699
700#include "SkColorPriv.h"
701
702static void reverseRedAndBlue(const SkBitmap& bm) {
703 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
704 uint8_t* p = (uint8_t*)bm.getPixels();
705 uint8_t* stop = p + bm.getSize();
706 while (p < stop) {
707 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
708 unsigned scale = SkAlpha255To256(p[3]);
709 unsigned r = p[2];
710 unsigned b = p[0];
711 p[0] = SkAlphaMul(r, scale);
712 p[1] = SkAlphaMul(p[1], scale);
713 p[2] = SkAlphaMul(b, scale);
714 p += 4;
715 }
716}
717
Scroggo8ac0d542011-06-21 14:44:57 +0000718void SampleWindow::saveToPdf()
719{
720 fSaveToPdf = true;
721 this->inval(NULL);
722}
723
reed@android.com8a1c16f2008-12-17 15:59:43 +0000724SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +0000725 if (fSaveToPdf) {
726 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
727 SkISize size = SkISize::Make(bmp.width(), bmp.height());
728 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
729 canvas->getTotalMatrix());
730 fPdfCanvas = new SkCanvas(pdfDevice);
731 pdfDevice->unref();
732 canvas = fPdfCanvas;
733 } else {
734 switch (fCanvasType) {
735 case kRaster_CanvasType:
reed@google.com29038ed2011-07-06 17:56:47 +0000736 case kGPU_CanvasType:
reed@android.comf2b98d62010-12-20 18:26:13 +0000737 canvas = this->INHERITED::beforeChildren(canvas);
Scroggo8ac0d542011-06-21 14:44:57 +0000738 break;
739 case kPicture_CanvasType:
740 fPicture = new SkPicture;
741 canvas = fPicture->beginRecording(9999, 9999);
742 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000743 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000744 }
745
746 if (fUseClip) {
747 canvas->drawColor(0xFFFF88FF);
748 canvas->clipPath(fClipPath);
749 }
750
751 return canvas;
752}
753
754static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
755 const SkRegion& rgn) {
756 SkCanvas canvas(bm);
757 SkRegion inval(rgn);
758
759 inval.translate(r.fLeft, r.fTop);
760 canvas.clipRegion(inval);
761 canvas.drawColor(0xFFFF8080);
762}
yangsu@google.com501775e2011-06-24 16:04:50 +0000763#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +0000764void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +0000765 if (fSaveToPdf) {
766 fSaveToPdf = false;
767 if (fShowZoomer) {
768 showZoomer(fPdfCanvas);
769 }
770 SkString name;
771 name.printf("%s.pdf", this->getTitle());
772 SkPDFDocument doc;
773 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
774 doc.appendPage(device);
775#ifdef ANDROID
776 name.prepend("/sdcard/");
777#endif
yangsu@google.com501775e2011-06-24 16:04:50 +0000778
779#ifdef SK_BUILD_FOR_IOS
780 SkDynamicMemoryWStream mstream;
781 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +0000782 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +0000783#endif
Scroggo8ac0d542011-06-21 14:44:57 +0000784 SkFILEWStream stream(name.c_str());
785 if (stream.isValid()) {
786 doc.emitPDF(&stream);
787 const char* desc = "File saved from Skia SampleApp";
788 this->onPDFSaved(this->getTitle(), desc, name.c_str());
789 }
yangsu@google.com501775e2011-06-24 16:04:50 +0000790
Scroggo8ac0d542011-06-21 14:44:57 +0000791 delete fPdfCanvas;
792 fPdfCanvas = NULL;
793
794 // We took over the draw calls in order to create the PDF, so we need
795 // to redraw.
796 this->inval(NULL);
797 return;
798 }
799
reed@android.comf2b98d62010-12-20 18:26:13 +0000800 if (fRequestGrabImage) {
801 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000802
reed@google.com29038ed2011-07-06 17:56:47 +0000803 SkDevice* device = orig->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000804 SkBitmap bmp;
805 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000806 static int gSampleGrabCounter;
807 SkString name;
808 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000809 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000810 SkImageEncoder::kPNG_Type, 100);
811 }
812 }
813
reed@android.com8a1c16f2008-12-17 15:59:43 +0000814 switch (fCanvasType) {
815 case kRaster_CanvasType:
816 break;
817 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000818 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000819 SkPicture* pict = new SkPicture(*fPicture);
820 fPicture->unref();
821 orig->drawPicture(*pict);
822 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000823 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000824 SkDynamicMemoryWStream ostream;
825 fPicture->serialize(&ostream);
826 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000827
reed@google.com8a85d0c2011-06-24 19:12:12 +0000828 SkAutoDataUnref data(ostream.copyToData());
829 SkMemoryStream istream(data.data(), data.size());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000830 SkPicture pict(&istream);
831 orig->drawPicture(pict);
832 } else {
833 fPicture->draw(orig);
834 fPicture->unref();
835 }
836 fPicture = NULL;
837 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000838#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000839 case kGPU_CanvasType:
reed@google.com29038ed2011-07-06 17:56:47 +0000840 if (fShowZoomer) {
841 this->showZoomer(orig);
Scroggo3272ba82011-05-25 20:50:42 +0000842 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000843 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000844#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000845 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000846
reed@google.com17d7aec2011-04-25 14:31:44 +0000847 // Do this after presentGL and other finishing, rather than in afterChild
848 if (fMeasureFPS && fMeasureFPS_Time) {
849 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
850 this->updateTitle();
Scroggo62b65b02011-06-21 16:01:26 +0000851 this->postInvalDelay();
reed@google.com17d7aec2011-04-25 14:31:44 +0000852 }
853
854 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000855 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000856 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
857 int dx = fScrollTestX * 7;
858 int dy = fScrollTestY * 7;
859 SkIRect r;
860 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000861
reed@android.com8a1c16f2008-12-17 15:59:43 +0000862 r.set(50, 50, 50+100, 50+100);
863 bm.scrollRect(&r, dx, dy, &inval);
864 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000865 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000866}
867
reed@android.com6c5f6f22009-08-14 16:08:38 +0000868void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
869 if (fScale) {
870 SkScalar scale = SK_Scalar1 * 7 / 10;
871 SkScalar cx = this->width() / 2;
872 SkScalar cy = this->height() / 2;
873 canvas->translate(cx, cy);
874 canvas->scale(scale, scale);
875 canvas->translate(-cx, -cy);
876 }
877 if (fRotate) {
878 SkScalar cx = this->width() / 2;
879 SkScalar cy = this->height() / 2;
880 canvas->translate(cx, cy);
881 canvas->rotate(SkIntToScalar(30));
882 canvas->translate(-cx, -cy);
883 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000884
reed@google.com09e3baa2011-05-18 12:04:31 +0000885 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
886 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000887
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000888 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000889 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
890 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
891 fMeasureFPS_Time = SkTime::GetMSecs();
892 }
893 } else {
894 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000895 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000896 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000897}
898
899void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000900 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000901}
902
reed@android.com8a1c16f2008-12-17 15:59:43 +0000903static SkBitmap::Config gConfigCycle[] = {
904 SkBitmap::kNo_Config, // none -> none
905 SkBitmap::kNo_Config, // a1 -> none
906 SkBitmap::kNo_Config, // a8 -> none
907 SkBitmap::kNo_Config, // index8 -> none
908 SkBitmap::kARGB_4444_Config, // 565 -> 4444
909 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
910 SkBitmap::kRGB_565_Config // 8888 -> 565
911};
912
913static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
914 return gConfigCycle[c];
915}
916
djsollen@google.come32b5832011-06-13 16:58:40 +0000917void SampleWindow::changeZoomLevel(float delta) {
918 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000919 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000920 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
921 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000922 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000923 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
924 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000925 } else {
926 fZoomScale = SK_Scalar1;
927 }
928
djsollen@google.come32b5832011-06-13 16:58:40 +0000929 this->updateTitle();
930
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000931 this->inval(NULL);
932}
933
Scroggo2c8208f2011-06-15 16:49:08 +0000934bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +0000935 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
Scroggo2c8208f2011-06-15 16:49:08 +0000936 this->loadView(fSamples[fCurrIndex]());
937 return true;
938}
939
reed@android.com8a1c16f2008-12-17 15:59:43 +0000940bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000941 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
942 this->loadView(fSamples[fCurrIndex]());
943 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000944}
945
yangsu@google.com501775e2011-06-24 16:04:50 +0000946bool SampleWindow::goToSample(int i) {
947 fCurrIndex = (i) % fSamples.count();
948 this->loadView(fSamples[fCurrIndex]());
949 return true;
950}
951
952SkString SampleWindow::getSampleTitle(int i) {
953 SkView* view = fSamples[i]();
954 SkString title;
955 SkEvent evt(gTitleEvtName);
956 if (view->doQuery(&evt)) {
957 title.set(evt.findString(gTitleEvtName));
958 }
959 view->unref();
960 return title;
961}
962
963int SampleWindow::sampleCount() {
964 return fSamples.count();
965}
966
Scroggo2c8208f2011-06-15 16:49:08 +0000967void SampleWindow::postAnimatingEvent() {
968 if (fAnimating) {
969 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
970 evt->post(this->getSinkID(), ANIMATING_DELAY);
971 }
972}
973
reed@android.com8a1c16f2008-12-17 15:59:43 +0000974bool SampleWindow::onEvent(const SkEvent& evt) {
975 if (evt.isType(ANIMATING_EVENTTYPE)) {
976 if (fAnimating) {
977 this->nextSample();
978 this->postAnimatingEvent();
979 }
980 return true;
981 }
reed@android.com34245c72009-11-03 04:00:48 +0000982 if (evt.isType("set-curr-index")) {
983 fCurrIndex = evt.getFast32() % fSamples.count();
984 this->loadView(fSamples[fCurrIndex]());
985 return true;
986 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000987 if (isInvalEvent(evt)) {
988 this->inval(NULL);
989 return true;
990 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000991 return this->INHERITED::onEvent(evt);
992}
993
reed@android.comf2b98d62010-12-20 18:26:13 +0000994bool SampleWindow::onQuery(SkEvent* query) {
995 if (query->isType("get-slide-count")) {
996 query->setFast32(fSamples.count());
997 return true;
998 }
999 if (query->isType("get-slide-title")) {
1000 SkView* view = fSamples[query->getFast32()]();
1001 SkEvent evt(gTitleEvtName);
1002 if (view->doQuery(&evt)) {
1003 query->setString("title", evt.findString(gTitleEvtName));
1004 }
1005 SkSafeUnref(view);
1006 return true;
1007 }
1008 if (query->isType("use-fast-text")) {
1009 SkEvent evt(gFastTextEvtName);
1010 return curr_view(this)->doQuery(&evt);
1011 }
reed@google.com29038ed2011-07-06 17:56:47 +00001012 if (query->isType("ignore-window-bitmap")) {
1013 query->setFast32(this->getGrContext() != NULL);
1014 return true;
1015 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001016 return this->INHERITED::onQuery(query);
1017}
1018
reed@android.com0ae6b242008-12-23 16:49:54 +00001019static void cleanup_for_filename(SkString* name) {
1020 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001021 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001022 switch (str[i]) {
1023 case ':': str[i] = '-'; break;
1024 case '/': str[i] = '-'; break;
1025 case ' ': str[i] = '_'; break;
1026 default: break;
1027 }
1028 }
1029}
reed@android.com8a1c16f2008-12-17 15:59:43 +00001030
1031bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001032 {
1033 SkView* view = curr_view(this);
1034 if (view) {
1035 SkEvent evt(gCharEvtName);
1036 evt.setFast32(uni);
1037 if (view->doQuery(&evt)) {
1038 return true;
1039 }
1040 }
1041 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001042
reed@android.com8a1c16f2008-12-17 15:59:43 +00001043 int dx = 0xFF;
1044 int dy = 0xFF;
1045
1046 switch (uni) {
1047 case '5': dx = 0; dy = 0; break;
1048 case '8': dx = 0; dy = -1; break;
1049 case '6': dx = 1; dy = 0; break;
1050 case '2': dx = 0; dy = 1; break;
1051 case '4': dx = -1; dy = 0; break;
1052 case '7': dx = -1; dy = -1; break;
1053 case '9': dx = 1; dy = -1; break;
1054 case '3': dx = 1; dy = 1; break;
1055 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001056
reed@android.com8a1c16f2008-12-17 15:59:43 +00001057 default:
1058 break;
1059 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001060
reed@android.com8a1c16f2008-12-17 15:59:43 +00001061 if (0xFF != dx && 0xFF != dy) {
1062 if ((dx | dy) == 0) {
1063 fScrollTestX = fScrollTestY = 0;
1064 } else {
1065 fScrollTestX += dx;
1066 fScrollTestY += dy;
1067 }
1068 this->inval(NULL);
1069 return true;
1070 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001071
reed@android.com0ae6b242008-12-23 16:49:54 +00001072 switch (uni) {
1073 case 'a':
Scroggo2c8208f2011-06-15 16:49:08 +00001074 this->toggleSlideshow();
reed@android.com0ae6b242008-12-23 16:49:54 +00001075 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001076 case 'b':
1077 fAAState = cycle_tristate(fAAState);
1078 this->updateTitle();
1079 this->inval(NULL);
1080 break;
1081 case 'c':
1082 fUseClip = !fUseClip;
1083 this->inval(NULL);
1084 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001085 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001086 case 'd':
1087 SkGraphics::SetFontCacheUsed(0);
1088 return true;
Scroggo8ac0d542011-06-21 14:44:57 +00001089 case 'e':
1090 this->saveToPdf();
1091 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001092 case 'f':
Scroggo2c8208f2011-06-15 16:49:08 +00001093 this->toggleFPS();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001094 break;
1095 case 'g':
1096 fRequestGrabImage = true;
1097 this->inval(NULL);
1098 break;
reed@google.com09e3baa2011-05-18 12:04:31 +00001099 case 'h':
1100 fHintingState = cycle_tristate(fHintingState);
1101 this->updateTitle();
1102 this->inval(NULL);
1103 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001104 case 'i':
1105 this->zoomIn();
1106 break;
1107 case 'l':
1108 fLCDState = cycle_tristate(fLCDState);
1109 this->updateTitle();
1110 this->inval(NULL);
1111 break;
reed@google.com176753a2011-05-17 15:32:04 +00001112 case 'n':
1113 fFilterState = cycle_tristate(fFilterState);
1114 this->updateTitle();
1115 this->inval(NULL);
1116 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001117 case 'o':
1118 this->zoomOut();
1119 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001120 case 'p':
1121 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001122 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001123 this->inval(NULL);
1124 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001125 case 'r':
1126 fRotate = !fRotate;
1127 this->inval(NULL);
1128 this->updateTitle();
1129 return true;
1130 case 's':
1131 fScale = !fScale;
1132 this->inval(NULL);
1133 this->updateTitle();
1134 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001135 case 'x':
1136 fFlipAxis ^= kFlipAxis_X;
1137 this->updateTitle();
1138 this->inval(NULL);
1139 break;
1140 case 'y':
1141 fFlipAxis ^= kFlipAxis_Y;
1142 this->updateTitle();
1143 this->inval(NULL);
1144 break;
scroggo08526c02011-03-22 14:03:21 +00001145 case 'z':
1146 this->toggleZoomer();
1147 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001148 default:
1149 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001150 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001151
reed@android.com8a1c16f2008-12-17 15:59:43 +00001152 return this->INHERITED::onHandleChar(uni);
1153}
1154
Scroggo2c8208f2011-06-15 16:49:08 +00001155void SampleWindow::toggleFPS() {
1156 fMeasureFPS = !fMeasureFPS;
1157 this->inval(NULL);
1158 this->updateTitle();
1159}
1160
1161void SampleWindow::toggleSlideshow() {
1162 fAnimating = !fAnimating;
1163 this->postAnimatingEvent();
1164 this->updateTitle();
1165}
1166
1167void SampleWindow::toggleRendering() {
1168 fCanvasType = cycle_canvastype(fCanvasType);
1169 this->updateTitle();
1170 this->inval(NULL);
1171}
1172
reed@android.com8a1c16f2008-12-17 15:59:43 +00001173#include "SkDumpCanvas.h"
1174
1175bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001176 {
1177 SkView* view = curr_view(this);
1178 if (view) {
1179 SkEvent evt(gKeyEvtName);
1180 evt.setFast32(key);
1181 if (view->doQuery(&evt)) {
1182 return true;
1183 }
1184 }
1185 }
1186
reed@android.com8a1c16f2008-12-17 15:59:43 +00001187 switch (key) {
1188 case kRight_SkKey:
1189 if (this->nextSample()) {
1190 return true;
1191 }
1192 break;
1193 case kLeft_SkKey:
Scroggo2c8208f2011-06-15 16:49:08 +00001194 toggleRendering();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001195 return true;
1196 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001197 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001198 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001199 } else {
1200 fNClip = !fNClip;
1201 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001202 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001203 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001204 return true;
1205 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001206 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001207 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001208 } else {
1209 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001210 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001211 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001212 return true;
1213 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001214 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001215 SkDebugfDumper dumper;
1216 SkDumpCanvas dc(&dumper);
1217 this->draw(&dc);
1218 } else {
1219 fRepeatDrawing = !fRepeatDrawing;
1220 if (fRepeatDrawing) {
1221 this->inval(NULL);
1222 }
1223 }
1224 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001225 case kBack_SkKey:
1226 this->loadView(NULL);
1227 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001228 default:
1229 break;
1230 }
1231 return this->INHERITED::onHandleKey(key);
1232}
1233
reed@google.com52f57e12011-03-16 12:10:02 +00001234///////////////////////////////////////////////////////////////////////////////
1235
1236static const char gGestureClickType[] = "GestureClickType";
1237
Scroggod3aed392011-06-22 13:26:56 +00001238bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
1239 void* owner) {
Scroggo0f185c22011-03-24 18:35:50 +00001240 if (Click::kMoved_State == state) {
1241 updatePointer(x, y);
1242 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001243 int w = SkScalarRound(this->width());
1244 int h = SkScalarRound(this->height());
1245
1246 // check for the resize-box
1247 if (w - x < 16 && h - y < 16) {
1248 return false; // let the OS handle the click
1249 } else {
Scroggod3aed392011-06-22 13:26:56 +00001250 return this->INHERITED::onDispatchClick(x, y, state, owner);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001251 }
1252}
1253
reed@google.com52f57e12011-03-16 12:10:02 +00001254class GestureClick : public SkView::Click {
1255public:
1256 GestureClick(SkView* target) : SkView::Click(target) {
1257 this->setType(gGestureClickType);
1258 }
1259
1260 static bool IsGesture(Click* click) {
1261 return click->isType(gGestureClickType);
1262 }
1263};
1264
1265SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1266 return new GestureClick(this);
1267}
1268
1269bool SampleWindow::onClick(Click* click) {
1270 if (GestureClick::IsGesture(click)) {
1271 float x = SkScalarToFloat(click->fCurr.fX);
1272 float y = SkScalarToFloat(click->fCurr.fY);
1273 switch (click->fState) {
1274 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00001275 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001276 break;
1277 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00001278 fGesture.touchMoved(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001279 this->inval(NULL);
1280 break;
1281 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00001282 fGesture.touchEnd(click->fOwner);
reed@google.com52f57e12011-03-16 12:10:02 +00001283 this->inval(NULL);
1284 break;
1285 }
1286 return true;
1287 }
1288 return false;
1289}
1290
1291///////////////////////////////////////////////////////////////////////////////
1292
reed@android.com8a1c16f2008-12-17 15:59:43 +00001293void SampleWindow::loadView(SkView* view) {
1294 SkView::F2BIter iter(this);
1295 SkView* prev = iter.next();
1296 if (prev) {
1297 prev->detachFromParent();
1298 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001299
reed@android.com34245c72009-11-03 04:00:48 +00001300 if (NULL == view) {
1301 view = create_overview(fSamples.count(), fSamples.begin());
1302 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001303 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001304 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001305 this->attachChildToFront(view)->unref();
1306 view->setSize(this->width(), this->height());
1307
1308 this->updateTitle();
1309}
1310
1311static const char* gConfigNames[] = {
1312 "unknown config",
1313 "A1",
1314 "A8",
1315 "Index8",
1316 "565",
1317 "4444",
1318 "8888"
1319};
1320
1321static const char* configToString(SkBitmap::Config c) {
1322 return gConfigNames[c];
1323}
1324
1325static const char* gCanvasTypePrefix[] = {
1326 "raster: ",
1327 "picture: ",
1328 "opengl: "
1329};
1330
reed@google.com569e0432011-04-05 13:07:03 +00001331static const char* trystate_str(SkTriState state,
1332 const char trueStr[], const char falseStr[]) {
1333 if (kTrue_SkTriState == state) {
1334 return trueStr;
1335 } else if (kFalse_SkTriState == state) {
1336 return falseStr;
1337 }
1338 return NULL;
1339}
1340
reed@android.com8a1c16f2008-12-17 15:59:43 +00001341void SampleWindow::updateTitle() {
1342 SkString title;
1343
1344 SkView::F2BIter iter(this);
1345 SkView* view = iter.next();
1346 SkEvent evt(gTitleEvtName);
1347 if (view->doQuery(&evt)) {
1348 title.set(evt.findString(gTitleEvtName));
1349 }
1350 if (title.size() == 0) {
1351 title.set("<unknown>");
1352 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001353
reed@android.com8a1c16f2008-12-17 15:59:43 +00001354 title.prepend(gCanvasTypePrefix[fCanvasType]);
1355
1356 title.prepend(" ");
1357 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001358
reed@android.com8a1c16f2008-12-17 15:59:43 +00001359 if (fAnimating) {
1360 title.prepend("<A> ");
1361 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001362 if (fScale) {
1363 title.prepend("<S> ");
1364 }
1365 if (fRotate) {
1366 title.prepend("<R> ");
1367 }
reed@android.come522ca52009-11-23 20:10:41 +00001368 if (fNClip) {
1369 title.prepend("<C> ");
1370 }
reed@google.com569e0432011-04-05 13:07:03 +00001371
1372 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1373 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001374 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001375 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1376 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001377
1378 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001379 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001380 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001381
1382 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001383 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001384 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001385 if (fUsePipe && SampleView::IsSampleView(view)) {
1386 title.prepend("<P> ");
1387 }
1388 if (SampleView::IsSampleView(view)) {
1389 title.prepend("! ");
1390 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001391
reed@android.com8a1c16f2008-12-17 15:59:43 +00001392 this->setTitle(title.c_str());
1393}
1394
1395void SampleWindow::onSizeChange() {
1396 this->INHERITED::onSizeChange();
1397
reed@google.com29038ed2011-07-06 17:56:47 +00001398 if (fGrContext) {
1399 this->attachGL();
1400
1401 GrPlatformSurfaceDesc desc;
1402 desc.reset();
1403 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
1404 desc.fWidth = SkScalarRound(this->width());
1405 desc.fHeight = SkScalarRound(this->height());
1406 desc.fConfig = kRGBA_8888_GrPixelConfig;
1407 desc.fStencilBits = 8;
1408 GrGLint buffer;
1409 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer);
1410 desc.fPlatformRenderTarget = buffer;
1411
1412 SkSafeUnref(fGrRenderTarget);
1413 fGrRenderTarget = static_cast<GrRenderTarget*>(
1414 fGrContext->createPlatformSurface(desc));
1415 }
1416
reed@android.com8a1c16f2008-12-17 15:59:43 +00001417 SkView::F2BIter iter(this);
1418 SkView* view = iter.next();
1419 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001420
reed@android.com8a1c16f2008-12-17 15:59:43 +00001421 // rebuild our clippath
1422 {
1423 const SkScalar W = this->width();
1424 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001425
reed@android.com8a1c16f2008-12-17 15:59:43 +00001426 fClipPath.reset();
1427#if 0
1428 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1429 SkRect r;
1430 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1431 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1432 fClipPath.addRect(r);
1433 }
1434#else
1435 SkRect r;
1436 r.set(0, 0, W, H);
1437 fClipPath.addRect(r, SkPath::kCCW_Direction);
1438 r.set(W/4, H/4, W*3/4, H*3/4);
1439 fClipPath.addRect(r, SkPath::kCW_Direction);
1440#endif
1441 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001442
Scroggo2c8208f2011-06-15 16:49:08 +00001443 fZoomCenterX = SkScalarHalf(this->width());
1444 fZoomCenterY = SkScalarHalf(this->height());
1445
Scroggo3e7ff9f2011-06-16 15:31:26 +00001446#ifdef ANDROID
Scroggob4490c72011-06-17 13:53:05 +00001447 // FIXME: The first draw after a size change does not work on Android, so
1448 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00001449 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00001450#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001451 this->updateTitle(); // to refresh our config
1452}
1453
1454///////////////////////////////////////////////////////////////////////////////
1455
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001456static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001457static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001458static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001459
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001460bool SampleView::IsSampleView(SkView* view) {
1461 SkEvent evt(is_sample_view_tag);
1462 return view->doQuery(&evt);
1463}
1464
reed@google.comf2183392011-04-22 14:10:48 +00001465bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001466 SkEvent evt(repeat_count_tag);
1467 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001468 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001469}
1470
reed@google.com0faac1e2011-05-11 05:58:58 +00001471bool SampleView::SetUsePipe(SkView* view, bool pred) {
1472 SkEvent evt(set_use_pipe_tag);
1473 evt.setFast32(pred);
1474 return view->doEvent(evt);
1475}
1476
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001477bool SampleView::onEvent(const SkEvent& evt) {
1478 if (evt.isType(repeat_count_tag)) {
1479 fRepeatCount = evt.getFast32();
1480 return true;
1481 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001482 if (evt.isType(set_use_pipe_tag)) {
1483 fUsePipe = !!evt.getFast32();
1484 return true;
1485 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001486 return this->INHERITED::onEvent(evt);
1487}
1488
1489bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001490 if (evt->isType(is_sample_view_tag)) {
1491 return true;
1492 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001493 return this->INHERITED::onQuery(evt);
1494}
1495
reed@google.com68f456d2011-05-02 18:55:39 +00001496#ifdef TEST_GPIPE
1497 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001498
1499class SimplePC : public SkGPipeController {
1500public:
1501 SimplePC(SkCanvas* target);
1502 ~SimplePC();
1503
1504 virtual void* requestBlock(size_t minRequest, size_t* actual);
1505 virtual void notifyWritten(size_t bytes);
1506
1507private:
reed@google.com961ddb02011-05-05 14:03:48 +00001508 SkGPipeReader fReader;
1509 void* fBlock;
1510 size_t fBlockSize;
1511 size_t fBytesWritten;
1512 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001513 SkGPipeReader::Status fStatus;
1514
1515 size_t fTotalWritten;
1516};
1517
1518SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1519 fBlock = NULL;
1520 fBlockSize = fBytesWritten = 0;
1521 fStatus = SkGPipeReader::kDone_Status;
1522 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001523 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001524}
1525
1526SimplePC::~SimplePC() {
1527// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1528 sk_free(fBlock);
1529
reed@google.com0faac1e2011-05-11 05:58:58 +00001530 if (fTotalWritten) {
1531 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1532 fAtomsWritten, fStatus);
1533 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001534}
1535
1536void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1537 sk_free(fBlock);
1538
1539 fBlockSize = minRequest * 4;
1540 fBlock = sk_malloc_throw(fBlockSize);
1541 fBytesWritten = 0;
1542 *actual = fBlockSize;
1543 return fBlock;
1544}
1545
1546void SimplePC::notifyWritten(size_t bytes) {
1547 SkASSERT(fBytesWritten + bytes <= fBlockSize);
yangsu@google.com1f394212011-06-01 18:03:34 +00001548
1549#ifdef PIPE_FILE
1550 //File is open in append mode
1551 FILE* f = fopen(FILE_PATH, "ab");
1552 SkASSERT(f != NULL);
1553 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
1554 fclose(f);
1555#endif
1556
reed@google.com64e3eb22011-05-04 14:32:04 +00001557 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1558 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1559 fBytesWritten += bytes;
1560 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001561
1562 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001563}
1564
reed@google.com68f456d2011-05-02 18:55:39 +00001565#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001566
reed@google.com64e3eb22011-05-04 14:32:04 +00001567
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001568void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001569#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001570 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001571 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001572 if (fUsePipe) {
reed@google.comdde09562011-05-23 12:21:05 +00001573 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
1574// flags = 0;
1575 canvas = writer.startRecording(&controller, flags);
reed@google.com0faac1e2011-05-11 05:58:58 +00001576 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001577#endif
1578
reed@google.com81e3d7f2011-06-01 12:42:36 +00001579 this->onDrawBackground(canvas);
1580
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001581 for (int i = 0; i < fRepeatCount; i++) {
1582 SkAutoCanvasRestore acr(canvas, true);
1583 this->onDrawContent(canvas);
1584 }
1585}
1586
1587void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001588 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001589}
1590
1591///////////////////////////////////////////////////////////////////////////////
1592
reed@android.comf2b98d62010-12-20 18:26:13 +00001593template <typename T> void SkTBSort(T array[], int count) {
1594 for (int i = 1; i < count - 1; i++) {
1595 bool didSwap = false;
1596 for (int j = count - 1; j > i; --j) {
1597 if (array[j] < array[j-1]) {
1598 T tmp(array[j-1]);
1599 array[j-1] = array[j];
1600 array[j] = tmp;
1601 didSwap = true;
1602 }
1603 }
1604 if (!didSwap) {
1605 break;
1606 }
1607 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001608
reed@android.comf2b98d62010-12-20 18:26:13 +00001609 for (int k = 0; k < count - 1; k++) {
1610 SkASSERT(!(array[k+1] < array[k]));
1611 }
1612}
1613
1614#include "SkRandom.h"
1615
1616static void rand_rect(SkIRect* rect, SkRandom& rand) {
1617 int bits = 8;
1618 int shift = 32 - bits;
1619 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1620 rand.nextU() >> shift, rand.nextU() >> shift);
1621 rect->sort();
1622}
1623
1624static void dumpRect(const SkIRect& r) {
1625 SkDebugf(" { %d, %d, %d, %d },\n",
1626 r.fLeft, r.fTop,
1627 r.fRight, r.fBottom);
1628}
1629
1630static void test_rects(const SkIRect rect[], int count) {
1631 SkRegion rgn0, rgn1;
1632
1633 for (int i = 0; i < count; i++) {
1634 rgn0.op(rect[i], SkRegion::kUnion_Op);
1635 // dumpRect(rect[i]);
1636 }
1637 rgn1.setRects(rect, count);
1638
1639 if (rgn0 != rgn1) {
1640 SkDebugf("\n");
1641 for (int i = 0; i < count; i++) {
1642 dumpRect(rect[i]);
1643 }
1644 SkDebugf("\n");
1645 }
1646}
1647
1648static void test() {
1649 size_t i;
1650
1651 const SkIRect r0[] = {
1652 { 0, 0, 1, 1 },
1653 { 2, 2, 3, 3 },
1654 };
1655 const SkIRect r1[] = {
1656 { 0, 0, 1, 3 },
1657 { 1, 1, 2, 2 },
1658 { 2, 0, 3, 3 },
1659 };
1660 const SkIRect r2[] = {
1661 { 0, 0, 1, 2 },
1662 { 2, 1, 3, 3 },
1663 { 4, 0, 5, 1 },
1664 { 6, 0, 7, 4 },
1665 };
1666
1667 static const struct {
1668 const SkIRect* fRects;
1669 int fCount;
1670 } gRecs[] = {
1671 { r0, SK_ARRAY_COUNT(r0) },
1672 { r1, SK_ARRAY_COUNT(r1) },
1673 { r2, SK_ARRAY_COUNT(r2) },
1674 };
1675
1676 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1677 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1678 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001679
reed@android.comf2b98d62010-12-20 18:26:13 +00001680 SkRandom rand;
1681 for (i = 0; i < 10000; i++) {
1682 SkRegion rgn0, rgn1;
1683
1684 const int N = 8;
1685 SkIRect rect[N];
1686 for (int j = 0; j < N; j++) {
1687 rand_rect(&rect[j], rand);
1688 }
1689 test_rects(rect, N);
1690 }
1691}
1692
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00001693SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001694// test();
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00001695 return new SampleWindow(hwnd, argc, argv);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001696}
1697
1698void get_preferred_size(int* x, int* y, int* width, int* height) {
1699 *x = 10;
1700 *y = 50;
1701 *width = 640;
1702 *height = 480;
1703}
1704
1705void application_init() {
1706// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001707#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001708 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001709#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001710 SkGraphics::Init();
1711 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001712}
1713
1714void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001715 SkEvent::Term();
1716 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001717}