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"
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()
20 TransportDIB::~TransportDIB() {
23 TransportDIB::TransportDIB(HANDLE handle)
24 : shared_memory_(handle, false /* read write */),
29 TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) {
30 TransportDIB* dib = new TransportDIB;
32 if (!dib->shared_memory_.CreateAnonymous(size)) {
38 dib->sequence_num_ = sequence_num;
44 TransportDIB* TransportDIB::Map(Handle handle) {
45 scoped_ptr<TransportDIB> dib(CreateWithHandle(handle));
52 TransportDIB* TransportDIB::CreateWithHandle(Handle handle) {
53 return new TransportDIB(handle);
57 bool TransportDIB::is_valid_handle(Handle dib) {
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
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
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.
80 size_ = skia::PlatformCanvasStrideForWidth(w) * h;
85 bool TransportDIB::Map() {
86 if (!is_valid_handle(handle()))
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();
98 size_ = shared_memory_.mapped_size();
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_);