• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ui/surface/transport_dib.h"
6 
7 // Desktop GTK Linux builds use the old-style SYSV SHM based DIBs.
8 #if !defined(TOOLKIT_GTK)
9 
10 #include <sys/stat.h>
11 #include <unistd.h>
12 
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/shared_memory.h"
16 #include "skia/ext/platform_canvas.h"
17 
TransportDIB()18 TransportDIB::TransportDIB()
19     : size_(0) {
20 }
21 
TransportDIB(TransportDIB::Handle dib)22 TransportDIB::TransportDIB(TransportDIB::Handle dib)
23     : shared_memory_(dib, false /* read write */),
24       size_(0) {
25 }
26 
~TransportDIB()27 TransportDIB::~TransportDIB() {
28 }
29 
30 // static
Create(size_t size,uint32 sequence_num)31 TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) {
32   TransportDIB* dib = new TransportDIB;
33   if (!dib->shared_memory_.CreateAndMapAnonymous(size)) {
34     delete dib;
35     return NULL;
36   }
37 
38   dib->size_ = size;
39   return dib;
40 }
41 
42 // static
Map(Handle handle)43 TransportDIB* TransportDIB::Map(Handle handle) {
44   scoped_ptr<TransportDIB> dib(CreateWithHandle(handle));
45   if (!dib->Map())
46     return NULL;
47   return dib.release();
48 }
49 
50 // static
CreateWithHandle(Handle handle)51 TransportDIB* TransportDIB::CreateWithHandle(Handle handle) {
52   return new TransportDIB(handle);
53 }
54 
55 // static
is_valid_handle(Handle dib)56 bool TransportDIB::is_valid_handle(Handle dib) {
57   return dib.fd >= 0;
58 }
59 
60 // static
is_valid_id(Id id)61 bool TransportDIB::is_valid_id(Id id) {
62 #if defined(OS_ANDROID)
63   return is_valid_handle(id);
64 #else
65   return id != 0;
66 #endif
67 }
68 
GetPlatformCanvas(int w,int h)69 skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) {
70   if ((!memory() && !Map()) || !VerifyCanvasSize(w, h))
71     return NULL;
72   return skia::CreatePlatformCanvas(w, h, true,
73                                     reinterpret_cast<uint8_t*>(memory()),
74                                     skia::RETURN_NULL_ON_FAILURE);
75 }
76 
Map()77 bool TransportDIB::Map() {
78   if (!is_valid_handle(handle()))
79     return false;
80 #if defined(OS_ANDROID)
81   if (!shared_memory_.Map(0))
82     return false;
83   size_ = shared_memory_.mapped_size();
84 #else
85   if (memory())
86     return true;
87 
88   struct stat st;
89   if ((fstat(shared_memory_.handle().fd, &st) != 0) ||
90       (!shared_memory_.Map(st.st_size))) {
91     return false;
92   }
93 
94   size_ = st.st_size;
95 #endif
96   return true;
97 }
98 
memory() const99 void* TransportDIB::memory() const {
100   return shared_memory_.memory();
101 }
102 
id() const103 TransportDIB::Id TransportDIB::id() const {
104 #if defined(OS_ANDROID)
105   return handle();
106 #else
107   return shared_memory_.id();
108 #endif
109 }
110 
handle() const111 TransportDIB::Handle TransportDIB::handle() const {
112   return shared_memory_.handle();
113 }
114 
115 #endif  // !defined(TOOLKIT_GTK)
116 
117