blob: f0c4e59528d8829a7e72685510d9d7b417caab36 [file] [log] [blame]
Scroggo2c8208f2011-06-15 16:49:08 +00001#include "SampleApp.h"
2
reed@android.com8a1c16f2008-12-17 15:59:43 +00003#include "SkCanvas.h"
4#include "SkDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00005#include "SkGpuCanvas.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
yangsu@google.com1f394212011-06-01 18:03:34 +000021#define TEST_GPIPEx
22
23#ifdef TEST_GPIPE
24#define PIPE_FILE
25#define FILE_PATH "/path/to/drawing.data"
26#endif
27
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +000028#define USE_ARROWS_FOR_ZOOM true
reed@android.comf2b98d62010-12-20 18:26:13 +000029//#define DEFAULT_TO_GPU
30
reed@android.come191b162009-12-18 21:33:39 +000031extern SkView* create_overview(int, const SkViewFactory[]);
reed@android.com34245c72009-11-03 04:00:48 +000032
reed@android.comcb342352010-07-22 18:27:53 +000033#define SK_SUPPORT_GL
reed@android.com8a1c16f2008-12-17 15:59:43 +000034
35#define ANIMATING_EVENTTYPE "nextSample"
36#define ANIMATING_DELAY 750
37
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000038#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +000039 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000040#else
reed@google.combad8c872011-05-18 20:10:31 +000041 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000042#endif
reed@google.combad8c872011-05-18 20:10:31 +000043#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000044
reed@google.comac10a2d2010-12-22 21:39:39 +000045#ifdef SK_SUPPORT_GL
46 #include "GrGLConfig.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000047#endif
48
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000049///////////////
50static const char view_inval_msg[] = "view-inval-msg";
51
52static void postInvalDelay(SkEventSinkID sinkID) {
53 SkEvent* evt = new SkEvent(view_inval_msg);
reed@google.comf2183392011-04-22 14:10:48 +000054 evt->post(sinkID, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000055}
56
57static bool isInvalEvent(const SkEvent& evt) {
58 return evt.isType(view_inval_msg);
59}
60//////////////////
61
reed@android.com8a1c16f2008-12-17 15:59:43 +000062SkViewRegister* SkViewRegister::gHead;
63SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) {
64 static bool gOnce;
65 if (!gOnce) {
66 gHead = NULL;
67 gOnce = true;
68 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000069
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 fChain = gHead;
71 gHead = this;
72}
73
reed@android.comf2b98d62010-12-20 18:26:13 +000074#if defined(SK_SUPPORT_GL)
75 #define SK_USE_SHADERS
76#endif
77
twiz@google.com06c3b6b2011-03-14 16:58:39 +000078#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +000079#include <CoreFoundation/CoreFoundation.h>
80#include <CoreFoundation/CFURLAccess.h>
81
82static void testpdf() {
83 CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf",
84 kCFStringEncodingUTF8);
85 CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path,
86 kCFURLPOSIXPathStyle,
87 false);
88 CFRelease(path);
89 CGRect box = CGRectMake(0, 0, 8*72, 10*72);
90 CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL);
91 CFRelease(url);
92
93 CGContextBeginPage(cg, &box);
94 CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5);
95 CGContextFillEllipseInRect(cg, r);
96 CGContextEndPage(cg);
97 CGContextRelease(cg);
98
99 if (false) {
100 SkBitmap bm;
101 bm.setConfig(SkBitmap::kA8_Config, 64, 64);
102 bm.allocPixels();
103 bm.eraseColor(0);
104
105 SkCanvas canvas(bm);
106
107 }
108}
109#endif
110
111//////////////////////////////////////////////////////////////////////////////
112
reed@google.com569e0432011-04-05 13:07:03 +0000113enum FlipAxisEnum {
114 kFlipAxis_X = (1 << 0),
115 kFlipAxis_Y = (1 << 1)
116};
117
reed@google.com569e0432011-04-05 13:07:03 +0000118static SkTriState cycle_tristate(SkTriState state) {
119 static const SkTriState gCycle[] = {
120 /* kFalse_SkTriState -> */ kUnknown_SkTriState,
121 /* kTrue_SkTriState -> */ kFalse_SkTriState,
122 /* kUnknown_SkTriState -> */ kTrue_SkTriState,
123 };
124 return gCycle[state];
125}
126
reed@google.comf0b5f682011-03-11 20:08:25 +0000127#include "SkDrawFilter.h"
128
reed@google.com569e0432011-04-05 13:07:03 +0000129class FlagsDrawFilter : public SkDrawFilter {
reed@google.comf0b5f682011-03-11 20:08:25 +0000130public:
reed@google.com09e3baa2011-05-18 12:04:31 +0000131 FlagsDrawFilter(SkTriState lcd, SkTriState aa, SkTriState filter,
132 SkTriState hinting) :
133 fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {}
reed@google.comf0b5f682011-03-11 20:08:25 +0000134
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000135 virtual void filter(SkPaint* paint, Type t) {
reed@google.com569e0432011-04-05 13:07:03 +0000136 if (kText_Type == t && kUnknown_SkTriState != fLCDState) {
reed@google.com569e0432011-04-05 13:07:03 +0000137 paint->setLCDRenderText(kTrue_SkTriState == fLCDState);
138 }
139 if (kUnknown_SkTriState != fAAState) {
reed@google.com569e0432011-04-05 13:07:03 +0000140 paint->setAntiAlias(kTrue_SkTriState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000141 }
reed@google.com176753a2011-05-17 15:32:04 +0000142 if (kUnknown_SkTriState != fFilterState) {
143 paint->setFilterBitmap(kTrue_SkTriState == fFilterState);
144 }
reed@google.com09e3baa2011-05-18 12:04:31 +0000145 if (kUnknown_SkTriState != fHintingState) {
146 paint->setHinting(kTrue_SkTriState == fHintingState ?
147 SkPaint::kNormal_Hinting :
148 SkPaint::kSlight_Hinting);
149 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000150 }
151
152private:
reed@google.com569e0432011-04-05 13:07:03 +0000153 SkTriState fLCDState;
reed@google.com569e0432011-04-05 13:07:03 +0000154 SkTriState fAAState;
reed@google.com176753a2011-05-17 15:32:04 +0000155 SkTriState fFilterState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000156 SkTriState fHintingState;
reed@google.comf0b5f682011-03-11 20:08:25 +0000157};
158
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159//////////////////////////////////////////////////////////////////////////////
160
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000161#define MAX_ZOOM_LEVEL 8
162#define MIN_ZOOM_LEVEL -8
163
reed@android.comf2b98d62010-12-20 18:26:13 +0000164static const char gCharEvtName[] = "SampleCode_Char_Event";
165static const char gKeyEvtName[] = "SampleCode_Key_Event";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166static const char gTitleEvtName[] = "SampleCode_Title_Event";
167static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
reed@android.comf2b98d62010-12-20 18:26:13 +0000168static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
169
170bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
171 if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
172 if (outUni) {
173 *outUni = evt.getFast32();
174 }
175 return true;
176 }
177 return false;
178}
179
180bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
181 if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
182 if (outKey) {
183 *outKey = (SkKey)evt.getFast32();
184 }
185 return true;
186 }
187 return false;
188}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189
190bool SampleCode::TitleQ(const SkEvent& evt) {
191 return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
192}
193
194void SampleCode::TitleR(SkEvent* evt, const char title[]) {
195 SkASSERT(evt && TitleQ(*evt));
196 evt->setString(gTitleEvtName, title);
197}
198
199bool SampleCode::PrefSizeQ(const SkEvent& evt) {
200 return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
201}
202
203void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
204 SkASSERT(evt && PrefSizeQ(*evt));
205 SkScalar size[2];
206 size[0] = width;
207 size[1] = height;
208 evt->setScalars(gPrefSizeEvtName, 2, size);
209}
210
reed@android.comf2b98d62010-12-20 18:26:13 +0000211bool SampleCode::FastTextQ(const SkEvent& evt) {
212 return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
213}
214
215///////////////////////////////////////////////////////////////////////////////
216
reed@android.com44177402009-11-23 21:07:51 +0000217static SkMSec gAnimTime;
reed@android.comf2b98d62010-12-20 18:26:13 +0000218static SkMSec gAnimTimePrev;
219
reed@android.com44177402009-11-23 21:07:51 +0000220SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
reed@android.comf2b98d62010-12-20 18:26:13 +0000221SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
222SkScalar SampleCode::GetAnimSecondsDelta() {
223 return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
224}
reed@android.com44177402009-11-23 21:07:51 +0000225
226SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000227 // since gAnimTime can be up to 32 bits, we can't convert it to a float
228 // or we'll lose the low bits. Hence we use doubles for the intermediate
229 // calculations
230 double seconds = (double)gAnimTime / 1000.0;
231 double value = SkScalarToDouble(speed) * seconds;
reed@android.com44177402009-11-23 21:07:51 +0000232 if (period) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000233 value = ::fmod(value, SkScalarToDouble(period));
reed@android.com44177402009-11-23 21:07:51 +0000234 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000235 return SkDoubleToScalar(value);
reed@android.com44177402009-11-23 21:07:51 +0000236}
237
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238//////////////////////////////////////////////////////////////////////////////
239
reed@android.comf2b98d62010-12-20 18:26:13 +0000240static SkView* curr_view(SkWindow* wind) {
241 SkView::F2BIter iter(wind);
242 return iter.next();
243}
244
Scroggo2c8208f2011-06-15 16:49:08 +0000245void SampleWindow::setZoomCenter(float x, float y)
246{
247 fZoomCenterX = SkFloatToScalar(x);
248 fZoomCenterY = SkFloatToScalar(y);
249}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250
djsollen@google.come32b5832011-06-13 16:58:40 +0000251bool SampleWindow::setGrContext(GrContext* context)
252{
253 if (fGrContext) {
254 fGrContext->unref();
255 }
256 fGrContext = context;
257 fGrContext->ref();
258 return true;
259}
260
261GrContext* SampleWindow::getGrContext()
262{
263 return fGrContext;
264}
djsollen@google.come32b5832011-06-13 16:58:40 +0000265
Scroggo0f185c22011-03-24 18:35:50 +0000266bool SampleWindow::zoomIn()
267{
268 // Arbitrarily decided
269 if (fFatBitsScale == 25) return false;
270 fFatBitsScale++;
271 this->inval(NULL);
272 return true;
273}
274
275bool SampleWindow::zoomOut()
276{
277 if (fFatBitsScale == 1) return false;
278 fFatBitsScale--;
279 this->inval(NULL);
280 return true;
281}
282
283void SampleWindow::toggleZoomer()
284{
285 fShowZoomer = !fShowZoomer;
286 this->inval(NULL);
287}
288
289void SampleWindow::updatePointer(int x, int y)
290{
291 fMouseX = x;
292 fMouseY = y;
293 if (fShowZoomer) {
294 this->inval(NULL);
295 }
296}
297
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000298bool SampleWindow::make3DReady() {
299
300#if defined(SK_SUPPORT_GL)
bsalomon@google.com498a6232011-03-10 18:24:15 +0000301 if (attachGL()) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000302 if (NULL != fGrContext) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000303 // various gr lifecycle tests
304 #if 0
305 fGrContext->freeGpuResources();
306 #elif 0
307 // this will leak resources.
308 fGrContext->contextLost();
309 #elif 0
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000310 GrAssert(1 == fGrContext->refcnt());
311 fGrContext->unref();
312 fGrContext = NULL;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000313 #endif
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000314 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000315
bsalomon@google.com498a6232011-03-10 18:24:15 +0000316 if (NULL == fGrContext) {
317 #if defined(SK_USE_SHADERS)
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000318 fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000319 #else
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000320 fGrContext = GrContext::Create(kOpenGL_Fixed_GrEngine, NULL);
bsalomon@google.com498a6232011-03-10 18:24:15 +0000321 #endif
reed@google.com569e0432011-04-05 13:07:03 +0000322 SkDebugf("---- constructor\n");
bsalomon@google.com498a6232011-03-10 18:24:15 +0000323 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000324
bsalomon@google.com498a6232011-03-10 18:24:15 +0000325 if (NULL != fGrContext) {
326 return true;
327 } else {
328 detachGL();
329 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000330 }
331#endif
332 SkDebugf("Failed to setup 3D");
333 return false;
334}
335
reed@android.com8a1c16f2008-12-17 15:59:43 +0000336SampleWindow::CanvasType SampleWindow::cycle_canvastype(CanvasType ct) {
337 static const CanvasType gCT[] = {
338 kPicture_CanvasType,
reed@android.comf2b98d62010-12-20 18:26:13 +0000339 kGPU_CanvasType,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340 kRaster_CanvasType
341 };
342 return gCT[ct];
343}
344
345SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) {
yangsu@google.com1f394212011-06-01 18:03:34 +0000346#ifdef PIPE_FILE
347 //Clear existing file or create file if it doesn't exist
348 FILE* f = fopen(FILE_PATH, "wb");
349 fclose(f);
350#endif
351
reed@android.com8a1c16f2008-12-17 15:59:43 +0000352 fPicture = NULL;
reed@android.comf2b98d62010-12-20 18:26:13 +0000353 fGpuCanvas = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000354
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000355 fGrContext = NULL;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000356
reed@android.comf2b98d62010-12-20 18:26:13 +0000357#ifdef DEFAULT_TO_GPU
358 fCanvasType = kGPU_CanvasType;
359#else
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360 fCanvasType = kRaster_CanvasType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000361#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000362 fUseClip = false;
reed@android.come522ca52009-11-23 20:10:41 +0000363 fNClip = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364 fRepeatDrawing = false;
365 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000366 fRotate = false;
367 fScale = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000368 fRequestGrabImage = false;
reed@google.com0faac1e2011-05-11 05:58:58 +0000369 fUsePipe = false;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000370 fMeasureFPS = false;
reed@google.com569e0432011-04-05 13:07:03 +0000371 fLCDState = kUnknown_SkTriState;
372 fAAState = kUnknown_SkTriState;
reed@google.com66f22fd2011-05-17 15:33:45 +0000373 fFilterState = kUnknown_SkTriState;
reed@google.com09e3baa2011-05-18 12:04:31 +0000374 fHintingState = kUnknown_SkTriState;
reed@google.com569e0432011-04-05 13:07:03 +0000375 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000376 fScrollTestX = fScrollTestY = 0;
377
Scroggo0f185c22011-03-24 18:35:50 +0000378 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000379 fFatBitsScale = 8;
Scroggo0f185c22011-03-24 18:35:50 +0000380 fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
381 fShowZoomer = false;
382
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000383 fZoomLevel = 0;
384 fZoomScale = SK_Scalar1;
385
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000386// this->setConfig(SkBitmap::kRGB_565_Config);
387 this->setConfig(SkBitmap::kARGB_8888_Config);
388 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000389 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390
reed@android.com34245c72009-11-03 04:00:48 +0000391 {
392 const SkViewRegister* reg = SkViewRegister::Head();
393 while (reg) {
394 *fSamples.append() = reg->factory();
395 reg = reg->next();
396 }
397 }
398 fCurrIndex = 0;
reed@android.come0f13ee2009-11-04 19:40:25 +0000399 this->loadView(fSamples[fCurrIndex]());
reed@google.comf0b5f682011-03-11 20:08:25 +0000400
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000401#ifdef SK_BUILD_FOR_MAC
reed@google.comf0b5f682011-03-11 20:08:25 +0000402 testpdf();
twiz@google.com06c3b6b2011-03-14 16:58:39 +0000403#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000404}
405
406SampleWindow::~SampleWindow() {
407 delete fPicture;
reed@android.comf2b98d62010-12-20 18:26:13 +0000408 delete fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000409 if (NULL != fGrContext) {
410 fGrContext->unref();
411 }
Scroggo0f185c22011-03-24 18:35:50 +0000412 fTypeface->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000413}
414
reed@android.com55e76b22009-11-23 21:46:47 +0000415static SkBitmap capture_bitmap(SkCanvas* canvas) {
416 SkBitmap bm;
417 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
418 src.copyTo(&bm, src.config());
419 return bm;
420}
421
422static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
423 SkBitmap* diff) {
424 const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000425
reed@android.com55e76b22009-11-23 21:46:47 +0000426 SkAutoLockPixels alp0(src);
427 SkAutoLockPixels alp1(orig);
428 for (int y = 0; y < src.height(); y++) {
429 const void* srcP = src.getAddr(0, y);
430 const void* origP = orig.getAddr(0, y);
431 size_t bytes = src.width() * src.bytesPerPixel();
432 if (memcmp(srcP, origP, bytes)) {
433 SkDebugf("---------- difference on line %d\n", y);
434 return true;
435 }
436 }
437 return false;
438}
439
Scroggo0f185c22011-03-24 18:35:50 +0000440static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
441{
442 SkColor desiredColor = paint.getColor();
443 paint.setColor(SK_ColorWHITE);
444 const char* c_str = string.c_str();
445 size_t size = string.size();
446 SkRect bounds;
447 paint.measureText(c_str, size, &bounds);
448 bounds.offset(left, top);
449 SkScalar inset = SkIntToScalar(-2);
450 bounds.inset(inset, inset);
451 canvas->drawRect(bounds, paint);
452 if (desiredColor != SK_ColorBLACK) {
453 paint.setColor(SK_ColorBLACK);
454 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
455 }
456 paint.setColor(desiredColor);
457 canvas->drawText(c_str, size, left, top, paint);
458}
459
reed@android.com44177402009-11-23 21:07:51 +0000460#define XCLIP_N 8
461#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +0000462
463void SampleWindow::draw(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000464 // update the animation time
reed@android.comf2b98d62010-12-20 18:26:13 +0000465 gAnimTimePrev = gAnimTime;
reed@android.com44177402009-11-23 21:07:51 +0000466 gAnimTime = SkTime::GetMSecs();
467
Scroggo2c8208f2011-06-15 16:49:08 +0000468 SkScalar cx = fZoomCenterX;
469 SkScalar cy = fZoomCenterY;
reed@google.com569e0432011-04-05 13:07:03 +0000470
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000471 if (fZoomLevel) {
472 SkMatrix m;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000473 SkPoint center;
474 m = canvas->getTotalMatrix();//.invert(&m);
475 m.mapXY(cx, cy, &center);
476 cx = center.fX;
477 cy = center.fY;
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000478
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000479 m.setTranslate(-cx, -cy);
480 m.postScale(fZoomScale, fZoomScale);
481 m.postTranslate(cx, cy);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000482
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000483 canvas->concat(m);
484 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000485
reed@google.com569e0432011-04-05 13:07:03 +0000486 if (fFlipAxis) {
487 SkMatrix m;
488 m.setTranslate(cx, cy);
489 if (fFlipAxis & kFlipAxis_X) {
490 m.preScale(-SK_Scalar1, SK_Scalar1);
491 }
492 if (fFlipAxis & kFlipAxis_Y) {
493 m.preScale(SK_Scalar1, -SK_Scalar1);
494 }
495 m.preTranslate(-cx, -cy);
496 canvas->concat(m);
497 }
498
reed@google.com52f57e12011-03-16 12:10:02 +0000499 // Apply any gesture matrix
500 if (true) {
501 const SkMatrix& localM = fGesture.localM();
502 if (localM.getType() & SkMatrix::kScale_Mask) {
503 canvas->setExternalMatrix(&localM);
504 }
505 canvas->concat(localM);
506 canvas->concat(fGesture.globalM());
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000507
reed@google.com52f57e12011-03-16 12:10:02 +0000508 if (fGesture.isActive()) {
509 this->inval(NULL);
510 }
511 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000512
reed@android.come522ca52009-11-23 20:10:41 +0000513 if (fNClip) {
reed@android.com55e76b22009-11-23 21:46:47 +0000514 this->INHERITED::draw(canvas);
515 SkBitmap orig = capture_bitmap(canvas);
reed@android.come522ca52009-11-23 20:10:41 +0000516
517 const SkScalar w = this->width();
518 const SkScalar h = this->height();
519 const SkScalar cw = w / XCLIP_N;
520 const SkScalar ch = h / YCLIP_N;
521 for (int y = 0; y < YCLIP_N; y++) {
reed@android.com55e76b22009-11-23 21:46:47 +0000522 SkRect r;
523 r.fTop = y * ch;
524 r.fBottom = (y + 1) * ch;
525 if (y == YCLIP_N - 1) {
526 r.fBottom = h;
527 }
reed@android.come522ca52009-11-23 20:10:41 +0000528 for (int x = 0; x < XCLIP_N; x++) {
529 SkAutoCanvasRestore acr(canvas, true);
reed@android.com55e76b22009-11-23 21:46:47 +0000530 r.fLeft = x * cw;
531 r.fRight = (x + 1) * cw;
532 if (x == XCLIP_N - 1) {
533 r.fRight = w;
534 }
reed@android.come522ca52009-11-23 20:10:41 +0000535 canvas->clipRect(r);
536 this->INHERITED::draw(canvas);
537 }
538 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000539
reed@android.com55e76b22009-11-23 21:46:47 +0000540 SkBitmap diff;
541 if (bitmap_diff(canvas, orig, &diff)) {
542 }
reed@android.come522ca52009-11-23 20:10:41 +0000543 } else {
544 this->INHERITED::draw(canvas);
545 }
Scroggo3272ba82011-05-25 20:50:42 +0000546 if (fShowZoomer && fCanvasType != kGPU_CanvasType) {
547 // In the GPU case, INHERITED::draw calls beforeChildren, which
548 // creates an SkGpuCanvas. All further draw calls are directed
549 // at that canvas, which is deleted in afterChildren (which is
550 // also called by draw), so we cannot show the zoomer here.
551 // Instead, we call it inside afterChildren.
552 showZoomer(canvas);
553 }
554}
555
556void SampleWindow::showZoomer(SkCanvas* canvas) {
Scroggo0f185c22011-03-24 18:35:50 +0000557 int count = canvas->save();
558 canvas->resetMatrix();
559 // Ensure the mouse position is on screen.
reed@google.com261b8e22011-04-14 17:53:24 +0000560 int width = SkScalarRound(this->width());
561 int height = SkScalarRound(this->height());
Scroggo0f185c22011-03-24 18:35:50 +0000562 if (fMouseX >= width) fMouseX = width - 1;
563 else if (fMouseX < 0) fMouseX = 0;
564 if (fMouseY >= height) fMouseY = height - 1;
565 else if (fMouseY < 0) fMouseY = 0;
reed@google.comb36334d2011-05-18 15:07:20 +0000566
Scroggo0f185c22011-03-24 18:35:50 +0000567 SkBitmap bitmap = capture_bitmap(canvas);
reed@google.comb36334d2011-05-18 15:07:20 +0000568 bitmap.lockPixels();
569
Scroggo0f185c22011-03-24 18:35:50 +0000570 // 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 +0000571 int zoomedWidth = (width >> 1) | 1;
572 int zoomedHeight = (height >> 1) | 1;
Scroggo0f185c22011-03-24 18:35:50 +0000573 SkIRect src;
574 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
575 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
576 SkRect dest;
577 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
578 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
579 SkPaint paint;
580 // Clear the background behind our zoomed in view
581 paint.setColor(SK_ColorWHITE);
582 canvas->drawRect(dest, paint);
583 canvas->drawBitmapRect(bitmap, &src, dest);
584 paint.setColor(SK_ColorBLACK);
585 paint.setStyle(SkPaint::kStroke_Style);
586 // Draw a border around the pixel in the middle
587 SkRect originalPixel;
588 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
589 SkMatrix matrix;
590 SkRect scalarSrc;
591 scalarSrc.set(src);
592 SkColor color = bitmap.getColor(fMouseX, fMouseY);
593 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
594 SkRect pixel;
595 matrix.mapRect(&pixel, originalPixel);
596 // TODO Perhaps measure the values and make the outline white if it's "dark"
597 if (color == SK_ColorBLACK) {
598 paint.setColor(SK_ColorWHITE);
599 }
600 canvas->drawRect(pixel, paint);
601 }
602 paint.setColor(SK_ColorBLACK);
603 // Draw a border around the destination rectangle
604 canvas->drawRect(dest, paint);
605 paint.setStyle(SkPaint::kStrokeAndFill_Style);
606 // Identify the pixel and its color on screen
607 paint.setTypeface(fTypeface);
608 paint.setAntiAlias(true);
609 SkScalar lineHeight = paint.getFontMetrics(NULL);
610 SkString string;
611 string.appendf("(%i, %i)", fMouseX, fMouseY);
612 SkScalar left = dest.fLeft + SkIntToScalar(3);
613 SkScalar i = SK_Scalar1;
614 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
615 // Alpha
616 i += SK_Scalar1;
617 string.reset();
618 string.appendf("A: %X", SkColorGetA(color));
619 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
620 // Red
621 i += SK_Scalar1;
622 string.reset();
623 string.appendf("R: %X", SkColorGetR(color));
624 paint.setColor(SK_ColorRED);
625 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
626 // Green
627 i += SK_Scalar1;
628 string.reset();
629 string.appendf("G: %X", SkColorGetG(color));
630 paint.setColor(SK_ColorGREEN);
631 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
632 // Blue
633 i += SK_Scalar1;
634 string.reset();
635 string.appendf("B: %X", SkColorGetB(color));
636 paint.setColor(SK_ColorBLUE);
637 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
638 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +0000639}
640
reed@android.com8a1c16f2008-12-17 15:59:43 +0000641void SampleWindow::onDraw(SkCanvas* canvas) {
642 if (fRepeatDrawing) {
643 this->inval(NULL);
644 }
645}
646
647#include "SkColorPriv.h"
648
649static void reverseRedAndBlue(const SkBitmap& bm) {
650 SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config);
651 uint8_t* p = (uint8_t*)bm.getPixels();
652 uint8_t* stop = p + bm.getSize();
653 while (p < stop) {
654 // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply
655 unsigned scale = SkAlpha255To256(p[3]);
656 unsigned r = p[2];
657 unsigned b = p[0];
658 p[0] = SkAlphaMul(r, scale);
659 p[1] = SkAlphaMul(p[1], scale);
660 p[2] = SkAlphaMul(b, scale);
661 p += 4;
662 }
663}
664
665SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000666 if (kGPU_CanvasType != fCanvasType) {
reed@android.com6efdc472008-12-19 18:24:35 +0000667#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000668 detachGL();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000669#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000670 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000671
reed@android.com8a1c16f2008-12-17 15:59:43 +0000672 switch (fCanvasType) {
673 case kRaster_CanvasType:
674 canvas = this->INHERITED::beforeChildren(canvas);
675 break;
676 case kPicture_CanvasType:
677 fPicture = new SkPicture;
678 canvas = fPicture->beginRecording(9999, 9999);
679 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000680 case kGPU_CanvasType: {
reed@google.com64e3eb22011-05-04 14:32:04 +0000681 if (make3DReady()) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000682 SkDevice* device = canvas->getDevice();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000683 const SkBitmap& bitmap = device->accessBitmap(true);
684
bsalomon@google.com5782d712011-01-21 21:03:59 +0000685 GrRenderTarget* renderTarget;
Scroggo3e7ff9f2011-06-16 15:31:26 +0000686
687 GrPlatformSurfaceDesc desc;
688 desc.reset();
689 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
690 desc.fWidth = bitmap.width();
691 desc.fHeight = bitmap.height();
692 desc.fConfig = kRGBA_8888_GrPixelConfig;
693 desc.fStencilBits = 8;
694 GrGLint buffer;
695 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer);
696 desc.fPlatformRenderTarget = buffer;
697
698 renderTarget = static_cast<GrRenderTarget*>(
699 fGrContext->createPlatformSurface(desc));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000700 fGpuCanvas = new SkGpuCanvas(fGrContext, renderTarget);
701 renderTarget->unref();
702
reed@google.comaf951c92011-06-16 19:10:39 +0000703 device = new SkGpuDevice(fGrContext, renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000704 fGpuCanvas->setDevice(device)->unref();
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000705
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000706 fGpuCanvas->concat(canvas->getTotalMatrix());
reed@android.comf2b98d62010-12-20 18:26:13 +0000707 canvas = fGpuCanvas;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000708
reed@android.comf2b98d62010-12-20 18:26:13 +0000709 } else {
710 canvas = this->INHERITED::beforeChildren(canvas);
711 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000712 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000713 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000714 }
715
716 if (fUseClip) {
717 canvas->drawColor(0xFFFF88FF);
718 canvas->clipPath(fClipPath);
719 }
720
721 return canvas;
722}
723
724static void paint_rgn(const SkBitmap& bm, const SkIRect& r,
725 const SkRegion& rgn) {
726 SkCanvas canvas(bm);
727 SkRegion inval(rgn);
728
729 inval.translate(r.fLeft, r.fTop);
730 canvas.clipRegion(inval);
731 canvas.drawColor(0xFFFF8080);
732}
733
734void SampleWindow::afterChildren(SkCanvas* orig) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000735 if (fRequestGrabImage) {
736 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000737
reed@android.comf2b98d62010-12-20 18:26:13 +0000738 SkCanvas* canvas = fGpuCanvas ? fGpuCanvas : orig;
739 SkDevice* device = canvas->getDevice();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000740 SkBitmap bmp;
741 if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000742 static int gSampleGrabCounter;
743 SkString name;
744 name.printf("sample_grab_%d", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000745 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +0000746 SkImageEncoder::kPNG_Type, 100);
747 }
748 }
749
reed@android.com8a1c16f2008-12-17 15:59:43 +0000750 switch (fCanvasType) {
751 case kRaster_CanvasType:
752 break;
753 case kPicture_CanvasType:
reed@android.comaefd2bc2009-03-30 21:02:14 +0000754 if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000755 SkPicture* pict = new SkPicture(*fPicture);
756 fPicture->unref();
757 orig->drawPicture(*pict);
758 pict->unref();
reed@android.comaefd2bc2009-03-30 21:02:14 +0000759 } else if (true) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000760 SkDynamicMemoryWStream ostream;
761 fPicture->serialize(&ostream);
762 fPicture->unref();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000763
reed@android.com8a1c16f2008-12-17 15:59:43 +0000764 SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
765 SkPicture pict(&istream);
766 orig->drawPicture(pict);
767 } else {
768 fPicture->draw(orig);
769 fPicture->unref();
770 }
771 fPicture = NULL;
772 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000773#ifdef SK_SUPPORT_GL
reed@android.comf2b98d62010-12-20 18:26:13 +0000774 case kGPU_CanvasType:
Scroggoaed68d92011-06-08 14:26:00 +0000775 if (fShowZoomer && fGpuCanvas) {
Scroggo3272ba82011-05-25 20:50:42 +0000776 this->showZoomer(fGpuCanvas);
777 }
reed@android.comf2b98d62010-12-20 18:26:13 +0000778 delete fGpuCanvas;
779 fGpuCanvas = NULL;
780 presentGL();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000781 break;
reed@android.com6efdc472008-12-19 18:24:35 +0000782#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000783 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000784
reed@google.com17d7aec2011-04-25 14:31:44 +0000785 // Do this after presentGL and other finishing, rather than in afterChild
786 if (fMeasureFPS && fMeasureFPS_Time) {
787 fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time;
788 this->updateTitle();
789 postInvalDelay(this->getSinkID());
790 }
791
792 // if ((fScrollTestX | fScrollTestY) != 0)
reed@android.comf2b98d62010-12-20 18:26:13 +0000793 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000794 const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
795 int dx = fScrollTestX * 7;
796 int dy = fScrollTestY * 7;
797 SkIRect r;
798 SkRegion inval;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000799
reed@android.com8a1c16f2008-12-17 15:59:43 +0000800 r.set(50, 50, 50+100, 50+100);
801 bm.scrollRect(&r, dx, dy, &inval);
802 paint_rgn(bm, r, inval);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000803 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000804}
805
reed@android.com6c5f6f22009-08-14 16:08:38 +0000806void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
807 if (fScale) {
808 SkScalar scale = SK_Scalar1 * 7 / 10;
809 SkScalar cx = this->width() / 2;
810 SkScalar cy = this->height() / 2;
811 canvas->translate(cx, cy);
812 canvas->scale(scale, scale);
813 canvas->translate(-cx, -cy);
814 }
815 if (fRotate) {
816 SkScalar cx = this->width() / 2;
817 SkScalar cy = this->height() / 2;
818 canvas->translate(cx, cy);
819 canvas->rotate(SkIntToScalar(30));
820 canvas->translate(-cx, -cy);
821 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000822
reed@google.com09e3baa2011-05-18 12:04:31 +0000823 canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState,
824 fFilterState, fHintingState))->unref();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000825
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000826 if (fMeasureFPS) {
reed@google.comf2183392011-04-22 14:10:48 +0000827 fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw
828 if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
829 fMeasureFPS_Time = SkTime::GetMSecs();
830 }
831 } else {
832 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000833 }
reed@google.com0faac1e2011-05-11 05:58:58 +0000834 (void)SampleView::SetUsePipe(child, fUsePipe);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000835}
836
837void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
reed@google.comf0b5f682011-03-11 20:08:25 +0000838 canvas->setDrawFilter(NULL);
reed@android.com6c5f6f22009-08-14 16:08:38 +0000839}
840
reed@android.com8a1c16f2008-12-17 15:59:43 +0000841static SkBitmap::Config gConfigCycle[] = {
842 SkBitmap::kNo_Config, // none -> none
843 SkBitmap::kNo_Config, // a1 -> none
844 SkBitmap::kNo_Config, // a8 -> none
845 SkBitmap::kNo_Config, // index8 -> none
846 SkBitmap::kARGB_4444_Config, // 565 -> 4444
847 SkBitmap::kARGB_8888_Config, // 4444 -> 8888
848 SkBitmap::kRGB_565_Config // 8888 -> 565
849};
850
851static SkBitmap::Config cycle_configs(SkBitmap::Config c) {
852 return gConfigCycle[c];
853}
854
djsollen@google.come32b5832011-06-13 16:58:40 +0000855void SampleWindow::changeZoomLevel(float delta) {
856 fZoomLevel += SkFloatToScalar(delta);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000857 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000858 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
859 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000860 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +0000861 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
862 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000863 } else {
864 fZoomScale = SK_Scalar1;
865 }
866
djsollen@google.come32b5832011-06-13 16:58:40 +0000867 this->updateTitle();
868
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000869 this->inval(NULL);
870}
871
Scroggo2c8208f2011-06-15 16:49:08 +0000872bool SampleWindow::previousSample() {
873 fCurrIndex = (fCurrIndex - 1) % fSamples.count();
874 this->loadView(fSamples[fCurrIndex]());
875 return true;
876}
877
reed@android.com8a1c16f2008-12-17 15:59:43 +0000878bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +0000879 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
880 this->loadView(fSamples[fCurrIndex]());
881 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000882}
883
Scroggo2c8208f2011-06-15 16:49:08 +0000884void SampleWindow::postAnimatingEvent() {
885 if (fAnimating) {
886 SkEvent* evt = new SkEvent(ANIMATING_EVENTTYPE);
887 evt->post(this->getSinkID(), ANIMATING_DELAY);
888 }
889}
890
reed@android.com8a1c16f2008-12-17 15:59:43 +0000891bool SampleWindow::onEvent(const SkEvent& evt) {
892 if (evt.isType(ANIMATING_EVENTTYPE)) {
893 if (fAnimating) {
894 this->nextSample();
895 this->postAnimatingEvent();
896 }
897 return true;
898 }
reed@android.com34245c72009-11-03 04:00:48 +0000899 if (evt.isType("set-curr-index")) {
900 fCurrIndex = evt.getFast32() % fSamples.count();
901 this->loadView(fSamples[fCurrIndex]());
902 return true;
903 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000904 if (isInvalEvent(evt)) {
905 this->inval(NULL);
906 return true;
907 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000908 return this->INHERITED::onEvent(evt);
909}
910
reed@android.comf2b98d62010-12-20 18:26:13 +0000911bool SampleWindow::onQuery(SkEvent* query) {
912 if (query->isType("get-slide-count")) {
913 query->setFast32(fSamples.count());
914 return true;
915 }
916 if (query->isType("get-slide-title")) {
917 SkView* view = fSamples[query->getFast32()]();
918 SkEvent evt(gTitleEvtName);
919 if (view->doQuery(&evt)) {
920 query->setString("title", evt.findString(gTitleEvtName));
921 }
922 SkSafeUnref(view);
923 return true;
924 }
925 if (query->isType("use-fast-text")) {
926 SkEvent evt(gFastTextEvtName);
927 return curr_view(this)->doQuery(&evt);
928 }
929 return this->INHERITED::onQuery(query);
930}
931
reed@android.com0ae6b242008-12-23 16:49:54 +0000932static void cleanup_for_filename(SkString* name) {
933 char* str = name->writable_str();
reed@android.come191b162009-12-18 21:33:39 +0000934 for (size_t i = 0; i < name->size(); i++) {
reed@android.com0ae6b242008-12-23 16:49:54 +0000935 switch (str[i]) {
936 case ':': str[i] = '-'; break;
937 case '/': str[i] = '-'; break;
938 case ' ': str[i] = '_'; break;
939 default: break;
940 }
941 }
942}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000943
944bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000945 {
946 SkView* view = curr_view(this);
947 if (view) {
948 SkEvent evt(gCharEvtName);
949 evt.setFast32(uni);
950 if (view->doQuery(&evt)) {
951 return true;
952 }
953 }
954 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000955
reed@android.com8a1c16f2008-12-17 15:59:43 +0000956 int dx = 0xFF;
957 int dy = 0xFF;
958
959 switch (uni) {
960 case '5': dx = 0; dy = 0; break;
961 case '8': dx = 0; dy = -1; break;
962 case '6': dx = 1; dy = 0; break;
963 case '2': dx = 0; dy = 1; break;
964 case '4': dx = -1; dy = 0; break;
965 case '7': dx = -1; dy = -1; break;
966 case '9': dx = 1; dy = -1; break;
967 case '3': dx = 1; dy = 1; break;
968 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000969
reed@android.com8a1c16f2008-12-17 15:59:43 +0000970 default:
971 break;
972 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000973
reed@android.com8a1c16f2008-12-17 15:59:43 +0000974 if (0xFF != dx && 0xFF != dy) {
975 if ((dx | dy) == 0) {
976 fScrollTestX = fScrollTestY = 0;
977 } else {
978 fScrollTestX += dx;
979 fScrollTestY += dy;
980 }
981 this->inval(NULL);
982 return true;
983 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000984
reed@android.com0ae6b242008-12-23 16:49:54 +0000985 switch (uni) {
986 case 'a':
Scroggo2c8208f2011-06-15 16:49:08 +0000987 this->toggleSlideshow();
reed@android.com0ae6b242008-12-23 16:49:54 +0000988 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000989 case 'b':
990 fAAState = cycle_tristate(fAAState);
991 this->updateTitle();
992 this->inval(NULL);
993 break;
994 case 'c':
995 fUseClip = !fUseClip;
996 this->inval(NULL);
997 this->updateTitle();
reed@android.com0ae6b242008-12-23 16:49:54 +0000998 return true;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000999 case 'd':
1000 SkGraphics::SetFontCacheUsed(0);
1001 return true;
1002 case 'f':
Scroggo2c8208f2011-06-15 16:49:08 +00001003 this->toggleFPS();
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001004 break;
1005 case 'g':
1006 fRequestGrabImage = true;
1007 this->inval(NULL);
1008 break;
reed@google.com09e3baa2011-05-18 12:04:31 +00001009 case 'h':
1010 fHintingState = cycle_tristate(fHintingState);
1011 this->updateTitle();
1012 this->inval(NULL);
1013 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001014 case 'i':
1015 this->zoomIn();
1016 break;
1017 case 'l':
1018 fLCDState = cycle_tristate(fLCDState);
1019 this->updateTitle();
1020 this->inval(NULL);
1021 break;
reed@google.com176753a2011-05-17 15:32:04 +00001022 case 'n':
1023 fFilterState = cycle_tristate(fFilterState);
1024 this->updateTitle();
1025 this->inval(NULL);
1026 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001027 case 'o':
1028 this->zoomOut();
1029 break;
reed@google.com0faac1e2011-05-11 05:58:58 +00001030 case 'p':
1031 fUsePipe = !fUsePipe;
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001032 this->updateTitle();
reed@google.com0faac1e2011-05-11 05:58:58 +00001033 this->inval(NULL);
1034 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001035 case 'r':
1036 fRotate = !fRotate;
1037 this->inval(NULL);
1038 this->updateTitle();
1039 return true;
1040 case 's':
1041 fScale = !fScale;
1042 this->inval(NULL);
1043 this->updateTitle();
1044 return true;
reed@google.com569e0432011-04-05 13:07:03 +00001045 case 'x':
1046 fFlipAxis ^= kFlipAxis_X;
1047 this->updateTitle();
1048 this->inval(NULL);
1049 break;
1050 case 'y':
1051 fFlipAxis ^= kFlipAxis_Y;
1052 this->updateTitle();
1053 this->inval(NULL);
1054 break;
scroggo08526c02011-03-22 14:03:21 +00001055 case 'z':
1056 this->toggleZoomer();
1057 break;
reed@android.com0ae6b242008-12-23 16:49:54 +00001058 default:
1059 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001060 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001061
reed@android.com8a1c16f2008-12-17 15:59:43 +00001062 return this->INHERITED::onHandleChar(uni);
1063}
1064
Scroggo2c8208f2011-06-15 16:49:08 +00001065void SampleWindow::toggleFPS() {
1066 fMeasureFPS = !fMeasureFPS;
1067 this->inval(NULL);
1068 this->updateTitle();
1069}
1070
1071void SampleWindow::toggleSlideshow() {
1072 fAnimating = !fAnimating;
1073 this->postAnimatingEvent();
1074 this->updateTitle();
1075}
1076
1077void SampleWindow::toggleRendering() {
1078 fCanvasType = cycle_canvastype(fCanvasType);
1079 this->updateTitle();
1080 this->inval(NULL);
1081}
1082
reed@android.com8a1c16f2008-12-17 15:59:43 +00001083#include "SkDumpCanvas.h"
1084
1085bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001086 {
1087 SkView* view = curr_view(this);
1088 if (view) {
1089 SkEvent evt(gKeyEvtName);
1090 evt.setFast32(key);
1091 if (view->doQuery(&evt)) {
1092 return true;
1093 }
1094 }
1095 }
1096
reed@android.com8a1c16f2008-12-17 15:59:43 +00001097 switch (key) {
1098 case kRight_SkKey:
1099 if (this->nextSample()) {
1100 return true;
1101 }
1102 break;
1103 case kLeft_SkKey:
Scroggo2c8208f2011-06-15 16:49:08 +00001104 toggleRendering();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001105 return true;
1106 case kUp_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001107 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001108 this->changeZoomLevel(1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001109 } else {
1110 fNClip = !fNClip;
1111 this->inval(NULL);
djsollen@google.come32b5832011-06-13 16:58:40 +00001112 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001113 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001114 return true;
1115 case kDown_SkKey:
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001116 if (USE_ARROWS_FOR_ZOOM) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001117 this->changeZoomLevel(-1.f);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001118 } else {
1119 this->setConfig(cycle_configs(this->getBitmap().config()));
djsollen@google.come32b5832011-06-13 16:58:40 +00001120 this->updateTitle();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001121 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001122 return true;
1123 case kOK_SkKey:
reed@android.comf2b98d62010-12-20 18:26:13 +00001124 if (false) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001125 SkDebugfDumper dumper;
1126 SkDumpCanvas dc(&dumper);
1127 this->draw(&dc);
1128 } else {
1129 fRepeatDrawing = !fRepeatDrawing;
1130 if (fRepeatDrawing) {
1131 this->inval(NULL);
1132 }
1133 }
1134 return true;
reed@android.com34245c72009-11-03 04:00:48 +00001135 case kBack_SkKey:
1136 this->loadView(NULL);
1137 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001138 default:
1139 break;
1140 }
1141 return this->INHERITED::onHandleKey(key);
1142}
1143
reed@google.com52f57e12011-03-16 12:10:02 +00001144///////////////////////////////////////////////////////////////////////////////
1145
1146static const char gGestureClickType[] = "GestureClickType";
1147
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001148bool SampleWindow::onDispatchClick(int x, int y, Click::State state) {
Scroggo0f185c22011-03-24 18:35:50 +00001149 if (Click::kMoved_State == state) {
1150 updatePointer(x, y);
1151 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001152 int w = SkScalarRound(this->width());
1153 int h = SkScalarRound(this->height());
1154
1155 // check for the resize-box
1156 if (w - x < 16 && h - y < 16) {
1157 return false; // let the OS handle the click
1158 } else {
1159 return this->INHERITED::onDispatchClick(x, y, state);
1160 }
1161}
1162
reed@google.com52f57e12011-03-16 12:10:02 +00001163class GestureClick : public SkView::Click {
1164public:
1165 GestureClick(SkView* target) : SkView::Click(target) {
1166 this->setType(gGestureClickType);
1167 }
1168
1169 static bool IsGesture(Click* click) {
1170 return click->isType(gGestureClickType);
1171 }
1172};
1173
1174SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) {
1175 return new GestureClick(this);
1176}
1177
1178bool SampleWindow::onClick(Click* click) {
1179 if (GestureClick::IsGesture(click)) {
1180 float x = SkScalarToFloat(click->fCurr.fX);
1181 float y = SkScalarToFloat(click->fCurr.fY);
1182 switch (click->fState) {
1183 case SkView::Click::kDown_State:
1184 fGesture.touchBegin(click, x, y);
1185 break;
1186 case SkView::Click::kMoved_State:
1187 fGesture.touchMoved(click, x, y);
1188 this->inval(NULL);
1189 break;
1190 case SkView::Click::kUp_State:
1191 fGesture.touchEnd(click);
1192 this->inval(NULL);
1193 break;
1194 }
1195 return true;
1196 }
1197 return false;
1198}
1199
1200///////////////////////////////////////////////////////////////////////////////
1201
reed@android.com8a1c16f2008-12-17 15:59:43 +00001202void SampleWindow::loadView(SkView* view) {
1203 SkView::F2BIter iter(this);
1204 SkView* prev = iter.next();
1205 if (prev) {
1206 prev->detachFromParent();
1207 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001208
reed@android.com34245c72009-11-03 04:00:48 +00001209 if (NULL == view) {
1210 view = create_overview(fSamples.count(), fSamples.begin());
1211 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001212 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001213 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001214 this->attachChildToFront(view)->unref();
1215 view->setSize(this->width(), this->height());
1216
1217 this->updateTitle();
1218}
1219
1220static const char* gConfigNames[] = {
1221 "unknown config",
1222 "A1",
1223 "A8",
1224 "Index8",
1225 "565",
1226 "4444",
1227 "8888"
1228};
1229
1230static const char* configToString(SkBitmap::Config c) {
1231 return gConfigNames[c];
1232}
1233
1234static const char* gCanvasTypePrefix[] = {
1235 "raster: ",
1236 "picture: ",
1237 "opengl: "
1238};
1239
reed@google.com569e0432011-04-05 13:07:03 +00001240static const char* trystate_str(SkTriState state,
1241 const char trueStr[], const char falseStr[]) {
1242 if (kTrue_SkTriState == state) {
1243 return trueStr;
1244 } else if (kFalse_SkTriState == state) {
1245 return falseStr;
1246 }
1247 return NULL;
1248}
1249
reed@android.com8a1c16f2008-12-17 15:59:43 +00001250void SampleWindow::updateTitle() {
1251 SkString title;
1252
1253 SkView::F2BIter iter(this);
1254 SkView* view = iter.next();
1255 SkEvent evt(gTitleEvtName);
1256 if (view->doQuery(&evt)) {
1257 title.set(evt.findString(gTitleEvtName));
1258 }
1259 if (title.size() == 0) {
1260 title.set("<unknown>");
1261 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001262
reed@android.com8a1c16f2008-12-17 15:59:43 +00001263 title.prepend(gCanvasTypePrefix[fCanvasType]);
1264
1265 title.prepend(" ");
1266 title.prepend(configToString(this->getBitmap().config()));
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001267
reed@android.com8a1c16f2008-12-17 15:59:43 +00001268 if (fAnimating) {
1269 title.prepend("<A> ");
1270 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001271 if (fScale) {
1272 title.prepend("<S> ");
1273 }
1274 if (fRotate) {
1275 title.prepend("<R> ");
1276 }
reed@android.come522ca52009-11-23 20:10:41 +00001277 if (fNClip) {
1278 title.prepend("<C> ");
1279 }
reed@google.com569e0432011-04-05 13:07:03 +00001280
1281 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
1282 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed@google.com09e3baa2011-05-18 12:04:31 +00001283 title.prepend(trystate_str(fFilterState, "H ", "h "));
reed@google.com569e0432011-04-05 13:07:03 +00001284 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
1285 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001286
1287 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001288 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001289 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001290
1291 if (fMeasureFPS) {
reed@google.combad8c872011-05-18 20:10:31 +00001292 title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001293 }
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001294 if (fUsePipe && SampleView::IsSampleView(view)) {
1295 title.prepend("<P> ");
1296 }
1297 if (SampleView::IsSampleView(view)) {
1298 title.prepend("! ");
1299 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001300
reed@android.com8a1c16f2008-12-17 15:59:43 +00001301 this->setTitle(title.c_str());
1302}
1303
1304void SampleWindow::onSizeChange() {
1305 this->INHERITED::onSizeChange();
1306
1307 SkView::F2BIter iter(this);
1308 SkView* view = iter.next();
1309 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001310
reed@android.com8a1c16f2008-12-17 15:59:43 +00001311 // rebuild our clippath
1312 {
1313 const SkScalar W = this->width();
1314 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001315
reed@android.com8a1c16f2008-12-17 15:59:43 +00001316 fClipPath.reset();
1317#if 0
1318 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
1319 SkRect r;
1320 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
1321 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
1322 fClipPath.addRect(r);
1323 }
1324#else
1325 SkRect r;
1326 r.set(0, 0, W, H);
1327 fClipPath.addRect(r, SkPath::kCCW_Direction);
1328 r.set(W/4, H/4, W*3/4, H*3/4);
1329 fClipPath.addRect(r, SkPath::kCW_Direction);
1330#endif
1331 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001332
Scroggo2c8208f2011-06-15 16:49:08 +00001333 fZoomCenterX = SkScalarHalf(this->width());
1334 fZoomCenterY = SkScalarHalf(this->height());
1335
Scroggo3e7ff9f2011-06-16 15:31:26 +00001336#ifdef ANDROID
1337 postInvalDelay(this->getSinkID());
Scroggo716a0382011-06-16 14:00:15 +00001338#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001339 this->updateTitle(); // to refresh our config
1340}
1341
1342///////////////////////////////////////////////////////////////////////////////
1343
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001344static const char is_sample_view_tag[] = "sample-is-sample-view";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001345static const char repeat_count_tag[] = "sample-set-repeat-count";
reed@google.com0faac1e2011-05-11 05:58:58 +00001346static const char set_use_pipe_tag[] = "sample-set-use-pipe";
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001347
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001348bool SampleView::IsSampleView(SkView* view) {
1349 SkEvent evt(is_sample_view_tag);
1350 return view->doQuery(&evt);
1351}
1352
reed@google.comf2183392011-04-22 14:10:48 +00001353bool SampleView::SetRepeatDraw(SkView* view, int count) {
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001354 SkEvent evt(repeat_count_tag);
1355 evt.setFast32(count);
reed@google.comf2183392011-04-22 14:10:48 +00001356 return view->doEvent(evt);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001357}
1358
reed@google.com0faac1e2011-05-11 05:58:58 +00001359bool SampleView::SetUsePipe(SkView* view, bool pred) {
1360 SkEvent evt(set_use_pipe_tag);
1361 evt.setFast32(pred);
1362 return view->doEvent(evt);
1363}
1364
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001365bool SampleView::onEvent(const SkEvent& evt) {
1366 if (evt.isType(repeat_count_tag)) {
1367 fRepeatCount = evt.getFast32();
1368 return true;
1369 }
reed@google.com0faac1e2011-05-11 05:58:58 +00001370 if (evt.isType(set_use_pipe_tag)) {
1371 fUsePipe = !!evt.getFast32();
1372 return true;
1373 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001374 return this->INHERITED::onEvent(evt);
1375}
1376
1377bool SampleView::onQuery(SkEvent* evt) {
reed@google.coma6ff4dc2011-05-12 22:08:24 +00001378 if (evt->isType(is_sample_view_tag)) {
1379 return true;
1380 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001381 return this->INHERITED::onQuery(evt);
1382}
1383
reed@google.com68f456d2011-05-02 18:55:39 +00001384#ifdef TEST_GPIPE
1385 #include "SkGPipe.h"
reed@google.com64e3eb22011-05-04 14:32:04 +00001386
1387class SimplePC : public SkGPipeController {
1388public:
1389 SimplePC(SkCanvas* target);
1390 ~SimplePC();
1391
1392 virtual void* requestBlock(size_t minRequest, size_t* actual);
1393 virtual void notifyWritten(size_t bytes);
1394
1395private:
reed@google.com961ddb02011-05-05 14:03:48 +00001396 SkGPipeReader fReader;
1397 void* fBlock;
1398 size_t fBlockSize;
1399 size_t fBytesWritten;
1400 int fAtomsWritten;
reed@google.com64e3eb22011-05-04 14:32:04 +00001401 SkGPipeReader::Status fStatus;
1402
1403 size_t fTotalWritten;
1404};
1405
1406SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
1407 fBlock = NULL;
1408 fBlockSize = fBytesWritten = 0;
1409 fStatus = SkGPipeReader::kDone_Status;
1410 fTotalWritten = 0;
reed@google.com961ddb02011-05-05 14:03:48 +00001411 fAtomsWritten = 0;
reed@google.com64e3eb22011-05-04 14:32:04 +00001412}
1413
1414SimplePC::~SimplePC() {
1415// SkASSERT(SkGPipeReader::kDone_Status == fStatus);
1416 sk_free(fBlock);
1417
reed@google.com0faac1e2011-05-11 05:58:58 +00001418 if (fTotalWritten) {
1419 SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
1420 fAtomsWritten, fStatus);
1421 }
reed@google.com64e3eb22011-05-04 14:32:04 +00001422}
1423
1424void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
1425 sk_free(fBlock);
1426
1427 fBlockSize = minRequest * 4;
1428 fBlock = sk_malloc_throw(fBlockSize);
1429 fBytesWritten = 0;
1430 *actual = fBlockSize;
1431 return fBlock;
1432}
1433
1434void SimplePC::notifyWritten(size_t bytes) {
1435 SkASSERT(fBytesWritten + bytes <= fBlockSize);
yangsu@google.com1f394212011-06-01 18:03:34 +00001436
1437#ifdef PIPE_FILE
1438 //File is open in append mode
1439 FILE* f = fopen(FILE_PATH, "ab");
1440 SkASSERT(f != NULL);
1441 fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
1442 fclose(f);
1443#endif
1444
reed@google.com64e3eb22011-05-04 14:32:04 +00001445 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
1446 SkASSERT(SkGPipeReader::kError_Status != fStatus);
1447 fBytesWritten += bytes;
1448 fTotalWritten += bytes;
reed@google.com961ddb02011-05-05 14:03:48 +00001449
1450 fAtomsWritten += 1;
reed@google.com64e3eb22011-05-04 14:32:04 +00001451}
1452
reed@google.com68f456d2011-05-02 18:55:39 +00001453#endif
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001454
reed@google.com64e3eb22011-05-04 14:32:04 +00001455
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001456void SampleView::onDraw(SkCanvas* canvas) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001457#ifdef TEST_GPIPE
reed@google.com64e3eb22011-05-04 14:32:04 +00001458 SimplePC controller(canvas);
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001459 SkGPipeWriter writer;
reed@google.com0faac1e2011-05-11 05:58:58 +00001460 if (fUsePipe) {
reed@google.comdde09562011-05-23 12:21:05 +00001461 uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
1462// flags = 0;
1463 canvas = writer.startRecording(&controller, flags);
reed@google.com0faac1e2011-05-11 05:58:58 +00001464 }
reed@google.com2f3dc9d2011-05-02 17:33:45 +00001465#endif
1466
reed@google.com81e3d7f2011-06-01 12:42:36 +00001467 this->onDrawBackground(canvas);
1468
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001469 for (int i = 0; i < fRepeatCount; i++) {
1470 SkAutoCanvasRestore acr(canvas, true);
1471 this->onDrawContent(canvas);
1472 }
1473}
1474
1475void SampleView::onDrawBackground(SkCanvas* canvas) {
reed@google.comf2183392011-04-22 14:10:48 +00001476 canvas->drawColor(fBGColor);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001477}
1478
1479///////////////////////////////////////////////////////////////////////////////
1480
reed@android.comf2b98d62010-12-20 18:26:13 +00001481template <typename T> void SkTBSort(T array[], int count) {
1482 for (int i = 1; i < count - 1; i++) {
1483 bool didSwap = false;
1484 for (int j = count - 1; j > i; --j) {
1485 if (array[j] < array[j-1]) {
1486 T tmp(array[j-1]);
1487 array[j-1] = array[j];
1488 array[j] = tmp;
1489 didSwap = true;
1490 }
1491 }
1492 if (!didSwap) {
1493 break;
1494 }
1495 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001496
reed@android.comf2b98d62010-12-20 18:26:13 +00001497 for (int k = 0; k < count - 1; k++) {
1498 SkASSERT(!(array[k+1] < array[k]));
1499 }
1500}
1501
1502#include "SkRandom.h"
1503
1504static void rand_rect(SkIRect* rect, SkRandom& rand) {
1505 int bits = 8;
1506 int shift = 32 - bits;
1507 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
1508 rand.nextU() >> shift, rand.nextU() >> shift);
1509 rect->sort();
1510}
1511
1512static void dumpRect(const SkIRect& r) {
1513 SkDebugf(" { %d, %d, %d, %d },\n",
1514 r.fLeft, r.fTop,
1515 r.fRight, r.fBottom);
1516}
1517
1518static void test_rects(const SkIRect rect[], int count) {
1519 SkRegion rgn0, rgn1;
1520
1521 for (int i = 0; i < count; i++) {
1522 rgn0.op(rect[i], SkRegion::kUnion_Op);
1523 // dumpRect(rect[i]);
1524 }
1525 rgn1.setRects(rect, count);
1526
1527 if (rgn0 != rgn1) {
1528 SkDebugf("\n");
1529 for (int i = 0; i < count; i++) {
1530 dumpRect(rect[i]);
1531 }
1532 SkDebugf("\n");
1533 }
1534}
1535
1536static void test() {
1537 size_t i;
1538
1539 const SkIRect r0[] = {
1540 { 0, 0, 1, 1 },
1541 { 2, 2, 3, 3 },
1542 };
1543 const SkIRect r1[] = {
1544 { 0, 0, 1, 3 },
1545 { 1, 1, 2, 2 },
1546 { 2, 0, 3, 3 },
1547 };
1548 const SkIRect r2[] = {
1549 { 0, 0, 1, 2 },
1550 { 2, 1, 3, 3 },
1551 { 4, 0, 5, 1 },
1552 { 6, 0, 7, 4 },
1553 };
1554
1555 static const struct {
1556 const SkIRect* fRects;
1557 int fCount;
1558 } gRecs[] = {
1559 { r0, SK_ARRAY_COUNT(r0) },
1560 { r1, SK_ARRAY_COUNT(r1) },
1561 { r2, SK_ARRAY_COUNT(r2) },
1562 };
1563
1564 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
1565 test_rects(gRecs[i].fRects, gRecs[i].fCount);
1566 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001567
reed@android.comf2b98d62010-12-20 18:26:13 +00001568 SkRandom rand;
1569 for (i = 0; i < 10000; i++) {
1570 SkRegion rgn0, rgn1;
1571
1572 const int N = 8;
1573 SkIRect rect[N];
1574 for (int j = 0; j < N; j++) {
1575 rand_rect(&rect[j], rand);
1576 }
1577 test_rects(rect, N);
1578 }
1579}
1580
reed@android.com8a1c16f2008-12-17 15:59:43 +00001581SkOSWindow* create_sk_window(void* hwnd) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001582// test();
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001583 return new SampleWindow(hwnd);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001584}
1585
1586void get_preferred_size(int* x, int* y, int* width, int* height) {
1587 *x = 10;
1588 *y = 50;
1589 *width = 640;
1590 *height = 480;
1591}
1592
1593void application_init() {
1594// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001595#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00001596 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00001597#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001598 SkGraphics::Init();
1599 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001600}
1601
1602void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00001603 SkEvent::Term();
1604 SkGraphics::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001605}