OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/gfx/surface/transport_dib.h" | |
6 | |
7 #include <unistd.h> | |
8 #include <sys/stat.h> | |
9 | |
10 #include "base/eintr_wrapper.h" | |
11 #include "base/logging.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/shared_memory.h" | |
14 #include "skia/ext/platform_canvas.h" | |
15 | |
16 TransportDIB::TransportDIB() | |
17 : size_(0) { | |
18 } | |
19 | |
20 TransportDIB::TransportDIB(TransportDIB::Handle dib) | |
21 : shared_memory_(dib, false /* read write */), | |
22 size_(0) { | |
23 } | |
24 | |
25 TransportDIB::~TransportDIB() { | |
26 } | |
27 | |
28 // static | |
29 TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) { | |
30 TransportDIB* dib = new TransportDIB; | |
31 // We will use ashmem_get_size_region() to figure out the size in Map(size). | |
32 if (!dib->shared_memory_.CreateAndMapAnonymous(size)) { | |
33 delete dib; | |
34 return NULL; | |
35 } | |
36 | |
37 dib->size_ = size; | |
38 return dib; | |
39 } | |
40 | |
41 // static | |
42 TransportDIB* TransportDIB::Map(Handle handle) { | |
43 scoped_ptr<TransportDIB> dib(CreateWithHandle(handle)); | |
44 if (!dib->Map()) | |
45 return NULL; | |
46 return dib.release(); | |
47 } | |
48 | |
49 // static | |
50 TransportDIB* TransportDIB::CreateWithHandle(Handle handle) { | |
51 return new TransportDIB(handle); | |
52 } | |
53 | |
54 // static | |
55 bool TransportDIB::is_valid_handle(Handle dib) { | |
56 return dib.fd >= 0; | |
57 } | |
58 | |
59 // static | |
60 bool TransportDIB::is_valid_id(Id id) { | |
61 // Same as is_valid_handle(). | |
62 return id.fd >= 0; | |
63 } | |
64 | |
65 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { | |
66 if (!memory() && !Map()) | |
67 return NULL; | |
68 scoped_ptr<skia::PlatformCanvas> canvas(new skia::PlatformCanvas); | |
69 if (!canvas->initialize(w, h, true, reinterpret_cast<uint8_t*>(memory()))) { | |
70 // TODO(husky): Remove when http://b/issue?id=4233182 is definitely fixed. | |
71 LOG(ERROR) << "Failed to initialize canvas of size " << w << "x" << h; | |
72 return NULL; | |
73 } | |
74 return canvas.release(); | |
75 } | |
76 | |
77 bool TransportDIB::Map() { | |
78 if (!is_valid_handle(handle())) | |
79 return false; | |
80 // We will use ashmem_get_size_region() to figure out the size in Map(size). | |
81 if (!shared_memory_.Map(0)) | |
82 return false; | |
83 | |
84 // TODO: Note that using created_size() below is a hack. See the comment in | |
85 // SharedMemory::Map(). | |
86 size_ = shared_memory_.created_size(); | |
87 return true; | |
88 } | |
89 | |
90 void* TransportDIB::memory() const { | |
91 return shared_memory_.memory(); | |
92 } | |
93 | |
94 TransportDIB::Id TransportDIB::id() const { | |
95 // Use FileDescriptor as id. | |
96 return shared_memory_.handle(); | |
97 } | |
98 | |
99 TransportDIB::Handle TransportDIB::handle() const { | |
100 return shared_memory_.handle(); | |
101 } | |
OLD | NEW |