Don't crash when SimpleCache index is corrupt.
[chromium-blink-merge.git] / ui / gfx / canvas_paint_gtk.cc
blob4c185c02e03ea515b013f13a16fb2907aed978ed
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/compiler_specific.h"
7 #include "ui/gfx/canvas.h"
8 #include "ui/gfx/canvas_skia_paint.h"
9 #include "ui/gfx/rect.h"
11 namespace gfx {
13 CanvasSkiaPaint::CanvasSkiaPaint(GdkEventExpose* event)
14 : context_(NULL),
15 window_(event->window),
16 region_(gdk_region_copy(event->region)),
17 composite_alpha_(false) {
18 Init(true);
21 CanvasSkiaPaint::CanvasSkiaPaint(GdkEventExpose* event, bool opaque)
22 : context_(NULL),
23 window_(event->window),
24 region_(gdk_region_copy(event->region)),
25 composite_alpha_(false) {
26 Init(opaque);
29 CanvasSkiaPaint::~CanvasSkiaPaint() {
30 if (!is_empty()) {
31 platform_canvas()->restoreToCount(1);
33 // Blit the dirty rect to the window.
34 CHECK(window_);
35 cairo_t* cr = gdk_cairo_create(window_);
36 CHECK(cr);
37 if (composite_alpha_)
38 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
39 cairo_surface_t* source_surface = cairo_get_target(context_);
40 CHECK(source_surface);
41 // Flush cairo's cache of the surface.
42 cairo_surface_mark_dirty(source_surface);
43 GdkRectangle bounds = rectangle();
44 cairo_set_source_surface(cr, source_surface, bounds.x, bounds.y);
45 gdk_cairo_region(cr, region_);
46 cairo_fill(cr);
47 cairo_destroy(cr);
50 gdk_region_destroy(region_);
53 void CanvasSkiaPaint::Init(bool opaque) {
54 GdkRectangle bounds = rectangle();
55 RecreateBackingCanvas(gfx::Size(bounds.width, bounds.height),
56 ui::SCALE_FACTOR_100P, opaque);
58 skia::PlatformCanvas* canvas = platform_canvas();
60 // Need to translate so that the dirty region appears at the origin of the
61 // surface.
62 canvas->translate(-SkIntToScalar(bounds.x), -SkIntToScalar(bounds.y));
64 context_ = skia::BeginPlatformPaint(canvas);
67 } // namespace gfx