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
3 // found in the LICENSE file.
5 #ifndef UI_SURFACE_TRANSPORT_DIB_H_
6 #define UI_SURFACE_TRANSPORT_DIB_H_
8 #include "base/basictypes.h"
9 #include "ui/surface/surface_export.h"
11 #if !defined(TOOLKIT_GTK)
12 #include "base/memory/shared_memory.h"
17 #elif defined(TOOLKIT_GTK)
18 #include "ui/base/x/x11_util.h"
19 #include "ui/gfx/x/x11_types.h"
24 // -----------------------------------------------------------------------------
25 // A TransportDIB is a block of memory that is used to transport pixels
26 // between processes: from the renderer process to the browser, and
27 // between renderer and plugin processes.
28 // -----------------------------------------------------------------------------
29 class SURFACE_EXPORT TransportDIB
{
33 // Two typedefs are defined. A Handle is the type which can be sent over
34 // the wire so that the remote side can map the transport DIB. The Id typedef
35 // is sufficient to identify the transport DIB when you know that the remote
36 // side already may have it mapped.
38 typedef HANDLE Handle
;
39 // On Windows, the Id type includes a sequence number (epoch) to solve an ABA
41 // 1) Process A creates a transport DIB with HANDLE=1 and sends to B.
42 // 2) Process B maps the transport DIB and caches 1 -> DIB.
43 // 3) Process A closes the transport DIB and creates a new one. The new DIB
44 // is also assigned HANDLE=1.
45 // 4) Process A sends the Handle to B, but B incorrectly believes that it
46 // already has it cached.
47 struct HandleAndSequenceNum
{
48 HandleAndSequenceNum()
53 HandleAndSequenceNum(HANDLE h
, uint32 seq_num
)
55 sequence_num(seq_num
) {
58 bool operator==(const HandleAndSequenceNum
& other
) const {
59 return other
.handle
== handle
&& other
.sequence_num
== sequence_num
;
62 bool operator<(const HandleAndSequenceNum
& other
) const {
63 // Use the lexicographic order on the tuple <handle, sequence_num>.
64 if (other
.handle
!= handle
)
65 return other
.handle
< handle
;
66 return other
.sequence_num
< sequence_num
;
72 typedef HandleAndSequenceNum Id
;
74 // Returns a default, invalid handle, that is meant to indicate a missing
76 static Handle
DefaultHandleValue() { return NULL
; }
78 // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE
79 // ACTUALLY USED AS A REAL HANDLE.
80 static Handle
GetFakeHandleForTest() {
81 static int fake_handle
= 10;
82 return reinterpret_cast<Handle
>(fake_handle
++);
84 #elif defined(TOOLKIT_GTK)
85 typedef int Handle
; // These two ints are SysV IPC shared memory keys
87 // Ensure that default initialized Ids are invalid.
91 bool operator<(const Id
& other
) const {
92 return shmkey
< other
.shmkey
;
95 bool operator==(const Id
& other
) const {
96 return shmkey
== other
.shmkey
;
102 // Returns a default, invalid handle, that is meant to indicate a missing
104 static Handle
DefaultHandleValue() { return -1; }
106 // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE
107 // ACTUALLY USED AS A REAL HANDLE.
108 static Handle
GetFakeHandleForTest() {
109 static int fake_handle
= 10;
110 return fake_handle
++;
113 typedef base::SharedMemoryHandle Handle
;
114 // On POSIX, the inode number of the backing file is used as an id.
115 #if defined(OS_ANDROID)
116 typedef base::SharedMemoryHandle Id
;
118 typedef base::SharedMemoryId Id
;
121 // Returns a default, invalid handle, that is meant to indicate a missing
123 static Handle
DefaultHandleValue() { return Handle(); }
125 // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE
126 // ACTUALLY USED AS A REAL HANDLE.
127 static Handle
GetFakeHandleForTest() {
128 static int fake_handle
= 10;
129 return Handle(fake_handle
++, false);
133 // Create a new TransportDIB, returning NULL on failure.
135 // The size is the minimum size in bytes of the memory backing the transport
136 // DIB (we may actually allocate more than that to give us better reuse when
139 // The sequence number is used to uniquely identify the transport DIB. It
140 // should be unique for all transport DIBs ever created in the same
142 static TransportDIB
* Create(size_t size
, uint32 sequence_num
);
144 // Map the referenced transport DIB. The caller owns the returned object.
145 // Returns NULL on failure.
146 static TransportDIB
* Map(Handle transport_dib
);
148 // Create a new |TransportDIB| with a handle to the shared memory. This
149 // always returns a valid pointer. The DIB is not mapped.
150 static TransportDIB
* CreateWithHandle(Handle handle
);
152 // Returns true if the handle is valid.
153 static bool is_valid_handle(Handle dib
);
155 // Returns true if the ID refers to a valid dib.
156 static bool is_valid_id(Id id
);
158 // Returns a canvas using the memory of this TransportDIB. The returned
159 // pointer will be owned by the caller. The bitmap will be of the given size,
160 // which should fit inside this memory.
162 // On POSIX, this |TransportDIB| will be mapped if not already. On Windows,
163 // this |TransportDIB| will NOT be mapped and should not be mapped prior,
164 // because PlatformCanvas will map the file internally.
166 // Will return NULL on allocation failure. This could be because the image
167 // is too large to map into the current process' address space.
168 SkCanvas
* GetPlatformCanvas(int w
, int h
);
170 // Map the DIB into the current process if it is not already. This is used to
171 // map a DIB that has already been created. Returns true if the DIB is mapped.
174 // Return a pointer to the shared memory.
175 void* memory() const;
177 // Return the maximum size of the shared memory. This is not the amount of
178 // data which is valid, you have to know that via other means, this is simply
179 // the maximum amount that /could/ be valid.
180 size_t size() const { return size_
; }
182 // Return the identifier which can be used to refer to this shared memory
186 // Return a handle to the underlying shared memory. This can be sent over the
187 // wire to give this transport DIB to another process.
188 Handle
handle() const;
190 #if defined(TOOLKIT_GTK)
191 // Map the shared memory into the X server and return an id for the shared
193 XID
MapToX(XDisplay
* connection
);
195 void IncreaseInFlightCounter() { inflight_counter_
++; }
196 // Decreases the inflight counter, and deletes the transport DIB if it is
198 void DecreaseInFlightCounter();
200 // Deletes this transport DIB and detaches the shared memory once the
201 // |inflight_counter_| is zero.
208 // Verifies that the dib can hold a canvas of the requested dimensions.
209 bool VerifyCanvasSize(int w
, int h
);
211 #if defined(TOOLKIT_GTK)
212 Id key_
; // SysV shared memory id
213 void* address_
; // mapped address
214 XSharedMemoryId x_shm_
; // X id for the shared segment
215 XDisplay
* display_
; // connection to the X server
216 size_t inflight_counter_
; // How many requests to the X server are in flight
217 bool detached_
; // If true, delete the transport DIB when it is idle
219 explicit TransportDIB(base::SharedMemoryHandle dib
);
220 base::SharedMemory shared_memory_
;
221 uint32 sequence_num_
;
223 size_t size_
; // length, in bytes
225 DISALLOW_COPY_AND_ASSIGN(TransportDIB
);
228 #endif // UI_SURFACE_TRANSPORT_DIB_H_