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 | */ |
reed | 868074b | 2014-06-03 10:53:59 -0700 | [diff] [blame] | 7 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 8 | #include "SampleApp.h" |
| 9 | |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 10 | #include "OverView.h" |
caryclark | f568f6a | 2014-07-18 10:58:56 -0700 | [diff] [blame] | 11 | #include "Resources.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 12 | #include "SampleCode.h" |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 13 | #include "SkAnimTimer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkCanvas.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 15 | #include "SkCommandLineFlags.h" |
| 16 | #include "SkData.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "SkDevice.h" |
reed | ddb5eca | 2014-10-08 11:10:51 -0700 | [diff] [blame] | 18 | #include "SkDocument.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkGraphics.h" |
reed@android.com | b08eb2b | 2009-01-06 20:16:26 +0000 | [diff] [blame] | 20 | #include "SkImageEncoder.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 21 | #include "SkOSFile.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkPaint.h" |
fmalita | 822ace9 | 2015-06-15 07:07:32 -0700 | [diff] [blame] | 23 | #include "SkPaintFilterCanvas.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 25 | #include "SkPictureRecorder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | #include "SkStream.h" |
commit-bot@chromium.org | 9a558d4 | 2014-05-30 15:06:24 +0000 | [diff] [blame] | 27 | #include "SkSurface.h" |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 28 | #include "SkTemplates.h" |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 29 | #include "SkTSort.h" |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 30 | #include "SkTime.h" |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 31 | #include "SkTypeface.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 32 | #include "SkWindow.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 33 | #include "sk_tool_utils.h" |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 34 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 35 | #if SK_SUPPORT_GPU |
tomhudson@google.com | 6bf38b5 | 2012-02-14 15:11:59 +0000 | [diff] [blame] | 36 | #include "gl/GrGLInterface.h" |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 37 | #include "gl/GrGLUtil.h" |
bsalomon@google.com | 583a1e3 | 2011-08-17 13:42:46 +0000 | [diff] [blame] | 38 | #include "GrRenderTarget.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 39 | #include "GrContext.h" |
| 40 | #include "SkGpuDevice.h" |
| 41 | #else |
| 42 | class GrContext; |
| 43 | #endif |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 44 | |
reed | 5f629b4 | 2015-08-15 11:57:23 -0700 | [diff] [blame] | 45 | // Should be 3x + 1 |
| 46 | #define kMaxFatBitsScale 28 |
| 47 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 48 | extern SampleView* CreateSamplePictFileView(const char filename[]); |
| 49 | |
| 50 | class PictFileFactory : public SkViewFactory { |
| 51 | SkString fFilename; |
| 52 | public: |
| 53 | PictFileFactory(const SkString& filename) : fFilename(filename) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | SkView* operator() () const override { |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 55 | return CreateSamplePictFileView(fFilename.c_str()); |
| 56 | } |
| 57 | }; |
| 58 | |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 59 | #ifdef SAMPLE_PDF_FILE_VIEWER |
| 60 | extern SampleView* CreateSamplePdfFileViewer(const char filename[]); |
| 61 | |
| 62 | class PdfFileViewerFactory : public SkViewFactory { |
| 63 | SkString fFilename; |
| 64 | public: |
| 65 | PdfFileViewerFactory(const SkString& filename) : fFilename(filename) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 66 | SkView* operator() () const override { |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 67 | return CreateSamplePdfFileViewer(fFilename.c_str()); |
| 68 | } |
| 69 | }; |
| 70 | #endif // SAMPLE_PDF_FILE_VIEWER |
| 71 | |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 72 | #if SK_COMMAND_BUFFER |
| 73 | #define DEFAULT_TO_COMMAND_BUFFER 1 |
| 74 | #elif SK_ANGLE |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 75 | //#define DEFAULT_TO_ANGLE 1 |
| 76 | #else |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 77 | #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] | 78 | #endif |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 79 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | #define ANIMATING_EVENTTYPE "nextSample" |
reed@google.com | 84cfce1 | 2013-05-29 19:22:20 +0000 | [diff] [blame] | 81 | #define ANIMATING_DELAY 250 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 83 | #ifdef SK_DEBUG |
reed@google.com | bad8c87 | 2011-05-18 20:10:31 +0000 | [diff] [blame] | 84 | #define FPS_REPEAT_MULTIPLIER 1 |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 85 | #else |
reed@google.com | bad8c87 | 2011-05-18 20:10:31 +0000 | [diff] [blame] | 86 | #define FPS_REPEAT_MULTIPLIER 10 |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 87 | #endif |
reed@google.com | bad8c87 | 2011-05-18 20:10:31 +0000 | [diff] [blame] | 88 | #define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER) |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 89 | |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 90 | static SampleWindow* gSampleWindow; |
| 91 | |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 92 | static bool gShowGMBounds; |
| 93 | |
commit-bot@chromium.org | 6de4a78 | 2013-12-11 17:05:23 +0000 | [diff] [blame] | 94 | static void post_event_to_sink(SkEvent* evt, SkEventSink* sink) { |
reed@google.com | 2072db8 | 2011-11-08 15:18:10 +0000 | [diff] [blame] | 95 | evt->setTargetID(sink->getSinkID())->post(); |
| 96 | } |
| 97 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 98 | static SkAnimTimer gAnimTimer; |
| 99 | |
mike@reedtribe.org | 6f6e8c3 | 2011-12-27 22:33:50 +0000 | [diff] [blame] | 100 | /////////////////////////////////////////////////////////////////////////////// |
| 101 | |
| 102 | static const char* skip_until(const char* str, const char* skip) { |
| 103 | if (!str) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 104 | return nullptr; |
mike@reedtribe.org | 6f6e8c3 | 2011-12-27 22:33:50 +0000 | [diff] [blame] | 105 | } |
| 106 | return strstr(str, skip); |
| 107 | } |
| 108 | |
| 109 | static const char* skip_past(const char* str, const char* skip) { |
| 110 | const char* found = skip_until(str, skip); |
| 111 | if (!found) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 112 | return nullptr; |
mike@reedtribe.org | 6f6e8c3 | 2011-12-27 22:33:50 +0000 | [diff] [blame] | 113 | } |
| 114 | return found + strlen(skip); |
| 115 | } |
| 116 | |
| 117 | static const char* gPrefFileName = "sampleapp_prefs.txt"; |
| 118 | |
epoger@google.com | 6fc7cc2 | 2011-12-28 15:13:41 +0000 | [diff] [blame] | 119 | static bool readTitleFromPrefs(SkString* title) { |
mike@reedtribe.org | 6f6e8c3 | 2011-12-27 22:33:50 +0000 | [diff] [blame] | 120 | SkFILEStream stream(gPrefFileName); |
| 121 | if (!stream.isValid()) { |
| 122 | return false; |
| 123 | } |
| 124 | |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 125 | size_t len = stream.getLength(); |
mike@reedtribe.org | 6f6e8c3 | 2011-12-27 22:33:50 +0000 | [diff] [blame] | 126 | SkString data(len); |
| 127 | stream.read(data.writable_str(), len); |
| 128 | const char* s = data.c_str(); |
| 129 | |
| 130 | s = skip_past(s, "curr-slide-title"); |
| 131 | s = skip_past(s, "="); |
| 132 | s = skip_past(s, "\""); |
| 133 | const char* stop = skip_until(s, "\""); |
| 134 | if (stop > s) { |
| 135 | title->set(s, stop - s); |
| 136 | return true; |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | |
epoger@google.com | 6fc7cc2 | 2011-12-28 15:13:41 +0000 | [diff] [blame] | 141 | static void writeTitleToPrefs(const char* title) { |
mike@reedtribe.org | 6f6e8c3 | 2011-12-27 22:33:50 +0000 | [diff] [blame] | 142 | SkFILEWStream stream(gPrefFileName); |
| 143 | SkString data; |
| 144 | data.printf("curr-slide-title = \"%s\"\n", title); |
| 145 | stream.write(data.c_str(), data.size()); |
| 146 | } |
| 147 | |
| 148 | /////////////////////////////////////////////////////////////////////////////// |
| 149 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 150 | class SampleWindow::DefaultDeviceManager : public SampleWindow::DeviceManager { |
| 151 | public: |
| 152 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 153 | DefaultDeviceManager() { |
| 154 | #if SK_SUPPORT_GPU |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 155 | fCurContext = nullptr; |
| 156 | fCurIntf = nullptr; |
| 157 | fCurRenderTarget = nullptr; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 158 | fMSAASampleCount = 0; |
| 159 | #endif |
| 160 | fBackend = kNone_BackEndType; |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | virtual ~DefaultDeviceManager() { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 164 | #if SK_SUPPORT_GPU |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 165 | SkSafeUnref(fCurContext); |
| 166 | SkSafeUnref(fCurIntf); |
| 167 | SkSafeUnref(fCurRenderTarget); |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 168 | #endif |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 169 | } |
| 170 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 171 | void setUpBackend(SampleWindow* win, int msaaSampleCount) override { |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 172 | SkASSERT(kNone_BackEndType == fBackend); |
robertphillips@google.com | 53e96a1 | 2012-03-30 14:47:53 +0000 | [diff] [blame] | 173 | |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 174 | fBackend = kNone_BackEndType; |
| 175 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 176 | #if SK_SUPPORT_GPU |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 177 | switch (win->getDeviceType()) { |
reed | beedb81 | 2015-05-05 12:14:45 -0700 | [diff] [blame] | 178 | case kRaster_DeviceType: // fallthrough |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 179 | case kGPU_DeviceType: |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 180 | // all these guys use the native backend |
| 181 | fBackend = kNativeGL_BackEndType; |
| 182 | break; |
robertphillips@google.com | bd8d7ad | 2012-03-30 15:18:14 +0000 | [diff] [blame] | 183 | #if SK_ANGLE |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 184 | case kANGLE_DeviceType: |
| 185 | // ANGLE is really the only odd man out |
| 186 | fBackend = kANGLE_BackEndType; |
| 187 | break; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 188 | #endif // SK_ANGLE |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 189 | #if SK_COMMAND_BUFFER |
| 190 | case kCommandBuffer_DeviceType: |
| 191 | // Command buffer is really the only other odd man out :D |
| 192 | fBackend = kCommandBuffer_BackEndType; |
| 193 | break; |
| 194 | #endif // SK_COMMAND_BUFFER |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 195 | default: |
| 196 | SkASSERT(false); |
| 197 | break; |
robertphillips@google.com | bd8d7ad | 2012-03-30 15:18:14 +0000 | [diff] [blame] | 198 | } |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 199 | AttachmentInfo attachmentInfo; |
| 200 | bool result = win->attach(fBackend, msaaSampleCount, &attachmentInfo); |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 201 | if (!result) { |
bsalomon@google.com | 7491372 | 2011-10-27 20:44:19 +0000 | [diff] [blame] | 202 | SkDebugf("Failed to initialize GL"); |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 203 | return; |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 204 | } |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 205 | fMSAASampleCount = msaaSampleCount; |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 206 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 207 | SkASSERT(nullptr == fCurIntf); |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 208 | SkAutoTUnref<const GrGLInterface> glInterface; |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 209 | switch (win->getDeviceType()) { |
reed | beedb81 | 2015-05-05 12:14:45 -0700 | [diff] [blame] | 210 | case kRaster_DeviceType: // fallthrough |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 211 | case kGPU_DeviceType: |
| 212 | // all these guys use the native interface |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 213 | glInterface.reset(GrGLCreateNativeInterface()); |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 214 | break; |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 215 | #if SK_ANGLE |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 216 | case kANGLE_DeviceType: |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 217 | glInterface.reset(GrGLCreateANGLEInterface()); |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 218 | break; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 219 | #endif // SK_ANGLE |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 220 | #if SK_COMMAND_BUFFER |
| 221 | case kCommandBuffer_DeviceType: |
| 222 | glInterface.reset(GrGLCreateCommandBufferInterface()); |
| 223 | break; |
| 224 | #endif // SK_COMMAND_BUFFER |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 225 | default: |
| 226 | SkASSERT(false); |
| 227 | break; |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 228 | } |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 229 | |
commit-bot@chromium.org | d8ed851 | 2014-01-24 20:49:44 +0000 | [diff] [blame] | 230 | // Currently SampleApp does not use NVPR. TODO: Provide an NVPR device type that is skipped |
| 231 | // when the driver doesn't support NVPR. |
| 232 | fCurIntf = GrGLInterfaceRemoveNVPR(glInterface.get()); |
| 233 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 234 | SkASSERT(nullptr == fCurContext); |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 235 | fCurContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf); |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 236 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 237 | if (nullptr == fCurContext || nullptr == fCurIntf) { |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 238 | // We need some context and interface to see results |
| 239 | SkSafeUnref(fCurContext); |
| 240 | SkSafeUnref(fCurIntf); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 241 | fCurContext = nullptr; |
| 242 | fCurIntf = nullptr; |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 243 | SkDebugf("Failed to setup 3D"); |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 244 | |
| 245 | win->detach(); |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 246 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 247 | #endif // SK_SUPPORT_GPU |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 248 | // call windowSizeChanged to create the render target |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 249 | this->windowSizeChanged(win); |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 250 | } |
| 251 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 252 | void tearDownBackend(SampleWindow *win) override { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 253 | #if SK_SUPPORT_GPU |
reed | 789e327 | 2014-10-30 11:29:08 -0700 | [diff] [blame] | 254 | if (fCurContext) { |
| 255 | // in case we have outstanding refs to this guy (lua?) |
| 256 | fCurContext->abandonContext(); |
| 257 | fCurContext->unref(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 258 | fCurContext = nullptr; |
reed | 789e327 | 2014-10-30 11:29:08 -0700 | [diff] [blame] | 259 | } |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 260 | |
| 261 | SkSafeUnref(fCurIntf); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 262 | fCurIntf = nullptr; |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 263 | |
| 264 | SkSafeUnref(fCurRenderTarget); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 265 | fCurRenderTarget = nullptr; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 266 | #endif |
bsalomon@google.com | ddd40e5 | 2012-04-10 15:56:29 +0000 | [diff] [blame] | 267 | win->detach(); |
| 268 | fBackend = kNone_BackEndType; |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 269 | } |
| 270 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 271 | SkSurface* createSurface(SampleWindow::DeviceType dType, SampleWindow* win) override { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 272 | #if SK_SUPPORT_GPU |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 273 | if (IsGpuDeviceType(dType) && fCurContext) { |
reed | 4302ae9 | 2014-10-06 12:29:56 -0700 | [diff] [blame] | 274 | SkSurfaceProps props(win->getSurfaceProps()); |
| 275 | return SkSurface::NewRenderTargetDirect(fCurRenderTarget, &props); |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 276 | } |
reed | 0397e9f | 2014-09-18 11:29:01 -0700 | [diff] [blame] | 277 | #endif |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 278 | return nullptr; |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 279 | } |
| 280 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 281 | void publishCanvas(SampleWindow::DeviceType dType, |
| 282 | SkCanvas* canvas, SampleWindow* win) override { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 283 | #if SK_SUPPORT_GPU |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 284 | if (fCurContext) { |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 285 | // in case we have queued drawing calls |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 286 | fCurContext->flush(); |
| 287 | |
bsalomon@google.com | 82502e2 | 2013-01-24 20:47:18 +0000 | [diff] [blame] | 288 | if (!IsGpuDeviceType(dType)) { |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 289 | // need to send the raster bits to the (gpu) window |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 290 | const SkBitmap& bm = win->getBitmap(); |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 291 | fCurRenderTarget->writePixels(0, 0, bm.width(), bm.height(), |
commit-bot@chromium.org | fb6fefe | 2014-04-29 16:25:36 +0000 | [diff] [blame] | 292 | SkImageInfo2GrPixelConfig(bm.colorType(), |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 293 | bm.alphaType(), |
| 294 | bm.profileType()), |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 295 | bm.getPixels(), |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 296 | bm.rowBytes(), |
| 297 | GrContext::kFlushWrites_PixelOp); |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 300 | #endif |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 301 | |
| 302 | win->present(); |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 303 | } |
| 304 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 305 | void windowSizeChanged(SampleWindow* win) override { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 306 | #if SK_SUPPORT_GPU |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 307 | if (fCurContext) { |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 308 | AttachmentInfo attachmentInfo; |
| 309 | win->attach(fBackend, fMSAASampleCount, &attachmentInfo); |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 310 | SkSafeUnref(fCurRenderTarget); |
caryclark | c8fcafb | 2015-01-30 12:37:02 -0800 | [diff] [blame] | 311 | fCurRenderTarget = win->renderTarget(attachmentInfo, fCurIntf, fCurContext); |
bsalomon@google.com | 7491372 | 2011-10-27 20:44:19 +0000 | [diff] [blame] | 312 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 313 | #endif |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 314 | } |
| 315 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 316 | GrContext* getGrContext() override { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 317 | #if SK_SUPPORT_GPU |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 318 | return fCurContext; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 319 | #else |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 320 | return nullptr; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 321 | #endif |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 322 | } |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 323 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 324 | GrRenderTarget* getGrRenderTarget() override { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 325 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 326 | return fCurRenderTarget; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 327 | #else |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 328 | return nullptr; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 329 | #endif |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 330 | } |
| 331 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 332 | private: |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 333 | |
| 334 | #if SK_SUPPORT_GPU |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 335 | GrContext* fCurContext; |
| 336 | const GrGLInterface* fCurIntf; |
| 337 | GrRenderTarget* fCurRenderTarget; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 338 | int fMSAASampleCount; |
| 339 | #endif |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 340 | |
| 341 | SkOSWindow::SkBackEndTypes fBackend; |
| 342 | |
| 343 | typedef SampleWindow::DeviceManager INHERITED; |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | /////////////// |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 347 | static const char view_inval_msg[] = "view-inval-msg"; |
| 348 | |
Scroggo | 62b65b0 | 2011-06-21 16:01:26 +0000 | [diff] [blame] | 349 | void SampleWindow::postInvalDelay() { |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 350 | (new SkEvent(view_inval_msg, this->getSinkID()))->postDelay(1); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static bool isInvalEvent(const SkEvent& evt) { |
| 354 | return evt.isType(view_inval_msg); |
| 355 | } |
| 356 | ////////////////// |
| 357 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 358 | SkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func) |
| 359 | : fCreateFunc(func) { |
| 360 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 361 | |
bsalomon@google.com | 8301de1 | 2011-10-31 16:47:13 +0000 | [diff] [blame] | 362 | SkView* SkFuncViewFactory::operator() () const { |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 363 | return (*fCreateFunc)(); |
| 364 | } |
| 365 | |
| 366 | #include "GMSampleView.h" |
| 367 | |
| 368 | SkGMSampleViewFactory::SkGMSampleViewFactory(GMFactoryFunc func) |
| 369 | : fFunc(func) { |
| 370 | } |
| 371 | |
| 372 | SkView* SkGMSampleViewFactory::operator() () const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 373 | skiagm::GM* gm = fFunc(nullptr); |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 374 | gm->setMode(skiagm::GM::kSample_Mode); |
| 375 | return new GMSampleView(gm); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | SkViewRegister* SkViewRegister::gHead; |
| 379 | SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) { |
| 380 | fFact->ref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 381 | fChain = gHead; |
| 382 | gHead = this; |
| 383 | } |
| 384 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 385 | SkViewRegister::SkViewRegister(SkViewCreateFunc func) { |
| 386 | fFact = new SkFuncViewFactory(func); |
| 387 | fChain = gHead; |
| 388 | gHead = this; |
| 389 | } |
| 390 | |
| 391 | SkViewRegister::SkViewRegister(GMFactoryFunc func) { |
| 392 | fFact = new SkGMSampleViewFactory(func); |
| 393 | fChain = gHead; |
| 394 | gHead = this; |
| 395 | } |
| 396 | |
| 397 | class AutoUnrefArray { |
| 398 | public: |
| 399 | AutoUnrefArray() {} |
| 400 | ~AutoUnrefArray() { |
| 401 | int count = fObjs.count(); |
| 402 | for (int i = 0; i < count; ++i) { |
| 403 | fObjs[i]->unref(); |
| 404 | } |
| 405 | } |
| 406 | SkRefCnt*& push_back() { return *fObjs.append(); } |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 407 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 408 | private: |
| 409 | SkTDArray<SkRefCnt*> fObjs; |
| 410 | }; |
| 411 | |
| 412 | // registers GMs as Samples |
| 413 | // This can't be performed during static initialization because it could be |
| 414 | // run before GMRegistry has been fully built. |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 415 | static void SkGMRegistyToSampleRegistry() { |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 416 | static bool gOnce; |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 417 | static AutoUnrefArray fRegisters; |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 418 | |
| 419 | if (!gOnce) { |
| 420 | const skiagm::GMRegistry* gmreg = skiagm::GMRegistry::Head(); |
| 421 | while (gmreg) { |
| 422 | fRegisters.push_back() = new SkViewRegister(gmreg->factory()); |
| 423 | gmreg = gmreg->next(); |
| 424 | } |
| 425 | gOnce = true; |
| 426 | } |
| 427 | } |
| 428 | |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 429 | ////////////////////////////////////////////////////////////////////////////// |
| 430 | |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 431 | enum FlipAxisEnum { |
| 432 | kFlipAxis_X = (1 << 0), |
| 433 | kFlipAxis_Y = (1 << 1) |
| 434 | }; |
| 435 | |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 436 | #include "SkDrawFilter.h" |
| 437 | |
bungeman@google.com | 96aabc8 | 2013-06-03 21:26:34 +0000 | [diff] [blame] | 438 | struct HintingState { |
| 439 | SkPaint::Hinting hinting; |
| 440 | const char* name; |
| 441 | const char* label; |
| 442 | }; |
| 443 | static HintingState gHintingStates[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 444 | {SkPaint::kNo_Hinting, "Mixed", nullptr }, |
bungeman@google.com | 96aabc8 | 2013-06-03 21:26:34 +0000 | [diff] [blame] | 445 | {SkPaint::kNo_Hinting, "None", "H0 " }, |
| 446 | {SkPaint::kSlight_Hinting, "Slight", "Hs " }, |
| 447 | {SkPaint::kNormal_Hinting, "Normal", "Hn " }, |
| 448 | {SkPaint::kFull_Hinting, "Full", "Hf " }, |
| 449 | }; |
| 450 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 451 | struct FilterQualityState { |
| 452 | SkFilterQuality fQuality; |
| 453 | const char* fName; |
| 454 | const char* fLabel; |
reed@google.com | 15bc13d | 2013-12-10 16:53:13 +0000 | [diff] [blame] | 455 | }; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 456 | static FilterQualityState gFilterQualityStates[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 457 | { kNone_SkFilterQuality, "Mixed", nullptr }, |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 458 | { kNone_SkFilterQuality, "None", "F0 " }, |
| 459 | { kLow_SkFilterQuality, "Low", "F1 " }, |
| 460 | { kMedium_SkFilterQuality, "Medium", "F2 " }, |
| 461 | { kHigh_SkFilterQuality, "High", "F3 " }, |
reed@google.com | 15bc13d | 2013-12-10 16:53:13 +0000 | [diff] [blame] | 462 | }; |
| 463 | |
fmalita | 822ace9 | 2015-06-15 07:07:32 -0700 | [diff] [blame] | 464 | class FlagsFilterCanvas : public SkPaintFilterCanvas { |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 465 | public: |
fmalita | 822ace9 | 2015-06-15 07:07:32 -0700 | [diff] [blame] | 466 | FlagsFilterCanvas(SkCanvas* canvas, SkOSMenu::TriState lcd, SkOSMenu::TriState aa, |
| 467 | SkOSMenu::TriState subpixel, int hinting, int filterQuality) |
fmalita | bad23dc | 2016-01-11 13:58:29 -0800 | [diff] [blame] | 468 | : INHERITED(canvas) |
fmalita | 822ace9 | 2015-06-15 07:07:32 -0700 | [diff] [blame] | 469 | , fLCDState(lcd) |
reed@google.com | 15bc13d | 2013-12-10 16:53:13 +0000 | [diff] [blame] | 470 | , fAAState(aa) |
| 471 | , fSubpixelState(subpixel) |
| 472 | , fHintingState(hinting) |
fmalita | 822ace9 | 2015-06-15 07:07:32 -0700 | [diff] [blame] | 473 | , fFilterQualityIndex(filterQuality) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 474 | SkASSERT((unsigned)filterQuality < SK_ARRAY_COUNT(gFilterQualityStates)); |
reed@google.com | 15bc13d | 2013-12-10 16:53:13 +0000 | [diff] [blame] | 475 | } |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 476 | |
fmalita | 822ace9 | 2015-06-15 07:07:32 -0700 | [diff] [blame] | 477 | protected: |
fmalita | 32cdc32 | 2016-01-12 07:21:11 -0800 | [diff] [blame] | 478 | bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type t) const override { |
| 479 | if (!*paint) { |
fmalita | bad23dc | 2016-01-11 13:58:29 -0800 | [diff] [blame] | 480 | return true; |
| 481 | } |
| 482 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 483 | if (kText_Type == t && SkOSMenu::kMixedState != fLCDState) { |
fmalita | 32cdc32 | 2016-01-12 07:21:11 -0800 | [diff] [blame] | 484 | paint->writable()->setLCDRenderText(SkOSMenu::kOnState == fLCDState); |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 485 | } |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 486 | if (SkOSMenu::kMixedState != fAAState) { |
fmalita | 32cdc32 | 2016-01-12 07:21:11 -0800 | [diff] [blame] | 487 | paint->writable()->setAntiAlias(SkOSMenu::kOnState == fAAState); |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 488 | } |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 489 | if (0 != fFilterQualityIndex) { |
fmalita | 32cdc32 | 2016-01-12 07:21:11 -0800 | [diff] [blame] | 490 | paint->writable()->setFilterQuality(gFilterQualityStates[fFilterQualityIndex].fQuality); |
reed@google.com | 176753a | 2011-05-17 15:32:04 +0000 | [diff] [blame] | 491 | } |
bungeman@google.com | 96aabc8 | 2013-06-03 21:26:34 +0000 | [diff] [blame] | 492 | if (SkOSMenu::kMixedState != fSubpixelState) { |
fmalita | 32cdc32 | 2016-01-12 07:21:11 -0800 | [diff] [blame] | 493 | paint->writable()->setSubpixelText(SkOSMenu::kOnState == fSubpixelState); |
bungeman@google.com | 96aabc8 | 2013-06-03 21:26:34 +0000 | [diff] [blame] | 494 | } |
bungeman@google.com | 055aa52 | 2013-06-03 21:35:03 +0000 | [diff] [blame] | 495 | if (0 != fHintingState && fHintingState < (int)SK_ARRAY_COUNT(gHintingStates)) { |
fmalita | 32cdc32 | 2016-01-12 07:21:11 -0800 | [diff] [blame] | 496 | paint->writable()->setHinting(gHintingStates[fHintingState].hinting); |
reed@google.com | 09e3baa | 2011-05-18 12:04:31 +0000 | [diff] [blame] | 497 | } |
fmalita | bad23dc | 2016-01-11 13:58:29 -0800 | [diff] [blame] | 498 | return true; |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | private: |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 502 | SkOSMenu::TriState fLCDState; |
| 503 | SkOSMenu::TriState fAAState; |
bungeman@google.com | 96aabc8 | 2013-06-03 21:26:34 +0000 | [diff] [blame] | 504 | SkOSMenu::TriState fSubpixelState; |
| 505 | int fHintingState; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 506 | int fFilterQualityIndex; |
fmalita | 822ace9 | 2015-06-15 07:07:32 -0700 | [diff] [blame] | 507 | |
| 508 | typedef SkPaintFilterCanvas INHERITED; |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 509 | }; |
| 510 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 511 | ////////////////////////////////////////////////////////////////////////////// |
| 512 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 513 | #define MAX_ZOOM_LEVEL 8 |
| 514 | #define MIN_ZOOM_LEVEL -8 |
| 515 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 516 | static const char gCharEvtName[] = "SampleCode_Char_Event"; |
| 517 | static const char gKeyEvtName[] = "SampleCode_Key_Event"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 518 | static const char gTitleEvtName[] = "SampleCode_Title_Event"; |
| 519 | static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event"; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 520 | static const char gFastTextEvtName[] = "SampleCode_FastText_Event"; |
reed@google.com | 2072db8 | 2011-11-08 15:18:10 +0000 | [diff] [blame] | 521 | static const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle"; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 522 | |
| 523 | bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) { |
| 524 | if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) { |
| 525 | if (outUni) { |
| 526 | *outUni = evt.getFast32(); |
| 527 | } |
| 528 | return true; |
| 529 | } |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) { |
| 534 | if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) { |
| 535 | if (outKey) { |
| 536 | *outKey = (SkKey)evt.getFast32(); |
| 537 | } |
| 538 | return true; |
| 539 | } |
| 540 | return false; |
| 541 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 542 | |
| 543 | bool SampleCode::TitleQ(const SkEvent& evt) { |
| 544 | return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1); |
| 545 | } |
| 546 | |
| 547 | void SampleCode::TitleR(SkEvent* evt, const char title[]) { |
| 548 | SkASSERT(evt && TitleQ(*evt)); |
| 549 | evt->setString(gTitleEvtName, title); |
| 550 | } |
| 551 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 552 | bool SampleCode::RequestTitle(SkView* view, SkString* title) { |
| 553 | SkEvent evt(gTitleEvtName); |
| 554 | if (view->doQuery(&evt)) { |
| 555 | title->set(evt.findString(gTitleEvtName)); |
| 556 | return true; |
| 557 | } |
| 558 | return false; |
| 559 | } |
| 560 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 561 | bool SampleCode::PrefSizeQ(const SkEvent& evt) { |
| 562 | return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1); |
| 563 | } |
| 564 | |
| 565 | void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) { |
| 566 | SkASSERT(evt && PrefSizeQ(*evt)); |
| 567 | SkScalar size[2]; |
| 568 | size[0] = width; |
| 569 | size[1] = height; |
| 570 | evt->setScalars(gPrefSizeEvtName, 2, size); |
| 571 | } |
| 572 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 573 | bool SampleCode::FastTextQ(const SkEvent& evt) { |
| 574 | return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1); |
| 575 | } |
| 576 | |
| 577 | /////////////////////////////////////////////////////////////////////////////// |
| 578 | |
commit-bot@chromium.org | cc63b32 | 2013-12-06 20:14:55 +0000 | [diff] [blame] | 579 | enum TilingMode { |
| 580 | kNo_Tiling, |
| 581 | kAbs_128x128_Tiling, |
| 582 | kAbs_256x256_Tiling, |
| 583 | kRel_4x4_Tiling, |
| 584 | kRel_1x16_Tiling, |
| 585 | kRel_16x1_Tiling, |
| 586 | |
| 587 | kLast_TilingMode_Enum |
| 588 | }; |
| 589 | |
| 590 | struct TilingInfo { |
| 591 | const char* label; |
| 592 | SkScalar w, h; |
| 593 | }; |
| 594 | |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 595 | static const struct TilingInfo gTilingInfo[] = { |
commit-bot@chromium.org | cc63b32 | 2013-12-06 20:14:55 +0000 | [diff] [blame] | 596 | { "No tiling", SK_Scalar1 , SK_Scalar1 }, // kNo_Tiling |
| 597 | { "128x128" , SkIntToScalar(128), SkIntToScalar(128) }, // kAbs_128x128_Tiling |
| 598 | { "256x256" , SkIntToScalar(256), SkIntToScalar(256) }, // kAbs_256x256_Tiling |
| 599 | { "1/4x1/4" , SK_Scalar1 / 4 , SK_Scalar1 / 4 }, // kRel_4x4_Tiling |
| 600 | { "1/1x1/16" , SK_Scalar1 , SK_Scalar1 / 16 }, // kRel_1x16_Tiling |
| 601 | { "1/16x1/1" , SK_Scalar1 / 16 , SK_Scalar1 }, // kRel_16x1_Tiling |
| 602 | }; |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 603 | static_assert((SK_ARRAY_COUNT(gTilingInfo) == kLast_TilingMode_Enum), |
| 604 | "Incomplete_tiling_labels"); |
commit-bot@chromium.org | cc63b32 | 2013-12-06 20:14:55 +0000 | [diff] [blame] | 605 | |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 606 | SkSize SampleWindow::tileSize() const { |
| 607 | SkASSERT((TilingMode)fTilingMode < kLast_TilingMode_Enum); |
| 608 | const struct TilingInfo* info = gTilingInfo + fTilingMode; |
| 609 | return SkSize::Make(info->w > SK_Scalar1 ? info->w : this->width() * info->w, |
| 610 | info->h > SK_Scalar1 ? info->h : this->height() * info->h); |
| 611 | } |
commit-bot@chromium.org | cc63b32 | 2013-12-06 20:14:55 +0000 | [diff] [blame] | 612 | ////////////////////////////////////////////////////////////////////////////// |
| 613 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 614 | static SkView* curr_view(SkWindow* wind) { |
| 615 | SkView::F2BIter iter(wind); |
| 616 | return iter.next(); |
| 617 | } |
| 618 | |
mike@reedtribe.org | 6f6e8c3 | 2011-12-27 22:33:50 +0000 | [diff] [blame] | 619 | static bool curr_title(SkWindow* wind, SkString* title) { |
| 620 | SkView* view = curr_view(wind); |
| 621 | if (view) { |
| 622 | SkEvent evt(gTitleEvtName); |
| 623 | if (view->doQuery(&evt)) { |
| 624 | title->set(evt.findString(gTitleEvtName)); |
| 625 | return true; |
| 626 | } |
| 627 | } |
| 628 | return false; |
| 629 | } |
| 630 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 631 | bool SampleWindow::sendAnimatePulse() { |
| 632 | SkView* view = curr_view(this); |
| 633 | if (SampleView::IsSampleView(view)) { |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 634 | return ((SampleView*)view)->animate(gAnimTimer); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 635 | } |
| 636 | return false; |
| 637 | } |
| 638 | |
reed | 5f629b4 | 2015-08-15 11:57:23 -0700 | [diff] [blame] | 639 | void SampleWindow::setZoomCenter(float x, float y) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 640 | fZoomCenterX = x; |
| 641 | fZoomCenterY = y; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 642 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 643 | |
reed | 5f629b4 | 2015-08-15 11:57:23 -0700 | [diff] [blame] | 644 | bool SampleWindow::zoomIn() { |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 645 | // Arbitrarily decided |
reed | 5f629b4 | 2015-08-15 11:57:23 -0700 | [diff] [blame] | 646 | if (fFatBitsScale == kMaxFatBitsScale) return false; |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 647 | fFatBitsScale++; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 648 | this->inval(nullptr); |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 649 | return true; |
| 650 | } |
| 651 | |
reed | 5f629b4 | 2015-08-15 11:57:23 -0700 | [diff] [blame] | 652 | bool SampleWindow::zoomOut() { |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 653 | if (fFatBitsScale == 1) return false; |
| 654 | fFatBitsScale--; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 655 | this->inval(nullptr); |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 656 | return true; |
| 657 | } |
| 658 | |
reed | 5f629b4 | 2015-08-15 11:57:23 -0700 | [diff] [blame] | 659 | void SampleWindow::updatePointer(int x, int y) { |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 660 | fMouseX = x; |
| 661 | fMouseY = y; |
| 662 | if (fShowZoomer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 663 | this->inval(nullptr); |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 667 | static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) { |
| 668 | static const SampleWindow::DeviceType gCT[] = { |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 669 | SampleWindow::kRaster_DeviceType |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 670 | #if SK_SUPPORT_GPU |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 671 | , SampleWindow::kGPU_DeviceType |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 672 | #if SK_ANGLE |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 673 | , SampleWindow::kANGLE_DeviceType |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 674 | #endif // SK_ANGLE |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 675 | #if SK_COMMAND_BUFFER |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 676 | , SampleWindow::kCommandBuffer_DeviceType |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 677 | #endif // SK_COMMAND_BUFFER |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 678 | #endif // SK_SUPPORT_GPU |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 679 | }; |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 680 | static_assert(SK_ARRAY_COUNT(gCT) == SampleWindow::kDeviceTypeCnt, "array_size_mismatch"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 681 | return gCT[ct]; |
| 682 | } |
| 683 | |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 684 | static SkString getSampleTitle(const SkViewFactory* sampleFactory) { |
| 685 | SkView* view = (*sampleFactory)(); |
| 686 | SkString title; |
| 687 | SampleCode::RequestTitle(view, &title); |
| 688 | view->unref(); |
| 689 | return title; |
| 690 | } |
| 691 | |
edisonn@google.com | de36b69 | 2013-07-11 15:40:31 +0000 | [diff] [blame] | 692 | static bool compareSampleTitle(const SkViewFactory* first, const SkViewFactory* second) { |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 693 | return strcmp(getSampleTitle(first).c_str(), getSampleTitle(second).c_str()) < 0; |
robertphillips@google.com | 8570b5c | 2012-03-20 17:40:58 +0000 | [diff] [blame] | 694 | } |
| 695 | |
reed | b7ffe03 | 2015-11-05 11:59:17 -0800 | [diff] [blame] | 696 | static int find_by_title(const SkViewFactory* const* factories, int count, const char title[]) { |
| 697 | for (int i = 0; i < count; i++) { |
| 698 | if (getSampleTitle(factories[i]).equals(title)) { |
| 699 | return i; |
| 700 | } |
| 701 | } |
| 702 | return -1; |
| 703 | } |
| 704 | |
| 705 | static void restrict_samples(SkTDArray<const SkViewFactory*>& factories, const SkString titles[], |
| 706 | int count) { |
| 707 | int newCount = 0; |
| 708 | for (int i = 0; i < count; ++i) { |
| 709 | int index = find_by_title(factories.begin(), factories.count(), titles[i].c_str()); |
| 710 | if (index >= 0) { |
| 711 | SkTSwap(factories.begin()[newCount], factories.begin()[index]); |
| 712 | newCount += 1; |
| 713 | } |
| 714 | } |
| 715 | if (newCount) { |
| 716 | factories.setCount(newCount); |
| 717 | } |
| 718 | } |
| 719 | |
mtklein | fd117d8 | 2014-06-19 14:18:56 -0700 | [diff] [blame] | 720 | DEFINE_string(slide, "", "Start on this sample."); |
| 721 | DEFINE_int32(msaa, 0, "Request multisampling with this count."); |
| 722 | DEFINE_string(pictureDir, "", "Read pictures from here."); |
| 723 | DEFINE_string(picture, "", "Path to single picture."); |
reed | b7ffe03 | 2015-11-05 11:59:17 -0800 | [diff] [blame] | 724 | DEFINE_string(sequence, "", "Path to file containing the desired samples/gms to show."); |
mtklein | fd117d8 | 2014-06-19 14:18:56 -0700 | [diff] [blame] | 725 | DEFINE_bool(sort, false, "Sort samples by title."); |
| 726 | DEFINE_bool(list, false, "List samples?"); |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 727 | DEFINE_bool(gpu, false, "Start up with gpu?"); |
caryclark | 6531c36 | 2015-07-20 13:38:56 -0700 | [diff] [blame] | 728 | DEFINE_string(key, "", ""); // dummy to enable gm tests that have platform-specific names |
mtklein | fd117d8 | 2014-06-19 14:18:56 -0700 | [diff] [blame] | 729 | #ifdef SAMPLE_PDF_FILE_VIEWER |
| 730 | DEFINE_string(pdfPath, "", "Path to direcotry of pdf files."); |
| 731 | #endif |
| 732 | |
reed | 89889b6 | 2014-10-29 12:36:45 -0700 | [diff] [blame] | 733 | #include "SkTaskGroup.h" |
| 734 | |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 735 | SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager) |
robertphillips@google.com | 4750fa5 | 2012-04-10 13:34:11 +0000 | [diff] [blame] | 736 | : INHERITED(hwnd) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 737 | , fDevManager(nullptr) { |
robertphillips@google.com | 8570b5c | 2012-03-20 17:40:58 +0000 | [diff] [blame] | 738 | |
mtklein | fd117d8 | 2014-06-19 14:18:56 -0700 | [diff] [blame] | 739 | SkCommandLineFlags::Parse(argc, argv); |
| 740 | |
reed@google.com | 7d05ac9 | 2013-06-06 13:14:13 +0000 | [diff] [blame] | 741 | fCurrIndex = -1; |
| 742 | |
mtklein | fd117d8 | 2014-06-19 14:18:56 -0700 | [diff] [blame] | 743 | if (!FLAGS_pictureDir.isEmpty()) { |
| 744 | SkOSFile::Iter iter(FLAGS_pictureDir[0], "skp"); |
| 745 | SkString filename; |
| 746 | while (iter.next(&filename)) { |
| 747 | *fSamples.append() = new PictFileFactory( |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 748 | SkOSPath::Join(FLAGS_pictureDir[0], filename.c_str())); |
mtklein | fd117d8 | 2014-06-19 14:18:56 -0700 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | if (!FLAGS_picture.isEmpty()) { |
| 752 | SkString path(FLAGS_picture[0]); |
| 753 | fCurrIndex = fSamples.count(); |
| 754 | *fSamples.append() = new PictFileFactory(path); |
| 755 | } |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 756 | #ifdef SAMPLE_PDF_FILE_VIEWER |
mtklein | fd117d8 | 2014-06-19 14:18:56 -0700 | [diff] [blame] | 757 | if (!FLAGS_pdfPath.isEmpty()) { |
| 758 | SkOSFile::Iter iter(FLAGS_pdfPath[0], "pdf"); |
| 759 | SkString filename; |
| 760 | while (iter.next(&filename)) { |
| 761 | *fSamples.append() = new PdfFileViewerFactory( |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 762 | SkOSPath::Join(FLAGS_pictureDir[0], filename.c_str())); |
mtklein | fd117d8 | 2014-06-19 14:18:56 -0700 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | #endif |
robertphillips@google.com | 76d4021 | 2012-03-22 14:12:15 +0000 | [diff] [blame] | 766 | SkGMRegistyToSampleRegistry(); |
| 767 | { |
| 768 | const SkViewRegister* reg = SkViewRegister::Head(); |
| 769 | while (reg) { |
| 770 | *fSamples.append() = reg->factory(); |
| 771 | reg = reg->next(); |
| 772 | } |
| 773 | } |
| 774 | |
reed | b7ffe03 | 2015-11-05 11:59:17 -0800 | [diff] [blame] | 775 | if (!FLAGS_sequence.isEmpty()) { |
| 776 | // The sequence file just contains a list (separated by CRs) of the samples or GM:gms |
| 777 | // you want to restrict to. Only these will appear when you cycle through. |
| 778 | // If none are found, or the file is empty, then it will be ignored, and all samples |
| 779 | // will be available. |
| 780 | SkFILEStream stream(FLAGS_sequence[0]); |
| 781 | if (stream.isValid()) { |
| 782 | size_t len = stream.getLength(); |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 783 | SkAutoTMalloc<char> storage(len + 1); |
| 784 | char* buffer = storage.get(); |
reed | b7ffe03 | 2015-11-05 11:59:17 -0800 | [diff] [blame] | 785 | stream.read(buffer, len); |
| 786 | buffer[len] = 0; |
| 787 | |
| 788 | SkTArray<SkString> titles; |
| 789 | SkStrSplit(buffer, "\n\r", &titles); |
| 790 | restrict_samples(fSamples, titles.begin(), titles.count()); |
| 791 | } |
| 792 | } |
| 793 | |
mtklein | fd117d8 | 2014-06-19 14:18:56 -0700 | [diff] [blame] | 794 | if (FLAGS_sort) { |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 795 | // Sort samples, so foo.skp and foo.pdf are consecutive and we can quickly spot where |
| 796 | // skp -> pdf -> png fails. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 797 | SkTQSort(fSamples.begin(), fSamples.end() ? fSamples.end() - 1 : nullptr, compareSampleTitle); |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 798 | } |
| 799 | |
mtklein | fd117d8 | 2014-06-19 14:18:56 -0700 | [diff] [blame] | 800 | if (!FLAGS_slide.isEmpty()) { |
| 801 | fCurrIndex = findByTitle(FLAGS_slide[0]); |
| 802 | if (fCurrIndex < 0) { |
| 803 | fprintf(stderr, "Unknown sample \"%s\"\n", FLAGS_slide[0]); |
robertphillips@google.com | 7265e72 | 2012-05-03 18:22:28 +0000 | [diff] [blame] | 804 | listTitles(); |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 805 | } |
mtklein | fd117d8 | 2014-06-19 14:18:56 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | fMSAASampleCount = FLAGS_msaa; |
| 809 | |
| 810 | if (FLAGS_list) { |
| 811 | listTitles(); |
robertphillips@google.com | 8570b5c | 2012-03-20 17:40:58 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | if (fCurrIndex < 0) { |
| 815 | SkString title; |
| 816 | if (readTitleFromPrefs(&title)) { |
| 817 | fCurrIndex = findByTitle(title.c_str()); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | if (fCurrIndex < 0) { |
| 822 | fCurrIndex = 0; |
| 823 | } |
| 824 | |
reed | 89889b6 | 2014-10-29 12:36:45 -0700 | [diff] [blame] | 825 | static SkTaskGroup::Enabler enabled(-1); |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 826 | gSampleWindow = this; |
| 827 | |
robertphillips@google.com | bd8d7ad | 2012-03-30 15:18:14 +0000 | [diff] [blame] | 828 | fDeviceType = kRaster_DeviceType; |
jvanverth | 41518eb | 2015-10-02 10:42:36 -0700 | [diff] [blame] | 829 | #if SK_SUPPORT_GPU |
jvanverth | 44dcb8a | 2015-10-02 09:12:05 -0700 | [diff] [blame] | 830 | if (FLAGS_gpu) { |
| 831 | fDeviceType = kGPU_DeviceType; |
| 832 | } |
jvanverth | 41518eb | 2015-10-02 10:42:36 -0700 | [diff] [blame] | 833 | #endif |
| 834 | |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 835 | #if DEFAULT_TO_GPU |
| 836 | fDeviceType = kGPU_DeviceType; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 837 | #endif |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 838 | #if SK_ANGLE && DEFAULT_TO_ANGLE |
| 839 | fDeviceType = kANGLE_DeviceType; |
| 840 | #endif |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 841 | #if SK_COMMAND_BUFFER && DEFAULT_TO_COMMAND_BUFFER |
| 842 | fDeviceType = kCommandBuffer_DeviceType; |
| 843 | #endif |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 844 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 845 | fUseClip = false; |
reed | 91b4dfb | 2016-01-08 08:23:19 -0800 | [diff] [blame] | 846 | fUsePicture = false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 847 | fAnimating = false; |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 848 | fRotate = false; |
bsalomon@google.com | e8f0910 | 2011-09-08 18:48:12 +0000 | [diff] [blame] | 849 | fPerspAnim = false; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 850 | fRequestGrabImage = false; |
commit-bot@chromium.org | cc63b32 | 2013-12-06 20:14:55 +0000 | [diff] [blame] | 851 | fTilingMode = kNo_Tiling; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 852 | fMeasureFPS = false; |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 853 | fLCDState = SkOSMenu::kMixedState; |
| 854 | fAAState = SkOSMenu::kMixedState; |
bungeman@google.com | 96aabc8 | 2013-06-03 21:26:34 +0000 | [diff] [blame] | 855 | fSubpixelState = SkOSMenu::kMixedState; |
| 856 | fHintingState = 0; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 857 | fFilterQualityIndex = 0; |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 858 | fFlipAxis = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 859 | fScrollTestX = fScrollTestY = 0; |
| 860 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 861 | fMouseX = fMouseY = 0; |
mike@reedtribe.org | 3ce59dc | 2011-04-08 00:38:05 +0000 | [diff] [blame] | 862 | fFatBitsScale = 8; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 863 | fTypeface = SkTypeface::CreateFromTypeface(nullptr, SkTypeface::kBold); |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 864 | fShowZoomer = false; |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 865 | |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 866 | fZoomLevel = 0; |
| 867 | fZoomScale = SK_Scalar1; |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 868 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 869 | fMagnify = false; |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 870 | |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 871 | fSaveToPdf = false; |
reed | 093b4e8 | 2015-08-04 18:46:38 -0700 | [diff] [blame] | 872 | fSaveToSKP = false; |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 873 | |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 874 | int sinkID = this->getSinkID(); |
scroggo@google.com | 7dadc74 | 2012-04-18 14:07:57 +0000 | [diff] [blame] | 875 | fAppMenu = new SkOSMenu; |
| 876 | fAppMenu->setTitle("Global Settings"); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 877 | int itemID; |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 878 | |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 879 | itemID = fAppMenu->appendList("Device Type", "Device Type", sinkID, 0, |
| 880 | "Raster", |
| 881 | "OpenGL", |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 882 | #if SK_ANGLE |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 883 | "ANGLE", |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 884 | #endif |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 885 | #if SK_COMMAND_BUFFER |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 886 | "Command Buffer", |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 887 | #endif |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 888 | nullptr); |
scroggo@google.com | 7dadc74 | 2012-04-18 14:07:57 +0000 | [diff] [blame] | 889 | fAppMenu->assignKeyEquivalentToItem(itemID, 'd'); |
| 890 | itemID = fAppMenu->appendTriState("AA", "AA", sinkID, fAAState); |
| 891 | fAppMenu->assignKeyEquivalentToItem(itemID, 'b'); |
| 892 | itemID = fAppMenu->appendTriState("LCD", "LCD", sinkID, fLCDState); |
| 893 | fAppMenu->assignKeyEquivalentToItem(itemID, 'l'); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 894 | itemID = fAppMenu->appendList("FilterQuality", "FilterQuality", sinkID, fFilterQualityIndex, |
| 895 | gFilterQualityStates[0].fName, |
| 896 | gFilterQualityStates[1].fName, |
| 897 | gFilterQualityStates[2].fName, |
| 898 | gFilterQualityStates[3].fName, |
| 899 | gFilterQualityStates[4].fName, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 900 | nullptr); |
scroggo@google.com | 7dadc74 | 2012-04-18 14:07:57 +0000 | [diff] [blame] | 901 | fAppMenu->assignKeyEquivalentToItem(itemID, 'n'); |
bungeman@google.com | 96aabc8 | 2013-06-03 21:26:34 +0000 | [diff] [blame] | 902 | itemID = fAppMenu->appendTriState("Subpixel", "Subpixel", sinkID, fSubpixelState); |
| 903 | fAppMenu->assignKeyEquivalentToItem(itemID, 's'); |
| 904 | itemID = fAppMenu->appendList("Hinting", "Hinting", sinkID, fHintingState, |
| 905 | gHintingStates[0].name, |
| 906 | gHintingStates[1].name, |
| 907 | gHintingStates[2].name, |
| 908 | gHintingStates[3].name, |
bungeman@google.com | a9133bb | 2013-06-04 17:49:20 +0000 | [diff] [blame] | 909 | gHintingStates[4].name, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 910 | nullptr); |
scroggo@google.com | 7dadc74 | 2012-04-18 14:07:57 +0000 | [diff] [blame] | 911 | fAppMenu->assignKeyEquivalentToItem(itemID, 'h'); |
reed@google.com | 67b89ee | 2012-08-15 14:41:58 +0000 | [diff] [blame] | 912 | |
commit-bot@chromium.org | cc63b32 | 2013-12-06 20:14:55 +0000 | [diff] [blame] | 913 | itemID =fAppMenu->appendList("Tiling", "Tiling", sinkID, fTilingMode, |
| 914 | gTilingInfo[kNo_Tiling].label, |
| 915 | gTilingInfo[kAbs_128x128_Tiling].label, |
| 916 | gTilingInfo[kAbs_256x256_Tiling].label, |
| 917 | gTilingInfo[kRel_4x4_Tiling].label, |
| 918 | gTilingInfo[kRel_1x16_Tiling].label, |
| 919 | gTilingInfo[kRel_16x1_Tiling].label, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 920 | nullptr); |
reed@google.com | 67b89ee | 2012-08-15 14:41:58 +0000 | [diff] [blame] | 921 | fAppMenu->assignKeyEquivalentToItem(itemID, 't'); |
| 922 | |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 923 | itemID = fAppMenu->appendSwitch("Slide Show", "Slide Show" , sinkID, false); |
| 924 | fAppMenu->assignKeyEquivalentToItem(itemID, 'a'); |
| 925 | itemID = fAppMenu->appendSwitch("Clip", "Clip" , sinkID, fUseClip); |
scroggo@google.com | 7dadc74 | 2012-04-18 14:07:57 +0000 | [diff] [blame] | 926 | fAppMenu->assignKeyEquivalentToItem(itemID, 'c'); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 927 | itemID = fAppMenu->appendSwitch("Flip X", "Flip X" , sinkID, false); |
scroggo@google.com | 7dadc74 | 2012-04-18 14:07:57 +0000 | [diff] [blame] | 928 | fAppMenu->assignKeyEquivalentToItem(itemID, 'x'); |
| 929 | itemID = fAppMenu->appendSwitch("Flip Y", "Flip Y" , sinkID, false); |
| 930 | fAppMenu->assignKeyEquivalentToItem(itemID, 'y'); |
| 931 | itemID = fAppMenu->appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer); |
| 932 | fAppMenu->assignKeyEquivalentToItem(itemID, 'z'); |
| 933 | itemID = fAppMenu->appendSwitch("Magnify", "Magnify" , sinkID, fMagnify); |
| 934 | fAppMenu->assignKeyEquivalentToItem(itemID, 'm'); |
reed | 093b4e8 | 2015-08-04 18:46:38 -0700 | [diff] [blame] | 935 | |
scroggo@google.com | 7dadc74 | 2012-04-18 14:07:57 +0000 | [diff] [blame] | 936 | itemID = fAppMenu->appendAction("Save to PDF", sinkID); |
| 937 | fAppMenu->assignKeyEquivalentToItem(itemID, 'e'); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 938 | |
scroggo@google.com | 7dadc74 | 2012-04-18 14:07:57 +0000 | [diff] [blame] | 939 | this->addMenu(fAppMenu); |
| 940 | fSlideMenu = new SkOSMenu; |
| 941 | this->addMenu(fSlideMenu); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 942 | |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 943 | this->setVisibleP(true); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 944 | this->setClipToBounds(false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 945 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 946 | this->loadView((*fSamples[fCurrIndex])()); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 947 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 948 | if (nullptr == devManager) { |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 949 | fDevManager = new DefaultDeviceManager(); |
| 950 | } else { |
| 951 | devManager->ref(); |
| 952 | fDevManager = devManager; |
| 953 | } |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 954 | fDevManager->setUpBackend(this, fMSAASampleCount); |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 955 | |
Scroggo | b4490c7 | 2011-06-17 13:53:05 +0000 | [diff] [blame] | 956 | // If another constructor set our dimensions, ensure that our |
| 957 | // onSizeChange gets called. |
| 958 | if (this->height() && this->width()) { |
| 959 | this->onSizeChange(); |
| 960 | } |
reed@google.com | 2072db8 | 2011-11-08 15:18:10 +0000 | [diff] [blame] | 961 | |
| 962 | // can't call this synchronously, since it may require a subclass to |
| 963 | // to implement, or the caller may need us to have returned from the |
| 964 | // constructor first. Hence we post an event to ourselves. |
| 965 | // this->updateTitle(); |
commit-bot@chromium.org | 6de4a78 | 2013-12-11 17:05:23 +0000 | [diff] [blame] | 966 | post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this); |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 967 | |
| 968 | gAnimTimer.run(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | SampleWindow::~SampleWindow() { |
Ben Wagner | b17c764 | 2014-07-10 16:21:49 -0400 | [diff] [blame] | 972 | SkSafeUnref(fTypeface); |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 973 | SkSafeUnref(fDevManager); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 974 | } |
| 975 | |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 976 | |
mike@reedtribe.org | 6f6e8c3 | 2011-12-27 22:33:50 +0000 | [diff] [blame] | 977 | int SampleWindow::findByTitle(const char title[]) { |
| 978 | int i, count = fSamples.count(); |
| 979 | for (i = 0; i < count; i++) { |
| 980 | if (getSampleTitle(i).equals(title)) { |
| 981 | return i; |
| 982 | } |
| 983 | } |
mike@reedtribe.org | dd52caa | 2011-12-28 20:02:10 +0000 | [diff] [blame] | 984 | return -1; |
mike@reedtribe.org | 6f6e8c3 | 2011-12-27 22:33:50 +0000 | [diff] [blame] | 985 | } |
| 986 | |
robertphillips@google.com | 7265e72 | 2012-05-03 18:22:28 +0000 | [diff] [blame] | 987 | void SampleWindow::listTitles() { |
| 988 | int count = fSamples.count(); |
| 989 | SkDebugf("All Slides:\n"); |
| 990 | for (int i = 0; i < count; i++) { |
| 991 | SkDebugf(" %s\n", getSampleTitle(i).c_str()); |
| 992 | } |
| 993 | } |
| 994 | |
reed@android.com | 55e76b2 | 2009-11-23 21:46:47 +0000 | [diff] [blame] | 995 | static SkBitmap capture_bitmap(SkCanvas* canvas) { |
| 996 | SkBitmap bm; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 997 | if (bm.tryAllocPixels(canvas->imageInfo())) { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 998 | canvas->readPixels(&bm, 0, 0); |
| 999 | } |
reed@android.com | 55e76b2 | 2009-11-23 21:46:47 +0000 | [diff] [blame] | 1000 | return bm; |
| 1001 | } |
| 1002 | |
reed | f7f79d2 | 2015-02-21 09:03:20 -0800 | [diff] [blame] | 1003 | static void drawText(SkCanvas* canvas, SkString str, SkScalar left, SkScalar top, SkPaint& paint) { |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 1004 | SkColor desiredColor = paint.getColor(); |
| 1005 | paint.setColor(SK_ColorWHITE); |
reed | f7f79d2 | 2015-02-21 09:03:20 -0800 | [diff] [blame] | 1006 | const char* c_str = str.c_str(); |
| 1007 | size_t size = str.size(); |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 1008 | SkRect bounds; |
| 1009 | paint.measureText(c_str, size, &bounds); |
| 1010 | bounds.offset(left, top); |
| 1011 | SkScalar inset = SkIntToScalar(-2); |
| 1012 | bounds.inset(inset, inset); |
| 1013 | canvas->drawRect(bounds, paint); |
| 1014 | if (desiredColor != SK_ColorBLACK) { |
| 1015 | paint.setColor(SK_ColorBLACK); |
| 1016 | canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint); |
| 1017 | } |
| 1018 | paint.setColor(desiredColor); |
| 1019 | canvas->drawText(c_str, size, left, top, paint); |
| 1020 | } |
| 1021 | |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 1022 | #define XCLIP_N 8 |
| 1023 | #define YCLIP_N 8 |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 1024 | |
| 1025 | void SampleWindow::draw(SkCanvas* canvas) { |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 1026 | gAnimTimer.updateTime(); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1027 | |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 1028 | if (fGesture.isActive()) { |
| 1029 | this->updateMatrix(); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1030 | } |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1031 | |
djsollen@google.com | 796763e | 2012-12-10 14:12:55 +0000 | [diff] [blame] | 1032 | if (fMeasureFPS) { |
| 1033 | fMeasureFPS_Time = 0; |
| 1034 | } |
| 1035 | |
reed | f7f79d2 | 2015-02-21 09:03:20 -0800 | [diff] [blame] | 1036 | SkSize tile = this->tileSize(); |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 1037 | |
reed | f7f79d2 | 2015-02-21 09:03:20 -0800 | [diff] [blame] | 1038 | if (kNo_Tiling == fTilingMode) { |
| 1039 | this->INHERITED::draw(canvas); // no looping or surfaces needed |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 1040 | } else { |
reed | f7f79d2 | 2015-02-21 09:03:20 -0800 | [diff] [blame] | 1041 | const int w = SkScalarRoundToInt(tile.width()); |
| 1042 | const int h = SkScalarRoundToInt(tile.height()); |
| 1043 | SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); |
| 1044 | SkAutoTUnref<SkSurface> surface(canvas->newSurface(info)); |
| 1045 | SkCanvas* tileCanvas = surface->getCanvas(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 1046 | |
reed | f7f79d2 | 2015-02-21 09:03:20 -0800 | [diff] [blame] | 1047 | for (SkScalar y = 0; y < height(); y += tile.height()) { |
| 1048 | for (SkScalar x = 0; x < width(); x += tile.width()) { |
| 1049 | SkAutoCanvasRestore acr(tileCanvas, true); |
| 1050 | tileCanvas->translate(-x, -y); |
| 1051 | tileCanvas->clear(0); |
| 1052 | this->INHERITED::draw(tileCanvas); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1053 | surface->draw(canvas, x, y, nullptr); |
commit-bot@chromium.org | 9a558d4 | 2014-05-30 15:06:24 +0000 | [diff] [blame] | 1054 | } |
reed | f7f79d2 | 2015-02-21 09:03:20 -0800 | [diff] [blame] | 1055 | } |
commit-bot@chromium.org | 9a558d4 | 2014-05-30 15:06:24 +0000 | [diff] [blame] | 1056 | |
reed | f7f79d2 | 2015-02-21 09:03:20 -0800 | [diff] [blame] | 1057 | // for drawing the borders between tiles |
| 1058 | SkPaint paint; |
| 1059 | paint.setColor(0x60FF00FF); |
| 1060 | paint.setStyle(SkPaint::kStroke_Style); |
commit-bot@chromium.org | cc63b32 | 2013-12-06 20:14:55 +0000 | [diff] [blame] | 1061 | |
reed | f7f79d2 | 2015-02-21 09:03:20 -0800 | [diff] [blame] | 1062 | for (SkScalar y = 0; y < height(); y += tile.height()) { |
| 1063 | for (SkScalar x = 0; x < width(); x += tile.width()) { |
| 1064 | canvas->drawRect(SkRect::MakeXYWH(x, y, tile.width(), tile.height()), paint); |
scroggo@google.com | 85cade0 | 2012-08-17 13:29:50 +0000 | [diff] [blame] | 1065 | } |
| 1066 | } |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 1067 | } |
reed | f7f79d2 | 2015-02-21 09:03:20 -0800 | [diff] [blame] | 1068 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 1069 | if (fShowZoomer && !fSaveToPdf) { |
Scroggo | 3272ba8 | 2011-05-25 20:50:42 +0000 | [diff] [blame] | 1070 | showZoomer(canvas); |
| 1071 | } |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1072 | if (fMagnify && !fSaveToPdf) { |
| 1073 | magnify(canvas); |
| 1074 | } |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1075 | |
djsollen@google.com | 796763e | 2012-12-10 14:12:55 +0000 | [diff] [blame] | 1076 | if (fMeasureFPS && fMeasureFPS_Time) { |
| 1077 | this->updateTitle(); |
| 1078 | this->postInvalDelay(); |
| 1079 | } |
| 1080 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 1081 | if (this->sendAnimatePulse()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1082 | this->inval(nullptr); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
reed@google.com | 29038ed | 2011-07-06 17:56:47 +0000 | [diff] [blame] | 1085 | // do this last |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 1086 | fDevManager->publishCanvas(fDeviceType, canvas, this); |
Scroggo | 3272ba8 | 2011-05-25 20:50:42 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1089 | static float clipW = 200; |
| 1090 | static float clipH = 200; |
| 1091 | void SampleWindow::magnify(SkCanvas* canvas) { |
| 1092 | SkRect r; |
| 1093 | int count = canvas->save(); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1094 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1095 | SkMatrix m = canvas->getTotalMatrix(); |
robertphillips@google.com | 07ef911 | 2012-06-04 13:22:14 +0000 | [diff] [blame] | 1096 | if (!m.invert(&m)) { |
| 1097 | return; |
| 1098 | } |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1099 | SkPoint offset, center; |
bsalomon@google.com | 820e80a | 2011-10-24 21:09:40 +0000 | [diff] [blame] | 1100 | SkScalar mouseX = fMouseX * SK_Scalar1; |
| 1101 | SkScalar mouseY = fMouseY * SK_Scalar1; |
| 1102 | m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset); |
| 1103 | m.mapXY(mouseX, mouseY, ¢er); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1104 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1105 | r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX()); |
| 1106 | r.offset(offset.fX, offset.fY); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1107 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1108 | SkPaint paint; |
| 1109 | paint.setColor(0xFF66AAEE); |
| 1110 | paint.setStyle(SkPaint::kStroke_Style); |
bsalomon@google.com | 820e80a | 2011-10-24 21:09:40 +0000 | [diff] [blame] | 1111 | paint.setStrokeWidth(10.f * m.getScaleX()); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1112 | //lense offset |
| 1113 | //canvas->translate(0, -250); |
| 1114 | canvas->drawRect(r, paint); |
| 1115 | canvas->clipRect(r); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1116 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1117 | m = canvas->getTotalMatrix(); |
| 1118 | m.setTranslate(-center.fX, -center.fY); |
bsalomon@google.com | 820e80a | 2011-10-24 21:09:40 +0000 | [diff] [blame] | 1119 | m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1120 | m.postTranslate(center.fX, center.fY); |
| 1121 | canvas->concat(m); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1122 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1123 | this->INHERITED::draw(canvas); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1124 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1125 | canvas->restoreToCount(count); |
| 1126 | } |
| 1127 | |
reed | 5f629b4 | 2015-08-15 11:57:23 -0700 | [diff] [blame] | 1128 | static SkPaint& set_color_ref(SkPaint& paint, SkColor c) { |
| 1129 | paint.setColor(c); |
| 1130 | return paint; |
| 1131 | } |
reed@google.com | b36334d | 2011-05-18 15:07:20 +0000 | [diff] [blame] | 1132 | |
reed | 5f629b4 | 2015-08-15 11:57:23 -0700 | [diff] [blame] | 1133 | static void show_lcd_box(SkCanvas* canvas, SkScalar x, SkScalar y, SkColor c, |
| 1134 | SkScalar sx, SkScalar sy) { |
| 1135 | const SkScalar w = (1 - 1/sx) / 3; |
| 1136 | SkPaint paint; |
| 1137 | SkRect r = SkRect::MakeXYWH(x, y, w, 1 - 1/sy); |
| 1138 | canvas->drawRect(r, set_color_ref(paint, SkColorSetRGB(SkColorGetR(c), 0, 0))); |
| 1139 | r.offset(w, 0); |
| 1140 | canvas->drawRect(r, set_color_ref(paint, SkColorSetRGB(0, SkColorGetG(c), 0))); |
| 1141 | r.offset(w, 0); |
| 1142 | canvas->drawRect(r, set_color_ref(paint, SkColorSetRGB(0, 0, SkColorGetB(c)))); |
| 1143 | } |
reed@google.com | b36334d | 2011-05-18 15:07:20 +0000 | [diff] [blame] | 1144 | |
reed | 5f629b4 | 2015-08-15 11:57:23 -0700 | [diff] [blame] | 1145 | static void show_lcd_circle(SkCanvas* canvas, SkScalar x, SkScalar y, SkColor c, |
| 1146 | SkScalar, SkScalar) { |
| 1147 | const SkRect r = SkRect::MakeXYWH(x, y, 1, 1); |
| 1148 | const SkScalar cx = x + 0.5f; |
| 1149 | const SkScalar cy = y + 0.5f; |
| 1150 | |
| 1151 | SkPaint paint; |
| 1152 | paint.setAntiAlias(true); |
| 1153 | |
| 1154 | SkPath path; |
| 1155 | path.addArc(r, 0, 120); path.lineTo(cx, cy); |
| 1156 | canvas->drawPath(path, set_color_ref(paint, SkColorSetRGB(SkColorGetR(c), 0, 0))); |
| 1157 | |
| 1158 | path.reset(); path.addArc(r, 120, 120); path.lineTo(cx, cy); |
| 1159 | canvas->drawPath(path, set_color_ref(paint, SkColorSetRGB(0, SkColorGetG(c), 0))); |
| 1160 | |
| 1161 | path.reset(); path.addArc(r, 240, 120); path.lineTo(cx, cy); |
| 1162 | canvas->drawPath(path, set_color_ref(paint, SkColorSetRGB(0, 0, SkColorGetB(c)))); |
| 1163 | } |
| 1164 | |
| 1165 | typedef void (*ShowLCDProc)(SkCanvas*, SkScalar, SkScalar, SkColor, SkScalar, SkScalar); |
| 1166 | |
| 1167 | /* |
| 1168 | * Like drawBitmapRect but we manually draw each pixels in RGB |
| 1169 | */ |
| 1170 | static void show_lcd_grid(SkCanvas* canvas, const SkBitmap& bitmap, |
reed | 8a41adb | 2015-08-15 14:02:29 -0700 | [diff] [blame] | 1171 | const SkIRect& origSrc, const SkRect& dst, ShowLCDProc proc) { |
reed | 5f629b4 | 2015-08-15 11:57:23 -0700 | [diff] [blame] | 1172 | SkIRect src; |
| 1173 | if (!src.intersect(origSrc, bitmap.bounds())) { |
| 1174 | return; |
| 1175 | } |
| 1176 | const SkScalar sx = dst.width() / src.width(); |
| 1177 | const SkScalar sy = dst.height() / src.height(); |
| 1178 | |
| 1179 | SkAutoCanvasRestore acr(canvas, true); |
| 1180 | canvas->translate(dst.left(), dst.top()); |
| 1181 | canvas->scale(sx, sy); |
| 1182 | |
reed | 5f629b4 | 2015-08-15 11:57:23 -0700 | [diff] [blame] | 1183 | for (int y = 0; y < src.height(); ++y) { |
| 1184 | for (int x = 0; x < src.width(); ++x) { |
| 1185 | proc(canvas, SkIntToScalar(x), SkIntToScalar(y), |
| 1186 | bitmap.getColor(src.left() + x, src.top() + y), sx, sy); |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 1187 | } |
reed | 5f629b4 | 2015-08-15 11:57:23 -0700 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | void SampleWindow::showZoomer(SkCanvas* canvas) { |
| 1192 | int count = canvas->save(); |
| 1193 | canvas->resetMatrix(); |
| 1194 | // Ensure the mouse position is on screen. |
| 1195 | int width = SkScalarRoundToInt(this->width()); |
| 1196 | int height = SkScalarRoundToInt(this->height()); |
| 1197 | if (fMouseX >= width) fMouseX = width - 1; |
| 1198 | else if (fMouseX < 0) fMouseX = 0; |
| 1199 | if (fMouseY >= height) fMouseY = height - 1; |
| 1200 | else if (fMouseY < 0) fMouseY = 0; |
| 1201 | |
| 1202 | SkBitmap bitmap = capture_bitmap(canvas); |
| 1203 | bitmap.lockPixels(); |
| 1204 | |
| 1205 | // Find the size of the zoomed in view, forced to be odd, so the examined pixel is in the middle. |
| 1206 | int zoomedWidth = (width >> 1) | 1; |
| 1207 | int zoomedHeight = (height >> 1) | 1; |
| 1208 | SkIRect src; |
| 1209 | src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale); |
| 1210 | src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1)); |
| 1211 | SkRect dest; |
| 1212 | dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight)); |
| 1213 | dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight)); |
| 1214 | SkPaint paint; |
| 1215 | // Clear the background behind our zoomed in view |
| 1216 | paint.setColor(SK_ColorWHITE); |
| 1217 | canvas->drawRect(dest, paint); |
reed | 8a41adb | 2015-08-15 14:02:29 -0700 | [diff] [blame] | 1218 | switch (fFatBitsScale) { |
| 1219 | case kMaxFatBitsScale: |
| 1220 | show_lcd_grid(canvas, bitmap, src, dest, show_lcd_box); |
| 1221 | break; |
| 1222 | case kMaxFatBitsScale - 1: |
| 1223 | show_lcd_grid(canvas, bitmap, src, dest, show_lcd_circle); |
| 1224 | break; |
| 1225 | default: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1226 | canvas->drawBitmapRect(bitmap, src, dest, nullptr); |
reed | 8a41adb | 2015-08-15 14:02:29 -0700 | [diff] [blame] | 1227 | break; |
reed | 5f629b4 | 2015-08-15 11:57:23 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | paint.setColor(SK_ColorBLACK); |
| 1231 | paint.setStyle(SkPaint::kStroke_Style); |
| 1232 | // Draw a border around the pixel in the middle |
| 1233 | SkRect originalPixel; |
| 1234 | originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1)); |
| 1235 | SkMatrix matrix; |
| 1236 | SkRect scalarSrc; |
| 1237 | scalarSrc.set(src); |
| 1238 | SkColor color = bitmap.getColor(fMouseX, fMouseY); |
| 1239 | if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) { |
| 1240 | SkRect pixel; |
| 1241 | matrix.mapRect(&pixel, originalPixel); |
| 1242 | // TODO Perhaps measure the values and make the outline white if it's "dark" |
| 1243 | if (color == SK_ColorBLACK) { |
| 1244 | paint.setColor(SK_ColorWHITE); |
| 1245 | } |
| 1246 | canvas->drawRect(pixel, paint); |
| 1247 | } |
| 1248 | paint.setColor(SK_ColorBLACK); |
| 1249 | // Draw a border around the destination rectangle |
| 1250 | canvas->drawRect(dest, paint); |
| 1251 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
| 1252 | // Identify the pixel and its color on screen |
| 1253 | paint.setTypeface(fTypeface); |
| 1254 | paint.setAntiAlias(true); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1255 | SkScalar lineHeight = paint.getFontMetrics(nullptr); |
reed | 5f629b4 | 2015-08-15 11:57:23 -0700 | [diff] [blame] | 1256 | SkString string; |
| 1257 | string.appendf("(%i, %i)", fMouseX, fMouseY); |
| 1258 | SkScalar left = dest.fLeft + SkIntToScalar(3); |
| 1259 | SkScalar i = SK_Scalar1; |
| 1260 | drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint); |
| 1261 | // Alpha |
| 1262 | i += SK_Scalar1; |
| 1263 | string.reset(); |
| 1264 | string.appendf("A: %X", SkColorGetA(color)); |
| 1265 | drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint); |
| 1266 | // Red |
| 1267 | i += SK_Scalar1; |
| 1268 | string.reset(); |
| 1269 | string.appendf("R: %X", SkColorGetR(color)); |
| 1270 | paint.setColor(SK_ColorRED); |
| 1271 | drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint); |
| 1272 | // Green |
| 1273 | i += SK_Scalar1; |
| 1274 | string.reset(); |
| 1275 | string.appendf("G: %X", SkColorGetG(color)); |
| 1276 | paint.setColor(SK_ColorGREEN); |
| 1277 | drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint); |
| 1278 | // Blue |
| 1279 | i += SK_Scalar1; |
| 1280 | string.reset(); |
| 1281 | string.appendf("B: %X", SkColorGetB(color)); |
| 1282 | paint.setColor(SK_ColorBLUE); |
| 1283 | drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint); |
| 1284 | canvas->restoreToCount(count); |
reed@android.com | e522ca5 | 2009-11-23 20:10:41 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1287 | void SampleWindow::onDraw(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | #include "SkColorPriv.h" |
| 1291 | |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 1292 | void SampleWindow::saveToPdf() |
| 1293 | { |
halcanary | 8ee06f2 | 2015-08-11 10:30:12 -0700 | [diff] [blame] | 1294 | #if SK_SUPPORT_PDF |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 1295 | fSaveToPdf = true; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1296 | this->inval(nullptr); |
halcanary | 8ee06f2 | 2015-08-11 10:30:12 -0700 | [diff] [blame] | 1297 | #endif // SK_SUPPORT_PDF |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1300 | SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) { |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 1301 | if (fSaveToPdf) { |
reed | ddb5eca | 2014-10-08 11:10:51 -0700 | [diff] [blame] | 1302 | SkString name; |
| 1303 | if (!this->getRawTitle(&name)) { |
| 1304 | name.set("unknown_sample"); |
| 1305 | } |
| 1306 | name.append(".pdf"); |
| 1307 | #ifdef SK_BUILD_FOR_ANDROID |
| 1308 | name.prepend("/sdcard/"); |
| 1309 | #endif |
| 1310 | fPDFDocument.reset(SkDocument::CreatePDF(name.c_str())); |
| 1311 | canvas = fPDFDocument->beginPage(this->width(), this->height()); |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 1312 | } else if (fSaveToSKP) { |
| 1313 | canvas = fRecorder.beginRecording(9999, 9999, nullptr, 0); |
reed | 91b4dfb | 2016-01-08 08:23:19 -0800 | [diff] [blame] | 1314 | } else if (fUsePicture) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1315 | canvas = fRecorder.beginRecording(9999, 9999, nullptr, 0); |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 1316 | } else { |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 1317 | canvas = this->INHERITED::beforeChildren(canvas); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | if (fUseClip) { |
| 1321 | canvas->drawColor(0xFFFF88FF); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1322 | canvas->clipPath(fClipPath, SkRegion::kIntersect_Op, true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
fmalita | 822ace9 | 2015-06-15 07:07:32 -0700 | [diff] [blame] | 1325 | // Install a flags filter proxy canvas if needed |
| 1326 | if (fLCDState != SkOSMenu::kMixedState || |
| 1327 | fAAState != SkOSMenu::kMixedState || |
| 1328 | fSubpixelState != SkOSMenu::kMixedState || |
| 1329 | fHintingState > 0 || |
| 1330 | fFilterQualityIndex > 0) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1331 | canvas = new FlagsFilterCanvas(canvas, fLCDState, fAAState, fSubpixelState, fHintingState, |
| 1332 | fFilterQualityIndex); |
fmalita | 822ace9 | 2015-06-15 07:07:32 -0700 | [diff] [blame] | 1333 | fFlagsFilterCanvas.reset(canvas); |
| 1334 | } |
| 1335 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1336 | return canvas; |
| 1337 | } |
reed | 89889b6 | 2014-10-29 12:36:45 -0700 | [diff] [blame] | 1338 | #include "SkMultiPictureDraw.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1339 | void SampleWindow::afterChildren(SkCanvas* orig) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1340 | fFlagsFilterCanvas.reset(nullptr); |
fmalita | 822ace9 | 2015-06-15 07:07:32 -0700 | [diff] [blame] | 1341 | |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 1342 | if (fSaveToPdf) { |
| 1343 | fSaveToPdf = false; |
reed | ddb5eca | 2014-10-08 11:10:51 -0700 | [diff] [blame] | 1344 | fPDFDocument->endPage(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1345 | fPDFDocument.reset(nullptr); |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 1346 | // We took over the draw calls in order to create the PDF, so we need |
| 1347 | // to redraw. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1348 | this->inval(nullptr); |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 1349 | return; |
| 1350 | } |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1351 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1352 | if (fRequestGrabImage) { |
| 1353 | fRequestGrabImage = false; |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1354 | |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 1355 | SkBitmap bmp = capture_bitmap(orig); |
| 1356 | if (!bmp.isNull()) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1357 | static int gSampleGrabCounter; |
| 1358 | SkString name; |
reed@google.com | 7c57e0e | 2012-09-08 21:20:33 +0000 | [diff] [blame] | 1359 | name.printf("sample_grab_%d.png", gSampleGrabCounter++); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 1360 | SkImageEncoder::EncodeFile(name.c_str(), bmp, |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1361 | SkImageEncoder::kPNG_Type, 100); |
| 1362 | } |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 1363 | this->inval(nullptr); |
| 1364 | return; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 1367 | if (fSaveToSKP) { |
robertphillips | dd52896 | 2014-07-13 07:55:53 -0700 | [diff] [blame] | 1368 | SkAutoTUnref<const SkPicture> picture(fRecorder.endRecording()); |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 1369 | SkFILEWStream stream("sample_app.skp"); |
| 1370 | picture->serialize(&stream); |
| 1371 | fSaveToSKP = false; |
| 1372 | this->inval(nullptr); |
| 1373 | return; |
| 1374 | } |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1375 | |
reed | 91b4dfb | 2016-01-08 08:23:19 -0800 | [diff] [blame] | 1376 | if (fUsePicture) { |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 1377 | SkAutoTUnref<const SkPicture> picture(fRecorder.endRecording()); |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 1378 | |
reed | 91b4dfb | 2016-01-08 08:23:19 -0800 | [diff] [blame] | 1379 | // serialize/deserialize? |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 1380 | if (false) { |
| 1381 | SkDynamicMemoryWStream wstream; |
| 1382 | picture->serialize(&wstream); |
| 1383 | |
| 1384 | SkAutoTDelete<SkStream> rstream(wstream.detachAsStream()); |
| 1385 | picture.reset(SkPicture::CreateFromStream(rstream)); |
| 1386 | } |
reed | 91b4dfb | 2016-01-08 08:23:19 -0800 | [diff] [blame] | 1387 | orig->drawPicture(picture); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1388 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1389 | |
reed@google.com | 17d7aec | 2011-04-25 14:31:44 +0000 | [diff] [blame] | 1390 | // Do this after presentGL and other finishing, rather than in afterChild |
jvanverth | 4d0d81a | 2015-08-11 11:07:07 -0700 | [diff] [blame] | 1391 | if (fMeasureFPS) { |
reed | b2ce0b0 | 2015-11-09 13:10:30 -0800 | [diff] [blame] | 1392 | orig->flush(); |
jvanverth | 4d0d81a | 2015-08-11 11:07:07 -0700 | [diff] [blame] | 1393 | fTimer.end(); |
| 1394 | fMeasureFPS_Time += fTimer.fWall; |
reed@google.com | 17d7aec | 2011-04-25 14:31:44 +0000 | [diff] [blame] | 1395 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 1398 | void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) { |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 1399 | if (fRotate) { |
| 1400 | SkScalar cx = this->width() / 2; |
| 1401 | SkScalar cy = this->height() / 2; |
| 1402 | canvas->translate(cx, cy); |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 1403 | canvas->rotate(gAnimTimer.scaled(10)); |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 1404 | canvas->translate(-cx, -cy); |
| 1405 | } |
bungeman@google.com | b178591 | 2013-07-09 21:58:56 +0000 | [diff] [blame] | 1406 | |
bsalomon@google.com | e8f0910 | 2011-09-08 18:48:12 +0000 | [diff] [blame] | 1407 | if (fPerspAnim) { |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 1408 | SkScalar secs = gAnimTimer.scaled(1); |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 1409 | |
| 1410 | static const SkScalar gAnimPeriod = 10 * SK_Scalar1; |
| 1411 | static const SkScalar gAnimMag = SK_Scalar1 / 1000; |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 1412 | SkScalar t = SkScalarMod(secs, gAnimPeriod); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 1413 | if (SkScalarFloorToInt(secs / gAnimPeriod) & 0x1) { |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 1414 | t = gAnimPeriod - t; |
| 1415 | } |
| 1416 | t = 2 * t - gAnimPeriod; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 1417 | t *= gAnimMag / gAnimPeriod; |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 1418 | SkMatrix m; |
| 1419 | m.reset(); |
reed | 5d9ab28 | 2014-09-19 13:04:17 -0700 | [diff] [blame] | 1420 | #if 1 |
bsalomon@google.com | 6fb736f | 2011-09-16 18:51:57 +0000 | [diff] [blame] | 1421 | m.setPerspY(t); |
reed | 5d9ab28 | 2014-09-19 13:04:17 -0700 | [diff] [blame] | 1422 | #else |
| 1423 | m.setPerspY(SK_Scalar1 / 1000); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 1424 | m.setSkewX(8.0f / 25); |
reed | 5d9ab28 | 2014-09-19 13:04:17 -0700 | [diff] [blame] | 1425 | m.dump(); |
| 1426 | #endif |
bsalomon@google.com | e8f0910 | 2011-09-08 18:48:12 +0000 | [diff] [blame] | 1427 | canvas->concat(m); |
| 1428 | } |
reed@google.com | f0b5f68 | 2011-03-11 20:08:25 +0000 | [diff] [blame] | 1429 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1430 | if (fMeasureFPS) { |
jvanverth | 4d0d81a | 2015-08-11 11:07:07 -0700 | [diff] [blame] | 1431 | (void)SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT); |
| 1432 | fTimer.start(); |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 1433 | } else { |
| 1434 | (void)SampleView::SetRepeatDraw(child, 1); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1435 | } |
bungeman@google.com | b178591 | 2013-07-09 21:58:56 +0000 | [diff] [blame] | 1436 | if (fPerspAnim || fRotate) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1437 | this->inval(nullptr); |
bsalomon@google.com | e8f0910 | 2011-09-08 18:48:12 +0000 | [diff] [blame] | 1438 | } |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 1441 | void SampleWindow::changeZoomLevel(float delta) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 1442 | fZoomLevel += delta; |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1443 | if (fZoomLevel > 0) { |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 1444 | fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL); |
| 1445 | fZoomScale = fZoomLevel + SK_Scalar1; |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1446 | } else if (fZoomLevel < 0) { |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 1447 | fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL); |
| 1448 | fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1449 | } else { |
| 1450 | fZoomScale = SK_Scalar1; |
| 1451 | } |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 1452 | this->updateMatrix(); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 1455 | void SampleWindow::updateMatrix(){ |
| 1456 | SkMatrix m; |
| 1457 | m.reset(); |
| 1458 | if (fZoomLevel) { |
| 1459 | SkPoint center; |
| 1460 | //m = this->getLocalMatrix();//.invert(&m); |
| 1461 | m.mapXY(fZoomCenterX, fZoomCenterY, ¢er); |
| 1462 | SkScalar cx = center.fX; |
| 1463 | SkScalar cy = center.fY; |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1464 | |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 1465 | m.setTranslate(-cx, -cy); |
| 1466 | m.postScale(fZoomScale, fZoomScale); |
| 1467 | m.postTranslate(cx, cy); |
| 1468 | } |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1469 | |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 1470 | if (fFlipAxis) { |
| 1471 | m.preTranslate(fZoomCenterX, fZoomCenterY); |
| 1472 | if (fFlipAxis & kFlipAxis_X) { |
| 1473 | m.preScale(-SK_Scalar1, SK_Scalar1); |
| 1474 | } |
| 1475 | if (fFlipAxis & kFlipAxis_Y) { |
| 1476 | m.preScale(SK_Scalar1, -SK_Scalar1); |
| 1477 | } |
| 1478 | m.preTranslate(-fZoomCenterX, -fZoomCenterY); |
| 1479 | //canvas->concat(m); |
| 1480 | } |
| 1481 | // Apply any gesture matrix |
| 1482 | m.preConcat(fGesture.localM()); |
| 1483 | m.preConcat(fGesture.globalM()); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1484 | |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 1485 | this->setLocalMatrix(m); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1486 | |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 1487 | this->updateTitle(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1488 | this->inval(nullptr); |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 1489 | } |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1490 | bool SampleWindow::previousSample() { |
Scroggo | 62b65b0 | 2011-06-21 16:01:26 +0000 | [diff] [blame] | 1491 | fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count(); |
reed | 63121f0 | 2015-01-13 19:43:04 -0800 | [diff] [blame] | 1492 | this->loadView((*fSamples[fCurrIndex])()); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1493 | return true; |
| 1494 | } |
| 1495 | |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 1496 | #include "SkResourceCache.h" |
| 1497 | #include "SkGlyphCache.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1498 | bool SampleWindow::nextSample() { |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 1499 | fCurrIndex = (fCurrIndex + 1) % fSamples.count(); |
reed | 63121f0 | 2015-01-13 19:43:04 -0800 | [diff] [blame] | 1500 | this->loadView((*fSamples[fCurrIndex])()); |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 1501 | |
| 1502 | if (false) { |
| 1503 | SkResourceCache::TestDumpMemoryStatistics(); |
| 1504 | SkGlyphCache::Dump(); |
| 1505 | SkDebugf("\n"); |
| 1506 | } |
| 1507 | |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 1508 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame] | 1511 | bool SampleWindow::goToSample(int i) { |
| 1512 | fCurrIndex = (i) % fSamples.count(); |
reed | 63121f0 | 2015-01-13 19:43:04 -0800 | [diff] [blame] | 1513 | this->loadView((*fSamples[fCurrIndex])()); |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame] | 1514 | return true; |
| 1515 | } |
| 1516 | |
| 1517 | SkString SampleWindow::getSampleTitle(int i) { |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 1518 | return ::getSampleTitle(fSamples[i]); |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | int SampleWindow::sampleCount() { |
| 1522 | return fSamples.count(); |
| 1523 | } |
| 1524 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1525 | void SampleWindow::showOverview() { |
reed | 63121f0 | 2015-01-13 19:43:04 -0800 | [diff] [blame] | 1526 | this->loadView(create_overview(fSamples.count(), fSamples.begin())); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1529 | void SampleWindow::postAnimatingEvent() { |
| 1530 | if (fAnimating) { |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 1531 | (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1532 | } |
| 1533 | } |
reed@google.com | e23f194 | 2011-12-08 19:36:00 +0000 | [diff] [blame] | 1534 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1535 | bool SampleWindow::onEvent(const SkEvent& evt) { |
reed@google.com | 2072db8 | 2011-11-08 15:18:10 +0000 | [diff] [blame] | 1536 | if (evt.isType(gUpdateWindowTitleEvtName)) { |
| 1537 | this->updateTitle(); |
| 1538 | return true; |
| 1539 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1540 | if (evt.isType(ANIMATING_EVENTTYPE)) { |
| 1541 | if (fAnimating) { |
| 1542 | this->nextSample(); |
| 1543 | this->postAnimatingEvent(); |
| 1544 | } |
| 1545 | return true; |
| 1546 | } |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 1547 | if (evt.isType("set-curr-index")) { |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1548 | this->goToSample(evt.getFast32()); |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 1549 | return true; |
| 1550 | } |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1551 | if (isInvalEvent(evt)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1552 | this->inval(nullptr); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1553 | return true; |
| 1554 | } |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1555 | int selected = -1; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 1556 | if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) { |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1557 | this->setDeviceType((DeviceType)selected); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1558 | return true; |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1559 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1560 | if (SkOSMenu::FindSwitchState(evt, "Slide Show", nullptr)) { |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1561 | this->toggleSlideshow(); |
| 1562 | return true; |
| 1563 | } |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 1564 | if (SkOSMenu::FindTriState(evt, "AA", &fAAState) || |
| 1565 | SkOSMenu::FindTriState(evt, "LCD", &fLCDState) || |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 1566 | SkOSMenu::FindListIndex(evt, "FilterQuality", &fFilterQualityIndex) || |
bungeman@google.com | 96aabc8 | 2013-06-03 21:26:34 +0000 | [diff] [blame] | 1567 | SkOSMenu::FindTriState(evt, "Subpixel", &fSubpixelState) || |
| 1568 | SkOSMenu::FindListIndex(evt, "Hinting", &fHintingState) || |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 1569 | SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) || |
| 1570 | SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) || |
reed | 093b4e8 | 2015-08-04 18:46:38 -0700 | [diff] [blame] | 1571 | SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify)) |
| 1572 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1573 | this->inval(nullptr); |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 1574 | this->updateTitle(); |
| 1575 | return true; |
| 1576 | } |
| 1577 | if (SkOSMenu::FindListIndex(evt, "Tiling", &fTilingMode)) { |
| 1578 | if (SampleView::IsSampleView(curr_view(this))) { |
| 1579 | ((SampleView*)curr_view(this))->onTileSizeChanged(this->tileSize()); |
| 1580 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1581 | this->inval(nullptr); |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1582 | this->updateTitle(); |
| 1583 | return true; |
| 1584 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1585 | if (SkOSMenu::FindSwitchState(evt, "Flip X", nullptr)) { |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1586 | fFlipAxis ^= kFlipAxis_X; |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 1587 | this->updateMatrix(); |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1588 | return true; |
| 1589 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1590 | if (SkOSMenu::FindSwitchState(evt, "Flip Y", nullptr)) { |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1591 | fFlipAxis ^= kFlipAxis_Y; |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 1592 | this->updateMatrix(); |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1593 | return true; |
| 1594 | } |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 1595 | if (SkOSMenu::FindAction(evt,"Save to PDF")) { |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1596 | this->saveToPdf(); |
| 1597 | return true; |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1598 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1599 | return this->INHERITED::onEvent(evt); |
| 1600 | } |
| 1601 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1602 | bool SampleWindow::onQuery(SkEvent* query) { |
| 1603 | if (query->isType("get-slide-count")) { |
| 1604 | query->setFast32(fSamples.count()); |
| 1605 | return true; |
| 1606 | } |
| 1607 | if (query->isType("get-slide-title")) { |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 1608 | SkView* view = (*fSamples[query->getFast32()])(); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1609 | SkEvent evt(gTitleEvtName); |
| 1610 | if (view->doQuery(&evt)) { |
| 1611 | query->setString("title", evt.findString(gTitleEvtName)); |
| 1612 | } |
| 1613 | SkSafeUnref(view); |
| 1614 | return true; |
| 1615 | } |
| 1616 | if (query->isType("use-fast-text")) { |
| 1617 | SkEvent evt(gFastTextEvtName); |
| 1618 | return curr_view(this)->doQuery(&evt); |
| 1619 | } |
reed@google.com | 29038ed | 2011-07-06 17:56:47 +0000 | [diff] [blame] | 1620 | if (query->isType("ignore-window-bitmap")) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1621 | query->setFast32(this->getGrContext() != nullptr); |
reed@google.com | 29038ed | 2011-07-06 17:56:47 +0000 | [diff] [blame] | 1622 | return true; |
| 1623 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1624 | return this->INHERITED::onQuery(query); |
| 1625 | } |
| 1626 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 1627 | DECLARE_bool(portableFonts); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 1628 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1629 | bool SampleWindow::onHandleChar(SkUnichar uni) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1630 | { |
| 1631 | SkView* view = curr_view(this); |
| 1632 | if (view) { |
| 1633 | SkEvent evt(gCharEvtName); |
| 1634 | evt.setFast32(uni); |
| 1635 | if (view->doQuery(&evt)) { |
| 1636 | return true; |
| 1637 | } |
| 1638 | } |
| 1639 | } |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1640 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1641 | int dx = 0xFF; |
| 1642 | int dy = 0xFF; |
| 1643 | |
| 1644 | switch (uni) { |
| 1645 | case '5': dx = 0; dy = 0; break; |
| 1646 | case '8': dx = 0; dy = -1; break; |
| 1647 | case '6': dx = 1; dy = 0; break; |
| 1648 | case '2': dx = 0; dy = 1; break; |
| 1649 | case '4': dx = -1; dy = 0; break; |
| 1650 | case '7': dx = -1; dy = -1; break; |
| 1651 | case '9': dx = 1; dy = -1; break; |
| 1652 | case '3': dx = 1; dy = 1; break; |
| 1653 | case '1': dx = -1; dy = 1; break; |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1654 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1655 | default: |
| 1656 | break; |
| 1657 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1658 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1659 | if (0xFF != dx && 0xFF != dy) { |
| 1660 | if ((dx | dy) == 0) { |
| 1661 | fScrollTestX = fScrollTestY = 0; |
| 1662 | } else { |
| 1663 | fScrollTestX += dx; |
| 1664 | fScrollTestY += dy; |
| 1665 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1666 | this->inval(nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1667 | return true; |
| 1668 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1669 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 1670 | switch (uni) { |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 1671 | case 27: // ESC |
| 1672 | gAnimTimer.stop(); |
| 1673 | if (this->sendAnimatePulse()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1674 | this->inval(nullptr); |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 1675 | } |
| 1676 | break; |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 1677 | case ' ': |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 1678 | gAnimTimer.togglePauseResume(); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 1679 | if (this->sendAnimatePulse()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1680 | this->inval(nullptr); |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame] | 1681 | } |
| 1682 | break; |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 1683 | case 'B': |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1684 | post_event_to_sink(new SkEvent("PictFileView::toggleBBox"), curr_view(this)); |
commit-bot@chromium.org | 6de4a78 | 2013-12-11 17:05:23 +0000 | [diff] [blame] | 1685 | // Cannot call updateTitle() synchronously, because the toggleBBox event is still in |
| 1686 | // the queue. |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1687 | post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1688 | this->inval(nullptr); |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 1689 | break; |
reed | 4302ae9 | 2014-10-06 12:29:56 -0700 | [diff] [blame] | 1690 | case 'D': |
| 1691 | toggleDistanceFieldFonts(); |
| 1692 | break; |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 1693 | case 'f': |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1694 | // only |
djsollen@google.com | 6ff8255 | 2011-11-07 15:43:57 +0000 | [diff] [blame] | 1695 | toggleFPS(); |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 1696 | break; |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 1697 | case 'F': |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 1698 | FLAGS_portableFonts ^= true; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1699 | this->inval(nullptr); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 1700 | break; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1701 | case 'g': |
| 1702 | fRequestGrabImage = true; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1703 | this->inval(nullptr); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1704 | break; |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 1705 | case 'G': |
| 1706 | gShowGMBounds = !gShowGMBounds; |
commit-bot@chromium.org | 6de4a78 | 2013-12-11 17:05:23 +0000 | [diff] [blame] | 1707 | post_event_to_sink(GMSampleView::NewShowSizeEvt(gShowGMBounds), |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 1708 | curr_view(this)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1709 | this->inval(nullptr); |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 1710 | break; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1711 | case 'i': |
| 1712 | this->zoomIn(); |
| 1713 | break; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 1714 | case 'o': |
| 1715 | this->zoomOut(); |
| 1716 | break; |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 1717 | case 'r': |
| 1718 | fRotate = !fRotate; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1719 | this->inval(nullptr); |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 1720 | this->updateTitle(); |
| 1721 | return true; |
bsalomon@google.com | e8f0910 | 2011-09-08 18:48:12 +0000 | [diff] [blame] | 1722 | case 'k': |
| 1723 | fPerspAnim = !fPerspAnim; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1724 | this->inval(nullptr); |
bsalomon@google.com | e8f0910 | 2011-09-08 18:48:12 +0000 | [diff] [blame] | 1725 | this->updateTitle(); |
| 1726 | return true; |
reed | 093b4e8 | 2015-08-04 18:46:38 -0700 | [diff] [blame] | 1727 | case 'K': |
| 1728 | fSaveToSKP = true; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1729 | this->inval(nullptr); |
reed | 093b4e8 | 2015-08-04 18:46:38 -0700 | [diff] [blame] | 1730 | return true; |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 1731 | case 'M': |
reed | 91b4dfb | 2016-01-08 08:23:19 -0800 | [diff] [blame] | 1732 | fUsePicture = !fUsePicture; |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 1733 | this->inval(nullptr); |
| 1734 | this->updateTitle(); |
| 1735 | return true; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1736 | #if SK_SUPPORT_GPU |
twiz@google.com | 05e7024 | 2012-01-27 19:12:00 +0000 | [diff] [blame] | 1737 | case 'p': |
| 1738 | { |
| 1739 | GrContext* grContext = this->getGrContext(); |
| 1740 | if (grContext) { |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 1741 | size_t cacheBytes; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1742 | grContext->getResourceCacheUsage(nullptr, &cacheBytes); |
twiz@google.com | 05e7024 | 2012-01-27 19:12:00 +0000 | [diff] [blame] | 1743 | grContext->freeGpuResources(); |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 1744 | SkDebugf("Purged %d bytes from the GPU resource cache.\n", cacheBytes); |
twiz@google.com | 05e7024 | 2012-01-27 19:12:00 +0000 | [diff] [blame] | 1745 | } |
| 1746 | } |
| 1747 | return true; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1748 | #endif |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 1749 | default: |
| 1750 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1751 | } |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1752 | |
scroggo@google.com | 7dadc74 | 2012-04-18 14:07:57 +0000 | [diff] [blame] | 1753 | if (fAppMenu->handleKeyEquivalent(uni)|| fSlideMenu->handleKeyEquivalent(uni)) { |
| 1754 | this->onUpdateMenu(fAppMenu); |
| 1755 | this->onUpdateMenu(fSlideMenu); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1756 | return true; |
| 1757 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1758 | return this->INHERITED::onHandleChar(uni); |
| 1759 | } |
| 1760 | |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1761 | void SampleWindow::setDeviceType(DeviceType type) { |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 1762 | if (type == fDeviceType) |
| 1763 | return; |
| 1764 | |
| 1765 | fDevManager->tearDownBackend(this); |
| 1766 | |
| 1767 | fDeviceType = type; |
| 1768 | |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 1769 | fDevManager->setUpBackend(this, fMSAASampleCount); |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 1770 | |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1771 | this->updateTitle(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1772 | this->inval(nullptr); |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1775 | void SampleWindow::toggleSlideshow() { |
| 1776 | fAnimating = !fAnimating; |
| 1777 | this->postAnimatingEvent(); |
| 1778 | this->updateTitle(); |
| 1779 | } |
| 1780 | |
| 1781 | void SampleWindow::toggleRendering() { |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 1782 | this->setDeviceType(cycle_devicetype(fDeviceType)); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1783 | this->updateTitle(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1784 | this->inval(nullptr); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
djsollen@google.com | 6ff8255 | 2011-11-07 15:43:57 +0000 | [diff] [blame] | 1787 | void SampleWindow::toggleFPS() { |
| 1788 | fMeasureFPS = !fMeasureFPS; |
| 1789 | this->updateTitle(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1790 | this->inval(nullptr); |
djsollen@google.com | 6ff8255 | 2011-11-07 15:43:57 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
reed | 4302ae9 | 2014-10-06 12:29:56 -0700 | [diff] [blame] | 1793 | void SampleWindow::toggleDistanceFieldFonts() { |
jvanverth | 629162d | 2015-11-08 08:07:24 -0800 | [diff] [blame] | 1794 | // reset backend |
| 1795 | fDevManager->tearDownBackend(this); |
| 1796 | fDevManager->setUpBackend(this, fMSAASampleCount); |
| 1797 | |
reed | 4302ae9 | 2014-10-06 12:29:56 -0700 | [diff] [blame] | 1798 | SkSurfaceProps props = this->getSurfaceProps(); |
bsalomon | afcd7cd | 2015-08-31 12:39:41 -0700 | [diff] [blame] | 1799 | uint32_t flags = props.flags() ^ SkSurfaceProps::kUseDeviceIndependentFonts_Flag; |
reed | 4302ae9 | 2014-10-06 12:29:56 -0700 | [diff] [blame] | 1800 | this->setSurfaceProps(SkSurfaceProps(flags, props.pixelGeometry())); |
| 1801 | |
| 1802 | this->updateTitle(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1803 | this->inval(nullptr); |
reed | 4302ae9 | 2014-10-06 12:29:56 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1806 | #include "SkDumpCanvas.h" |
| 1807 | |
| 1808 | bool SampleWindow::onHandleKey(SkKey key) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1809 | { |
| 1810 | SkView* view = curr_view(this); |
| 1811 | if (view) { |
| 1812 | SkEvent evt(gKeyEvtName); |
| 1813 | evt.setFast32(key); |
| 1814 | if (view->doQuery(&evt)) { |
| 1815 | return true; |
| 1816 | } |
| 1817 | } |
| 1818 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1819 | switch (key) { |
| 1820 | case kRight_SkKey: |
| 1821 | if (this->nextSample()) { |
| 1822 | return true; |
| 1823 | } |
| 1824 | break; |
| 1825 | case kLeft_SkKey: |
scroggo@google.com | e2dd973 | 2012-08-15 20:03:06 +0000 | [diff] [blame] | 1826 | if (this->previousSample()) { |
| 1827 | return true; |
| 1828 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1829 | return true; |
| 1830 | case kUp_SkKey: |
reed | f7f79d2 | 2015-02-21 09:03:20 -0800 | [diff] [blame] | 1831 | this->changeZoomLevel(1.f / 32.f); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1832 | return true; |
| 1833 | case kDown_SkKey: |
reed | f7f79d2 | 2015-02-21 09:03:20 -0800 | [diff] [blame] | 1834 | this->changeZoomLevel(-1.f / 32.f); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1835 | return true; |
mike@reedtribe.org | 6f6e8c3 | 2011-12-27 22:33:50 +0000 | [diff] [blame] | 1836 | case kOK_SkKey: { |
| 1837 | SkString title; |
| 1838 | if (curr_title(this, &title)) { |
| 1839 | writeTitleToPrefs(title.c_str()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1840 | } |
| 1841 | return true; |
mike@reedtribe.org | 6f6e8c3 | 2011-12-27 22:33:50 +0000 | [diff] [blame] | 1842 | } |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 1843 | case kBack_SkKey: |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1844 | this->showOverview(); |
reed@android.com | 34245c7 | 2009-11-03 04:00:48 +0000 | [diff] [blame] | 1845 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1846 | default: |
| 1847 | break; |
| 1848 | } |
| 1849 | return this->INHERITED::onHandleKey(key); |
| 1850 | } |
| 1851 | |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1852 | /////////////////////////////////////////////////////////////////////////////// |
| 1853 | |
| 1854 | static const char gGestureClickType[] = "GestureClickType"; |
| 1855 | |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 1856 | bool SampleWindow::onDispatchClick(int x, int y, Click::State state, |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 1857 | void* owner, unsigned modi) { |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 1858 | if (Click::kMoved_State == state) { |
| 1859 | updatePointer(x, y); |
| 1860 | } |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 1861 | int w = SkScalarRoundToInt(this->width()); |
| 1862 | int h = SkScalarRoundToInt(this->height()); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1863 | |
| 1864 | // check for the resize-box |
| 1865 | if (w - x < 16 && h - y < 16) { |
| 1866 | return false; // let the OS handle the click |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1867 | } |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1868 | else if (fMagnify) { |
| 1869 | //it's only necessary to update the drawing if there's a click |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1870 | this->inval(nullptr); |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1871 | return false; //prevent dragging while magnify is enabled |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 1872 | } else { |
| 1873 | // capture control+option, and trigger debugger |
| 1874 | if ((modi & kControl_SkModifierKey) && (modi & kOption_SkModifierKey)) { |
| 1875 | if (Click::kDown_State == state) { |
| 1876 | SkEvent evt("debug-hit-test"); |
| 1877 | evt.setS32("debug-hit-test-x", x); |
| 1878 | evt.setS32("debug-hit-test-y", y); |
| 1879 | curr_view(this)->doEvent(evt); |
| 1880 | } |
| 1881 | return true; |
| 1882 | } else { |
| 1883 | return this->INHERITED::onDispatchClick(x, y, state, owner, modi); |
| 1884 | } |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 1885 | } |
| 1886 | } |
| 1887 | |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1888 | class GestureClick : public SkView::Click { |
| 1889 | public: |
| 1890 | GestureClick(SkView* target) : SkView::Click(target) { |
| 1891 | this->setType(gGestureClickType); |
| 1892 | } |
| 1893 | |
| 1894 | static bool IsGesture(Click* click) { |
| 1895 | return click->isType(gGestureClickType); |
| 1896 | } |
| 1897 | }; |
| 1898 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 1899 | SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y, |
| 1900 | unsigned modi) { |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1901 | return new GestureClick(this); |
| 1902 | } |
| 1903 | |
| 1904 | bool SampleWindow::onClick(Click* click) { |
| 1905 | if (GestureClick::IsGesture(click)) { |
bsalomon@google.com | 4d4f281 | 2011-12-12 18:34:01 +0000 | [diff] [blame] | 1906 | float x = static_cast<float>(click->fICurr.fX); |
| 1907 | float y = static_cast<float>(click->fICurr.fY); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1908 | |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1909 | switch (click->fState) { |
| 1910 | case SkView::Click::kDown_State: |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 1911 | fGesture.touchBegin(click->fOwner, x, y); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1912 | break; |
| 1913 | case SkView::Click::kMoved_State: |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 1914 | fGesture.touchMoved(click->fOwner, x, y); |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 1915 | this->updateMatrix(); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1916 | break; |
| 1917 | case SkView::Click::kUp_State: |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 1918 | fGesture.touchEnd(click->fOwner); |
reed@google.com | f03bb56 | 2011-11-11 21:42:12 +0000 | [diff] [blame] | 1919 | this->updateMatrix(); |
reed@google.com | 52f57e1 | 2011-03-16 12:10:02 +0000 | [diff] [blame] | 1920 | break; |
| 1921 | } |
| 1922 | return true; |
| 1923 | } |
| 1924 | return false; |
| 1925 | } |
| 1926 | |
| 1927 | /////////////////////////////////////////////////////////////////////////////// |
| 1928 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1929 | void SampleWindow::loadView(SkView* view) { |
| 1930 | SkView::F2BIter iter(this); |
| 1931 | SkView* prev = iter.next(); |
| 1932 | if (prev) { |
| 1933 | prev->detachFromParent(); |
| 1934 | } |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 1935 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1936 | view->setVisibleP(true); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1937 | view->setClipToBounds(false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1938 | this->attachChildToFront(view)->unref(); |
| 1939 | view->setSize(this->width(), this->height()); |
| 1940 | |
yangsu@google.com | 921091f | 2011-08-02 13:39:12 +0000 | [diff] [blame] | 1941 | //repopulate the slide menu when a view is loaded |
scroggo@google.com | 7dadc74 | 2012-04-18 14:07:57 +0000 | [diff] [blame] | 1942 | fSlideMenu->reset(); |
djsollen@google.com | 388974f | 2013-01-17 13:20:01 +0000 | [diff] [blame] | 1943 | |
scroggo@google.com | 7dadc74 | 2012-04-18 14:07:57 +0000 | [diff] [blame] | 1944 | this->onUpdateMenu(fSlideMenu); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1945 | this->updateTitle(); |
| 1946 | } |
| 1947 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 1948 | static const char* gDeviceTypePrefix[] = { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1949 | "raster: ", |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1950 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 7491372 | 2011-10-27 20:44:19 +0000 | [diff] [blame] | 1951 | "opengl: ", |
robertphillips@google.com | b442a6d | 2012-04-02 19:24:21 +0000 | [diff] [blame] | 1952 | #if SK_ANGLE |
| 1953 | "angle: ", |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1954 | #endif // SK_ANGLE |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 1955 | #if SK_COMMAND_BUFFER |
| 1956 | "command buffer: ", |
| 1957 | #endif // SK_COMMAND_BUFFER |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 1958 | #endif // SK_SUPPORT_GPU |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1959 | }; |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 1960 | static_assert(SK_ARRAY_COUNT(gDeviceTypePrefix) == SampleWindow::kDeviceTypeCnt, |
| 1961 | "array_size_mismatch"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1962 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1963 | static const char* trystate_str(SkOSMenu::TriState state, |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 1964 | const char trueStr[], const char falseStr[]) { |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1965 | if (SkOSMenu::kOnState == state) { |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 1966 | return trueStr; |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 1967 | } else if (SkOSMenu::kOffState == state) { |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 1968 | return falseStr; |
| 1969 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1970 | return nullptr; |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
reed | ddb5eca | 2014-10-08 11:10:51 -0700 | [diff] [blame] | 1973 | bool SampleWindow::getRawTitle(SkString* title) { |
| 1974 | return curr_title(this, title); |
| 1975 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1976 | |
reed | ddb5eca | 2014-10-08 11:10:51 -0700 | [diff] [blame] | 1977 | void SampleWindow::updateTitle() { |
mike@reedtribe.org | 6f6e8c3 | 2011-12-27 22:33:50 +0000 | [diff] [blame] | 1978 | SkString title; |
reed | ddb5eca | 2014-10-08 11:10:51 -0700 | [diff] [blame] | 1979 | if (!this->getRawTitle(&title)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1980 | title.set("<unknown>"); |
| 1981 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 1982 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 1983 | title.prepend(gDeviceTypePrefix[fDeviceType]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1984 | |
commit-bot@chromium.org | cc63b32 | 2013-12-06 20:14:55 +0000 | [diff] [blame] | 1985 | if (fTilingMode != kNo_Tiling) { |
| 1986 | title.prependf("<T: %s> ", gTilingInfo[fTilingMode].label); |
| 1987 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1988 | if (fAnimating) { |
| 1989 | title.prepend("<A> "); |
| 1990 | } |
reed@android.com | 6c5f6f2 | 2009-08-14 16:08:38 +0000 | [diff] [blame] | 1991 | if (fRotate) { |
| 1992 | title.prepend("<R> "); |
| 1993 | } |
bsalomon@google.com | e8f0910 | 2011-09-08 18:48:12 +0000 | [diff] [blame] | 1994 | if (fPerspAnim) { |
| 1995 | title.prepend("<K> "); |
| 1996 | } |
bsalomon | afcd7cd | 2015-08-31 12:39:41 -0700 | [diff] [blame] | 1997 | if (this->getSurfaceProps().flags() & SkSurfaceProps::kUseDeviceIndependentFonts_Flag) { |
| 1998 | title.prepend("<DIF> "); |
reed | 4302ae9 | 2014-10-06 12:29:56 -0700 | [diff] [blame] | 1999 | } |
reed | 91b4dfb | 2016-01-08 08:23:19 -0800 | [diff] [blame] | 2000 | if (fUsePicture) { |
| 2001 | title.prepend("<P> "); |
kkinnunen | 8686a5e | 2016-01-05 00:04:32 -0800 | [diff] [blame] | 2002 | } |
reed@google.com | 569e043 | 2011-04-05 13:07:03 +0000 | [diff] [blame] | 2003 | |
| 2004 | title.prepend(trystate_str(fLCDState, "LCD ", "lcd ")); |
| 2005 | title.prepend(trystate_str(fAAState, "AA ", "aa ")); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 2006 | title.prepend(gFilterQualityStates[fFilterQualityIndex].fLabel); |
bungeman@google.com | 96aabc8 | 2013-06-03 21:26:34 +0000 | [diff] [blame] | 2007 | title.prepend(trystate_str(fSubpixelState, "S ", "s ")); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2008 | title.prepend(fFlipAxis & kFlipAxis_X ? "X " : nullptr); |
| 2009 | title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : nullptr); |
bungeman@google.com | 96aabc8 | 2013-06-03 21:26:34 +0000 | [diff] [blame] | 2010 | title.prepend(gHintingStates[fHintingState].label); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 2011 | |
| 2012 | if (fZoomLevel) { |
djsollen@google.com | e32b583 | 2011-06-13 16:58:40 +0000 | [diff] [blame] | 2013 | title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel)); |
mike@reedtribe.org | dd0cd34 | 2011-03-21 00:53:39 +0000 | [diff] [blame] | 2014 | } |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 2015 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 2016 | if (fMeasureFPS) { |
jvanverth | 4d0d81a | 2015-08-11 11:07:07 -0700 | [diff] [blame] | 2017 | title.appendf(" %8.4f ms", fMeasureFPS_Time / (float)FPS_REPEAT_COUNT); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 2018 | } |
reed | ddb5eca | 2014-10-08 11:10:51 -0700 | [diff] [blame] | 2019 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 2020 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 82502e2 | 2013-01-24 20:47:18 +0000 | [diff] [blame] | 2021 | if (IsGpuDeviceType(fDeviceType) && |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2022 | fDevManager && |
bsalomon@google.com | 82502e2 | 2013-01-24 20:47:18 +0000 | [diff] [blame] | 2023 | fDevManager->getGrRenderTarget() && |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 2024 | fDevManager->getGrRenderTarget()->numColorSamples() > 0) { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 2025 | title.appendf(" [MSAA: %d]", |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 2026 | fDevManager->getGrRenderTarget()->numColorSamples()); |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 2027 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 2028 | #endif |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 2029 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2030 | this->setTitle(title.c_str()); |
| 2031 | } |
| 2032 | |
| 2033 | void SampleWindow::onSizeChange() { |
| 2034 | this->INHERITED::onSizeChange(); |
chudy@google.com | 4605a3f | 2012-08-01 17:58:01 +0000 | [diff] [blame] | 2035 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2036 | SkView::F2BIter iter(this); |
| 2037 | SkView* view = iter.next(); |
| 2038 | view->setSize(this->width(), this->height()); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 2039 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2040 | // rebuild our clippath |
| 2041 | { |
| 2042 | const SkScalar W = this->width(); |
| 2043 | const SkScalar H = this->height(); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 2044 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2045 | fClipPath.reset(); |
| 2046 | #if 0 |
| 2047 | for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) { |
| 2048 | SkRect r; |
| 2049 | r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30)); |
| 2050 | for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0)) |
| 2051 | fClipPath.addRect(r); |
| 2052 | } |
| 2053 | #else |
| 2054 | SkRect r; |
| 2055 | r.set(0, 0, W, H); |
| 2056 | fClipPath.addRect(r, SkPath::kCCW_Direction); |
| 2057 | r.set(W/4, H/4, W*3/4, H*3/4); |
| 2058 | fClipPath.addRect(r, SkPath::kCW_Direction); |
| 2059 | #endif |
| 2060 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 2061 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 2062 | fZoomCenterX = SkScalarHalf(this->width()); |
| 2063 | fZoomCenterY = SkScalarHalf(this->height()); |
| 2064 | |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 2065 | #ifdef SK_BUILD_FOR_ANDROID |
Scroggo | b4490c7 | 2011-06-17 13:53:05 +0000 | [diff] [blame] | 2066 | // FIXME: The first draw after a size change does not work on Android, so |
| 2067 | // we post an invalidate. |
Scroggo | 62b65b0 | 2011-06-21 16:01:26 +0000 | [diff] [blame] | 2068 | this->postInvalDelay(); |
Scroggo | 716a038 | 2011-06-16 14:00:15 +0000 | [diff] [blame] | 2069 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2070 | this->updateTitle(); // to refresh our config |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame] | 2071 | fDevManager->windowSizeChanged(this); |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 2072 | |
| 2073 | if (fTilingMode != kNo_Tiling && SampleView::IsSampleView(view)) { |
| 2074 | ((SampleView*)view)->onTileSizeChanged(this->tileSize()); |
| 2075 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | /////////////////////////////////////////////////////////////////////////////// |
| 2079 | |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 2080 | static const char is_sample_view_tag[] = "sample-is-sample-view"; |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 2081 | static const char repeat_count_tag[] = "sample-set-repeat-count"; |
| 2082 | |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 2083 | bool SampleView::IsSampleView(SkView* view) { |
| 2084 | SkEvent evt(is_sample_view_tag); |
| 2085 | return view->doQuery(&evt); |
| 2086 | } |
| 2087 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 2088 | bool SampleView::SetRepeatDraw(SkView* view, int count) { |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 2089 | SkEvent evt(repeat_count_tag); |
| 2090 | evt.setFast32(count); |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 2091 | return view->doEvent(evt); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | bool SampleView::onEvent(const SkEvent& evt) { |
| 2095 | if (evt.isType(repeat_count_tag)) { |
| 2096 | fRepeatCount = evt.getFast32(); |
| 2097 | return true; |
| 2098 | } |
| 2099 | return this->INHERITED::onEvent(evt); |
| 2100 | } |
| 2101 | |
| 2102 | bool SampleView::onQuery(SkEvent* evt) { |
reed@google.com | a6ff4dc | 2011-05-12 22:08:24 +0000 | [diff] [blame] | 2103 | if (evt->isType(is_sample_view_tag)) { |
| 2104 | return true; |
| 2105 | } |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 2106 | return this->INHERITED::onQuery(evt); |
| 2107 | } |
| 2108 | |
yangsu@google.com | db03eaa | 2011-08-08 15:37:23 +0000 | [diff] [blame] | 2109 | void SampleView::onDraw(SkCanvas* canvas) { |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 2110 | if (!fHaveCalledOnceBeforeDraw) { |
| 2111 | fHaveCalledOnceBeforeDraw = true; |
| 2112 | this->onOnceBeforeDraw(); |
| 2113 | } |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 2114 | this->onDrawBackground(canvas); |
| 2115 | |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 2116 | for (int i = 0; i < fRepeatCount; i++) { |
scroggo@google.com | 85cade0 | 2012-08-17 13:29:50 +0000 | [diff] [blame] | 2117 | SkAutoCanvasRestore acr(canvas, true); |
| 2118 | this->onDrawContent(canvas); |
cdalton | e6d2024 | 2015-10-26 13:45:29 -0700 | [diff] [blame] | 2119 | #if SK_SUPPORT_GPU |
| 2120 | // Ensure the GrContext doesn't batch across draw loops. |
| 2121 | if (GrContext* context = canvas->getGrContext()) { |
| 2122 | context->flush(); |
| 2123 | } |
| 2124 | #endif |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | void SampleView::onDrawBackground(SkCanvas* canvas) { |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 2129 | canvas->drawColor(fBGColor); |
mike@reedtribe.org | 2eb5952 | 2011-04-22 01:59:09 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | /////////////////////////////////////////////////////////////////////////////// |
| 2133 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 2134 | template <typename T> void SkTBSort(T array[], int count) { |
| 2135 | for (int i = 1; i < count - 1; i++) { |
| 2136 | bool didSwap = false; |
| 2137 | for (int j = count - 1; j > i; --j) { |
| 2138 | if (array[j] < array[j-1]) { |
| 2139 | T tmp(array[j-1]); |
| 2140 | array[j-1] = array[j]; |
| 2141 | array[j] = tmp; |
| 2142 | didSwap = true; |
| 2143 | } |
| 2144 | } |
| 2145 | if (!didSwap) { |
| 2146 | break; |
| 2147 | } |
| 2148 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 2149 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 2150 | for (int k = 0; k < count - 1; k++) { |
| 2151 | SkASSERT(!(array[k+1] < array[k])); |
| 2152 | } |
| 2153 | } |
| 2154 | |
| 2155 | #include "SkRandom.h" |
| 2156 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 2157 | static void rand_rect(SkIRect* rect, SkRandom& rand) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 2158 | int bits = 8; |
| 2159 | int shift = 32 - bits; |
| 2160 | rect->set(rand.nextU() >> shift, rand.nextU() >> shift, |
| 2161 | rand.nextU() >> shift, rand.nextU() >> shift); |
| 2162 | rect->sort(); |
| 2163 | } |
| 2164 | |
| 2165 | static void dumpRect(const SkIRect& r) { |
| 2166 | SkDebugf(" { %d, %d, %d, %d },\n", |
| 2167 | r.fLeft, r.fTop, |
| 2168 | r.fRight, r.fBottom); |
| 2169 | } |
| 2170 | |
| 2171 | static void test_rects(const SkIRect rect[], int count) { |
| 2172 | SkRegion rgn0, rgn1; |
| 2173 | |
| 2174 | for (int i = 0; i < count; i++) { |
| 2175 | rgn0.op(rect[i], SkRegion::kUnion_Op); |
| 2176 | // dumpRect(rect[i]); |
| 2177 | } |
| 2178 | rgn1.setRects(rect, count); |
| 2179 | |
| 2180 | if (rgn0 != rgn1) { |
| 2181 | SkDebugf("\n"); |
| 2182 | for (int i = 0; i < count; i++) { |
| 2183 | dumpRect(rect[i]); |
| 2184 | } |
| 2185 | SkDebugf("\n"); |
| 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | static void test() { |
| 2190 | size_t i; |
| 2191 | |
| 2192 | const SkIRect r0[] = { |
| 2193 | { 0, 0, 1, 1 }, |
| 2194 | { 2, 2, 3, 3 }, |
| 2195 | }; |
| 2196 | const SkIRect r1[] = { |
| 2197 | { 0, 0, 1, 3 }, |
| 2198 | { 1, 1, 2, 2 }, |
| 2199 | { 2, 0, 3, 3 }, |
| 2200 | }; |
| 2201 | const SkIRect r2[] = { |
| 2202 | { 0, 0, 1, 2 }, |
| 2203 | { 2, 1, 3, 3 }, |
| 2204 | { 4, 0, 5, 1 }, |
| 2205 | { 6, 0, 7, 4 }, |
| 2206 | }; |
| 2207 | |
| 2208 | static const struct { |
| 2209 | const SkIRect* fRects; |
| 2210 | int fCount; |
| 2211 | } gRecs[] = { |
| 2212 | { r0, SK_ARRAY_COUNT(r0) }, |
| 2213 | { r1, SK_ARRAY_COUNT(r1) }, |
| 2214 | { r2, SK_ARRAY_COUNT(r2) }, |
| 2215 | }; |
| 2216 | |
| 2217 | for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) { |
| 2218 | test_rects(gRecs[i].fRects, gRecs[i].fCount); |
| 2219 | } |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 2220 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 2221 | SkRandom rand; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 2222 | for (i = 0; i < 10000; i++) { |
| 2223 | SkRegion rgn0, rgn1; |
| 2224 | |
| 2225 | const int N = 8; |
| 2226 | SkIRect rect[N]; |
| 2227 | for (int j = 0; j < N; j++) { |
| 2228 | rand_rect(&rect[j], rand); |
| 2229 | } |
| 2230 | test_rects(rect, N); |
| 2231 | } |
| 2232 | } |
| 2233 | |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 2234 | // FIXME: this should be in a header |
| 2235 | SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv); |
senorblanco@chromium.org | 78b8253 | 2011-06-28 19:44:03 +0000 | [diff] [blame] | 2236 | SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 2237 | if (false) { // avoid bit rot, suppress warning |
| 2238 | test(); |
| 2239 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2240 | return new SampleWindow(hwnd, argc, argv, nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 2243 | // FIXME: this should be in a header |
| 2244 | 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] | 2245 | void get_preferred_size(int* x, int* y, int* width, int* height) { |
| 2246 | *x = 10; |
| 2247 | *y = 50; |
| 2248 | *width = 640; |
| 2249 | *height = 480; |
| 2250 | } |
| 2251 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 2252 | #ifdef SK_BUILD_FOR_IOS |
reed | f0b1710 | 2014-10-22 13:06:00 -0700 | [diff] [blame] | 2253 | #include "SkApplication.h" |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 2254 | IOS_launch_type set_cmd_line_args(int , char *[], const char* resourceDir) { |
caryclark | f568f6a | 2014-07-18 10:58:56 -0700 | [diff] [blame] | 2255 | SetResourcePath(resourceDir); |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 2256 | return kApplication__iOSLaunchType; |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 2257 | } |
| 2258 | #endif |
| 2259 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2260 | void application_init() { |
| 2261 | // setenv("ANDROID_ROOT", "../../../data", 0); |
reed@android.com | e191b16 | 2009-12-18 21:33:39 +0000 | [diff] [blame] | 2262 | #ifdef SK_BUILD_FOR_MAC |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2263 | setenv("ANDROID_ROOT", "/android/device/data", 0); |
reed@android.com | e191b16 | 2009-12-18 21:33:39 +0000 | [diff] [blame] | 2264 | #endif |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 2265 | SkGraphics::Init(); |
| 2266 | SkEvent::Init(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | void application_term() { |
bsalomon@google.com | 2fbc7fa | 2011-01-05 16:34:41 +0000 | [diff] [blame] | 2270 | SkEvent::Term(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2271 | } |