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(gfx::Size size) OVERRIDE; 30 31 virtual SkCanvas* BeginPaint(gfx::Rect damage_rect) OVERRIDE; 32 virtual void EndPaint(cc::SoftwareFrameData* frame_data) OVERRIDE; 33 34 virtual void ReclaimSoftwareFrame(unsigned id) OVERRIDE; 35 36 private: 37 // Internal buffer class that manages shared memory lifetime and ownership. 38 // It also tracks buffers' history so we can calculate what's the minimum 39 // damage rect difference between any two given buffers (see SetParent and 40 // FindDamageDifferenceFrom). 41 class Buffer { 42 public: 43 explicit Buffer(unsigned id, scoped_ptr<base::SharedMemory> mem); 44 ~Buffer(); 45 46 unsigned id() const { return id_; } 47 48 void* memory() const { return mem_->memory(); } 49 base::SharedMemoryHandle handle() const { return mem_->handle(); } 50 51 bool free() const { return free_; } 52 void SetFree(bool free) { free_ = free; } 53 54 Buffer* parent() const { return parent_; } 55 void SetParent(Buffer* parent, const gfx::Rect& damage); 56 57 bool FindDamageDifferenceFrom(Buffer* buffer, SkRegion* result) const; 58 59 private: 60 const unsigned id_; 61 scoped_ptr<base::SharedMemory> mem_; 62 bool free_; 63 Buffer* parent_; 64 gfx::Rect damage_; 65 66 DISALLOW_COPY_AND_ASSIGN(Buffer); 67 }; 68 69 class CompareById { 70 public: 71 CompareById(unsigned id) : id_(id) {} 72 73 bool operator()(const Buffer* buffer) const { 74 return buffer->id() == id_; 75 } 76 77 private: 78 const unsigned id_; 79 }; 80 81 unsigned GetNextId(); 82 Buffer* CreateBuffer(); 83 size_t FindFreeBuffer(size_t hint); 84 85 size_t current_index_; 86 unsigned next_buffer_id_; 87 ScopedVector<Buffer> buffers_; 88 ScopedVector<Buffer> awaiting_ack_; 89 SkBitmap bitmap_; 90 RenderThread* render_thread_; 91 }; 92 93 } // namespace content 94 95 #endif // CONTENT_RENDERER_GPU_COMPOSITOR_SOFTWARE_OUTPUT_DEVICE_H_ 96