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"
10 #include "base/logging.h"
11 #include "base/posix/eintr_wrapper.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/shared_memory.h"
14 #include "skia/ext/platform_canvas.h"
16 TransportDIB::TransportDIB()
20 TransportDIB::TransportDIB(TransportDIB::Handle dib
)
21 : shared_memory_(dib
, false /* read write */),
25 TransportDIB::~TransportDIB() {
29 TransportDIB
* TransportDIB::Create(size_t size
, uint32 sequence_num
) {
30 TransportDIB
* dib
= new TransportDIB
;
31 if (!dib
->shared_memory_
.CreateAndMapAnonymous(size
)) {
41 TransportDIB
* TransportDIB::Map(Handle handle
) {
42 scoped_ptr
<TransportDIB
> dib(CreateWithHandle(handle
));
49 TransportDIB
* TransportDIB::CreateWithHandle(Handle handle
) {
50 return new TransportDIB(handle
);
54 bool TransportDIB::is_valid_handle(Handle dib
) {
59 bool TransportDIB::is_valid_id(Id id
) {
63 skia::PlatformCanvas
* TransportDIB::GetPlatformCanvas(int w
, int h
) {
64 if (!memory() && !Map())
66 return skia::CreatePlatformCanvas(w
, h
, true,
67 reinterpret_cast<uint8_t*>(memory()),
68 skia::RETURN_NULL_ON_FAILURE
);
71 bool TransportDIB::Map() {
72 if (!is_valid_handle(handle()))
78 if ((fstat(shared_memory_
.handle().fd
, &st
) != 0) ||
79 (!shared_memory_
.Map(st
.st_size
))) {
87 void* TransportDIB::memory() const {
88 return shared_memory_
.memory();
91 TransportDIB::Id
TransportDIB::id() const {
92 return shared_memory_
.id();
95 TransportDIB::Handle
TransportDIB::handle() const {
96 return shared_memory_
.handle();