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