Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 29 | class GrContext; |
reed@google.com | 29038ed | 2011-07-06 17:56:47 +0000 | [diff] [blame] | 30 | class GrRenderTarget; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 31 | |
| 32 | class SkEvent; |
| 33 | class SkCanvas; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 34 | class SkPicture; |
| 35 | class SkTypeface; |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame] | 36 | class SkData; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 37 | |
| 38 | enum SkTriState { |
| 39 | kFalse_SkTriState, |
| 40 | kTrue_SkTriState, |
| 41 | kUnknown_SkTriState, |
| 42 | }; |
| 43 | |
| 44 | class SampleWindow : public SkOSWindow { |
| 45 | SkTDArray<SkViewFactory> fSamples; |
| 46 | public: |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame^] | 47 | 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*); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 89 | virtual ~SampleWindow(); |
| 90 | |
| 91 | virtual void draw(SkCanvas* canvas); |
| 92 | |
| 93 | void toggleRendering(); |
| 94 | void toggleSlideshow(); |
| 95 | void toggleFPS(); |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame^] | 96 | |
| 97 | GrContext* getGrContext() const { return fDevManager->getGrContext(); } |
| 98 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 99 | void setZoomCenter(float x, float y); |
| 100 | void changeZoomLevel(float delta); |
| 101 | bool nextSample(); |
| 102 | bool previousSample(); |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame] | 103 | bool goToSample(int i); |
| 104 | SkString getSampleTitle(int i); |
| 105 | int sampleCount(); |
Scroggo | a54e2f6 | 2011-06-17 12:46:17 +0000 | [diff] [blame] | 106 | bool handleTouch(int ownerId, float x, float y, |
| 107 | SkView::Click::State state); |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 108 | void saveToPdf(); |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame] | 109 | SkData* getPDFData() { return fPDFData; } |
Scroggo | 62b65b0 | 2011-06-21 16:01:26 +0000 | [diff] [blame] | 110 | void postInvalDelay(); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 111 | |
| 112 | protected: |
| 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 | |
Scroggo | d3aed39 | 2011-06-22 13:26:56 +0000 | [diff] [blame] | 126 | virtual bool onDispatchClick(int x, int y, Click::State, void* owner); |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 127 | virtual bool onClick(Click* click); |
| 128 | virtual Click* onFindClickHandler(SkScalar x, SkScalar y); |
| 129 | |
| 130 | private: |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame^] | 131 | class DefaultDeviceManager; |
| 132 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 133 | int fCurrIndex; |
| 134 | |
| 135 | SkPicture* fPicture; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 136 | SkPath fClipPath; |
| 137 | |
| 138 | SkTouchGesture fGesture; |
| 139 | SkScalar fZoomLevel; |
| 140 | SkScalar fZoomScale; |
| 141 | |
bsalomon@google.com | 098e96d | 2011-07-14 14:30:46 +0000 | [diff] [blame^] | 142 | DeviceType fDeviceType; |
| 143 | DeviceManager* fDevManager; |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 144 | |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 145 | bool fSaveToPdf; |
| 146 | SkCanvas* fPdfCanvas; |
yangsu@google.com | 501775e | 2011-06-24 16:04:50 +0000 | [diff] [blame] | 147 | SkData* fPDFData; |
Scroggo | 8ac0d54 | 2011-06-21 14:44:57 +0000 | [diff] [blame] | 148 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 149 | 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 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 177 | 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 | |
Scroggo | 2c8208f | 2011-06-15 16:49:08 +0000 | [diff] [blame] | 188 | typedef SkOSWindow INHERITED; |
| 189 | }; |
| 190 | |
| 191 | #endif |