Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / renderer / gpu / compositor_software_output_device.h
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 "content/public/renderer/render_thread.h"
14 #include "third_party/skia/include/core/SkBitmap.h"
15
16 class SkRegion;
17
18 namespace content {
19
20 // This class can be created only on the main thread, but then becomes pinned
21 // to a fixed thread when BindToClient is called.
22 class CompositorSoftwareOutputDevice
23     : NON_EXPORTED_BASE(public cc::SoftwareOutputDevice),
24       NON_EXPORTED_BASE(public base::NonThreadSafe) {
25 public:
26   CompositorSoftwareOutputDevice();
27   virtual ~CompositorSoftwareOutputDevice();
28
29   virtual void Resize(const gfx::Size& size) OVERRIDE;
30
31   virtual SkCanvas* BeginPaint(const gfx::Rect& damage_rect) OVERRIDE;
32   virtual void EndPaint(cc::SoftwareFrameData* frame_data) OVERRIDE;
33   virtual void EnsureBackbuffer() OVERRIDE;
34   virtual void DiscardBackbuffer() OVERRIDE;
35
36   virtual void ReclaimSoftwareFrame(unsigned id) OVERRIDE;
37
38 private:
39   // Internal buffer class that manages shared memory lifetime and ownership.
40   // It also tracks buffers' history so we can calculate what's the minimum
41   // damage rect difference between any two given buffers (see SetParent and
42   // FindDamageDifferenceFrom).
43   class Buffer {
44    public:
45     explicit Buffer(unsigned id, scoped_ptr<base::SharedMemory> mem);
46     ~Buffer();
47
48     unsigned id() const { return id_; }
49
50     void* memory() const { return mem_->memory(); }
51     base::SharedMemoryHandle handle() const { return mem_->handle(); }
52     base::SharedMemory* shared_memory() const { return mem_.get(); }
53
54     bool free() const { return free_; }
55     void SetFree(bool free) { free_ = free; }
56
57     Buffer* parent() const { return parent_; }
58     void SetParent(Buffer* parent, const gfx::Rect& damage);
59
60     bool FindDamageDifferenceFrom(Buffer* buffer, SkRegion* result) const;
61
62    private:
63     const unsigned id_;
64     scoped_ptr<base::SharedMemory> mem_;
65     bool free_;
66     Buffer* parent_;
67     gfx::Rect damage_;
68
69     DISALLOW_COPY_AND_ASSIGN(Buffer);
70   };
71
72   class CompareById {
73    public:
74     CompareById(unsigned id) : id_(id) {}
75
76     bool operator()(const Buffer* buffer) const {
77       return buffer->id() == id_;
78     }
79
80    private:
81     const unsigned id_;
82   };
83
84   unsigned GetNextId();
85   Buffer* CreateBuffer();
86   size_t FindFreeBuffer(size_t hint);
87
88   size_t current_index_;
89   unsigned next_buffer_id_;
90   ScopedVector<Buffer> buffers_;
91   ScopedVector<Buffer> awaiting_ack_;
92   SkBitmap bitmap_;
93   RenderThread* render_thread_;
94 };
95
96 }  // namespace content
97
98 #endif  // CONTENT_RENDERER_GPU_COMPOSITOR_SOFTWARE_OUTPUT_DEVICE_H_