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 SKIA_EXT_CANVAS_PAINT_WIN_H_
6 #define SKIA_EXT_CANVAS_PAINT_WIN_H_
9 #include "skia/ext/canvas_paint_common.h"
10 #include "skia/ext/platform_canvas.h"
14 // A class designed to help with WM_PAINT operations on Windows. It will
15 // do BeginPaint/EndPaint on init/destruction, and will create the bitmap and
16 // canvas with the correct size and transform for the dirty rect. The bitmap
17 // 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:
26 // gfx::PlatformCanvasPaint canvas(hwnd);
27 // if (!canvas.isEmpty()) {
28 // ... paint to the canvas ...
33 class CanvasPaintT
: public T
{
35 // This constructor assumes the canvas is opaque.
36 explicit CanvasPaintT(HWND hwnd
) : hwnd_(hwnd
), paint_dc_(NULL
),
38 memset(&ps_
, 0, sizeof(ps_
));
42 CanvasPaintT(HWND hwnd
, bool opaque
) : hwnd_(hwnd
), paint_dc_(NULL
),
44 memset(&ps_
, 0, sizeof(ps_
));
48 // Creates a CanvasPaintT for the specified region that paints to the
49 // specified dc. This does NOT do BeginPaint/EndPaint.
50 CanvasPaintT(HDC dc
, bool opaque
, int x
, int y
, int w
, int h
)
54 memset(&ps_
, 0, sizeof(ps_
));
56 ps_
.rcPaint
.right
= x
+ w
;
58 ps_
.rcPaint
.bottom
= y
+ h
;
62 virtual ~CanvasPaintT() {
64 PlatformCanvas
* canvas
= GetPlatformCanvas(this);
65 canvas
->restoreToCount(1);
66 // Commit the drawing to the screen
67 skia::DrawToNativeContext(canvas
, paint_dc_
, ps_
.rcPaint
.left
,
68 ps_
.rcPaint
.top
, NULL
);
71 EndPaint(hwnd_
, &ps_
);
74 // Returns true if the invalid region is empty. The caller should call this
75 // function to determine if anything needs painting.
76 bool isEmpty() const {
77 return ps_
.rcPaint
.right
- ps_
.rcPaint
.left
== 0 ||
78 ps_
.rcPaint
.bottom
- ps_
.rcPaint
.top
== 0;
81 // Use to access the Windows painting parameters, especially useful for
82 // getting the bounding rect for painting: paintstruct().rcPaint
83 const PAINTSTRUCT
& paintStruct() const {
87 // Returns the DC that will be painted to
98 void initPaint(bool opaque
) {
99 paint_dc_
= BeginPaint(hwnd_
, &ps_
);
104 void init(bool opaque
) {
105 PlatformCanvas
* canvas
= GetPlatformCanvas(this);
106 // FIXME(brettw) for ClearType, we probably want to expand the bounds of
107 // painting by one pixel so that the boundaries will be correct (ClearType
108 // text can depend on the adjacent pixel). Then we would paint just the
109 // inset pixels to the screen.
110 const int width
= ps_
.rcPaint
.right
- ps_
.rcPaint
.left
;
111 const int height
= ps_
.rcPaint
.bottom
- ps_
.rcPaint
.top
;
112 if (!canvas
->initialize(width
, height
, opaque
, NULL
)) {
113 // Cause a deliberate crash;
117 // This will bring the canvas into the screen coordinate system for the
119 canvas
->translate(SkIntToScalar(-ps_
.rcPaint
.left
),
120 SkIntToScalar(-ps_
.rcPaint
.top
));
123 // If true, this canvas was created for a BeginPaint.
124 const bool for_paint_
;
126 // Disallow copy and assign.
127 CanvasPaintT(const CanvasPaintT
&);
128 CanvasPaintT
& operator=(const CanvasPaintT
&);
131 typedef CanvasPaintT
<PlatformCanvas
> PlatformCanvasPaint
;
135 #endif // SKIA_EXT_CANVAS_PAINT_WIN_H_