1// Copyright (c) 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_RENDERER_GPU_COMPOSITOR_SOFTWARE_OUTPUT_DEVICE_H_
6#define CONTENT_RENDERER_GPU_COMPOSITOR_SOFTWARE_OUTPUT_DEVICE_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "base/memory/scoped_vector.h"
10#include "base/memory/shared_memory.h"
11#include "base/threading/non_thread_safe.h"
12#include "cc/output/software_output_device.h"
13#include "cc/resources/shared_bitmap.h"
14#include "content/public/renderer/render_thread.h"
15#include "third_party/skia/include/core/SkBitmap.h"
16
17class SkRegion;
18
19namespace cc {
20class SharedBitmapManager;
21}
22
23namespace content {
24
25// This class can be created only on the main thread, but then becomes pinned
26// to a fixed thread when BindToClient is called.
27class CompositorSoftwareOutputDevice
28    : NON_EXPORTED_BASE(public cc::SoftwareOutputDevice),
29      NON_EXPORTED_BASE(public base::NonThreadSafe) {
30 public:
31  CompositorSoftwareOutputDevice();
32  virtual ~CompositorSoftwareOutputDevice();
33
34  virtual void Resize(const gfx::Size& pixel_size, float scale_factor) OVERRIDE;
35
36  virtual SkCanvas* BeginPaint(const gfx::Rect& damage_rect) OVERRIDE;
37  virtual void EndPaint(cc::SoftwareFrameData* frame_data) OVERRIDE;
38  virtual void EnsureBackbuffer() OVERRIDE;
39  virtual void DiscardBackbuffer() OVERRIDE;
40
41  virtual void ReclaimSoftwareFrame(unsigned id) OVERRIDE;
42
43 private:
44  // Internal buffer class that manages shared memory lifetime and ownership.
45  // It also tracks buffers' history so we can calculate what's the minimum
46  // damage rect difference between any two given buffers (see SetParent and
47  // FindDamageDifferenceFrom).
48  class Buffer {
49   public:
50    explicit Buffer(unsigned id, scoped_ptr<cc::SharedBitmap> bitmap);
51    ~Buffer();
52
53    unsigned id() const { return id_; }
54
55    void* memory() const { return shared_bitmap_->pixels(); }
56    cc::SharedBitmapId shared_bitmap_id() const { return shared_bitmap_->id(); }
57
58    bool free() const { return free_; }
59    void SetFree(bool free) { free_ = free; }
60
61    Buffer* parent() const { return parent_; }
62    void SetParent(Buffer* parent, const gfx::Rect& damage);
63
64    bool FindDamageDifferenceFrom(Buffer* buffer, SkRegion* result) const;
65
66   private:
67    const unsigned id_;
68    scoped_ptr<cc::SharedBitmap> shared_bitmap_;
69    bool free_;
70    Buffer* parent_;
71    gfx::Rect damage_;
72
73    DISALLOW_COPY_AND_ASSIGN(Buffer);
74  };
75
76  class CompareById {
77   public:
78    CompareById(unsigned id) : id_(id) {}
79
80    bool operator()(const Buffer* buffer) const {
81      return buffer->id() == id_;
82    }
83
84   private:
85    const unsigned id_;
86  };
87
88  unsigned GetNextId();
89  Buffer* CreateBuffer();
90  size_t FindFreeBuffer(size_t hint);
91
92  size_t current_index_;
93  unsigned next_buffer_id_;
94  ScopedVector<Buffer> buffers_;
95  ScopedVector<Buffer> awaiting_ack_;
96  cc::SharedBitmapManager* shared_bitmap_manager_;
97};
98
99}  // namespace content
100
101#endif  // CONTENT_RENDERER_GPU_COMPOSITOR_SOFTWARE_OUTPUT_DEVICE_H_
102