OLD | NEW |
| (Empty) |
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 #include "ui/surface/transport_dib.h" | |
6 | |
7 // Desktop GTK Linux builds use the old-style SYSV SHM based DIBs. | |
8 | |
9 #include <errno.h> | |
10 #include <stdlib.h> | |
11 #include <sys/ipc.h> | |
12 #include <sys/shm.h> | |
13 | |
14 #include "base/logging.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "skia/ext/platform_canvas.h" | |
17 #include "ui/base/x/x11_util.h" | |
18 #include "ui/gfx/size.h" | |
19 | |
20 #error "The GTK+ port will be deleted later this week. If you are seeing this, y
ou are trying to compile it. Please check your gyp flags for 'use_aura=0' and re
move them." | |
21 | |
22 // The shmat system call uses this as it's invalid return address | |
23 static void *const kInvalidAddress = (void*) -1; | |
24 | |
25 TransportDIB::TransportDIB() | |
26 : address_(kInvalidAddress), | |
27 x_shm_(0), | |
28 display_(NULL), | |
29 inflight_counter_(0), | |
30 detached_(false), | |
31 size_(0) { | |
32 } | |
33 | |
34 TransportDIB::~TransportDIB() { | |
35 if (address_ != kInvalidAddress) { | |
36 shmdt(address_); | |
37 address_ = kInvalidAddress; | |
38 } | |
39 | |
40 if (x_shm_) { | |
41 DCHECK(display_); | |
42 ui::DetachSharedMemory(display_, x_shm_); | |
43 } | |
44 } | |
45 | |
46 // static | |
47 TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) { | |
48 const int shmkey = shmget(IPC_PRIVATE, size, 0600); | |
49 if (shmkey == -1) { | |
50 DLOG(ERROR) << "Failed to create SysV shared memory region" | |
51 << " errno:" << errno; | |
52 return NULL; | |
53 } else { | |
54 VLOG(1) << "Created SysV shared memory region " << shmkey; | |
55 } | |
56 | |
57 void* address = shmat(shmkey, NULL /* desired address */, 0 /* flags */); | |
58 // Here we mark the shared memory for deletion. Since we attached it in the | |
59 // line above, it doesn't actually get deleted but, if we crash, this means | |
60 // that the kernel will automatically clean it up for us. | |
61 shmctl(shmkey, IPC_RMID, 0); | |
62 if (address == kInvalidAddress) | |
63 return NULL; | |
64 | |
65 TransportDIB* dib = new TransportDIB; | |
66 | |
67 dib->key_.shmkey = shmkey; | |
68 dib->address_ = address; | |
69 dib->size_ = size; | |
70 return dib; | |
71 } | |
72 | |
73 // static | |
74 TransportDIB* TransportDIB::Map(Handle handle) { | |
75 scoped_ptr<TransportDIB> dib(CreateWithHandle(handle)); | |
76 if (!dib->Map()) | |
77 return NULL; | |
78 return dib.release(); | |
79 } | |
80 | |
81 // static | |
82 TransportDIB* TransportDIB::CreateWithHandle(Handle shmkey) { | |
83 TransportDIB* dib = new TransportDIB; | |
84 dib->key_.shmkey = shmkey; | |
85 return dib; | |
86 } | |
87 | |
88 // static | |
89 bool TransportDIB::is_valid_handle(Handle dib) { | |
90 return dib >= 0; | |
91 } | |
92 | |
93 // static | |
94 bool TransportDIB::is_valid_id(Id id) { | |
95 return id.shmkey != -1; | |
96 } | |
97 | |
98 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { | |
99 if ((address_ == kInvalidAddress && !Map()) || !VerifyCanvasSize(w, h)) | |
100 return NULL; | |
101 return skia::CreatePlatformCanvas(w, h, true, | |
102 reinterpret_cast<uint8_t*>(memory()), | |
103 skia::RETURN_NULL_ON_FAILURE); | |
104 } | |
105 | |
106 bool TransportDIB::Map() { | |
107 if (!is_valid_id(key_)) | |
108 return false; | |
109 if (address_ != kInvalidAddress) | |
110 return true; | |
111 | |
112 struct shmid_ds shmst; | |
113 if (shmctl(key_.shmkey, IPC_STAT, &shmst) == -1) | |
114 return false; | |
115 | |
116 void* address = shmat(key_.shmkey, NULL /* desired address */, 0 /* flags */); | |
117 if (address == kInvalidAddress) | |
118 return false; | |
119 | |
120 address_ = address; | |
121 size_ = shmst.shm_segsz; | |
122 return true; | |
123 } | |
124 | |
125 void* TransportDIB::memory() const { | |
126 DCHECK_NE(address_, kInvalidAddress); | |
127 return address_; | |
128 } | |
129 | |
130 TransportDIB::Id TransportDIB::id() const { | |
131 return key_; | |
132 } | |
133 | |
134 TransportDIB::Handle TransportDIB::handle() const { | |
135 return key_.shmkey; | |
136 } | |
137 | |
138 XID TransportDIB::MapToX(XDisplay* display) { | |
139 if (!x_shm_) { | |
140 x_shm_ = ui::AttachSharedMemory(display, key_.shmkey); | |
141 display_ = display; | |
142 } | |
143 | |
144 return x_shm_; | |
145 } | |
146 | |
147 void TransportDIB::DecreaseInFlightCounter() { | |
148 CHECK(inflight_counter_); | |
149 inflight_counter_--; | |
150 if (!inflight_counter_ && detached_) | |
151 delete this; | |
152 } | |
153 | |
154 void TransportDIB::Detach() { | |
155 CHECK(!detached_); | |
156 detached_ = true; | |
157 if (!inflight_counter_) | |
158 delete this; | |
159 } | |
160 | |
OLD | NEW |