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