Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: ui/base/x/x11_util.cc

Issue 2504743002: Linux Aura: Fixes and improvements for cursor loader (Closed)
Patch Set: Formatting Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/base/x/x11_util.h ('k') | ui/views/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // This file defines utility functions for X11 (Linux only). This code has been 5 // This file defines utility functions for X11 (Linux only). This code has been
6 // ported from XCB since we can't use XCB on Ubuntu while its 32-bit support 6 // ported from XCB since we can't use XCB on Ubuntu while its 32-bit support
7 // remains woefully incomplete. 7 // remains woefully incomplete.
8 8
9 #include "ui/base/x/x11_util.h" 9 #include "ui/base/x/x11_util.h"
10 10
11 #include <ctype.h> 11 #include <ctype.h>
12 #include <sys/ipc.h> 12 #include <sys/ipc.h>
13 #include <sys/shm.h> 13 #include <sys/shm.h>
14 #include <X11/cursorfont.h>
14 #include <X11/extensions/shape.h> 15 #include <X11/extensions/shape.h>
15 #include <X11/extensions/XInput2.h> 16 #include <X11/extensions/XInput2.h>
16 #include <X11/Xcursor/Xcursor.h> 17 #include <X11/Xcursor/Xcursor.h>
17 18
18 #include <list> 19 #include <list>
19 #include <map> 20 #include <map>
20 #include <memory> 21 #include <memory>
21 #include <utility> 22 #include <utility>
22 #include <vector> 23 #include <vector>
23 24
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 168
168 // Custom release function that will be passed to Skia so that it deletes the 169 // Custom release function that will be passed to Skia so that it deletes the
169 // image when the SkBitmap goes out of scope. 170 // image when the SkBitmap goes out of scope.
170 // |address| is the pointer to the data inside the XImage. 171 // |address| is the pointer to the data inside the XImage.
171 // |context| is the pointer to the XImage. 172 // |context| is the pointer to the XImage.
172 void ReleaseXImage(void* address, void* context) { 173 void ReleaseXImage(void* address, void* context) {
173 if (context) 174 if (context)
174 XDestroyImage(static_cast<XImage*>(context)); 175 XDestroyImage(static_cast<XImage*>(context));
175 } 176 }
176 177
177 // A process wide singleton that manages the usage of X cursors.
178 class XCursorCache {
179 public:
180 XCursorCache() {}
181 ~XCursorCache() {
182 Clear();
183 }
184
185 ::Cursor GetCursor(int cursor_shape) {
186 // Lookup cursor by attempting to insert a null value, which avoids
187 // a second pass through the map after a cache miss.
188 std::pair<std::map<int, ::Cursor>::iterator, bool> it = cache_.insert(
189 std::make_pair(cursor_shape, 0));
190 if (it.second) {
191 XDisplay* display = gfx::GetXDisplay();
192 it.first->second = XCreateFontCursor(display, cursor_shape);
193 }
194 return it.first->second;
195 }
196
197 void Clear() {
198 XDisplay* display = gfx::GetXDisplay();
199 for (std::map<int, ::Cursor>::iterator it =
200 cache_.begin(); it != cache_.end(); ++it) {
201 XFreeCursor(display, it->second);
202 }
203 cache_.clear();
204 }
205
206 private:
207 // Maps X11 font cursor shapes to Cursor IDs.
208 std::map<int, ::Cursor> cache_;
209
210 DISALLOW_COPY_AND_ASSIGN(XCursorCache);
211 };
212
213 XCursorCache* cursor_cache = NULL;
214
215 // A process wide singleton cache for custom X cursors. 178 // A process wide singleton cache for custom X cursors.
216 class XCustomCursorCache { 179 class XCustomCursorCache {
217 public: 180 public:
218 static XCustomCursorCache* GetInstance() { 181 static XCustomCursorCache* GetInstance() {
219 return base::Singleton<XCustomCursorCache>::get(); 182 return base::Singleton<XCustomCursorCache>::get();
220 } 183 }
221 184
222 ::Cursor InstallCustomCursor(XcursorImage* image) { 185 ::Cursor InstallCustomCursor(XcursorImage* image) {
223 XCustomCursor* custom_cursor = new XCustomCursor(image); 186 XCustomCursor* custom_cursor = new XCustomCursor(image);
224 ::Cursor xcursor = custom_cursor->cursor(); 187 ::Cursor xcursor = custom_cursor->cursor();
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 267
305 bool QueryRenderSupport(Display* dpy) { 268 bool QueryRenderSupport(Display* dpy) {
306 int dummy; 269 int dummy;
307 // We don't care about the version of Xrender since all the features which 270 // We don't care about the version of Xrender since all the features which
308 // we use are included in every version. 271 // we use are included in every version.
309 static bool render_supported = XRenderQueryExtension(dpy, &dummy, &dummy); 272 static bool render_supported = XRenderQueryExtension(dpy, &dummy, &dummy);
310 273
311 return render_supported; 274 return render_supported;
312 } 275 }
313 276
314 ::Cursor GetXCursor(int cursor_shape) {
315 if (!cursor_cache)
316 cursor_cache = new XCursorCache;
317 return cursor_cache->GetCursor(cursor_shape);
318 }
319
320 ::Cursor CreateReffedCustomXCursor(XcursorImage* image) { 277 ::Cursor CreateReffedCustomXCursor(XcursorImage* image) {
321 return XCustomCursorCache::GetInstance()->InstallCustomCursor(image); 278 return XCustomCursorCache::GetInstance()->InstallCustomCursor(image);
322 } 279 }
323 280
324 void RefCustomXCursor(::Cursor cursor) { 281 void RefCustomXCursor(::Cursor cursor) {
325 XCustomCursorCache::GetInstance()->Ref(cursor); 282 XCustomCursorCache::GetInstance()->Ref(cursor);
326 } 283 }
327 284
328 void UnrefCustomXCursor(::Cursor cursor) { 285 void UnrefCustomXCursor(::Cursor cursor) {
329 XCustomCursorCache::GetInstance()->Unref(cursor); 286 XCustomCursorCache::GetInstance()->Unref(cursor);
(...skipping 977 matching lines...) Expand 10 before | Expand all | Expand 10 after
1307 } 1264 }
1308 1265
1309 void XScopedCursor::reset(::Cursor cursor) { 1266 void XScopedCursor::reset(::Cursor cursor) {
1310 if (cursor_) 1267 if (cursor_)
1311 XFreeCursor(display_, cursor_); 1268 XFreeCursor(display_, cursor_);
1312 cursor_ = cursor; 1269 cursor_ = cursor;
1313 } 1270 }
1314 1271
1315 namespace test { 1272 namespace test {
1316 1273
1317 void ResetXCursorCache() {
1318 delete cursor_cache;
1319 cursor_cache = NULL;
1320 }
1321
1322 const XcursorImage* GetCachedXcursorImage(::Cursor cursor) { 1274 const XcursorImage* GetCachedXcursorImage(::Cursor cursor) {
1323 return XCustomCursorCache::GetInstance()->GetXcursorImage(cursor); 1275 return XCustomCursorCache::GetInstance()->GetXcursorImage(cursor);
1324 } 1276 }
1325 } 1277 }
1326 1278
1327 // ---------------------------------------------------------------------------- 1279 // ----------------------------------------------------------------------------
1328 // These functions are declared in x11_util_internal.h because they require 1280 // These functions are declared in x11_util_internal.h because they require
1329 // XLib.h to be included, and it conflicts with many other headers. 1281 // XLib.h to be included, and it conflicts with many other headers.
1330 XRenderPictFormat* GetRenderARGB32Format(XDisplay* dpy) { 1282 XRenderPictFormat* GetRenderARGB32Format(XDisplay* dpy) {
1331 static XRenderPictFormat* pictformat = NULL; 1283 static XRenderPictFormat* pictformat = NULL;
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
1523 return colormap_; 1475 return colormap_;
1524 } 1476 }
1525 1477
1526 #endif 1478 #endif
1527 1479
1528 // ---------------------------------------------------------------------------- 1480 // ----------------------------------------------------------------------------
1529 // End of x11_util_internal.h 1481 // End of x11_util_internal.h
1530 1482
1531 1483
1532 } // namespace ui 1484 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/x/x11_util.h ('k') | ui/views/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698