blob: 9d5ea0bf5e254850463d3b9830b9ef1e3b2a190e [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"
raftias94888332016-10-18 10:02:51 -070015#include "SkColorSpace_XYZ.h"
tfarinabcbc1782014-06-18 14:32:48 -070016#include "SkCommandLineFlags.h"
csmartdalton008b9d82017-02-22 12:00:42 -070017#include "SkCommonFlagsPathRenderer.h"
tfarinabcbc1782014-06-18 14:32:48 -070018#include "SkData.h"
reedddb5eca2014-10-08 11:10:51 -070019#include "SkDocument.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkGraphics.h"
tfarinabcbc1782014-06-18 14:32:48 -070021#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050022#include "SkOSPath.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"
liyuqian38911a72016-10-04 11:23:22 -070036#include "SkScan.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050037#include "SkClipOpPriv.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000038
reed54dc4872016-09-13 08:09:45 -070039#include "SkReadBuffer.h"
40#include "SkStream.h"
41
Mike Reed476e6c92016-09-30 15:26:47 -040042#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
43#include "SkCGUtils.h"
44#endif
45
Mike Reed96cf2062017-03-14 21:10:42 -040046#define PICTURE_MEANS_PIPE false
47#define SERIALIZE_PICTURE true
48
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000049#if SK_SUPPORT_GPU
bsalomon3724e572016-03-30 18:56:19 -070050# include "gl/GrGLInterface.h"
51# include "gl/GrGLUtil.h"
bsalomon3724e572016-03-30 18:56:19 -070052# include "GrContext.h"
robertphillips7715e062016-04-22 10:57:16 -070053# include "SkGr.h"
bsalomon3724e572016-03-30 18:56:19 -070054# if SK_ANGLE
bsalomon273c0f52016-03-31 10:59:06 -070055# include "gl/angle/GLTestContext_angle.h"
bsalomon3724e572016-03-30 18:56:19 -070056# endif
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000057#else
58class GrContext;
59#endif
Scroggo2c8208f2011-06-15 16:49:08 +000060
brianosman35a02a82016-09-29 14:37:02 -070061enum OutputColorSpace {
62 kLegacy_OutputColorSpace,
63 kSRGB_OutputColorSpace,
Brian Osmanfc2e7e02016-12-02 12:10:36 -050064 kNarrow_OutputColorSpace,
brianosman35a02a82016-09-29 14:37:02 -070065 kMonitor_OutputColorSpace,
reeda34be682016-02-15 07:48:35 -080066};
67
brianosman35a02a82016-09-29 14:37:02 -070068const struct {
69 SkColorType fColorType;
70 OutputColorSpace fColorSpace;
71 const char* fName;
72} gConfig[] = {
73 { kN32_SkColorType, kLegacy_OutputColorSpace, "L32" },
74 { kN32_SkColorType, kSRGB_OutputColorSpace, "S32" },
75 { kRGBA_F16_SkColorType, kSRGB_OutputColorSpace, "F16" },
Brian Osmanfc2e7e02016-12-02 12:10:36 -050076 { kRGBA_F16_SkColorType, kNarrow_OutputColorSpace, "F16 Narrow" },
brianosman35a02a82016-09-29 14:37:02 -070077 { kRGBA_F16_SkColorType, kMonitor_OutputColorSpace, "F16 Device" },
78};
reeda34be682016-02-15 07:48:35 -080079
reed5f629b42015-08-15 11:57:23 -070080// Should be 3x + 1
81#define kMaxFatBitsScale 28
82
reed@google.com1830c7a2012-06-04 12:05:43 +000083extern SampleView* CreateSamplePictFileView(const char filename[]);
84
85class PictFileFactory : public SkViewFactory {
86 SkString fFilename;
87public:
88 PictFileFactory(const SkString& filename) : fFilename(filename) {}
mtklein36352bf2015-03-25 18:17:31 -070089 SkView* operator() () const override {
reed@google.com1830c7a2012-06-04 12:05:43 +000090 return CreateSamplePictFileView(fFilename.c_str());
91 }
92};
93
fmalita851d68a2016-08-15 07:48:47 -070094extern SampleView* CreateSampleSVGFileView(const SkString& filename);
fmalita6ceef3d2016-07-26 18:46:34 -070095
96class SVGFileFactory : public SkViewFactory {
97 SkString fFilename;
98public:
99 SVGFileFactory(const SkString& filename) : fFilename(filename) {}
100 SkView* operator() () const override {
fmalita851d68a2016-08-15 07:48:47 -0700101 return CreateSampleSVGFileView(fFilename);
fmalita6ceef3d2016-07-26 18:46:34 -0700102 }
103};
104
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000105#ifdef SAMPLE_PDF_FILE_VIEWER
106extern SampleView* CreateSamplePdfFileViewer(const char filename[]);
107
108class PdfFileViewerFactory : public SkViewFactory {
109 SkString fFilename;
110public:
111 PdfFileViewerFactory(const SkString& filename) : fFilename(filename) {}
mtklein36352bf2015-03-25 18:17:31 -0700112 SkView* operator() () const override {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000113 return CreateSamplePdfFileViewer(fFilename.c_str());
114 }
115};
116#endif // SAMPLE_PDF_FILE_VIEWER
117
bsalomon49996162016-06-14 09:43:52 -0700118#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000119//#define DEFAULT_TO_ANGLE 1
120#else
bsalomon@google.com82d12232013-09-09 15:36:26 +0000121#define DEFAULT_TO_GPU 0 // if 1 default rendering is on GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000122#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000123
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124#define ANIMATING_EVENTTYPE "nextSample"
reed@google.com84cfce12013-05-29 19:22:20 +0000125#define ANIMATING_DELAY 250
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000127#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +0000128 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000129#else
reed@google.combad8c872011-05-18 20:10:31 +0000130 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000131#endif
reed@google.combad8c872011-05-18 20:10:31 +0000132#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000133
reed@google.com3cec4d72011-07-06 13:59:47 +0000134static SampleWindow* gSampleWindow;
135
reed@google.coma4f81372012-11-15 15:56:38 +0000136static bool gShowGMBounds;
137
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +0000138static void post_event_to_sink(SkEvent* evt, SkEventSink* sink) {
reed@google.com2072db82011-11-08 15:18:10 +0000139 evt->setTargetID(sink->getSinkID())->post();
140}
141
reed76113a92015-02-02 12:55:02 -0800142static SkAnimTimer gAnimTimer;
143
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000144///////////////////////////////////////////////////////////////////////////////
145
146static const char* skip_until(const char* str, const char* skip) {
147 if (!str) {
halcanary96fcdcc2015-08-27 07:41:13 -0700148 return nullptr;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000149 }
150 return strstr(str, skip);
151}
152
153static const char* skip_past(const char* str, const char* skip) {
154 const char* found = skip_until(str, skip);
155 if (!found) {
halcanary96fcdcc2015-08-27 07:41:13 -0700156 return nullptr;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000157 }
158 return found + strlen(skip);
159}
160
161static const char* gPrefFileName = "sampleapp_prefs.txt";
162
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000163static bool readTitleFromPrefs(SkString* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000164 SkFILEStream stream(gPrefFileName);
165 if (!stream.isValid()) {
166 return false;
167 }
168
reed@google.com7fa2a652014-01-27 13:42:58 +0000169 size_t len = stream.getLength();
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000170 SkString data(len);
171 stream.read(data.writable_str(), len);
172 const char* s = data.c_str();
173
174 s = skip_past(s, "curr-slide-title");
175 s = skip_past(s, "=");
176 s = skip_past(s, "\"");
177 const char* stop = skip_until(s, "\"");
178 if (stop > s) {
179 title->set(s, stop - s);
180 return true;
181 }
182 return false;
183}
184
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000185static void writeTitleToPrefs(const char* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000186 SkFILEWStream stream(gPrefFileName);
187 SkString data;
188 data.printf("curr-slide-title = \"%s\"\n", title);
189 stream.write(data.c_str(), data.size());
190}
191
192///////////////////////////////////////////////////////////////////////////////
193
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000194class SampleWindow::DefaultDeviceManager : public SampleWindow::DeviceManager {
195public:
196
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000197 DefaultDeviceManager() {
198#if SK_SUPPORT_GPU
halcanary96fcdcc2015-08-27 07:41:13 -0700199 fCurContext = nullptr;
200 fCurIntf = nullptr;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000201 fMSAASampleCount = 0;
brianosman2d1ee792016-05-05 12:24:31 -0700202 fDeepColor = false;
203 fActualColorBits = 0;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000204#endif
205 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000206 }
207
208 virtual ~DefaultDeviceManager() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000209#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000210 SkSafeUnref(fCurContext);
211 SkSafeUnref(fCurIntf);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000212#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000213 }
214
csmartdalton008b9d82017-02-22 12:00:42 -0700215 void setUpBackend(SampleWindow* win, const BackendOptions& backendOptions) override {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000216 SkASSERT(kNone_BackEndType == fBackend);
robertphillips@google.com53e96a12012-03-30 14:47:53 +0000217
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000218 fBackend = kNone_BackEndType;
219
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000220#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000221 switch (win->getDeviceType()) {
reedbeedb812015-05-05 12:14:45 -0700222 case kRaster_DeviceType: // fallthrough
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000223 case kGPU_DeviceType:
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000224 // all these guys use the native backend
225 fBackend = kNativeGL_BackEndType;
226 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000227#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000228 case kANGLE_DeviceType:
229 // ANGLE is really the only odd man out
230 fBackend = kANGLE_BackEndType;
231 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000232#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000233 default:
234 SkASSERT(false);
235 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000236 }
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000237 AttachmentInfo attachmentInfo;
csmartdalton008b9d82017-02-22 12:00:42 -0700238 bool result = win->attach(fBackend, backendOptions.fMSAASampleCount,
239 backendOptions.fDeepColor, &attachmentInfo);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000240 if (!result) {
bsalomon@google.com74913722011-10-27 20:44:19 +0000241 SkDebugf("Failed to initialize GL");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000242 return;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000243 }
csmartdalton008b9d82017-02-22 12:00:42 -0700244 fMSAASampleCount = backendOptions.fMSAASampleCount;
245 fDeepColor = backendOptions.fDeepColor;
brianosman2d1ee792016-05-05 12:24:31 -0700246 // Assume that we have at least 24-bit output, for backends that don't supply this data
247 fActualColorBits = SkTMax(attachmentInfo.fColorBits, 24);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000248
halcanary96fcdcc2015-08-27 07:41:13 -0700249 SkASSERT(nullptr == fCurIntf);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000250 switch (win->getDeviceType()) {
reedbeedb812015-05-05 12:14:45 -0700251 case kRaster_DeviceType: // fallthrough
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000252 case kGPU_DeviceType:
253 // all these guys use the native interface
csmartdalton008b9d82017-02-22 12:00:42 -0700254 fCurIntf = GrGLCreateNativeInterface();
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000255 break;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000256#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000257 case kANGLE_DeviceType:
csmartdalton008b9d82017-02-22 12:00:42 -0700258 fCurIntf = sk_gpu_test::CreateANGLEGLInterface();
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000259 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000260#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000261 default:
262 SkASSERT(false);
263 break;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000264 }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000265
halcanary96fcdcc2015-08-27 07:41:13 -0700266 SkASSERT(nullptr == fCurContext);
csmartdalton008b9d82017-02-22 12:00:42 -0700267 fCurContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf,
268 backendOptions.fGrContextOptions);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000269
halcanary96fcdcc2015-08-27 07:41:13 -0700270 if (nullptr == fCurContext || nullptr == fCurIntf) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000271 // We need some context and interface to see results
272 SkSafeUnref(fCurContext);
273 SkSafeUnref(fCurIntf);
halcanary96fcdcc2015-08-27 07:41:13 -0700274 fCurContext = nullptr;
275 fCurIntf = nullptr;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000276 SkDebugf("Failed to setup 3D");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000277
mtklein18300a32016-03-16 13:53:35 -0700278 win->release();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000279 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000280#endif // SK_SUPPORT_GPU
robertphillipsecf3dbe2016-07-28 15:17:34 -0700281 // call windowSizeChanged to create the gpu-backed Surface
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000282 this->windowSizeChanged(win);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000283 }
284
mtkleinf0599002015-07-13 06:18:39 -0700285 void tearDownBackend(SampleWindow *win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000286#if SK_SUPPORT_GPU
reed789e3272014-10-30 11:29:08 -0700287 if (fCurContext) {
288 // in case we have outstanding refs to this guy (lua?)
289 fCurContext->abandonContext();
290 fCurContext->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700291 fCurContext = nullptr;
reed789e3272014-10-30 11:29:08 -0700292 }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000293
294 SkSafeUnref(fCurIntf);
halcanary96fcdcc2015-08-27 07:41:13 -0700295 fCurIntf = nullptr;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000296
robertphillipsecf3dbe2016-07-28 15:17:34 -0700297 fGpuSurface = nullptr;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000298#endif
mtklein18300a32016-03-16 13:53:35 -0700299 win->release();
bsalomon@google.comddd40e52012-04-10 15:56:29 +0000300 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000301 }
302
robertphillipsecf3dbe2016-07-28 15:17:34 -0700303 sk_sp<SkSurface> makeSurface(SampleWindow::DeviceType dType, SampleWindow* win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000304#if SK_SUPPORT_GPU
bsalomon49f085d2014-09-05 13:34:00 -0700305 if (IsGpuDeviceType(dType) && fCurContext) {
reed4302ae92014-10-06 12:29:56 -0700306 SkSurfaceProps props(win->getSurfaceProps());
brianosman2d1ee792016-05-05 12:24:31 -0700307 if (kRGBA_F16_SkColorType == win->info().colorType() || fActualColorBits > 24) {
308 // If we're rendering to F16, we need an off-screen surface - the current render
309 // target is most likely the wrong format.
310 //
311 // If we're using a deep (10-bit or higher) surface, we probably need an off-screen
312 // surface. 10-bit, in particular, has strange gamma behavior.
brianosman6b086522016-04-14 12:39:00 -0700313 return SkSurface::MakeRenderTarget(fCurContext, SkBudgeted::kNo, win->info(),
robertphillipsecf3dbe2016-07-28 15:17:34 -0700314 fMSAASampleCount, &props);
brianosman6b086522016-04-14 12:39:00 -0700315 } else {
robertphillipsecf3dbe2016-07-28 15:17:34 -0700316 return fGpuSurface;
brianosman6b086522016-04-14 12:39:00 -0700317 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000318 }
reed0397e9f2014-09-18 11:29:01 -0700319#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700320 return nullptr;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000321 }
322
mtkleinf0599002015-07-13 06:18:39 -0700323 void publishCanvas(SampleWindow::DeviceType dType,
robertphillips1e876182016-07-29 08:28:25 -0700324 SkCanvas* renderingCanvas, SampleWindow* win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000325#if SK_SUPPORT_GPU
brianosman2d1ee792016-05-05 12:24:31 -0700326 if (!IsGpuDeviceType(dType) ||
327 kRGBA_F16_SkColorType == win->info().colorType() ||
328 fActualColorBits > 24) {
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500329 // We made/have an off-screen surface. Extract the pixels exactly as we rendered them:
330 SkImageInfo info = win->info();
331 size_t rowBytes = info.minRowBytes();
332 size_t size = info.getSafeSize(rowBytes);
333 auto data = SkData::MakeUninitialized(size);
334 SkASSERT(data);
335
336 if (!renderingCanvas->readPixels(info, data->writable_data(), rowBytes, 0, 0)) {
337 SkDEBUGFAIL("Failed to read canvas pixels");
338 return;
brianosman35a02a82016-09-29 14:37:02 -0700339 }
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500340
341 // Now, re-interpret those pixels as sRGB, so they won't be color converted when we
342 // draw then to FBO0. This ensures that if we rendered in any strange gamut, we'll see
343 // the "correct" output (because we generated the pixel values we wanted in the
344 // offscreen canvas).
345 auto colorSpace = kRGBA_F16_SkColorType == info.colorType()
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500346 ? SkColorSpace::MakeSRGBLinear()
347 : SkColorSpace::MakeSRGB();
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500348 auto offscreenImage = SkImage::MakeRasterData(info.makeColorSpace(colorSpace), data,
349 rowBytes);
robertphillipsecf3dbe2016-07-28 15:17:34 -0700350
robertphillips1e876182016-07-29 08:28:25 -0700351 SkCanvas* gpuCanvas = fGpuSurface->getCanvas();
brianosman2d1ee792016-05-05 12:24:31 -0700352
353 // With ten-bit output, we need to manually apply the gamma of the output device
354 // (unless we're in non-gamma correct mode, in which case our data is already
355 // fake-sRGB, like we're expected to put in the 10-bit buffer):
Brian Osman7039f742016-11-01 15:56:16 -0400356 bool doGamma = (fActualColorBits == 30) && win->info().colorSpace();
robertphillips1e876182016-07-29 08:28:25 -0700357
358 SkPaint gammaPaint;
reed374772b2016-10-05 17:33:02 -0700359 gammaPaint.setBlendMode(SkBlendMode::kSrc);
robertphillips1e876182016-07-29 08:28:25 -0700360 if (doGamma) {
Brian Osman964dec32017-01-26 09:32:33 -0500361 gammaPaint.setColorFilter(sk_tool_utils::MakeLinearToSRGBColorFilter());
robertphillips1e876182016-07-29 08:28:25 -0700362 }
363
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500364 gpuCanvas->drawImage(offscreenImage, 0, 0, &gammaPaint);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000365 }
robertphillips1e876182016-07-29 08:28:25 -0700366
367 fGpuSurface->prepareForExternalIO();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000368#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000369
370 win->present();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000371 }
372
mtkleinf0599002015-07-13 06:18:39 -0700373 void windowSizeChanged(SampleWindow* win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000374#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000375 if (fCurContext) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000376 AttachmentInfo attachmentInfo;
brianosman2d1ee792016-05-05 12:24:31 -0700377 win->attach(fBackend, fMSAASampleCount, fDeepColor, &attachmentInfo);
brianosman2d1ee792016-05-05 12:24:31 -0700378 fActualColorBits = SkTMax(attachmentInfo.fColorBits, 24);
robertphillipsecf3dbe2016-07-28 15:17:34 -0700379 fGpuSurface = win->makeGpuBackedSurface(attachmentInfo, fCurIntf, fCurContext);
bsalomon@google.com74913722011-10-27 20:44:19 +0000380 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000381#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000382 }
383
mtkleinf0599002015-07-13 06:18:39 -0700384 GrContext* getGrContext() override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000385#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000386 return fCurContext;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000387#else
halcanary96fcdcc2015-08-27 07:41:13 -0700388 return nullptr;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000389#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000390 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000391
robertphillipsecf3dbe2016-07-28 15:17:34 -0700392 int numColorSamples() const override {
brianosman1dfec742016-09-15 07:19:53 -0700393#if SK_SUPPORT_GPU
robertphillipsecf3dbe2016-07-28 15:17:34 -0700394 return fMSAASampleCount;
brianosman1dfec742016-09-15 07:19:53 -0700395#else
396 return 0;
397#endif
bsalomon@google.com11959252012-04-06 20:13:38 +0000398 }
399
brianosman2d1ee792016-05-05 12:24:31 -0700400 int getColorBits() override {
401#if SK_SUPPORT_GPU
402 return fActualColorBits;
403#else
404 return 24;
405#endif
406 }
407
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000408private:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000409
410#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000411 GrContext* fCurContext;
412 const GrGLInterface* fCurIntf;
robertphillipsecf3dbe2016-07-28 15:17:34 -0700413 sk_sp<SkSurface> fGpuSurface;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000414 int fMSAASampleCount;
brianosman2d1ee792016-05-05 12:24:31 -0700415 bool fDeepColor;
416 int fActualColorBits;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000417#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000418
419 SkOSWindow::SkBackEndTypes fBackend;
420
421 typedef SampleWindow::DeviceManager INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000422};
423
424///////////////
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000425static const char view_inval_msg[] = "view-inval-msg";
426
Scroggo62b65b02011-06-21 16:01:26 +0000427void SampleWindow::postInvalDelay() {
reed@google.com87fac4a2011-08-04 13:50:17 +0000428 (new SkEvent(view_inval_msg, this->getSinkID()))->postDelay(1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000429}
430
431static bool isInvalEvent(const SkEvent& evt) {
432 return evt.isType(view_inval_msg);
433}
434//////////////////
435
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000436#include "GMSampleView.h"
437
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000438class AutoUnrefArray {
439public:
440 AutoUnrefArray() {}
441 ~AutoUnrefArray() {
442 int count = fObjs.count();
443 for (int i = 0; i < count; ++i) {
444 fObjs[i]->unref();
445 }
446 }
447 SkRefCnt*& push_back() { return *fObjs.append(); }
chudy@google.com4605a3f2012-08-01 17:58:01 +0000448
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000449private:
450 SkTDArray<SkRefCnt*> fObjs;
451};
452
453// registers GMs as Samples
454// This can't be performed during static initialization because it could be
455// run before GMRegistry has been fully built.
caryclark@google.com02939ce2012-06-06 12:09:51 +0000456static void SkGMRegistyToSampleRegistry() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000457 static bool gOnce;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000458 static AutoUnrefArray fRegisters;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000459
460 if (!gOnce) {
461 const skiagm::GMRegistry* gmreg = skiagm::GMRegistry::Head();
462 while (gmreg) {
463 fRegisters.push_back() = new SkViewRegister(gmreg->factory());
464 gmreg = gmreg->next();
465 }
466 gOnce = true;
467 }
468}
469
reed@google.comf0b5f682011-03-11 20:08:25 +0000470//////////////////////////////////////////////////////////////////////////////
471
reed@google.com569e0432011-04-05 13:07:03 +0000472enum FlipAxisEnum {
473 kFlipAxis_X = (1 << 0),
474 kFlipAxis_Y = (1 << 1)
475};
476
reed@google.comf0b5f682011-03-11 20:08:25 +0000477#include "SkDrawFilter.h"
478
bungeman@google.com96aabc82013-06-03 21:26:34 +0000479struct HintingState {
480 SkPaint::Hinting hinting;
481 const char* name;
482 const char* label;
483};
484static HintingState gHintingStates[] = {
halcanary96fcdcc2015-08-27 07:41:13 -0700485 {SkPaint::kNo_Hinting, "Mixed", nullptr },
bungeman@google.com96aabc82013-06-03 21:26:34 +0000486 {SkPaint::kNo_Hinting, "None", "H0 " },
487 {SkPaint::kSlight_Hinting, "Slight", "Hs " },
488 {SkPaint::kNormal_Hinting, "Normal", "Hn " },
489 {SkPaint::kFull_Hinting, "Full", "Hf " },
490};
491
bungeman5a59a422016-03-31 08:30:38 -0700492struct PixelGeometryState {
493 SkPixelGeometry pixelGeometry;
494 const char* name;
495 const char* label;
496};
497static PixelGeometryState gPixelGeometryStates[] = {
498 {SkPixelGeometry::kUnknown_SkPixelGeometry, "Mixed", nullptr },
499 {SkPixelGeometry::kUnknown_SkPixelGeometry, "Flat", "{Flat} " },
500 {SkPixelGeometry::kRGB_H_SkPixelGeometry, "RGB H", "{RGB H} " },
501 {SkPixelGeometry::kBGR_H_SkPixelGeometry, "BGR H", "{BGR H} " },
502 {SkPixelGeometry::kRGB_V_SkPixelGeometry, "RGB_V", "{RGB V} " },
503 {SkPixelGeometry::kBGR_V_SkPixelGeometry, "BGR_V", "{BGR V} " },
504};
505
reed93a12152015-03-16 10:08:34 -0700506struct FilterQualityState {
507 SkFilterQuality fQuality;
508 const char* fName;
509 const char* fLabel;
reed@google.com15bc13d2013-12-10 16:53:13 +0000510};
reed93a12152015-03-16 10:08:34 -0700511static FilterQualityState gFilterQualityStates[] = {
halcanary96fcdcc2015-08-27 07:41:13 -0700512 { kNone_SkFilterQuality, "Mixed", nullptr },
reed93a12152015-03-16 10:08:34 -0700513 { kNone_SkFilterQuality, "None", "F0 " },
514 { kLow_SkFilterQuality, "Low", "F1 " },
515 { kMedium_SkFilterQuality, "Medium", "F2 " },
516 { kHigh_SkFilterQuality, "High", "F3 " },
reed@google.com15bc13d2013-12-10 16:53:13 +0000517};
518
fmalita822ace92015-06-15 07:07:32 -0700519class FlagsFilterCanvas : public SkPaintFilterCanvas {
reed@google.comf0b5f682011-03-11 20:08:25 +0000520public:
fmalita822ace92015-06-15 07:07:32 -0700521 FlagsFilterCanvas(SkCanvas* canvas, SkOSMenu::TriState lcd, SkOSMenu::TriState aa,
522 SkOSMenu::TriState subpixel, int hinting, int filterQuality)
fmalitabad23dc2016-01-11 13:58:29 -0800523 : INHERITED(canvas)
fmalita822ace92015-06-15 07:07:32 -0700524 , fLCDState(lcd)
reed@google.com15bc13d2013-12-10 16:53:13 +0000525 , fAAState(aa)
526 , fSubpixelState(subpixel)
527 , fHintingState(hinting)
fmalita822ace92015-06-15 07:07:32 -0700528 , fFilterQualityIndex(filterQuality) {
reed93a12152015-03-16 10:08:34 -0700529 SkASSERT((unsigned)filterQuality < SK_ARRAY_COUNT(gFilterQualityStates));
reed@google.com15bc13d2013-12-10 16:53:13 +0000530 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000531
fmalita822ace92015-06-15 07:07:32 -0700532protected:
fmalita32cdc322016-01-12 07:21:11 -0800533 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type t) const override {
534 if (!*paint) {
fmalitabad23dc2016-01-11 13:58:29 -0800535 return true;
536 }
537
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000538 if (kText_Type == t && SkOSMenu::kMixedState != fLCDState) {
fmalita32cdc322016-01-12 07:21:11 -0800539 paint->writable()->setLCDRenderText(SkOSMenu::kOnState == fLCDState);
reed@google.com569e0432011-04-05 13:07:03 +0000540 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000541 if (SkOSMenu::kMixedState != fAAState) {
fmalita32cdc322016-01-12 07:21:11 -0800542 paint->writable()->setAntiAlias(SkOSMenu::kOnState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000543 }
reed93a12152015-03-16 10:08:34 -0700544 if (0 != fFilterQualityIndex) {
fmalita32cdc322016-01-12 07:21:11 -0800545 paint->writable()->setFilterQuality(gFilterQualityStates[fFilterQualityIndex].fQuality);
reed@google.com176753a2011-05-17 15:32:04 +0000546 }
bungeman@google.com96aabc82013-06-03 21:26:34 +0000547 if (SkOSMenu::kMixedState != fSubpixelState) {
fmalita32cdc322016-01-12 07:21:11 -0800548 paint->writable()->setSubpixelText(SkOSMenu::kOnState == fSubpixelState);
bungeman@google.com96aabc82013-06-03 21:26:34 +0000549 }
bungeman@google.com055aa522013-06-03 21:35:03 +0000550 if (0 != fHintingState && fHintingState < (int)SK_ARRAY_COUNT(gHintingStates)) {
fmalita32cdc322016-01-12 07:21:11 -0800551 paint->writable()->setHinting(gHintingStates[fHintingState].hinting);
reed@google.com09e3baa2011-05-18 12:04:31 +0000552 }
fmalitabad23dc2016-01-11 13:58:29 -0800553 return true;
reed@google.comf0b5f682011-03-11 20:08:25 +0000554 }
555
556private:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000557 SkOSMenu::TriState fLCDState;
558 SkOSMenu::TriState fAAState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000559 SkOSMenu::TriState fSubpixelState;
560 int fHintingState;
reed93a12152015-03-16 10:08:34 -0700561 int fFilterQualityIndex;
fmalita822ace92015-06-15 07:07:32 -0700562
563 typedef SkPaintFilterCanvas INHERITED;
reed@google.comf0b5f682011-03-11 20:08:25 +0000564};
565
reed@android.comf2b98d62010-12-20 18:26:13 +0000566///////////////////////////////////////////////////////////////////////////////
567
reed54dc4872016-09-13 08:09:45 -0700568class SampleTFSerializer : public SkTypefaceSerializer {
569public:
570 sk_sp<SkData> serialize(SkTypeface* tf) override {
571 tf->ref();
572 return SkData::MakeWithCopy(&tf, sizeof(tf));
573 }
574};
575
576class SampleTFDeserializer : public SkTypefaceDeserializer {
577public:
578 sk_sp<SkTypeface> deserialize(const void* data, size_t size) override {
579 SkASSERT(sizeof(SkTypeface*) == size);
580 SkTypeface* tf;
581 memcpy(&tf, data, size);
582 return sk_sp<SkTypeface>(tf); // this was ref'd in SampleTFSerializer
583 }
584};
585
586///////////////////////////////////////////////////////////////////////////////
587
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000588enum TilingMode {
589 kNo_Tiling,
590 kAbs_128x128_Tiling,
591 kAbs_256x256_Tiling,
592 kRel_4x4_Tiling,
593 kRel_1x16_Tiling,
594 kRel_16x1_Tiling,
595
596 kLast_TilingMode_Enum
597};
598
599struct TilingInfo {
600 const char* label;
601 SkScalar w, h;
602};
603
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000604static const struct TilingInfo gTilingInfo[] = {
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000605 { "No tiling", SK_Scalar1 , SK_Scalar1 }, // kNo_Tiling
606 { "128x128" , SkIntToScalar(128), SkIntToScalar(128) }, // kAbs_128x128_Tiling
607 { "256x256" , SkIntToScalar(256), SkIntToScalar(256) }, // kAbs_256x256_Tiling
608 { "1/4x1/4" , SK_Scalar1 / 4 , SK_Scalar1 / 4 }, // kRel_4x4_Tiling
609 { "1/1x1/16" , SK_Scalar1 , SK_Scalar1 / 16 }, // kRel_1x16_Tiling
610 { "1/16x1/1" , SK_Scalar1 / 16 , SK_Scalar1 }, // kRel_16x1_Tiling
611};
bungeman99fe8222015-08-20 07:57:51 -0700612static_assert((SK_ARRAY_COUNT(gTilingInfo) == kLast_TilingMode_Enum),
613 "Incomplete_tiling_labels");
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000614
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000615SkSize SampleWindow::tileSize() const {
616 SkASSERT((TilingMode)fTilingMode < kLast_TilingMode_Enum);
617 const struct TilingInfo* info = gTilingInfo + fTilingMode;
618 return SkSize::Make(info->w > SK_Scalar1 ? info->w : this->width() * info->w,
619 info->h > SK_Scalar1 ? info->h : this->height() * info->h);
620}
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000621//////////////////////////////////////////////////////////////////////////////
622
reed@android.comf2b98d62010-12-20 18:26:13 +0000623static SkView* curr_view(SkWindow* wind) {
624 SkView::F2BIter iter(wind);
625 return iter.next();
626}
627
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000628static bool curr_title(SkWindow* wind, SkString* title) {
629 SkView* view = curr_view(wind);
630 if (view) {
631 SkEvent evt(gTitleEvtName);
632 if (view->doQuery(&evt)) {
633 title->set(evt.findString(gTitleEvtName));
634 return true;
635 }
636 }
637 return false;
638}
639
reedd9adfe62015-02-01 19:01:04 -0800640bool SampleWindow::sendAnimatePulse() {
641 SkView* view = curr_view(this);
642 if (SampleView::IsSampleView(view)) {
reed76113a92015-02-02 12:55:02 -0800643 return ((SampleView*)view)->animate(gAnimTimer);
reedd9adfe62015-02-01 19:01:04 -0800644 }
645 return false;
646}
647
reed5f629b42015-08-15 11:57:23 -0700648void SampleWindow::setZoomCenter(float x, float y) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000649 fZoomCenterX = x;
650 fZoomCenterY = y;
Scroggo2c8208f2011-06-15 16:49:08 +0000651}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000652
reed5f629b42015-08-15 11:57:23 -0700653bool SampleWindow::zoomIn() {
Scroggo0f185c22011-03-24 18:35:50 +0000654 // Arbitrarily decided
reed5f629b42015-08-15 11:57:23 -0700655 if (fFatBitsScale == kMaxFatBitsScale) return false;
Scroggo0f185c22011-03-24 18:35:50 +0000656 fFatBitsScale++;
halcanary96fcdcc2015-08-27 07:41:13 -0700657 this->inval(nullptr);
Scroggo0f185c22011-03-24 18:35:50 +0000658 return true;
659}
660
reed5f629b42015-08-15 11:57:23 -0700661bool SampleWindow::zoomOut() {
Scroggo0f185c22011-03-24 18:35:50 +0000662 if (fFatBitsScale == 1) return false;
663 fFatBitsScale--;
halcanary96fcdcc2015-08-27 07:41:13 -0700664 this->inval(nullptr);
Scroggo0f185c22011-03-24 18:35:50 +0000665 return true;
666}
667
reed5f629b42015-08-15 11:57:23 -0700668void SampleWindow::updatePointer(int x, int y) {
Scroggo0f185c22011-03-24 18:35:50 +0000669 fMouseX = x;
670 fMouseY = y;
671 if (fShowZoomer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700672 this->inval(nullptr);
Scroggo0f185c22011-03-24 18:35:50 +0000673 }
674}
675
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000676static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
677 static const SampleWindow::DeviceType gCT[] = {
kkinnunen8686a5e2016-01-05 00:04:32 -0800678 SampleWindow::kRaster_DeviceType
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000679#if SK_SUPPORT_GPU
kkinnunen8686a5e2016-01-05 00:04:32 -0800680 , SampleWindow::kGPU_DeviceType
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000681#if SK_ANGLE
kkinnunen8686a5e2016-01-05 00:04:32 -0800682 , SampleWindow::kANGLE_DeviceType
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000683#endif // SK_ANGLE
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000684#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +0000685 };
bungeman99fe8222015-08-20 07:57:51 -0700686 static_assert(SK_ARRAY_COUNT(gCT) == SampleWindow::kDeviceTypeCnt, "array_size_mismatch");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000687 return gCT[ct];
688}
689
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000690static SkString getSampleTitle(const SkViewFactory* sampleFactory) {
691 SkView* view = (*sampleFactory)();
692 SkString title;
693 SampleCode::RequestTitle(view, &title);
694 view->unref();
695 return title;
696}
697
edisonn@google.comde36b692013-07-11 15:40:31 +0000698static bool compareSampleTitle(const SkViewFactory* first, const SkViewFactory* second) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000699 return strcmp(getSampleTitle(first).c_str(), getSampleTitle(second).c_str()) < 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000700}
701
reedb7ffe032015-11-05 11:59:17 -0800702static int find_by_title(const SkViewFactory* const* factories, int count, const char title[]) {
703 for (int i = 0; i < count; i++) {
704 if (getSampleTitle(factories[i]).equals(title)) {
705 return i;
706 }
707 }
708 return -1;
709}
710
711static void restrict_samples(SkTDArray<const SkViewFactory*>& factories, const SkString titles[],
712 int count) {
713 int newCount = 0;
714 for (int i = 0; i < count; ++i) {
715 int index = find_by_title(factories.begin(), factories.count(), titles[i].c_str());
716 if (index >= 0) {
717 SkTSwap(factories.begin()[newCount], factories.begin()[index]);
718 newCount += 1;
719 }
720 }
721 if (newCount) {
722 factories.setCount(newCount);
723 }
724}
725
mtkleinfd117d82014-06-19 14:18:56 -0700726DEFINE_string(slide, "", "Start on this sample.");
mtkleinfd117d82014-06-19 14:18:56 -0700727DEFINE_string(pictureDir, "", "Read pictures from here.");
728DEFINE_string(picture, "", "Path to single picture.");
fmalita6ceef3d2016-07-26 18:46:34 -0700729DEFINE_string(svg, "", "Path to single SVG file.");
730DEFINE_string(svgDir, "", "Read SVGs from here.");
reedb7ffe032015-11-05 11:59:17 -0800731DEFINE_string(sequence, "", "Path to file containing the desired samples/gms to show.");
mtkleinfd117d82014-06-19 14:18:56 -0700732DEFINE_bool(sort, false, "Sort samples by title.");
733DEFINE_bool(list, false, "List samples?");
Jim Van Verthb6c5e532016-10-28 10:38:08 -0400734DEFINE_bool(startgpu, false, "Start up with gpu?");
brianosmanca348462016-02-24 11:47:47 -0800735DEFINE_bool(redraw, false, "Force continuous redrawing, for profiling or debugging tools.");
mtkleinfd117d82014-06-19 14:18:56 -0700736#ifdef SAMPLE_PDF_FILE_VIEWER
737DEFINE_string(pdfPath, "", "Path to direcotry of pdf files.");
738#endif
csmartdalton008b9d82017-02-22 12:00:42 -0700739#if SK_SUPPORT_GPU
740DEFINE_pathrenderer_flag;
741DEFINE_int32(msaa, 0, "Request multisampling with this count.");
742DEFINE_bool(deepColor, false, "Request deep color (10-bit/channel or more) display buffer.");
743#endif
mtkleinfd117d82014-06-19 14:18:56 -0700744
reed89889b62014-10-29 12:36:45 -0700745#include "SkTaskGroup.h"
746
chudy@google.com4605a3f2012-08-01 17:58:01 +0000747SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager)
robertphillips@google.com4750fa52012-04-10 13:34:11 +0000748 : INHERITED(hwnd)
halcanary96fcdcc2015-08-27 07:41:13 -0700749 , fDevManager(nullptr) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000750
mtkleinfd117d82014-06-19 14:18:56 -0700751 SkCommandLineFlags::Parse(argc, argv);
752
reed@google.com7d05ac92013-06-06 13:14:13 +0000753 fCurrIndex = -1;
754
mtkleinfd117d82014-06-19 14:18:56 -0700755 if (!FLAGS_pictureDir.isEmpty()) {
756 SkOSFile::Iter iter(FLAGS_pictureDir[0], "skp");
757 SkString filename;
758 while (iter.next(&filename)) {
759 *fSamples.append() = new PictFileFactory(
tfarinaa8e2e152014-07-28 19:26:58 -0700760 SkOSPath::Join(FLAGS_pictureDir[0], filename.c_str()));
mtkleinfd117d82014-06-19 14:18:56 -0700761 }
762 }
763 if (!FLAGS_picture.isEmpty()) {
764 SkString path(FLAGS_picture[0]);
765 fCurrIndex = fSamples.count();
766 *fSamples.append() = new PictFileFactory(path);
767 }
fmalita6ceef3d2016-07-26 18:46:34 -0700768 if (!FLAGS_svg.isEmpty()) {
769 SkString path(FLAGS_svg[0]);
770 fCurrIndex = fSamples.count();
771 *fSamples.append() = new SVGFileFactory(path);
772 }
773 if (!FLAGS_svgDir.isEmpty()) {
774 SkOSFile::Iter iter(FLAGS_svgDir[0], "svg");
775 SkString filename;
776 while (iter.next(&filename)) {
777 *fSamples.append() = new SVGFileFactory(
778 SkOSPath::Join(FLAGS_svgDir[0], filename.c_str()));
779 }
780 }
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000781#ifdef SAMPLE_PDF_FILE_VIEWER
mtkleinfd117d82014-06-19 14:18:56 -0700782 if (!FLAGS_pdfPath.isEmpty()) {
783 SkOSFile::Iter iter(FLAGS_pdfPath[0], "pdf");
784 SkString filename;
785 while (iter.next(&filename)) {
786 *fSamples.append() = new PdfFileViewerFactory(
tfarinaa8e2e152014-07-28 19:26:58 -0700787 SkOSPath::Join(FLAGS_pictureDir[0], filename.c_str()));
mtkleinfd117d82014-06-19 14:18:56 -0700788 }
789 }
790#endif
robertphillips@google.com76d40212012-03-22 14:12:15 +0000791 SkGMRegistyToSampleRegistry();
792 {
793 const SkViewRegister* reg = SkViewRegister::Head();
794 while (reg) {
795 *fSamples.append() = reg->factory();
796 reg = reg->next();
797 }
798 }
799
reedb7ffe032015-11-05 11:59:17 -0800800 if (!FLAGS_sequence.isEmpty()) {
801 // The sequence file just contains a list (separated by CRs) of the samples or GM:gms
802 // you want to restrict to. Only these will appear when you cycle through.
803 // If none are found, or the file is empty, then it will be ignored, and all samples
804 // will be available.
805 SkFILEStream stream(FLAGS_sequence[0]);
806 if (stream.isValid()) {
807 size_t len = stream.getLength();
scroggo565901d2015-12-10 10:44:13 -0800808 SkAutoTMalloc<char> storage(len + 1);
809 char* buffer = storage.get();
reedb7ffe032015-11-05 11:59:17 -0800810 stream.read(buffer, len);
811 buffer[len] = 0;
812
813 SkTArray<SkString> titles;
814 SkStrSplit(buffer, "\n\r", &titles);
815 restrict_samples(fSamples, titles.begin(), titles.count());
816 }
817 }
818
mtkleinfd117d82014-06-19 14:18:56 -0700819 if (FLAGS_sort) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000820 // Sort samples, so foo.skp and foo.pdf are consecutive and we can quickly spot where
821 // skp -> pdf -> png fails.
halcanary96fcdcc2015-08-27 07:41:13 -0700822 SkTQSort(fSamples.begin(), fSamples.end() ? fSamples.end() - 1 : nullptr, compareSampleTitle);
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000823 }
824
mtkleinfd117d82014-06-19 14:18:56 -0700825 if (!FLAGS_slide.isEmpty()) {
826 fCurrIndex = findByTitle(FLAGS_slide[0]);
827 if (fCurrIndex < 0) {
828 fprintf(stderr, "Unknown sample \"%s\"\n", FLAGS_slide[0]);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000829 listTitles();
bsalomon@google.com11959252012-04-06 20:13:38 +0000830 }
mtkleinfd117d82014-06-19 14:18:56 -0700831 }
832
csmartdalton008b9d82017-02-22 12:00:42 -0700833#if SK_SUPPORT_GPU
834 fBackendOptions.fGrContextOptions.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
835 fBackendOptions.fMSAASampleCount = FLAGS_msaa;
836 fBackendOptions.fDeepColor = FLAGS_deepColor;
837#endif
brianosman35a02a82016-09-29 14:37:02 -0700838 fColorConfigIndex = 0;
mtkleinfd117d82014-06-19 14:18:56 -0700839
840 if (FLAGS_list) {
841 listTitles();
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000842 }
843
844 if (fCurrIndex < 0) {
845 SkString title;
846 if (readTitleFromPrefs(&title)) {
847 fCurrIndex = findByTitle(title.c_str());
848 }
849 }
850
851 if (fCurrIndex < 0) {
852 fCurrIndex = 0;
853 }
854
reed89889b62014-10-29 12:36:45 -0700855 static SkTaskGroup::Enabler enabled(-1);
reed@google.com3cec4d72011-07-06 13:59:47 +0000856 gSampleWindow = this;
857
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000858 fDeviceType = kRaster_DeviceType;
jvanverth41518eb2015-10-02 10:42:36 -0700859#if SK_SUPPORT_GPU
Jim Van Verthb6c5e532016-10-28 10:38:08 -0400860 if (FLAGS_startgpu) {
jvanverth44dcb8a2015-10-02 09:12:05 -0700861 fDeviceType = kGPU_DeviceType;
862 }
jvanverth41518eb2015-10-02 10:42:36 -0700863#endif
halcanary9d524f22016-03-29 09:03:52 -0700864
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000865#if DEFAULT_TO_GPU
866 fDeviceType = kGPU_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000867#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000868#if SK_ANGLE && DEFAULT_TO_ANGLE
869 fDeviceType = kANGLE_DeviceType;
870#endif
871
reed@android.com8a1c16f2008-12-17 15:59:43 +0000872 fUseClip = false;
reed91b4dfb2016-01-08 08:23:19 -0800873 fUsePicture = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000874 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000875 fRotate = false;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000876 fPerspAnim = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000877 fRequestGrabImage = false;
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000878 fTilingMode = kNo_Tiling;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000879 fMeasureFPS = false;
reedbabc3de2016-07-08 08:43:27 -0700880 fUseDeferredCanvas = false;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000881 fLCDState = SkOSMenu::kMixedState;
882 fAAState = SkOSMenu::kMixedState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000883 fSubpixelState = SkOSMenu::kMixedState;
884 fHintingState = 0;
bungeman5a59a422016-03-31 08:30:38 -0700885 fPixelGeometryIndex = 0;
reed93a12152015-03-16 10:08:34 -0700886 fFilterQualityIndex = 0;
reed@google.com569e0432011-04-05 13:07:03 +0000887 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000888
Scroggo0f185c22011-03-24 18:35:50 +0000889 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000890 fFatBitsScale = 8;
reed5755cd92016-08-21 19:30:13 -0700891 fTypeface = SkTypeface::MakeFromName("Courier", SkFontStyle(SkFontStyle::kBold_Weight,
892 SkFontStyle::kNormal_Width,
893 SkFontStyle::kUpright_Slant));
Scroggo0f185c22011-03-24 18:35:50 +0000894 fShowZoomer = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000895
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000896 fZoomLevel = 0;
897 fZoomScale = SK_Scalar1;
bungemance560262016-03-03 13:32:38 -0800898 fOffset = { 0, 0 };
chudy@google.com4605a3f2012-08-01 17:58:01 +0000899
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000900 fMagnify = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000901
Scroggo8ac0d542011-06-21 14:44:57 +0000902 fSaveToPdf = false;
reed093b4e82015-08-04 18:46:38 -0700903 fSaveToSKP = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000904
reed54dc4872016-09-13 08:09:45 -0700905 if (true) {
906 fPipeSerializer.setTypefaceSerializer(new SampleTFSerializer);
907 fPipeDeserializer.setTypefaceDeserializer(new SampleTFDeserializer);
908 }
909
yangsu@google.com921091f2011-08-02 13:39:12 +0000910 int sinkID = this->getSinkID();
scroggo@google.com7dadc742012-04-18 14:07:57 +0000911 fAppMenu = new SkOSMenu;
912 fAppMenu->setTitle("Global Settings");
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000913 int itemID;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000914
reeda34be682016-02-15 07:48:35 -0800915 itemID = fAppMenu->appendList("ColorType", "ColorType", sinkID, 0,
brianosman35a02a82016-09-29 14:37:02 -0700916 gConfig[0].fName,
917 gConfig[1].fName,
918 gConfig[2].fName,
919 gConfig[3].fName,
Brian Osmanfc2e7e02016-12-02 12:10:36 -0500920 gConfig[4].fName,
brianosman35a02a82016-09-29 14:37:02 -0700921 nullptr);
reeda34be682016-02-15 07:48:35 -0800922 fAppMenu->assignKeyEquivalentToItem(itemID, 'C');
923
kkinnunen8686a5e2016-01-05 00:04:32 -0800924 itemID = fAppMenu->appendList("Device Type", "Device Type", sinkID, 0,
925 "Raster",
926 "OpenGL",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000927#if SK_ANGLE
kkinnunen8686a5e2016-01-05 00:04:32 -0800928 "ANGLE",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000929#endif
kkinnunen8686a5e2016-01-05 00:04:32 -0800930 nullptr);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000931 fAppMenu->assignKeyEquivalentToItem(itemID, 'd');
932 itemID = fAppMenu->appendTriState("AA", "AA", sinkID, fAAState);
933 fAppMenu->assignKeyEquivalentToItem(itemID, 'b');
934 itemID = fAppMenu->appendTriState("LCD", "LCD", sinkID, fLCDState);
935 fAppMenu->assignKeyEquivalentToItem(itemID, 'l');
reed93a12152015-03-16 10:08:34 -0700936 itemID = fAppMenu->appendList("FilterQuality", "FilterQuality", sinkID, fFilterQualityIndex,
937 gFilterQualityStates[0].fName,
938 gFilterQualityStates[1].fName,
939 gFilterQualityStates[2].fName,
940 gFilterQualityStates[3].fName,
941 gFilterQualityStates[4].fName,
halcanary96fcdcc2015-08-27 07:41:13 -0700942 nullptr);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000943 fAppMenu->assignKeyEquivalentToItem(itemID, 'n');
bungeman@google.com96aabc82013-06-03 21:26:34 +0000944 itemID = fAppMenu->appendTriState("Subpixel", "Subpixel", sinkID, fSubpixelState);
945 fAppMenu->assignKeyEquivalentToItem(itemID, 's');
946 itemID = fAppMenu->appendList("Hinting", "Hinting", sinkID, fHintingState,
947 gHintingStates[0].name,
948 gHintingStates[1].name,
949 gHintingStates[2].name,
950 gHintingStates[3].name,
bungeman@google.coma9133bb2013-06-04 17:49:20 +0000951 gHintingStates[4].name,
halcanary96fcdcc2015-08-27 07:41:13 -0700952 nullptr);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000953 fAppMenu->assignKeyEquivalentToItem(itemID, 'h');
reed@google.com67b89ee2012-08-15 14:41:58 +0000954
bungeman5a59a422016-03-31 08:30:38 -0700955 itemID = fAppMenu->appendList("Pixel Geometry", "Pixel Geometry", sinkID, fPixelGeometryIndex,
956 gPixelGeometryStates[0].name,
957 gPixelGeometryStates[1].name,
958 gPixelGeometryStates[2].name,
959 gPixelGeometryStates[3].name,
960 gPixelGeometryStates[4].name,
961 gPixelGeometryStates[5].name,
962 nullptr);
963 fAppMenu->assignKeyEquivalentToItem(itemID, 'P');
964
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000965 itemID =fAppMenu->appendList("Tiling", "Tiling", sinkID, fTilingMode,
966 gTilingInfo[kNo_Tiling].label,
967 gTilingInfo[kAbs_128x128_Tiling].label,
968 gTilingInfo[kAbs_256x256_Tiling].label,
969 gTilingInfo[kRel_4x4_Tiling].label,
970 gTilingInfo[kRel_1x16_Tiling].label,
971 gTilingInfo[kRel_16x1_Tiling].label,
halcanary96fcdcc2015-08-27 07:41:13 -0700972 nullptr);
reed@google.com67b89ee2012-08-15 14:41:58 +0000973 fAppMenu->assignKeyEquivalentToItem(itemID, 't');
974
chudy@google.com4605a3f2012-08-01 17:58:01 +0000975 itemID = fAppMenu->appendSwitch("Slide Show", "Slide Show" , sinkID, false);
976 fAppMenu->assignKeyEquivalentToItem(itemID, 'a');
977 itemID = fAppMenu->appendSwitch("Clip", "Clip" , sinkID, fUseClip);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000978 fAppMenu->assignKeyEquivalentToItem(itemID, 'c');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000979 itemID = fAppMenu->appendSwitch("Flip X", "Flip X" , sinkID, false);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000980 fAppMenu->assignKeyEquivalentToItem(itemID, 'x');
981 itemID = fAppMenu->appendSwitch("Flip Y", "Flip Y" , sinkID, false);
982 fAppMenu->assignKeyEquivalentToItem(itemID, 'y');
983 itemID = fAppMenu->appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
984 fAppMenu->assignKeyEquivalentToItem(itemID, 'z');
985 itemID = fAppMenu->appendSwitch("Magnify", "Magnify" , sinkID, fMagnify);
986 fAppMenu->assignKeyEquivalentToItem(itemID, 'm');
reed093b4e82015-08-04 18:46:38 -0700987
scroggo@google.com7dadc742012-04-18 14:07:57 +0000988 itemID = fAppMenu->appendAction("Save to PDF", sinkID);
989 fAppMenu->assignKeyEquivalentToItem(itemID, 'e');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000990
scroggo@google.com7dadc742012-04-18 14:07:57 +0000991 this->addMenu(fAppMenu);
992 fSlideMenu = new SkOSMenu;
993 this->addMenu(fSlideMenu);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000994
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000995 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000996 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000997
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000998 this->loadView((*fSamples[fCurrIndex])());
chudy@google.com4605a3f2012-08-01 17:58:01 +0000999
halcanary96fcdcc2015-08-27 07:41:13 -07001000 if (nullptr == devManager) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001001 fDevManager = new DefaultDeviceManager();
1002 } else {
1003 devManager->ref();
1004 fDevManager = devManager;
1005 }
csmartdalton008b9d82017-02-22 12:00:42 -07001006 fDevManager->setUpBackend(this, fBackendOptions);
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001007
Scroggob4490c72011-06-17 13:53:05 +00001008 // If another constructor set our dimensions, ensure that our
1009 // onSizeChange gets called.
1010 if (this->height() && this->width()) {
1011 this->onSizeChange();
1012 }
reed@google.com2072db82011-11-08 15:18:10 +00001013
1014 // can't call this synchronously, since it may require a subclass to
1015 // to implement, or the caller may need us to have returned from the
1016 // constructor first. Hence we post an event to ourselves.
1017// this->updateTitle();
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001018 post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this);
reed76113a92015-02-02 12:55:02 -08001019
1020 gAnimTimer.run();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001021}
1022
1023SampleWindow::~SampleWindow() {
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001024 SkSafeUnref(fDevManager);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001025}
1026
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001027
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001028int SampleWindow::findByTitle(const char title[]) {
1029 int i, count = fSamples.count();
1030 for (i = 0; i < count; i++) {
1031 if (getSampleTitle(i).equals(title)) {
1032 return i;
1033 }
1034 }
mike@reedtribe.orgdd52caa2011-12-28 20:02:10 +00001035 return -1;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001036}
1037
robertphillips@google.com7265e722012-05-03 18:22:28 +00001038void SampleWindow::listTitles() {
1039 int count = fSamples.count();
1040 SkDebugf("All Slides:\n");
1041 for (int i = 0; i < count; i++) {
1042 SkDebugf(" %s\n", getSampleTitle(i).c_str());
1043 }
1044}
1045
reed@android.com55e76b22009-11-23 21:46:47 +00001046static SkBitmap capture_bitmap(SkCanvas* canvas) {
1047 SkBitmap bm;
reed84825042014-09-02 12:50:45 -07001048 if (bm.tryAllocPixels(canvas->imageInfo())) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001049 canvas->readPixels(&bm, 0, 0);
1050 }
reed@android.com55e76b22009-11-23 21:46:47 +00001051 return bm;
1052}
1053
reedf7f79d22015-02-21 09:03:20 -08001054static void drawText(SkCanvas* canvas, SkString str, SkScalar left, SkScalar top, SkPaint& paint) {
Scroggo0f185c22011-03-24 18:35:50 +00001055 SkColor desiredColor = paint.getColor();
1056 paint.setColor(SK_ColorWHITE);
reedf7f79d22015-02-21 09:03:20 -08001057 const char* c_str = str.c_str();
1058 size_t size = str.size();
Scroggo0f185c22011-03-24 18:35:50 +00001059 SkRect bounds;
1060 paint.measureText(c_str, size, &bounds);
1061 bounds.offset(left, top);
1062 SkScalar inset = SkIntToScalar(-2);
1063 bounds.inset(inset, inset);
1064 canvas->drawRect(bounds, paint);
Scroggo0f185c22011-03-24 18:35:50 +00001065 paint.setColor(desiredColor);
1066 canvas->drawText(c_str, size, left, top, paint);
1067}
1068
reed@android.com44177402009-11-23 21:07:51 +00001069#define XCLIP_N 8
1070#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +00001071
reedbabc3de2016-07-08 08:43:27 -07001072#include "SkDeferredCanvas.h"
1073#include "SkDumpCanvas.h"
1074
reed@android.come522ca52009-11-23 20:10:41 +00001075void SampleWindow::draw(SkCanvas* canvas) {
reed76113a92015-02-02 12:55:02 -08001076 gAnimTimer.updateTime();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001077
reed@google.comf03bb562011-11-11 21:42:12 +00001078 if (fGesture.isActive()) {
1079 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001080 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001081
djsollen@google.com796763e2012-12-10 14:12:55 +00001082 if (fMeasureFPS) {
1083 fMeasureFPS_Time = 0;
1084 }
1085
reedf7f79d22015-02-21 09:03:20 -08001086 SkSize tile = this->tileSize();
reed@android.come522ca52009-11-23 20:10:41 +00001087
reedf7f79d22015-02-21 09:03:20 -08001088 if (kNo_Tiling == fTilingMode) {
reedbabc3de2016-07-08 08:43:27 -07001089 SkDebugfDumper dumper;
1090 SkDumpCanvas dump(&dumper);
1091 SkDeferredCanvas deferred(canvas);
1092 SkCanvas* c = fUseDeferredCanvas ? &deferred : canvas;
1093 this->INHERITED::draw(c); // no looping or surfaces needed
reed@android.come522ca52009-11-23 20:10:41 +00001094 } else {
bungemance560262016-03-03 13:32:38 -08001095 const SkScalar w = SkScalarCeilToScalar(tile.width());
1096 const SkScalar h = SkScalarCeilToScalar(tile.height());
1097 SkImageInfo info = SkImageInfo::MakeN32Premul(SkScalarTruncToInt(w), SkScalarTruncToInt(h));
reede8f30622016-03-23 18:59:25 -07001098 auto surface(canvas->makeSurface(info));
reedf7f79d22015-02-21 09:03:20 -08001099 SkCanvas* tileCanvas = surface->getCanvas();
rmistry@google.comae933ce2012-08-23 18:19:56 +00001100
bungemance560262016-03-03 13:32:38 -08001101 for (SkScalar y = 0; y < height(); y += h) {
1102 for (SkScalar x = 0; x < width(); x += w) {
reedf7f79d22015-02-21 09:03:20 -08001103 SkAutoCanvasRestore acr(tileCanvas, true);
1104 tileCanvas->translate(-x, -y);
1105 tileCanvas->clear(0);
1106 this->INHERITED::draw(tileCanvas);
halcanary96fcdcc2015-08-27 07:41:13 -07001107 surface->draw(canvas, x, y, nullptr);
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +00001108 }
reedf7f79d22015-02-21 09:03:20 -08001109 }
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +00001110
reedf7f79d22015-02-21 09:03:20 -08001111 // for drawing the borders between tiles
1112 SkPaint paint;
1113 paint.setColor(0x60FF00FF);
1114 paint.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001115
reedf7f79d22015-02-21 09:03:20 -08001116 for (SkScalar y = 0; y < height(); y += tile.height()) {
1117 for (SkScalar x = 0; x < width(); x += tile.width()) {
1118 canvas->drawRect(SkRect::MakeXYWH(x, y, tile.width(), tile.height()), paint);
scroggo@google.com85cade02012-08-17 13:29:50 +00001119 }
1120 }
reed@android.come522ca52009-11-23 20:10:41 +00001121 }
reedf7f79d22015-02-21 09:03:20 -08001122
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001123 if (fShowZoomer && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +00001124 showZoomer(canvas);
1125 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001126 if (fMagnify && !fSaveToPdf) {
1127 magnify(canvas);
1128 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001129
djsollen@google.com796763e2012-12-10 14:12:55 +00001130 if (fMeasureFPS && fMeasureFPS_Time) {
1131 this->updateTitle();
1132 this->postInvalDelay();
1133 }
1134
brianosmanca348462016-02-24 11:47:47 -08001135 if (this->sendAnimatePulse() || FLAGS_redraw) {
halcanary96fcdcc2015-08-27 07:41:13 -07001136 this->inval(nullptr);
reedd9adfe62015-02-01 19:01:04 -08001137 }
1138
reed@google.com29038ed2011-07-06 17:56:47 +00001139 // do this last
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001140 fDevManager->publishCanvas(fDeviceType, canvas, this);
Scroggo3272ba82011-05-25 20:50:42 +00001141}
1142
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001143static float clipW = 200;
1144static float clipH = 200;
1145void SampleWindow::magnify(SkCanvas* canvas) {
1146 SkRect r;
1147 int count = canvas->save();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001148
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001149 SkMatrix m = canvas->getTotalMatrix();
robertphillips@google.com07ef9112012-06-04 13:22:14 +00001150 if (!m.invert(&m)) {
1151 return;
1152 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001153 SkPoint offset, center;
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001154 SkScalar mouseX = fMouseX * SK_Scalar1;
1155 SkScalar mouseY = fMouseY * SK_Scalar1;
1156 m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset);
1157 m.mapXY(mouseX, mouseY, &center);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001158
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001159 r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX());
1160 r.offset(offset.fX, offset.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001161
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001162 SkPaint paint;
1163 paint.setColor(0xFF66AAEE);
1164 paint.setStyle(SkPaint::kStroke_Style);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001165 paint.setStrokeWidth(10.f * m.getScaleX());
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001166 //lense offset
1167 //canvas->translate(0, -250);
1168 canvas->drawRect(r, paint);
1169 canvas->clipRect(r);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001170
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001171 m = canvas->getTotalMatrix();
1172 m.setTranslate(-center.fX, -center.fY);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001173 m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001174 m.postTranslate(center.fX, center.fY);
1175 canvas->concat(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001176
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001177 this->INHERITED::draw(canvas);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001178
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001179 canvas->restoreToCount(count);
1180}
1181
reed5f629b42015-08-15 11:57:23 -07001182static SkPaint& set_color_ref(SkPaint& paint, SkColor c) {
1183 paint.setColor(c);
1184 return paint;
1185}
reed@google.comb36334d2011-05-18 15:07:20 +00001186
reed5f629b42015-08-15 11:57:23 -07001187static void show_lcd_box(SkCanvas* canvas, SkScalar x, SkScalar y, SkColor c,
1188 SkScalar sx, SkScalar sy) {
1189 const SkScalar w = (1 - 1/sx) / 3;
1190 SkPaint paint;
1191 SkRect r = SkRect::MakeXYWH(x, y, w, 1 - 1/sy);
1192 canvas->drawRect(r, set_color_ref(paint, SkColorSetRGB(SkColorGetR(c), 0, 0)));
1193 r.offset(w, 0);
1194 canvas->drawRect(r, set_color_ref(paint, SkColorSetRGB(0, SkColorGetG(c), 0)));
1195 r.offset(w, 0);
1196 canvas->drawRect(r, set_color_ref(paint, SkColorSetRGB(0, 0, SkColorGetB(c))));
1197}
reed@google.comb36334d2011-05-18 15:07:20 +00001198
reed5f629b42015-08-15 11:57:23 -07001199static void show_lcd_circle(SkCanvas* canvas, SkScalar x, SkScalar y, SkColor c,
1200 SkScalar, SkScalar) {
1201 const SkRect r = SkRect::MakeXYWH(x, y, 1, 1);
1202 const SkScalar cx = x + 0.5f;
1203 const SkScalar cy = y + 0.5f;
1204
1205 SkPaint paint;
1206 paint.setAntiAlias(true);
1207
1208 SkPath path;
1209 path.addArc(r, 0, 120); path.lineTo(cx, cy);
1210 canvas->drawPath(path, set_color_ref(paint, SkColorSetRGB(SkColorGetR(c), 0, 0)));
1211
1212 path.reset(); path.addArc(r, 120, 120); path.lineTo(cx, cy);
1213 canvas->drawPath(path, set_color_ref(paint, SkColorSetRGB(0, SkColorGetG(c), 0)));
1214
1215 path.reset(); path.addArc(r, 240, 120); path.lineTo(cx, cy);
1216 canvas->drawPath(path, set_color_ref(paint, SkColorSetRGB(0, 0, SkColorGetB(c))));
1217}
1218
1219typedef void (*ShowLCDProc)(SkCanvas*, SkScalar, SkScalar, SkColor, SkScalar, SkScalar);
1220
1221/*
1222 * Like drawBitmapRect but we manually draw each pixels in RGB
1223 */
1224static void show_lcd_grid(SkCanvas* canvas, const SkBitmap& bitmap,
reed8a41adb2015-08-15 14:02:29 -07001225 const SkIRect& origSrc, const SkRect& dst, ShowLCDProc proc) {
reed5f629b42015-08-15 11:57:23 -07001226 SkIRect src;
1227 if (!src.intersect(origSrc, bitmap.bounds())) {
1228 return;
1229 }
1230 const SkScalar sx = dst.width() / src.width();
1231 const SkScalar sy = dst.height() / src.height();
1232
1233 SkAutoCanvasRestore acr(canvas, true);
1234 canvas->translate(dst.left(), dst.top());
1235 canvas->scale(sx, sy);
1236
reed5f629b42015-08-15 11:57:23 -07001237 for (int y = 0; y < src.height(); ++y) {
1238 for (int x = 0; x < src.width(); ++x) {
1239 proc(canvas, SkIntToScalar(x), SkIntToScalar(y),
1240 bitmap.getColor(src.left() + x, src.top() + y), sx, sy);
Scroggo0f185c22011-03-24 18:35:50 +00001241 }
reed5f629b42015-08-15 11:57:23 -07001242 }
1243}
1244
1245void SampleWindow::showZoomer(SkCanvas* canvas) {
1246 int count = canvas->save();
1247 canvas->resetMatrix();
1248 // Ensure the mouse position is on screen.
1249 int width = SkScalarRoundToInt(this->width());
1250 int height = SkScalarRoundToInt(this->height());
1251 if (fMouseX >= width) fMouseX = width - 1;
1252 else if (fMouseX < 0) fMouseX = 0;
1253 if (fMouseY >= height) fMouseY = height - 1;
1254 else if (fMouseY < 0) fMouseY = 0;
1255
1256 SkBitmap bitmap = capture_bitmap(canvas);
1257 bitmap.lockPixels();
1258
1259 // Find the size of the zoomed in view, forced to be odd, so the examined pixel is in the middle.
1260 int zoomedWidth = (width >> 1) | 1;
1261 int zoomedHeight = (height >> 1) | 1;
1262 SkIRect src;
1263 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
1264 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
1265 SkRect dest;
1266 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
1267 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
1268 SkPaint paint;
1269 // Clear the background behind our zoomed in view
1270 paint.setColor(SK_ColorWHITE);
1271 canvas->drawRect(dest, paint);
reed8a41adb2015-08-15 14:02:29 -07001272 switch (fFatBitsScale) {
1273 case kMaxFatBitsScale:
1274 show_lcd_grid(canvas, bitmap, src, dest, show_lcd_box);
1275 break;
1276 case kMaxFatBitsScale - 1:
1277 show_lcd_grid(canvas, bitmap, src, dest, show_lcd_circle);
1278 break;
1279 default:
halcanary96fcdcc2015-08-27 07:41:13 -07001280 canvas->drawBitmapRect(bitmap, src, dest, nullptr);
reed8a41adb2015-08-15 14:02:29 -07001281 break;
reed5f629b42015-08-15 11:57:23 -07001282 }
1283
1284 paint.setColor(SK_ColorBLACK);
1285 paint.setStyle(SkPaint::kStroke_Style);
1286 // Draw a border around the pixel in the middle
1287 SkRect originalPixel;
1288 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
1289 SkMatrix matrix;
1290 SkRect scalarSrc;
1291 scalarSrc.set(src);
1292 SkColor color = bitmap.getColor(fMouseX, fMouseY);
1293 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
1294 SkRect pixel;
1295 matrix.mapRect(&pixel, originalPixel);
1296 // TODO Perhaps measure the values and make the outline white if it's "dark"
1297 if (color == SK_ColorBLACK) {
1298 paint.setColor(SK_ColorWHITE);
1299 }
1300 canvas->drawRect(pixel, paint);
1301 }
1302 paint.setColor(SK_ColorBLACK);
1303 // Draw a border around the destination rectangle
1304 canvas->drawRect(dest, paint);
1305 paint.setStyle(SkPaint::kStrokeAndFill_Style);
1306 // Identify the pixel and its color on screen
1307 paint.setTypeface(fTypeface);
1308 paint.setAntiAlias(true);
reed5755cd92016-08-21 19:30:13 -07001309 paint.setTextSize(18);
halcanary96fcdcc2015-08-27 07:41:13 -07001310 SkScalar lineHeight = paint.getFontMetrics(nullptr);
reed5f629b42015-08-15 11:57:23 -07001311 SkString string;
1312 string.appendf("(%i, %i)", fMouseX, fMouseY);
1313 SkScalar left = dest.fLeft + SkIntToScalar(3);
1314 SkScalar i = SK_Scalar1;
Mike Reeddf85c382017-02-14 10:59:19 -05001315 drawText(canvas, string, left, lineHeight * i + dest.fTop, paint);
reed5f629b42015-08-15 11:57:23 -07001316 // Alpha
1317 i += SK_Scalar1;
1318 string.reset();
1319 string.appendf("A: %X", SkColorGetA(color));
Mike Reeddf85c382017-02-14 10:59:19 -05001320 drawText(canvas, string, left, lineHeight * i + dest.fTop, paint);
reed5f629b42015-08-15 11:57:23 -07001321 // Red
1322 i += SK_Scalar1;
1323 string.reset();
1324 string.appendf("R: %X", SkColorGetR(color));
1325 paint.setColor(SK_ColorRED);
Mike Reeddf85c382017-02-14 10:59:19 -05001326 drawText(canvas, string, left, lineHeight * i + dest.fTop, paint);
reed5f629b42015-08-15 11:57:23 -07001327 // Green
1328 i += SK_Scalar1;
1329 string.reset();
1330 string.appendf("G: %X", SkColorGetG(color));
reed5755cd92016-08-21 19:30:13 -07001331 paint.setColor(0xFF008800);
Mike Reeddf85c382017-02-14 10:59:19 -05001332 drawText(canvas, string, left, lineHeight * i + dest.fTop, paint);
reed5f629b42015-08-15 11:57:23 -07001333 // Blue
1334 i += SK_Scalar1;
1335 string.reset();
1336 string.appendf("B: %X", SkColorGetB(color));
1337 paint.setColor(SK_ColorBLUE);
Mike Reeddf85c382017-02-14 10:59:19 -05001338 drawText(canvas, string, left, lineHeight * i + dest.fTop, paint);
reed5f629b42015-08-15 11:57:23 -07001339 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +00001340}
1341
reed@android.com8a1c16f2008-12-17 15:59:43 +00001342void SampleWindow::onDraw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001343}
1344
1345#include "SkColorPriv.h"
1346
Scroggo8ac0d542011-06-21 14:44:57 +00001347void SampleWindow::saveToPdf()
1348{
1349 fSaveToPdf = true;
halcanary96fcdcc2015-08-27 07:41:13 -07001350 this->inval(nullptr);
Scroggo8ac0d542011-06-21 14:44:57 +00001351}
1352
reed@android.com8a1c16f2008-12-17 15:59:43 +00001353SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +00001354 if (fSaveToPdf) {
reedddb5eca2014-10-08 11:10:51 -07001355 SkString name;
1356 if (!this->getRawTitle(&name)) {
1357 name.set("unknown_sample");
1358 }
1359 name.append(".pdf");
1360#ifdef SK_BUILD_FOR_ANDROID
1361 name.prepend("/sdcard/");
1362#endif
halcanary676ab682016-05-03 12:10:04 -07001363 fPDFDocument = SkDocument::MakePDF(name.c_str());
reedddb5eca2014-10-08 11:10:51 -07001364 canvas = fPDFDocument->beginPage(this->width(), this->height());
kkinnunen8686a5e2016-01-05 00:04:32 -08001365 } else if (fSaveToSKP) {
1366 canvas = fRecorder.beginRecording(9999, 9999, nullptr, 0);
reed91b4dfb2016-01-08 08:23:19 -08001367 } else if (fUsePicture) {
Mike Reed96cf2062017-03-14 21:10:42 -04001368 if (PICTURE_MEANS_PIPE) {
1369 fPipeStream.reset(new SkDynamicMemoryWStream);
1370 canvas = fPipeSerializer.beginWrite(SkRect::MakeWH(this->width(), this->height()),
1371 fPipeStream.get());
1372 } else {
1373 canvas = fRecorder.beginRecording(9999, 9999, nullptr, 0);
1374 }
Scroggo8ac0d542011-06-21 14:44:57 +00001375 } else {
bsalomonbb0502e2015-02-11 11:11:11 -08001376 canvas = this->INHERITED::beforeChildren(canvas);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001377 }
1378
1379 if (fUseClip) {
1380 canvas->drawColor(0xFFFF88FF);
Mike Reedc1f77742016-12-09 09:00:50 -05001381 canvas->clipPath(fClipPath, kIntersect_SkClipOp, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001382 }
1383
fmalita822ace92015-06-15 07:07:32 -07001384 // Install a flags filter proxy canvas if needed
1385 if (fLCDState != SkOSMenu::kMixedState ||
1386 fAAState != SkOSMenu::kMixedState ||
1387 fSubpixelState != SkOSMenu::kMixedState ||
1388 fHintingState > 0 ||
1389 fFilterQualityIndex > 0) {
halcanary385fe4d2015-08-26 13:07:48 -07001390 canvas = new FlagsFilterCanvas(canvas, fLCDState, fAAState, fSubpixelState, fHintingState,
1391 fFilterQualityIndex);
fmalita822ace92015-06-15 07:07:32 -07001392 fFlagsFilterCanvas.reset(canvas);
1393 }
1394
reed@android.com8a1c16f2008-12-17 15:59:43 +00001395 return canvas;
1396}
reed89889b62014-10-29 12:36:45 -07001397#include "SkMultiPictureDraw.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001398void SampleWindow::afterChildren(SkCanvas* orig) {
halcanary96fcdcc2015-08-27 07:41:13 -07001399 fFlagsFilterCanvas.reset(nullptr);
fmalita822ace92015-06-15 07:07:32 -07001400
Scroggo8ac0d542011-06-21 14:44:57 +00001401 if (fSaveToPdf) {
1402 fSaveToPdf = false;
reedddb5eca2014-10-08 11:10:51 -07001403 fPDFDocument->endPage();
halcanary96fcdcc2015-08-27 07:41:13 -07001404 fPDFDocument.reset(nullptr);
Scroggo8ac0d542011-06-21 14:44:57 +00001405 // We took over the draw calls in order to create the PDF, so we need
1406 // to redraw.
halcanary96fcdcc2015-08-27 07:41:13 -07001407 this->inval(nullptr);
Scroggo8ac0d542011-06-21 14:44:57 +00001408 return;
1409 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001410
reed@android.comf2b98d62010-12-20 18:26:13 +00001411 if (fRequestGrabImage) {
1412 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001413
reed52d9ac62014-06-30 09:05:34 -07001414 SkBitmap bmp = capture_bitmap(orig);
1415 if (!bmp.isNull()) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001416 static int gSampleGrabCounter;
1417 SkString name;
reed@google.com7c57e0e2012-09-08 21:20:33 +00001418 name.printf("sample_grab_%d.png", gSampleGrabCounter++);
Hal Canarydb683012016-11-23 08:55:18 -07001419 sk_tool_utils::EncodeImageToFile(name.c_str(), bmp,
1420 SkEncodedImageFormat::kPNG, 100);
reed@android.comf2b98d62010-12-20 18:26:13 +00001421 }
kkinnunen8686a5e2016-01-05 00:04:32 -08001422 this->inval(nullptr);
1423 return;
reed@android.comf2b98d62010-12-20 18:26:13 +00001424 }
1425
kkinnunen8686a5e2016-01-05 00:04:32 -08001426 if (fSaveToSKP) {
reedca2622b2016-03-18 07:25:55 -07001427 sk_sp<SkPicture> picture(fRecorder.finishRecordingAsPicture());
kkinnunen8686a5e2016-01-05 00:04:32 -08001428 SkFILEWStream stream("sample_app.skp");
1429 picture->serialize(&stream);
1430 fSaveToSKP = false;
1431 this->inval(nullptr);
1432 return;
1433 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001434
reed91b4dfb2016-01-08 08:23:19 -08001435 if (fUsePicture) {
Mike Reed96cf2062017-03-14 21:10:42 -04001436 if (PICTURE_MEANS_PIPE) {
reed54dc4872016-09-13 08:09:45 -07001437 fPipeSerializer.endWrite();
1438 sk_sp<SkData> data(fPipeStream->detachAsData());
1439 fPipeDeserializer.playback(data->data(), data->size(), orig);
1440 fPipeStream.reset();
1441 } else {
1442 sk_sp<SkPicture> picture(fRecorder.finishRecordingAsPicture());
Mike Reed96cf2062017-03-14 21:10:42 -04001443 if (SERIALIZE_PICTURE) {
1444 auto data = picture->serialize();
1445 picture = SkPicture::MakeFromData(data.get(), nullptr);
1446 }
reed54dc4872016-09-13 08:09:45 -07001447 orig->drawPicture(picture.get());
reedbfd5f172016-01-07 11:28:08 -08001448 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001449 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001450
reed@google.com17d7aec2011-04-25 14:31:44 +00001451 // Do this after presentGL and other finishing, rather than in afterChild
jvanverth4d0d81a2015-08-11 11:07:07 -07001452 if (fMeasureFPS) {
reedb2ce0b02015-11-09 13:10:30 -08001453 orig->flush();
jvanverth4d0d81a2015-08-11 11:07:07 -07001454 fTimer.end();
1455 fMeasureFPS_Time += fTimer.fWall;
reed@google.com17d7aec2011-04-25 14:31:44 +00001456 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001457}
1458
reed@android.com6c5f6f22009-08-14 16:08:38 +00001459void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
reed@android.com6c5f6f22009-08-14 16:08:38 +00001460 if (fRotate) {
1461 SkScalar cx = this->width() / 2;
1462 SkScalar cy = this->height() / 2;
bungeman7438bfc2016-07-12 15:01:19 -07001463 canvas->rotate(gAnimTimer.scaled(10), cx, cy);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001464 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001465
bsalomon@google.come8f09102011-09-08 18:48:12 +00001466 if (fPerspAnim) {
reed76113a92015-02-02 12:55:02 -08001467 SkScalar secs = gAnimTimer.scaled(1);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001468
1469 static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
1470 static const SkScalar gAnimMag = SK_Scalar1 / 1000;
reed76113a92015-02-02 12:55:02 -08001471 SkScalar t = SkScalarMod(secs, gAnimPeriod);
reed80ea19c2015-05-12 10:37:34 -07001472 if (SkScalarFloorToInt(secs / gAnimPeriod) & 0x1) {
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001473 t = gAnimPeriod - t;
1474 }
1475 t = 2 * t - gAnimPeriod;
reed80ea19c2015-05-12 10:37:34 -07001476 t *= gAnimMag / gAnimPeriod;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001477 SkMatrix m;
1478 m.reset();
reed5d9ab282014-09-19 13:04:17 -07001479#if 1
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001480 m.setPerspY(t);
reed5d9ab282014-09-19 13:04:17 -07001481#else
1482 m.setPerspY(SK_Scalar1 / 1000);
reed80ea19c2015-05-12 10:37:34 -07001483 m.setSkewX(8.0f / 25);
reed5d9ab282014-09-19 13:04:17 -07001484 m.dump();
1485#endif
bsalomon@google.come8f09102011-09-08 18:48:12 +00001486 canvas->concat(m);
1487 }
reed@google.comf0b5f682011-03-11 20:08:25 +00001488
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001489 if (fMeasureFPS) {
jvanverth4d0d81a2015-08-11 11:07:07 -07001490 (void)SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT);
1491 fTimer.start();
reed@google.comf2183392011-04-22 14:10:48 +00001492 } else {
1493 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001494 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001495 if (fPerspAnim || fRotate) {
halcanary96fcdcc2015-08-27 07:41:13 -07001496 this->inval(nullptr);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001497 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001498}
1499
bungemance560262016-03-03 13:32:38 -08001500void SampleWindow::changeOffset(SkVector delta) {
1501 fOffset += delta;
1502 this->updateMatrix();
1503}
1504
djsollen@google.come32b5832011-06-13 16:58:40 +00001505void SampleWindow::changeZoomLevel(float delta) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001506 fZoomLevel += delta;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001507 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001508 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
1509 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001510 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001511 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
1512 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001513 } else {
1514 fZoomScale = SK_Scalar1;
1515 }
reed@google.comf03bb562011-11-11 21:42:12 +00001516 this->updateMatrix();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001517}
1518
reed@google.comf03bb562011-11-11 21:42:12 +00001519void SampleWindow::updateMatrix(){
1520 SkMatrix m;
1521 m.reset();
bungemance560262016-03-03 13:32:38 -08001522
reed@google.comf03bb562011-11-11 21:42:12 +00001523 if (fZoomLevel) {
1524 SkPoint center;
1525 //m = this->getLocalMatrix();//.invert(&m);
1526 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
1527 SkScalar cx = center.fX;
1528 SkScalar cy = center.fY;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001529
reed@google.comf03bb562011-11-11 21:42:12 +00001530 m.setTranslate(-cx, -cy);
1531 m.postScale(fZoomScale, fZoomScale);
1532 m.postTranslate(cx, cy);
1533 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001534
bungemance560262016-03-03 13:32:38 -08001535 m.postTranslate(fOffset.fX, fOffset.fY);
1536
reed@google.comf03bb562011-11-11 21:42:12 +00001537 if (fFlipAxis) {
1538 m.preTranslate(fZoomCenterX, fZoomCenterY);
1539 if (fFlipAxis & kFlipAxis_X) {
1540 m.preScale(-SK_Scalar1, SK_Scalar1);
1541 }
1542 if (fFlipAxis & kFlipAxis_Y) {
1543 m.preScale(SK_Scalar1, -SK_Scalar1);
1544 }
1545 m.preTranslate(-fZoomCenterX, -fZoomCenterY);
1546 //canvas->concat(m);
1547 }
1548 // Apply any gesture matrix
1549 m.preConcat(fGesture.localM());
1550 m.preConcat(fGesture.globalM());
chudy@google.com4605a3f2012-08-01 17:58:01 +00001551
reed@google.comf03bb562011-11-11 21:42:12 +00001552 this->setLocalMatrix(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001553
reed@google.comf03bb562011-11-11 21:42:12 +00001554 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001555 this->inval(nullptr);
reed@google.comf03bb562011-11-11 21:42:12 +00001556}
Scroggo2c8208f2011-06-15 16:49:08 +00001557bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +00001558 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
reed63121f02015-01-13 19:43:04 -08001559 this->loadView((*fSamples[fCurrIndex])());
Scroggo2c8208f2011-06-15 16:49:08 +00001560 return true;
1561}
1562
reed216b6432015-08-19 12:25:40 -07001563#include "SkResourceCache.h"
1564#include "SkGlyphCache.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001565bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +00001566 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
reed63121f02015-01-13 19:43:04 -08001567 this->loadView((*fSamples[fCurrIndex])());
reed216b6432015-08-19 12:25:40 -07001568
1569 if (false) {
1570 SkResourceCache::TestDumpMemoryStatistics();
1571 SkGlyphCache::Dump();
1572 SkDebugf("\n");
1573 }
1574
reed@android.com34245c72009-11-03 04:00:48 +00001575 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001576}
1577
yangsu@google.com501775e2011-06-24 16:04:50 +00001578bool SampleWindow::goToSample(int i) {
1579 fCurrIndex = (i) % fSamples.count();
reed63121f02015-01-13 19:43:04 -08001580 this->loadView((*fSamples[fCurrIndex])());
yangsu@google.com501775e2011-06-24 16:04:50 +00001581 return true;
1582}
1583
1584SkString SampleWindow::getSampleTitle(int i) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001585 return ::getSampleTitle(fSamples[i]);
yangsu@google.com501775e2011-06-24 16:04:50 +00001586}
1587
1588int SampleWindow::sampleCount() {
1589 return fSamples.count();
1590}
1591
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001592void SampleWindow::showOverview() {
reed63121f02015-01-13 19:43:04 -08001593 this->loadView(create_overview(fSamples.count(), fSamples.begin()));
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001594}
1595
Scroggo2c8208f2011-06-15 16:49:08 +00001596void SampleWindow::postAnimatingEvent() {
1597 if (fAnimating) {
reed@google.com87fac4a2011-08-04 13:50:17 +00001598 (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
Scroggo2c8208f2011-06-15 16:49:08 +00001599 }
1600}
reed@google.come23f1942011-12-08 19:36:00 +00001601
brianosman35a02a82016-09-29 14:37:02 -07001602static sk_sp<SkColorSpace> getMonitorColorSpace() {
1603#if defined(SK_BUILD_FOR_MAC)
1604 CGColorSpaceRef cs = CGDisplayCopyColorSpace(CGMainDisplayID());
1605 CFDataRef dataRef = CGColorSpaceCopyICCProfile(cs);
1606 const uint8_t* data = CFDataGetBytePtr(dataRef);
1607 size_t size = CFDataGetLength(dataRef);
1608
Brian Osman526972e2016-10-24 09:24:02 -04001609 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(data, size);
brianosman35a02a82016-09-29 14:37:02 -07001610
1611 CFRelease(cs);
1612 CFRelease(dataRef);
1613 return colorSpace;
1614#elif defined(SK_BUILD_FOR_WIN)
1615 DISPLAY_DEVICE dd = { sizeof(DISPLAY_DEVICE) };
1616
1617 // Chrome's code for this currently just gets the primary monitor's profile. This code iterates
1618 // over all attached monitors, so it's "better" in that sense. Making intelligent use of this
1619 // information (via things like MonitorFromWindow or MonitorFromRect to pick the correct
1620 // profile for a particular window or region of a window), is an exercise left to the reader.
1621 for (int i = 0; EnumDisplayDevices(NULL, i, &dd, 0); ++i) {
1622 if (dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) {
1623 // There are other helpful things in dd at this point:
1624 // dd.DeviceString has a longer name for the adapter
1625 // dd.StateFlags indicates primary display, mirroring, etc...
1626 HDC dc = CreateDC(NULL, dd.DeviceName, NULL, NULL);
1627 if (dc) {
1628 char icmPath[MAX_PATH + 1];
1629 DWORD pathLength = MAX_PATH;
Jim Van Verthb6c5e532016-10-28 10:38:08 -04001630 BOOL success = GetICMProfileA(dc, &pathLength, icmPath);
brianosman35a02a82016-09-29 14:37:02 -07001631 DeleteDC(dc);
1632 if (success) {
1633 sk_sp<SkData> iccData = SkData::MakeFromFileName(icmPath);
Brian Osman526972e2016-10-24 09:24:02 -04001634 return SkColorSpace::MakeICC(iccData->data(), iccData->size());
brianosman35a02a82016-09-29 14:37:02 -07001635 }
1636 }
1637 }
1638 }
1639
1640 return nullptr;
1641#else
1642 return nullptr;
1643#endif
1644}
1645
reed@android.com8a1c16f2008-12-17 15:59:43 +00001646bool SampleWindow::onEvent(const SkEvent& evt) {
reed@google.com2072db82011-11-08 15:18:10 +00001647 if (evt.isType(gUpdateWindowTitleEvtName)) {
1648 this->updateTitle();
1649 return true;
1650 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001651 if (evt.isType(ANIMATING_EVENTTYPE)) {
1652 if (fAnimating) {
1653 this->nextSample();
1654 this->postAnimatingEvent();
1655 }
1656 return true;
1657 }
reed@android.com34245c72009-11-03 04:00:48 +00001658 if (evt.isType("set-curr-index")) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001659 this->goToSample(evt.getFast32());
reed@android.com34245c72009-11-03 04:00:48 +00001660 return true;
1661 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001662 if (isInvalEvent(evt)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001663 this->inval(nullptr);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001664 return true;
1665 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001666 int selected = -1;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001667 if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001668 this->setDeviceType((DeviceType)selected);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001669 return true;
yangsu@google.com921091f2011-08-02 13:39:12 +00001670 }
reeda34be682016-02-15 07:48:35 -08001671 if (SkOSMenu::FindListIndex(evt, "ColorType", &selected)) {
brianosman35a02a82016-09-29 14:37:02 -07001672 fColorConfigIndex = selected;
1673 sk_sp<SkColorSpace> colorSpace = nullptr;
1674 switch (gConfig[selected].fColorSpace) {
1675 case kSRGB_OutputColorSpace:
Matt Sarett77a7a1b2017-02-07 13:56:11 -05001676 colorSpace = SkColorSpace::MakeSRGB();
brianosman35a02a82016-09-29 14:37:02 -07001677 break;
Brian Osmanfc2e7e02016-12-02 12:10:36 -05001678 case kNarrow_OutputColorSpace:
1679 {
1680 // NarrowGamut RGB (an artifically smaller than sRGB gamut)
1681 SkColorSpacePrimaries primaries ={
1682 0.54f, 0.33f, // Rx, Ry
1683 0.33f, 0.50f, // Gx, Gy
1684 0.25f, 0.20f, // Bx, By
1685 0.3127f, 0.3290f, // Wx, Wy
1686 };
1687 SkMatrix44 narrowGamutRGBMatrix(SkMatrix44::kUninitialized_Constructor);
1688 primaries.toXYZD50(&narrowGamutRGBMatrix);
1689 colorSpace = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
1690 narrowGamutRGBMatrix);
1691 }
1692 break;
brianosman35a02a82016-09-29 14:37:02 -07001693 case kMonitor_OutputColorSpace:
1694 colorSpace = getMonitorColorSpace();
1695 if (!colorSpace) {
1696 // Fallback for platforms / machines where we can't get a monitor profile
Matt Sarett77a7a1b2017-02-07 13:56:11 -05001697 colorSpace = SkColorSpace::MakeSRGB();
brianosman35a02a82016-09-29 14:37:02 -07001698 }
1699 break;
1700 case kLegacy_OutputColorSpace:
1701 default:
1702 // Do nothing
1703 break;
1704 }
brianosman0e22eb82016-08-30 07:07:59 -07001705 if (kRGBA_F16_SkColorType == gConfig[selected].fColorType) {
brianosman35a02a82016-09-29 14:37:02 -07001706 SkASSERT(colorSpace);
raftias94888332016-10-18 10:02:51 -07001707 SkASSERT(SkColorSpace_Base::Type::kXYZ == as_CSB(colorSpace)->type());
1708 SkColorSpace_XYZ* csXYZ = static_cast<SkColorSpace_XYZ*>(colorSpace.get());
1709 colorSpace = csXYZ->makeLinearGamma();
brianosman0e22eb82016-08-30 07:07:59 -07001710 }
brianosman35a02a82016-09-29 14:37:02 -07001711 this->setDeviceColorType(gConfig[selected].fColorType, colorSpace);
reeda34be682016-02-15 07:48:35 -08001712 return true;
1713 }
halcanary96fcdcc2015-08-27 07:41:13 -07001714 if (SkOSMenu::FindSwitchState(evt, "Slide Show", nullptr)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001715 this->toggleSlideshow();
1716 return true;
1717 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001718 if (SkOSMenu::FindTriState(evt, "AA", &fAAState) ||
1719 SkOSMenu::FindTriState(evt, "LCD", &fLCDState) ||
reed93a12152015-03-16 10:08:34 -07001720 SkOSMenu::FindListIndex(evt, "FilterQuality", &fFilterQualityIndex) ||
bungeman@google.com96aabc82013-06-03 21:26:34 +00001721 SkOSMenu::FindTriState(evt, "Subpixel", &fSubpixelState) ||
1722 SkOSMenu::FindListIndex(evt, "Hinting", &fHintingState) ||
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001723 SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) ||
1724 SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
reeda84d6602016-06-24 18:14:07 -07001725 SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify))
reed093b4e82015-08-04 18:46:38 -07001726 {
halcanary96fcdcc2015-08-27 07:41:13 -07001727 this->inval(nullptr);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00001728 this->updateTitle();
1729 return true;
1730 }
bungeman5a59a422016-03-31 08:30:38 -07001731 if (SkOSMenu::FindListIndex(evt, "Pixel Geometry", &fPixelGeometryIndex)) {
1732 this->setPixelGeometry(fPixelGeometryIndex);
1733 return true;
1734 }
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00001735 if (SkOSMenu::FindListIndex(evt, "Tiling", &fTilingMode)) {
1736 if (SampleView::IsSampleView(curr_view(this))) {
1737 ((SampleView*)curr_view(this))->onTileSizeChanged(this->tileSize());
1738 }
halcanary96fcdcc2015-08-27 07:41:13 -07001739 this->inval(nullptr);
yangsu@google.com921091f2011-08-02 13:39:12 +00001740 this->updateTitle();
1741 return true;
1742 }
halcanary96fcdcc2015-08-27 07:41:13 -07001743 if (SkOSMenu::FindSwitchState(evt, "Flip X", nullptr)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001744 fFlipAxis ^= kFlipAxis_X;
reed@google.comf03bb562011-11-11 21:42:12 +00001745 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001746 return true;
1747 }
halcanary96fcdcc2015-08-27 07:41:13 -07001748 if (SkOSMenu::FindSwitchState(evt, "Flip Y", nullptr)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001749 fFlipAxis ^= kFlipAxis_Y;
reed@google.comf03bb562011-11-11 21:42:12 +00001750 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001751 return true;
1752 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001753 if (SkOSMenu::FindAction(evt,"Save to PDF")) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001754 this->saveToPdf();
1755 return true;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001756 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001757 return this->INHERITED::onEvent(evt);
1758}
1759
reed@android.comf2b98d62010-12-20 18:26:13 +00001760bool SampleWindow::onQuery(SkEvent* query) {
1761 if (query->isType("get-slide-count")) {
1762 query->setFast32(fSamples.count());
1763 return true;
1764 }
1765 if (query->isType("get-slide-title")) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001766 SkView* view = (*fSamples[query->getFast32()])();
reed@android.comf2b98d62010-12-20 18:26:13 +00001767 SkEvent evt(gTitleEvtName);
1768 if (view->doQuery(&evt)) {
1769 query->setString("title", evt.findString(gTitleEvtName));
1770 }
1771 SkSafeUnref(view);
1772 return true;
1773 }
1774 if (query->isType("use-fast-text")) {
1775 SkEvent evt(gFastTextEvtName);
1776 return curr_view(this)->doQuery(&evt);
1777 }
reed@google.com29038ed2011-07-06 17:56:47 +00001778 if (query->isType("ignore-window-bitmap")) {
halcanary96fcdcc2015-08-27 07:41:13 -07001779 query->setFast32(this->getGrContext() != nullptr);
reed@google.com29038ed2011-07-06 17:56:47 +00001780 return true;
1781 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001782 return this->INHERITED::onQuery(query);
1783}
1784
Cary Clark992c7b02014-07-31 08:58:44 -04001785DECLARE_bool(portableFonts);
caryclark5fb6bd42014-06-23 11:25:00 -07001786
reed@android.com8a1c16f2008-12-17 15:59:43 +00001787bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001788 {
1789 SkView* view = curr_view(this);
1790 if (view) {
1791 SkEvent evt(gCharEvtName);
1792 evt.setFast32(uni);
1793 if (view->doQuery(&evt)) {
1794 return true;
1795 }
1796 }
1797 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001798
reed@android.com8a1c16f2008-12-17 15:59:43 +00001799 int dx = 0xFF;
1800 int dy = 0xFF;
1801
1802 switch (uni) {
1803 case '5': dx = 0; dy = 0; break;
1804 case '8': dx = 0; dy = -1; break;
1805 case '6': dx = 1; dy = 0; break;
1806 case '2': dx = 0; dy = 1; break;
1807 case '4': dx = -1; dy = 0; break;
1808 case '7': dx = -1; dy = -1; break;
1809 case '9': dx = 1; dy = -1; break;
1810 case '3': dx = 1; dy = 1; break;
1811 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001812
reed@android.com8a1c16f2008-12-17 15:59:43 +00001813 default:
1814 break;
1815 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001816
reed@android.com8a1c16f2008-12-17 15:59:43 +00001817 if (0xFF != dx && 0xFF != dy) {
bungemance560262016-03-03 13:32:38 -08001818 this->changeOffset({SkIntToScalar(dx / 32.0f), SkIntToScalar(dy / 32.0f)});
reed@android.com8a1c16f2008-12-17 15:59:43 +00001819 return true;
1820 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001821
reed@android.com0ae6b242008-12-23 16:49:54 +00001822 switch (uni) {
reed76113a92015-02-02 12:55:02 -08001823 case 27: // ESC
1824 gAnimTimer.stop();
1825 if (this->sendAnimatePulse()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001826 this->inval(nullptr);
reed76113a92015-02-02 12:55:02 -08001827 }
1828 break;
reedd9adfe62015-02-01 19:01:04 -08001829 case ' ':
reed76113a92015-02-02 12:55:02 -08001830 gAnimTimer.togglePauseResume();
reedd9adfe62015-02-01 19:01:04 -08001831 if (this->sendAnimatePulse()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001832 this->inval(nullptr);
reedd9adfe62015-02-01 19:01:04 -08001833 }
1834 break;
liyuqian38911a72016-10-04 11:23:22 -07001835 case 'A':
Yuqian Li550148b2017-01-13 10:13:13 -05001836 if (gSkUseAnalyticAA.load() && !gSkForceAnalyticAA.load()) {
1837 gSkForceAnalyticAA = true;
1838 } else {
1839 gSkUseAnalyticAA = !gSkUseAnalyticAA.load();
1840 gSkForceAnalyticAA = false;
1841 }
liyuqian38911a72016-10-04 11:23:22 -07001842 this->inval(nullptr);
liyuqianebcb8aa2016-10-11 12:20:08 -07001843 this->updateTitle();
liyuqian38911a72016-10-04 11:23:22 -07001844 break;
reed@google.coma4f81372012-11-15 15:56:38 +00001845 case 'B':
halcanary385fe4d2015-08-26 13:07:48 -07001846 post_event_to_sink(new SkEvent("PictFileView::toggleBBox"), curr_view(this));
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001847 // Cannot call updateTitle() synchronously, because the toggleBBox event is still in
1848 // the queue.
halcanary385fe4d2015-08-26 13:07:48 -07001849 post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this);
halcanary96fcdcc2015-08-27 07:41:13 -07001850 this->inval(nullptr);
reed@google.coma4f81372012-11-15 15:56:38 +00001851 break;
reed4302ae92014-10-06 12:29:56 -07001852 case 'D':
1853 toggleDistanceFieldFonts();
1854 break;
reedbabc3de2016-07-08 08:43:27 -07001855 case 'E':
1856 fUseDeferredCanvas = !fUseDeferredCanvas;
1857 this->inval(nullptr);
1858 break;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001859 case 'f':
chudy@google.com4605a3f2012-08-01 17:58:01 +00001860 // only
djsollen@google.com6ff82552011-11-07 15:43:57 +00001861 toggleFPS();
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001862 break;
caryclark5fb6bd42014-06-23 11:25:00 -07001863 case 'F':
Cary Clark992c7b02014-07-31 08:58:44 -04001864 FLAGS_portableFonts ^= true;
halcanary96fcdcc2015-08-27 07:41:13 -07001865 this->inval(nullptr);
caryclark5fb6bd42014-06-23 11:25:00 -07001866 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001867 case 'g':
1868 fRequestGrabImage = true;
halcanary96fcdcc2015-08-27 07:41:13 -07001869 this->inval(nullptr);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001870 break;
reed@google.coma4f81372012-11-15 15:56:38 +00001871 case 'G':
1872 gShowGMBounds = !gShowGMBounds;
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001873 post_event_to_sink(GMSampleView::NewShowSizeEvt(gShowGMBounds),
reed@google.coma4f81372012-11-15 15:56:38 +00001874 curr_view(this));
halcanary96fcdcc2015-08-27 07:41:13 -07001875 this->inval(nullptr);
reed@google.coma4f81372012-11-15 15:56:38 +00001876 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001877 case 'i':
1878 this->zoomIn();
1879 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001880 case 'o':
1881 this->zoomOut();
1882 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001883 case 'r':
1884 fRotate = !fRotate;
halcanary96fcdcc2015-08-27 07:41:13 -07001885 this->inval(nullptr);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001886 this->updateTitle();
1887 return true;
bsalomon@google.come8f09102011-09-08 18:48:12 +00001888 case 'k':
1889 fPerspAnim = !fPerspAnim;
halcanary96fcdcc2015-08-27 07:41:13 -07001890 this->inval(nullptr);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001891 this->updateTitle();
1892 return true;
reed093b4e82015-08-04 18:46:38 -07001893 case 'K':
1894 fSaveToSKP = true;
halcanary96fcdcc2015-08-27 07:41:13 -07001895 this->inval(nullptr);
reed093b4e82015-08-04 18:46:38 -07001896 return true;
kkinnunen8686a5e2016-01-05 00:04:32 -08001897 case 'M':
reed91b4dfb2016-01-08 08:23:19 -08001898 fUsePicture = !fUsePicture;
kkinnunen8686a5e2016-01-05 00:04:32 -08001899 this->inval(nullptr);
1900 this->updateTitle();
1901 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001902#if SK_SUPPORT_GPU
twiz@google.com05e70242012-01-27 19:12:00 +00001903 case 'p':
1904 {
1905 GrContext* grContext = this->getGrContext();
1906 if (grContext) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001907 size_t cacheBytes;
halcanary96fcdcc2015-08-27 07:41:13 -07001908 grContext->getResourceCacheUsage(nullptr, &cacheBytes);
twiz@google.com05e70242012-01-27 19:12:00 +00001909 grContext->freeGpuResources();
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001910 SkDebugf("Purged %d bytes from the GPU resource cache.\n", cacheBytes);
twiz@google.com05e70242012-01-27 19:12:00 +00001911 }
1912 }
1913 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001914#endif
reed@android.com0ae6b242008-12-23 16:49:54 +00001915 default:
1916 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001917 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001918
scroggo@google.com7dadc742012-04-18 14:07:57 +00001919 if (fAppMenu->handleKeyEquivalent(uni)|| fSlideMenu->handleKeyEquivalent(uni)) {
1920 this->onUpdateMenu(fAppMenu);
1921 this->onUpdateMenu(fSlideMenu);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001922 return true;
1923 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001924 return this->INHERITED::onHandleChar(uni);
1925}
1926
yangsu@google.com921091f2011-08-02 13:39:12 +00001927void SampleWindow::setDeviceType(DeviceType type) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001928 if (type == fDeviceType)
1929 return;
halcanary9d524f22016-03-29 09:03:52 -07001930
reeda34be682016-02-15 07:48:35 -08001931 fDevManager->tearDownBackend(this);
reeda34be682016-02-15 07:48:35 -08001932 fDeviceType = type;
csmartdalton008b9d82017-02-22 12:00:42 -07001933 fDevManager->setUpBackend(this, fBackendOptions);
halcanary9d524f22016-03-29 09:03:52 -07001934
reeda34be682016-02-15 07:48:35 -08001935 this->updateTitle();
1936 this->inval(nullptr);
1937}
1938
brianosmanb109b8c2016-06-16 13:03:24 -07001939void SampleWindow::setDeviceColorType(SkColorType ct, sk_sp<SkColorSpace> cs) {
1940 this->setColorType(ct, std::move(cs));
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001941
1942 fDevManager->tearDownBackend(this);
csmartdalton008b9d82017-02-22 12:00:42 -07001943 fDevManager->setUpBackend(this, fBackendOptions);
halcanary9d524f22016-03-29 09:03:52 -07001944
yangsu@google.com921091f2011-08-02 13:39:12 +00001945 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001946 this->inval(nullptr);
yangsu@google.com921091f2011-08-02 13:39:12 +00001947}
1948
Scroggo2c8208f2011-06-15 16:49:08 +00001949void SampleWindow::toggleSlideshow() {
1950 fAnimating = !fAnimating;
1951 this->postAnimatingEvent();
1952 this->updateTitle();
1953}
1954
1955void SampleWindow::toggleRendering() {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001956 this->setDeviceType(cycle_devicetype(fDeviceType));
Scroggo2c8208f2011-06-15 16:49:08 +00001957 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001958 this->inval(nullptr);
Scroggo2c8208f2011-06-15 16:49:08 +00001959}
1960
djsollen@google.com6ff82552011-11-07 15:43:57 +00001961void SampleWindow::toggleFPS() {
1962 fMeasureFPS = !fMeasureFPS;
1963 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001964 this->inval(nullptr);
djsollen@google.com6ff82552011-11-07 15:43:57 +00001965}
1966
reed4302ae92014-10-06 12:29:56 -07001967void SampleWindow::toggleDistanceFieldFonts() {
jvanverth629162d2015-11-08 08:07:24 -08001968 // reset backend
1969 fDevManager->tearDownBackend(this);
csmartdalton008b9d82017-02-22 12:00:42 -07001970 fDevManager->setUpBackend(this, fBackendOptions);
jvanverth629162d2015-11-08 08:07:24 -08001971
reed4302ae92014-10-06 12:29:56 -07001972 SkSurfaceProps props = this->getSurfaceProps();
bsalomonafcd7cd2015-08-31 12:39:41 -07001973 uint32_t flags = props.flags() ^ SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
reed4302ae92014-10-06 12:29:56 -07001974 this->setSurfaceProps(SkSurfaceProps(flags, props.pixelGeometry()));
1975
1976 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001977 this->inval(nullptr);
reed4302ae92014-10-06 12:29:56 -07001978}
1979
bungeman5a59a422016-03-31 08:30:38 -07001980void SampleWindow::setPixelGeometry(int pixelGeometryIndex) {
1981 // reset backend
1982 fDevManager->tearDownBackend(this);
csmartdalton008b9d82017-02-22 12:00:42 -07001983 fDevManager->setUpBackend(this, fBackendOptions);
bungeman5a59a422016-03-31 08:30:38 -07001984
1985 const SkSurfaceProps& oldProps = this->getSurfaceProps();
1986 SkSurfaceProps newProps(oldProps.flags(), SkSurfaceProps::kLegacyFontHost_InitType);
1987 if (pixelGeometryIndex > 0) {
1988 newProps = SkSurfaceProps(oldProps.flags(),
1989 gPixelGeometryStates[pixelGeometryIndex].pixelGeometry);
1990 }
1991 this->setSurfaceProps(newProps);
1992
1993 this->updateTitle();
1994 this->inval(nullptr);
1995}
1996
reed@android.com8a1c16f2008-12-17 15:59:43 +00001997#include "SkDumpCanvas.h"
1998
1999bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00002000 {
2001 SkView* view = curr_view(this);
2002 if (view) {
2003 SkEvent evt(gKeyEvtName);
2004 evt.setFast32(key);
2005 if (view->doQuery(&evt)) {
2006 return true;
2007 }
2008 }
2009 }
bungemance560262016-03-03 13:32:38 -08002010
2011 int dx = 0xFF;
2012 int dy = 0xFF;
2013
reed@android.com8a1c16f2008-12-17 15:59:43 +00002014 switch (key) {
2015 case kRight_SkKey:
2016 if (this->nextSample()) {
2017 return true;
2018 }
2019 break;
2020 case kLeft_SkKey:
scroggo@google.come2dd9732012-08-15 20:03:06 +00002021 if (this->previousSample()) {
2022 return true;
2023 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002024 return true;
2025 case kUp_SkKey:
reedf7f79d22015-02-21 09:03:20 -08002026 this->changeZoomLevel(1.f / 32.f);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002027 return true;
2028 case kDown_SkKey:
reedf7f79d22015-02-21 09:03:20 -08002029 this->changeZoomLevel(-1.f / 32.f);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002030 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002031 case kOK_SkKey: {
2032 SkString title;
2033 if (curr_title(this, &title)) {
2034 writeTitleToPrefs(title.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002035 }
2036 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002037 }
reed@android.com34245c72009-11-03 04:00:48 +00002038 case kBack_SkKey:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002039 this->showOverview();
reed@android.com34245c72009-11-03 04:00:48 +00002040 return true;
bungemance560262016-03-03 13:32:38 -08002041
2042 case k5_SkKey: dx = 0; dy = 0; break;
2043 case k8_SkKey: dx = 0; dy = -1; break;
2044 case k6_SkKey: dx = 1; dy = 0; break;
2045 case k2_SkKey: dx = 0; dy = 1; break;
2046 case k4_SkKey: dx = -1; dy = 0; break;
2047 case k7_SkKey: dx = -1; dy = -1; break;
2048 case k9_SkKey: dx = 1; dy = -1; break;
2049 case k3_SkKey: dx = 1; dy = 1; break;
2050 case k1_SkKey: dx = -1; dy = 1; break;
2051
reed@android.com8a1c16f2008-12-17 15:59:43 +00002052 default:
2053 break;
2054 }
bungemance560262016-03-03 13:32:38 -08002055
2056 if (0xFF != dx && 0xFF != dy) {
2057 this->changeOffset({SkIntToScalar(dx / 32.0f), SkIntToScalar(dy / 32.0f)});
2058 return true;
2059 }
2060
reed@android.com8a1c16f2008-12-17 15:59:43 +00002061 return this->INHERITED::onHandleKey(key);
2062}
2063
reed@google.com52f57e12011-03-16 12:10:02 +00002064///////////////////////////////////////////////////////////////////////////////
2065
2066static const char gGestureClickType[] = "GestureClickType";
2067
Scroggod3aed392011-06-22 13:26:56 +00002068bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
reed@google.com4d5c26d2013-01-08 16:17:50 +00002069 void* owner, unsigned modi) {
Scroggo0f185c22011-03-24 18:35:50 +00002070 if (Click::kMoved_State == state) {
2071 updatePointer(x, y);
2072 }
reed@google.come1ca7052013-12-17 19:22:07 +00002073 int w = SkScalarRoundToInt(this->width());
2074 int h = SkScalarRoundToInt(this->height());
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002075
2076 // check for the resize-box
2077 if (w - x < 16 && h - y < 16) {
2078 return false; // let the OS handle the click
chudy@google.com4605a3f2012-08-01 17:58:01 +00002079 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002080 else if (fMagnify) {
2081 //it's only necessary to update the drawing if there's a click
halcanary96fcdcc2015-08-27 07:41:13 -07002082 this->inval(nullptr);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002083 return false; //prevent dragging while magnify is enabled
reed@google.com4d5c26d2013-01-08 16:17:50 +00002084 } else {
2085 // capture control+option, and trigger debugger
2086 if ((modi & kControl_SkModifierKey) && (modi & kOption_SkModifierKey)) {
2087 if (Click::kDown_State == state) {
2088 SkEvent evt("debug-hit-test");
2089 evt.setS32("debug-hit-test-x", x);
2090 evt.setS32("debug-hit-test-y", y);
2091 curr_view(this)->doEvent(evt);
2092 }
2093 return true;
2094 } else {
2095 return this->INHERITED::onDispatchClick(x, y, state, owner, modi);
2096 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002097 }
2098}
2099
reed@google.com52f57e12011-03-16 12:10:02 +00002100class GestureClick : public SkView::Click {
2101public:
2102 GestureClick(SkView* target) : SkView::Click(target) {
2103 this->setType(gGestureClickType);
2104 }
2105
2106 static bool IsGesture(Click* click) {
2107 return click->isType(gGestureClickType);
2108 }
2109};
2110
reed@google.com4d5c26d2013-01-08 16:17:50 +00002111SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y,
2112 unsigned modi) {
reed@google.com52f57e12011-03-16 12:10:02 +00002113 return new GestureClick(this);
2114}
2115
2116bool SampleWindow::onClick(Click* click) {
2117 if (GestureClick::IsGesture(click)) {
bsalomon@google.com4d4f2812011-12-12 18:34:01 +00002118 float x = static_cast<float>(click->fICurr.fX);
2119 float y = static_cast<float>(click->fICurr.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00002120
reed@google.com52f57e12011-03-16 12:10:02 +00002121 switch (click->fState) {
2122 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00002123 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00002124 break;
2125 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00002126 fGesture.touchMoved(click->fOwner, x, y);
reed@google.comf03bb562011-11-11 21:42:12 +00002127 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002128 break;
2129 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00002130 fGesture.touchEnd(click->fOwner);
reed@google.comf03bb562011-11-11 21:42:12 +00002131 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002132 break;
2133 }
2134 return true;
2135 }
2136 return false;
2137}
2138
2139///////////////////////////////////////////////////////////////////////////////
2140
reed@android.com8a1c16f2008-12-17 15:59:43 +00002141void SampleWindow::loadView(SkView* view) {
2142 SkView::F2BIter iter(this);
2143 SkView* prev = iter.next();
2144 if (prev) {
2145 prev->detachFromParent();
2146 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002147
reed@android.com8a1c16f2008-12-17 15:59:43 +00002148 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00002149 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002150 this->attachChildToFront(view)->unref();
2151 view->setSize(this->width(), this->height());
2152
yangsu@google.com921091f2011-08-02 13:39:12 +00002153 //repopulate the slide menu when a view is loaded
scroggo@google.com7dadc742012-04-18 14:07:57 +00002154 fSlideMenu->reset();
djsollen@google.com388974f2013-01-17 13:20:01 +00002155
scroggo@google.com7dadc742012-04-18 14:07:57 +00002156 this->onUpdateMenu(fSlideMenu);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002157 this->updateTitle();
2158}
2159
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002160static const char* gDeviceTypePrefix[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002161 "raster: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002162#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00002163 "opengl: ",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00002164#if SK_ANGLE
2165 "angle: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002166#endif // SK_ANGLE
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002167#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +00002168};
bungeman99fe8222015-08-20 07:57:51 -07002169static_assert(SK_ARRAY_COUNT(gDeviceTypePrefix) == SampleWindow::kDeviceTypeCnt,
2170 "array_size_mismatch");
reed@android.com8a1c16f2008-12-17 15:59:43 +00002171
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002172static const char* trystate_str(SkOSMenu::TriState state,
reed@google.com569e0432011-04-05 13:07:03 +00002173 const char trueStr[], const char falseStr[]) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002174 if (SkOSMenu::kOnState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002175 return trueStr;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002176 } else if (SkOSMenu::kOffState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002177 return falseStr;
2178 }
halcanary96fcdcc2015-08-27 07:41:13 -07002179 return nullptr;
reed@google.com569e0432011-04-05 13:07:03 +00002180}
2181
reedddb5eca2014-10-08 11:10:51 -07002182bool SampleWindow::getRawTitle(SkString* title) {
2183 return curr_title(this, title);
2184}
reed@android.com8a1c16f2008-12-17 15:59:43 +00002185
reedddb5eca2014-10-08 11:10:51 -07002186void SampleWindow::updateTitle() {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002187 SkString title;
reedddb5eca2014-10-08 11:10:51 -07002188 if (!this->getRawTitle(&title)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002189 title.set("<unknown>");
2190 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002191
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002192 title.prepend(gDeviceTypePrefix[fDeviceType]);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002193
liyuqianebcb8aa2016-10-11 12:20:08 -07002194 if (gSkUseAnalyticAA) {
Yuqian Li550148b2017-01-13 10:13:13 -05002195 if (gSkForceAnalyticAA) {
2196 title.prepend("<FAAA> ");
2197 } else {
2198 title.prepend("<AAA> ");
2199 }
liyuqianebcb8aa2016-10-11 12:20:08 -07002200 }
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00002201 if (fTilingMode != kNo_Tiling) {
2202 title.prependf("<T: %s> ", gTilingInfo[fTilingMode].label);
2203 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002204 if (fAnimating) {
2205 title.prepend("<A> ");
2206 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00002207 if (fRotate) {
2208 title.prepend("<R> ");
2209 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00002210 if (fPerspAnim) {
2211 title.prepend("<K> ");
2212 }
bsalomonafcd7cd2015-08-31 12:39:41 -07002213 if (this->getSurfaceProps().flags() & SkSurfaceProps::kUseDeviceIndependentFonts_Flag) {
2214 title.prepend("<DIF> ");
reed4302ae92014-10-06 12:29:56 -07002215 }
reed91b4dfb2016-01-08 08:23:19 -08002216 if (fUsePicture) {
2217 title.prepend("<P> ");
kkinnunen8686a5e2016-01-05 00:04:32 -08002218 }
reedbabc3de2016-07-08 08:43:27 -07002219 if (fUseDeferredCanvas) {
2220 title.prepend("<E> ");
2221 }
reed@google.com569e0432011-04-05 13:07:03 +00002222
2223 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
2224 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed93a12152015-03-16 10:08:34 -07002225 title.prepend(gFilterQualityStates[fFilterQualityIndex].fLabel);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002226 title.prepend(trystate_str(fSubpixelState, "S ", "s "));
halcanary96fcdcc2015-08-27 07:41:13 -07002227 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : nullptr);
2228 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : nullptr);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002229 title.prepend(gHintingStates[fHintingState].label);
bungeman5a59a422016-03-31 08:30:38 -07002230 title.prepend(gPixelGeometryStates[fPixelGeometryIndex].label);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002231
bungemance560262016-03-03 13:32:38 -08002232 if (fOffset.fX || fOffset.fY) {
2233 title.prependf("(%.2f, %.2f) ", SkScalarToFloat(fOffset.fX), SkScalarToFloat(fOffset.fY));
2234 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002235 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00002236 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002237 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002238
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002239 if (fMeasureFPS) {
jvanverth4d0d81a2015-08-11 11:07:07 -07002240 title.appendf(" %8.4f ms", fMeasureFPS_Time / (float)FPS_REPEAT_COUNT);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002241 }
reedddb5eca2014-10-08 11:10:51 -07002242
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002243#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00002244 if (IsGpuDeviceType(fDeviceType) &&
bsalomon49f085d2014-09-05 13:34:00 -07002245 fDevManager &&
robertphillipsecf3dbe2016-07-28 15:17:34 -07002246 fDevManager->numColorSamples() > 0) {
bsalomon@google.com11959252012-04-06 20:13:38 +00002247 title.appendf(" [MSAA: %d]",
robertphillipsecf3dbe2016-07-28 15:17:34 -07002248 fDevManager->numColorSamples());
bsalomon@google.com11959252012-04-06 20:13:38 +00002249 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002250#endif
bsalomon@google.com11959252012-04-06 20:13:38 +00002251
brianosman35a02a82016-09-29 14:37:02 -07002252 title.appendf(" %s", gConfig[fColorConfigIndex].fName);
reeda34be682016-02-15 07:48:35 -08002253
brianosman2d1ee792016-05-05 12:24:31 -07002254 if (fDevManager && fDevManager->getColorBits() > 24) {
2255 title.appendf(" %d bpc", fDevManager->getColorBits());
2256 }
2257
reed@android.com8a1c16f2008-12-17 15:59:43 +00002258 this->setTitle(title.c_str());
2259}
2260
2261void SampleWindow::onSizeChange() {
2262 this->INHERITED::onSizeChange();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002263
reed@android.com8a1c16f2008-12-17 15:59:43 +00002264 SkView::F2BIter iter(this);
2265 SkView* view = iter.next();
2266 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002267
reed@android.com8a1c16f2008-12-17 15:59:43 +00002268 // rebuild our clippath
2269 {
2270 const SkScalar W = this->width();
2271 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002272
reed@android.com8a1c16f2008-12-17 15:59:43 +00002273 fClipPath.reset();
2274#if 0
2275 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
2276 SkRect r;
2277 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
2278 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
2279 fClipPath.addRect(r);
2280 }
2281#else
2282 SkRect r;
2283 r.set(0, 0, W, H);
2284 fClipPath.addRect(r, SkPath::kCCW_Direction);
2285 r.set(W/4, H/4, W*3/4, H*3/4);
2286 fClipPath.addRect(r, SkPath::kCW_Direction);
2287#endif
2288 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002289
Scroggo2c8208f2011-06-15 16:49:08 +00002290 fZoomCenterX = SkScalarHalf(this->width());
2291 fZoomCenterY = SkScalarHalf(this->height());
2292
djsollen@google.com56c69772011-11-08 19:00:26 +00002293#ifdef SK_BUILD_FOR_ANDROID
Scroggob4490c72011-06-17 13:53:05 +00002294 // FIXME: The first draw after a size change does not work on Android, so
2295 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00002296 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00002297#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00002298 this->updateTitle(); // to refresh our config
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002299 fDevManager->windowSizeChanged(this);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00002300
2301 if (fTilingMode != kNo_Tiling && SampleView::IsSampleView(view)) {
2302 ((SampleView*)view)->onTileSizeChanged(this->tileSize());
2303 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002304}
2305
2306///////////////////////////////////////////////////////////////////////////////
2307
reed@android.comf2b98d62010-12-20 18:26:13 +00002308template <typename T> void SkTBSort(T array[], int count) {
2309 for (int i = 1; i < count - 1; i++) {
2310 bool didSwap = false;
2311 for (int j = count - 1; j > i; --j) {
2312 if (array[j] < array[j-1]) {
2313 T tmp(array[j-1]);
2314 array[j-1] = array[j];
2315 array[j] = tmp;
2316 didSwap = true;
2317 }
2318 }
2319 if (!didSwap) {
2320 break;
2321 }
2322 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002323
reed@android.comf2b98d62010-12-20 18:26:13 +00002324 for (int k = 0; k < count - 1; k++) {
2325 SkASSERT(!(array[k+1] < array[k]));
2326 }
2327}
2328
2329#include "SkRandom.h"
2330
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002331static void rand_rect(SkIRect* rect, SkRandom& rand) {
reed@android.comf2b98d62010-12-20 18:26:13 +00002332 int bits = 8;
2333 int shift = 32 - bits;
2334 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
2335 rand.nextU() >> shift, rand.nextU() >> shift);
2336 rect->sort();
2337}
2338
2339static void dumpRect(const SkIRect& r) {
2340 SkDebugf(" { %d, %d, %d, %d },\n",
2341 r.fLeft, r.fTop,
2342 r.fRight, r.fBottom);
2343}
2344
2345static void test_rects(const SkIRect rect[], int count) {
2346 SkRegion rgn0, rgn1;
2347
2348 for (int i = 0; i < count; i++) {
2349 rgn0.op(rect[i], SkRegion::kUnion_Op);
2350 // dumpRect(rect[i]);
2351 }
2352 rgn1.setRects(rect, count);
2353
2354 if (rgn0 != rgn1) {
2355 SkDebugf("\n");
2356 for (int i = 0; i < count; i++) {
2357 dumpRect(rect[i]);
2358 }
2359 SkDebugf("\n");
2360 }
2361}
2362
2363static void test() {
2364 size_t i;
2365
2366 const SkIRect r0[] = {
2367 { 0, 0, 1, 1 },
2368 { 2, 2, 3, 3 },
2369 };
2370 const SkIRect r1[] = {
2371 { 0, 0, 1, 3 },
2372 { 1, 1, 2, 2 },
2373 { 2, 0, 3, 3 },
2374 };
2375 const SkIRect r2[] = {
2376 { 0, 0, 1, 2 },
2377 { 2, 1, 3, 3 },
2378 { 4, 0, 5, 1 },
2379 { 6, 0, 7, 4 },
2380 };
2381
2382 static const struct {
2383 const SkIRect* fRects;
2384 int fCount;
2385 } gRecs[] = {
2386 { r0, SK_ARRAY_COUNT(r0) },
2387 { r1, SK_ARRAY_COUNT(r1) },
2388 { r2, SK_ARRAY_COUNT(r2) },
2389 };
2390
2391 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
2392 test_rects(gRecs[i].fRects, gRecs[i].fCount);
2393 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002394
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002395 SkRandom rand;
reed@android.comf2b98d62010-12-20 18:26:13 +00002396 for (i = 0; i < 10000; i++) {
2397 SkRegion rgn0, rgn1;
2398
2399 const int N = 8;
2400 SkIRect rect[N];
2401 for (int j = 0; j < N; j++) {
2402 rand_rect(&rect[j], rand);
2403 }
2404 test_rects(rect, N);
2405 }
2406}
2407
caryclark@google.com02939ce2012-06-06 12:09:51 +00002408// FIXME: this should be in a header
2409SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00002410SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
caryclark@google.com02939ce2012-06-06 12:09:51 +00002411 if (false) { // avoid bit rot, suppress warning
2412 test();
2413 }
halcanary96fcdcc2015-08-27 07:41:13 -07002414 return new SampleWindow(hwnd, argc, argv, nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002415}
2416
caryclark@google.com02939ce2012-06-06 12:09:51 +00002417// FIXME: this should be in a header
2418void get_preferred_size(int* x, int* y, int* width, int* height);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002419void get_preferred_size(int* x, int* y, int* width, int* height) {
2420 *x = 10;
2421 *y = 50;
2422 *width = 640;
2423 *height = 480;
2424}
2425
caryclark@google.com5987f582012-10-02 18:33:14 +00002426#ifdef SK_BUILD_FOR_IOS
reedf0b17102014-10-22 13:06:00 -07002427#include "SkApplication.h"
caryclark17f0b6d2014-07-22 10:15:34 -07002428IOS_launch_type set_cmd_line_args(int , char *[], const char* resourceDir) {
caryclarkf568f6a2014-07-18 10:58:56 -07002429 SetResourcePath(resourceDir);
caryclark17f0b6d2014-07-22 10:15:34 -07002430 return kApplication__iOSLaunchType;
caryclark@google.com5987f582012-10-02 18:33:14 +00002431}
2432#endif
2433
reed@android.com8a1c16f2008-12-17 15:59:43 +00002434void application_init() {
2435// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002436#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00002437 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002438#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002439 SkGraphics::Init();
2440 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002441}
2442
2443void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002444 SkEvent::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002445}