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. 4 5 #ifndef UI_SURFACE_TRANSPORT_DIB_H_ 6 #define UI_SURFACE_TRANSPORT_DIB_H_ 7 8 #include "base/basictypes.h" 9 #include "ui/surface/surface_export.h" 10 11 #if !defined(TOOLKIT_GTK) 12 #include "base/memory/shared_memory.h" 13 #endif 14 15 #if defined(OS_WIN) 16 #include <windows.h> 17 #elif defined(TOOLKIT_GTK) 18 #include "ui/base/x/x11_util.h" 19 #endif 20 21 class SkCanvas; 22 23 // ----------------------------------------------------------------------------- 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 26 // between renderer and plugin processes. 27 // ----------------------------------------------------------------------------- 28 class SURFACE_EXPORT TransportDIB { 29 public: 30 ~TransportDIB(); 31 32 // Two typedefs are defined. A Handle is the type which can be sent over 33 // the wire so that the remote side can map the transport DIB. The Id typedef 34 // is sufficient to identify the transport DIB when you know that the remote 35 // side already may have it mapped. 36 #if defined(OS_WIN) 37 typedef HANDLE Handle; 38 // On Windows, the Id type includes a sequence number (epoch) to solve an ABA 39 // issue: 40 // 1) Process A creates a transport DIB with HANDLE=1 and sends to B. 41 // 2) Process B maps the transport DIB and caches 1 -> DIB. 42 // 3) Process A closes the transport DIB and creates a new one. The new DIB 43 // is also assigned HANDLE=1. 44 // 4) Process A sends the Handle to B, but B incorrectly believes that it 45 // already has it cached. 46 struct HandleAndSequenceNum { 47 HandleAndSequenceNum() 48 : handle(NULL), 49 sequence_num(0) { 50 } 51 52 HandleAndSequenceNum(HANDLE h, uint32 seq_num) 53 : handle(h), 54 sequence_num(seq_num) { 55 } 56 57 bool operator==(const HandleAndSequenceNum& other) const { 58 return other.handle == handle && other.sequence_num == sequence_num; 59 } 60 61 bool operator<(const HandleAndSequenceNum& other) const { 62 // Use the lexicographic order on the tuple <handle, sequence_num>. 63 if (other.handle != handle) 64 return other.handle < handle; 65 return other.sequence_num < sequence_num; 66 } 67 68 HANDLE handle; 69 uint32 sequence_num; 70 }; 71 typedef HandleAndSequenceNum Id; 72 73 // Returns a default, invalid handle, that is meant to indicate a missing 74 // Transport DIB. 75 static Handle DefaultHandleValue() { return NULL; } 76 77 // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE 78 // ACTUALLY USED AS A REAL HANDLE. 79 static Handle GetFakeHandleForTest() { 80 static int fake_handle = 10; 81 return reinterpret_cast<Handle>(fake_handle++); 82 } 83 #elif defined(TOOLKIT_GTK) 84 typedef int Handle; // These two ints are SysV IPC shared memory keys 85 struct Id { 86 // Ensure that default initialized Ids are invalid. 87 Id() : shmkey(-1) { 88 } 89 90 bool operator<(const Id& other) const { 91 return shmkey < other.shmkey; 92 } 93 94 bool operator==(const Id& other) const { 95 return shmkey == other.shmkey; 96 } 97 98 int shmkey; 99 }; 100 101 // Returns a default, invalid handle, that is meant to indicate a missing 102 // Transport DIB. 103 static Handle DefaultHandleValue() { return -1; } 104 105 // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE 106 // ACTUALLY USED AS A REAL HANDLE. 107 static Handle GetFakeHandleForTest() { 108 static int fake_handle = 10; 109 return fake_handle++; 110 } 111 #else // OS_POSIX 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) 115 typedef base::SharedMemoryHandle Id; 116 #else 117 typedef base::SharedMemoryId Id; 118 #endif 119 120 // Returns a default, invalid handle, that is meant to indicate a missing 121 // Transport DIB. 122 static Handle DefaultHandleValue() { return Handle(); } 123 124 // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE 125 // ACTUALLY USED AS A REAL HANDLE. 126 static Handle GetFakeHandleForTest() { 127 static int fake_handle = 10; 128 return Handle(fake_handle++, false); 129 } 130 #endif 131 132 // Create a new TransportDIB, returning NULL on failure. 133 // 134 // The size is the minimum size in bytes of the memory backing the transport 135 // DIB (we may actually allocate more than that to give us better reuse when 136 // cached). 137 // 138 // The sequence number is used to uniquely identify the transport DIB. It 139 // should be unique for all transport DIBs ever created in the same 140 // renderer. 141 static TransportDIB* Create(size_t size, uint32 sequence_num); 142 143 // Map the referenced transport DIB. The caller owns the returned object. 144 // Returns NULL on failure. 145 static TransportDIB* Map(Handle transport_dib); 146 147 // Create a new |TransportDIB| with a handle to the shared memory. This 148 // always returns a valid pointer. The DIB is not mapped. 149 static TransportDIB* CreateWithHandle(Handle handle); 150 151 // Returns true if the handle is valid. 152 static bool is_valid_handle(Handle dib); 153 154 // Returns true if the ID refers to a valid dib. 155 static bool is_valid_id(Id id); 156 157 // Returns a canvas using the memory of this TransportDIB. The returned 158 // pointer will be owned by the caller. The bitmap will be of the given size, 159 // which should fit inside this memory. 160 // 161 // On POSIX, this |TransportDIB| will be mapped if not already. On Windows, 162 // this |TransportDIB| will NOT be mapped and should not be mapped prior, 163 // because PlatformCanvas will map the file internally. 164 // 165 // Will return NULL on allocation failure. This could be because the image 166 // is too large to map into the current process' address space. 167 SkCanvas* GetPlatformCanvas(int w, int h); 168 169 // Map the DIB into the current process if it is not already. This is used to 170 // map a DIB that has already been created. Returns true if the DIB is mapped. 171 bool Map(); 172 173 // Return a pointer to the shared memory. 174 void* memory() const; 175 176 // Return the maximum size of the shared memory. This is not the amount of 177 // data which is valid, you have to know that via other means, this is simply 178 // the maximum amount that /could/ be valid. 179 size_t size() const { return size_; } 180 181 // Return the identifier which can be used to refer to this shared memory 182 // on the wire. 183 Id id() const; 184 185 // Return a handle to the underlying shared memory. This can be sent over the 186 // wire to give this transport DIB to another process. 187 Handle handle() const; 188 189 #if defined(TOOLKIT_GTK) 190 // Map the shared memory into the X server and return an id for the shared 191 // segment. 192 XID MapToX(Display* connection); 193 194 void IncreaseInFlightCounter() { inflight_counter_++; } 195 // Decreases the inflight counter, and deletes the transport DIB if it is 196 // detached. 197 void DecreaseInFlightCounter(); 198 199 // Deletes this transport DIB and detaches the shared memory once the 200 // |inflight_counter_| is zero. 201 void Detach(); 202 #endif 203 204 private: 205 TransportDIB(); 206 207 // Verifies that the dib can hold a canvas of the requested dimensions. 208 bool VerifyCanvasSize(int w, int h); 209 210 #if defined(TOOLKIT_GTK) 211 Id key_; // SysV shared memory id 212 void* address_; // mapped address 213 XSharedMemoryId x_shm_; // X id for the shared segment 214 Display* display_; // connection to the X server 215 size_t inflight_counter_; // How many requests to the X server are in flight 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_; 221 #endif 222 size_t size_; // length, in bytes 223 224 DISALLOW_COPY_AND_ASSIGN(TransportDIB); 225 }; 226 227 #endif // UI_SURFACE_TRANSPORT_DIB_H_ 228