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

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: make PPB_ImageData_API base non-exported Created 8 years, 4 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 backend_.reset(new ImageDataPlatformBackend);
37 return;
38 case NACL:
39 backend_.reset(new ImageDataNaClBackend);
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 backend_->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 backend_->IsMapped();
97 }
98
99 PluginDelegate::PlatformImage2D* PPB_ImageData_Impl::PlatformImage() const {
100 return backend_->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 backend_->Map();
113 }
114
115 void PPB_ImageData_Impl::Unmap() {
116 backend_->Unmap();
117 }
118
119 int32_t PPB_ImageData_Impl::GetSharedMemory(int* handle, uint32_t* byte_count) {
120 return backend_->GetSharedMemory(handle, byte_count);
121 }
122
123 skia::PlatformCanvas* PPB_ImageData_Impl::GetPlatformCanvas() {
124 return backend_->GetPlatformCanvas();
125 }
126
127 SkCanvas* PPB_ImageData_Impl::GetCanvas() {
128 return backend_->GetCanvas();
129 }
130
131 const SkBitmap* PPB_ImageData_Impl::GetMappedBitmap() const {
132 return backend_->GetMappedBitmap();
133 }
134
135 void PPB_ImageData_Impl::Swap(PPB_ImageData_Impl* other) {
136 backend_.swap(other->backend_);
137 std::swap(other->format_, format_);
138 std::swap(other->width_, width_);
139 std::swap(other->height_, height_);
140 }
141
142 // ImageDataPlatformBackend --------------------------------------------------
143
144 ImageDataPlatformBackend::ImageDataPlatformBackend() {
145 }
146
147 ImageDataPlatformBackend::~ImageDataPlatformBackend() {
148 }
149
150 bool ImageDataPlatformBackend::Init(PPB_ImageData_Impl* impl,
151 PP_ImageDataFormat format,
152 int width, int height,
153 bool init_to_zero) {
154 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(impl);
155 if (!plugin_delegate)
156 return false;
157
158 // TODO(brettw) use init_to_zero when we implement caching.
159 platform_image_.reset(plugin_delegate->CreateImage2D(width, height));
160 return !!platform_image_.get();
161 }
162
163 bool ImageDataPlatformBackend::IsMapped() const {
164 return !!mapped_canvas_.get();
165 }
166
167 PluginDelegate::PlatformImage2D*
168 ImageDataPlatformBackend::PlatformImage() const {
169 return platform_image_.get();
170 }
171
172 void* ImageDataPlatformBackend::Map() {
86 if (!mapped_canvas_.get()) { 173 if (!mapped_canvas_.get()) {
87 mapped_canvas_.reset(platform_image_->Map()); 174 mapped_canvas_.reset(platform_image_->Map());
88 if (!mapped_canvas_.get()) 175 if (!mapped_canvas_.get())
89 return NULL; 176 return NULL;
90 } 177 }
91 const SkBitmap& bitmap = 178 const SkBitmap& bitmap =
92 skia::GetTopDevice(*mapped_canvas_)->accessBitmap(true); 179 skia::GetTopDevice(*mapped_canvas_)->accessBitmap(true);
93 180
94 // Our platform bitmaps are set to opaque by default, which we don't want. 181 // Our platform bitmaps are set to opaque by default, which we don't want.
95 const_cast<SkBitmap&>(bitmap).setIsOpaque(false); 182 const_cast<SkBitmap&>(bitmap).setIsOpaque(false);
96 183
97 bitmap.lockPixels(); 184 bitmap.lockPixels();
98 return bitmap.getAddr32(0, 0); 185 return bitmap.getAddr32(0, 0);
99 } 186 }
100 187
101 void PPB_ImageData_Impl::Unmap() { 188 void ImageDataPlatformBackend::Unmap() {
102 // This is currently unimplemented, which is OK. The data will just always 189 // 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 190 // 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 191 // unmappable without freeing it, but this may be something we want to support
105 // in the future to save some memory. 192 // in the future to save some memory.
106 } 193 }
107 194
108 int32_t PPB_ImageData_Impl::GetSharedMemory(int* handle, 195 int32_t ImageDataPlatformBackend::GetSharedMemory(int* handle,
109 uint32_t* byte_count) { 196 uint32_t* byte_count) {
110 *handle = platform_image_->GetSharedMemoryHandle(byte_count); 197 *handle = platform_image_->GetSharedMemoryHandle(byte_count);
111 return PP_OK; 198 return PP_OK;
112 } 199 }
113 200
114 skia::PlatformCanvas* PPB_ImageData_Impl::GetPlatformCanvas() { 201 skia::PlatformCanvas* ImageDataPlatformBackend::GetPlatformCanvas() {
115 return mapped_canvas_.get(); 202 return mapped_canvas_.get();
116 } 203 }
117 204
118 const SkBitmap* PPB_ImageData_Impl::GetMappedBitmap() const { 205 SkCanvas* ImageDataPlatformBackend::GetCanvas() {
206 return mapped_canvas_.get();
207 }
208
209 const SkBitmap* ImageDataPlatformBackend::GetMappedBitmap() const {
119 if (!mapped_canvas_.get()) 210 if (!mapped_canvas_.get())
120 return NULL; 211 return NULL;
121 return &skia::GetTopDevice(*mapped_canvas_)->accessBitmap(false); 212 return &skia::GetTopDevice(*mapped_canvas_)->accessBitmap(false);
122 } 213 }
123 214
124 void PPB_ImageData_Impl::Swap(PPB_ImageData_Impl* other) { 215 // ImageDataNaClBackend ------------------------------------------------------
125 swap(other->platform_image_, platform_image_); 216
126 swap(other->mapped_canvas_, mapped_canvas_); 217 ImageDataNaClBackend::ImageDataNaClBackend()
127 std::swap(other->format_, format_); 218 : map_count_(0) {
128 std::swap(other->width_, width_); 219 }
129 std::swap(other->height_, height_); 220
221 ImageDataNaClBackend::~ImageDataNaClBackend() {
222 }
223
224 bool ImageDataNaClBackend::Init(PPB_ImageData_Impl* impl,
225 PP_ImageDataFormat format,
226 int width, int height,
227 bool init_to_zero) {
228 skia_bitmap_.setConfig(SkBitmap::kARGB_8888_Config,
229 impl->width(), impl->height());
230 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(impl);
231 if (!plugin_delegate)
232 return false;
233 shared_memory_.reset(
234 plugin_delegate->CreateAnonymousSharedMemory(skia_bitmap_.getSize()));
235 return !!shared_memory_.get();
236 }
237
238 bool ImageDataNaClBackend::IsMapped() const {
239 return map_count_ > 0;
240 }
241
242 PluginDelegate::PlatformImage2D* ImageDataNaClBackend::PlatformImage() const {
243 return NULL;
244 }
245
246 void* ImageDataNaClBackend::Map() {
247 DCHECK(shared_memory_.get());
248 if (map_count_++ == 0) {
249 shared_memory_->Map(skia_bitmap_.getSize());
250 skia_bitmap_.setPixels(shared_memory_->memory());
251 // Our platform bitmaps are set to opaque by default, which we don't want.
252 skia_bitmap_.setIsOpaque(false);
253 skia_canvas_.setBitmapDevice(skia_bitmap_);
254 return skia_bitmap_.getAddr32(0, 0);
255 }
256 return shared_memory_->memory();
257 }
258
259 void ImageDataNaClBackend::Unmap() {
260 if (--map_count_ == 0)
261 shared_memory_->Unmap();
262 }
263
264 int32_t ImageDataNaClBackend::GetSharedMemory(int* handle,
265 uint32_t* byte_count) {
266 *byte_count = skia_bitmap_.getSize();
267 #if defined(OS_POSIX)
268 *handle = shared_memory_->handle().fd;
269 #elif defined(OS_WIN)
270 *handle = reinterpret_cast<int>(shared_memory_->handle());
271 #else
272 #error "Platform not supported."
273 #endif
274 return PP_OK;
275 }
276
277 skia::PlatformCanvas* ImageDataNaClBackend::GetPlatformCanvas() {
278 return NULL;
279 }
280
281 SkCanvas* ImageDataNaClBackend::GetCanvas() {
282 if (!IsMapped())
283 return NULL;
284 return &skia_canvas_;
285 }
286
287 const SkBitmap* ImageDataNaClBackend::GetMappedBitmap() const {
288 if (!IsMapped())
289 return NULL;
290 return &skia_bitmap_;
130 } 291 }
131 292
132 } // namespace ppapi 293 } // namespace ppapi
133 } // namespace webkit 294 } // namespace webkit
134 295
OLDNEW
« no previous file with comments | « webkit/plugins/ppapi/ppb_image_data_impl.h ('k') | webkit/plugins/ppapi/resource_creation_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698