Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / gpu / compositor_software_output_device.h
blob48fa0507ebe5fec5a276235346c590c4dda29ba9
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.
5 #ifndef CONTENT_RENDERER_GPU_COMPOSITOR_SOFTWARE_OUTPUT_DEVICE_H_
6 #define CONTENT_RENDERER_GPU_COMPOSITOR_SOFTWARE_OUTPUT_DEVICE_H_
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"
17 class SkRegion;
19 namespace cc {
20 class SharedBitmapManager;
23 namespace content {
25 // This class can be created only on the main thread, but then becomes pinned
26 // to a fixed thread when BindToClient is called.
27 class CompositorSoftwareOutputDevice
28 : NON_EXPORTED_BASE(public cc::SoftwareOutputDevice),
29 NON_EXPORTED_BASE(public base::NonThreadSafe) {
30 public:
31 CompositorSoftwareOutputDevice();
32 ~CompositorSoftwareOutputDevice() override;
34 void Resize(const gfx::Size& pixel_size, float scale_factor) override;
36 SkCanvas* BeginPaint(const gfx::Rect& damage_rect) override;
37 void EndPaint(cc::SoftwareFrameData* frame_data) override;
38 void EnsureBackbuffer() override;
39 void DiscardBackbuffer() override;
41 void ReclaimSoftwareFrame(unsigned id) override;
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();
53 unsigned id() const { return id_; }
55 void* memory() const { return shared_bitmap_->pixels(); }
56 cc::SharedBitmapId shared_bitmap_id() const { return shared_bitmap_->id(); }
58 bool free() const { return free_; }
59 void SetFree(bool free) { free_ = free; }
61 Buffer* parent() const { return parent_; }
62 void SetParent(Buffer* parent, const gfx::Rect& damage);
64 bool FindDamageDifferenceFrom(Buffer* buffer, SkRegion* result) const;
66 private:
67 const unsigned id_;
68 scoped_ptr<cc::SharedBitmap> shared_bitmap_;
69 bool free_;
70 Buffer* parent_;
71 gfx::Rect damage_;
73 DISALLOW_COPY_AND_ASSIGN(Buffer);
76 class CompareById {
77 public:
78 CompareById(unsigned id) : id_(id) {}
80 bool operator()(const Buffer* buffer) const {
81 return buffer->id() == id_;
84 private:
85 const unsigned id_;
88 unsigned GetNextId();
89 Buffer* CreateBuffer();
90 size_t FindFreeBuffer(size_t hint);
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_;
99 } // namespace content
101 #endif // CONTENT_RENDERER_GPU_COMPOSITOR_SOFTWARE_OUTPUT_DEVICE_H_