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" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 return NULL; | 58 return NULL; |
59 | 59 |
60 TransportDIB* dib = new TransportDIB; | 60 TransportDIB* dib = new TransportDIB; |
61 | 61 |
62 dib->key_ = shmkey; | 62 dib->key_ = shmkey; |
63 dib->address_ = address; | 63 dib->address_ = address; |
64 dib->size_ = size; | 64 dib->size_ = size; |
65 return dib; | 65 return dib; |
66 } | 66 } |
67 | 67 |
68 TransportDIB* TransportDIB::Map(Handle shmkey) { | 68 // static |
69 struct shmid_ds shmst; | 69 TransportDIB* TransportDIB::Map(Handle handle) { |
70 if (shmctl(shmkey, IPC_STAT, &shmst) == -1) | 70 scoped_ptr<TransportDIB> dib(CreateWithHandle(handle)); |
| 71 if (!dib->Map()) |
71 return NULL; | 72 return NULL; |
| 73 return dib.release(); |
| 74 } |
72 | 75 |
73 void* address = shmat(shmkey, NULL /* desired address */, 0 /* flags */); | 76 // static |
74 if (address == kInvalidAddress) | 77 TransportDIB* TransportDIB::CreateWithHandle(Handle shmkey) { |
75 return NULL; | |
76 | |
77 TransportDIB* dib = new TransportDIB; | 78 TransportDIB* dib = new TransportDIB; |
78 | |
79 dib->address_ = address; | |
80 dib->size_ = shmst.shm_segsz; | |
81 dib->key_ = shmkey; | 79 dib->key_ = shmkey; |
82 return dib; | 80 return dib; |
83 } | 81 } |
84 | 82 |
| 83 // static |
85 bool TransportDIB::is_valid(Handle dib) { | 84 bool TransportDIB::is_valid(Handle dib) { |
86 return dib >= 0; | 85 return dib >= 0; |
87 } | 86 } |
88 | 87 |
89 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { | 88 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { |
| 89 if (address_ == kInvalidAddress && !Map()) |
| 90 return NULL; |
90 scoped_ptr<skia::PlatformCanvas> canvas(new skia::PlatformCanvas); | 91 scoped_ptr<skia::PlatformCanvas> canvas(new skia::PlatformCanvas); |
91 if (!canvas->initialize(w, h, true, reinterpret_cast<uint8_t*>(memory()))) | 92 if (!canvas->initialize(w, h, true, reinterpret_cast<uint8_t*>(memory()))) |
92 return NULL; | 93 return NULL; |
93 return canvas.release(); | 94 return canvas.release(); |
94 } | 95 } |
95 | 96 |
| 97 bool TransportDIB::Map() { |
| 98 if (!is_valid(key_)) |
| 99 return false; |
| 100 if (address_ != kInvalidAddress) |
| 101 return true; |
| 102 |
| 103 struct shmid_ds shmst; |
| 104 if (shmctl(key_, IPC_STAT, &shmst) == -1) |
| 105 return false; |
| 106 |
| 107 void* address = shmat(key_, NULL /* desired address */, 0 /* flags */); |
| 108 if (address == kInvalidAddress) |
| 109 return false; |
| 110 |
| 111 address_ = address; |
| 112 size_ = shmst.shm_segsz; |
| 113 return true; |
| 114 } |
| 115 |
96 void* TransportDIB::memory() const { | 116 void* TransportDIB::memory() const { |
97 DCHECK_NE(address_, kInvalidAddress); | 117 DCHECK_NE(address_, kInvalidAddress); |
98 return address_; | 118 return address_; |
99 } | 119 } |
100 | 120 |
101 TransportDIB::Id TransportDIB::id() const { | 121 TransportDIB::Id TransportDIB::id() const { |
102 return key_; | 122 return key_; |
103 } | 123 } |
104 | 124 |
105 TransportDIB::Handle TransportDIB::handle() const { | 125 TransportDIB::Handle TransportDIB::handle() const { |
106 return key_; | 126 return key_; |
107 } | 127 } |
108 | 128 |
109 XID TransportDIB::MapToX(Display* display) { | 129 XID TransportDIB::MapToX(Display* display) { |
110 if (!x_shm_) { | 130 if (!x_shm_) { |
111 x_shm_ = x11_util::AttachSharedMemory(display, key_); | 131 x_shm_ = x11_util::AttachSharedMemory(display, key_); |
112 display_ = display; | 132 display_ = display; |
113 } | 133 } |
114 | 134 |
115 return x_shm_; | 135 return x_shm_; |
116 } | 136 } |
OLD | NEW |