1// Copyright 2013 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_BROWSER_ANDROID_IN_PROCESS_SYNCHRONOUS_COMPOSITOR_IMPL_H_
6#define CONTENT_BROWSER_ANDROID_IN_PROCESS_SYNCHRONOUS_COMPOSITOR_IMPL_H_
7
8#include <vector>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "base/memory/scoped_ptr.h"
13#include "cc/input/layer_scroll_offset_delegate.h"
14#include "content/browser/android/in_process/synchronous_compositor_output_surface.h"
15#include "content/common/input/input_event_ack_state.h"
16#include "content/public/browser/android/synchronous_compositor.h"
17#include "content/public/browser/web_contents_user_data.h"
18#include "ipc/ipc_message.h"
19
20namespace cc {
21class InputHandler;
22}
23
24namespace blink {
25class WebInputEvent;
26}
27
28namespace content {
29class InputHandlerManager;
30struct DidOverscrollParams;
31
32// The purpose of this class is to act as the intermediary between the various
33// components that make up the 'synchronous compositor mode' implementation and
34// expose their functionality via the SynchronousCompositor interface.
35// This class is created on the main thread but most of the APIs are called
36// from the Compositor thread.
37class SynchronousCompositorImpl
38    : public cc::LayerScrollOffsetDelegate,
39      public SynchronousCompositor,
40      public SynchronousCompositorOutputSurfaceDelegate,
41      public WebContentsUserData<SynchronousCompositorImpl> {
42 public:
43  // When used from browser code, use both |process_id| and |routing_id|.
44  static SynchronousCompositorImpl* FromID(int process_id, int routing_id);
45  // When handling upcalls from renderer code, use this version; the process id
46  // is implicitly that of the in-process renderer.
47  static SynchronousCompositorImpl* FromRoutingID(int routing_id);
48
49  InputEventAckState HandleInputEvent(const blink::WebInputEvent& input_event);
50
51  // SynchronousCompositor
52  virtual void SetClient(SynchronousCompositorClient* compositor_client)
53      OVERRIDE;
54  virtual bool InitializeHwDraw() OVERRIDE;
55  virtual void ReleaseHwDraw() OVERRIDE;
56  virtual scoped_ptr<cc::CompositorFrame> DemandDrawHw(
57      gfx::Size surface_size,
58      const gfx::Transform& transform,
59      gfx::Rect viewport,
60      gfx::Rect clip,
61      gfx::Rect viewport_rect_for_tile_priority,
62      const gfx::Transform& transform_for_tile_priority) OVERRIDE;
63  virtual bool DemandDrawSw(SkCanvas* canvas) OVERRIDE;
64  virtual void ReturnResources(
65      const cc::CompositorFrameAck& frame_ack) OVERRIDE;
66  virtual void SetMemoryPolicy(
67      const SynchronousCompositorMemoryPolicy& policy) OVERRIDE;
68  virtual void DidChangeRootLayerScrollOffset() OVERRIDE;
69
70  // SynchronousCompositorOutputSurfaceDelegate
71  virtual void DidBindOutputSurface(
72      SynchronousCompositorOutputSurface* output_surface) OVERRIDE;
73  virtual void DidDestroySynchronousOutputSurface(
74      SynchronousCompositorOutputSurface* output_surface) OVERRIDE;
75  virtual void SetContinuousInvalidate(bool enable) OVERRIDE;
76  virtual void DidActivatePendingTree() OVERRIDE;
77
78  // LayerScrollOffsetDelegate
79  virtual gfx::Vector2dF GetTotalScrollOffset() OVERRIDE;
80  virtual void UpdateRootLayerState(const gfx::Vector2dF& total_scroll_offset,
81                                    const gfx::Vector2dF& max_scroll_offset,
82                                    const gfx::SizeF& scrollable_size,
83                                    float page_scale_factor,
84                                    float min_page_scale_factor,
85                                    float max_page_scale_factor) OVERRIDE;
86  virtual bool IsExternalFlingActive() const OVERRIDE;
87
88  void SetInputHandler(cc::InputHandler* input_handler);
89  void DidOverscroll(const DidOverscrollParams& params);
90  void DidStopFlinging();
91
92 private:
93  explicit SynchronousCompositorImpl(WebContents* contents);
94  virtual ~SynchronousCompositorImpl();
95  friend class WebContentsUserData<SynchronousCompositorImpl>;
96
97  void UpdateFrameMetaData(const cc::CompositorFrameMetadata& frame_info);
98  void DeliverMessages();
99  bool CalledOnValidThread() const;
100
101  SynchronousCompositorClient* compositor_client_;
102  SynchronousCompositorOutputSurface* output_surface_;
103  WebContents* contents_;
104  cc::InputHandler* input_handler_;
105
106  base::WeakPtrFactory<SynchronousCompositorImpl> weak_ptr_factory_;
107
108  DISALLOW_COPY_AND_ASSIGN(SynchronousCompositorImpl);
109};
110
111}  // namespace content
112
113#endif  // CONTENT_BROWSER_ANDROID_IN_PROCESS_SYNCHRONOUS_COMPOSITOR_IMPL_H_
114