chromeos: bluetooth: add BluetoothInputClient
[chromium-blink-merge.git] / skia / ext / canvas_paint_win.h
blob961002522b3c6724a4c2b427e5db4b79fbf7011b
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_
7 #pragma once
9 #include "skia/ext/canvas_paint_common.h"
10 #include "skia/ext/platform_canvas.h"
12 namespace skia {
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
22 // case.
24 // Therefore, all you need to do is:
25 // case WM_PAINT: {
26 // gfx::PlatformCanvasPaint canvas(hwnd);
27 // if (!canvas.isEmpty()) {
28 // ... paint to the canvas ...
29 // }
30 // return 0;
31 // }
32 template <class T>
33 class CanvasPaintT : public T {
34 public:
35 // This constructor assumes the canvas is opaque.
36 explicit CanvasPaintT(HWND hwnd) : hwnd_(hwnd), paint_dc_(NULL),
37 for_paint_(true) {
38 memset(&ps_, 0, sizeof(ps_));
39 initPaint(true);
42 CanvasPaintT(HWND hwnd, bool opaque) : hwnd_(hwnd), paint_dc_(NULL),
43 for_paint_(true) {
44 memset(&ps_, 0, sizeof(ps_));
45 initPaint(opaque);
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)
51 : hwnd_(NULL),
52 paint_dc_(dc),
53 for_paint_(false) {
54 memset(&ps_, 0, sizeof(ps_));
55 ps_.rcPaint.left = x;
56 ps_.rcPaint.right = x + w;
57 ps_.rcPaint.top = y;
58 ps_.rcPaint.bottom = y + h;
59 init(opaque);
62 virtual ~CanvasPaintT() {
63 if (!isEmpty()) {
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);
70 if (for_paint_)
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 {
84 return ps_;
87 // Returns the DC that will be painted to
88 HDC paintDC() const {
89 return paint_dc_;
92 protected:
93 HWND hwnd_;
94 HDC paint_dc_;
95 PAINTSTRUCT ps_;
97 private:
98 void initPaint(bool opaque) {
99 paint_dc_ = BeginPaint(hwnd_, &ps_);
101 init(opaque);
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;
114 *(char*) 0 = 0;
117 // This will bring the canvas into the screen coordinate system for the
118 // dirty rect
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;
133 } // namespace skia
135 #endif // SKIA_EXT_CANVAS_PAINT_WIN_H_