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 "content/renderer/pepper/ppb_image_data_impl.h" | 5 #include "content/renderer/pepper/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" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), | 44 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), |
45 width_(0), | 45 width_(0), |
46 height_(0) { | 46 height_(0) { |
47 switch (type) { | 47 switch (type) { |
48 case PPB_ImageData_Shared::PLATFORM: | 48 case PPB_ImageData_Shared::PLATFORM: |
49 backend_.reset(new ImageDataPlatformBackend(IsBrowserAllocated())); | 49 backend_.reset(new ImageDataPlatformBackend(IsBrowserAllocated())); |
50 return; | 50 return; |
51 case PPB_ImageData_Shared::SIMPLE: | 51 case PPB_ImageData_Shared::SIMPLE: |
52 backend_.reset(new ImageDataSimpleBackend); | 52 backend_.reset(new ImageDataSimpleBackend); |
53 return; | 53 return; |
54 // No default: so that we get a compiler warning if any types are added. | 54 // No default: so that we get a compiler warning if any types are added. |
55 } | 55 } |
56 NOTREACHED(); | 56 NOTREACHED(); |
57 } | 57 } |
58 | 58 |
59 PPB_ImageData_Impl::PPB_ImageData_Impl(PP_Instance instance, | 59 PPB_ImageData_Impl::PPB_ImageData_Impl(PP_Instance instance, ForTest) |
60 ForTest) | |
61 : Resource(ppapi::OBJECT_IS_IMPL, instance), | 60 : Resource(ppapi::OBJECT_IS_IMPL, instance), |
62 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), | 61 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), |
63 width_(0), | 62 width_(0), |
64 height_(0) { | 63 height_(0) { |
65 backend_.reset(new ImageDataPlatformBackend(false)); | 64 backend_.reset(new ImageDataPlatformBackend(false)); |
66 } | 65 } |
67 | 66 |
68 PPB_ImageData_Impl::~PPB_ImageData_Impl() { | 67 PPB_ImageData_Impl::~PPB_ImageData_Impl() {} |
69 } | |
70 | 68 |
71 bool PPB_ImageData_Impl::Init(PP_ImageDataFormat format, | 69 bool PPB_ImageData_Impl::Init(PP_ImageDataFormat format, |
72 int width, int height, | 70 int width, |
| 71 int height, |
73 bool init_to_zero) { | 72 bool init_to_zero) { |
74 // TODO(brettw) this should be called only on the main thread! | 73 // TODO(brettw) this should be called only on the main thread! |
75 if (!IsImageDataFormatSupported(format)) | 74 if (!IsImageDataFormatSupported(format)) |
76 return false; // Only support this one format for now. | 75 return false; // Only support this one format for now. |
77 if (width <= 0 || height <= 0) | 76 if (width <= 0 || height <= 0) |
78 return false; | 77 return false; |
79 if (static_cast<int64>(width) * static_cast<int64>(height) >= | 78 if (static_cast<int64>(width) * static_cast<int64>(height) >= |
80 std::numeric_limits<int32>::max() / 4) | 79 std::numeric_limits<int32>::max() / 4) |
81 return false; // Prevent overflow of signed 32-bit ints. | 80 return false; // Prevent overflow of signed 32-bit ints. |
82 | 81 |
83 format_ = format; | 82 format_ = format; |
84 width_ = width; | 83 width_ = width; |
85 height_ = height; | 84 height_ = height; |
86 return backend_->Init(this, format, width, height, init_to_zero); | 85 return backend_->Init(this, format, width, height, init_to_zero); |
87 } | 86 } |
88 | 87 |
89 // static | 88 // static |
90 PP_Resource PPB_ImageData_Impl::Create(PP_Instance instance, | 89 PP_Resource PPB_ImageData_Impl::Create(PP_Instance instance, |
91 PPB_ImageData_Shared::ImageDataType type, | 90 PPB_ImageData_Shared::ImageDataType type, |
92 PP_ImageDataFormat format, | 91 PP_ImageDataFormat format, |
93 const PP_Size& size, | 92 const PP_Size& size, |
94 PP_Bool init_to_zero) { | 93 PP_Bool init_to_zero) { |
95 scoped_refptr<PPB_ImageData_Impl> | 94 scoped_refptr<PPB_ImageData_Impl> data( |
96 data(new PPB_ImageData_Impl(instance, type)); | 95 new PPB_ImageData_Impl(instance, type)); |
97 if (!data->Init(format, size.width, size.height, !!init_to_zero)) | 96 if (!data->Init(format, size.width, size.height, !!init_to_zero)) |
98 return 0; | 97 return 0; |
99 return data->GetReference(); | 98 return data->GetReference(); |
100 } | 99 } |
101 | 100 |
102 PPB_ImageData_API* PPB_ImageData_Impl::AsPPB_ImageData_API() { | 101 PPB_ImageData_API* PPB_ImageData_Impl::AsPPB_ImageData_API() { return this; } |
103 return this; | |
104 } | |
105 | 102 |
106 bool PPB_ImageData_Impl::IsMapped() const { | 103 bool PPB_ImageData_Impl::IsMapped() const { return backend_->IsMapped(); } |
107 return backend_->IsMapped(); | |
108 } | |
109 | 104 |
110 TransportDIB* PPB_ImageData_Impl::GetTransportDIB() const { | 105 TransportDIB* PPB_ImageData_Impl::GetTransportDIB() const { |
111 return backend_->GetTransportDIB(); | 106 return backend_->GetTransportDIB(); |
112 } | 107 } |
113 | 108 |
114 PP_Bool PPB_ImageData_Impl::Describe(PP_ImageDataDesc* desc) { | 109 PP_Bool PPB_ImageData_Impl::Describe(PP_ImageDataDesc* desc) { |
115 desc->format = format_; | 110 desc->format = format_; |
116 desc->size.width = width_; | 111 desc->size.width = width_; |
117 desc->size.height = height_; | 112 desc->size.height = height_; |
118 desc->stride = width_ * 4; | 113 desc->stride = width_ * 4; |
119 return PP_TRUE; | 114 return PP_TRUE; |
120 } | 115 } |
121 | 116 |
122 void* PPB_ImageData_Impl::Map() { | 117 void* PPB_ImageData_Impl::Map() { return backend_->Map(); } |
123 return backend_->Map(); | |
124 } | |
125 | 118 |
126 void PPB_ImageData_Impl::Unmap() { | 119 void PPB_ImageData_Impl::Unmap() { backend_->Unmap(); } |
127 backend_->Unmap(); | |
128 } | |
129 | 120 |
130 int32_t PPB_ImageData_Impl::GetSharedMemory(int* handle, uint32_t* byte_count) { | 121 int32_t PPB_ImageData_Impl::GetSharedMemory(int* handle, uint32_t* byte_count) { |
131 return backend_->GetSharedMemory(handle, byte_count); | 122 return backend_->GetSharedMemory(handle, byte_count); |
132 } | 123 } |
133 | 124 |
134 skia::PlatformCanvas* PPB_ImageData_Impl::GetPlatformCanvas() { | 125 skia::PlatformCanvas* PPB_ImageData_Impl::GetPlatformCanvas() { |
135 return backend_->GetPlatformCanvas(); | 126 return backend_->GetPlatformCanvas(); |
136 } | 127 } |
137 | 128 |
138 SkCanvas* PPB_ImageData_Impl::GetCanvas() { | 129 SkCanvas* PPB_ImageData_Impl::GetCanvas() { return backend_->GetCanvas(); } |
139 return backend_->GetCanvas(); | |
140 } | |
141 | 130 |
142 void PPB_ImageData_Impl::SetIsCandidateForReuse() { | 131 void PPB_ImageData_Impl::SetIsCandidateForReuse() { |
143 // Nothing to do since we don't support image data re-use in-process. | 132 // Nothing to do since we don't support image data re-use in-process. |
144 } | 133 } |
145 | 134 |
146 const SkBitmap* PPB_ImageData_Impl::GetMappedBitmap() const { | 135 const SkBitmap* PPB_ImageData_Impl::GetMappedBitmap() const { |
147 return backend_->GetMappedBitmap(); | 136 return backend_->GetMappedBitmap(); |
148 } | 137 } |
149 | 138 |
150 // ImageDataPlatformBackend ---------------------------------------------------- | 139 // ImageDataPlatformBackend ---------------------------------------------------- |
151 | 140 |
152 ImageDataPlatformBackend::ImageDataPlatformBackend(bool is_browser_allocated) | 141 ImageDataPlatformBackend::ImageDataPlatformBackend(bool is_browser_allocated) |
153 : width_(0), | 142 : width_(0), height_(0), is_browser_allocated_(is_browser_allocated) {} |
154 height_(0), | |
155 is_browser_allocated_(is_browser_allocated) { | |
156 } | |
157 | 143 |
158 // On POSIX, we have to tell the browser to free the transport DIB. | 144 // On POSIX, we have to tell the browser to free the transport DIB. |
159 ImageDataPlatformBackend::~ImageDataPlatformBackend() { | 145 ImageDataPlatformBackend::~ImageDataPlatformBackend() { |
160 if (is_browser_allocated_) { | 146 if (is_browser_allocated_) { |
161 #if defined(OS_POSIX) | 147 #if defined(OS_POSIX) |
162 if (dib_) { | 148 if (dib_) { |
163 RenderThreadImpl::current()->Send( | 149 RenderThreadImpl::current()->Send( |
164 new ViewHostMsg_FreeTransportDIB(dib_->id())); | 150 new ViewHostMsg_FreeTransportDIB(dib_->id())); |
165 } | 151 } |
166 #endif | 152 #endif |
167 } | 153 } |
168 } | 154 } |
169 | 155 |
170 bool ImageDataPlatformBackend::Init(PPB_ImageData_Impl* impl, | 156 bool ImageDataPlatformBackend::Init(PPB_ImageData_Impl* impl, |
171 PP_ImageDataFormat format, | 157 PP_ImageDataFormat format, |
172 int width, int height, | 158 int width, |
| 159 int height, |
173 bool init_to_zero) { | 160 bool init_to_zero) { |
174 // TODO(brettw) use init_to_zero when we implement caching. | 161 // TODO(brettw) use init_to_zero when we implement caching. |
175 width_ = width; | 162 width_ = width; |
176 height_ = height; | 163 height_ = height; |
177 uint32 buffer_size = width_ * height_ * 4; | 164 uint32 buffer_size = width_ * height_ * 4; |
178 | 165 |
179 // Allocate the transport DIB and the PlatformCanvas pointing to it. | 166 // Allocate the transport DIB and the PlatformCanvas pointing to it. |
180 TransportDIB* dib = NULL; | 167 TransportDIB* dib = NULL; |
181 if (is_browser_allocated_) { | 168 if (is_browser_allocated_) { |
182 #if defined(OS_POSIX) | 169 #if defined(OS_POSIX) |
183 // Allocate the image data by sending a message to the browser requesting a | 170 // Allocate the image data by sending a message to the browser requesting a |
184 // TransportDIB (see also chrome/renderer/webplugin_delegate_proxy.cc, | 171 // TransportDIB (see also chrome/renderer/webplugin_delegate_proxy.cc, |
185 // method WebPluginDelegateProxy::CreateBitmap() for similar code). The | 172 // method WebPluginDelegateProxy::CreateBitmap() for similar code). The |
186 // TransportDIB is cached in the browser, and is freed (in typical cases) by | 173 // TransportDIB is cached in the browser, and is freed (in typical cases) by |
187 // the TransportDIB's destructor. | 174 // the TransportDIB's destructor. |
188 TransportDIB::Handle dib_handle; | 175 TransportDIB::Handle dib_handle; |
189 IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(buffer_size, | 176 IPC::Message* msg = |
190 true, | 177 new ViewHostMsg_AllocTransportDIB(buffer_size, true, &dib_handle); |
191 &dib_handle); | |
192 if (!RenderThreadImpl::current()->Send(msg)) | 178 if (!RenderThreadImpl::current()->Send(msg)) |
193 return false; | 179 return false; |
194 if (!TransportDIB::is_valid_handle(dib_handle)) | 180 if (!TransportDIB::is_valid_handle(dib_handle)) |
195 return false; | 181 return false; |
196 | 182 |
197 dib = TransportDIB::CreateWithHandle(dib_handle); | 183 dib = TransportDIB::CreateWithHandle(dib_handle); |
198 #endif | 184 #endif |
199 } else { | 185 } else { |
200 static int next_dib_id = 0; | 186 static int next_dib_id = 0; |
201 dib = TransportDIB::Create(buffer_size, next_dib_id++); | 187 dib = TransportDIB::Create(buffer_size, next_dib_id++); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 *handle = static_cast<intptr_t>(dib_->handle().fd); | 235 *handle = static_cast<intptr_t>(dib_->handle().fd); |
250 #endif | 236 #endif |
251 | 237 |
252 return PP_OK; | 238 return PP_OK; |
253 } | 239 } |
254 | 240 |
255 skia::PlatformCanvas* ImageDataPlatformBackend::GetPlatformCanvas() { | 241 skia::PlatformCanvas* ImageDataPlatformBackend::GetPlatformCanvas() { |
256 return mapped_canvas_.get(); | 242 return mapped_canvas_.get(); |
257 } | 243 } |
258 | 244 |
259 SkCanvas* ImageDataPlatformBackend::GetCanvas() { | 245 SkCanvas* ImageDataPlatformBackend::GetCanvas() { return mapped_canvas_.get(); } |
260 return mapped_canvas_.get(); | |
261 } | |
262 | 246 |
263 const SkBitmap* ImageDataPlatformBackend::GetMappedBitmap() const { | 247 const SkBitmap* ImageDataPlatformBackend::GetMappedBitmap() const { |
264 if (!mapped_canvas_) | 248 if (!mapped_canvas_) |
265 return NULL; | 249 return NULL; |
266 return &skia::GetTopDevice(*mapped_canvas_)->accessBitmap(false); | 250 return &skia::GetTopDevice(*mapped_canvas_)->accessBitmap(false); |
267 } | 251 } |
268 | 252 |
269 // ImageDataSimpleBackend ------------------------------------------------------ | 253 // ImageDataSimpleBackend ------------------------------------------------------ |
270 | 254 |
271 ImageDataSimpleBackend::ImageDataSimpleBackend() | 255 ImageDataSimpleBackend::ImageDataSimpleBackend() : map_count_(0) {} |
272 : map_count_(0) { | |
273 } | |
274 | 256 |
275 ImageDataSimpleBackend::~ImageDataSimpleBackend() { | 257 ImageDataSimpleBackend::~ImageDataSimpleBackend() {} |
276 } | |
277 | 258 |
278 bool ImageDataSimpleBackend::Init(PPB_ImageData_Impl* impl, | 259 bool ImageDataSimpleBackend::Init(PPB_ImageData_Impl* impl, |
279 PP_ImageDataFormat format, | 260 PP_ImageDataFormat format, |
280 int width, int height, | 261 int width, |
| 262 int height, |
281 bool init_to_zero) { | 263 bool init_to_zero) { |
282 skia_bitmap_.setConfig(SkBitmap::kARGB_8888_Config, | 264 skia_bitmap_.setConfig( |
283 impl->width(), impl->height()); | 265 SkBitmap::kARGB_8888_Config, impl->width(), impl->height()); |
284 shared_memory_.reset(RenderThread::Get()->HostAllocateSharedMemoryBuffer( | 266 shared_memory_.reset( |
285 skia_bitmap_.getSize()).release()); | 267 RenderThread::Get() |
| 268 ->HostAllocateSharedMemoryBuffer(skia_bitmap_.getSize()) |
| 269 .release()); |
286 return !!shared_memory_.get(); | 270 return !!shared_memory_.get(); |
287 } | 271 } |
288 | 272 |
289 bool ImageDataSimpleBackend::IsMapped() const { | 273 bool ImageDataSimpleBackend::IsMapped() const { return map_count_ > 0; } |
290 return map_count_ > 0; | |
291 } | |
292 | 274 |
293 TransportDIB* ImageDataSimpleBackend::GetTransportDIB() const { | 275 TransportDIB* ImageDataSimpleBackend::GetTransportDIB() const { return NULL; } |
294 return NULL; | |
295 } | |
296 | 276 |
297 void* ImageDataSimpleBackend::Map() { | 277 void* ImageDataSimpleBackend::Map() { |
298 DCHECK(shared_memory_.get()); | 278 DCHECK(shared_memory_.get()); |
299 if (map_count_++ == 0) { | 279 if (map_count_++ == 0) { |
300 shared_memory_->Map(skia_bitmap_.getSize()); | 280 shared_memory_->Map(skia_bitmap_.getSize()); |
301 skia_bitmap_.setPixels(shared_memory_->memory()); | 281 skia_bitmap_.setPixels(shared_memory_->memory()); |
302 // Our platform bitmaps are set to opaque by default, which we don't want. | 282 // Our platform bitmaps are set to opaque by default, which we don't want. |
303 skia_bitmap_.setAlphaType(kPremul_SkAlphaType); | 283 skia_bitmap_.setAlphaType(kPremul_SkAlphaType); |
304 skia_canvas_.reset(new SkCanvas(skia_bitmap_)); | 284 skia_canvas_.reset(new SkCanvas(skia_bitmap_)); |
305 return skia_bitmap_.getAddr32(0, 0); | 285 return skia_bitmap_.getAddr32(0, 0); |
(...skipping 29 matching lines...) Expand all Loading... |
335 return skia_canvas_.get(); | 315 return skia_canvas_.get(); |
336 } | 316 } |
337 | 317 |
338 const SkBitmap* ImageDataSimpleBackend::GetMappedBitmap() const { | 318 const SkBitmap* ImageDataSimpleBackend::GetMappedBitmap() const { |
339 if (!IsMapped()) | 319 if (!IsMapped()) |
340 return NULL; | 320 return NULL; |
341 return &skia_bitmap_; | 321 return &skia_bitmap_; |
342 } | 322 } |
343 | 323 |
344 } // namespace content | 324 } // namespace content |
OLD | NEW |