pepper_view.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
12ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// Use of this source code is governed by a BSD-style license that can be
32ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// found in the LICENSE file.
42ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
52ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// This class is an implementation of the ChromotingView for Pepper.  It is
62ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson// callable only on the Pepper thread.
72ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
82ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#ifndef REMOTING_CLIENT_PLUGIN_PEPPER_VIEW_H_
92ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#define REMOTING_CLIENT_PLUGIN_PEPPER_VIEW_H_
102ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
112ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#include <list>
122ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
132ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#include "base/memory/weak_ptr.h"
142ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#include "ppapi/cpp/graphics_2d.h"
152ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#include "ppapi/cpp/view.h"
162ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#include "ppapi/cpp/point.h"
172ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson#include "remoting/client/frame_consumer.h"
182ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
192ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodsonnamespace base {
202ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodsonclass Time;
212ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson}  // namespace base
222ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
232ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodsonnamespace remoting {
242ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
252ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodsonclass ChromotingInstance;
262ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodsonclass ClientContext;
272ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodsonclass FrameProducer;
282ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
292ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodsonclass PepperView : public FrameConsumer,
302ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson                   public base::SupportsWeakPtr<PepperView> {
312ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson public:
322ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Constructs a PepperView for the |instance|. The |instance|, |context|
332ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // and |producer| must outlive this class.
342ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  PepperView(ChromotingInstance* instance,
352ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson             ClientContext* context,
362ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson             FrameProducer* producer);
372ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  virtual ~PepperView();
382ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
392ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // FrameConsumer implementation.
402ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  virtual void ApplyBuffer(const SkISize& view_size,
412ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson                           const SkIRect& clip_area,
422ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson                           pp::ImageData* buffer,
432ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson                           const SkRegion& region) OVERRIDE;
442ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  virtual void ReturnBuffer(pp::ImageData* buffer) OVERRIDE;
452ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  virtual void SetSourceSize(const SkISize& source_size,
462ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson                             const SkIPoint& dpi) OVERRIDE;
472ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
482ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Updates the PepperView's size & clipping area, taking into account the
492ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // DIP-to-device scale factor.
502ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  void SetView(const pp::View& view);
510d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
522ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Returns the dimensions of the most recently displayed frame, in pixels.
532ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  const SkISize& get_source_size() const {
540d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    return source_size_;
552ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  }
562ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
570d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  // Return the dimensions of the view in Density Independent Pixels (DIPs).
580d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  // Note that there may be multiple device pixels per DIP.
590d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  const SkISize& get_view_size_dips() const {
600d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    return dips_size_;
610d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  }
620d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
630d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin private:
640d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  // Allocates a new frame buffer to supply to the FrameProducer to render into.
652ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Returns NULL if the maximum number of buffers has already been allocated.
662ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  pp::ImageData* AllocateBuffer();
672ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
682ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Frees a frame buffer previously allocated by AllocateBuffer.
690d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  void FreeBuffer(pp::ImageData* buffer);
702ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
712ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Allocates buffers and passes them to the FrameProducer to render into until
722ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // the maximum number of buffers are in-flight.
732ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  void InitiateDrawing();
742ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
752ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Renders the parts of |buffer| identified by |region| to the view.  If the
762ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // clip area of the view has changed since the buffer was generated then
772ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // FrameProducer is supplied the missed parts of |region|.  The FrameProducer
782ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // will be supplied a new buffer when FlushBuffer() completes.
790d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  void FlushBuffer(const SkIRect& clip_area,
802ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson                   pp::ImageData* buffer,
812ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson                   const SkRegion& region);
822ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
832ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // Handles completion of FlushBuffer(), triggering a new buffer to be
842ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  // returned to FrameProducer for rendering.
852ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson  void OnFlushDone(base::Time paint_start, pp::ImageData* buffer, int result);
862ee91b4af4353b9e6a9d591c32fedfc58fd4ef35Ian Hodson
87  // Reference to the creating plugin instance. Needed for interacting with
88  // pepper.  Marking explicitly as const since it must be initialized at
89  // object creation, and never change.
90  ChromotingInstance* const instance_;
91
92  // Context should be constant for the lifetime of the plugin.
93  ClientContext* const context_;
94
95  pp::Graphics2D graphics2d_;
96
97  FrameProducer* producer_;
98
99  // List of allocated image buffers.
100  std::list<pp::ImageData*> buffers_;
101
102  // Queued buffer to paint, with clip area and dirty region in device pixels.
103  pp::ImageData* merge_buffer_;
104  SkIRect merge_clip_area_;
105  SkRegion merge_region_;
106
107  // View size in Density Independent Pixels (DIPs).
108  SkISize dips_size_;
109
110  // Scale factor from DIPs to device pixels.
111  float dips_to_device_scale_;
112
113  // View size in output pixels. This is the size at which FrameProducer must
114  // render frames. It usually matches the DIPs size of the view, but may match
115  // the size in device pixels when scaling is in effect, to reduce artefacts.
116  SkISize view_size_;
117
118  // Scale factor from output pixels to device pixels.
119  float dips_to_view_scale_;
120
121  // Visible area of the view, in output pixels.
122  SkIRect clip_area_;
123
124  // Size of the most recent source frame in pixels.
125  SkISize source_size_;
126
127  // Resolution of the most recent source frame dots-per-inch.
128  SkIPoint source_dpi_;
129
130  // True if there is already a Flush() pending on the Graphics2D context.
131  bool flush_pending_;
132
133  // True after Initialize() has been called, until TearDown().
134  bool is_initialized_;
135
136  // True after the first call to ApplyBuffer().
137  bool frame_received_;
138
139  DISALLOW_COPY_AND_ASSIGN(PepperView);
140};
141
142}  // namespace remoting
143
144#endif  // REMOTING_CLIENT_PLUGIN_PEPPER_VIEW_H_
145