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