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 "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
13 #include "skia/ext/platform_canvas.h" | 13 #include "skia/ext/platform_canvas.h" |
14 #include "ui/base/x/x11_util.h" | 14 #include "ui/base/x/x11_util.h" |
15 #include "ui/gfx/size.h" | 15 #include "ui/gfx/size.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 TransportDIB::TransportDIB() | 20 TransportDIB::TransportDIB() |
21 : key_(-1), | 21 : address_(kInvalidAddress), |
22 address_(kInvalidAddress), | |
23 x_shm_(0), | 22 x_shm_(0), |
24 display_(NULL), | 23 display_(NULL), |
25 size_(0) { | 24 size_(0) { |
26 } | 25 } |
27 | 26 |
28 TransportDIB::~TransportDIB() { | 27 TransportDIB::~TransportDIB() { |
29 if (address_ != kInvalidAddress) { | 28 if (address_ != kInvalidAddress) { |
30 shmdt(address_); | 29 shmdt(address_); |
31 address_ = kInvalidAddress; | 30 address_ = kInvalidAddress; |
32 } | 31 } |
(...skipping 19 matching lines...) Expand all Loading... |
52 void* address = shmat(shmkey, NULL /* desired address */, 0 /* flags */); | 51 void* address = shmat(shmkey, NULL /* desired address */, 0 /* flags */); |
53 // Here we mark the shared memory for deletion. Since we attached it in the | 52 // Here we mark the shared memory for deletion. Since we attached it in the |
54 // line above, it doesn't actually get deleted but, if we crash, this means | 53 // line above, it doesn't actually get deleted but, if we crash, this means |
55 // that the kernel will automatically clean it up for us. | 54 // that the kernel will automatically clean it up for us. |
56 shmctl(shmkey, IPC_RMID, 0); | 55 shmctl(shmkey, IPC_RMID, 0); |
57 if (address == kInvalidAddress) | 56 if (address == kInvalidAddress) |
58 return NULL; | 57 return NULL; |
59 | 58 |
60 TransportDIB* dib = new TransportDIB; | 59 TransportDIB* dib = new TransportDIB; |
61 | 60 |
62 dib->key_ = shmkey; | 61 dib->key_.shmkey = shmkey; |
63 dib->address_ = address; | 62 dib->address_ = address; |
64 dib->size_ = size; | 63 dib->size_ = size; |
65 return dib; | 64 return dib; |
66 } | 65 } |
67 | 66 |
68 // static | 67 // static |
69 TransportDIB* TransportDIB::Map(Handle handle) { | 68 TransportDIB* TransportDIB::Map(Handle handle) { |
70 scoped_ptr<TransportDIB> dib(CreateWithHandle(handle)); | 69 scoped_ptr<TransportDIB> dib(CreateWithHandle(handle)); |
71 if (!dib->Map()) | 70 if (!dib->Map()) |
72 return NULL; | 71 return NULL; |
73 return dib.release(); | 72 return dib.release(); |
74 } | 73 } |
75 | 74 |
76 // static | 75 // static |
77 TransportDIB* TransportDIB::CreateWithHandle(Handle shmkey) { | 76 TransportDIB* TransportDIB::CreateWithHandle(Handle shmkey) { |
78 TransportDIB* dib = new TransportDIB; | 77 TransportDIB* dib = new TransportDIB; |
79 dib->key_ = shmkey; | 78 dib->key_.shmkey = shmkey; |
80 return dib; | 79 return dib; |
81 } | 80 } |
82 | 81 |
83 // static | 82 // static |
84 bool TransportDIB::is_valid(Handle dib) { | 83 bool TransportDIB::is_valid_handle(Handle dib) { |
85 return dib >= 0; | 84 return dib >= 0; |
86 } | 85 } |
87 | 86 |
| 87 // static |
| 88 bool TransportDIB::is_valid_id(Id id) { |
| 89 return id.shmkey != -1; |
| 90 } |
| 91 |
88 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { | 92 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { |
89 if (address_ == kInvalidAddress && !Map()) | 93 if (address_ == kInvalidAddress && !Map()) |
90 return NULL; | 94 return NULL; |
91 scoped_ptr<skia::PlatformCanvas> canvas(new skia::PlatformCanvas); | 95 scoped_ptr<skia::PlatformCanvas> canvas(new skia::PlatformCanvas); |
92 if (!canvas->initialize(w, h, true, reinterpret_cast<uint8_t*>(memory()))) | 96 if (!canvas->initialize(w, h, true, reinterpret_cast<uint8_t*>(memory()))) |
93 return NULL; | 97 return NULL; |
94 return canvas.release(); | 98 return canvas.release(); |
95 } | 99 } |
96 | 100 |
97 bool TransportDIB::Map() { | 101 bool TransportDIB::Map() { |
98 if (!is_valid(key_)) | 102 if (!is_valid_id(key_)) |
99 return false; | 103 return false; |
100 if (address_ != kInvalidAddress) | 104 if (address_ != kInvalidAddress) |
101 return true; | 105 return true; |
102 | 106 |
103 struct shmid_ds shmst; | 107 struct shmid_ds shmst; |
104 if (shmctl(key_, IPC_STAT, &shmst) == -1) | 108 if (shmctl(key_.shmkey, IPC_STAT, &shmst) == -1) |
105 return false; | 109 return false; |
106 | 110 |
107 void* address = shmat(key_, NULL /* desired address */, 0 /* flags */); | 111 void* address = shmat(key_.shmkey, NULL /* desired address */, 0 /* flags */); |
108 if (address == kInvalidAddress) | 112 if (address == kInvalidAddress) |
109 return false; | 113 return false; |
110 | 114 |
111 address_ = address; | 115 address_ = address; |
112 size_ = shmst.shm_segsz; | 116 size_ = shmst.shm_segsz; |
113 return true; | 117 return true; |
114 } | 118 } |
115 | 119 |
116 void* TransportDIB::memory() const { | 120 void* TransportDIB::memory() const { |
117 DCHECK_NE(address_, kInvalidAddress); | 121 DCHECK_NE(address_, kInvalidAddress); |
118 return address_; | 122 return address_; |
119 } | 123 } |
120 | 124 |
121 TransportDIB::Id TransportDIB::id() const { | 125 TransportDIB::Id TransportDIB::id() const { |
122 return key_; | 126 return key_; |
123 } | 127 } |
124 | 128 |
125 TransportDIB::Handle TransportDIB::handle() const { | 129 TransportDIB::Handle TransportDIB::handle() const { |
126 return key_; | 130 return key_.shmkey; |
127 } | 131 } |
128 | 132 |
129 XID TransportDIB::MapToX(Display* display) { | 133 XID TransportDIB::MapToX(Display* display) { |
130 if (!x_shm_) { | 134 if (!x_shm_) { |
131 x_shm_ = ui::AttachSharedMemory(display, key_); | 135 x_shm_ = ui::AttachSharedMemory(display, key_.shmkey); |
132 display_ = display; | 136 display_ = display; |
133 } | 137 } |
134 | 138 |
135 return x_shm_; | 139 return x_shm_; |
136 } | 140 } |
OLD | NEW |