OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "SkWindow.h" | 8 #include "SkWindow.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkDevice.h" | 10 #include "SkDevice.h" |
11 #include "SkOSMenu.h" | 11 #include "SkOSMenu.h" |
12 #include "SkSystemEventTypes.h" | 12 #include "SkSystemEventTypes.h" |
13 #include "SkTime.h" | 13 #include "SkTime.h" |
14 | 14 |
15 #define SK_EventDelayInval "\xd" "n" "\xa" "l" | 15 #define SK_EventDelayInval "\xd" "n" "\xa" "l" |
16 | 16 |
17 #define TEST_BOUNDERx | 17 #define TEST_BOUNDERx |
18 | 18 |
19 #include "SkBounder.h" | 19 #include "SkBounder.h" |
20 class test_bounder : public SkBounder { | 20 class TestSkBounder : public SkBounder { |
21 public: | 21 public: |
22 test_bounder(const SkBitmap& bm) : fCanvas(bm) {} | 22 explicit TestSkBounder(const SkBitmap& bm) : fCanvas(bm) {} |
| 23 |
23 protected: | 24 protected: |
24 virtual bool onIRect(const SkIRect& r) | 25 virtual bool onIRect(const SkIRect& r) SK_OVERRIDE { |
25 { | |
26 SkRect rr; | 26 SkRect rr; |
27 | 27 |
28 rr.set(SkIntToScalar(r.fLeft), SkIntToScalar(r.fTop), | 28 rr.set(SkIntToScalar(r.fLeft), SkIntToScalar(r.fTop), |
29 SkIntToScalar(r.fRight), SkIntToScalar(r.fBottom)); | 29 SkIntToScalar(r.fRight), SkIntToScalar(r.fBottom)); |
30 | 30 |
31 SkPaint p; | 31 SkPaint p; |
32 | 32 |
33 p.setStyle(SkPaint::kStroke_Style); | 33 p.setStyle(SkPaint::kStroke_Style); |
34 p.setColor(SK_ColorYELLOW); | 34 p.setColor(SK_ColorYELLOW); |
35 | 35 |
36 #if 0 | 36 #if 0 |
37 rr.inset(SK_ScalarHalf, SK_ScalarHalf); | 37 rr.inset(SK_ScalarHalf, SK_ScalarHalf); |
38 #else | 38 #else |
39 rr.inset(-SK_ScalarHalf, -SK_ScalarHalf); | 39 rr.inset(-SK_ScalarHalf, -SK_ScalarHalf); |
40 #endif | 40 #endif |
41 | 41 |
42 fCanvas.drawRect(rr, p); | 42 fCanvas.drawRect(rr, p); |
43 return true; | 43 return true; |
44 } | 44 } |
45 private: | 45 private: |
46 SkCanvas fCanvas; | 46 SkCanvas fCanvas; |
47 }; | 47 }; |
48 | 48 |
49 SkWindow::SkWindow() : fFocusView(NULL) | 49 SkWindow::SkWindow() : fFocusView(NULL) { |
50 { | |
51 fClicks.reset(); | 50 fClicks.reset(); |
52 fWaitingOnInval = false; | 51 fWaitingOnInval = false; |
53 | 52 |
54 #ifdef SK_BUILD_FOR_WINCE | 53 #ifdef SK_BUILD_FOR_WINCE |
55 fConfig = SkBitmap::kRGB_565_Config; | 54 fConfig = SkBitmap::kRGB_565_Config; |
56 #else | 55 #else |
57 fConfig = SkBitmap::kARGB_8888_Config; | 56 fConfig = SkBitmap::kARGB_8888_Config; |
58 #endif | 57 #endif |
59 | 58 |
60 fMatrix.reset(); | 59 fMatrix.reset(); |
61 } | 60 } |
62 | 61 |
63 SkWindow::~SkWindow() | 62 SkWindow::~SkWindow() { |
64 { | |
65 fClicks.deleteAll(); | 63 fClicks.deleteAll(); |
66 fMenus.deleteAll(); | 64 fMenus.deleteAll(); |
67 } | 65 } |
68 | 66 |
69 SkCanvas* SkWindow::createCanvas() { | 67 SkCanvas* SkWindow::createCanvas() { |
70 return new SkCanvas(this->getBitmap()); | 68 return new SkCanvas(this->getBitmap()); |
71 } | 69 } |
72 | 70 |
73 void SkWindow::setMatrix(const SkMatrix& matrix) { | 71 void SkWindow::setMatrix(const SkMatrix& matrix) { |
74 if (fMatrix != matrix) { | 72 if (fMatrix != matrix) { |
75 fMatrix = matrix; | 73 fMatrix = matrix; |
76 this->inval(NULL); | 74 this->inval(NULL); |
77 } | 75 } |
78 } | 76 } |
79 | 77 |
80 void SkWindow::preConcat(const SkMatrix& matrix) { | 78 void SkWindow::preConcat(const SkMatrix& matrix) { |
81 SkMatrix m; | 79 SkMatrix m; |
82 m.setConcat(fMatrix, matrix); | 80 m.setConcat(fMatrix, matrix); |
83 this->setMatrix(m); | 81 this->setMatrix(m); |
84 } | 82 } |
85 | 83 |
86 void SkWindow::postConcat(const SkMatrix& matrix) { | 84 void SkWindow::postConcat(const SkMatrix& matrix) { |
87 SkMatrix m; | 85 SkMatrix m; |
88 m.setConcat(matrix, fMatrix); | 86 m.setConcat(matrix, fMatrix); |
89 this->setMatrix(m); | 87 this->setMatrix(m); |
90 } | 88 } |
91 | 89 |
92 void SkWindow::setConfig(SkBitmap::Config config) | 90 void SkWindow::setConfig(SkBitmap::Config config) { |
93 { | |
94 this->resize(fBitmap.width(), fBitmap.height(), config); | 91 this->resize(fBitmap.width(), fBitmap.height(), config); |
95 } | 92 } |
96 | 93 |
97 void SkWindow::resize(int width, int height, SkBitmap::Config config) | 94 void SkWindow::resize(int width, int height, SkBitmap::Config config) { |
98 { | |
99 if (config == SkBitmap::kNo_Config) | 95 if (config == SkBitmap::kNo_Config) |
100 config = fConfig; | 96 config = fConfig; |
101 | 97 |
102 if (width != fBitmap.width() || height != fBitmap.height() || config != fCon
fig) | 98 if (width != fBitmap.width() || height != fBitmap.height() || config != fCon
fig) { |
103 { | |
104 fConfig = config; | 99 fConfig = config; |
105 fBitmap.setConfig(config, width, height, 0, kOpaque_SkAlphaType); | 100 fBitmap.setConfig(config, width, height, 0, kOpaque_SkAlphaType); |
106 fBitmap.allocPixels(); | 101 fBitmap.allocPixels(); |
107 | 102 |
108 this->setSize(SkIntToScalar(width), SkIntToScalar(height)); | 103 this->setSize(SkIntToScalar(width), SkIntToScalar(height)); |
109 this->inval(NULL); | 104 this->inval(NULL); |
110 } | 105 } |
111 } | 106 } |
112 | 107 |
113 void SkWindow::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) | 108 bool SkWindow::handleInval(const SkRect* localR) { |
114 { | 109 SkIRect ir; |
115 fBitmap.eraseARGB(a, r, g, b); | |
116 } | |
117 | |
118 void SkWindow::eraseRGB(U8CPU r, U8CPU g, U8CPU b) | |
119 { | |
120 fBitmap.eraseARGB(0xFF, r, g, b); | |
121 } | |
122 | |
123 bool SkWindow::handleInval(const SkRect* localR) | |
124 { | |
125 SkIRect ir; | |
126 | 110 |
127 if (localR) { | 111 if (localR) { |
128 SkRect devR; | 112 SkRect devR; |
129 SkMatrix inverse; | 113 SkMatrix inverse; |
130 if (!fMatrix.invert(&inverse)) { | 114 if (!fMatrix.invert(&inverse)) { |
131 return false; | 115 return false; |
132 } | 116 } |
133 fMatrix.mapRect(&devR, *localR); | 117 fMatrix.mapRect(&devR, *localR); |
134 devR.round(&ir); | 118 devR.round(&ir); |
135 } else { | 119 } else { |
(...skipping 16 matching lines...) Expand all Loading... |
152 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN) | 136 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN) |
153 #include <windows.h> | 137 #include <windows.h> |
154 #include <gx.h> | 138 #include <gx.h> |
155 extern GXDisplayProperties gDisplayProps; | 139 extern GXDisplayProperties gDisplayProps; |
156 #endif | 140 #endif |
157 | 141 |
158 #ifdef SK_SIMULATE_FAILED_MALLOC | 142 #ifdef SK_SIMULATE_FAILED_MALLOC |
159 extern bool gEnableControlledThrow; | 143 extern bool gEnableControlledThrow; |
160 #endif | 144 #endif |
161 | 145 |
162 bool SkWindow::update(SkIRect* updateArea) | 146 bool SkWindow::update(SkIRect* updateArea) { |
163 { | 147 if (!fDirtyRgn.isEmpty()) { |
164 if (!fDirtyRgn.isEmpty()) | |
165 { | |
166 SkBitmap bm = this->getBitmap(); | 148 SkBitmap bm = this->getBitmap(); |
167 | 149 |
168 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN) | 150 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN) |
169 char* buffer = (char*)GXBeginDraw(); | 151 char* buffer = (char*)GXBeginDraw(); |
170 SkASSERT(buffer); | 152 SkASSERT(buffer); |
171 | 153 |
172 RECT rect; | 154 RECT rect; |
173 GetWindowRect((HWND)((SkOSWindow*)this)->getHWND(), &rect); | 155 GetWindowRect((HWND)((SkOSWindow*)this)->getHWND(), &rect); |
174 buffer += rect.top * gDisplayProps.cbyPitch + rect.left * gDisplayProps.
cbxPitch; | 156 buffer += rect.top * gDisplayProps.cbyPitch + rect.left * gDisplayProps.
cbxPitch; |
175 | 157 |
176 bm.setPixels(buffer); | 158 bm.setPixels(buffer); |
177 #endif | 159 #endif |
178 | 160 |
179 SkAutoTUnref<SkCanvas> canvas(this->createCanvas()); | 161 SkAutoTUnref<SkCanvas> canvas(this->createCanvas()); |
180 | 162 |
181 canvas->clipRegion(fDirtyRgn); | 163 canvas->clipRegion(fDirtyRgn); |
182 if (updateArea) | 164 if (updateArea) |
183 *updateArea = fDirtyRgn.getBounds(); | 165 *updateArea = fDirtyRgn.getBounds(); |
184 | 166 |
185 SkAutoCanvasRestore acr(canvas, true); | 167 SkAutoCanvasRestore acr(canvas, true); |
186 canvas->concat(fMatrix); | 168 canvas->concat(fMatrix); |
187 | 169 |
188 // empty this now, so we can correctly record any inval calls that | 170 // empty this now, so we can correctly record any inval calls that |
189 // might be made during the draw call. | 171 // might be made during the draw call. |
190 fDirtyRgn.setEmpty(); | 172 fDirtyRgn.setEmpty(); |
191 | 173 |
192 #ifdef TEST_BOUNDER | 174 #ifdef TEST_BOUNDER |
193 test_bounder b(bm); | 175 TestSkBounder bounder(bm); |
194 canvas->setBounder(&b); | 176 canvas->setBounder(&bounder); |
195 #endif | 177 #endif |
196 #ifdef SK_SIMULATE_FAILED_MALLOC | 178 #ifdef SK_SIMULATE_FAILED_MALLOC |
197 gEnableControlledThrow = true; | 179 gEnableControlledThrow = true; |
198 #endif | 180 #endif |
199 #ifdef SK_BUILD_FOR_WIN32 | 181 #ifdef SK_BUILD_FOR_WIN32 |
200 //try { | 182 //try { |
201 this->draw(canvas); | 183 this->draw(canvas); |
202 //} | 184 //} |
203 //catch (...) { | 185 //catch (...) { |
204 //} | 186 //} |
205 #else | 187 #else |
206 this->draw(canvas); | 188 this->draw(canvas); |
207 #endif | 189 #endif |
208 #ifdef SK_SIMULATE_FAILED_MALLOC | 190 #ifdef SK_SIMULATE_FAILED_MALLOC |
209 gEnableControlledThrow = false; | 191 gEnableControlledThrow = false; |
210 #endif | 192 #endif |
211 #ifdef TEST_BOUNDER | 193 #ifdef TEST_BOUNDER |
212 canvas->setBounder(NULL); | 194 canvas->setBounder(NULL); |
213 #endif | 195 #endif |
214 | 196 |
215 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN) | 197 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN) |
216 GXEndDraw(); | 198 GXEndDraw(); |
217 #endif | 199 #endif |
218 | 200 |
219 return true; | 201 return true; |
220 } | 202 } |
221 return false; | 203 return false; |
222 } | 204 } |
223 | 205 |
224 bool SkWindow::handleChar(SkUnichar uni) | 206 bool SkWindow::handleChar(SkUnichar uni) { |
225 { | |
226 if (this->onHandleChar(uni)) | 207 if (this->onHandleChar(uni)) |
227 return true; | 208 return true; |
228 | 209 |
229 SkView* focus = this->getFocusView(); | 210 SkView* focus = this->getFocusView(); |
230 if (focus == NULL) | 211 if (focus == NULL) |
231 focus = this; | 212 focus = this; |
232 | 213 |
233 SkEvent evt(SK_EventType_Unichar); | 214 SkEvent evt(SK_EventType_Unichar); |
234 evt.setFast32(uni); | 215 evt.setFast32(uni); |
235 return focus->doEvent(evt); | 216 return focus->doEvent(evt); |
236 } | 217 } |
237 | 218 |
238 bool SkWindow::handleKey(SkKey key) | 219 bool SkWindow::handleKey(SkKey key) { |
239 { | |
240 if (key == kNONE_SkKey) | 220 if (key == kNONE_SkKey) |
241 return false; | 221 return false; |
242 | 222 |
243 if (this->onHandleKey(key)) | 223 if (this->onHandleKey(key)) |
244 return true; | 224 return true; |
245 | 225 |
246 // send an event to the focus-view | 226 // send an event to the focus-view |
247 { | 227 { |
248 SkView* focus = this->getFocusView(); | 228 SkView* focus = this->getFocusView(); |
249 if (focus == NULL) | 229 if (focus == NULL) |
250 focus = this; | 230 focus = this; |
251 | 231 |
252 SkEvent evt(SK_EventType_Key); | 232 SkEvent evt(SK_EventType_Key); |
253 evt.setFast32(key); | 233 evt.setFast32(key); |
254 if (focus->doEvent(evt)) | 234 if (focus->doEvent(evt)) |
255 return true; | 235 return true; |
256 } | 236 } |
257 | 237 |
258 if (key == kUp_SkKey || key == kDown_SkKey) | 238 if (key == kUp_SkKey || key == kDown_SkKey) { |
259 { | |
260 if (this->moveFocus(key == kUp_SkKey ? kPrev_FocusDirection : kNext_Focu
sDirection) == NULL) | 239 if (this->moveFocus(key == kUp_SkKey ? kPrev_FocusDirection : kNext_Focu
sDirection) == NULL) |
261 this->onSetFocusView(NULL); | 240 this->onSetFocusView(NULL); |
262 return true; | 241 return true; |
263 } | 242 } |
264 return false; | 243 return false; |
265 } | 244 } |
266 | 245 |
267 bool SkWindow::handleKeyUp(SkKey key) | 246 bool SkWindow::handleKeyUp(SkKey key) { |
268 { | |
269 if (key == kNONE_SkKey) | 247 if (key == kNONE_SkKey) |
270 return false; | 248 return false; |
271 | 249 |
272 if (this->onHandleKeyUp(key)) | 250 if (this->onHandleKeyUp(key)) |
273 return true; | 251 return true; |
274 | 252 |
275 //send an event to the focus-view | 253 //send an event to the focus-view |
276 { | 254 { |
277 SkView* focus = this->getFocusView(); | 255 SkView* focus = this->getFocusView(); |
278 if (focus == NULL) | 256 if (focus == NULL) |
(...skipping 14 matching lines...) Expand all Loading... |
293 } | 271 } |
294 | 272 |
295 void SkWindow::setTitle(const char title[]) { | 273 void SkWindow::setTitle(const char title[]) { |
296 if (NULL == title) { | 274 if (NULL == title) { |
297 title = ""; | 275 title = ""; |
298 } | 276 } |
299 fTitle.set(title); | 277 fTitle.set(title); |
300 this->onSetTitle(title); | 278 this->onSetTitle(title); |
301 } | 279 } |
302 | 280 |
303 ////////////////////////////////////////////////////////////////////// | 281 bool SkWindow::onEvent(const SkEvent& evt) { |
304 | 282 if (evt.isType(SK_EventDelayInval)) { |
305 bool SkWindow::onEvent(const SkEvent& evt) | 283 for (SkRegion::Iterator iter(fDirtyRgn); !iter.done(); iter.next()) |
306 { | |
307 if (evt.isType(SK_EventDelayInval)) | |
308 { | |
309 SkRegion::Iterator iter(fDirtyRgn); | |
310 | |
311 for (; !iter.done(); iter.next()) | |
312 this->onHandleInval(iter.rect()); | 284 this->onHandleInval(iter.rect()); |
313 fWaitingOnInval = false; | 285 fWaitingOnInval = false; |
314 return true; | 286 return true; |
315 } | 287 } |
316 return this->INHERITED::onEvent(evt); | 288 return this->INHERITED::onEvent(evt); |
317 } | 289 } |
318 | 290 |
319 bool SkWindow::onGetFocusView(SkView** focus) const | 291 bool SkWindow::onGetFocusView(SkView** focus) const { |
320 { | |
321 if (focus) | 292 if (focus) |
322 *focus = fFocusView; | 293 *focus = fFocusView; |
323 return true; | 294 return true; |
324 } | 295 } |
325 | 296 |
326 bool SkWindow::onSetFocusView(SkView* focus) | 297 bool SkWindow::onSetFocusView(SkView* focus) { |
327 { | 298 if (fFocusView != focus) { |
328 if (fFocusView != focus) | |
329 { | |
330 if (fFocusView) | 299 if (fFocusView) |
331 fFocusView->onFocusChange(false); | 300 fFocusView->onFocusChange(false); |
332 fFocusView = focus; | 301 fFocusView = focus; |
333 if (focus) | 302 if (focus) |
334 focus->onFocusChange(true); | 303 focus->onFocusChange(true); |
335 } | 304 } |
336 return true; | 305 return true; |
337 } | 306 } |
338 | 307 |
339 ////////////////////////////////////////////////////////////////////// | 308 void SkWindow::onHandleInval(const SkIRect&) { |
340 | |
341 void SkWindow::onHandleInval(const SkIRect&) | |
342 { | |
343 } | 309 } |
344 | 310 |
345 bool SkWindow::onHandleChar(SkUnichar) | 311 bool SkWindow::onHandleChar(SkUnichar) { |
346 { | |
347 return false; | 312 return false; |
348 } | 313 } |
349 | 314 |
350 bool SkWindow::onHandleKey(SkKey) | 315 bool SkWindow::onHandleKey(SkKey) { |
351 { | |
352 return false; | 316 return false; |
353 } | 317 } |
354 | 318 |
355 bool SkWindow::onHandleKeyUp(SkKey) | 319 bool SkWindow::onHandleKeyUp(SkKey) { |
356 { | |
357 return false; | 320 return false; |
358 } | 321 } |
359 | 322 |
360 bool SkWindow::handleClick(int x, int y, Click::State state, void *owner, | 323 bool SkWindow::handleClick(int x, int y, Click::State state, void *owner, |
361 unsigned modifierKeys) { | 324 unsigned modifierKeys) { |
362 return this->onDispatchClick(x, y, state, owner, modifierKeys); | 325 return this->onDispatchClick(x, y, state, owner, modifierKeys); |
363 } | 326 } |
364 | 327 |
365 bool SkWindow::onDispatchClick(int x, int y, Click::State state, | 328 bool SkWindow::onDispatchClick(int x, int y, Click::State state, |
366 void* owner, unsigned modifierKeys) { | 329 void* owner, unsigned modifierKeys) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 fClicks.remove(index); | 368 fClicks.remove(index); |
406 handled = true; | 369 handled = true; |
407 } | 370 } |
408 break; | 371 break; |
409 default: | 372 default: |
410 // Do nothing | 373 // Do nothing |
411 break; | 374 break; |
412 } | 375 } |
413 return handled; | 376 return handled; |
414 } | 377 } |
OLD | NEW |