blob: 00353b208a5b25516d2b8d2467688938a0cd30c8 [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:
bsalomon@google.com11959252012-04-06 20:13:38 +000057 virtual void setUpBackend(SampleWindow* win, int msaaSampleCount) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000058
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000059 virtual void tearDownBackend(SampleWindow* win) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000060
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
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000077 // return the GrContext backing gpu devices (NULL if not built with GPU support)
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000078 virtual GrContext* getGrContext() = 0;
bsalomon@google.com11959252012-04-06 20:13:38 +000079
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000080 // return the GrRenderTarget backing gpu devices (NULL if not built with GPU support)
bsalomon@google.com11959252012-04-06 20:13:38 +000081 virtual GrRenderTarget* getGrRenderTarget() = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000082 };
83
84 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
Scroggo2c8208f2011-06-15 16:49:08 +000085 virtual ~SampleWindow();
86
87 virtual void draw(SkCanvas* canvas);
88
yangsu@google.com921091f2011-08-02 13:39:12 +000089 void setDeviceType(DeviceType type);
Scroggo2c8208f2011-06-15 16:49:08 +000090 void toggleRendering();
91 void toggleSlideshow();
92 void toggleFPS();
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000093 void showOverview();
bsalomon@google.com098e96d2011-07-14 14:30:46 +000094
robertphillips@google.comb442a6d2012-04-02 19:24:21 +000095 GrContext* getGrContext() const { return fDevManager->getGrContext(); }
bsalomon@google.com098e96d2011-07-14 14:30:46 +000096
Scroggo2c8208f2011-06-15 16:49:08 +000097 void setZoomCenter(float x, float y);
98 void changeZoomLevel(float delta);
99 bool nextSample();
100 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +0000101 bool goToSample(int i);
102 SkString getSampleTitle(int i);
103 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +0000104 bool handleTouch(int ownerId, float x, float y,
105 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +0000106 void saveToPdf();
yangsu@google.com501775e2011-06-24 16:04:50 +0000107 SkData* getPDFData() { return fPDFData; }
Scroggo62b65b02011-06-21 16:01:26 +0000108 void postInvalDelay();
Scroggo2c8208f2011-06-15 16:49:08 +0000109
robertphillips@google.comb442a6d2012-04-02 19:24:21 +0000110 DeviceType getDeviceType() const { return fDeviceType; }
111
Scroggo2c8208f2011-06-15 16:49:08 +0000112protected:
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
Scroggod3aed392011-06-22 13:26:56 +0000126 virtual bool onDispatchClick(int x, int y, Click::State, void* owner);
Scroggo2c8208f2011-06-15 16:49:08 +0000127 virtual bool onClick(Click* click);
128 virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
129
reed@google.com1830c7a2012-06-04 12:05:43 +0000130 void registerPictFileSamples(char** argv, int argc);
chudy@google.com4605a3f2012-08-01 17:58:01 +0000131 void registerPictFileSample(char** argv, int argc);
reed@google.com1830c7a2012-06-04 12:05:43 +0000132
Scroggo2c8208f2011-06-15 16:49:08 +0000133private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000134 class DefaultDeviceManager;
135
Scroggo2c8208f2011-06-15 16:49:08 +0000136 int fCurrIndex;
137
138 SkPicture* fPicture;
Scroggo2c8208f2011-06-15 16:49:08 +0000139 SkPath fClipPath;
140
141 SkTouchGesture fGesture;
142 SkScalar fZoomLevel;
143 SkScalar fZoomScale;
144
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000145 DeviceType fDeviceType;
146 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000147
Scroggo8ac0d542011-06-21 14:44:57 +0000148 bool fSaveToPdf;
149 SkCanvas* fPdfCanvas;
yangsu@google.com501775e2011-06-24 16:04:50 +0000150 SkData* fPDFData;
Scroggo8ac0d542011-06-21 14:44:57 +0000151
Scroggo2c8208f2011-06-15 16:49:08 +0000152 bool fUseClip;
153 bool fNClip;
Scroggo2c8208f2011-06-15 16:49:08 +0000154 bool fAnimating;
155 bool fRotate;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000156 bool fPerspAnim;
157 SkScalar fPerspAnimTime;
Scroggo2c8208f2011-06-15 16:49:08 +0000158 bool fScale;
159 bool fRequestGrabImage;
Scroggo2c8208f2011-06-15 16:49:08 +0000160 bool fMeasureFPS;
161 SkMSec fMeasureFPS_Time;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000162 bool fMagnify;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000163
164
scroggo@google.comb073d922012-06-08 15:35:03 +0000165 SkOSMenu::TriState fPipeState; // Mixed uses a tiled pipe
166 // On uses a normal pipe
167 // Off uses no pipe
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000168 int fUsePipeMenuItemID;
169 bool fDebugger;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000170
Scroggo2c8208f2011-06-15 16:49:08 +0000171 // 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;
chudy@google.com4605a3f2012-08-01 17:58:01 +0000178
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000179 SkOSMenu::TriState fLCDState;
180 SkOSMenu::TriState fAAState;
181 SkOSMenu::TriState fFilterState;
182 SkOSMenu::TriState fHintingState;
reed@google.com67b89ee2012-08-15 14:41:58 +0000183 SkOSMenu::TriState fTilingState;
Scroggo2c8208f2011-06-15 16:49:08 +0000184 unsigned fFlipAxis;
185
bsalomon@google.com11959252012-04-06 20:13:38 +0000186 int fMSAASampleCount;
187
Scroggo2c8208f2011-06-15 16:49:08 +0000188 int fScrollTestX, fScrollTestY;
189 SkScalar fZoomCenterX, fZoomCenterY;
190
yangsu@google.com921091f2011-08-02 13:39:12 +0000191 //Stores global settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000192 SkOSMenu* fAppMenu; // We pass ownership to SkWindow, when we call addMenu
yangsu@google.com921091f2011-08-02 13:39:12 +0000193 //Stores slide specific settings
scroggo@google.com7dadc742012-04-18 14:07:57 +0000194 SkOSMenu* fSlideMenu; // We pass ownership to SkWindow, when we call addMenu
195
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000196 int fTransitionNext;
197 int fTransitionPrev;
scroggo@google.com7dadc742012-04-18 14:07:57 +0000198
Scroggo2c8208f2011-06-15 16:49:08 +0000199 void loadView(SkView*);
200 void updateTitle();
201
Scroggo2c8208f2011-06-15 16:49:08 +0000202 bool zoomIn();
203 bool zoomOut();
204 void updatePointer(int x, int y);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000205 void magnify(SkCanvas* canvas);
Scroggo2c8208f2011-06-15 16:49:08 +0000206 void showZoomer(SkCanvas* canvas);
reed@google.comf03bb562011-11-11 21:42:12 +0000207 void updateMatrix();
Scroggo2c8208f2011-06-15 16:49:08 +0000208 void postAnimatingEvent();
reed@google.come23f1942011-12-08 19:36:00 +0000209 void installDrawFilter(SkCanvas*);
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000210 int findByTitle(const char*);
robertphillips@google.com7265e722012-05-03 18:22:28 +0000211 void listTitles();
Scroggo2c8208f2011-06-15 16:49:08 +0000212
Scroggo2c8208f2011-06-15 16:49:08 +0000213 typedef SkOSWindow INHERITED;
214};
215
216#endif