win: Remove a few instances of the "goto error" pattern.
[chromium-blink-merge.git] / ui / surface / transport_dib_posix.cc
blob0a55dbdb0b7f1c85645d456b4326451b896044b6
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 <sys/stat.h>
8 #include <unistd.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()
16 : size_(0) {
19 TransportDIB::TransportDIB(TransportDIB::Handle dib)
20 : shared_memory_(dib, false /* read write */),
21 size_(0) {
24 TransportDIB::~TransportDIB() {
27 // static
28 TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) {
29 TransportDIB* dib = new TransportDIB;
30 if (!dib->shared_memory_.CreateAndMapAnonymous(size)) {
31 delete dib;
32 return NULL;
35 dib->size_ = size;
36 return dib;
39 // static
40 TransportDIB* TransportDIB::Map(Handle handle) {
41 scoped_ptr<TransportDIB> dib(CreateWithHandle(handle));
42 if (!dib->Map())
43 return NULL;
44 return dib.release();
47 // static
48 TransportDIB* TransportDIB::CreateWithHandle(Handle handle) {
49 return new TransportDIB(handle);
52 // static
53 bool TransportDIB::is_valid_handle(Handle dib) {
54 return base::SharedMemory::IsHandleValid(dib);
57 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) {
58 if ((!memory() && !Map()) || !VerifyCanvasSize(w, h))
59 return NULL;
60 return skia::CreatePlatformCanvas(w, h, true,
61 reinterpret_cast<uint8_t*>(memory()),
62 skia::RETURN_NULL_ON_FAILURE);
65 bool TransportDIB::Map() {
66 if (!is_valid_handle(shared_memory_.handle()))
67 return false;
68 #if defined(OS_ANDROID)
69 if (!shared_memory_.Map(0))
70 return false;
71 size_ = shared_memory_.mapped_size();
72 #else
73 if (memory())
74 return true;
76 int size = base::SharedMemory::GetSizeFromSharedMemoryHandle(
77 shared_memory_.handle());
78 if (size == -1 || !shared_memory_.Map(size))
79 return false;
81 size_ = size;
82 #endif
83 return true;
86 void* TransportDIB::memory() const {
87 return shared_memory_.memory();