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" |
(...skipping 20 matching lines...) Expand all Loading... |
31 if (!dib->shared_memory_.CreateAndMapAnonymous(size)) { | 31 if (!dib->shared_memory_.CreateAndMapAnonymous(size)) { |
32 delete dib; | 32 delete dib; |
33 return NULL; | 33 return NULL; |
34 } | 34 } |
35 | 35 |
36 dib->size_ = size; | 36 dib->size_ = size; |
37 return dib; | 37 return dib; |
38 } | 38 } |
39 | 39 |
40 // static | 40 // static |
41 TransportDIB* TransportDIB::Map(TransportDIB::Handle handle) { | 41 TransportDIB* TransportDIB::Map(Handle handle) { |
42 if (!is_valid(handle)) | 42 scoped_ptr<TransportDIB> dib(CreateWithHandle(handle)); |
| 43 if (!dib->Map()) |
43 return NULL; | 44 return NULL; |
44 | 45 return dib.release(); |
45 TransportDIB* dib = new TransportDIB(handle); | |
46 struct stat st; | |
47 if ((fstat(handle.fd, &st) != 0) || | |
48 (!dib->shared_memory_.Map(st.st_size))) { | |
49 delete dib; | |
50 if (HANDLE_EINTR(close(handle.fd)) < 0) | |
51 PLOG(ERROR) << "close"; | |
52 return NULL; | |
53 } | |
54 | |
55 dib->size_ = st.st_size; | |
56 | |
57 return dib; | |
58 } | 46 } |
59 | 47 |
| 48 // static |
| 49 TransportDIB* TransportDIB::CreateWithHandle(Handle handle) { |
| 50 return new TransportDIB(handle); |
| 51 } |
| 52 |
| 53 // static |
60 bool TransportDIB::is_valid(Handle dib) { | 54 bool TransportDIB::is_valid(Handle dib) { |
61 return dib.fd >= 0; | 55 return dib.fd >= 0; |
62 } | 56 } |
63 | 57 |
64 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { | 58 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { |
| 59 if (!memory() && !Map()) |
| 60 return NULL; |
65 scoped_ptr<skia::PlatformCanvas> canvas(new skia::PlatformCanvas); | 61 scoped_ptr<skia::PlatformCanvas> canvas(new skia::PlatformCanvas); |
66 if (!canvas->initialize(w, h, true, reinterpret_cast<uint8_t*>(memory()))) | 62 if (!canvas->initialize(w, h, true, reinterpret_cast<uint8_t*>(memory()))) |
67 return NULL; | 63 return NULL; |
68 return canvas.release(); | 64 return canvas.release(); |
69 } | 65 } |
70 | 66 |
| 67 bool TransportDIB::Map() { |
| 68 if (!is_valid(handle())) |
| 69 return false; |
| 70 if (memory()) |
| 71 return true; |
| 72 |
| 73 struct stat st; |
| 74 if ((fstat(shared_memory_.handle().fd, &st) != 0) || |
| 75 (!shared_memory_.Map(st.st_size))) { |
| 76 return false; |
| 77 } |
| 78 |
| 79 size_ = st.st_size; |
| 80 return true; |
| 81 } |
| 82 |
71 void* TransportDIB::memory() const { | 83 void* TransportDIB::memory() const { |
72 return shared_memory_.memory(); | 84 return shared_memory_.memory(); |
73 } | 85 } |
74 | 86 |
75 TransportDIB::Id TransportDIB::id() const { | 87 TransportDIB::Id TransportDIB::id() const { |
76 return shared_memory_.id(); | 88 return shared_memory_.id(); |
77 } | 89 } |
78 | 90 |
79 TransportDIB::Handle TransportDIB::handle() const { | 91 TransportDIB::Handle TransportDIB::handle() const { |
80 return shared_memory_.handle(); | 92 return shared_memory_.handle(); |
81 } | 93 } |
OLD | NEW |