| 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" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "content/common/view_messages.h" | 12 #include "content/common/view_messages.h" |
| 13 #include "content/renderer/pepper/common.h" | 13 #include "content/renderer/pepper/common.h" |
| 14 #include "content/renderer/render_thread_impl.h" | 14 #include "content/renderer/render_thread_impl.h" |
| 15 #include "ppapi/c/pp_errors.h" | 15 #include "ppapi/c/pp_errors.h" |
| 16 #include "ppapi/c/pp_instance.h" | 16 #include "ppapi/c/pp_instance.h" |
| 17 #include "ppapi/c/pp_resource.h" | 17 #include "ppapi/c/pp_resource.h" |
| 18 #include "ppapi/c/ppb_image_data.h" | 18 #include "ppapi/c/ppb_image_data.h" |
| 19 #include "ppapi/thunk/thunk.h" | 19 #include "ppapi/thunk/thunk.h" |
| 20 #include "skia/ext/platform_canvas.h" | 20 #include "skia/ext/platform_canvas.h" |
| 21 #include "third_party/skia/include/core/SkColorPriv.h" | 21 #include "third_party/skia/include/core/SkColorPriv.h" |
| 22 #include "ui/surface/transport_dib.h" | 22 #include "ui/surface/transport_dib.h" |
| 23 | 23 |
| 24 using ppapi::thunk::PPB_ImageData_API; | 24 using ppapi::thunk::PPB_ImageData_API; |
| 25 | 25 |
| 26 namespace content { | 26 namespace content { |
| 27 | 27 |
| 28 namespace { |
| 29 // Returns true if the ImageData shared memory should be allocated in the |
| 30 // browser process for the current platform. |
| 31 bool IsBrowserAllocated() { |
| 32 #if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID) |
| 33 // On the Mac, shared memory has to be created in the browser in order to |
| 34 // work in the sandbox. |
| 35 return true; |
| 36 #endif |
| 37 return false; |
| 38 } |
| 39 } // namespace |
| 40 |
| 28 PPB_ImageData_Impl::PPB_ImageData_Impl(PP_Instance instance, | 41 PPB_ImageData_Impl::PPB_ImageData_Impl(PP_Instance instance, |
| 29 PPB_ImageData_Shared::ImageDataType type) | 42 PPB_ImageData_Shared::ImageDataType type) |
| 30 : Resource(ppapi::OBJECT_IS_IMPL, instance), | 43 : Resource(ppapi::OBJECT_IS_IMPL, instance), |
| 31 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), | 44 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), |
| 32 width_(0), | 45 width_(0), |
| 33 height_(0) { | 46 height_(0) { |
| 34 switch (type) { | 47 switch (type) { |
| 35 case PPB_ImageData_Shared::PLATFORM: | 48 case PPB_ImageData_Shared::PLATFORM: |
| 36 backend_.reset(new ImageDataPlatformBackend); | 49 backend_.reset(new ImageDataPlatformBackend(IsBrowserAllocated())); |
| 37 return; | 50 return; |
| 38 case PPB_ImageData_Shared::SIMPLE: | 51 case PPB_ImageData_Shared::SIMPLE: |
| 39 backend_.reset(new ImageDataSimpleBackend); | 52 backend_.reset(new ImageDataSimpleBackend); |
| 40 return; | 53 return; |
| 41 // 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. |
| 42 } | 55 } |
| 43 NOTREACHED(); | 56 NOTREACHED(); |
| 44 } | 57 } |
| 45 | 58 |
| 59 PPB_ImageData_Impl::PPB_ImageData_Impl(PP_Instance instance, |
| 60 ForTest) |
| 61 : Resource(ppapi::OBJECT_IS_IMPL, instance), |
| 62 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), |
| 63 width_(0), |
| 64 height_(0) { |
| 65 backend_.reset(new ImageDataPlatformBackend(false)); |
| 66 } |
| 67 |
| 46 PPB_ImageData_Impl::~PPB_ImageData_Impl() { | 68 PPB_ImageData_Impl::~PPB_ImageData_Impl() { |
| 47 } | 69 } |
| 48 | 70 |
| 49 bool PPB_ImageData_Impl::Init(PP_ImageDataFormat format, | 71 bool PPB_ImageData_Impl::Init(PP_ImageDataFormat format, |
| 50 int width, int height, | 72 int width, int height, |
| 51 bool init_to_zero) { | 73 bool init_to_zero) { |
| 52 // TODO(brettw) this should be called only on the main thread! | 74 // TODO(brettw) this should be called only on the main thread! |
| 53 if (!IsImageDataFormatSupported(format)) | 75 if (!IsImageDataFormatSupported(format)) |
| 54 return false; // Only support this one format for now. | 76 return false; // Only support this one format for now. |
| 55 if (width <= 0 || height <= 0) | 77 if (width <= 0 || height <= 0) |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 void PPB_ImageData_Impl::SetIsCandidateForReuse() { | 142 void PPB_ImageData_Impl::SetIsCandidateForReuse() { |
| 121 // Nothing to do since we don't support image data re-use in-process. | 143 // Nothing to do since we don't support image data re-use in-process. |
| 122 } | 144 } |
| 123 | 145 |
| 124 const SkBitmap* PPB_ImageData_Impl::GetMappedBitmap() const { | 146 const SkBitmap* PPB_ImageData_Impl::GetMappedBitmap() const { |
| 125 return backend_->GetMappedBitmap(); | 147 return backend_->GetMappedBitmap(); |
| 126 } | 148 } |
| 127 | 149 |
| 128 // ImageDataPlatformBackend ---------------------------------------------------- | 150 // ImageDataPlatformBackend ---------------------------------------------------- |
| 129 | 151 |
| 130 ImageDataPlatformBackend::ImageDataPlatformBackend() | 152 ImageDataPlatformBackend::ImageDataPlatformBackend(bool is_browser_allocated) |
| 131 : width_(0), | 153 : width_(0), |
| 132 height_(0) { | 154 height_(0), |
| 155 is_browser_allocated_(is_browser_allocated) { |
| 133 } | 156 } |
| 134 | 157 |
| 135 // On POSIX, we have to tell the browser to free the transport DIB. | 158 // On POSIX, we have to tell the browser to free the transport DIB. |
| 136 ImageDataPlatformBackend::~ImageDataPlatformBackend() { | 159 ImageDataPlatformBackend::~ImageDataPlatformBackend() { |
| 137 #if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID) | 160 if (is_browser_allocated_) { |
| 138 if (dib_) { | 161 #if defined(OS_POSIX) |
| 139 RenderThreadImpl::current()->Send( | 162 if (dib_) { |
| 140 new ViewHostMsg_FreeTransportDIB(dib_->id())); | 163 RenderThreadImpl::current()->Send( |
| 164 new ViewHostMsg_FreeTransportDIB(dib_->id())); |
| 165 } |
| 166 #endif |
| 141 } | 167 } |
| 142 #endif | |
| 143 } | 168 } |
| 144 | 169 |
| 145 bool ImageDataPlatformBackend::Init(PPB_ImageData_Impl* impl, | 170 bool ImageDataPlatformBackend::Init(PPB_ImageData_Impl* impl, |
| 146 PP_ImageDataFormat format, | 171 PP_ImageDataFormat format, |
| 147 int width, int height, | 172 int width, int height, |
| 148 bool init_to_zero) { | 173 bool init_to_zero) { |
| 149 // TODO(brettw) use init_to_zero when we implement caching. | 174 // TODO(brettw) use init_to_zero when we implement caching. |
| 150 width_ = width; | 175 width_ = width; |
| 151 height_ = height; | 176 height_ = height; |
| 152 uint32 buffer_size = width_ * height_ * 4; | 177 uint32 buffer_size = width_ * height_ * 4; |
| 153 | 178 |
| 154 // Allocate the transport DIB and the PlatformCanvas pointing to it. | 179 // Allocate the transport DIB and the PlatformCanvas pointing to it. |
| 155 #if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID) | 180 TransportDIB* dib = NULL; |
| 156 // On the Mac, shared memory has to be created in the browser in order to | 181 if (is_browser_allocated_) { |
| 157 // work in the sandbox. Do this by sending a message to the browser | 182 #if defined(OS_POSIX) |
| 158 // requesting a TransportDIB (see also | 183 // Allocate the image data by sending a message to the browser requesting a |
| 159 // chrome/renderer/webplugin_delegate_proxy.cc, method | 184 // TransportDIB (see also chrome/renderer/webplugin_delegate_proxy.cc, |
| 160 // WebPluginDelegateProxy::CreateBitmap() for similar code). The TransportDIB | 185 // method WebPluginDelegateProxy::CreateBitmap() for similar code). The |
| 161 // is cached in the browser, and is freed (in typical cases) by the | 186 // TransportDIB is cached in the browser, and is freed (in typical cases) by |
| 162 // TransportDIB's destructor. | 187 // the TransportDIB's destructor. |
| 163 TransportDIB::Handle dib_handle; | 188 TransportDIB::Handle dib_handle; |
| 164 IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(buffer_size, | 189 IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(buffer_size, |
| 165 true, | 190 true, |
| 166 &dib_handle); | 191 &dib_handle); |
| 167 if (!RenderThreadImpl::current()->Send(msg)) | 192 if (!RenderThreadImpl::current()->Send(msg)) |
| 168 return false; | 193 return false; |
| 169 if (!TransportDIB::is_valid_handle(dib_handle)) | 194 if (!TransportDIB::is_valid_handle(dib_handle)) |
| 170 return false; | 195 return false; |
| 171 | 196 |
| 172 TransportDIB* dib = TransportDIB::CreateWithHandle(dib_handle); | 197 dib = TransportDIB::CreateWithHandle(dib_handle); |
| 173 #else | |
| 174 static int next_dib_id = 0; | |
| 175 TransportDIB* dib = TransportDIB::Create(buffer_size, next_dib_id++); | |
| 176 if (!dib) | |
| 177 return false; | |
| 178 #endif | 198 #endif |
| 199 } else { |
| 200 static int next_dib_id = 0; |
| 201 dib = TransportDIB::Create(buffer_size, next_dib_id++); |
| 202 if (!dib) |
| 203 return false; |
| 204 } |
| 205 DCHECK(dib); |
| 179 dib_.reset(dib); | 206 dib_.reset(dib); |
| 180 return true; | 207 return true; |
| 181 } | 208 } |
| 182 | 209 |
| 183 bool ImageDataPlatformBackend::IsMapped() const { | 210 bool ImageDataPlatformBackend::IsMapped() const { |
| 184 return !!mapped_canvas_.get(); | 211 return !!mapped_canvas_.get(); |
| 185 } | 212 } |
| 186 | 213 |
| 187 TransportDIB* ImageDataPlatformBackend::GetTransportDIB() const { | 214 TransportDIB* ImageDataPlatformBackend::GetTransportDIB() const { |
| 188 return dib_.get(); | 215 return dib_.get(); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 return skia_canvas_.get(); | 335 return skia_canvas_.get(); |
| 309 } | 336 } |
| 310 | 337 |
| 311 const SkBitmap* ImageDataSimpleBackend::GetMappedBitmap() const { | 338 const SkBitmap* ImageDataSimpleBackend::GetMappedBitmap() const { |
| 312 if (!IsMapped()) | 339 if (!IsMapped()) |
| 313 return NULL; | 340 return NULL; |
| 314 return &skia_bitmap_; | 341 return &skia_bitmap_; |
| 315 } | 342 } |
| 316 | 343 |
| 317 } // namespace content | 344 } // namespace content |
| OLD | NEW |