gfx/skia/trunk/src/views/SkWindow.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial