blob: 770d6206906dd957ef8f9bd599216b45a21cd4e6 [file] [log] [blame]
Scroggo2c8208f2011-06-15 16:49:08 +00001/*
2 * Copyright (C) 2011 Skia
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SampleWindow_DEFINED
18#define SampleWindow_DEFINED
19
20#include "SkWindow.h"
21
22#include "SampleCode.h"
23#include "SkPath.h"
24#include "SkScalar.h"
25#include "SkTDArray.h"
26#include "SkTouchGesture.h"
27#include "SkWindow.h"
28
29class GrContext;
reed@google.com29038ed2011-07-06 17:56:47 +000030class GrRenderTarget;
Scroggo2c8208f2011-06-15 16:49:08 +000031
32class SkEvent;
33class SkCanvas;
Scroggo2c8208f2011-06-15 16:49:08 +000034class SkPicture;
35class SkTypeface;
yangsu@google.com501775e2011-06-24 16:04:50 +000036class SkData;
Scroggo2c8208f2011-06-15 16:49:08 +000037
38enum SkTriState {
39 kFalse_SkTriState,
40 kTrue_SkTriState,
41 kUnknown_SkTriState,
42};
43
44class SampleWindow : public SkOSWindow {
45 SkTDArray<SkViewFactory> fSamples;
46public:
bsalomon@google.com098e96d2011-07-14 14:30:46 +000047 enum DeviceType {
48 kRaster_DeviceType,
49 kPicture_DeviceType,
50 kGPU_DeviceType
51 };
52 /**
53 * SampleApp ports can subclass this manager class if they want to:
54 * * filter the types of devices supported
55 * * customize plugging of SkDevice objects into an SkCanvas
56 * * customize publishing the results of draw to the OS window
57 * * manage GrContext / GrRenderTarget lifetimes
58 */
59 class DeviceManager : public SkRefCnt {
60 public:
61 // called at end of SampleWindow cons
62 virtual void init(SampleWindow* win) = 0;
63
64 // called when selecting a new device type
65 // can disallow a device type by returning false.
66 virtual bool supportsDeviceType(DeviceType dType) = 0;
67
68 // called before drawing. should install correct device
69 // type on the canvas. Will skip drawing if returns false.
70 virtual bool prepareCanvas(DeviceType dType,
71 SkCanvas* canvas,
72 SampleWindow* win) = 0;
73
74 // called after drawing, should get the results onto the
75 // screen.
76 virtual void publishCanvas(DeviceType dType,
77 SkCanvas* canvas,
78 SampleWindow* win) = 0;
79
80 // called when window changes size, guaranteed to be called
81 // at least once before first draw (after init)
82 virtual void windowSizeChanged(SampleWindow* win) = 0;
83
84 // return the GrContext backing gpu devices
85 virtual GrContext* getGrContext() = 0;
86 };
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
93 void toggleRendering();
94 void toggleSlideshow();
95 void toggleFPS();
bsalomon@google.com098e96d2011-07-14 14:30:46 +000096
97 GrContext* getGrContext() const { return fDevManager->getGrContext(); }
98
Scroggo2c8208f2011-06-15 16:49:08 +000099 void setZoomCenter(float x, float y);
100 void changeZoomLevel(float delta);
101 bool nextSample();
102 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +0000103 bool goToSample(int i);
104 SkString getSampleTitle(int i);
105 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +0000106 bool handleTouch(int ownerId, float x, float y,
107 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +0000108 void saveToPdf();
yangsu@google.com501775e2011-06-24 16:04:50 +0000109 SkData* getPDFData() { return fPDFData; }
Scroggo62b65b02011-06-21 16:01:26 +0000110 void postInvalDelay();
Scroggo2c8208f2011-06-15 16:49:08 +0000111
112protected:
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
130private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000131 class DefaultDeviceManager;
132
Scroggo2c8208f2011-06-15 16:49:08 +0000133 int fCurrIndex;
134
135 SkPicture* fPicture;
Scroggo2c8208f2011-06-15 16:49:08 +0000136 SkPath fClipPath;
137
138 SkTouchGesture fGesture;
139 SkScalar fZoomLevel;
140 SkScalar fZoomScale;
141
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000142 DeviceType fDeviceType;
143 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000144
Scroggo8ac0d542011-06-21 14:44:57 +0000145 bool fSaveToPdf;
146 SkCanvas* fPdfCanvas;
yangsu@google.com501775e2011-06-24 16:04:50 +0000147 SkData* fPDFData;
Scroggo8ac0d542011-06-21 14:44:57 +0000148
Scroggo2c8208f2011-06-15 16:49:08 +0000149 bool fUseClip;
150 bool fNClip;
151 bool fRepeatDrawing;
152 bool fAnimating;
153 bool fRotate;
154 bool fScale;
155 bool fRequestGrabImage;
156 bool fUsePipe;
157 bool fMeasureFPS;
158 SkMSec fMeasureFPS_Time;
159
160 // The following are for the 'fatbits' drawing
161 // Latest position of the mouse.
162 int fMouseX, fMouseY;
163 int fFatBitsScale;
164 // Used by the text showing position and color values.
165 SkTypeface* fTypeface;
166 bool fShowZoomer;
167
168 SkTriState fLCDState;
169 SkTriState fAAState;
170 SkTriState fFilterState;
171 SkTriState fHintingState;
172 unsigned fFlipAxis;
173
174 int fScrollTestX, fScrollTestY;
175 SkScalar fZoomCenterX, fZoomCenterY;
176
Scroggo2c8208f2011-06-15 16:49:08 +0000177 void loadView(SkView*);
178 void updateTitle();
179
180 void toggleZoomer();
181 bool zoomIn();
182 bool zoomOut();
183 void updatePointer(int x, int y);
184 void showZoomer(SkCanvas* canvas);
185
186 void postAnimatingEvent();
187
Scroggo2c8208f2011-06-15 16:49:08 +0000188 typedef SkOSWindow INHERITED;
189};
190
191#endif