| 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 : size_(0) { |
| 18 } | 18 } |
| 19 | 19 |
| 20 TransportDIB::~TransportDIB() { | 20 TransportDIB::~TransportDIB() { |
| 21 } | 21 } |
| 22 | 22 |
| 23 TransportDIB::TransportDIB(HANDLE handle) | 23 TransportDIB::TransportDIB(base::SharedMemoryHandle handle) |
| 24 : shared_memory_(handle, false /* read write */), | 24 : shared_memory_(handle, false /* read write */), size_(0) {} |
| 25 size_(0) { | |
| 26 } | |
| 27 | 25 |
| 28 // static | 26 // static |
| 29 TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) { | 27 TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) { |
| 30 TransportDIB* dib = new TransportDIB; | 28 TransportDIB* dib = new TransportDIB; |
| 31 | 29 |
| 32 if (!dib->shared_memory_.CreateAnonymous(size)) { | 30 if (!dib->shared_memory_.CreateAnonymous(size)) { |
| 33 delete dib; | 31 delete dib; |
| 34 return NULL; | 32 return NULL; |
| 35 } | 33 } |
| 36 | 34 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 48 return dib.release(); | 46 return dib.release(); |
| 49 } | 47 } |
| 50 | 48 |
| 51 // static | 49 // static |
| 52 TransportDIB* TransportDIB::CreateWithHandle(Handle handle) { | 50 TransportDIB* TransportDIB::CreateWithHandle(Handle handle) { |
| 53 return new TransportDIB(handle); | 51 return new TransportDIB(handle); |
| 54 } | 52 } |
| 55 | 53 |
| 56 // static | 54 // static |
| 57 bool TransportDIB::is_valid_handle(Handle dib) { | 55 bool TransportDIB::is_valid_handle(Handle dib) { |
| 58 return dib != NULL; | 56 return dib.IsValid(); |
| 59 } | 57 } |
| 60 | 58 |
| 61 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { | 59 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) { |
| 62 // This DIB already mapped the file into this process, but PlatformCanvas | 60 // This DIB already mapped the file into this process, but PlatformCanvas |
| 63 // will map it again. | 61 // will map it again. |
| 64 DCHECK(!memory()) << "Mapped file twice in the same process."; | 62 DCHECK(!memory()) << "Mapped file twice in the same process."; |
| 65 | 63 |
| 66 // We can't check the canvas size before mapping, but it's safe because | 64 // We can't check the canvas size before mapping, but it's safe because |
| 67 // Windows will fail to map the section if the dimensions of the canvas | 65 // Windows will fail to map the section if the dimensions of the canvas |
| 68 // are too large. | 66 // are too large. |
| 69 skia::PlatformCanvas* canvas = | 67 skia::PlatformCanvas* canvas = skia::CreatePlatformCanvas( |
| 70 skia::CreatePlatformCanvas(w, h, true, shared_memory_.handle(), | 68 w, h, true, shared_memory_.handle().GetHandle(), |
| 71 skia::RETURN_NULL_ON_FAILURE); | 69 skia::RETURN_NULL_ON_FAILURE); |
| 72 | 70 |
| 73 // Calculate the size for the memory region backing the canvas. | 71 // Calculate the size for the memory region backing the canvas. |
| 74 if (canvas) | 72 if (canvas) |
| 75 size_ = skia::PlatformCanvasStrideForWidth(w) * h; | 73 size_ = skia::PlatformCanvasStrideForWidth(w) * h; |
| 76 | 74 |
| 77 return canvas; | 75 return canvas; |
| 78 } | 76 } |
| 79 | 77 |
| 80 bool TransportDIB::Map() { | 78 bool TransportDIB::Map() { |
| 81 if (!is_valid_handle(shared_memory_.handle())) | 79 if (!is_valid_handle(shared_memory_.handle())) |
| 82 return false; | 80 return false; |
| 83 if (memory()) | 81 if (memory()) |
| 84 return true; | 82 return true; |
| 85 | 83 |
| 86 if (!shared_memory_.Map(0 /* map whole shared memory segment */)) { | 84 if (!shared_memory_.Map(0 /* map whole shared memory segment */)) { |
| 87 LOG(ERROR) << "Failed to map transport DIB" | 85 LOG(ERROR) << "Failed to map transport DIB" |
| 88 << " handle:" << shared_memory_.handle() | 86 << " handle:" << shared_memory_.handle().GetHandle() |
| 89 << " error:" << ::GetLastError(); | 87 << " error:" << ::GetLastError(); |
| 90 return false; | 88 return false; |
| 91 } | 89 } |
| 92 | 90 |
| 93 size_ = shared_memory_.mapped_size(); | 91 size_ = shared_memory_.mapped_size(); |
| 94 return true; | 92 return true; |
| 95 } | 93 } |
| 96 | 94 |
| 97 void* TransportDIB::memory() const { | 95 void* TransportDIB::memory() const { |
| 98 return shared_memory_.memory(); | 96 return shared_memory_.memory(); |
| 99 } | 97 } |
| OLD | NEW |