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
) {
61 skia::PlatformCanvas
* TransportDIB::GetPlatformCanvas(int w
, int h
) {
62 // This DIB already mapped the file into this process, but PlatformCanvas
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
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.
75 size_
= skia::PlatformCanvasStrideForWidth(w
) * h
;
80 bool TransportDIB::Map() {
81 if (!is_valid_handle(shared_memory_
.handle()))
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();
93 size_
= shared_memory_
.mapped_size();
97 void* TransportDIB::memory() const {
98 return shared_memory_
.memory();