Home | History | Annotate | Download | only in pepper
      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 #include "ui/events/latency_info.h"
     19 #include "ui/gfx/point.h"
     20 #include "ui/gfx/size.h"
     21 
     22 namespace cc {
     23 class SharedBitmap;
     24 class SingleReleaseCallback;
     25 class TextureMailbox;
     26 }
     27 
     28 namespace gfx {
     29 class Rect;
     30 }
     31 
     32 namespace ppapi {
     33 struct ViewData;
     34 }
     35 
     36 namespace content {
     37 
     38 class PepperPluginInstanceImpl;
     39 class PPB_ImageData_Impl;
     40 class RendererPpapiHost;
     41 
     42 class CONTENT_EXPORT PepperGraphics2DHost
     43     : public ppapi::host::ResourceHost,
     44       public base::SupportsWeakPtr<PepperGraphics2DHost> {
     45  public:
     46   static PepperGraphics2DHost* Create(
     47       RendererPpapiHost* host,
     48       PP_Instance instance,
     49       PP_Resource resource,
     50       const PP_Size& size,
     51       PP_Bool is_always_opaque,
     52       scoped_refptr<PPB_ImageData_Impl> backing_store);
     53 
     54   virtual ~PepperGraphics2DHost();
     55 
     56   // ppapi::host::ResourceHost override.
     57   virtual int32_t OnResourceMessageReceived(
     58       const IPC::Message& msg,
     59       ppapi::host::HostMessageContext* context) OVERRIDE;
     60   virtual bool IsGraphics2DHost() OVERRIDE;
     61 
     62   bool ReadImageData(PP_Resource image, 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 SetScale(float scale);
     84   float GetScale() const;
     85   bool IsAlwaysOpaque() const;
     86   PPB_ImageData_Impl* ImageData();
     87   gfx::Size Size() const;
     88 
     89   void ClearCache();
     90 
     91  private:
     92   PepperGraphics2DHost(RendererPpapiHost* host,
     93                        PP_Instance instance,
     94                        PP_Resource resource);
     95 
     96   bool Init(int width,
     97             int height,
     98             bool is_always_opaque,
     99             scoped_refptr<PPB_ImageData_Impl> backing_store);
    100 
    101   int32_t OnHostMsgPaintImageData(ppapi::host::HostMessageContext* context,
    102                                   const ppapi::HostResource& image_data,
    103                                   const PP_Point& top_left,
    104                                   bool src_rect_specified,
    105                                   const PP_Rect& src_rect);
    106   int32_t OnHostMsgScroll(ppapi::host::HostMessageContext* context,
    107                           bool clip_specified,
    108                           const PP_Rect& clip,
    109                           const PP_Point& amount);
    110   int32_t OnHostMsgReplaceContents(ppapi::host::HostMessageContext* context,
    111                                    const ppapi::HostResource& image_data);
    112   int32_t OnHostMsgFlush(ppapi::host::HostMessageContext* context,
    113                          const std::vector<ui::LatencyInfo>& latency_info);
    114   int32_t OnHostMsgSetScale(ppapi::host::HostMessageContext* context,
    115                             float scale);
    116   int32_t OnHostMsgReadImageData(ppapi::host::HostMessageContext* context,
    117                                  PP_Resource image,
    118                                  const PP_Point& top_left);
    119 
    120   // If |old_image_data| is not NULL, a previous used ImageData object will be
    121   // reused.  This is used by ReplaceContents.
    122   int32_t Flush(PP_Resource* old_image_data);
    123 
    124   // Called internally to execute the different queued commands. The
    125   // parameters to these functions will have already been validated. The last
    126   // rect argument will be filled by each function with the area affected by
    127   // the update that requires invalidation. If there were no pixels changed,
    128   // this rect can be untouched.
    129   void ExecutePaintImageData(PPB_ImageData_Impl* image,
    130                              int x,
    131                              int y,
    132                              const gfx::Rect& src_rect,
    133                              gfx::Rect* invalidated_rect);
    134   void ExecuteScroll(const gfx::Rect& clip,
    135                      int dx,
    136                      int dy,
    137                      gfx::Rect* invalidated_rect);
    138   void ExecuteReplaceContents(PPB_ImageData_Impl* image,
    139                               gfx::Rect* invalidated_rect,
    140                               PP_Resource* old_image_data);
    141 
    142   void SendFlushAck();
    143 
    144   // Function scheduled to execute by ScheduleOffscreenFlushAck that actually
    145   // issues the offscreen callbacks.
    146   void SendOffscreenFlushAck();
    147 
    148   // Schedules the offscreen flush ACK at a future time.
    149   void ScheduleOffscreenFlushAck();
    150 
    151   // Returns true if there is any type of flush callback pending.
    152   bool HasPendingFlush() const;
    153 
    154   // Scale |op_rect| to logical pixels, taking care to include partially-
    155   // covered logical pixels (aka DIPs). Also scale optional |delta| to logical
    156   // pixels as well for scrolling cases. Returns false for scrolling cases where
    157   // scaling either |op_rect| or |delta| would require scrolling to fall back to
    158   // invalidation due to rounding errors, true otherwise.
    159   static bool ConvertToLogicalPixels(float scale,
    160                                      gfx::Rect* op_rect,
    161                                      gfx::Point* delta);
    162 
    163   void ReleaseCallback(scoped_ptr<cc::SharedBitmap> bitmap,
    164                        const gfx::Size& bitmap_size,
    165                        uint32 sync_point,
    166                        bool lost_resource);
    167 
    168   RendererPpapiHost* renderer_ppapi_host_;
    169 
    170   scoped_refptr<PPB_ImageData_Impl> image_data_;
    171 
    172   // Non-owning pointer to the plugin instance this context is currently bound
    173   // to, if any. If the context is currently unbound, this will be NULL.
    174   PepperPluginInstanceImpl* bound_instance_;
    175 
    176   // Keeps track of all drawing commands queued before a Flush call.
    177   struct QueuedOperation;
    178   typedef std::vector<QueuedOperation> OperationQueue;
    179   OperationQueue queued_operations_;
    180 
    181   // True if we need to send an ACK to plugin.
    182   bool need_flush_ack_;
    183 
    184   // When doing offscreen flushes, we issue a task that issues the callback
    185   // later. This is set when one of those tasks is pending so that we can
    186   // enforce the "only one pending flush at a time" constraint in the API.
    187   bool offscreen_flush_pending_;
    188 
    189   // Set to true if the plugin declares that this device will always be opaque.
    190   // This allows us to do more optimized painting in some cases.
    191   bool is_always_opaque_;
    192 
    193   // Set to the scale between what the plugin considers to be one pixel and one
    194   // DIP
    195   float scale_;
    196 
    197   ppapi::host::ReplyMessageContext flush_reply_context_;
    198 
    199   bool is_running_in_process_;
    200 
    201   bool texture_mailbox_modified_;
    202   bool is_using_texture_layer_;
    203 
    204   // This is a bitmap that was recently released by the compositor and may be
    205   // used to transfer bytes to the compositor again.
    206   scoped_ptr<cc::SharedBitmap> cached_bitmap_;
    207   gfx::Size cached_bitmap_size_;
    208 
    209   friend class PepperGraphics2DHostTest;
    210   DISALLOW_COPY_AND_ASSIGN(PepperGraphics2DHost);
    211 };
    212 
    213 }  // namespace content
    214 
    215 #endif  // CONTENT_RENDERER_PEPPER_PEPPER_GRAPHICS_2D_HOST_H_
    216