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/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"
22 class SingleReleaseCallback;
36 class PepperPluginInstanceImpl;
37 class PPB_ImageData_Impl;
38 class RendererPpapiHost;
40 class CONTENT_EXPORT PepperGraphics2DHost
41 : public ppapi::host::ResourceHost,
42 public base::SupportsWeakPtr<PepperGraphics2DHost> {
44 static PepperGraphics2DHost* Create(
45 RendererPpapiHost* host,
49 PP_Bool is_always_opaque,
50 scoped_refptr<PPB_ImageData_Impl> backing_store);
52 virtual ~PepperGraphics2DHost();
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;
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);
71 bool PrepareTextureMailbox(
72 cc::TextureMailbox* mailbox,
73 scoped_ptr<cc::SingleReleaseCallback>* release_callback);
74 void AttachedToNewLayer();
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();
81 void SetScale(float scale);
82 float GetScale() const;
83 bool IsAlwaysOpaque() const;
84 PPB_ImageData_Impl* ImageData();
85 gfx::Size Size() const;
88 PepperGraphics2DHost(RendererPpapiHost* host,
90 PP_Resource resource);
94 bool is_always_opaque,
95 scoped_refptr<PPB_ImageData_Impl> backing_store);
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,
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,
111 int32_t OnHostMsgReadImageData(ppapi::host::HostMessageContext* context,
113 const PP_Point& top_left);
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);
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,
127 const gfx::Rect& src_rect,
128 gfx::Rect* invalidated_rect);
129 void ExecuteScroll(const gfx::Rect& clip,
132 gfx::Rect* invalidated_rect);
133 void ExecuteReplaceContents(PPB_ImageData_Impl* image,
134 gfx::Rect* invalidated_rect,
135 PP_Resource* old_image_data);
139 // Function scheduled to execute by ScheduleOffscreenFlushAck that actually
140 // issues the offscreen callbacks.
141 void SendOffscreenFlushAck();
143 // Schedules the offscreen flush ACK at a future time.
144 void ScheduleOffscreenFlushAck();
146 // Returns true if there is any type of flush callback pending.
147 bool HasPendingFlush() const;
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,
158 RendererPpapiHost* renderer_ppapi_host_;
160 scoped_refptr<PPB_ImageData_Impl> image_data_;
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_;
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_;
171 // True if we need to send an ACK to plugin.
172 bool need_flush_ack_;
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_;
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_;
183 // Set to the scale between what the plugin considers to be one pixel and one
187 ppapi::host::ReplyMessageContext flush_reply_context_;
189 bool is_running_in_process_;
191 bool texture_mailbox_modified_;
192 bool is_using_texture_layer_;
194 friend class PepperGraphics2DHostTest;
195 DISALLOW_COPY_AND_ASSIGN(PepperGraphics2DHost);
198 } // namespace content
200 #endif // CONTENT_RENDERER_PEPPER_PEPPER_GRAPHICS_2D_HOST_H_