2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkSurface.h"
12 #include "SkSystemEventTypes.h"
15 #define SK_EventDelayInval "\xd" "n" "\xa" "l"
18 : fSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType)
22 fWaitingOnInval = false;
24 #ifdef SK_BUILD_FOR_WINCE
25 fColorType = kRGB_565_SkColorType;
27 fColorType = kN32_SkColorType;
33 SkWindow::~SkWindow() {
38 SkSurface* SkWindow::createSurface() {
39 const SkBitmap& bm = this->getBitmap();
40 return SkSurface::NewRasterDirect(bm.info(), bm.getPixels(), bm.rowBytes(), &fSurfaceProps);
43 void SkWindow::setMatrix(const SkMatrix& matrix) {
44 if (fMatrix != matrix) {
50 void SkWindow::preConcat(const SkMatrix& matrix) {
52 m.setConcat(fMatrix, matrix);
56 void SkWindow::postConcat(const SkMatrix& matrix) {
58 m.setConcat(matrix, fMatrix);
62 void SkWindow::setColorType(SkColorType ct) {
63 this->resize(fBitmap.width(), fBitmap.height(), ct);
66 void SkWindow::resize(int width, int height, SkColorType ct) {
67 if (ct == kUnknown_SkColorType)
70 if (width != fBitmap.width() || height != fBitmap.height() || ct != fColorType) {
72 fBitmap.allocPixels(SkImageInfo::Make(width, height,
73 ct, kPremul_SkAlphaType));
75 this->setSize(SkIntToScalar(width), SkIntToScalar(height));
80 bool SkWindow::handleInval(const SkRect* localR) {
86 if (!fMatrix.invert(&inverse)) {
89 fMatrix.mapRect(&devR, *localR);
93 SkScalarRoundToInt(this->width()),
94 SkScalarRoundToInt(this->height()));
96 fDirtyRgn.op(ir, SkRegion::kUnion_Op);
98 this->onHandleInval(ir);
102 void SkWindow::forceInvalAll() {
103 fDirtyRgn.setRect(0, 0,
104 SkScalarCeilToInt(this->width()),
105 SkScalarCeilToInt(this->height()));
108 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN)
111 extern GXDisplayProperties gDisplayProps;
114 #ifdef SK_SIMULATE_FAILED_MALLOC
115 extern bool gEnableControlledThrow;
118 bool SkWindow::update(SkIRect* updateArea) {
119 if (!fDirtyRgn.isEmpty()) {
120 SkBitmap bm = this->getBitmap();
122 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN)
123 char* buffer = (char*)GXBeginDraw();
127 GetWindowRect((HWND)((SkOSWindow*)this)->getHWND(), &rect);
128 buffer += rect.top * gDisplayProps.cbyPitch + rect.left * gDisplayProps.cbxPitch;
130 bm.setPixels(buffer);
133 SkAutoTUnref<SkSurface> surface(this->createSurface());
134 SkCanvas* canvas = surface->getCanvas();
136 canvas->clipRegion(fDirtyRgn);
138 *updateArea = fDirtyRgn.getBounds();
140 SkAutoCanvasRestore acr(canvas, true);
141 canvas->concat(fMatrix);
143 // empty this now, so we can correctly record any inval calls that
144 // might be made during the draw call.
145 fDirtyRgn.setEmpty();
147 #ifdef SK_SIMULATE_FAILED_MALLOC
148 gEnableControlledThrow = true;
150 #ifdef SK_BUILD_FOR_WIN32
159 #ifdef SK_SIMULATE_FAILED_MALLOC
160 gEnableControlledThrow = false;
163 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN)
172 bool SkWindow::handleChar(SkUnichar uni) {
173 if (this->onHandleChar(uni))
176 SkView* focus = this->getFocusView();
180 SkEvent evt(SK_EventType_Unichar);
182 return focus->doEvent(evt);
185 bool SkWindow::handleKey(SkKey key) {
186 if (key == kNONE_SkKey)
189 if (this->onHandleKey(key))
192 // send an event to the focus-view
194 SkView* focus = this->getFocusView();
198 SkEvent evt(SK_EventType_Key);
200 if (focus->doEvent(evt))
204 if (key == kUp_SkKey || key == kDown_SkKey) {
205 if (this->moveFocus(key == kUp_SkKey ? kPrev_FocusDirection : kNext_FocusDirection) == NULL)
206 this->onSetFocusView(NULL);
212 bool SkWindow::handleKeyUp(SkKey key) {
213 if (key == kNONE_SkKey)
216 if (this->onHandleKeyUp(key))
219 //send an event to the focus-view
221 SkView* focus = this->getFocusView();
225 //should this one be the same?
226 SkEvent evt(SK_EventType_KeyUp);
228 if (focus->doEvent(evt))
234 void SkWindow::addMenu(SkOSMenu* menu) {
235 *fMenus.append() = menu;
236 this->onAddMenu(menu);
239 void SkWindow::setTitle(const char title[]) {
244 this->onSetTitle(title);
247 bool SkWindow::onEvent(const SkEvent& evt) {
248 if (evt.isType(SK_EventDelayInval)) {
249 for (SkRegion::Iterator iter(fDirtyRgn); !iter.done(); iter.next())
250 this->onHandleInval(iter.rect());
251 fWaitingOnInval = false;
254 return this->INHERITED::onEvent(evt);
257 bool SkWindow::onGetFocusView(SkView** focus) const {
263 bool SkWindow::onSetFocusView(SkView* focus) {
264 if (fFocusView != focus) {
266 fFocusView->onFocusChange(false);
269 focus->onFocusChange(true);
274 void SkWindow::onHandleInval(const SkIRect&) {
277 bool SkWindow::onHandleChar(SkUnichar) {
281 bool SkWindow::onHandleKey(SkKey) {
285 bool SkWindow::onHandleKeyUp(SkKey) {
289 bool SkWindow::handleClick(int x, int y, Click::State state, void *owner,
290 unsigned modifierKeys) {
291 return this->onDispatchClick(x, y, state, owner, modifierKeys);
294 bool SkWindow::onDispatchClick(int x, int y, Click::State state,
295 void* owner, unsigned modifierKeys) {
296 bool handled = false;
298 // First, attempt to find an existing click with this owner.
300 for (int i = 0; i < fClicks.count(); i++) {
301 if (owner == fClicks[i]->fOwner) {
308 case Click::kDown_State: {
310 delete fClicks[index];
311 fClicks.remove(index);
313 Click* click = this->findClickHandler(SkIntToScalar(x),
314 SkIntToScalar(y), modifierKeys);
317 click->fOwner = owner;
318 *fClicks.append() = click;
319 SkView::DoClickDown(click, x, y, modifierKeys);
324 case Click::kMoved_State:
326 SkView::DoClickMoved(fClicks[index], x, y, modifierKeys);
330 case Click::kUp_State:
332 SkView::DoClickUp(fClicks[index], x, y, modifierKeys);
333 delete fClicks[index];
334 fClicks.remove(index);