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