1 // Copyright (c) 2011 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 #ifndef UI_GFX_CANVAS_PAINT_WIN_H_
6 #define UI_GFX_CANVAS_PAINT_WIN_H_
8 #include "skia/ext/platform_canvas.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/geometry/size.h"
11 #include "ui/gfx/win/dpi.h"
15 // A class designed to help with WM_PAINT operations on Windows. It will create
16 // the bitmap and canvas with the correct size and transform for the dirty rect.
17 // The bitmap will be automatically painted to the screen on destruction.
19 // You MUST call isEmpty before painting to determine if anything needs
20 // painting. Sometimes the dirty rect can actually be empty, and this makes
21 // the bitmap functions we call unhappy. The caller should not paint in this
24 // Therefore, all you need to do is:
27 // HDC hdc = BeginPaint(hwnd, &ps);
28 // gfx::CanvasSkiaPaint canvas(hwnd, hdc, ps);
29 // if (!canvas.isEmpty()) {
30 // ... paint to the canvas ...
32 // EndPaint(hwnd, &ps);
35 // Note: The created context is always inialized to (0, 0, 0, 0).
36 class GFX_EXPORT CanvasSkiaPaint
: public Canvas
{
38 // This constructor assumes the canvas is opaque.
39 CanvasSkiaPaint(HWND hwnd
, HDC dc
, const PAINTSTRUCT
& ps
);
40 ~CanvasSkiaPaint() override
;
42 // Creates a CanvasSkiaPaint for the specified region that paints to the
44 CanvasSkiaPaint(HDC dc
, bool opaque
, int x
, int y
, int w
, int h
);
46 // Returns the rectangle that is invalid.
47 virtual gfx::Rect
GetInvalidRect() const;
49 // Returns true if the invalid region is empty. The caller should call this
50 // function to determine if anything needs painting.
51 bool is_empty() const {
52 return ps_
.rcPaint
.right
- ps_
.rcPaint
.left
== 0 ||
53 ps_
.rcPaint
.bottom
- ps_
.rcPaint
.top
== 0;
56 // Use to access the Windows painting parameters, especially useful for
57 // getting the bounding rect for painting: paintstruct().rcPaint
58 const PAINTSTRUCT
& paint_struct() const { return ps_
; }
60 // Returns the DC that will be painted to
61 HDC
paint_dc() const { return paint_dc_
; }
64 void Init(bool opaque
);
70 // Disallow copy and assign.
71 DISALLOW_COPY_AND_ASSIGN(CanvasSkiaPaint
);
76 #endif // UI_GFX_CANVAS_PAINT_WIN_H_