Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: webkit/plugins/ppapi/ppb_image_data_impl.cc

Issue 10790063: PPAPI/NaCl: Make ImageData for NaCl just use shared memory (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix component build, minor style fixes Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/c/trusted/ppb_image_data_trusted.h" 16 #include "ppapi/c/trusted/ppb_image_data_trusted.h"
17 #include "ppapi/thunk/thunk.h" 17 #include "ppapi/thunk/thunk.h"
18 #include "third_party/skia/include/core/SkColorPriv.h" 18 #include "third_party/skia/include/core/SkColorPriv.h"
19 #include "webkit/plugins/ppapi/common.h" 19 #include "webkit/plugins/ppapi/common.h"
20 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" 20 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
21 #include "webkit/plugins/ppapi/resource_helper.h" 21 #include "webkit/plugins/ppapi/resource_helper.h"
22 22
23 using ::ppapi::thunk::PPB_ImageData_API; 23 using ::ppapi::thunk::PPB_ImageData_API;
24 24
25 namespace webkit { 25 namespace webkit {
26 namespace ppapi { 26 namespace ppapi {
27 27
28 PPB_ImageData_Impl::PPB_ImageData_Impl(PP_Instance instance) 28 PPB_ImageData_Impl::PPB_ImageData_Impl(PP_Instance instance,
29 ImageDataType type)
29 : Resource(::ppapi::OBJECT_IS_IMPL, instance), 30 : Resource(::ppapi::OBJECT_IS_IMPL, instance),
30 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), 31 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL),
31 width_(0), 32 width_(0),
32 height_(0) { 33 height_(0) {
34 switch (type) {
35 case PLATFORM:
36 delegate_.reset(new ImageDataPlatformDelegate);
37 return;
38 case NACL:
39 delegate_.reset(new ImageDataNaClDelegate);
40 return;
41 // No default: so that we get a compiler warning if any types are added.
42 }
43 NOTREACHED();
33 } 44 }
34 45
35 PPB_ImageData_Impl::~PPB_ImageData_Impl() { 46 PPB_ImageData_Impl::~PPB_ImageData_Impl() {
36 } 47 }
37 48
38 // static
39 PP_Resource PPB_ImageData_Impl::Create(PP_Instance instance,
40 PP_ImageDataFormat format,
41 const PP_Size& size,
42 PP_Bool init_to_zero) {
43 scoped_refptr<PPB_ImageData_Impl> data(new PPB_ImageData_Impl(instance));
44 if (!data->Init(format, size.width, size.height, !!init_to_zero))
45 return 0;
46 return data->GetReference();
47 }
48
49 PPB_ImageData_API* PPB_ImageData_Impl::AsPPB_ImageData_API() {
50 return this;
51 }
52
53 bool PPB_ImageData_Impl::Init(PP_ImageDataFormat format, 49 bool PPB_ImageData_Impl::Init(PP_ImageDataFormat format,
54 int width, int height, 50 int width, int height,
55 bool init_to_zero) { 51 bool init_to_zero) {
56 // TODO(brettw) this should be called only on the main thread! 52 // TODO(brettw) this should be called only on the main thread!
57 // TODO(brettw) use init_to_zero when we implement caching.
58 if (!IsImageDataFormatSupported(format)) 53 if (!IsImageDataFormatSupported(format))
59 return false; // Only support this one format for now. 54 return false; // Only support this one format for now.
60 if (width <= 0 || height <= 0) 55 if (width <= 0 || height <= 0)
61 return false; 56 return false;
62 if (static_cast<int64>(width) * static_cast<int64>(height) * 4 >= 57 if (static_cast<int64>(width) * static_cast<int64>(height) * 4 >=
63 std::numeric_limits<int32>::max()) 58 std::numeric_limits<int32>::max())
64 return false; // Prevent overflow of signed 32-bit ints. 59 return false; // Prevent overflow of signed 32-bit ints.
65 60
66 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this);
67 if (!plugin_delegate)
68 return false;
69
70 platform_image_.reset(plugin_delegate->CreateImage2D(width, height));
71 format_ = format; 61 format_ = format;
72 width_ = width; 62 width_ = width;
73 height_ = height; 63 height_ = height;
74 return !!platform_image_.get(); 64 return delegate_->Init(this, format, width, height, init_to_zero);
65 }
66
67 // static
68 PP_Resource PPB_ImageData_Impl::CreatePlatform(PP_Instance instance,
69 PP_ImageDataFormat format,
70 const PP_Size& size,
71 PP_Bool init_to_zero) {
72 scoped_refptr<PPB_ImageData_Impl>
73 data(new PPB_ImageData_Impl(instance, PLATFORM));
74 if (!data->Init(format, size.width, size.height, !!init_to_zero))
75 return 0;
76 return data->GetReference();
77 }
78
79 // static
80 PP_Resource PPB_ImageData_Impl::CreateNaCl(PP_Instance instance,
81 PP_ImageDataFormat format,
82 const PP_Size& size,
83 PP_Bool init_to_zero) {
84 scoped_refptr<PPB_ImageData_Impl>
85 data(new PPB_ImageData_Impl(instance, NACL));
86 if (!data->Init(format, size.width, size.height, !!init_to_zero))
87 return 0;
88 return data->GetReference();
89 }
90
91 PPB_ImageData_API* PPB_ImageData_Impl::AsPPB_ImageData_API() {
92 return this;
93 }
94
95 bool PPB_ImageData_Impl::IsMapped() const {
96 return delegate_->IsMapped();
97 }
98
99 PluginDelegate::PlatformImage2D* PPB_ImageData_Impl::PlatformImage() const {
100 return delegate_->PlatformImage();
75 } 101 }
76 102
77 PP_Bool PPB_ImageData_Impl::Describe(PP_ImageDataDesc* desc) { 103 PP_Bool PPB_ImageData_Impl::Describe(PP_ImageDataDesc* desc) {
78 desc->format = format_; 104 desc->format = format_;
79 desc->size.width = width_; 105 desc->size.width = width_;
80 desc->size.height = height_; 106 desc->size.height = height_;
81 desc->stride = width_ * 4; 107 desc->stride = width_ * 4;
82 return PP_TRUE; 108 return PP_TRUE;
83 } 109 }
84 110
85 void* PPB_ImageData_Impl::Map() { 111 void* PPB_ImageData_Impl::Map() {
112 return delegate_->Map();
113 }
114
115 void PPB_ImageData_Impl::Unmap() {
116 delegate_->Unmap();
117 }
118
119 int32_t PPB_ImageData_Impl::GetSharedMemory(int* handle, uint32_t* byte_count) {
120 return delegate_->GetSharedMemory(handle, byte_count);
121 }
122
123 skia::PlatformCanvas* PPB_ImageData_Impl::GetPlatformCanvas() {
124 return delegate_->GetPlatformCanvas();
125 }
126
127 SkCanvas* PPB_ImageData_Impl::GetCanvas() {
128 return delegate_->GetCanvas();
129 }
130
131 const SkBitmap* PPB_ImageData_Impl::GetMappedBitmap() const {
132 return delegate_->GetMappedBitmap();
133 }
134
135 void PPB_ImageData_Impl::Swap(PPB_ImageData_Impl* other) {
136 delegate_.swap(other->delegate_);
137 std::swap(other->format_, format_);
138 std::swap(other->width_, width_);
139 std::swap(other->height_, height_);
140 }
141
142 // ImageDataPlatformDelegate --------------------------------------------------
brettw 2012/07/20 21:57:04 I'd do a blank line after this.
dmichael (off chromium) 2012/07/20 22:32:40 Done.
143 ImageDataPlatformDelegate::ImageDataPlatformDelegate() {
144 }
145
146 ImageDataPlatformDelegate::~ImageDataPlatformDelegate() {
147 }
148
149 bool ImageDataPlatformDelegate::Init(PPB_ImageData_Impl* impl,
150 PP_ImageDataFormat format,
151 int width, int height,
152 bool init_to_zero) {
153 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(impl);
154 if (!plugin_delegate)
155 return false;
156
157 // TODO(brettw) use init_to_zero when we implement caching.
158 platform_image_.reset(plugin_delegate->CreateImage2D(width, height));
159 return !!platform_image_.get();
160 }
161
162 bool ImageDataPlatformDelegate::IsMapped() const {
163 return !!mapped_canvas_.get();
164 }
165
166 PluginDelegate::PlatformImage2D*
167 ImageDataPlatformDelegate::PlatformImage() const {
168 return platform_image_.get();
169 }
170
171 void* ImageDataPlatformDelegate::Map() {
86 if (!mapped_canvas_.get()) { 172 if (!mapped_canvas_.get()) {
87 mapped_canvas_.reset(platform_image_->Map()); 173 mapped_canvas_.reset(platform_image_->Map());
88 if (!mapped_canvas_.get()) 174 if (!mapped_canvas_.get())
89 return NULL; 175 return NULL;
90 } 176 }
91 const SkBitmap& bitmap = 177 const SkBitmap& bitmap =
92 skia::GetTopDevice(*mapped_canvas_)->accessBitmap(true); 178 skia::GetTopDevice(*mapped_canvas_)->accessBitmap(true);
93 179
94 // Our platform bitmaps are set to opaque by default, which we don't want. 180 // Our platform bitmaps are set to opaque by default, which we don't want.
95 const_cast<SkBitmap&>(bitmap).setIsOpaque(false); 181 const_cast<SkBitmap&>(bitmap).setIsOpaque(false);
96 182
97 bitmap.lockPixels(); 183 bitmap.lockPixels();
98 return bitmap.getAddr32(0, 0); 184 return bitmap.getAddr32(0, 0);
99 } 185 }
100 186
101 void PPB_ImageData_Impl::Unmap() { 187 void ImageDataPlatformDelegate::Unmap() {
102 // This is currently unimplemented, which is OK. The data will just always 188 // This is currently unimplemented, which is OK. The data will just always
103 // be around once it's mapped. Chrome's TransportDIB isn't currently 189 // be around once it's mapped. Chrome's TransportDIB isn't currently
104 // unmappable without freeing it, but this may be something we want to support 190 // unmappable without freeing it, but this may be something we want to support
105 // in the future to save some memory. 191 // in the future to save some memory.
106 } 192 }
107 193
108 int32_t PPB_ImageData_Impl::GetSharedMemory(int* handle, 194 int32_t ImageDataPlatformDelegate::GetSharedMemory(int* handle,
109 uint32_t* byte_count) { 195 uint32_t* byte_count) {
110 *handle = platform_image_->GetSharedMemoryHandle(byte_count); 196 *handle = platform_image_->GetSharedMemoryHandle(byte_count);
111 return PP_OK; 197 return PP_OK;
112 } 198 }
113 199
114 skia::PlatformCanvas* PPB_ImageData_Impl::GetPlatformCanvas() { 200 skia::PlatformCanvas* ImageDataPlatformDelegate::GetPlatformCanvas() {
115 return mapped_canvas_.get(); 201 return mapped_canvas_.get();
116 } 202 }
117 203
118 const SkBitmap* PPB_ImageData_Impl::GetMappedBitmap() const { 204 SkCanvas* ImageDataPlatformDelegate::GetCanvas() {
205 return mapped_canvas_.get();
206 }
207
208 const SkBitmap* ImageDataPlatformDelegate::GetMappedBitmap() const {
119 if (!mapped_canvas_.get()) 209 if (!mapped_canvas_.get())
120 return NULL; 210 return NULL;
121 return &skia::GetTopDevice(*mapped_canvas_)->accessBitmap(false); 211 return &skia::GetTopDevice(*mapped_canvas_)->accessBitmap(false);
122 } 212 }
123 213
124 void PPB_ImageData_Impl::Swap(PPB_ImageData_Impl* other) { 214 // ImageDataNaClDelegate ------------------------------------------------------
125 swap(other->platform_image_, platform_image_); 215 ImageDataNaClDelegate::ImageDataNaClDelegate()
126 swap(other->mapped_canvas_, mapped_canvas_); 216 : map_count_(0) {
127 std::swap(other->format_, format_); 217 }
128 std::swap(other->width_, width_); 218
129 std::swap(other->height_, height_); 219 ImageDataNaClDelegate::~ImageDataNaClDelegate() {
220 }
221
222 bool ImageDataNaClDelegate::Init(PPB_ImageData_Impl* impl,
223 PP_ImageDataFormat format,
224 int width, int height,
225 bool init_to_zero) {
226 skia_bitmap_.setConfig(SkBitmap::kARGB_8888_Config,
227 impl->width(), impl->height());
228 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(impl);
229 if (!plugin_delegate)
230 return false;
231 shared_memory_.reset(
232 plugin_delegate->CreateAnonymousSharedMemory(skia_bitmap_.getSize()));
233 return !!shared_memory_.get();
234 }
235
236 bool ImageDataNaClDelegate::IsMapped() const {
237 return map_count_ > 0;
238 }
239
240 PluginDelegate::PlatformImage2D* ImageDataNaClDelegate::PlatformImage() const {
241 return NULL;
242 }
243
244 void* ImageDataNaClDelegate::Map() {
245 DCHECK(shared_memory_.get());
246 if (map_count_++ == 0) {
247 shared_memory_->Map(skia_bitmap_.getSize());
248 skia_bitmap_.setPixels(shared_memory_->memory());
249 // Our platform bitmaps are set to opaque by default, which we don't want.
250 skia_bitmap_.setIsOpaque(false);
251 // TODO(dmichael): Is lockPixels necessary? We own the pixels:
brettw 2012/07/20 21:57:04 I'm pretty sure it isn't necessary. I think assign
dmichael (off chromium) 2012/07/20 22:32:40 Okay, I'll take the lock and unlock out until we h
252 skia_bitmap_.lockPixels();
253 skia_canvas_.setBitmapDevice(skia_bitmap_);
254 return skia_bitmap_.getAddr32(0, 0);
255 }
256 return shared_memory_->memory();
257 }
258
259 void ImageDataNaClDelegate::Unmap() {
260 if (--map_count_ == 0) {
261 shared_memory_->Unmap();
262 skia_bitmap_.unlockPixels();
brettw 2012/07/20 21:57:04 I'm going to add Mike Reed to check if this is cor
263 }
264 }
265
266 int32_t ImageDataNaClDelegate::GetSharedMemory(int* handle,
267 uint32_t* byte_count) {
268 *byte_count = skia_bitmap_.getSize();
269 #if defined(OS_POSIX)
270 *handle = shared_memory_->handle().fd;
271 #elif defined(OS_WIN)
272 *handle = reinterpret_cast<int>(shared_memory_->handle());
273 #else
274 #error "Platform not supported."
275 #endif
276 return PP_OK;
277 }
278
279 skia::PlatformCanvas* ImageDataNaClDelegate::GetPlatformCanvas() {
280 return NULL;
281 }
282
283 SkCanvas* ImageDataNaClDelegate::GetCanvas() {
284 if (!IsMapped())
285 return NULL;
286 return &skia_canvas_;
287 }
288
289 const SkBitmap* ImageDataNaClDelegate::GetMappedBitmap() const {
290 if (!IsMapped())
291 return NULL;
292 return &skia_bitmap_;
130 } 293 }
131 294
132 } // namespace ppapi 295 } // namespace ppapi
133 } // namespace webkit 296 } // namespace webkit
134 297
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698