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