OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/renderer_host/backing_store.h" | 5 #include "chrome/browser/renderer_host/backing_store.h" |
6 | 6 |
7 #include "base/gfx/gdi_util.h" | 7 #include "base/gfx/gdi_util.h" |
8 #include "chrome/browser/renderer_host/render_widget_host.h" | 8 #include "chrome/browser/renderer_host/render_widget_host.h" |
9 #include "chrome/common/transport_dib.h" | 9 #include "chrome/common/transport_dib.h" |
10 | 10 |
| 11 namespace { |
| 12 |
| 13 // Creates a dib conforming to the height/width/section parameters passed in. |
| 14 HANDLE CreateDIB(HDC dc, int width, int height, int color_depth) { |
| 15 BITMAPINFOHEADER hdr; |
| 16 gfx::CreateBitmapHeaderWithColorDepth(width, height, color_depth, &hdr); |
| 17 void* data = NULL; |
| 18 HANDLE dib = CreateDIBSection(dc, reinterpret_cast<BITMAPINFO*>(&hdr), |
| 19 0, &data, NULL, 0); |
| 20 DCHECK(data); |
| 21 return dib; |
| 22 } |
| 23 |
| 24 } // namespace |
| 25 |
11 // BackingStore (Windows) ------------------------------------------------------ | 26 // BackingStore (Windows) ------------------------------------------------------ |
12 | 27 |
13 BackingStore::BackingStore(const gfx::Size& size) | 28 BackingStore::BackingStore(const gfx::Size& size) |
14 : size_(size), | 29 : size_(size), |
15 backing_store_dib_(NULL), | 30 backing_store_dib_(NULL), |
16 original_bitmap_(NULL) { | 31 original_bitmap_(NULL) { |
17 HDC screen_dc = ::GetDC(NULL); | 32 HDC screen_dc = ::GetDC(NULL); |
| 33 color_depth_ = ::GetDeviceCaps(screen_dc, BITSPIXEL); |
| 34 // Color depths less than 16 bpp require a palette to be specified. Instead, |
| 35 // we specify the desired color depth as 16 which lets the OS to come up |
| 36 // with an approximation. |
| 37 if (color_depth_ < 16) |
| 38 color_depth_ = 16; |
18 hdc_ = CreateCompatibleDC(screen_dc); | 39 hdc_ = CreateCompatibleDC(screen_dc); |
19 ReleaseDC(NULL, screen_dc); | 40 ReleaseDC(NULL, screen_dc); |
20 } | 41 } |
21 | 42 |
22 BackingStore::~BackingStore() { | 43 BackingStore::~BackingStore() { |
23 DCHECK(hdc_); | 44 DCHECK(hdc_); |
24 | 45 if (original_bitmap_) { |
25 DeleteDC(hdc_); | 46 SelectObject(hdc_, original_bitmap_); |
26 | 47 } |
27 if (backing_store_dib_) { | 48 if (backing_store_dib_) { |
28 DeleteObject(backing_store_dib_); | 49 DeleteObject(backing_store_dib_); |
29 backing_store_dib_ = NULL; | 50 backing_store_dib_ = NULL; |
30 } | 51 } |
| 52 DeleteDC(hdc_); |
31 } | 53 } |
32 | 54 |
33 void BackingStore::PaintRect(base::ProcessHandle process, | 55 void BackingStore::PaintRect(base::ProcessHandle process, |
34 TransportDIB* bitmap, | 56 TransportDIB* bitmap, |
35 const gfx::Rect& bitmap_rect) { | 57 const gfx::Rect& bitmap_rect) { |
36 if (!backing_store_dib_) { | 58 if (!backing_store_dib_) { |
37 backing_store_dib_ = CreateDIB(hdc_, size_.width(), size_.height(), true, | 59 backing_store_dib_ = CreateDIB(hdc_, size_.width(), |
38 NULL); | 60 size_.height(), color_depth_); |
39 DCHECK(backing_store_dib_ != NULL); | 61 if (!backing_store_dib_) { |
| 62 NOTREACHED(); |
| 63 return; |
| 64 } |
40 original_bitmap_ = SelectObject(hdc_, backing_store_dib_); | 65 original_bitmap_ = SelectObject(hdc_, backing_store_dib_); |
41 } | 66 } |
42 | 67 |
43 // TODO(darin): protect against integer overflow | |
44 DWORD size = 4 * bitmap_rect.width() * bitmap_rect.height(); | |
45 | |
46 // These values are shared with gfx::PlatformDevice | |
47 BITMAPINFOHEADER hdr; | 68 BITMAPINFOHEADER hdr; |
48 gfx::CreateBitmapHeader(bitmap_rect.width(), bitmap_rect.height(), &hdr); | 69 gfx::CreateBitmapHeader(bitmap_rect.width(), bitmap_rect.height(), &hdr); |
49 // Account for a bitmap_rect that exceeds the bounds of our view | 70 // Account for a bitmap_rect that exceeds the bounds of our view |
50 gfx::Rect view_rect(0, 0, size_.width(), size_.height()); | 71 gfx::Rect view_rect(0, 0, size_.width(), size_.height()); |
51 gfx::Rect paint_rect = view_rect.Intersect(bitmap_rect); | 72 gfx::Rect paint_rect = view_rect.Intersect(bitmap_rect); |
52 | 73 |
53 StretchDIBits(hdc_, | 74 int rv = StretchDIBits(hdc_, |
54 paint_rect.x(), | 75 paint_rect.x(), paint_rect.y(), |
55 paint_rect.y(), | 76 paint_rect.width(), paint_rect.height(), |
56 paint_rect.width(), | 77 0, 0, // source x,y. |
57 paint_rect.height(), | 78 paint_rect.width(), paint_rect.height(), |
58 0, 0, // source x,y | 79 bitmap->memory(), |
59 paint_rect.width(), | 80 reinterpret_cast<BITMAPINFO*>(&hdr), |
60 paint_rect.height(), | 81 DIB_RGB_COLORS, SRCCOPY); |
61 bitmap->memory(), | 82 DCHECK(rv != GDI_ERROR); |
62 reinterpret_cast<BITMAPINFO*>(&hdr), | |
63 DIB_RGB_COLORS, | |
64 SRCCOPY); | |
65 } | 83 } |
66 | 84 |
67 void BackingStore::ScrollRect(base::ProcessHandle process, | 85 void BackingStore::ScrollRect(base::ProcessHandle process, |
68 TransportDIB* bitmap, | 86 TransportDIB* bitmap, |
69 const gfx::Rect& bitmap_rect, | 87 const gfx::Rect& bitmap_rect, |
70 int dx, int dy, | 88 int dx, int dy, |
71 const gfx::Rect& clip_rect, | 89 const gfx::Rect& clip_rect, |
72 const gfx::Size& view_size) { | 90 const gfx::Size& view_size) { |
73 RECT damaged_rect, r = clip_rect.ToRECT(); | 91 RECT damaged_rect, r = clip_rect.ToRECT(); |
74 ScrollDC(hdc_, dx, dy, NULL, &r, NULL, &damaged_rect); | 92 ScrollDC(hdc_, dx, dy, NULL, &r, NULL, &damaged_rect); |
75 | 93 |
76 // TODO(darin): this doesn't work if dx and dy are both non-zero! | 94 // TODO(darin): this doesn't work if dx and dy are both non-zero! |
77 DCHECK(dx == 0 || dy == 0); | 95 DCHECK(dx == 0 || dy == 0); |
78 | 96 |
79 // We expect that damaged_rect should equal bitmap_rect. | 97 // We expect that damaged_rect should equal bitmap_rect. |
80 DCHECK(gfx::Rect(damaged_rect) == bitmap_rect); | 98 DCHECK(gfx::Rect(damaged_rect) == bitmap_rect); |
81 | 99 |
82 PaintRect(process, bitmap, bitmap_rect); | 100 PaintRect(process, bitmap, bitmap_rect); |
83 } | 101 } |
84 | |
85 HANDLE BackingStore::CreateDIB(HDC dc, | |
86 int width, int height, | |
87 bool use_system_color_depth, | |
88 HANDLE section) { | |
89 BITMAPINFOHEADER hdr; | |
90 | |
91 if (use_system_color_depth) { | |
92 HDC screen_dc = ::GetDC(NULL); | |
93 int color_depth = GetDeviceCaps(screen_dc, BITSPIXEL); | |
94 ::ReleaseDC(NULL, screen_dc); | |
95 | |
96 // Color depths less than 16 bpp require a palette to be specified in the | |
97 // BITMAPINFO structure passed to CreateDIBSection. Instead of creating | |
98 // the palette, we specify the desired color depth as 16 which allows the | |
99 // OS to come up with an approximation. Tested this with 8bpp. | |
100 if (color_depth < 16) | |
101 color_depth = 16; | |
102 | |
103 gfx::CreateBitmapHeaderWithColorDepth(width, height, color_depth, &hdr); | |
104 } else { | |
105 gfx::CreateBitmapHeader(width, height, &hdr); | |
106 } | |
107 void* data = NULL; | |
108 HANDLE dib = | |
109 CreateDIBSection(hdc_, reinterpret_cast<BITMAPINFO*>(&hdr), | |
110 0, &data, section, 0); | |
111 return dib; | |
112 } | |
OLD | NEW |