OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/proxy/ppb_image_data_proxy.h" | 5 #include "ppapi/proxy/ppb_image_data_proxy.h" |
6 | 6 |
7 #include <string.h> // For memcpy | 7 #include <string.h> // For memcpy |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "ppapi/c/pp_completion_callback.h" | 13 #include "ppapi/c/pp_completion_callback.h" |
14 #include "ppapi/c/pp_errors.h" | 14 #include "ppapi/c/pp_errors.h" |
15 #include "ppapi/c/pp_resource.h" | 15 #include "ppapi/c/pp_resource.h" |
16 #include "ppapi/proxy/host_resource.h" | 16 #include "ppapi/proxy/host_resource.h" |
17 #include "ppapi/proxy/plugin_dispatcher.h" | 17 #include "ppapi/proxy/plugin_dispatcher.h" |
18 #include "ppapi/proxy/plugin_resource_tracker.h" | 18 #include "ppapi/proxy/plugin_resource_tracker.h" |
19 #include "ppapi/proxy/ppapi_messages.h" | 19 #include "ppapi/proxy/ppapi_messages.h" |
20 #include "ppapi/thunk/thunk.h" | 20 #include "ppapi/thunk/thunk.h" |
21 | 21 #include "skia/ext/platform_canvas.h" |
22 #if defined(OS_LINUX) | 22 #include "ui/gfx/surface/transport_dib.h" |
23 #include <sys/shm.h> | |
24 #elif defined(OS_MACOSX) | |
25 #include <sys/stat.h> | |
26 #include <sys/mman.h> | |
27 #elif defined(OS_WIN) | |
28 #include <windows.h> | |
29 #endif | |
30 | 23 |
31 namespace pp { | 24 namespace pp { |
32 namespace proxy { | 25 namespace proxy { |
33 | 26 |
34 namespace { | 27 namespace { |
35 | 28 |
36 InterfaceProxy* CreateImageDataProxy(Dispatcher* dispatcher, | 29 InterfaceProxy* CreateImageDataProxy(Dispatcher* dispatcher, |
37 const void* target_interface) { | 30 const void* target_interface) { |
38 return new PPB_ImageData_Proxy(dispatcher, target_interface); | 31 return new PPB_ImageData_Proxy(dispatcher, target_interface); |
39 } | 32 } |
(...skipping 25 matching lines...) Expand all Loading... |
65 bool PPB_ImageData_Proxy::OnMessageReceived(const IPC::Message& msg) { | 58 bool PPB_ImageData_Proxy::OnMessageReceived(const IPC::Message& msg) { |
66 return false; | 59 return false; |
67 } | 60 } |
68 | 61 |
69 // ImageData ------------------------------------------------------------------- | 62 // ImageData ------------------------------------------------------------------- |
70 | 63 |
71 ImageData::ImageData(const HostResource& resource, | 64 ImageData::ImageData(const HostResource& resource, |
72 const PP_ImageDataDesc& desc, | 65 const PP_ImageDataDesc& desc, |
73 ImageHandle handle) | 66 ImageHandle handle) |
74 : PluginResource(resource), | 67 : PluginResource(resource), |
75 desc_(desc), | 68 desc_(desc) { |
76 handle_(handle), | 69 #if defined(OS_WIN) |
77 mapped_data_(NULL) { | 70 transport_dib_.reset(TransportDIB::CreateWithHandle(handle)); |
| 71 #else |
| 72 transport_dib_.reset(TransportDIB::Map(handle)); |
| 73 #endif |
78 } | 74 } |
79 | 75 |
80 ImageData::~ImageData() { | 76 ImageData::~ImageData() { |
81 Unmap(); | |
82 } | 77 } |
83 | 78 |
84 ::ppapi::thunk::PPB_ImageData_API* ImageData::AsImageData_API() { | 79 ::ppapi::thunk::PPB_ImageData_API* ImageData::AsImageData_API() { |
85 return this; | 80 return this; |
86 } | 81 } |
87 | 82 |
88 ImageData* ImageData::AsImageData() { | 83 ImageData* ImageData::AsImageData() { |
89 return this; | 84 return this; |
90 } | 85 } |
91 | 86 |
92 PP_Bool ImageData::Describe(PP_ImageDataDesc* desc) { | 87 PP_Bool ImageData::Describe(PP_ImageDataDesc* desc) { |
93 memcpy(desc, &desc_, sizeof(PP_ImageDataDesc)); | 88 memcpy(desc, &desc_, sizeof(PP_ImageDataDesc)); |
94 return PP_TRUE; | 89 return PP_TRUE; |
95 } | 90 } |
96 | 91 |
97 void* ImageData::Map() { | 92 void* ImageData::Map() { |
98 #if defined(OS_WIN) | 93 if (!mapped_canvas_.get()) { |
99 mapped_data_ = ::MapViewOfFile(handle_, FILE_MAP_READ | FILE_MAP_WRITE, | 94 mapped_canvas_.reset(transport_dib_->GetPlatformCanvas(desc_.size.width, |
100 0, 0, 0); | 95 desc_.size.height)); |
101 return mapped_data_; | 96 if (!mapped_canvas_.get()) |
102 #elif defined(OS_MACOSX) | 97 return NULL; |
103 struct stat st; | 98 } |
104 if (fstat(handle_.fd, &st) != 0) | 99 const SkBitmap& bitmap = |
105 return NULL; | 100 mapped_canvas_->getTopPlatformDevice().accessBitmap(true); |
106 void* memory = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, | 101 |
107 MAP_SHARED, handle_.fd, 0); | 102 bitmap.lockPixels(); |
108 if (memory == MAP_FAILED) | 103 return bitmap.getAddr(0, 0); |
109 return NULL; | |
110 mapped_data_ = memory; | |
111 return mapped_data_; | |
112 #else | |
113 int shmkey = handle_; | |
114 void* address = shmat(shmkey, NULL, 0); | |
115 // Mark for deletion in case we crash so the kernel will clean it up. | |
116 shmctl(shmkey, IPC_RMID, 0); | |
117 if (address == (void*)-1) | |
118 return NULL; | |
119 mapped_data_ = address; | |
120 return address; | |
121 #endif | |
122 } | 104 } |
123 | 105 |
124 void ImageData::Unmap() { | 106 void ImageData::Unmap() { |
125 #if defined(OS_WIN) | 107 // TODO(brettw) have a way to unmap a TransportDIB. Currently this isn't |
126 if (mapped_data_) | 108 // possible since deleting the TransportDIB also frees all the handles. |
127 ::UnmapViewOfFile(mapped_data_); | 109 // We need to add a method to TransportDIB to release the handles. |
128 #elif defined(OS_MACOSX) | |
129 if (mapped_data_) { | |
130 struct stat st; | |
131 if (fstat(handle_.fd, &st) == 0) | |
132 munmap(mapped_data_, st.st_size); | |
133 } | |
134 #else | |
135 if (mapped_data_) | |
136 shmdt(mapped_data_); | |
137 #endif | |
138 mapped_data_ = NULL; | |
139 } | 110 } |
140 | 111 |
141 #if defined(OS_WIN) | 112 #if defined(OS_WIN) |
142 const ImageHandle ImageData::NullHandle = NULL; | 113 const ImageHandle ImageData::NullHandle = NULL; |
143 #elif defined(OS_MACOSX) | 114 #elif defined(OS_MACOSX) |
144 const ImageHandle ImageData::NullHandle = ImageHandle(); | 115 const ImageHandle ImageData::NullHandle = ImageHandle(); |
145 #else | 116 #else |
146 const ImageHandle ImageData::NullHandle = 0; | 117 const ImageHandle ImageData::NullHandle = 0; |
147 #endif | 118 #endif |
148 | 119 |
149 ImageHandle ImageData::HandleFromInt(int32_t i) { | 120 ImageHandle ImageData::HandleFromInt(int32_t i) { |
150 #if defined(OS_WIN) | 121 #if defined(OS_WIN) |
151 return reinterpret_cast<ImageHandle>(i); | 122 return reinterpret_cast<ImageHandle>(i); |
152 #elif defined(OS_MACOSX) | 123 #elif defined(OS_MACOSX) |
153 return ImageHandle(i, false); | 124 return ImageHandle(i, false); |
154 #else | 125 #else |
155 return static_cast<ImageHandle>(i); | 126 return static_cast<ImageHandle>(i); |
156 #endif | 127 #endif |
157 } | 128 } |
158 | 129 |
159 } // namespace proxy | 130 } // namespace proxy |
160 } // namespace pp | 131 } // namespace pp |
OLD | NEW |