blob: 43b979943b535ea04654e1e6cd5ce89ddaab293b [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"
21
22class GrContext;
reed@google.com29038ed2011-07-06 17:56:47 +000023class GrRenderTarget;
Scroggo2c8208f2011-06-15 16:49:08 +000024
25class SkEvent;
26class SkCanvas;
Scroggo2c8208f2011-06-15 16:49:08 +000027class SkPicture;
28class SkTypeface;
yangsu@google.com501775e2011-06-24 16:04:50 +000029class SkData;
Scroggo2c8208f2011-06-15 16:49:08 +000030
31enum SkTriState {
32 kFalse_SkTriState,
33 kTrue_SkTriState,
34 kUnknown_SkTriState,
35};
36
37class SampleWindow : public SkOSWindow {
38 SkTDArray<SkViewFactory> fSamples;
39public:
bsalomon@google.com098e96d2011-07-14 14:30:46 +000040 enum DeviceType {
41 kRaster_DeviceType,
42 kPicture_DeviceType,
43 kGPU_DeviceType
44 };
45 /**
46 * SampleApp ports can subclass this manager class if they want to:
47 * * filter the types of devices supported
48 * * customize plugging of SkDevice objects into an SkCanvas
49 * * customize publishing the results of draw to the OS window
50 * * manage GrContext / GrRenderTarget lifetimes
51 */
52 class DeviceManager : public SkRefCnt {
53 public:
54 // called at end of SampleWindow cons
55 virtual void init(SampleWindow* win) = 0;
56
57 // called when selecting a new device type
58 // can disallow a device type by returning false.
59 virtual bool supportsDeviceType(DeviceType dType) = 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
78 virtual GrContext* getGrContext() = 0;
79 };
80
81 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
Scroggo2c8208f2011-06-15 16:49:08 +000082 virtual ~SampleWindow();
83
84 virtual void draw(SkCanvas* canvas);
85
86 void toggleRendering();
87 void toggleSlideshow();
88 void toggleFPS();
bsalomon@google.com098e96d2011-07-14 14:30:46 +000089
90 GrContext* getGrContext() const { return fDevManager->getGrContext(); }
91
Scroggo2c8208f2011-06-15 16:49:08 +000092 void setZoomCenter(float x, float y);
93 void changeZoomLevel(float delta);
94 bool nextSample();
95 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +000096 bool goToSample(int i);
97 SkString getSampleTitle(int i);
98 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +000099 bool handleTouch(int ownerId, float x, float y,
100 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +0000101 void saveToPdf();
yangsu@google.com501775e2011-06-24 16:04:50 +0000102 SkData* getPDFData() { return fPDFData; }
Scroggo62b65b02011-06-21 16:01:26 +0000103 void postInvalDelay();
Scroggo2c8208f2011-06-15 16:49:08 +0000104
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
Scroggod3aed392011-06-22 13:26:56 +0000119 virtual bool onDispatchClick(int x, int y, Click::State, void* owner);
Scroggo2c8208f2011-06-15 16:49:08 +0000120 virtual bool onClick(Click* click);
121 virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
122
123private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000124 class DefaultDeviceManager;
125
Scroggo2c8208f2011-06-15 16:49:08 +0000126 int fCurrIndex;
127
128 SkPicture* fPicture;
Scroggo2c8208f2011-06-15 16:49:08 +0000129 SkPath fClipPath;
130
131 SkTouchGesture fGesture;
132 SkScalar fZoomLevel;
133 SkScalar fZoomScale;
134
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000135 DeviceType fDeviceType;
136 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000137
Scroggo8ac0d542011-06-21 14:44:57 +0000138 bool fSaveToPdf;
139 SkCanvas* fPdfCanvas;
yangsu@google.com501775e2011-06-24 16:04:50 +0000140 SkData* fPDFData;
Scroggo8ac0d542011-06-21 14:44:57 +0000141
Scroggo2c8208f2011-06-15 16:49:08 +0000142 bool fUseClip;
143 bool fNClip;
144 bool fRepeatDrawing;
145 bool fAnimating;
146 bool fRotate;
147 bool fScale;
148 bool fRequestGrabImage;
149 bool fUsePipe;
150 bool fMeasureFPS;
151 SkMSec fMeasureFPS_Time;
152
153 // The following are for the 'fatbits' drawing
154 // Latest position of the mouse.
155 int fMouseX, fMouseY;
156 int fFatBitsScale;
157 // Used by the text showing position and color values.
158 SkTypeface* fTypeface;
159 bool fShowZoomer;
160
161 SkTriState fLCDState;
162 SkTriState fAAState;
163 SkTriState fFilterState;
164 SkTriState fHintingState;
165 unsigned fFlipAxis;
166
167 int fScrollTestX, fScrollTestY;
168 SkScalar fZoomCenterX, fZoomCenterY;
169
Scroggo2c8208f2011-06-15 16:49:08 +0000170 void loadView(SkView*);
171 void updateTitle();
172
173 void toggleZoomer();
174 bool zoomIn();
175 bool zoomOut();
176 void updatePointer(int x, int y);
177 void showZoomer(SkCanvas* canvas);
178
179 void postAnimatingEvent();
180
Scroggo2c8208f2011-06-15 16:49:08 +0000181 typedef SkOSWindow INHERITED;
182};
183
184#endif