OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ui/surface/transport_dib.h" | 5 #include "ui/surface/transport_dib.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/sys_info.h" | 13 #include "base/sys_info.h" |
14 #include "skia/ext/platform_canvas.h" | 14 #include "skia/ext/platform_canvas.h" |
15 | 15 |
16 TransportDIB::TransportDIB() { | 16 TransportDIB::TransportDIB() |
| 17 : size_(0) { |
17 } | 18 } |
18 | 19 |
19 TransportDIB::~TransportDIB() { | 20 TransportDIB::~TransportDIB() { |
20 } | 21 } |
21 | 22 |
22 TransportDIB::TransportDIB(HANDLE handle) | 23 TransportDIB::TransportDIB(HANDLE handle) |
23 : shared_memory_(handle, false /* read write */) { | 24 : shared_memory_(handle, false /* read write */), |
| 25 size_(0) { |
24 } | 26 } |
25 | 27 |
26 // static | 28 // static |
27 TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) { | 29 TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) { |
28 size_t allocation_granularity = base::SysInfo::VMAllocationGranularity(); | |
29 size = size / allocation_granularity + 1; | |
30 size = size * allocation_granularity; | |
31 | |
32 TransportDIB* dib = new TransportDIB; | 30 TransportDIB* dib = new TransportDIB; |
33 | 31 |
34 if (!dib->shared_memory_.CreateAnonymous(size)) { | 32 if (!dib->shared_memory_.CreateAnonymous(size)) { |
35 delete dib; | 33 delete dib; |
36 return NULL; | 34 return NULL; |
37 } | 35 } |
38 | 36 |
39 dib->size_ = size; | 37 dib->size_ = size; |
40 dib->sequence_num_ = sequence_num; | 38 dib->sequence_num_ = sequence_num; |
41 | 39 |
(...skipping 21 matching lines...) Expand all Loading... |
63 // static | 61 // static |
64 bool TransportDIB::is_valid_id(TransportDIB::Id id) { | 62 bool TransportDIB::is_valid_id(TransportDIB::Id id) { |
65 return is_valid_handle(id.handle); | 63 return is_valid_handle(id.handle); |
66 } | 64 } |
67 | 65 |
68 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { | 66 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { |
69 // This DIB already mapped the file into this process, but PlatformCanvas | 67 // This DIB already mapped the file into this process, but PlatformCanvas |
70 // will map it again. | 68 // will map it again. |
71 DCHECK(!memory()) << "Mapped file twice in the same process."; | 69 DCHECK(!memory()) << "Mapped file twice in the same process."; |
72 | 70 |
73 return skia::CreatePlatformCanvas(w, h, true, handle(), | 71 // We can't check the canvas size before mapping, but it's safe because |
74 skia::RETURN_NULL_ON_FAILURE); | 72 // Windows will fail to map the section if the dimensions of the canvas |
| 73 // are too large. |
| 74 skia::PlatformCanvas* canvas = |
| 75 skia::CreatePlatformCanvas(w, h, true, handle(), |
| 76 skia::RETURN_NULL_ON_FAILURE); |
| 77 |
| 78 // Calculate the size for the memory region backing the canvas. |
| 79 if (canvas) |
| 80 size_ = skia::PlatformCanvasStrideForWidth(w) * h; |
| 81 |
| 82 return canvas; |
75 } | 83 } |
76 | 84 |
77 bool TransportDIB::Map() { | 85 bool TransportDIB::Map() { |
78 if (!is_valid_handle(handle())) | 86 if (!is_valid_handle(handle())) |
79 return false; | 87 return false; |
80 if (memory()) | 88 if (memory()) |
81 return true; | 89 return true; |
82 | 90 |
83 if (!shared_memory_.Map(0 /* map whole shared memory segment */)) { | 91 if (!shared_memory_.Map(0 /* map whole shared memory segment */)) { |
84 LOG(ERROR) << "Failed to map transport DIB" | 92 LOG(ERROR) << "Failed to map transport DIB" |
85 << " handle:" << shared_memory_.handle() | 93 << " handle:" << shared_memory_.handle() |
86 << " error:" << ::GetLastError(); | 94 << " error:" << ::GetLastError(); |
87 return false; | 95 return false; |
88 } | 96 } |
89 | 97 |
90 // There doesn't seem to be any way to find the size of the shared memory | 98 size_ = shared_memory_.mapped_size(); |
91 // region! GetFileSize indicates that the handle is invalid. Thus, we | |
92 // conservatively set the size to the maximum and hope that the renderer | |
93 // isn't about to ask us to read off the end of the array. | |
94 size_ = std::numeric_limits<size_t>::max(); | |
95 return true; | 99 return true; |
96 } | 100 } |
97 | 101 |
98 void* TransportDIB::memory() const { | 102 void* TransportDIB::memory() const { |
99 return shared_memory_.memory(); | 103 return shared_memory_.memory(); |
100 } | 104 } |
101 | 105 |
102 TransportDIB::Handle TransportDIB::handle() const { | 106 TransportDIB::Handle TransportDIB::handle() const { |
103 return shared_memory_.handle(); | 107 return shared_memory_.handle(); |
104 } | 108 } |
105 | 109 |
106 TransportDIB::Id TransportDIB::id() const { | 110 TransportDIB::Id TransportDIB::id() const { |
107 return Id(handle(), sequence_num_); | 111 return Id(handle(), sequence_num_); |
108 } | 112 } |
OLD | NEW |