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.
5 #ifndef CONTENT_RENDERER_PEPPER_PEPPER_GRAPHICS_2D_HOST_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_GRAPHICS_2D_HOST_H_
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/dev/ppb_graphics_2d_dev.h"
15 #include "ppapi/c/ppb_graphics_2d.h"
16 #include "ppapi/host/host_message_context.h"
17 #include "ppapi/host/resource_host.h"
18 #include "third_party/WebKit/public/platform/WebCanvas.h"
19 #include "ui/gfx/point.h"
20 #include "ui/gfx/size.h"
23 class SingleReleaseCallback;
37 class PepperPluginInstanceImpl;
38 class PPB_ImageData_Impl;
39 class RendererPpapiHost;
41 class CONTENT_EXPORT PepperGraphics2DHost
42 : public ppapi::host::ResourceHost,
43 public base::SupportsWeakPtr<PepperGraphics2DHost> {
45 static PepperGraphics2DHost* Create(
46 RendererPpapiHost* host,
50 PP_Bool is_always_opaque,
51 scoped_refptr<PPB_ImageData_Impl> backing_store);
53 virtual ~PepperGraphics2DHost();
55 // ppapi::host::ResourceHost override.
56 virtual int32_t OnResourceMessageReceived(
57 const IPC::Message& msg,
58 ppapi::host::HostMessageContext* context) OVERRIDE;
59 virtual bool IsGraphics2DHost() OVERRIDE;
61 bool ReadImageData(PP_Resource image,
62 const PP_Point* top_left);
63 // Assciates this device with the given plugin instance. You can pass NULL
64 // to clear the existing device. Returns true on success. In this case, a
65 // repaint of the page will also be scheduled. Failure means that the device
66 // is already bound to a different instance, and nothing will happen.
67 bool BindToInstance(PepperPluginInstanceImpl* new_instance);
68 // Paints the current backing store to the web page.
69 void Paint(blink::WebCanvas* canvas,
70 const gfx::Rect& plugin_rect,
71 const gfx::Rect& paint_rect);
73 bool PrepareTextureMailbox(
74 cc::TextureMailbox* mailbox,
75 scoped_ptr<cc::SingleReleaseCallback>* release_callback);
76 void AttachedToNewLayer();
78 // Notifications about the view's progress painting. See PluginInstance.
79 // These messages are used to send Flush callbacks to the plugin.
80 void ViewInitiatedPaint();
81 void ViewFlushedPaint();
83 void DidChangeView(const ppapi::ViewData& view_data);
85 void SetScale(float scale);
86 float GetScale() const;
87 bool IsAlwaysOpaque() const;
88 PPB_ImageData_Impl* ImageData();
89 gfx::Size Size() const;
91 // The amount to resize the backing store by when painting to the canvas.
92 // This is used to stretch the backing store when resizing the plugin element.
93 gfx::PointF GetResizeScale() const;
94 // The offset of the backing store into the plugin element.
95 gfx::Point plugin_offset() const { return plugin_offset_; }
98 PepperGraphics2DHost(RendererPpapiHost* host,
100 PP_Resource resource);
104 bool is_always_opaque,
105 scoped_refptr<PPB_ImageData_Impl> backing_store);
107 int32_t OnHostMsgPaintImageData(ppapi::host::HostMessageContext* context,
108 const ppapi::HostResource& image_data,
109 const PP_Point& top_left,
110 bool src_rect_specified,
111 const PP_Rect& src_rect);
112 int32_t OnHostMsgScroll(ppapi::host::HostMessageContext* context,
115 const PP_Point& amount);
116 int32_t OnHostMsgReplaceContents(ppapi::host::HostMessageContext* context,
117 const ppapi::HostResource& image_data);
118 int32_t OnHostMsgFlush(ppapi::host::HostMessageContext* context,
119 const ppapi::ViewData& view_data);
120 int32_t OnHostMsgSetScale(ppapi::host::HostMessageContext* context,
122 int32_t OnHostMsgSetOffset(ppapi::host::HostMessageContext* context,
123 const PP_Point& offset);
124 int32_t OnHostMsgSetResizeMode(ppapi::host::HostMessageContext* context,
125 PP_Graphics2D_Dev_ResizeMode resize_mode);
126 int32_t OnHostMsgReadImageData(ppapi::host::HostMessageContext* context,
128 const PP_Point& top_left);
130 // If |old_image_data| is not NULL, a previous used ImageData object will be
131 // reused. This is used by ReplaceContents.
132 int32_t Flush(PP_Resource* old_image_data,
133 const gfx::Size& flushed_plugin_size);
135 // Called internally to execute the different queued commands. The
136 // parameters to these functions will have already been validated. The last
137 // rect argument will be filled by each function with the area affected by
138 // the update that requires invalidation. If there were no pixels changed,
139 // this rect can be untouched.
140 void ExecutePaintImageData(PPB_ImageData_Impl* image,
142 const gfx::Rect& src_rect,
143 gfx::Rect* invalidated_rect);
144 void ExecuteScroll(const gfx::Rect& clip, int dx, int dy,
145 gfx::Rect* invalidated_rect);
146 void ExecuteReplaceContents(PPB_ImageData_Impl* image,
147 gfx::Rect* invalidated_rect,
148 PP_Resource* old_image_data);
152 // Function scheduled to execute by ScheduleOffscreenFlushAck that actually
153 // issues the offscreen callbacks.
154 void SendOffscreenFlushAck();
156 // Schedules the offscreen flush ACK at a future time.
157 void ScheduleOffscreenFlushAck();
159 // Returns true if there is any type of flush callback pending.
160 bool HasPendingFlush() const;
162 // Scale |op_rect| to logical pixels, taking care to include partially-
163 // covered logical pixels (aka DIPs). Also scale optional |delta| to logical
164 // pixels as well for scrolling cases. Returns false for scrolling cases where
165 // scaling either |op_rect| or |delta| would require scrolling to fall back to
166 // invalidation due to rounding errors, true otherwise.
167 static bool ConvertToLogicalPixels(float scale,
171 RendererPpapiHost* renderer_ppapi_host_;
173 scoped_refptr<PPB_ImageData_Impl> image_data_;
175 // Non-owning pointer to the plugin instance this context is currently bound
176 // to, if any. If the context is currently unbound, this will be NULL.
177 PepperPluginInstanceImpl* bound_instance_;
179 // Keeps track of all drawing commands queued before a Flush call.
180 struct QueuedOperation;
181 typedef std::vector<QueuedOperation> OperationQueue;
182 OperationQueue queued_operations_;
184 // True if we need to send an ACK to plugin.
185 bool need_flush_ack_;
187 // When doing offscreen flushes, we issue a task that issues the callback
188 // later. This is set when one of those tasks is pending so that we can
189 // enforce the "only one pending flush at a time" constraint in the API.
190 bool offscreen_flush_pending_;
192 // Set to true if the plugin declares that this device will always be opaque.
193 // This allows us to do more optimized painting in some cases.
194 bool is_always_opaque_;
196 // Set to the scale between what the plugin considers to be one pixel and one
200 ppapi::host::ReplyMessageContext flush_reply_context_;
202 bool is_running_in_process_;
204 bool texture_mailbox_modified_;
205 bool is_using_texture_layer_;
207 // The offset into the plugin area at which to draw the contents of the
209 gfx::Point plugin_offset_;
211 // The size of the plugin element from the plugin's perspective at the last
212 // time Flush() was called. Because the plugin runs in a separate process and
213 // the size of the plugin element in the renderer is updated asynchronously,
214 // it may believe that the plugin element is a different size to what it
216 gfx::Size flushed_plugin_size_;
217 // The current size of the plugin element.
218 gfx::Size current_plugin_size_;
220 PP_Graphics2D_Dev_ResizeMode resize_mode_;
222 friend class PepperGraphics2DHostTest;
223 DISALLOW_COPY_AND_ASSIGN(PepperGraphics2DHost);
226 } // namespace content
228 #endif // CONTENT_RENDERER_PEPPER_PEPPER_GRAPHICS_2D_HOST_H_