blob: 55b1e48557844c6a23edbadd6d2e71f6001b82b1 [file] [log] [blame]
Scroggo2c8208f2011-06-15 16:49:08 +00001#include "SampleApp.h"
2
reed@google.com8a85d0c2011-06-24 19:12:12 +00003#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00004#include "SkCanvas.h"
5#include "SkDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00006#include "SkGpuCanvas.h"
reed@google.comaf951c92011-06-16 19:10:39 +00007#include "SkGpuDevice.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkGraphics.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +00009#include "SkImageEncoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkPaint.h"
11#include "SkPicture.h"
12#include "SkStream.h"
reed@android.com44177402009-11-23 21:07:51 +000013#include "SkTime.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkWindow.h"
15
16#include "SampleCode.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017#include "GrContext.h"
Scroggo0f185c22011-03-24 18:35:50 +000018#include "SkTypeface.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000019
Scroggo3e7ff9f2011-06-16 15:31:26 +000020#include "GrGLInterface.h"
Scroggo2c8208f2011-06-15 16:49:08 +000021
Scroggo8ac0d542011-06-21 14:44:57 +000022#include "SkPDFDevice.h"
23#include "SkPDFDocument.h"
24#include "SkStream.h"
25
yangsu@google.com1f394212011-06-01 18:03:34 +000026#define TEST_GPIPEx
27
28#ifdef TEST_GPIPE
29#define PIPE_FILE
30#define FILE_PATH "/path/to/drawing.data"
31#endif
32
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +000033#define USE_ARROWS_FOR_ZOOM true
reed@android.comf2b98d62010-12-20 18:26:13 +000034//#define DEFAULT_TO_GPU
35
reed@android.come191b162009-12-18 21:33:39 +000036extern SkView* create_overview(int, const SkViewFactory[]);
reed@android.com34245c72009-11-03 04:00:48 +000037
reed@android.comcb342352010-07-22 18:27:53 +000038#define SK_SUPPORT_GL
reed@android.com8a1c16f2008-12-17 15:59:43 +000039
40#define ANIMATING_EVENTTYPE "nextSample"
41#define ANIMATING_DELAY 750
42
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000043#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +000044 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000045#else
reed@google.combad8c872011-05-18 20:10:31 +000046 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000047#endif
reed@google.combad8c872011-05-18 20:10:31 +000048#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000049
reed@google.comac10a2d2010-12-22 21:39:39 +000050#ifdef SK_SUPPORT_GL
51 #include "GrGLConfig.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000052#endif
53
reed@google.com3cec4d72011-07-06 13:59:47 +000054static SampleWindow* gSampleWindow;
55
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000056///////////////
57static const char view_inval_msg[] = "view-inval-msg";
58
Scroggo62b65b02011-06-21 16:01:26 +000059void SampleWindow::postInvalDelay() {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000060 SkEvent* evt = new SkEvent(view_inval_msg);
Scroggo62b65b02011-06-21 16:01:26 +000061 evt->post(this->getSinkID(), 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000062}
63
64static bool isInvalEvent(const SkEvent& evt) {
65 return evt.isType(view_inval_msg);
66}
67//////////////////
68
reed@android.com8a1c16f2008-12-17 15:59:43 +000069SkViewRegister* SkViewRegister::gHead;
70SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) {
71 static bool gOnce;
72 if (!gOnce) {
73 gHead = NULL;
74 gOnce = true;
75 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000076
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 fChain = gHead;
78 gHead = this;
79}
80
reed@android.comf2b98d62010-12-20 18:26:13 +000081#if defined(SK_SUPPORT_GL)
82 #define SK_USE_SHADERS
83#endif
84
twiz@google.com06c3b6b2011-03-14 16:58:39 +000085#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +000086#include <CoreFoundation/CoreFoundation.h>
87#include <CoreFoundation/CFURLAccess.h>
88
89static void testpdf() {
90 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
91 kCFStringEncodingUTF8);
92 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
93 kCFURLPOSIXPathStyle,
94 false);
95 CFRelease(path);
96 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
97 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
98 CFRelease(url);
99
100 CGContextBeginPage(cg, &box);
101 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
102 CGContextFillEllipseInRect(cg, r);
103 CGContextEndPage(cg);
104 CGContextRelease(cg);
105
106 if (false) {
107 SkBitmap bm;
108 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
109 bm.allocPixels();
110 bm.eraseColor(0);
111
112 SkCanvas canvas(bm);
113
114 }
115}
116#endif
117
118//////////////////////////////////////////////////////////////////////////////
119
reed@google.com569e0432011-04-05 13:07:03 +0000120enum FlipAxisEnum {
121 kFlipAxis_X = (1 << 0),
122 kFlipAxis_Y = (1 << 1)
123};
124
reed@google.com569e0432011-04-05 13:07:03 +0000125static SkTriState cycle_tristate(SkTriState state) {
126 static const SkTriState gCycle[] = {
127 /* kFalse_SkTriState -> */ kUnknown_SkTriState,
128 /* kTrue_SkTriState -> */ kFalse_SkTriState,
129 /* kUnknown_SkTriState -> */ kTrue_SkTriState,
130 };
131 return gCycle[state];
132}
133
reed@google.comf0b5f682011-03-11 20:08:25 +0000134#include "SkDrawFilter.h"
135
reed@google.com569e0432011-04-05 13:07:03 +0000136class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000137public:
reed@google.com09e3baa2011-05-18 12:04:31 +0000138 FlagsDrawFilter(SkTriState lcd, SkTriState aa, SkTriState filter,
139 SkTriState hinting) :
140 fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000141
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000142 virtual void filter(SkPaint* paint, Type t) {
reed@google.com569e0432011-04-05 13:07:03 +0000143 if (kText_Type == t && kUnknown_SkTriState != fLCDState) {
reed@google.com569e0432011-04-05 13:07:03 +0000144 paint->setLCDRenderText(kTrue_SkTriState == fLCDState);
145 }
146 if (kUnknown_SkTriState != fAAState) {
reed@google.com569e0432011-04-05 13:07:03 +0000147 paint->setAntiAlias(kTrue_SkTriState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000148 }
reed@google.com176753a2011-05-17 15:32:04 +0000149 if (kUnknown_SkTriState != fFilterState) {
150 paint->setFilterBitmap(kTrue_SkTriState == fFilterState);
151 }
reed@google.com09e3baa2011-05-18 12:04:31 +0000152 if (kUnknown_SkTriState != fHintingState) {
153 paint->setHinting(kTrue_SkTriState == fHintingState ?
154 SkPaint::kNormal_Hinting :
155 SkPaint::kSlight_Hinting);
156 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000157 }
158
159private:
reed@google.com569e0432011-04-05 13:07:03 +0000160 SkTriState fLCDState;
reed@google.com569e0432011-04-05 13:07:03 +0000161 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000162 SkTriState fFilterState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000163 SkTriState fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000164};
165
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166//////////////////////////////////////////////////////////////////////////////
167
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000168#define MAX_ZOOM_LEVEL 8
169#define MIN_ZOOM_LEVEL -8
170
reed@android.comf2b98d62010-12-20 18:26:13 +0000171static const char gCharEvtName[] = "SampleCode_Char_Event";
172static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173static const char gTitleEvtName[] = "SampleCode_Title_Event";
174static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000175static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
176
177bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
178 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
179 if (outUni) {
180 *outUni = evt.getFast32();
181 }
182 return true;
183 }
184 return false;
185}
186
187bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
188 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
189 if (outKey) {
190 *outKey = (SkKey)evt.getFast32();
191 }
192 return true;
193 }
194 return false;
195}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196
197bool SampleCode::TitleQ(const SkEvent& evt) {
198 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
199}
200
201void SampleCode::TitleR(SkEvent* evt, const char title[]) {
202 SkASSERT(evt && TitleQ(*evt));
203 evt->setString(gTitleEvtName, title);
204}
205
206bool SampleCode::PrefSizeQ(const SkEvent& evt) {
207 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
208}
209
210void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
211 SkASSERT(evt && PrefSizeQ(*evt));
212 SkScalar size[2];
213 size[0] = width;
214 size[1] = height;
215 evt->setScalars(gPrefSizeEvtName, 2, size);
216}
217
reed@android.comf2b98d62010-12-20 18:26:13 +0000218bool SampleCode::FastTextQ(const SkEvent& evt) {
219 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
220}
221
222///////////////////////////////////////////////////////////////////////////////
223
reed@android.com44177402009-11-23 21:07:51 +0000224static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000225static SkMSec gAnimTimePrev;
226
reed@android.com44177402009-11-23 21:07:51 +0000227SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000228SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
229SkScalar SampleCode::GetAnimSecondsDelta() {
230 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
231}
reed@android.com44177402009-11-23 21:07:51 +0000232
233SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000234 // since gAnimTime can be up to 32 bits, we can't convert it to a float
235 // or we'll lose the low bits. Hence we use doubles for the intermediate
236 // calculations
237 double seconds = (double)gAnimTime / 1000.0;
238 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000239 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000240 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000241 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000242 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000243}
244
reed@google.com3cec4d72011-07-06 13:59:47 +0000245GrContext* SampleCode::GetGr() {
246 return gSampleWindow ? gSampleWindow->getGrContext() : NULL;
247}
248
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249//////////////////////////////////////////////////////////////////////////////
250
reed@android.comf2b98d62010-12-20 18:26:13 +0000251static SkView* curr_view(SkWindow* wind) {
252 SkView::F2BIter iter(wind);
253 return iter.next();
254}
255
Scroggo2c8208f2011-06-15 16:49:08 +0000256void SampleWindow::setZoomCenter(float x, float y)
257{
258 fZoomCenterX = SkFloatToScalar(x);
259 fZoomCenterY = SkFloatToScalar(y);
260}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261
djsollen@google.come32b5832011-06-13 16:58:40 +0000262bool SampleWindow::setGrContext(GrContext* context)
263{
264 if (fGrContext) {
265 fGrContext->unref();
266 }
267 fGrContext = context;
268 fGrContext->ref();
269 return true;
270}
271
272GrContext* SampleWindow::getGrContext()
273{
274 return fGrContext;
275}
djsollen@google.come32b5832011-06-13 16:58:40 +0000276
Scroggo0f185c22011-03-24 18:35:50 +0000277bool SampleWindow::zoomIn()
278{
279 // Arbitrarily decided
280 if (fFatBitsScale == 25) return false;
281 fFatBitsScale++;
282 this->inval(NULL);
283 return true;
284}
285
286bool SampleWindow::zoomOut()
287{
288 if (fFatBitsScale == 1) return false;
289 fFatBitsScale--;
290 this->inval(NULL);
291 return true;
292}
293
294void SampleWindow::toggleZoomer()
295{
296 fShowZoomer = !fShowZoomer;
297 this->inval(NULL);
298}
299
300void SampleWindow::updatePointer(int x, int y)
301{
302 fMouseX = x;
303 fMouseY = y;
304 if (fShowZoomer) {
305 this->inval(NULL);
306 }
307}
308
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000309bool SampleWindow::make3DReady() {
310
311#if defined(SK_SUPPORT_GL)
bsalomon@google.com498a6232011-03-10 18:24:15 +0000312 if (attachGL()) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000313 if (NULL != fGrContext) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000314 // various gr lifecycle tests
315 #if 0
316 fGrContext->freeGpuResources();
317 #elif 0
318 // this will leak resources.
319 fGrContext->contextLost();
320 #elif 0
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000321 GrAssert(1 == fGrContext->refcnt());
322 fGrContext->unref();
323 fGrContext = NULL;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000324 #endif
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000325 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000326
bsalomon@google.com498a6232011-03-10 18:24:15 +0000327 if (NULL == fGrContext) {
328 #if defined(SK_USE_SHADERS)
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000329 fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000330 #else
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000331 fGrContext = GrContext::Create(kOpenGL_Fixed_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000332 #endif
reed@google.com569e0432011-04-05 13:07:03 +0000333 SkDebugf("---- constructor\n");
bsalomon@google.com498a6232011-03-10 18:24:15 +0000334 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000335
bsalomon@google.com498a6232011-03-10 18:24:15 +0000336 if (NULL != fGrContext) {
337 return true;
338 } else {
339 detachGL();
340 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000341 }
342#endif
343 SkDebugf("Failed to setup 3D");
344 return false;
345}
346
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) {
348 static const CanvasType gCT[] = {
349 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000350 kGPU_CanvasType,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000351 kRaster_CanvasType
352 };
353 return gCT[ct];
354}
355
senorblanco@chromium.org78b82532011-06-28 19:44:03 +0000356SampleWindow::SampleWindow(void* hwnd, int argc, char** argv) : INHERITED(hwnd) {
reed@google.com3cec4d72011-07-06 13:59:47 +0000357 gSampleWindow = this;
358
yangsu@google.com1f394212011-06-01 18:03:34 +0000359#ifdef PIPE_FILE
360 //Clear existing file or create file if it doesn't exist
361 FILE* f = fopen(FILE_PATH, "wb");
362 fclose(f);
363#endif
364
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365 fPicture = NULL;
reed@android.comf2b98d62010-12-20 18:26:13 +0000366 fGpuCanvas = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000367
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000368 fGrContext = NULL;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000369
reed@android.comf2b98d62010-12-20 18:26:13 +0000370#ifdef DEFAULT_TO_GPU
371 fCanvasType = kGPU_CanvasType;
372#else
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 fCanvasType = kRaster_CanvasType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000374#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000375 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000376 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000377 fRepeatDrawing = false;
378 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000379 fRotate = false;
380 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000381 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000382 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000383 fMeasureFPS = false;
reed@google.com569e0432011-04-05 13:07:03 +0000384 fLCDState = kUnknown_SkTriState;
385 fAAState = kUnknown_SkTriState;
reed@google.com66f22fd2011-05-17 15:33:45 +0000386 fFilterState = kUnknown_SkTriState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000387 fHintingState = kUnknown_SkTriState;
reed@google.com569e0432011-04-05 13:07:03 +0000388 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000389 fScrollTestX = fScrollTestY = 0;
390
Scroggo0f185c22011-03-24 18:35:50 +0000391 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000392 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000393 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
394 fShowZoomer = false;
395
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000396 fZoomLevel = 0;
397 fZoomScale = SK_Scalar1;
398
Scroggo8ac0d542011-06-21 14:44:57 +0000399 fSaveToPdf = false;
400 fPdfCanvas = NULL;
401
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000402// this->setConfig(SkBitmap::kRGB_565_Config);
403 this->setConfig(SkBitmap::kARGB_8888_Config);
404 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000405 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000406
reed@android.com34245c72009-11-03 04:00:48 +0000407 {
408 const SkViewRegister* reg = SkViewRegister::Head();
409 while (reg) {
410 *fSamples.append() = reg->factory();
411 reg = reg->next();
412 }
413 }
414 fCurrIndex = 0;
senorblanco@chromium.org78b82532011-06-28 19:44:03 +0000415 if (argc > 1) {
416 int i, count = fSamples.count();
417 for (i = 0; i < count; i++) {
418 SkString title = getSampleTitle(i);
419 if (title.equals(argv[1])) {
420 fCurrIndex = i;
421 break;
422 }
423 }
424 if (i == count) {
425 fprintf(stderr, "Unknown sample \"%s\"\n", argv[1]);
426 }
427 }
reed@android.come0f13ee2009-11-04 19:40:25 +0000428 this->loadView(fSamples[fCurrIndex]());
reed@google.comf0b5f682011-03-11 20:08:25 +0000429
Scroggob4490c72011-06-17 13:53:05 +0000430 // If another constructor set our dimensions, ensure that our
431 // onSizeChange gets called.
432 if (this->height() && this->width()) {
433 this->onSizeChange();
434 }
yangsu@google.com501775e2011-06-24 16:04:50 +0000435
436 fPDFData = NULL;
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000437#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +0000438 testpdf();
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000439#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440}
441
442SampleWindow::~SampleWindow() {
443 delete fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000444 delete fGpuCanvas;
Scroggo8ac0d542011-06-21 14:44:57 +0000445 delete fPdfCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000446 if (NULL != fGrContext) {
447 fGrContext->unref();
448 }
Scroggo0f185c22011-03-24 18:35:50 +0000449 fTypeface->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000450}
451
reed@android.com55e76b22009-11-23 21:46:47 +0000452static SkBitmap capture_bitmap(SkCanvas* canvas) {
453 SkBitmap bm;
454 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
455 src.copyTo(&bm, src.config());
456 return bm;
457}
458
459static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
460 SkBitmap* diff) {
461 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000462
reed@android.com55e76b22009-11-23 21:46:47 +0000463 SkAutoLockPixels alp0(src);
464 SkAutoLockPixels alp1(orig);
465 for (int y = 0; y < src.height(); y++) {
466 const void* srcP = src.getAddr(0, y);
467 const void* origP = orig.getAddr(0, y);
468 size_t bytes = src.width() * src.bytesPerPixel();
469 if (memcmp(srcP, origP, bytes)) {
470 SkDebugf("---------- difference on line %d\n", y);
471 return true;
472 }
473 }
474 return false;
475}
476
Scroggo0f185c22011-03-24 18:35:50 +0000477static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
478{
479 SkColor desiredColor = paint.getColor();
480 paint.setColor(SK_ColorWHITE);
481 const char* c_str = string.c_str();
482 size_t size = string.size();
483 SkRect bounds;
484 paint.measureText(c_str, size, &bounds);
485 bounds.offset(left, top);
486 SkScalar inset = SkIntToScalar(-2);
487 bounds.inset(inset, inset);
488 canvas->drawRect(bounds, paint);
489 if (desiredColor != SK_ColorBLACK) {
490 paint.setColor(SK_ColorBLACK);
491 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
492 }
493 paint.setColor(desiredColor);
494 canvas->drawText(c_str, size, left, top, paint);
495}
496
reed@android.com44177402009-11-23 21:07:51 +0000497#define XCLIP_N 8
498#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000499
500void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000501 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000502 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000503 gAnimTime = SkTime::GetMSecs();
504
Scroggo2c8208f2011-06-15 16:49:08 +0000505 SkScalar cx = fZoomCenterX;
506 SkScalar cy = fZoomCenterY;
reed@google.com569e0432011-04-05 13:07:03 +0000507
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000508 if (fZoomLevel) {
509 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000510 SkPoint center;
511 m = canvas->getTotalMatrix();//.invert(&m);
512 m.mapXY(cx, cy, &center);
513 cx = center.fX;
514 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000515
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000516 m.setTranslate(-cx, -cy);
517 m.postScale(fZoomScale, fZoomScale);
518 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000519
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000520 canvas->concat(m);
521 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000522
reed@google.com569e0432011-04-05 13:07:03 +0000523 if (fFlipAxis) {
524 SkMatrix m;
525 m.setTranslate(cx, cy);
526 if (fFlipAxis & kFlipAxis_X) {
527 m.preScale(-SK_Scalar1, SK_Scalar1);
528 }
529 if (fFlipAxis & kFlipAxis_Y) {
530 m.preScale(SK_Scalar1, -SK_Scalar1);
531 }
532 m.preTranslate(-cx, -cy);
533 canvas->concat(m);
534 }
535
reed@google.com52f57e12011-03-16 12:10:02 +0000536 // Apply any gesture matrix
537 if (true) {
538 const SkMatrix& localM = fGesture.localM();
539 if (localM.getType() & SkMatrix::kScale_Mask) {
540 canvas->setExternalMatrix(&localM);
541 }
542 canvas->concat(localM);
543 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000544
reed@google.com52f57e12011-03-16 12:10:02 +0000545 if (fGesture.isActive()) {
546 this->inval(NULL);
547 }
548 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000549
reed@android.come522ca52009-11-23 20:10:41 +0000550 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000551 this->INHERITED::draw(canvas);
552 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000553
554 const SkScalar w = this->width();
555 const SkScalar h = this->height();
556 const SkScalar cw = w / XCLIP_N;
557 const SkScalar ch = h / YCLIP_N;
558 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000559 SkRect r;
560 r.fTop = y * ch;
561 r.fBottom = (y + 1) * ch;
562 if (y == YCLIP_N - 1) {
563 r.fBottom = h;
564 }
reed@android.come522ca52009-11-23 20:10:41 +0000565 for (int x = 0; x < XCLIP_N; x++) {
566 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000567 r.fLeft = x * cw;
568 r.fRight = (x + 1) * cw;
569 if (x == XCLIP_N - 1) {
570 r.fRight = w;
571 }
reed@android.come522ca52009-11-23 20:10:41 +0000572 canvas->clipRect(r);
573 this->INHERITED::draw(canvas);
574 }
575 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000576
reed@android.com55e76b22009-11-23 21:46:47 +0000577 SkBitmap diff;
578 if (bitmap_diff(canvas, orig, &diff)) {
579 }
reed@android.come522ca52009-11-23 20:10:41 +0000580 } else {
581 this->INHERITED::draw(canvas);
582 }
Scroggo8ac0d542011-06-21 14:44:57 +0000583 if (fShowZoomer && fCanvasType != kGPU_CanvasType && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +0000584 // In the GPU case, INHERITED::draw calls beforeChildren, which
585 // creates an SkGpuCanvas. All further draw calls are directed
586 // at that canvas, which is deleted in afterChildren (which is
587 // also called by draw), so we cannot show the zoomer here.
588 // Instead, we call it inside afterChildren.
589 showZoomer(canvas);
590 }
591}
592
593void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +0000594 int count = canvas->save();
595 canvas->resetMatrix();
596 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000597 int width = SkScalarRound(this->width());
598 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000599 if (fMouseX >= width) fMouseX = width - 1;
600 else if (fMouseX < 0) fMouseX = 0;
601 if (fMouseY >= height) fMouseY = height - 1;
602 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000603
Scroggo0f185c22011-03-24 18:35:50 +0000604 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000605 bitmap.lockPixels();
606
Scroggo0f185c22011-03-24 18:35:50 +0000607 // 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 +0000608 int zoomedWidth = (width >> 1) | 1;
609 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000610 SkIRect src;
611 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
612 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
613 SkRect dest;
614 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
615 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
616 SkPaint paint;
617 // Clear the background behind our zoomed in view
618 paint.setColor(SK_ColorWHITE);
619 canvas->drawRect(dest, paint);
620 canvas->drawBitmapRect(bitmap, &src, dest);
621 paint.setColor(SK_ColorBLACK);
622 paint.setStyle(SkPaint::kStroke_Style);
623 // Draw a border around the pixel in the middle
624 SkRect originalPixel;
625 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
626 SkMatrix matrix;
627 SkRect scalarSrc;
628 scalarSrc.set(src);
629 SkColor color = bitmap.getColor(fMouseX, fMouseY);
630 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
631 SkRect pixel;
632 matrix.mapRect(&pixel, originalPixel);
633 // TODO Perhaps measure the values and make the outline white if it's "dark"
634 if (color == SK_ColorBLACK) {
635 paint.setColor(SK_ColorWHITE);
636 }
637 canvas->drawRect(pixel, paint);
638 }
639 paint.setColor(SK_ColorBLACK);
640 // Draw a border around the destination rectangle
641 canvas->drawRect(dest, paint);
642 paint.setStyle(SkPaint::kStrokeAndFill_Style);
643 // Identify the pixel and its color on screen
644 paint.setTypeface(fTypeface);
645 paint.setAntiAlias(true);
646 SkScalar lineHeight = paint.getFontMetrics(NULL);
647 SkString string;
648 string.appendf("(%i, %i)", fMouseX, fMouseY);
649 SkScalar left = dest.fLeft + SkIntToScalar(3);
650 SkScalar i = SK_Scalar1;
651 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
652 // Alpha
653 i += SK_Scalar1;
654 string.reset();
655 string.appendf("A: %X", SkColorGetA(color));
656 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
657 // Red
658 i += SK_Scalar1;
659 string.reset();
660 string.appendf("R: %X", SkColorGetR(color));
661 paint.setColor(SK_ColorRED);
662 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
663 // Green
664 i += SK_Scalar1;
665 string.reset();
666 string.appendf("G: %X", SkColorGetG(color));
667 paint.setColor(SK_ColorGREEN);
668 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
669 // Blue
670 i += SK_Scalar1;
671 string.reset();
672 string.appendf("B: %X", SkColorGetB(color));
673 paint.setColor(SK_ColorBLUE);
674 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
675 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +0000676}
677
reed@android.com8a1c16f2008-12-17 15:59:43 +0000678void SampleWindow::onDraw(SkCanvas* canvas) {
679 if (fRepeatDrawing) {
680 this->inval(NULL);
681 }
682}
683
684#include "SkColorPriv.h"
685
686static void reverseRedAndBlue(const SkBitmap& bm) {
687 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
688 uint8_t* p = (uint8_t*)bm.getPixels();
689 uint8_t* stop = p + bm.getSize();
690 while (p < stop) {
691 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
692 unsigned scale = SkAlpha255To256(p[3]);
693 unsigned r = p[2];
694 unsigned b = p[0];
695 p[0] = SkAlphaMul(r, scale);
696 p[1] = SkAlphaMul(p[1], scale);
697 p[2] = SkAlphaMul(b, scale);
698 p += 4;
699 }
700}
701
Scroggo8ac0d542011-06-21 14:44:57 +0000702void SampleWindow::saveToPdf()
703{
704 fSaveToPdf = true;
705 this->inval(NULL);
706}
707
reed@android.com8a1c16f2008-12-17 15:59:43 +0000708SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000709 if (kGPU_CanvasType != fCanvasType) {
reed@android.com6efdc472008-12-19 18:24:35 +0000710#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000711 detachGL();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000712#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000713 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000714
Scroggo8ac0d542011-06-21 14:44:57 +0000715 if (fSaveToPdf) {
716 const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
717 SkISize size = SkISize::Make(bmp.width(), bmp.height());
718 SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
719 canvas->getTotalMatrix());
720 fPdfCanvas = new SkCanvas(pdfDevice);
721 pdfDevice->unref();
722 canvas = fPdfCanvas;
723 } else {
724 switch (fCanvasType) {
725 case kRaster_CanvasType:
reed@android.comf2b98d62010-12-20 18:26:13 +0000726 canvas = this->INHERITED::beforeChildren(canvas);
Scroggo8ac0d542011-06-21 14:44:57 +0000727 break;
728 case kPicture_CanvasType:
729 fPicture = new SkPicture;
730 canvas = fPicture->beginRecording(9999, 9999);
731 break;
732 case kGPU_CanvasType: {
733 if (make3DReady()) {
734 SkDevice* device = canvas->getDevice();
735 const SkBitmap& bitmap = device->accessBitmap(true);
736
737 GrRenderTarget* renderTarget;
738
739 GrPlatformSurfaceDesc desc;
740 desc.reset();
741 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
742 desc.fWidth = bitmap.width();
743 desc.fHeight = bitmap.height();
744 desc.fConfig = kRGBA_8888_GrPixelConfig;
745 desc.fStencilBits = 8;
746 GrGLint buffer;
747 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer);
748 desc.fPlatformRenderTarget = buffer;
749
750 renderTarget = static_cast<GrRenderTarget*>(
751 fGrContext->createPlatformSurface(desc));
752 fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
753 renderTarget->unref();
754
755 device = new SkGpuDevice(fGrContext, renderTarget);
756 fGpuCanvas->setDevice(device)->unref();
757
758 fGpuCanvas->concat(canvas->getTotalMatrix());
759 canvas = fGpuCanvas;
760
761 } else {
762 canvas = this->INHERITED::beforeChildren(canvas);
763 }
764 break;
reed@android.comf2b98d62010-12-20 18:26:13 +0000765 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000766 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000767 }
768
769 if (fUseClip) {
770 canvas->drawColor(0xFFFF88FF);
771 canvas->clipPath(fClipPath);
772 }
773
774 return canvas;
775}
776
777static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
778 const SkRegion& rgn) {
779 SkCanvas canvas(bm);
780 SkRegion inval(rgn);
781
782 inval.translate(r.fLeft, r.fTop);
783 canvas.clipRegion(inval);
784 canvas.drawColor(0xFFFF8080);
785}
yangsu@google.com501775e2011-06-24 16:04:50 +0000786#include "SkData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +0000787void SampleWindow::afterChildren(SkCanvas* orig) {
Scroggo8ac0d542011-06-21 14:44:57 +0000788 if (fSaveToPdf) {
789 fSaveToPdf = false;
790 if (fShowZoomer) {
791 showZoomer(fPdfCanvas);
792 }
793 SkString name;
794 name.printf("%s.pdf", this->getTitle());
795 SkPDFDocument doc;
796 SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
797 doc.appendPage(device);
798#ifdef ANDROID
799 name.prepend("/sdcard/");
800#endif
yangsu@google.com501775e2011-06-24 16:04:50 +0000801
802#ifdef SK_BUILD_FOR_IOS
803 SkDynamicMemoryWStream mstream;
804 doc.emitPDF(&mstream);
yangsu@google.comae8a2e52011-06-24 21:09:39 +0000805 fPDFData = mstream.copyToData();
yangsu@google.com501775e2011-06-24 16:04:50 +0000806#endif
Scroggo8ac0d542011-06-21 14:44:57 +0000807 SkFILEWStream stream(name.c_str());
808 if (stream.isValid()) {
809 doc.emitPDF(&stream);
810 const char* desc = "File saved from Skia SampleApp";
811 this->onPDFSaved(this->getTitle(), desc, name.c_str());
812 }
yangsu@google.com501775e2011-06-24 16:04:50 +0000813
Scroggo8ac0d542011-06-21 14:44:57 +0000814 delete fPdfCanvas;
815 fPdfCanvas = NULL;
816
817 // We took over the draw calls in order to create the PDF, so we need
818 // to redraw.
819 this->inval(NULL);
820 return;
821 }
822
reed@android.comf2b98d62010-12-20 18:26:13 +0000823 if (fRequestGrabImage) {
824 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000825
reed@android.comf2b98d62010-12-20 18:26:13 +0000826 SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
827 SkDevice* device = canvas->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000828 SkBitmap bmp;
829 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000830 static int gSampleGrabCounter;
831 SkString name;
832 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000833 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000834 SkImageEncoder::kPNG_Type, 100);
835 }
836 }
837
reed@android.com8a1c16f2008-12-17 15:59:43 +0000838 switch (fCanvasType) {
839 case kRaster_CanvasType:
840 break;
841 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000842 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000843 SkPicture* pict = new SkPicture(*fPicture);
844 fPicture->unref();
845 orig->drawPicture(*pict);
846 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000847 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000848 SkDynamicMemoryWStream ostream;
849 fPicture->serialize(&ostream);
850 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000851
reed@google.com8a85d0c2011-06-24 19:12:12 +0000852 SkAutoDataUnref data(ostream.copyToData());
853 SkMemoryStream istream(data.data(), data.size());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000854 SkPicture pict(&istream);
855 orig->drawPicture(pict);
856 } else {
857 fPicture->draw(orig);
858 fPicture->unref();
859 }
860 fPicture = NULL;
861 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000862#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000863 case kGPU_CanvasType:
Scroggoaed68d92011-06-08 14:26:00 +0000864 if (fShowZoomer && fGpuCanvas) {
Scroggo3272ba82011-05-25 20:50:42 +0000865 this->showZoomer(fGpuCanvas);
866 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000867 delete fGpuCanvas;
868 fGpuCanvas = NULL;
869 presentGL();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000870 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000871#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000872 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000873
reed@google.com17d7aec2011-04-25 14:31:44 +0000874 // Do this after presentGL and other finishing, rather than in afterChild
875 if (fMeasureFPS && fMeasureFPS_Time) {
876 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
877 this->updateTitle();
Scroggo62b65b02011-06-21 16:01:26 +0000878 this->postInvalDelay();
reed@google.com17d7aec2011-04-25 14:31:44 +0000879 }
880
881 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000882 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000883 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
884 int dx = fScrollTestX * 7;
885 int dy = fScrollTestY * 7;
886 SkIRect r;
887 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000888
reed@android.com8a1c16f2008-12-17 15:59:43 +0000889 r.set(50, 50, 50+100, 50+100);
890 bm.scrollRect(&r, dx, dy, &inval);
891 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000892 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000893}
894
reed@android.com6c5f6f22009-08-14 16:08:38 +0000895void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
896 if (fScale) {
897 SkScalar scale = SK_Scalar1 * 7 / 10;
898 SkScalar cx = this->width() / 2;
899 SkScalar cy = this->height() / 2;
900 canvas->translate(cx, cy);
901 canvas->scale(scale, scale);
902 canvas->translate(-cx, -cy);
903 }
904 if (fRotate) {
905 SkScalar cx = this->width() / 2;
906 SkScalar cy = this->height() / 2;
907 canvas->translate(cx, cy);
908 canvas->rotate(SkIntToScalar(30));
909 canvas->translate(-cx, -cy);
910 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000911
reed@google.com09e3baa2011-05-18 12:04:31 +0000912 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
913 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000914
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000915 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000916 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
917 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
918 fMeasureFPS_Time = SkTime::GetMSecs();
919 }
920 } else {
921 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000922 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000923 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000924}
925
926void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000927 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000928}
929
reed@android.com8a1c16f2008-12-17 15:59:43 +0000930static SkBitmap::Config gConfigCycle[] = {
931 SkBitmap::kNo_Config, // none -> none
932 SkBitmap::kNo_Config, // a1 -> none
933 SkBitmap::kNo_Config, // a8 -> none
934 SkBitmap::kNo_Config, // index8 -> none
935 SkBitmap::kARGB_4444_Config, // 565 -> 4444
936 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
937 SkBitmap::kRGB_565_Config // 8888 -> 565
938};
939
940static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
941 return gConfigCycle[c];
942}
943
djsollen@google.come32b5832011-06-13 16:58:40 +0000944void SampleWindow::changeZoomLevel(float delta) {
945 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000946 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000947 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
948 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000949 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000950 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
951 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000952 } else {
953 fZoomScale = SK_Scalar1;
954 }
955
djsollen@google.come32b5832011-06-13 16:58:40 +0000956 this->updateTitle();
957
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000958 this->inval(NULL);
959}
960
Scroggo2c8208f2011-06-15 16:49:08 +0000961bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +0000962 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
Scroggo2c8208f2011-06-15 16:49:08 +0000963 this->loadView(fSamples[fCurrIndex]());
964 return true;
965}
966
reed@android.com8a1c16f2008-12-17 15:59:43 +0000967bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000968 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
969 this->loadView(fSamples[fCurrIndex]());
970 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000971}
972
yangsu@google.com501775e2011-06-24 16:04:50 +0000973bool SampleWindow::goToSample(int i) {
974 fCurrIndex = (i) % fSamples.count();
975 this->loadView(fSamples[fCurrIndex]());
976 return true;
977}
978
979SkString SampleWindow::getSampleTitle(int i) {
980 SkView* view = fSamples[i]();
981 SkString title;
982 SkEvent evt(gTitleEvtName);
983 if (view->doQuery(&evt)) {
984 title.set(evt.findString(gTitleEvtName));
985 }
986 view->unref();
987 return title;
988}
989
990int SampleWindow::sampleCount() {
991 return fSamples.count();
992}
993
Scroggo2c8208f2011-06-15 16:49:08 +0000994void SampleWindow::postAnimatingEvent() {
995 if (fAnimating) {
996 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
997 evt->post(this->getSinkID(), ANIMATING_DELAY);
998 }
999}
1000
reed@android.com8a1c16f2008-12-17 15:59:43 +00001001bool SampleWindow::onEvent(const SkEvent& evt) {
1002 if (evt.isType(ANIMATING_EVENTTYPE)) {
1003 if (fAnimating) {
1004 this->nextSample();
1005 this->postAnimatingEvent();
1006 }
1007 return true;
1008 }
reed@android.com34245c72009-11-03 04:00:48 +00001009 if (evt.isType("set-curr-index")) {
1010 fCurrIndex = evt.getFast32() % fSamples.count();
1011 this->loadView(fSamples[fCurrIndex]());
1012 return true;
1013 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001014 if (isInvalEvent(evt)) {
1015 this->inval(NULL);
1016 return true;
1017 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001018 return this->INHERITED::onEvent(evt);
1019}
1020
reed@android.comf2b98d62010-12-20 18:26:13 +00001021bool SampleWindow::onQuery(SkEvent* query) {
1022 if (query->isType("get-slide-count")) {
1023 query->setFast32(fSamples.count());
1024 return true;
1025 }
1026 if (query->isType("get-slide-title")) {
1027 SkView* view = fSamples[query->getFast32()]();
1028 SkEvent evt(gTitleEvtName);
1029 if (view->doQuery(&evt)) {
1030 query->setString("title", evt.findString(gTitleEvtName));
1031 }
1032 SkSafeUnref(view);
1033 return true;
1034 }
1035 if (query->isType("use-fast-text")) {
1036 SkEvent evt(gFastTextEvtName);
1037 return curr_view(this)->doQuery(&evt);
1038 }
1039 return this->INHERITED::onQuery(query);
1040}
1041
reed@android.com0ae6b242008-12-23 16:49:54 +00001042static void cleanup_for_filename(SkString* name) {
1043 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +00001044 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +00001045 switch (str[i]) {
1046 case ':': str[i] = '-'; break;
1047 case '/': str[i] = '-'; break;
1048 case ' ': str[i] = '_'; break;
1049 default: break;
1050 }
1051 }
1052}
reed@android.com8a1c16f2008-12-17 15:59:43 +00001053
1054bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001055 {
1056 SkView* view = curr_view(this);
1057 if (view) {
1058 SkEvent evt(gCharEvtName);
1059 evt.setFast32(uni);
1060 if (view->doQuery(&evt)) {
1061 return true;
1062 }
1063 }
1064 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001065
reed@android.com8a1c16f2008-12-17 15:59:43 +00001066 int dx = 0xFF;
1067 int dy = 0xFF;
1068
1069 switch (uni) {
1070 case '5': dx = 0; dy = 0; break;
1071 case '8': dx = 0; dy = -1; break;
1072 case '6': dx = 1; dy = 0; break;
1073 case '2': dx = 0; dy = 1; break;
1074 case '4': dx = -1; dy = 0; break;
1075 case '7': dx = -1; dy = -1; break;
1076 case '9': dx = 1; dy = -1; break;
1077 case '3': dx = 1; dy = 1; break;
1078 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001079
reed@android.com8a1c16f2008-12-17 15:59:43 +00001080 default:
1081 break;
1082 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001083
reed@android.com8a1c16f2008-12-17 15:59:43 +00001084 if (0xFF != dx && 0xFF != dy) {
1085 if ((dx | dy) == 0) {
1086 fScrollTestX = fScrollTestY = 0;
1087 } else {
1088 fScrollTestX += dx;
1089 fScrollTestY += dy;
1090 }
1091 this->inval(NULL);
1092 return true;
1093 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001094
reed@android.com0ae6b242008-12-23 16:49:54 +00001095 switch (uni) {
1096 case 'a':
Scroggo2c8208f2011-06-15 16:49:08 +00001097 this->toggleSlideshow();
reed@android.com0ae6b242008-12-23 16:49:54 +00001098 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001099 case 'b':
1100 fAAState = cycle_tristate(fAAState);
1101 this->updateTitle();
1102 this->inval(NULL);
1103 break;
1104 case 'c':
1105 fUseClip = !fUseClip;
1106 this->inval(NULL);
1107 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +00001108 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001109 case 'd':
1110 SkGraphics::SetFontCacheUsed(0);
1111 return true;
Scroggo8ac0d542011-06-21 14:44:57 +00001112 case 'e':
1113 this->saveToPdf();
1114 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001115 case 'f':
Scroggo2c8208f2011-06-15 16:49:08 +00001116 this->toggleFPS();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001117 break;
1118 case 'g':
1119 fRequestGrabImage = true;
1120 this->inval(NULL);
1121 break;
reed@google.com09e3baa2011-05-18 12:04:31 +00001122 case 'h':
1123 fHintingState = cycle_tristate(fHintingState);
1124 this->updateTitle();
1125 this->inval(NULL);
1126 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001127 case 'i':
1128 this->zoomIn();
1129 break;
1130 case 'l':
1131 fLCDState = cycle_tristate(fLCDState);
1132 this->updateTitle();
1133 this->inval(NULL);
1134 break;
reed@google.com176753a2011-05-17 15:32:04 +00001135 case 'n':
1136 fFilterState = cycle_tristate(fFilterState);
1137 this->updateTitle();
1138 this->inval(NULL);
1139 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001140 case 'o':
1141 this->zoomOut();
1142 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001143 case 'p':
1144 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001145 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001146 this->inval(NULL);
1147 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001148 case 'r':
1149 fRotate = !fRotate;
1150 this->inval(NULL);
1151 this->updateTitle();
1152 return true;
1153 case 's':
1154 fScale = !fScale;
1155 this->inval(NULL);
1156 this->updateTitle();
1157 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001158 case 'x':
1159 fFlipAxis ^= kFlipAxis_X;
1160 this->updateTitle();
1161 this->inval(NULL);
1162 break;
1163 case 'y':
1164 fFlipAxis ^= kFlipAxis_Y;
1165 this->updateTitle();
1166 this->inval(NULL);
1167 break;
scroggo08526c02011-03-22 14:03:21 +00001168 case 'z':
1169 this->toggleZoomer();
1170 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001171 default:
1172 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001173 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001174
reed@android.com8a1c16f2008-12-17 15:59:43 +00001175 return this->INHERITED::onHandleChar(uni);
1176}
1177
Scroggo2c8208f2011-06-15 16:49:08 +00001178void SampleWindow::toggleFPS() {
1179 fMeasureFPS = !fMeasureFPS;
1180 this->inval(NULL);
1181 this->updateTitle();
1182}
1183
1184void SampleWindow::toggleSlideshow() {
1185 fAnimating = !fAnimating;
1186 this->postAnimatingEvent();
1187 this->updateTitle();
1188}
1189
1190void SampleWindow::toggleRendering() {
1191 fCanvasType = cycle_canvastype(fCanvasType);
1192 this->updateTitle();
1193 this->inval(NULL);
1194}
1195
reed@android.com8a1c16f2008-12-17 15:59:43 +00001196#include "SkDumpCanvas.h"
1197
1198bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001199 {
1200 SkView* view = curr_view(this);
1201 if (view) {
1202 SkEvent evt(gKeyEvtName);
1203 evt.setFast32(key);
1204 if (view->doQuery(&evt)) {
1205 return true;
1206 }
1207 }
1208 }
1209
reed@android.com8a1c16f2008-12-17 15:59:43 +00001210 switch (key) {
1211 case kRight_SkKey:
1212 if (this->nextSample()) {
1213 return true;
1214 }
1215 break;
1216 case kLeft_SkKey:
Scroggo2c8208f2011-06-15 16:49:08 +00001217 toggleRendering();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001218 return true;
1219 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001220 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001221 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001222 } else {
1223 fNClip = !fNClip;
1224 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001225 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001226 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001227 return true;
1228 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001229 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001230 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001231 } else {
1232 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001233 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001234 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001235 return true;
1236 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001237 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001238 SkDebugfDumper dumper;
1239 SkDumpCanvas dc(&dumper);
1240 this->draw(&dc);
1241 } else {
1242 fRepeatDrawing = !fRepeatDrawing;
1243 if (fRepeatDrawing) {
1244 this->inval(NULL);
1245 }
1246 }
1247 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001248 case kBack_SkKey:
1249 this->loadView(NULL);
1250 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001251 default:
1252 break;
1253 }
1254 return this->INHERITED::onHandleKey(key);
1255}
1256
reed@google.com52f57e12011-03-16 12:10:02 +00001257///////////////////////////////////////////////////////////////////////////////
1258
1259static const char gGestureClickType[] = "GestureClickType";
1260
Scroggod3aed392011-06-22 13:26:56 +00001261bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
1262 void* owner) {
Scroggo0f185c22011-03-24 18:35:50 +00001263 if (Click::kMoved_State == state) {
1264 updatePointer(x, y);
1265 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001266 int w = SkScalarRound(this->width());
1267 int h = SkScalarRound(this->height());
1268
1269 // check for the resize-box
1270 if (w - x < 16 && h - y < 16) {
1271 return false; // let the OS handle the click
1272 } else {
Scroggod3aed392011-06-22 13:26:56 +00001273 return this->INHERITED::onDispatchClick(x, y, state, owner);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001274 }
1275}
1276
reed@google.com52f57e12011-03-16 12:10:02 +00001277class GestureClick : public SkView::Click {
1278public:
1279 GestureClick(SkView* target) : SkView::Click(target) {
1280 this->setType(gGestureClickType);
1281 }
1282
1283 static bool IsGesture(Click* click) {
1284 return click->isType(gGestureClickType);
1285 }
1286};
1287
1288SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1289 return new GestureClick(this);
1290}
1291
1292bool SampleWindow::onClick(Click* click) {
1293 if (GestureClick::IsGesture(click)) {
1294 float x = SkScalarToFloat(click->fCurr.fX);
1295 float y = SkScalarToFloat(click->fCurr.fY);
1296 switch (click->fState) {
1297 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00001298 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001299 break;
1300 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00001301 fGesture.touchMoved(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001302 this->inval(NULL);
1303 break;
1304 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00001305 fGesture.touchEnd(click->fOwner);
reed@google.com52f57e12011-03-16 12:10:02 +00001306 this->inval(NULL);
1307 break;
1308 }
1309 return true;
1310 }
1311 return false;
1312}
1313
1314///////////////////////////////////////////////////////////////////////////////
1315
reed@android.com8a1c16f2008-12-17 15:59:43 +00001316void SampleWindow::loadView(SkView* view) {
1317 SkView::F2BIter iter(this);
1318 SkView* prev = iter.next();
1319 if (prev) {
1320 prev->detachFromParent();
1321 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001322
reed@android.com34245c72009-11-03 04:00:48 +00001323 if (NULL == view) {
1324 view = create_overview(fSamples.count(), fSamples.begin());
1325 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001326 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001327 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001328 this->attachChildToFront(view)->unref();
1329 view->setSize(this->width(), this->height());
1330
1331 this->updateTitle();
1332}
1333
1334static const char* gConfigNames[] = {
1335 "unknown config",
1336 "A1",
1337 "A8",
1338 "Index8",
1339 "565",
1340 "4444",
1341 "8888"
1342};
1343
1344static const char* configToString(SkBitmap::Config c) {
1345 return gConfigNames[c];
1346}
1347
1348static const char* gCanvasTypePrefix[] = {
1349 "raster: ",
1350 "picture: ",
1351 "opengl: "
1352};
1353
reed@google.com569e0432011-04-05 13:07:03 +00001354static const char* trystate_str(SkTriState state,
1355 const char trueStr[], const char falseStr[]) {
1356 if (kTrue_SkTriState == state) {
1357 return trueStr;
1358 } else if (kFalse_SkTriState == state) {
1359 return falseStr;
1360 }
1361 return NULL;
1362}
1363
reed@android.com8a1c16f2008-12-17 15:59:43 +00001364void SampleWindow::updateTitle() {
1365 SkString title;
1366
1367 SkView::F2BIter iter(this);
1368 SkView* view = iter.next();
1369 SkEvent evt(gTitleEvtName);
1370 if (view->doQuery(&evt)) {
1371 title.set(evt.findString(gTitleEvtName));
1372 }
1373 if (title.size() == 0) {
1374 title.set("<unknown>");
1375 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001376
reed@android.com8a1c16f2008-12-17 15:59:43 +00001377 title.prepend(gCanvasTypePrefix[fCanvasType]);
1378
1379 title.prepend(" ");
1380 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001381
reed@android.com8a1c16f2008-12-17 15:59:43 +00001382 if (fAnimating) {
1383 title.prepend("<A> ");
1384 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001385 if (fScale) {
1386 title.prepend("<S> ");
1387 }
1388 if (fRotate) {
1389 title.prepend("<R> ");
1390 }
reed@android.come522ca52009-11-23 20:10:41 +00001391 if (fNClip) {
1392 title.prepend("<C> ");
1393 }
reed@google.com569e0432011-04-05 13:07:03 +00001394
1395 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1396 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001397 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001398 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1399 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001400
1401 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001402 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001403 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001404
1405 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001406 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001407 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001408 if (fUsePipe && SampleView::IsSampleView(view)) {
1409 title.prepend("<P> ");
1410 }
1411 if (SampleView::IsSampleView(view)) {
1412 title.prepend("! ");
1413 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001414
reed@android.com8a1c16f2008-12-17 15:59:43 +00001415 this->setTitle(title.c_str());
1416}
1417
1418void SampleWindow::onSizeChange() {
1419 this->INHERITED::onSizeChange();
1420
1421 SkView::F2BIter iter(this);
1422 SkView* view = iter.next();
1423 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001424
reed@android.com8a1c16f2008-12-17 15:59:43 +00001425 // rebuild our clippath
1426 {
1427 const SkScalar W = this->width();
1428 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001429
reed@android.com8a1c16f2008-12-17 15:59:43 +00001430 fClipPath.reset();
1431#if 0
1432 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1433 SkRect r;
1434 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1435 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1436 fClipPath.addRect(r);
1437 }
1438#else
1439 SkRect r;
1440 r.set(0, 0, W, H);
1441 fClipPath.addRect(r, SkPath::kCCW_Direction);
1442 r.set(W/4, H/4, W*3/4, H*3/4);
1443 fClipPath.addRect(r, SkPath::kCW_Direction);
1444#endif
1445 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001446
Scroggo2c8208f2011-06-15 16:49:08 +00001447 fZoomCenterX = SkScalarHalf(this->width());
1448 fZoomCenterY = SkScalarHalf(this->height());
1449
Scroggo3e7ff9f2011-06-16 15:31:26 +00001450#ifdef ANDROID
Scroggob4490c72011-06-17 13:53:05 +00001451 // FIXME: The first draw after a size change does not work on Android, so
1452 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00001453 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00001454#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001455 this->updateTitle(); // to refresh our config
1456}
1457
1458///////////////////////////////////////////////////////////////////////////////
1459
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001460static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001461static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001462static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001463
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001464bool SampleView::IsSampleView(SkView* view) {
1465 SkEvent evt(is_sample_view_tag);
1466 return view->doQuery(&evt);
1467}
1468
reed@google.comf2183392011-04-22 14:10:48 +00001469bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001470 SkEvent evt(repeat_count_tag);
1471 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001472 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001473}
1474
reed@google.com0faac1e2011-05-11 05:58:58 +00001475bool SampleView::SetUsePipe(SkView* view, bool pred) {
1476 SkEvent evt(set_use_pipe_tag);
1477 evt.setFast32(pred);
1478 return view->doEvent(evt);
1479}
1480
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001481bool SampleView::onEvent(const SkEvent& evt) {
1482 if (evt.isType(repeat_count_tag)) {
1483 fRepeatCount = evt.getFast32();
1484 return true;
1485 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001486 if (evt.isType(set_use_pipe_tag)) {
1487 fUsePipe = !!evt.getFast32();
1488 return true;
1489 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001490 return this->INHERITED::onEvent(evt);
1491}
1492
1493bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001494 if (evt->isType(is_sample_view_tag)) {
1495 return true;
1496 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001497 return this->INHERITED::onQuery(evt);
1498}
1499
reed@google.com68f456d2011-05-02 18:55:39 +00001500#ifdef TEST_GPIPE
1501 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001502
1503class SimplePC : public SkGPipeController {
1504public:
1505 SimplePC(SkCanvas* target);
1506 ~SimplePC();
1507
1508 virtual void* requestBlock(size_t minRequest, size_t* actual);
1509 virtual void notifyWritten(size_t bytes);
1510
1511private:
reed@google.com961ddb02011-05-05 14:03:48 +00001512 SkGPipeReader fReader;
1513 void* fBlock;
1514 size_t fBlockSize;
1515 size_t fBytesWritten;
1516 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001517 SkGPipeReader::Status fStatus;
1518
1519 size_t fTotalWritten;
1520};
1521
1522SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1523 fBlock = NULL;
1524 fBlockSize = fBytesWritten = 0;
1525 fStatus = SkGPipeReader::kDone_Status;
1526 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001527 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001528}
1529
1530SimplePC::~SimplePC() {
1531// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1532 sk_free(fBlock);
1533
reed@google.com0faac1e2011-05-11 05:58:58 +00001534 if (fTotalWritten) {
1535 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1536 fAtomsWritten, fStatus);
1537 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001538}
1539
1540void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1541 sk_free(fBlock);
1542
1543 fBlockSize = minRequest * 4;
1544 fBlock = sk_malloc_throw(fBlockSize);
1545 fBytesWritten = 0;
1546 *actual = fBlockSize;
1547 return fBlock;
1548}
1549
1550void SimplePC::notifyWritten(size_t bytes) {
1551 SkASSERT(fBytesWritten + bytes <= fBlockSize);
yangsu@google.com1f394212011-06-01 18:03:34 +00001552
1553#ifdef PIPE_FILE
1554 //File is open in append mode
1555 FILE* f = fopen(FILE_PATH, "ab");
1556 SkASSERT(f != NULL);
1557 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
1558 fclose(f);
1559#endif
1560
reed@google.com64e3eb22011-05-04 14:32:04 +00001561 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1562 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1563 fBytesWritten += bytes;
1564 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001565
1566 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001567}
1568
reed@google.com68f456d2011-05-02 18:55:39 +00001569#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001570
reed@google.com64e3eb22011-05-04 14:32:04 +00001571
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001572void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001573#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001574 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001575 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001576 if (fUsePipe) {
reed@google.comdde09562011-05-23 12:21:05 +00001577 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
1578// flags = 0;
1579 canvas = writer.startRecording(&controller, flags);
reed@google.com0faac1e2011-05-11 05:58:58 +00001580 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001581#endif
1582
reed@google.com81e3d7f2011-06-01 12:42:36 +00001583 this->onDrawBackground(canvas);
1584
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001585 for (int i = 0; i < fRepeatCount; i++) {
1586 SkAutoCanvasRestore acr(canvas, true);
1587 this->onDrawContent(canvas);
1588 }
1589}
1590
1591void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001592 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001593}
1594
1595///////////////////////////////////////////////////////////////////////////////
1596
reed@android.comf2b98d62010-12-20 18:26:13 +00001597template <typename T> void SkTBSort(T array[], int count) {
1598 for (int i = 1; i < count - 1; i++) {
1599 bool didSwap = false;
1600 for (int j = count - 1; j > i; --j) {
1601 if (array[j] < array[j-1]) {
1602 T tmp(array[j-1]);
1603 array[j-1] = array[j];
1604 array[j] = tmp;
1605 didSwap = true;
1606 }
1607 }
1608 if (!didSwap) {
1609 break;
1610 }
1611 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001612
reed@android.comf2b98d62010-12-20 18:26:13 +00001613 for (int k = 0; k < count - 1; k++) {
1614 SkASSERT(!(array[k+1] < array[k]));
1615 }
1616}
1617
1618#include "SkRandom.h"
1619
1620static void rand_rect(SkIRect* rect, SkRandom& rand) {
1621 int bits = 8;
1622 int shift = 32 - bits;
1623 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1624 rand.nextU() >> shift, rand.nextU() >> shift);
1625 rect->sort();
1626}
1627
1628static void dumpRect(const SkIRect& r) {
1629 SkDebugf(" { %d, %d, %d, %d },\n",
1630 r.fLeft, r.fTop,
1631 r.fRight, r.fBottom);
1632}
1633
1634static void test_rects(const SkIRect rect[], int count) {
1635 SkRegion rgn0, rgn1;
1636
1637 for (int i = 0; i < count; i++) {
1638 rgn0.op(rect[i], SkRegion::kUnion_Op);
1639 // dumpRect(rect[i]);
1640 }
1641 rgn1.setRects(rect, count);
1642
1643 if (rgn0 != rgn1) {
1644 SkDebugf("\n");
1645 for (int i = 0; i < count; i++) {
1646 dumpRect(rect[i]);
1647 }
1648 SkDebugf("\n");
1649 }
1650}
1651
1652static void test() {
1653 size_t i;
1654
1655 const SkIRect r0[] = {
1656 { 0, 0, 1, 1 },
1657 { 2, 2, 3, 3 },
1658 };
1659 const SkIRect r1[] = {
1660 { 0, 0, 1, 3 },
1661 { 1, 1, 2, 2 },
1662 { 2, 0, 3, 3 },
1663 };
1664 const SkIRect r2[] = {
1665 { 0, 0, 1, 2 },
1666 { 2, 1, 3, 3 },
1667 { 4, 0, 5, 1 },
1668 { 6, 0, 7, 4 },
1669 };
1670
1671 static const struct {
1672 const SkIRect* fRects;
1673 int fCount;
1674 } gRecs[] = {
1675 { r0, SK_ARRAY_COUNT(r0) },
1676 { r1, SK_ARRAY_COUNT(r1) },
1677 { r2, SK_ARRAY_COUNT(r2) },
1678 };
1679
1680 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1681 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1682 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001683
reed@android.comf2b98d62010-12-20 18:26:13 +00001684 SkRandom rand;
1685 for (i = 0; i < 10000; i++) {
1686 SkRegion rgn0, rgn1;
1687
1688 const int N = 8;
1689 SkIRect rect[N];
1690 for (int j = 0; j < N; j++) {
1691 rand_rect(&rect[j], rand);
1692 }
1693 test_rects(rect, N);
1694 }
1695}
1696
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00001697SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001698// test();
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00001699 return new SampleWindow(hwnd, argc, argv);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001700}
1701
1702void get_preferred_size(int* x, int* y, int* width, int* height) {
1703 *x = 10;
1704 *y = 50;
1705 *width = 640;
1706 *height = 480;
1707}
1708
1709void application_init() {
1710// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001711#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001712 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001713#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001714 SkGraphics::Init();
1715 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001716}
1717
1718void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001719 SkEvent::Term();
1720 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001721}