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 #ifndef UI_SURFACE_TRANSPORT_DIB_H_ | 5 #ifndef UI_SURFACE_TRANSPORT_DIB_H_ |
6 #define UI_SURFACE_TRANSPORT_DIB_H_ | 6 #define UI_SURFACE_TRANSPORT_DIB_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "ui/surface/surface_export.h" | 9 #include "ui/surface/surface_export.h" |
10 | 10 |
11 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_ANDROID) | 11 #if !defined(TOOLKIT_GTK) |
12 #include "base/memory/shared_memory.h" | 12 #include "base/memory/shared_memory.h" |
13 #endif | 13 #endif |
14 | 14 |
15 #if defined(OS_WIN) | 15 #if defined(OS_WIN) |
16 #include <windows.h> | 16 #include <windows.h> |
17 #elif defined(USE_X11) | 17 #elif defined(TOOLKIT_GTK) || (defined(OS_LINUX) && defined(USE_AURA)) |
18 #include "ui/base/x/x11_util.h" | 18 #include "ui/base/x/x11_util.h" |
19 #endif | 19 #endif |
20 | 20 |
21 class SkCanvas; | 21 class SkCanvas; |
22 | 22 |
23 // ----------------------------------------------------------------------------- | 23 // ----------------------------------------------------------------------------- |
24 // A TransportDIB is a block of memory that is used to transport pixels | 24 // A TransportDIB is a block of memory that is used to transport pixels |
25 // between processes: from the renderer process to the browser, and | 25 // between processes: from the renderer process to the browser, and |
26 // between renderer and plugin processes. | 26 // between renderer and plugin processes. |
27 // ----------------------------------------------------------------------------- | 27 // ----------------------------------------------------------------------------- |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 // Returns a default, invalid handle, that is meant to indicate a missing | 73 // Returns a default, invalid handle, that is meant to indicate a missing |
74 // Transport DIB. | 74 // Transport DIB. |
75 static Handle DefaultHandleValue() { return NULL; } | 75 static Handle DefaultHandleValue() { return NULL; } |
76 | 76 |
77 // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE | 77 // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE |
78 // ACTUALLY USED AS A REAL HANDLE. | 78 // ACTUALLY USED AS A REAL HANDLE. |
79 static Handle GetFakeHandleForTest() { | 79 static Handle GetFakeHandleForTest() { |
80 static int fake_handle = 10; | 80 static int fake_handle = 10; |
81 return reinterpret_cast<Handle>(fake_handle++); | 81 return reinterpret_cast<Handle>(fake_handle++); |
82 } | 82 } |
83 #elif defined(OS_MACOSX) | 83 #elif defined(TOOLKIT_GTK) || (defined(OS_LINUX) && defined(USE_AURA)) |
84 typedef base::SharedMemoryHandle Handle; | |
85 // On Mac, the inode number of the backing file is used as an id. | |
86 typedef base::SharedMemoryId Id; | |
87 | |
88 // Returns a default, invalid handle, that is meant to indicate a missing | |
89 // Transport DIB. | |
90 static Handle DefaultHandleValue() { return Handle(); } | |
91 | |
92 // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE | |
93 // ACTUALLY USED AS A REAL HANDLE. | |
94 static Handle GetFakeHandleForTest() { | |
95 static int fake_handle = 10; | |
96 return Handle(fake_handle++, false); | |
97 } | |
98 #elif defined(USE_X11) | |
99 typedef int Handle; // These two ints are SysV IPC shared memory keys | 84 typedef int Handle; // These two ints are SysV IPC shared memory keys |
100 struct Id { | 85 struct Id { |
101 // Ensure that default initialized Ids are invalid. | 86 // Ensure that default initialized Ids are invalid. |
102 Id() : shmkey(-1) { | 87 Id() : shmkey(-1) { |
103 } | 88 } |
104 | 89 |
105 bool operator<(const Id& other) const { | 90 bool operator<(const Id& other) const { |
106 return shmkey < other.shmkey; | 91 return shmkey < other.shmkey; |
107 } | 92 } |
108 | 93 |
109 bool operator==(const Id& other) const { | 94 bool operator==(const Id& other) const { |
110 return shmkey == other.shmkey; | 95 return shmkey == other.shmkey; |
111 } | 96 } |
112 | 97 |
113 int shmkey; | 98 int shmkey; |
114 }; | 99 }; |
115 | 100 |
116 // Returns a default, invalid handle, that is meant to indicate a missing | 101 // Returns a default, invalid handle, that is meant to indicate a missing |
117 // Transport DIB. | 102 // Transport DIB. |
118 static Handle DefaultHandleValue() { return -1; } | 103 static Handle DefaultHandleValue() { return -1; } |
119 | 104 |
120 // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE | 105 // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE |
121 // ACTUALLY USED AS A REAL HANDLE. | 106 // ACTUALLY USED AS A REAL HANDLE. |
122 static Handle GetFakeHandleForTest() { | 107 static Handle GetFakeHandleForTest() { |
123 static int fake_handle = 10; | 108 static int fake_handle = 10; |
124 return fake_handle++; | 109 return fake_handle++; |
125 } | 110 } |
126 #elif defined(OS_ANDROID) | 111 #else // OS_POSIX |
127 typedef base::SharedMemoryHandle Handle; | 112 typedef base::SharedMemoryHandle Handle; |
| 113 // On POSIX, the inode number of the backing file is used as an id. |
| 114 #if defined(OS_ANDROID) |
128 typedef base::SharedMemoryHandle Id; | 115 typedef base::SharedMemoryHandle Id; |
| 116 #else |
| 117 typedef base::SharedMemoryId Id; |
| 118 #endif |
129 | 119 |
130 // Returns a default, invalid handle, that is meant to indicate a missing | 120 // Returns a default, invalid handle, that is meant to indicate a missing |
131 // Transport DIB. | 121 // Transport DIB. |
132 static Handle DefaultHandleValue() { return Handle(); } | 122 static Handle DefaultHandleValue() { return Handle(); } |
133 | 123 |
134 // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE | 124 // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE |
135 // ACTUALLY USED AS A REAL HANDLE. | 125 // ACTUALLY USED AS A REAL HANDLE. |
136 static Handle GetFakeHandleForTest() { | 126 static Handle GetFakeHandleForTest() { |
137 static int fake_handle = 10; | 127 static int fake_handle = 10; |
138 return Handle(fake_handle++, false); | 128 return Handle(fake_handle++, false); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 size_t size() const { return size_; } | 179 size_t size() const { return size_; } |
190 | 180 |
191 // Return the identifier which can be used to refer to this shared memory | 181 // Return the identifier which can be used to refer to this shared memory |
192 // on the wire. | 182 // on the wire. |
193 Id id() const; | 183 Id id() const; |
194 | 184 |
195 // Return a handle to the underlying shared memory. This can be sent over the | 185 // Return a handle to the underlying shared memory. This can be sent over the |
196 // wire to give this transport DIB to another process. | 186 // wire to give this transport DIB to another process. |
197 Handle handle() const; | 187 Handle handle() const; |
198 | 188 |
199 #if defined(USE_X11) | 189 #if defined(TOOLKIT_GTK) || (defined(OS_LINUX) && defined(USE_AURA)) |
200 // Map the shared memory into the X server and return an id for the shared | 190 // Map the shared memory into the X server and return an id for the shared |
201 // segment. | 191 // segment. |
202 XID MapToX(Display* connection); | 192 XID MapToX(Display* connection); |
203 | 193 |
204 void IncreaseInFlightCounter() { inflight_counter_++; } | 194 void IncreaseInFlightCounter() { inflight_counter_++; } |
205 // Decreases the inflight counter, and deletes the transport DIB if it is | 195 // Decreases the inflight counter, and deletes the transport DIB if it is |
206 // detached. | 196 // detached. |
207 void DecreaseInFlightCounter(); | 197 void DecreaseInFlightCounter(); |
208 | 198 |
209 // Deletes this transport DIB and detaches the shared memory once the | 199 // Deletes this transport DIB and detaches the shared memory once the |
210 // |inflight_counter_| is zero. | 200 // |inflight_counter_| is zero. |
211 void Detach(); | 201 void Detach(); |
212 #endif | 202 #endif |
213 | 203 |
214 private: | 204 private: |
215 TransportDIB(); | 205 TransportDIB(); |
216 | 206 |
217 // Verifies that the dib can hold a canvas of the requested dimensions. | 207 // Verifies that the dib can hold a canvas of the requested dimensions. |
218 bool VerifyCanvasSize(int w, int h); | 208 bool VerifyCanvasSize(int w, int h); |
219 | 209 |
220 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_ANDROID) | 210 #if defined(TOOLKIT_GTK) || (defined(OS_LINUX) && defined(USE_AURA)) |
221 explicit TransportDIB(base::SharedMemoryHandle dib); | |
222 base::SharedMemory shared_memory_; | |
223 uint32 sequence_num_; | |
224 #elif defined(USE_X11) | |
225 Id key_; // SysV shared memory id | 211 Id key_; // SysV shared memory id |
226 void* address_; // mapped address | 212 void* address_; // mapped address |
227 XSharedMemoryId x_shm_; // X id for the shared segment | 213 XSharedMemoryId x_shm_; // X id for the shared segment |
228 Display* display_; // connection to the X server | 214 Display* display_; // connection to the X server |
229 size_t inflight_counter_; // How many requests to the X server are in flight | 215 size_t inflight_counter_; // How many requests to the X server are in flight |
230 bool detached_; // If true, delete the transport DIB when it is idle | 216 bool detached_; // If true, delete the transport DIB when it is idle |
| 217 #else |
| 218 explicit TransportDIB(base::SharedMemoryHandle dib); |
| 219 base::SharedMemory shared_memory_; |
| 220 uint32 sequence_num_; |
231 #endif | 221 #endif |
232 size_t size_; // length, in bytes | 222 size_t size_; // length, in bytes |
233 | 223 |
234 DISALLOW_COPY_AND_ASSIGN(TransportDIB); | 224 DISALLOW_COPY_AND_ASSIGN(TransportDIB); |
235 }; | 225 }; |
236 | 226 |
237 #endif // UI_SURFACE_TRANSPORT_DIB_H_ | 227 #endif // UI_SURFACE_TRANSPORT_DIB_H_ |
OLD | NEW |