blob: 33b38c0876e715b5ddd813323918239c2d23ad5e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed868074b2014-06-03 10:53:59 -07007
Scroggo2c8208f2011-06-15 16:49:08 +00008#include "SampleApp.h"
9
tfarinabcbc1782014-06-18 14:32:48 -070010#include "OverView.h"
caryclarkf568f6a2014-07-18 10:58:56 -070011#include "Resources.h"
tfarinabcbc1782014-06-18 14:32:48 -070012#include "SampleCode.h"
reed76113a92015-02-02 12:55:02 -080013#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkCanvas.h"
tfarinabcbc1782014-06-18 14:32:48 -070015#include "SkCommandLineFlags.h"
16#include "SkData.h"
reedddb5eca2014-10-08 11:10:51 -070017#include "SkDocument.h"
robertphillips1e876182016-07-29 08:28:25 -070018#include "SkGammaColorFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkGraphics.h"
brianosman2d1ee792016-05-05 12:24:31 -070020#include "SkImage_Base.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +000021#include "SkImageEncoder.h"
tfarinabcbc1782014-06-18 14:32:48 -070022#include "SkOSFile.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkPaint.h"
fmalita822ace92015-06-15 07:07:32 -070024#include "SkPaintFilterCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000026#include "SkPictureRecorder.h"
brianosman36db3f42016-04-20 09:20:46 -070027#include "SkPM4fPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000028#include "SkStream.h"
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +000029#include "SkSurface.h"
scroggo565901d2015-12-10 10:44:13 -080030#include "SkTemplates.h"
edisonn@google.comf8b6b012013-07-11 14:28:04 +000031#include "SkTSort.h"
reed@android.com44177402009-11-23 21:07:51 +000032#include "SkTime.h"
Scroggo0f185c22011-03-24 18:35:50 +000033#include "SkTypeface.h"
tfarinabcbc1782014-06-18 14:32:48 -070034#include "SkWindow.h"
tfarinabcbc1782014-06-18 14:32:48 -070035#include "sk_tool_utils.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000036
reed54dc4872016-09-13 08:09:45 -070037#include "SkReadBuffer.h"
38#include "SkStream.h"
39
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000040#if SK_SUPPORT_GPU
bsalomon3724e572016-03-30 18:56:19 -070041# include "gl/GrGLInterface.h"
42# include "gl/GrGLUtil.h"
bsalomon3724e572016-03-30 18:56:19 -070043# include "GrContext.h"
robertphillips7715e062016-04-22 10:57:16 -070044# include "SkGr.h"
bsalomon3724e572016-03-30 18:56:19 -070045# if SK_ANGLE
bsalomon273c0f52016-03-31 10:59:06 -070046# include "gl/angle/GLTestContext_angle.h"
bsalomon3724e572016-03-30 18:56:19 -070047# endif
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000048#else
49class GrContext;
50#endif
Scroggo2c8208f2011-06-15 16:49:08 +000051
reeda34be682016-02-15 07:48:35 -080052const struct {
53 SkColorType fColorType;
brianosmanb109b8c2016-06-16 13:03:24 -070054 bool fSRGB;
reeda34be682016-02-15 07:48:35 -080055 const char* fName;
56} gConfig[] = {
brianosmanb109b8c2016-06-16 13:03:24 -070057 { kN32_SkColorType, false, "L32" },
58 { kN32_SkColorType, true, "S32" },
brianosmanefded512016-07-26 08:11:50 -070059 { kRGBA_F16_SkColorType, true, "F16" },
reeda34be682016-02-15 07:48:35 -080060};
61
62static const char* find_config_name(const SkImageInfo& info) {
63 for (const auto& config : gConfig) {
brianosmanb109b8c2016-06-16 13:03:24 -070064 if (config.fColorType == info.colorType() &&
65 config.fSRGB == (info.colorSpace() != nullptr)) {
reeda34be682016-02-15 07:48:35 -080066 return config.fName;
67 }
68 }
69 return "???";
70}
71
reed5f629b42015-08-15 11:57:23 -070072// Should be 3x + 1
73#define kMaxFatBitsScale 28
74
reed@google.com1830c7a2012-06-04 12:05:43 +000075extern SampleView* CreateSamplePictFileView(const char filename[]);
76
77class PictFileFactory : public SkViewFactory {
78 SkString fFilename;
79public:
80 PictFileFactory(const SkString& filename) : fFilename(filename) {}
mtklein36352bf2015-03-25 18:17:31 -070081 SkView* operator() () const override {
reed@google.com1830c7a2012-06-04 12:05:43 +000082 return CreateSamplePictFileView(fFilename.c_str());
83 }
84};
85
fmalita851d68a2016-08-15 07:48:47 -070086extern SampleView* CreateSampleSVGFileView(const SkString& filename);
fmalita6ceef3d2016-07-26 18:46:34 -070087
88class SVGFileFactory : public SkViewFactory {
89 SkString fFilename;
90public:
91 SVGFileFactory(const SkString& filename) : fFilename(filename) {}
92 SkView* operator() () const override {
fmalita851d68a2016-08-15 07:48:47 -070093 return CreateSampleSVGFileView(fFilename);
fmalita6ceef3d2016-07-26 18:46:34 -070094 }
95};
96
edisonn@google.comf8b6b012013-07-11 14:28:04 +000097#ifdef SAMPLE_PDF_FILE_VIEWER
98extern SampleView* CreateSamplePdfFileViewer(const char filename[]);
99
100class PdfFileViewerFactory : public SkViewFactory {
101 SkString fFilename;
102public:
103 PdfFileViewerFactory(const SkString& filename) : fFilename(filename) {}
mtklein36352bf2015-03-25 18:17:31 -0700104 SkView* operator() () const override {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000105 return CreateSamplePdfFileViewer(fFilename.c_str());
106 }
107};
108#endif // SAMPLE_PDF_FILE_VIEWER
109
bsalomon49996162016-06-14 09:43:52 -0700110#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000111//#define DEFAULT_TO_ANGLE 1
112#else
bsalomon@google.com82d12232013-09-09 15:36:26 +0000113#define DEFAULT_TO_GPU 0 // if 1 default rendering is on GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000114#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000115
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116#define ANIMATING_EVENTTYPE "nextSample"
reed@google.com84cfce12013-05-29 19:22:20 +0000117#define ANIMATING_DELAY 250
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000119#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +0000120 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000121#else
reed@google.combad8c872011-05-18 20:10:31 +0000122 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000123#endif
reed@google.combad8c872011-05-18 20:10:31 +0000124#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000125
reed@google.com3cec4d72011-07-06 13:59:47 +0000126static SampleWindow* gSampleWindow;
127
reed@google.coma4f81372012-11-15 15:56:38 +0000128static bool gShowGMBounds;
129
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +0000130static void post_event_to_sink(SkEvent* evt, SkEventSink* sink) {
reed@google.com2072db82011-11-08 15:18:10 +0000131 evt->setTargetID(sink->getSinkID())->post();
132}
133
reed76113a92015-02-02 12:55:02 -0800134static SkAnimTimer gAnimTimer;
135
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000136///////////////////////////////////////////////////////////////////////////////
137
138static const char* skip_until(const char* str, const char* skip) {
139 if (!str) {
halcanary96fcdcc2015-08-27 07:41:13 -0700140 return nullptr;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000141 }
142 return strstr(str, skip);
143}
144
145static const char* skip_past(const char* str, const char* skip) {
146 const char* found = skip_until(str, skip);
147 if (!found) {
halcanary96fcdcc2015-08-27 07:41:13 -0700148 return nullptr;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000149 }
150 return found + strlen(skip);
151}
152
153static const char* gPrefFileName = "sampleapp_prefs.txt";
154
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000155static bool readTitleFromPrefs(SkString* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000156 SkFILEStream stream(gPrefFileName);
157 if (!stream.isValid()) {
158 return false;
159 }
160
reed@google.com7fa2a652014-01-27 13:42:58 +0000161 size_t len = stream.getLength();
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000162 SkString data(len);
163 stream.read(data.writable_str(), len);
164 const char* s = data.c_str();
165
166 s = skip_past(s, "curr-slide-title");
167 s = skip_past(s, "=");
168 s = skip_past(s, "\"");
169 const char* stop = skip_until(s, "\"");
170 if (stop > s) {
171 title->set(s, stop - s);
172 return true;
173 }
174 return false;
175}
176
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000177static void writeTitleToPrefs(const char* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000178 SkFILEWStream stream(gPrefFileName);
179 SkString data;
180 data.printf("curr-slide-title = \"%s\"\n", title);
181 stream.write(data.c_str(), data.size());
182}
183
184///////////////////////////////////////////////////////////////////////////////
185
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000186class SampleWindow::DefaultDeviceManager : public SampleWindow::DeviceManager {
187public:
188
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000189 DefaultDeviceManager() {
190#if SK_SUPPORT_GPU
halcanary96fcdcc2015-08-27 07:41:13 -0700191 fCurContext = nullptr;
192 fCurIntf = nullptr;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000193 fMSAASampleCount = 0;
brianosman2d1ee792016-05-05 12:24:31 -0700194 fDeepColor = false;
195 fActualColorBits = 0;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000196#endif
197 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000198 }
199
200 virtual ~DefaultDeviceManager() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000201#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000202 SkSafeUnref(fCurContext);
203 SkSafeUnref(fCurIntf);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000204#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000205 }
206
brianosman2d1ee792016-05-05 12:24:31 -0700207 void setUpBackend(SampleWindow* win, int msaaSampleCount, bool deepColor) override {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000208 SkASSERT(kNone_BackEndType == fBackend);
robertphillips@google.com53e96a12012-03-30 14:47:53 +0000209
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000210 fBackend = kNone_BackEndType;
211
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000212#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000213 switch (win->getDeviceType()) {
reedbeedb812015-05-05 12:14:45 -0700214 case kRaster_DeviceType: // fallthrough
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000215 case kGPU_DeviceType:
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000216 // all these guys use the native backend
217 fBackend = kNativeGL_BackEndType;
218 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000219#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000220 case kANGLE_DeviceType:
221 // ANGLE is really the only odd man out
222 fBackend = kANGLE_BackEndType;
223 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000224#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000225 default:
226 SkASSERT(false);
227 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000228 }
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000229 AttachmentInfo attachmentInfo;
brianosman2d1ee792016-05-05 12:24:31 -0700230 bool result = win->attach(fBackend, msaaSampleCount, deepColor, &attachmentInfo);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000231 if (!result) {
bsalomon@google.com74913722011-10-27 20:44:19 +0000232 SkDebugf("Failed to initialize GL");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000233 return;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000234 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000235 fMSAASampleCount = msaaSampleCount;
brianosman2d1ee792016-05-05 12:24:31 -0700236 fDeepColor = deepColor;
237 // Assume that we have at least 24-bit output, for backends that don't supply this data
238 fActualColorBits = SkTMax(attachmentInfo.fColorBits, 24);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000239
halcanary96fcdcc2015-08-27 07:41:13 -0700240 SkASSERT(nullptr == fCurIntf);
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000241 SkAutoTUnref<const GrGLInterface> glInterface;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000242 switch (win->getDeviceType()) {
reedbeedb812015-05-05 12:14:45 -0700243 case kRaster_DeviceType: // fallthrough
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000244 case kGPU_DeviceType:
245 // all these guys use the native interface
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000246 glInterface.reset(GrGLCreateNativeInterface());
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000247 break;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000248#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000249 case kANGLE_DeviceType:
bsalomon3724e572016-03-30 18:56:19 -0700250 glInterface.reset(sk_gpu_test::CreateANGLEGLInterface());
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000251 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000252#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000253 default:
254 SkASSERT(false);
255 break;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000256 }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000257
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000258 // Currently SampleApp does not use NVPR. TODO: Provide an NVPR device type that is skipped
259 // when the driver doesn't support NVPR.
260 fCurIntf = GrGLInterfaceRemoveNVPR(glInterface.get());
261
halcanary96fcdcc2015-08-27 07:41:13 -0700262 SkASSERT(nullptr == fCurContext);
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000263 fCurContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000264
halcanary96fcdcc2015-08-27 07:41:13 -0700265 if (nullptr == fCurContext || nullptr == fCurIntf) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000266 // We need some context and interface to see results
267 SkSafeUnref(fCurContext);
268 SkSafeUnref(fCurIntf);
halcanary96fcdcc2015-08-27 07:41:13 -0700269 fCurContext = nullptr;
270 fCurIntf = nullptr;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000271 SkDebugf("Failed to setup 3D");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000272
mtklein18300a32016-03-16 13:53:35 -0700273 win->release();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000274 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000275#endif // SK_SUPPORT_GPU
robertphillipsecf3dbe2016-07-28 15:17:34 -0700276 // call windowSizeChanged to create the gpu-backed Surface
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000277 this->windowSizeChanged(win);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000278 }
279
mtkleinf0599002015-07-13 06:18:39 -0700280 void tearDownBackend(SampleWindow *win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000281#if SK_SUPPORT_GPU
reed789e3272014-10-30 11:29:08 -0700282 if (fCurContext) {
283 // in case we have outstanding refs to this guy (lua?)
284 fCurContext->abandonContext();
285 fCurContext->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700286 fCurContext = nullptr;
reed789e3272014-10-30 11:29:08 -0700287 }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000288
289 SkSafeUnref(fCurIntf);
halcanary96fcdcc2015-08-27 07:41:13 -0700290 fCurIntf = nullptr;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000291
robertphillipsecf3dbe2016-07-28 15:17:34 -0700292 fGpuSurface = nullptr;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000293#endif
mtklein18300a32016-03-16 13:53:35 -0700294 win->release();
bsalomon@google.comddd40e52012-04-10 15:56:29 +0000295 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000296 }
297
robertphillipsecf3dbe2016-07-28 15:17:34 -0700298 sk_sp<SkSurface> makeSurface(SampleWindow::DeviceType dType, SampleWindow* win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000299#if SK_SUPPORT_GPU
bsalomon49f085d2014-09-05 13:34:00 -0700300 if (IsGpuDeviceType(dType) && fCurContext) {
reed4302ae92014-10-06 12:29:56 -0700301 SkSurfaceProps props(win->getSurfaceProps());
brianosman2d1ee792016-05-05 12:24:31 -0700302 if (kRGBA_F16_SkColorType == win->info().colorType() || fActualColorBits > 24) {
303 // If we're rendering to F16, we need an off-screen surface - the current render
304 // target is most likely the wrong format.
305 //
306 // If we're using a deep (10-bit or higher) surface, we probably need an off-screen
307 // surface. 10-bit, in particular, has strange gamma behavior.
brianosman6b086522016-04-14 12:39:00 -0700308 return SkSurface::MakeRenderTarget(fCurContext, SkBudgeted::kNo, win->info(),
robertphillipsecf3dbe2016-07-28 15:17:34 -0700309 fMSAASampleCount, &props);
brianosman6b086522016-04-14 12:39:00 -0700310 } else {
robertphillipsecf3dbe2016-07-28 15:17:34 -0700311 return fGpuSurface;
brianosman6b086522016-04-14 12:39:00 -0700312 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000313 }
reed0397e9f2014-09-18 11:29:01 -0700314#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700315 return nullptr;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000316 }
317
mtkleinf0599002015-07-13 06:18:39 -0700318 void publishCanvas(SampleWindow::DeviceType dType,
robertphillips1e876182016-07-29 08:28:25 -0700319 SkCanvas* renderingCanvas, SampleWindow* win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000320#if SK_SUPPORT_GPU
brianosman2d1ee792016-05-05 12:24:31 -0700321 if (!IsGpuDeviceType(dType) ||
322 kRGBA_F16_SkColorType == win->info().colorType() ||
323 fActualColorBits > 24) {
324 // We made/have an off-screen surface. Get the contents as an SkImage:
325 SkBitmap bm;
326 bm.allocPixels(win->info());
robertphillips1e876182016-07-29 08:28:25 -0700327 renderingCanvas->readPixels(&bm, 0, 0);
brianosman2d1ee792016-05-05 12:24:31 -0700328 SkPixmap pm;
329 bm.peekPixels(&pm);
330 sk_sp<SkImage> image(SkImage::MakeTextureFromPixmap(fCurContext, pm,
331 SkBudgeted::kNo));
robertphillipsecf3dbe2016-07-28 15:17:34 -0700332
robertphillips1e876182016-07-29 08:28:25 -0700333 SkCanvas* gpuCanvas = fGpuSurface->getCanvas();
brianosman2d1ee792016-05-05 12:24:31 -0700334
335 // With ten-bit output, we need to manually apply the gamma of the output device
336 // (unless we're in non-gamma correct mode, in which case our data is already
337 // fake-sRGB, like we're expected to put in the 10-bit buffer):
338 bool doGamma = (fActualColorBits == 30) && SkImageInfoIsGammaCorrect(win->info());
robertphillips1e876182016-07-29 08:28:25 -0700339
340 SkPaint gammaPaint;
341 gammaPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
342 if (doGamma) {
343 gammaPaint.setColorFilter(SkGammaColorFilter::Make(1.0f / 2.2f));
344 }
345
346 gpuCanvas->drawImage(image, 0, 0, &gammaPaint);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000347 }
robertphillips1e876182016-07-29 08:28:25 -0700348
349 fGpuSurface->prepareForExternalIO();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000350#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000351
352 win->present();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000353 }
354
mtkleinf0599002015-07-13 06:18:39 -0700355 void windowSizeChanged(SampleWindow* win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000356#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000357 if (fCurContext) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000358 AttachmentInfo attachmentInfo;
brianosman2d1ee792016-05-05 12:24:31 -0700359 win->attach(fBackend, fMSAASampleCount, fDeepColor, &attachmentInfo);
brianosman2d1ee792016-05-05 12:24:31 -0700360 fActualColorBits = SkTMax(attachmentInfo.fColorBits, 24);
robertphillipsecf3dbe2016-07-28 15:17:34 -0700361 fGpuSurface = win->makeGpuBackedSurface(attachmentInfo, fCurIntf, fCurContext);
bsalomon@google.com74913722011-10-27 20:44:19 +0000362 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000363#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000364 }
365
mtkleinf0599002015-07-13 06:18:39 -0700366 GrContext* getGrContext() override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000367#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000368 return fCurContext;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000369#else
halcanary96fcdcc2015-08-27 07:41:13 -0700370 return nullptr;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000371#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000372 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000373
robertphillipsecf3dbe2016-07-28 15:17:34 -0700374 int numColorSamples() const override {
brianosman1dfec742016-09-15 07:19:53 -0700375#if SK_SUPPORT_GPU
robertphillipsecf3dbe2016-07-28 15:17:34 -0700376 return fMSAASampleCount;
brianosman1dfec742016-09-15 07:19:53 -0700377#else
378 return 0;
379#endif
bsalomon@google.com11959252012-04-06 20:13:38 +0000380 }
381
brianosman2d1ee792016-05-05 12:24:31 -0700382 int getColorBits() override {
383#if SK_SUPPORT_GPU
384 return fActualColorBits;
385#else
386 return 24;
387#endif
388 }
389
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000390private:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000391
392#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000393 GrContext* fCurContext;
394 const GrGLInterface* fCurIntf;
robertphillipsecf3dbe2016-07-28 15:17:34 -0700395 sk_sp<SkSurface> fGpuSurface;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000396 int fMSAASampleCount;
brianosman2d1ee792016-05-05 12:24:31 -0700397 bool fDeepColor;
398 int fActualColorBits;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000399#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000400
401 SkOSWindow::SkBackEndTypes fBackend;
402
403 typedef SampleWindow::DeviceManager INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000404};
405
406///////////////
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000407static const char view_inval_msg[] = "view-inval-msg";
408
Scroggo62b65b02011-06-21 16:01:26 +0000409void SampleWindow::postInvalDelay() {
reed@google.com87fac4a2011-08-04 13:50:17 +0000410 (new SkEvent(view_inval_msg, this->getSinkID()))->postDelay(1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000411}
412
413static bool isInvalEvent(const SkEvent& evt) {
414 return evt.isType(view_inval_msg);
415}
416//////////////////
417
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000418#include "GMSampleView.h"
419
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000420class AutoUnrefArray {
421public:
422 AutoUnrefArray() {}
423 ~AutoUnrefArray() {
424 int count = fObjs.count();
425 for (int i = 0; i < count; ++i) {
426 fObjs[i]->unref();
427 }
428 }
429 SkRefCnt*& push_back() { return *fObjs.append(); }
chudy@google.com4605a3f2012-08-01 17:58:01 +0000430
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000431private:
432 SkTDArray<SkRefCnt*> fObjs;
433};
434
435// registers GMs as Samples
436// This can't be performed during static initialization because it could be
437// run before GMRegistry has been fully built.
caryclark@google.com02939ce2012-06-06 12:09:51 +0000438static void SkGMRegistyToSampleRegistry() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000439 static bool gOnce;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000440 static AutoUnrefArray fRegisters;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000441
442 if (!gOnce) {
443 const skiagm::GMRegistry* gmreg = skiagm::GMRegistry::Head();
444 while (gmreg) {
445 fRegisters.push_back() = new SkViewRegister(gmreg->factory());
446 gmreg = gmreg->next();
447 }
448 gOnce = true;
449 }
450}
451
reed@google.comf0b5f682011-03-11 20:08:25 +0000452//////////////////////////////////////////////////////////////////////////////
453
reed@google.com569e0432011-04-05 13:07:03 +0000454enum FlipAxisEnum {
455 kFlipAxis_X = (1 << 0),
456 kFlipAxis_Y = (1 << 1)
457};
458
reed@google.comf0b5f682011-03-11 20:08:25 +0000459#include "SkDrawFilter.h"
460
bungeman@google.com96aabc82013-06-03 21:26:34 +0000461struct HintingState {
462 SkPaint::Hinting hinting;
463 const char* name;
464 const char* label;
465};
466static HintingState gHintingStates[] = {
halcanary96fcdcc2015-08-27 07:41:13 -0700467 {SkPaint::kNo_Hinting, "Mixed", nullptr },
bungeman@google.com96aabc82013-06-03 21:26:34 +0000468 {SkPaint::kNo_Hinting, "None", "H0 " },
469 {SkPaint::kSlight_Hinting, "Slight", "Hs " },
470 {SkPaint::kNormal_Hinting, "Normal", "Hn " },
471 {SkPaint::kFull_Hinting, "Full", "Hf " },
472};
473
bungeman5a59a422016-03-31 08:30:38 -0700474struct PixelGeometryState {
475 SkPixelGeometry pixelGeometry;
476 const char* name;
477 const char* label;
478};
479static PixelGeometryState gPixelGeometryStates[] = {
480 {SkPixelGeometry::kUnknown_SkPixelGeometry, "Mixed", nullptr },
481 {SkPixelGeometry::kUnknown_SkPixelGeometry, "Flat", "{Flat} " },
482 {SkPixelGeometry::kRGB_H_SkPixelGeometry, "RGB H", "{RGB H} " },
483 {SkPixelGeometry::kBGR_H_SkPixelGeometry, "BGR H", "{BGR H} " },
484 {SkPixelGeometry::kRGB_V_SkPixelGeometry, "RGB_V", "{RGB V} " },
485 {SkPixelGeometry::kBGR_V_SkPixelGeometry, "BGR_V", "{BGR V} " },
486};
487
reed93a12152015-03-16 10:08:34 -0700488struct FilterQualityState {
489 SkFilterQuality fQuality;
490 const char* fName;
491 const char* fLabel;
reed@google.com15bc13d2013-12-10 16:53:13 +0000492};
reed93a12152015-03-16 10:08:34 -0700493static FilterQualityState gFilterQualityStates[] = {
halcanary96fcdcc2015-08-27 07:41:13 -0700494 { kNone_SkFilterQuality, "Mixed", nullptr },
reed93a12152015-03-16 10:08:34 -0700495 { kNone_SkFilterQuality, "None", "F0 " },
496 { kLow_SkFilterQuality, "Low", "F1 " },
497 { kMedium_SkFilterQuality, "Medium", "F2 " },
498 { kHigh_SkFilterQuality, "High", "F3 " },
reed@google.com15bc13d2013-12-10 16:53:13 +0000499};
500
fmalita822ace92015-06-15 07:07:32 -0700501class FlagsFilterCanvas : public SkPaintFilterCanvas {
reed@google.comf0b5f682011-03-11 20:08:25 +0000502public:
fmalita822ace92015-06-15 07:07:32 -0700503 FlagsFilterCanvas(SkCanvas* canvas, SkOSMenu::TriState lcd, SkOSMenu::TriState aa,
504 SkOSMenu::TriState subpixel, int hinting, int filterQuality)
fmalitabad23dc2016-01-11 13:58:29 -0800505 : INHERITED(canvas)
fmalita822ace92015-06-15 07:07:32 -0700506 , fLCDState(lcd)
reed@google.com15bc13d2013-12-10 16:53:13 +0000507 , fAAState(aa)
508 , fSubpixelState(subpixel)
509 , fHintingState(hinting)
fmalita822ace92015-06-15 07:07:32 -0700510 , fFilterQualityIndex(filterQuality) {
reed93a12152015-03-16 10:08:34 -0700511 SkASSERT((unsigned)filterQuality < SK_ARRAY_COUNT(gFilterQualityStates));
reed@google.com15bc13d2013-12-10 16:53:13 +0000512 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000513
fmalita822ace92015-06-15 07:07:32 -0700514protected:
fmalita32cdc322016-01-12 07:21:11 -0800515 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type t) const override {
516 if (!*paint) {
fmalitabad23dc2016-01-11 13:58:29 -0800517 return true;
518 }
519
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000520 if (kText_Type == t && SkOSMenu::kMixedState != fLCDState) {
fmalita32cdc322016-01-12 07:21:11 -0800521 paint->writable()->setLCDRenderText(SkOSMenu::kOnState == fLCDState);
reed@google.com569e0432011-04-05 13:07:03 +0000522 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000523 if (SkOSMenu::kMixedState != fAAState) {
fmalita32cdc322016-01-12 07:21:11 -0800524 paint->writable()->setAntiAlias(SkOSMenu::kOnState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000525 }
reed93a12152015-03-16 10:08:34 -0700526 if (0 != fFilterQualityIndex) {
fmalita32cdc322016-01-12 07:21:11 -0800527 paint->writable()->setFilterQuality(gFilterQualityStates[fFilterQualityIndex].fQuality);
reed@google.com176753a2011-05-17 15:32:04 +0000528 }
bungeman@google.com96aabc82013-06-03 21:26:34 +0000529 if (SkOSMenu::kMixedState != fSubpixelState) {
fmalita32cdc322016-01-12 07:21:11 -0800530 paint->writable()->setSubpixelText(SkOSMenu::kOnState == fSubpixelState);
bungeman@google.com96aabc82013-06-03 21:26:34 +0000531 }
bungeman@google.com055aa522013-06-03 21:35:03 +0000532 if (0 != fHintingState && fHintingState < (int)SK_ARRAY_COUNT(gHintingStates)) {
fmalita32cdc322016-01-12 07:21:11 -0800533 paint->writable()->setHinting(gHintingStates[fHintingState].hinting);
reed@google.com09e3baa2011-05-18 12:04:31 +0000534 }
fmalitabad23dc2016-01-11 13:58:29 -0800535 return true;
reed@google.comf0b5f682011-03-11 20:08:25 +0000536 }
537
538private:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000539 SkOSMenu::TriState fLCDState;
540 SkOSMenu::TriState fAAState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000541 SkOSMenu::TriState fSubpixelState;
542 int fHintingState;
reed93a12152015-03-16 10:08:34 -0700543 int fFilterQualityIndex;
fmalita822ace92015-06-15 07:07:32 -0700544
545 typedef SkPaintFilterCanvas INHERITED;
reed@google.comf0b5f682011-03-11 20:08:25 +0000546};
547
reed@android.comf2b98d62010-12-20 18:26:13 +0000548///////////////////////////////////////////////////////////////////////////////
549
reed54dc4872016-09-13 08:09:45 -0700550class SampleTFSerializer : public SkTypefaceSerializer {
551public:
552 sk_sp<SkData> serialize(SkTypeface* tf) override {
553 tf->ref();
554 return SkData::MakeWithCopy(&tf, sizeof(tf));
555 }
556};
557
558class SampleTFDeserializer : public SkTypefaceDeserializer {
559public:
560 sk_sp<SkTypeface> deserialize(const void* data, size_t size) override {
561 SkASSERT(sizeof(SkTypeface*) == size);
562 SkTypeface* tf;
563 memcpy(&tf, data, size);
564 return sk_sp<SkTypeface>(tf); // this was ref'd in SampleTFSerializer
565 }
566};
567
568///////////////////////////////////////////////////////////////////////////////
569
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000570enum TilingMode {
571 kNo_Tiling,
572 kAbs_128x128_Tiling,
573 kAbs_256x256_Tiling,
574 kRel_4x4_Tiling,
575 kRel_1x16_Tiling,
576 kRel_16x1_Tiling,
577
578 kLast_TilingMode_Enum
579};
580
581struct TilingInfo {
582 const char* label;
583 SkScalar w, h;
584};
585
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000586static const struct TilingInfo gTilingInfo[] = {
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000587 { "No tiling", SK_Scalar1 , SK_Scalar1 }, // kNo_Tiling
588 { "128x128" , SkIntToScalar(128), SkIntToScalar(128) }, // kAbs_128x128_Tiling
589 { "256x256" , SkIntToScalar(256), SkIntToScalar(256) }, // kAbs_256x256_Tiling
590 { "1/4x1/4" , SK_Scalar1 / 4 , SK_Scalar1 / 4 }, // kRel_4x4_Tiling
591 { "1/1x1/16" , SK_Scalar1 , SK_Scalar1 / 16 }, // kRel_1x16_Tiling
592 { "1/16x1/1" , SK_Scalar1 / 16 , SK_Scalar1 }, // kRel_16x1_Tiling
593};
bungeman99fe8222015-08-20 07:57:51 -0700594static_assert((SK_ARRAY_COUNT(gTilingInfo) == kLast_TilingMode_Enum),
595 "Incomplete_tiling_labels");
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000596
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000597SkSize SampleWindow::tileSize() const {
598 SkASSERT((TilingMode)fTilingMode < kLast_TilingMode_Enum);
599 const struct TilingInfo* info = gTilingInfo + fTilingMode;
600 return SkSize::Make(info->w > SK_Scalar1 ? info->w : this->width() * info->w,
601 info->h > SK_Scalar1 ? info->h : this->height() * info->h);
602}
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000603//////////////////////////////////////////////////////////////////////////////
604
reed@android.comf2b98d62010-12-20 18:26:13 +0000605static SkView* curr_view(SkWindow* wind) {
606 SkView::F2BIter iter(wind);
607 return iter.next();
608}
609
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000610static bool curr_title(SkWindow* wind, SkString* title) {
611 SkView* view = curr_view(wind);
612 if (view) {
613 SkEvent evt(gTitleEvtName);
614 if (view->doQuery(&evt)) {
615 title->set(evt.findString(gTitleEvtName));
616 return true;
617 }
618 }
619 return false;
620}
621
reedd9adfe62015-02-01 19:01:04 -0800622bool SampleWindow::sendAnimatePulse() {
623 SkView* view = curr_view(this);
624 if (SampleView::IsSampleView(view)) {
reed76113a92015-02-02 12:55:02 -0800625 return ((SampleView*)view)->animate(gAnimTimer);
reedd9adfe62015-02-01 19:01:04 -0800626 }
627 return false;
628}
629
reed5f629b42015-08-15 11:57:23 -0700630void SampleWindow::setZoomCenter(float x, float y) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000631 fZoomCenterX = x;
632 fZoomCenterY = y;
Scroggo2c8208f2011-06-15 16:49:08 +0000633}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000634
reed5f629b42015-08-15 11:57:23 -0700635bool SampleWindow::zoomIn() {
Scroggo0f185c22011-03-24 18:35:50 +0000636 // Arbitrarily decided
reed5f629b42015-08-15 11:57:23 -0700637 if (fFatBitsScale == kMaxFatBitsScale) return false;
Scroggo0f185c22011-03-24 18:35:50 +0000638 fFatBitsScale++;
halcanary96fcdcc2015-08-27 07:41:13 -0700639 this->inval(nullptr);
Scroggo0f185c22011-03-24 18:35:50 +0000640 return true;
641}
642
reed5f629b42015-08-15 11:57:23 -0700643bool SampleWindow::zoomOut() {
Scroggo0f185c22011-03-24 18:35:50 +0000644 if (fFatBitsScale == 1) return false;
645 fFatBitsScale--;
halcanary96fcdcc2015-08-27 07:41:13 -0700646 this->inval(nullptr);
Scroggo0f185c22011-03-24 18:35:50 +0000647 return true;
648}
649
reed5f629b42015-08-15 11:57:23 -0700650void SampleWindow::updatePointer(int x, int y) {
Scroggo0f185c22011-03-24 18:35:50 +0000651 fMouseX = x;
652 fMouseY = y;
653 if (fShowZoomer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700654 this->inval(nullptr);
Scroggo0f185c22011-03-24 18:35:50 +0000655 }
656}
657
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000658static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
659 static const SampleWindow::DeviceType gCT[] = {
kkinnunen8686a5e2016-01-05 00:04:32 -0800660 SampleWindow::kRaster_DeviceType
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000661#if SK_SUPPORT_GPU
kkinnunen8686a5e2016-01-05 00:04:32 -0800662 , SampleWindow::kGPU_DeviceType
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000663#if SK_ANGLE
kkinnunen8686a5e2016-01-05 00:04:32 -0800664 , SampleWindow::kANGLE_DeviceType
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000665#endif // SK_ANGLE
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000666#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +0000667 };
bungeman99fe8222015-08-20 07:57:51 -0700668 static_assert(SK_ARRAY_COUNT(gCT) == SampleWindow::kDeviceTypeCnt, "array_size_mismatch");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000669 return gCT[ct];
670}
671
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000672static SkString getSampleTitle(const SkViewFactory* sampleFactory) {
673 SkView* view = (*sampleFactory)();
674 SkString title;
675 SampleCode::RequestTitle(view, &title);
676 view->unref();
677 return title;
678}
679
edisonn@google.comde36b692013-07-11 15:40:31 +0000680static bool compareSampleTitle(const SkViewFactory* first, const SkViewFactory* second) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000681 return strcmp(getSampleTitle(first).c_str(), getSampleTitle(second).c_str()) < 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000682}
683
reedb7ffe032015-11-05 11:59:17 -0800684static int find_by_title(const SkViewFactory* const* factories, int count, const char title[]) {
685 for (int i = 0; i < count; i++) {
686 if (getSampleTitle(factories[i]).equals(title)) {
687 return i;
688 }
689 }
690 return -1;
691}
692
693static void restrict_samples(SkTDArray<const SkViewFactory*>& factories, const SkString titles[],
694 int count) {
695 int newCount = 0;
696 for (int i = 0; i < count; ++i) {
697 int index = find_by_title(factories.begin(), factories.count(), titles[i].c_str());
698 if (index >= 0) {
699 SkTSwap(factories.begin()[newCount], factories.begin()[index]);
700 newCount += 1;
701 }
702 }
703 if (newCount) {
704 factories.setCount(newCount);
705 }
706}
707
mtkleinfd117d82014-06-19 14:18:56 -0700708DEFINE_string(slide, "", "Start on this sample.");
709DEFINE_int32(msaa, 0, "Request multisampling with this count.");
brianosman2d1ee792016-05-05 12:24:31 -0700710DEFINE_bool(deepColor, false, "Request deep color (10-bit/channel or more) display buffer.");
mtkleinfd117d82014-06-19 14:18:56 -0700711DEFINE_string(pictureDir, "", "Read pictures from here.");
712DEFINE_string(picture, "", "Path to single picture.");
fmalita6ceef3d2016-07-26 18:46:34 -0700713DEFINE_string(svg, "", "Path to single SVG file.");
714DEFINE_string(svgDir, "", "Read SVGs from here.");
reedb7ffe032015-11-05 11:59:17 -0800715DEFINE_string(sequence, "", "Path to file containing the desired samples/gms to show.");
mtkleinfd117d82014-06-19 14:18:56 -0700716DEFINE_bool(sort, false, "Sort samples by title.");
717DEFINE_bool(list, false, "List samples?");
jvanverth44dcb8a2015-10-02 09:12:05 -0700718DEFINE_bool(gpu, false, "Start up with gpu?");
brianosmanca348462016-02-24 11:47:47 -0800719DEFINE_bool(redraw, false, "Force continuous redrawing, for profiling or debugging tools.");
caryclark6531c362015-07-20 13:38:56 -0700720DEFINE_string(key, "", ""); // dummy to enable gm tests that have platform-specific names
mtkleinfd117d82014-06-19 14:18:56 -0700721#ifdef SAMPLE_PDF_FILE_VIEWER
722DEFINE_string(pdfPath, "", "Path to direcotry of pdf files.");
723#endif
724
reed89889b62014-10-29 12:36:45 -0700725#include "SkTaskGroup.h"
reed54dc4872016-09-13 08:09:45 -0700726#include "SkForceLinking.h"
reed89889b62014-10-29 12:36:45 -0700727
chudy@google.com4605a3f2012-08-01 17:58:01 +0000728SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager)
robertphillips@google.com4750fa52012-04-10 13:34:11 +0000729 : INHERITED(hwnd)
halcanary96fcdcc2015-08-27 07:41:13 -0700730 , fDevManager(nullptr) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000731
reed54dc4872016-09-13 08:09:45 -0700732 SkForceLinking(false);
733
mtkleinfd117d82014-06-19 14:18:56 -0700734 SkCommandLineFlags::Parse(argc, argv);
735
reed@google.com7d05ac92013-06-06 13:14:13 +0000736 fCurrIndex = -1;
737
mtkleinfd117d82014-06-19 14:18:56 -0700738 if (!FLAGS_pictureDir.isEmpty()) {
739 SkOSFile::Iter iter(FLAGS_pictureDir[0], "skp");
740 SkString filename;
741 while (iter.next(&filename)) {
742 *fSamples.append() = new PictFileFactory(
tfarinaa8e2e152014-07-28 19:26:58 -0700743 SkOSPath::Join(FLAGS_pictureDir[0], filename.c_str()));
mtkleinfd117d82014-06-19 14:18:56 -0700744 }
745 }
746 if (!FLAGS_picture.isEmpty()) {
747 SkString path(FLAGS_picture[0]);
748 fCurrIndex = fSamples.count();
749 *fSamples.append() = new PictFileFactory(path);
750 }
fmalita6ceef3d2016-07-26 18:46:34 -0700751 if (!FLAGS_svg.isEmpty()) {
752 SkString path(FLAGS_svg[0]);
753 fCurrIndex = fSamples.count();
754 *fSamples.append() = new SVGFileFactory(path);
755 }
756 if (!FLAGS_svgDir.isEmpty()) {
757 SkOSFile::Iter iter(FLAGS_svgDir[0], "svg");
758 SkString filename;
759 while (iter.next(&filename)) {
760 *fSamples.append() = new SVGFileFactory(
761 SkOSPath::Join(FLAGS_svgDir[0], filename.c_str()));
762 }
763 }
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000764#ifdef SAMPLE_PDF_FILE_VIEWER
mtkleinfd117d82014-06-19 14:18:56 -0700765 if (!FLAGS_pdfPath.isEmpty()) {
766 SkOSFile::Iter iter(FLAGS_pdfPath[0], "pdf");
767 SkString filename;
768 while (iter.next(&filename)) {
769 *fSamples.append() = new PdfFileViewerFactory(
tfarinaa8e2e152014-07-28 19:26:58 -0700770 SkOSPath::Join(FLAGS_pictureDir[0], filename.c_str()));
mtkleinfd117d82014-06-19 14:18:56 -0700771 }
772 }
773#endif
robertphillips@google.com76d40212012-03-22 14:12:15 +0000774 SkGMRegistyToSampleRegistry();
775 {
776 const SkViewRegister* reg = SkViewRegister::Head();
777 while (reg) {
778 *fSamples.append() = reg->factory();
779 reg = reg->next();
780 }
781 }
782
reedb7ffe032015-11-05 11:59:17 -0800783 if (!FLAGS_sequence.isEmpty()) {
784 // The sequence file just contains a list (separated by CRs) of the samples or GM:gms
785 // you want to restrict to. Only these will appear when you cycle through.
786 // If none are found, or the file is empty, then it will be ignored, and all samples
787 // will be available.
788 SkFILEStream stream(FLAGS_sequence[0]);
789 if (stream.isValid()) {
790 size_t len = stream.getLength();
scroggo565901d2015-12-10 10:44:13 -0800791 SkAutoTMalloc<char> storage(len + 1);
792 char* buffer = storage.get();
reedb7ffe032015-11-05 11:59:17 -0800793 stream.read(buffer, len);
794 buffer[len] = 0;
795
796 SkTArray<SkString> titles;
797 SkStrSplit(buffer, "\n\r", &titles);
798 restrict_samples(fSamples, titles.begin(), titles.count());
799 }
800 }
801
mtkleinfd117d82014-06-19 14:18:56 -0700802 if (FLAGS_sort) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000803 // Sort samples, so foo.skp and foo.pdf are consecutive and we can quickly spot where
804 // skp -> pdf -> png fails.
halcanary96fcdcc2015-08-27 07:41:13 -0700805 SkTQSort(fSamples.begin(), fSamples.end() ? fSamples.end() - 1 : nullptr, compareSampleTitle);
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000806 }
807
mtkleinfd117d82014-06-19 14:18:56 -0700808 if (!FLAGS_slide.isEmpty()) {
809 fCurrIndex = findByTitle(FLAGS_slide[0]);
810 if (fCurrIndex < 0) {
811 fprintf(stderr, "Unknown sample \"%s\"\n", FLAGS_slide[0]);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000812 listTitles();
bsalomon@google.com11959252012-04-06 20:13:38 +0000813 }
mtkleinfd117d82014-06-19 14:18:56 -0700814 }
815
816 fMSAASampleCount = FLAGS_msaa;
brianosman2d1ee792016-05-05 12:24:31 -0700817 fDeepColor = FLAGS_deepColor;
mtkleinfd117d82014-06-19 14:18:56 -0700818
819 if (FLAGS_list) {
820 listTitles();
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000821 }
822
823 if (fCurrIndex < 0) {
824 SkString title;
825 if (readTitleFromPrefs(&title)) {
826 fCurrIndex = findByTitle(title.c_str());
827 }
828 }
829
830 if (fCurrIndex < 0) {
831 fCurrIndex = 0;
832 }
833
reed89889b62014-10-29 12:36:45 -0700834 static SkTaskGroup::Enabler enabled(-1);
reed@google.com3cec4d72011-07-06 13:59:47 +0000835 gSampleWindow = this;
836
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000837 fDeviceType = kRaster_DeviceType;
jvanverth41518eb2015-10-02 10:42:36 -0700838#if SK_SUPPORT_GPU
jvanverth44dcb8a2015-10-02 09:12:05 -0700839 if (FLAGS_gpu) {
840 fDeviceType = kGPU_DeviceType;
841 }
jvanverth41518eb2015-10-02 10:42:36 -0700842#endif
halcanary9d524f22016-03-29 09:03:52 -0700843
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000844#if DEFAULT_TO_GPU
845 fDeviceType = kGPU_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000846#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000847#if SK_ANGLE && DEFAULT_TO_ANGLE
848 fDeviceType = kANGLE_DeviceType;
849#endif
850
reed@android.com8a1c16f2008-12-17 15:59:43 +0000851 fUseClip = false;
reed91b4dfb2016-01-08 08:23:19 -0800852 fUsePicture = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000853 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000854 fRotate = false;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000855 fPerspAnim = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000856 fRequestGrabImage = false;
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000857 fTilingMode = kNo_Tiling;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000858 fMeasureFPS = false;
reedbabc3de2016-07-08 08:43:27 -0700859 fUseDeferredCanvas = false;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000860 fLCDState = SkOSMenu::kMixedState;
861 fAAState = SkOSMenu::kMixedState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000862 fSubpixelState = SkOSMenu::kMixedState;
863 fHintingState = 0;
bungeman5a59a422016-03-31 08:30:38 -0700864 fPixelGeometryIndex = 0;
reed93a12152015-03-16 10:08:34 -0700865 fFilterQualityIndex = 0;
reed@google.com569e0432011-04-05 13:07:03 +0000866 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000867
Scroggo0f185c22011-03-24 18:35:50 +0000868 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000869 fFatBitsScale = 8;
reed5755cd92016-08-21 19:30:13 -0700870 fTypeface = SkTypeface::MakeFromName("Courier", SkFontStyle(SkFontStyle::kBold_Weight,
871 SkFontStyle::kNormal_Width,
872 SkFontStyle::kUpright_Slant));
Scroggo0f185c22011-03-24 18:35:50 +0000873 fShowZoomer = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000874
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000875 fZoomLevel = 0;
876 fZoomScale = SK_Scalar1;
bungemance560262016-03-03 13:32:38 -0800877 fOffset = { 0, 0 };
chudy@google.com4605a3f2012-08-01 17:58:01 +0000878
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000879 fMagnify = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000880
Scroggo8ac0d542011-06-21 14:44:57 +0000881 fSaveToPdf = false;
reed093b4e82015-08-04 18:46:38 -0700882 fSaveToSKP = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000883
reed54dc4872016-09-13 08:09:45 -0700884 if (true) {
885 fPipeSerializer.setTypefaceSerializer(new SampleTFSerializer);
886 fPipeDeserializer.setTypefaceDeserializer(new SampleTFDeserializer);
887 }
888
yangsu@google.com921091f2011-08-02 13:39:12 +0000889 int sinkID = this->getSinkID();
scroggo@google.com7dadc742012-04-18 14:07:57 +0000890 fAppMenu = new SkOSMenu;
891 fAppMenu->setTitle("Global Settings");
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000892 int itemID;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000893
reeda34be682016-02-15 07:48:35 -0800894 itemID = fAppMenu->appendList("ColorType", "ColorType", sinkID, 0,
895 gConfig[0].fName, gConfig[1].fName, gConfig[2].fName, nullptr);
896 fAppMenu->assignKeyEquivalentToItem(itemID, 'C');
897
kkinnunen8686a5e2016-01-05 00:04:32 -0800898 itemID = fAppMenu->appendList("Device Type", "Device Type", sinkID, 0,
899 "Raster",
900 "OpenGL",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000901#if SK_ANGLE
kkinnunen8686a5e2016-01-05 00:04:32 -0800902 "ANGLE",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000903#endif
kkinnunen8686a5e2016-01-05 00:04:32 -0800904 nullptr);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000905 fAppMenu->assignKeyEquivalentToItem(itemID, 'd');
906 itemID = fAppMenu->appendTriState("AA", "AA", sinkID, fAAState);
907 fAppMenu->assignKeyEquivalentToItem(itemID, 'b');
908 itemID = fAppMenu->appendTriState("LCD", "LCD", sinkID, fLCDState);
909 fAppMenu->assignKeyEquivalentToItem(itemID, 'l');
reed93a12152015-03-16 10:08:34 -0700910 itemID = fAppMenu->appendList("FilterQuality", "FilterQuality", sinkID, fFilterQualityIndex,
911 gFilterQualityStates[0].fName,
912 gFilterQualityStates[1].fName,
913 gFilterQualityStates[2].fName,
914 gFilterQualityStates[3].fName,
915 gFilterQualityStates[4].fName,
halcanary96fcdcc2015-08-27 07:41:13 -0700916 nullptr);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000917 fAppMenu->assignKeyEquivalentToItem(itemID, 'n');
bungeman@google.com96aabc82013-06-03 21:26:34 +0000918 itemID = fAppMenu->appendTriState("Subpixel", "Subpixel", sinkID, fSubpixelState);
919 fAppMenu->assignKeyEquivalentToItem(itemID, 's');
920 itemID = fAppMenu->appendList("Hinting", "Hinting", sinkID, fHintingState,
921 gHintingStates[0].name,
922 gHintingStates[1].name,
923 gHintingStates[2].name,
924 gHintingStates[3].name,
bungeman@google.coma9133bb2013-06-04 17:49:20 +0000925 gHintingStates[4].name,
halcanary96fcdcc2015-08-27 07:41:13 -0700926 nullptr);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000927 fAppMenu->assignKeyEquivalentToItem(itemID, 'h');
reed@google.com67b89ee2012-08-15 14:41:58 +0000928
bungeman5a59a422016-03-31 08:30:38 -0700929 itemID = fAppMenu->appendList("Pixel Geometry", "Pixel Geometry", sinkID, fPixelGeometryIndex,
930 gPixelGeometryStates[0].name,
931 gPixelGeometryStates[1].name,
932 gPixelGeometryStates[2].name,
933 gPixelGeometryStates[3].name,
934 gPixelGeometryStates[4].name,
935 gPixelGeometryStates[5].name,
936 nullptr);
937 fAppMenu->assignKeyEquivalentToItem(itemID, 'P');
938
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000939 itemID =fAppMenu->appendList("Tiling", "Tiling", sinkID, fTilingMode,
940 gTilingInfo[kNo_Tiling].label,
941 gTilingInfo[kAbs_128x128_Tiling].label,
942 gTilingInfo[kAbs_256x256_Tiling].label,
943 gTilingInfo[kRel_4x4_Tiling].label,
944 gTilingInfo[kRel_1x16_Tiling].label,
945 gTilingInfo[kRel_16x1_Tiling].label,
halcanary96fcdcc2015-08-27 07:41:13 -0700946 nullptr);
reed@google.com67b89ee2012-08-15 14:41:58 +0000947 fAppMenu->assignKeyEquivalentToItem(itemID, 't');
948
chudy@google.com4605a3f2012-08-01 17:58:01 +0000949 itemID = fAppMenu->appendSwitch("Slide Show", "Slide Show" , sinkID, false);
950 fAppMenu->assignKeyEquivalentToItem(itemID, 'a');
951 itemID = fAppMenu->appendSwitch("Clip", "Clip" , sinkID, fUseClip);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000952 fAppMenu->assignKeyEquivalentToItem(itemID, 'c');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000953 itemID = fAppMenu->appendSwitch("Flip X", "Flip X" , sinkID, false);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000954 fAppMenu->assignKeyEquivalentToItem(itemID, 'x');
955 itemID = fAppMenu->appendSwitch("Flip Y", "Flip Y" , sinkID, false);
956 fAppMenu->assignKeyEquivalentToItem(itemID, 'y');
957 itemID = fAppMenu->appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
958 fAppMenu->assignKeyEquivalentToItem(itemID, 'z');
959 itemID = fAppMenu->appendSwitch("Magnify", "Magnify" , sinkID, fMagnify);
960 fAppMenu->assignKeyEquivalentToItem(itemID, 'm');
reed093b4e82015-08-04 18:46:38 -0700961
scroggo@google.com7dadc742012-04-18 14:07:57 +0000962 itemID = fAppMenu->appendAction("Save to PDF", sinkID);
963 fAppMenu->assignKeyEquivalentToItem(itemID, 'e');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000964
scroggo@google.com7dadc742012-04-18 14:07:57 +0000965 this->addMenu(fAppMenu);
966 fSlideMenu = new SkOSMenu;
967 this->addMenu(fSlideMenu);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000968
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000969 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000970 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000971
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000972 this->loadView((*fSamples[fCurrIndex])());
chudy@google.com4605a3f2012-08-01 17:58:01 +0000973
halcanary96fcdcc2015-08-27 07:41:13 -0700974 if (nullptr == devManager) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000975 fDevManager = new DefaultDeviceManager();
976 } else {
977 devManager->ref();
978 fDevManager = devManager;
979 }
brianosman2d1ee792016-05-05 12:24:31 -0700980 fDevManager->setUpBackend(this, fMSAASampleCount, fDeepColor);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000981
Scroggob4490c72011-06-17 13:53:05 +0000982 // If another constructor set our dimensions, ensure that our
983 // onSizeChange gets called.
984 if (this->height() && this->width()) {
985 this->onSizeChange();
986 }
reed@google.com2072db82011-11-08 15:18:10 +0000987
988 // can't call this synchronously, since it may require a subclass to
989 // to implement, or the caller may need us to have returned from the
990 // constructor first. Hence we post an event to ourselves.
991// this->updateTitle();
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +0000992 post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this);
reed76113a92015-02-02 12:55:02 -0800993
994 gAnimTimer.run();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000995}
996
997SampleWindow::~SampleWindow() {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000998 SkSafeUnref(fDevManager);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000999}
1000
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001001
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001002int SampleWindow::findByTitle(const char title[]) {
1003 int i, count = fSamples.count();
1004 for (i = 0; i < count; i++) {
1005 if (getSampleTitle(i).equals(title)) {
1006 return i;
1007 }
1008 }
mike@reedtribe.orgdd52caa2011-12-28 20:02:10 +00001009 return -1;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001010}
1011
robertphillips@google.com7265e722012-05-03 18:22:28 +00001012void SampleWindow::listTitles() {
1013 int count = fSamples.count();
1014 SkDebugf("All Slides:\n");
1015 for (int i = 0; i < count; i++) {
1016 SkDebugf(" %s\n", getSampleTitle(i).c_str());
1017 }
1018}
1019
reed@android.com55e76b22009-11-23 21:46:47 +00001020static SkBitmap capture_bitmap(SkCanvas* canvas) {
1021 SkBitmap bm;
reed84825042014-09-02 12:50:45 -07001022 if (bm.tryAllocPixels(canvas->imageInfo())) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001023 canvas->readPixels(&bm, 0, 0);
1024 }
reed@android.com55e76b22009-11-23 21:46:47 +00001025 return bm;
1026}
1027
reedf7f79d22015-02-21 09:03:20 -08001028static void drawText(SkCanvas* canvas, SkString str, SkScalar left, SkScalar top, SkPaint& paint) {
Scroggo0f185c22011-03-24 18:35:50 +00001029 SkColor desiredColor = paint.getColor();
1030 paint.setColor(SK_ColorWHITE);
reedf7f79d22015-02-21 09:03:20 -08001031 const char* c_str = str.c_str();
1032 size_t size = str.size();
Scroggo0f185c22011-03-24 18:35:50 +00001033 SkRect bounds;
1034 paint.measureText(c_str, size, &bounds);
1035 bounds.offset(left, top);
1036 SkScalar inset = SkIntToScalar(-2);
1037 bounds.inset(inset, inset);
1038 canvas->drawRect(bounds, paint);
Scroggo0f185c22011-03-24 18:35:50 +00001039 paint.setColor(desiredColor);
1040 canvas->drawText(c_str, size, left, top, paint);
1041}
1042
reed@android.com44177402009-11-23 21:07:51 +00001043#define XCLIP_N 8
1044#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +00001045
reedbabc3de2016-07-08 08:43:27 -07001046#include "SkDeferredCanvas.h"
1047#include "SkDumpCanvas.h"
1048
reed@android.come522ca52009-11-23 20:10:41 +00001049void SampleWindow::draw(SkCanvas* canvas) {
reed76113a92015-02-02 12:55:02 -08001050 gAnimTimer.updateTime();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001051
reed@google.comf03bb562011-11-11 21:42:12 +00001052 if (fGesture.isActive()) {
1053 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001054 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001055
djsollen@google.com796763e2012-12-10 14:12:55 +00001056 if (fMeasureFPS) {
1057 fMeasureFPS_Time = 0;
1058 }
1059
reedf7f79d22015-02-21 09:03:20 -08001060 SkSize tile = this->tileSize();
reed@android.come522ca52009-11-23 20:10:41 +00001061
reedf7f79d22015-02-21 09:03:20 -08001062 if (kNo_Tiling == fTilingMode) {
reedbabc3de2016-07-08 08:43:27 -07001063 SkDebugfDumper dumper;
1064 SkDumpCanvas dump(&dumper);
1065 SkDeferredCanvas deferred(canvas);
1066 SkCanvas* c = fUseDeferredCanvas ? &deferred : canvas;
1067 this->INHERITED::draw(c); // no looping or surfaces needed
reed@android.come522ca52009-11-23 20:10:41 +00001068 } else {
bungemance560262016-03-03 13:32:38 -08001069 const SkScalar w = SkScalarCeilToScalar(tile.width());
1070 const SkScalar h = SkScalarCeilToScalar(tile.height());
1071 SkImageInfo info = SkImageInfo::MakeN32Premul(SkScalarTruncToInt(w), SkScalarTruncToInt(h));
reede8f30622016-03-23 18:59:25 -07001072 auto surface(canvas->makeSurface(info));
reedf7f79d22015-02-21 09:03:20 -08001073 SkCanvas* tileCanvas = surface->getCanvas();
rmistry@google.comae933ce2012-08-23 18:19:56 +00001074
bungemance560262016-03-03 13:32:38 -08001075 for (SkScalar y = 0; y < height(); y += h) {
1076 for (SkScalar x = 0; x < width(); x += w) {
reedf7f79d22015-02-21 09:03:20 -08001077 SkAutoCanvasRestore acr(tileCanvas, true);
1078 tileCanvas->translate(-x, -y);
1079 tileCanvas->clear(0);
1080 this->INHERITED::draw(tileCanvas);
halcanary96fcdcc2015-08-27 07:41:13 -07001081 surface->draw(canvas, x, y, nullptr);
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +00001082 }
reedf7f79d22015-02-21 09:03:20 -08001083 }
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +00001084
reedf7f79d22015-02-21 09:03:20 -08001085 // for drawing the borders between tiles
1086 SkPaint paint;
1087 paint.setColor(0x60FF00FF);
1088 paint.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001089
reedf7f79d22015-02-21 09:03:20 -08001090 for (SkScalar y = 0; y < height(); y += tile.height()) {
1091 for (SkScalar x = 0; x < width(); x += tile.width()) {
1092 canvas->drawRect(SkRect::MakeXYWH(x, y, tile.width(), tile.height()), paint);
scroggo@google.com85cade02012-08-17 13:29:50 +00001093 }
1094 }
reed@android.come522ca52009-11-23 20:10:41 +00001095 }
reedf7f79d22015-02-21 09:03:20 -08001096
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001097 if (fShowZoomer && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +00001098 showZoomer(canvas);
1099 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001100 if (fMagnify && !fSaveToPdf) {
1101 magnify(canvas);
1102 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001103
djsollen@google.com796763e2012-12-10 14:12:55 +00001104 if (fMeasureFPS && fMeasureFPS_Time) {
1105 this->updateTitle();
1106 this->postInvalDelay();
1107 }
1108
brianosmanca348462016-02-24 11:47:47 -08001109 if (this->sendAnimatePulse() || FLAGS_redraw) {
halcanary96fcdcc2015-08-27 07:41:13 -07001110 this->inval(nullptr);
reedd9adfe62015-02-01 19:01:04 -08001111 }
1112
reed@google.com29038ed2011-07-06 17:56:47 +00001113 // do this last
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001114 fDevManager->publishCanvas(fDeviceType, canvas, this);
Scroggo3272ba82011-05-25 20:50:42 +00001115}
1116
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001117static float clipW = 200;
1118static float clipH = 200;
1119void SampleWindow::magnify(SkCanvas* canvas) {
1120 SkRect r;
1121 int count = canvas->save();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001122
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001123 SkMatrix m = canvas->getTotalMatrix();
robertphillips@google.com07ef9112012-06-04 13:22:14 +00001124 if (!m.invert(&m)) {
1125 return;
1126 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001127 SkPoint offset, center;
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001128 SkScalar mouseX = fMouseX * SK_Scalar1;
1129 SkScalar mouseY = fMouseY * SK_Scalar1;
1130 m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset);
1131 m.mapXY(mouseX, mouseY, &center);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001132
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001133 r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX());
1134 r.offset(offset.fX, offset.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001135
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001136 SkPaint paint;
1137 paint.setColor(0xFF66AAEE);
1138 paint.setStyle(SkPaint::kStroke_Style);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001139 paint.setStrokeWidth(10.f * m.getScaleX());
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001140 //lense offset
1141 //canvas->translate(0, -250);
1142 canvas->drawRect(r, paint);
1143 canvas->clipRect(r);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001144
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001145 m = canvas->getTotalMatrix();
1146 m.setTranslate(-center.fX, -center.fY);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001147 m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001148 m.postTranslate(center.fX, center.fY);
1149 canvas->concat(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001150
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001151 this->INHERITED::draw(canvas);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001152
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001153 canvas->restoreToCount(count);
1154}
1155
reed5f629b42015-08-15 11:57:23 -07001156static SkPaint& set_color_ref(SkPaint& paint, SkColor c) {
1157 paint.setColor(c);
1158 return paint;
1159}
reed@google.comb36334d2011-05-18 15:07:20 +00001160
reed5f629b42015-08-15 11:57:23 -07001161static void show_lcd_box(SkCanvas* canvas, SkScalar x, SkScalar y, SkColor c,
1162 SkScalar sx, SkScalar sy) {
1163 const SkScalar w = (1 - 1/sx) / 3;
1164 SkPaint paint;
1165 SkRect r = SkRect::MakeXYWH(x, y, w, 1 - 1/sy);
1166 canvas->drawRect(r, set_color_ref(paint, SkColorSetRGB(SkColorGetR(c), 0, 0)));
1167 r.offset(w, 0);
1168 canvas->drawRect(r, set_color_ref(paint, SkColorSetRGB(0, SkColorGetG(c), 0)));
1169 r.offset(w, 0);
1170 canvas->drawRect(r, set_color_ref(paint, SkColorSetRGB(0, 0, SkColorGetB(c))));
1171}
reed@google.comb36334d2011-05-18 15:07:20 +00001172
reed5f629b42015-08-15 11:57:23 -07001173static void show_lcd_circle(SkCanvas* canvas, SkScalar x, SkScalar y, SkColor c,
1174 SkScalar, SkScalar) {
1175 const SkRect r = SkRect::MakeXYWH(x, y, 1, 1);
1176 const SkScalar cx = x + 0.5f;
1177 const SkScalar cy = y + 0.5f;
1178
1179 SkPaint paint;
1180 paint.setAntiAlias(true);
1181
1182 SkPath path;
1183 path.addArc(r, 0, 120); path.lineTo(cx, cy);
1184 canvas->drawPath(path, set_color_ref(paint, SkColorSetRGB(SkColorGetR(c), 0, 0)));
1185
1186 path.reset(); path.addArc(r, 120, 120); path.lineTo(cx, cy);
1187 canvas->drawPath(path, set_color_ref(paint, SkColorSetRGB(0, SkColorGetG(c), 0)));
1188
1189 path.reset(); path.addArc(r, 240, 120); path.lineTo(cx, cy);
1190 canvas->drawPath(path, set_color_ref(paint, SkColorSetRGB(0, 0, SkColorGetB(c))));
1191}
1192
1193typedef void (*ShowLCDProc)(SkCanvas*, SkScalar, SkScalar, SkColor, SkScalar, SkScalar);
1194
1195/*
1196 * Like drawBitmapRect but we manually draw each pixels in RGB
1197 */
1198static void show_lcd_grid(SkCanvas* canvas, const SkBitmap& bitmap,
reed8a41adb2015-08-15 14:02:29 -07001199 const SkIRect& origSrc, const SkRect& dst, ShowLCDProc proc) {
reed5f629b42015-08-15 11:57:23 -07001200 SkIRect src;
1201 if (!src.intersect(origSrc, bitmap.bounds())) {
1202 return;
1203 }
1204 const SkScalar sx = dst.width() / src.width();
1205 const SkScalar sy = dst.height() / src.height();
1206
1207 SkAutoCanvasRestore acr(canvas, true);
1208 canvas->translate(dst.left(), dst.top());
1209 canvas->scale(sx, sy);
1210
reed5f629b42015-08-15 11:57:23 -07001211 for (int y = 0; y < src.height(); ++y) {
1212 for (int x = 0; x < src.width(); ++x) {
1213 proc(canvas, SkIntToScalar(x), SkIntToScalar(y),
1214 bitmap.getColor(src.left() + x, src.top() + y), sx, sy);
Scroggo0f185c22011-03-24 18:35:50 +00001215 }
reed5f629b42015-08-15 11:57:23 -07001216 }
1217}
1218
1219void SampleWindow::showZoomer(SkCanvas* canvas) {
1220 int count = canvas->save();
1221 canvas->resetMatrix();
1222 // Ensure the mouse position is on screen.
1223 int width = SkScalarRoundToInt(this->width());
1224 int height = SkScalarRoundToInt(this->height());
1225 if (fMouseX >= width) fMouseX = width - 1;
1226 else if (fMouseX < 0) fMouseX = 0;
1227 if (fMouseY >= height) fMouseY = height - 1;
1228 else if (fMouseY < 0) fMouseY = 0;
1229
1230 SkBitmap bitmap = capture_bitmap(canvas);
1231 bitmap.lockPixels();
1232
1233 // Find the size of the zoomed in view, forced to be odd, so the examined pixel is in the middle.
1234 int zoomedWidth = (width >> 1) | 1;
1235 int zoomedHeight = (height >> 1) | 1;
1236 SkIRect src;
1237 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
1238 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
1239 SkRect dest;
1240 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
1241 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
1242 SkPaint paint;
1243 // Clear the background behind our zoomed in view
1244 paint.setColor(SK_ColorWHITE);
1245 canvas->drawRect(dest, paint);
reed8a41adb2015-08-15 14:02:29 -07001246 switch (fFatBitsScale) {
1247 case kMaxFatBitsScale:
1248 show_lcd_grid(canvas, bitmap, src, dest, show_lcd_box);
1249 break;
1250 case kMaxFatBitsScale - 1:
1251 show_lcd_grid(canvas, bitmap, src, dest, show_lcd_circle);
1252 break;
1253 default:
halcanary96fcdcc2015-08-27 07:41:13 -07001254 canvas->drawBitmapRect(bitmap, src, dest, nullptr);
reed8a41adb2015-08-15 14:02:29 -07001255 break;
reed5f629b42015-08-15 11:57:23 -07001256 }
1257
1258 paint.setColor(SK_ColorBLACK);
1259 paint.setStyle(SkPaint::kStroke_Style);
1260 // Draw a border around the pixel in the middle
1261 SkRect originalPixel;
1262 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
1263 SkMatrix matrix;
1264 SkRect scalarSrc;
1265 scalarSrc.set(src);
1266 SkColor color = bitmap.getColor(fMouseX, fMouseY);
1267 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
1268 SkRect pixel;
1269 matrix.mapRect(&pixel, originalPixel);
1270 // TODO Perhaps measure the values and make the outline white if it's "dark"
1271 if (color == SK_ColorBLACK) {
1272 paint.setColor(SK_ColorWHITE);
1273 }
1274 canvas->drawRect(pixel, paint);
1275 }
1276 paint.setColor(SK_ColorBLACK);
1277 // Draw a border around the destination rectangle
1278 canvas->drawRect(dest, paint);
1279 paint.setStyle(SkPaint::kStrokeAndFill_Style);
1280 // Identify the pixel and its color on screen
1281 paint.setTypeface(fTypeface);
1282 paint.setAntiAlias(true);
reed5755cd92016-08-21 19:30:13 -07001283 paint.setTextSize(18);
halcanary96fcdcc2015-08-27 07:41:13 -07001284 SkScalar lineHeight = paint.getFontMetrics(nullptr);
reed5f629b42015-08-15 11:57:23 -07001285 SkString string;
1286 string.appendf("(%i, %i)", fMouseX, fMouseY);
1287 SkScalar left = dest.fLeft + SkIntToScalar(3);
1288 SkScalar i = SK_Scalar1;
1289 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1290 // Alpha
1291 i += SK_Scalar1;
1292 string.reset();
1293 string.appendf("A: %X", SkColorGetA(color));
1294 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1295 // Red
1296 i += SK_Scalar1;
1297 string.reset();
1298 string.appendf("R: %X", SkColorGetR(color));
1299 paint.setColor(SK_ColorRED);
1300 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1301 // Green
1302 i += SK_Scalar1;
1303 string.reset();
1304 string.appendf("G: %X", SkColorGetG(color));
reed5755cd92016-08-21 19:30:13 -07001305 paint.setColor(0xFF008800);
reed5f629b42015-08-15 11:57:23 -07001306 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1307 // Blue
1308 i += SK_Scalar1;
1309 string.reset();
1310 string.appendf("B: %X", SkColorGetB(color));
1311 paint.setColor(SK_ColorBLUE);
1312 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1313 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +00001314}
1315
reed@android.com8a1c16f2008-12-17 15:59:43 +00001316void SampleWindow::onDraw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001317}
1318
1319#include "SkColorPriv.h"
1320
Scroggo8ac0d542011-06-21 14:44:57 +00001321void SampleWindow::saveToPdf()
1322{
1323 fSaveToPdf = true;
halcanary96fcdcc2015-08-27 07:41:13 -07001324 this->inval(nullptr);
Scroggo8ac0d542011-06-21 14:44:57 +00001325}
1326
reed@android.com8a1c16f2008-12-17 15:59:43 +00001327SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +00001328 if (fSaveToPdf) {
reedddb5eca2014-10-08 11:10:51 -07001329 SkString name;
1330 if (!this->getRawTitle(&name)) {
1331 name.set("unknown_sample");
1332 }
1333 name.append(".pdf");
1334#ifdef SK_BUILD_FOR_ANDROID
1335 name.prepend("/sdcard/");
1336#endif
halcanary676ab682016-05-03 12:10:04 -07001337 fPDFDocument = SkDocument::MakePDF(name.c_str());
reedddb5eca2014-10-08 11:10:51 -07001338 canvas = fPDFDocument->beginPage(this->width(), this->height());
kkinnunen8686a5e2016-01-05 00:04:32 -08001339 } else if (fSaveToSKP) {
1340 canvas = fRecorder.beginRecording(9999, 9999, nullptr, 0);
reed91b4dfb2016-01-08 08:23:19 -08001341 } else if (fUsePicture) {
reed54dc4872016-09-13 08:09:45 -07001342 fPipeStream.reset(new SkDynamicMemoryWStream);
1343 canvas = fPipeSerializer.beginWrite(SkRect::MakeWH(this->width(), this->height()),
1344 fPipeStream.get());
1345// canvas = fRecorder.beginRecording(9999, 9999, nullptr, 0);
Scroggo8ac0d542011-06-21 14:44:57 +00001346 } else {
bsalomonbb0502e2015-02-11 11:11:11 -08001347 canvas = this->INHERITED::beforeChildren(canvas);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001348 }
1349
1350 if (fUseClip) {
1351 canvas->drawColor(0xFFFF88FF);
reed73603f32016-09-20 08:42:38 -07001352 canvas->clipPath(fClipPath, SkCanvas::kIntersect_Op, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001353 }
1354
fmalita822ace92015-06-15 07:07:32 -07001355 // Install a flags filter proxy canvas if needed
1356 if (fLCDState != SkOSMenu::kMixedState ||
1357 fAAState != SkOSMenu::kMixedState ||
1358 fSubpixelState != SkOSMenu::kMixedState ||
1359 fHintingState > 0 ||
1360 fFilterQualityIndex > 0) {
halcanary385fe4d2015-08-26 13:07:48 -07001361 canvas = new FlagsFilterCanvas(canvas, fLCDState, fAAState, fSubpixelState, fHintingState,
1362 fFilterQualityIndex);
fmalita822ace92015-06-15 07:07:32 -07001363 fFlagsFilterCanvas.reset(canvas);
1364 }
1365
reed@android.com8a1c16f2008-12-17 15:59:43 +00001366 return canvas;
1367}
reed89889b62014-10-29 12:36:45 -07001368#include "SkMultiPictureDraw.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001369void SampleWindow::afterChildren(SkCanvas* orig) {
halcanary96fcdcc2015-08-27 07:41:13 -07001370 fFlagsFilterCanvas.reset(nullptr);
fmalita822ace92015-06-15 07:07:32 -07001371
Scroggo8ac0d542011-06-21 14:44:57 +00001372 if (fSaveToPdf) {
1373 fSaveToPdf = false;
reedddb5eca2014-10-08 11:10:51 -07001374 fPDFDocument->endPage();
halcanary96fcdcc2015-08-27 07:41:13 -07001375 fPDFDocument.reset(nullptr);
Scroggo8ac0d542011-06-21 14:44:57 +00001376 // We took over the draw calls in order to create the PDF, so we need
1377 // to redraw.
halcanary96fcdcc2015-08-27 07:41:13 -07001378 this->inval(nullptr);
Scroggo8ac0d542011-06-21 14:44:57 +00001379 return;
1380 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001381
reed@android.comf2b98d62010-12-20 18:26:13 +00001382 if (fRequestGrabImage) {
1383 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001384
reed52d9ac62014-06-30 09:05:34 -07001385 SkBitmap bmp = capture_bitmap(orig);
1386 if (!bmp.isNull()) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001387 static int gSampleGrabCounter;
1388 SkString name;
reed@google.com7c57e0e2012-09-08 21:20:33 +00001389 name.printf("sample_grab_%d.png", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001390 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +00001391 SkImageEncoder::kPNG_Type, 100);
1392 }
kkinnunen8686a5e2016-01-05 00:04:32 -08001393 this->inval(nullptr);
1394 return;
reed@android.comf2b98d62010-12-20 18:26:13 +00001395 }
1396
kkinnunen8686a5e2016-01-05 00:04:32 -08001397 if (fSaveToSKP) {
reedca2622b2016-03-18 07:25:55 -07001398 sk_sp<SkPicture> picture(fRecorder.finishRecordingAsPicture());
kkinnunen8686a5e2016-01-05 00:04:32 -08001399 SkFILEWStream stream("sample_app.skp");
1400 picture->serialize(&stream);
1401 fSaveToSKP = false;
1402 this->inval(nullptr);
1403 return;
1404 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001405
reed91b4dfb2016-01-08 08:23:19 -08001406 if (fUsePicture) {
reed54dc4872016-09-13 08:09:45 -07001407 if (true) {
1408 fPipeSerializer.endWrite();
1409 sk_sp<SkData> data(fPipeStream->detachAsData());
1410 fPipeDeserializer.playback(data->data(), data->size(), orig);
1411 fPipeStream.reset();
1412 } else {
1413 sk_sp<SkPicture> picture(fRecorder.finishRecordingAsPicture());
1414 orig->drawPicture(picture.get());
reedbfd5f172016-01-07 11:28:08 -08001415 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001416 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001417
reed@google.com17d7aec2011-04-25 14:31:44 +00001418 // Do this after presentGL and other finishing, rather than in afterChild
jvanverth4d0d81a2015-08-11 11:07:07 -07001419 if (fMeasureFPS) {
reedb2ce0b02015-11-09 13:10:30 -08001420 orig->flush();
jvanverth4d0d81a2015-08-11 11:07:07 -07001421 fTimer.end();
1422 fMeasureFPS_Time += fTimer.fWall;
reed@google.com17d7aec2011-04-25 14:31:44 +00001423 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001424}
1425
reed@android.com6c5f6f22009-08-14 16:08:38 +00001426void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
reed@android.com6c5f6f22009-08-14 16:08:38 +00001427 if (fRotate) {
1428 SkScalar cx = this->width() / 2;
1429 SkScalar cy = this->height() / 2;
bungeman7438bfc2016-07-12 15:01:19 -07001430 canvas->rotate(gAnimTimer.scaled(10), cx, cy);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001431 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001432
bsalomon@google.come8f09102011-09-08 18:48:12 +00001433 if (fPerspAnim) {
reed76113a92015-02-02 12:55:02 -08001434 SkScalar secs = gAnimTimer.scaled(1);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001435
1436 static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
1437 static const SkScalar gAnimMag = SK_Scalar1 / 1000;
reed76113a92015-02-02 12:55:02 -08001438 SkScalar t = SkScalarMod(secs, gAnimPeriod);
reed80ea19c2015-05-12 10:37:34 -07001439 if (SkScalarFloorToInt(secs / gAnimPeriod) & 0x1) {
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001440 t = gAnimPeriod - t;
1441 }
1442 t = 2 * t - gAnimPeriod;
reed80ea19c2015-05-12 10:37:34 -07001443 t *= gAnimMag / gAnimPeriod;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001444 SkMatrix m;
1445 m.reset();
reed5d9ab282014-09-19 13:04:17 -07001446#if 1
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001447 m.setPerspY(t);
reed5d9ab282014-09-19 13:04:17 -07001448#else
1449 m.setPerspY(SK_Scalar1 / 1000);
reed80ea19c2015-05-12 10:37:34 -07001450 m.setSkewX(8.0f / 25);
reed5d9ab282014-09-19 13:04:17 -07001451 m.dump();
1452#endif
bsalomon@google.come8f09102011-09-08 18:48:12 +00001453 canvas->concat(m);
1454 }
reed@google.comf0b5f682011-03-11 20:08:25 +00001455
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001456 if (fMeasureFPS) {
jvanverth4d0d81a2015-08-11 11:07:07 -07001457 (void)SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT);
1458 fTimer.start();
reed@google.comf2183392011-04-22 14:10:48 +00001459 } else {
1460 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001461 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001462 if (fPerspAnim || fRotate) {
halcanary96fcdcc2015-08-27 07:41:13 -07001463 this->inval(nullptr);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001464 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001465}
1466
bungemance560262016-03-03 13:32:38 -08001467void SampleWindow::changeOffset(SkVector delta) {
1468 fOffset += delta;
1469 this->updateMatrix();
1470}
1471
djsollen@google.come32b5832011-06-13 16:58:40 +00001472void SampleWindow::changeZoomLevel(float delta) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001473 fZoomLevel += delta;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001474 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001475 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
1476 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001477 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001478 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
1479 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001480 } else {
1481 fZoomScale = SK_Scalar1;
1482 }
reed@google.comf03bb562011-11-11 21:42:12 +00001483 this->updateMatrix();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001484}
1485
reed@google.comf03bb562011-11-11 21:42:12 +00001486void SampleWindow::updateMatrix(){
1487 SkMatrix m;
1488 m.reset();
bungemance560262016-03-03 13:32:38 -08001489
reed@google.comf03bb562011-11-11 21:42:12 +00001490 if (fZoomLevel) {
1491 SkPoint center;
1492 //m = this->getLocalMatrix();//.invert(&m);
1493 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
1494 SkScalar cx = center.fX;
1495 SkScalar cy = center.fY;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001496
reed@google.comf03bb562011-11-11 21:42:12 +00001497 m.setTranslate(-cx, -cy);
1498 m.postScale(fZoomScale, fZoomScale);
1499 m.postTranslate(cx, cy);
1500 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001501
bungemance560262016-03-03 13:32:38 -08001502 m.postTranslate(fOffset.fX, fOffset.fY);
1503
reed@google.comf03bb562011-11-11 21:42:12 +00001504 if (fFlipAxis) {
1505 m.preTranslate(fZoomCenterX, fZoomCenterY);
1506 if (fFlipAxis & kFlipAxis_X) {
1507 m.preScale(-SK_Scalar1, SK_Scalar1);
1508 }
1509 if (fFlipAxis & kFlipAxis_Y) {
1510 m.preScale(SK_Scalar1, -SK_Scalar1);
1511 }
1512 m.preTranslate(-fZoomCenterX, -fZoomCenterY);
1513 //canvas->concat(m);
1514 }
1515 // Apply any gesture matrix
1516 m.preConcat(fGesture.localM());
1517 m.preConcat(fGesture.globalM());
chudy@google.com4605a3f2012-08-01 17:58:01 +00001518
reed@google.comf03bb562011-11-11 21:42:12 +00001519 this->setLocalMatrix(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001520
reed@google.comf03bb562011-11-11 21:42:12 +00001521 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001522 this->inval(nullptr);
reed@google.comf03bb562011-11-11 21:42:12 +00001523}
Scroggo2c8208f2011-06-15 16:49:08 +00001524bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +00001525 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
reed63121f02015-01-13 19:43:04 -08001526 this->loadView((*fSamples[fCurrIndex])());
Scroggo2c8208f2011-06-15 16:49:08 +00001527 return true;
1528}
1529
reed216b6432015-08-19 12:25:40 -07001530#include "SkResourceCache.h"
1531#include "SkGlyphCache.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001532bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +00001533 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
reed63121f02015-01-13 19:43:04 -08001534 this->loadView((*fSamples[fCurrIndex])());
reed216b6432015-08-19 12:25:40 -07001535
1536 if (false) {
1537 SkResourceCache::TestDumpMemoryStatistics();
1538 SkGlyphCache::Dump();
1539 SkDebugf("\n");
1540 }
1541
reed@android.com34245c72009-11-03 04:00:48 +00001542 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001543}
1544
yangsu@google.com501775e2011-06-24 16:04:50 +00001545bool SampleWindow::goToSample(int i) {
1546 fCurrIndex = (i) % fSamples.count();
reed63121f02015-01-13 19:43:04 -08001547 this->loadView((*fSamples[fCurrIndex])());
yangsu@google.com501775e2011-06-24 16:04:50 +00001548 return true;
1549}
1550
1551SkString SampleWindow::getSampleTitle(int i) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001552 return ::getSampleTitle(fSamples[i]);
yangsu@google.com501775e2011-06-24 16:04:50 +00001553}
1554
1555int SampleWindow::sampleCount() {
1556 return fSamples.count();
1557}
1558
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001559void SampleWindow::showOverview() {
reed63121f02015-01-13 19:43:04 -08001560 this->loadView(create_overview(fSamples.count(), fSamples.begin()));
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001561}
1562
Scroggo2c8208f2011-06-15 16:49:08 +00001563void SampleWindow::postAnimatingEvent() {
1564 if (fAnimating) {
reed@google.com87fac4a2011-08-04 13:50:17 +00001565 (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
Scroggo2c8208f2011-06-15 16:49:08 +00001566 }
1567}
reed@google.come23f1942011-12-08 19:36:00 +00001568
reed@android.com8a1c16f2008-12-17 15:59:43 +00001569bool SampleWindow::onEvent(const SkEvent& evt) {
reed@google.com2072db82011-11-08 15:18:10 +00001570 if (evt.isType(gUpdateWindowTitleEvtName)) {
1571 this->updateTitle();
1572 return true;
1573 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001574 if (evt.isType(ANIMATING_EVENTTYPE)) {
1575 if (fAnimating) {
1576 this->nextSample();
1577 this->postAnimatingEvent();
1578 }
1579 return true;
1580 }
reed@android.com34245c72009-11-03 04:00:48 +00001581 if (evt.isType("set-curr-index")) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001582 this->goToSample(evt.getFast32());
reed@android.com34245c72009-11-03 04:00:48 +00001583 return true;
1584 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001585 if (isInvalEvent(evt)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001586 this->inval(nullptr);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001587 return true;
1588 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001589 int selected = -1;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001590 if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001591 this->setDeviceType((DeviceType)selected);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001592 return true;
yangsu@google.com921091f2011-08-02 13:39:12 +00001593 }
reeda34be682016-02-15 07:48:35 -08001594 if (SkOSMenu::FindListIndex(evt, "ColorType", &selected)) {
brianosman0e22eb82016-08-30 07:07:59 -07001595 auto colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
1596 if (kRGBA_F16_SkColorType == gConfig[selected].fColorType) {
1597 colorSpace = colorSpace->makeLinearGamma();
1598 }
brianosmanb109b8c2016-06-16 13:03:24 -07001599 this->setDeviceColorType(gConfig[selected].fColorType,
brianosman0e22eb82016-08-30 07:07:59 -07001600 gConfig[selected].fSRGB ? colorSpace : nullptr);
reeda34be682016-02-15 07:48:35 -08001601 return true;
1602 }
halcanary96fcdcc2015-08-27 07:41:13 -07001603 if (SkOSMenu::FindSwitchState(evt, "Slide Show", nullptr)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001604 this->toggleSlideshow();
1605 return true;
1606 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001607 if (SkOSMenu::FindTriState(evt, "AA", &fAAState) ||
1608 SkOSMenu::FindTriState(evt, "LCD", &fLCDState) ||
reed93a12152015-03-16 10:08:34 -07001609 SkOSMenu::FindListIndex(evt, "FilterQuality", &fFilterQualityIndex) ||
bungeman@google.com96aabc82013-06-03 21:26:34 +00001610 SkOSMenu::FindTriState(evt, "Subpixel", &fSubpixelState) ||
1611 SkOSMenu::FindListIndex(evt, "Hinting", &fHintingState) ||
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001612 SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) ||
1613 SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
reeda84d6602016-06-24 18:14:07 -07001614 SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify))
reed093b4e82015-08-04 18:46:38 -07001615 {
halcanary96fcdcc2015-08-27 07:41:13 -07001616 this->inval(nullptr);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00001617 this->updateTitle();
1618 return true;
1619 }
bungeman5a59a422016-03-31 08:30:38 -07001620 if (SkOSMenu::FindListIndex(evt, "Pixel Geometry", &fPixelGeometryIndex)) {
1621 this->setPixelGeometry(fPixelGeometryIndex);
1622 return true;
1623 }
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00001624 if (SkOSMenu::FindListIndex(evt, "Tiling", &fTilingMode)) {
1625 if (SampleView::IsSampleView(curr_view(this))) {
1626 ((SampleView*)curr_view(this))->onTileSizeChanged(this->tileSize());
1627 }
halcanary96fcdcc2015-08-27 07:41:13 -07001628 this->inval(nullptr);
yangsu@google.com921091f2011-08-02 13:39:12 +00001629 this->updateTitle();
1630 return true;
1631 }
halcanary96fcdcc2015-08-27 07:41:13 -07001632 if (SkOSMenu::FindSwitchState(evt, "Flip X", nullptr)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001633 fFlipAxis ^= kFlipAxis_X;
reed@google.comf03bb562011-11-11 21:42:12 +00001634 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001635 return true;
1636 }
halcanary96fcdcc2015-08-27 07:41:13 -07001637 if (SkOSMenu::FindSwitchState(evt, "Flip Y", nullptr)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001638 fFlipAxis ^= kFlipAxis_Y;
reed@google.comf03bb562011-11-11 21:42:12 +00001639 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001640 return true;
1641 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001642 if (SkOSMenu::FindAction(evt,"Save to PDF")) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001643 this->saveToPdf();
1644 return true;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001645 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001646 return this->INHERITED::onEvent(evt);
1647}
1648
reed@android.comf2b98d62010-12-20 18:26:13 +00001649bool SampleWindow::onQuery(SkEvent* query) {
1650 if (query->isType("get-slide-count")) {
1651 query->setFast32(fSamples.count());
1652 return true;
1653 }
1654 if (query->isType("get-slide-title")) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001655 SkView* view = (*fSamples[query->getFast32()])();
reed@android.comf2b98d62010-12-20 18:26:13 +00001656 SkEvent evt(gTitleEvtName);
1657 if (view->doQuery(&evt)) {
1658 query->setString("title", evt.findString(gTitleEvtName));
1659 }
1660 SkSafeUnref(view);
1661 return true;
1662 }
1663 if (query->isType("use-fast-text")) {
1664 SkEvent evt(gFastTextEvtName);
1665 return curr_view(this)->doQuery(&evt);
1666 }
reed@google.com29038ed2011-07-06 17:56:47 +00001667 if (query->isType("ignore-window-bitmap")) {
halcanary96fcdcc2015-08-27 07:41:13 -07001668 query->setFast32(this->getGrContext() != nullptr);
reed@google.com29038ed2011-07-06 17:56:47 +00001669 return true;
1670 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001671 return this->INHERITED::onQuery(query);
1672}
1673
Cary Clark992c7b02014-07-31 08:58:44 -04001674DECLARE_bool(portableFonts);
caryclark5fb6bd42014-06-23 11:25:00 -07001675
reed@android.com8a1c16f2008-12-17 15:59:43 +00001676bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001677 {
1678 SkView* view = curr_view(this);
1679 if (view) {
1680 SkEvent evt(gCharEvtName);
1681 evt.setFast32(uni);
1682 if (view->doQuery(&evt)) {
1683 return true;
1684 }
1685 }
1686 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001687
reed@android.com8a1c16f2008-12-17 15:59:43 +00001688 int dx = 0xFF;
1689 int dy = 0xFF;
1690
1691 switch (uni) {
1692 case '5': dx = 0; dy = 0; break;
1693 case '8': dx = 0; dy = -1; break;
1694 case '6': dx = 1; dy = 0; break;
1695 case '2': dx = 0; dy = 1; break;
1696 case '4': dx = -1; dy = 0; break;
1697 case '7': dx = -1; dy = -1; break;
1698 case '9': dx = 1; dy = -1; break;
1699 case '3': dx = 1; dy = 1; break;
1700 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001701
reed@android.com8a1c16f2008-12-17 15:59:43 +00001702 default:
1703 break;
1704 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001705
reed@android.com8a1c16f2008-12-17 15:59:43 +00001706 if (0xFF != dx && 0xFF != dy) {
bungemance560262016-03-03 13:32:38 -08001707 this->changeOffset({SkIntToScalar(dx / 32.0f), SkIntToScalar(dy / 32.0f)});
reed@android.com8a1c16f2008-12-17 15:59:43 +00001708 return true;
1709 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001710
reed@android.com0ae6b242008-12-23 16:49:54 +00001711 switch (uni) {
reed76113a92015-02-02 12:55:02 -08001712 case 27: // ESC
1713 gAnimTimer.stop();
1714 if (this->sendAnimatePulse()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001715 this->inval(nullptr);
reed76113a92015-02-02 12:55:02 -08001716 }
1717 break;
reedd9adfe62015-02-01 19:01:04 -08001718 case ' ':
reed76113a92015-02-02 12:55:02 -08001719 gAnimTimer.togglePauseResume();
reedd9adfe62015-02-01 19:01:04 -08001720 if (this->sendAnimatePulse()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001721 this->inval(nullptr);
reedd9adfe62015-02-01 19:01:04 -08001722 }
1723 break;
reed@google.coma4f81372012-11-15 15:56:38 +00001724 case 'B':
halcanary385fe4d2015-08-26 13:07:48 -07001725 post_event_to_sink(new SkEvent("PictFileView::toggleBBox"), curr_view(this));
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001726 // Cannot call updateTitle() synchronously, because the toggleBBox event is still in
1727 // the queue.
halcanary385fe4d2015-08-26 13:07:48 -07001728 post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this);
halcanary96fcdcc2015-08-27 07:41:13 -07001729 this->inval(nullptr);
reed@google.coma4f81372012-11-15 15:56:38 +00001730 break;
reed4302ae92014-10-06 12:29:56 -07001731 case 'D':
1732 toggleDistanceFieldFonts();
1733 break;
reedbabc3de2016-07-08 08:43:27 -07001734 case 'E':
1735 fUseDeferredCanvas = !fUseDeferredCanvas;
1736 this->inval(nullptr);
1737 break;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001738 case 'f':
chudy@google.com4605a3f2012-08-01 17:58:01 +00001739 // only
djsollen@google.com6ff82552011-11-07 15:43:57 +00001740 toggleFPS();
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001741 break;
caryclark5fb6bd42014-06-23 11:25:00 -07001742 case 'F':
Cary Clark992c7b02014-07-31 08:58:44 -04001743 FLAGS_portableFonts ^= true;
halcanary96fcdcc2015-08-27 07:41:13 -07001744 this->inval(nullptr);
caryclark5fb6bd42014-06-23 11:25:00 -07001745 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001746 case 'g':
1747 fRequestGrabImage = true;
halcanary96fcdcc2015-08-27 07:41:13 -07001748 this->inval(nullptr);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001749 break;
reed@google.coma4f81372012-11-15 15:56:38 +00001750 case 'G':
1751 gShowGMBounds = !gShowGMBounds;
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001752 post_event_to_sink(GMSampleView::NewShowSizeEvt(gShowGMBounds),
reed@google.coma4f81372012-11-15 15:56:38 +00001753 curr_view(this));
halcanary96fcdcc2015-08-27 07:41:13 -07001754 this->inval(nullptr);
reed@google.coma4f81372012-11-15 15:56:38 +00001755 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001756 case 'i':
1757 this->zoomIn();
1758 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001759 case 'o':
1760 this->zoomOut();
1761 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001762 case 'r':
1763 fRotate = !fRotate;
halcanary96fcdcc2015-08-27 07:41:13 -07001764 this->inval(nullptr);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001765 this->updateTitle();
1766 return true;
bsalomon@google.come8f09102011-09-08 18:48:12 +00001767 case 'k':
1768 fPerspAnim = !fPerspAnim;
halcanary96fcdcc2015-08-27 07:41:13 -07001769 this->inval(nullptr);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001770 this->updateTitle();
1771 return true;
reed093b4e82015-08-04 18:46:38 -07001772 case 'K':
1773 fSaveToSKP = true;
halcanary96fcdcc2015-08-27 07:41:13 -07001774 this->inval(nullptr);
reed093b4e82015-08-04 18:46:38 -07001775 return true;
kkinnunen8686a5e2016-01-05 00:04:32 -08001776 case 'M':
reed91b4dfb2016-01-08 08:23:19 -08001777 fUsePicture = !fUsePicture;
kkinnunen8686a5e2016-01-05 00:04:32 -08001778 this->inval(nullptr);
1779 this->updateTitle();
1780 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001781#if SK_SUPPORT_GPU
twiz@google.com05e70242012-01-27 19:12:00 +00001782 case 'p':
1783 {
1784 GrContext* grContext = this->getGrContext();
1785 if (grContext) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001786 size_t cacheBytes;
halcanary96fcdcc2015-08-27 07:41:13 -07001787 grContext->getResourceCacheUsage(nullptr, &cacheBytes);
twiz@google.com05e70242012-01-27 19:12:00 +00001788 grContext->freeGpuResources();
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001789 SkDebugf("Purged %d bytes from the GPU resource cache.\n", cacheBytes);
twiz@google.com05e70242012-01-27 19:12:00 +00001790 }
1791 }
1792 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001793#endif
reed@android.com0ae6b242008-12-23 16:49:54 +00001794 default:
1795 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001796 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001797
scroggo@google.com7dadc742012-04-18 14:07:57 +00001798 if (fAppMenu->handleKeyEquivalent(uni)|| fSlideMenu->handleKeyEquivalent(uni)) {
1799 this->onUpdateMenu(fAppMenu);
1800 this->onUpdateMenu(fSlideMenu);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001801 return true;
1802 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001803 return this->INHERITED::onHandleChar(uni);
1804}
1805
yangsu@google.com921091f2011-08-02 13:39:12 +00001806void SampleWindow::setDeviceType(DeviceType type) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001807 if (type == fDeviceType)
1808 return;
halcanary9d524f22016-03-29 09:03:52 -07001809
reeda34be682016-02-15 07:48:35 -08001810 fDevManager->tearDownBackend(this);
reeda34be682016-02-15 07:48:35 -08001811 fDeviceType = type;
brianosman2d1ee792016-05-05 12:24:31 -07001812 fDevManager->setUpBackend(this, fMSAASampleCount, fDeepColor);
halcanary9d524f22016-03-29 09:03:52 -07001813
reeda34be682016-02-15 07:48:35 -08001814 this->updateTitle();
1815 this->inval(nullptr);
1816}
1817
brianosmanb109b8c2016-06-16 13:03:24 -07001818void SampleWindow::setDeviceColorType(SkColorType ct, sk_sp<SkColorSpace> cs) {
1819 this->setColorType(ct, std::move(cs));
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001820
1821 fDevManager->tearDownBackend(this);
brianosman2d1ee792016-05-05 12:24:31 -07001822 fDevManager->setUpBackend(this, fMSAASampleCount, fDeepColor);
halcanary9d524f22016-03-29 09:03:52 -07001823
yangsu@google.com921091f2011-08-02 13:39:12 +00001824 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001825 this->inval(nullptr);
yangsu@google.com921091f2011-08-02 13:39:12 +00001826}
1827
Scroggo2c8208f2011-06-15 16:49:08 +00001828void SampleWindow::toggleSlideshow() {
1829 fAnimating = !fAnimating;
1830 this->postAnimatingEvent();
1831 this->updateTitle();
1832}
1833
1834void SampleWindow::toggleRendering() {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001835 this->setDeviceType(cycle_devicetype(fDeviceType));
Scroggo2c8208f2011-06-15 16:49:08 +00001836 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001837 this->inval(nullptr);
Scroggo2c8208f2011-06-15 16:49:08 +00001838}
1839
djsollen@google.com6ff82552011-11-07 15:43:57 +00001840void SampleWindow::toggleFPS() {
1841 fMeasureFPS = !fMeasureFPS;
1842 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001843 this->inval(nullptr);
djsollen@google.com6ff82552011-11-07 15:43:57 +00001844}
1845
reed4302ae92014-10-06 12:29:56 -07001846void SampleWindow::toggleDistanceFieldFonts() {
jvanverth629162d2015-11-08 08:07:24 -08001847 // reset backend
1848 fDevManager->tearDownBackend(this);
brianosman2d1ee792016-05-05 12:24:31 -07001849 fDevManager->setUpBackend(this, fMSAASampleCount, fDeepColor);
jvanverth629162d2015-11-08 08:07:24 -08001850
reed4302ae92014-10-06 12:29:56 -07001851 SkSurfaceProps props = this->getSurfaceProps();
bsalomonafcd7cd2015-08-31 12:39:41 -07001852 uint32_t flags = props.flags() ^ SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
reed4302ae92014-10-06 12:29:56 -07001853 this->setSurfaceProps(SkSurfaceProps(flags, props.pixelGeometry()));
1854
1855 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001856 this->inval(nullptr);
reed4302ae92014-10-06 12:29:56 -07001857}
1858
bungeman5a59a422016-03-31 08:30:38 -07001859void SampleWindow::setPixelGeometry(int pixelGeometryIndex) {
1860 // reset backend
1861 fDevManager->tearDownBackend(this);
brianosman2d1ee792016-05-05 12:24:31 -07001862 fDevManager->setUpBackend(this, fMSAASampleCount, fDeepColor);
bungeman5a59a422016-03-31 08:30:38 -07001863
1864 const SkSurfaceProps& oldProps = this->getSurfaceProps();
1865 SkSurfaceProps newProps(oldProps.flags(), SkSurfaceProps::kLegacyFontHost_InitType);
1866 if (pixelGeometryIndex > 0) {
1867 newProps = SkSurfaceProps(oldProps.flags(),
1868 gPixelGeometryStates[pixelGeometryIndex].pixelGeometry);
1869 }
1870 this->setSurfaceProps(newProps);
1871
1872 this->updateTitle();
1873 this->inval(nullptr);
1874}
1875
reed@android.com8a1c16f2008-12-17 15:59:43 +00001876#include "SkDumpCanvas.h"
1877
1878bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001879 {
1880 SkView* view = curr_view(this);
1881 if (view) {
1882 SkEvent evt(gKeyEvtName);
1883 evt.setFast32(key);
1884 if (view->doQuery(&evt)) {
1885 return true;
1886 }
1887 }
1888 }
bungemance560262016-03-03 13:32:38 -08001889
1890 int dx = 0xFF;
1891 int dy = 0xFF;
1892
reed@android.com8a1c16f2008-12-17 15:59:43 +00001893 switch (key) {
1894 case kRight_SkKey:
1895 if (this->nextSample()) {
1896 return true;
1897 }
1898 break;
1899 case kLeft_SkKey:
scroggo@google.come2dd9732012-08-15 20:03:06 +00001900 if (this->previousSample()) {
1901 return true;
1902 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001903 return true;
1904 case kUp_SkKey:
reedf7f79d22015-02-21 09:03:20 -08001905 this->changeZoomLevel(1.f / 32.f);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001906 return true;
1907 case kDown_SkKey:
reedf7f79d22015-02-21 09:03:20 -08001908 this->changeZoomLevel(-1.f / 32.f);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001909 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001910 case kOK_SkKey: {
1911 SkString title;
1912 if (curr_title(this, &title)) {
1913 writeTitleToPrefs(title.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001914 }
1915 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001916 }
reed@android.com34245c72009-11-03 04:00:48 +00001917 case kBack_SkKey:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001918 this->showOverview();
reed@android.com34245c72009-11-03 04:00:48 +00001919 return true;
bungemance560262016-03-03 13:32:38 -08001920
1921 case k5_SkKey: dx = 0; dy = 0; break;
1922 case k8_SkKey: dx = 0; dy = -1; break;
1923 case k6_SkKey: dx = 1; dy = 0; break;
1924 case k2_SkKey: dx = 0; dy = 1; break;
1925 case k4_SkKey: dx = -1; dy = 0; break;
1926 case k7_SkKey: dx = -1; dy = -1; break;
1927 case k9_SkKey: dx = 1; dy = -1; break;
1928 case k3_SkKey: dx = 1; dy = 1; break;
1929 case k1_SkKey: dx = -1; dy = 1; break;
1930
reed@android.com8a1c16f2008-12-17 15:59:43 +00001931 default:
1932 break;
1933 }
bungemance560262016-03-03 13:32:38 -08001934
1935 if (0xFF != dx && 0xFF != dy) {
1936 this->changeOffset({SkIntToScalar(dx / 32.0f), SkIntToScalar(dy / 32.0f)});
1937 return true;
1938 }
1939
reed@android.com8a1c16f2008-12-17 15:59:43 +00001940 return this->INHERITED::onHandleKey(key);
1941}
1942
reed@google.com52f57e12011-03-16 12:10:02 +00001943///////////////////////////////////////////////////////////////////////////////
1944
1945static const char gGestureClickType[] = "GestureClickType";
1946
Scroggod3aed392011-06-22 13:26:56 +00001947bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
reed@google.com4d5c26d2013-01-08 16:17:50 +00001948 void* owner, unsigned modi) {
Scroggo0f185c22011-03-24 18:35:50 +00001949 if (Click::kMoved_State == state) {
1950 updatePointer(x, y);
1951 }
reed@google.come1ca7052013-12-17 19:22:07 +00001952 int w = SkScalarRoundToInt(this->width());
1953 int h = SkScalarRoundToInt(this->height());
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001954
1955 // check for the resize-box
1956 if (w - x < 16 && h - y < 16) {
1957 return false; // let the OS handle the click
chudy@google.com4605a3f2012-08-01 17:58:01 +00001958 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001959 else if (fMagnify) {
1960 //it's only necessary to update the drawing if there's a click
halcanary96fcdcc2015-08-27 07:41:13 -07001961 this->inval(nullptr);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001962 return false; //prevent dragging while magnify is enabled
reed@google.com4d5c26d2013-01-08 16:17:50 +00001963 } else {
1964 // capture control+option, and trigger debugger
1965 if ((modi & kControl_SkModifierKey) && (modi & kOption_SkModifierKey)) {
1966 if (Click::kDown_State == state) {
1967 SkEvent evt("debug-hit-test");
1968 evt.setS32("debug-hit-test-x", x);
1969 evt.setS32("debug-hit-test-y", y);
1970 curr_view(this)->doEvent(evt);
1971 }
1972 return true;
1973 } else {
1974 return this->INHERITED::onDispatchClick(x, y, state, owner, modi);
1975 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001976 }
1977}
1978
reed@google.com52f57e12011-03-16 12:10:02 +00001979class GestureClick : public SkView::Click {
1980public:
1981 GestureClick(SkView* target) : SkView::Click(target) {
1982 this->setType(gGestureClickType);
1983 }
1984
1985 static bool IsGesture(Click* click) {
1986 return click->isType(gGestureClickType);
1987 }
1988};
1989
reed@google.com4d5c26d2013-01-08 16:17:50 +00001990SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y,
1991 unsigned modi) {
reed@google.com52f57e12011-03-16 12:10:02 +00001992 return new GestureClick(this);
1993}
1994
1995bool SampleWindow::onClick(Click* click) {
1996 if (GestureClick::IsGesture(click)) {
bsalomon@google.com4d4f2812011-12-12 18:34:01 +00001997 float x = static_cast<float>(click->fICurr.fX);
1998 float y = static_cast<float>(click->fICurr.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001999
reed@google.com52f57e12011-03-16 12:10:02 +00002000 switch (click->fState) {
2001 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00002002 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00002003 break;
2004 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00002005 fGesture.touchMoved(click->fOwner, x, y);
reed@google.comf03bb562011-11-11 21:42:12 +00002006 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002007 break;
2008 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00002009 fGesture.touchEnd(click->fOwner);
reed@google.comf03bb562011-11-11 21:42:12 +00002010 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002011 break;
2012 }
2013 return true;
2014 }
2015 return false;
2016}
2017
2018///////////////////////////////////////////////////////////////////////////////
2019
reed@android.com8a1c16f2008-12-17 15:59:43 +00002020void SampleWindow::loadView(SkView* view) {
2021 SkView::F2BIter iter(this);
2022 SkView* prev = iter.next();
2023 if (prev) {
2024 prev->detachFromParent();
2025 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002026
reed@android.com8a1c16f2008-12-17 15:59:43 +00002027 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00002028 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002029 this->attachChildToFront(view)->unref();
2030 view->setSize(this->width(), this->height());
2031
yangsu@google.com921091f2011-08-02 13:39:12 +00002032 //repopulate the slide menu when a view is loaded
scroggo@google.com7dadc742012-04-18 14:07:57 +00002033 fSlideMenu->reset();
djsollen@google.com388974f2013-01-17 13:20:01 +00002034
scroggo@google.com7dadc742012-04-18 14:07:57 +00002035 this->onUpdateMenu(fSlideMenu);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002036 this->updateTitle();
2037}
2038
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002039static const char* gDeviceTypePrefix[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002040 "raster: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002041#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00002042 "opengl: ",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00002043#if SK_ANGLE
2044 "angle: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002045#endif // SK_ANGLE
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002046#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +00002047};
bungeman99fe8222015-08-20 07:57:51 -07002048static_assert(SK_ARRAY_COUNT(gDeviceTypePrefix) == SampleWindow::kDeviceTypeCnt,
2049 "array_size_mismatch");
reed@android.com8a1c16f2008-12-17 15:59:43 +00002050
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002051static const char* trystate_str(SkOSMenu::TriState state,
reed@google.com569e0432011-04-05 13:07:03 +00002052 const char trueStr[], const char falseStr[]) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002053 if (SkOSMenu::kOnState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002054 return trueStr;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002055 } else if (SkOSMenu::kOffState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002056 return falseStr;
2057 }
halcanary96fcdcc2015-08-27 07:41:13 -07002058 return nullptr;
reed@google.com569e0432011-04-05 13:07:03 +00002059}
2060
reedddb5eca2014-10-08 11:10:51 -07002061bool SampleWindow::getRawTitle(SkString* title) {
2062 return curr_title(this, title);
2063}
reed@android.com8a1c16f2008-12-17 15:59:43 +00002064
reedddb5eca2014-10-08 11:10:51 -07002065void SampleWindow::updateTitle() {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002066 SkString title;
reedddb5eca2014-10-08 11:10:51 -07002067 if (!this->getRawTitle(&title)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002068 title.set("<unknown>");
2069 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002070
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002071 title.prepend(gDeviceTypePrefix[fDeviceType]);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002072
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00002073 if (fTilingMode != kNo_Tiling) {
2074 title.prependf("<T: %s> ", gTilingInfo[fTilingMode].label);
2075 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002076 if (fAnimating) {
2077 title.prepend("<A> ");
2078 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00002079 if (fRotate) {
2080 title.prepend("<R> ");
2081 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00002082 if (fPerspAnim) {
2083 title.prepend("<K> ");
2084 }
bsalomonafcd7cd2015-08-31 12:39:41 -07002085 if (this->getSurfaceProps().flags() & SkSurfaceProps::kUseDeviceIndependentFonts_Flag) {
2086 title.prepend("<DIF> ");
reed4302ae92014-10-06 12:29:56 -07002087 }
reed91b4dfb2016-01-08 08:23:19 -08002088 if (fUsePicture) {
2089 title.prepend("<P> ");
kkinnunen8686a5e2016-01-05 00:04:32 -08002090 }
reedbabc3de2016-07-08 08:43:27 -07002091 if (fUseDeferredCanvas) {
2092 title.prepend("<E> ");
2093 }
reed@google.com569e0432011-04-05 13:07:03 +00002094
2095 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
2096 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed93a12152015-03-16 10:08:34 -07002097 title.prepend(gFilterQualityStates[fFilterQualityIndex].fLabel);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002098 title.prepend(trystate_str(fSubpixelState, "S ", "s "));
halcanary96fcdcc2015-08-27 07:41:13 -07002099 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : nullptr);
2100 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : nullptr);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002101 title.prepend(gHintingStates[fHintingState].label);
bungeman5a59a422016-03-31 08:30:38 -07002102 title.prepend(gPixelGeometryStates[fPixelGeometryIndex].label);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002103
bungemance560262016-03-03 13:32:38 -08002104 if (fOffset.fX || fOffset.fY) {
2105 title.prependf("(%.2f, %.2f) ", SkScalarToFloat(fOffset.fX), SkScalarToFloat(fOffset.fY));
2106 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002107 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00002108 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002109 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002110
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002111 if (fMeasureFPS) {
jvanverth4d0d81a2015-08-11 11:07:07 -07002112 title.appendf(" %8.4f ms", fMeasureFPS_Time / (float)FPS_REPEAT_COUNT);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002113 }
reedddb5eca2014-10-08 11:10:51 -07002114
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002115#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00002116 if (IsGpuDeviceType(fDeviceType) &&
bsalomon49f085d2014-09-05 13:34:00 -07002117 fDevManager &&
robertphillipsecf3dbe2016-07-28 15:17:34 -07002118 fDevManager->numColorSamples() > 0) {
bsalomon@google.com11959252012-04-06 20:13:38 +00002119 title.appendf(" [MSAA: %d]",
robertphillipsecf3dbe2016-07-28 15:17:34 -07002120 fDevManager->numColorSamples());
bsalomon@google.com11959252012-04-06 20:13:38 +00002121 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002122#endif
bsalomon@google.com11959252012-04-06 20:13:38 +00002123
reeda34be682016-02-15 07:48:35 -08002124 title.appendf(" %s", find_config_name(this->info()));
2125
brianosman2d1ee792016-05-05 12:24:31 -07002126 if (fDevManager && fDevManager->getColorBits() > 24) {
2127 title.appendf(" %d bpc", fDevManager->getColorBits());
2128 }
2129
reed@android.com8a1c16f2008-12-17 15:59:43 +00002130 this->setTitle(title.c_str());
2131}
2132
2133void SampleWindow::onSizeChange() {
2134 this->INHERITED::onSizeChange();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002135
reed@android.com8a1c16f2008-12-17 15:59:43 +00002136 SkView::F2BIter iter(this);
2137 SkView* view = iter.next();
2138 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002139
reed@android.com8a1c16f2008-12-17 15:59:43 +00002140 // rebuild our clippath
2141 {
2142 const SkScalar W = this->width();
2143 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002144
reed@android.com8a1c16f2008-12-17 15:59:43 +00002145 fClipPath.reset();
2146#if 0
2147 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
2148 SkRect r;
2149 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
2150 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
2151 fClipPath.addRect(r);
2152 }
2153#else
2154 SkRect r;
2155 r.set(0, 0, W, H);
2156 fClipPath.addRect(r, SkPath::kCCW_Direction);
2157 r.set(W/4, H/4, W*3/4, H*3/4);
2158 fClipPath.addRect(r, SkPath::kCW_Direction);
2159#endif
2160 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002161
Scroggo2c8208f2011-06-15 16:49:08 +00002162 fZoomCenterX = SkScalarHalf(this->width());
2163 fZoomCenterY = SkScalarHalf(this->height());
2164
djsollen@google.com56c69772011-11-08 19:00:26 +00002165#ifdef SK_BUILD_FOR_ANDROID
Scroggob4490c72011-06-17 13:53:05 +00002166 // FIXME: The first draw after a size change does not work on Android, so
2167 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00002168 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00002169#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00002170 this->updateTitle(); // to refresh our config
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002171 fDevManager->windowSizeChanged(this);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00002172
2173 if (fTilingMode != kNo_Tiling && SampleView::IsSampleView(view)) {
2174 ((SampleView*)view)->onTileSizeChanged(this->tileSize());
2175 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002176}
2177
2178///////////////////////////////////////////////////////////////////////////////
2179
reed@android.comf2b98d62010-12-20 18:26:13 +00002180template <typename T> void SkTBSort(T array[], int count) {
2181 for (int i = 1; i < count - 1; i++) {
2182 bool didSwap = false;
2183 for (int j = count - 1; j > i; --j) {
2184 if (array[j] < array[j-1]) {
2185 T tmp(array[j-1]);
2186 array[j-1] = array[j];
2187 array[j] = tmp;
2188 didSwap = true;
2189 }
2190 }
2191 if (!didSwap) {
2192 break;
2193 }
2194 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002195
reed@android.comf2b98d62010-12-20 18:26:13 +00002196 for (int k = 0; k < count - 1; k++) {
2197 SkASSERT(!(array[k+1] < array[k]));
2198 }
2199}
2200
2201#include "SkRandom.h"
2202
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002203static void rand_rect(SkIRect* rect, SkRandom& rand) {
reed@android.comf2b98d62010-12-20 18:26:13 +00002204 int bits = 8;
2205 int shift = 32 - bits;
2206 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
2207 rand.nextU() >> shift, rand.nextU() >> shift);
2208 rect->sort();
2209}
2210
2211static void dumpRect(const SkIRect& r) {
2212 SkDebugf(" { %d, %d, %d, %d },\n",
2213 r.fLeft, r.fTop,
2214 r.fRight, r.fBottom);
2215}
2216
2217static void test_rects(const SkIRect rect[], int count) {
2218 SkRegion rgn0, rgn1;
2219
2220 for (int i = 0; i < count; i++) {
2221 rgn0.op(rect[i], SkRegion::kUnion_Op);
2222 // dumpRect(rect[i]);
2223 }
2224 rgn1.setRects(rect, count);
2225
2226 if (rgn0 != rgn1) {
2227 SkDebugf("\n");
2228 for (int i = 0; i < count; i++) {
2229 dumpRect(rect[i]);
2230 }
2231 SkDebugf("\n");
2232 }
2233}
2234
2235static void test() {
2236 size_t i;
2237
2238 const SkIRect r0[] = {
2239 { 0, 0, 1, 1 },
2240 { 2, 2, 3, 3 },
2241 };
2242 const SkIRect r1[] = {
2243 { 0, 0, 1, 3 },
2244 { 1, 1, 2, 2 },
2245 { 2, 0, 3, 3 },
2246 };
2247 const SkIRect r2[] = {
2248 { 0, 0, 1, 2 },
2249 { 2, 1, 3, 3 },
2250 { 4, 0, 5, 1 },
2251 { 6, 0, 7, 4 },
2252 };
2253
2254 static const struct {
2255 const SkIRect* fRects;
2256 int fCount;
2257 } gRecs[] = {
2258 { r0, SK_ARRAY_COUNT(r0) },
2259 { r1, SK_ARRAY_COUNT(r1) },
2260 { r2, SK_ARRAY_COUNT(r2) },
2261 };
2262
2263 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
2264 test_rects(gRecs[i].fRects, gRecs[i].fCount);
2265 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002266
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002267 SkRandom rand;
reed@android.comf2b98d62010-12-20 18:26:13 +00002268 for (i = 0; i < 10000; i++) {
2269 SkRegion rgn0, rgn1;
2270
2271 const int N = 8;
2272 SkIRect rect[N];
2273 for (int j = 0; j < N; j++) {
2274 rand_rect(&rect[j], rand);
2275 }
2276 test_rects(rect, N);
2277 }
2278}
2279
caryclark@google.com02939ce2012-06-06 12:09:51 +00002280// FIXME: this should be in a header
2281SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00002282SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
caryclark@google.com02939ce2012-06-06 12:09:51 +00002283 if (false) { // avoid bit rot, suppress warning
2284 test();
2285 }
halcanary96fcdcc2015-08-27 07:41:13 -07002286 return new SampleWindow(hwnd, argc, argv, nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002287}
2288
caryclark@google.com02939ce2012-06-06 12:09:51 +00002289// FIXME: this should be in a header
2290void get_preferred_size(int* x, int* y, int* width, int* height);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002291void get_preferred_size(int* x, int* y, int* width, int* height) {
2292 *x = 10;
2293 *y = 50;
2294 *width = 640;
2295 *height = 480;
2296}
2297
caryclark@google.com5987f582012-10-02 18:33:14 +00002298#ifdef SK_BUILD_FOR_IOS
reedf0b17102014-10-22 13:06:00 -07002299#include "SkApplication.h"
caryclark17f0b6d2014-07-22 10:15:34 -07002300IOS_launch_type set_cmd_line_args(int , char *[], const char* resourceDir) {
caryclarkf568f6a2014-07-18 10:58:56 -07002301 SetResourcePath(resourceDir);
caryclark17f0b6d2014-07-22 10:15:34 -07002302 return kApplication__iOSLaunchType;
caryclark@google.com5987f582012-10-02 18:33:14 +00002303}
2304#endif
2305
reed@android.com8a1c16f2008-12-17 15:59:43 +00002306void application_init() {
2307// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002308#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00002309 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002310#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002311 SkGraphics::Init();
2312 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002313}
2314
2315void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002316 SkEvent::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002317}