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/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" 21 22 namespace cc { 23 class SingleReleaseCallback; 24 class TextureMailbox; 25 } 26 27 namespace gfx { 28 class Rect; 29 } 30 31 namespace ppapi { 32 struct ViewData; 33 } 34 35 namespace content { 36 37 class PepperPluginInstanceImpl; 38 class PPB_ImageData_Impl; 39 class RendererPpapiHost; 40 41 class CONTENT_EXPORT PepperGraphics2DHost 42 : public ppapi::host::ResourceHost, 43 public base::SupportsWeakPtr<PepperGraphics2DHost> { 44 public: 45 static PepperGraphics2DHost* Create( 46 RendererPpapiHost* host, 47 PP_Instance instance, 48 PP_Resource resource, 49 const PP_Size& size, 50 PP_Bool is_always_opaque, 51 scoped_refptr<PPB_ImageData_Impl> backing_store); 52 53 virtual ~PepperGraphics2DHost(); 54 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; 60 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); 72 73 bool PrepareTextureMailbox( 74 cc::TextureMailbox* mailbox, 75 scoped_ptr<cc::SingleReleaseCallback>* release_callback); 76 void AttachedToNewLayer(); 77 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(); 82 83 void DidChangeView(const ppapi::ViewData& view_data); 84 85 void SetScale(float scale); 86 float GetScale() const; 87 bool IsAlwaysOpaque() const; 88 PPB_ImageData_Impl* ImageData(); 89 gfx::Size Size() const; 90 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_; } 96 97 private: 98 PepperGraphics2DHost(RendererPpapiHost* host, 99 PP_Instance instance, 100 PP_Resource resource); 101 102 bool Init(int width, 103 int height, 104 bool is_always_opaque, 105 scoped_refptr<PPB_ImageData_Impl> backing_store); 106 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, 113 bool clip_specified, 114 const PP_Rect& clip, 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, 121 float scale); 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, 127 PP_Resource image, 128 const PP_Point& top_left); 129 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); 134 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, 141 int x, int y, 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); 149 150 void SendFlushAck(); 151 152 // Function scheduled to execute by ScheduleOffscreenFlushAck that actually 153 // issues the offscreen callbacks. 154 void SendOffscreenFlushAck(); 155 156 // Schedules the offscreen flush ACK at a future time. 157 void ScheduleOffscreenFlushAck(); 158 159 // Returns true if there is any type of flush callback pending. 160 bool HasPendingFlush() const; 161 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, 168 gfx::Rect* op_rect, 169 gfx::Point* delta); 170 171 RendererPpapiHost* renderer_ppapi_host_; 172 173 scoped_refptr<PPB_ImageData_Impl> image_data_; 174 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_; 178 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_; 183 184 // True if we need to send an ACK to plugin. 185 bool need_flush_ack_; 186 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_; 191 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_; 195 196 // Set to the scale between what the plugin considers to be one pixel and one 197 // DIP 198 float scale_; 199 200 ppapi::host::ReplyMessageContext flush_reply_context_; 201 202 bool is_running_in_process_; 203 204 bool texture_mailbox_modified_; 205 bool is_using_texture_layer_; 206 207 // The offset into the plugin area at which to draw the contents of the 208 // graphics context. 209 gfx::Point plugin_offset_; 210 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 215 // actually is. 216 gfx::Size flushed_plugin_size_; 217 // The current size of the plugin element. 218 gfx::Size current_plugin_size_; 219 220 PP_Graphics2D_Dev_ResizeMode resize_mode_; 221 222 friend class PepperGraphics2DHostTest; 223 DISALLOW_COPY_AND_ASSIGN(PepperGraphics2DHost); 224 }; 225 226 } // namespace content 227 228 #endif // CONTENT_RENDERER_PEPPER_PEPPER_GRAPHICS_2D_HOST_H_ 229