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