Home | History | Annotate | Download | only in samplecode
      1 /*
      2  * Copyright 2011 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "SampleApp.h"
      9 
     10 #include "OverView.h"
     11 #include "Resources.h"
     12 #include "SampleCode.h"
     13 #include "SamplePipeControllers.h"
     14 #include "SkCanvas.h"
     15 #include "SkCommandLineFlags.h"
     16 #include "SkData.h"
     17 #include "SkDevice.h"
     18 #include "SkGPipe.h"
     19 #include "SkGraphics.h"
     20 #include "SkImageEncoder.h"
     21 #include "SkOSFile.h"
     22 #include "SkPDFDevice.h"
     23 #include "SkPDFDocument.h"
     24 #include "SkPaint.h"
     25 #include "SkPicture.h"
     26 #include "SkPictureRecorder.h"
     27 #include "SkStream.h"
     28 #include "SkSurface.h"
     29 #include "SkTSort.h"
     30 #include "SkTime.h"
     31 #include "SkTypeface.h"
     32 #include "SkWindow.h"
     33 #include "TransitionView.h"
     34 #include "sk_tool_utils.h"
     35 
     36 #if SK_SUPPORT_GPU
     37 #include "gl/GrGLInterface.h"
     38 #include "gl/GrGLUtil.h"
     39 #include "GrRenderTarget.h"
     40 #include "GrContext.h"
     41 #include "SkGpuDevice.h"
     42 #else
     43 class GrContext;
     44 #endif
     45 
     46 extern SampleView* CreateSamplePictFileView(const char filename[]);
     47 
     48 class PictFileFactory : public SkViewFactory {
     49     SkString fFilename;
     50 public:
     51     PictFileFactory(const SkString& filename) : fFilename(filename) {}
     52     virtual SkView* operator() () const SK_OVERRIDE {
     53         return CreateSamplePictFileView(fFilename.c_str());
     54     }
     55 };
     56 
     57 #ifdef SAMPLE_PDF_FILE_VIEWER
     58 extern SampleView* CreateSamplePdfFileViewer(const char filename[]);
     59 
     60 class PdfFileViewerFactory : public SkViewFactory {
     61     SkString fFilename;
     62 public:
     63     PdfFileViewerFactory(const SkString& filename) : fFilename(filename) {}
     64     virtual SkView* operator() () const SK_OVERRIDE {
     65         return CreateSamplePdfFileViewer(fFilename.c_str());
     66     }
     67 };
     68 #endif  // SAMPLE_PDF_FILE_VIEWER
     69 
     70 #define PIPE_FILEx
     71 #ifdef  PIPE_FILE
     72 #define FILE_PATH "/path/to/drawing.data"
     73 #endif
     74 
     75 #define PIPE_NETx
     76 #ifdef  PIPE_NET
     77 #include "SkSockets.h"
     78 SkTCPServer gServer;
     79 #endif
     80 
     81 #define USE_ARROWS_FOR_ZOOM true
     82 
     83 #if SK_ANGLE
     84 //#define DEFAULT_TO_ANGLE 1
     85 #else
     86 #define DEFAULT_TO_GPU 0 // if 1 default rendering is on GPU
     87 #endif
     88 
     89 #define ANIMATING_EVENTTYPE "nextSample"
     90 #define ANIMATING_DELAY     250
     91 
     92 #ifdef SK_DEBUG
     93     #define FPS_REPEAT_MULTIPLIER   1
     94 #else
     95     #define FPS_REPEAT_MULTIPLIER   10
     96 #endif
     97 #define FPS_REPEAT_COUNT    (10 * FPS_REPEAT_MULTIPLIER)
     98 
     99 static SampleWindow* gSampleWindow;
    100 
    101 static bool gShowGMBounds;
    102 
    103 static void post_event_to_sink(SkEvent* evt, SkEventSink* sink) {
    104     evt->setTargetID(sink->getSinkID())->post();
    105 }
    106 
    107 ///////////////////////////////////////////////////////////////////////////////
    108 
    109 static const char* skip_until(const char* str, const char* skip) {
    110     if (!str) {
    111         return NULL;
    112     }
    113     return strstr(str, skip);
    114 }
    115 
    116 static const char* skip_past(const char* str, const char* skip) {
    117     const char* found = skip_until(str, skip);
    118     if (!found) {
    119         return NULL;
    120     }
    121     return found + strlen(skip);
    122 }
    123 
    124 static const char* gPrefFileName = "sampleapp_prefs.txt";
    125 
    126 static bool readTitleFromPrefs(SkString* title) {
    127     SkFILEStream stream(gPrefFileName);
    128     if (!stream.isValid()) {
    129         return false;
    130     }
    131 
    132     size_t len = stream.getLength();
    133     SkString data(len);
    134     stream.read(data.writable_str(), len);
    135     const char* s = data.c_str();
    136 
    137     s = skip_past(s, "curr-slide-title");
    138     s = skip_past(s, "=");
    139     s = skip_past(s, "\"");
    140     const char* stop = skip_until(s, "\"");
    141     if (stop > s) {
    142         title->set(s, stop - s);
    143         return true;
    144     }
    145     return false;
    146 }
    147 
    148 static void writeTitleToPrefs(const char* title) {
    149     SkFILEWStream stream(gPrefFileName);
    150     SkString data;
    151     data.printf("curr-slide-title = \"%s\"\n", title);
    152     stream.write(data.c_str(), data.size());
    153 }
    154 
    155 ///////////////////////////////////////////////////////////////////////////////
    156 
    157 class SampleWindow::DefaultDeviceManager : public SampleWindow::DeviceManager {
    158 public:
    159 
    160     DefaultDeviceManager() {
    161 #if SK_SUPPORT_GPU
    162         fCurContext = NULL;
    163         fCurIntf = NULL;
    164         fCurRenderTarget = NULL;
    165         fMSAASampleCount = 0;
    166 #endif
    167         fBackend = kNone_BackEndType;
    168     }
    169 
    170     virtual ~DefaultDeviceManager() {
    171 #if SK_SUPPORT_GPU
    172         SkSafeUnref(fCurContext);
    173         SkSafeUnref(fCurIntf);
    174         SkSafeUnref(fCurRenderTarget);
    175 #endif
    176     }
    177 
    178     virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) {
    179         SkASSERT(kNone_BackEndType == fBackend);
    180 
    181         fBackend = kNone_BackEndType;
    182 
    183 #if SK_SUPPORT_GPU
    184         switch (win->getDeviceType()) {
    185             case kRaster_DeviceType:
    186                 // fallthrough
    187             case kPicture_DeviceType:
    188                 // fallthrough
    189             case kGPU_DeviceType:
    190                 // fallthrough
    191             case kNullGPU_DeviceType:
    192                 // all these guys use the native backend
    193                 fBackend = kNativeGL_BackEndType;
    194                 break;
    195 #if SK_ANGLE
    196             case kANGLE_DeviceType:
    197                 // ANGLE is really the only odd man out
    198                 fBackend = kANGLE_BackEndType;
    199                 break;
    200 #endif // SK_ANGLE
    201             default:
    202                 SkASSERT(false);
    203                 break;
    204         }
    205         AttachmentInfo attachmentInfo;
    206         bool result = win->attach(fBackend, msaaSampleCount, &attachmentInfo);
    207         if (!result) {
    208             SkDebugf("Failed to initialize GL");
    209             return;
    210         }
    211         fMSAASampleCount = msaaSampleCount;
    212 
    213         SkASSERT(NULL == fCurIntf);
    214         SkAutoTUnref<const GrGLInterface> glInterface;
    215         switch (win->getDeviceType()) {
    216             case kRaster_DeviceType:
    217                 // fallthrough
    218             case kPicture_DeviceType:
    219                 // fallthrough
    220             case kGPU_DeviceType:
    221                 // all these guys use the native interface
    222                 glInterface.reset(GrGLCreateNativeInterface());
    223                 break;
    224 #if SK_ANGLE
    225             case kANGLE_DeviceType:
    226                 glInterface.reset(GrGLCreateANGLEInterface());
    227                 break;
    228 #endif // SK_ANGLE
    229             case kNullGPU_DeviceType:
    230                 glInterface.reset(GrGLCreateNullInterface());
    231                 break;
    232             default:
    233                 SkASSERT(false);
    234                 break;
    235         }
    236 
    237         // Currently SampleApp does not use NVPR. TODO: Provide an NVPR device type that is skipped
    238         // when the driver doesn't support NVPR.
    239         fCurIntf = GrGLInterfaceRemoveNVPR(glInterface.get());
    240 
    241         SkASSERT(NULL == fCurContext);
    242         fCurContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf);
    243 
    244         if (NULL == fCurContext || NULL == fCurIntf) {
    245             // We need some context and interface to see results
    246             SkSafeUnref(fCurContext);
    247             SkSafeUnref(fCurIntf);
    248             fCurContext = NULL;
    249             fCurIntf = NULL;
    250             SkDebugf("Failed to setup 3D");
    251 
    252             win->detach();
    253         }
    254 #endif // SK_SUPPORT_GPU
    255         // call windowSizeChanged to create the render target
    256         this->windowSizeChanged(win);
    257     }
    258 
    259     virtual void tearDownBackend(SampleWindow *win) {
    260 #if SK_SUPPORT_GPU
    261         SkSafeUnref(fCurContext);
    262         fCurContext = NULL;
    263 
    264         SkSafeUnref(fCurIntf);
    265         fCurIntf = NULL;
    266 
    267         SkSafeUnref(fCurRenderTarget);
    268         fCurRenderTarget = NULL;
    269 #endif
    270         win->detach();
    271         fBackend = kNone_BackEndType;
    272     }
    273 
    274     virtual SkSurface* createSurface(SampleWindow::DeviceType dType,
    275                                      SampleWindow* win) SK_OVERRIDE {
    276 #if SK_SUPPORT_GPU
    277         if (IsGpuDeviceType(dType) && fCurContext) {
    278             return SkSurface::NewRenderTargetDirect(fCurRenderTarget);
    279         }
    280 #endif
    281         return NULL;
    282     }
    283 
    284     virtual void publishCanvas(SampleWindow::DeviceType dType,
    285                                SkCanvas* canvas,
    286                                SampleWindow* win) {
    287 #if SK_SUPPORT_GPU
    288         if (fCurContext) {
    289             // in case we have queued drawing calls
    290             fCurContext->flush();
    291 
    292             if (!IsGpuDeviceType(dType)) {
    293                 // need to send the raster bits to the (gpu) window
    294                 fCurContext->setRenderTarget(fCurRenderTarget);
    295                 const SkBitmap& bm = win->getBitmap();
    296                 fCurRenderTarget->writePixels(0, 0, bm.width(), bm.height(),
    297                                              SkImageInfo2GrPixelConfig(bm.colorType(),
    298                                                                        bm.alphaType()),
    299                                              bm.getPixels(),
    300                                              bm.rowBytes());
    301             }
    302         }
    303 #endif
    304 
    305         win->present();
    306     }
    307 
    308     virtual void windowSizeChanged(SampleWindow* win) {
    309 #if SK_SUPPORT_GPU
    310         if (fCurContext) {
    311             AttachmentInfo attachmentInfo;
    312             win->attach(fBackend, fMSAASampleCount, &attachmentInfo);
    313 
    314             GrBackendRenderTargetDesc desc;
    315             desc.fWidth = SkScalarRoundToInt(win->width());
    316             desc.fHeight = SkScalarRoundToInt(win->height());
    317             desc.fConfig = kSkia8888_GrPixelConfig;
    318             desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
    319             desc.fSampleCnt = attachmentInfo.fSampleCount;
    320             desc.fStencilBits = attachmentInfo.fStencilBits;
    321             GrGLint buffer;
    322             GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
    323             desc.fRenderTargetHandle = buffer;
    324 
    325             SkSafeUnref(fCurRenderTarget);
    326             fCurRenderTarget = fCurContext->wrapBackendRenderTarget(desc);
    327         }
    328 #endif
    329     }
    330 
    331     virtual GrContext* getGrContext() {
    332 #if SK_SUPPORT_GPU
    333         return fCurContext;
    334 #else
    335         return NULL;
    336 #endif
    337     }
    338 
    339     virtual GrRenderTarget* getGrRenderTarget() SK_OVERRIDE {
    340 #if SK_SUPPORT_GPU
    341         return fCurRenderTarget;
    342 #else
    343         return NULL;
    344 #endif
    345     }
    346 
    347 private:
    348 
    349 #if SK_SUPPORT_GPU
    350     GrContext*              fCurContext;
    351     const GrGLInterface*    fCurIntf;
    352     GrRenderTarget*         fCurRenderTarget;
    353     int fMSAASampleCount;
    354 #endif
    355 
    356     SkOSWindow::SkBackEndTypes fBackend;
    357 
    358     typedef SampleWindow::DeviceManager INHERITED;
    359 };
    360 
    361 ///////////////
    362 static const char view_inval_msg[] = "view-inval-msg";
    363 
    364 void SampleWindow::postInvalDelay() {
    365     (new SkEvent(view_inval_msg, this->getSinkID()))->postDelay(1);
    366 }
    367 
    368 static bool isInvalEvent(const SkEvent& evt) {
    369     return evt.isType(view_inval_msg);
    370 }
    371 //////////////////
    372 
    373 SkFuncViewFactory::SkFuncViewFactory(SkViewCreateFunc func)
    374     : fCreateFunc(func) {
    375 }
    376 
    377 SkView* SkFuncViewFactory::operator() () const {
    378     return (*fCreateFunc)();
    379 }
    380 
    381 #include "GMSampleView.h"
    382 
    383 SkGMSampleViewFactory::SkGMSampleViewFactory(GMFactoryFunc func)
    384     : fFunc(func) {
    385 }
    386 
    387 SkView* SkGMSampleViewFactory::operator() () const {
    388     skiagm::GM* gm = fFunc(NULL);
    389     gm->setMode(skiagm::GM::kSample_Mode);
    390     return new GMSampleView(gm);
    391 }
    392 
    393 SkViewRegister* SkViewRegister::gHead;
    394 SkViewRegister::SkViewRegister(SkViewFactory* fact) : fFact(fact) {
    395     fFact->ref();
    396     fChain = gHead;
    397     gHead = this;
    398 }
    399 
    400 SkViewRegister::SkViewRegister(SkViewCreateFunc func) {
    401     fFact = new SkFuncViewFactory(func);
    402     fChain = gHead;
    403     gHead = this;
    404 }
    405 
    406 SkViewRegister::SkViewRegister(GMFactoryFunc func) {
    407     fFact = new SkGMSampleViewFactory(func);
    408     fChain = gHead;
    409     gHead = this;
    410 }
    411 
    412 class AutoUnrefArray {
    413 public:
    414     AutoUnrefArray() {}
    415     ~AutoUnrefArray() {
    416         int count = fObjs.count();
    417         for (int i = 0; i < count; ++i) {
    418             fObjs[i]->unref();
    419         }
    420     }
    421     SkRefCnt*& push_back() { return *fObjs.append(); }
    422 
    423 private:
    424     SkTDArray<SkRefCnt*> fObjs;
    425 };
    426 
    427 // registers GMs as Samples
    428 // This can't be performed during static initialization because it could be
    429 // run before GMRegistry has been fully built.
    430 static void SkGMRegistyToSampleRegistry() {
    431     static bool gOnce;
    432     static AutoUnrefArray fRegisters;
    433 
    434     if (!gOnce) {
    435         const skiagm::GMRegistry* gmreg = skiagm::GMRegistry::Head();
    436         while (gmreg) {
    437             fRegisters.push_back() = new SkViewRegister(gmreg->factory());
    438             gmreg = gmreg->next();
    439         }
    440         gOnce = true;
    441     }
    442 }
    443 
    444 //////////////////////////////////////////////////////////////////////////////
    445 
    446 enum FlipAxisEnum {
    447     kFlipAxis_X = (1 << 0),
    448     kFlipAxis_Y = (1 << 1)
    449 };
    450 
    451 #include "SkDrawFilter.h"
    452 
    453 struct HintingState {
    454     SkPaint::Hinting hinting;
    455     const char* name;
    456     const char* label;
    457 };
    458 static HintingState gHintingStates[] = {
    459     {SkPaint::kNo_Hinting, "Mixed", NULL },
    460     {SkPaint::kNo_Hinting, "None", "H0 " },
    461     {SkPaint::kSlight_Hinting, "Slight", "Hs " },
    462     {SkPaint::kNormal_Hinting, "Normal", "Hn " },
    463     {SkPaint::kFull_Hinting, "Full", "Hf " },
    464 };
    465 
    466 struct FilterLevelState {
    467     SkPaint::FilterLevel    fLevel;
    468     const char*             fName;
    469     const char*             fLabel;
    470 };
    471 static FilterLevelState gFilterLevelStates[] = {
    472     { SkPaint::kNone_FilterLevel,   "Mixed",    NULL    },
    473     { SkPaint::kNone_FilterLevel,   "None",     "F0 "   },
    474     { SkPaint::kLow_FilterLevel,    "Low",      "F1 "   },
    475     { SkPaint::kMedium_FilterLevel, "Medium",   "F2 "   },
    476     { SkPaint::kHigh_FilterLevel,   "High",     "F3 "   },
    477 };
    478 
    479 class FlagsDrawFilter : public SkDrawFilter {
    480 public:
    481     FlagsDrawFilter(SkOSMenu::TriState lcd, SkOSMenu::TriState aa,
    482                     SkOSMenu::TriState subpixel, int hinting, int filterlevel)
    483         : fLCDState(lcd)
    484         , fAAState(aa)
    485         , fSubpixelState(subpixel)
    486         , fHintingState(hinting)
    487         , fFilterLevelIndex(filterlevel)
    488     {
    489         SkASSERT((unsigned)filterlevel < SK_ARRAY_COUNT(gFilterLevelStates));
    490     }
    491 
    492     virtual bool filter(SkPaint* paint, Type t) {
    493         if (kText_Type == t && SkOSMenu::kMixedState != fLCDState) {
    494             paint->setLCDRenderText(SkOSMenu::kOnState == fLCDState);
    495         }
    496         if (SkOSMenu::kMixedState != fAAState) {
    497             paint->setAntiAlias(SkOSMenu::kOnState == fAAState);
    498         }
    499         if (0 != fFilterLevelIndex) {
    500             paint->setFilterLevel(gFilterLevelStates[fFilterLevelIndex].fLevel);
    501         }
    502         if (SkOSMenu::kMixedState != fSubpixelState) {
    503             paint->setSubpixelText(SkOSMenu::kOnState == fSubpixelState);
    504         }
    505         if (0 != fHintingState && fHintingState < (int)SK_ARRAY_COUNT(gHintingStates)) {
    506             paint->setHinting(gHintingStates[fHintingState].hinting);
    507         }
    508         return true;
    509     }
    510 
    511 private:
    512     SkOSMenu::TriState  fLCDState;
    513     SkOSMenu::TriState  fAAState;
    514     SkOSMenu::TriState  fSubpixelState;
    515     int fHintingState;
    516     int fFilterLevelIndex;
    517 };
    518 
    519 //////////////////////////////////////////////////////////////////////////////
    520 
    521 #define MAX_ZOOM_LEVEL  8
    522 #define MIN_ZOOM_LEVEL  -8
    523 
    524 static const char gCharEvtName[] = "SampleCode_Char_Event";
    525 static const char gKeyEvtName[] = "SampleCode_Key_Event";
    526 static const char gTitleEvtName[] = "SampleCode_Title_Event";
    527 static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
    528 static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
    529 static const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle";
    530 
    531 bool SampleCode::CharQ(const SkEvent& evt, SkUnichar* outUni) {
    532     if (evt.isType(gCharEvtName, sizeof(gCharEvtName) - 1)) {
    533         if (outUni) {
    534             *outUni = evt.getFast32();
    535         }
    536         return true;
    537     }
    538     return false;
    539 }
    540 
    541 bool SampleCode::KeyQ(const SkEvent& evt, SkKey* outKey) {
    542     if (evt.isType(gKeyEvtName, sizeof(gKeyEvtName) - 1)) {
    543         if (outKey) {
    544             *outKey = (SkKey)evt.getFast32();
    545         }
    546         return true;
    547     }
    548     return false;
    549 }
    550 
    551 bool SampleCode::TitleQ(const SkEvent& evt) {
    552     return evt.isType(gTitleEvtName, sizeof(gTitleEvtName) - 1);
    553 }
    554 
    555 void SampleCode::TitleR(SkEvent* evt, const char title[]) {
    556     SkASSERT(evt && TitleQ(*evt));
    557     evt->setString(gTitleEvtName, title);
    558 }
    559 
    560 bool SampleCode::RequestTitle(SkView* view, SkString* title) {
    561     SkEvent evt(gTitleEvtName);
    562     if (view->doQuery(&evt)) {
    563         title->set(evt.findString(gTitleEvtName));
    564         return true;
    565     }
    566     return false;
    567 }
    568 
    569 bool SampleCode::PrefSizeQ(const SkEvent& evt) {
    570     return evt.isType(gPrefSizeEvtName, sizeof(gPrefSizeEvtName) - 1);
    571 }
    572 
    573 void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
    574     SkASSERT(evt && PrefSizeQ(*evt));
    575     SkScalar size[2];
    576     size[0] = width;
    577     size[1] = height;
    578     evt->setScalars(gPrefSizeEvtName, 2, size);
    579 }
    580 
    581 bool SampleCode::FastTextQ(const SkEvent& evt) {
    582     return evt.isType(gFastTextEvtName, sizeof(gFastTextEvtName) - 1);
    583 }
    584 
    585 ///////////////////////////////////////////////////////////////////////////////
    586 
    587 static SkMSec gAnimTime;
    588 static SkMSec gAnimTimePrev;
    589 
    590 SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
    591 SkMSec SampleCode::GetAnimTimeDelta() { return gAnimTime - gAnimTimePrev; }
    592 SkScalar SampleCode::GetAnimSecondsDelta() {
    593     return SkDoubleToScalar(GetAnimTimeDelta() / 1000.0);
    594 }
    595 
    596 SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
    597     // since gAnimTime can be up to 32 bits, we can't convert it to a float
    598     // or we'll lose the low bits. Hence we use doubles for the intermediate
    599     // calculations
    600     double seconds = (double)gAnimTime / 1000.0;
    601     double value = SkScalarToDouble(speed) * seconds;
    602     if (period) {
    603         value = ::fmod(value, SkScalarToDouble(period));
    604     }
    605     return SkDoubleToScalar(value);
    606 }
    607 
    608 SkScalar SampleCode::GetAnimSinScalar(SkScalar amplitude,
    609                                       SkScalar periodInSec,
    610                                       SkScalar phaseInSec) {
    611     if (!periodInSec) {
    612         return 0;
    613     }
    614     double t = (double)gAnimTime / 1000.0 + phaseInSec;
    615     t *= SkScalarToFloat(2 * SK_ScalarPI) / periodInSec;
    616     amplitude = SK_ScalarHalf * amplitude;
    617     return SkScalarMul(amplitude, SkDoubleToScalar(sin(t))) + amplitude;
    618 }
    619 
    620 enum TilingMode {
    621     kNo_Tiling,
    622     kAbs_128x128_Tiling,
    623     kAbs_256x256_Tiling,
    624     kRel_4x4_Tiling,
    625     kRel_1x16_Tiling,
    626     kRel_16x1_Tiling,
    627 
    628     kLast_TilingMode_Enum
    629 };
    630 
    631 struct TilingInfo {
    632     const char* label;
    633     SkScalar    w, h;
    634 };
    635 
    636 static const struct TilingInfo gTilingInfo[] = {
    637     { "No tiling", SK_Scalar1        , SK_Scalar1         }, // kNo_Tiling
    638     { "128x128"  , SkIntToScalar(128), SkIntToScalar(128) }, // kAbs_128x128_Tiling
    639     { "256x256"  , SkIntToScalar(256), SkIntToScalar(256) }, // kAbs_256x256_Tiling
    640     { "1/4x1/4"  , SK_Scalar1 / 4    , SK_Scalar1 / 4     }, // kRel_4x4_Tiling
    641     { "1/1x1/16" , SK_Scalar1        , SK_Scalar1 / 16    }, // kRel_1x16_Tiling
    642     { "1/16x1/1" , SK_Scalar1 / 16   , SK_Scalar1         }, // kRel_16x1_Tiling
    643 };
    644 SK_COMPILE_ASSERT((SK_ARRAY_COUNT(gTilingInfo) == kLast_TilingMode_Enum),
    645                   Incomplete_tiling_labels);
    646 
    647 SkSize SampleWindow::tileSize() const {
    648     SkASSERT((TilingMode)fTilingMode < kLast_TilingMode_Enum);
    649     const struct TilingInfo* info = gTilingInfo + fTilingMode;
    650     return SkSize::Make(info->w > SK_Scalar1 ? info->w : this->width() * info->w,
    651                         info->h > SK_Scalar1 ? info->h : this->height() * info->h);
    652 }
    653 //////////////////////////////////////////////////////////////////////////////
    654 
    655 static SkView* curr_view(SkWindow* wind) {
    656     SkView::F2BIter iter(wind);
    657     return iter.next();
    658 }
    659 
    660 static bool curr_title(SkWindow* wind, SkString* title) {
    661     SkView* view = curr_view(wind);
    662     if (view) {
    663         SkEvent evt(gTitleEvtName);
    664         if (view->doQuery(&evt)) {
    665             title->set(evt.findString(gTitleEvtName));
    666             return true;
    667         }
    668     }
    669     return false;
    670 }
    671 
    672 void SampleWindow::setZoomCenter(float x, float y)
    673 {
    674     fZoomCenterX = x;
    675     fZoomCenterY = y;
    676 }
    677 
    678 bool SampleWindow::zoomIn()
    679 {
    680     // Arbitrarily decided
    681     if (fFatBitsScale == 25) return false;
    682     fFatBitsScale++;
    683     this->inval(NULL);
    684     return true;
    685 }
    686 
    687 bool SampleWindow::zoomOut()
    688 {
    689     if (fFatBitsScale == 1) return false;
    690     fFatBitsScale--;
    691     this->inval(NULL);
    692     return true;
    693 }
    694 
    695 void SampleWindow::updatePointer(int x, int y)
    696 {
    697     fMouseX = x;
    698     fMouseY = y;
    699     if (fShowZoomer) {
    700         this->inval(NULL);
    701     }
    702 }
    703 
    704 static inline SampleWindow::DeviceType cycle_devicetype(SampleWindow::DeviceType ct) {
    705     static const SampleWindow::DeviceType gCT[] = {
    706         SampleWindow::kPicture_DeviceType,
    707 #if SK_SUPPORT_GPU
    708         SampleWindow::kGPU_DeviceType,
    709 #if SK_ANGLE
    710         SampleWindow::kANGLE_DeviceType,
    711 #endif // SK_ANGLE
    712         SampleWindow::kRaster_DeviceType, // skip the null gpu device in normal cycling
    713 #endif // SK_SUPPORT_GPU
    714         SampleWindow::kRaster_DeviceType
    715     };
    716     SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gCT) == SampleWindow::kDeviceTypeCnt, array_size_mismatch);
    717     return gCT[ct];
    718 }
    719 
    720 static SkString getSampleTitle(const SkViewFactory* sampleFactory) {
    721     SkView* view = (*sampleFactory)();
    722     SkString title;
    723     SampleCode::RequestTitle(view, &title);
    724     view->unref();
    725     return title;
    726 }
    727 
    728 static bool compareSampleTitle(const SkViewFactory* first, const SkViewFactory* second) {
    729     return strcmp(getSampleTitle(first).c_str(), getSampleTitle(second).c_str()) < 0;
    730 }
    731 
    732 DEFINE_string(slide, "", "Start on this sample.");
    733 DEFINE_int32(msaa, 0, "Request multisampling with this count.");
    734 DEFINE_string(pictureDir, "", "Read pictures from here.");
    735 DEFINE_string(picture, "", "Path to single picture.");
    736 DEFINE_bool(sort, false, "Sort samples by title.");
    737 DEFINE_bool(list, false, "List samples?");
    738 #ifdef SAMPLE_PDF_FILE_VIEWER
    739 DEFINE_string(pdfPath, "", "Path to direcotry of pdf files.");
    740 #endif
    741 
    742 SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* devManager)
    743     : INHERITED(hwnd)
    744     , fDevManager(NULL) {
    745 
    746     SkCommandLineFlags::Parse(argc, argv);
    747 
    748     fCurrIndex = -1;
    749 
    750     if (!FLAGS_pictureDir.isEmpty()) {
    751         SkOSFile::Iter iter(FLAGS_pictureDir[0], "skp");
    752         SkString filename;
    753         while (iter.next(&filename)) {
    754             *fSamples.append() = new PictFileFactory(
    755                 SkOSPath::Join(FLAGS_pictureDir[0], filename.c_str()));
    756         }
    757     }
    758     if (!FLAGS_picture.isEmpty()) {
    759         SkString path(FLAGS_picture[0]);
    760         fCurrIndex = fSamples.count();
    761         *fSamples.append() = new PictFileFactory(path);
    762     }
    763 #ifdef SAMPLE_PDF_FILE_VIEWER
    764     if (!FLAGS_pdfPath.isEmpty()) {
    765         SkOSFile::Iter iter(FLAGS_pdfPath[0], "pdf");
    766         SkString filename;
    767         while (iter.next(&filename)) {
    768             *fSamples.append() = new PdfFileViewerFactory(
    769                 SkOSPath::Join(FLAGS_pictureDir[0], filename.c_str()));
    770         }
    771     }
    772 #endif
    773     SkGMRegistyToSampleRegistry();
    774     {
    775         const SkViewRegister* reg = SkViewRegister::Head();
    776         while (reg) {
    777             *fSamples.append() = reg->factory();
    778             reg = reg->next();
    779         }
    780     }
    781 
    782     if (FLAGS_sort) {
    783         // Sort samples, so foo.skp and foo.pdf are consecutive and we can quickly spot where
    784         // skp -> pdf -> png fails.
    785         SkTQSort(fSamples.begin(), fSamples.end() ? fSamples.end() - 1 : NULL, compareSampleTitle);
    786     }
    787 
    788     if (!FLAGS_slide.isEmpty()) {
    789         fCurrIndex = findByTitle(FLAGS_slide[0]);
    790         if (fCurrIndex < 0) {
    791             fprintf(stderr, "Unknown sample \"%s\"\n", FLAGS_slide[0]);
    792             listTitles();
    793         }
    794     }
    795 
    796     fMSAASampleCount = FLAGS_msaa;
    797 
    798     if (FLAGS_list) {
    799         listTitles();
    800     }
    801 
    802     if (fCurrIndex < 0) {
    803         SkString title;
    804         if (readTitleFromPrefs(&title)) {
    805             fCurrIndex = findByTitle(title.c_str());
    806         }
    807     }
    808 
    809     if (fCurrIndex < 0) {
    810         fCurrIndex = 0;
    811     }
    812 
    813     gSampleWindow = this;
    814 
    815 #ifdef  PIPE_FILE
    816     //Clear existing file or create file if it doesn't exist
    817     FILE* f = fopen(FILE_PATH, "wb");
    818     fclose(f);
    819 #endif
    820 
    821     fDeviceType = kRaster_DeviceType;
    822 
    823 #if DEFAULT_TO_GPU
    824     fDeviceType = kGPU_DeviceType;
    825 #endif
    826 #if SK_ANGLE && DEFAULT_TO_ANGLE
    827     fDeviceType = kANGLE_DeviceType;
    828 #endif
    829 
    830     fUseClip = false;
    831     fNClip = false;
    832     fAnimating = false;
    833     fRotate = false;
    834     fRotateAnimTime = 0;
    835     fPerspAnim = false;
    836     fPerspAnimTime = 0;
    837     fRequestGrabImage = false;
    838     fPipeState = SkOSMenu::kOffState;
    839     fTilingMode = kNo_Tiling;
    840     fMeasureFPS = false;
    841     fLCDState = SkOSMenu::kMixedState;
    842     fAAState = SkOSMenu::kMixedState;
    843     fSubpixelState = SkOSMenu::kMixedState;
    844     fHintingState = 0;
    845     fFilterLevelIndex = 0;
    846     fFlipAxis = 0;
    847     fScrollTestX = fScrollTestY = 0;
    848 
    849     fMouseX = fMouseY = 0;
    850     fFatBitsScale = 8;
    851     fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
    852     fShowZoomer = false;
    853 
    854     fZoomLevel = 0;
    855     fZoomScale = SK_Scalar1;
    856 
    857     fMagnify = false;
    858 
    859     fSaveToPdf = false;
    860     fPdfCanvas = NULL;
    861 
    862     fTransitionNext = 6;
    863     fTransitionPrev = 2;
    864 
    865     int sinkID = this->getSinkID();
    866     fAppMenu = new SkOSMenu;
    867     fAppMenu->setTitle("Global Settings");
    868     int itemID;
    869 
    870     itemID =fAppMenu->appendList("Device Type", "Device Type", sinkID, 0,
    871                                 "Raster", "Picture", "OpenGL",
    872 #if SK_ANGLE
    873                                 "ANGLE",
    874 #endif
    875                                 NULL);
    876     fAppMenu->assignKeyEquivalentToItem(itemID, 'd');
    877     itemID = fAppMenu->appendTriState("AA", "AA", sinkID, fAAState);
    878     fAppMenu->assignKeyEquivalentToItem(itemID, 'b');
    879     itemID = fAppMenu->appendTriState("LCD", "LCD", sinkID, fLCDState);
    880     fAppMenu->assignKeyEquivalentToItem(itemID, 'l');
    881     itemID = fAppMenu->appendList("FilterLevel", "FilterLevel", sinkID, fFilterLevelIndex,
    882                                   gFilterLevelStates[0].fName,
    883                                   gFilterLevelStates[1].fName,
    884                                   gFilterLevelStates[2].fName,
    885                                   gFilterLevelStates[3].fName,
    886                                   gFilterLevelStates[4].fName,
    887                                   NULL);
    888     fAppMenu->assignKeyEquivalentToItem(itemID, 'n');
    889     itemID = fAppMenu->appendTriState("Subpixel", "Subpixel", sinkID, fSubpixelState);
    890     fAppMenu->assignKeyEquivalentToItem(itemID, 's');
    891     itemID = fAppMenu->appendList("Hinting", "Hinting", sinkID, fHintingState,
    892                                   gHintingStates[0].name,
    893                                   gHintingStates[1].name,
    894                                   gHintingStates[2].name,
    895                                   gHintingStates[3].name,
    896                                   gHintingStates[4].name,
    897                                   NULL);
    898     fAppMenu->assignKeyEquivalentToItem(itemID, 'h');
    899 
    900     fUsePipeMenuItemID = fAppMenu->appendTriState("Pipe", "Pipe" , sinkID,
    901                                                   fPipeState);
    902     fAppMenu->assignKeyEquivalentToItem(fUsePipeMenuItemID, 'P');
    903 
    904     itemID =fAppMenu->appendList("Tiling", "Tiling", sinkID, fTilingMode,
    905                                  gTilingInfo[kNo_Tiling].label,
    906                                  gTilingInfo[kAbs_128x128_Tiling].label,
    907                                  gTilingInfo[kAbs_256x256_Tiling].label,
    908                                  gTilingInfo[kRel_4x4_Tiling].label,
    909                                  gTilingInfo[kRel_1x16_Tiling].label,
    910                                  gTilingInfo[kRel_16x1_Tiling].label,
    911                                  NULL);
    912     fAppMenu->assignKeyEquivalentToItem(itemID, 't');
    913 
    914     itemID = fAppMenu->appendSwitch("Slide Show", "Slide Show" , sinkID, false);
    915     fAppMenu->assignKeyEquivalentToItem(itemID, 'a');
    916     itemID = fAppMenu->appendSwitch("Clip", "Clip" , sinkID, fUseClip);
    917     fAppMenu->assignKeyEquivalentToItem(itemID, 'c');
    918     itemID = fAppMenu->appendSwitch("Flip X", "Flip X" , sinkID, false);
    919     fAppMenu->assignKeyEquivalentToItem(itemID, 'x');
    920     itemID = fAppMenu->appendSwitch("Flip Y", "Flip Y" , sinkID, false);
    921     fAppMenu->assignKeyEquivalentToItem(itemID, 'y');
    922     itemID = fAppMenu->appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
    923     fAppMenu->assignKeyEquivalentToItem(itemID, 'z');
    924     itemID = fAppMenu->appendSwitch("Magnify", "Magnify" , sinkID, fMagnify);
    925     fAppMenu->assignKeyEquivalentToItem(itemID, 'm');
    926     itemID =fAppMenu->appendList("Transition-Next", "Transition-Next", sinkID,
    927                                 fTransitionNext, "Up", "Up and Right", "Right",
    928                                 "Down and Right", "Down", "Down and Left",
    929                                 "Left", "Up and Left", NULL);
    930     fAppMenu->assignKeyEquivalentToItem(itemID, 'j');
    931     itemID =fAppMenu->appendList("Transition-Prev", "Transition-Prev", sinkID,
    932                                 fTransitionPrev, "Up", "Up and Right", "Right",
    933                                 "Down and Right", "Down", "Down and Left",
    934                                 "Left", "Up and Left", NULL);
    935     fAppMenu->assignKeyEquivalentToItem(itemID, 'k');
    936     itemID = fAppMenu->appendAction("Save to PDF", sinkID);
    937     fAppMenu->assignKeyEquivalentToItem(itemID, 'e');
    938 
    939     this->addMenu(fAppMenu);
    940     fSlideMenu = new SkOSMenu;
    941     this->addMenu(fSlideMenu);
    942 
    943     this->setColorType(kN32_SkColorType);
    944     this->setVisibleP(true);
    945     this->setClipToBounds(false);
    946 
    947     this->loadView((*fSamples[fCurrIndex])());
    948 
    949     fPDFData = NULL;
    950 
    951     if (NULL == devManager) {
    952         fDevManager = new DefaultDeviceManager();
    953     } else {
    954         devManager->ref();
    955         fDevManager = devManager;
    956     }
    957     fDevManager->setUpBackend(this, fMSAASampleCount);
    958 
    959     // If another constructor set our dimensions, ensure that our
    960     // onSizeChange gets called.
    961     if (this->height() && this->width()) {
    962         this->onSizeChange();
    963     }
    964 
    965     // can't call this synchronously, since it may require a subclass to
    966     // to implement, or the caller may need us to have returned from the
    967     // constructor first. Hence we post an event to ourselves.
    968 //    this->updateTitle();
    969     post_event_to_sink(new SkEvent(gUpdateWindowTitleEvtName), this);
    970 }
    971 
    972 SampleWindow::~SampleWindow() {
    973     delete fPdfCanvas;
    974     SkSafeUnref(fTypeface);
    975     SkSafeUnref(fDevManager);
    976 }
    977 
    978 
    979 int SampleWindow::findByTitle(const char title[]) {
    980     int i, count = fSamples.count();
    981     for (i = 0; i < count; i++) {
    982         if (getSampleTitle(i).equals(title)) {
    983             return i;
    984         }
    985     }
    986     return -1;
    987 }
    988 
    989 void SampleWindow::listTitles() {
    990     int count = fSamples.count();
    991     SkDebugf("All Slides:\n");
    992     for (int i = 0; i < count; i++) {
    993         SkDebugf("    %s\n", getSampleTitle(i).c_str());
    994     }
    995 }
    996 
    997 static SkBitmap capture_bitmap(SkCanvas* canvas) {
    998     SkBitmap bm;
    999     if (bm.tryAllocPixels(canvas->imageInfo())) {
   1000         canvas->readPixels(&bm, 0, 0);
   1001     }
   1002     return bm;
   1003 }
   1004 
   1005 static bool bitmap_diff(SkCanvas* canvas, const SkBitmap& orig,
   1006                         SkBitmap* diff) {
   1007     SkBitmap src = capture_bitmap(canvas);
   1008 
   1009     SkAutoLockPixels alp0(src);
   1010     SkAutoLockPixels alp1(orig);
   1011     for (int y = 0; y < src.height(); y++) {
   1012         const void* srcP = src.getAddr(0, y);
   1013         const void* origP = orig.getAddr(0, y);
   1014         size_t bytes = src.width() * src.bytesPerPixel();
   1015         if (memcmp(srcP, origP, bytes)) {
   1016             SkDebugf("---------- difference on line %d\n", y);
   1017             return true;
   1018         }
   1019     }
   1020     return false;
   1021 }
   1022 
   1023 static void drawText(SkCanvas* canvas, SkString string, SkScalar left, SkScalar top, SkPaint& paint)
   1024 {
   1025     SkColor desiredColor = paint.getColor();
   1026     paint.setColor(SK_ColorWHITE);
   1027     const char* c_str = string.c_str();
   1028     size_t size = string.size();
   1029     SkRect bounds;
   1030     paint.measureText(c_str, size, &bounds);
   1031     bounds.offset(left, top);
   1032     SkScalar inset = SkIntToScalar(-2);
   1033     bounds.inset(inset, inset);
   1034     canvas->drawRect(bounds, paint);
   1035     if (desiredColor != SK_ColorBLACK) {
   1036         paint.setColor(SK_ColorBLACK);
   1037         canvas->drawText(c_str, size, left + SK_Scalar1, top + SK_Scalar1, paint);
   1038     }
   1039     paint.setColor(desiredColor);
   1040     canvas->drawText(c_str, size, left, top, paint);
   1041 }
   1042 
   1043 #define XCLIP_N  8
   1044 #define YCLIP_N  8
   1045 
   1046 void SampleWindow::draw(SkCanvas* canvas) {
   1047     // update the animation time
   1048     if (!gAnimTimePrev && !gAnimTime) {
   1049         // first time make delta be 0
   1050         gAnimTime = SkTime::GetMSecs();
   1051         gAnimTimePrev = gAnimTime;
   1052     } else {
   1053         gAnimTimePrev = gAnimTime;
   1054         gAnimTime = SkTime::GetMSecs();
   1055     }
   1056 
   1057     if (fGesture.isActive()) {
   1058         this->updateMatrix();
   1059     }
   1060 
   1061     if (fMeasureFPS) {
   1062         fMeasureFPS_Time = 0;
   1063     }
   1064 
   1065     if (fNClip) {
   1066         this->INHERITED::draw(canvas);
   1067         SkBitmap orig = capture_bitmap(canvas);
   1068 
   1069         const SkScalar w = this->width();
   1070         const SkScalar h = this->height();
   1071         const SkScalar cw = w / XCLIP_N;
   1072         const SkScalar ch = h / YCLIP_N;
   1073         for (int y = 0; y < YCLIP_N; y++) {
   1074             SkRect r;
   1075             r.fTop = y * ch;
   1076             r.fBottom = (y + 1) * ch;
   1077             if (y == YCLIP_N - 1) {
   1078                 r.fBottom = h;
   1079             }
   1080             for (int x = 0; x < XCLIP_N; x++) {
   1081                 SkAutoCanvasRestore acr(canvas, true);
   1082                 r.fLeft = x * cw;
   1083                 r.fRight = (x + 1) * cw;
   1084                 if (x == XCLIP_N - 1) {
   1085                     r.fRight = w;
   1086                 }
   1087                 canvas->clipRect(r);
   1088                 this->INHERITED::draw(canvas);
   1089             }
   1090         }
   1091 
   1092         SkBitmap diff;
   1093         if (bitmap_diff(canvas, orig, &diff)) {
   1094         }
   1095     } else {
   1096         SkSize tile = this->tileSize();
   1097 
   1098         if (kNo_Tiling == fTilingMode) {
   1099             this->INHERITED::draw(canvas); // no looping or surfaces needed
   1100         } else {
   1101             const int w = SkScalarRoundToInt(tile.width());
   1102             const int h = SkScalarRoundToInt(tile.height());
   1103             SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
   1104             SkAutoTUnref<SkSurface> surface(canvas->newSurface(info));
   1105             SkCanvas* tileCanvas = surface->getCanvas();
   1106 
   1107             for (SkScalar y = 0; y < height(); y += tile.height()) {
   1108                 for (SkScalar x = 0; x < width(); x += tile.width()) {
   1109                     SkAutoCanvasRestore acr(tileCanvas, true);
   1110                     tileCanvas->translate(-x, -y);
   1111                     tileCanvas->clear(0);
   1112                     this->INHERITED::draw(tileCanvas);
   1113                     surface->draw(canvas, x, y, NULL);
   1114                 }
   1115             }
   1116 
   1117             // for drawing the borders between tiles
   1118             SkPaint paint;
   1119             paint.setColor(0x60FF00FF);
   1120             paint.setStyle(SkPaint::kStroke_Style);
   1121 
   1122             for (SkScalar y = 0; y < height(); y += tile.height()) {
   1123                 for (SkScalar x = 0; x < width(); x += tile.width()) {
   1124                     canvas->drawRect(SkRect::MakeXYWH(x, y, tile.width(), tile.height()), paint);
   1125                 }
   1126             }
   1127         }
   1128     }
   1129     if (fShowZoomer && !fSaveToPdf) {
   1130         showZoomer(canvas);
   1131     }
   1132     if (fMagnify && !fSaveToPdf) {
   1133         magnify(canvas);
   1134     }
   1135 
   1136     if (fMeasureFPS && fMeasureFPS_Time) {
   1137         this->updateTitle();
   1138         this->postInvalDelay();
   1139     }
   1140 
   1141     // do this last
   1142     fDevManager->publishCanvas(fDeviceType, canvas, this);
   1143 }
   1144 
   1145 static float clipW = 200;
   1146 static float clipH = 200;
   1147 void SampleWindow::magnify(SkCanvas* canvas) {
   1148     SkRect r;
   1149     int count = canvas->save();
   1150 
   1151     SkMatrix m = canvas->getTotalMatrix();
   1152     if (!m.invert(&m)) {
   1153         return;
   1154     }
   1155     SkPoint offset, center;
   1156     SkScalar mouseX = fMouseX * SK_Scalar1;
   1157     SkScalar mouseY = fMouseY * SK_Scalar1;
   1158     m.mapXY(mouseX - clipW/2, mouseY - clipH/2, &offset);
   1159     m.mapXY(mouseX, mouseY, &center);
   1160 
   1161     r.set(0, 0, clipW * m.getScaleX(), clipH * m.getScaleX());
   1162     r.offset(offset.fX, offset.fY);
   1163 
   1164     SkPaint paint;
   1165     paint.setColor(0xFF66AAEE);
   1166     paint.setStyle(SkPaint::kStroke_Style);
   1167     paint.setStrokeWidth(10.f * m.getScaleX());
   1168     //lense offset
   1169     //canvas->translate(0, -250);
   1170     canvas->drawRect(r, paint);
   1171     canvas->clipRect(r);
   1172 
   1173     m = canvas->getTotalMatrix();
   1174     m.setTranslate(-center.fX, -center.fY);
   1175     m.postScale(0.5f * fFatBitsScale, 0.5f * fFatBitsScale);
   1176     m.postTranslate(center.fX, center.fY);
   1177     canvas->concat(m);
   1178 
   1179     this->INHERITED::draw(canvas);
   1180 
   1181     canvas->restoreToCount(count);
   1182 }
   1183 
   1184 void SampleWindow::showZoomer(SkCanvas* canvas) {
   1185         int count = canvas->save();
   1186         canvas->resetMatrix();
   1187         // Ensure the mouse position is on screen.
   1188         int width = SkScalarRoundToInt(this->width());
   1189         int height = SkScalarRoundToInt(this->height());
   1190         if (fMouseX >= width) fMouseX = width - 1;
   1191         else if (fMouseX < 0) fMouseX = 0;
   1192         if (fMouseY >= height) fMouseY = height - 1;
   1193         else if (fMouseY < 0) fMouseY = 0;
   1194 
   1195         SkBitmap bitmap = capture_bitmap(canvas);
   1196         bitmap.lockPixels();
   1197 
   1198         // Find the size of the zoomed in view, forced to be odd, so the examined pixel is in the middle.
   1199         int zoomedWidth = (width >> 1) | 1;
   1200         int zoomedHeight = (height >> 1) | 1;
   1201         SkIRect src;
   1202         src.set(0, 0, zoomedWidth / fFatBitsScale, zoomedHeight / fFatBitsScale);
   1203         src.offset(fMouseX - (src.width()>>1), fMouseY - (src.height()>>1));
   1204         SkRect dest;
   1205         dest.set(0, 0, SkIntToScalar(zoomedWidth), SkIntToScalar(zoomedHeight));
   1206         dest.offset(SkIntToScalar(width - zoomedWidth), SkIntToScalar(height - zoomedHeight));
   1207         SkPaint paint;
   1208         // Clear the background behind our zoomed in view
   1209         paint.setColor(SK_ColorWHITE);
   1210         canvas->drawRect(dest, paint);
   1211         canvas->drawBitmapRect(bitmap, &src, dest);
   1212         paint.setColor(SK_ColorBLACK);
   1213         paint.setStyle(SkPaint::kStroke_Style);
   1214         // Draw a border around the pixel in the middle
   1215         SkRect originalPixel;
   1216         originalPixel.set(SkIntToScalar(fMouseX), SkIntToScalar(fMouseY), SkIntToScalar(fMouseX + 1), SkIntToScalar(fMouseY + 1));
   1217         SkMatrix matrix;
   1218         SkRect scalarSrc;
   1219         scalarSrc.set(src);
   1220         SkColor color = bitmap.getColor(fMouseX, fMouseY);
   1221         if (matrix.setRectToRect(scalarSrc, dest, SkMatrix::kFill_ScaleToFit)) {
   1222             SkRect pixel;
   1223             matrix.mapRect(&pixel, originalPixel);
   1224             // TODO Perhaps measure the values and make the outline white if it's "dark"
   1225             if (color == SK_ColorBLACK) {
   1226                 paint.setColor(SK_ColorWHITE);
   1227             }
   1228             canvas->drawRect(pixel, paint);
   1229         }
   1230         paint.setColor(SK_ColorBLACK);
   1231         // Draw a border around the destination rectangle
   1232         canvas->drawRect(dest, paint);
   1233         paint.setStyle(SkPaint::kStrokeAndFill_Style);
   1234         // Identify the pixel and its color on screen
   1235         paint.setTypeface(fTypeface);
   1236         paint.setAntiAlias(true);
   1237         SkScalar lineHeight = paint.getFontMetrics(NULL);
   1238         SkString string;
   1239         string.appendf("(%i, %i)", fMouseX, fMouseY);
   1240         SkScalar left = dest.fLeft + SkIntToScalar(3);
   1241         SkScalar i = SK_Scalar1;
   1242         drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
   1243         // Alpha
   1244         i += SK_Scalar1;
   1245         string.reset();
   1246         string.appendf("A: %X", SkColorGetA(color));
   1247         drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
   1248         // Red
   1249         i += SK_Scalar1;
   1250         string.reset();
   1251         string.appendf("R: %X", SkColorGetR(color));
   1252         paint.setColor(SK_ColorRED);
   1253         drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
   1254         // Green
   1255         i += SK_Scalar1;
   1256         string.reset();
   1257         string.appendf("G: %X", SkColorGetG(color));
   1258         paint.setColor(SK_ColorGREEN);
   1259         drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
   1260         // Blue
   1261         i += SK_Scalar1;
   1262         string.reset();
   1263         string.appendf("B: %X", SkColorGetB(color));
   1264         paint.setColor(SK_ColorBLUE);
   1265         drawText(canvas, string, left, SkScalarMulAdd(lineHeight, i, dest.fTop), paint);
   1266         canvas->restoreToCount(count);
   1267 }
   1268 
   1269 void SampleWindow::onDraw(SkCanvas* canvas) {
   1270 }
   1271 
   1272 #include "SkColorPriv.h"
   1273 
   1274 void SampleWindow::saveToPdf()
   1275 {
   1276     fSaveToPdf = true;
   1277     this->inval(NULL);
   1278 }
   1279 
   1280 SkCanvas* SampleWindow::beforeChildren(SkCanvas* canvas) {
   1281     if (fSaveToPdf) {
   1282         const SkBitmap bmp = capture_bitmap(canvas);
   1283         SkISize size = SkISize::Make(bmp.width(), bmp.height());
   1284         SkPDFDevice* pdfDevice = new SkPDFDevice(size, size,
   1285                 canvas->getTotalMatrix());
   1286         fPdfCanvas = new SkCanvas(pdfDevice);
   1287         pdfDevice->unref();
   1288         canvas = fPdfCanvas;
   1289     } else if (kPicture_DeviceType == fDeviceType) {
   1290         canvas = fRecorder.beginRecording(9999, 9999, NULL, 0);
   1291     } else {
   1292 #if SK_SUPPORT_GPU
   1293         if (kNullGPU_DeviceType != fDeviceType)
   1294 #endif
   1295         {
   1296             canvas = this->INHERITED::beforeChildren(canvas);
   1297         }
   1298     }
   1299 
   1300     if (fUseClip) {
   1301         canvas->drawColor(0xFFFF88FF);
   1302         canvas->clipPath(fClipPath, SkRegion::kIntersect_Op, true);
   1303     }
   1304 
   1305     return canvas;
   1306 }
   1307 
   1308 #include "SkData.h"
   1309 void SampleWindow::afterChildren(SkCanvas* orig) {
   1310     if (fSaveToPdf) {
   1311         fSaveToPdf = false;
   1312         if (fShowZoomer) {
   1313             showZoomer(fPdfCanvas);
   1314         }
   1315         SkString name;
   1316         name.printf("%s.pdf", this->getTitle());
   1317         SkPDFDocument doc;
   1318         SkPDFDevice* device = NULL;//static_cast<SkPDFDevice*>(fPdfCanvas->getDevice());
   1319         SkASSERT(false);
   1320         doc.appendPage(device);
   1321 #ifdef SK_BUILD_FOR_ANDROID
   1322         name.prepend("/sdcard/");
   1323 #endif
   1324 
   1325 #ifdef SK_BUILD_FOR_IOS
   1326         SkDynamicMemoryWStream mstream;
   1327         doc.emitPDF(&mstream);
   1328         fPDFData = mstream.copyToData();
   1329 #endif
   1330         SkFILEWStream stream(name.c_str());
   1331         if (stream.isValid()) {
   1332             doc.emitPDF(&stream);
   1333             const char* desc = "File saved from Skia SampleApp";
   1334             this->onPDFSaved(this->getTitle(), desc, name.c_str());
   1335         }
   1336 
   1337         delete fPdfCanvas;
   1338         fPdfCanvas = NULL;
   1339 
   1340         // We took over the draw calls in order to create the PDF, so we need
   1341         // to redraw.
   1342         this->inval(NULL);
   1343         return;
   1344     }
   1345 
   1346     if (fRequestGrabImage) {
   1347         fRequestGrabImage = false;
   1348 
   1349         SkBitmap bmp = capture_bitmap(orig);
   1350         if (!bmp.isNull()) {
   1351             static int gSampleGrabCounter;
   1352             SkString name;
   1353             name.printf("sample_grab_%d.png", gSampleGrabCounter++);
   1354             SkImageEncoder::EncodeFile(name.c_str(), bmp,
   1355                                        SkImageEncoder::kPNG_Type, 100);
   1356         }
   1357     }
   1358 
   1359     if (kPicture_DeviceType == fDeviceType) {
   1360         SkAutoTUnref<const SkPicture> picture(fRecorder.endRecording());
   1361 
   1362         if (true) {
   1363             this->installDrawFilter(orig);
   1364             orig->drawPicture(picture);
   1365         } else if (true) {
   1366             SkDynamicMemoryWStream ostream;
   1367             picture->serialize(&ostream);
   1368 
   1369             SkAutoDataUnref data(ostream.copyToData());
   1370             SkMemoryStream istream(data->data(), data->size());
   1371             SkAutoTUnref<SkPicture> pict(SkPicture::CreateFromStream(&istream));
   1372             if (pict.get() != NULL) {
   1373                 orig->drawPicture(pict.get());
   1374             }
   1375         } else {
   1376             picture->playback(orig);
   1377         }
   1378     }
   1379 
   1380     // Do this after presentGL and other finishing, rather than in afterChild
   1381     if (fMeasureFPS && fMeasureFPS_StartTime) {
   1382         fMeasureFPS_Time += SkTime::GetMSecs() - fMeasureFPS_StartTime;
   1383     }
   1384 }
   1385 
   1386 void SampleWindow::beforeChild(SkView* child, SkCanvas* canvas) {
   1387     if (fRotate) {
   1388         fRotateAnimTime += SampleCode::GetAnimSecondsDelta();
   1389 
   1390         SkScalar cx = this->width() / 2;
   1391         SkScalar cy = this->height() / 2;
   1392         canvas->translate(cx, cy);
   1393         canvas->rotate(fRotateAnimTime * 10);
   1394         canvas->translate(-cx, -cy);
   1395     }
   1396 
   1397     if (fPerspAnim) {
   1398         fPerspAnimTime += SampleCode::GetAnimSecondsDelta();
   1399 
   1400         static const SkScalar gAnimPeriod = 10 * SK_Scalar1;
   1401         static const SkScalar gAnimMag = SK_Scalar1 / 1000;
   1402         SkScalar t = SkScalarMod(fPerspAnimTime, gAnimPeriod);
   1403         if (SkScalarFloorToInt(SkScalarDiv(fPerspAnimTime, gAnimPeriod)) & 0x1) {
   1404             t = gAnimPeriod - t;
   1405         }
   1406         t = 2 * t - gAnimPeriod;
   1407         t = SkScalarMul(SkScalarDiv(t, gAnimPeriod), gAnimMag);
   1408         SkMatrix m;
   1409         m.reset();
   1410 #if 1
   1411         m.setPerspY(t);
   1412 #else
   1413         m.setPerspY(SK_Scalar1 / 1000);
   1414         m.setSkewX(SkScalarDiv(8, 25));
   1415         m.dump();
   1416 #endif
   1417         canvas->concat(m);
   1418     }
   1419 
   1420     this->installDrawFilter(canvas);
   1421 
   1422     if (fMeasureFPS) {
   1423         if (SampleView::SetRepeatDraw(child, FPS_REPEAT_COUNT)) {
   1424             fMeasureFPS_StartTime = SkTime::GetMSecs();
   1425         }
   1426     } else {
   1427         (void)SampleView::SetRepeatDraw(child, 1);
   1428     }
   1429     if (fPerspAnim || fRotate) {
   1430         this->inval(NULL);
   1431     }
   1432 }
   1433 
   1434 void SampleWindow::afterChild(SkView* child, SkCanvas* canvas) {
   1435     canvas->setDrawFilter(NULL);
   1436 }
   1437 
   1438 static SkColorType gColorTypeCycle[] = {
   1439     kUnknown_SkColorType,           // none -> none
   1440     kUnknown_SkColorType,           // a8 -> none
   1441     kN32_SkColorType,               // 565 -> 8888
   1442     kN32_SkColorType,               // 4444 -> 8888
   1443     kRGB_565_SkColorType,           // 8888 -> 565
   1444     kRGB_565_SkColorType,           // 8888 -> 565
   1445     kUnknown_SkColorType,           // index8 -> none
   1446 };
   1447 
   1448 static SkColorType cycle_colortypes(SkColorType c) {
   1449     return gColorTypeCycle[c];
   1450 }
   1451 
   1452 void SampleWindow::changeZoomLevel(float delta) {
   1453     fZoomLevel += delta;
   1454     if (fZoomLevel > 0) {
   1455         fZoomLevel = SkMinScalar(fZoomLevel, MAX_ZOOM_LEVEL);
   1456         fZoomScale = fZoomLevel + SK_Scalar1;
   1457     } else if (fZoomLevel < 0) {
   1458         fZoomLevel = SkMaxScalar(fZoomLevel, MIN_ZOOM_LEVEL);
   1459         fZoomScale = SK_Scalar1 / (SK_Scalar1 - fZoomLevel);
   1460     } else {
   1461         fZoomScale = SK_Scalar1;
   1462     }
   1463     this->updateMatrix();
   1464 }
   1465 
   1466 void SampleWindow::updateMatrix(){
   1467     SkMatrix m;
   1468     m.reset();
   1469     if (fZoomLevel) {
   1470         SkPoint center;
   1471         //m = this->getLocalMatrix();//.invert(&m);
   1472         m.mapXY(fZoomCenterX, fZoomCenterY, &center);
   1473         SkScalar cx = center.fX;
   1474         SkScalar cy = center.fY;
   1475 
   1476         m.setTranslate(-cx, -cy);
   1477         m.postScale(fZoomScale, fZoomScale);
   1478         m.postTranslate(cx, cy);
   1479     }
   1480 
   1481     if (fFlipAxis) {
   1482         m.preTranslate(fZoomCenterX, fZoomCenterY);
   1483         if (fFlipAxis & kFlipAxis_X) {
   1484             m.preScale(-SK_Scalar1, SK_Scalar1);
   1485         }
   1486         if (fFlipAxis & kFlipAxis_Y) {
   1487             m.preScale(SK_Scalar1, -SK_Scalar1);
   1488         }
   1489         m.preTranslate(-fZoomCenterX, -fZoomCenterY);
   1490         //canvas->concat(m);
   1491     }
   1492     // Apply any gesture matrix
   1493     m.preConcat(fGesture.localM());
   1494     m.preConcat(fGesture.globalM());
   1495 
   1496     this->setLocalMatrix(m);
   1497 
   1498     this->updateTitle();
   1499     this->inval(NULL);
   1500 }
   1501 bool SampleWindow::previousSample() {
   1502     fCurrIndex = (fCurrIndex - 1 + fSamples.count()) % fSamples.count();
   1503     this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
   1504                                      fTransitionPrev));
   1505     return true;
   1506 }
   1507 
   1508 bool SampleWindow::nextSample() {
   1509     fCurrIndex = (fCurrIndex + 1) % fSamples.count();
   1510     this->loadView(create_transition(curr_view(this), (*fSamples[fCurrIndex])(),
   1511                                      fTransitionNext));
   1512     return true;
   1513 }
   1514 
   1515 bool SampleWindow::goToSample(int i) {
   1516     fCurrIndex = (i) % fSamples.count();
   1517     this->loadView(create_transition(curr_view(this),(*fSamples[fCurrIndex])(), 6));
   1518     return true;
   1519 }
   1520 
   1521 SkString SampleWindow::getSampleTitle(int i) {
   1522     return ::getSampleTitle(fSamples[i]);
   1523 }
   1524 
   1525 int SampleWindow::sampleCount() {
   1526     return fSamples.count();
   1527 }
   1528 
   1529 void SampleWindow::showOverview() {
   1530     this->loadView(create_transition(curr_view(this),
   1531                                      create_overview(fSamples.count(), fSamples.begin()),
   1532                                      4));
   1533 }
   1534 
   1535 void SampleWindow::installDrawFilter(SkCanvas* canvas) {
   1536     canvas->setDrawFilter(new FlagsDrawFilter(fLCDState, fAAState, fSubpixelState,
   1537                                               fHintingState, fFilterLevelIndex))->unref();
   1538 }
   1539 
   1540 void SampleWindow::postAnimatingEvent() {
   1541     if (fAnimating) {
   1542         (new SkEvent(ANIMATING_EVENTTYPE, this->getSinkID()))->postDelay(ANIMATING_DELAY);
   1543     }
   1544 }
   1545 
   1546 bool SampleWindow::onEvent(const SkEvent& evt) {
   1547     if (evt.isType(gUpdateWindowTitleEvtName)) {
   1548         this->updateTitle();
   1549         return true;
   1550     }
   1551     if (evt.isType(ANIMATING_EVENTTYPE)) {
   1552         if (fAnimating) {
   1553             this->nextSample();
   1554             this->postAnimatingEvent();
   1555         }
   1556         return true;
   1557     }
   1558     if (evt.isType("replace-transition-view")) {
   1559         this->loadView((SkView*)SkEventSink::FindSink(evt.getFast32()));
   1560         return true;
   1561     }
   1562     if (evt.isType("set-curr-index")) {
   1563         this->goToSample(evt.getFast32());
   1564         return true;
   1565     }
   1566     if (isInvalEvent(evt)) {
   1567         this->inval(NULL);
   1568         return true;
   1569     }
   1570     int selected = -1;
   1571     if (SkOSMenu::FindListIndex(evt, "Device Type", &selected)) {
   1572         this->setDeviceType((DeviceType)selected);
   1573         return true;
   1574     }
   1575     if (SkOSMenu::FindTriState(evt, "Pipe", &fPipeState)) {
   1576 #ifdef PIPE_NET
   1577         if (!fPipeState != SkOSMenu::kOnState)
   1578             gServer.disconnectAll();
   1579 #endif
   1580         (void)SampleView::SetUsePipe(curr_view(this), fPipeState);
   1581         this->updateTitle();
   1582         this->inval(NULL);
   1583         return true;
   1584     }
   1585     if (SkOSMenu::FindSwitchState(evt, "Slide Show", NULL)) {
   1586         this->toggleSlideshow();
   1587         return true;
   1588     }
   1589     if (SkOSMenu::FindTriState(evt, "AA", &fAAState) ||
   1590         SkOSMenu::FindTriState(evt, "LCD", &fLCDState) ||
   1591         SkOSMenu::FindListIndex(evt, "FilterLevel", &fFilterLevelIndex) ||
   1592         SkOSMenu::FindTriState(evt, "Subpixel", &fSubpixelState) ||
   1593         SkOSMenu::FindListIndex(evt, "Hinting", &fHintingState) ||
   1594         SkOSMenu::FindSwitchState(evt, "Clip", &fUseClip) ||
   1595         SkOSMenu::FindSwitchState(evt, "Zoomer", &fShowZoomer) ||
   1596         SkOSMenu::FindSwitchState(evt, "Magnify", &fMagnify) ||
   1597         SkOSMenu::FindListIndex(evt, "Transition-Next", &fTransitionNext) ||
   1598         SkOSMenu::FindListIndex(evt, "Transition-Prev", &fTransitionPrev)) {
   1599         this->inval(NULL);
   1600         this->updateTitle();
   1601         return true;
   1602     }
   1603     if (SkOSMenu::FindListIndex(evt, "Tiling", &fTilingMode)) {
   1604         if (SampleView::IsSampleView(curr_view(this))) {
   1605             ((SampleView*)curr_view(this))->onTileSizeChanged(this->tileSize());
   1606         }
   1607         this->inval(NULL);
   1608         this->updateTitle();
   1609         return true;
   1610     }
   1611     if (SkOSMenu::FindSwitchState(evt, "Flip X", NULL)) {
   1612         fFlipAxis ^= kFlipAxis_X;
   1613         this->updateMatrix();
   1614         return true;
   1615     }
   1616     if (SkOSMenu::FindSwitchState(evt, "Flip Y", NULL)) {
   1617         fFlipAxis ^= kFlipAxis_Y;
   1618         this->updateMatrix();
   1619         return true;
   1620     }
   1621     if (SkOSMenu::FindAction(evt,"Save to PDF")) {
   1622         this->saveToPdf();
   1623         return true;
   1624     }
   1625     return this->INHERITED::onEvent(evt);
   1626 }
   1627 
   1628 bool SampleWindow::onQuery(SkEvent* query) {
   1629     if (query->isType("get-slide-count")) {
   1630         query->setFast32(fSamples.count());
   1631         return true;
   1632     }
   1633     if (query->isType("get-slide-title")) {
   1634         SkView* view = (*fSamples[query->getFast32()])();
   1635         SkEvent evt(gTitleEvtName);
   1636         if (view->doQuery(&evt)) {
   1637             query->setString("title", evt.findString(gTitleEvtName));
   1638         }
   1639         SkSafeUnref(view);
   1640         return true;
   1641     }
   1642     if (query->isType("use-fast-text")) {
   1643         SkEvent evt(gFastTextEvtName);
   1644         return curr_view(this)->doQuery(&evt);
   1645     }
   1646     if (query->isType("ignore-window-bitmap")) {
   1647         query->setFast32(this->getGrContext() != NULL);
   1648         return true;
   1649     }
   1650     return this->INHERITED::onQuery(query);
   1651 }
   1652 
   1653 #if 0 // UNUSED
   1654 static void cleanup_for_filename(SkString* name) {
   1655     char* str = name->writable_str();
   1656     for (size_t i = 0; i < name->size(); i++) {
   1657         switch (str[i]) {
   1658             case ':': str[i] = '-'; break;
   1659             case '/': str[i] = '-'; break;
   1660             case ' ': str[i] = '_'; break;
   1661             default: break;
   1662         }
   1663     }
   1664 }
   1665 #endif
   1666 
   1667 DECLARE_bool(portableFonts);
   1668 
   1669 bool SampleWindow::onHandleChar(SkUnichar uni) {
   1670     {
   1671         SkView* view = curr_view(this);
   1672         if (view) {
   1673             SkEvent evt(gCharEvtName);
   1674             evt.setFast32(uni);
   1675             if (view->doQuery(&evt)) {
   1676                 return true;
   1677             }
   1678         }
   1679     }
   1680 
   1681     int dx = 0xFF;
   1682     int dy = 0xFF;
   1683 
   1684     switch (uni) {
   1685         case '5': dx =  0; dy =  0; break;
   1686         case '8': dx =  0; dy = -1; break;
   1687         case '6': dx =  1; dy =  0; break;
   1688         case '2': dx =  0; dy =  1; break;
   1689         case '4': dx = -1; dy =  0; break;
   1690         case '7': dx = -1; dy = -1; break;
   1691         case '9': dx =  1; dy = -1; break;
   1692         case '3': dx =  1; dy =  1; break;
   1693         case '1': dx = -1; dy =  1; break;
   1694 
   1695         default:
   1696             break;
   1697     }
   1698 
   1699     if (0xFF != dx && 0xFF != dy) {
   1700         if ((dx | dy) == 0) {
   1701             fScrollTestX = fScrollTestY = 0;
   1702         } else {
   1703             fScrollTestX += dx;
   1704             fScrollTestY += dy;
   1705         }
   1706         this->inval(NULL);
   1707         return true;
   1708     }
   1709 
   1710     switch (uni) {
   1711         case 'B':
   1712             post_event_to_sink(SkNEW_ARGS(SkEvent, ("PictFileView::toggleBBox")), curr_view(this));
   1713             // Cannot call updateTitle() synchronously, because the toggleBBox event is still in
   1714             // the queue.
   1715             post_event_to_sink(SkNEW_ARGS(SkEvent, (gUpdateWindowTitleEvtName)), this);
   1716             this->inval(NULL);
   1717             break;
   1718         case 'f':
   1719             // only
   1720             toggleFPS();
   1721             break;
   1722         case 'F':
   1723             FLAGS_portableFonts ^= true;
   1724             this->inval(NULL);
   1725             break;
   1726         case 'g':
   1727             fRequestGrabImage = true;
   1728             this->inval(NULL);
   1729             break;
   1730         case 'G':
   1731             gShowGMBounds = !gShowGMBounds;
   1732             post_event_to_sink(GMSampleView::NewShowSizeEvt(gShowGMBounds),
   1733                             curr_view(this));
   1734             this->inval(NULL);
   1735             break;
   1736         case 'i':
   1737             this->zoomIn();
   1738             break;
   1739         case 'o':
   1740             this->zoomOut();
   1741             break;
   1742         case 'r':
   1743             fRotate = !fRotate;
   1744             fRotateAnimTime = 0;
   1745             this->inval(NULL);
   1746             this->updateTitle();
   1747             return true;
   1748         case 'k':
   1749             fPerspAnim = !fPerspAnim;
   1750             this->inval(NULL);
   1751             this->updateTitle();
   1752             return true;
   1753 #if SK_SUPPORT_GPU
   1754         case '\\':
   1755             this->setDeviceType(kNullGPU_DeviceType);
   1756             this->inval(NULL);
   1757             this->updateTitle();
   1758             return true;
   1759         case 'p':
   1760             {
   1761                 GrContext* grContext = this->getGrContext();
   1762                 if (grContext) {
   1763                     size_t cacheBytes;
   1764                     grContext->getResourceCacheUsage(NULL, &cacheBytes);
   1765                     grContext->freeGpuResources();
   1766                     SkDebugf("Purged %d bytes from the GPU resource cache.\n", cacheBytes);
   1767                 }
   1768             }
   1769             return true;
   1770 #endif
   1771         default:
   1772             break;
   1773     }
   1774 
   1775     if (fAppMenu->handleKeyEquivalent(uni)|| fSlideMenu->handleKeyEquivalent(uni)) {
   1776         this->onUpdateMenu(fAppMenu);
   1777         this->onUpdateMenu(fSlideMenu);
   1778         return true;
   1779     }
   1780     return this->INHERITED::onHandleChar(uni);
   1781 }
   1782 
   1783 void SampleWindow::setDeviceType(DeviceType type) {
   1784     if (type == fDeviceType)
   1785         return;
   1786 
   1787     fDevManager->tearDownBackend(this);
   1788 
   1789     fDeviceType = type;
   1790 
   1791     fDevManager->setUpBackend(this, fMSAASampleCount);
   1792 
   1793     this->updateTitle();
   1794     this->inval(NULL);
   1795 }
   1796 
   1797 void SampleWindow::toggleSlideshow() {
   1798     fAnimating = !fAnimating;
   1799     this->postAnimatingEvent();
   1800     this->updateTitle();
   1801 }
   1802 
   1803 void SampleWindow::toggleRendering() {
   1804     this->setDeviceType(cycle_devicetype(fDeviceType));
   1805     this->updateTitle();
   1806     this->inval(NULL);
   1807 }
   1808 
   1809 void SampleWindow::toggleFPS() {
   1810     fMeasureFPS = !fMeasureFPS;
   1811     this->updateTitle();
   1812     this->inval(NULL);
   1813 }
   1814 
   1815 #include "SkDumpCanvas.h"
   1816 
   1817 bool SampleWindow::onHandleKey(SkKey key) {
   1818     {
   1819         SkView* view = curr_view(this);
   1820         if (view) {
   1821             SkEvent evt(gKeyEvtName);
   1822             evt.setFast32(key);
   1823             if (view->doQuery(&evt)) {
   1824                 return true;
   1825             }
   1826         }
   1827     }
   1828     switch (key) {
   1829         case kRight_SkKey:
   1830             if (this->nextSample()) {
   1831                 return true;
   1832             }
   1833             break;
   1834         case kLeft_SkKey:
   1835             if (this->previousSample()) {
   1836                 return true;
   1837             }
   1838             return true;
   1839         case kUp_SkKey:
   1840             if (USE_ARROWS_FOR_ZOOM) {
   1841                 this->changeZoomLevel(1.f / 32.f);
   1842             } else {
   1843                 fNClip = !fNClip;
   1844                 this->inval(NULL);
   1845                 this->updateTitle();
   1846             }
   1847             return true;
   1848         case kDown_SkKey:
   1849             if (USE_ARROWS_FOR_ZOOM) {
   1850                 this->changeZoomLevel(-1.f / 32.f);
   1851             } else {
   1852                 this->setColorType(cycle_colortypes(this->getBitmap().colorType()));
   1853                 this->updateTitle();
   1854             }
   1855             return true;
   1856         case kOK_SkKey: {
   1857             SkString title;
   1858             if (curr_title(this, &title)) {
   1859                 writeTitleToPrefs(title.c_str());
   1860             }
   1861             return true;
   1862         }
   1863         case kBack_SkKey:
   1864             this->showOverview();
   1865             return true;
   1866         default:
   1867             break;
   1868     }
   1869     return this->INHERITED::onHandleKey(key);
   1870 }
   1871 
   1872 ///////////////////////////////////////////////////////////////////////////////
   1873 
   1874 static const char gGestureClickType[] = "GestureClickType";
   1875 
   1876 bool SampleWindow::onDispatchClick(int x, int y, Click::State state,
   1877         void* owner, unsigned modi) {
   1878     if (Click::kMoved_State == state) {
   1879         updatePointer(x, y);
   1880     }
   1881     int w = SkScalarRoundToInt(this->width());
   1882     int h = SkScalarRoundToInt(this->height());
   1883 
   1884     // check for the resize-box
   1885     if (w - x < 16 && h - y < 16) {
   1886         return false;   // let the OS handle the click
   1887     }
   1888     else if (fMagnify) {
   1889         //it's only necessary to update the drawing if there's a click
   1890         this->inval(NULL);
   1891         return false; //prevent dragging while magnify is enabled
   1892     } else {
   1893         // capture control+option, and trigger debugger
   1894         if ((modi & kControl_SkModifierKey) && (modi & kOption_SkModifierKey)) {
   1895             if (Click::kDown_State == state) {
   1896                 SkEvent evt("debug-hit-test");
   1897                 evt.setS32("debug-hit-test-x", x);
   1898                 evt.setS32("debug-hit-test-y", y);
   1899                 curr_view(this)->doEvent(evt);
   1900             }
   1901             return true;
   1902         } else {
   1903             return this->INHERITED::onDispatchClick(x, y, state, owner, modi);
   1904         }
   1905     }
   1906 }
   1907 
   1908 class GestureClick : public SkView::Click {
   1909 public:
   1910     GestureClick(SkView* target) : SkView::Click(target) {
   1911         this->setType(gGestureClickType);
   1912     }
   1913 
   1914     static bool IsGesture(Click* click) {
   1915         return click->isType(gGestureClickType);
   1916     }
   1917 };
   1918 
   1919 SkView::Click* SampleWindow::onFindClickHandler(SkScalar x, SkScalar y,
   1920                                                 unsigned modi) {
   1921     return new GestureClick(this);
   1922 }
   1923 
   1924 bool SampleWindow::onClick(Click* click) {
   1925     if (GestureClick::IsGesture(click)) {
   1926         float x = static_cast<float>(click->fICurr.fX);
   1927         float y = static_cast<float>(click->fICurr.fY);
   1928 
   1929         switch (click->fState) {
   1930             case SkView::Click::kDown_State:
   1931                 fGesture.touchBegin(click->fOwner, x, y);
   1932                 break;
   1933             case SkView::Click::kMoved_State:
   1934                 fGesture.touchMoved(click->fOwner, x, y);
   1935                 this->updateMatrix();
   1936                 break;
   1937             case SkView::Click::kUp_State:
   1938                 fGesture.touchEnd(click->fOwner);
   1939                 this->updateMatrix();
   1940                 break;
   1941         }
   1942         return true;
   1943     }
   1944     return false;
   1945 }
   1946 
   1947 ///////////////////////////////////////////////////////////////////////////////
   1948 
   1949 void SampleWindow::loadView(SkView* view) {
   1950     SkView::F2BIter iter(this);
   1951     SkView* prev = iter.next();
   1952     if (prev) {
   1953         prev->detachFromParent();
   1954     }
   1955 
   1956     view->setVisibleP(true);
   1957     view->setClipToBounds(false);
   1958     this->attachChildToFront(view)->unref();
   1959     view->setSize(this->width(), this->height());
   1960 
   1961     //repopulate the slide menu when a view is loaded
   1962     fSlideMenu->reset();
   1963 
   1964     (void)SampleView::SetUsePipe(view, fPipeState);
   1965     if (SampleView::IsSampleView(view)) {
   1966         SampleView* sampleView = (SampleView*)view;
   1967         sampleView->requestMenu(fSlideMenu);
   1968         sampleView->onTileSizeChanged(this->tileSize());
   1969     }
   1970     this->onUpdateMenu(fSlideMenu);
   1971     this->updateTitle();
   1972 }
   1973 
   1974 static const char* gDeviceTypePrefix[] = {
   1975     "raster: ",
   1976     "picture: ",
   1977 #if SK_SUPPORT_GPU
   1978     "opengl: ",
   1979 #if SK_ANGLE
   1980     "angle: ",
   1981 #endif // SK_ANGLE
   1982     "null-gl: "
   1983 #endif // SK_SUPPORT_GPU
   1984 };
   1985 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gDeviceTypePrefix) == SampleWindow::kDeviceTypeCnt,
   1986                   array_size_mismatch);
   1987 
   1988 static const char* trystate_str(SkOSMenu::TriState state,
   1989                                 const char trueStr[], const char falseStr[]) {
   1990     if (SkOSMenu::kOnState == state) {
   1991         return trueStr;
   1992     } else if (SkOSMenu::kOffState == state) {
   1993         return falseStr;
   1994     }
   1995     return NULL;
   1996 }
   1997 
   1998 void SampleWindow::updateTitle() {
   1999     SkView* view = curr_view(this);
   2000 
   2001     SkString title;
   2002     if (!curr_title(this, &title)) {
   2003         title.set("<unknown>");
   2004     }
   2005 
   2006     title.prepend(gDeviceTypePrefix[fDeviceType]);
   2007 
   2008     title.prepend(" ");
   2009     title.prepend(sk_tool_utils::colortype_name(this->getBitmap().colorType()));
   2010 
   2011     if (fTilingMode != kNo_Tiling) {
   2012         title.prependf("<T: %s> ", gTilingInfo[fTilingMode].label);
   2013     }
   2014     if (fAnimating) {
   2015         title.prepend("<A> ");
   2016     }
   2017     if (fRotate) {
   2018         title.prepend("<R> ");
   2019     }
   2020     if (fNClip) {
   2021         title.prepend("<C> ");
   2022     }
   2023     if (fPerspAnim) {
   2024         title.prepend("<K> ");
   2025     }
   2026 
   2027     title.prepend(trystate_str(fLCDState, "LCD ", "lcd "));
   2028     title.prepend(trystate_str(fAAState, "AA ", "aa "));
   2029     title.prepend(gFilterLevelStates[fFilterLevelIndex].fLabel);
   2030     title.prepend(trystate_str(fSubpixelState, "S ", "s "));
   2031     title.prepend(fFlipAxis & kFlipAxis_X ? "X " : NULL);
   2032     title.prepend(fFlipAxis & kFlipAxis_Y ? "Y " : NULL);
   2033     title.prepend(gHintingStates[fHintingState].label);
   2034 
   2035     if (fZoomLevel) {
   2036         title.prependf("{%.2f} ", SkScalarToFloat(fZoomLevel));
   2037     }
   2038 
   2039     if (fMeasureFPS) {
   2040         title.appendf(" %8.3f ms", fMeasureFPS_Time / (float)FPS_REPEAT_COUNT);
   2041     }
   2042     if (SampleView::IsSampleView(view)) {
   2043         switch (fPipeState) {
   2044             case SkOSMenu::kOnState:
   2045                 title.prepend("<Pipe> ");
   2046                 break;
   2047             case SkOSMenu::kMixedState:
   2048                 title.prepend("<Tiled Pipe> ");
   2049                 break;
   2050 
   2051             default:
   2052                 break;
   2053         }
   2054         title.prepend("! ");
   2055     }
   2056 
   2057 #if SK_SUPPORT_GPU
   2058     if (IsGpuDeviceType(fDeviceType) &&
   2059         fDevManager &&
   2060         fDevManager->getGrRenderTarget() &&
   2061         fDevManager->getGrRenderTarget()->numSamples() > 0) {
   2062         title.appendf(" [MSAA: %d]",
   2063                        fDevManager->getGrRenderTarget()->numSamples());
   2064     }
   2065 #endif
   2066 
   2067     this->setTitle(title.c_str());
   2068 }
   2069 
   2070 void SampleWindow::onSizeChange() {
   2071     this->INHERITED::onSizeChange();
   2072 
   2073     SkView::F2BIter iter(this);
   2074     SkView* view = iter.next();
   2075     view->setSize(this->width(), this->height());
   2076 
   2077     // rebuild our clippath
   2078     {
   2079         const SkScalar W = this->width();
   2080         const SkScalar H = this->height();
   2081 
   2082         fClipPath.reset();
   2083 #if 0
   2084         for (SkScalar y = SK_Scalar1; y < H; y += SkIntToScalar(32)) {
   2085             SkRect r;
   2086             r.set(SK_Scalar1, y, SkIntToScalar(30), y + SkIntToScalar(30));
   2087             for (; r.fLeft < W; r.offset(SkIntToScalar(32), 0))
   2088                 fClipPath.addRect(r);
   2089         }
   2090 #else
   2091         SkRect r;
   2092         r.set(0, 0, W, H);
   2093         fClipPath.addRect(r, SkPath::kCCW_Direction);
   2094         r.set(W/4, H/4, W*3/4, H*3/4);
   2095         fClipPath.addRect(r, SkPath::kCW_Direction);
   2096 #endif
   2097     }
   2098 
   2099     fZoomCenterX = SkScalarHalf(this->width());
   2100     fZoomCenterY = SkScalarHalf(this->height());
   2101 
   2102 #ifdef SK_BUILD_FOR_ANDROID
   2103     // FIXME: The first draw after a size change does not work on Android, so
   2104     // we post an invalidate.
   2105     this->postInvalDelay();
   2106 #endif
   2107     this->updateTitle();    // to refresh our config
   2108     fDevManager->windowSizeChanged(this);
   2109 
   2110     if (fTilingMode != kNo_Tiling && SampleView::IsSampleView(view)) {
   2111         ((SampleView*)view)->onTileSizeChanged(this->tileSize());
   2112     }
   2113 }
   2114 
   2115 ///////////////////////////////////////////////////////////////////////////////
   2116 
   2117 static const char is_sample_view_tag[] = "sample-is-sample-view";
   2118 static const char repeat_count_tag[] = "sample-set-repeat-count";
   2119 static const char set_use_pipe_tag[] = "sample-set-use-pipe";
   2120 
   2121 bool SampleView::IsSampleView(SkView* view) {
   2122     SkEvent evt(is_sample_view_tag);
   2123     return view->doQuery(&evt);
   2124 }
   2125 
   2126 bool SampleView::SetRepeatDraw(SkView* view, int count) {
   2127     SkEvent evt(repeat_count_tag);
   2128     evt.setFast32(count);
   2129     return view->doEvent(evt);
   2130 }
   2131 
   2132 bool SampleView::SetUsePipe(SkView* view, SkOSMenu::TriState state) {
   2133     SkEvent evt;
   2134     evt.setS32(set_use_pipe_tag, state);
   2135     return view->doEvent(evt);
   2136 }
   2137 
   2138 bool SampleView::onEvent(const SkEvent& evt) {
   2139     if (evt.isType(repeat_count_tag)) {
   2140         fRepeatCount = evt.getFast32();
   2141         return true;
   2142     }
   2143 
   2144     int32_t pipeHolder;
   2145     if (evt.findS32(set_use_pipe_tag, &pipeHolder)) {
   2146         fPipeState = static_cast<SkOSMenu::TriState>(pipeHolder);
   2147         return true;
   2148     }
   2149 
   2150     return this->INHERITED::onEvent(evt);
   2151 }
   2152 
   2153 bool SampleView::onQuery(SkEvent* evt) {
   2154     if (evt->isType(is_sample_view_tag)) {
   2155         return true;
   2156     }
   2157     return this->INHERITED::onQuery(evt);
   2158 }
   2159 
   2160 
   2161 class SimplePC : public SkGPipeController {
   2162 public:
   2163     SimplePC(SkCanvas* target);
   2164     ~SimplePC();
   2165 
   2166     virtual void* requestBlock(size_t minRequest, size_t* actual);
   2167     virtual void notifyWritten(size_t bytes);
   2168 
   2169 private:
   2170     SkGPipeReader   fReader;
   2171     void*           fBlock;
   2172     size_t          fBlockSize;
   2173     size_t          fBytesWritten;
   2174     int             fAtomsWritten;
   2175     SkGPipeReader::Status   fStatus;
   2176 
   2177     size_t        fTotalWritten;
   2178 };
   2179 
   2180 SimplePC::SimplePC(SkCanvas* target) : fReader(target) {
   2181     fBlock = NULL;
   2182     fBlockSize = fBytesWritten = 0;
   2183     fStatus = SkGPipeReader::kDone_Status;
   2184     fTotalWritten = 0;
   2185     fAtomsWritten = 0;
   2186     fReader.setBitmapDecoder(&SkImageDecoder::DecodeMemory);
   2187 }
   2188 
   2189 SimplePC::~SimplePC() {
   2190 //    SkASSERT(SkGPipeReader::kDone_Status == fStatus);
   2191     if (fTotalWritten) {
   2192         SkDebugf("--- %d bytes %d atoms, status %d\n", fTotalWritten,
   2193                  fAtomsWritten, fStatus);
   2194 #ifdef  PIPE_FILE
   2195         //File is open in append mode
   2196         FILE* f = fopen(FILE_PATH, "ab");
   2197         SkASSERT(f != NULL);
   2198         fwrite((const char*)fBlock + fBytesWritten, 1, bytes, f);
   2199         fclose(f);
   2200 #endif
   2201 #ifdef PIPE_NET
   2202         if (fAtomsWritten > 1 && fTotalWritten > 4) { //ignore done
   2203             gServer.acceptConnections();
   2204             gServer.writePacket(fBlock, fTotalWritten);
   2205         }
   2206 #endif
   2207     }
   2208     sk_free(fBlock);
   2209 }
   2210 
   2211 void* SimplePC::requestBlock(size_t minRequest, size_t* actual) {
   2212     sk_free(fBlock);
   2213 
   2214     fBlockSize = minRequest * 4;
   2215     fBlock = sk_malloc_throw(fBlockSize);
   2216     fBytesWritten = 0;
   2217     *actual = fBlockSize;
   2218     return fBlock;
   2219 }
   2220 
   2221 void SimplePC::notifyWritten(size_t bytes) {
   2222     SkASSERT(fBytesWritten + bytes <= fBlockSize);
   2223     fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
   2224     SkASSERT(SkGPipeReader::kError_Status != fStatus);
   2225     fBytesWritten += bytes;
   2226     fTotalWritten += bytes;
   2227 
   2228     fAtomsWritten += 1;
   2229 }
   2230 
   2231 void SampleView::draw(SkCanvas* canvas) {
   2232     if (SkOSMenu::kOffState == fPipeState) {
   2233         this->INHERITED::draw(canvas);
   2234     } else {
   2235         SkGPipeWriter writer;
   2236         SimplePC controller(canvas);
   2237         SkBitmap bitmap = capture_bitmap(canvas);
   2238         TiledPipeController tc(bitmap, &SkImageDecoder::DecodeMemory, &canvas->getTotalMatrix());
   2239         SkGPipeController* pc;
   2240         if (SkOSMenu::kMixedState == fPipeState) {
   2241             pc = &tc;
   2242         } else {
   2243             pc = &controller;
   2244         }
   2245         uint32_t flags = SkGPipeWriter::kCrossProcess_Flag;
   2246 
   2247         canvas = writer.startRecording(pc, flags);
   2248         //Must draw before controller goes out of scope and sends data
   2249         this->INHERITED::draw(canvas);
   2250         //explicitly end recording to ensure writer is flushed before the memory
   2251         //is freed in the deconstructor of the controller
   2252         writer.endRecording();
   2253     }
   2254 }
   2255 
   2256 void SampleView::onDraw(SkCanvas* canvas) {
   2257     this->onDrawBackground(canvas);
   2258 
   2259     for (int i = 0; i < fRepeatCount; i++) {
   2260         SkAutoCanvasRestore acr(canvas, true);
   2261         this->onDrawContent(canvas);
   2262     }
   2263 }
   2264 
   2265 void SampleView::onDrawBackground(SkCanvas* canvas) {
   2266     canvas->drawColor(fBGColor);
   2267 }
   2268 
   2269 ///////////////////////////////////////////////////////////////////////////////
   2270 
   2271 template <typename T> void SkTBSort(T array[], int count) {
   2272     for (int i = 1; i < count - 1; i++) {
   2273         bool didSwap = false;
   2274         for (int j = count - 1; j > i; --j) {
   2275             if (array[j] < array[j-1]) {
   2276                 T tmp(array[j-1]);
   2277                 array[j-1] = array[j];
   2278                 array[j] = tmp;
   2279                 didSwap = true;
   2280             }
   2281         }
   2282         if (!didSwap) {
   2283             break;
   2284         }
   2285     }
   2286 
   2287     for (int k = 0; k < count - 1; k++) {
   2288         SkASSERT(!(array[k+1] < array[k]));
   2289     }
   2290 }
   2291 
   2292 #include "SkRandom.h"
   2293 
   2294 static void rand_rect(SkIRect* rect, SkRandom& rand) {
   2295     int bits = 8;
   2296     int shift = 32 - bits;
   2297     rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
   2298               rand.nextU() >> shift, rand.nextU() >> shift);
   2299     rect->sort();
   2300 }
   2301 
   2302 static void dumpRect(const SkIRect& r) {
   2303     SkDebugf(" { %d, %d, %d, %d },\n",
   2304              r.fLeft, r.fTop,
   2305              r.fRight, r.fBottom);
   2306 }
   2307 
   2308 static void test_rects(const SkIRect rect[], int count) {
   2309     SkRegion rgn0, rgn1;
   2310 
   2311     for (int i = 0; i < count; i++) {
   2312         rgn0.op(rect[i], SkRegion::kUnion_Op);
   2313      //   dumpRect(rect[i]);
   2314     }
   2315     rgn1.setRects(rect, count);
   2316 
   2317     if (rgn0 != rgn1) {
   2318         SkDebugf("\n");
   2319         for (int i = 0; i < count; i++) {
   2320             dumpRect(rect[i]);
   2321         }
   2322         SkDebugf("\n");
   2323     }
   2324 }
   2325 
   2326 static void test() {
   2327     size_t i;
   2328 
   2329     const SkIRect r0[] = {
   2330         { 0, 0, 1, 1 },
   2331         { 2, 2, 3, 3 },
   2332     };
   2333     const SkIRect r1[] = {
   2334         { 0, 0, 1, 3 },
   2335         { 1, 1, 2, 2 },
   2336         { 2, 0, 3, 3 },
   2337     };
   2338     const SkIRect r2[] = {
   2339         { 0, 0, 1, 2 },
   2340         { 2, 1, 3, 3 },
   2341         { 4, 0, 5, 1 },
   2342         { 6, 0, 7, 4 },
   2343     };
   2344 
   2345     static const struct {
   2346         const SkIRect* fRects;
   2347         int            fCount;
   2348     } gRecs[] = {
   2349         { r0, SK_ARRAY_COUNT(r0) },
   2350         { r1, SK_ARRAY_COUNT(r1) },
   2351         { r2, SK_ARRAY_COUNT(r2) },
   2352     };
   2353 
   2354     for (i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
   2355         test_rects(gRecs[i].fRects, gRecs[i].fCount);
   2356     }
   2357 
   2358     SkRandom rand;
   2359     for (i = 0; i < 10000; i++) {
   2360         SkRegion rgn0, rgn1;
   2361 
   2362         const int N = 8;
   2363         SkIRect rect[N];
   2364         for (int j = 0; j < N; j++) {
   2365             rand_rect(&rect[j], rand);
   2366         }
   2367         test_rects(rect, N);
   2368     }
   2369 }
   2370 
   2371 // FIXME: this should be in a header
   2372 SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
   2373 SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
   2374     if (false) { // avoid bit rot, suppress warning
   2375         test();
   2376     }
   2377     return new SampleWindow(hwnd, argc, argv, NULL);
   2378 }
   2379 
   2380 // FIXME: this should be in a header
   2381 void get_preferred_size(int* x, int* y, int* width, int* height);
   2382 void get_preferred_size(int* x, int* y, int* width, int* height) {
   2383     *x = 10;
   2384     *y = 50;
   2385     *width = 640;
   2386     *height = 480;
   2387 }
   2388 
   2389 #ifdef SK_BUILD_FOR_IOS
   2390 IOS_launch_type set_cmd_line_args(int , char *[], const char* resourceDir) {
   2391     SetResourcePath(resourceDir);
   2392     return kApplication__iOSLaunchType;
   2393 }
   2394 #endif
   2395 
   2396 void application_init() {
   2397 //    setenv("ANDROID_ROOT", "../../../data", 0);
   2398 #ifdef SK_BUILD_FOR_MAC
   2399     setenv("ANDROID_ROOT", "/android/device/data", 0);
   2400 #endif
   2401     SkGraphics::Init();
   2402     SkEvent::Init();
   2403 }
   2404 
   2405 void application_term() {
   2406     SkEvent::Term();
   2407     SkGraphics::Term();
   2408 }
   2409