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/memory/scoped_ptr.h"
12 #include "base/memory/shared_memory.h"
13 #include "skia/ext/platform_canvas.h"
15 TransportDIB::TransportDIB()
19 TransportDIB::TransportDIB(TransportDIB::Handle dib
)
20 : shared_memory_(dib
, false /* read write */),
24 TransportDIB::~TransportDIB() {
28 TransportDIB
* TransportDIB::Create(size_t size
, uint32 sequence_num
) {
29 TransportDIB
* dib
= new TransportDIB
;
30 if (!dib
->shared_memory_
.CreateAndMapAnonymous(size
)) {
40 TransportDIB
* TransportDIB::Map(Handle handle
) {
41 scoped_ptr
<TransportDIB
> dib(CreateWithHandle(handle
));
48 TransportDIB
* TransportDIB::CreateWithHandle(Handle handle
) {
49 return new TransportDIB(handle
);
53 bool TransportDIB::is_valid_handle(Handle dib
) {
58 bool TransportDIB::is_valid_id(Id id
) {
59 #if defined(OS_ANDROID)
60 return is_valid_handle(id
);
66 skia::PlatformCanvas
* TransportDIB::GetPlatformCanvas(int w
, int h
) {
67 if ((!memory() && !Map()) || !VerifyCanvasSize(w
, h
))
69 return skia::CreatePlatformCanvas(w
, h
, true,
70 reinterpret_cast<uint8_t*>(memory()),
71 skia::RETURN_NULL_ON_FAILURE
);
74 bool TransportDIB::Map() {
75 if (!is_valid_handle(handle()))
77 #if defined(OS_ANDROID)
78 if (!shared_memory_
.Map(0))
80 size_
= shared_memory_
.mapped_size();
86 if ((fstat(shared_memory_
.handle().fd
, &st
) != 0) ||
87 (!shared_memory_
.Map(st
.st_size
))) {
96 void* TransportDIB::memory() const {
97 return shared_memory_
.memory();
100 TransportDIB::Id
TransportDIB::id() const {
101 #if defined(OS_ANDROID)
104 return shared_memory_
.id();
108 TransportDIB::Handle
TransportDIB::handle() const {
109 return shared_memory_
.handle();