Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / pepper_graphics_2d_host.h
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 CONTENT_RENDERER_PEPPER_PEPPER_GRAPHICS_2D_HOST_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_GRAPHICS_2D_HOST_H_
7
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/weak_ptr.h"
13 #include "content/common/content_export.h"
14 #include "ppapi/c/ppb_graphics_2d.h"
15 #include "ppapi/host/host_message_context.h"
16 #include "ppapi/host/resource_host.h"
17 #include "third_party/WebKit/public/platform/WebCanvas.h"
18 #include "ui/gfx/point.h"
19 #include "ui/gfx/size.h"
20
21 namespace cc {
22 class SingleReleaseCallback;
23 class TextureMailbox;
24 }
25
26 namespace gfx {
27 class Rect;
28 }
29
30 namespace ppapi {
31 struct ViewData;
32 }
33
34 namespace content {
35
36 class PepperPluginInstanceImpl;
37 class PPB_ImageData_Impl;
38 class RendererPpapiHost;
39
40 class CONTENT_EXPORT PepperGraphics2DHost
41     : public ppapi::host::ResourceHost,
42       public base::SupportsWeakPtr<PepperGraphics2DHost> {
43  public:
44   static PepperGraphics2DHost* Create(
45       RendererPpapiHost* host,
46       PP_Instance instance,
47       PP_Resource resource,
48       const PP_Size& size,
49       PP_Bool is_always_opaque,
50       scoped_refptr<PPB_ImageData_Impl> backing_store);
51
52   virtual ~PepperGraphics2DHost();
53
54   // ppapi::host::ResourceHost override.
55   virtual int32_t OnResourceMessageReceived(
56       const IPC::Message& msg,
57       ppapi::host::HostMessageContext* context) OVERRIDE;
58   virtual bool IsGraphics2DHost() OVERRIDE;
59
60   bool ReadImageData(PP_Resource image, const PP_Point* top_left);
61   // Assciates this device with the given plugin instance. You can pass NULL
62   // to clear the existing device. Returns true on success. In this case, a
63   // repaint of the page will also be scheduled. Failure means that the device
64   // is already bound to a different instance, and nothing will happen.
65   bool BindToInstance(PepperPluginInstanceImpl* new_instance);
66   // Paints the current backing store to the web page.
67   void Paint(blink::WebCanvas* canvas,
68              const gfx::Rect& plugin_rect,
69              const gfx::Rect& paint_rect);
70
71   bool PrepareTextureMailbox(
72       cc::TextureMailbox* mailbox,
73       scoped_ptr<cc::SingleReleaseCallback>* release_callback);
74   void AttachedToNewLayer();
75
76   // Notifications about the view's progress painting.  See PluginInstance.
77   // These messages are used to send Flush callbacks to the plugin.
78   void ViewInitiatedPaint();
79   void ViewFlushedPaint();
80
81   void SetScale(float scale);
82   float GetScale() const;
83   bool IsAlwaysOpaque() const;
84   PPB_ImageData_Impl* ImageData();
85   gfx::Size Size() const;
86
87  private:
88   PepperGraphics2DHost(RendererPpapiHost* host,
89                        PP_Instance instance,
90                        PP_Resource resource);
91
92   bool Init(int width,
93             int height,
94             bool is_always_opaque,
95             scoped_refptr<PPB_ImageData_Impl> backing_store);
96
97   int32_t OnHostMsgPaintImageData(ppapi::host::HostMessageContext* context,
98                                   const ppapi::HostResource& image_data,
99                                   const PP_Point& top_left,
100                                   bool src_rect_specified,
101                                   const PP_Rect& src_rect);
102   int32_t OnHostMsgScroll(ppapi::host::HostMessageContext* context,
103                           bool clip_specified,
104                           const PP_Rect& clip,
105                           const PP_Point& amount);
106   int32_t OnHostMsgReplaceContents(ppapi::host::HostMessageContext* context,
107                                    const ppapi::HostResource& image_data);
108   int32_t OnHostMsgFlush(ppapi::host::HostMessageContext* context);
109   int32_t OnHostMsgSetScale(ppapi::host::HostMessageContext* context,
110                             float scale);
111   int32_t OnHostMsgReadImageData(ppapi::host::HostMessageContext* context,
112                                  PP_Resource image,
113                                  const PP_Point& top_left);
114
115   // If |old_image_data| is not NULL, a previous used ImageData object will be
116   // reused.  This is used by ReplaceContents.
117   int32_t Flush(PP_Resource* old_image_data);
118
119   // Called internally to execute the different queued commands. The
120   // parameters to these functions will have already been validated. The last
121   // rect argument will be filled by each function with the area affected by
122   // the update that requires invalidation. If there were no pixels changed,
123   // this rect can be untouched.
124   void ExecutePaintImageData(PPB_ImageData_Impl* image,
125                              int x,
126                              int y,
127                              const gfx::Rect& src_rect,
128                              gfx::Rect* invalidated_rect);
129   void ExecuteScroll(const gfx::Rect& clip,
130                      int dx,
131                      int dy,
132                      gfx::Rect* invalidated_rect);
133   void ExecuteReplaceContents(PPB_ImageData_Impl* image,
134                               gfx::Rect* invalidated_rect,
135                               PP_Resource* old_image_data);
136
137   void SendFlushAck();
138
139   // Function scheduled to execute by ScheduleOffscreenFlushAck that actually
140   // issues the offscreen callbacks.
141   void SendOffscreenFlushAck();
142
143   // Schedules the offscreen flush ACK at a future time.
144   void ScheduleOffscreenFlushAck();
145
146   // Returns true if there is any type of flush callback pending.
147   bool HasPendingFlush() const;
148
149   // Scale |op_rect| to logical pixels, taking care to include partially-
150   // covered logical pixels (aka DIPs). Also scale optional |delta| to logical
151   // pixels as well for scrolling cases. Returns false for scrolling cases where
152   // scaling either |op_rect| or |delta| would require scrolling to fall back to
153   // invalidation due to rounding errors, true otherwise.
154   static bool ConvertToLogicalPixels(float scale,
155                                      gfx::Rect* op_rect,
156                                      gfx::Point* delta);
157
158   RendererPpapiHost* renderer_ppapi_host_;
159
160   scoped_refptr<PPB_ImageData_Impl> image_data_;
161
162   // Non-owning pointer to the plugin instance this context is currently bound
163   // to, if any. If the context is currently unbound, this will be NULL.
164   PepperPluginInstanceImpl* bound_instance_;
165
166   // Keeps track of all drawing commands queued before a Flush call.
167   struct QueuedOperation;
168   typedef std::vector<QueuedOperation> OperationQueue;
169   OperationQueue queued_operations_;
170
171   // True if we need to send an ACK to plugin.
172   bool need_flush_ack_;
173
174   // When doing offscreen flushes, we issue a task that issues the callback
175   // later. This is set when one of those tasks is pending so that we can
176   // enforce the "only one pending flush at a time" constraint in the API.
177   bool offscreen_flush_pending_;
178
179   // Set to true if the plugin declares that this device will always be opaque.
180   // This allows us to do more optimized painting in some cases.
181   bool is_always_opaque_;
182
183   // Set to the scale between what the plugin considers to be one pixel and one
184   // DIP
185   float scale_;
186
187   ppapi::host::ReplyMessageContext flush_reply_context_;
188
189   bool is_running_in_process_;
190
191   bool texture_mailbox_modified_;
192   bool is_using_texture_layer_;
193
194   friend class PepperGraphics2DHostTest;
195   DISALLOW_COPY_AND_ASSIGN(PepperGraphics2DHost);
196 };
197
198 }  // namespace content
199
200 #endif  // CONTENT_RENDERER_PEPPER_PEPPER_GRAPHICS_2D_HOST_H_