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; |
128 typedef base::SharedMemoryHandle Id; | 113 // On POSIX, the inode number of the backing file is used as an id. |
| 114 typedef base::SharedMemoryId Id; |
129 | 115 |
130 // Returns a default, invalid handle, that is meant to indicate a missing | 116 // Returns a default, invalid handle, that is meant to indicate a missing |
131 // Transport DIB. | 117 // Transport DIB. |
132 static Handle DefaultHandleValue() { return Handle(); } | 118 static Handle DefaultHandleValue() { return Handle(); } |
133 | 119 |
134 // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE | 120 // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE |
135 // ACTUALLY USED AS A REAL HANDLE. | 121 // ACTUALLY USED AS A REAL HANDLE. |
136 static Handle GetFakeHandleForTest() { | 122 static Handle GetFakeHandleForTest() { |
137 static int fake_handle = 10; | 123 static int fake_handle = 10; |
138 return Handle(fake_handle++, false); | 124 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_; } | 175 size_t size() const { return size_; } |
190 | 176 |
191 // Return the identifier which can be used to refer to this shared memory | 177 // Return the identifier which can be used to refer to this shared memory |
192 // on the wire. | 178 // on the wire. |
193 Id id() const; | 179 Id id() const; |
194 | 180 |
195 // Return a handle to the underlying shared memory. This can be sent over the | 181 // Return a handle to the underlying shared memory. This can be sent over the |
196 // wire to give this transport DIB to another process. | 182 // wire to give this transport DIB to another process. |
197 Handle handle() const; | 183 Handle handle() const; |
198 | 184 |
199 #if defined(USE_X11) | 185 #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 | 186 // Map the shared memory into the X server and return an id for the shared |
201 // segment. | 187 // segment. |
202 XID MapToX(Display* connection); | 188 XID MapToX(Display* connection); |
203 | 189 |
204 void IncreaseInFlightCounter() { inflight_counter_++; } | 190 void IncreaseInFlightCounter() { inflight_counter_++; } |
205 // Decreases the inflight counter, and deletes the transport DIB if it is | 191 // Decreases the inflight counter, and deletes the transport DIB if it is |
206 // detached. | 192 // detached. |
207 void DecreaseInFlightCounter(); | 193 void DecreaseInFlightCounter(); |
208 | 194 |
209 // Deletes this transport DIB and detaches the shared memory once the | 195 // Deletes this transport DIB and detaches the shared memory once the |
210 // |inflight_counter_| is zero. | 196 // |inflight_counter_| is zero. |
211 void Detach(); | 197 void Detach(); |
212 #endif | 198 #endif |
213 | 199 |
214 private: | 200 private: |
215 TransportDIB(); | 201 TransportDIB(); |
216 | 202 |
217 // Verifies that the dib can hold a canvas of the requested dimensions. | 203 // Verifies that the dib can hold a canvas of the requested dimensions. |
218 bool VerifyCanvasSize(int w, int h); | 204 bool VerifyCanvasSize(int w, int h); |
219 | 205 |
220 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_ANDROID) | 206 #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 | 207 Id key_; // SysV shared memory id |
226 void* address_; // mapped address | 208 void* address_; // mapped address |
227 XSharedMemoryId x_shm_; // X id for the shared segment | 209 XSharedMemoryId x_shm_; // X id for the shared segment |
228 Display* display_; // connection to the X server | 210 Display* display_; // connection to the X server |
229 size_t inflight_counter_; // How many requests to the X server are in flight | 211 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 | 212 bool detached_; // If true, delete the transport DIB when it is idle |
| 213 #else |
| 214 explicit TransportDIB(base::SharedMemoryHandle dib); |
| 215 base::SharedMemory shared_memory_; |
| 216 uint32 sequence_num_; |
231 #endif | 217 #endif |
232 size_t size_; // length, in bytes | 218 size_t size_; // length, in bytes |
233 | 219 |
234 DISALLOW_COPY_AND_ASSIGN(TransportDIB); | 220 DISALLOW_COPY_AND_ASSIGN(TransportDIB); |
235 }; | 221 }; |
236 | 222 |
237 #endif // UI_SURFACE_TRANSPORT_DIB_H_ | 223 #endif // UI_SURFACE_TRANSPORT_DIB_H_ |
OLD | NEW |