Chromium Code Reviews| 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 // Get the size for the memory region backing the canvas. | |
| 79 if (canvas) { | |
| 80 MEMORY_BASIC_INFORMATION memory_info; | |
| 81 if (!::VirtualQuery(canvas, &memory_info, sizeof(memory_info))) { | |
| 82 delete canvas; | |
| 83 return NULL; | |
| 84 } | |
| 85 | |
| 86 size_ = memory_info.RegionSize - (reinterpret_cast<char*>(canvas) - | |
|
cpu_(ooo_6.6-7.5)
2013/03/21 17:33:40
do we have any guarantee that skia::CreatePlatform
jschuh
2013/03/21 19:32:06
As written now, yes. It just ends up calling Creat
cpu_(ooo_6.6-7.5)
2013/03/21 20:02:27
But what does the subtraction of line 86 and 87 do
jschuh
2013/03/26 22:09:24
I think I over-complicated things. We can just cal
| |
| 87 static_cast<char*>(memory_info.AllocationBase)); | |
| 88 } | |
| 89 | |
| 90 return canvas; | |
| 75 } | 91 } |
| 76 | 92 |
| 77 bool TransportDIB::Map() { | 93 bool TransportDIB::Map() { |
| 78 if (!is_valid_handle(handle())) | 94 if (!is_valid_handle(handle())) |
| 79 return false; | 95 return false; |
| 80 if (memory()) | 96 if (memory()) |
| 81 return true; | 97 return true; |
| 82 | 98 |
| 83 if (!shared_memory_.Map(0 /* map whole shared memory segment */)) { | 99 if (!shared_memory_.Map(0 /* map whole shared memory segment */)) { |
| 84 LOG(ERROR) << "Failed to map transport DIB" | 100 LOG(ERROR) << "Failed to map transport DIB" |
| 85 << " handle:" << shared_memory_.handle() | 101 << " handle:" << shared_memory_.handle() |
| 86 << " error:" << ::GetLastError(); | 102 << " error:" << ::GetLastError(); |
| 87 return false; | 103 return false; |
| 88 } | 104 } |
| 89 | 105 |
| 90 // There doesn't seem to be any way to find the size of the shared memory | 106 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; | 107 return true; |
| 96 } | 108 } |
| 97 | 109 |
| 98 void* TransportDIB::memory() const { | 110 void* TransportDIB::memory() const { |
| 99 return shared_memory_.memory(); | 111 return shared_memory_.memory(); |
| 100 } | 112 } |
| 101 | 113 |
| 102 TransportDIB::Handle TransportDIB::handle() const { | 114 TransportDIB::Handle TransportDIB::handle() const { |
| 103 return shared_memory_.handle(); | 115 return shared_memory_.handle(); |
| 104 } | 116 } |
| 105 | 117 |
| 106 TransportDIB::Id TransportDIB::id() const { | 118 TransportDIB::Id TransportDIB::id() const { |
| 107 return Id(handle(), sequence_num_); | 119 return Id(handle(), sequence_num_); |
| 108 } | 120 } |
| OLD | NEW |