OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/renderer_host/backing_store_gtk.h" | 5 #include "content/browser/renderer_host/backing_store_gtk.h" |
6 | 6 |
7 #include <cairo-xlib.h> | 7 #include <cairo-xlib.h> |
8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <sys/ipc.h> | 10 #include <sys/ipc.h> |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 #include "base/compiler_specific.h" | 23 #include "base/compiler_specific.h" |
24 #include "base/logging.h" | 24 #include "base/logging.h" |
25 #include "base/memory/singleton.h" | 25 #include "base/memory/singleton.h" |
26 #include "base/metrics/histogram.h" | 26 #include "base/metrics/histogram.h" |
27 #include "base/time/time.h" | 27 #include "base/time/time.h" |
28 #include "content/browser/renderer_host/render_process_host_impl.h" | 28 #include "content/browser/renderer_host/render_process_host_impl.h" |
29 #include "skia/ext/platform_canvas.h" | 29 #include "skia/ext/platform_canvas.h" |
30 #include "third_party/skia/include/core/SkBitmap.h" | 30 #include "third_party/skia/include/core/SkBitmap.h" |
31 #include "ui/base/gtk/gtk_signal.h" | 31 #include "ui/base/gtk/gtk_signal.h" |
32 #include "ui/base/x/x11_util.h" | |
33 #include "ui/base/x/x11_util_internal.h" | 32 #include "ui/base/x/x11_util_internal.h" |
34 #include "ui/gfx/rect.h" | 33 #include "ui/gfx/rect.h" |
35 #include "ui/gfx/rect_conversions.h" | 34 #include "ui/gfx/rect_conversions.h" |
| 35 #include "ui/gfx/x/x11_types.h" |
36 #include "ui/surface/transport_dib.h" | 36 #include "ui/surface/transport_dib.h" |
37 | 37 |
38 namespace content { | 38 namespace content { |
39 namespace { | 39 namespace { |
40 | 40 |
41 // Assume that somewhere along the line, someone will do width * height * 4 | 41 // Assume that somewhere along the line, someone will do width * height * 4 |
42 // with signed numbers. If the maximum value is 2**31, then 2**31 / 4 = | 42 // with signed numbers. If the maximum value is 2**31, then 2**31 / 4 = |
43 // 2**29 and floor(sqrt(2**29)) = 23170. | 43 // 2**29 and floor(sqrt(2**29)) = 23170. |
44 | 44 |
45 // Max height and width for layers | 45 // Max height and width for layers |
46 static const int kMaxVideoLayerSize = 23170; | 46 static const int kMaxVideoLayerSize = 23170; |
47 | 47 |
48 | 48 |
49 // X Backing Stores: | 49 // X Backing Stores: |
50 // | 50 // |
51 // Unlike Windows, where the backing store is kept in heap memory, we keep our | 51 // Unlike Windows, where the backing store is kept in heap memory, we keep our |
52 // backing store in the X server, as a pixmap. Thus expose events just require | 52 // backing store in the X server, as a pixmap. Thus expose events just require |
53 // instructing the X server to copy from the backing store to the window. | 53 // instructing the X server to copy from the backing store to the window. |
54 // | 54 // |
55 // The backing store is in the same format as the visual which our main window | 55 // The backing store is in the same format as the visual which our main window |
56 // is using. Bitmaps from the renderer are uploaded to the X server, either via | 56 // is using. Bitmaps from the renderer are uploaded to the X server, either via |
57 // shared memory or over the wire, and XRENDER is used to convert them to the | 57 // shared memory or over the wire, and XRENDER is used to convert them to the |
58 // correct format for the backing store. | 58 // correct format for the backing store. |
59 | 59 |
60 // Destroys the image and the associated shared memory structures. This is a | 60 // Destroys the image and the associated shared memory structures. This is a |
61 // helper function for code using shared memory. | 61 // helper function for code using shared memory. |
62 void DestroySharedImage(Display* display, | 62 void DestroySharedImage(XDisplay* display, |
63 XImage* image, | 63 XImage* image, |
64 XShmSegmentInfo* shminfo) { | 64 XShmSegmentInfo* shminfo) { |
65 XShmDetach(display, shminfo); | 65 XShmDetach(display, shminfo); |
66 XDestroyImage(image); | 66 XDestroyImage(image); |
67 shmdt(shminfo->shmaddr); | 67 shmdt(shminfo->shmaddr); |
68 } | 68 } |
69 | 69 |
70 // So we don't don't want to call XSync(), which can block the UI loop for | 70 // So we don't don't want to call XSync(), which can block the UI loop for |
71 // ~100ms on first paint and is generally slow. We optionally use the | 71 // ~100ms on first paint and is generally slow. We optionally use the |
72 // XSyncExtension to push a callback into the X11 event queue and get a | 72 // XSyncExtension to push a callback into the X11 event queue and get a |
73 // callback instead of blocking until the event queue is cleared. | 73 // callback instead of blocking until the event queue is cleared. |
74 // | 74 // |
75 // TODO(erg): If ui::GetXDisplay() ever gets fixed to handle multiple Displays, | 75 // TODO(erg): If gfx::GetXDisplay() ever gets fixed to handle multiple Displays, |
76 // this must be modified to be per Display instead of a Singleton. | 76 // this must be modified to be per Display instead of a Singleton. |
77 class XSyncHandler { | 77 class XSyncHandler { |
78 public: | 78 public: |
79 static XSyncHandler* GetInstance() { | 79 static XSyncHandler* GetInstance() { |
80 return Singleton<XSyncHandler>::get(); | 80 return Singleton<XSyncHandler>::get(); |
81 } | 81 } |
82 | 82 |
83 bool Enabled() { | 83 bool Enabled() { |
84 return loaded_extension_; | 84 return loaded_extension_; |
85 } | 85 } |
86 | 86 |
87 void PushPaintCounter(TransportDIB* dib, | 87 void PushPaintCounter(TransportDIB* dib, |
88 Display* display, | 88 XDisplay* display, |
89 Picture picture, | 89 Picture picture, |
90 Pixmap pixmap, | 90 Pixmap pixmap, |
91 const base::Closure& completion_callback); | 91 const base::Closure& completion_callback); |
92 | 92 |
93 private: | 93 private: |
94 friend struct DefaultSingletonTraits<XSyncHandler>; | 94 friend struct DefaultSingletonTraits<XSyncHandler>; |
95 | 95 |
96 // A struct that has cleanup and callback tasks that were queued into the | 96 // A struct that has cleanup and callback tasks that were queued into the |
97 // future and are run on |g_backing_store_sync_alarm| firing. | 97 // future and are run on |g_backing_store_sync_alarm| firing. |
98 struct BackingStoreEvents { | 98 struct BackingStoreEvents { |
99 BackingStoreEvents(TransportDIB* dib, Display* d, Picture pic, Pixmap pix, | 99 BackingStoreEvents(TransportDIB* dib, XDisplay* d, Picture pic, Pixmap pix, |
100 const base::Closure& c) | 100 const base::Closure& c) |
101 : dib(dib), | 101 : dib(dib), |
102 display(d), | 102 display(d), |
103 picture(pic), | 103 picture(pic), |
104 pixmap(pix), | 104 pixmap(pix), |
105 closure(c) { | 105 closure(c) { |
106 dib->IncreaseInFlightCounter(); | 106 dib->IncreaseInFlightCounter(); |
107 } | 107 } |
108 | 108 |
109 TransportDIB* dib; | 109 TransportDIB* dib; |
110 | 110 |
111 // The display we're running on. | 111 // The display we're running on. |
112 Display* display; | 112 XDisplay* display; |
113 | 113 |
114 // Data to delete. | 114 // Data to delete. |
115 Picture picture; | 115 Picture picture; |
116 Pixmap pixmap; | 116 Pixmap pixmap; |
117 | 117 |
118 // Callback once everything else is done. | 118 // Callback once everything else is done. |
119 base::Closure closure; | 119 base::Closure closure; |
120 }; | 120 }; |
121 | 121 |
122 XSyncHandler(); | 122 XSyncHandler(); |
(...skipping 12 matching lines...) Expand all Loading... |
135 int xsync_error_base_; | 135 int xsync_error_base_; |
136 | 136 |
137 XSyncCounter backing_store_sync_counter_; | 137 XSyncCounter backing_store_sync_counter_; |
138 XSyncAlarm backing_store_sync_alarm_; | 138 XSyncAlarm backing_store_sync_alarm_; |
139 | 139 |
140 // A queue of pending paints that we clean up after as alarms fire. | 140 // A queue of pending paints that we clean up after as alarms fire. |
141 std::queue<BackingStoreEvents*> backing_store_events_; | 141 std::queue<BackingStoreEvents*> backing_store_events_; |
142 }; | 142 }; |
143 | 143 |
144 void XSyncHandler::PushPaintCounter(TransportDIB* dib, | 144 void XSyncHandler::PushPaintCounter(TransportDIB* dib, |
145 Display* display, | 145 XDisplay* display, |
146 Picture picture, | 146 Picture picture, |
147 Pixmap pixmap, | 147 Pixmap pixmap, |
148 const base::Closure& completion_callback) { | 148 const base::Closure& completion_callback) { |
149 backing_store_events_.push(new BackingStoreEvents( | 149 backing_store_events_.push(new BackingStoreEvents( |
150 dib, display, picture, pixmap, completion_callback)); | 150 dib, display, picture, pixmap, completion_callback)); |
151 | 151 |
152 // Push a change counter event into the X11 event queue that will trigger our | 152 // Push a change counter event into the X11 event queue that will trigger our |
153 // alarm when it is processed. | 153 // alarm when it is processed. |
154 XSyncValue value; | 154 XSyncValue value; |
155 XSyncIntToValue(&value, 1); | 155 XSyncIntToValue(&value, 1); |
156 XSyncChangeCounter(ui::GetXDisplay(), | 156 XSyncChangeCounter(gfx::GetXDisplay(), |
157 backing_store_sync_counter_, | 157 backing_store_sync_counter_, |
158 value); | 158 value); |
159 } | 159 } |
160 | 160 |
161 XSyncHandler::XSyncHandler() | 161 XSyncHandler::XSyncHandler() |
162 : loaded_extension_(false), | 162 : loaded_extension_(false), |
163 xsync_event_base_(0), | 163 xsync_event_base_(0), |
164 xsync_error_base_(0), | 164 xsync_error_base_(0), |
165 backing_store_sync_counter_(0), | 165 backing_store_sync_counter_(0), |
166 backing_store_sync_alarm_(0) { | 166 backing_store_sync_alarm_(0) { |
167 Display* display = ui::GetXDisplay(); | 167 XDisplay* display = gfx::GetXDisplay(); |
168 if (XSyncQueryExtension(display, | 168 if (XSyncQueryExtension(display, |
169 &xsync_event_base_, | 169 &xsync_event_base_, |
170 &xsync_error_base_)) { | 170 &xsync_error_base_)) { |
171 // Create our monotonically increasing counter. | 171 // Create our monotonically increasing counter. |
172 XSyncValue value; | 172 XSyncValue value; |
173 XSyncIntToValue(&value, 0); | 173 XSyncIntToValue(&value, 0); |
174 backing_store_sync_counter_ = XSyncCreateCounter(display, value); | 174 backing_store_sync_counter_ = XSyncCreateCounter(display, value); |
175 | 175 |
176 // Cerate our alarm that watches for changes to our counter. | 176 // Cerate our alarm that watches for changes to our counter. |
177 XSyncAlarmAttributes attributes; | 177 XSyncAlarmAttributes attributes; |
178 attributes.trigger.counter = backing_store_sync_counter_; | 178 attributes.trigger.counter = backing_store_sync_counter_; |
179 backing_store_sync_alarm_ = XSyncCreateAlarm(display, | 179 backing_store_sync_alarm_ = XSyncCreateAlarm(display, |
180 XSyncCACounter, | 180 XSyncCACounter, |
181 &attributes); | 181 &attributes); |
182 | 182 |
183 // Add our filter to the message loop to handle alarm triggers. | 183 // Add our filter to the message loop to handle alarm triggers. |
184 gdk_window_add_filter(NULL, &OnEventThunk, this); | 184 gdk_window_add_filter(NULL, &OnEventThunk, this); |
185 | 185 |
186 loaded_extension_ = true; | 186 loaded_extension_ = true; |
187 } | 187 } |
188 } | 188 } |
189 | 189 |
190 XSyncHandler::~XSyncHandler() { | 190 XSyncHandler::~XSyncHandler() { |
191 if (loaded_extension_) | 191 if (loaded_extension_) |
192 gdk_window_remove_filter(NULL, &OnEventThunk, this); | 192 gdk_window_remove_filter(NULL, &OnEventThunk, this); |
193 | 193 |
194 XSync(ui::GetXDisplay(), False); | 194 XSync(gfx::GetXDisplay(), False); |
195 while (!backing_store_events_.empty()) { | 195 while (!backing_store_events_.empty()) { |
196 // We delete the X11 resources we're holding onto. We don't run the | 196 // We delete the X11 resources we're holding onto. We don't run the |
197 // callbacks because we are shutting down. | 197 // callbacks because we are shutting down. |
198 BackingStoreEvents* data = backing_store_events_.front(); | 198 BackingStoreEvents* data = backing_store_events_.front(); |
199 backing_store_events_.pop(); | 199 backing_store_events_.pop(); |
200 XRenderFreePicture(data->display, data->picture); | 200 XRenderFreePicture(data->display, data->picture); |
201 XFreePixmap(data->display, data->pixmap); | 201 XFreePixmap(data->display, data->pixmap); |
202 data->dib->DecreaseInFlightCounter(); | 202 data->dib->DecreaseInFlightCounter(); |
203 delete data; | 203 delete data; |
204 } | 204 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 return GDK_FILTER_CONTINUE; | 241 return GDK_FILTER_CONTINUE; |
242 } | 242 } |
243 | 243 |
244 } // namespace | 244 } // namespace |
245 | 245 |
246 BackingStoreGtk::BackingStoreGtk(RenderWidgetHost* widget, | 246 BackingStoreGtk::BackingStoreGtk(RenderWidgetHost* widget, |
247 const gfx::Size& size, | 247 const gfx::Size& size, |
248 void* visual, | 248 void* visual, |
249 int depth) | 249 int depth) |
250 : BackingStore(widget, size), | 250 : BackingStore(widget, size), |
251 display_(ui::GetXDisplay()), | 251 display_(gfx::GetXDisplay()), |
252 shared_memory_support_(ui::QuerySharedMemorySupport(display_)), | 252 shared_memory_support_(ui::QuerySharedMemorySupport(display_)), |
253 use_render_(ui::QueryRenderSupport(display_)), | 253 use_render_(ui::QueryRenderSupport(display_)), |
254 visual_(visual), | 254 visual_(visual), |
255 visual_depth_(depth), | 255 visual_depth_(depth), |
256 root_window_(ui::GetX11RootWindow()) { | 256 root_window_(ui::GetX11RootWindow()) { |
257 #if defined(OS_OPENBSD) || defined(OS_FREEBSD) | 257 #if defined(OS_OPENBSD) || defined(OS_FREEBSD) |
258 COMPILE_ASSERT(_BYTE_ORDER == _LITTLE_ENDIAN, assumes_little_endian); | 258 COMPILE_ASSERT(_BYTE_ORDER == _LITTLE_ENDIAN, assumes_little_endian); |
259 #else | 259 #else |
260 COMPILE_ASSERT(__BYTE_ORDER == __LITTLE_ENDIAN, assumes_little_endian); | 260 COMPILE_ASSERT(__BYTE_ORDER == __LITTLE_ENDIAN, assumes_little_endian); |
261 #endif | 261 #endif |
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 | 683 |
684 cairo_identity_matrix(cr); | 684 cairo_identity_matrix(cr); |
685 | 685 |
686 cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height()); | 686 cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height()); |
687 cairo_fill(cr); | 687 cairo_fill(cr); |
688 cairo_destroy(cr); | 688 cairo_destroy(cr); |
689 } | 689 } |
690 #endif | 690 #endif |
691 | 691 |
692 } // namespace content | 692 } // namespace content |
OLD | NEW |