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