SampleApp.cpp revision bcbc1788b478b1e54079318ad073e8490aa66fae
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
7868074b50b0fc3e460d2aa97c1096827fe0a1935reed
82c8208f3a444098aee562f5a1ef620142807eda2Scroggo#include "SampleApp.h"
92c8208f3a444098aee562f5a1ef620142807eda2Scroggo
10bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "OverView.h"
11bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "SampleCode.h"
12bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "SamplePipeControllers.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
14bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "SkCommandLineFlags.h"
15bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "SkData.h"
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDevice.h"
17bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "SkGPipe.h"
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkGraphics.h"
19b08eb2b3bce45411494fe00258e4c91248ccd15ereed@android.com#include "SkImageEncoder.h"
20bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "SkOSFile.h"
21bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "SkPDFDevice.h"
22bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "SkPDFDocument.h"
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPaint.h"
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPicture.h"
25770963f23f4fc313db0fa3bac18b1b8aafb55f17robertphillips@google.com#include "SkPictureRecorder.h"
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkStream.h"
279a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org#include "SkSurface.h"
28f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com#include "SkTSort.h"
294417740a480786ca65379ba5b398c500ba34ee18reed@android.com#include "SkTime.h"
300f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo#include "SkTypeface.h"
31bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "SkWindow.h"
32bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "TransitionView.h"
33bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "sk_tool_utils.h"
34f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
35cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
366bf38b59c9de1cd35cd091cf8766117f4e1f66a0tomhudson@google.com#include "gl/GrGLInterface.h"
379c1f1ac5d399c73f5f331bdf7b392ea7f0c44dafbsalomon@google.com#include "gl/GrGLUtil.h"
38583a1e38503ebd57ba9bd39a3fabe89bead8e76cbsalomon@google.com#include "GrRenderTarget.h"
39cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#include "GrContext.h"
40cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#include "SkGpuDevice.h"
41cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#else
42cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.comclass GrContext;
43cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
442c8208f3a444098aee562f5a1ef620142807eda2Scroggo
451830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.comextern SampleView* CreateSamplePictFileView(const char filename[]);
461830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com
471830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.comclass PictFileFactory : public SkViewFactory {
481830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com    SkString fFilename;
491830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.compublic:
501830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com    PictFileFactory(const SkString& filename) : fFilename(filename) {}
511830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com    virtual SkView* operator() () const SK_OVERRIDE {
521830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com        return CreateSamplePictFileView(fFilename.c_str());
531830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com    }
541830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com};
551830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com
56f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com#ifdef SAMPLE_PDF_FILE_VIEWER
57f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.comextern SampleView* CreateSamplePdfFileViewer(const char filename[]);
58f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com
59f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.comclass PdfFileViewerFactory : public SkViewFactory {
60f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    SkString fFilename;
61f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.compublic:
62f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    PdfFileViewerFactory(const SkString& filename) : fFilename(filename) {}
63f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    virtual SkView* operator() () const SK_OVERRIDE {
64f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com        return CreateSamplePdfFileViewer(fFilename.c_str());
65f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    }
66f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com};
67f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com#endif  // SAMPLE_PDF_FILE_VIEWER
68f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com
69db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com#define PIPE_FILEx
70db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com#ifdef  PIPE_FILE
711f3942110a6fcec86eb411b49eab16d2bd91eb5byangsu@google.com#define FILE_PATH "/path/to/drawing.data"
721f3942110a6fcec86eb411b49eab16d2bd91eb5byangsu@google.com#endif
731f3942110a6fcec86eb411b49eab16d2bd91eb5byangsu@google.com
74f121b0525485fb18e96c4b9dbc57594c430e42f4yangsu@google.com#define PIPE_NETx
75db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com#ifdef  PIPE_NET
76db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com#include "SkSockets.h"
77db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.comSkTCPServer gServer;
78db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com#endif
79ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com
80628ed0b22045a9544b56e00e73137d6050ce900ccommit-bot@chromium.org#define USE_ARROWS_FOR_ZOOM true
81b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
82b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com#if SK_ANGLE
83b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com//#define DEFAULT_TO_ANGLE 1
84b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com#else
8582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com#define DEFAULT_TO_GPU 0 // if 1 default rendering is on GPU
86b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com#endif
87f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define ANIMATING_EVENTTYPE "nextSample"
8984cfce14fec1f963fedaa3c81b8f3faccd3db13areed@google.com#define ANIMATING_DELAY     250
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
912eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org#ifdef SK_DEBUG
92bad8c87812234117d3dc411382fca60e2e868477reed@google.com    #define FPS_REPEAT_MULTIPLIER   1
932eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org#else
94bad8c87812234117d3dc411382fca60e2e868477reed@google.com    #define FPS_REPEAT_MULTIPLIER   10
952eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org#endif
96bad8c87812234117d3dc411382fca60e2e868477reed@google.com#define FPS_REPEAT_COUNT    (10 * FPS_REPEAT_MULTIPLIER)
972eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
983cec4d71e6f53fda99705427f9820d025cfbf64breed@google.comstatic SampleWindow* gSampleWindow;
993cec4d71e6f53fda99705427f9820d025cfbf64breed@google.com
100a4f8137e67e31390b0e7edc5e637a3a02495d1a2reed@google.comstatic bool gShowGMBounds;
101a4f8137e67e31390b0e7edc5e637a3a02495d1a2reed@google.com
1026de4a7836f1c7988b42cbbfe1a339ef7619aed8fcommit-bot@chromium.orgstatic void post_event_to_sink(SkEvent* evt, SkEventSink* sink) {
1032072db89016c4e53a648f4d4cec08b1d6be8558creed@google.com    evt->setTargetID(sink->getSinkID())->post();
1042072db89016c4e53a648f4d4cec08b1d6be8558creed@google.com}
1052072db89016c4e53a648f4d4cec08b1d6be8558creed@google.com
1066f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org///////////////////////////////////////////////////////////////////////////////
1076f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org
1086f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.orgstatic const char* skip_until(const char* str, const char* skip) {
1096f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    if (!str) {
1106f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org        return NULL;
1116f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    }
1126f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    return strstr(str, skip);
1136f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org}
1146f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org
1156f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.orgstatic const char* skip_past(const char* str, const char* skip) {
1166f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    const char* found = skip_until(str, skip);
1176f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    if (!found) {
1186f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org        return NULL;
1196f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    }
1206f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    return found + strlen(skip);
1216f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org}
1226f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org
1236f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.orgstatic const char* gPrefFileName = "sampleapp_prefs.txt";
1246f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org
1256fc7cc23a9c33960b879f69d92d6fb0e1373e556epoger@google.comstatic bool readTitleFromPrefs(SkString* title) {
1266f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    SkFILEStream stream(gPrefFileName);
1276f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    if (!stream.isValid()) {
1286f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org        return false;
1296f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    }
1306f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org
1317fa2a65c0cfc714364490cb715171461143024e0reed@google.com    size_t len = stream.getLength();
1326f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    SkString data(len);
1336f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    stream.read(data.writable_str(), len);
1346f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    const char* s = data.c_str();
1356f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org
1366f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    s = skip_past(s, "curr-slide-title");
1376f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    s = skip_past(s, "=");
1386f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    s = skip_past(s, "\"");
1396f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    const char* stop = skip_until(s, "\"");
1406f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    if (stop > s) {
1416f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org        title->set(s, stop - s);
1426f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org        return true;
1436f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    }
1446f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    return false;
1456f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org}
1466f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org
1476fc7cc23a9c33960b879f69d92d6fb0e1373e556epoger@google.comstatic void writeTitleToPrefs(const char* title) {
1486f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    SkFILEWStream stream(gPrefFileName);
1496f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    SkString data;
1506f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    data.printf("curr-slide-title = \"%s\"\n", title);
1516f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    stream.write(data.c_str(), data.size());
1526f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org}
1536f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org
1546f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org///////////////////////////////////////////////////////////////////////////////
1556f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org
156098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.comclass SampleWindow::DefaultDeviceManager : public SampleWindow::DeviceManager {
157098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.compublic:
158098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com
159cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com    DefaultDeviceManager() {
160cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
161cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com        fCurContext = NULL;
162cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com        fCurIntf = NULL;
163cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com        fCurRenderTarget = NULL;
164cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com        fMSAASampleCount = 0;
165cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
166cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com        fBackend = kNone_BackEndType;
167098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    }
168098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com
169098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    virtual ~DefaultDeviceManager() {
170cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
171b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        SkSafeUnref(fCurContext);
172b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        SkSafeUnref(fCurIntf);
173b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        SkSafeUnref(fCurRenderTarget);
174cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
175098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    }
176098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com
1771195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com    virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) {
178b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        SkASSERT(kNone_BackEndType == fBackend);
179b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
180b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        fBackend = kNone_BackEndType;
181d3b9fbbc48c13a1b2a664cf7e01374a44c201f51robertphillips@google.com
182cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
183b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        switch (win->getDeviceType()) {
184b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            case kRaster_DeviceType:
185b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                // fallthrough
186b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            case kPicture_DeviceType:
187b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                // fallthrough
188b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            case kGPU_DeviceType:
189b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                // fallthrough
190b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            case kNullGPU_DeviceType:
191b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                // all these guys use the native backend
192b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                fBackend = kNativeGL_BackEndType;
193b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                break;
194d3b9fbbc48c13a1b2a664cf7e01374a44c201f51robertphillips@google.com#if SK_ANGLE
195b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            case kANGLE_DeviceType:
196b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                // ANGLE is really the only odd man out
197b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                fBackend = kANGLE_BackEndType;
198b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                break;
199cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif // SK_ANGLE
200b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            default:
201b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                SkASSERT(false);
202b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                break;
203d3b9fbbc48c13a1b2a664cf7e01374a44c201f51robertphillips@google.com        }
20464cc810ad165724f9c666a75bd52e41c67f13564bsalomon@google.com        AttachmentInfo attachmentInfo;
20564cc810ad165724f9c666a75bd52e41c67f13564bsalomon@google.com        bool result = win->attach(fBackend, msaaSampleCount, &attachmentInfo);
206d3b9fbbc48c13a1b2a664cf7e01374a44c201f51robertphillips@google.com        if (!result) {
20774913722bfe5e4b6810545891958e3d8e9c63791bsalomon@google.com            SkDebugf("Failed to initialize GL");
208b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            return;
209098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com        }
2101195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com        fMSAASampleCount = msaaSampleCount;
211b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
212b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        SkASSERT(NULL == fCurIntf);
213d8ed85101ee77ad2cb0c186a79d197698a75d246commit-bot@chromium.org        SkAutoTUnref<const GrGLInterface> glInterface;
214b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        switch (win->getDeviceType()) {
215b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            case kRaster_DeviceType:
216b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                // fallthrough
217b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            case kPicture_DeviceType:
218b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                // fallthrough
219b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            case kGPU_DeviceType:
220b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                // all these guys use the native interface
221d8ed85101ee77ad2cb0c186a79d197698a75d246commit-bot@chromium.org                glInterface.reset(GrGLCreateNativeInterface());
222b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                break;
223d3b9fbbc48c13a1b2a664cf7e01374a44c201f51robertphillips@google.com#if SK_ANGLE
224b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            case kANGLE_DeviceType:
225d8ed85101ee77ad2cb0c186a79d197698a75d246commit-bot@chromium.org                glInterface.reset(GrGLCreateANGLEInterface());
226b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                break;
227cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif // SK_ANGLE
228b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            case kNullGPU_DeviceType:
229d8ed85101ee77ad2cb0c186a79d197698a75d246commit-bot@chromium.org                glInterface.reset(GrGLCreateNullInterface());
230b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                break;
231b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            default:
232b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                SkASSERT(false);
233b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                break;
2346fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com        }
235b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
236d8ed85101ee77ad2cb0c186a79d197698a75d246commit-bot@chromium.org        // Currently SampleApp does not use NVPR. TODO: Provide an NVPR device type that is skipped
237d8ed85101ee77ad2cb0c186a79d197698a75d246commit-bot@chromium.org        // when the driver doesn't support NVPR.
238d8ed85101ee77ad2cb0c186a79d197698a75d246commit-bot@chromium.org        fCurIntf = GrGLInterfaceRemoveNVPR(glInterface.get());
239d8ed85101ee77ad2cb0c186a79d197698a75d246commit-bot@chromium.org
240b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        SkASSERT(NULL == fCurContext);
24116e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com        fCurContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf);
242b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
243b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        if (NULL == fCurContext || NULL == fCurIntf) {
244b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            // We need some context and interface to see results
245b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            SkSafeUnref(fCurContext);
246b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            SkSafeUnref(fCurIntf);
247d8ed85101ee77ad2cb0c186a79d197698a75d246commit-bot@chromium.org            fCurContext = NULL;
248d8ed85101ee77ad2cb0c186a79d197698a75d246commit-bot@chromium.org            fCurIntf = NULL;
249098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com            SkDebugf("Failed to setup 3D");
250b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
251b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            win->detach();
25274913722bfe5e4b6810545891958e3d8e9c63791bsalomon@google.com        }
253cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif // SK_SUPPORT_GPU
254b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        // call windowSizeChanged to create the render target
255cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com        this->windowSizeChanged(win);
256098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    }
257098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com
258b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com    virtual void tearDownBackend(SampleWindow *win) {
259cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
260b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        SkSafeUnref(fCurContext);
261b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        fCurContext = NULL;
262b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
263b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        SkSafeUnref(fCurIntf);
264b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        fCurIntf = NULL;
265b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
266b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        SkSafeUnref(fCurRenderTarget);
267b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        fCurRenderTarget = NULL;
268cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
269ddd40e5e96c43ff6730745b53abf09bf0129f6d2bsalomon@google.com        win->detach();
270ddd40e5e96c43ff6730745b53abf09bf0129f6d2bsalomon@google.com        fBackend = kNone_BackEndType;
271098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    }
272098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com
2735957f47e8de0ff55b8c0bf84984d4915c7c72825reed@google.com    virtual SkCanvas* createCanvas(SampleWindow::DeviceType dType,
2745957f47e8de0ff55b8c0bf84984d4915c7c72825reed@google.com                                   SampleWindow* win) {
275cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
27682502e2e3966a9b0e48665f20154eaacd2a452b3bsalomon@google.com        if (IsGpuDeviceType(dType) && NULL != fCurContext) {
2771f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com            SkAutoTUnref<SkBaseDevice> device(new SkGpuDevice(fCurContext, fCurRenderTarget));
27882502e2e3966a9b0e48665f20154eaacd2a452b3bsalomon@google.com            return new SkCanvas(device);
27982502e2e3966a9b0e48665f20154eaacd2a452b3bsalomon@google.com        } else
280cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
28182502e2e3966a9b0e48665f20154eaacd2a452b3bsalomon@google.com        {
28282502e2e3966a9b0e48665f20154eaacd2a452b3bsalomon@google.com            return NULL;
283098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com        }
284098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    }
285098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com
286098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    virtual void publishCanvas(SampleWindow::DeviceType dType,
287098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com                               SkCanvas* canvas,
288098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com                               SampleWindow* win) {
289cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
290b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        if (fCurContext) {
291098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com            // in case we have queued drawing calls
292b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            fCurContext->flush();
293b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
29482502e2e3966a9b0e48665f20154eaacd2a452b3bsalomon@google.com            if (!IsGpuDeviceType(dType)) {
295098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com                // need to send the raster bits to the (gpu) window
296b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                fCurContext->setRenderTarget(fCurRenderTarget);
297098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com                const SkBitmap& bm = win->getBitmap();
298b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                fCurRenderTarget->writePixels(0, 0, bm.width(), bm.height(),
299fb6fefed0976d7fed193155900a186dc880451b6commit-bot@chromium.org                                             SkImageInfo2GrPixelConfig(bm.colorType(),
300fb6fefed0976d7fed193155900a186dc880451b6commit-bot@chromium.org                                                                       bm.alphaType()),
3016f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com                                             bm.getPixels(),
3026f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com                                             bm.rowBytes());
303098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com            }
304098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com        }
305cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
306b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
307b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        win->present();
308098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    }
309098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com
310098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    virtual void windowSizeChanged(SampleWindow* win) {
311cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
312b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        if (fCurContext) {
31364cc810ad165724f9c666a75bd52e41c67f13564bsalomon@google.com            AttachmentInfo attachmentInfo;
31464cc810ad165724f9c666a75bd52e41c67f13564bsalomon@google.com            win->attach(fBackend, fMSAASampleCount, &attachmentInfo);
315098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com
31616e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com            GrBackendRenderTargetDesc desc;
317e1ca705cac4b946993f6cbf798e2a0ba27e739f3reed@google.com            desc.fWidth = SkScalarRoundToInt(win->width());
318e1ca705cac4b946993f6cbf798e2a0ba27e739f3reed@google.com            desc.fHeight = SkScalarRoundToInt(win->height());
319fec0bc3fc13481f5bcb341ab2d2d695911f39bd4bsalomon@google.com            desc.fConfig = kSkia8888_GrPixelConfig;
3203cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org            desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
32164cc810ad165724f9c666a75bd52e41c67f13564bsalomon@google.com            desc.fSampleCnt = attachmentInfo.fSampleCount;
32264cc810ad165724f9c666a75bd52e41c67f13564bsalomon@google.com            desc.fStencilBits = attachmentInfo.fStencilBits;
323098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com            GrGLint buffer;
324b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
325e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com            desc.fRenderTargetHandle = buffer;
326098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com
327b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            SkSafeUnref(fCurRenderTarget);
32816e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com            fCurRenderTarget = fCurContext->wrapBackendRenderTarget(desc);
32974913722bfe5e4b6810545891958e3d8e9c63791bsalomon@google.com        }
330cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
331098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    }
332098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com
333b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com    virtual GrContext* getGrContext() {
334cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
335b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        return fCurContext;
336cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#else
337cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com        return NULL;
338cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
339098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    }
3401195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com
3411195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com    virtual GrRenderTarget* getGrRenderTarget() SK_OVERRIDE {
342cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
3431195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com        return fCurRenderTarget;
344cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#else
345cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com        return NULL;
346cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
3471195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com    }
3481195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com
349098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.comprivate:
350cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com
351cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
352b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com    GrContext*              fCurContext;
353b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com    const GrGLInterface*    fCurIntf;
354b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com    GrRenderTarget*         fCurRenderTarget;
355cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com    int fMSAASampleCount;
356cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
357b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
358b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com    SkOSWindow::SkBackEndTypes fBackend;
359b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
360b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com    typedef SampleWindow::DeviceManager INHERITED;
361098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com};
362098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com
363098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com///////////////
3642eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgstatic const char view_inval_msg[] = "view-inval-msg";
3652eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
36662b65b0913566381db468277de07e5dd2a9237caScroggovoid SampleWindow::postInvalDelay() {
36787fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com    (new SkEvent(view_inval_msg, this->getSinkID()))->postDelay(1);
3682eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org}
3692eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
3702eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgstatic bool isInvalEvent(const SkEvent& evt) {
3712eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    return evt.isType(view_inval_msg);
3722eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org}
3732eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org//////////////////
3742eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
37548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comSkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func)
37648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    : fCreateFunc(func) {
37748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com}
37848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
3798301de132eb016f56450017c0000f9b5d913809dbsalomon@google.comSkView* SkFuncViewFactory::operator() () const {
38048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    return (*fCreateFunc)();
38148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com}
38248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
38348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com#include "GMSampleView.h"
38448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
38548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comSkGMSampleViewFactory::SkGMSampleViewFactory(GMFactoryFunc func)
38648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    : fFunc(func) {
38748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com}
38848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
38948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comSkView* SkGMSampleViewFactory::operator() () const {
390b21fac156d9287d6c0cfd446d707c4c7be6fae6ecommit-bot@chromium.org    skiagm::GM* gm = fFunc(NULL);
391b21fac156d9287d6c0cfd446d707c4c7be6fae6ecommit-bot@chromium.org    gm->setMode(skiagm::GM::kSample_Mode);
392b21fac156d9287d6c0cfd446d707c4c7be6fae6ecommit-bot@chromium.org    return new GMSampleView(gm);
39348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com}
39448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkViewRegister* SkViewRegister::gHead;
39648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comSkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) {
39748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    fFact->ref();
39848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    fChain = gHead;
39948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    gHead = this;
40048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com}
40148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
40248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comSkViewRegister::SkViewRegister(SkViewCreateFunc func) {
40348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    fFact = new SkFuncViewFactory(func);
40448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    fChain = gHead;
40548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    gHead = this;
40648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com}
40748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
40848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comSkViewRegister::SkViewRegister(GMFactoryFunc func) {
40948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    fFact = new SkGMSampleViewFactory(func);
41048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    fChain = gHead;
41148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    gHead = this;
41248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com}
41348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
41448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass AutoUnrefArray {
41548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.compublic:
41648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    AutoUnrefArray() {}
41748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    ~AutoUnrefArray() {
41848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com        int count = fObjs.count();
41948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com        for (int i = 0; i < count; ++i) {
42048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com            fObjs[i]->unref();
42148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com        }
42248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    }
42348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    SkRefCnt*& push_back() { return *fObjs.append(); }
4244605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
42548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comprivate:
42648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    SkTDArray<SkRefCnt*> fObjs;
42748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com};
42848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
42948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// registers GMs as Samples
43048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// This can't be performed during static initialization because it could be
43148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// run before GMRegistry has been fully built.
43202939ce4bddd3223d9e8cc98d4abd02939b7f70acaryclark@google.comstatic void SkGMRegistyToSampleRegistry() {
4338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool gOnce;
4344605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    static AutoUnrefArray fRegisters;
43548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
4368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (!gOnce) {
43748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com        const skiagm::GMRegistry* gmreg = skiagm::GMRegistry::Head();
43848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com        while (gmreg) {
43948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com            fRegisters.push_back() = new SkViewRegister(gmreg->factory());
44048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com            gmreg = gmreg->next();
44148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com        }
4428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        gOnce = true;
4438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
4448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
4458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
446f0b5f68807cfa18cce0647d275778147901945cdreed@google.com//////////////////////////////////////////////////////////////////////////////
447f0b5f68807cfa18cce0647d275778147901945cdreed@google.com
448569e0435af62bdb88a662fa0592f75673a17afd9reed@google.comenum FlipAxisEnum {
449569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com    kFlipAxis_X = (1 << 0),
450569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com    kFlipAxis_Y = (1 << 1)
451569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com};
452f0b5f68807cfa18cce0647d275778147901945cdreed@google.com
453569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com#include "SkDrawFilter.h"
454569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com
45596aabc850835f6d08a117c0277be93848ddbf785bungeman@google.comstruct HintingState {
45696aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    SkPaint::Hinting hinting;
45796aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    const char* name;
45896aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    const char* label;
45996aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com};
46096aabc850835f6d08a117c0277be93848ddbf785bungeman@google.comstatic HintingState gHintingStates[] = {
46196aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    {SkPaint::kNo_Hinting, "Mixed", NULL },
46296aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    {SkPaint::kNo_Hinting, "None", "H0 " },
46396aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    {SkPaint::kSlight_Hinting, "Slight", "Hs " },
46496aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    {SkPaint::kNormal_Hinting, "Normal", "Hn " },
46596aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    {SkPaint::kFull_Hinting, "Full", "Hf " },
46696aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com};
46796aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com
46815bc13d20fe79914c7128881e45064becd11bc9dreed@google.comstruct FilterLevelState {
46915bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    SkPaint::FilterLevel    fLevel;
47015bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    const char*             fName;
47115bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    const char*             fLabel;
47215bc13d20fe79914c7128881e45064becd11bc9dreed@google.com};
47315bc13d20fe79914c7128881e45064becd11bc9dreed@google.comstatic FilterLevelState gFilterLevelStates[] = {
47415bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    { SkPaint::kNone_FilterLevel,   "Mixed",    NULL    },
47515bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    { SkPaint::kNone_FilterLevel,   "None",     "F0 "   },
47615bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    { SkPaint::kLow_FilterLevel,    "Low",      "F1 "   },
47715bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    { SkPaint::kMedium_FilterLevel, "Medium",   "F2 "   },
47815bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    { SkPaint::kHigh_FilterLevel,   "High",     "F3 "   },
47915bc13d20fe79914c7128881e45064becd11bc9dreed@google.com};
48015bc13d20fe79914c7128881e45064becd11bc9dreed@google.com
481569e0435af62bdb88a662fa0592f75673a17afd9reed@google.comclass FlagsDrawFilter : public SkDrawFilter {
482569e0435af62bdb88a662fa0592f75673a17afd9reed@google.compublic:
48315bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    FlagsDrawFilter(SkOSMenu::TriState lcd, SkOSMenu::TriState aa,
48415bc13d20fe79914c7128881e45064becd11bc9dreed@google.com                    SkOSMenu::TriState subpixel, int hinting, int filterlevel)
48515bc13d20fe79914c7128881e45064becd11bc9dreed@google.com        : fLCDState(lcd)
48615bc13d20fe79914c7128881e45064becd11bc9dreed@google.com        , fAAState(aa)
48715bc13d20fe79914c7128881e45064becd11bc9dreed@google.com        , fSubpixelState(subpixel)
48815bc13d20fe79914c7128881e45064becd11bc9dreed@google.com        , fHintingState(hinting)
48915bc13d20fe79914c7128881e45064becd11bc9dreed@google.com        , fFilterLevelIndex(filterlevel)
49015bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    {
49115bc13d20fe79914c7128881e45064becd11bc9dreed@google.com        SkASSERT((unsigned)filterlevel < SK_ARRAY_COUNT(gFilterLevelStates));
49215bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    }
493f0b5f68807cfa18cce0647d275778147901945cdreed@google.com
494971aca75572ed6e0c5e1cc959173dc58ca7b6b8dreed@google.com    virtual bool filter(SkPaint* paint, Type t) {
495db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com        if (kText_Type == t && SkOSMenu::kMixedState != fLCDState) {
496db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com            paint->setLCDRenderText(SkOSMenu::kOnState == fLCDState);
497569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com        }
498db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com        if (SkOSMenu::kMixedState != fAAState) {
499db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com            paint->setAntiAlias(SkOSMenu::kOnState == fAAState);
500f0b5f68807cfa18cce0647d275778147901945cdreed@google.com        }
50115bc13d20fe79914c7128881e45064becd11bc9dreed@google.com        if (0 != fFilterLevelIndex) {
50215bc13d20fe79914c7128881e45064becd11bc9dreed@google.com            paint->setFilterLevel(gFilterLevelStates[fFilterLevelIndex].fLevel);
503176753aa6d695ebcb76c0f82265ad69dc41dc593reed@google.com        }
50496aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com        if (SkOSMenu::kMixedState != fSubpixelState) {
50596aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com            paint->setSubpixelText(SkOSMenu::kOnState == fSubpixelState);
50696aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com        }
507055aa52b2d450f41d0488bd50797d6e88d02852ebungeman@google.com        if (0 != fHintingState && fHintingState < (int)SK_ARRAY_COUNT(gHintingStates)) {
50896aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com            paint->setHinting(gHintingStates[fHintingState].hinting);
50909e3baaaf54a8a3c701cfc5ad24a8180464a4742reed@google.com        }
510971aca75572ed6e0c5e1cc959173dc58ca7b6b8dreed@google.com        return true;
511f0b5f68807cfa18cce0647d275778147901945cdreed@google.com    }
512f0b5f68807cfa18cce0647d275778147901945cdreed@google.com
513f0b5f68807cfa18cce0647d275778147901945cdreed@google.comprivate:
514db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    SkOSMenu::TriState  fLCDState;
515db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    SkOSMenu::TriState  fAAState;
51696aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    SkOSMenu::TriState  fSubpixelState;
51796aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    int fHintingState;
51815bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    int fFilterLevelIndex;
519f0b5f68807cfa18cce0647d275778147901945cdreed@google.com};
520f0b5f68807cfa18cce0647d275778147901945cdreed@google.com
5218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
5228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
523dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org#define MAX_ZOOM_LEVEL  8
524dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org#define MIN_ZOOM_LEVEL  -8
525dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org
526f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comstatic const char gCharEvtName[] = "SampleCode_Char_Event";
527f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comstatic const char gKeyEvtName[] = "SampleCode_Key_Event";
5288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic const char gTitleEvtName[] = "SampleCode_Title_Event";
5298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
530f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comstatic const char gFastTextEvtName[] = "SampleCode_FastText_Event";
5312072db89016c4e53a648f4d4cec08b1d6be8558creed@google.comstatic const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle";
532f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
533f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.combool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
534f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
535f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        if (outUni) {
536f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            *outUni = evt.getFast32();
537f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
538f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        return true;
539f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
540f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    return false;
541f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
542f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
543f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.combool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
544f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
545f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        if (outKey) {
546f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            *outKey = (SkKey)evt.getFast32();
547f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
548f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        return true;
549f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
550f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    return false;
551f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
5528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SampleCode::TitleQ(const SkEvent& evt) {
5548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
5558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SampleCode::TitleR(SkEvent* evt, const char title[]) {
5588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(evt && TitleQ(*evt));
5598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    evt->setString(gTitleEvtName, title);
5608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
562db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.combool SampleCode::RequestTitle(SkView* view, SkString* title) {
563db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    SkEvent evt(gTitleEvtName);
564db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    if (view->doQuery(&evt)) {
565db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com        title->set(evt.findString(gTitleEvtName));
566db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com        return true;
567db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    }
568db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    return false;
569db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com}
570db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com
5718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SampleCode::PrefSizeQ(const SkEvent& evt) {
5728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
5738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
5768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(evt && PrefSizeQ(*evt));
5778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar size[2];
5788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    size[0] = width;
5798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    size[1] = height;
5808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    evt->setScalars(gPrefSizeEvtName, 2, size);
5818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
5828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
583f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.combool SampleCode::FastTextQ(const SkEvent& evt) {
584f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
585f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
586f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
587f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com///////////////////////////////////////////////////////////////////////////////
588f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
5894417740a480786ca65379ba5b398c500ba34ee18reed@android.comstatic SkMSec gAnimTime;
590f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comstatic SkMSec gAnimTimePrev;
591f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
5924417740a480786ca65379ba5b398c500ba34ee18reed@android.comSkMSec SampleCode::GetAnimTime() { return gAnimTime; }
593f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comSkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
594f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comSkScalar SampleCode::GetAnimSecondsDelta() {
595f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
596f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
5974417740a480786ca65379ba5b398c500ba34ee18reed@android.com
5984417740a480786ca65379ba5b398c500ba34ee18reed@android.comSkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
599f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    // since gAnimTime can be up to 32 bits, we can't convert it to a float
600f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    // or we'll lose the low bits. Hence we use doubles for the intermediate
601f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    // calculations
602f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    double seconds = (double)gAnimTime / 1000.0;
603f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    double value = SkScalarToDouble(speed) * seconds;
6044417740a480786ca65379ba5b398c500ba34ee18reed@android.com    if (period) {
605f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        value = ::fmod(value, SkScalarToDouble(period));
6064417740a480786ca65379ba5b398c500ba34ee18reed@android.com    }
607f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    return SkDoubleToScalar(value);
6084417740a480786ca65379ba5b398c500ba34ee18reed@android.com}
6094417740a480786ca65379ba5b398c500ba34ee18reed@android.com
6108500322537c5158a4632be6805b0c4210a20558bbsalomon@google.comSkScalar SampleCode::GetAnimSinScalar(SkScalar amplitude,
6118500322537c5158a4632be6805b0c4210a20558bbsalomon@google.com                                      SkScalar periodInSec,
6128500322537c5158a4632be6805b0c4210a20558bbsalomon@google.com                                      SkScalar phaseInSec) {
6138500322537c5158a4632be6805b0c4210a20558bbsalomon@google.com    if (!periodInSec) {
6148500322537c5158a4632be6805b0c4210a20558bbsalomon@google.com        return 0;
6158500322537c5158a4632be6805b0c4210a20558bbsalomon@google.com    }
6168500322537c5158a4632be6805b0c4210a20558bbsalomon@google.com    double t = (double)gAnimTime / 1000.0 + phaseInSec;
6178500322537c5158a4632be6805b0c4210a20558bbsalomon@google.com    t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
6188500322537c5158a4632be6805b0c4210a20558bbsalomon@google.com    amplitude = SK_ScalarHalf * amplitude;
6198500322537c5158a4632be6805b0c4210a20558bbsalomon@google.com    return SkScalarMul(amplitude, SkDoubleToScalar(sin(t))) + amplitude;
6208500322537c5158a4632be6805b0c4210a20558bbsalomon@google.com}
6218500322537c5158a4632be6805b0c4210a20558bbsalomon@google.com
622cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.orgenum TilingMode {
623cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    kNo_Tiling,
624cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    kAbs_128x128_Tiling,
625cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    kAbs_256x256_Tiling,
626cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    kRel_4x4_Tiling,
627cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    kRel_1x16_Tiling,
628cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    kRel_16x1_Tiling,
629cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org
630cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    kLast_TilingMode_Enum
631cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org};
632cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org
633cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.orgstruct TilingInfo {
634cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    const char* label;
635cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    SkScalar    w, h;
636cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org};
637cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org
638bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.orgstatic const struct TilingInfo gTilingInfo[] = {
639cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    { "No tiling", SK_Scalar1        , SK_Scalar1         }, // kNo_Tiling
640cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    { "128x128"  , SkIntToScalar(128), SkIntToScalar(128) }, // kAbs_128x128_Tiling
641cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    { "256x256"  , SkIntToScalar(256), SkIntToScalar(256) }, // kAbs_256x256_Tiling
642cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    { "1/4x1/4"  , SK_Scalar1 / 4    , SK_Scalar1 / 4     }, // kRel_4x4_Tiling
643cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    { "1/1x1/16" , SK_Scalar1        , SK_Scalar1 / 16    }, // kRel_1x16_Tiling
644cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    { "1/16x1/1" , SK_Scalar1 / 16   , SK_Scalar1         }, // kRel_16x1_Tiling
645cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org};
646cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.orgSK_COMPILE_ASSERT((SK_ARRAY_COUNT(gTilingInfo) == kLast_TilingMode_Enum),
647cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org                  Incomplete_tiling_labels);
648cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org
649bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.orgSkSize SampleWindow::tileSize() const {
650bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org    SkASSERT((TilingMode)fTilingMode < kLast_TilingMode_Enum);
651bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org    const struct TilingInfo* info = gTilingInfo + fTilingMode;
652bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org    return SkSize::Make(info->w > SK_Scalar1 ? info->w : this->width() * info->w,
653bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org                        info->h > SK_Scalar1 ? info->h : this->height() * info->h);
654bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org}
655cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org//////////////////////////////////////////////////////////////////////////////
656cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org
657f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comstatic SkView* curr_view(SkWindow* wind) {
658f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkView::F2BIter iter(wind);
659f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    return iter.next();
660f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
661f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
6626f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.orgstatic bool curr_title(SkWindow* wind, SkString* title) {
6636f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    SkView* view = curr_view(wind);
6646f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    if (view) {
6656f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org        SkEvent evt(gTitleEvtName);
6666f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org        if (view->doQuery(&evt)) {
6676f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org            title->set(evt.findString(gTitleEvtName));
6686f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org            return true;
6696f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org        }
6706f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    }
6716f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    return false;
6726f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org}
6736f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org
6742c8208f3a444098aee562f5a1ef620142807eda2Scroggovoid SampleWindow::setZoomCenter(float x, float y)
6752c8208f3a444098aee562f5a1ef620142807eda2Scroggo{
6764b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org    fZoomCenterX = x;
6774b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org    fZoomCenterY = y;
6782c8208f3a444098aee562f5a1ef620142807eda2Scroggo}
6798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
6800f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggobool SampleWindow::zoomIn()
6810f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo{
6820f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    // Arbitrarily decided
6830f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    if (fFatBitsScale == 25) return false;
6840f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    fFatBitsScale++;
6850f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    this->inval(NULL);
6860f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    return true;
6870f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo}
6880f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo
6890f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggobool SampleWindow::zoomOut()
6900f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo{
6910f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    if (fFatBitsScale == 1) return false;
6920f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    fFatBitsScale--;
6930f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    this->inval(NULL);
6940f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    return true;
6950f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo}
6960f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo
6970f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggovoid SampleWindow::updatePointer(int x, int y)
6980f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo{
6990f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    fMouseX = x;
7000f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    fMouseY = y;
7010f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    if (fShowZoomer) {
7020f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        this->inval(NULL);
7030f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    }
7040f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo}
7050f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo
706098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.comstatic inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
707098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    static const SampleWindow::DeviceType gCT[] = {
708098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com        SampleWindow::kPicture_DeviceType,
709cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
710098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com        SampleWindow::kGPU_DeviceType,
711b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com#if SK_ANGLE
712b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        SampleWindow::kANGLE_DeviceType,
713cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif // SK_ANGLE
71474913722bfe5e4b6810545891958e3d8e9c63791bsalomon@google.com        SampleWindow::kRaster_DeviceType, // skip the null gpu device in normal cycling
715cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif // SK_SUPPORT_GPU
716098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com        SampleWindow::kRaster_DeviceType
7178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
718cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com    SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gCT) == SampleWindow::kDeviceTypeCnt, array_size_mismatch);
7198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return gCT[ct];
7208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
7218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
7228570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.comstatic void usage(const char * argv0) {
723f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    SkDebugf("%s [--slide sampleName] [-i resourcePath] [--msaa sampleCount] [--pictureDir dirPath] [--picture path] [--sort]\n", argv0);
724f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com#ifdef SAMPLE_PDF_FILE_VIEWER
725f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    SkDebugf("                [--pdfDir pdfPath]\n");
726f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    SkDebugf("    pdfPath: path to directory pdf files are read from\n");
727f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com#endif  // SAMPLE_PDF_FILE_VIEWER
7288570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com    SkDebugf("    sampleName: sample at which to start.\n");
7298570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com    SkDebugf("    resourcePath: directory that stores image resources.\n");
7301195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com    SkDebugf("    msaa: request multisampling with the given sample count.\n");
7314605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    SkDebugf("    dirPath: path to directory skia pictures are read from\n");
7324605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    SkDebugf("    path: path to skia picture\n");
733f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    SkDebugf("    --sort: sort samples by title, this would help to compare pdf rendering (P:foo.pdf) with skp rendering (P:foo.pdf)\n");
734f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com}
735f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com
736f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.comstatic SkString getSampleTitle(const SkViewFactory* sampleFactory) {
737f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    SkView* view = (*sampleFactory)();
738f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    SkString title;
739f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    SampleCode::RequestTitle(view, &title);
740f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    view->unref();
741f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    return title;
742f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com}
743f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com
744de36b6948116a3d498ad8d808869ba182a66e206edisonn@google.comstatic bool compareSampleTitle(const SkViewFactory* first, const SkViewFactory* second) {
745f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    return strcmp(getSampleTitle(first).c_str(), getSampleTitle(second).c_str()) < 0;
7468570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com}
7478570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com
7484605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.comSampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager)
7494750fa55f27e43770e7f5f3161ebdbac42e0da8arobertphillips@google.com    : INHERITED(hwnd)
7504750fa55f27e43770e7f5f3161ebdbac42e0da8arobertphillips@google.com    , fDevManager(NULL) {
7518570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com
7527d05ac9508bff5b1c62fec42dacb8aabd4cf8adbreed@google.com    fCurrIndex = -1;
7537d05ac9508bff5b1c62fec42dacb8aabd4cf8adbreed@google.com
7541830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com    this->registerPictFileSamples(argv, argc);
7554605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    this->registerPictFileSample(argv, argc);
756f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com#ifdef SAMPLE_PDF_FILE_VIEWER
757f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    this->registerPdfFileViewerSamples(argv, argc);
758f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com#endif  // SAMPLE_PDF_FILE_VIEWER
75976d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com    SkGMRegistyToSampleRegistry();
76076d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com    {
76176d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com        const SkViewRegister* reg = SkViewRegister::Head();
76276d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com        while (reg) {
76376d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com            *fSamples.append() = reg->factory();
76476d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com            reg = reg->next();
76576d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com        }
76676d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com    }
76776d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com
768f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    bool sort = false;
769f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    for (int i = 0; i < argc; ++i) {
770f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com        if (!strcmp(argv[i], "--sort")) {
771f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com            sort = true;
772f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com            break;
773f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com        }
774f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    }
775f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com
776f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    if (sort) {
777f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com        // Sort samples, so foo.skp and foo.pdf are consecutive and we can quickly spot where
778f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com        // skp -> pdf -> png fails.
779f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com        SkTQSort(fSamples.begin(), fSamples.end() ? fSamples.end() - 1 : NULL, compareSampleTitle);
780f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    }
781f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com
7821195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com    fMSAASampleCount = 0;
7838570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com
784bcbc1788b478b1e54079318ad073e8490aa66faetfarina    SkCommandLineFlags::Parse(argc, argv);
785bcbc1788b478b1e54079318ad073e8490aa66faetfarina
7868570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com    const char* const commandName = argv[0];
7878570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com    char* const* stop = argv + argc;
7888570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com    for (++argv; argv < stop; ++argv) {
789bcbc1788b478b1e54079318ad073e8490aa66faetfarina        if (strcmp(*argv, "--slide") == 0) {
79076d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com            argv++;
79176d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com            if (argv < stop && **argv) {
79276d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com                fCurrIndex = findByTitle(*argv);
79376d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com                if (fCurrIndex < 0) {
79476d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com                    fprintf(stderr, "Unknown sample \"%s\"\n", *argv);
7957265e725555098637498db2f397623d7991ceb4brobertphillips@google.com                    listTitles();
79676d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com                }
7978570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com            }
7981195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com        } else if (strcmp(*argv, "--msaa") == 0) {
7991195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com            ++argv;
8001195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com            if (argv < stop && **argv) {
8011195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com                fMSAASampleCount = atoi(*argv);
8021195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com            }
8037265e725555098637498db2f397623d7991ceb4brobertphillips@google.com        } else if (strcmp(*argv, "--list") == 0) {
8047265e725555098637498db2f397623d7991ceb4brobertphillips@google.com            listTitles();
805805ca19625ff477bf1354fa0a1c7cacebccd8940halcanary@google.com        } else if (strcmp(*argv, "--pictureDir") == 0) {
806805ca19625ff477bf1354fa0a1c7cacebccd8940halcanary@google.com            ++argv;  // This case is dealt with in registerPictFileSamples().
807805ca19625ff477bf1354fa0a1c7cacebccd8940halcanary@google.com        } else if (strcmp(*argv, "--picture") == 0) {
808805ca19625ff477bf1354fa0a1c7cacebccd8940halcanary@google.com            ++argv;  // This case is dealt with in registerPictFileSample().
8091195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com        }
810d3b9fbbc48c13a1b2a664cf7e01374a44c201f51robertphillips@google.com        else {
81176d4021ee1bfbf31da274e9feb4c675cfd86e6d3robertphillips@google.com            usage(commandName);
8128570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com        }
8138570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com    }
8148570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com
8158570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com    if (fCurrIndex < 0) {
8168570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com        SkString title;
8178570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com        if (readTitleFromPrefs(&title)) {
8188570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com            fCurrIndex = findByTitle(title.c_str());
8198570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com        }
8208570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com    }
8218570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com
8228570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com    if (fCurrIndex < 0) {
8238570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com        fCurrIndex = 0;
8248570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com    }
8258570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com
8263cec4d71e6f53fda99705427f9820d025cfbf64breed@google.com    gSampleWindow = this;
8273cec4d71e6f53fda99705427f9820d025cfbf64breed@google.com
8281f3942110a6fcec86eb411b49eab16d2bd91eb5byangsu@google.com#ifdef  PIPE_FILE
8291f3942110a6fcec86eb411b49eab16d2bd91eb5byangsu@google.com    //Clear existing file or create file if it doesn't exist
8301f3942110a6fcec86eb411b49eab16d2bd91eb5byangsu@google.com    FILE* f = fopen(FILE_PATH, "wb");
8311f3942110a6fcec86eb411b49eab16d2bd91eb5byangsu@google.com    fclose(f);
8321f3942110a6fcec86eb411b49eab16d2bd91eb5byangsu@google.com#endif
8334605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
834bd8d7adb0ac9533a04e7b7a481371d4c66bbbc9brobertphillips@google.com    fDeviceType = kRaster_DeviceType;
835b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
836b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com#if DEFAULT_TO_GPU
837b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com    fDeviceType = kGPU_DeviceType;
838b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com#endif
839b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com#if SK_ANGLE && DEFAULT_TO_ANGLE
840b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com    fDeviceType = kANGLE_DeviceType;
84153e96a14cac520c1fd071ad65d80b5bacc5c4e28robertphillips@google.com#endif
842b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
8438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fUseClip = false;
844e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com    fNClip = false;
8458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fAnimating = false;
8466c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.com    fRotate = false;
847b17859133aeb0ebe80805fbd437759e2b956d859bungeman@google.com    fRotateAnimTime = 0;
848e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com    fPerspAnim = false;
849e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com    fPerspAnimTime = 0;
850f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    fRequestGrabImage = false;
851b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com    fPipeState = SkOSMenu::kOffState;
852cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    fTilingMode = kNo_Tiling;
8532eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    fMeasureFPS = false;
854db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    fLCDState = SkOSMenu::kMixedState;
855db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    fAAState = SkOSMenu::kMixedState;
85696aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    fSubpixelState = SkOSMenu::kMixedState;
85796aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    fHintingState = 0;
85815bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    fFilterLevelIndex = 0;
859569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com    fFlipAxis = 0;
8608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fScrollTestX = fScrollTestY = 0;
8618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8620f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    fMouseX = fMouseY = 0;
8633ce59dc4999de5d6c6913af662c8b0b8020a4ef7mike@reedtribe.org    fFatBitsScale = 8;
8640f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
8650f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    fShowZoomer = false;
8664605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
867dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org    fZoomLevel = 0;
868dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org    fZoomScale = SK_Scalar1;
8694605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
870db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    fMagnify = false;
8714605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
8728ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo    fSaveToPdf = false;
8738ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo    fPdfCanvas = NULL;
8748ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo
875db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    fTransitionNext = 6;
876db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    fTransitionPrev = 2;
8774605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
878921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com    int sinkID = this->getSinkID();
8797dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fAppMenu = new SkOSMenu;
8807dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fAppMenu->setTitle("Global Settings");
881db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    int itemID;
8824605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
8834605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    itemID =fAppMenu->appendList("Device Type", "Device Type", sinkID, 0,
8844605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com                                "Raster", "Picture", "OpenGL",
885b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com#if SK_ANGLE
886b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                                "ANGLE",
887b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com#endif
888b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com                                NULL);
8897dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 'd');
8907dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    itemID = fAppMenu->appendTriState("AA", "AA", sinkID, fAAState);
8917dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 'b');
8927dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    itemID = fAppMenu->appendTriState("LCD", "LCD", sinkID, fLCDState);
8937dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 'l');
89415bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    itemID = fAppMenu->appendList("FilterLevel", "FilterLevel", sinkID, fFilterLevelIndex,
89515bc13d20fe79914c7128881e45064becd11bc9dreed@google.com                                  gFilterLevelStates[0].fName,
89615bc13d20fe79914c7128881e45064becd11bc9dreed@google.com                                  gFilterLevelStates[1].fName,
89715bc13d20fe79914c7128881e45064becd11bc9dreed@google.com                                  gFilterLevelStates[2].fName,
89815bc13d20fe79914c7128881e45064becd11bc9dreed@google.com                                  gFilterLevelStates[3].fName,
89915bc13d20fe79914c7128881e45064becd11bc9dreed@google.com                                  gFilterLevelStates[4].fName,
90015bc13d20fe79914c7128881e45064becd11bc9dreed@google.com                                  NULL);
9017dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 'n');
90296aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    itemID = fAppMenu->appendTriState("Subpixel", "Subpixel", sinkID, fSubpixelState);
90396aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 's');
90496aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    itemID = fAppMenu->appendList("Hinting", "Hinting", sinkID, fHintingState,
90596aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com                                  gHintingStates[0].name,
90696aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com                                  gHintingStates[1].name,
90796aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com                                  gHintingStates[2].name,
90896aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com                                  gHintingStates[3].name,
909a9133bbcc6deceb5cd883305767093e10c17c359bungeman@google.com                                  gHintingStates[4].name,
910a9133bbcc6deceb5cd883305767093e10c17c359bungeman@google.com                                  NULL);
9117dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 'h');
91267b89eeeb07aa0485db233b5f356031fe15b57c4reed@google.com
913b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com    fUsePipeMenuItemID = fAppMenu->appendTriState("Pipe", "Pipe" , sinkID,
9144605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com                                                  fPipeState);
915b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com    fAppMenu->assignKeyEquivalentToItem(fUsePipeMenuItemID, 'P');
91667b89eeeb07aa0485db233b5f356031fe15b57c4reed@google.com
917cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    itemID =fAppMenu->appendList("Tiling", "Tiling", sinkID, fTilingMode,
918cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org                                 gTilingInfo[kNo_Tiling].label,
919cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org                                 gTilingInfo[kAbs_128x128_Tiling].label,
920cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org                                 gTilingInfo[kAbs_256x256_Tiling].label,
921cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org                                 gTilingInfo[kRel_4x4_Tiling].label,
922cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org                                 gTilingInfo[kRel_1x16_Tiling].label,
923cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org                                 gTilingInfo[kRel_16x1_Tiling].label,
924cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org                                 NULL);
92567b89eeeb07aa0485db233b5f356031fe15b57c4reed@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 't');
92667b89eeeb07aa0485db233b5f356031fe15b57c4reed@google.com
9274605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    itemID = fAppMenu->appendSwitch("Slide Show", "Slide Show" , sinkID, false);
9284605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 'a');
9294605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    itemID = fAppMenu->appendSwitch("Clip", "Clip" , sinkID, fUseClip);
9307dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 'c');
9314605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    itemID = fAppMenu->appendSwitch("Flip X", "Flip X" , sinkID, false);
9327dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 'x');
9337dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    itemID = fAppMenu->appendSwitch("Flip Y", "Flip Y" , sinkID, false);
9347dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 'y');
9357dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    itemID = fAppMenu->appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
9367dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 'z');
9377dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    itemID = fAppMenu->appendSwitch("Magnify", "Magnify" , sinkID, fMagnify);
9387dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 'm');
9394605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    itemID =fAppMenu->appendList("Transition-Next", "Transition-Next", sinkID,
9404605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com                                fTransitionNext, "Up", "Up and Right", "Right",
9414605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com                                "Down and Right", "Down", "Down and Left",
942db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com                                "Left", "Up and Left", NULL);
9437dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 'j');
9444605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    itemID =fAppMenu->appendList("Transition-Prev", "Transition-Prev", sinkID,
9454605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com                                fTransitionPrev, "Up", "Up and Right", "Right",
9464605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com                                "Down and Right", "Down", "Down and Left",
947db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com                                "Left", "Up and Left", NULL);
9487dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 'k');
9497dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    itemID = fAppMenu->appendAction("Save to PDF", sinkID);
9507dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fAppMenu->assignKeyEquivalentToItem(itemID, 'e');
9514605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
9527dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    this->addMenu(fAppMenu);
9537dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fSlideMenu = new SkOSMenu;
9547dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    this->addMenu(fSlideMenu);
9554605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
95628fcae2ec77eb16a79e155f8d788b20457f1c951commit-bot@chromium.org    this->setColorType(kN32_SkColorType);
9572fbc7fa460c99bffa78b9afb9aaa4dc0db68e1babsalomon@google.com    this->setVisibleP(true);
958f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    this->setClipToBounds(false);
9598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
96048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    this->loadView((*fSamples[fCurrIndex])());
9614605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
962840e9f3776ea99d9c1cfe5ccc0b85f3db3da9affbsalomon@google.com    fPDFData = NULL;
963f0b5f68807cfa18cce0647d275778147901945cdreed@google.com
964098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    if (NULL == devManager) {
965098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com        fDevManager = new DefaultDeviceManager();
966098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    } else {
967098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com        devManager->ref();
968098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com        fDevManager = devManager;
969098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    }
9701195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com    fDevManager->setUpBackend(this, fMSAASampleCount);
971098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com
972b4490c7435df46261c00dbfdcdd9d2e9d56d1f52Scroggo    // If another constructor set our dimensions, ensure that our
973b4490c7435df46261c00dbfdcdd9d2e9d56d1f52Scroggo    // onSizeChange gets called.
974b4490c7435df46261c00dbfdcdd9d2e9d56d1f52Scroggo    if (this->height() && this->width()) {
975b4490c7435df46261c00dbfdcdd9d2e9d56d1f52Scroggo        this->onSizeChange();
976b4490c7435df46261c00dbfdcdd9d2e9d56d1f52Scroggo    }
9772072db89016c4e53a648f4d4cec08b1d6be8558creed@google.com
9782072db89016c4e53a648f4d4cec08b1d6be8558creed@google.com    // can't call this synchronously, since it may require a subclass to
9792072db89016c4e53a648f4d4cec08b1d6be8558creed@google.com    // to implement, or the caller may need us to have returned from the
9802072db89016c4e53a648f4d4cec08b1d6be8558creed@google.com    // constructor first. Hence we post an event to ourselves.
9812072db89016c4e53a648f4d4cec08b1d6be8558creed@google.com//    this->updateTitle();
9826de4a7836f1c7988b42cbbfe1a339ef7619aed8fcommit-bot@chromium.org    post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this);
9838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
9848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSampleWindow::~SampleWindow() {
9868ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo    delete fPdfCanvas;
9870f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    fTypeface->unref();
98829038ed2ab5cb87fda5a2a4d3267f0a5aa227a3dreed@google.com
989098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    SkSafeUnref(fDevManager);
9908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
9918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9921830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.comstatic void make_filepath(SkString* path, const char* dir, const SkString& name) {
9931830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com    size_t len = strlen(dir);
9941830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com    path->set(dir);
9951830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com    if (len > 0 && dir[len - 1] != '/') {
9961830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com        path->append("/");
9971830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com    }
9981830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com    path->append(name);
9991830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com}
10001830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com
10014605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.comvoid SampleWindow::registerPictFileSample(char** argv, int argc) {
10024605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    const char* pict = NULL;
10034605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
10044605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    for (int i = 0; i < argc; ++i) {
10054605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com        if (!strcmp(argv[i], "--picture")) {
10064605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com            i += 1;
10074605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com            if (i < argc) {
10084605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com                pict = argv[i];
10094605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com                break;
10104605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com            }
10114605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com        }
10124605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    }
10134605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    if (pict) {
10144605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com        SkString path(pict);
10157d05ac9508bff5b1c62fec42dacb8aabd4cf8adbreed@google.com        fCurrIndex = fSamples.count();
10164605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com        *fSamples.append() = new PictFileFactory(path);
10174605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    }
10184605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com}
10194605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
10201830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.comvoid SampleWindow::registerPictFileSamples(char** argv, int argc) {
10211830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com    const char* pictDir = NULL;
10221830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com
10231830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com    for (int i = 0; i < argc; ++i) {
10241830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com        if (!strcmp(argv[i], "--pictureDir")) {
10251830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com            i += 1;
10261830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com            if (i < argc) {
10271830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com                pictDir = argv[i];
10281830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com                break;
10291830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com            }
10301830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com        }
10311830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com    }
10321830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com    if (pictDir) {
10331830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com        SkOSFile::Iter iter(pictDir, "skp");
10341830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com        SkString filename;
10351830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com        while (iter.next(&filename)) {
10361830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com            SkString path;
10371830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com            make_filepath(&path, pictDir, filename);
10381830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com            *fSamples.append() = new PictFileFactory(path);
10391830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com        }
10401830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com    }
10411830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com}
10421830c7aa3c4045af438fb24ee0bd22598f1bfca7reed@google.com
1043f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com#ifdef SAMPLE_PDF_FILE_VIEWER
1044f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.comvoid SampleWindow::registerPdfFileViewerSamples(char** argv, int argc) {
1045f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    const char* pdfDir = NULL;
1046f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com
1047f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    for (int i = 0; i < argc; ++i) {
1048f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com        if (!strcmp(argv[i], "--pdfDir")) {
1049f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com            i += 1;
1050f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com            if (i < argc) {
1051f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com                pdfDir = argv[i];
1052f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com                break;
1053f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com            }
1054f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com        }
1055f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    }
1056f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    if (pdfDir) {
1057f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com        SkOSFile::Iter iter(pdfDir, "pdf");
1058f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com        SkString filename;
1059f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com        while (iter.next(&filename)) {
1060f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com            SkString path;
1061f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com            make_filepath(&path, pdfDir, filename);
1062f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com            *fSamples.append() = new PdfFileViewerFactory(path);
1063f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com        }
1064f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    }
1065f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com}
1066f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com#endif  // SAMPLE_PDF_FILE_VIEWER
1067f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com
1068f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com
10696f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.orgint SampleWindow::findByTitle(const char title[]) {
10706f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    int i, count = fSamples.count();
10716f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    for (i = 0; i < count; i++) {
10726f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org        if (getSampleTitle(i).equals(title)) {
10736f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org            return i;
10746f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org        }
10756f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    }
1076dd52caaa093845c7c5157be78d94feb72f9cb155mike@reedtribe.org    return -1;
10776f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org}
10786f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org
10797265e725555098637498db2f397623d7991ceb4brobertphillips@google.comvoid SampleWindow::listTitles() {
10807265e725555098637498db2f397623d7991ceb4brobertphillips@google.com    int count = fSamples.count();
10817265e725555098637498db2f397623d7991ceb4brobertphillips@google.com    SkDebugf("All Slides:\n");
10827265e725555098637498db2f397623d7991ceb4brobertphillips@google.com    for (int i = 0; i < count; i++) {
10837265e725555098637498db2f397623d7991ceb4brobertphillips@google.com        SkDebugf("    %s\n", getSampleTitle(i).c_str());
10847265e725555098637498db2f397623d7991ceb4brobertphillips@google.com    }
10857265e725555098637498db2f397623d7991ceb4brobertphillips@google.com}
10867265e725555098637498db2f397623d7991ceb4brobertphillips@google.com
108755e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.comstatic SkBitmap capture_bitmap(SkCanvas* canvas) {
108855e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com    SkBitmap bm;
1089c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    if (bm.allocPixels(canvas->imageInfo())) {
1090c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org        canvas->readPixels(&bm, 0, 0);
1091c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    }
109255e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com    return bm;
109355e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com}
109455e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com
109555e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.comstatic bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
109655e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com                        SkBitmap* diff) {
109755e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com    const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
10982e7b43d33cc495663cb814a7a9d1ecdc09c31828bsalomon@google.com
109955e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com    SkAutoLockPixels alp0(src);
110055e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com    SkAutoLockPixels alp1(orig);
110155e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com    for (int y = 0; y < src.height(); y++) {
110255e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        const void* srcP = src.getAddr(0, y);
110355e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        const void* origP = orig.getAddr(0, y);
110455e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        size_t bytes = src.width() * src.bytesPerPixel();
110555e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        if (memcmp(srcP, origP, bytes)) {
110655e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com            SkDebugf("---------- difference on line %d\n", y);
110755e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com            return true;
110855e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        }
110955e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com    }
111055e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com    return false;
111155e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com}
111255e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com
11130f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggostatic void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
11140f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo{
11150f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    SkColor desiredColor = paint.getColor();
11160f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    paint.setColor(SK_ColorWHITE);
11170f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    const char* c_str = string.c_str();
11180f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    size_t size = string.size();
11190f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    SkRect bounds;
11200f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    paint.measureText(c_str, size, &bounds);
11210f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    bounds.offset(left, top);
11220f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    SkScalar inset = SkIntToScalar(-2);
11230f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    bounds.inset(inset, inset);
11240f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    canvas->drawRect(bounds, paint);
11250f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    if (desiredColor != SK_ColorBLACK) {
11260f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        paint.setColor(SK_ColorBLACK);
11270f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
11280f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    }
11290f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    paint.setColor(desiredColor);
11300f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    canvas->drawText(c_str, size, left, top, paint);
11310f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo}
11320f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo
11334417740a480786ca65379ba5b398c500ba34ee18reed@android.com#define XCLIP_N  8
11344417740a480786ca65379ba5b398c500ba34ee18reed@android.com#define YCLIP_N  8
1135e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com
1136e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.comvoid SampleWindow::draw(SkCanvas* canvas) {
11374417740a480786ca65379ba5b398c500ba34ee18reed@android.com    // update the animation time
1138e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com    if (!gAnimTimePrev && !gAnimTime) {
1139e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com        // first time make delta be 0
1140e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com        gAnimTime = SkTime::GetMSecs();
1141e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com        gAnimTimePrev = gAnimTime;
1142e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com    } else {
1143e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com        gAnimTimePrev = gAnimTime;
1144e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com        gAnimTime = SkTime::GetMSecs();
1145e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com    }
11464605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
1147f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com    if (fGesture.isActive()) {
1148f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        this->updateMatrix();
114952f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com    }
11504605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
1151796763e0b2c06a7789185322561a1135e5484688djsollen@google.com    if (fMeasureFPS) {
1152796763e0b2c06a7789185322561a1135e5484688djsollen@google.com        fMeasureFPS_Time = 0;
1153796763e0b2c06a7789185322561a1135e5484688djsollen@google.com    }
1154796763e0b2c06a7789185322561a1135e5484688djsollen@google.com
1155e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com    if (fNClip) {
115655e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        this->INHERITED::draw(canvas);
115755e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        SkBitmap orig = capture_bitmap(canvas);
1158e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com
1159e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com        const SkScalar w = this->width();
1160e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com        const SkScalar h = this->height();
1161e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com        const SkScalar cw = w / XCLIP_N;
1162e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com        const SkScalar ch = h / YCLIP_N;
1163e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com        for (int y = 0; y < YCLIP_N; y++) {
116455e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com            SkRect r;
116555e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com            r.fTop = y * ch;
116655e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com            r.fBottom = (y + 1) * ch;
116755e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com            if (y == YCLIP_N - 1) {
116855e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com                r.fBottom = h;
116955e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com            }
1170e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com            for (int x = 0; x < XCLIP_N; x++) {
1171e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com                SkAutoCanvasRestore acr(canvas, true);
117255e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com                r.fLeft = x * cw;
117355e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com                r.fRight = (x + 1) * cw;
117455e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com                if (x == XCLIP_N - 1) {
117555e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com                    r.fRight = w;
117655e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com                }
1177e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com                canvas->clipRect(r);
1178e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com                this->INHERITED::draw(canvas);
1179e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com            }
1180e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com        }
11812e7b43d33cc495663cb814a7a9d1ecdc09c31828bsalomon@google.com
118255e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        SkBitmap diff;
118355e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        if (bitmap_diff(canvas, orig, &diff)) {
118455e76b209c9249308a9ba3d75c2472dd55e9d298reed@android.com        }
1185e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com    } else {
1186bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org        SkSize tile = this->tileSize();
1187cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org
11889a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org        if (kNo_Tiling == fTilingMode) {
11899a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org            this->INHERITED::draw(canvas); // no looping or surfaces needed
11909a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org        } else {
11919a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org            const int w = SkScalarRoundToInt(tile.width());
11929a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org            const int h = SkScalarRoundToInt(tile.height());
11939a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org            SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
11949a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org            SkAutoTUnref<SkSurface> surface(canvas->newSurface(info));
11959a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org            SkCanvas* tileCanvas = surface->getCanvas();
11969a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org
11979a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org            for (SkScalar y = 0; y < height(); y += tile.height()) {
11989a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org                for (SkScalar x = 0; x < width(); x += tile.width()) {
11999a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org                    SkAutoCanvasRestore acr(tileCanvas, true);
12009a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org                    tileCanvas->translate(-x, -y);
12019a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org                    tileCanvas->clear(0);
12029a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org                    this->INHERITED::draw(tileCanvas);
12039a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org                    surface->draw(canvas, x, y, NULL);
12049a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org                }
120585cade0a10217a50aebb495099ecc0435fe08b23scroggo@google.com            }
1206ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
12079a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org            // for drawing the borders between tiles
120885cade0a10217a50aebb495099ecc0435fe08b23scroggo@google.com            SkPaint paint;
120985cade0a10217a50aebb495099ecc0435fe08b23scroggo@google.com            paint.setColor(0x60FF00FF);
121085cade0a10217a50aebb495099ecc0435fe08b23scroggo@google.com            paint.setStyle(SkPaint::kStroke_Style);
1211cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org
1212cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org            for (SkScalar y = 0; y < height(); y += tile.height()) {
1213cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org                for (SkScalar x = 0; x < width(); x += tile.width()) {
12149a558d495dfe305a52946cdf97e8883190e0bbcccommit-bot@chromium.org                    canvas->drawRect(SkRect::MakeXYWH(x, y, tile.width(), tile.height()), paint);
121585cade0a10217a50aebb495099ecc0435fe08b23scroggo@google.com                }
121685cade0a10217a50aebb495099ecc0435fe08b23scroggo@google.com            }
121785cade0a10217a50aebb495099ecc0435fe08b23scroggo@google.com        }
1218e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com    }
1219098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    if (fShowZoomer && !fSaveToPdf) {
12203272ba8abcf322f5e9bcf2f7f2dcddaf68550268Scroggo        showZoomer(canvas);
12213272ba8abcf322f5e9bcf2f7f2dcddaf68550268Scroggo    }
1222db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    if (fMagnify && !fSaveToPdf) {
1223db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com        magnify(canvas);
1224db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    }
12254605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
1226796763e0b2c06a7789185322561a1135e5484688djsollen@google.com    if (fMeasureFPS && fMeasureFPS_Time) {
1227796763e0b2c06a7789185322561a1135e5484688djsollen@google.com        this->updateTitle();
1228796763e0b2c06a7789185322561a1135e5484688djsollen@google.com        this->postInvalDelay();
1229796763e0b2c06a7789185322561a1135e5484688djsollen@google.com    }
1230796763e0b2c06a7789185322561a1135e5484688djsollen@google.com
123129038ed2ab5cb87fda5a2a4d3267f0a5aa227a3dreed@google.com    // do this last
1232098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    fDevManager->publishCanvas(fDeviceType, canvas, this);
12333272ba8abcf322f5e9bcf2f7f2dcddaf68550268Scroggo}
12343272ba8abcf322f5e9bcf2f7f2dcddaf68550268Scroggo
1235db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.comstatic float clipW = 200;
1236db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.comstatic float clipH = 200;
1237db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.comvoid SampleWindow::magnify(SkCanvas* canvas) {
1238db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    SkRect r;
1239db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    int count = canvas->save();
12404605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
1241db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    SkMatrix m = canvas->getTotalMatrix();
124207ef911f18e30566d8a9d790e0bd69a836fd9d24robertphillips@google.com    if (!m.invert(&m)) {
124307ef911f18e30566d8a9d790e0bd69a836fd9d24robertphillips@google.com        return;
124407ef911f18e30566d8a9d790e0bd69a836fd9d24robertphillips@google.com    }
1245db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    SkPoint offset, center;
1246820e80ad6390e120d4991a5280d0452ec87beaebbsalomon@google.com    SkScalar mouseX = fMouseX * SK_Scalar1;
1247820e80ad6390e120d4991a5280d0452ec87beaebbsalomon@google.com    SkScalar mouseY = fMouseY * SK_Scalar1;
1248820e80ad6390e120d4991a5280d0452ec87beaebbsalomon@google.com    m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset);
1249820e80ad6390e120d4991a5280d0452ec87beaebbsalomon@google.com    m.mapXY(mouseX, mouseY, &center);
12504605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
1251db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX());
1252db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    r.offset(offset.fX, offset.fY);
12534605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
1254db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    SkPaint paint;
1255db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    paint.setColor(0xFF66AAEE);
1256db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    paint.setStyle(SkPaint::kStroke_Style);
1257820e80ad6390e120d4991a5280d0452ec87beaebbsalomon@google.com    paint.setStrokeWidth(10.f * m.getScaleX());
1258db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    //lense offset
1259db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    //canvas->translate(0, -250);
1260db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    canvas->drawRect(r, paint);
1261db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    canvas->clipRect(r);
12624605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
1263db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    m = canvas->getTotalMatrix();
1264db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    m.setTranslate(-center.fX, -center.fY);
1265820e80ad6390e120d4991a5280d0452ec87beaebbsalomon@google.com    m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale);
1266db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    m.postTranslate(center.fX, center.fY);
1267db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    canvas->concat(m);
12684605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
1269db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    this->INHERITED::draw(canvas);
12704605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
1271db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    canvas->restoreToCount(count);
1272db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com}
1273db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com
12743272ba8abcf322f5e9bcf2f7f2dcddaf68550268Scroggovoid SampleWindow::showZoomer(SkCanvas* canvas) {
12750f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        int count = canvas->save();
12760f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        canvas->resetMatrix();
12770f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        // Ensure the mouse position is on screen.
1278e1ca705cac4b946993f6cbf798e2a0ba27e739f3reed@google.com        int width = SkScalarRoundToInt(this->width());
1279e1ca705cac4b946993f6cbf798e2a0ba27e739f3reed@google.com        int height = SkScalarRoundToInt(this->height());
12800f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        if (fMouseX >= width) fMouseX = width - 1;
12810f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        else if (fMouseX < 0) fMouseX = 0;
12820f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        if (fMouseY >= height) fMouseY = height - 1;
12830f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        else if (fMouseY < 0) fMouseY = 0;
1284b36334d46a488c2d4104a2cabf406cac0585f27ereed@google.com
12850f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        SkBitmap bitmap = capture_bitmap(canvas);
1286b36334d46a488c2d4104a2cabf406cac0585f27ereed@google.com        bitmap.lockPixels();
1287b36334d46a488c2d4104a2cabf406cac0585f27ereed@google.com
12880f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        // Find the size of the zoomed in view, forced to be odd, so the examined pixel is in the middle.
12893ce59dc4999de5d6c6913af662c8b0b8020a4ef7mike@reedtribe.org        int zoomedWidth = (width >> 1) | 1;
12903ce59dc4999de5d6c6913af662c8b0b8020a4ef7mike@reedtribe.org        int zoomedHeight = (height >> 1) | 1;
12910f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        SkIRect src;
12920f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
12930f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
12940f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        SkRect dest;
12950f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
12960f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
12970f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        SkPaint paint;
12980f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        // Clear the background behind our zoomed in view
12990f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        paint.setColor(SK_ColorWHITE);
13000f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        canvas->drawRect(dest, paint);
13010f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        canvas->drawBitmapRect(bitmap, &src, dest);
13020f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        paint.setColor(SK_ColorBLACK);
13030f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        paint.setStyle(SkPaint::kStroke_Style);
13040f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        // Draw a border around the pixel in the middle
13050f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        SkRect originalPixel;
13060f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
13070f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        SkMatrix matrix;
13080f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        SkRect scalarSrc;
13090f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        scalarSrc.set(src);
13100f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        SkColor color = bitmap.getColor(fMouseX, fMouseY);
13110f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
13120f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo            SkRect pixel;
13130f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo            matrix.mapRect(&pixel, originalPixel);
13140f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo            // TODO Perhaps measure the values and make the outline white if it's "dark"
13150f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo            if (color == SK_ColorBLACK) {
13160f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo                paint.setColor(SK_ColorWHITE);
13170f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo            }
13180f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo            canvas->drawRect(pixel, paint);
13190f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        }
13200f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        paint.setColor(SK_ColorBLACK);
13210f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        // Draw a border around the destination rectangle
13220f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        canvas->drawRect(dest, paint);
13230f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        paint.setStyle(SkPaint::kStrokeAndFill_Style);
13240f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        // Identify the pixel and its color on screen
13250f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        paint.setTypeface(fTypeface);
13260f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        paint.setAntiAlias(true);
13270f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        SkScalar lineHeight = paint.getFontMetrics(NULL);
13280f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        SkString string;
13290f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        string.appendf("(%i, %i)", fMouseX, fMouseY);
13300f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        SkScalar left = dest.fLeft + SkIntToScalar(3);
13310f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        SkScalar i = SK_Scalar1;
13320f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
13330f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        // Alpha
13340f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        i += SK_Scalar1;
13350f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        string.reset();
13360f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        string.appendf("A: %X", SkColorGetA(color));
13370f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
13380f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        // Red
13390f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        i += SK_Scalar1;
13400f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        string.reset();
13410f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        string.appendf("R: %X", SkColorGetR(color));
13420f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        paint.setColor(SK_ColorRED);
13430f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
13440f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        // Green
13450f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        i += SK_Scalar1;
13460f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        string.reset();
13470f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        string.appendf("G: %X", SkColorGetG(color));
13480f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        paint.setColor(SK_ColorGREEN);
13490f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
13500f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        // Blue
13510f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        i += SK_Scalar1;
13520f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        string.reset();
13530f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        string.appendf("B: %X", SkColorGetB(color));
13540f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        paint.setColor(SK_ColorBLUE);
13550f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
13560f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        canvas->restoreToCount(count);
1357e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com}
1358e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com
13598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SampleWindow::onDraw(SkCanvas* canvas) {
13608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h"
13638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13648ac0d542b0226e68b0faa935b3402ffc91409a02Scroggovoid SampleWindow::saveToPdf()
13658ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo{
13668ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo    fSaveToPdf = true;
13678ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo    this->inval(NULL);
13688ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo}
13698ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo
13708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
13718ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo    if (fSaveToPdf) {
13728ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        const SkBitmap& bmp = canvas->getDevice()->accessBitmap(false);
13738ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        SkISize size = SkISize::Make(bmp.width(), bmp.height());
13748ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
13758ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo                canvas->getTotalMatrix());
13768ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        fPdfCanvas = new SkCanvas(pdfDevice);
13778ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        pdfDevice->unref();
13788ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        canvas = fPdfCanvas;
137982502e2e3966a9b0e48665f20154eaacd2a452b3bsalomon@google.com    } else if (kPicture_DeviceType == fDeviceType) {
13805fb2ce38b3dcb8e60e9e112df23c9d42456d7069commit-bot@chromium.org        canvas = fRecorder.beginRecording(9999, 9999, NULL, 0);
13818ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo    } else {
1382cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
138382502e2e3966a9b0e48665f20154eaacd2a452b3bsalomon@google.com        if (kNullGPU_DeviceType != fDeviceType)
1384cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
138582502e2e3966a9b0e48665f20154eaacd2a452b3bsalomon@google.com        {
138682502e2e3966a9b0e48665f20154eaacd2a452b3bsalomon@google.com            canvas = this->INHERITED::beforeChildren(canvas);
1387ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com        }
13888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fUseClip) {
13918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        canvas->drawColor(0xFFFF88FF);
1392045e62d715f5ee9b03deb5af3c750f8318096179reed@google.com        canvas->clipPath(fClipPath, SkRegion::kIntersect_Op, true);
13938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
13948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return canvas;
13968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
13978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void paint_rgn(const SkBitmap& bm, const SkIRect& r,
13998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                      const SkRegion& rgn) {
14008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkCanvas    canvas(bm);
14018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRegion    inval(rgn);
14028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inval.translate(r.fLeft, r.fTop);
14048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    canvas.clipRegion(inval);
14058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    canvas.drawColor(0xFFFF8080);
14068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1407501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.com#include "SkData.h"
14088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SampleWindow::afterChildren(SkCanvas* orig) {
14098ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo    if (fSaveToPdf) {
14108ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        fSaveToPdf = false;
14118ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        if (fShowZoomer) {
14128ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo            showZoomer(fPdfCanvas);
14138ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        }
14148ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        SkString name;
14158ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        name.printf("%s.pdf", this->getTitle());
14168ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        SkPDFDocument doc;
14178ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        SkPDFDevice* device = static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
14188ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        doc.appendPage(device);
141956c69773aea56c6c6bd47bc7e7970dd081205184djsollen@google.com#ifdef SK_BUILD_FOR_ANDROID
14208ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        name.prepend("/sdcard/");
14218ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo#endif
14224605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
1423501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.com#ifdef SK_BUILD_FOR_IOS
1424501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.com        SkDynamicMemoryWStream mstream;
1425501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.com        doc.emitPDF(&mstream);
1426ae8a2e5cba553267b189b8b5cae40330c4ff5bdbyangsu@google.com        fPDFData = mstream.copyToData();
1427501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.com#endif
14288ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        SkFILEWStream stream(name.c_str());
14298ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        if (stream.isValid()) {
14308ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo            doc.emitPDF(&stream);
14318ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo            const char* desc = "File saved from Skia SampleApp";
14328ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo            this->onPDFSaved(this->getTitle(), desc, name.c_str());
14338ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        }
14344605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
14358ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        delete fPdfCanvas;
14368ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        fPdfCanvas = NULL;
14378ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo
14388ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        // We took over the draw calls in order to create the PDF, so we need
14398ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        // to redraw.
14408ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        this->inval(NULL);
14418ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo        return;
14428ac0d542b0226e68b0faa935b3402ffc91409a02Scroggo    }
14434605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
1444f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    if (fRequestGrabImage) {
1445f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        fRequestGrabImage = false;
14462e7b43d33cc495663cb814a7a9d1ecdc09c31828bsalomon@google.com
14471f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com        SkBaseDevice* device = orig->getDevice();
1448669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com        SkBitmap bmp;
144928fcae2ec77eb16a79e155f8d788b20457f1c951commit-bot@chromium.org        if (device->accessBitmap(false).copyTo(&bmp, kN32_SkColorType)) {
1450f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            static int gSampleGrabCounter;
1451f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            SkString name;
14527c57e0e7ccb05a4b7ce05b883ba06380d5578881reed@google.com            name.printf("sample_grab_%d.png", gSampleGrabCounter++);
1453669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com            SkImageEncoder::EncodeFile(name.c_str(), bmp,
1454f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                       SkImageEncoder::kPNG_Type, 100);
1455f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
1456f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
1457f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
1458098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    if (kPicture_DeviceType == fDeviceType) {
145984b18c7e3e042bf206e1ace3d1b6ea5bb929fe51robertphillips@google.com        SkAutoTUnref<SkPicture> picture(fRecorder.endRecording());
146084b18c7e3e042bf206e1ace3d1b6ea5bb929fe51robertphillips@google.com
1461098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com        if (true) {
146284b18c7e3e042bf206e1ace3d1b6ea5bb929fe51robertphillips@google.com            SkPicture* pict = new SkPicture(*picture);
1463e23f194bf9747267b9dd42e238dfe217ed585c03reed@google.com            this->installDrawFilter(orig);
14649b14f26d0f3a974f3dd626c8354e1db1cfcd322frobertphillips            orig->drawPicture(pict);
1465098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com            pict->unref();
1466098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com        } else if (true) {
1467098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com            SkDynamicMemoryWStream ostream;
146884b18c7e3e042bf206e1ace3d1b6ea5bb929fe51robertphillips@google.com            picture->serialize(&ostream);
1469098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com
1470098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com            SkAutoDataUnref data(ostream.copyToData());
147159f46b81f8bdd1b524f5cc43bc27603f9604c71arobertphillips@google.com            SkMemoryStream istream(data->data(), data->size());
1472f1754ec69131801c1a6ed3c704501a9400bbf324scroggo@google.com            SkAutoTUnref<SkPicture> pict(SkPicture::CreateFromStream(&istream));
1473f1754ec69131801c1a6ed3c704501a9400bbf324scroggo@google.com            if (pict.get() != NULL) {
14749b14f26d0f3a974f3dd626c8354e1db1cfcd322frobertphillips                orig->drawPicture(pict.get());
1475f1754ec69131801c1a6ed3c704501a9400bbf324scroggo@google.com            }
1476098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com        } else {
147784b18c7e3e042bf206e1ace3d1b6ea5bb929fe51robertphillips@google.com            picture->draw(orig);
1478098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com        }
14798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
14802e7b43d33cc495663cb814a7a9d1ecdc09c31828bsalomon@google.com
148117d7aec8967f7e2f6bb420c6647be11285fac2f1reed@google.com    // Do this after presentGL and other finishing, rather than in afterChild
1482796763e0b2c06a7789185322561a1135e5484688djsollen@google.com    if (fMeasureFPS && fMeasureFPS_StartTime) {
1483796763e0b2c06a7789185322561a1135e5484688djsollen@google.com        fMeasureFPS_Time += SkTime::GetMSecs() - fMeasureFPS_StartTime;
148417d7aec8967f7e2f6bb420c6647be11285fac2f1reed@google.com    }
148517d7aec8967f7e2f6bb420c6647be11285fac2f1reed@google.com
148617d7aec8967f7e2f6bb420c6647be11285fac2f1reed@google.com    //    if ((fScrollTestX | fScrollTestY) != 0)
1487f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    if (false) {
14888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkBitmap& bm = orig->getDevice()->accessBitmap(true);
14898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int dx = fScrollTestX * 7;
14908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int dy = fScrollTestY * 7;
14918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkIRect r;
14928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRegion inval;
14932e7b43d33cc495663cb814a7a9d1ecdc09c31828bsalomon@google.com
14948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.set(50, 50, 50+100, 50+100);
14958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bm.scrollRect(&r, dx, dy, &inval);
14968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint_rgn(bm, r, inval);
1497ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com    }
14988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
14998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
15006c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.comvoid SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
15016c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.com    if (fRotate) {
1502b17859133aeb0ebe80805fbd437759e2b956d859bungeman@google.com        fRotateAnimTime += SampleCode::GetAnimSecondsDelta();
1503b17859133aeb0ebe80805fbd437759e2b956d859bungeman@google.com
15046c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.com        SkScalar cx = this->width() / 2;
15056c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.com        SkScalar cy = this->height() / 2;
15066c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.com        canvas->translate(cx, cy);
1507b17859133aeb0ebe80805fbd437759e2b956d859bungeman@google.com        canvas->rotate(fRotateAnimTime * 10);
15086c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.com        canvas->translate(-cx, -cy);
15096c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.com    }
1510b17859133aeb0ebe80805fbd437759e2b956d859bungeman@google.com
1511e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com    if (fPerspAnim) {
15126fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com        fPerspAnimTime += SampleCode::GetAnimSecondsDelta();
15136fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com
15146fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com        static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
15156fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com        static const SkScalar gAnimMag = SK_Scalar1 / 1000;
15166fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com        SkScalar t = SkScalarMod(fPerspAnimTime, gAnimPeriod);
15176fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com        if (SkScalarFloorToInt(SkScalarDiv(fPerspAnimTime, gAnimPeriod)) & 0x1) {
15186fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com            t = gAnimPeriod - t;
15196fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com        }
15206fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com        t = 2 * t - gAnimPeriod;
15216fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com        t = SkScalarMul(SkScalarDiv(t, gAnimPeriod), gAnimMag);
15226fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com        SkMatrix m;
15236fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com        m.reset();
15246fb736fc2ea5f3f7ac44494211cc6755180ca192bsalomon@google.com        m.setPerspY(t);
1525e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com        canvas->concat(m);
1526e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com    }
1527f0b5f68807cfa18cce0647d275778147901945cdreed@google.com
1528e23f194bf9747267b9dd42e238dfe217ed585c03reed@google.com    this->installDrawFilter(canvas);
15292eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
15302eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    if (fMeasureFPS) {
1531f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com        if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
1532796763e0b2c06a7789185322561a1135e5484688djsollen@google.com            fMeasureFPS_StartTime = SkTime::GetMSecs();
1533f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com        }
1534f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com    } else {
1535f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com        (void)SampleView::SetRepeatDraw(child, 1);
15362eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    }
1537b17859133aeb0ebe80805fbd437759e2b956d859bungeman@google.com    if (fPerspAnim || fRotate) {
1538e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com        this->inval(NULL);
1539e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com    }
15406c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.com}
15416c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.com
15426c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.comvoid SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
1543f0b5f68807cfa18cce0647d275778147901945cdreed@google.com    canvas->setDrawFilter(NULL);
15446c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.com}
15456c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.com
1546e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.orgstatic SkColorType gColorTypeCycle[] = {
1547e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org    kUnknown_SkColorType,           // none -> none
1548e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org    kUnknown_SkColorType,           // a8 -> none
1549fb6fefed0976d7fed193155900a186dc880451b6commit-bot@chromium.org    kN32_SkColorType,               // 565 -> 8888
155028fcae2ec77eb16a79e155f8d788b20457f1c951commit-bot@chromium.org    kN32_SkColorType,               // 4444 -> 8888
1551e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org    kRGB_565_SkColorType,           // 8888 -> 565
1552e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org    kRGB_565_SkColorType,           // 8888 -> 565
1553e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org    kUnknown_SkColorType,           // index8 -> none
15548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
15558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1556a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.orgstatic SkColorType cycle_colortypes(SkColorType c) {
1557e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org    return gColorTypeCycle[c];
15588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
15598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1560e32b5837c3f35b80502759f3de3e8cadf6625f4edjsollen@google.comvoid SampleWindow::changeZoomLevel(float delta) {
15614b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org    fZoomLevel += delta;
1562dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org    if (fZoomLevel > 0) {
1563e32b5837c3f35b80502759f3de3e8cadf6625f4edjsollen@google.com        fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
1564e32b5837c3f35b80502759f3de3e8cadf6625f4edjsollen@google.com        fZoomScale = fZoomLevel + SK_Scalar1;
1565dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org    } else if (fZoomLevel < 0) {
1566e32b5837c3f35b80502759f3de3e8cadf6625f4edjsollen@google.com        fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
1567e32b5837c3f35b80502759f3de3e8cadf6625f4edjsollen@google.com        fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
1568dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org    } else {
1569dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org        fZoomScale = SK_Scalar1;
1570dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org    }
1571f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com    this->updateMatrix();
1572f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com}
1573dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org
1574f03bb566e25ace918f8fdda3cb8426626a00894creed@google.comvoid SampleWindow::updateMatrix(){
1575f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com    SkMatrix m;
1576f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com    m.reset();
1577f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com    if (fZoomLevel) {
1578f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        SkPoint center;
1579f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        //m = this->getLocalMatrix();//.invert(&m);
1580f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        m.mapXY(fZoomCenterX, fZoomCenterY, &center);
1581f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        SkScalar cx = center.fX;
1582f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        SkScalar cy = center.fY;
15834605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
1584f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        m.setTranslate(-cx, -cy);
1585f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        m.postScale(fZoomScale, fZoomScale);
1586f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        m.postTranslate(cx, cy);
1587f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com    }
15884605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
1589f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com    if (fFlipAxis) {
1590f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        m.preTranslate(fZoomCenterX, fZoomCenterY);
1591f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        if (fFlipAxis & kFlipAxis_X) {
1592f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com            m.preScale(-SK_Scalar1, SK_Scalar1);
1593f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        }
1594f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        if (fFlipAxis & kFlipAxis_Y) {
1595f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com            m.preScale(SK_Scalar1, -SK_Scalar1);
1596f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        }
1597f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        m.preTranslate(-fZoomCenterX, -fZoomCenterY);
1598f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        //canvas->concat(m);
1599f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com    }
1600f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com    // Apply any gesture matrix
1601f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com    m.preConcat(fGesture.localM());
1602f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com    m.preConcat(fGesture.globalM());
16034605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
1604f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com    this->setLocalMatrix(m);
16054605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
1606e32b5837c3f35b80502759f3de3e8cadf6625f4edjsollen@google.com    this->updateTitle();
1607dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org    this->inval(NULL);
1608dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org}
16092c8208f3a444098aee562f5a1ef620142807eda2Scroggobool SampleWindow::previousSample() {
161062b65b0913566381db468277de07e5dd2a9237caScroggo    fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
16114605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
1612db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com                                     fTransitionPrev));
16132c8208f3a444098aee562f5a1ef620142807eda2Scroggo    return true;
16142c8208f3a444098aee562f5a1ef620142807eda2Scroggo}
16152c8208f3a444098aee562f5a1ef620142807eda2Scroggo
16168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SampleWindow::nextSample() {
161734245c7871f6339de8cc2be8fb1090ca3cba54efreed@android.com    fCurrIndex = (fCurrIndex + 1) % fSamples.count();
16184605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
1619db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com                                     fTransitionNext));
162034245c7871f6339de8cc2be8fb1090ca3cba54efreed@android.com    return true;
16218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
16228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1623501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.combool SampleWindow::goToSample(int i) {
1624501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.com    fCurrIndex = (i) % fSamples.count();
162548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    this->loadView(create_transition(curr_view(this),(*fSamples[fCurrIndex])(), 6));
1626501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.com    return true;
1627501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.com}
1628501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.com
1629501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.comSkString SampleWindow::getSampleTitle(int i) {
1630f8b6b01d51d08e39499d1c88063af2964fe72a0fedisonn@google.com    return ::getSampleTitle(fSamples[i]);
1631501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.com}
1632501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.com
1633501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.comint SampleWindow::sampleCount() {
1634501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.com    return fSamples.count();
1635501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.com}
1636501775e5ac99f9805d1aba3aa7ca554a83d2dc43yangsu@google.com
1637db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.comvoid SampleWindow::showOverview() {
16384605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    this->loadView(create_transition(curr_view(this),
16394605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com                                     create_overview(fSamples.count(), fSamples.begin()),
1640db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com                                     4));
1641db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com}
1642db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com
1643e23f194bf9747267b9dd42e238dfe217ed585c03reed@google.comvoid SampleWindow::installDrawFilter(SkCanvas* canvas) {
164415bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState, fSubpixelState,
164515bc13d20fe79914c7128881e45064becd11bc9dreed@google.com                                              fHintingState, fFilterLevelIndex))->unref();
1646e23f194bf9747267b9dd42e238dfe217ed585c03reed@google.com}
1647e23f194bf9747267b9dd42e238dfe217ed585c03reed@google.com
16482c8208f3a444098aee562f5a1ef620142807eda2Scroggovoid SampleWindow::postAnimatingEvent() {
16492c8208f3a444098aee562f5a1ef620142807eda2Scroggo    if (fAnimating) {
165087fac4abd7ea446c05d5cfd4a8ad27420223b591reed@google.com        (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
16512c8208f3a444098aee562f5a1ef620142807eda2Scroggo    }
16522c8208f3a444098aee562f5a1ef620142807eda2Scroggo}
1653e23f194bf9747267b9dd42e238dfe217ed585c03reed@google.com
16548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SampleWindow::onEvent(const SkEvent& evt) {
16552072db89016c4e53a648f4d4cec08b1d6be8558creed@google.com    if (evt.isType(gUpdateWindowTitleEvtName)) {
16562072db89016c4e53a648f4d4cec08b1d6be8558creed@google.com        this->updateTitle();
16572072db89016c4e53a648f4d4cec08b1d6be8558creed@google.com        return true;
16582072db89016c4e53a648f4d4cec08b1d6be8558creed@google.com    }
16598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (evt.isType(ANIMATING_EVENTTYPE)) {
16608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (fAnimating) {
16618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            this->nextSample();
16628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            this->postAnimatingEvent();
16638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
16648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return true;
16658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1666ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com    if (evt.isType("replace-transition-view")) {
1667ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com        this->loadView((SkView*)SkEventSink::FindSink(evt.getFast32()));
1668db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com        return true;
1669db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    }
167034245c7871f6339de8cc2be8fb1090ca3cba54efreed@android.com    if (evt.isType("set-curr-index")) {
1671db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com        this->goToSample(evt.getFast32());
167234245c7871f6339de8cc2be8fb1090ca3cba54efreed@android.com        return true;
167334245c7871f6339de8cc2be8fb1090ca3cba54efreed@android.com    }
16742eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    if (isInvalEvent(evt)) {
16752eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org        this->inval(NULL);
16762eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org        return true;
16772eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    }
1678921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com    int selected = -1;
1679ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com    if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) {
1680921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com        this->setDeviceType((DeviceType)selected);
16814605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com        return true;
1682921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com    }
1683b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com    if (SkOSMenu::FindTriState(evt, "Pipe", &fPipeState)) {
1684ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com#ifdef PIPE_NET
1685b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        if (!fPipeState != SkOSMenu::kOnState)
1686ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com            gServer.disconnectAll();
1687ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com#endif
1688b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        (void)SampleView::SetUsePipe(curr_view(this), fPipeState);
1689ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com        this->updateTitle();
1690ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com        this->inval(NULL);
1691921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com        return true;
1692921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com    }
1693ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com    if (SkOSMenu::FindSwitchState(evt, "Slide Show", NULL)) {
1694921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com        this->toggleSlideshow();
1695921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com        return true;
1696921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com    }
1697ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com    if (SkOSMenu::FindTriState(evt, "AA", &fAAState) ||
1698ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com        SkOSMenu::FindTriState(evt, "LCD", &fLCDState) ||
169915bc13d20fe79914c7128881e45064becd11bc9dreed@google.com        SkOSMenu::FindListIndex(evt, "FilterLevel", &fFilterLevelIndex) ||
170096aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com        SkOSMenu::FindTriState(evt, "Subpixel", &fSubpixelState) ||
170196aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com        SkOSMenu::FindListIndex(evt, "Hinting", &fHintingState) ||
1702ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com        SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) ||
1703ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com        SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
1704ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com        SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify) ||
1705ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com        SkOSMenu::FindListIndex(evt, "Transition-Next", &fTransitionNext) ||
1706bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org        SkOSMenu::FindListIndex(evt, "Transition-Prev", &fTransitionPrev)) {
1707bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org        this->inval(NULL);
1708bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org        this->updateTitle();
1709bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org        return true;
1710bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org    }
1711bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org    if (SkOSMenu::FindListIndex(evt, "Tiling", &fTilingMode)) {
1712bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org        if (SampleView::IsSampleView(curr_view(this))) {
1713bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org            ((SampleView*)curr_view(this))->onTileSizeChanged(this->tileSize());
1714bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org        }
1715921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com        this->inval(NULL);
1716921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com        this->updateTitle();
1717921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com        return true;
1718921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com    }
1719ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com    if (SkOSMenu::FindSwitchState(evt, "Flip X", NULL)) {
1720921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com        fFlipAxis ^= kFlipAxis_X;
1721f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        this->updateMatrix();
1722921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com        return true;
1723921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com    }
1724ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com    if (SkOSMenu::FindSwitchState(evt, "Flip Y", NULL)) {
1725921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com        fFlipAxis ^= kFlipAxis_Y;
1726f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com        this->updateMatrix();
1727921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com        return true;
1728921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com    }
1729ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com    if (SkOSMenu::FindAction(evt,"Save to PDF")) {
1730921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com        this->saveToPdf();
1731921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com        return true;
17324605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    }
17338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->INHERITED::onEvent(evt);
17348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
17358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1736f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.combool SampleWindow::onQuery(SkEvent* query) {
1737f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    if (query->isType("get-slide-count")) {
1738f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        query->setFast32(fSamples.count());
1739f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        return true;
1740f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
1741f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    if (query->isType("get-slide-title")) {
174248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com        SkView* view = (*fSamples[query->getFast32()])();
1743f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        SkEvent evt(gTitleEvtName);
1744f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        if (view->doQuery(&evt)) {
1745f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            query->setString("title", evt.findString(gTitleEvtName));
1746f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
1747f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        SkSafeUnref(view);
1748f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        return true;
1749f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
1750f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    if (query->isType("use-fast-text")) {
1751f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        SkEvent evt(gFastTextEvtName);
1752f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        return curr_view(this)->doQuery(&evt);
1753f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
175429038ed2ab5cb87fda5a2a4d3267f0a5aa227a3dreed@google.com    if (query->isType("ignore-window-bitmap")) {
175529038ed2ab5cb87fda5a2a4d3267f0a5aa227a3dreed@google.com        query->setFast32(this->getGrContext() != NULL);
175629038ed2ab5cb87fda5a2a4d3267f0a5aa227a3dreed@google.com        return true;
175729038ed2ab5cb87fda5a2a4d3267f0a5aa227a3dreed@google.com    }
1758f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    return this->INHERITED::onQuery(query);
1759f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
1760f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
176102939ce4bddd3223d9e8cc98d4abd02939b7f70acaryclark@google.com#if 0 // UNUSED
17620ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.comstatic void cleanup_for_filename(SkString* name) {
17630ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com    char* str = name->writable_str();
1764e191b16fda2f5afea3a90653c1cd4f5a3c436e89reed@android.com    for (size_t i = 0; i < name->size(); i++) {
17650ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com        switch (str[i]) {
17660ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com            case ':': str[i] = '-'; break;
17670ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com            case '/': str[i] = '-'; break;
17680ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com            case ' ': str[i] = '_'; break;
17690ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com            default: break;
17700ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com        }
17710ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com    }
17720ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com}
177302939ce4bddd3223d9e8cc98d4abd02939b7f70acaryclark@google.com#endif
17748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SampleWindow::onHandleChar(SkUnichar uni) {
1776f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    {
1777f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        SkView* view = curr_view(this);
1778f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        if (view) {
1779f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            SkEvent evt(gCharEvtName);
1780f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            evt.setFast32(uni);
1781f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            if (view->doQuery(&evt)) {
1782f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                return true;
1783f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            }
1784f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
1785f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
17864605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
17878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int dx = 0xFF;
17888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int dy = 0xFF;
17898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (uni) {
17918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case '5': dx =  0; dy =  0; break;
17928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case '8': dx =  0; dy = -1; break;
17938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case '6': dx =  1; dy =  0; break;
17948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case '2': dx =  0; dy =  1; break;
17958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case '4': dx = -1; dy =  0; break;
17968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case '7': dx = -1; dy = -1; break;
17978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case '9': dx =  1; dy = -1; break;
17988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case '3': dx =  1; dy =  1; break;
17998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case '1': dx = -1; dy =  1; break;
18002e7b43d33cc495663cb814a7a9d1ecdc09c31828bsalomon@google.com
18018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        default:
18028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
18038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18042e7b43d33cc495663cb814a7a9d1ecdc09c31828bsalomon@google.com
18058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (0xFF != dx && 0xFF != dy) {
18068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if ((dx | dy) == 0) {
18078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fScrollTestX = fScrollTestY = 0;
18088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
18098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fScrollTestX += dx;
18108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            fScrollTestY += dy;
18118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
18128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->inval(NULL);
18138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return true;
18148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18152e7b43d33cc495663cb814a7a9d1ecdc09c31828bsalomon@google.com
18160ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com    switch (uni) {
1817a4f8137e67e31390b0e7edc5e637a3a02495d1a2reed@google.com        case 'B':
18186de4a7836f1c7988b42cbbfe1a339ef7619aed8fcommit-bot@chromium.org            post_event_to_sink(SkNEW_ARGS(SkEvent, ("PictFileView::toggleBBox")), curr_view(this));
18196de4a7836f1c7988b42cbbfe1a339ef7619aed8fcommit-bot@chromium.org            // Cannot call updateTitle() synchronously, because the toggleBBox event is still in
18206de4a7836f1c7988b42cbbfe1a339ef7619aed8fcommit-bot@chromium.org            // the queue.
18216de4a7836f1c7988b42cbbfe1a339ef7619aed8fcommit-bot@chromium.org            post_event_to_sink(SkNEW_ARGS(SkEvent, (gUpdateWindowTitleEvtName)), this);
1822a4f8137e67e31390b0e7edc5e637a3a02495d1a2reed@google.com            this->inval(NULL);
1823a4f8137e67e31390b0e7edc5e637a3a02495d1a2reed@google.com            break;
1824ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com        case 'f':
18254605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com            // only
18266ff82553df07ec9502bfe5b4a97a2c2aaf15e39fdjsollen@google.com            toggleFPS();
1827ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com            break;
1828f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        case 'g':
1829f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            fRequestGrabImage = true;
1830f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            this->inval(NULL);
1831a4f8137e67e31390b0e7edc5e637a3a02495d1a2reed@google.com            break;
1832a4f8137e67e31390b0e7edc5e637a3a02495d1a2reed@google.com        case 'G':
1833a4f8137e67e31390b0e7edc5e637a3a02495d1a2reed@google.com            gShowGMBounds = !gShowGMBounds;
18346de4a7836f1c7988b42cbbfe1a339ef7619aed8fcommit-bot@chromium.org            post_event_to_sink(GMSampleView::NewShowSizeEvt(gShowGMBounds),
1835a4f8137e67e31390b0e7edc5e637a3a02495d1a2reed@google.com                            curr_view(this));
1836a4f8137e67e31390b0e7edc5e637a3a02495d1a2reed@google.com            this->inval(NULL);
1837f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            break;
18382eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org        case 'i':
18392eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org            this->zoomIn();
18402eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org            break;
184108526c07f4f530e56b70d4b22f5a4af35d9ebccascroggo        case 'o':
184208526c07f4f530e56b70d4b22f5a4af35d9ebccascroggo            this->zoomOut();
184308526c07f4f530e56b70d4b22f5a4af35d9ebccascroggo            break;
18442eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org        case 'r':
18452eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org            fRotate = !fRotate;
1846b17859133aeb0ebe80805fbd437759e2b956d859bungeman@google.com            fRotateAnimTime = 0;
18472eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org            this->inval(NULL);
18482eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org            this->updateTitle();
18492eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org            return true;
1850e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com        case 'k':
1851e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com            fPerspAnim = !fPerspAnim;
1852e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com            this->inval(NULL);
1853e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com            this->updateTitle();
1854e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com            return true;
1855cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
185674913722bfe5e4b6810545891958e3d8e9c63791bsalomon@google.com        case '\\':
1857b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            this->setDeviceType(kNullGPU_DeviceType);
1858b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            this->inval(NULL);
1859b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com            this->updateTitle();
186074913722bfe5e4b6810545891958e3d8e9c63791bsalomon@google.com            return true;
186105e70247c31ae927074ef27ea9893634a8dda543twiz@google.com        case 'p':
186205e70247c31ae927074ef27ea9893634a8dda543twiz@google.com            {
186305e70247c31ae927074ef27ea9893634a8dda543twiz@google.com                GrContext* grContext = this->getGrContext();
186405e70247c31ae927074ef27ea9893634a8dda543twiz@google.com                if (grContext) {
186595c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org                    size_t cacheBytes;
186695c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org                    grContext->getResourceCacheUsage(NULL, &cacheBytes);
186705e70247c31ae927074ef27ea9893634a8dda543twiz@google.com                    grContext->freeGpuResources();
186895c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org                    SkDebugf("Purged %d bytes from the GPU resource cache.\n", cacheBytes);
186905e70247c31ae927074ef27ea9893634a8dda543twiz@google.com                }
187005e70247c31ae927074ef27ea9893634a8dda543twiz@google.com            }
187105e70247c31ae927074ef27ea9893634a8dda543twiz@google.com            return true;
1872cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
18730ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com        default:
18740ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com            break;
18758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
18764605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
18777dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    if (fAppMenu->handleKeyEquivalent(uni)|| fSlideMenu->handleKeyEquivalent(uni)) {
18787dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com        this->onUpdateMenu(fAppMenu);
18797dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com        this->onUpdateMenu(fSlideMenu);
1880db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com        return true;
1881db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    }
18828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->INHERITED::onHandleChar(uni);
18838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
18848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1885921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.comvoid SampleWindow::setDeviceType(DeviceType type) {
1886b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com    if (type == fDeviceType)
1887b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com        return;
1888b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
1889b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com    fDevManager->tearDownBackend(this);
1890b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
1891b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com    fDeviceType = type;
1892b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
18931195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com    fDevManager->setUpBackend(this, fMSAASampleCount);
1894b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com
1895921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com    this->updateTitle();
1896921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com    this->inval(NULL);
1897921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com}
1898921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com
18992c8208f3a444098aee562f5a1ef620142807eda2Scroggovoid SampleWindow::toggleSlideshow() {
19002c8208f3a444098aee562f5a1ef620142807eda2Scroggo    fAnimating = !fAnimating;
19012c8208f3a444098aee562f5a1ef620142807eda2Scroggo    this->postAnimatingEvent();
19022c8208f3a444098aee562f5a1ef620142807eda2Scroggo    this->updateTitle();
19032c8208f3a444098aee562f5a1ef620142807eda2Scroggo}
19042c8208f3a444098aee562f5a1ef620142807eda2Scroggo
19052c8208f3a444098aee562f5a1ef620142807eda2Scroggovoid SampleWindow::toggleRendering() {
1906b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com    this->setDeviceType(cycle_devicetype(fDeviceType));
19072c8208f3a444098aee562f5a1ef620142807eda2Scroggo    this->updateTitle();
19082c8208f3a444098aee562f5a1ef620142807eda2Scroggo    this->inval(NULL);
19092c8208f3a444098aee562f5a1ef620142807eda2Scroggo}
19102c8208f3a444098aee562f5a1ef620142807eda2Scroggo
19116ff82553df07ec9502bfe5b4a97a2c2aaf15e39fdjsollen@google.comvoid SampleWindow::toggleFPS() {
19126ff82553df07ec9502bfe5b4a97a2c2aaf15e39fdjsollen@google.com    fMeasureFPS = !fMeasureFPS;
19136ff82553df07ec9502bfe5b4a97a2c2aaf15e39fdjsollen@google.com    this->updateTitle();
19146ff82553df07ec9502bfe5b4a97a2c2aaf15e39fdjsollen@google.com    this->inval(NULL);
19156ff82553df07ec9502bfe5b4a97a2c2aaf15e39fdjsollen@google.com}
19166ff82553df07ec9502bfe5b4a97a2c2aaf15e39fdjsollen@google.com
19178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDumpCanvas.h"
19188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
19198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SampleWindow::onHandleKey(SkKey key) {
1920f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    {
1921f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        SkView* view = curr_view(this);
1922f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        if (view) {
1923f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            SkEvent evt(gKeyEvtName);
1924f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            evt.setFast32(key);
1925f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            if (view->doQuery(&evt)) {
1926f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                return true;
1927f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            }
1928f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
1929f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
19308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (key) {
19318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case kRight_SkKey:
19328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            if (this->nextSample()) {
19338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                return true;
19348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
19358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
19368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case kLeft_SkKey:
1937e2dd973e4b0dac58610b73d577e91899c2b1c4eascroggo@google.com            if (this->previousSample()) {
1938e2dd973e4b0dac58610b73d577e91899c2b1c4eascroggo@google.com                return true;
1939e2dd973e4b0dac58610b73d577e91899c2b1c4eascroggo@google.com            }
19408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return true;
19418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case kUp_SkKey:
1942dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org            if (USE_ARROWS_FOR_ZOOM) {
1943cefd981a4c4e90be33d1433592f11f84a102d2e0bungeman@google.com                this->changeZoomLevel(1.f / 32.f);
1944dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org            } else {
1945dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org                fNClip = !fNClip;
1946dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org                this->inval(NULL);
1947e32b5837c3f35b80502759f3de3e8cadf6625f4edjsollen@google.com                this->updateTitle();
1948dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org            }
19498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return true;
19508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        case kDown_SkKey:
1951dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org            if (USE_ARROWS_FOR_ZOOM) {
1952cefd981a4c4e90be33d1433592f11f84a102d2e0bungeman@google.com                this->changeZoomLevel(-1.f / 32.f);
1953dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org            } else {
1954a8c183125f2861067daf432cada06d431a795cd0commit-bot@chromium.org                this->setColorType(cycle_colortypes(this->getBitmap().colorType()));
1955e32b5837c3f35b80502759f3de3e8cadf6625f4edjsollen@google.com                this->updateTitle();
1956dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org            }
19578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return true;
19586f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org        case kOK_SkKey: {
19596f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org            SkString title;
19606f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org            if (curr_title(this, &title)) {
19616f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org                writeTitleToPrefs(title.c_str());
19628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
19638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return true;
19646f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org        }
196534245c7871f6339de8cc2be8fb1090ca3cba54efreed@android.com        case kBack_SkKey:
1966db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com            this->showOverview();
196734245c7871f6339de8cc2be8fb1090ca3cba54efreed@android.com            return true;
19688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        default:
19698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            break;
19708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
19718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return this->INHERITED::onHandleKey(key);
19728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
19738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
197452f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com///////////////////////////////////////////////////////////////////////////////
197552f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com
197652f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.comstatic const char gGestureClickType[] = "GestureClickType";
197752f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com
1978d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggobool SampleWindow::onDispatchClick(int x, int y, Click::State state,
19794d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com        void* owner, unsigned modi) {
19800f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    if (Click::kMoved_State == state) {
19810f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo        updatePointer(x, y);
19820f185c2709f638c0f4d452cb38b434cadfd15ab9Scroggo    }
1983e1ca705cac4b946993f6cbf798e2a0ba27e739f3reed@google.com    int w = SkScalarRoundToInt(this->width());
1984e1ca705cac4b946993f6cbf798e2a0ba27e739f3reed@google.com    int h = SkScalarRoundToInt(this->height());
1985dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org
1986dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org    // check for the resize-box
1987dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org    if (w - x < 16 && h - y < 16) {
1988dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org        return false;   // let the OS handle the click
19894605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com    }
1990db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    else if (fMagnify) {
1991db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com        //it's only necessary to update the drawing if there's a click
1992db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com        this->inval(NULL);
1993db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com        return false; //prevent dragging while magnify is enabled
19944d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com    } else {
19954d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com        // capture control+option, and trigger debugger
19964d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com        if ((modi & kControl_SkModifierKey) && (modi & kOption_SkModifierKey)) {
19974d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com            if (Click::kDown_State == state) {
19984d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com                SkEvent evt("debug-hit-test");
19994d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com                evt.setS32("debug-hit-test-x", x);
20004d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com                evt.setS32("debug-hit-test-y", y);
20014d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com                curr_view(this)->doEvent(evt);
20024d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com            }
20034d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com            return true;
20044d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com        } else {
20054d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com            return this->INHERITED::onDispatchClick(x, y, state, owner, modi);
20064d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com        }
2007dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org    }
2008dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org}
2009dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org
201052f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.comclass GestureClick : public SkView::Click {
201152f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.compublic:
201252f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com    GestureClick(SkView* target) : SkView::Click(target) {
201352f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com        this->setType(gGestureClickType);
201452f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com    }
201552f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com
201652f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com    static bool IsGesture(Click* click) {
201752f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com        return click->isType(gGestureClickType);
201852f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com    }
201952f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com};
202052f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com
20214d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.comSkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y,
20224d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com                                                unsigned modi) {
202352f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com    return new GestureClick(this);
202452f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com}
202552f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com
202652f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.combool SampleWindow::onClick(Click* click) {
202752f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com    if (GestureClick::IsGesture(click)) {
20284d4f281b28c3db0513ea1a68db4577e2518002b3bsalomon@google.com        float x = static_cast<float>(click->fICurr.fX);
20294d4f281b28c3db0513ea1a68db4577e2518002b3bsalomon@google.com        float y = static_cast<float>(click->fICurr.fY);
20304605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
203152f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com        switch (click->fState) {
203252f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com            case SkView::Click::kDown_State:
2033d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo                fGesture.touchBegin(click->fOwner, x, y);
203452f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com                break;
203552f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com            case SkView::Click::kMoved_State:
2036d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo                fGesture.touchMoved(click->fOwner, x, y);
2037f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com                this->updateMatrix();
203852f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com                break;
203952f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com            case SkView::Click::kUp_State:
2040d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo                fGesture.touchEnd(click->fOwner);
2041f03bb566e25ace918f8fdda3cb8426626a00894creed@google.com                this->updateMatrix();
204252f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com                break;
204352f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com        }
204452f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com        return true;
204552f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com    }
204652f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com    return false;
204752f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com}
204852f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com
204952f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com///////////////////////////////////////////////////////////////////////////////
205052f57e1d11a00bdc5efb96709446546cdbc1ff9dreed@google.com
20518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SampleWindow::loadView(SkView* view) {
20528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkView::F2BIter iter(this);
20538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkView* prev = iter.next();
20548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (prev) {
20558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        prev->detachFromParent();
20568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
20574605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
20588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    view->setVisibleP(true);
2059f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    view->setClipToBounds(false);
20608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->attachChildToFront(view)->unref();
20618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    view->setSize(this->width(), this->height());
20628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2063921091f29e2ecfec55f4d1ac5611b4aa4731e7d6yangsu@google.com    //repopulate the slide menu when a view is loaded
20647dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    fSlideMenu->reset();
2065388974f1dd4d7f1a202aac8a007dc2a4eaf051cbdjsollen@google.com
2066b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com    (void)SampleView::SetUsePipe(view, fPipeState);
2067bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org    if (SampleView::IsSampleView(view)) {
2068bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org        SampleView* sampleView = (SampleView*)view;
2069bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org        sampleView->requestMenu(fSlideMenu);
2070bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org        sampleView->onTileSizeChanged(this->tileSize());
2071bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org    }
20727dadc748d96fdf6c1eadc960f1481807d2ac90cfscroggo@google.com    this->onUpdateMenu(fSlideMenu);
20738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->updateTitle();
20748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
20758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2076098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.comstatic const char* gDeviceTypePrefix[] = {
20778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    "raster: ",
20788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    "picture: ",
2079cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
208074913722bfe5e4b6810545891958e3d8e9c63791bsalomon@google.com    "opengl: ",
2081b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com#if SK_ANGLE
2082b442a6d5726569364e15c3056bbfb59b9b6e38cerobertphillips@google.com    "angle: ",
2083cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif // SK_ANGLE
208474913722bfe5e4b6810545891958e3d8e9c63791bsalomon@google.com    "null-gl: "
2085cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif // SK_SUPPORT_GPU
20868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
2087cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.comSK_COMPILE_ASSERT(SK_ARRAY_COUNT(gDeviceTypePrefix) == SampleWindow::kDeviceTypeCnt,
2088cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com                  array_size_mismatch);
20898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2090db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.comstatic const char* trystate_str(SkOSMenu::TriState state,
2091569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com                                const char trueStr[], const char falseStr[]) {
2092db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    if (SkOSMenu::kOnState == state) {
2093569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com        return trueStr;
2094db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    } else if (SkOSMenu::kOffState == state) {
2095569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com        return falseStr;
2096569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com    }
2097569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com    return NULL;
2098569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com}
2099569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com
21008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SampleWindow::updateTitle() {
21016f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    SkView* view = curr_view(this);
21028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21036f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    SkString title;
21046f6e8c39a7b07f8a916a60ad27f17bd13f3c89admike@reedtribe.org    if (!curr_title(this, &title)) {
21058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        title.set("<unknown>");
21068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21072e7b43d33cc495663cb814a7a9d1ecdc09c31828bsalomon@google.com
2108098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    title.prepend(gDeviceTypePrefix[fDeviceType]);
21098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    title.prepend(" ");
2111c3b3266b7db2f1a41d41ecac010c766b7ad8eebcreed    title.prepend(sk_tool_utils::colortype_name(this->getBitmap().colorType()));
21122e7b43d33cc495663cb814a7a9d1ecdc09c31828bsalomon@google.com
2113cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    if (fTilingMode != kNo_Tiling) {
2114cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org        title.prependf("<T: %s> ", gTilingInfo[fTilingMode].label);
2115cc63b32eb8702bfa4d965fa443cf00fc1c1d10b7commit-bot@chromium.org    }
21168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fAnimating) {
21178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        title.prepend("<A> ");
21188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
21196c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.com    if (fRotate) {
21206c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.com        title.prepend("<R> ");
21216c5f6f25b43f1210decb48956c1a2fbe5b58f889reed@android.com    }
2122e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com    if (fNClip) {
2123e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com        title.prepend("<C> ");
2124e522ca5d5f249bd51a00cb68bb051f811d0a9e85reed@android.com    }
2125e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com    if (fPerspAnim) {
2126e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com        title.prepend("<K> ");
2127e8f091093509d946a7f6722b40033a3dc946dc0fbsalomon@google.com    }
2128569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com
2129569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com    title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
2130569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com    title.prepend(trystate_str(fAAState, "AA ", "aa "));
213115bc13d20fe79914c7128881e45064becd11bc9dreed@google.com    title.prepend(gFilterLevelStates[fFilterLevelIndex].fLabel);
213296aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    title.prepend(trystate_str(fSubpixelState, "S ", "s "));
2133569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com    title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
2134569e0435af62bdb88a662fa0592f75673a17afd9reed@google.com    title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
213596aabc850835f6d08a117c0277be93848ddbf785bungeman@google.com    title.prepend(gHintingStates[fHintingState].label);
2136dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org
2137dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org    if (fZoomLevel) {
2138e32b5837c3f35b80502759f3de3e8cadf6625f4edjsollen@google.com        title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
2139dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org    }
21404605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
21412eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    if (fMeasureFPS) {
214243e10145911bde6ac8c3f14fd46611a13c76f8b9reed@google.com        title.appendf(" %8.3f ms", fMeasureFPS_Time / (float)FPS_REPEAT_COUNT);
21432eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    }
2144a6ff4dc22d5006dc588769aa20c8b0bb4fde6d43reed@google.com    if (SampleView::IsSampleView(view)) {
2145b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        switch (fPipeState) {
2146b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com            case SkOSMenu::kOnState:
2147b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com                title.prepend("<Pipe> ");
2148b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com                break;
2149b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com            case SkOSMenu::kMixedState:
2150b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com                title.prepend("<Tiled Pipe> ");
2151b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com                break;
21524605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
2153b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com            default:
2154b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com                break;
2155b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        }
2156a6ff4dc22d5006dc588769aa20c8b0bb4fde6d43reed@google.com        title.prepend("! ");
2157a6ff4dc22d5006dc588769aa20c8b0bb4fde6d43reed@google.com    }
21582eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
2159cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
216082502e2e3966a9b0e48665f20154eaacd2a452b3bsalomon@google.com    if (IsGpuDeviceType(fDeviceType) &&
21614750fa55f27e43770e7f5f3161ebdbac42e0da8arobertphillips@google.com        NULL != fDevManager &&
216282502e2e3966a9b0e48665f20154eaacd2a452b3bsalomon@google.com        fDevManager->getGrRenderTarget() &&
21631195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com        fDevManager->getGrRenderTarget()->numSamples() > 0) {
21641195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com        title.appendf(" [MSAA: %d]",
21651195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com                       fDevManager->getGrRenderTarget()->numSamples());
21661195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com    }
2167cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
21681195925b05ee9d666ea8a8f68fde5d8ca7e49b04bsalomon@google.com
21698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->setTitle(title.c_str());
21708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
21718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SampleWindow::onSizeChange() {
21738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->INHERITED::onSizeChange();
21744605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
21758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkView::F2BIter iter(this);
21768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkView* view = iter.next();
21778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    view->setSize(this->width(), this->height());
21782e7b43d33cc495663cb814a7a9d1ecdc09c31828bsalomon@google.com
21798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // rebuild our clippath
21808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
21818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkScalar W = this->width();
21828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const SkScalar H = this->height();
21832e7b43d33cc495663cb814a7a9d1ecdc09c31828bsalomon@google.com
21848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fClipPath.reset();
21858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if 0
21868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
21878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkRect r;
21888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
21898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
21908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                fClipPath.addRect(r);
21918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
21928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
21938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect r;
21948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.set(0, 0, W, H);
21958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fClipPath.addRect(r, SkPath::kCCW_Direction);
21968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r.set(W/4, H/4, W*3/4, H*3/4);
21978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fClipPath.addRect(r, SkPath::kCW_Direction);
21988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
21998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
22002e7b43d33cc495663cb814a7a9d1ecdc09c31828bsalomon@google.com
22012c8208f3a444098aee562f5a1ef620142807eda2Scroggo    fZoomCenterX = SkScalarHalf(this->width());
22022c8208f3a444098aee562f5a1ef620142807eda2Scroggo    fZoomCenterY = SkScalarHalf(this->height());
22032c8208f3a444098aee562f5a1ef620142807eda2Scroggo
220456c69773aea56c6c6bd47bc7e7970dd081205184djsollen@google.com#ifdef SK_BUILD_FOR_ANDROID
2205b4490c7435df46261c00dbfdcdd9d2e9d56d1f52Scroggo    // FIXME: The first draw after a size change does not work on Android, so
2206b4490c7435df46261c00dbfdcdd9d2e9d56d1f52Scroggo    // we post an invalidate.
220762b65b0913566381db468277de07e5dd2a9237caScroggo    this->postInvalDelay();
2208716a0387ca197ca728ba691f3d812d4252c0f10bScroggo#endif
22098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->updateTitle();    // to refresh our config
2210098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    fDevManager->windowSizeChanged(this);
2211bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org
2212bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org    if (fTilingMode != kNo_Tiling && SampleView::IsSampleView(view)) {
2213bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org        ((SampleView*)view)->onTileSizeChanged(this->tileSize());
2214bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org    }
22158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
22168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////////////////////////
22188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2219a6ff4dc22d5006dc588769aa20c8b0bb4fde6d43reed@google.comstatic const char is_sample_view_tag[] = "sample-is-sample-view";
22202eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgstatic const char repeat_count_tag[] = "sample-set-repeat-count";
22210faac1e8579088a39f38d02ff675f14d7deb608dreed@google.comstatic const char set_use_pipe_tag[] = "sample-set-use-pipe";
22222eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
2223a6ff4dc22d5006dc588769aa20c8b0bb4fde6d43reed@google.combool SampleView::IsSampleView(SkView* view) {
2224a6ff4dc22d5006dc588769aa20c8b0bb4fde6d43reed@google.com    SkEvent evt(is_sample_view_tag);
2225a6ff4dc22d5006dc588769aa20c8b0bb4fde6d43reed@google.com    return view->doQuery(&evt);
2226a6ff4dc22d5006dc588769aa20c8b0bb4fde6d43reed@google.com}
2227a6ff4dc22d5006dc588769aa20c8b0bb4fde6d43reed@google.com
2228f21833999d90ad9c8c584cff3238797b39cfc00breed@google.combool SampleView::SetRepeatDraw(SkView* view, int count) {
22292eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    SkEvent evt(repeat_count_tag);
22302eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    evt.setFast32(count);
2231f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com    return view->doEvent(evt);
22322eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org}
22332eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
2234b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.combool SampleView::SetUsePipe(SkView* view, SkOSMenu::TriState state) {
2235b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com    SkEvent evt;
2236b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com    evt.setS32(set_use_pipe_tag, state);
22370faac1e8579088a39f38d02ff675f14d7deb608dreed@google.com    return view->doEvent(evt);
22380faac1e8579088a39f38d02ff675f14d7deb608dreed@google.com}
22390faac1e8579088a39f38d02ff675f14d7deb608dreed@google.com
22402eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgbool SampleView::onEvent(const SkEvent& evt) {
22412eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    if (evt.isType(repeat_count_tag)) {
22422eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org        fRepeatCount = evt.getFast32();
22432eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org        return true;
22442eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    }
22454d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com
2246b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com    int32_t pipeHolder;
2247b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com    if (evt.findS32(set_use_pipe_tag, &pipeHolder)) {
2248b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        fPipeState = static_cast<SkOSMenu::TriState>(pipeHolder);
22490faac1e8579088a39f38d02ff675f14d7deb608dreed@google.com        return true;
22500faac1e8579088a39f38d02ff675f14d7deb608dreed@google.com    }
22514d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com
22522eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    return this->INHERITED::onEvent(evt);
22532eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org}
22542eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
22552eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgbool SampleView::onQuery(SkEvent* evt) {
2256a6ff4dc22d5006dc588769aa20c8b0bb4fde6d43reed@google.com    if (evt->isType(is_sample_view_tag)) {
2257a6ff4dc22d5006dc588769aa20c8b0bb4fde6d43reed@google.com        return true;
2258a6ff4dc22d5006dc588769aa20c8b0bb4fde6d43reed@google.com    }
22592eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    return this->INHERITED::onQuery(evt);
22602eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org}
22612eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
226264e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com
226364e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.comclass SimplePC : public SkGPipeController {
226464e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.compublic:
226564e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    SimplePC(SkCanvas* target);
226664e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    ~SimplePC();
22674605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
226864e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    virtual void* requestBlock(size_t minRequest, size_t* actual);
226964e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    virtual void notifyWritten(size_t bytes);
227064e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com
227164e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.comprivate:
2272961ddb04a0a7aba843032d829ab867518e52559ereed@google.com    SkGPipeReader   fReader;
2273961ddb04a0a7aba843032d829ab867518e52559ereed@google.com    void*           fBlock;
2274961ddb04a0a7aba843032d829ab867518e52559ereed@google.com    size_t          fBlockSize;
2275961ddb04a0a7aba843032d829ab867518e52559ereed@google.com    size_t          fBytesWritten;
2276961ddb04a0a7aba843032d829ab867518e52559ereed@google.com    int             fAtomsWritten;
227764e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    SkGPipeReader::Status   fStatus;
227864e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com
227964e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    size_t        fTotalWritten;
228064e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com};
228164e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com
228264e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.comSimplePC::SimplePC(SkCanvas* target) : fReader(target) {
228364e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    fBlock = NULL;
228464e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    fBlockSize = fBytesWritten = 0;
228564e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    fStatus = SkGPipeReader::kDone_Status;
228664e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    fTotalWritten = 0;
2287961ddb04a0a7aba843032d829ab867518e52559ereed@google.com    fAtomsWritten = 0;
228874b7ffda687c66d46ac3cfa4f2baedd4c62e3fbescroggo@google.com    fReader.setBitmapDecoder(&SkImageDecoder::DecodeMemory);
228964e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com}
229064e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com
229164e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.comSimplePC::~SimplePC() {
229264e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com//    SkASSERT(SkGPipeReader::kDone_Status == fStatus);
22930faac1e8579088a39f38d02ff675f14d7deb608dreed@google.com    if (fTotalWritten) {
2294b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
2295b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com                 fAtomsWritten, fStatus);
2296db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com#ifdef  PIPE_FILE
2297b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        //File is open in append mode
2298b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        FILE* f = fopen(FILE_PATH, "ab");
2299b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        SkASSERT(f != NULL);
2300b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
2301b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        fclose(f);
2302db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com#endif
2303db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com#ifdef PIPE_NET
2304b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        if (fAtomsWritten > 1 && fTotalWritten > 4) { //ignore done
2305b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com            gServer.acceptConnections();
2306b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com            gServer.writePacket(fBlock, fTotalWritten);
2307b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        }
2308db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com#endif
23090faac1e8579088a39f38d02ff675f14d7deb608dreed@google.com    }
2310db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    sk_free(fBlock);
231164e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com}
231264e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com
231364e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.comvoid* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
231464e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    sk_free(fBlock);
231564e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com
231664e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    fBlockSize = minRequest * 4;
231764e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    fBlock = sk_malloc_throw(fBlockSize);
231864e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    fBytesWritten = 0;
231964e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    *actual = fBlockSize;
232064e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    return fBlock;
232164e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com}
232264e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com
232364e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.comvoid SimplePC::notifyWritten(size_t bytes) {
232464e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    SkASSERT(fBytesWritten + bytes <= fBlockSize);
232564e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
232664e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    SkASSERT(SkGPipeReader::kError_Status != fStatus);
232764e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    fBytesWritten += bytes;
232864e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com    fTotalWritten += bytes;
23294605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
2330961ddb04a0a7aba843032d829ab867518e52559ereed@google.com    fAtomsWritten += 1;
233164e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com}
233264e3eb24e52a84613ff8c1da5dac6f4b5ba38320reed@google.com
2333db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.comvoid SampleView::draw(SkCanvas* canvas) {
2334b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com    if (SkOSMenu::kOffState == fPipeState) {
2335b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        this->INHERITED::draw(canvas);
2336b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com    } else {
2337db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com        SkGPipeWriter writer;
2338db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com        SimplePC controller(canvas);
2339b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        TiledPipeController tc(canvas->getDevice()->accessBitmap(false),
234074b7ffda687c66d46ac3cfa4f2baedd4c62e3fbescroggo@google.com                               &SkImageDecoder::DecodeMemory,
2341b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com                               &canvas->getTotalMatrix());
2342b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        SkGPipeController* pc;
2343b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        if (SkOSMenu::kMixedState == fPipeState) {
2344b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com            pc = &tc;
2345b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        } else {
2346b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com            pc = &controller;
2347b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        }
2348dde0956375e87027df2fdd80d430dd819c217aacreed@google.com        uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
23494605a3f3ff8fa2072f98e6bcb71bc43d4c6196d6chudy@google.com
2350b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com        canvas = writer.startRecording(pc, flags);
2351db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com        //Must draw before controller goes out of scope and sends data
2352db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com        this->INHERITED::draw(canvas);
2353ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com        //explicitly end recording to ensure writer is flushed before the memory
2354ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com        //is freed in the deconstructor of the controller
2355ef7bdfac618f60e9edc9f42cd4661d563937e6d8yangsu@google.com        writer.endRecording();
23560faac1e8579088a39f38d02ff675f14d7deb608dreed@google.com    }
2357db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com}
23584d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com
2359db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.comvoid SampleView::onDraw(SkCanvas* canvas) {
236081e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.com    this->onDrawBackground(canvas);
236181e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.com
23622eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    for (int i = 0; i < fRepeatCount; i++) {
236385cade0a10217a50aebb495099ecc0435fe08b23scroggo@google.com        SkAutoCanvasRestore acr(canvas, true);
236485cade0a10217a50aebb495099ecc0435fe08b23scroggo@google.com        this->onDrawContent(canvas);
23652eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    }
23662eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org}
23672eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
23682eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgvoid SampleView::onDrawBackground(SkCanvas* canvas) {
2369f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com    canvas->drawColor(fBGColor);
23702eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org}
23712eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
23722eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org///////////////////////////////////////////////////////////////////////////////
23732eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
2374f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comtemplate <typename T> void SkTBSort(T array[], int count) {
2375f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    for (int i = 1; i < count - 1; i++) {
2376f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        bool didSwap = false;
2377f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        for (int j = count - 1; j > i; --j) {
2378f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            if (array[j] < array[j-1]) {
2379f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                T tmp(array[j-1]);
2380f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                array[j-1] = array[j];
2381f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                array[j] = tmp;
2382f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                didSwap = true;
2383f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            }
2384f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
2385f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        if (!didSwap) {
2386f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            break;
2387f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
2388f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
23892e7b43d33cc495663cb814a7a9d1ecdc09c31828bsalomon@google.com
2390f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    for (int k = 0; k < count - 1; k++) {
2391f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        SkASSERT(!(array[k+1] < array[k]));
2392f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
2393f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
2394f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
2395f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com#include "SkRandom.h"
2396f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
2397e0e7cfe44bb9d66d76120a79e5275c294bacaa22commit-bot@chromium.orgstatic void rand_rect(SkIRect* rect, SkRandom& rand) {
2398f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int bits = 8;
2399f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    int shift = 32 - bits;
2400f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
2401f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com              rand.nextU() >> shift, rand.nextU() >> shift);
2402f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    rect->sort();
2403f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
2404f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
2405f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comstatic void dumpRect(const SkIRect& r) {
2406f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkDebugf(" { %d, %d, %d, %d },\n",
2407f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com             r.fLeft, r.fTop,
2408f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com             r.fRight, r.fBottom);
2409f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
2410f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
2411f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comstatic void test_rects(const SkIRect rect[], int count) {
2412f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    SkRegion rgn0, rgn1;
2413f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
2414f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    for (int i = 0; i < count; i++) {
2415f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        rgn0.op(rect[i], SkRegion::kUnion_Op);
2416f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     //   dumpRect(rect[i]);
2417f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
2418f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    rgn1.setRects(rect, count);
2419f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
2420f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    if (rgn0 != rgn1) {
2421f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        SkDebugf("\n");
2422f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        for (int i = 0; i < count; i++) {
2423f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            dumpRect(rect[i]);
2424f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
2425f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        SkDebugf("\n");
2426f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
2427f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
2428f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
2429f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comstatic void test() {
2430f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    size_t i;
2431f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
2432f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    const SkIRect r0[] = {
2433f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        { 0, 0, 1, 1 },
2434f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        { 2, 2, 3, 3 },
2435f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    };
2436f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    const SkIRect r1[] = {
2437f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        { 0, 0, 1, 3 },
2438f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        { 1, 1, 2, 2 },
2439f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        { 2, 0, 3, 3 },
2440f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    };
2441f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    const SkIRect r2[] = {
2442f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        { 0, 0, 1, 2 },
2443f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        { 2, 1, 3, 3 },
2444f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        { 4, 0, 5, 1 },
2445f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        { 6, 0, 7, 4 },
2446f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    };
2447f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
2448f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    static const struct {
2449f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        const SkIRect* fRects;
2450f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        int            fCount;
2451f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    } gRecs[] = {
2452f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        { r0, SK_ARRAY_COUNT(r0) },
2453f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        { r1, SK_ARRAY_COUNT(r1) },
2454f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        { r2, SK_ARRAY_COUNT(r2) },
2455f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    };
2456f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
2457f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
2458f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        test_rects(gRecs[i].fRects, gRecs[i].fCount);
2459f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
24602e7b43d33cc495663cb814a7a9d1ecdc09c31828bsalomon@google.com
2461e0e7cfe44bb9d66d76120a79e5275c294bacaa22commit-bot@chromium.org    SkRandom rand;
2462f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    for (i = 0; i < 10000; i++) {
2463f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        SkRegion rgn0, rgn1;
2464f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
2465f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        const int N = 8;
2466f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        SkIRect rect[N];
2467f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        for (int j = 0; j < N; j++) {
2468f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            rand_rect(&rect[j], rand);
2469f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
2470f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        test_rects(rect, N);
2471f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    }
2472f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
2473f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
247402939ce4bddd3223d9e8cc98d4abd02939b7f70acaryclark@google.com// FIXME: this should be in a header
247502939ce4bddd3223d9e8cc98d4abd02939b7f70acaryclark@google.comSkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
247678b8253c0a389a484e15439722e35a1658eb3b01senorblanco@chromium.orgSkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
247702939ce4bddd3223d9e8cc98d4abd02939b7f70acaryclark@google.com    if (false) { // avoid bit rot, suppress warning
247802939ce4bddd3223d9e8cc98d4abd02939b7f70acaryclark@google.com        test();
247902939ce4bddd3223d9e8cc98d4abd02939b7f70acaryclark@google.com    }
2480098e96df6ab0327f18537c7006ff2b0b4116ee30bsalomon@google.com    return new SampleWindow(hwnd, argc, argv, NULL);
24818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
24828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
248302939ce4bddd3223d9e8cc98d4abd02939b7f70acaryclark@google.com// FIXME: this should be in a header
248402939ce4bddd3223d9e8cc98d4abd02939b7f70acaryclark@google.comvoid get_preferred_size(int* x, int* y, int* width, int* height);
24858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid get_preferred_size(int* x, int* y, int* width, int* height) {
24868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    *x = 10;
24878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    *y = 50;
24888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    *width = 640;
24898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    *height = 480;
24908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
24918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
24925987f58036574ccf23049b5fe71f29a441d0641dcaryclark@google.com#ifdef SK_BUILD_FOR_IOS
24935987f58036574ccf23049b5fe71f29a441d0641dcaryclark@google.comvoid save_args(int argc, char *argv[]) {
24945987f58036574ccf23049b5fe71f29a441d0641dcaryclark@google.com}
24955987f58036574ccf23049b5fe71f29a441d0641dcaryclark@google.com#endif
24965987f58036574ccf23049b5fe71f29a441d0641dcaryclark@google.com
249702939ce4bddd3223d9e8cc98d4abd02939b7f70acaryclark@google.com// FIXME: this should be in a header
249802939ce4bddd3223d9e8cc98d4abd02939b7f70acaryclark@google.comvoid application_init();
24998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid application_init() {
25008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//    setenv("ANDROID_ROOT", "../../../data", 0);
2501e191b16fda2f5afea3a90653c1cd4f5a3c436e89reed@android.com#ifdef SK_BUILD_FOR_MAC
25028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    setenv("ANDROID_ROOT", "/android/device/data", 0);
2503e191b16fda2f5afea3a90653c1cd4f5a3c436e89reed@android.com#endif
25042fbc7fa460c99bffa78b9afb9aaa4dc0db68e1babsalomon@google.com    SkGraphics::Init();
25052fbc7fa460c99bffa78b9afb9aaa4dc0db68e1babsalomon@google.com    SkEvent::Init();
25068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
25078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
250802939ce4bddd3223d9e8cc98d4abd02939b7f70acaryclark@google.com// FIXME: this should be in a header
250902939ce4bddd3223d9e8cc98d4abd02939b7f70acaryclark@google.comvoid application_term();
25108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid application_term() {
25112fbc7fa460c99bffa78b9afb9aaa4dc0db68e1babsalomon@google.com    SkEvent::Term();
25122fbc7fa460c99bffa78b9afb9aaa4dc0db68e1babsalomon@google.com    SkGraphics::Term();
25138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2514