ppb_image_data_proxy.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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
3// found in the LICENSE file.
4
5#ifndef PPAPI_PPB_IMAGE_DATA_PROXY_H_
6#define PPAPI_PPB_IMAGE_DATA_PROXY_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "base/shared_memory.h"
10#include "build/build_config.h"
11#include "ppapi/c/pp_bool.h"
12#include "ppapi/c/pp_completion_callback.h"
13#include "ppapi/c/pp_instance.h"
14#include "ppapi/c/pp_module.h"
15#include "ppapi/c/pp_resource.h"
16#include "ppapi/c/pp_size.h"
17#include "ppapi/c/pp_var.h"
18#include "ppapi/c/ppb_image_data.h"
19#include "ppapi/proxy/interface_proxy.h"
20#include "ppapi/proxy/serialized_structs.h"
21#include "ppapi/shared_impl/ppb_image_data_shared.h"
22#include "ppapi/shared_impl/resource.h"
23#include "ppapi/thunk/ppb_image_data_api.h"
24
25class TransportDIB;
26
27namespace ppapi {
28namespace proxy {
29
30class SerializedHandle;
31
32// The proxied image data resource. Unlike most resources, this needs to be
33// public in the header since a number of other resources need to access it.
34class ImageData : public ppapi::Resource,
35                  public ppapi::thunk::PPB_ImageData_API,
36                  public ppapi::PPB_ImageData_Shared {
37 public:
38#if !defined(OS_NACL)
39  ImageData(const ppapi::HostResource& resource,
40            const PP_ImageDataDesc& desc,
41            ImageHandle handle);
42#else
43  // In NaCl, we only allow creating an ImageData using a SharedMemoryHandle.
44  // ImageHandle can differ by host platform. We need something that is
45  // more consistent across platforms for NaCl, so that we can communicate to
46  // the host OS in a consistent way.
47  ImageData(const ppapi::HostResource& resource,
48            const PP_ImageDataDesc& desc,
49            const base::SharedMemoryHandle& handle);
50#endif
51  virtual ~ImageData();
52
53  // Resource overrides.
54  virtual ppapi::thunk::PPB_ImageData_API* AsPPB_ImageData_API() OVERRIDE;
55  virtual void LastPluginRefWasDeleted() OVERRIDE;
56  virtual void InstanceWasDeleted() OVERRIDE;
57
58  // PPB_ImageData API.
59  virtual PP_Bool Describe(PP_ImageDataDesc* desc) OVERRIDE;
60  virtual void* Map() OVERRIDE;
61  virtual void Unmap() OVERRIDE;
62  virtual int32_t GetSharedMemory(int* handle, uint32_t* byte_count) OVERRIDE;
63  virtual SkCanvas* GetPlatformCanvas() OVERRIDE;
64  virtual SkCanvas* GetCanvas() OVERRIDE;
65  virtual void SetUsedInReplaceContents() OVERRIDE;
66
67  const PP_ImageDataDesc& desc() const { return desc_; }
68
69  // Prepares this image data to be recycled to the plugin. The contents will be
70  // cleared if zero_contents is set.
71  void RecycleToPlugin(bool zero_contents);
72
73#if !defined(OS_NACL)
74  static ImageHandle NullHandle();
75  static ImageHandle HandleFromInt(int32_t i);
76#endif
77
78 private:
79  PP_ImageDataDesc desc_;
80
81#if defined(OS_NACL)
82  base::SharedMemory shm_;
83  uint32 size_;
84  int map_count_;
85#else
86  scoped_ptr<TransportDIB> transport_dib_;
87
88  // Null when the image isn't mapped.
89  scoped_ptr<SkCanvas> mapped_canvas_;
90#endif
91
92  // Set to true when this ImageData has been used in a call to
93  // Graphics2D.ReplaceContents. This is used to signal that it can be cached.
94  bool used_in_replace_contents_;
95
96  DISALLOW_COPY_AND_ASSIGN(ImageData);
97};
98
99class PPB_ImageData_Proxy : public InterfaceProxy {
100 public:
101  PPB_ImageData_Proxy(Dispatcher* dispatcher);
102  virtual ~PPB_ImageData_Proxy();
103
104  static PP_Resource CreateProxyResource(PP_Instance instance,
105                                         PP_ImageDataFormat format,
106                                         const PP_Size& size,
107                                         PP_Bool init_to_zero);
108
109  // InterfaceProxy implementation.
110  virtual bool OnMessageReceived(const IPC::Message& msg);
111
112  static const ApiID kApiID = API_ID_PPB_IMAGE_DATA;
113
114 private:
115  // Plugin->Host message handlers.
116  void OnHostMsgCreate(PP_Instance instance,
117                       int32_t format,
118                       const PP_Size& size,
119                       PP_Bool init_to_zero,
120                       HostResource* result,
121                       std::string* image_data_desc,
122                       ImageHandle* result_image_handle);
123  void OnHostMsgCreateNaCl(PP_Instance instance,
124                           int32_t format,
125                           const PP_Size& size,
126                           PP_Bool init_to_zero,
127                           HostResource* result,
128                           std::string* image_data_desc,
129                           ppapi::proxy::SerializedHandle* result_image_handle);
130
131  // Host->Plugin message handlers.
132  void OnPluginMsgNotifyUnusedImageData(const HostResource& old_image_data);
133
134  DISALLOW_COPY_AND_ASSIGN(PPB_ImageData_Proxy);
135};
136
137}  // namespace proxy
138}  // namespace ppapi
139
140#endif  // PPAPI_PPB_IMAGE_DATA_PROXY_H_
141