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