OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "webkit/plugins/ppapi/ppb_image_data_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "skia/ext/platform_canvas.h" | 12 #include "skia/ext/platform_canvas.h" |
13 #include "ppapi/c/pp_instance.h" | 13 #include "ppapi/c/pp_instance.h" |
14 #include "ppapi/c/pp_resource.h" | 14 #include "ppapi/c/pp_resource.h" |
15 #include "ppapi/c/ppb_image_data.h" | 15 #include "ppapi/c/ppb_image_data.h" |
16 #include "ppapi/thunk/thunk.h" | 16 #include "ppapi/thunk/thunk.h" |
17 #include "third_party/skia/include/core/SkColorPriv.h" | 17 #include "third_party/skia/include/core/SkColorPriv.h" |
18 #include "webkit/plugins/ppapi/common.h" | 18 #include "webkit/plugins/ppapi/common.h" |
19 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 19 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
20 #include "webkit/plugins/ppapi/resource_helper.h" | 20 #include "webkit/plugins/ppapi/resource_helper.h" |
21 | 21 |
22 using ::ppapi::thunk::PPB_ImageData_API; | 22 using ::ppapi::thunk::PPB_ImageData_API; |
23 | 23 |
24 namespace webkit { | 24 namespace webkit { |
25 namespace ppapi { | 25 namespace ppapi { |
26 | 26 |
27 PPB_ImageData_Impl::PPB_ImageData_Impl(PP_Instance instance, | 27 PPB_ImageData_Impl::PPB_ImageData_Impl(PP_Instance instance, |
28 ImageDataType type) | 28 PPB_ImageData_Shared::ImageDataType type) |
29 : Resource(::ppapi::OBJECT_IS_IMPL, instance), | 29 : Resource(::ppapi::OBJECT_IS_IMPL, instance), |
30 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), | 30 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), |
31 width_(0), | 31 width_(0), |
32 height_(0) { | 32 height_(0) { |
33 switch (type) { | 33 switch (type) { |
34 case PLATFORM: | 34 case PPB_ImageData_Shared::PLATFORM: |
35 backend_.reset(new ImageDataPlatformBackend); | 35 backend_.reset(new ImageDataPlatformBackend); |
36 return; | 36 return; |
37 case NACL: | 37 case PPB_ImageData_Shared::SIMPLE: |
38 backend_.reset(new ImageDataNaClBackend); | 38 backend_.reset(new ImageDataSimpleBackend); |
39 return; | 39 return; |
40 // No default: so that we get a compiler warning if any types are added. | 40 // No default: so that we get a compiler warning if any types are added. |
41 } | 41 } |
42 NOTREACHED(); | 42 NOTREACHED(); |
43 } | 43 } |
44 | 44 |
45 PPB_ImageData_Impl::~PPB_ImageData_Impl() { | 45 PPB_ImageData_Impl::~PPB_ImageData_Impl() { |
46 } | 46 } |
47 | 47 |
48 bool PPB_ImageData_Impl::Init(PP_ImageDataFormat format, | 48 bool PPB_ImageData_Impl::Init(PP_ImageDataFormat format, |
49 int width, int height, | 49 int width, int height, |
50 bool init_to_zero) { | 50 bool init_to_zero) { |
51 // TODO(brettw) this should be called only on the main thread! | 51 // TODO(brettw) this should be called only on the main thread! |
52 if (!IsImageDataFormatSupported(format)) | 52 if (!IsImageDataFormatSupported(format)) |
53 return false; // Only support this one format for now. | 53 return false; // Only support this one format for now. |
54 if (width <= 0 || height <= 0) | 54 if (width <= 0 || height <= 0) |
55 return false; | 55 return false; |
56 if (static_cast<int64>(width) * static_cast<int64>(height) >= | 56 if (static_cast<int64>(width) * static_cast<int64>(height) >= |
57 std::numeric_limits<int32>::max() / 4) | 57 std::numeric_limits<int32>::max() / 4) |
58 return false; // Prevent overflow of signed 32-bit ints. | 58 return false; // Prevent overflow of signed 32-bit ints. |
59 | 59 |
60 format_ = format; | 60 format_ = format; |
61 width_ = width; | 61 width_ = width; |
62 height_ = height; | 62 height_ = height; |
63 return backend_->Init(this, format, width, height, init_to_zero); | 63 return backend_->Init(this, format, width, height, init_to_zero); |
64 } | 64 } |
65 | 65 |
66 // static | 66 // static |
67 PP_Resource PPB_ImageData_Impl::CreatePlatform(PP_Instance instance, | 67 PP_Resource PPB_ImageData_Impl::Create(PP_Instance instance, |
68 PP_ImageDataFormat format, | 68 PPB_ImageData_Shared::ImageDataType type, |
69 const PP_Size& size, | 69 PP_ImageDataFormat format, |
70 PP_Bool init_to_zero) { | 70 const PP_Size& size, |
| 71 PP_Bool init_to_zero) { |
71 scoped_refptr<PPB_ImageData_Impl> | 72 scoped_refptr<PPB_ImageData_Impl> |
72 data(new PPB_ImageData_Impl(instance, PLATFORM)); | 73 data(new PPB_ImageData_Impl(instance, type)); |
73 if (!data->Init(format, size.width, size.height, !!init_to_zero)) | 74 if (!data->Init(format, size.width, size.height, !!init_to_zero)) |
74 return 0; | 75 return 0; |
75 return data->GetReference(); | 76 return data->GetReference(); |
76 } | |
77 | |
78 // static | |
79 PP_Resource PPB_ImageData_Impl::CreateNaCl(PP_Instance instance, | |
80 PP_ImageDataFormat format, | |
81 const PP_Size& size, | |
82 PP_Bool init_to_zero) { | |
83 scoped_refptr<PPB_ImageData_Impl> | |
84 data(new PPB_ImageData_Impl(instance, NACL)); | |
85 if (!data->Init(format, size.width, size.height, !!init_to_zero)) | |
86 return 0; | |
87 return data->GetReference(); | |
88 } | 77 } |
89 | 78 |
90 PPB_ImageData_API* PPB_ImageData_Impl::AsPPB_ImageData_API() { | 79 PPB_ImageData_API* PPB_ImageData_Impl::AsPPB_ImageData_API() { |
91 return this; | 80 return this; |
92 } | 81 } |
93 | 82 |
94 bool PPB_ImageData_Impl::IsMapped() const { | 83 bool PPB_ImageData_Impl::IsMapped() const { |
95 return backend_->IsMapped(); | 84 return backend_->IsMapped(); |
96 } | 85 } |
97 | 86 |
(...skipping 30 matching lines...) Expand all Loading... |
128 } | 117 } |
129 | 118 |
130 void PPB_ImageData_Impl::SetIsCandidateForReuse() { | 119 void PPB_ImageData_Impl::SetIsCandidateForReuse() { |
131 // Nothing to do since we don't support image data re-use in-process. | 120 // Nothing to do since we don't support image data re-use in-process. |
132 } | 121 } |
133 | 122 |
134 const SkBitmap* PPB_ImageData_Impl::GetMappedBitmap() const { | 123 const SkBitmap* PPB_ImageData_Impl::GetMappedBitmap() const { |
135 return backend_->GetMappedBitmap(); | 124 return backend_->GetMappedBitmap(); |
136 } | 125 } |
137 | 126 |
138 // ImageDataPlatformBackend -------------------------------------------------- | 127 // ImageDataPlatformBackend ---------------------------------------------------- |
139 | 128 |
140 ImageDataPlatformBackend::ImageDataPlatformBackend() { | 129 ImageDataPlatformBackend::ImageDataPlatformBackend() { |
141 } | 130 } |
142 | 131 |
143 ImageDataPlatformBackend::~ImageDataPlatformBackend() { | 132 ImageDataPlatformBackend::~ImageDataPlatformBackend() { |
144 } | 133 } |
145 | 134 |
146 bool ImageDataPlatformBackend::Init(PPB_ImageData_Impl* impl, | 135 bool ImageDataPlatformBackend::Init(PPB_ImageData_Impl* impl, |
147 PP_ImageDataFormat format, | 136 PP_ImageDataFormat format, |
148 int width, int height, | 137 int width, int height, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 SkCanvas* ImageDataPlatformBackend::GetCanvas() { | 190 SkCanvas* ImageDataPlatformBackend::GetCanvas() { |
202 return mapped_canvas_.get(); | 191 return mapped_canvas_.get(); |
203 } | 192 } |
204 | 193 |
205 const SkBitmap* ImageDataPlatformBackend::GetMappedBitmap() const { | 194 const SkBitmap* ImageDataPlatformBackend::GetMappedBitmap() const { |
206 if (!mapped_canvas_) | 195 if (!mapped_canvas_) |
207 return NULL; | 196 return NULL; |
208 return &skia::GetTopDevice(*mapped_canvas_)->accessBitmap(false); | 197 return &skia::GetTopDevice(*mapped_canvas_)->accessBitmap(false); |
209 } | 198 } |
210 | 199 |
211 // ImageDataNaClBackend ------------------------------------------------------ | 200 // ImageDataSimpleBackend ------------------------------------------------------ |
212 | 201 |
213 ImageDataNaClBackend::ImageDataNaClBackend() | 202 ImageDataSimpleBackend::ImageDataSimpleBackend() |
214 : map_count_(0) { | 203 : map_count_(0) { |
215 } | 204 } |
216 | 205 |
217 ImageDataNaClBackend::~ImageDataNaClBackend() { | 206 ImageDataSimpleBackend::~ImageDataSimpleBackend() { |
218 } | 207 } |
219 | 208 |
220 bool ImageDataNaClBackend::Init(PPB_ImageData_Impl* impl, | 209 bool ImageDataSimpleBackend::Init(PPB_ImageData_Impl* impl, |
221 PP_ImageDataFormat format, | 210 PP_ImageDataFormat format, |
222 int width, int height, | 211 int width, int height, |
223 bool init_to_zero) { | 212 bool init_to_zero) { |
224 skia_bitmap_.setConfig(SkBitmap::kARGB_8888_Config, | 213 skia_bitmap_.setConfig(SkBitmap::kARGB_8888_Config, |
225 impl->width(), impl->height()); | 214 impl->width(), impl->height()); |
226 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(impl); | 215 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(impl); |
227 if (!plugin_delegate) | 216 if (!plugin_delegate) |
228 return false; | 217 return false; |
229 shared_memory_.reset( | 218 shared_memory_.reset( |
230 plugin_delegate->CreateAnonymousSharedMemory(skia_bitmap_.getSize())); | 219 plugin_delegate->CreateAnonymousSharedMemory(skia_bitmap_.getSize())); |
231 return !!shared_memory_.get(); | 220 return !!shared_memory_.get(); |
232 } | 221 } |
233 | 222 |
234 bool ImageDataNaClBackend::IsMapped() const { | 223 bool ImageDataSimpleBackend::IsMapped() const { |
235 return map_count_ > 0; | 224 return map_count_ > 0; |
236 } | 225 } |
237 | 226 |
238 PluginDelegate::PlatformImage2D* ImageDataNaClBackend::PlatformImage() const { | 227 PluginDelegate::PlatformImage2D* ImageDataSimpleBackend::PlatformImage() const { |
239 return NULL; | 228 return NULL; |
240 } | 229 } |
241 | 230 |
242 void* ImageDataNaClBackend::Map() { | 231 void* ImageDataSimpleBackend::Map() { |
243 DCHECK(shared_memory_.get()); | 232 DCHECK(shared_memory_.get()); |
244 if (map_count_++ == 0) { | 233 if (map_count_++ == 0) { |
245 shared_memory_->Map(skia_bitmap_.getSize()); | 234 shared_memory_->Map(skia_bitmap_.getSize()); |
246 skia_bitmap_.setPixels(shared_memory_->memory()); | 235 skia_bitmap_.setPixels(shared_memory_->memory()); |
247 // Our platform bitmaps are set to opaque by default, which we don't want. | 236 // Our platform bitmaps are set to opaque by default, which we don't want. |
248 skia_bitmap_.setIsOpaque(false); | 237 skia_bitmap_.setIsOpaque(false); |
249 skia_canvas_.reset(new SkCanvas(skia_bitmap_)); | 238 skia_canvas_.reset(new SkCanvas(skia_bitmap_)); |
250 return skia_bitmap_.getAddr32(0, 0); | 239 return skia_bitmap_.getAddr32(0, 0); |
251 } | 240 } |
252 return shared_memory_->memory(); | 241 return shared_memory_->memory(); |
253 } | 242 } |
254 | 243 |
255 void ImageDataNaClBackend::Unmap() { | 244 void ImageDataSimpleBackend::Unmap() { |
256 if (--map_count_ == 0) | 245 if (--map_count_ == 0) |
257 shared_memory_->Unmap(); | 246 shared_memory_->Unmap(); |
258 } | 247 } |
259 | 248 |
260 int32_t ImageDataNaClBackend::GetSharedMemory(int* handle, | 249 int32_t ImageDataSimpleBackend::GetSharedMemory(int* handle, |
261 uint32_t* byte_count) { | 250 uint32_t* byte_count) { |
262 *byte_count = skia_bitmap_.getSize(); | 251 *byte_count = skia_bitmap_.getSize(); |
263 #if defined(OS_POSIX) | 252 #if defined(OS_POSIX) |
264 *handle = shared_memory_->handle().fd; | 253 *handle = shared_memory_->handle().fd; |
265 #elif defined(OS_WIN) | 254 #elif defined(OS_WIN) |
266 *handle = reinterpret_cast<int>(shared_memory_->handle()); | 255 *handle = reinterpret_cast<int>(shared_memory_->handle()); |
267 #else | 256 #else |
268 #error "Platform not supported." | 257 #error "Platform not supported." |
269 #endif | 258 #endif |
270 return PP_OK; | 259 return PP_OK; |
271 } | 260 } |
272 | 261 |
273 skia::PlatformCanvas* ImageDataNaClBackend::GetPlatformCanvas() { | 262 skia::PlatformCanvas* ImageDataSimpleBackend::GetPlatformCanvas() { |
274 return NULL; | 263 return NULL; |
275 } | 264 } |
276 | 265 |
277 SkCanvas* ImageDataNaClBackend::GetCanvas() { | 266 SkCanvas* ImageDataSimpleBackend::GetCanvas() { |
278 if (!IsMapped()) | 267 if (!IsMapped()) |
279 return NULL; | 268 return NULL; |
280 return skia_canvas_.get(); | 269 return skia_canvas_.get(); |
281 } | 270 } |
282 | 271 |
283 const SkBitmap* ImageDataNaClBackend::GetMappedBitmap() const { | 272 const SkBitmap* ImageDataSimpleBackend::GetMappedBitmap() const { |
284 if (!IsMapped()) | 273 if (!IsMapped()) |
285 return NULL; | 274 return NULL; |
286 return &skia_bitmap_; | 275 return &skia_bitmap_; |
287 } | 276 } |
288 | 277 |
289 } // namespace ppapi | 278 } // namespace ppapi |
290 } // namespace webkit | 279 } // namespace webkit |
OLD | NEW |