summaryrefslogtreecommitdiff
path: root/chromium/ui/gfx/canvas_paint_win.cc
blob: 71c836724a20051e06ad9cad8f1c4b9f8ff414ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/canvas_skia_paint.h"
#include "ui/gfx/rect.h"

namespace gfx {

CanvasSkiaPaint::CanvasSkiaPaint(HWND hwnd, HDC dc, const PAINTSTRUCT& ps)
    : hwnd_(hwnd),
      paint_dc_(dc) {
  memset(&ps_, 0, sizeof(ps_));
  ps_.rcPaint.left = ps.rcPaint.left;
  ps_.rcPaint.right = ps.rcPaint.right;
  ps_.rcPaint.top = ps.rcPaint.top;
  ps_.rcPaint.bottom = ps.rcPaint.bottom;
  Init(true);
}

CanvasSkiaPaint::CanvasSkiaPaint(HDC dc, bool opaque, int x, int y,
                                 int w, int h)
    : hwnd_(NULL),
      paint_dc_(dc) {
  memset(&ps_, 0, sizeof(ps_));
  ps_.rcPaint.left = x;
  ps_.rcPaint.right = x + w;
  ps_.rcPaint.top = y;
  ps_.rcPaint.bottom = y + h;
  Init(opaque);
}

CanvasSkiaPaint::~CanvasSkiaPaint() {
  if (!is_empty()) {
    skia::PlatformCanvas* canvas = platform_canvas();
    canvas->restoreToCount(1);
    // Commit the drawing to the screen
    skia::DrawToNativeContext(canvas, paint_dc_, ps_.rcPaint.left,
                              ps_.rcPaint.top, NULL);
  }
}

gfx::Rect CanvasSkiaPaint::GetInvalidRect() const {
  return gfx::Rect(paint_struct().rcPaint);
}

void CanvasSkiaPaint::Init(bool opaque) {
  // FIXME(brettw) for ClearType, we probably want to expand the bounds of
  // painting by one pixel so that the boundaries will be correct (ClearType
  // text can depend on the adjacent pixel). Then we would paint just the
  // inset pixels to the screen.
  const int width = ps_.rcPaint.right - ps_.rcPaint.left;
  const int height = ps_.rcPaint.bottom - ps_.rcPaint.top;

  RecreateBackingCanvas(gfx::Size(width, height),
      ui::GetScaleFactorFromScale(gfx::win::GetDeviceScaleFactor()),
      opaque);
  skia::PlatformCanvas* canvas = platform_canvas();

  canvas->clear(SkColorSetARGB(0, 0, 0, 0));

  // This will bring the canvas into the screen coordinate system for the
  // dirty rect
  canvas->translate(
      -ps_.rcPaint.left / gfx::win::GetDeviceScaleFactor(),
      -ps_.rcPaint.top / gfx::win::GetDeviceScaleFactor());
}

}  // namespace gfx