epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 8 | #include "SampleApp.h" |
| 9 | |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 10 | #include "SkData.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| 12 | #include "SkDevice.h" |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 13 | #include "SkGpuDevice.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkGraphics.h" |
reed@android.com | b08eb2b | 2009-01-06 20:16:26 +0000 | [diff] [blame] | 15 | #include "SkImageEncoder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkPaint.h" |
| 17 | #include "SkPicture.h" |
| 18 | #include "SkStream.h" |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 19 | #include "SkTime.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #include "SkWindow.h" |
| 21 | |
| 22 | #include "SampleCode.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | #include "GrContext.h" |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 24 | #include "SkTypeface.h" |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 25 | |
Scroggo | 3e7ff9f | 2011-06-16 15:31:26 +0000 | [diff] [blame] | 26 | #include "GrGLInterface.h" |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 27 | |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 28 | #include "SkPDFDevice.h" |
| 29 | #include "SkPDFDocument.h" |
| 30 | #include "SkStream.h" |
| 31 | |
yangsu@google.com | 1f39421 | 2011-06-01 18:03:34 +0000 | [diff] [blame] | 32 | #define TEST_GPIPEx |
| 33 | |
| 34 | #ifdef TEST_GPIPE |
| 35 | #define PIPE_FILE |
| 36 | #define FILE_PATH "/path/to/drawing.data" |
| 37 | #endif |
| 38 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 39 | #define USE_ARROWS_FOR_ZOOM true |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 40 | //#define DEFAULT_TO_GPU |
| 41 | |
reed@android.com | e191b16 | 2009-12-18 21:33:39 +0000 | [diff] [blame] | 42 | extern SkView* create_overview(int, const SkViewFactory[]); |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 43 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | #define ANIMATING_EVENTTYPE "nextSample" |
| 45 | #define ANIMATING_DELAY 750 |
| 46 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 47 | #ifdef SK_DEBUG |
reed@google.com | bad8c87 | 2011-05-18 20:10:31 +0000 | [diff] [blame] | 48 | #define FPS_REPEAT_MULTIPLIER 1 |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 49 | #else |
reed@google.com | bad8c87 | 2011-05-18 20:10:31 +0000 | [diff] [blame] | 50 | #define FPS_REPEAT_MULTIPLIER 10 |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 51 | #endif |
reed@google.com | bad8c87 | 2011-05-18 20:10:31 +0000 | [diff] [blame] | 52 | #define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER) |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 53 | |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 54 | static SampleWindow* gSampleWindow; |
| 55 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 56 | /////////////// |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 57 | class SampleWindow::DefaultDeviceManager : public SampleWindow::DeviceManager { |
| 58 | public: |
| 59 | |
| 60 | DefaultDeviceManager() { |
| 61 | fGrRenderTarget = NULL; |
| 62 | fGrContext = NULL; |
| 63 | } |
| 64 | |
| 65 | virtual ~DefaultDeviceManager() { |
| 66 | SkSafeUnref(fGrRenderTarget); |
| 67 | SkSafeUnref(fGrContext); |
| 68 | } |
| 69 | |
| 70 | virtual void init(SampleWindow* win) { |
| 71 | win->attachGL(); |
| 72 | if (NULL == fGrContext) { |
| 73 | fGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, NULL); |
| 74 | } |
| 75 | if (NULL == fGrContext) { |
| 76 | SkDebugf("Failed to setup 3D"); |
| 77 | win->detachGL(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | virtual bool supportsDeviceType(SampleWindow::DeviceType dType) { |
| 82 | switch (dType) { |
| 83 | case kRaster_DeviceType: |
| 84 | case kPicture_DeviceType: // fallthru |
| 85 | return true; |
| 86 | case kGPU_DeviceType: |
| 87 | return NULL != fGrContext && NULL != fGrRenderTarget; |
| 88 | default: |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | virtual bool prepareCanvas(SampleWindow::DeviceType dType, |
| 94 | SkCanvas* canvas, |
| 95 | SampleWindow* win) { |
| 96 | if (kGPU_DeviceType == dType) { |
| 97 | if (fGrContext) { |
| 98 | canvas->setDevice(new SkGpuDevice(fGrContext, |
| 99 | fGrRenderTarget))->unref(); |
| 100 | } else { |
| 101 | return false; |
| 102 | } |
| 103 | } |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | virtual void publishCanvas(SampleWindow::DeviceType dType, |
| 108 | SkCanvas* canvas, |
| 109 | SampleWindow* win) { |
| 110 | if (fGrContext) { |
| 111 | // in case we have queued drawing calls |
| 112 | fGrContext->flush(); |
| 113 | if (dType != kGPU_DeviceType) { |
| 114 | // need to send the raster bits to the (gpu) window |
| 115 | fGrContext->setRenderTarget(fGrRenderTarget); |
| 116 | const SkBitmap& bm = win->getBitmap(); |
| 117 | fGrContext->writePixels(0, 0, bm.width(), bm.height(), |
| 118 | kRGBA_8888_GrPixelConfig, bm.getPixels(), |
| 119 | bm.rowBytes()); |
| 120 | } |
| 121 | } |
| 122 | win->presentGL(); |
| 123 | } |
| 124 | |
| 125 | virtual void windowSizeChanged(SampleWindow* win) { |
| 126 | if (fGrContext) { |
| 127 | win->attachGL(); |
| 128 | |
| 129 | GrPlatformSurfaceDesc desc; |
| 130 | desc.reset(); |
| 131 | desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType; |
| 132 | desc.fWidth = SkScalarRound(win->width()); |
| 133 | desc.fHeight = SkScalarRound(win->height()); |
| 134 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 135 | GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, &desc.fStencilBits); |
| 136 | GR_GL_GetIntegerv(GR_GL_SAMPLES, &desc.fSampleCnt); |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 137 | GrGLint buffer; |
| 138 | GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &buffer); |
| 139 | desc.fPlatformRenderTarget = buffer; |
| 140 | |
| 141 | SkSafeUnref(fGrRenderTarget); |
| 142 | fGrRenderTarget = static_cast<GrRenderTarget*>( |
| 143 | fGrContext->createPlatformSurface(desc)); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | virtual GrContext* getGrContext() { |
| 148 | return fGrContext; |
| 149 | } |
| 150 | private: |
| 151 | GrContext* fGrContext; |
| 152 | GrRenderTarget* fGrRenderTarget; |
| 153 | }; |
| 154 | |
| 155 | /////////////// |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 156 | static const char view_inval_msg[] = "view-inval-msg"; |
| 157 | |
Scroggo | 62b65b0 | 2011-06-21 16:01:26 +0000 | [diff] [blame] | 158 | void SampleWindow::postInvalDelay() { |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame^] | 159 | (new SkEvent(view_inval_msg, this->getSinkID()))->postDelay(1); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static bool isInvalEvent(const SkEvent& evt) { |
| 163 | return evt.isType(view_inval_msg); |
| 164 | } |
| 165 | ////////////////// |
| 166 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | SkViewRegister* SkViewRegister::gHead; |
| 168 | SkViewRegister::SkViewRegister(SkViewFactory fact) : fFact(fact) { |
| 169 | static bool gOnce; |
| 170 | if (!gOnce) { |
| 171 | gHead = NULL; |
| 172 | gOnce = true; |
| 173 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 174 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | fChain = gHead; |
| 176 | gHead = this; |
| 177 | } |
| 178 | |
reed@google.com | 29038ed | 2011-07-06 17:56:47 +0000 | [diff] [blame] | 179 | #if 0 |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 180 | #include <CoreFoundation/CoreFoundation.h> |
| 181 | #include <CoreFoundation/CFURLAccess.h> |
| 182 | |
| 183 | static void testpdf() { |
| 184 | CFStringRef path = CFStringCreateWithCString(NULL, "/test.pdf", |
| 185 | kCFStringEncodingUTF8); |
| 186 | CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path, |
| 187 | kCFURLPOSIXPathStyle, |
| 188 | false); |
| 189 | CFRelease(path); |
| 190 | CGRect box = CGRectMake(0, 0, 8*72, 10*72); |
| 191 | CGContextRef cg = CGPDFContextCreateWithURL(url, &box, NULL); |
| 192 | CFRelease(url); |
| 193 | |
| 194 | CGContextBeginPage(cg, &box); |
| 195 | CGRect r = CGRectMake(10, 10, 40 + 0.5, 50 + 0.5); |
| 196 | CGContextFillEllipseInRect(cg, r); |
| 197 | CGContextEndPage(cg); |
| 198 | CGContextRelease(cg); |
| 199 | |
| 200 | if (false) { |
| 201 | SkBitmap bm; |
| 202 | bm.setConfig(SkBitmap::kA8_Config, 64, 64); |
| 203 | bm.allocPixels(); |
| 204 | bm.eraseColor(0); |
| 205 | |
| 206 | SkCanvas canvas(bm); |
| 207 | |
| 208 | } |
| 209 | } |
| 210 | #endif |
| 211 | |
| 212 | ////////////////////////////////////////////////////////////////////////////// |
| 213 | |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 214 | enum FlipAxisEnum { |
| 215 | kFlipAxis_X = (1 << 0), |
| 216 | kFlipAxis_Y = (1 << 1) |
| 217 | }; |
| 218 | |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 219 | static SkTriState cycle_tristate(SkTriState state) { |
| 220 | static const SkTriState gCycle[] = { |
| 221 | /* kFalse_SkTriState -> */ kUnknown_SkTriState, |
| 222 | /* kTrue_SkTriState -> */ kFalse_SkTriState, |
| 223 | /* kUnknown_SkTriState -> */ kTrue_SkTriState, |
| 224 | }; |
| 225 | return gCycle[state]; |
| 226 | } |
| 227 | |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 228 | #include "SkDrawFilter.h" |
| 229 | |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 230 | class FlagsDrawFilter : public SkDrawFilter { |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 231 | public: |
reed@google.com | 09e3baa | 2011-05-18 12:04:31 +0000 | [diff] [blame] | 232 | FlagsDrawFilter(SkTriState lcd, SkTriState aa, SkTriState filter, |
| 233 | SkTriState hinting) : |
| 234 | fLCDState(lcd), fAAState(aa), fFilterState(filter), fHintingState(hinting) {} |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 235 | |
mike@reedtribe.org | 3ce59dc | 2011-04-08 00:38:05 +0000 | [diff] [blame] | 236 | virtual void filter(SkPaint* paint, Type t) { |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 237 | if (kText_Type == t && kUnknown_SkTriState != fLCDState) { |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 238 | paint->setLCDRenderText(kTrue_SkTriState == fLCDState); |
| 239 | } |
| 240 | if (kUnknown_SkTriState != fAAState) { |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 241 | paint->setAntiAlias(kTrue_SkTriState == fAAState); |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 242 | } |
reed@google.com | 176753a | 2011-05-17 15:32:04 +0000 | [diff] [blame] | 243 | if (kUnknown_SkTriState != fFilterState) { |
| 244 | paint->setFilterBitmap(kTrue_SkTriState == fFilterState); |
| 245 | } |
reed@google.com | 09e3baa | 2011-05-18 12:04:31 +0000 | [diff] [blame] | 246 | if (kUnknown_SkTriState != fHintingState) { |
| 247 | paint->setHinting(kTrue_SkTriState == fHintingState ? |
| 248 | SkPaint::kNormal_Hinting : |
| 249 | SkPaint::kSlight_Hinting); |
| 250 | } |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | private: |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 254 | SkTriState fLCDState; |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 255 | SkTriState fAAState; |
reed@google.com | 176753a | 2011-05-17 15:32:04 +0000 | [diff] [blame] | 256 | SkTriState fFilterState; |
reed@google.com | 09e3baa | 2011-05-18 12:04:31 +0000 | [diff] [blame] | 257 | SkTriState fHintingState; |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 258 | }; |
| 259 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 260 | ////////////////////////////////////////////////////////////////////////////// |
| 261 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 262 | #define MAX_ZOOM_LEVEL 8 |
| 263 | #define MIN_ZOOM_LEVEL -8 |
| 264 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 265 | static const char gCharEvtName[] = "SampleCode_Char_Event"; |
| 266 | static const char gKeyEvtName[] = "SampleCode_Key_Event"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 267 | static const char gTitleEvtName[] = "SampleCode_Title_Event"; |
| 268 | static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event"; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 269 | static const char gFastTextEvtName[] = "SampleCode_FastText_Event"; |
| 270 | |
| 271 | bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) { |
| 272 | if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) { |
| 273 | if (outUni) { |
| 274 | *outUni = evt.getFast32(); |
| 275 | } |
| 276 | return true; |
| 277 | } |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) { |
| 282 | if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) { |
| 283 | if (outKey) { |
| 284 | *outKey = (SkKey)evt.getFast32(); |
| 285 | } |
| 286 | return true; |
| 287 | } |
| 288 | return false; |
| 289 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 290 | |
| 291 | bool SampleCode::TitleQ(const SkEvent& evt) { |
| 292 | return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1); |
| 293 | } |
| 294 | |
| 295 | void SampleCode::TitleR(SkEvent* evt, const char title[]) { |
| 296 | SkASSERT(evt && TitleQ(*evt)); |
| 297 | evt->setString(gTitleEvtName, title); |
| 298 | } |
| 299 | |
| 300 | bool SampleCode::PrefSizeQ(const SkEvent& evt) { |
| 301 | return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1); |
| 302 | } |
| 303 | |
| 304 | void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) { |
| 305 | SkASSERT(evt && PrefSizeQ(*evt)); |
| 306 | SkScalar size[2]; |
| 307 | size[0] = width; |
| 308 | size[1] = height; |
| 309 | evt->setScalars(gPrefSizeEvtName, 2, size); |
| 310 | } |
| 311 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 312 | bool SampleCode::FastTextQ(const SkEvent& evt) { |
| 313 | return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1); |
| 314 | } |
| 315 | |
| 316 | /////////////////////////////////////////////////////////////////////////////// |
| 317 | |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 318 | static SkMSec gAnimTime; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 319 | static SkMSec gAnimTimePrev; |
| 320 | |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 321 | SkMSec SampleCode::GetAnimTime() { return gAnimTime; } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 322 | SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; } |
| 323 | SkScalar SampleCode::GetAnimSecondsDelta() { |
| 324 | return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0); |
| 325 | } |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 326 | |
| 327 | SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 328 | // since gAnimTime can be up to 32 bits, we can't convert it to a float |
| 329 | // or we'll lose the low bits. Hence we use doubles for the intermediate |
| 330 | // calculations |
| 331 | double seconds = (double)gAnimTime / 1000.0; |
| 332 | double value = SkScalarToDouble(speed) * seconds; |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 333 | if (period) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 334 | value = ::fmod(value, SkScalarToDouble(period)); |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 335 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 336 | return SkDoubleToScalar(value); |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 337 | } |
| 338 | |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 339 | GrContext* SampleCode::GetGr() { |
| 340 | return gSampleWindow ? gSampleWindow->getGrContext() : NULL; |
| 341 | } |
| 342 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 343 | ////////////////////////////////////////////////////////////////////////////// |
| 344 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 345 | static SkView* curr_view(SkWindow* wind) { |
| 346 | SkView::F2BIter iter(wind); |
| 347 | return iter.next(); |
| 348 | } |
| 349 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 350 | void SampleWindow::setZoomCenter(float x, float y) |
| 351 | { |
| 352 | fZoomCenterX = SkFloatToScalar(x); |
| 353 | fZoomCenterY = SkFloatToScalar(y); |
| 354 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 355 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 356 | bool SampleWindow::zoomIn() |
| 357 | { |
| 358 | // Arbitrarily decided |
| 359 | if (fFatBitsScale == 25) return false; |
| 360 | fFatBitsScale++; |
| 361 | this->inval(NULL); |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | bool SampleWindow::zoomOut() |
| 366 | { |
| 367 | if (fFatBitsScale == 1) return false; |
| 368 | fFatBitsScale--; |
| 369 | this->inval(NULL); |
| 370 | return true; |
| 371 | } |
| 372 | |
| 373 | void SampleWindow::toggleZoomer() |
| 374 | { |
| 375 | fShowZoomer = !fShowZoomer; |
| 376 | this->inval(NULL); |
| 377 | } |
| 378 | |
| 379 | void SampleWindow::updatePointer(int x, int y) |
| 380 | { |
| 381 | fMouseX = x; |
| 382 | fMouseY = y; |
| 383 | if (fShowZoomer) { |
| 384 | this->inval(NULL); |
| 385 | } |
| 386 | } |
| 387 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 388 | static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) { |
| 389 | static const SampleWindow::DeviceType gCT[] = { |
| 390 | SampleWindow::kPicture_DeviceType, |
| 391 | SampleWindow::kGPU_DeviceType, |
| 392 | SampleWindow::kRaster_DeviceType |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 393 | }; |
| 394 | return gCT[ct]; |
| 395 | } |
| 396 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 397 | SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager) : INHERITED(hwnd) { |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 398 | gSampleWindow = this; |
| 399 | |
yangsu@google.com | 1f39421 | 2011-06-01 18:03:34 +0000 | [diff] [blame] | 400 | #ifdef PIPE_FILE |
| 401 | //Clear existing file or create file if it doesn't exist |
| 402 | FILE* f = fopen(FILE_PATH, "wb"); |
| 403 | fclose(f); |
| 404 | #endif |
| 405 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 406 | fPicture = NULL; |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 407 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 408 | #ifdef DEFAULT_TO_GPU |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 409 | fDeviceType = kGPU_DeviceType; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 410 | #else |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 411 | fDeviceType = kRaster_DeviceType; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 412 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 413 | fUseClip = false; |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 414 | fNClip = false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 415 | fRepeatDrawing = false; |
| 416 | fAnimating = false; |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 417 | fRotate = false; |
| 418 | fScale = false; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 419 | fRequestGrabImage = false; |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 420 | fUsePipe = false; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 421 | fMeasureFPS = false; |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 422 | fLCDState = kUnknown_SkTriState; |
| 423 | fAAState = kUnknown_SkTriState; |
reed@google.com | 66f22fd | 2011-05-17 15:33:45 +0000 | [diff] [blame] | 424 | fFilterState = kUnknown_SkTriState; |
reed@google.com | 09e3baa | 2011-05-18 12:04:31 +0000 | [diff] [blame] | 425 | fHintingState = kUnknown_SkTriState; |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 426 | fFlipAxis = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 427 | fScrollTestX = fScrollTestY = 0; |
| 428 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 429 | fMouseX = fMouseY = 0; |
mike@reedtribe.org | 3ce59dc | 2011-04-08 00:38:05 +0000 | [diff] [blame] | 430 | fFatBitsScale = 8; |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 431 | fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold); |
| 432 | fShowZoomer = false; |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 433 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 434 | fZoomLevel = 0; |
| 435 | fZoomScale = SK_Scalar1; |
| 436 | |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 437 | fSaveToPdf = false; |
| 438 | fPdfCanvas = NULL; |
| 439 | |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 440 | int sinkID = this->getSinkID(); |
| 441 | fAppMenu.setTitle("Global Settings"); |
| 442 | fAppMenu.appendList("Device Type", "Device Type", sinkID, 0, "Raster", "Picture", "OpenGL", NULL); |
| 443 | fAppMenu.appendTriState("AA", "AA", sinkID, SkOSMenu::kMixedState); |
| 444 | fAppMenu.appendTriState("LCD", "LCD", sinkID, SkOSMenu::kMixedState); |
| 445 | fAppMenu.appendTriState("Filter", "Filter", sinkID, SkOSMenu::kMixedState); |
| 446 | fAppMenu.appendTriState("Hinting", "Hinting", sinkID, SkOSMenu::kMixedState); |
| 447 | fAppMenu.appendSwitch("Pipe", "Pipe" , sinkID, fUsePipe); |
| 448 | fAppMenu.appendSwitch("Slide Show", "Slide Show" , sinkID, false); |
| 449 | fAppMenu.appendSwitch("Clip", "Clip" , sinkID, fUseClip); |
| 450 | fAppMenu.appendSwitch("Measure FPS", "Measure FPS" , sinkID, fMeasureFPS); |
| 451 | fAppMenu.appendSwitch("Flip X", "Flip X" , sinkID, false); |
| 452 | fAppMenu.appendSwitch("Flip Y", "Flip Y" , sinkID, false); |
| 453 | fAppMenu.appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer); |
| 454 | fAppMenu.appendAction("Save to PDF", sinkID); |
| 455 | |
| 456 | this->addMenu(&fAppMenu); |
| 457 | this->addMenu(&fSlideMenu); |
| 458 | |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 459 | // this->setConfig(SkBitmap::kRGB_565_Config); |
| 460 | this->setConfig(SkBitmap::kARGB_8888_Config); |
| 461 | this->setVisibleP(true); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 462 | this->setClipToBounds(false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 463 | |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 464 | { |
| 465 | const SkViewRegister* reg = SkViewRegister::Head(); |
| 466 | while (reg) { |
| 467 | *fSamples.append() = reg->factory(); |
| 468 | reg = reg->next(); |
| 469 | } |
| 470 | } |
| 471 | fCurrIndex = 0; |
senorblanco@chromium.org | 78b8253 | 2011-06-28 19:44:03 +0000 | [diff] [blame] | 472 | if (argc > 1) { |
| 473 | int i, count = fSamples.count(); |
| 474 | for (i = 0; i < count; i++) { |
| 475 | SkString title = getSampleTitle(i); |
| 476 | if (title.equals(argv[1])) { |
| 477 | fCurrIndex = i; |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | if (i == count) { |
| 482 | fprintf(stderr, "Unknown sample \"%s\"\n", argv[1]); |
| 483 | } |
| 484 | } |
reed@android.com | e0f13ee | 2009-11-04 19:40:25 +0000 | [diff] [blame] | 485 | this->loadView(fSamples[fCurrIndex]()); |
bsalomon@google.com | 840e9f3 | 2011-07-06 21:59:09 +0000 | [diff] [blame] | 486 | |
| 487 | fPDFData = NULL; |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 488 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 489 | if (NULL == devManager) { |
| 490 | fDevManager = new DefaultDeviceManager(); |
| 491 | } else { |
| 492 | devManager->ref(); |
| 493 | fDevManager = devManager; |
| 494 | } |
| 495 | fDevManager->init(this); |
| 496 | |
Scroggo | b4490c7 | 2011-06-17 13:53:05 +0000 | [diff] [blame] | 497 | // If another constructor set our dimensions, ensure that our |
| 498 | // onSizeChange gets called. |
| 499 | if (this->height() && this->width()) { |
| 500 | this->onSizeChange(); |
| 501 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | SampleWindow::~SampleWindow() { |
| 505 | delete fPicture; |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 506 | delete fPdfCanvas; |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 507 | fTypeface->unref(); |
reed@google.com | 29038ed | 2011-07-06 17:56:47 +0000 | [diff] [blame] | 508 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 509 | SkSafeUnref(fDevManager); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 510 | } |
| 511 | |
reed@android.com | 55e76b2 | 2009-11-23 21:46:47 +0000 | [diff] [blame] | 512 | static SkBitmap capture_bitmap(SkCanvas* canvas) { |
| 513 | SkBitmap bm; |
| 514 | const SkBitmap& src = canvas->getDevice()->accessBitmap(false); |
| 515 | src.copyTo(&bm, src.config()); |
| 516 | return bm; |
| 517 | } |
| 518 | |
| 519 | static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig, |
| 520 | SkBitmap* diff) { |
| 521 | const SkBitmap& src = canvas->getDevice()->accessBitmap(false); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 522 | |
reed@android.com | 55e76b2 | 2009-11-23 21:46:47 +0000 | [diff] [blame] | 523 | SkAutoLockPixels alp0(src); |
| 524 | SkAutoLockPixels alp1(orig); |
| 525 | for (int y = 0; y < src.height(); y++) { |
| 526 | const void* srcP = src.getAddr(0, y); |
| 527 | const void* origP = orig.getAddr(0, y); |
| 528 | size_t bytes = src.width() * src.bytesPerPixel(); |
| 529 | if (memcmp(srcP, origP, bytes)) { |
| 530 | SkDebugf("---------- difference on line %d\n", y); |
| 531 | return true; |
| 532 | } |
| 533 | } |
| 534 | return false; |
| 535 | } |
| 536 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 537 | static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint) |
| 538 | { |
| 539 | SkColor desiredColor = paint.getColor(); |
| 540 | paint.setColor(SK_ColorWHITE); |
| 541 | const char* c_str = string.c_str(); |
| 542 | size_t size = string.size(); |
| 543 | SkRect bounds; |
| 544 | paint.measureText(c_str, size, &bounds); |
| 545 | bounds.offset(left, top); |
| 546 | SkScalar inset = SkIntToScalar(-2); |
| 547 | bounds.inset(inset, inset); |
| 548 | canvas->drawRect(bounds, paint); |
| 549 | if (desiredColor != SK_ColorBLACK) { |
| 550 | paint.setColor(SK_ColorBLACK); |
| 551 | canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint); |
| 552 | } |
| 553 | paint.setColor(desiredColor); |
| 554 | canvas->drawText(c_str, size, left, top, paint); |
| 555 | } |
| 556 | |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 557 | #define XCLIP_N 8 |
| 558 | #define YCLIP_N 8 |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 559 | |
| 560 | void SampleWindow::draw(SkCanvas* canvas) { |
reed@google.com | 29038ed | 2011-07-06 17:56:47 +0000 | [diff] [blame] | 561 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 562 | if (!fDevManager->prepareCanvas(fDeviceType, canvas, this)) { |
| 563 | return; |
| 564 | } |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 565 | // update the animation time |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 566 | gAnimTimePrev = gAnimTime; |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 567 | gAnimTime = SkTime::GetMSecs(); |
| 568 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 569 | SkScalar cx = fZoomCenterX; |
| 570 | SkScalar cy = fZoomCenterY; |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 571 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 572 | if (fZoomLevel) { |
| 573 | SkMatrix m; |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 574 | SkPoint center; |
| 575 | m = canvas->getTotalMatrix();//.invert(&m); |
| 576 | m.mapXY(cx, cy, ¢er); |
| 577 | cx = center.fX; |
| 578 | cy = center.fY; |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 579 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 580 | m.setTranslate(-cx, -cy); |
| 581 | m.postScale(fZoomScale, fZoomScale); |
| 582 | m.postTranslate(cx, cy); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 583 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 584 | canvas->concat(m); |
| 585 | } |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 586 | |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 587 | if (fFlipAxis) { |
| 588 | SkMatrix m; |
| 589 | m.setTranslate(cx, cy); |
| 590 | if (fFlipAxis & kFlipAxis_X) { |
| 591 | m.preScale(-SK_Scalar1, SK_Scalar1); |
| 592 | } |
| 593 | if (fFlipAxis & kFlipAxis_Y) { |
| 594 | m.preScale(SK_Scalar1, -SK_Scalar1); |
| 595 | } |
| 596 | m.preTranslate(-cx, -cy); |
| 597 | canvas->concat(m); |
| 598 | } |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 599 | |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 600 | // Apply any gesture matrix |
| 601 | if (true) { |
| 602 | const SkMatrix& localM = fGesture.localM(); |
| 603 | if (localM.getType() & SkMatrix::kScale_Mask) { |
| 604 | canvas->setExternalMatrix(&localM); |
| 605 | } |
| 606 | canvas->concat(localM); |
| 607 | canvas->concat(fGesture.globalM()); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 608 | |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 609 | if (fGesture.isActive()) { |
| 610 | this->inval(NULL); |
| 611 | } |
| 612 | } |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 613 | |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 614 | if (fNClip) { |
reed@android.com | 55e76b2 | 2009-11-23 21:46:47 +0000 | [diff] [blame] | 615 | this->INHERITED::draw(canvas); |
| 616 | SkBitmap orig = capture_bitmap(canvas); |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 617 | |
| 618 | const SkScalar w = this->width(); |
| 619 | const SkScalar h = this->height(); |
| 620 | const SkScalar cw = w / XCLIP_N; |
| 621 | const SkScalar ch = h / YCLIP_N; |
| 622 | for (int y = 0; y < YCLIP_N; y++) { |
reed@android.com | 55e76b2 | 2009-11-23 21:46:47 +0000 | [diff] [blame] | 623 | SkRect r; |
| 624 | r.fTop = y * ch; |
| 625 | r.fBottom = (y + 1) * ch; |
| 626 | if (y == YCLIP_N - 1) { |
| 627 | r.fBottom = h; |
| 628 | } |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 629 | for (int x = 0; x < XCLIP_N; x++) { |
| 630 | SkAutoCanvasRestore acr(canvas, true); |
reed@android.com | 55e76b2 | 2009-11-23 21:46:47 +0000 | [diff] [blame] | 631 | r.fLeft = x * cw; |
| 632 | r.fRight = (x + 1) * cw; |
| 633 | if (x == XCLIP_N - 1) { |
| 634 | r.fRight = w; |
| 635 | } |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 636 | canvas->clipRect(r); |
| 637 | this->INHERITED::draw(canvas); |
| 638 | } |
| 639 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 640 | |
reed@android.com | 55e76b2 | 2009-11-23 21:46:47 +0000 | [diff] [blame] | 641 | SkBitmap diff; |
| 642 | if (bitmap_diff(canvas, orig, &diff)) { |
| 643 | } |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 644 | } else { |
| 645 | this->INHERITED::draw(canvas); |
| 646 | } |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 647 | if (fShowZoomer && !fSaveToPdf) { |
Scroggo | 3272ba8 | 2011-05-25 20:50:42 +0000 | [diff] [blame] | 648 | showZoomer(canvas); |
| 649 | } |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 650 | |
reed@google.com | 29038ed | 2011-07-06 17:56:47 +0000 | [diff] [blame] | 651 | // do this last |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 652 | fDevManager->publishCanvas(fDeviceType, canvas, this); |
Scroggo | 3272ba8 | 2011-05-25 20:50:42 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | void SampleWindow::showZoomer(SkCanvas* canvas) { |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 656 | int count = canvas->save(); |
| 657 | canvas->resetMatrix(); |
| 658 | // Ensure the mouse position is on screen. |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 659 | int width = SkScalarRound(this->width()); |
| 660 | int height = SkScalarRound(this->height()); |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 661 | if (fMouseX >= width) fMouseX = width - 1; |
| 662 | else if (fMouseX < 0) fMouseX = 0; |
| 663 | if (fMouseY >= height) fMouseY = height - 1; |
| 664 | else if (fMouseY < 0) fMouseY = 0; |
reed@google.com | b36334d | 2011-05-18 15:07:20 +0000 | [diff] [blame] | 665 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 666 | SkBitmap bitmap = capture_bitmap(canvas); |
reed@google.com | b36334d | 2011-05-18 15:07:20 +0000 | [diff] [blame] | 667 | bitmap.lockPixels(); |
| 668 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 669 | // Find the size of the zoomed in view, forced to be odd, so the examined pixel is in the middle. |
mike@reedtribe.org | 3ce59dc | 2011-04-08 00:38:05 +0000 | [diff] [blame] | 670 | int zoomedWidth = (width >> 1) | 1; |
| 671 | int zoomedHeight = (height >> 1) | 1; |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 672 | SkIRect src; |
| 673 | src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale); |
| 674 | src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1)); |
| 675 | SkRect dest; |
| 676 | dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight)); |
| 677 | dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight)); |
| 678 | SkPaint paint; |
| 679 | // Clear the background behind our zoomed in view |
| 680 | paint.setColor(SK_ColorWHITE); |
| 681 | canvas->drawRect(dest, paint); |
| 682 | canvas->drawBitmapRect(bitmap, &src, dest); |
| 683 | paint.setColor(SK_ColorBLACK); |
| 684 | paint.setStyle(SkPaint::kStroke_Style); |
| 685 | // Draw a border around the pixel in the middle |
| 686 | SkRect originalPixel; |
| 687 | originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1)); |
| 688 | SkMatrix matrix; |
| 689 | SkRect scalarSrc; |
| 690 | scalarSrc.set(src); |
| 691 | SkColor color = bitmap.getColor(fMouseX, fMouseY); |
| 692 | if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) { |
| 693 | SkRect pixel; |
| 694 | matrix.mapRect(&pixel, originalPixel); |
| 695 | // TODO Perhaps measure the values and make the outline white if it's "dark" |
| 696 | if (color == SK_ColorBLACK) { |
| 697 | paint.setColor(SK_ColorWHITE); |
| 698 | } |
| 699 | canvas->drawRect(pixel, paint); |
| 700 | } |
| 701 | paint.setColor(SK_ColorBLACK); |
| 702 | // Draw a border around the destination rectangle |
| 703 | canvas->drawRect(dest, paint); |
| 704 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
| 705 | // Identify the pixel and its color on screen |
| 706 | paint.setTypeface(fTypeface); |
| 707 | paint.setAntiAlias(true); |
| 708 | SkScalar lineHeight = paint.getFontMetrics(NULL); |
| 709 | SkString string; |
| 710 | string.appendf("(%i, %i)", fMouseX, fMouseY); |
| 711 | SkScalar left = dest.fLeft + SkIntToScalar(3); |
| 712 | SkScalar i = SK_Scalar1; |
| 713 | drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint); |
| 714 | // Alpha |
| 715 | i += SK_Scalar1; |
| 716 | string.reset(); |
| 717 | string.appendf("A: %X", SkColorGetA(color)); |
| 718 | drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint); |
| 719 | // Red |
| 720 | i += SK_Scalar1; |
| 721 | string.reset(); |
| 722 | string.appendf("R: %X", SkColorGetR(color)); |
| 723 | paint.setColor(SK_ColorRED); |
| 724 | drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint); |
| 725 | // Green |
| 726 | i += SK_Scalar1; |
| 727 | string.reset(); |
| 728 | string.appendf("G: %X", SkColorGetG(color)); |
| 729 | paint.setColor(SK_ColorGREEN); |
| 730 | drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint); |
| 731 | // Blue |
| 732 | i += SK_Scalar1; |
| 733 | string.reset(); |
| 734 | string.appendf("B: %X", SkColorGetB(color)); |
| 735 | paint.setColor(SK_ColorBLUE); |
| 736 | drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint); |
| 737 | canvas->restoreToCount(count); |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 738 | } |
| 739 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 740 | void SampleWindow::onDraw(SkCanvas* canvas) { |
| 741 | if (fRepeatDrawing) { |
| 742 | this->inval(NULL); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | #include "SkColorPriv.h" |
| 747 | |
| 748 | static void reverseRedAndBlue(const SkBitmap& bm) { |
| 749 | SkASSERT(bm.config() == SkBitmap::kARGB_8888_Config); |
| 750 | uint8_t* p = (uint8_t*)bm.getPixels(); |
| 751 | uint8_t* stop = p + bm.getSize(); |
| 752 | while (p < stop) { |
| 753 | // swap red/blue (to go from ARGB(int) to RGBA(memory) and premultiply |
| 754 | unsigned scale = SkAlpha255To256(p[3]); |
| 755 | unsigned r = p[2]; |
| 756 | unsigned b = p[0]; |
| 757 | p[0] = SkAlphaMul(r, scale); |
| 758 | p[1] = SkAlphaMul(p[1], scale); |
| 759 | p[2] = SkAlphaMul(b, scale); |
| 760 | p += 4; |
| 761 | } |
| 762 | } |
| 763 | |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 764 | void SampleWindow::saveToPdf() |
| 765 | { |
| 766 | fSaveToPdf = true; |
| 767 | this->inval(NULL); |
| 768 | } |
| 769 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 770 | SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) { |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 771 | if (fSaveToPdf) { |
| 772 | const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false); |
| 773 | SkISize size = SkISize::Make(bmp.width(), bmp.height()); |
| 774 | SkPDFDevice* pdfDevice = new SkPDFDevice(size, size, |
| 775 | canvas->getTotalMatrix()); |
| 776 | fPdfCanvas = new SkCanvas(pdfDevice); |
| 777 | pdfDevice->unref(); |
| 778 | canvas = fPdfCanvas; |
| 779 | } else { |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 780 | switch (fDeviceType) { |
| 781 | case kRaster_DeviceType: |
| 782 | case kGPU_DeviceType: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 783 | canvas = this->INHERITED::beforeChildren(canvas); |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 784 | break; |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 785 | case kPicture_DeviceType: |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 786 | fPicture = new SkPicture; |
| 787 | canvas = fPicture->beginRecording(9999, 9999); |
| 788 | break; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 789 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | if (fUseClip) { |
| 793 | canvas->drawColor(0xFFFF88FF); |
| 794 | canvas->clipPath(fClipPath); |
| 795 | } |
| 796 | |
| 797 | return canvas; |
| 798 | } |
| 799 | |
| 800 | static void paint_rgn(const SkBitmap& bm, const SkIRect& r, |
| 801 | const SkRegion& rgn) { |
| 802 | SkCanvas canvas(bm); |
| 803 | SkRegion inval(rgn); |
| 804 | |
| 805 | inval.translate(r.fLeft, r.fTop); |
| 806 | canvas.clipRegion(inval); |
| 807 | canvas.drawColor(0xFFFF8080); |
| 808 | } |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame] | 809 | #include "SkData.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 810 | void SampleWindow::afterChildren(SkCanvas* orig) { |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 811 | if (fSaveToPdf) { |
| 812 | fSaveToPdf = false; |
| 813 | if (fShowZoomer) { |
| 814 | showZoomer(fPdfCanvas); |
| 815 | } |
| 816 | SkString name; |
| 817 | name.printf("%s.pdf", this->getTitle()); |
| 818 | SkPDFDocument doc; |
| 819 | SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice()); |
| 820 | doc.appendPage(device); |
| 821 | #ifdef ANDROID |
| 822 | name.prepend("/sdcard/"); |
| 823 | #endif |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame] | 824 | |
| 825 | #ifdef SK_BUILD_FOR_IOS |
| 826 | SkDynamicMemoryWStream mstream; |
| 827 | doc.emitPDF(&mstream); |
yangsu@google.com | ae8a2e5 | 2011-06-24 21:09:39 +0000 | [diff] [blame] | 828 | fPDFData = mstream.copyToData(); |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame] | 829 | #endif |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 830 | SkFILEWStream stream(name.c_str()); |
| 831 | if (stream.isValid()) { |
| 832 | doc.emitPDF(&stream); |
| 833 | const char* desc = "File saved from Skia SampleApp"; |
| 834 | this->onPDFSaved(this->getTitle(), desc, name.c_str()); |
| 835 | } |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame] | 836 | |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 837 | delete fPdfCanvas; |
| 838 | fPdfCanvas = NULL; |
| 839 | |
| 840 | // We took over the draw calls in order to create the PDF, so we need |
| 841 | // to redraw. |
| 842 | this->inval(NULL); |
| 843 | return; |
| 844 | } |
| 845 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 846 | if (fRequestGrabImage) { |
| 847 | fRequestGrabImage = false; |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 848 | |
reed@google.com | 29038ed | 2011-07-06 17:56:47 +0000 | [diff] [blame] | 849 | SkDevice* device = orig->getDevice(); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 850 | SkBitmap bmp; |
| 851 | if (device->accessBitmap(false).copyTo(&bmp, SkBitmap::kARGB_8888_Config)) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 852 | static int gSampleGrabCounter; |
| 853 | SkString name; |
| 854 | name.printf("sample_grab_%d", gSampleGrabCounter++); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 855 | SkImageEncoder::EncodeFile(name.c_str(), bmp, |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 856 | SkImageEncoder::kPNG_Type, 100); |
| 857 | } |
| 858 | } |
| 859 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 860 | if (kPicture_DeviceType == fDeviceType) { |
| 861 | if (true) { |
| 862 | SkPicture* pict = new SkPicture(*fPicture); |
| 863 | fPicture->unref(); |
| 864 | orig->drawPicture(*pict); |
| 865 | pict->unref(); |
| 866 | } else if (true) { |
| 867 | SkDynamicMemoryWStream ostream; |
| 868 | fPicture->serialize(&ostream); |
| 869 | fPicture->unref(); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 870 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 871 | SkAutoDataUnref data(ostream.copyToData()); |
| 872 | SkMemoryStream istream(data.data(), data.size()); |
| 873 | SkPicture pict(&istream); |
| 874 | orig->drawPicture(pict); |
| 875 | } else { |
| 876 | fPicture->draw(orig); |
| 877 | fPicture->unref(); |
| 878 | } |
| 879 | fPicture = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 880 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 881 | |
reed@google.com | 17d7aec | 2011-04-25 14:31:44 +0000 | [diff] [blame] | 882 | // Do this after presentGL and other finishing, rather than in afterChild |
| 883 | if (fMeasureFPS && fMeasureFPS_Time) { |
| 884 | fMeasureFPS_Time = SkTime::GetMSecs() - fMeasureFPS_Time; |
| 885 | this->updateTitle(); |
Scroggo | 62b65b0 | 2011-06-21 16:01:26 +0000 | [diff] [blame] | 886 | this->postInvalDelay(); |
reed@google.com | 17d7aec | 2011-04-25 14:31:44 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | // if ((fScrollTestX | fScrollTestY) != 0) |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 890 | if (false) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 891 | const SkBitmap& bm = orig->getDevice()->accessBitmap(true); |
| 892 | int dx = fScrollTestX * 7; |
| 893 | int dy = fScrollTestY * 7; |
| 894 | SkIRect r; |
| 895 | SkRegion inval; |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 896 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 897 | r.set(50, 50, 50+100, 50+100); |
| 898 | bm.scrollRect(&r, dx, dy, &inval); |
| 899 | paint_rgn(bm, r, inval); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 900 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 901 | } |
| 902 | |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 903 | void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) { |
| 904 | if (fScale) { |
| 905 | SkScalar scale = SK_Scalar1 * 7 / 10; |
| 906 | SkScalar cx = this->width() / 2; |
| 907 | SkScalar cy = this->height() / 2; |
| 908 | canvas->translate(cx, cy); |
| 909 | canvas->scale(scale, scale); |
| 910 | canvas->translate(-cx, -cy); |
| 911 | } |
| 912 | if (fRotate) { |
| 913 | SkScalar cx = this->width() / 2; |
| 914 | SkScalar cy = this->height() / 2; |
| 915 | canvas->translate(cx, cy); |
| 916 | canvas->rotate(SkIntToScalar(30)); |
| 917 | canvas->translate(-cx, -cy); |
| 918 | } |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 919 | |
reed@google.com | 09e3baa | 2011-05-18 12:04:31 +0000 | [diff] [blame] | 920 | canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState, |
| 921 | fFilterState, fHintingState))->unref(); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 922 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 923 | if (fMeasureFPS) { |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 924 | fMeasureFPS_Time = 0; // 0 means the child is not aware of repeat-draw |
| 925 | if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) { |
| 926 | fMeasureFPS_Time = SkTime::GetMSecs(); |
| 927 | } |
| 928 | } else { |
| 929 | (void)SampleView::SetRepeatDraw(child, 1); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 930 | } |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 931 | (void)SampleView::SetUsePipe(child, fUsePipe); |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) { |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 935 | canvas->setDrawFilter(NULL); |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 936 | } |
| 937 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 938 | static SkBitmap::Config gConfigCycle[] = { |
| 939 | SkBitmap::kNo_Config, // none -> none |
| 940 | SkBitmap::kNo_Config, // a1 -> none |
| 941 | SkBitmap::kNo_Config, // a8 -> none |
| 942 | SkBitmap::kNo_Config, // index8 -> none |
| 943 | SkBitmap::kARGB_4444_Config, // 565 -> 4444 |
| 944 | SkBitmap::kARGB_8888_Config, // 4444 -> 8888 |
| 945 | SkBitmap::kRGB_565_Config // 8888 -> 565 |
| 946 | }; |
| 947 | |
| 948 | static SkBitmap::Config cycle_configs(SkBitmap::Config c) { |
| 949 | return gConfigCycle[c]; |
| 950 | } |
| 951 | |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 952 | void SampleWindow::changeZoomLevel(float delta) { |
| 953 | fZoomLevel += SkFloatToScalar(delta); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 954 | if (fZoomLevel > 0) { |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 955 | fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL); |
| 956 | fZoomScale = fZoomLevel + SK_Scalar1; |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 957 | } else if (fZoomLevel < 0) { |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 958 | fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL); |
| 959 | fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 960 | } else { |
| 961 | fZoomScale = SK_Scalar1; |
| 962 | } |
| 963 | |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 964 | this->updateTitle(); |
| 965 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 966 | this->inval(NULL); |
| 967 | } |
| 968 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 969 | bool SampleWindow::previousSample() { |
Scroggo | 62b65b0 | 2011-06-21 16:01:26 +0000 | [diff] [blame] | 970 | fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count(); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 971 | this->loadView(fSamples[fCurrIndex]()); |
| 972 | return true; |
| 973 | } |
| 974 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 975 | bool SampleWindow::nextSample() { |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 976 | fCurrIndex = (fCurrIndex + 1) % fSamples.count(); |
| 977 | this->loadView(fSamples[fCurrIndex]()); |
| 978 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 979 | } |
| 980 | |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame] | 981 | bool SampleWindow::goToSample(int i) { |
| 982 | fCurrIndex = (i) % fSamples.count(); |
| 983 | this->loadView(fSamples[fCurrIndex]()); |
| 984 | return true; |
| 985 | } |
| 986 | |
| 987 | SkString SampleWindow::getSampleTitle(int i) { |
| 988 | SkView* view = fSamples[i](); |
| 989 | SkString title; |
| 990 | SkEvent evt(gTitleEvtName); |
| 991 | if (view->doQuery(&evt)) { |
| 992 | title.set(evt.findString(gTitleEvtName)); |
| 993 | } |
| 994 | view->unref(); |
| 995 | return title; |
| 996 | } |
| 997 | |
| 998 | int SampleWindow::sampleCount() { |
| 999 | return fSamples.count(); |
| 1000 | } |
| 1001 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1002 | void SampleWindow::postAnimatingEvent() { |
| 1003 | if (fAnimating) { |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame^] | 1004 | (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1008 | bool SampleWindow::onEvent(const SkEvent& evt) { |
| 1009 | if (evt.isType(ANIMATING_EVENTTYPE)) { |
| 1010 | if (fAnimating) { |
| 1011 | this->nextSample(); |
| 1012 | this->postAnimatingEvent(); |
| 1013 | } |
| 1014 | return true; |
| 1015 | } |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 1016 | if (evt.isType("set-curr-index")) { |
| 1017 | fCurrIndex = evt.getFast32() % fSamples.count(); |
| 1018 | this->loadView(fSamples[fCurrIndex]()); |
| 1019 | return true; |
| 1020 | } |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1021 | if (isInvalEvent(evt)) { |
| 1022 | this->inval(NULL); |
| 1023 | return true; |
| 1024 | } |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1025 | int selected = -1; |
| 1026 | if (SkOSMenu::FindListIndex(&evt, "Device Type", &selected)) { |
| 1027 | this->setDeviceType((DeviceType)selected); |
| 1028 | return true; |
| 1029 | } |
| 1030 | SkOSMenu::TriState state; |
| 1031 | if (SkOSMenu::FindTriState(&evt, "AA", &state)) { |
| 1032 | fAAState = (SkTriState)state; |
| 1033 | this->updateTitle(); |
| 1034 | this->inval(NULL); |
| 1035 | } |
| 1036 | if (SkOSMenu::FindTriState(&evt, "LCD", &state)) { |
| 1037 | fLCDState = (SkTriState)state; |
| 1038 | this->updateTitle(); |
| 1039 | this->inval(NULL); |
| 1040 | } |
| 1041 | if (SkOSMenu::FindTriState(&evt, "Filter", &state)) { |
| 1042 | fFilterState = (SkTriState)state; |
| 1043 | this->updateTitle(); |
| 1044 | this->inval(NULL); |
| 1045 | } |
| 1046 | if (SkOSMenu::FindTriState(&evt, "Hinting", &state)) { |
| 1047 | fHintingState = (SkTriState)state; |
| 1048 | this->updateTitle(); |
| 1049 | this->inval(NULL); |
| 1050 | } |
| 1051 | if (SkOSMenu::FindSwitchState(&evt, "Pipe", NULL)) { |
| 1052 | this->togglePipe(); |
| 1053 | return true; |
| 1054 | } |
| 1055 | if (SkOSMenu::FindSwitchState(&evt, "Slide Show", NULL)) { |
| 1056 | this->toggleSlideshow(); |
| 1057 | return true; |
| 1058 | } |
| 1059 | if (SkOSMenu::FindSwitchState(&evt, "Clip", NULL)) { |
| 1060 | fUseClip = !fUseClip; |
| 1061 | this->inval(NULL); |
| 1062 | this->updateTitle(); |
| 1063 | return true; |
| 1064 | } |
| 1065 | if (SkOSMenu::FindSwitchState(&evt, "Measure FPS", NULL)) { |
| 1066 | this->toggleFPS(); |
| 1067 | return true; |
| 1068 | } |
| 1069 | if (SkOSMenu::FindSwitchState(&evt, "Flip X", NULL)) { |
| 1070 | fFlipAxis ^= kFlipAxis_X; |
| 1071 | this->updateTitle(); |
| 1072 | this->inval(NULL); |
| 1073 | return true; |
| 1074 | } |
| 1075 | if (SkOSMenu::FindSwitchState(&evt, "Flip Y", NULL)) { |
| 1076 | fFlipAxis ^= kFlipAxis_Y; |
| 1077 | this->updateTitle(); |
| 1078 | this->inval(NULL); |
| 1079 | return true; |
| 1080 | } |
| 1081 | if (SkOSMenu::FindSwitchState(&evt, "Zoomer", NULL)) { |
| 1082 | this->toggleZoomer(); |
| 1083 | return true; |
| 1084 | } |
| 1085 | if (SkOSMenu::FindAction(&evt,"Save to PDF")) { |
| 1086 | this->saveToPdf(); |
| 1087 | return true; |
| 1088 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1089 | return this->INHERITED::onEvent(evt); |
| 1090 | } |
| 1091 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1092 | bool SampleWindow::onQuery(SkEvent* query) { |
| 1093 | if (query->isType("get-slide-count")) { |
| 1094 | query->setFast32(fSamples.count()); |
| 1095 | return true; |
| 1096 | } |
| 1097 | if (query->isType("get-slide-title")) { |
| 1098 | SkView* view = fSamples[query->getFast32()](); |
| 1099 | SkEvent evt(gTitleEvtName); |
| 1100 | if (view->doQuery(&evt)) { |
| 1101 | query->setString("title", evt.findString(gTitleEvtName)); |
| 1102 | } |
| 1103 | SkSafeUnref(view); |
| 1104 | return true; |
| 1105 | } |
| 1106 | if (query->isType("use-fast-text")) { |
| 1107 | SkEvent evt(gFastTextEvtName); |
| 1108 | return curr_view(this)->doQuery(&evt); |
| 1109 | } |
reed@google.com | 29038ed | 2011-07-06 17:56:47 +0000 | [diff] [blame] | 1110 | if (query->isType("ignore-window-bitmap")) { |
| 1111 | query->setFast32(this->getGrContext() != NULL); |
| 1112 | return true; |
| 1113 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1114 | return this->INHERITED::onQuery(query); |
| 1115 | } |
| 1116 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 1117 | static void cleanup_for_filename(SkString* name) { |
| 1118 | char* str = name->writable_str(); |
reed@android.com | e191b16 | 2009-12-18 21:33:39 +0000 | [diff] [blame] | 1119 | for (size_t i = 0; i < name->size(); i++) { |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 1120 | switch (str[i]) { |
| 1121 | case ':': str[i] = '-'; break; |
| 1122 | case '/': str[i] = '-'; break; |
| 1123 | case ' ': str[i] = '_'; break; |
| 1124 | default: break; |
| 1125 | } |
| 1126 | } |
| 1127 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1128 | |
| 1129 | bool SampleWindow::onHandleChar(SkUnichar uni) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1130 | { |
| 1131 | SkView* view = curr_view(this); |
| 1132 | if (view) { |
| 1133 | SkEvent evt(gCharEvtName); |
| 1134 | evt.setFast32(uni); |
| 1135 | if (view->doQuery(&evt)) { |
| 1136 | return true; |
| 1137 | } |
| 1138 | } |
| 1139 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1140 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1141 | int dx = 0xFF; |
| 1142 | int dy = 0xFF; |
| 1143 | |
| 1144 | switch (uni) { |
| 1145 | case '5': dx = 0; dy = 0; break; |
| 1146 | case '8': dx = 0; dy = -1; break; |
| 1147 | case '6': dx = 1; dy = 0; break; |
| 1148 | case '2': dx = 0; dy = 1; break; |
| 1149 | case '4': dx = -1; dy = 0; break; |
| 1150 | case '7': dx = -1; dy = -1; break; |
| 1151 | case '9': dx = 1; dy = -1; break; |
| 1152 | case '3': dx = 1; dy = 1; break; |
| 1153 | case '1': dx = -1; dy = 1; break; |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1154 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1155 | default: |
| 1156 | break; |
| 1157 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1158 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1159 | if (0xFF != dx && 0xFF != dy) { |
| 1160 | if ((dx | dy) == 0) { |
| 1161 | fScrollTestX = fScrollTestY = 0; |
| 1162 | } else { |
| 1163 | fScrollTestX += dx; |
| 1164 | fScrollTestY += dy; |
| 1165 | } |
| 1166 | this->inval(NULL); |
| 1167 | return true; |
| 1168 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1169 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 1170 | switch (uni) { |
| 1171 | case 'a': |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1172 | this->toggleSlideshow(); |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 1173 | return true; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1174 | case 'b': |
| 1175 | fAAState = cycle_tristate(fAAState); |
| 1176 | this->updateTitle(); |
| 1177 | this->inval(NULL); |
| 1178 | break; |
| 1179 | case 'c': |
| 1180 | fUseClip = !fUseClip; |
| 1181 | this->inval(NULL); |
| 1182 | this->updateTitle(); |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 1183 | return true; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1184 | case 'd': |
| 1185 | SkGraphics::SetFontCacheUsed(0); |
| 1186 | return true; |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 1187 | case 'e': |
| 1188 | this->saveToPdf(); |
| 1189 | break; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1190 | case 'f': |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1191 | this->toggleFPS(); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1192 | break; |
| 1193 | case 'g': |
| 1194 | fRequestGrabImage = true; |
| 1195 | this->inval(NULL); |
| 1196 | break; |
reed@google.com | 09e3baa | 2011-05-18 12:04:31 +0000 | [diff] [blame] | 1197 | case 'h': |
| 1198 | fHintingState = cycle_tristate(fHintingState); |
| 1199 | this->updateTitle(); |
| 1200 | this->inval(NULL); |
| 1201 | break; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1202 | case 'i': |
| 1203 | this->zoomIn(); |
| 1204 | break; |
| 1205 | case 'l': |
| 1206 | fLCDState = cycle_tristate(fLCDState); |
| 1207 | this->updateTitle(); |
| 1208 | this->inval(NULL); |
| 1209 | break; |
reed@google.com | 176753a | 2011-05-17 15:32:04 +0000 | [diff] [blame] | 1210 | case 'n': |
| 1211 | fFilterState = cycle_tristate(fFilterState); |
| 1212 | this->updateTitle(); |
| 1213 | this->inval(NULL); |
| 1214 | break; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1215 | case 'o': |
| 1216 | this->zoomOut(); |
| 1217 | break; |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1218 | case 'p': |
| 1219 | fUsePipe = !fUsePipe; |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 1220 | this->updateTitle(); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1221 | this->inval(NULL); |
| 1222 | break; |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 1223 | case 'r': |
| 1224 | fRotate = !fRotate; |
| 1225 | this->inval(NULL); |
| 1226 | this->updateTitle(); |
| 1227 | return true; |
| 1228 | case 's': |
| 1229 | fScale = !fScale; |
| 1230 | this->inval(NULL); |
| 1231 | this->updateTitle(); |
| 1232 | return true; |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 1233 | case 'x': |
| 1234 | fFlipAxis ^= kFlipAxis_X; |
| 1235 | this->updateTitle(); |
| 1236 | this->inval(NULL); |
| 1237 | break; |
| 1238 | case 'y': |
| 1239 | fFlipAxis ^= kFlipAxis_Y; |
| 1240 | this->updateTitle(); |
| 1241 | this->inval(NULL); |
| 1242 | break; |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 1243 | case 'z': |
| 1244 | this->toggleZoomer(); |
| 1245 | break; |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 1246 | default: |
| 1247 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1248 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1249 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1250 | return this->INHERITED::onHandleChar(uni); |
| 1251 | } |
| 1252 | |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1253 | void SampleWindow::setDeviceType(DeviceType type) { |
| 1254 | if (type != fDeviceType && fDevManager->supportsDeviceType(fDeviceType)) |
| 1255 | fDeviceType = type; |
| 1256 | this->updateTitle(); |
| 1257 | this->inval(NULL); |
| 1258 | } |
| 1259 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1260 | void SampleWindow::toggleFPS() { |
| 1261 | fMeasureFPS = !fMeasureFPS; |
| 1262 | this->inval(NULL); |
| 1263 | this->updateTitle(); |
| 1264 | } |
| 1265 | |
| 1266 | void SampleWindow::toggleSlideshow() { |
| 1267 | fAnimating = !fAnimating; |
| 1268 | this->postAnimatingEvent(); |
| 1269 | this->updateTitle(); |
| 1270 | } |
| 1271 | |
| 1272 | void SampleWindow::toggleRendering() { |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 1273 | DeviceType origDevType = fDeviceType; |
| 1274 | do { |
| 1275 | fDeviceType = cycle_devicetype(fDeviceType); |
| 1276 | } while (origDevType != fDeviceType && |
| 1277 | !fDevManager->supportsDeviceType(fDeviceType)); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1278 | this->updateTitle(); |
| 1279 | this->inval(NULL); |
| 1280 | } |
| 1281 | |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1282 | void SampleWindow::togglePipe() { |
| 1283 | fUsePipe = !fUsePipe; |
| 1284 | this->updateTitle(); |
| 1285 | } |
| 1286 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1287 | #include "SkDumpCanvas.h" |
| 1288 | |
| 1289 | bool SampleWindow::onHandleKey(SkKey key) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1290 | { |
| 1291 | SkView* view = curr_view(this); |
| 1292 | if (view) { |
| 1293 | SkEvent evt(gKeyEvtName); |
| 1294 | evt.setFast32(key); |
| 1295 | if (view->doQuery(&evt)) { |
| 1296 | return true; |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1301 | switch (key) { |
| 1302 | case kRight_SkKey: |
| 1303 | if (this->nextSample()) { |
| 1304 | return true; |
| 1305 | } |
| 1306 | break; |
| 1307 | case kLeft_SkKey: |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1308 | toggleRendering(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1309 | return true; |
| 1310 | case kUp_SkKey: |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1311 | if (USE_ARROWS_FOR_ZOOM) { |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 1312 | this->changeZoomLevel(1.f); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1313 | } else { |
| 1314 | fNClip = !fNClip; |
| 1315 | this->inval(NULL); |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 1316 | this->updateTitle(); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1317 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1318 | return true; |
| 1319 | case kDown_SkKey: |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1320 | if (USE_ARROWS_FOR_ZOOM) { |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 1321 | this->changeZoomLevel(-1.f); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1322 | } else { |
| 1323 | this->setConfig(cycle_configs(this->getBitmap().config())); |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 1324 | this->updateTitle(); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1325 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1326 | return true; |
| 1327 | case kOK_SkKey: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1328 | if (false) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1329 | SkDebugfDumper dumper; |
| 1330 | SkDumpCanvas dc(&dumper); |
| 1331 | this->draw(&dc); |
| 1332 | } else { |
| 1333 | fRepeatDrawing = !fRepeatDrawing; |
| 1334 | if (fRepeatDrawing) { |
| 1335 | this->inval(NULL); |
| 1336 | } |
| 1337 | } |
| 1338 | return true; |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 1339 | case kBack_SkKey: |
| 1340 | this->loadView(NULL); |
| 1341 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1342 | default: |
| 1343 | break; |
| 1344 | } |
| 1345 | return this->INHERITED::onHandleKey(key); |
| 1346 | } |
| 1347 | |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1348 | /////////////////////////////////////////////////////////////////////////////// |
| 1349 | |
| 1350 | static const char gGestureClickType[] = "GestureClickType"; |
| 1351 | |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 1352 | bool SampleWindow::onDispatchClick(int x, int y, Click::State state, |
| 1353 | void* owner) { |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 1354 | if (Click::kMoved_State == state) { |
| 1355 | updatePointer(x, y); |
| 1356 | } |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1357 | int w = SkScalarRound(this->width()); |
| 1358 | int h = SkScalarRound(this->height()); |
| 1359 | |
| 1360 | // check for the resize-box |
| 1361 | if (w - x < 16 && h - y < 16) { |
| 1362 | return false; // let the OS handle the click |
| 1363 | } else { |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 1364 | return this->INHERITED::onDispatchClick(x, y, state, owner); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1368 | class GestureClick : public SkView::Click { |
| 1369 | public: |
| 1370 | GestureClick(SkView* target) : SkView::Click(target) { |
| 1371 | this->setType(gGestureClickType); |
| 1372 | } |
| 1373 | |
| 1374 | static bool IsGesture(Click* click) { |
| 1375 | return click->isType(gGestureClickType); |
| 1376 | } |
| 1377 | }; |
| 1378 | |
| 1379 | SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y) { |
| 1380 | return new GestureClick(this); |
| 1381 | } |
| 1382 | |
| 1383 | bool SampleWindow::onClick(Click* click) { |
| 1384 | if (GestureClick::IsGesture(click)) { |
| 1385 | float x = SkScalarToFloat(click->fCurr.fX); |
| 1386 | float y = SkScalarToFloat(click->fCurr.fY); |
| 1387 | switch (click->fState) { |
| 1388 | case SkView::Click::kDown_State: |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 1389 | fGesture.touchBegin(click->fOwner, x, y); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1390 | break; |
| 1391 | case SkView::Click::kMoved_State: |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 1392 | fGesture.touchMoved(click->fOwner, x, y); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1393 | this->inval(NULL); |
| 1394 | break; |
| 1395 | case SkView::Click::kUp_State: |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 1396 | fGesture.touchEnd(click->fOwner); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1397 | this->inval(NULL); |
| 1398 | break; |
| 1399 | } |
| 1400 | return true; |
| 1401 | } |
| 1402 | return false; |
| 1403 | } |
| 1404 | |
| 1405 | /////////////////////////////////////////////////////////////////////////////// |
| 1406 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1407 | void SampleWindow::loadView(SkView* view) { |
| 1408 | SkView::F2BIter iter(this); |
| 1409 | SkView* prev = iter.next(); |
| 1410 | if (prev) { |
| 1411 | prev->detachFromParent(); |
| 1412 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1413 | |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 1414 | if (NULL == view) { |
| 1415 | view = create_overview(fSamples.count(), fSamples.begin()); |
| 1416 | } |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame^] | 1417 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1418 | view->setVisibleP(true); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1419 | view->setClipToBounds(false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1420 | this->attachChildToFront(view)->unref(); |
| 1421 | view->setSize(this->width(), this->height()); |
| 1422 | |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1423 | //repopulate the slide menu when a view is loaded |
| 1424 | fSlideMenu.reset(); |
| 1425 | if (SampleView::IsSampleView(view)) |
| 1426 | ((SampleView*)view)->requestMenus(&fSlideMenu); |
| 1427 | this->onUpdateMenu(&fSlideMenu); |
| 1428 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1429 | this->updateTitle(); |
| 1430 | } |
| 1431 | |
| 1432 | static const char* gConfigNames[] = { |
| 1433 | "unknown config", |
| 1434 | "A1", |
| 1435 | "A8", |
| 1436 | "Index8", |
| 1437 | "565", |
| 1438 | "4444", |
| 1439 | "8888" |
| 1440 | }; |
| 1441 | |
| 1442 | static const char* configToString(SkBitmap::Config c) { |
| 1443 | return gConfigNames[c]; |
| 1444 | } |
| 1445 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 1446 | static const char* gDeviceTypePrefix[] = { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1447 | "raster: ", |
| 1448 | "picture: ", |
| 1449 | "opengl: " |
| 1450 | }; |
| 1451 | |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 1452 | static const char* trystate_str(SkTriState state, |
| 1453 | const char trueStr[], const char falseStr[]) { |
| 1454 | if (kTrue_SkTriState == state) { |
| 1455 | return trueStr; |
| 1456 | } else if (kFalse_SkTriState == state) { |
| 1457 | return falseStr; |
| 1458 | } |
| 1459 | return NULL; |
| 1460 | } |
| 1461 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1462 | void SampleWindow::updateTitle() { |
| 1463 | SkString title; |
| 1464 | |
| 1465 | SkView::F2BIter iter(this); |
| 1466 | SkView* view = iter.next(); |
| 1467 | SkEvent evt(gTitleEvtName); |
| 1468 | if (view->doQuery(&evt)) { |
| 1469 | title.set(evt.findString(gTitleEvtName)); |
| 1470 | } |
| 1471 | if (title.size() == 0) { |
| 1472 | title.set("<unknown>"); |
| 1473 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1474 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 1475 | title.prepend(gDeviceTypePrefix[fDeviceType]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1476 | |
| 1477 | title.prepend(" "); |
| 1478 | title.prepend(configToString(this->getBitmap().config())); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1479 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1480 | if (fAnimating) { |
| 1481 | title.prepend("<A> "); |
| 1482 | } |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 1483 | if (fScale) { |
| 1484 | title.prepend("<S> "); |
| 1485 | } |
| 1486 | if (fRotate) { |
| 1487 | title.prepend("<R> "); |
| 1488 | } |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 1489 | if (fNClip) { |
| 1490 | title.prepend("<C> "); |
| 1491 | } |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 1492 | |
| 1493 | title.prepend(trystate_str(fLCDState, "LCD ", "lcd ")); |
| 1494 | title.prepend(trystate_str(fAAState, "AA ", "aa ")); |
reed@google.com | 09e3baa | 2011-05-18 12:04:31 +0000 | [diff] [blame] | 1495 | title.prepend(trystate_str(fFilterState, "H ", "h ")); |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 1496 | title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL); |
| 1497 | title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1498 | |
| 1499 | if (fZoomLevel) { |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 1500 | title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel)); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1501 | } |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1502 | |
| 1503 | if (fMeasureFPS) { |
reed@google.com | bad8c87 | 2011-05-18 20:10:31 +0000 | [diff] [blame] | 1504 | title.appendf(" %6.1f ms", fMeasureFPS_Time / (float)FPS_REPEAT_MULTIPLIER); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1505 | } |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 1506 | if (fUsePipe && SampleView::IsSampleView(view)) { |
| 1507 | title.prepend("<P> "); |
| 1508 | } |
| 1509 | if (SampleView::IsSampleView(view)) { |
| 1510 | title.prepend("! "); |
| 1511 | } |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1512 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1513 | this->setTitle(title.c_str()); |
| 1514 | } |
| 1515 | |
| 1516 | void SampleWindow::onSizeChange() { |
| 1517 | this->INHERITED::onSizeChange(); |
reed@google.com | 29038ed | 2011-07-06 17:56:47 +0000 | [diff] [blame] | 1518 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1519 | SkView::F2BIter iter(this); |
| 1520 | SkView* view = iter.next(); |
| 1521 | view->setSize(this->width(), this->height()); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1522 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1523 | // rebuild our clippath |
| 1524 | { |
| 1525 | const SkScalar W = this->width(); |
| 1526 | const SkScalar H = this->height(); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1527 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1528 | fClipPath.reset(); |
| 1529 | #if 0 |
| 1530 | for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) { |
| 1531 | SkRect r; |
| 1532 | r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30)); |
| 1533 | for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0)) |
| 1534 | fClipPath.addRect(r); |
| 1535 | } |
| 1536 | #else |
| 1537 | SkRect r; |
| 1538 | r.set(0, 0, W, H); |
| 1539 | fClipPath.addRect(r, SkPath::kCCW_Direction); |
| 1540 | r.set(W/4, H/4, W*3/4, H*3/4); |
| 1541 | fClipPath.addRect(r, SkPath::kCW_Direction); |
| 1542 | #endif |
| 1543 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1544 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1545 | fZoomCenterX = SkScalarHalf(this->width()); |
| 1546 | fZoomCenterY = SkScalarHalf(this->height()); |
| 1547 | |
Scroggo | 3e7ff9f | 2011-06-16 15:31:26 +0000 | [diff] [blame] | 1548 | #ifdef ANDROID |
Scroggo | b4490c7 | 2011-06-17 13:53:05 +0000 | [diff] [blame] | 1549 | // FIXME: The first draw after a size change does not work on Android, so |
| 1550 | // we post an invalidate. |
Scroggo | 62b65b0 | 2011-06-21 16:01:26 +0000 | [diff] [blame] | 1551 | this->postInvalDelay(); |
Scroggo | 716a038 | 2011-06-16 14:00:15 +0000 | [diff] [blame] | 1552 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1553 | this->updateTitle(); // to refresh our config |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 1554 | fDevManager->windowSizeChanged(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | /////////////////////////////////////////////////////////////////////////////// |
| 1558 | |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 1559 | static const char is_sample_view_tag[] = "sample-is-sample-view"; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1560 | static const char repeat_count_tag[] = "sample-set-repeat-count"; |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1561 | static const char set_use_pipe_tag[] = "sample-set-use-pipe"; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1562 | |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 1563 | bool SampleView::IsSampleView(SkView* view) { |
| 1564 | SkEvent evt(is_sample_view_tag); |
| 1565 | return view->doQuery(&evt); |
| 1566 | } |
| 1567 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 1568 | bool SampleView::SetRepeatDraw(SkView* view, int count) { |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1569 | SkEvent evt(repeat_count_tag); |
| 1570 | evt.setFast32(count); |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 1571 | return view->doEvent(evt); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1574 | bool SampleView::SetUsePipe(SkView* view, bool pred) { |
| 1575 | SkEvent evt(set_use_pipe_tag); |
| 1576 | evt.setFast32(pred); |
| 1577 | return view->doEvent(evt); |
| 1578 | } |
| 1579 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1580 | bool SampleView::onEvent(const SkEvent& evt) { |
| 1581 | if (evt.isType(repeat_count_tag)) { |
| 1582 | fRepeatCount = evt.getFast32(); |
| 1583 | return true; |
| 1584 | } |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1585 | if (evt.isType(set_use_pipe_tag)) { |
| 1586 | fUsePipe = !!evt.getFast32(); |
| 1587 | return true; |
| 1588 | } |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1589 | return this->INHERITED::onEvent(evt); |
| 1590 | } |
| 1591 | |
| 1592 | bool SampleView::onQuery(SkEvent* evt) { |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 1593 | if (evt->isType(is_sample_view_tag)) { |
| 1594 | return true; |
| 1595 | } |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1596 | return this->INHERITED::onQuery(evt); |
| 1597 | } |
| 1598 | |
reed@google.com | 68f456d | 2011-05-02 18:55:39 +0000 | [diff] [blame] | 1599 | #ifdef TEST_GPIPE |
| 1600 | #include "SkGPipe.h" |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1601 | |
| 1602 | class SimplePC : public SkGPipeController { |
| 1603 | public: |
| 1604 | SimplePC(SkCanvas* target); |
| 1605 | ~SimplePC(); |
| 1606 | |
| 1607 | virtual void* requestBlock(size_t minRequest, size_t* actual); |
| 1608 | virtual void notifyWritten(size_t bytes); |
| 1609 | |
| 1610 | private: |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 1611 | SkGPipeReader fReader; |
| 1612 | void* fBlock; |
| 1613 | size_t fBlockSize; |
| 1614 | size_t fBytesWritten; |
| 1615 | int fAtomsWritten; |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1616 | SkGPipeReader::Status fStatus; |
| 1617 | |
| 1618 | size_t fTotalWritten; |
| 1619 | }; |
| 1620 | |
| 1621 | SimplePC::SimplePC(SkCanvas* target) : fReader(target) { |
| 1622 | fBlock = NULL; |
| 1623 | fBlockSize = fBytesWritten = 0; |
| 1624 | fStatus = SkGPipeReader::kDone_Status; |
| 1625 | fTotalWritten = 0; |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 1626 | fAtomsWritten = 0; |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | SimplePC::~SimplePC() { |
| 1630 | // SkASSERT(SkGPipeReader::kDone_Status == fStatus); |
| 1631 | sk_free(fBlock); |
| 1632 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1633 | if (fTotalWritten) { |
| 1634 | SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten, |
| 1635 | fAtomsWritten, fStatus); |
| 1636 | } |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | void* SimplePC::requestBlock(size_t minRequest, size_t* actual) { |
| 1640 | sk_free(fBlock); |
| 1641 | |
| 1642 | fBlockSize = minRequest * 4; |
| 1643 | fBlock = sk_malloc_throw(fBlockSize); |
| 1644 | fBytesWritten = 0; |
| 1645 | *actual = fBlockSize; |
| 1646 | return fBlock; |
| 1647 | } |
| 1648 | |
| 1649 | void SimplePC::notifyWritten(size_t bytes) { |
| 1650 | SkASSERT(fBytesWritten + bytes <= fBlockSize); |
yangsu@google.com | 1f39421 | 2011-06-01 18:03:34 +0000 | [diff] [blame] | 1651 | |
| 1652 | #ifdef PIPE_FILE |
| 1653 | //File is open in append mode |
| 1654 | FILE* f = fopen(FILE_PATH, "ab"); |
| 1655 | SkASSERT(f != NULL); |
| 1656 | fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f); |
| 1657 | fclose(f); |
| 1658 | #endif |
| 1659 | |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1660 | fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes); |
| 1661 | SkASSERT(SkGPipeReader::kError_Status != fStatus); |
| 1662 | fBytesWritten += bytes; |
| 1663 | fTotalWritten += bytes; |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 1664 | |
| 1665 | fAtomsWritten += 1; |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
reed@google.com | 68f456d | 2011-05-02 18:55:39 +0000 | [diff] [blame] | 1668 | #endif |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 1669 | |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1670 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1671 | void SampleView::onDraw(SkCanvas* canvas) { |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 1672 | #ifdef TEST_GPIPE |
reed@google.com | 64e3eb2 | 2011-05-04 14:32:04 +0000 | [diff] [blame] | 1673 | SimplePC controller(canvas); |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 1674 | SkGPipeWriter writer; |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1675 | if (fUsePipe) { |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 1676 | uint32_t flags = SkGPipeWriter::kCrossProcess_Flag; |
| 1677 | // flags = 0; |
| 1678 | canvas = writer.startRecording(&controller, flags); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 1679 | } |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 1680 | #endif |
| 1681 | |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 1682 | this->onDrawBackground(canvas); |
| 1683 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1684 | for (int i = 0; i < fRepeatCount; i++) { |
| 1685 | SkAutoCanvasRestore acr(canvas, true); |
| 1686 | this->onDrawContent(canvas); |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | void SampleView::onDrawBackground(SkCanvas* canvas) { |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 1691 | canvas->drawColor(fBGColor); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | /////////////////////////////////////////////////////////////////////////////// |
| 1695 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1696 | template <typename T> void SkTBSort(T array[], int count) { |
| 1697 | for (int i = 1; i < count - 1; i++) { |
| 1698 | bool didSwap = false; |
| 1699 | for (int j = count - 1; j > i; --j) { |
| 1700 | if (array[j] < array[j-1]) { |
| 1701 | T tmp(array[j-1]); |
| 1702 | array[j-1] = array[j]; |
| 1703 | array[j] = tmp; |
| 1704 | didSwap = true; |
| 1705 | } |
| 1706 | } |
| 1707 | if (!didSwap) { |
| 1708 | break; |
| 1709 | } |
| 1710 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1711 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1712 | for (int k = 0; k < count - 1; k++) { |
| 1713 | SkASSERT(!(array[k+1] < array[k])); |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | #include "SkRandom.h" |
| 1718 | |
| 1719 | static void rand_rect(SkIRect* rect, SkRandom& rand) { |
| 1720 | int bits = 8; |
| 1721 | int shift = 32 - bits; |
| 1722 | rect->set(rand.nextU() >> shift, rand.nextU() >> shift, |
| 1723 | rand.nextU() >> shift, rand.nextU() >> shift); |
| 1724 | rect->sort(); |
| 1725 | } |
| 1726 | |
| 1727 | static void dumpRect(const SkIRect& r) { |
| 1728 | SkDebugf(" { %d, %d, %d, %d },\n", |
| 1729 | r.fLeft, r.fTop, |
| 1730 | r.fRight, r.fBottom); |
| 1731 | } |
| 1732 | |
| 1733 | static void test_rects(const SkIRect rect[], int count) { |
| 1734 | SkRegion rgn0, rgn1; |
| 1735 | |
| 1736 | for (int i = 0; i < count; i++) { |
| 1737 | rgn0.op(rect[i], SkRegion::kUnion_Op); |
| 1738 | // dumpRect(rect[i]); |
| 1739 | } |
| 1740 | rgn1.setRects(rect, count); |
| 1741 | |
| 1742 | if (rgn0 != rgn1) { |
| 1743 | SkDebugf("\n"); |
| 1744 | for (int i = 0; i < count; i++) { |
| 1745 | dumpRect(rect[i]); |
| 1746 | } |
| 1747 | SkDebugf("\n"); |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | static void test() { |
| 1752 | size_t i; |
| 1753 | |
| 1754 | const SkIRect r0[] = { |
| 1755 | { 0, 0, 1, 1 }, |
| 1756 | { 2, 2, 3, 3 }, |
| 1757 | }; |
| 1758 | const SkIRect r1[] = { |
| 1759 | { 0, 0, 1, 3 }, |
| 1760 | { 1, 1, 2, 2 }, |
| 1761 | { 2, 0, 3, 3 }, |
| 1762 | }; |
| 1763 | const SkIRect r2[] = { |
| 1764 | { 0, 0, 1, 2 }, |
| 1765 | { 2, 1, 3, 3 }, |
| 1766 | { 4, 0, 5, 1 }, |
| 1767 | { 6, 0, 7, 4 }, |
| 1768 | }; |
| 1769 | |
| 1770 | static const struct { |
| 1771 | const SkIRect* fRects; |
| 1772 | int fCount; |
| 1773 | } gRecs[] = { |
| 1774 | { r0, SK_ARRAY_COUNT(r0) }, |
| 1775 | { r1, SK_ARRAY_COUNT(r1) }, |
| 1776 | { r2, SK_ARRAY_COUNT(r2) }, |
| 1777 | }; |
| 1778 | |
| 1779 | for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) { |
| 1780 | test_rects(gRecs[i].fRects, gRecs[i].fCount); |
| 1781 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1782 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1783 | SkRandom rand; |
| 1784 | for (i = 0; i < 10000; i++) { |
| 1785 | SkRegion rgn0, rgn1; |
| 1786 | |
| 1787 | const int N = 8; |
| 1788 | SkIRect rect[N]; |
| 1789 | for (int j = 0; j < N; j++) { |
| 1790 | rand_rect(&rect[j], rand); |
| 1791 | } |
| 1792 | test_rects(rect, N); |
| 1793 | } |
| 1794 | } |
| 1795 | |
senorblanco@chromium.org | 78b8253 | 2011-06-28 19:44:03 +0000 | [diff] [blame] | 1796 | SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1797 | // test(); |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 1798 | return new SampleWindow(hwnd, argc, argv, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | void get_preferred_size(int* x, int* y, int* width, int* height) { |
| 1802 | *x = 10; |
| 1803 | *y = 50; |
| 1804 | *width = 640; |
| 1805 | *height = 480; |
| 1806 | } |
| 1807 | |
| 1808 | void application_init() { |
| 1809 | // setenv("ANDROID_ROOT", "../../../data", 0); |
reed@android.com | e191b16 | 2009-12-18 21:33:39 +0000 | [diff] [blame] | 1810 | #ifdef SK_BUILD_FOR_MAC |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1811 | setenv("ANDROID_ROOT", "/android/device/data", 0); |
reed@android.com | e191b16 | 2009-12-18 21:33:39 +0000 | [diff] [blame] | 1812 | #endif |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 1813 | SkGraphics::Init(); |
| 1814 | SkEvent::Init(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | void application_term() { |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 1818 | SkEvent::Term(); |
| 1819 | SkGraphics::Term(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1820 | } |