Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / surface / transport_dib_win.cc
bloba664266dae6e26831be25f67513d0a156b4c267b
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
3 // found in the LICENSE file.
5 #include "ui/surface/transport_dib.h"
7 #include <windows.h>
9 #include <limits>
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/sys_info.h"
14 #include "skia/ext/platform_canvas.h"
16 TransportDIB::TransportDIB()
17 : size_(0) {
20 TransportDIB::~TransportDIB() {
23 TransportDIB::TransportDIB(HANDLE handle)
24 : shared_memory_(handle, false /* read write */),
25 size_(0) {
28 // static
29 TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) {
30 TransportDIB* dib = new TransportDIB;
32 if (!dib->shared_memory_.CreateAnonymous(size)) {
33 delete dib;
34 return NULL;
37 dib->size_ = size;
38 dib->sequence_num_ = sequence_num;
40 return dib;
43 // static
44 TransportDIB* TransportDIB::Map(Handle handle) {
45 scoped_ptr<TransportDIB> dib(CreateWithHandle(handle));
46 if (!dib->Map())
47 return NULL;
48 return dib.release();
51 // static
52 TransportDIB* TransportDIB::CreateWithHandle(Handle handle) {
53 return new TransportDIB(handle);
56 // static
57 bool TransportDIB::is_valid_handle(Handle dib) {
58 return dib != NULL;
61 // static
62 bool TransportDIB::is_valid_id(TransportDIB::Id id) {
63 return is_valid_handle(id.handle);
66 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) {
67 // This DIB already mapped the file into this process, but PlatformCanvas
68 // will map it again.
69 DCHECK(!memory()) << "Mapped file twice in the same process.";
71 // We can't check the canvas size before mapping, but it's safe because
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);
78 // Calculate the size for the memory region backing the canvas.
79 if (canvas)
80 size_ = skia::PlatformCanvasStrideForWidth(w) * h;
82 return canvas;
85 bool TransportDIB::Map() {
86 if (!is_valid_handle(handle()))
87 return false;
88 if (memory())
89 return true;
91 if (!shared_memory_.Map(0 /* map whole shared memory segment */)) {
92 LOG(ERROR) << "Failed to map transport DIB"
93 << " handle:" << shared_memory_.handle()
94 << " error:" << ::GetLastError();
95 return false;
98 size_ = shared_memory_.mapped_size();
99 return true;
102 void* TransportDIB::memory() const {
103 return shared_memory_.memory();
106 TransportDIB::Handle TransportDIB::handle() const {
107 return shared_memory_.handle();
110 TransportDIB::Id TransportDIB::id() const {
111 return Id(handle(), sequence_num_);