1// Copyright 2014 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_COMPOSITOR_BUFFERED_OUTPUT_SURFACE_H_
6#define CONTENT_BROWSER_COMPOSITOR_BUFFERED_OUTPUT_SURFACE_H_
7
8#include <queue>
9#include <vector>
10
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "content/common/content_export.h"
14#include "ui/gfx/rect.h"
15#include "ui/gfx/size.h"
16
17namespace cc {
18class ContextProvider;
19}
20
21namespace content {
22
23// Provides a surface that manages its own uffers, backed by
24// CreateImageCHROMIUM. Double/triple buffering is implemented
25// internally. Doublebuffering occurs if PageFlipComplete is called before
26// the next BindFramebuffer call, otherwise it creates extra buffers.
27class CONTENT_EXPORT BufferQueue {
28 public:
29  BufferQueue(scoped_refptr<cc::ContextProvider> context_provider,
30              unsigned int internalformat);
31  virtual ~BufferQueue();
32
33  bool Initialize();
34
35  void BindFramebuffer();
36  void SwapBuffers(const gfx::Rect& damage);
37  void PageFlipComplete();
38  void Reshape(const gfx::Size& size, float scale_factor);
39
40  unsigned int current_texture_id() { return current_surface_.texture; }
41
42 private:
43  friend class BufferQueueTest;
44
45  struct AllocatedSurface {
46    AllocatedSurface() : texture(0), image(0) {}
47    AllocatedSurface(unsigned int texture,
48                     unsigned int image,
49                     const gfx::Rect& rect)
50        : texture(texture), image(image), damage(rect) {}
51    unsigned int texture;
52    unsigned int image;
53    gfx::Rect damage;  // This is the damage for this frame from the previous.
54  };
55
56  void FreeAllSurfaces();
57
58  void FreeSurface(AllocatedSurface* surface);
59
60  // Copy everything that is in |copy_rect|, except for what is in
61  // |exclude_rect| from |source_texture| to |texture|.
62  virtual void CopyBufferDamage(int texture,
63                                int source_texture,
64                                const gfx::Rect& new_damage,
65                                const gfx::Rect& old_damage);
66
67  void UpdateBufferDamage(const gfx::Rect& damage);
68
69  // Return a surface, available to be drawn into.
70  AllocatedSurface GetNextSurface();
71
72  gfx::Size size_;
73  scoped_refptr<cc::ContextProvider> context_provider_;
74  unsigned int fbo_;
75  size_t allocated_count_;
76  unsigned int internalformat_;
77  AllocatedSurface current_surface_;  // This surface is currently bound.
78  std::vector<AllocatedSurface> available_surfaces_;  // These are free for use.
79  std::deque<AllocatedSurface> in_flight_surfaces_;
80
81  DISALLOW_COPY_AND_ASSIGN(BufferQueue);
82};
83
84}  // namespace content
85
86#endif  // CONTENT_BROWSER_COMPOSITOR_BUFFERED_OUTPUT_SURFACE_H_
87