blob: e587e1ff6295a4860f195a5d3c95edbbaf7914e2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
Scroggo2c8208f2011-06-15 16:49:08 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Skia
Scroggo2c8208f2011-06-15 16:49:08 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
Scroggo2c8208f2011-06-15 16:49:08 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
Scroggo2c8208f2011-06-15 16:49:08 +000010#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"
yangsu@google.com921091f2011-08-02 13:39:12 +000021#include "SkOSMenu.h"
Scroggo2c8208f2011-06-15 16:49:08 +000022
23class GrContext;
reed@google.com29038ed2011-07-06 17:56:47 +000024class GrRenderTarget;
Scroggo2c8208f2011-06-15 16:49:08 +000025
26class SkEvent;
27class SkCanvas;
Scroggo2c8208f2011-06-15 16:49:08 +000028class SkPicture;
29class SkTypeface;
yangsu@google.com501775e2011-06-24 16:04:50 +000030class SkData;
Scroggo2c8208f2011-06-15 16:49:08 +000031
Scroggo2c8208f2011-06-15 16:49:08 +000032class SampleWindow : public SkOSWindow {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000033 SkTDArray<const SkViewFactory*> fSamples;
Scroggo2c8208f2011-06-15 16:49:08 +000034public:
bsalomon@google.com098e96d2011-07-14 14:30:46 +000035 enum DeviceType {
36 kRaster_DeviceType,
37 kPicture_DeviceType,
bsalomon@google.com74913722011-10-27 20:44:19 +000038 kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000039#if SK_ANGLE
40 kANGLE_DeviceType,
41#endif
bsalomon@google.com74913722011-10-27 20:44:19 +000042 kNullGPU_DeviceType
bsalomon@google.com098e96d2011-07-14 14:30:46 +000043 };
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:
bsalomon@google.com11959252012-04-06 20:13:38 +000053 virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000054
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000055 virtual void tearDownBackend(SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000056
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
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000074 virtual GrContext* getGrContext() = 0;
bsalomon@google.com11959252012-04-06 20:13:38 +000075
76 // return the GrRenderTarget backing gpu devices
77 virtual GrRenderTarget* getGrRenderTarget() = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000078 };
79
80 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
Scroggo2c8208f2011-06-15 16:49:08 +000081 virtual ~SampleWindow();
82
83 virtual void draw(SkCanvas* canvas);
84
yangsu@google.com921091f2011-08-02 13:39:12 +000085 void setDeviceType(DeviceType type);
Scroggo2c8208f2011-06-15 16:49:08 +000086 void toggleRendering();
87 void toggleSlideshow();
88 void toggleFPS();
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000089 void showOverview();
bsalomon@google.com098e96d2011-07-14 14:30:46 +000090
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000091 GrContext* getGrContext() const { return fDevManager->getGrContext(); }
bsalomon@google.com098e96d2011-07-14 14:30:46 +000092
Scroggo2c8208f2011-06-15 16:49:08 +000093 void setZoomCenter(float x, float y);
94 void changeZoomLevel(float delta);
95 bool nextSample();
96 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +000097 bool goToSample(int i);
98 SkString getSampleTitle(int i);
99 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +0000100 bool handleTouch(int ownerId, float x, float y,
101 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +0000102 void saveToPdf();
yangsu@google.com501775e2011-06-24 16:04:50 +0000103 SkData* getPDFData() { return fPDFData; }
Scroggo62b65b02011-06-21 16:01:26 +0000104 void postInvalDelay();
Scroggo2c8208f2011-06-15 16:49:08 +0000105
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000106 DeviceType getDeviceType() const { return fDeviceType; }
107
Scroggo2c8208f2011-06-15 16:49:08 +0000108protected:
109 virtual void onDraw(SkCanvas* canvas);
110 virtual bool onHandleKey(SkKey key);
111 virtual bool onHandleChar(SkUnichar);
112 virtual void onSizeChange();
113
114 virtual SkCanvas* beforeChildren(SkCanvas*);
115 virtual void afterChildren(SkCanvas*);
116 virtual void beforeChild(SkView* child, SkCanvas* canvas);
117 virtual void afterChild(SkView* child, SkCanvas* canvas);
118
119 virtual bool onEvent(const SkEvent& evt);
120 virtual bool onQuery(SkEvent* evt);
121
Scroggod3aed392011-06-22 13:26:56 +0000122 virtual bool onDispatchClick(int x, int y, Click::State, void* owner);
Scroggo2c8208f2011-06-15 16:49:08 +0000123 virtual bool onClick(Click* click);
124 virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
125
126private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000127 class DefaultDeviceManager;
128
Scroggo2c8208f2011-06-15 16:49:08 +0000129 int fCurrIndex;
130
131 SkPicture* fPicture;
Scroggo2c8208f2011-06-15 16:49:08 +0000132 SkPath fClipPath;
133
134 SkTouchGesture fGesture;
135 SkScalar fZoomLevel;
136 SkScalar fZoomScale;
137
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000138 DeviceType fDeviceType;
139 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000140
Scroggo8ac0d542011-06-21 14:44:57 +0000141 bool fSaveToPdf;
142 SkCanvas* fPdfCanvas;
yangsu@google.com501775e2011-06-24 16:04:50 +0000143 SkData* fPDFData;
Scroggo8ac0d542011-06-21 14:44:57 +0000144
Scroggo2c8208f2011-06-15 16:49:08 +0000145 bool fUseClip;
146 bool fNClip;
Scroggo2c8208f2011-06-15 16:49:08 +0000147 bool fAnimating;
148 bool fRotate;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000149 bool fPerspAnim;
150 SkScalar fPerspAnimTime;
Scroggo2c8208f2011-06-15 16:49:08 +0000151 bool fScale;
152 bool fRequestGrabImage;
Scroggo2c8208f2011-06-15 16:49:08 +0000153 bool fMeasureFPS;
154 SkMSec fMeasureFPS_Time;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000155 bool fMagnify;
156
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000157
158 bool fUsePipe;
159 int fUsePipeMenuItemID;
160 bool fDebugger;
161
Scroggo2c8208f2011-06-15 16:49:08 +0000162 // The following are for the 'fatbits' drawing
163 // Latest position of the mouse.
164 int fMouseX, fMouseY;
165 int fFatBitsScale;
166 // Used by the text showing position and color values.
167 SkTypeface* fTypeface;
168 bool fShowZoomer;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000169
170 SkOSMenu::TriState fLCDState;
171 SkOSMenu::TriState fAAState;
172 SkOSMenu::TriState fFilterState;
173 SkOSMenu::TriState fHintingState;
Scroggo2c8208f2011-06-15 16:49:08 +0000174 unsigned fFlipAxis;
175
bsalomon@google.com11959252012-04-06 20:13:38 +0000176 int fMSAASampleCount;
177
Scroggo2c8208f2011-06-15 16:49:08 +0000178 int fScrollTestX, fScrollTestY;
179 SkScalar fZoomCenterX, fZoomCenterY;
180
yangsu@google.com921091f2011-08-02 13:39:12 +0000181 //Stores global settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000182 SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
yangsu@google.com921091f2011-08-02 13:39:12 +0000183 //Stores slide specific settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000184 SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
185
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000186 int fTransitionNext;
187 int fTransitionPrev;
scroggo@google.com7dadc742012-04-18 14:07:57 +0000188
Scroggo2c8208f2011-06-15 16:49:08 +0000189 void loadView(SkView*);
190 void updateTitle();
191
Scroggo2c8208f2011-06-15 16:49:08 +0000192 bool zoomIn();
193 bool zoomOut();
194 void updatePointer(int x, int y);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000195 void magnify(SkCanvas* canvas);
Scroggo2c8208f2011-06-15 16:49:08 +0000196 void showZoomer(SkCanvas* canvas);
reed@google.comf03bb562011-11-11 21:42:12 +0000197 void updateMatrix();
Scroggo2c8208f2011-06-15 16:49:08 +0000198 void postAnimatingEvent();
reed@google.come23f1942011-12-08 19:36:00 +0000199 void installDrawFilter(SkCanvas*);
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000200 int findByTitle(const char*);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000201 void listTitles();
Scroggo2c8208f2011-06-15 16:49:08 +0000202
Scroggo2c8208f2011-06-15 16:49:08 +0000203 typedef SkOSWindow INHERITED;
204};
205
206#endif