SampleApp.h revision 796763e0b2c06a7789185322561a1135e5484688
1/* 2 * Copyright 2011 Skia 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#ifndef SampleApp_DEFINED 9#define SampleApp_DEFINED 10 11#include "SkOSMenu.h" 12#include "SkPath.h" 13#include "SkScalar.h" 14#include "SkTDArray.h" 15#include "SkTouchGesture.h" 16#include "SkWindow.h" 17 18class GrContext; 19class GrRenderTarget; 20 21class SkCanvas; 22class SkData; 23class SkEvent; 24class SkPicture; 25class SkTypeface; 26class SkViewFactory; 27 28class SampleWindow : public SkOSWindow { 29 SkTDArray<const SkViewFactory*> fSamples; 30public: 31 enum DeviceType { 32 kRaster_DeviceType, 33 kPicture_DeviceType, 34#if SK_SUPPORT_GPU 35 kGPU_DeviceType, 36#if SK_ANGLE 37 kANGLE_DeviceType, 38#endif // SK_ANGLE 39 kNullGPU_DeviceType, 40#endif // SK_SUPPORT_GPU 41 42 kDeviceTypeCnt 43 }; 44 /** 45 * SampleApp ports can subclass this manager class if they want to: 46 * * filter the types of devices supported 47 * * customize plugging of SkDevice objects into an SkCanvas 48 * * customize publishing the results of draw to the OS window 49 * * manage GrContext / GrRenderTarget lifetimes 50 */ 51 class DeviceManager : public SkRefCnt { 52 public: 53 SK_DECLARE_INST_COUNT(DeviceManager) 54 55 virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) = 0; 56 57 virtual void tearDownBackend(SampleWindow* win) = 0; 58 59 // called before drawing. should install correct device 60 // type on the canvas. Will skip drawing if returns false. 61 virtual SkCanvas* createCanvas(DeviceType dType, SampleWindow* win) = 0; 62 63 // called after drawing, should get the results onto the 64 // screen. 65 virtual void publishCanvas(DeviceType dType, 66 SkCanvas* canvas, 67 SampleWindow* win) = 0; 68 69 // called when window changes size, guaranteed to be called 70 // at least once before first draw (after init) 71 virtual void windowSizeChanged(SampleWindow* win) = 0; 72 73 // return the GrContext backing gpu devices (NULL if not built with GPU support) 74 virtual GrContext* getGrContext() = 0; 75 76 // return the GrRenderTarget backing gpu devices (NULL if not built with GPU support) 77 virtual GrRenderTarget* getGrRenderTarget() = 0; 78 private: 79 typedef SkRefCnt INHERITED; 80 }; 81 82 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*); 83 virtual ~SampleWindow(); 84 85 virtual SkCanvas* createCanvas() SK_OVERRIDE { 86 SkCanvas* canvas = NULL; 87 if (fDevManager) { 88 canvas = fDevManager->createCanvas(fDeviceType, this); 89 } 90 if (NULL == canvas) { 91 canvas = this->INHERITED::createCanvas(); 92 } 93 return canvas; 94 } 95 96 virtual void draw(SkCanvas* canvas); 97 98 void setDeviceType(DeviceType type); 99 void toggleRendering(); 100 void toggleSlideshow(); 101 void toggleFPS(); 102 void showOverview(); 103 104 GrContext* getGrContext() const { return fDevManager->getGrContext(); } 105 106 void setZoomCenter(float x, float y); 107 void changeZoomLevel(float delta); 108 bool nextSample(); 109 bool previousSample(); 110 bool goToSample(int i); 111 SkString getSampleTitle(int i); 112 int sampleCount(); 113 bool handleTouch(int ownerId, float x, float y, 114 SkView::Click::State state); 115 void saveToPdf(); 116 SkData* getPDFData() { return fPDFData; } 117 void postInvalDelay(); 118 119 DeviceType getDeviceType() const { return fDeviceType; } 120 121protected: 122 virtual void onDraw(SkCanvas* canvas); 123 virtual bool onHandleKey(SkKey key); 124 virtual bool onHandleChar(SkUnichar); 125 virtual void onSizeChange(); 126 127 virtual SkCanvas* beforeChildren(SkCanvas*); 128 virtual void afterChildren(SkCanvas*); 129 virtual void beforeChild(SkView* child, SkCanvas* canvas); 130 virtual void afterChild(SkView* child, SkCanvas* canvas); 131 132 virtual bool onEvent(const SkEvent& evt); 133 virtual bool onQuery(SkEvent* evt); 134 135 virtual bool onDispatchClick(int x, int y, Click::State, void* owner); 136 virtual bool onClick(Click* click); 137 virtual Click* onFindClickHandler(SkScalar x, SkScalar y); 138 139 void registerPictFileSamples(char** argv, int argc); 140 void registerPictFileSample(char** argv, int argc); 141 142private: 143 class DefaultDeviceManager; 144 145 int fCurrIndex; 146 147 SkPicture* fPicture; 148 SkPath fClipPath; 149 150 SkTouchGesture fGesture; 151 SkScalar fZoomLevel; 152 SkScalar fZoomScale; 153 154 DeviceType fDeviceType; 155 DeviceManager* fDevManager; 156 157 bool fSaveToPdf; 158 SkCanvas* fPdfCanvas; 159 SkData* fPDFData; 160 161 bool fUseClip; 162 bool fNClip; 163 bool fAnimating; 164 bool fRotate; 165 bool fPerspAnim; 166 SkScalar fPerspAnimTime; 167 bool fScale; 168 bool fRequestGrabImage; 169 bool fMeasureFPS; 170 SkMSec fMeasureFPS_Time; 171 SkMSec fMeasureFPS_StartTime; 172 bool fMagnify; 173 SkISize fTileCount; 174 175 176 SkOSMenu::TriState fPipeState; // Mixed uses a tiled pipe 177 // On uses a normal pipe 178 // Off uses no pipe 179 int fUsePipeMenuItemID; 180 bool fDebugger; 181 182 // The following are for the 'fatbits' drawing 183 // Latest position of the mouse. 184 int fMouseX, fMouseY; 185 int fFatBitsScale; 186 // Used by the text showing position and color values. 187 SkTypeface* fTypeface; 188 bool fShowZoomer; 189 190 SkOSMenu::TriState fLCDState; 191 SkOSMenu::TriState fAAState; 192 SkOSMenu::TriState fFilterState; 193 SkOSMenu::TriState fHintingState; 194 SkOSMenu::TriState fTilingState; 195 unsigned fFlipAxis; 196 197 int fMSAASampleCount; 198 199 int fScrollTestX, fScrollTestY; 200 SkScalar fZoomCenterX, fZoomCenterY; 201 202 //Stores global settings 203 SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu 204 //Stores slide specific settings 205 SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu 206 207 int fTransitionNext; 208 int fTransitionPrev; 209 210 void loadView(SkView*); 211 void updateTitle(); 212 213 bool zoomIn(); 214 bool zoomOut(); 215 void updatePointer(int x, int y); 216 void magnify(SkCanvas* canvas); 217 void showZoomer(SkCanvas* canvas); 218 void updateMatrix(); 219 void postAnimatingEvent(); 220 void installDrawFilter(SkCanvas*); 221 int findByTitle(const char*); 222 void listTitles(); 223 224 typedef SkOSWindow INHERITED; 225}; 226 227#endif 228