blob: 0d4ff44213b00100e1e891e984988da48a0c7a4a [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
594 if (fGrContext && (fCanvasType != kGPU_CanvasType)) {
595 fGrContext->setRenderTarget(fGrRenderTarget);
596 // need to send the bits to the (gpu) window
597 const SkBitmap& bm = this->getBitmap();
598 fGrContext->writePixels(0, 0, bm.width(), bm.height(),
599 kRGBA_8888_GrPixelConfig, bm.getPixels(),
600 bm.rowBytes());
601 }
602 presentGL();
Scroggo3272ba82011-05-25 20:50:42 +0000603}
604
605void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +0000606 int count = canvas->save();
607 canvas->resetMatrix();
608 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000609 int width = SkScalarRound(this->width());
610 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000611 if (fMouseX >= width) fMouseX = width - 1;
612 else if (fMouseX < 0) fMouseX = 0;
613 if (fMouseY >= height) fMouseY = height - 1;
614 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000615
Scroggo0f185c22011-03-24 18:35:50 +0000616 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000617 bitmap.lockPixels();
618
Scroggo0f185c22011-03-24 18:35:50 +0000619 // 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 +0000620 int zoomedWidth = (width >> 1) | 1;
621 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000622 SkIRect src;
623 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
624 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
625 SkRect dest;
626 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
627 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
628 SkPaint paint;
629 // Clear the background behind our zoomed in view
630 paint.setColor(SK_ColorWHITE);
631 canvas->drawRect(dest, paint);
632 canvas->drawBitmapRect(bitmap, &src, dest);
633 paint.setColor(SK_ColorBLACK);
634 paint.setStyle(SkPaint::kStroke_Style);
635 // Draw a border around the pixel in the middle
636 SkRect originalPixel;
637 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
638 SkMatrix matrix;
639 SkRect scalarSrc;
640 scalarSrc.set(src);
641 SkColor color = bitmap.getColor(fMouseX, fMouseY);
642 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
643 SkRect pixel;
644 matrix.mapRect(&pixel, originalPixel);
645 // TODO Perhaps measure the values and make the outline white if it's "dark"
646 if (color == SK_ColorBLACK) {
647 paint.setColor(SK_ColorWHITE);
648 }
649 canvas->drawRect(pixel, paint);
650 }
651 paint.setColor(SK_ColorBLACK);
652 // Draw a border around the destination rectangle
653 canvas->drawRect(dest, paint);
654 paint.setStyle(SkPaint::kStrokeAndFill_Style);
655 // Identify the pixel and its color on screen
656 paint.setTypeface(fTypeface);
657 paint.setAntiAlias(true);
658 SkScalar lineHeight = paint.getFontMetrics(NULL);
659 SkString string;
660 string.appendf("(%i, %i)", fMouseX, fMouseY);
661 SkScalar left = dest.fLeft + SkIntToScalar(3);
662 SkScalar i = SK_Scalar1;
663 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
664 // Alpha
665 i += SK_Scalar1;
666 string.reset();
667 string.appendf("A: %X", SkColorGetA(color));
668 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
669 // Red
670 i += SK_Scalar1;
671 string.reset();
672 string.appendf("R: %X", SkColorGetR(color));
673 paint.setColor(SK_ColorRED);
674 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
675 // Green
676 i += SK_Scalar1;
677 string.reset();
678 string.appendf("G: %X", SkColorGetG(color));
679 paint.setColor(SK_ColorGREEN);
680 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
681 // Blue
682 i += SK_Scalar1;
683 string.reset();
684 string.appendf("B: %X", SkColorGetB(color));
685 paint.setColor(SK_ColorBLUE);
686 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
687 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +0000688}
689
reed@android.com8a1c16f2008-12-17 15:59:43 +0000690void SampleWindow::onDraw(SkCanvas* canvas) {
691 if (fRepeatDrawing) {
692 this->inval(NULL);
693 }
694}
695
696#include "SkColorPriv.h"
697
698static void reverseRedAndBlue(const SkBitmap& bm) {
699 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
700 uint8_t* p = (uint8_t*)bm.getPixels();
701 uint8_t* stop = p + bm.getSize();
702 while (p < stop) {
703 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
704 unsigned scale = SkAlpha255To256(p[3]);
705 unsigned r = p[2];
706 unsigned b = p[0];
707 p[0] = SkAlphaMul(r, scale);
708 p[1] = SkAlphaMul(p[1], scale);
709 p[2] = SkAlphaMul(b, scale);
710 p += 4;
711 }
712}
713
Scroggo8ac0d542011-06-21 14:44:57 +0000714void SampleWindow::saveToPdf()
715{
716 fSaveToPdf = true;
717 this->inval(NULL);
718}
719
reed@android.com8a1c16f2008-12-17 15:59:43 +0000720SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +0000721 if (fSaveToPdf) {
722 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
723 SkISize size = SkISize::Make(bmp.width(), bmp.height());
724 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
725 canvas->getTotalMatrix());
726 fPdfCanvas = new SkCanvas(pdfDevice);
727 pdfDevice->unref();
728 canvas = fPdfCanvas;
729 } else {
730 switch (fCanvasType) {
731 case kRaster_CanvasType:
reed@google.com29038ed2011-07-06 17:56:47 +0000732 case kGPU_CanvasType:
reed@android.comf2b98d62010-12-20 18:26:13 +0000733 canvas = this->INHERITED::beforeChildren(canvas);
Scroggo8ac0d542011-06-21 14:44:57 +0000734 break;
735 case kPicture_CanvasType:
736 fPicture = new SkPicture;
737 canvas = fPicture->beginRecording(9999, 9999);
738 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000739 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000740 }
741
742 if (fUseClip) {
743 canvas->drawColor(0xFFFF88FF);
744 canvas->clipPath(fClipPath);
745 }
746
747 return canvas;
748}
749
750static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
751 const SkRegion& rgn) {
752 SkCanvas canvas(bm);
753 SkRegion inval(rgn);
754
755 inval.translate(r.fLeft, r.fTop);
756 canvas.clipRegion(inval);
757 canvas.drawColor(0xFFFF8080);
758}
yangsu@google.com501775e2011-06-24 16:04:50 +0000759#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +0000760void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +0000761 if (fSaveToPdf) {
762 fSaveToPdf = false;
763 if (fShowZoomer) {
764 showZoomer(fPdfCanvas);
765 }
766 SkString name;
767 name.printf("%s.pdf", this->getTitle());
768 SkPDFDocument doc;
769 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
770 doc.appendPage(device);
771#ifdef ANDROID
772 name.prepend("/sdcard/");
773#endif
yangsu@google.com501775e2011-06-24 16:04:50 +0000774
775#ifdef SK_BUILD_FOR_IOS
776 SkDynamicMemoryWStream mstream;
777 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +0000778 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +0000779#endif
Scroggo8ac0d542011-06-21 14:44:57 +0000780 SkFILEWStream stream(name.c_str());
781 if (stream.isValid()) {
782 doc.emitPDF(&stream);
783 const char* desc = "File saved from Skia SampleApp";
784 this->onPDFSaved(this->getTitle(), desc, name.c_str());
785 }
yangsu@google.com501775e2011-06-24 16:04:50 +0000786
Scroggo8ac0d542011-06-21 14:44:57 +0000787 delete fPdfCanvas;
788 fPdfCanvas = NULL;
789
790 // We took over the draw calls in order to create the PDF, so we need
791 // to redraw.
792 this->inval(NULL);
793 return;
794 }
795
reed@android.comf2b98d62010-12-20 18:26:13 +0000796 if (fRequestGrabImage) {
797 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000798
reed@google.com29038ed2011-07-06 17:56:47 +0000799 SkDevice* device = orig->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000800 SkBitmap bmp;
801 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000802 static int gSampleGrabCounter;
803 SkString name;
804 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000805 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000806 SkImageEncoder::kPNG_Type, 100);
807 }
808 }
809
reed@android.com8a1c16f2008-12-17 15:59:43 +0000810 switch (fCanvasType) {
811 case kRaster_CanvasType:
812 break;
813 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000814 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000815 SkPicture* pict = new SkPicture(*fPicture);
816 fPicture->unref();
817 orig->drawPicture(*pict);
818 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000819 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000820 SkDynamicMemoryWStream ostream;
821 fPicture->serialize(&ostream);
822 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000823
reed@google.com8a85d0c2011-06-24 19:12:12 +0000824 SkAutoDataUnref data(ostream.copyToData());
825 SkMemoryStream istream(data.data(), data.size());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000826 SkPicture pict(&istream);
827 orig->drawPicture(pict);
828 } else {
829 fPicture->draw(orig);
830 fPicture->unref();
831 }
832 fPicture = NULL;
833 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000834#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000835 case kGPU_CanvasType:
reed@google.com29038ed2011-07-06 17:56:47 +0000836 if (fShowZoomer) {
837 this->showZoomer(orig);
Scroggo3272ba82011-05-25 20:50:42 +0000838 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000839 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000840#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000841 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000842
reed@google.com17d7aec2011-04-25 14:31:44 +0000843 // Do this after presentGL and other finishing, rather than in afterChild
844 if (fMeasureFPS && fMeasureFPS_Time) {
845 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
846 this->updateTitle();
Scroggo62b65b02011-06-21 16:01:26 +0000847 this->postInvalDelay();
reed@google.com17d7aec2011-04-25 14:31:44 +0000848 }
849
850 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000851 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000852 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
853 int dx = fScrollTestX * 7;
854 int dy = fScrollTestY * 7;
855 SkIRect r;
856 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000857
reed@android.com8a1c16f2008-12-17 15:59:43 +0000858 r.set(50, 50, 50+100, 50+100);
859 bm.scrollRect(&r, dx, dy, &inval);
860 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000861 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000862}
863
reed@android.com6c5f6f22009-08-14 16:08:38 +0000864void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
865 if (fScale) {
866 SkScalar scale = SK_Scalar1 * 7 / 10;
867 SkScalar cx = this->width() / 2;
868 SkScalar cy = this->height() / 2;
869 canvas->translate(cx, cy);
870 canvas->scale(scale, scale);
871 canvas->translate(-cx, -cy);
872 }
873 if (fRotate) {
874 SkScalar cx = this->width() / 2;
875 SkScalar cy = this->height() / 2;
876 canvas->translate(cx, cy);
877 canvas->rotate(SkIntToScalar(30));
878 canvas->translate(-cx, -cy);
879 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000880
reed@google.com09e3baa2011-05-18 12:04:31 +0000881 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
882 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000883
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000884 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000885 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
886 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
887 fMeasureFPS_Time = SkTime::GetMSecs();
888 }
889 } else {
890 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000891 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000892 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000893}
894
895void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000896 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000897}
898
reed@android.com8a1c16f2008-12-17 15:59:43 +0000899static SkBitmap::Config gConfigCycle[] = {
900 SkBitmap::kNo_Config, // none -> none
901 SkBitmap::kNo_Config, // a1 -> none
902 SkBitmap::kNo_Config, // a8 -> none
903 SkBitmap::kNo_Config, // index8 -> none
904 SkBitmap::kARGB_4444_Config, // 565 -> 4444
905 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
906 SkBitmap::kRGB_565_Config // 8888 -> 565
907};
908
909static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
910 return gConfigCycle[c];
911}
912
djsollen@google.come32b5832011-06-13 16:58:40 +0000913void SampleWindow::changeZoomLevel(float delta) {
914 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000915 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000916 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
917 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000918 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000919 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
920 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000921 } else {
922 fZoomScale = SK_Scalar1;
923 }
924
djsollen@google.come32b5832011-06-13 16:58:40 +0000925 this->updateTitle();
926
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000927 this->inval(NULL);
928}
929
Scroggo2c8208f2011-06-15 16:49:08 +0000930bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +0000931 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
Scroggo2c8208f2011-06-15 16:49:08 +0000932 this->loadView(fSamples[fCurrIndex]());
933 return true;
934}
935
reed@android.com8a1c16f2008-12-17 15:59:43 +0000936bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000937 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
938 this->loadView(fSamples[fCurrIndex]());
939 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000940}
941
yangsu@google.com501775e2011-06-24 16:04:50 +0000942bool SampleWindow::goToSample(int i) {
943 fCurrIndex = (i) % fSamples.count();
944 this->loadView(fSamples[fCurrIndex]());
945 return true;
946}
947
948SkString SampleWindow::getSampleTitle(int i) {
949 SkView* view = fSamples[i]();
950 SkString title;
951 SkEvent evt(gTitleEvtName);
952 if (view->doQuery(&evt)) {
953 title.set(evt.findString(gTitleEvtName));
954 }
955 view->unref();
956 return title;
957}
958
959int SampleWindow::sampleCount() {
960 return fSamples.count();
961}
962
Scroggo2c8208f2011-06-15 16:49:08 +0000963void SampleWindow::postAnimatingEvent() {
964 if (fAnimating) {
965 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
966 evt->post(this->getSinkID(), ANIMATING_DELAY);
967 }
968}
969
reed@android.com8a1c16f2008-12-17 15:59:43 +0000970bool SampleWindow::onEvent(const SkEvent& evt) {
971 if (evt.isType(ANIMATING_EVENTTYPE)) {
972 if (fAnimating) {
973 this->nextSample();
974 this->postAnimatingEvent();
975 }
976 return true;
977 }
reed@android.com34245c72009-11-03 04:00:48 +0000978 if (evt.isType("set-curr-index")) {
979 fCurrIndex = evt.getFast32() % fSamples.count();
980 this->loadView(fSamples[fCurrIndex]());
981 return true;
982 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000983 if (isInvalEvent(evt)) {
984 this->inval(NULL);
985 return true;
986 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000987 return this->INHERITED::onEvent(evt);
988}
989
reed@android.comf2b98d62010-12-20 18:26:13 +0000990bool SampleWindow::onQuery(SkEvent* query) {
991 if (query->isType("get-slide-count")) {
992 query->setFast32(fSamples.count());
993 return true;
994 }
995 if (query->isType("get-slide-title")) {
996 SkView* view = fSamples[query->getFast32()]();
997 SkEvent evt(gTitleEvtName);
998 if (view->doQuery(&evt)) {
999 query->setString("title", evt.findString(gTitleEvtName));
1000 }
1001 SkSafeUnref(view);
1002 return true;
1003 }
1004 if (query->isType("use-fast-text")) {
1005 SkEvent evt(gFastTextEvtName);
1006 return curr_view(this)->doQuery(&evt);
1007 }
reed@google.com29038ed2011-07-06 17:56:47 +00001008 if (query->isType("ignore-window-bitmap")) {
1009 query->setFast32(this->getGrContext() != NULL);
1010 return true;
1011 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001012 return this->INHERITED::onQuery(query);
1013}
1014
reed@android.com0ae6b242008-12-23 16:49:54 +00001015static void cleanup_for_filename(SkString* name) {
1016 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001017 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001018 switch (str[i]) {
1019 case ':': str[i] = '-'; break;
1020 case '/': str[i] = '-'; break;
1021 case ' ': str[i] = '_'; break;
1022 default: break;
1023 }
1024 }
1025}
reed@android.com8a1c16f2008-12-17 15:59:43 +00001026
1027bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001028 {
1029 SkView* view = curr_view(this);
1030 if (view) {
1031 SkEvent evt(gCharEvtName);
1032 evt.setFast32(uni);
1033 if (view->doQuery(&evt)) {
1034 return true;
1035 }
1036 }
1037 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001038
reed@android.com8a1c16f2008-12-17 15:59:43 +00001039 int dx = 0xFF;
1040 int dy = 0xFF;
1041
1042 switch (uni) {
1043 case '5': dx = 0; dy = 0; break;
1044 case '8': dx = 0; dy = -1; break;
1045 case '6': dx = 1; dy = 0; break;
1046 case '2': dx = 0; dy = 1; break;
1047 case '4': dx = -1; dy = 0; break;
1048 case '7': dx = -1; dy = -1; break;
1049 case '9': dx = 1; dy = -1; break;
1050 case '3': dx = 1; dy = 1; break;
1051 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001052
reed@android.com8a1c16f2008-12-17 15:59:43 +00001053 default:
1054 break;
1055 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001056
reed@android.com8a1c16f2008-12-17 15:59:43 +00001057 if (0xFF != dx && 0xFF != dy) {
1058 if ((dx | dy) == 0) {
1059 fScrollTestX = fScrollTestY = 0;
1060 } else {
1061 fScrollTestX += dx;
1062 fScrollTestY += dy;
1063 }
1064 this->inval(NULL);
1065 return true;
1066 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001067
reed@android.com0ae6b242008-12-23 16:49:54 +00001068 switch (uni) {
1069 case 'a':
Scroggo2c8208f2011-06-15 16:49:08 +00001070 this->toggleSlideshow();
reed@android.com0ae6b242008-12-23 16:49:54 +00001071 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001072 case 'b':
1073 fAAState = cycle_tristate(fAAState);
1074 this->updateTitle();
1075 this->inval(NULL);
1076 break;
1077 case 'c':
1078 fUseClip = !fUseClip;
1079 this->inval(NULL);
1080 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001081 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001082 case 'd':
1083 SkGraphics::SetFontCacheUsed(0);
1084 return true;
Scroggo8ac0d542011-06-21 14:44:57 +00001085 case 'e':
1086 this->saveToPdf();
1087 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001088 case 'f':
Scroggo2c8208f2011-06-15 16:49:08 +00001089 this->toggleFPS();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001090 break;
1091 case 'g':
1092 fRequestGrabImage = true;
1093 this->inval(NULL);
1094 break;
reed@google.com09e3baa2011-05-18 12:04:31 +00001095 case 'h':
1096 fHintingState = cycle_tristate(fHintingState);
1097 this->updateTitle();
1098 this->inval(NULL);
1099 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001100 case 'i':
1101 this->zoomIn();
1102 break;
1103 case 'l':
1104 fLCDState = cycle_tristate(fLCDState);
1105 this->updateTitle();
1106 this->inval(NULL);
1107 break;
reed@google.com176753a2011-05-17 15:32:04 +00001108 case 'n':
1109 fFilterState = cycle_tristate(fFilterState);
1110 this->updateTitle();
1111 this->inval(NULL);
1112 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001113 case 'o':
1114 this->zoomOut();
1115 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001116 case 'p':
1117 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001118 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001119 this->inval(NULL);
1120 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001121 case 'r':
1122 fRotate = !fRotate;
1123 this->inval(NULL);
1124 this->updateTitle();
1125 return true;
1126 case 's':
1127 fScale = !fScale;
1128 this->inval(NULL);
1129 this->updateTitle();
1130 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001131 case 'x':
1132 fFlipAxis ^= kFlipAxis_X;
1133 this->updateTitle();
1134 this->inval(NULL);
1135 break;
1136 case 'y':
1137 fFlipAxis ^= kFlipAxis_Y;
1138 this->updateTitle();
1139 this->inval(NULL);
1140 break;
scroggo08526c02011-03-22 14:03:21 +00001141 case 'z':
1142 this->toggleZoomer();
1143 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001144 default:
1145 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001146 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001147
reed@android.com8a1c16f2008-12-17 15:59:43 +00001148 return this->INHERITED::onHandleChar(uni);
1149}
1150
Scroggo2c8208f2011-06-15 16:49:08 +00001151void SampleWindow::toggleFPS() {
1152 fMeasureFPS = !fMeasureFPS;
1153 this->inval(NULL);
1154 this->updateTitle();
1155}
1156
1157void SampleWindow::toggleSlideshow() {
1158 fAnimating = !fAnimating;
1159 this->postAnimatingEvent();
1160 this->updateTitle();
1161}
1162
1163void SampleWindow::toggleRendering() {
1164 fCanvasType = cycle_canvastype(fCanvasType);
1165 this->updateTitle();
1166 this->inval(NULL);
1167}
1168
reed@android.com8a1c16f2008-12-17 15:59:43 +00001169#include "SkDumpCanvas.h"
1170
1171bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001172 {
1173 SkView* view = curr_view(this);
1174 if (view) {
1175 SkEvent evt(gKeyEvtName);
1176 evt.setFast32(key);
1177 if (view->doQuery(&evt)) {
1178 return true;
1179 }
1180 }
1181 }
1182
reed@android.com8a1c16f2008-12-17 15:59:43 +00001183 switch (key) {
1184 case kRight_SkKey:
1185 if (this->nextSample()) {
1186 return true;
1187 }
1188 break;
1189 case kLeft_SkKey:
Scroggo2c8208f2011-06-15 16:49:08 +00001190 toggleRendering();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001191 return true;
1192 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001193 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001194 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001195 } else {
1196 fNClip = !fNClip;
1197 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001198 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001199 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001200 return true;
1201 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001202 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001203 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001204 } else {
1205 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001206 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001207 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001208 return true;
1209 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001210 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001211 SkDebugfDumper dumper;
1212 SkDumpCanvas dc(&dumper);
1213 this->draw(&dc);
1214 } else {
1215 fRepeatDrawing = !fRepeatDrawing;
1216 if (fRepeatDrawing) {
1217 this->inval(NULL);
1218 }
1219 }
1220 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001221 case kBack_SkKey:
1222 this->loadView(NULL);
1223 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001224 default:
1225 break;
1226 }
1227 return this->INHERITED::onHandleKey(key);
1228}
1229
reed@google.com52f57e12011-03-16 12:10:02 +00001230///////////////////////////////////////////////////////////////////////////////
1231
1232static const char gGestureClickType[] = "GestureClickType";
1233
Scroggod3aed392011-06-22 13:26:56 +00001234bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
1235 void* owner) {
Scroggo0f185c22011-03-24 18:35:50 +00001236 if (Click::kMoved_State == state) {
1237 updatePointer(x, y);
1238 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001239 int w = SkScalarRound(this->width());
1240 int h = SkScalarRound(this->height());
1241
1242 // check for the resize-box
1243 if (w - x < 16 && h - y < 16) {
1244 return false; // let the OS handle the click
1245 } else {
Scroggod3aed392011-06-22 13:26:56 +00001246 return this->INHERITED::onDispatchClick(x, y, state, owner);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001247 }
1248}
1249
reed@google.com52f57e12011-03-16 12:10:02 +00001250class GestureClick : public SkView::Click {
1251public:
1252 GestureClick(SkView* target) : SkView::Click(target) {
1253 this->setType(gGestureClickType);
1254 }
1255
1256 static bool IsGesture(Click* click) {
1257 return click->isType(gGestureClickType);
1258 }
1259};
1260
1261SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1262 return new GestureClick(this);
1263}
1264
1265bool SampleWindow::onClick(Click* click) {
1266 if (GestureClick::IsGesture(click)) {
1267 float x = SkScalarToFloat(click->fCurr.fX);
1268 float y = SkScalarToFloat(click->fCurr.fY);
1269 switch (click->fState) {
1270 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00001271 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001272 break;
1273 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00001274 fGesture.touchMoved(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001275 this->inval(NULL);
1276 break;
1277 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00001278 fGesture.touchEnd(click->fOwner);
reed@google.com52f57e12011-03-16 12:10:02 +00001279 this->inval(NULL);
1280 break;
1281 }
1282 return true;
1283 }
1284 return false;
1285}
1286
1287///////////////////////////////////////////////////////////////////////////////
1288
reed@android.com8a1c16f2008-12-17 15:59:43 +00001289void SampleWindow::loadView(SkView* view) {
1290 SkView::F2BIter iter(this);
1291 SkView* prev = iter.next();
1292 if (prev) {
1293 prev->detachFromParent();
1294 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001295
reed@android.com34245c72009-11-03 04:00:48 +00001296 if (NULL == view) {
1297 view = create_overview(fSamples.count(), fSamples.begin());
1298 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001299 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001300 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001301 this->attachChildToFront(view)->unref();
1302 view->setSize(this->width(), this->height());
1303
1304 this->updateTitle();
1305}
1306
1307static const char* gConfigNames[] = {
1308 "unknown config",
1309 "A1",
1310 "A8",
1311 "Index8",
1312 "565",
1313 "4444",
1314 "8888"
1315};
1316
1317static const char* configToString(SkBitmap::Config c) {
1318 return gConfigNames[c];
1319}
1320
1321static const char* gCanvasTypePrefix[] = {
1322 "raster: ",
1323 "picture: ",
1324 "opengl: "
1325};
1326
reed@google.com569e0432011-04-05 13:07:03 +00001327static const char* trystate_str(SkTriState state,
1328 const char trueStr[], const char falseStr[]) {
1329 if (kTrue_SkTriState == state) {
1330 return trueStr;
1331 } else if (kFalse_SkTriState == state) {
1332 return falseStr;
1333 }
1334 return NULL;
1335}
1336
reed@android.com8a1c16f2008-12-17 15:59:43 +00001337void SampleWindow::updateTitle() {
1338 SkString title;
1339
1340 SkView::F2BIter iter(this);
1341 SkView* view = iter.next();
1342 SkEvent evt(gTitleEvtName);
1343 if (view->doQuery(&evt)) {
1344 title.set(evt.findString(gTitleEvtName));
1345 }
1346 if (title.size() == 0) {
1347 title.set("<unknown>");
1348 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001349
reed@android.com8a1c16f2008-12-17 15:59:43 +00001350 title.prepend(gCanvasTypePrefix[fCanvasType]);
1351
1352 title.prepend(" ");
1353 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001354
reed@android.com8a1c16f2008-12-17 15:59:43 +00001355 if (fAnimating) {
1356 title.prepend("<A> ");
1357 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001358 if (fScale) {
1359 title.prepend("<S> ");
1360 }
1361 if (fRotate) {
1362 title.prepend("<R> ");
1363 }
reed@android.come522ca52009-11-23 20:10:41 +00001364 if (fNClip) {
1365 title.prepend("<C> ");
1366 }
reed@google.com569e0432011-04-05 13:07:03 +00001367
1368 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1369 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001370 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001371 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1372 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001373
1374 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001375 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001376 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001377
1378 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001379 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001380 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001381 if (fUsePipe && SampleView::IsSampleView(view)) {
1382 title.prepend("<P> ");
1383 }
1384 if (SampleView::IsSampleView(view)) {
1385 title.prepend("! ");
1386 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001387
reed@android.com8a1c16f2008-12-17 15:59:43 +00001388 this->setTitle(title.c_str());
1389}
1390
1391void SampleWindow::onSizeChange() {
1392 this->INHERITED::onSizeChange();
1393
reed@google.com29038ed2011-07-06 17:56:47 +00001394 if (fGrContext) {
1395 this->attachGL();
1396
1397 GrPlatformSurfaceDesc desc;
1398 desc.reset();
1399 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
1400 desc.fWidth = SkScalarRound(this->width());
1401 desc.fHeight = SkScalarRound(this->height());
1402 desc.fConfig = kRGBA_8888_GrPixelConfig;
1403 desc.fStencilBits = 8;
1404 GrGLint buffer;
1405 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer);
1406 desc.fPlatformRenderTarget = buffer;
1407
1408 SkSafeUnref(fGrRenderTarget);
1409 fGrRenderTarget = static_cast<GrRenderTarget*>(
1410 fGrContext->createPlatformSurface(desc));
1411 }
1412
reed@android.com8a1c16f2008-12-17 15:59:43 +00001413 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}