blob: 202fe21ad31370265c9b94670db8f084c9ae28e8 [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.comcf8fb1f2012-08-02 14:03:32 +000038#if SK_SUPPORT_GPU
bsalomon@google.com74913722011-10-27 20:44:19 +000039 kGPU_DeviceType,
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000040#if SK_ANGLE
41 kANGLE_DeviceType,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000042#endif // SK_ANGLE
43 kNullGPU_DeviceType,
44#endif // SK_SUPPORT_GPU
45
46 kDeviceTypeCnt
bsalomon@google.com098e96d2011-07-14 14:30:46 +000047 };
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:
robertphillips@google.coma22e2112012-08-16 14:58:06 +000057 SK_DECLARE_INST_COUNT(DeviceManager)
58
bsalomon@google.com11959252012-04-06 20:13:38 +000059 virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000060
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000061 virtual void tearDownBackend(SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000062
63 // called before drawing. should install correct device
64 // type on the canvas. Will skip drawing if returns false.
65 virtual bool prepareCanvas(DeviceType dType,
66 SkCanvas* canvas,
67 SampleWindow* win) = 0;
68
69 // called after drawing, should get the results onto the
70 // screen.
71 virtual void publishCanvas(DeviceType dType,
72 SkCanvas* canvas,
73 SampleWindow* win) = 0;
74
75 // called when window changes size, guaranteed to be called
76 // at least once before first draw (after init)
77 virtual void windowSizeChanged(SampleWindow* win) = 0;
78
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000079 // return the GrContext backing gpu devices (NULL if not built with GPU support)
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000080 virtual GrContext* getGrContext() = 0;
bsalomon@google.com11959252012-04-06 20:13:38 +000081
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000082 // return the GrRenderTarget backing gpu devices (NULL if not built with GPU support)
bsalomon@google.com11959252012-04-06 20:13:38 +000083 virtual GrRenderTarget* getGrRenderTarget() = 0;
robertphillips@google.coma22e2112012-08-16 14:58:06 +000084 private:
85 typedef SkRefCnt INHERITED;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000086 };
87
88 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
Scroggo2c8208f2011-06-15 16:49:08 +000089 virtual ~SampleWindow();
90
91 virtual void draw(SkCanvas* canvas);
92
yangsu@google.com921091f2011-08-02 13:39:12 +000093 void setDeviceType(DeviceType type);
Scroggo2c8208f2011-06-15 16:49:08 +000094 void toggleRendering();
95 void toggleSlideshow();
96 void toggleFPS();
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000097 void showOverview();
bsalomon@google.com098e96d2011-07-14 14:30:46 +000098
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000099 GrContext* getGrContext() const { return fDevManager->getGrContext(); }
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000100
Scroggo2c8208f2011-06-15 16:49:08 +0000101 void setZoomCenter(float x, float y);
102 void changeZoomLevel(float delta);
103 bool nextSample();
104 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +0000105 bool goToSample(int i);
106 SkString getSampleTitle(int i);
107 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +0000108 bool handleTouch(int ownerId, float x, float y,
109 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +0000110 void saveToPdf();
yangsu@google.com501775e2011-06-24 16:04:50 +0000111 SkData* getPDFData() { return fPDFData; }
Scroggo62b65b02011-06-21 16:01:26 +0000112 void postInvalDelay();
Scroggo2c8208f2011-06-15 16:49:08 +0000113
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000114 DeviceType getDeviceType() const { return fDeviceType; }
115
Scroggo2c8208f2011-06-15 16:49:08 +0000116protected:
117 virtual void onDraw(SkCanvas* canvas);
118 virtual bool onHandleKey(SkKey key);
119 virtual bool onHandleChar(SkUnichar);
120 virtual void onSizeChange();
121
122 virtual SkCanvas* beforeChildren(SkCanvas*);
123 virtual void afterChildren(SkCanvas*);
124 virtual void beforeChild(SkView* child, SkCanvas* canvas);
125 virtual void afterChild(SkView* child, SkCanvas* canvas);
126
127 virtual bool onEvent(const SkEvent& evt);
128 virtual bool onQuery(SkEvent* evt);
129
Scroggod3aed392011-06-22 13:26:56 +0000130 virtual bool onDispatchClick(int x, int y, Click::State, void* owner);
Scroggo2c8208f2011-06-15 16:49:08 +0000131 virtual bool onClick(Click* click);
132 virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
133
reed@google.com1830c7a2012-06-04 12:05:43 +0000134 void registerPictFileSamples(char** argv, int argc);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000135 void registerPictFileSample(char** argv, int argc);
reed@google.com1830c7a2012-06-04 12:05:43 +0000136
Scroggo2c8208f2011-06-15 16:49:08 +0000137private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000138 class DefaultDeviceManager;
139
Scroggo2c8208f2011-06-15 16:49:08 +0000140 int fCurrIndex;
141
142 SkPicture* fPicture;
Scroggo2c8208f2011-06-15 16:49:08 +0000143 SkPath fClipPath;
144
145 SkTouchGesture fGesture;
146 SkScalar fZoomLevel;
147 SkScalar fZoomScale;
148
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000149 DeviceType fDeviceType;
150 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000151
Scroggo8ac0d542011-06-21 14:44:57 +0000152 bool fSaveToPdf;
153 SkCanvas* fPdfCanvas;
yangsu@google.com501775e2011-06-24 16:04:50 +0000154 SkData* fPDFData;
Scroggo8ac0d542011-06-21 14:44:57 +0000155
Scroggo2c8208f2011-06-15 16:49:08 +0000156 bool fUseClip;
157 bool fNClip;
Scroggo2c8208f2011-06-15 16:49:08 +0000158 bool fAnimating;
159 bool fRotate;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000160 bool fPerspAnim;
161 SkScalar fPerspAnimTime;
Scroggo2c8208f2011-06-15 16:49:08 +0000162 bool fScale;
163 bool fRequestGrabImage;
Scroggo2c8208f2011-06-15 16:49:08 +0000164 bool fMeasureFPS;
165 SkMSec fMeasureFPS_Time;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000166 bool fMagnify;
scroggo@google.com85cade02012-08-17 13:29:50 +0000167 SkISize fTileCount;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000168
169
scroggo@google.comb073d922012-06-08 15:35:03 +0000170 SkOSMenu::TriState fPipeState; // Mixed uses a tiled pipe
171 // On uses a normal pipe
172 // Off uses no pipe
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000173 int fUsePipeMenuItemID;
174 bool fDebugger;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000175
Scroggo2c8208f2011-06-15 16:49:08 +0000176 // The following are for the 'fatbits' drawing
177 // Latest position of the mouse.
178 int fMouseX, fMouseY;
179 int fFatBitsScale;
180 // Used by the text showing position and color values.
181 SkTypeface* fTypeface;
182 bool fShowZoomer;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000183
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000184 SkOSMenu::TriState fLCDState;
185 SkOSMenu::TriState fAAState;
186 SkOSMenu::TriState fFilterState;
187 SkOSMenu::TriState fHintingState;
reed@google.com67b89ee2012-08-15 14:41:58 +0000188 SkOSMenu::TriState fTilingState;
Scroggo2c8208f2011-06-15 16:49:08 +0000189 unsigned fFlipAxis;
190
bsalomon@google.com11959252012-04-06 20:13:38 +0000191 int fMSAASampleCount;
192
Scroggo2c8208f2011-06-15 16:49:08 +0000193 int fScrollTestX, fScrollTestY;
194 SkScalar fZoomCenterX, fZoomCenterY;
195
yangsu@google.com921091f2011-08-02 13:39:12 +0000196 //Stores global settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000197 SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
yangsu@google.com921091f2011-08-02 13:39:12 +0000198 //Stores slide specific settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000199 SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
200
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000201 int fTransitionNext;
202 int fTransitionPrev;
scroggo@google.com7dadc742012-04-18 14:07:57 +0000203
Scroggo2c8208f2011-06-15 16:49:08 +0000204 void loadView(SkView*);
205 void updateTitle();
206
Scroggo2c8208f2011-06-15 16:49:08 +0000207 bool zoomIn();
208 bool zoomOut();
209 void updatePointer(int x, int y);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000210 void magnify(SkCanvas* canvas);
Scroggo2c8208f2011-06-15 16:49:08 +0000211 void showZoomer(SkCanvas* canvas);
reed@google.comf03bb562011-11-11 21:42:12 +0000212 void updateMatrix();
Scroggo2c8208f2011-06-15 16:49:08 +0000213 void postAnimatingEvent();
reed@google.come23f1942011-12-08 19:36:00 +0000214 void installDrawFilter(SkCanvas*);
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000215 int findByTitle(const char*);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000216 void listTitles();
Scroggo2c8208f2011-06-15 16:49:08 +0000217
Scroggo2c8208f2011-06-15 16:49:08 +0000218 typedef SkOSWindow INHERITED;
219};
220
221#endif