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 "app/surface/transport_dib.h" | 5 #include "app/surface/transport_dib.h" |
6 | 6 |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
9 | 9 |
10 #include "base/eintr_wrapper.h" | 10 #include "base/eintr_wrapper.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 "base/shared_memory.h" | 13 #include "base/shared_memory.h" |
14 #include "skia/ext/platform_canvas.h" | 14 #include "skia/ext/platform_canvas.h" |
15 | 15 |
16 void TransportDIB::ScopedHandle::Close() { | |
17 if (is_valid(handle_)) | |
18 base::SharedMemory::CloseHandle(handle_); | |
19 } | |
Scott Hess - ex-Googler
2010/10/25 21:38:19
AFAICT, handle_ is a file descriptor, and since th
kkania
2010/10/27 18:12:59
That is correct. However, since Close() is private
Scott Hess - ex-Googler
2010/10/27 19:34:02
Not if you happen to be multi-threaded.
| |
20 | |
16 TransportDIB::TransportDIB() | 21 TransportDIB::TransportDIB() |
17 : size_(0) { | 22 : size_(0) { |
18 } | 23 } |
19 | 24 |
20 TransportDIB::TransportDIB(TransportDIB::Handle dib) | 25 TransportDIB::TransportDIB(TransportDIB::Handle dib) |
21 : shared_memory_(dib, false /* read write */), | 26 : shared_memory_(dib, false /* read write */), |
22 size_(0) { | 27 size_(0) { |
23 } | 28 } |
24 | 29 |
25 TransportDIB::~TransportDIB() { | 30 TransportDIB::~TransportDIB() { |
26 } | 31 } |
27 | 32 |
28 // static | 33 // static |
29 TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) { | 34 TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) { |
30 TransportDIB* dib = new TransportDIB; | 35 TransportDIB* dib = new TransportDIB; |
31 if (!dib->shared_memory_.Create("", false /* read write */, | 36 if (!dib->shared_memory_.Create("", false /* read write */, |
32 false /* do not open existing */, size)) { | 37 false /* do not open existing */, size)) { |
33 delete dib; | 38 delete dib; |
34 return NULL; | 39 return NULL; |
35 } | 40 } |
36 | 41 |
37 if (!dib->shared_memory_.Map(size)) { | |
38 delete dib; | |
39 return NULL; | |
40 } | |
41 | |
42 dib->size_ = size; | 42 dib->size_ = size; |
43 return dib; | 43 return dib; |
44 } | 44 } |
45 | 45 |
46 // static | 46 // static |
47 TransportDIB* TransportDIB::Map(TransportDIB::Handle handle) { | 47 TransportDIB* TransportDIB::Map(Handle handle) { |
48 if (!is_valid(handle)) | 48 scoped_ptr<TransportDIB> dib(CreateWithHandle(handle)); |
49 if (!dib->Map()) | |
49 return NULL; | 50 return NULL; |
50 | 51 return dib.release(); |
51 TransportDIB* dib = new TransportDIB(handle); | |
52 struct stat st; | |
53 if ((fstat(handle.fd, &st) != 0) || | |
54 (!dib->shared_memory_.Map(st.st_size))) { | |
55 delete dib; | |
56 if (HANDLE_EINTR(close(handle.fd)) < 0) | |
57 PLOG(ERROR) << "close"; | |
58 return NULL; | |
59 } | |
60 | |
61 dib->size_ = st.st_size; | |
62 | |
63 return dib; | |
64 } | 52 } |
65 | 53 |
54 // static | |
55 TransportDIB* TransportDIB::CreateWithHandle(Handle handle) { | |
56 return new TransportDIB(handle); | |
57 } | |
58 | |
59 // static | |
66 bool TransportDIB::is_valid(Handle dib) { | 60 bool TransportDIB::is_valid(Handle dib) { |
67 return dib.fd >= 0; | 61 return dib.fd >= 0; |
68 } | 62 } |
69 | 63 |
70 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { | 64 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { |
65 if (!memory() && !Map()) | |
66 return NULL; | |
71 scoped_ptr<skia::PlatformCanvas> canvas(new skia::PlatformCanvas); | 67 scoped_ptr<skia::PlatformCanvas> canvas(new skia::PlatformCanvas); |
72 if (!canvas->initialize(w, h, true, reinterpret_cast<uint8_t*>(memory()))) | 68 if (!canvas->initialize(w, h, true, reinterpret_cast<uint8_t*>(memory()))) |
73 return NULL; | 69 return NULL; |
74 return canvas.release(); | 70 return canvas.release(); |
75 } | 71 } |
76 | 72 |
73 bool TransportDIB::Map() { | |
74 if (memory()) | |
75 return true; | |
76 | |
77 struct stat st; | |
78 if ((fstat(shared_memory_.handle().fd, &st) != 0) || | |
79 (!shared_memory_.Map(st.st_size))) { | |
80 return false; | |
81 } | |
82 | |
83 size_ = st.st_size; | |
84 return true; | |
85 } | |
86 | |
87 TransportDIB::Handle TransportDIB::GetHandleForProcess( | |
88 base::ProcessHandle process_handle) const { | |
89 return handle(); | |
90 } | |
91 | |
77 void* TransportDIB::memory() const { | 92 void* TransportDIB::memory() const { |
78 return shared_memory_.memory(); | 93 return shared_memory_.memory(); |
79 } | 94 } |
80 | 95 |
81 TransportDIB::Id TransportDIB::id() const { | 96 TransportDIB::Id TransportDIB::id() const { |
82 return shared_memory_.id(); | 97 return shared_memory_.id(); |
83 } | 98 } |
84 | 99 |
85 TransportDIB::Handle TransportDIB::handle() const { | 100 TransportDIB::Handle TransportDIB::handle() const { |
86 return shared_memory_.handle(); | 101 return shared_memory_.handle(); |
87 } | 102 } |
OLD | NEW |