blob: 8797c4d5ca37bceda6a4609b19b1f81087f1d996 [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"
17#include "SkData.h"
reedddb5eca2014-10-08 11:10:51 -070018#include "SkDocument.h"
robertphillips1e876182016-07-29 08:28:25 -070019#include "SkGammaColorFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkGraphics.h"
brianosman2d1ee792016-05-05 12:24:31 -070021#include "SkImage_Base.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +000022#include "SkImageEncoder.h"
tfarinabcbc1782014-06-18 14:32:48 -070023#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050024#include "SkOSPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025#include "SkPaint.h"
fmalita822ace92015-06-15 07:07:32 -070026#include "SkPaintFilterCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000027#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000028#include "SkPictureRecorder.h"
brianosman36db3f42016-04-20 09:20:46 -070029#include "SkPM4fPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000030#include "SkStream.h"
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +000031#include "SkSurface.h"
scroggo565901d2015-12-10 10:44:13 -080032#include "SkTemplates.h"
edisonn@google.comf8b6b012013-07-11 14:28:04 +000033#include "SkTSort.h"
reed@android.com44177402009-11-23 21:07:51 +000034#include "SkTime.h"
Scroggo0f185c22011-03-24 18:35:50 +000035#include "SkTypeface.h"
tfarinabcbc1782014-06-18 14:32:48 -070036#include "SkWindow.h"
tfarinabcbc1782014-06-18 14:32:48 -070037#include "sk_tool_utils.h"
liyuqian38911a72016-10-04 11:23:22 -070038#include "SkScan.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050039#include "SkClipOpPriv.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000040
reed54dc4872016-09-13 08:09:45 -070041#include "SkReadBuffer.h"
42#include "SkStream.h"
43
Mike Reed476e6c92016-09-30 15:26:47 -040044#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
45#include "SkCGUtils.h"
46#endif
47
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000048#if SK_SUPPORT_GPU
bsalomon3724e572016-03-30 18:56:19 -070049# include "gl/GrGLInterface.h"
50# include "gl/GrGLUtil.h"
bsalomon3724e572016-03-30 18:56:19 -070051# include "GrContext.h"
robertphillips7715e062016-04-22 10:57:16 -070052# include "SkGr.h"
bsalomon3724e572016-03-30 18:56:19 -070053# if SK_ANGLE
bsalomon273c0f52016-03-31 10:59:06 -070054# include "gl/angle/GLTestContext_angle.h"
bsalomon3724e572016-03-30 18:56:19 -070055# endif
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000056#else
57class GrContext;
58#endif
Scroggo2c8208f2011-06-15 16:49:08 +000059
brianosman35a02a82016-09-29 14:37:02 -070060enum OutputColorSpace {
61 kLegacy_OutputColorSpace,
62 kSRGB_OutputColorSpace,
Brian Osmanfc2e7e02016-12-02 12:10:36 -050063 kNarrow_OutputColorSpace,
brianosman35a02a82016-09-29 14:37:02 -070064 kMonitor_OutputColorSpace,
reeda34be682016-02-15 07:48:35 -080065};
66
brianosman35a02a82016-09-29 14:37:02 -070067const struct {
68 SkColorType fColorType;
69 OutputColorSpace fColorSpace;
70 const char* fName;
71} gConfig[] = {
72 { kN32_SkColorType, kLegacy_OutputColorSpace, "L32" },
73 { kN32_SkColorType, kSRGB_OutputColorSpace, "S32" },
74 { kRGBA_F16_SkColorType, kSRGB_OutputColorSpace, "F16" },
Brian Osmanfc2e7e02016-12-02 12:10:36 -050075 { kRGBA_F16_SkColorType, kNarrow_OutputColorSpace, "F16 Narrow" },
brianosman35a02a82016-09-29 14:37:02 -070076 { kRGBA_F16_SkColorType, kMonitor_OutputColorSpace, "F16 Device" },
77};
reeda34be682016-02-15 07:48:35 -080078
reed5f629b42015-08-15 11:57:23 -070079// Should be 3x + 1
80#define kMaxFatBitsScale 28
81
reed@google.com1830c7a2012-06-04 12:05:43 +000082extern SampleView* CreateSamplePictFileView(const char filename[]);
83
84class PictFileFactory : public SkViewFactory {
85 SkString fFilename;
86public:
87 PictFileFactory(const SkString& filename) : fFilename(filename) {}
mtklein36352bf2015-03-25 18:17:31 -070088 SkView* operator() () const override {
reed@google.com1830c7a2012-06-04 12:05:43 +000089 return CreateSamplePictFileView(fFilename.c_str());
90 }
91};
92
fmalita851d68a2016-08-15 07:48:47 -070093extern SampleView* CreateSampleSVGFileView(const SkString& filename);
fmalita6ceef3d2016-07-26 18:46:34 -070094
95class SVGFileFactory : public SkViewFactory {
96 SkString fFilename;
97public:
98 SVGFileFactory(const SkString& filename) : fFilename(filename) {}
99 SkView* operator() () const override {
fmalita851d68a2016-08-15 07:48:47 -0700100 return CreateSampleSVGFileView(fFilename);
fmalita6ceef3d2016-07-26 18:46:34 -0700101 }
102};
103
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000104#ifdef SAMPLE_PDF_FILE_VIEWER
105extern SampleView* CreateSamplePdfFileViewer(const char filename[]);
106
107class PdfFileViewerFactory : public SkViewFactory {
108 SkString fFilename;
109public:
110 PdfFileViewerFactory(const SkString& filename) : fFilename(filename) {}
mtklein36352bf2015-03-25 18:17:31 -0700111 SkView* operator() () const override {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000112 return CreateSamplePdfFileViewer(fFilename.c_str());
113 }
114};
115#endif // SAMPLE_PDF_FILE_VIEWER
116
bsalomon49996162016-06-14 09:43:52 -0700117#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000118//#define DEFAULT_TO_ANGLE 1
119#else
bsalomon@google.com82d12232013-09-09 15:36:26 +0000120#define DEFAULT_TO_GPU 0 // if 1 default rendering is on GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000121#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123#define ANIMATING_EVENTTYPE "nextSample"
reed@google.com84cfce12013-05-29 19:22:20 +0000124#define ANIMATING_DELAY 250
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000126#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +0000127 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000128#else
reed@google.combad8c872011-05-18 20:10:31 +0000129 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000130#endif
reed@google.combad8c872011-05-18 20:10:31 +0000131#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000132
reed@google.com3cec4d72011-07-06 13:59:47 +0000133static SampleWindow* gSampleWindow;
134
reed@google.coma4f81372012-11-15 15:56:38 +0000135static bool gShowGMBounds;
136
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +0000137static void post_event_to_sink(SkEvent* evt, SkEventSink* sink) {
reed@google.com2072db82011-11-08 15:18:10 +0000138 evt->setTargetID(sink->getSinkID())->post();
139}
140
reed76113a92015-02-02 12:55:02 -0800141static SkAnimTimer gAnimTimer;
142
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000143///////////////////////////////////////////////////////////////////////////////
144
145static const char* skip_until(const char* str, const char* skip) {
146 if (!str) {
halcanary96fcdcc2015-08-27 07:41:13 -0700147 return nullptr;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000148 }
149 return strstr(str, skip);
150}
151
152static const char* skip_past(const char* str, const char* skip) {
153 const char* found = skip_until(str, skip);
154 if (!found) {
halcanary96fcdcc2015-08-27 07:41:13 -0700155 return nullptr;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000156 }
157 return found + strlen(skip);
158}
159
160static const char* gPrefFileName = "sampleapp_prefs.txt";
161
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000162static bool readTitleFromPrefs(SkString* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000163 SkFILEStream stream(gPrefFileName);
164 if (!stream.isValid()) {
165 return false;
166 }
167
reed@google.com7fa2a652014-01-27 13:42:58 +0000168 size_t len = stream.getLength();
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000169 SkString data(len);
170 stream.read(data.writable_str(), len);
171 const char* s = data.c_str();
172
173 s = skip_past(s, "curr-slide-title");
174 s = skip_past(s, "=");
175 s = skip_past(s, "\"");
176 const char* stop = skip_until(s, "\"");
177 if (stop > s) {
178 title->set(s, stop - s);
179 return true;
180 }
181 return false;
182}
183
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000184static void writeTitleToPrefs(const char* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000185 SkFILEWStream stream(gPrefFileName);
186 SkString data;
187 data.printf("curr-slide-title = \"%s\"\n", title);
188 stream.write(data.c_str(), data.size());
189}
190
191///////////////////////////////////////////////////////////////////////////////
192
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000193class SampleWindow::DefaultDeviceManager : public SampleWindow::DeviceManager {
194public:
195
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000196 DefaultDeviceManager() {
197#if SK_SUPPORT_GPU
halcanary96fcdcc2015-08-27 07:41:13 -0700198 fCurContext = nullptr;
199 fCurIntf = nullptr;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000200 fMSAASampleCount = 0;
brianosman2d1ee792016-05-05 12:24:31 -0700201 fDeepColor = false;
202 fActualColorBits = 0;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000203#endif
204 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000205 }
206
207 virtual ~DefaultDeviceManager() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000208#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000209 SkSafeUnref(fCurContext);
210 SkSafeUnref(fCurIntf);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000211#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000212 }
213
brianosman2d1ee792016-05-05 12:24:31 -0700214 void setUpBackend(SampleWindow* win, int msaaSampleCount, bool deepColor) override {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000215 SkASSERT(kNone_BackEndType == fBackend);
robertphillips@google.com53e96a12012-03-30 14:47:53 +0000216
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000217 fBackend = kNone_BackEndType;
218
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000219#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000220 switch (win->getDeviceType()) {
reedbeedb812015-05-05 12:14:45 -0700221 case kRaster_DeviceType: // fallthrough
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000222 case kGPU_DeviceType:
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000223 // all these guys use the native backend
224 fBackend = kNativeGL_BackEndType;
225 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000226#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000227 case kANGLE_DeviceType:
228 // ANGLE is really the only odd man out
229 fBackend = kANGLE_BackEndType;
230 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000231#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000232 default:
233 SkASSERT(false);
234 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000235 }
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000236 AttachmentInfo attachmentInfo;
brianosman2d1ee792016-05-05 12:24:31 -0700237 bool result = win->attach(fBackend, msaaSampleCount, deepColor, &attachmentInfo);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000238 if (!result) {
bsalomon@google.com74913722011-10-27 20:44:19 +0000239 SkDebugf("Failed to initialize GL");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000240 return;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000241 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000242 fMSAASampleCount = msaaSampleCount;
brianosman2d1ee792016-05-05 12:24:31 -0700243 fDeepColor = deepColor;
244 // Assume that we have at least 24-bit output, for backends that don't supply this data
245 fActualColorBits = SkTMax(attachmentInfo.fColorBits, 24);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000246
halcanary96fcdcc2015-08-27 07:41:13 -0700247 SkASSERT(nullptr == fCurIntf);
Hal Canary2db83612016-11-04 13:02:54 -0400248 sk_sp<const GrGLInterface> glInterface;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000249 switch (win->getDeviceType()) {
reedbeedb812015-05-05 12:14:45 -0700250 case kRaster_DeviceType: // fallthrough
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000251 case kGPU_DeviceType:
252 // all these guys use the native interface
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000253 glInterface.reset(GrGLCreateNativeInterface());
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000254 break;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000255#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000256 case kANGLE_DeviceType:
bsalomon3724e572016-03-30 18:56:19 -0700257 glInterface.reset(sk_gpu_test::CreateANGLEGLInterface());
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000258 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000259#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000260 default:
261 SkASSERT(false);
262 break;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000263 }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000264
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000265 // Currently SampleApp does not use NVPR. TODO: Provide an NVPR device type that is skipped
266 // when the driver doesn't support NVPR.
267 fCurIntf = GrGLInterfaceRemoveNVPR(glInterface.get());
268
halcanary96fcdcc2015-08-27 07:41:13 -0700269 SkASSERT(nullptr == fCurContext);
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000270 fCurContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000271
halcanary96fcdcc2015-08-27 07:41:13 -0700272 if (nullptr == fCurContext || nullptr == fCurIntf) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000273 // We need some context and interface to see results
274 SkSafeUnref(fCurContext);
275 SkSafeUnref(fCurIntf);
halcanary96fcdcc2015-08-27 07:41:13 -0700276 fCurContext = nullptr;
277 fCurIntf = nullptr;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000278 SkDebugf("Failed to setup 3D");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000279
mtklein18300a32016-03-16 13:53:35 -0700280 win->release();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000281 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000282#endif // SK_SUPPORT_GPU
robertphillipsecf3dbe2016-07-28 15:17:34 -0700283 // call windowSizeChanged to create the gpu-backed Surface
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000284 this->windowSizeChanged(win);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000285 }
286
mtkleinf0599002015-07-13 06:18:39 -0700287 void tearDownBackend(SampleWindow *win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000288#if SK_SUPPORT_GPU
reed789e3272014-10-30 11:29:08 -0700289 if (fCurContext) {
290 // in case we have outstanding refs to this guy (lua?)
291 fCurContext->abandonContext();
292 fCurContext->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700293 fCurContext = nullptr;
reed789e3272014-10-30 11:29:08 -0700294 }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000295
296 SkSafeUnref(fCurIntf);
halcanary96fcdcc2015-08-27 07:41:13 -0700297 fCurIntf = nullptr;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000298
robertphillipsecf3dbe2016-07-28 15:17:34 -0700299 fGpuSurface = nullptr;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000300#endif
mtklein18300a32016-03-16 13:53:35 -0700301 win->release();
bsalomon@google.comddd40e52012-04-10 15:56:29 +0000302 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000303 }
304
robertphillipsecf3dbe2016-07-28 15:17:34 -0700305 sk_sp<SkSurface> makeSurface(SampleWindow::DeviceType dType, SampleWindow* win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000306#if SK_SUPPORT_GPU
bsalomon49f085d2014-09-05 13:34:00 -0700307 if (IsGpuDeviceType(dType) && fCurContext) {
reed4302ae92014-10-06 12:29:56 -0700308 SkSurfaceProps props(win->getSurfaceProps());
brianosman2d1ee792016-05-05 12:24:31 -0700309 if (kRGBA_F16_SkColorType == win->info().colorType() || fActualColorBits > 24) {
310 // If we're rendering to F16, we need an off-screen surface - the current render
311 // target is most likely the wrong format.
312 //
313 // If we're using a deep (10-bit or higher) surface, we probably need an off-screen
314 // surface. 10-bit, in particular, has strange gamma behavior.
brianosman6b086522016-04-14 12:39:00 -0700315 return SkSurface::MakeRenderTarget(fCurContext, SkBudgeted::kNo, win->info(),
robertphillipsecf3dbe2016-07-28 15:17:34 -0700316 fMSAASampleCount, &props);
brianosman6b086522016-04-14 12:39:00 -0700317 } else {
robertphillipsecf3dbe2016-07-28 15:17:34 -0700318 return fGpuSurface;
brianosman6b086522016-04-14 12:39:00 -0700319 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000320 }
reed0397e9f2014-09-18 11:29:01 -0700321#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700322 return nullptr;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000323 }
324
mtkleinf0599002015-07-13 06:18:39 -0700325 void publishCanvas(SampleWindow::DeviceType dType,
robertphillips1e876182016-07-29 08:28:25 -0700326 SkCanvas* renderingCanvas, SampleWindow* win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000327#if SK_SUPPORT_GPU
brianosman2d1ee792016-05-05 12:24:31 -0700328 if (!IsGpuDeviceType(dType) ||
329 kRGBA_F16_SkColorType == win->info().colorType() ||
330 fActualColorBits > 24) {
331 // We made/have an off-screen surface. Get the contents as an SkImage:
brianosman35a02a82016-09-29 14:37:02 -0700332 SkImageInfo offscreenInfo = win->info();
Brian Osmanfc2e7e02016-12-02 12:10:36 -0500333 if (kMonitor_OutputColorSpace == gConfig[win->getColorConfigIndex()].fColorSpace ||
334 kNarrow_OutputColorSpace == gConfig[win->getColorConfigIndex()].fColorSpace) {
brianosman35a02a82016-09-29 14:37:02 -0700335 // This is a big hack. We want our final output to be color "correct". If we snap
336 // an image in the gamut of the monitor, and then render to FBO0 (which we've tagged
337 // as sRGB), then we end up doing round-trip gamut conversion, and still seeing the
338 // same colors on-screen as if we weren't color managed at all.
339 // Instead, we readPixels into a buffer that we claim is sRGB (readPixels doesn't
340 // do gamut conversion), so these pixels then get thrown directly at the monitor,
341 // giving us the expected results (the output is adapted to the monitor's gamut).
Brian Osman526972e2016-10-24 09:24:02 -0400342 auto srgb = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
brianosman35a02a82016-09-29 14:37:02 -0700343 offscreenInfo = offscreenInfo.makeColorSpace(srgb);
344 }
brianosman2d1ee792016-05-05 12:24:31 -0700345 SkBitmap bm;
brianosman35a02a82016-09-29 14:37:02 -0700346 bm.allocPixels(offscreenInfo);
robertphillips1e876182016-07-29 08:28:25 -0700347 renderingCanvas->readPixels(&bm, 0, 0);
brianosman2d1ee792016-05-05 12:24:31 -0700348 SkPixmap pm;
349 bm.peekPixels(&pm);
350 sk_sp<SkImage> image(SkImage::MakeTextureFromPixmap(fCurContext, pm,
351 SkBudgeted::kNo));
robertphillipsecf3dbe2016-07-28 15:17:34 -0700352
robertphillips1e876182016-07-29 08:28:25 -0700353 SkCanvas* gpuCanvas = fGpuSurface->getCanvas();
brianosman2d1ee792016-05-05 12:24:31 -0700354
355 // With ten-bit output, we need to manually apply the gamma of the output device
356 // (unless we're in non-gamma correct mode, in which case our data is already
357 // fake-sRGB, like we're expected to put in the 10-bit buffer):
Brian Osman7039f742016-11-01 15:56:16 -0400358 bool doGamma = (fActualColorBits == 30) && win->info().colorSpace();
robertphillips1e876182016-07-29 08:28:25 -0700359
360 SkPaint gammaPaint;
reed374772b2016-10-05 17:33:02 -0700361 gammaPaint.setBlendMode(SkBlendMode::kSrc);
robertphillips1e876182016-07-29 08:28:25 -0700362 if (doGamma) {
363 gammaPaint.setColorFilter(SkGammaColorFilter::Make(1.0f / 2.2f));
364 }
365
366 gpuCanvas->drawImage(image, 0, 0, &gammaPaint);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000367 }
robertphillips1e876182016-07-29 08:28:25 -0700368
369 fGpuSurface->prepareForExternalIO();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000370#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000371
372 win->present();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000373 }
374
mtkleinf0599002015-07-13 06:18:39 -0700375 void windowSizeChanged(SampleWindow* win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000376#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000377 if (fCurContext) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000378 AttachmentInfo attachmentInfo;
brianosman2d1ee792016-05-05 12:24:31 -0700379 win->attach(fBackend, fMSAASampleCount, fDeepColor, &attachmentInfo);
brianosman2d1ee792016-05-05 12:24:31 -0700380 fActualColorBits = SkTMax(attachmentInfo.fColorBits, 24);
robertphillipsecf3dbe2016-07-28 15:17:34 -0700381 fGpuSurface = win->makeGpuBackedSurface(attachmentInfo, fCurIntf, fCurContext);
bsalomon@google.com74913722011-10-27 20:44:19 +0000382 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000383#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000384 }
385
mtkleinf0599002015-07-13 06:18:39 -0700386 GrContext* getGrContext() override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000387#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000388 return fCurContext;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000389#else
halcanary96fcdcc2015-08-27 07:41:13 -0700390 return nullptr;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000391#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000392 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000393
robertphillipsecf3dbe2016-07-28 15:17:34 -0700394 int numColorSamples() const override {
brianosman1dfec742016-09-15 07:19:53 -0700395#if SK_SUPPORT_GPU
robertphillipsecf3dbe2016-07-28 15:17:34 -0700396 return fMSAASampleCount;
brianosman1dfec742016-09-15 07:19:53 -0700397#else
398 return 0;
399#endif
bsalomon@google.com11959252012-04-06 20:13:38 +0000400 }
401
brianosman2d1ee792016-05-05 12:24:31 -0700402 int getColorBits() override {
403#if SK_SUPPORT_GPU
404 return fActualColorBits;
405#else
406 return 24;
407#endif
408 }
409
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000410private:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000411
412#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000413 GrContext* fCurContext;
414 const GrGLInterface* fCurIntf;
robertphillipsecf3dbe2016-07-28 15:17:34 -0700415 sk_sp<SkSurface> fGpuSurface;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000416 int fMSAASampleCount;
brianosman2d1ee792016-05-05 12:24:31 -0700417 bool fDeepColor;
418 int fActualColorBits;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000419#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000420
421 SkOSWindow::SkBackEndTypes fBackend;
422
423 typedef SampleWindow::DeviceManager INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000424};
425
426///////////////
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000427static const char view_inval_msg[] = "view-inval-msg";
428
Scroggo62b65b02011-06-21 16:01:26 +0000429void SampleWindow::postInvalDelay() {
reed@google.com87fac4a2011-08-04 13:50:17 +0000430 (new SkEvent(view_inval_msg, this->getSinkID()))->postDelay(1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000431}
432
433static bool isInvalEvent(const SkEvent& evt) {
434 return evt.isType(view_inval_msg);
435}
436//////////////////
437
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000438#include "GMSampleView.h"
439
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000440class AutoUnrefArray {
441public:
442 AutoUnrefArray() {}
443 ~AutoUnrefArray() {
444 int count = fObjs.count();
445 for (int i = 0; i < count; ++i) {
446 fObjs[i]->unref();
447 }
448 }
449 SkRefCnt*& push_back() { return *fObjs.append(); }
chudy@google.com4605a3f2012-08-01 17:58:01 +0000450
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000451private:
452 SkTDArray<SkRefCnt*> fObjs;
453};
454
455// registers GMs as Samples
456// This can't be performed during static initialization because it could be
457// run before GMRegistry has been fully built.
caryclark@google.com02939ce2012-06-06 12:09:51 +0000458static void SkGMRegistyToSampleRegistry() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000459 static bool gOnce;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000460 static AutoUnrefArray fRegisters;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000461
462 if (!gOnce) {
463 const skiagm::GMRegistry* gmreg = skiagm::GMRegistry::Head();
464 while (gmreg) {
465 fRegisters.push_back() = new SkViewRegister(gmreg->factory());
466 gmreg = gmreg->next();
467 }
468 gOnce = true;
469 }
470}
471
reed@google.comf0b5f682011-03-11 20:08:25 +0000472//////////////////////////////////////////////////////////////////////////////
473
reed@google.com569e0432011-04-05 13:07:03 +0000474enum FlipAxisEnum {
475 kFlipAxis_X = (1 << 0),
476 kFlipAxis_Y = (1 << 1)
477};
478
reed@google.comf0b5f682011-03-11 20:08:25 +0000479#include "SkDrawFilter.h"
480
bungeman@google.com96aabc82013-06-03 21:26:34 +0000481struct HintingState {
482 SkPaint::Hinting hinting;
483 const char* name;
484 const char* label;
485};
486static HintingState gHintingStates[] = {
halcanary96fcdcc2015-08-27 07:41:13 -0700487 {SkPaint::kNo_Hinting, "Mixed", nullptr },
bungeman@google.com96aabc82013-06-03 21:26:34 +0000488 {SkPaint::kNo_Hinting, "None", "H0 " },
489 {SkPaint::kSlight_Hinting, "Slight", "Hs " },
490 {SkPaint::kNormal_Hinting, "Normal", "Hn " },
491 {SkPaint::kFull_Hinting, "Full", "Hf " },
492};
493
bungeman5a59a422016-03-31 08:30:38 -0700494struct PixelGeometryState {
495 SkPixelGeometry pixelGeometry;
496 const char* name;
497 const char* label;
498};
499static PixelGeometryState gPixelGeometryStates[] = {
500 {SkPixelGeometry::kUnknown_SkPixelGeometry, "Mixed", nullptr },
501 {SkPixelGeometry::kUnknown_SkPixelGeometry, "Flat", "{Flat} " },
502 {SkPixelGeometry::kRGB_H_SkPixelGeometry, "RGB H", "{RGB H} " },
503 {SkPixelGeometry::kBGR_H_SkPixelGeometry, "BGR H", "{BGR H} " },
504 {SkPixelGeometry::kRGB_V_SkPixelGeometry, "RGB_V", "{RGB V} " },
505 {SkPixelGeometry::kBGR_V_SkPixelGeometry, "BGR_V", "{BGR V} " },
506};
507
reed93a12152015-03-16 10:08:34 -0700508struct FilterQualityState {
509 SkFilterQuality fQuality;
510 const char* fName;
511 const char* fLabel;
reed@google.com15bc13d2013-12-10 16:53:13 +0000512};
reed93a12152015-03-16 10:08:34 -0700513static FilterQualityState gFilterQualityStates[] = {
halcanary96fcdcc2015-08-27 07:41:13 -0700514 { kNone_SkFilterQuality, "Mixed", nullptr },
reed93a12152015-03-16 10:08:34 -0700515 { kNone_SkFilterQuality, "None", "F0 " },
516 { kLow_SkFilterQuality, "Low", "F1 " },
517 { kMedium_SkFilterQuality, "Medium", "F2 " },
518 { kHigh_SkFilterQuality, "High", "F3 " },
reed@google.com15bc13d2013-12-10 16:53:13 +0000519};
520
fmalita822ace92015-06-15 07:07:32 -0700521class FlagsFilterCanvas : public SkPaintFilterCanvas {
reed@google.comf0b5f682011-03-11 20:08:25 +0000522public:
fmalita822ace92015-06-15 07:07:32 -0700523 FlagsFilterCanvas(SkCanvas* canvas, SkOSMenu::TriState lcd, SkOSMenu::TriState aa,
524 SkOSMenu::TriState subpixel, int hinting, int filterQuality)
fmalitabad23dc2016-01-11 13:58:29 -0800525 : INHERITED(canvas)
fmalita822ace92015-06-15 07:07:32 -0700526 , fLCDState(lcd)
reed@google.com15bc13d2013-12-10 16:53:13 +0000527 , fAAState(aa)
528 , fSubpixelState(subpixel)
529 , fHintingState(hinting)
fmalita822ace92015-06-15 07:07:32 -0700530 , fFilterQualityIndex(filterQuality) {
reed93a12152015-03-16 10:08:34 -0700531 SkASSERT((unsigned)filterQuality < SK_ARRAY_COUNT(gFilterQualityStates));
reed@google.com15bc13d2013-12-10 16:53:13 +0000532 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000533
fmalita822ace92015-06-15 07:07:32 -0700534protected:
fmalita32cdc322016-01-12 07:21:11 -0800535 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type t) const override {
536 if (!*paint) {
fmalitabad23dc2016-01-11 13:58:29 -0800537 return true;
538 }
539
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000540 if (kText_Type == t && SkOSMenu::kMixedState != fLCDState) {
fmalita32cdc322016-01-12 07:21:11 -0800541 paint->writable()->setLCDRenderText(SkOSMenu::kOnState == fLCDState);
reed@google.com569e0432011-04-05 13:07:03 +0000542 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000543 if (SkOSMenu::kMixedState != fAAState) {
fmalita32cdc322016-01-12 07:21:11 -0800544 paint->writable()->setAntiAlias(SkOSMenu::kOnState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000545 }
reed93a12152015-03-16 10:08:34 -0700546 if (0 != fFilterQualityIndex) {
fmalita32cdc322016-01-12 07:21:11 -0800547 paint->writable()->setFilterQuality(gFilterQualityStates[fFilterQualityIndex].fQuality);
reed@google.com176753a2011-05-17 15:32:04 +0000548 }
bungeman@google.com96aabc82013-06-03 21:26:34 +0000549 if (SkOSMenu::kMixedState != fSubpixelState) {
fmalita32cdc322016-01-12 07:21:11 -0800550 paint->writable()->setSubpixelText(SkOSMenu::kOnState == fSubpixelState);
bungeman@google.com96aabc82013-06-03 21:26:34 +0000551 }
bungeman@google.com055aa522013-06-03 21:35:03 +0000552 if (0 != fHintingState && fHintingState < (int)SK_ARRAY_COUNT(gHintingStates)) {
fmalita32cdc322016-01-12 07:21:11 -0800553 paint->writable()->setHinting(gHintingStates[fHintingState].hinting);
reed@google.com09e3baa2011-05-18 12:04:31 +0000554 }
fmalitabad23dc2016-01-11 13:58:29 -0800555 return true;
reed@google.comf0b5f682011-03-11 20:08:25 +0000556 }
557
558private:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000559 SkOSMenu::TriState fLCDState;
560 SkOSMenu::TriState fAAState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000561 SkOSMenu::TriState fSubpixelState;
562 int fHintingState;
reed93a12152015-03-16 10:08:34 -0700563 int fFilterQualityIndex;
fmalita822ace92015-06-15 07:07:32 -0700564
565 typedef SkPaintFilterCanvas INHERITED;
reed@google.comf0b5f682011-03-11 20:08:25 +0000566};
567
reed@android.comf2b98d62010-12-20 18:26:13 +0000568///////////////////////////////////////////////////////////////////////////////
569
reed54dc4872016-09-13 08:09:45 -0700570class SampleTFSerializer : public SkTypefaceSerializer {
571public:
572 sk_sp<SkData> serialize(SkTypeface* tf) override {
573 tf->ref();
574 return SkData::MakeWithCopy(&tf, sizeof(tf));
575 }
576};
577
578class SampleTFDeserializer : public SkTypefaceDeserializer {
579public:
580 sk_sp<SkTypeface> deserialize(const void* data, size_t size) override {
581 SkASSERT(sizeof(SkTypeface*) == size);
582 SkTypeface* tf;
583 memcpy(&tf, data, size);
584 return sk_sp<SkTypeface>(tf); // this was ref'd in SampleTFSerializer
585 }
586};
587
588///////////////////////////////////////////////////////////////////////////////
589
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000590enum TilingMode {
591 kNo_Tiling,
592 kAbs_128x128_Tiling,
593 kAbs_256x256_Tiling,
594 kRel_4x4_Tiling,
595 kRel_1x16_Tiling,
596 kRel_16x1_Tiling,
597
598 kLast_TilingMode_Enum
599};
600
601struct TilingInfo {
602 const char* label;
603 SkScalar w, h;
604};
605
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000606static const struct TilingInfo gTilingInfo[] = {
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000607 { "No tiling", SK_Scalar1 , SK_Scalar1 }, // kNo_Tiling
608 { "128x128" , SkIntToScalar(128), SkIntToScalar(128) }, // kAbs_128x128_Tiling
609 { "256x256" , SkIntToScalar(256), SkIntToScalar(256) }, // kAbs_256x256_Tiling
610 { "1/4x1/4" , SK_Scalar1 / 4 , SK_Scalar1 / 4 }, // kRel_4x4_Tiling
611 { "1/1x1/16" , SK_Scalar1 , SK_Scalar1 / 16 }, // kRel_1x16_Tiling
612 { "1/16x1/1" , SK_Scalar1 / 16 , SK_Scalar1 }, // kRel_16x1_Tiling
613};
bungeman99fe8222015-08-20 07:57:51 -0700614static_assert((SK_ARRAY_COUNT(gTilingInfo) == kLast_TilingMode_Enum),
615 "Incomplete_tiling_labels");
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000616
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000617SkSize SampleWindow::tileSize() const {
618 SkASSERT((TilingMode)fTilingMode < kLast_TilingMode_Enum);
619 const struct TilingInfo* info = gTilingInfo + fTilingMode;
620 return SkSize::Make(info->w > SK_Scalar1 ? info->w : this->width() * info->w,
621 info->h > SK_Scalar1 ? info->h : this->height() * info->h);
622}
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000623//////////////////////////////////////////////////////////////////////////////
624
reed@android.comf2b98d62010-12-20 18:26:13 +0000625static SkView* curr_view(SkWindow* wind) {
626 SkView::F2BIter iter(wind);
627 return iter.next();
628}
629
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000630static bool curr_title(SkWindow* wind, SkString* title) {
631 SkView* view = curr_view(wind);
632 if (view) {
633 SkEvent evt(gTitleEvtName);
634 if (view->doQuery(&evt)) {
635 title->set(evt.findString(gTitleEvtName));
636 return true;
637 }
638 }
639 return false;
640}
641
reedd9adfe62015-02-01 19:01:04 -0800642bool SampleWindow::sendAnimatePulse() {
643 SkView* view = curr_view(this);
644 if (SampleView::IsSampleView(view)) {
reed76113a92015-02-02 12:55:02 -0800645 return ((SampleView*)view)->animate(gAnimTimer);
reedd9adfe62015-02-01 19:01:04 -0800646 }
647 return false;
648}
649
reed5f629b42015-08-15 11:57:23 -0700650void SampleWindow::setZoomCenter(float x, float y) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000651 fZoomCenterX = x;
652 fZoomCenterY = y;
Scroggo2c8208f2011-06-15 16:49:08 +0000653}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000654
reed5f629b42015-08-15 11:57:23 -0700655bool SampleWindow::zoomIn() {
Scroggo0f185c22011-03-24 18:35:50 +0000656 // Arbitrarily decided
reed5f629b42015-08-15 11:57:23 -0700657 if (fFatBitsScale == kMaxFatBitsScale) return false;
Scroggo0f185c22011-03-24 18:35:50 +0000658 fFatBitsScale++;
halcanary96fcdcc2015-08-27 07:41:13 -0700659 this->inval(nullptr);
Scroggo0f185c22011-03-24 18:35:50 +0000660 return true;
661}
662
reed5f629b42015-08-15 11:57:23 -0700663bool SampleWindow::zoomOut() {
Scroggo0f185c22011-03-24 18:35:50 +0000664 if (fFatBitsScale == 1) return false;
665 fFatBitsScale--;
halcanary96fcdcc2015-08-27 07:41:13 -0700666 this->inval(nullptr);
Scroggo0f185c22011-03-24 18:35:50 +0000667 return true;
668}
669
reed5f629b42015-08-15 11:57:23 -0700670void SampleWindow::updatePointer(int x, int y) {
Scroggo0f185c22011-03-24 18:35:50 +0000671 fMouseX = x;
672 fMouseY = y;
673 if (fShowZoomer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700674 this->inval(nullptr);
Scroggo0f185c22011-03-24 18:35:50 +0000675 }
676}
677
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000678static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
679 static const SampleWindow::DeviceType gCT[] = {
kkinnunen8686a5e2016-01-05 00:04:32 -0800680 SampleWindow::kRaster_DeviceType
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000681#if SK_SUPPORT_GPU
kkinnunen8686a5e2016-01-05 00:04:32 -0800682 , SampleWindow::kGPU_DeviceType
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000683#if SK_ANGLE
kkinnunen8686a5e2016-01-05 00:04:32 -0800684 , SampleWindow::kANGLE_DeviceType
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000685#endif // SK_ANGLE
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000686#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +0000687 };
bungeman99fe8222015-08-20 07:57:51 -0700688 static_assert(SK_ARRAY_COUNT(gCT) == SampleWindow::kDeviceTypeCnt, "array_size_mismatch");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000689 return gCT[ct];
690}
691
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000692static SkString getSampleTitle(const SkViewFactory* sampleFactory) {
693 SkView* view = (*sampleFactory)();
694 SkString title;
695 SampleCode::RequestTitle(view, &title);
696 view->unref();
697 return title;
698}
699
edisonn@google.comde36b692013-07-11 15:40:31 +0000700static bool compareSampleTitle(const SkViewFactory* first, const SkViewFactory* second) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000701 return strcmp(getSampleTitle(first).c_str(), getSampleTitle(second).c_str()) < 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000702}
703
reedb7ffe032015-11-05 11:59:17 -0800704static int find_by_title(const SkViewFactory* const* factories, int count, const char title[]) {
705 for (int i = 0; i < count; i++) {
706 if (getSampleTitle(factories[i]).equals(title)) {
707 return i;
708 }
709 }
710 return -1;
711}
712
713static void restrict_samples(SkTDArray<const SkViewFactory*>& factories, const SkString titles[],
714 int count) {
715 int newCount = 0;
716 for (int i = 0; i < count; ++i) {
717 int index = find_by_title(factories.begin(), factories.count(), titles[i].c_str());
718 if (index >= 0) {
719 SkTSwap(factories.begin()[newCount], factories.begin()[index]);
720 newCount += 1;
721 }
722 }
723 if (newCount) {
724 factories.setCount(newCount);
725 }
726}
727
mtkleinfd117d82014-06-19 14:18:56 -0700728DEFINE_string(slide, "", "Start on this sample.");
729DEFINE_int32(msaa, 0, "Request multisampling with this count.");
brianosman2d1ee792016-05-05 12:24:31 -0700730DEFINE_bool(deepColor, false, "Request deep color (10-bit/channel or more) display buffer.");
mtkleinfd117d82014-06-19 14:18:56 -0700731DEFINE_string(pictureDir, "", "Read pictures from here.");
732DEFINE_string(picture, "", "Path to single picture.");
fmalita6ceef3d2016-07-26 18:46:34 -0700733DEFINE_string(svg, "", "Path to single SVG file.");
734DEFINE_string(svgDir, "", "Read SVGs from here.");
reedb7ffe032015-11-05 11:59:17 -0800735DEFINE_string(sequence, "", "Path to file containing the desired samples/gms to show.");
mtkleinfd117d82014-06-19 14:18:56 -0700736DEFINE_bool(sort, false, "Sort samples by title.");
737DEFINE_bool(list, false, "List samples?");
Jim Van Verthb6c5e532016-10-28 10:38:08 -0400738DEFINE_bool(startgpu, false, "Start up with gpu?");
brianosmanca348462016-02-24 11:47:47 -0800739DEFINE_bool(redraw, false, "Force continuous redrawing, for profiling or debugging tools.");
mtkleinfd117d82014-06-19 14:18:56 -0700740#ifdef SAMPLE_PDF_FILE_VIEWER
741DEFINE_string(pdfPath, "", "Path to direcotry of pdf files.");
742#endif
743
reed89889b62014-10-29 12:36:45 -0700744#include "SkTaskGroup.h"
745
chudy@google.com4605a3f2012-08-01 17:58:01 +0000746SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager)
robertphillips@google.com4750fa52012-04-10 13:34:11 +0000747 : INHERITED(hwnd)
halcanary96fcdcc2015-08-27 07:41:13 -0700748 , fDevManager(nullptr) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000749
mtkleinfd117d82014-06-19 14:18:56 -0700750 SkCommandLineFlags::Parse(argc, argv);
751
reed@google.com7d05ac92013-06-06 13:14:13 +0000752 fCurrIndex = -1;
753
mtkleinfd117d82014-06-19 14:18:56 -0700754 if (!FLAGS_pictureDir.isEmpty()) {
755 SkOSFile::Iter iter(FLAGS_pictureDir[0], "skp");
756 SkString filename;
757 while (iter.next(&filename)) {
758 *fSamples.append() = new PictFileFactory(
tfarinaa8e2e152014-07-28 19:26:58 -0700759 SkOSPath::Join(FLAGS_pictureDir[0], filename.c_str()));
mtkleinfd117d82014-06-19 14:18:56 -0700760 }
761 }
762 if (!FLAGS_picture.isEmpty()) {
763 SkString path(FLAGS_picture[0]);
764 fCurrIndex = fSamples.count();
765 *fSamples.append() = new PictFileFactory(path);
766 }
fmalita6ceef3d2016-07-26 18:46:34 -0700767 if (!FLAGS_svg.isEmpty()) {
768 SkString path(FLAGS_svg[0]);
769 fCurrIndex = fSamples.count();
770 *fSamples.append() = new SVGFileFactory(path);
771 }
772 if (!FLAGS_svgDir.isEmpty()) {
773 SkOSFile::Iter iter(FLAGS_svgDir[0], "svg");
774 SkString filename;
775 while (iter.next(&filename)) {
776 *fSamples.append() = new SVGFileFactory(
777 SkOSPath::Join(FLAGS_svgDir[0], filename.c_str()));
778 }
779 }
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000780#ifdef SAMPLE_PDF_FILE_VIEWER
mtkleinfd117d82014-06-19 14:18:56 -0700781 if (!FLAGS_pdfPath.isEmpty()) {
782 SkOSFile::Iter iter(FLAGS_pdfPath[0], "pdf");
783 SkString filename;
784 while (iter.next(&filename)) {
785 *fSamples.append() = new PdfFileViewerFactory(
tfarinaa8e2e152014-07-28 19:26:58 -0700786 SkOSPath::Join(FLAGS_pictureDir[0], filename.c_str()));
mtkleinfd117d82014-06-19 14:18:56 -0700787 }
788 }
789#endif
robertphillips@google.com76d40212012-03-22 14:12:15 +0000790 SkGMRegistyToSampleRegistry();
791 {
792 const SkViewRegister* reg = SkViewRegister::Head();
793 while (reg) {
794 *fSamples.append() = reg->factory();
795 reg = reg->next();
796 }
797 }
798
reedb7ffe032015-11-05 11:59:17 -0800799 if (!FLAGS_sequence.isEmpty()) {
800 // The sequence file just contains a list (separated by CRs) of the samples or GM:gms
801 // you want to restrict to. Only these will appear when you cycle through.
802 // If none are found, or the file is empty, then it will be ignored, and all samples
803 // will be available.
804 SkFILEStream stream(FLAGS_sequence[0]);
805 if (stream.isValid()) {
806 size_t len = stream.getLength();
scroggo565901d2015-12-10 10:44:13 -0800807 SkAutoTMalloc<char> storage(len + 1);
808 char* buffer = storage.get();
reedb7ffe032015-11-05 11:59:17 -0800809 stream.read(buffer, len);
810 buffer[len] = 0;
811
812 SkTArray<SkString> titles;
813 SkStrSplit(buffer, "\n\r", &titles);
814 restrict_samples(fSamples, titles.begin(), titles.count());
815 }
816 }
817
mtkleinfd117d82014-06-19 14:18:56 -0700818 if (FLAGS_sort) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000819 // Sort samples, so foo.skp and foo.pdf are consecutive and we can quickly spot where
820 // skp -> pdf -> png fails.
halcanary96fcdcc2015-08-27 07:41:13 -0700821 SkTQSort(fSamples.begin(), fSamples.end() ? fSamples.end() - 1 : nullptr, compareSampleTitle);
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000822 }
823
mtkleinfd117d82014-06-19 14:18:56 -0700824 if (!FLAGS_slide.isEmpty()) {
825 fCurrIndex = findByTitle(FLAGS_slide[0]);
826 if (fCurrIndex < 0) {
827 fprintf(stderr, "Unknown sample \"%s\"\n", FLAGS_slide[0]);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000828 listTitles();
bsalomon@google.com11959252012-04-06 20:13:38 +0000829 }
mtkleinfd117d82014-06-19 14:18:56 -0700830 }
831
832 fMSAASampleCount = FLAGS_msaa;
brianosman2d1ee792016-05-05 12:24:31 -0700833 fDeepColor = FLAGS_deepColor;
brianosman35a02a82016-09-29 14:37:02 -0700834 fColorConfigIndex = 0;
mtkleinfd117d82014-06-19 14:18:56 -0700835
836 if (FLAGS_list) {
837 listTitles();
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000838 }
839
840 if (fCurrIndex < 0) {
841 SkString title;
842 if (readTitleFromPrefs(&title)) {
843 fCurrIndex = findByTitle(title.c_str());
844 }
845 }
846
847 if (fCurrIndex < 0) {
848 fCurrIndex = 0;
849 }
850
reed89889b62014-10-29 12:36:45 -0700851 static SkTaskGroup::Enabler enabled(-1);
reed@google.com3cec4d72011-07-06 13:59:47 +0000852 gSampleWindow = this;
853
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000854 fDeviceType = kRaster_DeviceType;
jvanverth41518eb2015-10-02 10:42:36 -0700855#if SK_SUPPORT_GPU
Jim Van Verthb6c5e532016-10-28 10:38:08 -0400856 if (FLAGS_startgpu) {
jvanverth44dcb8a2015-10-02 09:12:05 -0700857 fDeviceType = kGPU_DeviceType;
858 }
jvanverth41518eb2015-10-02 10:42:36 -0700859#endif
halcanary9d524f22016-03-29 09:03:52 -0700860
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000861#if DEFAULT_TO_GPU
862 fDeviceType = kGPU_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000863#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000864#if SK_ANGLE && DEFAULT_TO_ANGLE
865 fDeviceType = kANGLE_DeviceType;
866#endif
867
reed@android.com8a1c16f2008-12-17 15:59:43 +0000868 fUseClip = false;
reed91b4dfb2016-01-08 08:23:19 -0800869 fUsePicture = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000870 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000871 fRotate = false;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000872 fPerspAnim = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000873 fRequestGrabImage = false;
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000874 fTilingMode = kNo_Tiling;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000875 fMeasureFPS = false;
reedbabc3de2016-07-08 08:43:27 -0700876 fUseDeferredCanvas = false;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000877 fLCDState = SkOSMenu::kMixedState;
878 fAAState = SkOSMenu::kMixedState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000879 fSubpixelState = SkOSMenu::kMixedState;
880 fHintingState = 0;
bungeman5a59a422016-03-31 08:30:38 -0700881 fPixelGeometryIndex = 0;
reed93a12152015-03-16 10:08:34 -0700882 fFilterQualityIndex = 0;
reed@google.com569e0432011-04-05 13:07:03 +0000883 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000884
Scroggo0f185c22011-03-24 18:35:50 +0000885 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000886 fFatBitsScale = 8;
reed5755cd92016-08-21 19:30:13 -0700887 fTypeface = SkTypeface::MakeFromName("Courier", SkFontStyle(SkFontStyle::kBold_Weight,
888 SkFontStyle::kNormal_Width,
889 SkFontStyle::kUpright_Slant));
Scroggo0f185c22011-03-24 18:35:50 +0000890 fShowZoomer = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000891
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000892 fZoomLevel = 0;
893 fZoomScale = SK_Scalar1;
bungemance560262016-03-03 13:32:38 -0800894 fOffset = { 0, 0 };
chudy@google.com4605a3f2012-08-01 17:58:01 +0000895
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000896 fMagnify = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000897
Scroggo8ac0d542011-06-21 14:44:57 +0000898 fSaveToPdf = false;
reed093b4e82015-08-04 18:46:38 -0700899 fSaveToSKP = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000900
reed54dc4872016-09-13 08:09:45 -0700901 if (true) {
902 fPipeSerializer.setTypefaceSerializer(new SampleTFSerializer);
903 fPipeDeserializer.setTypefaceDeserializer(new SampleTFDeserializer);
904 }
905
yangsu@google.com921091f2011-08-02 13:39:12 +0000906 int sinkID = this->getSinkID();
scroggo@google.com7dadc742012-04-18 14:07:57 +0000907 fAppMenu = new SkOSMenu;
908 fAppMenu->setTitle("Global Settings");
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000909 int itemID;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000910
reeda34be682016-02-15 07:48:35 -0800911 itemID = fAppMenu->appendList("ColorType", "ColorType", sinkID, 0,
brianosman35a02a82016-09-29 14:37:02 -0700912 gConfig[0].fName,
913 gConfig[1].fName,
914 gConfig[2].fName,
915 gConfig[3].fName,
Brian Osmanfc2e7e02016-12-02 12:10:36 -0500916 gConfig[4].fName,
brianosman35a02a82016-09-29 14:37:02 -0700917 nullptr);
reeda34be682016-02-15 07:48:35 -0800918 fAppMenu->assignKeyEquivalentToItem(itemID, 'C');
919
kkinnunen8686a5e2016-01-05 00:04:32 -0800920 itemID = fAppMenu->appendList("Device Type", "Device Type", sinkID, 0,
921 "Raster",
922 "OpenGL",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000923#if SK_ANGLE
kkinnunen8686a5e2016-01-05 00:04:32 -0800924 "ANGLE",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000925#endif
kkinnunen8686a5e2016-01-05 00:04:32 -0800926 nullptr);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000927 fAppMenu->assignKeyEquivalentToItem(itemID, 'd');
928 itemID = fAppMenu->appendTriState("AA", "AA", sinkID, fAAState);
929 fAppMenu->assignKeyEquivalentToItem(itemID, 'b');
930 itemID = fAppMenu->appendTriState("LCD", "LCD", sinkID, fLCDState);
931 fAppMenu->assignKeyEquivalentToItem(itemID, 'l');
reed93a12152015-03-16 10:08:34 -0700932 itemID = fAppMenu->appendList("FilterQuality", "FilterQuality", sinkID, fFilterQualityIndex,
933 gFilterQualityStates[0].fName,
934 gFilterQualityStates[1].fName,
935 gFilterQualityStates[2].fName,
936 gFilterQualityStates[3].fName,
937 gFilterQualityStates[4].fName,
halcanary96fcdcc2015-08-27 07:41:13 -0700938 nullptr);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000939 fAppMenu->assignKeyEquivalentToItem(itemID, 'n');
bungeman@google.com96aabc82013-06-03 21:26:34 +0000940 itemID = fAppMenu->appendTriState("Subpixel", "Subpixel", sinkID, fSubpixelState);
941 fAppMenu->assignKeyEquivalentToItem(itemID, 's');
942 itemID = fAppMenu->appendList("Hinting", "Hinting", sinkID, fHintingState,
943 gHintingStates[0].name,
944 gHintingStates[1].name,
945 gHintingStates[2].name,
946 gHintingStates[3].name,
bungeman@google.coma9133bb2013-06-04 17:49:20 +0000947 gHintingStates[4].name,
halcanary96fcdcc2015-08-27 07:41:13 -0700948 nullptr);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000949 fAppMenu->assignKeyEquivalentToItem(itemID, 'h');
reed@google.com67b89ee2012-08-15 14:41:58 +0000950
bungeman5a59a422016-03-31 08:30:38 -0700951 itemID = fAppMenu->appendList("Pixel Geometry", "Pixel Geometry", sinkID, fPixelGeometryIndex,
952 gPixelGeometryStates[0].name,
953 gPixelGeometryStates[1].name,
954 gPixelGeometryStates[2].name,
955 gPixelGeometryStates[3].name,
956 gPixelGeometryStates[4].name,
957 gPixelGeometryStates[5].name,
958 nullptr);
959 fAppMenu->assignKeyEquivalentToItem(itemID, 'P');
960
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000961 itemID =fAppMenu->appendList("Tiling", "Tiling", sinkID, fTilingMode,
962 gTilingInfo[kNo_Tiling].label,
963 gTilingInfo[kAbs_128x128_Tiling].label,
964 gTilingInfo[kAbs_256x256_Tiling].label,
965 gTilingInfo[kRel_4x4_Tiling].label,
966 gTilingInfo[kRel_1x16_Tiling].label,
967 gTilingInfo[kRel_16x1_Tiling].label,
halcanary96fcdcc2015-08-27 07:41:13 -0700968 nullptr);
reed@google.com67b89ee2012-08-15 14:41:58 +0000969 fAppMenu->assignKeyEquivalentToItem(itemID, 't');
970
chudy@google.com4605a3f2012-08-01 17:58:01 +0000971 itemID = fAppMenu->appendSwitch("Slide Show", "Slide Show" , sinkID, false);
972 fAppMenu->assignKeyEquivalentToItem(itemID, 'a');
973 itemID = fAppMenu->appendSwitch("Clip", "Clip" , sinkID, fUseClip);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000974 fAppMenu->assignKeyEquivalentToItem(itemID, 'c');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000975 itemID = fAppMenu->appendSwitch("Flip X", "Flip X" , sinkID, false);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000976 fAppMenu->assignKeyEquivalentToItem(itemID, 'x');
977 itemID = fAppMenu->appendSwitch("Flip Y", "Flip Y" , sinkID, false);
978 fAppMenu->assignKeyEquivalentToItem(itemID, 'y');
979 itemID = fAppMenu->appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
980 fAppMenu->assignKeyEquivalentToItem(itemID, 'z');
981 itemID = fAppMenu->appendSwitch("Magnify", "Magnify" , sinkID, fMagnify);
982 fAppMenu->assignKeyEquivalentToItem(itemID, 'm');
reed093b4e82015-08-04 18:46:38 -0700983
scroggo@google.com7dadc742012-04-18 14:07:57 +0000984 itemID = fAppMenu->appendAction("Save to PDF", sinkID);
985 fAppMenu->assignKeyEquivalentToItem(itemID, 'e');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000986
scroggo@google.com7dadc742012-04-18 14:07:57 +0000987 this->addMenu(fAppMenu);
988 fSlideMenu = new SkOSMenu;
989 this->addMenu(fSlideMenu);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000990
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000991 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000992 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000993
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000994 this->loadView((*fSamples[fCurrIndex])());
chudy@google.com4605a3f2012-08-01 17:58:01 +0000995
halcanary96fcdcc2015-08-27 07:41:13 -0700996 if (nullptr == devManager) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000997 fDevManager = new DefaultDeviceManager();
998 } else {
999 devManager->ref();
1000 fDevManager = devManager;
1001 }
brianosman2d1ee792016-05-05 12:24:31 -07001002 fDevManager->setUpBackend(this, fMSAASampleCount, fDeepColor);
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001003
Scroggob4490c72011-06-17 13:53:05 +00001004 // If another constructor set our dimensions, ensure that our
1005 // onSizeChange gets called.
1006 if (this->height() && this->width()) {
1007 this->onSizeChange();
1008 }
reed@google.com2072db82011-11-08 15:18:10 +00001009
1010 // can't call this synchronously, since it may require a subclass to
1011 // to implement, or the caller may need us to have returned from the
1012 // constructor first. Hence we post an event to ourselves.
1013// this->updateTitle();
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001014 post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this);
reed76113a92015-02-02 12:55:02 -08001015
1016 gAnimTimer.run();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001017}
1018
1019SampleWindow::~SampleWindow() {
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001020 SkSafeUnref(fDevManager);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001021}
1022
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001023
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001024int SampleWindow::findByTitle(const char title[]) {
1025 int i, count = fSamples.count();
1026 for (i = 0; i < count; i++) {
1027 if (getSampleTitle(i).equals(title)) {
1028 return i;
1029 }
1030 }
mike@reedtribe.orgdd52caa2011-12-28 20:02:10 +00001031 return -1;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001032}
1033
robertphillips@google.com7265e722012-05-03 18:22:28 +00001034void SampleWindow::listTitles() {
1035 int count = fSamples.count();
1036 SkDebugf("All Slides:\n");
1037 for (int i = 0; i < count; i++) {
1038 SkDebugf(" %s\n", getSampleTitle(i).c_str());
1039 }
1040}
1041
reed@android.com55e76b22009-11-23 21:46:47 +00001042static SkBitmap capture_bitmap(SkCanvas* canvas) {
1043 SkBitmap bm;
reed84825042014-09-02 12:50:45 -07001044 if (bm.tryAllocPixels(canvas->imageInfo())) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001045 canvas->readPixels(&bm, 0, 0);
1046 }
reed@android.com55e76b22009-11-23 21:46:47 +00001047 return bm;
1048}
1049
reedf7f79d22015-02-21 09:03:20 -08001050static void drawText(SkCanvas* canvas, SkString str, SkScalar left, SkScalar top, SkPaint& paint) {
Scroggo0f185c22011-03-24 18:35:50 +00001051 SkColor desiredColor = paint.getColor();
1052 paint.setColor(SK_ColorWHITE);
reedf7f79d22015-02-21 09:03:20 -08001053 const char* c_str = str.c_str();
1054 size_t size = str.size();
Scroggo0f185c22011-03-24 18:35:50 +00001055 SkRect bounds;
1056 paint.measureText(c_str, size, &bounds);
1057 bounds.offset(left, top);
1058 SkScalar inset = SkIntToScalar(-2);
1059 bounds.inset(inset, inset);
1060 canvas->drawRect(bounds, paint);
Scroggo0f185c22011-03-24 18:35:50 +00001061 paint.setColor(desiredColor);
1062 canvas->drawText(c_str, size, left, top, paint);
1063}
1064
reed@android.com44177402009-11-23 21:07:51 +00001065#define XCLIP_N 8
1066#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +00001067
reedbabc3de2016-07-08 08:43:27 -07001068#include "SkDeferredCanvas.h"
1069#include "SkDumpCanvas.h"
1070
reed@android.come522ca52009-11-23 20:10:41 +00001071void SampleWindow::draw(SkCanvas* canvas) {
reed76113a92015-02-02 12:55:02 -08001072 gAnimTimer.updateTime();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001073
reed@google.comf03bb562011-11-11 21:42:12 +00001074 if (fGesture.isActive()) {
1075 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001076 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001077
djsollen@google.com796763e2012-12-10 14:12:55 +00001078 if (fMeasureFPS) {
1079 fMeasureFPS_Time = 0;
1080 }
1081
reedf7f79d22015-02-21 09:03:20 -08001082 SkSize tile = this->tileSize();
reed@android.come522ca52009-11-23 20:10:41 +00001083
reedf7f79d22015-02-21 09:03:20 -08001084 if (kNo_Tiling == fTilingMode) {
reedbabc3de2016-07-08 08:43:27 -07001085 SkDebugfDumper dumper;
1086 SkDumpCanvas dump(&dumper);
1087 SkDeferredCanvas deferred(canvas);
1088 SkCanvas* c = fUseDeferredCanvas ? &deferred : canvas;
1089 this->INHERITED::draw(c); // no looping or surfaces needed
reed@android.come522ca52009-11-23 20:10:41 +00001090 } else {
bungemance560262016-03-03 13:32:38 -08001091 const SkScalar w = SkScalarCeilToScalar(tile.width());
1092 const SkScalar h = SkScalarCeilToScalar(tile.height());
1093 SkImageInfo info = SkImageInfo::MakeN32Premul(SkScalarTruncToInt(w), SkScalarTruncToInt(h));
reede8f30622016-03-23 18:59:25 -07001094 auto surface(canvas->makeSurface(info));
reedf7f79d22015-02-21 09:03:20 -08001095 SkCanvas* tileCanvas = surface->getCanvas();
rmistry@google.comae933ce2012-08-23 18:19:56 +00001096
bungemance560262016-03-03 13:32:38 -08001097 for (SkScalar y = 0; y < height(); y += h) {
1098 for (SkScalar x = 0; x < width(); x += w) {
reedf7f79d22015-02-21 09:03:20 -08001099 SkAutoCanvasRestore acr(tileCanvas, true);
1100 tileCanvas->translate(-x, -y);
1101 tileCanvas->clear(0);
1102 this->INHERITED::draw(tileCanvas);
halcanary96fcdcc2015-08-27 07:41:13 -07001103 surface->draw(canvas, x, y, nullptr);
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +00001104 }
reedf7f79d22015-02-21 09:03:20 -08001105 }
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +00001106
reedf7f79d22015-02-21 09:03:20 -08001107 // for drawing the borders between tiles
1108 SkPaint paint;
1109 paint.setColor(0x60FF00FF);
1110 paint.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001111
reedf7f79d22015-02-21 09:03:20 -08001112 for (SkScalar y = 0; y < height(); y += tile.height()) {
1113 for (SkScalar x = 0; x < width(); x += tile.width()) {
1114 canvas->drawRect(SkRect::MakeXYWH(x, y, tile.width(), tile.height()), paint);
scroggo@google.com85cade02012-08-17 13:29:50 +00001115 }
1116 }
reed@android.come522ca52009-11-23 20:10:41 +00001117 }
reedf7f79d22015-02-21 09:03:20 -08001118
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001119 if (fShowZoomer && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +00001120 showZoomer(canvas);
1121 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001122 if (fMagnify && !fSaveToPdf) {
1123 magnify(canvas);
1124 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001125
djsollen@google.com796763e2012-12-10 14:12:55 +00001126 if (fMeasureFPS && fMeasureFPS_Time) {
1127 this->updateTitle();
1128 this->postInvalDelay();
1129 }
1130
brianosmanca348462016-02-24 11:47:47 -08001131 if (this->sendAnimatePulse() || FLAGS_redraw) {
halcanary96fcdcc2015-08-27 07:41:13 -07001132 this->inval(nullptr);
reedd9adfe62015-02-01 19:01:04 -08001133 }
1134
reed@google.com29038ed2011-07-06 17:56:47 +00001135 // do this last
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001136 fDevManager->publishCanvas(fDeviceType, canvas, this);
Scroggo3272ba82011-05-25 20:50:42 +00001137}
1138
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001139static float clipW = 200;
1140static float clipH = 200;
1141void SampleWindow::magnify(SkCanvas* canvas) {
1142 SkRect r;
1143 int count = canvas->save();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001144
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001145 SkMatrix m = canvas->getTotalMatrix();
robertphillips@google.com07ef9112012-06-04 13:22:14 +00001146 if (!m.invert(&m)) {
1147 return;
1148 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001149 SkPoint offset, center;
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001150 SkScalar mouseX = fMouseX * SK_Scalar1;
1151 SkScalar mouseY = fMouseY * SK_Scalar1;
1152 m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset);
1153 m.mapXY(mouseX, mouseY, &center);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001154
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001155 r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX());
1156 r.offset(offset.fX, offset.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001157
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001158 SkPaint paint;
1159 paint.setColor(0xFF66AAEE);
1160 paint.setStyle(SkPaint::kStroke_Style);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001161 paint.setStrokeWidth(10.f * m.getScaleX());
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001162 //lense offset
1163 //canvas->translate(0, -250);
1164 canvas->drawRect(r, paint);
1165 canvas->clipRect(r);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001166
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001167 m = canvas->getTotalMatrix();
1168 m.setTranslate(-center.fX, -center.fY);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001169 m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001170 m.postTranslate(center.fX, center.fY);
1171 canvas->concat(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001172
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001173 this->INHERITED::draw(canvas);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001174
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001175 canvas->restoreToCount(count);
1176}
1177
reed5f629b42015-08-15 11:57:23 -07001178static SkPaint& set_color_ref(SkPaint& paint, SkColor c) {
1179 paint.setColor(c);
1180 return paint;
1181}
reed@google.comb36334d2011-05-18 15:07:20 +00001182
reed5f629b42015-08-15 11:57:23 -07001183static void show_lcd_box(SkCanvas* canvas, SkScalar x, SkScalar y, SkColor c,
1184 SkScalar sx, SkScalar sy) {
1185 const SkScalar w = (1 - 1/sx) / 3;
1186 SkPaint paint;
1187 SkRect r = SkRect::MakeXYWH(x, y, w, 1 - 1/sy);
1188 canvas->drawRect(r, set_color_ref(paint, SkColorSetRGB(SkColorGetR(c), 0, 0)));
1189 r.offset(w, 0);
1190 canvas->drawRect(r, set_color_ref(paint, SkColorSetRGB(0, SkColorGetG(c), 0)));
1191 r.offset(w, 0);
1192 canvas->drawRect(r, set_color_ref(paint, SkColorSetRGB(0, 0, SkColorGetB(c))));
1193}
reed@google.comb36334d2011-05-18 15:07:20 +00001194
reed5f629b42015-08-15 11:57:23 -07001195static void show_lcd_circle(SkCanvas* canvas, SkScalar x, SkScalar y, SkColor c,
1196 SkScalar, SkScalar) {
1197 const SkRect r = SkRect::MakeXYWH(x, y, 1, 1);
1198 const SkScalar cx = x + 0.5f;
1199 const SkScalar cy = y + 0.5f;
1200
1201 SkPaint paint;
1202 paint.setAntiAlias(true);
1203
1204 SkPath path;
1205 path.addArc(r, 0, 120); path.lineTo(cx, cy);
1206 canvas->drawPath(path, set_color_ref(paint, SkColorSetRGB(SkColorGetR(c), 0, 0)));
1207
1208 path.reset(); path.addArc(r, 120, 120); path.lineTo(cx, cy);
1209 canvas->drawPath(path, set_color_ref(paint, SkColorSetRGB(0, SkColorGetG(c), 0)));
1210
1211 path.reset(); path.addArc(r, 240, 120); path.lineTo(cx, cy);
1212 canvas->drawPath(path, set_color_ref(paint, SkColorSetRGB(0, 0, SkColorGetB(c))));
1213}
1214
1215typedef void (*ShowLCDProc)(SkCanvas*, SkScalar, SkScalar, SkColor, SkScalar, SkScalar);
1216
1217/*
1218 * Like drawBitmapRect but we manually draw each pixels in RGB
1219 */
1220static void show_lcd_grid(SkCanvas* canvas, const SkBitmap& bitmap,
reed8a41adb2015-08-15 14:02:29 -07001221 const SkIRect& origSrc, const SkRect& dst, ShowLCDProc proc) {
reed5f629b42015-08-15 11:57:23 -07001222 SkIRect src;
1223 if (!src.intersect(origSrc, bitmap.bounds())) {
1224 return;
1225 }
1226 const SkScalar sx = dst.width() / src.width();
1227 const SkScalar sy = dst.height() / src.height();
1228
1229 SkAutoCanvasRestore acr(canvas, true);
1230 canvas->translate(dst.left(), dst.top());
1231 canvas->scale(sx, sy);
1232
reed5f629b42015-08-15 11:57:23 -07001233 for (int y = 0; y < src.height(); ++y) {
1234 for (int x = 0; x < src.width(); ++x) {
1235 proc(canvas, SkIntToScalar(x), SkIntToScalar(y),
1236 bitmap.getColor(src.left() + x, src.top() + y), sx, sy);
Scroggo0f185c22011-03-24 18:35:50 +00001237 }
reed5f629b42015-08-15 11:57:23 -07001238 }
1239}
1240
1241void SampleWindow::showZoomer(SkCanvas* canvas) {
1242 int count = canvas->save();
1243 canvas->resetMatrix();
1244 // Ensure the mouse position is on screen.
1245 int width = SkScalarRoundToInt(this->width());
1246 int height = SkScalarRoundToInt(this->height());
1247 if (fMouseX >= width) fMouseX = width - 1;
1248 else if (fMouseX < 0) fMouseX = 0;
1249 if (fMouseY >= height) fMouseY = height - 1;
1250 else if (fMouseY < 0) fMouseY = 0;
1251
1252 SkBitmap bitmap = capture_bitmap(canvas);
1253 bitmap.lockPixels();
1254
1255 // Find the size of the zoomed in view, forced to be odd, so the examined pixel is in the middle.
1256 int zoomedWidth = (width >> 1) | 1;
1257 int zoomedHeight = (height >> 1) | 1;
1258 SkIRect src;
1259 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
1260 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
1261 SkRect dest;
1262 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
1263 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
1264 SkPaint paint;
1265 // Clear the background behind our zoomed in view
1266 paint.setColor(SK_ColorWHITE);
1267 canvas->drawRect(dest, paint);
reed8a41adb2015-08-15 14:02:29 -07001268 switch (fFatBitsScale) {
1269 case kMaxFatBitsScale:
1270 show_lcd_grid(canvas, bitmap, src, dest, show_lcd_box);
1271 break;
1272 case kMaxFatBitsScale - 1:
1273 show_lcd_grid(canvas, bitmap, src, dest, show_lcd_circle);
1274 break;
1275 default:
halcanary96fcdcc2015-08-27 07:41:13 -07001276 canvas->drawBitmapRect(bitmap, src, dest, nullptr);
reed8a41adb2015-08-15 14:02:29 -07001277 break;
reed5f629b42015-08-15 11:57:23 -07001278 }
1279
1280 paint.setColor(SK_ColorBLACK);
1281 paint.setStyle(SkPaint::kStroke_Style);
1282 // Draw a border around the pixel in the middle
1283 SkRect originalPixel;
1284 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
1285 SkMatrix matrix;
1286 SkRect scalarSrc;
1287 scalarSrc.set(src);
1288 SkColor color = bitmap.getColor(fMouseX, fMouseY);
1289 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
1290 SkRect pixel;
1291 matrix.mapRect(&pixel, originalPixel);
1292 // TODO Perhaps measure the values and make the outline white if it's "dark"
1293 if (color == SK_ColorBLACK) {
1294 paint.setColor(SK_ColorWHITE);
1295 }
1296 canvas->drawRect(pixel, paint);
1297 }
1298 paint.setColor(SK_ColorBLACK);
1299 // Draw a border around the destination rectangle
1300 canvas->drawRect(dest, paint);
1301 paint.setStyle(SkPaint::kStrokeAndFill_Style);
1302 // Identify the pixel and its color on screen
1303 paint.setTypeface(fTypeface);
1304 paint.setAntiAlias(true);
reed5755cd92016-08-21 19:30:13 -07001305 paint.setTextSize(18);
halcanary96fcdcc2015-08-27 07:41:13 -07001306 SkScalar lineHeight = paint.getFontMetrics(nullptr);
reed5f629b42015-08-15 11:57:23 -07001307 SkString string;
1308 string.appendf("(%i, %i)", fMouseX, fMouseY);
1309 SkScalar left = dest.fLeft + SkIntToScalar(3);
1310 SkScalar i = SK_Scalar1;
1311 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1312 // Alpha
1313 i += SK_Scalar1;
1314 string.reset();
1315 string.appendf("A: %X", SkColorGetA(color));
1316 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1317 // Red
1318 i += SK_Scalar1;
1319 string.reset();
1320 string.appendf("R: %X", SkColorGetR(color));
1321 paint.setColor(SK_ColorRED);
1322 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1323 // Green
1324 i += SK_Scalar1;
1325 string.reset();
1326 string.appendf("G: %X", SkColorGetG(color));
reed5755cd92016-08-21 19:30:13 -07001327 paint.setColor(0xFF008800);
reed5f629b42015-08-15 11:57:23 -07001328 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1329 // Blue
1330 i += SK_Scalar1;
1331 string.reset();
1332 string.appendf("B: %X", SkColorGetB(color));
1333 paint.setColor(SK_ColorBLUE);
1334 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1335 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +00001336}
1337
reed@android.com8a1c16f2008-12-17 15:59:43 +00001338void SampleWindow::onDraw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001339}
1340
1341#include "SkColorPriv.h"
1342
Scroggo8ac0d542011-06-21 14:44:57 +00001343void SampleWindow::saveToPdf()
1344{
1345 fSaveToPdf = true;
halcanary96fcdcc2015-08-27 07:41:13 -07001346 this->inval(nullptr);
Scroggo8ac0d542011-06-21 14:44:57 +00001347}
1348
reed@android.com8a1c16f2008-12-17 15:59:43 +00001349SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +00001350 if (fSaveToPdf) {
reedddb5eca2014-10-08 11:10:51 -07001351 SkString name;
1352 if (!this->getRawTitle(&name)) {
1353 name.set("unknown_sample");
1354 }
1355 name.append(".pdf");
1356#ifdef SK_BUILD_FOR_ANDROID
1357 name.prepend("/sdcard/");
1358#endif
halcanary676ab682016-05-03 12:10:04 -07001359 fPDFDocument = SkDocument::MakePDF(name.c_str());
reedddb5eca2014-10-08 11:10:51 -07001360 canvas = fPDFDocument->beginPage(this->width(), this->height());
kkinnunen8686a5e2016-01-05 00:04:32 -08001361 } else if (fSaveToSKP) {
1362 canvas = fRecorder.beginRecording(9999, 9999, nullptr, 0);
reed91b4dfb2016-01-08 08:23:19 -08001363 } else if (fUsePicture) {
reed54dc4872016-09-13 08:09:45 -07001364 fPipeStream.reset(new SkDynamicMemoryWStream);
1365 canvas = fPipeSerializer.beginWrite(SkRect::MakeWH(this->width(), this->height()),
1366 fPipeStream.get());
1367// canvas = fRecorder.beginRecording(9999, 9999, nullptr, 0);
Scroggo8ac0d542011-06-21 14:44:57 +00001368 } else {
bsalomonbb0502e2015-02-11 11:11:11 -08001369 canvas = this->INHERITED::beforeChildren(canvas);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001370 }
1371
1372 if (fUseClip) {
1373 canvas->drawColor(0xFFFF88FF);
Mike Reedc1f77742016-12-09 09:00:50 -05001374 canvas->clipPath(fClipPath, kIntersect_SkClipOp, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001375 }
1376
fmalita822ace92015-06-15 07:07:32 -07001377 // Install a flags filter proxy canvas if needed
1378 if (fLCDState != SkOSMenu::kMixedState ||
1379 fAAState != SkOSMenu::kMixedState ||
1380 fSubpixelState != SkOSMenu::kMixedState ||
1381 fHintingState > 0 ||
1382 fFilterQualityIndex > 0) {
halcanary385fe4d2015-08-26 13:07:48 -07001383 canvas = new FlagsFilterCanvas(canvas, fLCDState, fAAState, fSubpixelState, fHintingState,
1384 fFilterQualityIndex);
fmalita822ace92015-06-15 07:07:32 -07001385 fFlagsFilterCanvas.reset(canvas);
1386 }
1387
reed@android.com8a1c16f2008-12-17 15:59:43 +00001388 return canvas;
1389}
reed89889b62014-10-29 12:36:45 -07001390#include "SkMultiPictureDraw.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001391void SampleWindow::afterChildren(SkCanvas* orig) {
halcanary96fcdcc2015-08-27 07:41:13 -07001392 fFlagsFilterCanvas.reset(nullptr);
fmalita822ace92015-06-15 07:07:32 -07001393
Scroggo8ac0d542011-06-21 14:44:57 +00001394 if (fSaveToPdf) {
1395 fSaveToPdf = false;
reedddb5eca2014-10-08 11:10:51 -07001396 fPDFDocument->endPage();
halcanary96fcdcc2015-08-27 07:41:13 -07001397 fPDFDocument.reset(nullptr);
Scroggo8ac0d542011-06-21 14:44:57 +00001398 // We took over the draw calls in order to create the PDF, so we need
1399 // to redraw.
halcanary96fcdcc2015-08-27 07:41:13 -07001400 this->inval(nullptr);
Scroggo8ac0d542011-06-21 14:44:57 +00001401 return;
1402 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001403
reed@android.comf2b98d62010-12-20 18:26:13 +00001404 if (fRequestGrabImage) {
1405 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001406
reed52d9ac62014-06-30 09:05:34 -07001407 SkBitmap bmp = capture_bitmap(orig);
1408 if (!bmp.isNull()) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001409 static int gSampleGrabCounter;
1410 SkString name;
reed@google.com7c57e0e2012-09-08 21:20:33 +00001411 name.printf("sample_grab_%d.png", gSampleGrabCounter++);
Hal Canarydb683012016-11-23 08:55:18 -07001412 sk_tool_utils::EncodeImageToFile(name.c_str(), bmp,
1413 SkEncodedImageFormat::kPNG, 100);
reed@android.comf2b98d62010-12-20 18:26:13 +00001414 }
kkinnunen8686a5e2016-01-05 00:04:32 -08001415 this->inval(nullptr);
1416 return;
reed@android.comf2b98d62010-12-20 18:26:13 +00001417 }
1418
kkinnunen8686a5e2016-01-05 00:04:32 -08001419 if (fSaveToSKP) {
reedca2622b2016-03-18 07:25:55 -07001420 sk_sp<SkPicture> picture(fRecorder.finishRecordingAsPicture());
kkinnunen8686a5e2016-01-05 00:04:32 -08001421 SkFILEWStream stream("sample_app.skp");
1422 picture->serialize(&stream);
1423 fSaveToSKP = false;
1424 this->inval(nullptr);
1425 return;
1426 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001427
reed91b4dfb2016-01-08 08:23:19 -08001428 if (fUsePicture) {
reed54dc4872016-09-13 08:09:45 -07001429 if (true) {
1430 fPipeSerializer.endWrite();
1431 sk_sp<SkData> data(fPipeStream->detachAsData());
1432 fPipeDeserializer.playback(data->data(), data->size(), orig);
1433 fPipeStream.reset();
1434 } else {
1435 sk_sp<SkPicture> picture(fRecorder.finishRecordingAsPicture());
1436 orig->drawPicture(picture.get());
reedbfd5f172016-01-07 11:28:08 -08001437 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001438 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001439
reed@google.com17d7aec2011-04-25 14:31:44 +00001440 // Do this after presentGL and other finishing, rather than in afterChild
jvanverth4d0d81a2015-08-11 11:07:07 -07001441 if (fMeasureFPS) {
reedb2ce0b02015-11-09 13:10:30 -08001442 orig->flush();
jvanverth4d0d81a2015-08-11 11:07:07 -07001443 fTimer.end();
1444 fMeasureFPS_Time += fTimer.fWall;
reed@google.com17d7aec2011-04-25 14:31:44 +00001445 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001446}
1447
reed@android.com6c5f6f22009-08-14 16:08:38 +00001448void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
reed@android.com6c5f6f22009-08-14 16:08:38 +00001449 if (fRotate) {
1450 SkScalar cx = this->width() / 2;
1451 SkScalar cy = this->height() / 2;
bungeman7438bfc2016-07-12 15:01:19 -07001452 canvas->rotate(gAnimTimer.scaled(10), cx, cy);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001453 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001454
bsalomon@google.come8f09102011-09-08 18:48:12 +00001455 if (fPerspAnim) {
reed76113a92015-02-02 12:55:02 -08001456 SkScalar secs = gAnimTimer.scaled(1);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001457
1458 static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
1459 static const SkScalar gAnimMag = SK_Scalar1 / 1000;
reed76113a92015-02-02 12:55:02 -08001460 SkScalar t = SkScalarMod(secs, gAnimPeriod);
reed80ea19c2015-05-12 10:37:34 -07001461 if (SkScalarFloorToInt(secs / gAnimPeriod) & 0x1) {
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001462 t = gAnimPeriod - t;
1463 }
1464 t = 2 * t - gAnimPeriod;
reed80ea19c2015-05-12 10:37:34 -07001465 t *= gAnimMag / gAnimPeriod;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001466 SkMatrix m;
1467 m.reset();
reed5d9ab282014-09-19 13:04:17 -07001468#if 1
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001469 m.setPerspY(t);
reed5d9ab282014-09-19 13:04:17 -07001470#else
1471 m.setPerspY(SK_Scalar1 / 1000);
reed80ea19c2015-05-12 10:37:34 -07001472 m.setSkewX(8.0f / 25);
reed5d9ab282014-09-19 13:04:17 -07001473 m.dump();
1474#endif
bsalomon@google.come8f09102011-09-08 18:48:12 +00001475 canvas->concat(m);
1476 }
reed@google.comf0b5f682011-03-11 20:08:25 +00001477
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001478 if (fMeasureFPS) {
jvanverth4d0d81a2015-08-11 11:07:07 -07001479 (void)SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT);
1480 fTimer.start();
reed@google.comf2183392011-04-22 14:10:48 +00001481 } else {
1482 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001483 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001484 if (fPerspAnim || fRotate) {
halcanary96fcdcc2015-08-27 07:41:13 -07001485 this->inval(nullptr);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001486 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001487}
1488
bungemance560262016-03-03 13:32:38 -08001489void SampleWindow::changeOffset(SkVector delta) {
1490 fOffset += delta;
1491 this->updateMatrix();
1492}
1493
djsollen@google.come32b5832011-06-13 16:58:40 +00001494void SampleWindow::changeZoomLevel(float delta) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001495 fZoomLevel += delta;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001496 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001497 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
1498 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001499 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001500 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
1501 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001502 } else {
1503 fZoomScale = SK_Scalar1;
1504 }
reed@google.comf03bb562011-11-11 21:42:12 +00001505 this->updateMatrix();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001506}
1507
reed@google.comf03bb562011-11-11 21:42:12 +00001508void SampleWindow::updateMatrix(){
1509 SkMatrix m;
1510 m.reset();
bungemance560262016-03-03 13:32:38 -08001511
reed@google.comf03bb562011-11-11 21:42:12 +00001512 if (fZoomLevel) {
1513 SkPoint center;
1514 //m = this->getLocalMatrix();//.invert(&m);
1515 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
1516 SkScalar cx = center.fX;
1517 SkScalar cy = center.fY;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001518
reed@google.comf03bb562011-11-11 21:42:12 +00001519 m.setTranslate(-cx, -cy);
1520 m.postScale(fZoomScale, fZoomScale);
1521 m.postTranslate(cx, cy);
1522 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001523
bungemance560262016-03-03 13:32:38 -08001524 m.postTranslate(fOffset.fX, fOffset.fY);
1525
reed@google.comf03bb562011-11-11 21:42:12 +00001526 if (fFlipAxis) {
1527 m.preTranslate(fZoomCenterX, fZoomCenterY);
1528 if (fFlipAxis & kFlipAxis_X) {
1529 m.preScale(-SK_Scalar1, SK_Scalar1);
1530 }
1531 if (fFlipAxis & kFlipAxis_Y) {
1532 m.preScale(SK_Scalar1, -SK_Scalar1);
1533 }
1534 m.preTranslate(-fZoomCenterX, -fZoomCenterY);
1535 //canvas->concat(m);
1536 }
1537 // Apply any gesture matrix
1538 m.preConcat(fGesture.localM());
1539 m.preConcat(fGesture.globalM());
chudy@google.com4605a3f2012-08-01 17:58:01 +00001540
reed@google.comf03bb562011-11-11 21:42:12 +00001541 this->setLocalMatrix(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001542
reed@google.comf03bb562011-11-11 21:42:12 +00001543 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001544 this->inval(nullptr);
reed@google.comf03bb562011-11-11 21:42:12 +00001545}
Scroggo2c8208f2011-06-15 16:49:08 +00001546bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +00001547 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
reed63121f02015-01-13 19:43:04 -08001548 this->loadView((*fSamples[fCurrIndex])());
Scroggo2c8208f2011-06-15 16:49:08 +00001549 return true;
1550}
1551
reed216b6432015-08-19 12:25:40 -07001552#include "SkResourceCache.h"
1553#include "SkGlyphCache.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001554bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +00001555 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
reed63121f02015-01-13 19:43:04 -08001556 this->loadView((*fSamples[fCurrIndex])());
reed216b6432015-08-19 12:25:40 -07001557
1558 if (false) {
1559 SkResourceCache::TestDumpMemoryStatistics();
1560 SkGlyphCache::Dump();
1561 SkDebugf("\n");
1562 }
1563
reed@android.com34245c72009-11-03 04:00:48 +00001564 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001565}
1566
yangsu@google.com501775e2011-06-24 16:04:50 +00001567bool SampleWindow::goToSample(int i) {
1568 fCurrIndex = (i) % fSamples.count();
reed63121f02015-01-13 19:43:04 -08001569 this->loadView((*fSamples[fCurrIndex])());
yangsu@google.com501775e2011-06-24 16:04:50 +00001570 return true;
1571}
1572
1573SkString SampleWindow::getSampleTitle(int i) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001574 return ::getSampleTitle(fSamples[i]);
yangsu@google.com501775e2011-06-24 16:04:50 +00001575}
1576
1577int SampleWindow::sampleCount() {
1578 return fSamples.count();
1579}
1580
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001581void SampleWindow::showOverview() {
reed63121f02015-01-13 19:43:04 -08001582 this->loadView(create_overview(fSamples.count(), fSamples.begin()));
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001583}
1584
Scroggo2c8208f2011-06-15 16:49:08 +00001585void SampleWindow::postAnimatingEvent() {
1586 if (fAnimating) {
reed@google.com87fac4a2011-08-04 13:50:17 +00001587 (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
Scroggo2c8208f2011-06-15 16:49:08 +00001588 }
1589}
reed@google.come23f1942011-12-08 19:36:00 +00001590
brianosman35a02a82016-09-29 14:37:02 -07001591static sk_sp<SkColorSpace> getMonitorColorSpace() {
1592#if defined(SK_BUILD_FOR_MAC)
1593 CGColorSpaceRef cs = CGDisplayCopyColorSpace(CGMainDisplayID());
1594 CFDataRef dataRef = CGColorSpaceCopyICCProfile(cs);
1595 const uint8_t* data = CFDataGetBytePtr(dataRef);
1596 size_t size = CFDataGetLength(dataRef);
1597
Brian Osman526972e2016-10-24 09:24:02 -04001598 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(data, size);
brianosman35a02a82016-09-29 14:37:02 -07001599
1600 CFRelease(cs);
1601 CFRelease(dataRef);
1602 return colorSpace;
1603#elif defined(SK_BUILD_FOR_WIN)
1604 DISPLAY_DEVICE dd = { sizeof(DISPLAY_DEVICE) };
1605
1606 // Chrome's code for this currently just gets the primary monitor's profile. This code iterates
1607 // over all attached monitors, so it's "better" in that sense. Making intelligent use of this
1608 // information (via things like MonitorFromWindow or MonitorFromRect to pick the correct
1609 // profile for a particular window or region of a window), is an exercise left to the reader.
1610 for (int i = 0; EnumDisplayDevices(NULL, i, &dd, 0); ++i) {
1611 if (dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) {
1612 // There are other helpful things in dd at this point:
1613 // dd.DeviceString has a longer name for the adapter
1614 // dd.StateFlags indicates primary display, mirroring, etc...
1615 HDC dc = CreateDC(NULL, dd.DeviceName, NULL, NULL);
1616 if (dc) {
1617 char icmPath[MAX_PATH + 1];
1618 DWORD pathLength = MAX_PATH;
Jim Van Verthb6c5e532016-10-28 10:38:08 -04001619 BOOL success = GetICMProfileA(dc, &pathLength, icmPath);
brianosman35a02a82016-09-29 14:37:02 -07001620 DeleteDC(dc);
1621 if (success) {
1622 sk_sp<SkData> iccData = SkData::MakeFromFileName(icmPath);
Brian Osman526972e2016-10-24 09:24:02 -04001623 return SkColorSpace::MakeICC(iccData->data(), iccData->size());
brianosman35a02a82016-09-29 14:37:02 -07001624 }
1625 }
1626 }
1627 }
1628
1629 return nullptr;
1630#else
1631 return nullptr;
1632#endif
1633}
1634
reed@android.com8a1c16f2008-12-17 15:59:43 +00001635bool SampleWindow::onEvent(const SkEvent& evt) {
reed@google.com2072db82011-11-08 15:18:10 +00001636 if (evt.isType(gUpdateWindowTitleEvtName)) {
1637 this->updateTitle();
1638 return true;
1639 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001640 if (evt.isType(ANIMATING_EVENTTYPE)) {
1641 if (fAnimating) {
1642 this->nextSample();
1643 this->postAnimatingEvent();
1644 }
1645 return true;
1646 }
reed@android.com34245c72009-11-03 04:00:48 +00001647 if (evt.isType("set-curr-index")) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001648 this->goToSample(evt.getFast32());
reed@android.com34245c72009-11-03 04:00:48 +00001649 return true;
1650 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001651 if (isInvalEvent(evt)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001652 this->inval(nullptr);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001653 return true;
1654 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001655 int selected = -1;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001656 if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001657 this->setDeviceType((DeviceType)selected);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001658 return true;
yangsu@google.com921091f2011-08-02 13:39:12 +00001659 }
reeda34be682016-02-15 07:48:35 -08001660 if (SkOSMenu::FindListIndex(evt, "ColorType", &selected)) {
brianosman35a02a82016-09-29 14:37:02 -07001661 fColorConfigIndex = selected;
1662 sk_sp<SkColorSpace> colorSpace = nullptr;
1663 switch (gConfig[selected].fColorSpace) {
1664 case kSRGB_OutputColorSpace:
Brian Osman526972e2016-10-24 09:24:02 -04001665 colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
brianosman35a02a82016-09-29 14:37:02 -07001666 break;
Brian Osmanfc2e7e02016-12-02 12:10:36 -05001667 case kNarrow_OutputColorSpace:
1668 {
1669 // NarrowGamut RGB (an artifically smaller than sRGB gamut)
1670 SkColorSpacePrimaries primaries ={
1671 0.54f, 0.33f, // Rx, Ry
1672 0.33f, 0.50f, // Gx, Gy
1673 0.25f, 0.20f, // Bx, By
1674 0.3127f, 0.3290f, // Wx, Wy
1675 };
1676 SkMatrix44 narrowGamutRGBMatrix(SkMatrix44::kUninitialized_Constructor);
1677 primaries.toXYZD50(&narrowGamutRGBMatrix);
1678 colorSpace = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
1679 narrowGamutRGBMatrix);
1680 }
1681 break;
brianosman35a02a82016-09-29 14:37:02 -07001682 case kMonitor_OutputColorSpace:
1683 colorSpace = getMonitorColorSpace();
1684 if (!colorSpace) {
1685 // Fallback for platforms / machines where we can't get a monitor profile
Brian Osman526972e2016-10-24 09:24:02 -04001686 colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
brianosman35a02a82016-09-29 14:37:02 -07001687 }
1688 break;
1689 case kLegacy_OutputColorSpace:
1690 default:
1691 // Do nothing
1692 break;
1693 }
brianosman0e22eb82016-08-30 07:07:59 -07001694 if (kRGBA_F16_SkColorType == gConfig[selected].fColorType) {
brianosman35a02a82016-09-29 14:37:02 -07001695 SkASSERT(colorSpace);
raftias94888332016-10-18 10:02:51 -07001696 SkASSERT(SkColorSpace_Base::Type::kXYZ == as_CSB(colorSpace)->type());
1697 SkColorSpace_XYZ* csXYZ = static_cast<SkColorSpace_XYZ*>(colorSpace.get());
1698 colorSpace = csXYZ->makeLinearGamma();
brianosman0e22eb82016-08-30 07:07:59 -07001699 }
brianosman35a02a82016-09-29 14:37:02 -07001700 this->setDeviceColorType(gConfig[selected].fColorType, colorSpace);
reeda34be682016-02-15 07:48:35 -08001701 return true;
1702 }
halcanary96fcdcc2015-08-27 07:41:13 -07001703 if (SkOSMenu::FindSwitchState(evt, "Slide Show", nullptr)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001704 this->toggleSlideshow();
1705 return true;
1706 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001707 if (SkOSMenu::FindTriState(evt, "AA", &fAAState) ||
1708 SkOSMenu::FindTriState(evt, "LCD", &fLCDState) ||
reed93a12152015-03-16 10:08:34 -07001709 SkOSMenu::FindListIndex(evt, "FilterQuality", &fFilterQualityIndex) ||
bungeman@google.com96aabc82013-06-03 21:26:34 +00001710 SkOSMenu::FindTriState(evt, "Subpixel", &fSubpixelState) ||
1711 SkOSMenu::FindListIndex(evt, "Hinting", &fHintingState) ||
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001712 SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) ||
1713 SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
reeda84d6602016-06-24 18:14:07 -07001714 SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify))
reed093b4e82015-08-04 18:46:38 -07001715 {
halcanary96fcdcc2015-08-27 07:41:13 -07001716 this->inval(nullptr);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00001717 this->updateTitle();
1718 return true;
1719 }
bungeman5a59a422016-03-31 08:30:38 -07001720 if (SkOSMenu::FindListIndex(evt, "Pixel Geometry", &fPixelGeometryIndex)) {
1721 this->setPixelGeometry(fPixelGeometryIndex);
1722 return true;
1723 }
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00001724 if (SkOSMenu::FindListIndex(evt, "Tiling", &fTilingMode)) {
1725 if (SampleView::IsSampleView(curr_view(this))) {
1726 ((SampleView*)curr_view(this))->onTileSizeChanged(this->tileSize());
1727 }
halcanary96fcdcc2015-08-27 07:41:13 -07001728 this->inval(nullptr);
yangsu@google.com921091f2011-08-02 13:39:12 +00001729 this->updateTitle();
1730 return true;
1731 }
halcanary96fcdcc2015-08-27 07:41:13 -07001732 if (SkOSMenu::FindSwitchState(evt, "Flip X", nullptr)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001733 fFlipAxis ^= kFlipAxis_X;
reed@google.comf03bb562011-11-11 21:42:12 +00001734 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001735 return true;
1736 }
halcanary96fcdcc2015-08-27 07:41:13 -07001737 if (SkOSMenu::FindSwitchState(evt, "Flip Y", nullptr)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001738 fFlipAxis ^= kFlipAxis_Y;
reed@google.comf03bb562011-11-11 21:42:12 +00001739 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001740 return true;
1741 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001742 if (SkOSMenu::FindAction(evt,"Save to PDF")) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001743 this->saveToPdf();
1744 return true;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001745 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001746 return this->INHERITED::onEvent(evt);
1747}
1748
reed@android.comf2b98d62010-12-20 18:26:13 +00001749bool SampleWindow::onQuery(SkEvent* query) {
1750 if (query->isType("get-slide-count")) {
1751 query->setFast32(fSamples.count());
1752 return true;
1753 }
1754 if (query->isType("get-slide-title")) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001755 SkView* view = (*fSamples[query->getFast32()])();
reed@android.comf2b98d62010-12-20 18:26:13 +00001756 SkEvent evt(gTitleEvtName);
1757 if (view->doQuery(&evt)) {
1758 query->setString("title", evt.findString(gTitleEvtName));
1759 }
1760 SkSafeUnref(view);
1761 return true;
1762 }
1763 if (query->isType("use-fast-text")) {
1764 SkEvent evt(gFastTextEvtName);
1765 return curr_view(this)->doQuery(&evt);
1766 }
reed@google.com29038ed2011-07-06 17:56:47 +00001767 if (query->isType("ignore-window-bitmap")) {
halcanary96fcdcc2015-08-27 07:41:13 -07001768 query->setFast32(this->getGrContext() != nullptr);
reed@google.com29038ed2011-07-06 17:56:47 +00001769 return true;
1770 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001771 return this->INHERITED::onQuery(query);
1772}
1773
Cary Clark992c7b02014-07-31 08:58:44 -04001774DECLARE_bool(portableFonts);
caryclark5fb6bd42014-06-23 11:25:00 -07001775
reed@android.com8a1c16f2008-12-17 15:59:43 +00001776bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001777 {
1778 SkView* view = curr_view(this);
1779 if (view) {
1780 SkEvent evt(gCharEvtName);
1781 evt.setFast32(uni);
1782 if (view->doQuery(&evt)) {
1783 return true;
1784 }
1785 }
1786 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001787
reed@android.com8a1c16f2008-12-17 15:59:43 +00001788 int dx = 0xFF;
1789 int dy = 0xFF;
1790
1791 switch (uni) {
1792 case '5': dx = 0; dy = 0; break;
1793 case '8': dx = 0; dy = -1; break;
1794 case '6': dx = 1; dy = 0; break;
1795 case '2': dx = 0; dy = 1; break;
1796 case '4': dx = -1; dy = 0; break;
1797 case '7': dx = -1; dy = -1; break;
1798 case '9': dx = 1; dy = -1; break;
1799 case '3': dx = 1; dy = 1; break;
1800 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001801
reed@android.com8a1c16f2008-12-17 15:59:43 +00001802 default:
1803 break;
1804 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001805
reed@android.com8a1c16f2008-12-17 15:59:43 +00001806 if (0xFF != dx && 0xFF != dy) {
bungemance560262016-03-03 13:32:38 -08001807 this->changeOffset({SkIntToScalar(dx / 32.0f), SkIntToScalar(dy / 32.0f)});
reed@android.com8a1c16f2008-12-17 15:59:43 +00001808 return true;
1809 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001810
reed@android.com0ae6b242008-12-23 16:49:54 +00001811 switch (uni) {
reed76113a92015-02-02 12:55:02 -08001812 case 27: // ESC
1813 gAnimTimer.stop();
1814 if (this->sendAnimatePulse()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001815 this->inval(nullptr);
reed76113a92015-02-02 12:55:02 -08001816 }
1817 break;
reedd9adfe62015-02-01 19:01:04 -08001818 case ' ':
reed76113a92015-02-02 12:55:02 -08001819 gAnimTimer.togglePauseResume();
reedd9adfe62015-02-01 19:01:04 -08001820 if (this->sendAnimatePulse()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001821 this->inval(nullptr);
reedd9adfe62015-02-01 19:01:04 -08001822 }
1823 break;
liyuqian38911a72016-10-04 11:23:22 -07001824 case 'A':
Mike Klein511f2d72016-10-04 15:45:56 -04001825 gSkUseAnalyticAA = !gSkUseAnalyticAA.load();
liyuqian38911a72016-10-04 11:23:22 -07001826 this->inval(nullptr);
liyuqianebcb8aa2016-10-11 12:20:08 -07001827 this->updateTitle();
liyuqian38911a72016-10-04 11:23:22 -07001828 break;
reed@google.coma4f81372012-11-15 15:56:38 +00001829 case 'B':
halcanary385fe4d2015-08-26 13:07:48 -07001830 post_event_to_sink(new SkEvent("PictFileView::toggleBBox"), curr_view(this));
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001831 // Cannot call updateTitle() synchronously, because the toggleBBox event is still in
1832 // the queue.
halcanary385fe4d2015-08-26 13:07:48 -07001833 post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this);
halcanary96fcdcc2015-08-27 07:41:13 -07001834 this->inval(nullptr);
reed@google.coma4f81372012-11-15 15:56:38 +00001835 break;
reed4302ae92014-10-06 12:29:56 -07001836 case 'D':
1837 toggleDistanceFieldFonts();
1838 break;
reedbabc3de2016-07-08 08:43:27 -07001839 case 'E':
1840 fUseDeferredCanvas = !fUseDeferredCanvas;
1841 this->inval(nullptr);
1842 break;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001843 case 'f':
chudy@google.com4605a3f2012-08-01 17:58:01 +00001844 // only
djsollen@google.com6ff82552011-11-07 15:43:57 +00001845 toggleFPS();
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001846 break;
caryclark5fb6bd42014-06-23 11:25:00 -07001847 case 'F':
Cary Clark992c7b02014-07-31 08:58:44 -04001848 FLAGS_portableFonts ^= true;
halcanary96fcdcc2015-08-27 07:41:13 -07001849 this->inval(nullptr);
caryclark5fb6bd42014-06-23 11:25:00 -07001850 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001851 case 'g':
1852 fRequestGrabImage = true;
halcanary96fcdcc2015-08-27 07:41:13 -07001853 this->inval(nullptr);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001854 break;
reed@google.coma4f81372012-11-15 15:56:38 +00001855 case 'G':
1856 gShowGMBounds = !gShowGMBounds;
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001857 post_event_to_sink(GMSampleView::NewShowSizeEvt(gShowGMBounds),
reed@google.coma4f81372012-11-15 15:56:38 +00001858 curr_view(this));
halcanary96fcdcc2015-08-27 07:41:13 -07001859 this->inval(nullptr);
reed@google.coma4f81372012-11-15 15:56:38 +00001860 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001861 case 'i':
1862 this->zoomIn();
1863 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001864 case 'o':
1865 this->zoomOut();
1866 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001867 case 'r':
1868 fRotate = !fRotate;
halcanary96fcdcc2015-08-27 07:41:13 -07001869 this->inval(nullptr);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001870 this->updateTitle();
1871 return true;
bsalomon@google.come8f09102011-09-08 18:48:12 +00001872 case 'k':
1873 fPerspAnim = !fPerspAnim;
halcanary96fcdcc2015-08-27 07:41:13 -07001874 this->inval(nullptr);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001875 this->updateTitle();
1876 return true;
reed093b4e82015-08-04 18:46:38 -07001877 case 'K':
1878 fSaveToSKP = true;
halcanary96fcdcc2015-08-27 07:41:13 -07001879 this->inval(nullptr);
reed093b4e82015-08-04 18:46:38 -07001880 return true;
kkinnunen8686a5e2016-01-05 00:04:32 -08001881 case 'M':
reed91b4dfb2016-01-08 08:23:19 -08001882 fUsePicture = !fUsePicture;
kkinnunen8686a5e2016-01-05 00:04:32 -08001883 this->inval(nullptr);
1884 this->updateTitle();
1885 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001886#if SK_SUPPORT_GPU
twiz@google.com05e70242012-01-27 19:12:00 +00001887 case 'p':
1888 {
1889 GrContext* grContext = this->getGrContext();
1890 if (grContext) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001891 size_t cacheBytes;
halcanary96fcdcc2015-08-27 07:41:13 -07001892 grContext->getResourceCacheUsage(nullptr, &cacheBytes);
twiz@google.com05e70242012-01-27 19:12:00 +00001893 grContext->freeGpuResources();
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001894 SkDebugf("Purged %d bytes from the GPU resource cache.\n", cacheBytes);
twiz@google.com05e70242012-01-27 19:12:00 +00001895 }
1896 }
1897 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001898#endif
reed@android.com0ae6b242008-12-23 16:49:54 +00001899 default:
1900 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001901 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001902
scroggo@google.com7dadc742012-04-18 14:07:57 +00001903 if (fAppMenu->handleKeyEquivalent(uni)|| fSlideMenu->handleKeyEquivalent(uni)) {
1904 this->onUpdateMenu(fAppMenu);
1905 this->onUpdateMenu(fSlideMenu);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001906 return true;
1907 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001908 return this->INHERITED::onHandleChar(uni);
1909}
1910
yangsu@google.com921091f2011-08-02 13:39:12 +00001911void SampleWindow::setDeviceType(DeviceType type) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001912 if (type == fDeviceType)
1913 return;
halcanary9d524f22016-03-29 09:03:52 -07001914
reeda34be682016-02-15 07:48:35 -08001915 fDevManager->tearDownBackend(this);
reeda34be682016-02-15 07:48:35 -08001916 fDeviceType = type;
brianosman2d1ee792016-05-05 12:24:31 -07001917 fDevManager->setUpBackend(this, fMSAASampleCount, fDeepColor);
halcanary9d524f22016-03-29 09:03:52 -07001918
reeda34be682016-02-15 07:48:35 -08001919 this->updateTitle();
1920 this->inval(nullptr);
1921}
1922
brianosmanb109b8c2016-06-16 13:03:24 -07001923void SampleWindow::setDeviceColorType(SkColorType ct, sk_sp<SkColorSpace> cs) {
1924 this->setColorType(ct, std::move(cs));
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001925
1926 fDevManager->tearDownBackend(this);
brianosman2d1ee792016-05-05 12:24:31 -07001927 fDevManager->setUpBackend(this, fMSAASampleCount, fDeepColor);
halcanary9d524f22016-03-29 09:03:52 -07001928
yangsu@google.com921091f2011-08-02 13:39:12 +00001929 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001930 this->inval(nullptr);
yangsu@google.com921091f2011-08-02 13:39:12 +00001931}
1932
Scroggo2c8208f2011-06-15 16:49:08 +00001933void SampleWindow::toggleSlideshow() {
1934 fAnimating = !fAnimating;
1935 this->postAnimatingEvent();
1936 this->updateTitle();
1937}
1938
1939void SampleWindow::toggleRendering() {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001940 this->setDeviceType(cycle_devicetype(fDeviceType));
Scroggo2c8208f2011-06-15 16:49:08 +00001941 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001942 this->inval(nullptr);
Scroggo2c8208f2011-06-15 16:49:08 +00001943}
1944
djsollen@google.com6ff82552011-11-07 15:43:57 +00001945void SampleWindow::toggleFPS() {
1946 fMeasureFPS = !fMeasureFPS;
1947 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001948 this->inval(nullptr);
djsollen@google.com6ff82552011-11-07 15:43:57 +00001949}
1950
reed4302ae92014-10-06 12:29:56 -07001951void SampleWindow::toggleDistanceFieldFonts() {
jvanverth629162d2015-11-08 08:07:24 -08001952 // reset backend
1953 fDevManager->tearDownBackend(this);
brianosman2d1ee792016-05-05 12:24:31 -07001954 fDevManager->setUpBackend(this, fMSAASampleCount, fDeepColor);
jvanverth629162d2015-11-08 08:07:24 -08001955
reed4302ae92014-10-06 12:29:56 -07001956 SkSurfaceProps props = this->getSurfaceProps();
bsalomonafcd7cd2015-08-31 12:39:41 -07001957 uint32_t flags = props.flags() ^ SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
reed4302ae92014-10-06 12:29:56 -07001958 this->setSurfaceProps(SkSurfaceProps(flags, props.pixelGeometry()));
1959
1960 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001961 this->inval(nullptr);
reed4302ae92014-10-06 12:29:56 -07001962}
1963
bungeman5a59a422016-03-31 08:30:38 -07001964void SampleWindow::setPixelGeometry(int pixelGeometryIndex) {
1965 // reset backend
1966 fDevManager->tearDownBackend(this);
brianosman2d1ee792016-05-05 12:24:31 -07001967 fDevManager->setUpBackend(this, fMSAASampleCount, fDeepColor);
bungeman5a59a422016-03-31 08:30:38 -07001968
1969 const SkSurfaceProps& oldProps = this->getSurfaceProps();
1970 SkSurfaceProps newProps(oldProps.flags(), SkSurfaceProps::kLegacyFontHost_InitType);
1971 if (pixelGeometryIndex > 0) {
1972 newProps = SkSurfaceProps(oldProps.flags(),
1973 gPixelGeometryStates[pixelGeometryIndex].pixelGeometry);
1974 }
1975 this->setSurfaceProps(newProps);
1976
1977 this->updateTitle();
1978 this->inval(nullptr);
1979}
1980
reed@android.com8a1c16f2008-12-17 15:59:43 +00001981#include "SkDumpCanvas.h"
1982
1983bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001984 {
1985 SkView* view = curr_view(this);
1986 if (view) {
1987 SkEvent evt(gKeyEvtName);
1988 evt.setFast32(key);
1989 if (view->doQuery(&evt)) {
1990 return true;
1991 }
1992 }
1993 }
bungemance560262016-03-03 13:32:38 -08001994
1995 int dx = 0xFF;
1996 int dy = 0xFF;
1997
reed@android.com8a1c16f2008-12-17 15:59:43 +00001998 switch (key) {
1999 case kRight_SkKey:
2000 if (this->nextSample()) {
2001 return true;
2002 }
2003 break;
2004 case kLeft_SkKey:
scroggo@google.come2dd9732012-08-15 20:03:06 +00002005 if (this->previousSample()) {
2006 return true;
2007 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002008 return true;
2009 case kUp_SkKey:
reedf7f79d22015-02-21 09:03:20 -08002010 this->changeZoomLevel(1.f / 32.f);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002011 return true;
2012 case kDown_SkKey:
reedf7f79d22015-02-21 09:03:20 -08002013 this->changeZoomLevel(-1.f / 32.f);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002014 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002015 case kOK_SkKey: {
2016 SkString title;
2017 if (curr_title(this, &title)) {
2018 writeTitleToPrefs(title.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002019 }
2020 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002021 }
reed@android.com34245c72009-11-03 04:00:48 +00002022 case kBack_SkKey:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002023 this->showOverview();
reed@android.com34245c72009-11-03 04:00:48 +00002024 return true;
bungemance560262016-03-03 13:32:38 -08002025
2026 case k5_SkKey: dx = 0; dy = 0; break;
2027 case k8_SkKey: dx = 0; dy = -1; break;
2028 case k6_SkKey: dx = 1; dy = 0; break;
2029 case k2_SkKey: dx = 0; dy = 1; break;
2030 case k4_SkKey: dx = -1; dy = 0; break;
2031 case k7_SkKey: dx = -1; dy = -1; break;
2032 case k9_SkKey: dx = 1; dy = -1; break;
2033 case k3_SkKey: dx = 1; dy = 1; break;
2034 case k1_SkKey: dx = -1; dy = 1; break;
2035
reed@android.com8a1c16f2008-12-17 15:59:43 +00002036 default:
2037 break;
2038 }
bungemance560262016-03-03 13:32:38 -08002039
2040 if (0xFF != dx && 0xFF != dy) {
2041 this->changeOffset({SkIntToScalar(dx / 32.0f), SkIntToScalar(dy / 32.0f)});
2042 return true;
2043 }
2044
reed@android.com8a1c16f2008-12-17 15:59:43 +00002045 return this->INHERITED::onHandleKey(key);
2046}
2047
reed@google.com52f57e12011-03-16 12:10:02 +00002048///////////////////////////////////////////////////////////////////////////////
2049
2050static const char gGestureClickType[] = "GestureClickType";
2051
Scroggod3aed392011-06-22 13:26:56 +00002052bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
reed@google.com4d5c26d2013-01-08 16:17:50 +00002053 void* owner, unsigned modi) {
Scroggo0f185c22011-03-24 18:35:50 +00002054 if (Click::kMoved_State == state) {
2055 updatePointer(x, y);
2056 }
reed@google.come1ca7052013-12-17 19:22:07 +00002057 int w = SkScalarRoundToInt(this->width());
2058 int h = SkScalarRoundToInt(this->height());
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002059
2060 // check for the resize-box
2061 if (w - x < 16 && h - y < 16) {
2062 return false; // let the OS handle the click
chudy@google.com4605a3f2012-08-01 17:58:01 +00002063 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002064 else if (fMagnify) {
2065 //it's only necessary to update the drawing if there's a click
halcanary96fcdcc2015-08-27 07:41:13 -07002066 this->inval(nullptr);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002067 return false; //prevent dragging while magnify is enabled
reed@google.com4d5c26d2013-01-08 16:17:50 +00002068 } else {
2069 // capture control+option, and trigger debugger
2070 if ((modi & kControl_SkModifierKey) && (modi & kOption_SkModifierKey)) {
2071 if (Click::kDown_State == state) {
2072 SkEvent evt("debug-hit-test");
2073 evt.setS32("debug-hit-test-x", x);
2074 evt.setS32("debug-hit-test-y", y);
2075 curr_view(this)->doEvent(evt);
2076 }
2077 return true;
2078 } else {
2079 return this->INHERITED::onDispatchClick(x, y, state, owner, modi);
2080 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002081 }
2082}
2083
reed@google.com52f57e12011-03-16 12:10:02 +00002084class GestureClick : public SkView::Click {
2085public:
2086 GestureClick(SkView* target) : SkView::Click(target) {
2087 this->setType(gGestureClickType);
2088 }
2089
2090 static bool IsGesture(Click* click) {
2091 return click->isType(gGestureClickType);
2092 }
2093};
2094
reed@google.com4d5c26d2013-01-08 16:17:50 +00002095SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y,
2096 unsigned modi) {
reed@google.com52f57e12011-03-16 12:10:02 +00002097 return new GestureClick(this);
2098}
2099
2100bool SampleWindow::onClick(Click* click) {
2101 if (GestureClick::IsGesture(click)) {
bsalomon@google.com4d4f2812011-12-12 18:34:01 +00002102 float x = static_cast<float>(click->fICurr.fX);
2103 float y = static_cast<float>(click->fICurr.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00002104
reed@google.com52f57e12011-03-16 12:10:02 +00002105 switch (click->fState) {
2106 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00002107 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00002108 break;
2109 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00002110 fGesture.touchMoved(click->fOwner, x, y);
reed@google.comf03bb562011-11-11 21:42:12 +00002111 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002112 break;
2113 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00002114 fGesture.touchEnd(click->fOwner);
reed@google.comf03bb562011-11-11 21:42:12 +00002115 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00002116 break;
2117 }
2118 return true;
2119 }
2120 return false;
2121}
2122
2123///////////////////////////////////////////////////////////////////////////////
2124
reed@android.com8a1c16f2008-12-17 15:59:43 +00002125void SampleWindow::loadView(SkView* view) {
2126 SkView::F2BIter iter(this);
2127 SkView* prev = iter.next();
2128 if (prev) {
2129 prev->detachFromParent();
2130 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002131
reed@android.com8a1c16f2008-12-17 15:59:43 +00002132 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00002133 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002134 this->attachChildToFront(view)->unref();
2135 view->setSize(this->width(), this->height());
2136
yangsu@google.com921091f2011-08-02 13:39:12 +00002137 //repopulate the slide menu when a view is loaded
scroggo@google.com7dadc742012-04-18 14:07:57 +00002138 fSlideMenu->reset();
djsollen@google.com388974f2013-01-17 13:20:01 +00002139
scroggo@google.com7dadc742012-04-18 14:07:57 +00002140 this->onUpdateMenu(fSlideMenu);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002141 this->updateTitle();
2142}
2143
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002144static const char* gDeviceTypePrefix[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002145 "raster: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002146#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00002147 "opengl: ",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00002148#if SK_ANGLE
2149 "angle: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002150#endif // SK_ANGLE
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002151#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +00002152};
bungeman99fe8222015-08-20 07:57:51 -07002153static_assert(SK_ARRAY_COUNT(gDeviceTypePrefix) == SampleWindow::kDeviceTypeCnt,
2154 "array_size_mismatch");
reed@android.com8a1c16f2008-12-17 15:59:43 +00002155
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002156static const char* trystate_str(SkOSMenu::TriState state,
reed@google.com569e0432011-04-05 13:07:03 +00002157 const char trueStr[], const char falseStr[]) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002158 if (SkOSMenu::kOnState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002159 return trueStr;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002160 } else if (SkOSMenu::kOffState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002161 return falseStr;
2162 }
halcanary96fcdcc2015-08-27 07:41:13 -07002163 return nullptr;
reed@google.com569e0432011-04-05 13:07:03 +00002164}
2165
reedddb5eca2014-10-08 11:10:51 -07002166bool SampleWindow::getRawTitle(SkString* title) {
2167 return curr_title(this, title);
2168}
reed@android.com8a1c16f2008-12-17 15:59:43 +00002169
reedddb5eca2014-10-08 11:10:51 -07002170void SampleWindow::updateTitle() {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002171 SkString title;
reedddb5eca2014-10-08 11:10:51 -07002172 if (!this->getRawTitle(&title)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002173 title.set("<unknown>");
2174 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002175
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002176 title.prepend(gDeviceTypePrefix[fDeviceType]);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002177
liyuqianebcb8aa2016-10-11 12:20:08 -07002178 if (gSkUseAnalyticAA) {
2179 title.prepend("<AAA> ");
2180 }
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00002181 if (fTilingMode != kNo_Tiling) {
2182 title.prependf("<T: %s> ", gTilingInfo[fTilingMode].label);
2183 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002184 if (fAnimating) {
2185 title.prepend("<A> ");
2186 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00002187 if (fRotate) {
2188 title.prepend("<R> ");
2189 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00002190 if (fPerspAnim) {
2191 title.prepend("<K> ");
2192 }
bsalomonafcd7cd2015-08-31 12:39:41 -07002193 if (this->getSurfaceProps().flags() & SkSurfaceProps::kUseDeviceIndependentFonts_Flag) {
2194 title.prepend("<DIF> ");
reed4302ae92014-10-06 12:29:56 -07002195 }
reed91b4dfb2016-01-08 08:23:19 -08002196 if (fUsePicture) {
2197 title.prepend("<P> ");
kkinnunen8686a5e2016-01-05 00:04:32 -08002198 }
reedbabc3de2016-07-08 08:43:27 -07002199 if (fUseDeferredCanvas) {
2200 title.prepend("<E> ");
2201 }
reed@google.com569e0432011-04-05 13:07:03 +00002202
2203 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
2204 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed93a12152015-03-16 10:08:34 -07002205 title.prepend(gFilterQualityStates[fFilterQualityIndex].fLabel);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002206 title.prepend(trystate_str(fSubpixelState, "S ", "s "));
halcanary96fcdcc2015-08-27 07:41:13 -07002207 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : nullptr);
2208 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : nullptr);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002209 title.prepend(gHintingStates[fHintingState].label);
bungeman5a59a422016-03-31 08:30:38 -07002210 title.prepend(gPixelGeometryStates[fPixelGeometryIndex].label);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002211
bungemance560262016-03-03 13:32:38 -08002212 if (fOffset.fX || fOffset.fY) {
2213 title.prependf("(%.2f, %.2f) ", SkScalarToFloat(fOffset.fX), SkScalarToFloat(fOffset.fY));
2214 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002215 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00002216 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002217 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002218
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002219 if (fMeasureFPS) {
jvanverth4d0d81a2015-08-11 11:07:07 -07002220 title.appendf(" %8.4f ms", fMeasureFPS_Time / (float)FPS_REPEAT_COUNT);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002221 }
reedddb5eca2014-10-08 11:10:51 -07002222
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002223#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00002224 if (IsGpuDeviceType(fDeviceType) &&
bsalomon49f085d2014-09-05 13:34:00 -07002225 fDevManager &&
robertphillipsecf3dbe2016-07-28 15:17:34 -07002226 fDevManager->numColorSamples() > 0) {
bsalomon@google.com11959252012-04-06 20:13:38 +00002227 title.appendf(" [MSAA: %d]",
robertphillipsecf3dbe2016-07-28 15:17:34 -07002228 fDevManager->numColorSamples());
bsalomon@google.com11959252012-04-06 20:13:38 +00002229 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002230#endif
bsalomon@google.com11959252012-04-06 20:13:38 +00002231
brianosman35a02a82016-09-29 14:37:02 -07002232 title.appendf(" %s", gConfig[fColorConfigIndex].fName);
reeda34be682016-02-15 07:48:35 -08002233
brianosman2d1ee792016-05-05 12:24:31 -07002234 if (fDevManager && fDevManager->getColorBits() > 24) {
2235 title.appendf(" %d bpc", fDevManager->getColorBits());
2236 }
2237
reed@android.com8a1c16f2008-12-17 15:59:43 +00002238 this->setTitle(title.c_str());
2239}
2240
2241void SampleWindow::onSizeChange() {
2242 this->INHERITED::onSizeChange();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002243
reed@android.com8a1c16f2008-12-17 15:59:43 +00002244 SkView::F2BIter iter(this);
2245 SkView* view = iter.next();
2246 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002247
reed@android.com8a1c16f2008-12-17 15:59:43 +00002248 // rebuild our clippath
2249 {
2250 const SkScalar W = this->width();
2251 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002252
reed@android.com8a1c16f2008-12-17 15:59:43 +00002253 fClipPath.reset();
2254#if 0
2255 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
2256 SkRect r;
2257 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
2258 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
2259 fClipPath.addRect(r);
2260 }
2261#else
2262 SkRect r;
2263 r.set(0, 0, W, H);
2264 fClipPath.addRect(r, SkPath::kCCW_Direction);
2265 r.set(W/4, H/4, W*3/4, H*3/4);
2266 fClipPath.addRect(r, SkPath::kCW_Direction);
2267#endif
2268 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002269
Scroggo2c8208f2011-06-15 16:49:08 +00002270 fZoomCenterX = SkScalarHalf(this->width());
2271 fZoomCenterY = SkScalarHalf(this->height());
2272
djsollen@google.com56c69772011-11-08 19:00:26 +00002273#ifdef SK_BUILD_FOR_ANDROID
Scroggob4490c72011-06-17 13:53:05 +00002274 // FIXME: The first draw after a size change does not work on Android, so
2275 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00002276 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00002277#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00002278 this->updateTitle(); // to refresh our config
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002279 fDevManager->windowSizeChanged(this);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00002280
2281 if (fTilingMode != kNo_Tiling && SampleView::IsSampleView(view)) {
2282 ((SampleView*)view)->onTileSizeChanged(this->tileSize());
2283 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002284}
2285
2286///////////////////////////////////////////////////////////////////////////////
2287
reed@android.comf2b98d62010-12-20 18:26:13 +00002288template <typename T> void SkTBSort(T array[], int count) {
2289 for (int i = 1; i < count - 1; i++) {
2290 bool didSwap = false;
2291 for (int j = count - 1; j > i; --j) {
2292 if (array[j] < array[j-1]) {
2293 T tmp(array[j-1]);
2294 array[j-1] = array[j];
2295 array[j] = tmp;
2296 didSwap = true;
2297 }
2298 }
2299 if (!didSwap) {
2300 break;
2301 }
2302 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002303
reed@android.comf2b98d62010-12-20 18:26:13 +00002304 for (int k = 0; k < count - 1; k++) {
2305 SkASSERT(!(array[k+1] < array[k]));
2306 }
2307}
2308
2309#include "SkRandom.h"
2310
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002311static void rand_rect(SkIRect* rect, SkRandom& rand) {
reed@android.comf2b98d62010-12-20 18:26:13 +00002312 int bits = 8;
2313 int shift = 32 - bits;
2314 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
2315 rand.nextU() >> shift, rand.nextU() >> shift);
2316 rect->sort();
2317}
2318
2319static void dumpRect(const SkIRect& r) {
2320 SkDebugf(" { %d, %d, %d, %d },\n",
2321 r.fLeft, r.fTop,
2322 r.fRight, r.fBottom);
2323}
2324
2325static void test_rects(const SkIRect rect[], int count) {
2326 SkRegion rgn0, rgn1;
2327
2328 for (int i = 0; i < count; i++) {
2329 rgn0.op(rect[i], SkRegion::kUnion_Op);
2330 // dumpRect(rect[i]);
2331 }
2332 rgn1.setRects(rect, count);
2333
2334 if (rgn0 != rgn1) {
2335 SkDebugf("\n");
2336 for (int i = 0; i < count; i++) {
2337 dumpRect(rect[i]);
2338 }
2339 SkDebugf("\n");
2340 }
2341}
2342
2343static void test() {
2344 size_t i;
2345
2346 const SkIRect r0[] = {
2347 { 0, 0, 1, 1 },
2348 { 2, 2, 3, 3 },
2349 };
2350 const SkIRect r1[] = {
2351 { 0, 0, 1, 3 },
2352 { 1, 1, 2, 2 },
2353 { 2, 0, 3, 3 },
2354 };
2355 const SkIRect r2[] = {
2356 { 0, 0, 1, 2 },
2357 { 2, 1, 3, 3 },
2358 { 4, 0, 5, 1 },
2359 { 6, 0, 7, 4 },
2360 };
2361
2362 static const struct {
2363 const SkIRect* fRects;
2364 int fCount;
2365 } gRecs[] = {
2366 { r0, SK_ARRAY_COUNT(r0) },
2367 { r1, SK_ARRAY_COUNT(r1) },
2368 { r2, SK_ARRAY_COUNT(r2) },
2369 };
2370
2371 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
2372 test_rects(gRecs[i].fRects, gRecs[i].fCount);
2373 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002374
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002375 SkRandom rand;
reed@android.comf2b98d62010-12-20 18:26:13 +00002376 for (i = 0; i < 10000; i++) {
2377 SkRegion rgn0, rgn1;
2378
2379 const int N = 8;
2380 SkIRect rect[N];
2381 for (int j = 0; j < N; j++) {
2382 rand_rect(&rect[j], rand);
2383 }
2384 test_rects(rect, N);
2385 }
2386}
2387
caryclark@google.com02939ce2012-06-06 12:09:51 +00002388// FIXME: this should be in a header
2389SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00002390SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
caryclark@google.com02939ce2012-06-06 12:09:51 +00002391 if (false) { // avoid bit rot, suppress warning
2392 test();
2393 }
halcanary96fcdcc2015-08-27 07:41:13 -07002394 return new SampleWindow(hwnd, argc, argv, nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002395}
2396
caryclark@google.com02939ce2012-06-06 12:09:51 +00002397// FIXME: this should be in a header
2398void get_preferred_size(int* x, int* y, int* width, int* height);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002399void get_preferred_size(int* x, int* y, int* width, int* height) {
2400 *x = 10;
2401 *y = 50;
2402 *width = 640;
2403 *height = 480;
2404}
2405
caryclark@google.com5987f582012-10-02 18:33:14 +00002406#ifdef SK_BUILD_FOR_IOS
reedf0b17102014-10-22 13:06:00 -07002407#include "SkApplication.h"
caryclark17f0b6d2014-07-22 10:15:34 -07002408IOS_launch_type set_cmd_line_args(int , char *[], const char* resourceDir) {
caryclarkf568f6a2014-07-18 10:58:56 -07002409 SetResourcePath(resourceDir);
caryclark17f0b6d2014-07-22 10:15:34 -07002410 return kApplication__iOSLaunchType;
caryclark@google.com5987f582012-10-02 18:33:14 +00002411}
2412#endif
2413
reed@android.com8a1c16f2008-12-17 15:59:43 +00002414void application_init() {
2415// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002416#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00002417 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002418#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002419 SkGraphics::Init();
2420 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002421}
2422
2423void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002424 SkEvent::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002425}