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