OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include <errno.h> | 5 #include <errno.h> |
6 #include <stdlib.h> | 6 #include <stdlib.h> |
7 #include <sys/ipc.h> | 7 #include <sys/ipc.h> |
8 #include <sys/shm.h> | 8 #include <sys/shm.h> |
9 | 9 |
10 #include "app/surface/transport_dib.h" | 10 #include "app/surface/transport_dib.h" |
11 #include "app/x11_util.h" | 11 #include "app/x11_util.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/scoped_ptr.h" | 13 #include "base/scoped_ptr.h" |
14 #include "gfx/size.h" | 14 #include "gfx/size.h" |
15 #include "skia/ext/platform_canvas.h" | 15 #include "skia/ext/platform_canvas.h" |
16 | 16 |
17 // The shmat system call uses this as it's invalid return address | 17 // The shmat system call uses this as it's invalid return address |
18 static void *const kInvalidAddress = (void*) -1; | 18 static void *const kInvalidAddress = (void*) -1; |
19 | 19 |
| 20 void TransportDIB::ScopedHandle::Close() { |
| 21 } |
| 22 |
20 TransportDIB::TransportDIB() | 23 TransportDIB::TransportDIB() |
21 : key_(-1), | 24 : key_(-1), |
22 address_(kInvalidAddress), | 25 address_(kInvalidAddress), |
23 x_shm_(0), | 26 x_shm_(0), |
24 display_(NULL), | 27 display_(NULL), |
25 size_(0) { | 28 size_(0) { |
26 } | 29 } |
27 | 30 |
28 TransportDIB::~TransportDIB() { | 31 TransportDIB::~TransportDIB() { |
29 if (address_ != kInvalidAddress) { | 32 if (address_ != kInvalidAddress) { |
(...skipping 28 matching lines...) Expand all Loading... |
58 return NULL; | 61 return NULL; |
59 | 62 |
60 TransportDIB* dib = new TransportDIB; | 63 TransportDIB* dib = new TransportDIB; |
61 | 64 |
62 dib->key_ = shmkey; | 65 dib->key_ = shmkey; |
63 dib->address_ = address; | 66 dib->address_ = address; |
64 dib->size_ = size; | 67 dib->size_ = size; |
65 return dib; | 68 return dib; |
66 } | 69 } |
67 | 70 |
68 TransportDIB* TransportDIB::Map(Handle shmkey) { | 71 // static |
69 struct shmid_ds shmst; | 72 TransportDIB* TransportDIB::Map(Handle handle) { |
70 if (shmctl(shmkey, IPC_STAT, &shmst) == -1) | 73 scoped_ptr<TransportDIB> dib(CreateWithHandle(handle)); |
| 74 if (!dib->Map()) |
71 return NULL; | 75 return NULL; |
| 76 return dib.release(); |
| 77 } |
72 | 78 |
73 void* address = shmat(shmkey, NULL /* desired address */, 0 /* flags */); | 79 // static |
74 if (address == kInvalidAddress) | 80 TransportDIB* TransportDIB::CreateWithHandle(Handle shmkey) { |
75 return NULL; | |
76 | |
77 TransportDIB* dib = new TransportDIB; | 81 TransportDIB* dib = new TransportDIB; |
78 | |
79 dib->address_ = address; | |
80 dib->size_ = shmst.shm_segsz; | |
81 dib->key_ = shmkey; | 82 dib->key_ = shmkey; |
82 return dib; | 83 return dib; |
83 } | 84 } |
84 | 85 |
| 86 // static |
85 bool TransportDIB::is_valid(Handle dib) { | 87 bool TransportDIB::is_valid(Handle dib) { |
86 return dib >= 0; | 88 return dib >= 0; |
87 } | 89 } |
88 | 90 |
89 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { | 91 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { |
| 92 if (address_ == kInvalidAddress && !Map()) |
| 93 return NULL; |
90 scoped_ptr<skia::PlatformCanvas> canvas(new skia::PlatformCanvas); | 94 scoped_ptr<skia::PlatformCanvas> canvas(new skia::PlatformCanvas); |
91 if (!canvas->initialize(w, h, true, reinterpret_cast<uint8_t*>(memory()))) | 95 if (!canvas->initialize(w, h, true, reinterpret_cast<uint8_t*>(memory()))) |
92 return NULL; | 96 return NULL; |
93 return canvas.release(); | 97 return canvas.release(); |
94 } | 98 } |
95 | 99 |
| 100 bool TransportDIB::Map() { |
| 101 if (address_ != kInvalidAddress) |
| 102 return true; |
| 103 |
| 104 struct shmid_ds shmst; |
| 105 if (shmctl(key_, IPC_STAT, &shmst) == -1) |
| 106 return false; |
| 107 |
| 108 void* address = shmat(key_, NULL /* desired address */, 0 /* flags */); |
| 109 if (address == kInvalidAddress) |
| 110 return false; |
| 111 |
| 112 address_ = address; |
| 113 size_ = shmst.shm_segsz; |
| 114 return true; |
| 115 } |
| 116 |
| 117 TransportDIB::Handle TransportDIB::GetHandleForProcess( |
| 118 base::ProcessHandle process_handle) const { |
| 119 return handle(); |
| 120 } |
| 121 |
96 void* TransportDIB::memory() const { | 122 void* TransportDIB::memory() const { |
97 DCHECK_NE(address_, kInvalidAddress); | 123 DCHECK_NE(address_, kInvalidAddress); |
98 return address_; | 124 return address_; |
99 } | 125 } |
100 | 126 |
101 TransportDIB::Id TransportDIB::id() const { | 127 TransportDIB::Id TransportDIB::id() const { |
102 return key_; | 128 return key_; |
103 } | 129 } |
104 | 130 |
105 TransportDIB::Handle TransportDIB::handle() const { | 131 TransportDIB::Handle TransportDIB::handle() const { |
106 return key_; | 132 return key_; |
107 } | 133 } |
108 | 134 |
109 XID TransportDIB::MapToX(Display* display) { | 135 XID TransportDIB::MapToX(Display* display) { |
110 if (!x_shm_) { | 136 if (!x_shm_) { |
111 x_shm_ = x11_util::AttachSharedMemory(display, key_); | 137 x_shm_ = x11_util::AttachSharedMemory(display, key_); |
112 display_ = display; | 138 display_ = display; |
113 } | 139 } |
114 | 140 |
115 return x_shm_; | 141 return x_shm_; |
116 } | 142 } |
OLD | NEW |