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"
20 class SingleReleaseCallback;
31 class PepperPluginInstanceImpl;
32 class PPB_ImageData_Impl;
33 class RendererPpapiHost;
35 class CONTENT_EXPORT PepperGraphics2DHost
36 : public ppapi::host::ResourceHost,
37 public base::SupportsWeakPtr<PepperGraphics2DHost> {
39 static PepperGraphics2DHost* Create(RendererPpapiHost* host,
43 PP_Bool is_always_opaque);
45 virtual ~PepperGraphics2DHost();
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;
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);
65 bool PrepareTextureMailbox(
66 cc::TextureMailbox* mailbox,
67 scoped_ptr<cc::SingleReleaseCallback>* release_callback);
68 void AttachedToNewLayer();
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();
75 void SetScale(float scale);
76 float GetScale() const;
77 bool IsAlwaysOpaque() const;
78 PPB_ImageData_Impl* ImageData();
81 PepperGraphics2DHost(RendererPpapiHost* host,
83 PP_Resource resource);
85 bool Init(int width, int height, bool is_always_opaque);
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,
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,
101 int32_t OnHostMsgReadImageData(ppapi::host::HostMessageContext* context,
103 const PP_Point& top_left);
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);
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,
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);
126 // Function scheduled to execute by ScheduleOffscreenFlushAck that actually
127 // issues the offscreen callbacks.
128 void SendOffscreenFlushAck();
130 // Schedules the offscreen flush ACK at a future time.
131 void ScheduleOffscreenFlushAck();
133 // Returns true if there is any type of flush callback pending.
134 bool HasPendingFlush() const;
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,
146 RendererPpapiHost* renderer_ppapi_host_;
148 scoped_refptr<PPB_ImageData_Impl> image_data_;
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_;
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_;
159 // True if we need to send an ACK to plugin.
160 bool need_flush_ack_;
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_;
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_;
171 // Set to the scale between what the plugin considers to be one pixel and one
175 ppapi::host::ReplyMessageContext flush_reply_context_;
177 bool is_running_in_process_;
179 bool texture_mailbox_modified_;
181 friend class PepperGraphics2DHostTest;
182 DISALLOW_COPY_AND_ASSIGN(PepperGraphics2DHost);
185 } // namespace content
187 #endif // CONTENT_RENDERER_PEPPER_PEPPER_GRAPHICS_2D_HOST_H_