diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-04 19:38:48 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-04 19:38:48 +0000 |
commit | 1de08b360a306baa27e5f810fa5b5dd7a382ccde (patch) | |
tree | 44c496efb7a52bb0ec74c537120fe176e90a5f9f /ppapi | |
parent | 218a5a204661ad6185f01d6233b35b2d5f86027b (diff) | |
download | chromium_src-1de08b360a306baa27e5f810fa5b5dd7a382ccde.zip chromium_src-1de08b360a306baa27e5f810fa5b5dd7a382ccde.tar.gz chromium_src-1de08b360a306baa27e5f810fa5b5dd7a382ccde.tar.bz2 |
Add proxies for ImageData and Graphics2D. These don't build by themselves, this
is part of a larger patch. You can see most of the serialization mechanics
already checked in to the same directory.
TEST=none
BUG=none
Review URL: http://codereview.chromium.org/4265002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@65097 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi')
-rw-r--r-- | ppapi/proxy/ppb_graphics_2d_proxy.cc | 202 | ||||
-rw-r--r-- | ppapi/proxy/ppb_graphics_2d_proxy.h | 62 | ||||
-rw-r--r-- | ppapi/proxy/ppb_image_data_proxy.cc | 234 | ||||
-rw-r--r-- | ppapi/proxy/ppb_image_data_proxy.h | 50 |
4 files changed, 548 insertions, 0 deletions
diff --git a/ppapi/proxy/ppb_graphics_2d_proxy.cc b/ppapi/proxy/ppb_graphics_2d_proxy.cc new file mode 100644 index 0000000..99748fa --- /dev/null +++ b/ppapi/proxy/ppb_graphics_2d_proxy.cc @@ -0,0 +1,202 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ppapi/proxy/ppb_graphics_2d_proxy.h" + +#include <string.h> // For memset + +#include "base/logging.h" +#include "ppapi/c/pp_completion_callback.h" +#include "ppapi/c/pp_errors.h" +#include "ppapi/c/pp_resource.h" +#include "ppapi/c/ppb_graphics_2d.h" +#include "ppapi/proxy/plugin_dispatcher.h" +#include "ppapi/proxy/plugin_resource.h" +#include "ppapi/proxy/ppapi_messages.h" + +namespace pp { +namespace proxy { + +class Graphics2D : public PluginResource { + public: + Graphics2D(const PP_Size& size, bool is_always_opaque) + : size_(size), is_always_opaque_(is_always_opaque) { + } + + // Resource overrides. + virtual Graphics2D* AsGraphics2D() { return this; } + + const PP_Size& size() const { return size_; } + bool is_always_opaque() const { return is_always_opaque_; } + + private: + PP_Size size_; + bool is_always_opaque_; + + DISALLOW_COPY_AND_ASSIGN(Graphics2D); +}; + +namespace { + +PP_Resource Create(PP_Module module_id, + const PP_Size* size, + bool is_always_opaque) { + PluginDispatcher* dispatcher = PluginDispatcher::Get(); + PP_Resource result = 0; + dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_Create( + INTERFACE_ID_PPB_GRAPHICS_2D, module_id, *size, is_always_opaque, + &result)); + if (result) { + linked_ptr<Graphics2D> graphics_2d(new Graphics2D(*size, is_always_opaque)); + dispatcher->plugin_resource_tracker()->AddResource(result, graphics_2d); + } + return result; +} + +bool IsGraphics2D(PP_Resource resource) { + Graphics2D* object = PluginResource::GetAs<Graphics2D>(resource); + return !!object; +} + +bool Describe(PP_Resource graphics_2d, + PP_Size* size, + bool* is_always_opaque) { + Graphics2D* object = PluginResource::GetAs<Graphics2D>(graphics_2d); + if (!object) { + size->width = 0; + size->height = 0; + *is_always_opaque = false; + return false; + } + + *size = object->size(); + *is_always_opaque = object->is_always_opaque(); + return true; +} + +void PaintImageData(PP_Resource graphics_2d, + PP_Resource image_data, + const PP_Point* top_left, + const PP_Rect* src_rect) { + PP_Rect dummy; + memset(&dummy, 0, sizeof(PP_Rect)); + PluginDispatcher::Get()->Send(new PpapiHostMsg_PPBGraphics2D_PaintImageData( + INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, image_data, *top_left, + !!src_rect, src_rect ? *src_rect : dummy)); +} + +void Scroll(PP_Resource graphics_2d, + const PP_Rect* clip_rect, + const PP_Point* amount) { + PP_Rect dummy; + memset(&dummy, 0, sizeof(PP_Rect)); + PluginDispatcher::Get()->Send(new PpapiHostMsg_PPBGraphics2D_Scroll( + INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, !!clip_rect, + clip_rect ? *clip_rect : dummy, *amount)); +} + +void ReplaceContents(PP_Resource graphics_2d, PP_Resource image_data) { + PluginDispatcher::Get()->Send(new PpapiHostMsg_PPBGraphics2D_ReplaceContents( + INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, image_data)); +} + +int32_t Flush(PP_Resource graphics_2d, + PP_CompletionCallback callback) { + PluginDispatcher* dispatcher = PluginDispatcher::Get(); + int32_t result = PP_ERROR_FAILED; + dispatcher->Send(new PpapiHostMsg_PPBGraphics2D_Flush( + INTERFACE_ID_PPB_GRAPHICS_2D, graphics_2d, + dispatcher->callback_tracker().SendCallback(callback), + &result)); + return result; +} + +const PPB_Graphics2D ppb_graphics_2d = { + &Create, + &IsGraphics2D, + &Describe, + &PaintImageData, + &Scroll, + &ReplaceContents, + &Flush +}; + +} // namespace + +PPB_Graphics2D_Proxy::PPB_Graphics2D_Proxy(Dispatcher* dispatcher, + const void* target_interface) + : InterfaceProxy(dispatcher, target_interface) { +} + +PPB_Graphics2D_Proxy::~PPB_Graphics2D_Proxy() { +} + +const void* PPB_Graphics2D_Proxy::GetSourceInterface() const { + return &ppb_graphics_2d; +} + +InterfaceID PPB_Graphics2D_Proxy::GetInterfaceId() const { + return INTERFACE_ID_PPB_GRAPHICS_2D; +} + +void PPB_Graphics2D_Proxy::OnMessageReceived(const IPC::Message& msg) { + IPC_BEGIN_MESSAGE_MAP(PPB_Graphics2D_Proxy, msg) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_Create, + OnMsgCreate) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_PaintImageData, + OnMsgPaintImageData) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_Scroll, + OnMsgScroll) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_ReplaceContents, + OnMsgReplaceContents) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBGraphics2D_Flush, + OnMsgFlush) + IPC_END_MESSAGE_MAP() + // FIXME(brettw) handle bad messages! +} + +void PPB_Graphics2D_Proxy::OnMsgCreate(PP_Module module, + const PP_Size& size, + bool is_always_opaque, + PP_Resource* result) { + *result = ppb_graphics_2d_target()->Create( + module, &size, is_always_opaque); +} + +void PPB_Graphics2D_Proxy::OnMsgPaintImageData(PP_Resource graphics_2d, + PP_Resource image_data, + const PP_Point& top_left, + bool src_rect_specified, + const PP_Rect& src_rect) { + ppb_graphics_2d_target()->PaintImageData( + graphics_2d, image_data, &top_left, + src_rect_specified ? &src_rect : NULL); +} + +void PPB_Graphics2D_Proxy::OnMsgScroll(PP_Resource graphics_2d, + bool clip_specified, + const PP_Rect& clip, + const PP_Point& amount) { + ppb_graphics_2d_target()->Scroll( + graphics_2d, + clip_specified ? &clip : NULL, &amount); +} + +void PPB_Graphics2D_Proxy::OnMsgReplaceContents(PP_Resource graphics_2d, + PP_Resource image_data) { + ppb_graphics_2d_target()->ReplaceContents(graphics_2d, image_data); +} + +void PPB_Graphics2D_Proxy::OnMsgFlush(PP_Resource graphics_2d, + uint32_t serialized_callback, + int32_t* result) { + // TODO(brettw) this should be a non-sync function. Ideally it would call + // the callback with a failure code from here if you weren't allowed to + // call Flush there. + *result = ppb_graphics_2d_target()->Flush( + graphics_2d, ReceiveCallback(serialized_callback)); +} + +} // namespace proxy +} // namespace pp diff --git a/ppapi/proxy/ppb_graphics_2d_proxy.h b/ppapi/proxy/ppb_graphics_2d_proxy.h new file mode 100644 index 0000000..b5cabfb --- /dev/null +++ b/ppapi/proxy/ppb_graphics_2d_proxy.h @@ -0,0 +1,62 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PPAPI_PPB_GRAPHICS_2D_PROXY_H_ +#define PPAPI_PPB_GRAPHICS_2D_PROXY_H_ + +#include "ppapi/c/pp_completion_callback.h" +#include "ppapi/c/pp_module.h" +#include "ppapi/c/pp_module.h" +#include "ppapi/c/pp_resource.h" +#include "ppapi/c/pp_size.h" +#include "ppapi/c/pp_var.h" +#include "ppapi/proxy/interface_proxy.h" + +struct PPB_Graphics2D; +struct PP_Point; +struct PP_Rect; + +namespace pp { +namespace proxy { + +class PPB_Graphics2D_Proxy : public InterfaceProxy { + public: + PPB_Graphics2D_Proxy(Dispatcher* dispatcher, const void* target_interface); + virtual ~PPB_Graphics2D_Proxy(); + + const PPB_Graphics2D* ppb_graphics_2d_target() const { + return static_cast<const PPB_Graphics2D*>(target_interface()); + } + + // InterfaceProxy implementation. + virtual const void* GetSourceInterface() const; + virtual InterfaceID GetInterfaceId() const; + virtual void OnMessageReceived(const IPC::Message& msg); + + private: + // Message handlers. + void OnMsgCreate(PP_Module module, + const PP_Size& size, + bool is_always_opaque, + PP_Resource* result); + void OnMsgPaintImageData(PP_Resource graphics_2d, + PP_Resource image_data, + const PP_Point& top_left, + bool src_rect_specified, + const PP_Rect& src_rect); + void OnMsgScroll(PP_Resource graphics_2d, + bool clip_specified, + const PP_Rect& clip, + const PP_Point& amount); + void OnMsgReplaceContents(PP_Resource graphics_2d, + PP_Resource image_data); + void OnMsgFlush(PP_Resource graphics_2d, + uint32_t serialized_callback, + int32_t* result); +}; + +} // namespace proxy +} // namespace pp + +#endif // PPAPI_PPB_GRAPHICS_2D_PROXY_H_ diff --git a/ppapi/proxy/ppb_image_data_proxy.cc b/ppapi/proxy/ppb_image_data_proxy.cc new file mode 100644 index 0000000..19d0932 --- /dev/null +++ b/ppapi/proxy/ppb_image_data_proxy.cc @@ -0,0 +1,234 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ppapi/proxy/ppb_image_data_proxy.h" + +#include <string.h> // For memcpy + +#include <vector> + +#include "base/logging.h" +#include "build/build_config.h" +#include "ppapi/c/pp_completion_callback.h" +#include "ppapi/c/pp_resource.h" +#include "ppapi/c/ppb_image_data.h" +#include "ppapi/c/trusted/ppb_image_data_trusted.h" +#include "ppapi/proxy/plugin_dispatcher.h" +#include "ppapi/proxy/plugin_resource.h" +#include "ppapi/proxy/ppapi_messages.h" + +#if defined(OS_LINUX) +#include <sys/shm.h> +#endif + +namespace pp { +namespace proxy { + +class ImageData : public PluginResource { + public: + ImageData(const PP_ImageDataDesc& desc, uint64_t memory_handle); + virtual ~ImageData(); + + // Resource overrides. + virtual ImageData* AsImageData() { return this; } + + void* Map(); + void Unmap(); + + const PP_ImageDataDesc& desc() const { return desc_; } + + private: + PP_ImageDataDesc desc_; + uint64_t memory_handle_; + + void* mapped_data_; + + DISALLOW_COPY_AND_ASSIGN(ImageData); +}; + +ImageData::ImageData(const PP_ImageDataDesc& desc, + uint64_t memory_handle) + : desc_(desc), + memory_handle_(memory_handle), + mapped_data_(NULL) { +} + +ImageData::~ImageData() { + Unmap(); +} + +void* ImageData::Map() { +#if defined(OS_LINUX) + // On linux, the memory handle is a SysV shared memory segment. + int shmkey = static_cast<int>(memory_handle_); + void* address = shmat(shmkey, NULL, 0); + // Mark for deletion in case we crash so the kernel will clean it up. + shmctl(shmkey, IPC_RMID, 0); + if (address == (void*)-1) + return NULL; + mapped_data_ = address; + return address; +#else + #error write this +#endif +} + +void ImageData::Unmap() { +#if defined(OS_LINUX) + if (mapped_data_) + shmdt(mapped_data_); +#else + #error write this +#endif + mapped_data_ = NULL; +} + +namespace { + +PP_ImageDataFormat GetNativeImageDataFormat() { + int32 format = 0; + PluginDispatcher::Get()->Send( + new PpapiHostMsg_PPBImageData_GetNativeImageDataFormat( + INTERFACE_ID_PPB_IMAGE_DATA, &format)); + return static_cast<PP_ImageDataFormat>(format); +} + +bool IsImageDataFormatSupported(PP_ImageDataFormat format) { + bool supported = false; + PluginDispatcher::Get()->Send( + new PpapiHostMsg_PPBImageData_IsImageDataFormatSupported( + INTERFACE_ID_PPB_IMAGE_DATA, static_cast<int32_t>(format), + &supported)); + return supported; +} + +PP_Resource Create(PP_Module module_id, + PP_ImageDataFormat format, + const PP_Size* size, + bool init_to_zero) { + PP_Resource result = 0; + std::string image_data_desc; + uint64_t shm_handle = -1; + PluginDispatcher::Get()->Send( + new PpapiHostMsg_PPBImageData_Create( + INTERFACE_ID_PPB_IMAGE_DATA, module_id, format, *size, init_to_zero, + &result, &image_data_desc, &shm_handle)); + + if (result && image_data_desc.size() == sizeof(PP_ImageDataDesc)) { + // We serialize the PP_ImageDataDesc just by copying to a string. + PP_ImageDataDesc desc; + memcpy(&desc, image_data_desc.data(), sizeof(PP_ImageDataDesc)); + + linked_ptr<ImageData> object( + new ImageData(desc, shm_handle)); + PluginDispatcher::Get()->plugin_resource_tracker()->AddResource( + result, object); + } + return result; +} + +bool IsImageData(PP_Resource resource) { + ImageData* object = PluginResource::GetAs<ImageData>(resource); + return !!object; +} + +bool Describe(PP_Resource resource, PP_ImageDataDesc* desc) { + ImageData* object = PluginResource::GetAs<ImageData>(resource); + if (!object) + return false; + memcpy(desc, &object->desc(), sizeof(PP_ImageDataDesc)); + return true; +} + +void* Map(PP_Resource resource) { + ImageData* object = PluginResource::GetAs<ImageData>(resource); + if (!object) + return NULL; + return object->Map(); +} + +void Unmap(PP_Resource resource) { + ImageData* object = PluginResource::GetAs<ImageData>(resource); + if (object) + object->Unmap(); +} + +const PPB_ImageData ppb_imagedata = { + &GetNativeImageDataFormat, + &IsImageDataFormatSupported, + &Create, + &IsImageData, + &Describe, + &Map, + &Unmap, +}; + +} // namespace + +PPB_ImageData_Proxy::PPB_ImageData_Proxy(Dispatcher* dispatcher, + const void* target_interface) + : InterfaceProxy(dispatcher, target_interface) { +} + +PPB_ImageData_Proxy::~PPB_ImageData_Proxy() { +} + +const void* PPB_ImageData_Proxy::GetSourceInterface() const { + return &ppb_imagedata; +} + +InterfaceID PPB_ImageData_Proxy::GetInterfaceId() const { + return INTERFACE_ID_PPB_IMAGE_DATA; +} + +void PPB_ImageData_Proxy::OnMessageReceived(const IPC::Message& msg) { + IPC_BEGIN_MESSAGE_MAP(PPB_ImageData_Proxy, msg) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBImageData_GetNativeImageDataFormat, + OnMsgGetNativeImageDataFormat) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBImageData_IsImageDataFormatSupported, + OnMsgIsImageDataFormatSupported) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBImageData_Create, OnMsgCreate) + IPC_END_MESSAGE_MAP() + // FIXME(brettw) handle bad messages! +} + +void PPB_ImageData_Proxy::OnMsgGetNativeImageDataFormat(int32* result) { + *result = ppb_image_data_target()->GetNativeImageDataFormat(); +} + +void PPB_ImageData_Proxy::OnMsgIsImageDataFormatSupported(int32 format, + bool* result) { + *result = ppb_image_data_target()->IsImageDataFormatSupported( + static_cast<PP_ImageDataFormat>(format)); +} + +void PPB_ImageData_Proxy::OnMsgCreate(PP_Module module, + int32_t format, + const PP_Size& size, + bool init_to_zero, + PP_Resource* result, + std::string* image_data_desc, + uint64_t* result_shm_handle) { + *result = ppb_image_data_target()->Create( + module, static_cast<PP_ImageDataFormat>(format), &size, init_to_zero); + *result_shm_handle = 0; + if (*result) { + // The ImageDesc is just serialized as a string. + PP_ImageDataDesc desc; + if (ppb_image_data_target()->Describe(*result, &desc)) { + image_data_desc->resize(sizeof(PP_ImageDataDesc)); + memcpy(&(*image_data_desc)[0], &desc, sizeof(PP_ImageDataDesc)); + } + + // Get the shared memory handle. + const PPB_ImageDataTrusted* trusted = + reinterpret_cast<const PPB_ImageDataTrusted*>( + dispatcher()->GetLocalInterface(PPB_IMAGEDATA_TRUSTED_INTERFACE)); + if (trusted) + *result_shm_handle = trusted->GetNativeMemoryHandle(*result); + } +} + +} // namespace proxy +} // namespace pp diff --git a/ppapi/proxy/ppb_image_data_proxy.h b/ppapi/proxy/ppb_image_data_proxy.h new file mode 100644 index 0000000..70d8ce0 --- /dev/null +++ b/ppapi/proxy/ppb_image_data_proxy.h @@ -0,0 +1,50 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PPAPI_PPB_IMAGE_DATA_PROXY_H_ +#define PPAPI_PPB_IMAGE_DATA_PROXY_H_ + +#include "ppapi/c/pp_completion_callback.h" +#include "ppapi/c/pp_module.h" +#include "ppapi/c/pp_resource.h" +#include "ppapi/c/pp_size.h" +#include "ppapi/c/pp_var.h" +#include "ppapi/proxy/interface_proxy.h" + +struct PPB_ImageData; + +namespace pp { +namespace proxy { + +class PPB_ImageData_Proxy : public InterfaceProxy { + public: + PPB_ImageData_Proxy(Dispatcher* dispatcher, const void* target_interface); + virtual ~PPB_ImageData_Proxy(); + + const PPB_ImageData* ppb_image_data_target() const { + return static_cast<const PPB_ImageData*>(target_interface()); + } + + // InterfaceProxy implementation. + virtual const void* GetSourceInterface() const; + virtual InterfaceID GetInterfaceId() const; + virtual void OnMessageReceived(const IPC::Message& msg); + + private: + // Message handlers. + void OnMsgGetNativeImageDataFormat(int32* result); + void OnMsgIsImageDataFormatSupported(int32 format, bool* result); + void OnMsgCreate(PP_Module module, + int32_t format, + const PP_Size& size, + bool init_to_zero, + PP_Resource* result, + std::string* image_data_desc, + uint64_t* result_shm_handle); +}; + +} // namespace proxy +} // namespace pp + +#endif // PPAPI_PPB_IMAGE_DATA_PROXY_H_ |