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