[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / ui / surface / transport_dib_win.cc
blobe8d36adbd5a5454c36e64bb46ac18b5bbbe6d762
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 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) {
62 // This DIB already mapped the file into this process, but PlatformCanvas
63 // will map it again.
64 DCHECK(!memory()) << "Mapped file twice in the same process.";
66 // 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
68 // are too large.
69 skia::PlatformCanvas* canvas =
70 skia::CreatePlatformCanvas(w, h, true, shared_memory_.handle(),
71 skia::RETURN_NULL_ON_FAILURE);
73 // Calculate the size for the memory region backing the canvas.
74 if (canvas)
75 size_ = skia::PlatformCanvasStrideForWidth(w) * h;
77 return canvas;
80 bool TransportDIB::Map() {
81 if (!is_valid_handle(shared_memory_.handle()))
82 return false;
83 if (memory())
84 return true;
86 if (!shared_memory_.Map(0 /* map whole shared memory segment */)) {
87 LOG(ERROR) << "Failed to map transport DIB"
88 << " handle:" << shared_memory_.handle()
89 << " error:" << ::GetLastError();
90 return false;
93 size_ = shared_memory_.mapped_size();
94 return true;
97 void* TransportDIB::memory() const {
98 return shared_memory_.memory();