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