SampleApp.h revision 5f017a4ab001baf1b9f433a9b02c6e01f93a97a1
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 kGPU_DeviceType, 39#if SK_ANGLE 40 kANGLE_DeviceType, 41#endif 42 kNullGPU_DeviceType 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 virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) = 0; 54 55 virtual void tearDownBackend(SampleWindow* win) = 0; 56 57 // called before drawing. should install correct device 58 // type on the canvas. Will skip drawing if returns false. 59 virtual bool prepareCanvas(DeviceType dType, 60 SkCanvas* canvas, 61 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 74 virtual GrContext* getGrContext() = 0; 75 }; 76 77 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*); 78 virtual ~SampleWindow(); 79 80 virtual void draw(SkCanvas* canvas); 81 82 void setDeviceType(DeviceType type); 83 void toggleRendering(); 84 void toggleSlideshow(); 85 void toggleFPS(); 86 void showOverview(); 87 88 GrContext* getGrContext() const { return fDevManager->getGrContext(); } 89 90 void setZoomCenter(float x, float y); 91 void changeZoomLevel(float delta); 92 bool nextSample(); 93 bool previousSample(); 94 bool goToSample(int i); 95 SkString getSampleTitle(int i); 96 int sampleCount(); 97 bool handleTouch(int ownerId, float x, float y, 98 SkView::Click::State state); 99 void saveToPdf(); 100 SkData* getPDFData() { return fPDFData; } 101 void postInvalDelay(); 102 103 DeviceType getDeviceType() const { return fDeviceType; } 104 105protected: 106 virtual void onDraw(SkCanvas* canvas); 107 virtual bool onHandleKey(SkKey key); 108 virtual bool onHandleChar(SkUnichar); 109 virtual void onSizeChange(); 110 111 virtual SkCanvas* beforeChildren(SkCanvas*); 112 virtual void afterChildren(SkCanvas*); 113 virtual void beforeChild(SkView* child, SkCanvas* canvas); 114 virtual void afterChild(SkView* child, SkCanvas* canvas); 115 116 virtual bool onEvent(const SkEvent& evt); 117 virtual bool onQuery(SkEvent* evt); 118 119 virtual bool onDispatchClick(int x, int y, Click::State, void* owner); 120 virtual bool onClick(Click* click); 121 virtual Click* onFindClickHandler(SkScalar x, SkScalar y); 122 123private: 124 class DefaultDeviceManager; 125 126 int fCurrIndex; 127 128 SkPicture* fPicture; 129 SkPath fClipPath; 130 131 SkTouchGesture fGesture; 132 SkScalar fZoomLevel; 133 SkScalar fZoomScale; 134 135 DeviceType fDeviceType; 136 DeviceManager* fDevManager; 137 138 bool fSaveToPdf; 139 SkCanvas* fPdfCanvas; 140 SkData* fPDFData; 141 142 bool fUseClip; 143 bool fNClip; 144 bool fAnimating; 145 bool fRotate; 146 bool fPerspAnim; 147 SkScalar fPerspAnimTime; 148 bool fScale; 149 bool fRequestGrabImage; 150 bool fMeasureFPS; 151 SkMSec fMeasureFPS_Time; 152 bool fMagnify; 153 154 155 bool fUsePipe; 156 int fUsePipeMenuItemID; 157 bool fDebugger; 158 159 // The following are for the 'fatbits' drawing 160 // Latest position of the mouse. 161 int fMouseX, fMouseY; 162 int fFatBitsScale; 163 // Used by the text showing position and color values. 164 SkTypeface* fTypeface; 165 bool fShowZoomer; 166 167 SkOSMenu::TriState fLCDState; 168 SkOSMenu::TriState fAAState; 169 SkOSMenu::TriState fFilterState; 170 SkOSMenu::TriState fHintingState; 171 unsigned fFlipAxis; 172 173 int fMSAASampleCount; 174 175 int fScrollTestX, fScrollTestY; 176 SkScalar fZoomCenterX, fZoomCenterY; 177 178 //Stores global settings 179 SkOSMenu fAppMenu; 180 //Stores slide specific settings 181 SkOSMenu fSlideMenu; 182 int fTransitionNext; 183 int fTransitionPrev; 184 185 void loadView(SkView*); 186 void updateTitle(); 187 188 bool zoomIn(); 189 bool zoomOut(); 190 void updatePointer(int x, int y); 191 void magnify(SkCanvas* canvas); 192 void showZoomer(SkCanvas* canvas); 193 void updateMatrix(); 194 void postAnimatingEvent(); 195 void installDrawFilter(SkCanvas*); 196 int findByTitle(const char*); 197 198 typedef SkOSWindow INHERITED; 199}; 200 201#endif 202