blob: 439b699c383ac1c933333a650c91e3cff2111c2c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed868074b2014-06-03 10:53:59 -07007
Scroggo2c8208f2011-06-15 16:49:08 +00008#include "SampleApp.h"
9
tfarinabcbc1782014-06-18 14:32:48 -070010#include "OverView.h"
caryclarkf568f6a2014-07-18 10:58:56 -070011#include "Resources.h"
tfarinabcbc1782014-06-18 14:32:48 -070012#include "SampleCode.h"
reed76113a92015-02-02 12:55:02 -080013#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkCanvas.h"
tfarinabcbc1782014-06-18 14:32:48 -070015#include "SkCommandLineFlags.h"
16#include "SkData.h"
reedddb5eca2014-10-08 11:10:51 -070017#include "SkDocument.h"
robertphillips1e876182016-07-29 08:28:25 -070018#include "SkGammaColorFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkGraphics.h"
brianosman2d1ee792016-05-05 12:24:31 -070020#include "SkImage_Base.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +000021#include "SkImageEncoder.h"
tfarinabcbc1782014-06-18 14:32:48 -070022#include "SkOSFile.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkPaint.h"
fmalita822ace92015-06-15 07:07:32 -070024#include "SkPaintFilterCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000026#include "SkPictureRecorder.h"
brianosman36db3f42016-04-20 09:20:46 -070027#include "SkPM4fPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000028#include "SkStream.h"
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +000029#include "SkSurface.h"
scroggo565901d2015-12-10 10:44:13 -080030#include "SkTemplates.h"
edisonn@google.comf8b6b012013-07-11 14:28:04 +000031#include "SkTSort.h"
reed@android.com44177402009-11-23 21:07:51 +000032#include "SkTime.h"
Scroggo0f185c22011-03-24 18:35:50 +000033#include "SkTypeface.h"
tfarinabcbc1782014-06-18 14:32:48 -070034#include "SkWindow.h"
tfarinabcbc1782014-06-18 14:32:48 -070035#include "sk_tool_utils.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000036
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000037#if SK_SUPPORT_GPU
bsalomon3724e572016-03-30 18:56:19 -070038# include "gl/GrGLInterface.h"
39# include "gl/GrGLUtil.h"
bsalomon3724e572016-03-30 18:56:19 -070040# include "GrContext.h"
robertphillips7715e062016-04-22 10:57:16 -070041# include "SkGr.h"
bsalomon3724e572016-03-30 18:56:19 -070042# if SK_ANGLE
bsalomon273c0f52016-03-31 10:59:06 -070043# include "gl/angle/GLTestContext_angle.h"
bsalomon3724e572016-03-30 18:56:19 -070044# endif
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000045#else
46class GrContext;
47#endif
Scroggo2c8208f2011-06-15 16:49:08 +000048
reeda34be682016-02-15 07:48:35 -080049const struct {
50 SkColorType fColorType;
brianosmanb109b8c2016-06-16 13:03:24 -070051 bool fSRGB;
reeda34be682016-02-15 07:48:35 -080052 const char* fName;
53} gConfig[] = {
brianosmanb109b8c2016-06-16 13:03:24 -070054 { kN32_SkColorType, false, "L32" },
55 { kN32_SkColorType, true, "S32" },
brianosmanefded512016-07-26 08:11:50 -070056 { kRGBA_F16_SkColorType, true, "F16" },
reeda34be682016-02-15 07:48:35 -080057};
58
59static const char* find_config_name(const SkImageInfo& info) {
60 for (const auto& config : gConfig) {
brianosmanb109b8c2016-06-16 13:03:24 -070061 if (config.fColorType == info.colorType() &&
62 config.fSRGB == (info.colorSpace() != nullptr)) {
reeda34be682016-02-15 07:48:35 -080063 return config.fName;
64 }
65 }
66 return "???";
67}
68
reed5f629b42015-08-15 11:57:23 -070069// Should be 3x + 1
70#define kMaxFatBitsScale 28
71
reed@google.com1830c7a2012-06-04 12:05:43 +000072extern SampleView* CreateSamplePictFileView(const char filename[]);
73
74class PictFileFactory : public SkViewFactory {
75 SkString fFilename;
76public:
77 PictFileFactory(const SkString& filename) : fFilename(filename) {}
mtklein36352bf2015-03-25 18:17:31 -070078 SkView* operator() () const override {
reed@google.com1830c7a2012-06-04 12:05:43 +000079 return CreateSamplePictFileView(fFilename.c_str());
80 }
81};
82
fmalita6ceef3d2016-07-26 18:46:34 -070083extern SampleView* CreateSampleSVGFileView(const char filename[]);
84
85class SVGFileFactory : public SkViewFactory {
86 SkString fFilename;
87public:
88 SVGFileFactory(const SkString& filename) : fFilename(filename) {}
89 SkView* operator() () const override {
90 return CreateSampleSVGFileView(fFilename.c_str());
91 }
92};
93
edisonn@google.comf8b6b012013-07-11 14:28:04 +000094#ifdef SAMPLE_PDF_FILE_VIEWER
95extern SampleView* CreateSamplePdfFileViewer(const char filename[]);
96
97class PdfFileViewerFactory : public SkViewFactory {
98 SkString fFilename;
99public:
100 PdfFileViewerFactory(const SkString& filename) : fFilename(filename) {}
mtklein36352bf2015-03-25 18:17:31 -0700101 SkView* operator() () const override {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000102 return CreateSamplePdfFileViewer(fFilename.c_str());
103 }
104};
105#endif // SAMPLE_PDF_FILE_VIEWER
106
bsalomon49996162016-06-14 09:43:52 -0700107#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000108//#define DEFAULT_TO_ANGLE 1
109#else
bsalomon@google.com82d12232013-09-09 15:36:26 +0000110#define DEFAULT_TO_GPU 0 // if 1 default rendering is on GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000111#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113#define ANIMATING_EVENTTYPE "nextSample"
reed@google.com84cfce12013-05-29 19:22:20 +0000114#define ANIMATING_DELAY 250
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000116#ifdef SK_DEBUG
reed@google.combad8c872011-05-18 20:10:31 +0000117 #define FPS_REPEAT_MULTIPLIER 1
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000118#else
reed@google.combad8c872011-05-18 20:10:31 +0000119 #define FPS_REPEAT_MULTIPLIER 10
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000120#endif
reed@google.combad8c872011-05-18 20:10:31 +0000121#define FPS_REPEAT_COUNT (10 * FPS_REPEAT_MULTIPLIER)
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000122
reed@google.com3cec4d72011-07-06 13:59:47 +0000123static SampleWindow* gSampleWindow;
124
reed@google.coma4f81372012-11-15 15:56:38 +0000125static bool gShowGMBounds;
126
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +0000127static void post_event_to_sink(SkEvent* evt, SkEventSink* sink) {
reed@google.com2072db82011-11-08 15:18:10 +0000128 evt->setTargetID(sink->getSinkID())->post();
129}
130
reed76113a92015-02-02 12:55:02 -0800131static SkAnimTimer gAnimTimer;
132
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000133///////////////////////////////////////////////////////////////////////////////
134
135static const char* skip_until(const char* str, const char* skip) {
136 if (!str) {
halcanary96fcdcc2015-08-27 07:41:13 -0700137 return nullptr;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000138 }
139 return strstr(str, skip);
140}
141
142static const char* skip_past(const char* str, const char* skip) {
143 const char* found = skip_until(str, skip);
144 if (!found) {
halcanary96fcdcc2015-08-27 07:41:13 -0700145 return nullptr;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000146 }
147 return found + strlen(skip);
148}
149
150static const char* gPrefFileName = "sampleapp_prefs.txt";
151
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000152static bool readTitleFromPrefs(SkString* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000153 SkFILEStream stream(gPrefFileName);
154 if (!stream.isValid()) {
155 return false;
156 }
157
reed@google.com7fa2a652014-01-27 13:42:58 +0000158 size_t len = stream.getLength();
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000159 SkString data(len);
160 stream.read(data.writable_str(), len);
161 const char* s = data.c_str();
162
163 s = skip_past(s, "curr-slide-title");
164 s = skip_past(s, "=");
165 s = skip_past(s, "\"");
166 const char* stop = skip_until(s, "\"");
167 if (stop > s) {
168 title->set(s, stop - s);
169 return true;
170 }
171 return false;
172}
173
epoger@google.com6fc7cc22011-12-28 15:13:41 +0000174static void writeTitleToPrefs(const char* title) {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000175 SkFILEWStream stream(gPrefFileName);
176 SkString data;
177 data.printf("curr-slide-title = \"%s\"\n", title);
178 stream.write(data.c_str(), data.size());
179}
180
181///////////////////////////////////////////////////////////////////////////////
182
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000183class SampleWindow::DefaultDeviceManager : public SampleWindow::DeviceManager {
184public:
185
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000186 DefaultDeviceManager() {
187#if SK_SUPPORT_GPU
halcanary96fcdcc2015-08-27 07:41:13 -0700188 fCurContext = nullptr;
189 fCurIntf = nullptr;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000190 fMSAASampleCount = 0;
brianosman2d1ee792016-05-05 12:24:31 -0700191 fDeepColor = false;
192 fActualColorBits = 0;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000193#endif
194 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000195 }
196
197 virtual ~DefaultDeviceManager() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000198#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000199 SkSafeUnref(fCurContext);
200 SkSafeUnref(fCurIntf);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000201#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000202 }
203
brianosman2d1ee792016-05-05 12:24:31 -0700204 void setUpBackend(SampleWindow* win, int msaaSampleCount, bool deepColor) override {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000205 SkASSERT(kNone_BackEndType == fBackend);
robertphillips@google.com53e96a12012-03-30 14:47:53 +0000206
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000207 fBackend = kNone_BackEndType;
208
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000209#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000210 switch (win->getDeviceType()) {
reedbeedb812015-05-05 12:14:45 -0700211 case kRaster_DeviceType: // fallthrough
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000212 case kGPU_DeviceType:
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000213 // all these guys use the native backend
214 fBackend = kNativeGL_BackEndType;
215 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000216#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000217 case kANGLE_DeviceType:
218 // ANGLE is really the only odd man out
219 fBackend = kANGLE_BackEndType;
220 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000221#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000222 default:
223 SkASSERT(false);
224 break;
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000225 }
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000226 AttachmentInfo attachmentInfo;
brianosman2d1ee792016-05-05 12:24:31 -0700227 bool result = win->attach(fBackend, msaaSampleCount, deepColor, &attachmentInfo);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000228 if (!result) {
bsalomon@google.com74913722011-10-27 20:44:19 +0000229 SkDebugf("Failed to initialize GL");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000230 return;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000231 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000232 fMSAASampleCount = msaaSampleCount;
brianosman2d1ee792016-05-05 12:24:31 -0700233 fDeepColor = deepColor;
234 // Assume that we have at least 24-bit output, for backends that don't supply this data
235 fActualColorBits = SkTMax(attachmentInfo.fColorBits, 24);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000236
halcanary96fcdcc2015-08-27 07:41:13 -0700237 SkASSERT(nullptr == fCurIntf);
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000238 SkAutoTUnref<const GrGLInterface> glInterface;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000239 switch (win->getDeviceType()) {
reedbeedb812015-05-05 12:14:45 -0700240 case kRaster_DeviceType: // fallthrough
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000241 case kGPU_DeviceType:
242 // all these guys use the native interface
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000243 glInterface.reset(GrGLCreateNativeInterface());
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000244 break;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000245#if SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000246 case kANGLE_DeviceType:
bsalomon3724e572016-03-30 18:56:19 -0700247 glInterface.reset(sk_gpu_test::CreateANGLEGLInterface());
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000248 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000249#endif // SK_ANGLE
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000250 default:
251 SkASSERT(false);
252 break;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +0000253 }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000254
commit-bot@chromium.orgd8ed8512014-01-24 20:49:44 +0000255 // Currently SampleApp does not use NVPR. TODO: Provide an NVPR device type that is skipped
256 // when the driver doesn't support NVPR.
257 fCurIntf = GrGLInterfaceRemoveNVPR(glInterface.get());
258
halcanary96fcdcc2015-08-27 07:41:13 -0700259 SkASSERT(nullptr == fCurContext);
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000260 fCurContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf);
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000261
halcanary96fcdcc2015-08-27 07:41:13 -0700262 if (nullptr == fCurContext || nullptr == fCurIntf) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000263 // We need some context and interface to see results
264 SkSafeUnref(fCurContext);
265 SkSafeUnref(fCurIntf);
halcanary96fcdcc2015-08-27 07:41:13 -0700266 fCurContext = nullptr;
267 fCurIntf = nullptr;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000268 SkDebugf("Failed to setup 3D");
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000269
mtklein18300a32016-03-16 13:53:35 -0700270 win->release();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000271 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000272#endif // SK_SUPPORT_GPU
robertphillipsecf3dbe2016-07-28 15:17:34 -0700273 // call windowSizeChanged to create the gpu-backed Surface
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000274 this->windowSizeChanged(win);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000275 }
276
mtkleinf0599002015-07-13 06:18:39 -0700277 void tearDownBackend(SampleWindow *win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000278#if SK_SUPPORT_GPU
reed789e3272014-10-30 11:29:08 -0700279 if (fCurContext) {
280 // in case we have outstanding refs to this guy (lua?)
281 fCurContext->abandonContext();
282 fCurContext->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700283 fCurContext = nullptr;
reed789e3272014-10-30 11:29:08 -0700284 }
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000285
286 SkSafeUnref(fCurIntf);
halcanary96fcdcc2015-08-27 07:41:13 -0700287 fCurIntf = nullptr;
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000288
robertphillipsecf3dbe2016-07-28 15:17:34 -0700289 fGpuSurface = nullptr;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000290#endif
mtklein18300a32016-03-16 13:53:35 -0700291 win->release();
bsalomon@google.comddd40e52012-04-10 15:56:29 +0000292 fBackend = kNone_BackEndType;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000293 }
294
robertphillipsecf3dbe2016-07-28 15:17:34 -0700295 sk_sp<SkSurface> makeSurface(SampleWindow::DeviceType dType, SampleWindow* win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000296#if SK_SUPPORT_GPU
bsalomon49f085d2014-09-05 13:34:00 -0700297 if (IsGpuDeviceType(dType) && fCurContext) {
reed4302ae92014-10-06 12:29:56 -0700298 SkSurfaceProps props(win->getSurfaceProps());
brianosman2d1ee792016-05-05 12:24:31 -0700299 if (kRGBA_F16_SkColorType == win->info().colorType() || fActualColorBits > 24) {
300 // If we're rendering to F16, we need an off-screen surface - the current render
301 // target is most likely the wrong format.
302 //
303 // If we're using a deep (10-bit or higher) surface, we probably need an off-screen
304 // surface. 10-bit, in particular, has strange gamma behavior.
brianosman6b086522016-04-14 12:39:00 -0700305 return SkSurface::MakeRenderTarget(fCurContext, SkBudgeted::kNo, win->info(),
robertphillipsecf3dbe2016-07-28 15:17:34 -0700306 fMSAASampleCount, &props);
brianosman6b086522016-04-14 12:39:00 -0700307 } else {
robertphillipsecf3dbe2016-07-28 15:17:34 -0700308 return fGpuSurface;
brianosman6b086522016-04-14 12:39:00 -0700309 }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000310 }
reed0397e9f2014-09-18 11:29:01 -0700311#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700312 return nullptr;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000313 }
314
mtkleinf0599002015-07-13 06:18:39 -0700315 void publishCanvas(SampleWindow::DeviceType dType,
robertphillips1e876182016-07-29 08:28:25 -0700316 SkCanvas* renderingCanvas, SampleWindow* win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000317#if SK_SUPPORT_GPU
brianosman2d1ee792016-05-05 12:24:31 -0700318 if (!IsGpuDeviceType(dType) ||
319 kRGBA_F16_SkColorType == win->info().colorType() ||
320 fActualColorBits > 24) {
321 // We made/have an off-screen surface. Get the contents as an SkImage:
322 SkBitmap bm;
323 bm.allocPixels(win->info());
robertphillips1e876182016-07-29 08:28:25 -0700324 renderingCanvas->readPixels(&bm, 0, 0);
brianosman2d1ee792016-05-05 12:24:31 -0700325 SkPixmap pm;
326 bm.peekPixels(&pm);
327 sk_sp<SkImage> image(SkImage::MakeTextureFromPixmap(fCurContext, pm,
328 SkBudgeted::kNo));
robertphillipsecf3dbe2016-07-28 15:17:34 -0700329
robertphillips1e876182016-07-29 08:28:25 -0700330 SkCanvas* gpuCanvas = fGpuSurface->getCanvas();
brianosman2d1ee792016-05-05 12:24:31 -0700331
332 // With ten-bit output, we need to manually apply the gamma of the output device
333 // (unless we're in non-gamma correct mode, in which case our data is already
334 // fake-sRGB, like we're expected to put in the 10-bit buffer):
335 bool doGamma = (fActualColorBits == 30) && SkImageInfoIsGammaCorrect(win->info());
robertphillips1e876182016-07-29 08:28:25 -0700336
337 SkPaint gammaPaint;
338 gammaPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
339 if (doGamma) {
340 gammaPaint.setColorFilter(SkGammaColorFilter::Make(1.0f / 2.2f));
341 }
342
343 gpuCanvas->drawImage(image, 0, 0, &gammaPaint);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000344 }
robertphillips1e876182016-07-29 08:28:25 -0700345
346 fGpuSurface->prepareForExternalIO();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000347#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000348
349 win->present();
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000350 }
351
mtkleinf0599002015-07-13 06:18:39 -0700352 void windowSizeChanged(SampleWindow* win) override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000353#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000354 if (fCurContext) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000355 AttachmentInfo attachmentInfo;
brianosman2d1ee792016-05-05 12:24:31 -0700356 win->attach(fBackend, fMSAASampleCount, fDeepColor, &attachmentInfo);
brianosman2d1ee792016-05-05 12:24:31 -0700357 fActualColorBits = SkTMax(attachmentInfo.fColorBits, 24);
robertphillipsecf3dbe2016-07-28 15:17:34 -0700358 fGpuSurface = win->makeGpuBackedSurface(attachmentInfo, fCurIntf, fCurContext);
bsalomon@google.com74913722011-10-27 20:44:19 +0000359 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000360#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000361 }
362
mtkleinf0599002015-07-13 06:18:39 -0700363 GrContext* getGrContext() override {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000364#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000365 return fCurContext;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000366#else
halcanary96fcdcc2015-08-27 07:41:13 -0700367 return nullptr;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000368#endif
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000369 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000370
robertphillipsecf3dbe2016-07-28 15:17:34 -0700371 int numColorSamples() const override {
372 return fMSAASampleCount;
bsalomon@google.com11959252012-04-06 20:13:38 +0000373 }
374
brianosman2d1ee792016-05-05 12:24:31 -0700375 int getColorBits() override {
376#if SK_SUPPORT_GPU
377 return fActualColorBits;
378#else
379 return 24;
380#endif
381 }
382
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000383private:
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000384
385#if SK_SUPPORT_GPU
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000386 GrContext* fCurContext;
387 const GrGLInterface* fCurIntf;
robertphillipsecf3dbe2016-07-28 15:17:34 -0700388 sk_sp<SkSurface> fGpuSurface;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000389 int fMSAASampleCount;
brianosman2d1ee792016-05-05 12:24:31 -0700390 bool fDeepColor;
391 int fActualColorBits;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000392#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000393
394 SkOSWindow::SkBackEndTypes fBackend;
395
396 typedef SampleWindow::DeviceManager INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000397};
398
399///////////////
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000400static const char view_inval_msg[] = "view-inval-msg";
401
Scroggo62b65b02011-06-21 16:01:26 +0000402void SampleWindow::postInvalDelay() {
reed@google.com87fac4a2011-08-04 13:50:17 +0000403 (new SkEvent(view_inval_msg, this->getSinkID()))->postDelay(1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000404}
405
406static bool isInvalEvent(const SkEvent& evt) {
407 return evt.isType(view_inval_msg);
408}
409//////////////////
410
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000411#include "GMSampleView.h"
412
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000413class AutoUnrefArray {
414public:
415 AutoUnrefArray() {}
416 ~AutoUnrefArray() {
417 int count = fObjs.count();
418 for (int i = 0; i < count; ++i) {
419 fObjs[i]->unref();
420 }
421 }
422 SkRefCnt*& push_back() { return *fObjs.append(); }
chudy@google.com4605a3f2012-08-01 17:58:01 +0000423
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000424private:
425 SkTDArray<SkRefCnt*> fObjs;
426};
427
428// registers GMs as Samples
429// This can't be performed during static initialization because it could be
430// run before GMRegistry has been fully built.
caryclark@google.com02939ce2012-06-06 12:09:51 +0000431static void SkGMRegistyToSampleRegistry() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000432 static bool gOnce;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000433 static AutoUnrefArray fRegisters;
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000434
435 if (!gOnce) {
436 const skiagm::GMRegistry* gmreg = skiagm::GMRegistry::Head();
437 while (gmreg) {
438 fRegisters.push_back() = new SkViewRegister(gmreg->factory());
439 gmreg = gmreg->next();
440 }
441 gOnce = true;
442 }
443}
444
reed@google.comf0b5f682011-03-11 20:08:25 +0000445//////////////////////////////////////////////////////////////////////////////
446
reed@google.com569e0432011-04-05 13:07:03 +0000447enum FlipAxisEnum {
448 kFlipAxis_X = (1 << 0),
449 kFlipAxis_Y = (1 << 1)
450};
451
reed@google.comf0b5f682011-03-11 20:08:25 +0000452#include "SkDrawFilter.h"
453
bungeman@google.com96aabc82013-06-03 21:26:34 +0000454struct HintingState {
455 SkPaint::Hinting hinting;
456 const char* name;
457 const char* label;
458};
459static HintingState gHintingStates[] = {
halcanary96fcdcc2015-08-27 07:41:13 -0700460 {SkPaint::kNo_Hinting, "Mixed", nullptr },
bungeman@google.com96aabc82013-06-03 21:26:34 +0000461 {SkPaint::kNo_Hinting, "None", "H0 " },
462 {SkPaint::kSlight_Hinting, "Slight", "Hs " },
463 {SkPaint::kNormal_Hinting, "Normal", "Hn " },
464 {SkPaint::kFull_Hinting, "Full", "Hf " },
465};
466
bungeman5a59a422016-03-31 08:30:38 -0700467struct PixelGeometryState {
468 SkPixelGeometry pixelGeometry;
469 const char* name;
470 const char* label;
471};
472static PixelGeometryState gPixelGeometryStates[] = {
473 {SkPixelGeometry::kUnknown_SkPixelGeometry, "Mixed", nullptr },
474 {SkPixelGeometry::kUnknown_SkPixelGeometry, "Flat", "{Flat} " },
475 {SkPixelGeometry::kRGB_H_SkPixelGeometry, "RGB H", "{RGB H} " },
476 {SkPixelGeometry::kBGR_H_SkPixelGeometry, "BGR H", "{BGR H} " },
477 {SkPixelGeometry::kRGB_V_SkPixelGeometry, "RGB_V", "{RGB V} " },
478 {SkPixelGeometry::kBGR_V_SkPixelGeometry, "BGR_V", "{BGR V} " },
479};
480
reed93a12152015-03-16 10:08:34 -0700481struct FilterQualityState {
482 SkFilterQuality fQuality;
483 const char* fName;
484 const char* fLabel;
reed@google.com15bc13d2013-12-10 16:53:13 +0000485};
reed93a12152015-03-16 10:08:34 -0700486static FilterQualityState gFilterQualityStates[] = {
halcanary96fcdcc2015-08-27 07:41:13 -0700487 { kNone_SkFilterQuality, "Mixed", nullptr },
reed93a12152015-03-16 10:08:34 -0700488 { kNone_SkFilterQuality, "None", "F0 " },
489 { kLow_SkFilterQuality, "Low", "F1 " },
490 { kMedium_SkFilterQuality, "Medium", "F2 " },
491 { kHigh_SkFilterQuality, "High", "F3 " },
reed@google.com15bc13d2013-12-10 16:53:13 +0000492};
493
fmalita822ace92015-06-15 07:07:32 -0700494class FlagsFilterCanvas : public SkPaintFilterCanvas {
reed@google.comf0b5f682011-03-11 20:08:25 +0000495public:
fmalita822ace92015-06-15 07:07:32 -0700496 FlagsFilterCanvas(SkCanvas* canvas, SkOSMenu::TriState lcd, SkOSMenu::TriState aa,
497 SkOSMenu::TriState subpixel, int hinting, int filterQuality)
fmalitabad23dc2016-01-11 13:58:29 -0800498 : INHERITED(canvas)
fmalita822ace92015-06-15 07:07:32 -0700499 , fLCDState(lcd)
reed@google.com15bc13d2013-12-10 16:53:13 +0000500 , fAAState(aa)
501 , fSubpixelState(subpixel)
502 , fHintingState(hinting)
fmalita822ace92015-06-15 07:07:32 -0700503 , fFilterQualityIndex(filterQuality) {
reed93a12152015-03-16 10:08:34 -0700504 SkASSERT((unsigned)filterQuality < SK_ARRAY_COUNT(gFilterQualityStates));
reed@google.com15bc13d2013-12-10 16:53:13 +0000505 }
reed@google.comf0b5f682011-03-11 20:08:25 +0000506
fmalita822ace92015-06-15 07:07:32 -0700507protected:
fmalita32cdc322016-01-12 07:21:11 -0800508 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type t) const override {
509 if (!*paint) {
fmalitabad23dc2016-01-11 13:58:29 -0800510 return true;
511 }
512
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000513 if (kText_Type == t && SkOSMenu::kMixedState != fLCDState) {
fmalita32cdc322016-01-12 07:21:11 -0800514 paint->writable()->setLCDRenderText(SkOSMenu::kOnState == fLCDState);
reed@google.com569e0432011-04-05 13:07:03 +0000515 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000516 if (SkOSMenu::kMixedState != fAAState) {
fmalita32cdc322016-01-12 07:21:11 -0800517 paint->writable()->setAntiAlias(SkOSMenu::kOnState == fAAState);
reed@google.comf0b5f682011-03-11 20:08:25 +0000518 }
reed93a12152015-03-16 10:08:34 -0700519 if (0 != fFilterQualityIndex) {
fmalita32cdc322016-01-12 07:21:11 -0800520 paint->writable()->setFilterQuality(gFilterQualityStates[fFilterQualityIndex].fQuality);
reed@google.com176753a2011-05-17 15:32:04 +0000521 }
bungeman@google.com96aabc82013-06-03 21:26:34 +0000522 if (SkOSMenu::kMixedState != fSubpixelState) {
fmalita32cdc322016-01-12 07:21:11 -0800523 paint->writable()->setSubpixelText(SkOSMenu::kOnState == fSubpixelState);
bungeman@google.com96aabc82013-06-03 21:26:34 +0000524 }
bungeman@google.com055aa522013-06-03 21:35:03 +0000525 if (0 != fHintingState && fHintingState < (int)SK_ARRAY_COUNT(gHintingStates)) {
fmalita32cdc322016-01-12 07:21:11 -0800526 paint->writable()->setHinting(gHintingStates[fHintingState].hinting);
reed@google.com09e3baa2011-05-18 12:04:31 +0000527 }
fmalitabad23dc2016-01-11 13:58:29 -0800528 return true;
reed@google.comf0b5f682011-03-11 20:08:25 +0000529 }
530
531private:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000532 SkOSMenu::TriState fLCDState;
533 SkOSMenu::TriState fAAState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000534 SkOSMenu::TriState fSubpixelState;
535 int fHintingState;
reed93a12152015-03-16 10:08:34 -0700536 int fFilterQualityIndex;
fmalita822ace92015-06-15 07:07:32 -0700537
538 typedef SkPaintFilterCanvas INHERITED;
reed@google.comf0b5f682011-03-11 20:08:25 +0000539};
540
reed@android.comf2b98d62010-12-20 18:26:13 +0000541///////////////////////////////////////////////////////////////////////////////
542
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000543enum TilingMode {
544 kNo_Tiling,
545 kAbs_128x128_Tiling,
546 kAbs_256x256_Tiling,
547 kRel_4x4_Tiling,
548 kRel_1x16_Tiling,
549 kRel_16x1_Tiling,
550
551 kLast_TilingMode_Enum
552};
553
554struct TilingInfo {
555 const char* label;
556 SkScalar w, h;
557};
558
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000559static const struct TilingInfo gTilingInfo[] = {
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000560 { "No tiling", SK_Scalar1 , SK_Scalar1 }, // kNo_Tiling
561 { "128x128" , SkIntToScalar(128), SkIntToScalar(128) }, // kAbs_128x128_Tiling
562 { "256x256" , SkIntToScalar(256), SkIntToScalar(256) }, // kAbs_256x256_Tiling
563 { "1/4x1/4" , SK_Scalar1 / 4 , SK_Scalar1 / 4 }, // kRel_4x4_Tiling
564 { "1/1x1/16" , SK_Scalar1 , SK_Scalar1 / 16 }, // kRel_1x16_Tiling
565 { "1/16x1/1" , SK_Scalar1 / 16 , SK_Scalar1 }, // kRel_16x1_Tiling
566};
bungeman99fe8222015-08-20 07:57:51 -0700567static_assert((SK_ARRAY_COUNT(gTilingInfo) == kLast_TilingMode_Enum),
568 "Incomplete_tiling_labels");
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000569
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000570SkSize SampleWindow::tileSize() const {
571 SkASSERT((TilingMode)fTilingMode < kLast_TilingMode_Enum);
572 const struct TilingInfo* info = gTilingInfo + fTilingMode;
573 return SkSize::Make(info->w > SK_Scalar1 ? info->w : this->width() * info->w,
574 info->h > SK_Scalar1 ? info->h : this->height() * info->h);
575}
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000576//////////////////////////////////////////////////////////////////////////////
577
reed@android.comf2b98d62010-12-20 18:26:13 +0000578static SkView* curr_view(SkWindow* wind) {
579 SkView::F2BIter iter(wind);
580 return iter.next();
581}
582
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000583static bool curr_title(SkWindow* wind, SkString* title) {
584 SkView* view = curr_view(wind);
585 if (view) {
586 SkEvent evt(gTitleEvtName);
587 if (view->doQuery(&evt)) {
588 title->set(evt.findString(gTitleEvtName));
589 return true;
590 }
591 }
592 return false;
593}
594
reedd9adfe62015-02-01 19:01:04 -0800595bool SampleWindow::sendAnimatePulse() {
596 SkView* view = curr_view(this);
597 if (SampleView::IsSampleView(view)) {
reed76113a92015-02-02 12:55:02 -0800598 return ((SampleView*)view)->animate(gAnimTimer);
reedd9adfe62015-02-01 19:01:04 -0800599 }
600 return false;
601}
602
reed5f629b42015-08-15 11:57:23 -0700603void SampleWindow::setZoomCenter(float x, float y) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000604 fZoomCenterX = x;
605 fZoomCenterY = y;
Scroggo2c8208f2011-06-15 16:49:08 +0000606}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000607
reed5f629b42015-08-15 11:57:23 -0700608bool SampleWindow::zoomIn() {
Scroggo0f185c22011-03-24 18:35:50 +0000609 // Arbitrarily decided
reed5f629b42015-08-15 11:57:23 -0700610 if (fFatBitsScale == kMaxFatBitsScale) return false;
Scroggo0f185c22011-03-24 18:35:50 +0000611 fFatBitsScale++;
halcanary96fcdcc2015-08-27 07:41:13 -0700612 this->inval(nullptr);
Scroggo0f185c22011-03-24 18:35:50 +0000613 return true;
614}
615
reed5f629b42015-08-15 11:57:23 -0700616bool SampleWindow::zoomOut() {
Scroggo0f185c22011-03-24 18:35:50 +0000617 if (fFatBitsScale == 1) return false;
618 fFatBitsScale--;
halcanary96fcdcc2015-08-27 07:41:13 -0700619 this->inval(nullptr);
Scroggo0f185c22011-03-24 18:35:50 +0000620 return true;
621}
622
reed5f629b42015-08-15 11:57:23 -0700623void SampleWindow::updatePointer(int x, int y) {
Scroggo0f185c22011-03-24 18:35:50 +0000624 fMouseX = x;
625 fMouseY = y;
626 if (fShowZoomer) {
halcanary96fcdcc2015-08-27 07:41:13 -0700627 this->inval(nullptr);
Scroggo0f185c22011-03-24 18:35:50 +0000628 }
629}
630
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000631static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
632 static const SampleWindow::DeviceType gCT[] = {
kkinnunen8686a5e2016-01-05 00:04:32 -0800633 SampleWindow::kRaster_DeviceType
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000634#if SK_SUPPORT_GPU
kkinnunen8686a5e2016-01-05 00:04:32 -0800635 , SampleWindow::kGPU_DeviceType
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000636#if SK_ANGLE
kkinnunen8686a5e2016-01-05 00:04:32 -0800637 , SampleWindow::kANGLE_DeviceType
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000638#endif // SK_ANGLE
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000639#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +0000640 };
bungeman99fe8222015-08-20 07:57:51 -0700641 static_assert(SK_ARRAY_COUNT(gCT) == SampleWindow::kDeviceTypeCnt, "array_size_mismatch");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000642 return gCT[ct];
643}
644
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000645static SkString getSampleTitle(const SkViewFactory* sampleFactory) {
646 SkView* view = (*sampleFactory)();
647 SkString title;
648 SampleCode::RequestTitle(view, &title);
649 view->unref();
650 return title;
651}
652
edisonn@google.comde36b692013-07-11 15:40:31 +0000653static bool compareSampleTitle(const SkViewFactory* first, const SkViewFactory* second) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000654 return strcmp(getSampleTitle(first).c_str(), getSampleTitle(second).c_str()) < 0;
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000655}
656
reedb7ffe032015-11-05 11:59:17 -0800657static int find_by_title(const SkViewFactory* const* factories, int count, const char title[]) {
658 for (int i = 0; i < count; i++) {
659 if (getSampleTitle(factories[i]).equals(title)) {
660 return i;
661 }
662 }
663 return -1;
664}
665
666static void restrict_samples(SkTDArray<const SkViewFactory*>& factories, const SkString titles[],
667 int count) {
668 int newCount = 0;
669 for (int i = 0; i < count; ++i) {
670 int index = find_by_title(factories.begin(), factories.count(), titles[i].c_str());
671 if (index >= 0) {
672 SkTSwap(factories.begin()[newCount], factories.begin()[index]);
673 newCount += 1;
674 }
675 }
676 if (newCount) {
677 factories.setCount(newCount);
678 }
679}
680
mtkleinfd117d82014-06-19 14:18:56 -0700681DEFINE_string(slide, "", "Start on this sample.");
682DEFINE_int32(msaa, 0, "Request multisampling with this count.");
brianosman2d1ee792016-05-05 12:24:31 -0700683DEFINE_bool(deepColor, false, "Request deep color (10-bit/channel or more) display buffer.");
mtkleinfd117d82014-06-19 14:18:56 -0700684DEFINE_string(pictureDir, "", "Read pictures from here.");
685DEFINE_string(picture, "", "Path to single picture.");
fmalita6ceef3d2016-07-26 18:46:34 -0700686DEFINE_string(svg, "", "Path to single SVG file.");
687DEFINE_string(svgDir, "", "Read SVGs from here.");
reedb7ffe032015-11-05 11:59:17 -0800688DEFINE_string(sequence, "", "Path to file containing the desired samples/gms to show.");
mtkleinfd117d82014-06-19 14:18:56 -0700689DEFINE_bool(sort, false, "Sort samples by title.");
690DEFINE_bool(list, false, "List samples?");
jvanverth44dcb8a2015-10-02 09:12:05 -0700691DEFINE_bool(gpu, false, "Start up with gpu?");
brianosmanca348462016-02-24 11:47:47 -0800692DEFINE_bool(redraw, false, "Force continuous redrawing, for profiling or debugging tools.");
caryclark6531c362015-07-20 13:38:56 -0700693DEFINE_string(key, "", ""); // dummy to enable gm tests that have platform-specific names
mtkleinfd117d82014-06-19 14:18:56 -0700694#ifdef SAMPLE_PDF_FILE_VIEWER
695DEFINE_string(pdfPath, "", "Path to direcotry of pdf files.");
696#endif
697
reed89889b62014-10-29 12:36:45 -0700698#include "SkTaskGroup.h"
699
chudy@google.com4605a3f2012-08-01 17:58:01 +0000700SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager)
robertphillips@google.com4750fa52012-04-10 13:34:11 +0000701 : INHERITED(hwnd)
halcanary96fcdcc2015-08-27 07:41:13 -0700702 , fDevManager(nullptr) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000703
mtkleinfd117d82014-06-19 14:18:56 -0700704 SkCommandLineFlags::Parse(argc, argv);
705
reed@google.com7d05ac92013-06-06 13:14:13 +0000706 fCurrIndex = -1;
707
mtkleinfd117d82014-06-19 14:18:56 -0700708 if (!FLAGS_pictureDir.isEmpty()) {
709 SkOSFile::Iter iter(FLAGS_pictureDir[0], "skp");
710 SkString filename;
711 while (iter.next(&filename)) {
712 *fSamples.append() = new PictFileFactory(
tfarinaa8e2e152014-07-28 19:26:58 -0700713 SkOSPath::Join(FLAGS_pictureDir[0], filename.c_str()));
mtkleinfd117d82014-06-19 14:18:56 -0700714 }
715 }
716 if (!FLAGS_picture.isEmpty()) {
717 SkString path(FLAGS_picture[0]);
718 fCurrIndex = fSamples.count();
719 *fSamples.append() = new PictFileFactory(path);
720 }
fmalita6ceef3d2016-07-26 18:46:34 -0700721 if (!FLAGS_svg.isEmpty()) {
722 SkString path(FLAGS_svg[0]);
723 fCurrIndex = fSamples.count();
724 *fSamples.append() = new SVGFileFactory(path);
725 }
726 if (!FLAGS_svgDir.isEmpty()) {
727 SkOSFile::Iter iter(FLAGS_svgDir[0], "svg");
728 SkString filename;
729 while (iter.next(&filename)) {
730 *fSamples.append() = new SVGFileFactory(
731 SkOSPath::Join(FLAGS_svgDir[0], filename.c_str()));
732 }
733 }
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000734#ifdef SAMPLE_PDF_FILE_VIEWER
mtkleinfd117d82014-06-19 14:18:56 -0700735 if (!FLAGS_pdfPath.isEmpty()) {
736 SkOSFile::Iter iter(FLAGS_pdfPath[0], "pdf");
737 SkString filename;
738 while (iter.next(&filename)) {
739 *fSamples.append() = new PdfFileViewerFactory(
tfarinaa8e2e152014-07-28 19:26:58 -0700740 SkOSPath::Join(FLAGS_pictureDir[0], filename.c_str()));
mtkleinfd117d82014-06-19 14:18:56 -0700741 }
742 }
743#endif
robertphillips@google.com76d40212012-03-22 14:12:15 +0000744 SkGMRegistyToSampleRegistry();
745 {
746 const SkViewRegister* reg = SkViewRegister::Head();
747 while (reg) {
748 *fSamples.append() = reg->factory();
749 reg = reg->next();
750 }
751 }
752
reedb7ffe032015-11-05 11:59:17 -0800753 if (!FLAGS_sequence.isEmpty()) {
754 // The sequence file just contains a list (separated by CRs) of the samples or GM:gms
755 // you want to restrict to. Only these will appear when you cycle through.
756 // If none are found, or the file is empty, then it will be ignored, and all samples
757 // will be available.
758 SkFILEStream stream(FLAGS_sequence[0]);
759 if (stream.isValid()) {
760 size_t len = stream.getLength();
scroggo565901d2015-12-10 10:44:13 -0800761 SkAutoTMalloc<char> storage(len + 1);
762 char* buffer = storage.get();
reedb7ffe032015-11-05 11:59:17 -0800763 stream.read(buffer, len);
764 buffer[len] = 0;
765
766 SkTArray<SkString> titles;
767 SkStrSplit(buffer, "\n\r", &titles);
768 restrict_samples(fSamples, titles.begin(), titles.count());
769 }
770 }
771
mtkleinfd117d82014-06-19 14:18:56 -0700772 if (FLAGS_sort) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000773 // Sort samples, so foo.skp and foo.pdf are consecutive and we can quickly spot where
774 // skp -> pdf -> png fails.
halcanary96fcdcc2015-08-27 07:41:13 -0700775 SkTQSort(fSamples.begin(), fSamples.end() ? fSamples.end() - 1 : nullptr, compareSampleTitle);
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000776 }
777
mtkleinfd117d82014-06-19 14:18:56 -0700778 if (!FLAGS_slide.isEmpty()) {
779 fCurrIndex = findByTitle(FLAGS_slide[0]);
780 if (fCurrIndex < 0) {
781 fprintf(stderr, "Unknown sample \"%s\"\n", FLAGS_slide[0]);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000782 listTitles();
bsalomon@google.com11959252012-04-06 20:13:38 +0000783 }
mtkleinfd117d82014-06-19 14:18:56 -0700784 }
785
786 fMSAASampleCount = FLAGS_msaa;
brianosman2d1ee792016-05-05 12:24:31 -0700787 fDeepColor = FLAGS_deepColor;
mtkleinfd117d82014-06-19 14:18:56 -0700788
789 if (FLAGS_list) {
790 listTitles();
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000791 }
792
793 if (fCurrIndex < 0) {
794 SkString title;
795 if (readTitleFromPrefs(&title)) {
796 fCurrIndex = findByTitle(title.c_str());
797 }
798 }
799
800 if (fCurrIndex < 0) {
801 fCurrIndex = 0;
802 }
803
reed89889b62014-10-29 12:36:45 -0700804 static SkTaskGroup::Enabler enabled(-1);
reed@google.com3cec4d72011-07-06 13:59:47 +0000805 gSampleWindow = this;
806
robertphillips@google.combd8d7ad2012-03-30 15:18:14 +0000807 fDeviceType = kRaster_DeviceType;
jvanverth41518eb2015-10-02 10:42:36 -0700808#if SK_SUPPORT_GPU
jvanverth44dcb8a2015-10-02 09:12:05 -0700809 if (FLAGS_gpu) {
810 fDeviceType = kGPU_DeviceType;
811 }
jvanverth41518eb2015-10-02 10:42:36 -0700812#endif
halcanary9d524f22016-03-29 09:03:52 -0700813
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000814#if DEFAULT_TO_GPU
815 fDeviceType = kGPU_DeviceType;
reed@android.comf2b98d62010-12-20 18:26:13 +0000816#endif
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000817#if SK_ANGLE && DEFAULT_TO_ANGLE
818 fDeviceType = kANGLE_DeviceType;
819#endif
820
reed@android.com8a1c16f2008-12-17 15:59:43 +0000821 fUseClip = false;
reed91b4dfb2016-01-08 08:23:19 -0800822 fUsePicture = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000823 fAnimating = false;
reed@android.com6c5f6f22009-08-14 16:08:38 +0000824 fRotate = false;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000825 fPerspAnim = false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000826 fRequestGrabImage = false;
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000827 fTilingMode = kNo_Tiling;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +0000828 fMeasureFPS = false;
reedbabc3de2016-07-08 08:43:27 -0700829 fUseDeferredCanvas = false;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000830 fLCDState = SkOSMenu::kMixedState;
831 fAAState = SkOSMenu::kMixedState;
bungeman@google.com96aabc82013-06-03 21:26:34 +0000832 fSubpixelState = SkOSMenu::kMixedState;
833 fHintingState = 0;
bungeman5a59a422016-03-31 08:30:38 -0700834 fPixelGeometryIndex = 0;
reed93a12152015-03-16 10:08:34 -0700835 fFilterQualityIndex = 0;
reed@google.com569e0432011-04-05 13:07:03 +0000836 fFlipAxis = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000837
Scroggo0f185c22011-03-24 18:35:50 +0000838 fMouseX = fMouseY = 0;
mike@reedtribe.org3ce59dc2011-04-08 00:38:05 +0000839 fFatBitsScale = 8;
bungeman13b9c952016-05-12 10:09:30 -0700840 fTypeface = SkTypeface::MakeFromTypeface(nullptr, SkTypeface::kBold);
Scroggo0f185c22011-03-24 18:35:50 +0000841 fShowZoomer = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000842
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +0000843 fZoomLevel = 0;
844 fZoomScale = SK_Scalar1;
bungemance560262016-03-03 13:32:38 -0800845 fOffset = { 0, 0 };
chudy@google.com4605a3f2012-08-01 17:58:01 +0000846
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000847 fMagnify = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000848
Scroggo8ac0d542011-06-21 14:44:57 +0000849 fSaveToPdf = false;
reed093b4e82015-08-04 18:46:38 -0700850 fSaveToSKP = false;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000851
yangsu@google.com921091f2011-08-02 13:39:12 +0000852 int sinkID = this->getSinkID();
scroggo@google.com7dadc742012-04-18 14:07:57 +0000853 fAppMenu = new SkOSMenu;
854 fAppMenu->setTitle("Global Settings");
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000855 int itemID;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000856
reeda34be682016-02-15 07:48:35 -0800857 itemID = fAppMenu->appendList("ColorType", "ColorType", sinkID, 0,
858 gConfig[0].fName, gConfig[1].fName, gConfig[2].fName, nullptr);
859 fAppMenu->assignKeyEquivalentToItem(itemID, 'C');
860
kkinnunen8686a5e2016-01-05 00:04:32 -0800861 itemID = fAppMenu->appendList("Device Type", "Device Type", sinkID, 0,
862 "Raster",
863 "OpenGL",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000864#if SK_ANGLE
kkinnunen8686a5e2016-01-05 00:04:32 -0800865 "ANGLE",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000866#endif
kkinnunen8686a5e2016-01-05 00:04:32 -0800867 nullptr);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000868 fAppMenu->assignKeyEquivalentToItem(itemID, 'd');
869 itemID = fAppMenu->appendTriState("AA", "AA", sinkID, fAAState);
870 fAppMenu->assignKeyEquivalentToItem(itemID, 'b');
871 itemID = fAppMenu->appendTriState("LCD", "LCD", sinkID, fLCDState);
872 fAppMenu->assignKeyEquivalentToItem(itemID, 'l');
reed93a12152015-03-16 10:08:34 -0700873 itemID = fAppMenu->appendList("FilterQuality", "FilterQuality", sinkID, fFilterQualityIndex,
874 gFilterQualityStates[0].fName,
875 gFilterQualityStates[1].fName,
876 gFilterQualityStates[2].fName,
877 gFilterQualityStates[3].fName,
878 gFilterQualityStates[4].fName,
halcanary96fcdcc2015-08-27 07:41:13 -0700879 nullptr);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000880 fAppMenu->assignKeyEquivalentToItem(itemID, 'n');
bungeman@google.com96aabc82013-06-03 21:26:34 +0000881 itemID = fAppMenu->appendTriState("Subpixel", "Subpixel", sinkID, fSubpixelState);
882 fAppMenu->assignKeyEquivalentToItem(itemID, 's');
883 itemID = fAppMenu->appendList("Hinting", "Hinting", sinkID, fHintingState,
884 gHintingStates[0].name,
885 gHintingStates[1].name,
886 gHintingStates[2].name,
887 gHintingStates[3].name,
bungeman@google.coma9133bb2013-06-04 17:49:20 +0000888 gHintingStates[4].name,
halcanary96fcdcc2015-08-27 07:41:13 -0700889 nullptr);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000890 fAppMenu->assignKeyEquivalentToItem(itemID, 'h');
reed@google.com67b89ee2012-08-15 14:41:58 +0000891
bungeman5a59a422016-03-31 08:30:38 -0700892 itemID = fAppMenu->appendList("Pixel Geometry", "Pixel Geometry", sinkID, fPixelGeometryIndex,
893 gPixelGeometryStates[0].name,
894 gPixelGeometryStates[1].name,
895 gPixelGeometryStates[2].name,
896 gPixelGeometryStates[3].name,
897 gPixelGeometryStates[4].name,
898 gPixelGeometryStates[5].name,
899 nullptr);
900 fAppMenu->assignKeyEquivalentToItem(itemID, 'P');
901
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +0000902 itemID =fAppMenu->appendList("Tiling", "Tiling", sinkID, fTilingMode,
903 gTilingInfo[kNo_Tiling].label,
904 gTilingInfo[kAbs_128x128_Tiling].label,
905 gTilingInfo[kAbs_256x256_Tiling].label,
906 gTilingInfo[kRel_4x4_Tiling].label,
907 gTilingInfo[kRel_1x16_Tiling].label,
908 gTilingInfo[kRel_16x1_Tiling].label,
halcanary96fcdcc2015-08-27 07:41:13 -0700909 nullptr);
reed@google.com67b89ee2012-08-15 14:41:58 +0000910 fAppMenu->assignKeyEquivalentToItem(itemID, 't');
911
chudy@google.com4605a3f2012-08-01 17:58:01 +0000912 itemID = fAppMenu->appendSwitch("Slide Show", "Slide Show" , sinkID, false);
913 fAppMenu->assignKeyEquivalentToItem(itemID, 'a');
914 itemID = fAppMenu->appendSwitch("Clip", "Clip" , sinkID, fUseClip);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000915 fAppMenu->assignKeyEquivalentToItem(itemID, 'c');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000916 itemID = fAppMenu->appendSwitch("Flip X", "Flip X" , sinkID, false);
scroggo@google.com7dadc742012-04-18 14:07:57 +0000917 fAppMenu->assignKeyEquivalentToItem(itemID, 'x');
918 itemID = fAppMenu->appendSwitch("Flip Y", "Flip Y" , sinkID, false);
919 fAppMenu->assignKeyEquivalentToItem(itemID, 'y');
920 itemID = fAppMenu->appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
921 fAppMenu->assignKeyEquivalentToItem(itemID, 'z');
922 itemID = fAppMenu->appendSwitch("Magnify", "Magnify" , sinkID, fMagnify);
923 fAppMenu->assignKeyEquivalentToItem(itemID, 'm');
reed093b4e82015-08-04 18:46:38 -0700924
scroggo@google.com7dadc742012-04-18 14:07:57 +0000925 itemID = fAppMenu->appendAction("Save to PDF", sinkID);
926 fAppMenu->assignKeyEquivalentToItem(itemID, 'e');
chudy@google.com4605a3f2012-08-01 17:58:01 +0000927
scroggo@google.com7dadc742012-04-18 14:07:57 +0000928 this->addMenu(fAppMenu);
929 fSlideMenu = new SkOSMenu;
930 this->addMenu(fSlideMenu);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000931
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000932 this->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +0000933 this->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000934
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000935 this->loadView((*fSamples[fCurrIndex])());
chudy@google.com4605a3f2012-08-01 17:58:01 +0000936
halcanary96fcdcc2015-08-27 07:41:13 -0700937 if (nullptr == devManager) {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000938 fDevManager = new DefaultDeviceManager();
939 } else {
940 devManager->ref();
941 fDevManager = devManager;
942 }
brianosman2d1ee792016-05-05 12:24:31 -0700943 fDevManager->setUpBackend(this, fMSAASampleCount, fDeepColor);
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000944
Scroggob4490c72011-06-17 13:53:05 +0000945 // If another constructor set our dimensions, ensure that our
946 // onSizeChange gets called.
947 if (this->height() && this->width()) {
948 this->onSizeChange();
949 }
reed@google.com2072db82011-11-08 15:18:10 +0000950
951 // can't call this synchronously, since it may require a subclass to
952 // to implement, or the caller may need us to have returned from the
953 // constructor first. Hence we post an event to ourselves.
954// this->updateTitle();
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +0000955 post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this);
reed76113a92015-02-02 12:55:02 -0800956
957 gAnimTimer.run();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000958}
959
960SampleWindow::~SampleWindow() {
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000961 SkSafeUnref(fDevManager);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000962}
963
edisonn@google.comf8b6b012013-07-11 14:28:04 +0000964
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000965int SampleWindow::findByTitle(const char title[]) {
966 int i, count = fSamples.count();
967 for (i = 0; i < count; i++) {
968 if (getSampleTitle(i).equals(title)) {
969 return i;
970 }
971 }
mike@reedtribe.orgdd52caa2011-12-28 20:02:10 +0000972 return -1;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000973}
974
robertphillips@google.com7265e722012-05-03 18:22:28 +0000975void SampleWindow::listTitles() {
976 int count = fSamples.count();
977 SkDebugf("All Slides:\n");
978 for (int i = 0; i < count; i++) {
979 SkDebugf(" %s\n", getSampleTitle(i).c_str());
980 }
981}
982
reed@android.com55e76b22009-11-23 21:46:47 +0000983static SkBitmap capture_bitmap(SkCanvas* canvas) {
984 SkBitmap bm;
reed84825042014-09-02 12:50:45 -0700985 if (bm.tryAllocPixels(canvas->imageInfo())) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000986 canvas->readPixels(&bm, 0, 0);
987 }
reed@android.com55e76b22009-11-23 21:46:47 +0000988 return bm;
989}
990
reedf7f79d22015-02-21 09:03:20 -0800991static void drawText(SkCanvas* canvas, SkString str, SkScalar left, SkScalar top, SkPaint& paint) {
Scroggo0f185c22011-03-24 18:35:50 +0000992 SkColor desiredColor = paint.getColor();
993 paint.setColor(SK_ColorWHITE);
reedf7f79d22015-02-21 09:03:20 -0800994 const char* c_str = str.c_str();
995 size_t size = str.size();
Scroggo0f185c22011-03-24 18:35:50 +0000996 SkRect bounds;
997 paint.measureText(c_str, size, &bounds);
998 bounds.offset(left, top);
999 SkScalar inset = SkIntToScalar(-2);
1000 bounds.inset(inset, inset);
1001 canvas->drawRect(bounds, paint);
1002 if (desiredColor != SK_ColorBLACK) {
1003 paint.setColor(SK_ColorBLACK);
1004 canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
1005 }
1006 paint.setColor(desiredColor);
1007 canvas->drawText(c_str, size, left, top, paint);
1008}
1009
reed@android.com44177402009-11-23 21:07:51 +00001010#define XCLIP_N 8
1011#define YCLIP_N 8
reed@android.come522ca52009-11-23 20:10:41 +00001012
reedbabc3de2016-07-08 08:43:27 -07001013#include "SkDeferredCanvas.h"
1014#include "SkDumpCanvas.h"
1015
reed@android.come522ca52009-11-23 20:10:41 +00001016void SampleWindow::draw(SkCanvas* canvas) {
reed76113a92015-02-02 12:55:02 -08001017 gAnimTimer.updateTime();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001018
reed@google.comf03bb562011-11-11 21:42:12 +00001019 if (fGesture.isActive()) {
1020 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001021 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001022
djsollen@google.com796763e2012-12-10 14:12:55 +00001023 if (fMeasureFPS) {
1024 fMeasureFPS_Time = 0;
1025 }
1026
reedf7f79d22015-02-21 09:03:20 -08001027 SkSize tile = this->tileSize();
reed@android.come522ca52009-11-23 20:10:41 +00001028
reedf7f79d22015-02-21 09:03:20 -08001029 if (kNo_Tiling == fTilingMode) {
reedbabc3de2016-07-08 08:43:27 -07001030 SkDebugfDumper dumper;
1031 SkDumpCanvas dump(&dumper);
1032 SkDeferredCanvas deferred(canvas);
1033 SkCanvas* c = fUseDeferredCanvas ? &deferred : canvas;
1034 this->INHERITED::draw(c); // no looping or surfaces needed
reed@android.come522ca52009-11-23 20:10:41 +00001035 } else {
bungemance560262016-03-03 13:32:38 -08001036 const SkScalar w = SkScalarCeilToScalar(tile.width());
1037 const SkScalar h = SkScalarCeilToScalar(tile.height());
1038 SkImageInfo info = SkImageInfo::MakeN32Premul(SkScalarTruncToInt(w), SkScalarTruncToInt(h));
reede8f30622016-03-23 18:59:25 -07001039 auto surface(canvas->makeSurface(info));
reedf7f79d22015-02-21 09:03:20 -08001040 SkCanvas* tileCanvas = surface->getCanvas();
rmistry@google.comae933ce2012-08-23 18:19:56 +00001041
bungemance560262016-03-03 13:32:38 -08001042 for (SkScalar y = 0; y < height(); y += h) {
1043 for (SkScalar x = 0; x < width(); x += w) {
reedf7f79d22015-02-21 09:03:20 -08001044 SkAutoCanvasRestore acr(tileCanvas, true);
1045 tileCanvas->translate(-x, -y);
1046 tileCanvas->clear(0);
1047 this->INHERITED::draw(tileCanvas);
halcanary96fcdcc2015-08-27 07:41:13 -07001048 surface->draw(canvas, x, y, nullptr);
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +00001049 }
reedf7f79d22015-02-21 09:03:20 -08001050 }
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +00001051
reedf7f79d22015-02-21 09:03:20 -08001052 // for drawing the borders between tiles
1053 SkPaint paint;
1054 paint.setColor(0x60FF00FF);
1055 paint.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00001056
reedf7f79d22015-02-21 09:03:20 -08001057 for (SkScalar y = 0; y < height(); y += tile.height()) {
1058 for (SkScalar x = 0; x < width(); x += tile.width()) {
1059 canvas->drawRect(SkRect::MakeXYWH(x, y, tile.width(), tile.height()), paint);
scroggo@google.com85cade02012-08-17 13:29:50 +00001060 }
1061 }
reed@android.come522ca52009-11-23 20:10:41 +00001062 }
reedf7f79d22015-02-21 09:03:20 -08001063
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001064 if (fShowZoomer && !fSaveToPdf) {
Scroggo3272ba82011-05-25 20:50:42 +00001065 showZoomer(canvas);
1066 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001067 if (fMagnify && !fSaveToPdf) {
1068 magnify(canvas);
1069 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001070
djsollen@google.com796763e2012-12-10 14:12:55 +00001071 if (fMeasureFPS && fMeasureFPS_Time) {
1072 this->updateTitle();
1073 this->postInvalDelay();
1074 }
1075
brianosmanca348462016-02-24 11:47:47 -08001076 if (this->sendAnimatePulse() || FLAGS_redraw) {
halcanary96fcdcc2015-08-27 07:41:13 -07001077 this->inval(nullptr);
reedd9adfe62015-02-01 19:01:04 -08001078 }
1079
reed@google.com29038ed2011-07-06 17:56:47 +00001080 // do this last
bsalomon@google.com098e96d2011-07-14 14:30:46 +00001081 fDevManager->publishCanvas(fDeviceType, canvas, this);
Scroggo3272ba82011-05-25 20:50:42 +00001082}
1083
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001084static float clipW = 200;
1085static float clipH = 200;
1086void SampleWindow::magnify(SkCanvas* canvas) {
1087 SkRect r;
1088 int count = canvas->save();
chudy@google.com4605a3f2012-08-01 17:58:01 +00001089
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001090 SkMatrix m = canvas->getTotalMatrix();
robertphillips@google.com07ef9112012-06-04 13:22:14 +00001091 if (!m.invert(&m)) {
1092 return;
1093 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001094 SkPoint offset, center;
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001095 SkScalar mouseX = fMouseX * SK_Scalar1;
1096 SkScalar mouseY = fMouseY * SK_Scalar1;
1097 m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset);
1098 m.mapXY(mouseX, mouseY, &center);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001099
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001100 r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX());
1101 r.offset(offset.fX, offset.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001102
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001103 SkPaint paint;
1104 paint.setColor(0xFF66AAEE);
1105 paint.setStyle(SkPaint::kStroke_Style);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001106 paint.setStrokeWidth(10.f * m.getScaleX());
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001107 //lense offset
1108 //canvas->translate(0, -250);
1109 canvas->drawRect(r, paint);
1110 canvas->clipRect(r);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001111
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001112 m = canvas->getTotalMatrix();
1113 m.setTranslate(-center.fX, -center.fY);
bsalomon@google.com820e80a2011-10-24 21:09:40 +00001114 m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001115 m.postTranslate(center.fX, center.fY);
1116 canvas->concat(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001117
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001118 this->INHERITED::draw(canvas);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001119
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001120 canvas->restoreToCount(count);
1121}
1122
reed5f629b42015-08-15 11:57:23 -07001123static SkPaint& set_color_ref(SkPaint& paint, SkColor c) {
1124 paint.setColor(c);
1125 return paint;
1126}
reed@google.comb36334d2011-05-18 15:07:20 +00001127
reed5f629b42015-08-15 11:57:23 -07001128static void show_lcd_box(SkCanvas* canvas, SkScalar x, SkScalar y, SkColor c,
1129 SkScalar sx, SkScalar sy) {
1130 const SkScalar w = (1 - 1/sx) / 3;
1131 SkPaint paint;
1132 SkRect r = SkRect::MakeXYWH(x, y, w, 1 - 1/sy);
1133 canvas->drawRect(r, set_color_ref(paint, SkColorSetRGB(SkColorGetR(c), 0, 0)));
1134 r.offset(w, 0);
1135 canvas->drawRect(r, set_color_ref(paint, SkColorSetRGB(0, SkColorGetG(c), 0)));
1136 r.offset(w, 0);
1137 canvas->drawRect(r, set_color_ref(paint, SkColorSetRGB(0, 0, SkColorGetB(c))));
1138}
reed@google.comb36334d2011-05-18 15:07:20 +00001139
reed5f629b42015-08-15 11:57:23 -07001140static void show_lcd_circle(SkCanvas* canvas, SkScalar x, SkScalar y, SkColor c,
1141 SkScalar, SkScalar) {
1142 const SkRect r = SkRect::MakeXYWH(x, y, 1, 1);
1143 const SkScalar cx = x + 0.5f;
1144 const SkScalar cy = y + 0.5f;
1145
1146 SkPaint paint;
1147 paint.setAntiAlias(true);
1148
1149 SkPath path;
1150 path.addArc(r, 0, 120); path.lineTo(cx, cy);
1151 canvas->drawPath(path, set_color_ref(paint, SkColorSetRGB(SkColorGetR(c), 0, 0)));
1152
1153 path.reset(); path.addArc(r, 120, 120); path.lineTo(cx, cy);
1154 canvas->drawPath(path, set_color_ref(paint, SkColorSetRGB(0, SkColorGetG(c), 0)));
1155
1156 path.reset(); path.addArc(r, 240, 120); path.lineTo(cx, cy);
1157 canvas->drawPath(path, set_color_ref(paint, SkColorSetRGB(0, 0, SkColorGetB(c))));
1158}
1159
1160typedef void (*ShowLCDProc)(SkCanvas*, SkScalar, SkScalar, SkColor, SkScalar, SkScalar);
1161
1162/*
1163 * Like drawBitmapRect but we manually draw each pixels in RGB
1164 */
1165static void show_lcd_grid(SkCanvas* canvas, const SkBitmap& bitmap,
reed8a41adb2015-08-15 14:02:29 -07001166 const SkIRect& origSrc, const SkRect& dst, ShowLCDProc proc) {
reed5f629b42015-08-15 11:57:23 -07001167 SkIRect src;
1168 if (!src.intersect(origSrc, bitmap.bounds())) {
1169 return;
1170 }
1171 const SkScalar sx = dst.width() / src.width();
1172 const SkScalar sy = dst.height() / src.height();
1173
1174 SkAutoCanvasRestore acr(canvas, true);
1175 canvas->translate(dst.left(), dst.top());
1176 canvas->scale(sx, sy);
1177
reed5f629b42015-08-15 11:57:23 -07001178 for (int y = 0; y < src.height(); ++y) {
1179 for (int x = 0; x < src.width(); ++x) {
1180 proc(canvas, SkIntToScalar(x), SkIntToScalar(y),
1181 bitmap.getColor(src.left() + x, src.top() + y), sx, sy);
Scroggo0f185c22011-03-24 18:35:50 +00001182 }
reed5f629b42015-08-15 11:57:23 -07001183 }
1184}
1185
1186void SampleWindow::showZoomer(SkCanvas* canvas) {
1187 int count = canvas->save();
1188 canvas->resetMatrix();
1189 // Ensure the mouse position is on screen.
1190 int width = SkScalarRoundToInt(this->width());
1191 int height = SkScalarRoundToInt(this->height());
1192 if (fMouseX >= width) fMouseX = width - 1;
1193 else if (fMouseX < 0) fMouseX = 0;
1194 if (fMouseY >= height) fMouseY = height - 1;
1195 else if (fMouseY < 0) fMouseY = 0;
1196
1197 SkBitmap bitmap = capture_bitmap(canvas);
1198 bitmap.lockPixels();
1199
1200 // Find the size of the zoomed in view, forced to be odd, so the examined pixel is in the middle.
1201 int zoomedWidth = (width >> 1) | 1;
1202 int zoomedHeight = (height >> 1) | 1;
1203 SkIRect src;
1204 src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
1205 src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
1206 SkRect dest;
1207 dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
1208 dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
1209 SkPaint paint;
1210 // Clear the background behind our zoomed in view
1211 paint.setColor(SK_ColorWHITE);
1212 canvas->drawRect(dest, paint);
reed8a41adb2015-08-15 14:02:29 -07001213 switch (fFatBitsScale) {
1214 case kMaxFatBitsScale:
1215 show_lcd_grid(canvas, bitmap, src, dest, show_lcd_box);
1216 break;
1217 case kMaxFatBitsScale - 1:
1218 show_lcd_grid(canvas, bitmap, src, dest, show_lcd_circle);
1219 break;
1220 default:
halcanary96fcdcc2015-08-27 07:41:13 -07001221 canvas->drawBitmapRect(bitmap, src, dest, nullptr);
reed8a41adb2015-08-15 14:02:29 -07001222 break;
reed5f629b42015-08-15 11:57:23 -07001223 }
1224
1225 paint.setColor(SK_ColorBLACK);
1226 paint.setStyle(SkPaint::kStroke_Style);
1227 // Draw a border around the pixel in the middle
1228 SkRect originalPixel;
1229 originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
1230 SkMatrix matrix;
1231 SkRect scalarSrc;
1232 scalarSrc.set(src);
1233 SkColor color = bitmap.getColor(fMouseX, fMouseY);
1234 if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
1235 SkRect pixel;
1236 matrix.mapRect(&pixel, originalPixel);
1237 // TODO Perhaps measure the values and make the outline white if it's "dark"
1238 if (color == SK_ColorBLACK) {
1239 paint.setColor(SK_ColorWHITE);
1240 }
1241 canvas->drawRect(pixel, paint);
1242 }
1243 paint.setColor(SK_ColorBLACK);
1244 // Draw a border around the destination rectangle
1245 canvas->drawRect(dest, paint);
1246 paint.setStyle(SkPaint::kStrokeAndFill_Style);
1247 // Identify the pixel and its color on screen
1248 paint.setTypeface(fTypeface);
1249 paint.setAntiAlias(true);
halcanary96fcdcc2015-08-27 07:41:13 -07001250 SkScalar lineHeight = paint.getFontMetrics(nullptr);
reed5f629b42015-08-15 11:57:23 -07001251 SkString string;
1252 string.appendf("(%i, %i)", fMouseX, fMouseY);
1253 SkScalar left = dest.fLeft + SkIntToScalar(3);
1254 SkScalar i = SK_Scalar1;
1255 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1256 // Alpha
1257 i += SK_Scalar1;
1258 string.reset();
1259 string.appendf("A: %X", SkColorGetA(color));
1260 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1261 // Red
1262 i += SK_Scalar1;
1263 string.reset();
1264 string.appendf("R: %X", SkColorGetR(color));
1265 paint.setColor(SK_ColorRED);
1266 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1267 // Green
1268 i += SK_Scalar1;
1269 string.reset();
1270 string.appendf("G: %X", SkColorGetG(color));
1271 paint.setColor(SK_ColorGREEN);
1272 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1273 // Blue
1274 i += SK_Scalar1;
1275 string.reset();
1276 string.appendf("B: %X", SkColorGetB(color));
1277 paint.setColor(SK_ColorBLUE);
1278 drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
1279 canvas->restoreToCount(count);
reed@android.come522ca52009-11-23 20:10:41 +00001280}
1281
reed@android.com8a1c16f2008-12-17 15:59:43 +00001282void SampleWindow::onDraw(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001283}
1284
1285#include "SkColorPriv.h"
1286
Scroggo8ac0d542011-06-21 14:44:57 +00001287void SampleWindow::saveToPdf()
1288{
1289 fSaveToPdf = true;
halcanary96fcdcc2015-08-27 07:41:13 -07001290 this->inval(nullptr);
Scroggo8ac0d542011-06-21 14:44:57 +00001291}
1292
reed@android.com8a1c16f2008-12-17 15:59:43 +00001293SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
Scroggo8ac0d542011-06-21 14:44:57 +00001294 if (fSaveToPdf) {
reedddb5eca2014-10-08 11:10:51 -07001295 SkString name;
1296 if (!this->getRawTitle(&name)) {
1297 name.set("unknown_sample");
1298 }
1299 name.append(".pdf");
1300#ifdef SK_BUILD_FOR_ANDROID
1301 name.prepend("/sdcard/");
1302#endif
halcanary676ab682016-05-03 12:10:04 -07001303 fPDFDocument = SkDocument::MakePDF(name.c_str());
reedddb5eca2014-10-08 11:10:51 -07001304 canvas = fPDFDocument->beginPage(this->width(), this->height());
kkinnunen8686a5e2016-01-05 00:04:32 -08001305 } else if (fSaveToSKP) {
1306 canvas = fRecorder.beginRecording(9999, 9999, nullptr, 0);
reed91b4dfb2016-01-08 08:23:19 -08001307 } else if (fUsePicture) {
halcanary96fcdcc2015-08-27 07:41:13 -07001308 canvas = fRecorder.beginRecording(9999, 9999, nullptr, 0);
Scroggo8ac0d542011-06-21 14:44:57 +00001309 } else {
bsalomonbb0502e2015-02-11 11:11:11 -08001310 canvas = this->INHERITED::beforeChildren(canvas);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001311 }
1312
1313 if (fUseClip) {
1314 canvas->drawColor(0xFFFF88FF);
reed@google.com045e62d2011-10-24 12:19:46 +00001315 canvas->clipPath(fClipPath, SkRegion::kIntersect_Op, true);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001316 }
1317
fmalita822ace92015-06-15 07:07:32 -07001318 // Install a flags filter proxy canvas if needed
1319 if (fLCDState != SkOSMenu::kMixedState ||
1320 fAAState != SkOSMenu::kMixedState ||
1321 fSubpixelState != SkOSMenu::kMixedState ||
1322 fHintingState > 0 ||
1323 fFilterQualityIndex > 0) {
halcanary385fe4d2015-08-26 13:07:48 -07001324 canvas = new FlagsFilterCanvas(canvas, fLCDState, fAAState, fSubpixelState, fHintingState,
1325 fFilterQualityIndex);
fmalita822ace92015-06-15 07:07:32 -07001326 fFlagsFilterCanvas.reset(canvas);
1327 }
1328
reed@android.com8a1c16f2008-12-17 15:59:43 +00001329 return canvas;
1330}
reed89889b62014-10-29 12:36:45 -07001331#include "SkMultiPictureDraw.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001332void SampleWindow::afterChildren(SkCanvas* orig) {
halcanary96fcdcc2015-08-27 07:41:13 -07001333 fFlagsFilterCanvas.reset(nullptr);
fmalita822ace92015-06-15 07:07:32 -07001334
Scroggo8ac0d542011-06-21 14:44:57 +00001335 if (fSaveToPdf) {
1336 fSaveToPdf = false;
reedddb5eca2014-10-08 11:10:51 -07001337 fPDFDocument->endPage();
halcanary96fcdcc2015-08-27 07:41:13 -07001338 fPDFDocument.reset(nullptr);
Scroggo8ac0d542011-06-21 14:44:57 +00001339 // We took over the draw calls in order to create the PDF, so we need
1340 // to redraw.
halcanary96fcdcc2015-08-27 07:41:13 -07001341 this->inval(nullptr);
Scroggo8ac0d542011-06-21 14:44:57 +00001342 return;
1343 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001344
reed@android.comf2b98d62010-12-20 18:26:13 +00001345 if (fRequestGrabImage) {
1346 fRequestGrabImage = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001347
reed52d9ac62014-06-30 09:05:34 -07001348 SkBitmap bmp = capture_bitmap(orig);
1349 if (!bmp.isNull()) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001350 static int gSampleGrabCounter;
1351 SkString name;
reed@google.com7c57e0e2012-09-08 21:20:33 +00001352 name.printf("sample_grab_%d.png", gSampleGrabCounter++);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001353 SkImageEncoder::EncodeFile(name.c_str(), bmp,
reed@android.comf2b98d62010-12-20 18:26:13 +00001354 SkImageEncoder::kPNG_Type, 100);
1355 }
kkinnunen8686a5e2016-01-05 00:04:32 -08001356 this->inval(nullptr);
1357 return;
reed@android.comf2b98d62010-12-20 18:26:13 +00001358 }
1359
kkinnunen8686a5e2016-01-05 00:04:32 -08001360 if (fSaveToSKP) {
reedca2622b2016-03-18 07:25:55 -07001361 sk_sp<SkPicture> picture(fRecorder.finishRecordingAsPicture());
kkinnunen8686a5e2016-01-05 00:04:32 -08001362 SkFILEWStream stream("sample_app.skp");
1363 picture->serialize(&stream);
1364 fSaveToSKP = false;
1365 this->inval(nullptr);
1366 return;
1367 }
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001368
reed91b4dfb2016-01-08 08:23:19 -08001369 if (fUsePicture) {
reedca2622b2016-03-18 07:25:55 -07001370 sk_sp<SkPicture> picture(fRecorder.finishRecordingAsPicture());
reedbfd5f172016-01-07 11:28:08 -08001371
reed91b4dfb2016-01-08 08:23:19 -08001372 // serialize/deserialize?
reedbfd5f172016-01-07 11:28:08 -08001373 if (false) {
1374 SkDynamicMemoryWStream wstream;
1375 picture->serialize(&wstream);
1376
1377 SkAutoTDelete<SkStream> rstream(wstream.detachAsStream());
reedca2622b2016-03-18 07:25:55 -07001378 picture = SkPicture::MakeFromStream(rstream);
reedbfd5f172016-01-07 11:28:08 -08001379 }
reedca2622b2016-03-18 07:25:55 -07001380 orig->drawPicture(picture.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001381 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001382
reed@google.com17d7aec2011-04-25 14:31:44 +00001383 // Do this after presentGL and other finishing, rather than in afterChild
jvanverth4d0d81a2015-08-11 11:07:07 -07001384 if (fMeasureFPS) {
reedb2ce0b02015-11-09 13:10:30 -08001385 orig->flush();
jvanverth4d0d81a2015-08-11 11:07:07 -07001386 fTimer.end();
1387 fMeasureFPS_Time += fTimer.fWall;
reed@google.com17d7aec2011-04-25 14:31:44 +00001388 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001389}
1390
reed@android.com6c5f6f22009-08-14 16:08:38 +00001391void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
reed@android.com6c5f6f22009-08-14 16:08:38 +00001392 if (fRotate) {
1393 SkScalar cx = this->width() / 2;
1394 SkScalar cy = this->height() / 2;
bungeman7438bfc2016-07-12 15:01:19 -07001395 canvas->rotate(gAnimTimer.scaled(10), cx, cy);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001396 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001397
bsalomon@google.come8f09102011-09-08 18:48:12 +00001398 if (fPerspAnim) {
reed76113a92015-02-02 12:55:02 -08001399 SkScalar secs = gAnimTimer.scaled(1);
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001400
1401 static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
1402 static const SkScalar gAnimMag = SK_Scalar1 / 1000;
reed76113a92015-02-02 12:55:02 -08001403 SkScalar t = SkScalarMod(secs, gAnimPeriod);
reed80ea19c2015-05-12 10:37:34 -07001404 if (SkScalarFloorToInt(secs / gAnimPeriod) & 0x1) {
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001405 t = gAnimPeriod - t;
1406 }
1407 t = 2 * t - gAnimPeriod;
reed80ea19c2015-05-12 10:37:34 -07001408 t *= gAnimMag / gAnimPeriod;
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001409 SkMatrix m;
1410 m.reset();
reed5d9ab282014-09-19 13:04:17 -07001411#if 1
bsalomon@google.com6fb736f2011-09-16 18:51:57 +00001412 m.setPerspY(t);
reed5d9ab282014-09-19 13:04:17 -07001413#else
1414 m.setPerspY(SK_Scalar1 / 1000);
reed80ea19c2015-05-12 10:37:34 -07001415 m.setSkewX(8.0f / 25);
reed5d9ab282014-09-19 13:04:17 -07001416 m.dump();
1417#endif
bsalomon@google.come8f09102011-09-08 18:48:12 +00001418 canvas->concat(m);
1419 }
reed@google.comf0b5f682011-03-11 20:08:25 +00001420
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001421 if (fMeasureFPS) {
jvanverth4d0d81a2015-08-11 11:07:07 -07001422 (void)SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT);
1423 fTimer.start();
reed@google.comf2183392011-04-22 14:10:48 +00001424 } else {
1425 (void)SampleView::SetRepeatDraw(child, 1);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001426 }
bungeman@google.comb1785912013-07-09 21:58:56 +00001427 if (fPerspAnim || fRotate) {
halcanary96fcdcc2015-08-27 07:41:13 -07001428 this->inval(nullptr);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001429 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00001430}
1431
bungemance560262016-03-03 13:32:38 -08001432void SampleWindow::changeOffset(SkVector delta) {
1433 fOffset += delta;
1434 this->updateMatrix();
1435}
1436
djsollen@google.come32b5832011-06-13 16:58:40 +00001437void SampleWindow::changeZoomLevel(float delta) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001438 fZoomLevel += delta;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001439 if (fZoomLevel > 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001440 fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
1441 fZoomScale = fZoomLevel + SK_Scalar1;
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001442 } else if (fZoomLevel < 0) {
djsollen@google.come32b5832011-06-13 16:58:40 +00001443 fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
1444 fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001445 } else {
1446 fZoomScale = SK_Scalar1;
1447 }
reed@google.comf03bb562011-11-11 21:42:12 +00001448 this->updateMatrix();
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001449}
1450
reed@google.comf03bb562011-11-11 21:42:12 +00001451void SampleWindow::updateMatrix(){
1452 SkMatrix m;
1453 m.reset();
bungemance560262016-03-03 13:32:38 -08001454
reed@google.comf03bb562011-11-11 21:42:12 +00001455 if (fZoomLevel) {
1456 SkPoint center;
1457 //m = this->getLocalMatrix();//.invert(&m);
1458 m.mapXY(fZoomCenterX, fZoomCenterY, &center);
1459 SkScalar cx = center.fX;
1460 SkScalar cy = center.fY;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001461
reed@google.comf03bb562011-11-11 21:42:12 +00001462 m.setTranslate(-cx, -cy);
1463 m.postScale(fZoomScale, fZoomScale);
1464 m.postTranslate(cx, cy);
1465 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001466
bungemance560262016-03-03 13:32:38 -08001467 m.postTranslate(fOffset.fX, fOffset.fY);
1468
reed@google.comf03bb562011-11-11 21:42:12 +00001469 if (fFlipAxis) {
1470 m.preTranslate(fZoomCenterX, fZoomCenterY);
1471 if (fFlipAxis & kFlipAxis_X) {
1472 m.preScale(-SK_Scalar1, SK_Scalar1);
1473 }
1474 if (fFlipAxis & kFlipAxis_Y) {
1475 m.preScale(SK_Scalar1, -SK_Scalar1);
1476 }
1477 m.preTranslate(-fZoomCenterX, -fZoomCenterY);
1478 //canvas->concat(m);
1479 }
1480 // Apply any gesture matrix
1481 m.preConcat(fGesture.localM());
1482 m.preConcat(fGesture.globalM());
chudy@google.com4605a3f2012-08-01 17:58:01 +00001483
reed@google.comf03bb562011-11-11 21:42:12 +00001484 this->setLocalMatrix(m);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001485
reed@google.comf03bb562011-11-11 21:42:12 +00001486 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001487 this->inval(nullptr);
reed@google.comf03bb562011-11-11 21:42:12 +00001488}
Scroggo2c8208f2011-06-15 16:49:08 +00001489bool SampleWindow::previousSample() {
Scroggo62b65b02011-06-21 16:01:26 +00001490 fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
reed63121f02015-01-13 19:43:04 -08001491 this->loadView((*fSamples[fCurrIndex])());
Scroggo2c8208f2011-06-15 16:49:08 +00001492 return true;
1493}
1494
reed216b6432015-08-19 12:25:40 -07001495#include "SkResourceCache.h"
1496#include "SkGlyphCache.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00001497bool SampleWindow::nextSample() {
reed@android.com34245c72009-11-03 04:00:48 +00001498 fCurrIndex = (fCurrIndex + 1) % fSamples.count();
reed63121f02015-01-13 19:43:04 -08001499 this->loadView((*fSamples[fCurrIndex])());
reed216b6432015-08-19 12:25:40 -07001500
1501 if (false) {
1502 SkResourceCache::TestDumpMemoryStatistics();
1503 SkGlyphCache::Dump();
1504 SkDebugf("\n");
1505 }
1506
reed@android.com34245c72009-11-03 04:00:48 +00001507 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001508}
1509
yangsu@google.com501775e2011-06-24 16:04:50 +00001510bool SampleWindow::goToSample(int i) {
1511 fCurrIndex = (i) % fSamples.count();
reed63121f02015-01-13 19:43:04 -08001512 this->loadView((*fSamples[fCurrIndex])());
yangsu@google.com501775e2011-06-24 16:04:50 +00001513 return true;
1514}
1515
1516SkString SampleWindow::getSampleTitle(int i) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001517 return ::getSampleTitle(fSamples[i]);
yangsu@google.com501775e2011-06-24 16:04:50 +00001518}
1519
1520int SampleWindow::sampleCount() {
1521 return fSamples.count();
1522}
1523
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001524void SampleWindow::showOverview() {
reed63121f02015-01-13 19:43:04 -08001525 this->loadView(create_overview(fSamples.count(), fSamples.begin()));
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001526}
1527
Scroggo2c8208f2011-06-15 16:49:08 +00001528void SampleWindow::postAnimatingEvent() {
1529 if (fAnimating) {
reed@google.com87fac4a2011-08-04 13:50:17 +00001530 (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
Scroggo2c8208f2011-06-15 16:49:08 +00001531 }
1532}
reed@google.come23f1942011-12-08 19:36:00 +00001533
reed@android.com8a1c16f2008-12-17 15:59:43 +00001534bool SampleWindow::onEvent(const SkEvent& evt) {
reed@google.com2072db82011-11-08 15:18:10 +00001535 if (evt.isType(gUpdateWindowTitleEvtName)) {
1536 this->updateTitle();
1537 return true;
1538 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001539 if (evt.isType(ANIMATING_EVENTTYPE)) {
1540 if (fAnimating) {
1541 this->nextSample();
1542 this->postAnimatingEvent();
1543 }
1544 return true;
1545 }
reed@android.com34245c72009-11-03 04:00:48 +00001546 if (evt.isType("set-curr-index")) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001547 this->goToSample(evt.getFast32());
reed@android.com34245c72009-11-03 04:00:48 +00001548 return true;
1549 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001550 if (isInvalEvent(evt)) {
halcanary96fcdcc2015-08-27 07:41:13 -07001551 this->inval(nullptr);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001552 return true;
1553 }
yangsu@google.com921091f2011-08-02 13:39:12 +00001554 int selected = -1;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001555 if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001556 this->setDeviceType((DeviceType)selected);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001557 return true;
yangsu@google.com921091f2011-08-02 13:39:12 +00001558 }
reeda34be682016-02-15 07:48:35 -08001559 if (SkOSMenu::FindListIndex(evt, "ColorType", &selected)) {
brianosmanb109b8c2016-06-16 13:03:24 -07001560 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
1561 this->setDeviceColorType(gConfig[selected].fColorType,
1562 gConfig[selected].fSRGB ? srgbColorSpace : nullptr);
reeda34be682016-02-15 07:48:35 -08001563 return true;
1564 }
halcanary96fcdcc2015-08-27 07:41:13 -07001565 if (SkOSMenu::FindSwitchState(evt, "Slide Show", nullptr)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001566 this->toggleSlideshow();
1567 return true;
1568 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001569 if (SkOSMenu::FindTriState(evt, "AA", &fAAState) ||
1570 SkOSMenu::FindTriState(evt, "LCD", &fLCDState) ||
reed93a12152015-03-16 10:08:34 -07001571 SkOSMenu::FindListIndex(evt, "FilterQuality", &fFilterQualityIndex) ||
bungeman@google.com96aabc82013-06-03 21:26:34 +00001572 SkOSMenu::FindTriState(evt, "Subpixel", &fSubpixelState) ||
1573 SkOSMenu::FindListIndex(evt, "Hinting", &fHintingState) ||
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001574 SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) ||
1575 SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
reeda84d6602016-06-24 18:14:07 -07001576 SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify))
reed093b4e82015-08-04 18:46:38 -07001577 {
halcanary96fcdcc2015-08-27 07:41:13 -07001578 this->inval(nullptr);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00001579 this->updateTitle();
1580 return true;
1581 }
bungeman5a59a422016-03-31 08:30:38 -07001582 if (SkOSMenu::FindListIndex(evt, "Pixel Geometry", &fPixelGeometryIndex)) {
1583 this->setPixelGeometry(fPixelGeometryIndex);
1584 return true;
1585 }
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00001586 if (SkOSMenu::FindListIndex(evt, "Tiling", &fTilingMode)) {
1587 if (SampleView::IsSampleView(curr_view(this))) {
1588 ((SampleView*)curr_view(this))->onTileSizeChanged(this->tileSize());
1589 }
halcanary96fcdcc2015-08-27 07:41:13 -07001590 this->inval(nullptr);
yangsu@google.com921091f2011-08-02 13:39:12 +00001591 this->updateTitle();
1592 return true;
1593 }
halcanary96fcdcc2015-08-27 07:41:13 -07001594 if (SkOSMenu::FindSwitchState(evt, "Flip X", nullptr)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001595 fFlipAxis ^= kFlipAxis_X;
reed@google.comf03bb562011-11-11 21:42:12 +00001596 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001597 return true;
1598 }
halcanary96fcdcc2015-08-27 07:41:13 -07001599 if (SkOSMenu::FindSwitchState(evt, "Flip Y", nullptr)) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001600 fFlipAxis ^= kFlipAxis_Y;
reed@google.comf03bb562011-11-11 21:42:12 +00001601 this->updateMatrix();
yangsu@google.com921091f2011-08-02 13:39:12 +00001602 return true;
1603 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001604 if (SkOSMenu::FindAction(evt,"Save to PDF")) {
yangsu@google.com921091f2011-08-02 13:39:12 +00001605 this->saveToPdf();
1606 return true;
chudy@google.com4605a3f2012-08-01 17:58:01 +00001607 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001608 return this->INHERITED::onEvent(evt);
1609}
1610
reed@android.comf2b98d62010-12-20 18:26:13 +00001611bool SampleWindow::onQuery(SkEvent* query) {
1612 if (query->isType("get-slide-count")) {
1613 query->setFast32(fSamples.count());
1614 return true;
1615 }
1616 if (query->isType("get-slide-title")) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +00001617 SkView* view = (*fSamples[query->getFast32()])();
reed@android.comf2b98d62010-12-20 18:26:13 +00001618 SkEvent evt(gTitleEvtName);
1619 if (view->doQuery(&evt)) {
1620 query->setString("title", evt.findString(gTitleEvtName));
1621 }
1622 SkSafeUnref(view);
1623 return true;
1624 }
1625 if (query->isType("use-fast-text")) {
1626 SkEvent evt(gFastTextEvtName);
1627 return curr_view(this)->doQuery(&evt);
1628 }
reed@google.com29038ed2011-07-06 17:56:47 +00001629 if (query->isType("ignore-window-bitmap")) {
halcanary96fcdcc2015-08-27 07:41:13 -07001630 query->setFast32(this->getGrContext() != nullptr);
reed@google.com29038ed2011-07-06 17:56:47 +00001631 return true;
1632 }
reed@android.comf2b98d62010-12-20 18:26:13 +00001633 return this->INHERITED::onQuery(query);
1634}
1635
Cary Clark992c7b02014-07-31 08:58:44 -04001636DECLARE_bool(portableFonts);
caryclark5fb6bd42014-06-23 11:25:00 -07001637
reed@android.com8a1c16f2008-12-17 15:59:43 +00001638bool SampleWindow::onHandleChar(SkUnichar uni) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001639 {
1640 SkView* view = curr_view(this);
1641 if (view) {
1642 SkEvent evt(gCharEvtName);
1643 evt.setFast32(uni);
1644 if (view->doQuery(&evt)) {
1645 return true;
1646 }
1647 }
1648 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001649
reed@android.com8a1c16f2008-12-17 15:59:43 +00001650 int dx = 0xFF;
1651 int dy = 0xFF;
1652
1653 switch (uni) {
1654 case '5': dx = 0; dy = 0; break;
1655 case '8': dx = 0; dy = -1; break;
1656 case '6': dx = 1; dy = 0; break;
1657 case '2': dx = 0; dy = 1; break;
1658 case '4': dx = -1; dy = 0; break;
1659 case '7': dx = -1; dy = -1; break;
1660 case '9': dx = 1; dy = -1; break;
1661 case '3': dx = 1; dy = 1; break;
1662 case '1': dx = -1; dy = 1; break;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001663
reed@android.com8a1c16f2008-12-17 15:59:43 +00001664 default:
1665 break;
1666 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001667
reed@android.com8a1c16f2008-12-17 15:59:43 +00001668 if (0xFF != dx && 0xFF != dy) {
bungemance560262016-03-03 13:32:38 -08001669 this->changeOffset({SkIntToScalar(dx / 32.0f), SkIntToScalar(dy / 32.0f)});
reed@android.com8a1c16f2008-12-17 15:59:43 +00001670 return true;
1671 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001672
reed@android.com0ae6b242008-12-23 16:49:54 +00001673 switch (uni) {
reed76113a92015-02-02 12:55:02 -08001674 case 27: // ESC
1675 gAnimTimer.stop();
1676 if (this->sendAnimatePulse()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001677 this->inval(nullptr);
reed76113a92015-02-02 12:55:02 -08001678 }
1679 break;
reedd9adfe62015-02-01 19:01:04 -08001680 case ' ':
reed76113a92015-02-02 12:55:02 -08001681 gAnimTimer.togglePauseResume();
reedd9adfe62015-02-01 19:01:04 -08001682 if (this->sendAnimatePulse()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001683 this->inval(nullptr);
reedd9adfe62015-02-01 19:01:04 -08001684 }
1685 break;
reed@google.coma4f81372012-11-15 15:56:38 +00001686 case 'B':
halcanary385fe4d2015-08-26 13:07:48 -07001687 post_event_to_sink(new SkEvent("PictFileView::toggleBBox"), curr_view(this));
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001688 // Cannot call updateTitle() synchronously, because the toggleBBox event is still in
1689 // the queue.
halcanary385fe4d2015-08-26 13:07:48 -07001690 post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this);
halcanary96fcdcc2015-08-27 07:41:13 -07001691 this->inval(nullptr);
reed@google.coma4f81372012-11-15 15:56:38 +00001692 break;
reed4302ae92014-10-06 12:29:56 -07001693 case 'D':
1694 toggleDistanceFieldFonts();
1695 break;
reedbabc3de2016-07-08 08:43:27 -07001696 case 'E':
1697 fUseDeferredCanvas = !fUseDeferredCanvas;
1698 this->inval(nullptr);
1699 break;
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001700 case 'f':
chudy@google.com4605a3f2012-08-01 17:58:01 +00001701 // only
djsollen@google.com6ff82552011-11-07 15:43:57 +00001702 toggleFPS();
yangsu@google.comef7bdfa2011-08-12 14:27:47 +00001703 break;
caryclark5fb6bd42014-06-23 11:25:00 -07001704 case 'F':
Cary Clark992c7b02014-07-31 08:58:44 -04001705 FLAGS_portableFonts ^= true;
halcanary96fcdcc2015-08-27 07:41:13 -07001706 this->inval(nullptr);
caryclark5fb6bd42014-06-23 11:25:00 -07001707 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001708 case 'g':
1709 fRequestGrabImage = true;
halcanary96fcdcc2015-08-27 07:41:13 -07001710 this->inval(nullptr);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001711 break;
reed@google.coma4f81372012-11-15 15:56:38 +00001712 case 'G':
1713 gShowGMBounds = !gShowGMBounds;
commit-bot@chromium.org6de4a782013-12-11 17:05:23 +00001714 post_event_to_sink(GMSampleView::NewShowSizeEvt(gShowGMBounds),
reed@google.coma4f81372012-11-15 15:56:38 +00001715 curr_view(this));
halcanary96fcdcc2015-08-27 07:41:13 -07001716 this->inval(nullptr);
reed@google.coma4f81372012-11-15 15:56:38 +00001717 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001718 case 'i':
1719 this->zoomIn();
1720 break;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00001721 case 'o':
1722 this->zoomOut();
1723 break;
reed@android.com6c5f6f22009-08-14 16:08:38 +00001724 case 'r':
1725 fRotate = !fRotate;
halcanary96fcdcc2015-08-27 07:41:13 -07001726 this->inval(nullptr);
reed@android.com6c5f6f22009-08-14 16:08:38 +00001727 this->updateTitle();
1728 return true;
bsalomon@google.come8f09102011-09-08 18:48:12 +00001729 case 'k':
1730 fPerspAnim = !fPerspAnim;
halcanary96fcdcc2015-08-27 07:41:13 -07001731 this->inval(nullptr);
bsalomon@google.come8f09102011-09-08 18:48:12 +00001732 this->updateTitle();
1733 return true;
reed093b4e82015-08-04 18:46:38 -07001734 case 'K':
1735 fSaveToSKP = true;
halcanary96fcdcc2015-08-27 07:41:13 -07001736 this->inval(nullptr);
reed093b4e82015-08-04 18:46:38 -07001737 return true;
kkinnunen8686a5e2016-01-05 00:04:32 -08001738 case 'M':
reed91b4dfb2016-01-08 08:23:19 -08001739 fUsePicture = !fUsePicture;
kkinnunen8686a5e2016-01-05 00:04:32 -08001740 this->inval(nullptr);
1741 this->updateTitle();
1742 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001743#if SK_SUPPORT_GPU
twiz@google.com05e70242012-01-27 19:12:00 +00001744 case 'p':
1745 {
1746 GrContext* grContext = this->getGrContext();
1747 if (grContext) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001748 size_t cacheBytes;
halcanary96fcdcc2015-08-27 07:41:13 -07001749 grContext->getResourceCacheUsage(nullptr, &cacheBytes);
twiz@google.com05e70242012-01-27 19:12:00 +00001750 grContext->freeGpuResources();
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001751 SkDebugf("Purged %d bytes from the GPU resource cache.\n", cacheBytes);
twiz@google.com05e70242012-01-27 19:12:00 +00001752 }
1753 }
1754 return true;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001755#endif
reed@android.com0ae6b242008-12-23 16:49:54 +00001756 default:
1757 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001758 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001759
scroggo@google.com7dadc742012-04-18 14:07:57 +00001760 if (fAppMenu->handleKeyEquivalent(uni)|| fSlideMenu->handleKeyEquivalent(uni)) {
1761 this->onUpdateMenu(fAppMenu);
1762 this->onUpdateMenu(fSlideMenu);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001763 return true;
1764 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001765 return this->INHERITED::onHandleChar(uni);
1766}
1767
yangsu@google.com921091f2011-08-02 13:39:12 +00001768void SampleWindow::setDeviceType(DeviceType type) {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001769 if (type == fDeviceType)
1770 return;
halcanary9d524f22016-03-29 09:03:52 -07001771
reeda34be682016-02-15 07:48:35 -08001772 fDevManager->tearDownBackend(this);
reeda34be682016-02-15 07:48:35 -08001773 fDeviceType = type;
brianosman2d1ee792016-05-05 12:24:31 -07001774 fDevManager->setUpBackend(this, fMSAASampleCount, fDeepColor);
halcanary9d524f22016-03-29 09:03:52 -07001775
reeda34be682016-02-15 07:48:35 -08001776 this->updateTitle();
1777 this->inval(nullptr);
1778}
1779
brianosmanb109b8c2016-06-16 13:03:24 -07001780void SampleWindow::setDeviceColorType(SkColorType ct, sk_sp<SkColorSpace> cs) {
1781 this->setColorType(ct, std::move(cs));
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001782
1783 fDevManager->tearDownBackend(this);
brianosman2d1ee792016-05-05 12:24:31 -07001784 fDevManager->setUpBackend(this, fMSAASampleCount, fDeepColor);
halcanary9d524f22016-03-29 09:03:52 -07001785
yangsu@google.com921091f2011-08-02 13:39:12 +00001786 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001787 this->inval(nullptr);
yangsu@google.com921091f2011-08-02 13:39:12 +00001788}
1789
Scroggo2c8208f2011-06-15 16:49:08 +00001790void SampleWindow::toggleSlideshow() {
1791 fAnimating = !fAnimating;
1792 this->postAnimatingEvent();
1793 this->updateTitle();
1794}
1795
1796void SampleWindow::toggleRendering() {
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00001797 this->setDeviceType(cycle_devicetype(fDeviceType));
Scroggo2c8208f2011-06-15 16:49:08 +00001798 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001799 this->inval(nullptr);
Scroggo2c8208f2011-06-15 16:49:08 +00001800}
1801
djsollen@google.com6ff82552011-11-07 15:43:57 +00001802void SampleWindow::toggleFPS() {
1803 fMeasureFPS = !fMeasureFPS;
1804 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001805 this->inval(nullptr);
djsollen@google.com6ff82552011-11-07 15:43:57 +00001806}
1807
reed4302ae92014-10-06 12:29:56 -07001808void SampleWindow::toggleDistanceFieldFonts() {
jvanverth629162d2015-11-08 08:07:24 -08001809 // reset backend
1810 fDevManager->tearDownBackend(this);
brianosman2d1ee792016-05-05 12:24:31 -07001811 fDevManager->setUpBackend(this, fMSAASampleCount, fDeepColor);
jvanverth629162d2015-11-08 08:07:24 -08001812
reed4302ae92014-10-06 12:29:56 -07001813 SkSurfaceProps props = this->getSurfaceProps();
bsalomonafcd7cd2015-08-31 12:39:41 -07001814 uint32_t flags = props.flags() ^ SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
reed4302ae92014-10-06 12:29:56 -07001815 this->setSurfaceProps(SkSurfaceProps(flags, props.pixelGeometry()));
1816
1817 this->updateTitle();
halcanary96fcdcc2015-08-27 07:41:13 -07001818 this->inval(nullptr);
reed4302ae92014-10-06 12:29:56 -07001819}
1820
bungeman5a59a422016-03-31 08:30:38 -07001821void SampleWindow::setPixelGeometry(int pixelGeometryIndex) {
1822 // reset backend
1823 fDevManager->tearDownBackend(this);
brianosman2d1ee792016-05-05 12:24:31 -07001824 fDevManager->setUpBackend(this, fMSAASampleCount, fDeepColor);
bungeman5a59a422016-03-31 08:30:38 -07001825
1826 const SkSurfaceProps& oldProps = this->getSurfaceProps();
1827 SkSurfaceProps newProps(oldProps.flags(), SkSurfaceProps::kLegacyFontHost_InitType);
1828 if (pixelGeometryIndex > 0) {
1829 newProps = SkSurfaceProps(oldProps.flags(),
1830 gPixelGeometryStates[pixelGeometryIndex].pixelGeometry);
1831 }
1832 this->setSurfaceProps(newProps);
1833
1834 this->updateTitle();
1835 this->inval(nullptr);
1836}
1837
reed@android.com8a1c16f2008-12-17 15:59:43 +00001838#include "SkDumpCanvas.h"
1839
1840bool SampleWindow::onHandleKey(SkKey key) {
reed@android.comf2b98d62010-12-20 18:26:13 +00001841 {
1842 SkView* view = curr_view(this);
1843 if (view) {
1844 SkEvent evt(gKeyEvtName);
1845 evt.setFast32(key);
1846 if (view->doQuery(&evt)) {
1847 return true;
1848 }
1849 }
1850 }
bungemance560262016-03-03 13:32:38 -08001851
1852 int dx = 0xFF;
1853 int dy = 0xFF;
1854
reed@android.com8a1c16f2008-12-17 15:59:43 +00001855 switch (key) {
1856 case kRight_SkKey:
1857 if (this->nextSample()) {
1858 return true;
1859 }
1860 break;
1861 case kLeft_SkKey:
scroggo@google.come2dd9732012-08-15 20:03:06 +00001862 if (this->previousSample()) {
1863 return true;
1864 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001865 return true;
1866 case kUp_SkKey:
reedf7f79d22015-02-21 09:03:20 -08001867 this->changeZoomLevel(1.f / 32.f);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001868 return true;
1869 case kDown_SkKey:
reedf7f79d22015-02-21 09:03:20 -08001870 this->changeZoomLevel(-1.f / 32.f);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001871 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001872 case kOK_SkKey: {
1873 SkString title;
1874 if (curr_title(this, &title)) {
1875 writeTitleToPrefs(title.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001876 }
1877 return true;
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00001878 }
reed@android.com34245c72009-11-03 04:00:48 +00001879 case kBack_SkKey:
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001880 this->showOverview();
reed@android.com34245c72009-11-03 04:00:48 +00001881 return true;
bungemance560262016-03-03 13:32:38 -08001882
1883 case k5_SkKey: dx = 0; dy = 0; break;
1884 case k8_SkKey: dx = 0; dy = -1; break;
1885 case k6_SkKey: dx = 1; dy = 0; break;
1886 case k2_SkKey: dx = 0; dy = 1; break;
1887 case k4_SkKey: dx = -1; dy = 0; break;
1888 case k7_SkKey: dx = -1; dy = -1; break;
1889 case k9_SkKey: dx = 1; dy = -1; break;
1890 case k3_SkKey: dx = 1; dy = 1; break;
1891 case k1_SkKey: dx = -1; dy = 1; break;
1892
reed@android.com8a1c16f2008-12-17 15:59:43 +00001893 default:
1894 break;
1895 }
bungemance560262016-03-03 13:32:38 -08001896
1897 if (0xFF != dx && 0xFF != dy) {
1898 this->changeOffset({SkIntToScalar(dx / 32.0f), SkIntToScalar(dy / 32.0f)});
1899 return true;
1900 }
1901
reed@android.com8a1c16f2008-12-17 15:59:43 +00001902 return this->INHERITED::onHandleKey(key);
1903}
1904
reed@google.com52f57e12011-03-16 12:10:02 +00001905///////////////////////////////////////////////////////////////////////////////
1906
1907static const char gGestureClickType[] = "GestureClickType";
1908
Scroggod3aed392011-06-22 13:26:56 +00001909bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
reed@google.com4d5c26d2013-01-08 16:17:50 +00001910 void* owner, unsigned modi) {
Scroggo0f185c22011-03-24 18:35:50 +00001911 if (Click::kMoved_State == state) {
1912 updatePointer(x, y);
1913 }
reed@google.come1ca7052013-12-17 19:22:07 +00001914 int w = SkScalarRoundToInt(this->width());
1915 int h = SkScalarRoundToInt(this->height());
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001916
1917 // check for the resize-box
1918 if (w - x < 16 && h - y < 16) {
1919 return false; // let the OS handle the click
chudy@google.com4605a3f2012-08-01 17:58:01 +00001920 }
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001921 else if (fMagnify) {
1922 //it's only necessary to update the drawing if there's a click
halcanary96fcdcc2015-08-27 07:41:13 -07001923 this->inval(nullptr);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00001924 return false; //prevent dragging while magnify is enabled
reed@google.com4d5c26d2013-01-08 16:17:50 +00001925 } else {
1926 // capture control+option, and trigger debugger
1927 if ((modi & kControl_SkModifierKey) && (modi & kOption_SkModifierKey)) {
1928 if (Click::kDown_State == state) {
1929 SkEvent evt("debug-hit-test");
1930 evt.setS32("debug-hit-test-x", x);
1931 evt.setS32("debug-hit-test-y", y);
1932 curr_view(this)->doEvent(evt);
1933 }
1934 return true;
1935 } else {
1936 return this->INHERITED::onDispatchClick(x, y, state, owner, modi);
1937 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00001938 }
1939}
1940
reed@google.com52f57e12011-03-16 12:10:02 +00001941class GestureClick : public SkView::Click {
1942public:
1943 GestureClick(SkView* target) : SkView::Click(target) {
1944 this->setType(gGestureClickType);
1945 }
1946
1947 static bool IsGesture(Click* click) {
1948 return click->isType(gGestureClickType);
1949 }
1950};
1951
reed@google.com4d5c26d2013-01-08 16:17:50 +00001952SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y,
1953 unsigned modi) {
reed@google.com52f57e12011-03-16 12:10:02 +00001954 return new GestureClick(this);
1955}
1956
1957bool SampleWindow::onClick(Click* click) {
1958 if (GestureClick::IsGesture(click)) {
bsalomon@google.com4d4f2812011-12-12 18:34:01 +00001959 float x = static_cast<float>(click->fICurr.fX);
1960 float y = static_cast<float>(click->fICurr.fY);
chudy@google.com4605a3f2012-08-01 17:58:01 +00001961
reed@google.com52f57e12011-03-16 12:10:02 +00001962 switch (click->fState) {
1963 case SkView::Click::kDown_State:
Scroggod3aed392011-06-22 13:26:56 +00001964 fGesture.touchBegin(click->fOwner, x, y);
reed@google.com52f57e12011-03-16 12:10:02 +00001965 break;
1966 case SkView::Click::kMoved_State:
Scroggod3aed392011-06-22 13:26:56 +00001967 fGesture.touchMoved(click->fOwner, x, y);
reed@google.comf03bb562011-11-11 21:42:12 +00001968 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001969 break;
1970 case SkView::Click::kUp_State:
Scroggod3aed392011-06-22 13:26:56 +00001971 fGesture.touchEnd(click->fOwner);
reed@google.comf03bb562011-11-11 21:42:12 +00001972 this->updateMatrix();
reed@google.com52f57e12011-03-16 12:10:02 +00001973 break;
1974 }
1975 return true;
1976 }
1977 return false;
1978}
1979
1980///////////////////////////////////////////////////////////////////////////////
1981
reed@android.com8a1c16f2008-12-17 15:59:43 +00001982void SampleWindow::loadView(SkView* view) {
1983 SkView::F2BIter iter(this);
1984 SkView* prev = iter.next();
1985 if (prev) {
1986 prev->detachFromParent();
1987 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00001988
reed@android.com8a1c16f2008-12-17 15:59:43 +00001989 view->setVisibleP(true);
reed@android.comf2b98d62010-12-20 18:26:13 +00001990 view->setClipToBounds(false);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001991 this->attachChildToFront(view)->unref();
1992 view->setSize(this->width(), this->height());
1993
yangsu@google.com921091f2011-08-02 13:39:12 +00001994 //repopulate the slide menu when a view is loaded
scroggo@google.com7dadc742012-04-18 14:07:57 +00001995 fSlideMenu->reset();
djsollen@google.com388974f2013-01-17 13:20:01 +00001996
scroggo@google.com7dadc742012-04-18 14:07:57 +00001997 this->onUpdateMenu(fSlideMenu);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001998 this->updateTitle();
1999}
2000
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002001static const char* gDeviceTypePrefix[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002002 "raster: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002003#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +00002004 "opengl: ",
robertphillips@google.comb442a6d2012-04-02 19:24:21 +00002005#if SK_ANGLE
2006 "angle: ",
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002007#endif // SK_ANGLE
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002008#endif // SK_SUPPORT_GPU
reed@android.com8a1c16f2008-12-17 15:59:43 +00002009};
bungeman99fe8222015-08-20 07:57:51 -07002010static_assert(SK_ARRAY_COUNT(gDeviceTypePrefix) == SampleWindow::kDeviceTypeCnt,
2011 "array_size_mismatch");
reed@android.com8a1c16f2008-12-17 15:59:43 +00002012
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002013static const char* trystate_str(SkOSMenu::TriState state,
reed@google.com569e0432011-04-05 13:07:03 +00002014 const char trueStr[], const char falseStr[]) {
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002015 if (SkOSMenu::kOnState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002016 return trueStr;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +00002017 } else if (SkOSMenu::kOffState == state) {
reed@google.com569e0432011-04-05 13:07:03 +00002018 return falseStr;
2019 }
halcanary96fcdcc2015-08-27 07:41:13 -07002020 return nullptr;
reed@google.com569e0432011-04-05 13:07:03 +00002021}
2022
reedddb5eca2014-10-08 11:10:51 -07002023bool SampleWindow::getRawTitle(SkString* title) {
2024 return curr_title(this, title);
2025}
reed@android.com8a1c16f2008-12-17 15:59:43 +00002026
reedddb5eca2014-10-08 11:10:51 -07002027void SampleWindow::updateTitle() {
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +00002028 SkString title;
reedddb5eca2014-10-08 11:10:51 -07002029 if (!this->getRawTitle(&title)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002030 title.set("<unknown>");
2031 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002032
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002033 title.prepend(gDeviceTypePrefix[fDeviceType]);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002034
commit-bot@chromium.orgcc63b322013-12-06 20:14:55 +00002035 if (fTilingMode != kNo_Tiling) {
2036 title.prependf("<T: %s> ", gTilingInfo[fTilingMode].label);
2037 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002038 if (fAnimating) {
2039 title.prepend("<A> ");
2040 }
reed@android.com6c5f6f22009-08-14 16:08:38 +00002041 if (fRotate) {
2042 title.prepend("<R> ");
2043 }
bsalomon@google.come8f09102011-09-08 18:48:12 +00002044 if (fPerspAnim) {
2045 title.prepend("<K> ");
2046 }
bsalomonafcd7cd2015-08-31 12:39:41 -07002047 if (this->getSurfaceProps().flags() & SkSurfaceProps::kUseDeviceIndependentFonts_Flag) {
2048 title.prepend("<DIF> ");
reed4302ae92014-10-06 12:29:56 -07002049 }
reed91b4dfb2016-01-08 08:23:19 -08002050 if (fUsePicture) {
2051 title.prepend("<P> ");
kkinnunen8686a5e2016-01-05 00:04:32 -08002052 }
reedbabc3de2016-07-08 08:43:27 -07002053 if (fUseDeferredCanvas) {
2054 title.prepend("<E> ");
2055 }
reed@google.com569e0432011-04-05 13:07:03 +00002056
2057 title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
2058 title.prepend(trystate_str(fAAState, "AA ", "aa "));
reed93a12152015-03-16 10:08:34 -07002059 title.prepend(gFilterQualityStates[fFilterQualityIndex].fLabel);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002060 title.prepend(trystate_str(fSubpixelState, "S ", "s "));
halcanary96fcdcc2015-08-27 07:41:13 -07002061 title.prepend(fFlipAxis & kFlipAxis_X ? "X " : nullptr);
2062 title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : nullptr);
bungeman@google.com96aabc82013-06-03 21:26:34 +00002063 title.prepend(gHintingStates[fHintingState].label);
bungeman5a59a422016-03-31 08:30:38 -07002064 title.prepend(gPixelGeometryStates[fPixelGeometryIndex].label);
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002065
bungemance560262016-03-03 13:32:38 -08002066 if (fOffset.fX || fOffset.fY) {
2067 title.prependf("(%.2f, %.2f) ", SkScalarToFloat(fOffset.fX), SkScalarToFloat(fOffset.fY));
2068 }
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002069 if (fZoomLevel) {
djsollen@google.come32b5832011-06-13 16:58:40 +00002070 title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
mike@reedtribe.orgdd0cd342011-03-21 00:53:39 +00002071 }
chudy@google.com4605a3f2012-08-01 17:58:01 +00002072
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002073 if (fMeasureFPS) {
jvanverth4d0d81a2015-08-11 11:07:07 -07002074 title.appendf(" %8.4f ms", fMeasureFPS_Time / (float)FPS_REPEAT_COUNT);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +00002075 }
reedddb5eca2014-10-08 11:10:51 -07002076
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002077#if SK_SUPPORT_GPU
bsalomon@google.com82502e22013-01-24 20:47:18 +00002078 if (IsGpuDeviceType(fDeviceType) &&
bsalomon49f085d2014-09-05 13:34:00 -07002079 fDevManager &&
robertphillipsecf3dbe2016-07-28 15:17:34 -07002080 fDevManager->numColorSamples() > 0) {
bsalomon@google.com11959252012-04-06 20:13:38 +00002081 title.appendf(" [MSAA: %d]",
robertphillipsecf3dbe2016-07-28 15:17:34 -07002082 fDevManager->numColorSamples());
bsalomon@google.com11959252012-04-06 20:13:38 +00002083 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002084#endif
bsalomon@google.com11959252012-04-06 20:13:38 +00002085
reeda34be682016-02-15 07:48:35 -08002086 title.appendf(" %s", find_config_name(this->info()));
2087
brianosman2d1ee792016-05-05 12:24:31 -07002088 if (fDevManager && fDevManager->getColorBits() > 24) {
2089 title.appendf(" %d bpc", fDevManager->getColorBits());
2090 }
2091
reed@android.com8a1c16f2008-12-17 15:59:43 +00002092 this->setTitle(title.c_str());
2093}
2094
2095void SampleWindow::onSizeChange() {
2096 this->INHERITED::onSizeChange();
chudy@google.com4605a3f2012-08-01 17:58:01 +00002097
reed@android.com8a1c16f2008-12-17 15:59:43 +00002098 SkView::F2BIter iter(this);
2099 SkView* view = iter.next();
2100 view->setSize(this->width(), this->height());
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002101
reed@android.com8a1c16f2008-12-17 15:59:43 +00002102 // rebuild our clippath
2103 {
2104 const SkScalar W = this->width();
2105 const SkScalar H = this->height();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002106
reed@android.com8a1c16f2008-12-17 15:59:43 +00002107 fClipPath.reset();
2108#if 0
2109 for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
2110 SkRect r;
2111 r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
2112 for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
2113 fClipPath.addRect(r);
2114 }
2115#else
2116 SkRect r;
2117 r.set(0, 0, W, H);
2118 fClipPath.addRect(r, SkPath::kCCW_Direction);
2119 r.set(W/4, H/4, W*3/4, H*3/4);
2120 fClipPath.addRect(r, SkPath::kCW_Direction);
2121#endif
2122 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002123
Scroggo2c8208f2011-06-15 16:49:08 +00002124 fZoomCenterX = SkScalarHalf(this->width());
2125 fZoomCenterY = SkScalarHalf(this->height());
2126
djsollen@google.com56c69772011-11-08 19:00:26 +00002127#ifdef SK_BUILD_FOR_ANDROID
Scroggob4490c72011-06-17 13:53:05 +00002128 // FIXME: The first draw after a size change does not work on Android, so
2129 // we post an invalidate.
Scroggo62b65b02011-06-21 16:01:26 +00002130 this->postInvalDelay();
Scroggo716a0382011-06-16 14:00:15 +00002131#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00002132 this->updateTitle(); // to refresh our config
bsalomon@google.com098e96d2011-07-14 14:30:46 +00002133 fDevManager->windowSizeChanged(this);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +00002134
2135 if (fTilingMode != kNo_Tiling && SampleView::IsSampleView(view)) {
2136 ((SampleView*)view)->onTileSizeChanged(this->tileSize());
2137 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00002138}
2139
2140///////////////////////////////////////////////////////////////////////////////
2141
reed@android.comf2b98d62010-12-20 18:26:13 +00002142template <typename T> void SkTBSort(T array[], int count) {
2143 for (int i = 1; i < count - 1; i++) {
2144 bool didSwap = false;
2145 for (int j = count - 1; j > i; --j) {
2146 if (array[j] < array[j-1]) {
2147 T tmp(array[j-1]);
2148 array[j-1] = array[j];
2149 array[j] = tmp;
2150 didSwap = true;
2151 }
2152 }
2153 if (!didSwap) {
2154 break;
2155 }
2156 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002157
reed@android.comf2b98d62010-12-20 18:26:13 +00002158 for (int k = 0; k < count - 1; k++) {
2159 SkASSERT(!(array[k+1] < array[k]));
2160 }
2161}
2162
2163#include "SkRandom.h"
2164
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002165static void rand_rect(SkIRect* rect, SkRandom& rand) {
reed@android.comf2b98d62010-12-20 18:26:13 +00002166 int bits = 8;
2167 int shift = 32 - bits;
2168 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
2169 rand.nextU() >> shift, rand.nextU() >> shift);
2170 rect->sort();
2171}
2172
2173static void dumpRect(const SkIRect& r) {
2174 SkDebugf(" { %d, %d, %d, %d },\n",
2175 r.fLeft, r.fTop,
2176 r.fRight, r.fBottom);
2177}
2178
2179static void test_rects(const SkIRect rect[], int count) {
2180 SkRegion rgn0, rgn1;
2181
2182 for (int i = 0; i < count; i++) {
2183 rgn0.op(rect[i], SkRegion::kUnion_Op);
2184 // dumpRect(rect[i]);
2185 }
2186 rgn1.setRects(rect, count);
2187
2188 if (rgn0 != rgn1) {
2189 SkDebugf("\n");
2190 for (int i = 0; i < count; i++) {
2191 dumpRect(rect[i]);
2192 }
2193 SkDebugf("\n");
2194 }
2195}
2196
2197static void test() {
2198 size_t i;
2199
2200 const SkIRect r0[] = {
2201 { 0, 0, 1, 1 },
2202 { 2, 2, 3, 3 },
2203 };
2204 const SkIRect r1[] = {
2205 { 0, 0, 1, 3 },
2206 { 1, 1, 2, 2 },
2207 { 2, 0, 3, 3 },
2208 };
2209 const SkIRect r2[] = {
2210 { 0, 0, 1, 2 },
2211 { 2, 1, 3, 3 },
2212 { 4, 0, 5, 1 },
2213 { 6, 0, 7, 4 },
2214 };
2215
2216 static const struct {
2217 const SkIRect* fRects;
2218 int fCount;
2219 } gRecs[] = {
2220 { r0, SK_ARRAY_COUNT(r0) },
2221 { r1, SK_ARRAY_COUNT(r1) },
2222 { r2, SK_ARRAY_COUNT(r2) },
2223 };
2224
2225 for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
2226 test_rects(gRecs[i].fRects, gRecs[i].fCount);
2227 }
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002228
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002229 SkRandom rand;
reed@android.comf2b98d62010-12-20 18:26:13 +00002230 for (i = 0; i < 10000; i++) {
2231 SkRegion rgn0, rgn1;
2232
2233 const int N = 8;
2234 SkIRect rect[N];
2235 for (int j = 0; j < N; j++) {
2236 rand_rect(&rect[j], rand);
2237 }
2238 test_rects(rect, N);
2239 }
2240}
2241
caryclark@google.com02939ce2012-06-06 12:09:51 +00002242// FIXME: this should be in a header
2243SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
senorblanco@chromium.org78b82532011-06-28 19:44:03 +00002244SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
caryclark@google.com02939ce2012-06-06 12:09:51 +00002245 if (false) { // avoid bit rot, suppress warning
2246 test();
2247 }
halcanary96fcdcc2015-08-27 07:41:13 -07002248 return new SampleWindow(hwnd, argc, argv, nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002249}
2250
caryclark@google.com02939ce2012-06-06 12:09:51 +00002251// FIXME: this should be in a header
2252void get_preferred_size(int* x, int* y, int* width, int* height);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002253void get_preferred_size(int* x, int* y, int* width, int* height) {
2254 *x = 10;
2255 *y = 50;
2256 *width = 640;
2257 *height = 480;
2258}
2259
caryclark@google.com5987f582012-10-02 18:33:14 +00002260#ifdef SK_BUILD_FOR_IOS
reedf0b17102014-10-22 13:06:00 -07002261#include "SkApplication.h"
caryclark17f0b6d2014-07-22 10:15:34 -07002262IOS_launch_type set_cmd_line_args(int , char *[], const char* resourceDir) {
caryclarkf568f6a2014-07-18 10:58:56 -07002263 SetResourcePath(resourceDir);
caryclark17f0b6d2014-07-22 10:15:34 -07002264 return kApplication__iOSLaunchType;
caryclark@google.com5987f582012-10-02 18:33:14 +00002265}
2266#endif
2267
reed@android.com8a1c16f2008-12-17 15:59:43 +00002268void application_init() {
2269// setenv("ANDROID_ROOT", "../../../data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002270#ifdef SK_BUILD_FOR_MAC
reed@android.com8a1c16f2008-12-17 15:59:43 +00002271 setenv("ANDROID_ROOT", "/android/device/data", 0);
reed@android.come191b162009-12-18 21:33:39 +00002272#endif
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002273 SkGraphics::Init();
2274 SkEvent::Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002275}
2276
2277void application_term() {
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002278 SkEvent::Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +00002279}