OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/gpu/compositor_software_output_device.h" | 5 #include "content/renderer/gpu/compositor_software_output_device.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "cc/output/software_frame_data.h" | 8 #include "cc/output/software_frame_data.h" |
9 #include "content/renderer/render_process.h" | 9 #include "content/renderer/render_process.h" |
10 #include "third_party/skia/include/core/SkCanvas.h" | 10 #include "third_party/skia/include/core/SkCanvas.h" |
11 #include "third_party/skia/include/core/SkDevice.h" | 11 #include "third_party/skia/include/core/SkDevice.h" |
12 #include "third_party/skia/include/core/SkPixelRef.h" | 12 #include "third_party/skia/include/core/SkPixelRef.h" |
13 #include "third_party/skia/include/core/SkRegion.h" | |
13 #include "ui/gfx/skia_util.h" | 14 #include "ui/gfx/skia_util.h" |
14 #include "ui/surface/transport_dib.h" | |
15 | 15 |
16 namespace content { | 16 namespace content { |
17 | 17 |
18 CompositorSoftwareOutputDevice::DIB::DIB(size_t size) { | 18 CompositorSoftwareOutputDevice::Buffer::Buffer( |
19 RenderProcess* render_process = RenderProcess::current(); | 19 int id, scoped_ptr<base::SharedMemory> mem) |
20 dib_ = render_process->CreateTransportDIB(size); | 20 : id_(id), |
21 CHECK(dib_); | 21 mem_(mem.Pass()), |
22 bool success = dib_->Map(); | 22 free_(true), |
23 CHECK(success); | 23 parent_(NULL) { |
24 } | 24 } |
25 | 25 |
26 CompositorSoftwareOutputDevice::DIB::~DIB() { | 26 CompositorSoftwareOutputDevice::Buffer::~Buffer() { |
27 RenderProcess* render_process = RenderProcess::current(); | 27 } |
28 render_process->FreeTransportDIB(dib_); | 28 |
29 void CompositorSoftwareOutputDevice::Buffer::SetParent( | |
30 Buffer* parent, const gfx::Rect& damage) { | |
31 parent_ = parent; | |
32 damage_ = damage; | |
33 } | |
34 | |
35 bool CompositorSoftwareOutputDevice::Buffer::FindDamageDifferenceFrom( | |
36 Buffer* buffer, SkRegion* result) const { | |
37 if (!buffer) | |
38 return false; | |
39 | |
40 if (buffer == this) { | |
41 *result = SkRegion(); | |
42 return true; | |
43 } | |
44 | |
45 SkRegion damage; | |
46 const Buffer* current = this; | |
47 while (current->parent_) { | |
48 damage.op(RectToSkIRect(current->damage_), SkRegion::kUnion_Op); | |
49 if (current->parent_ == buffer) { | |
50 *result = damage; | |
51 return true; | |
52 } | |
53 current = current->parent_; | |
54 } | |
55 | |
56 return false; | |
29 } | 57 } |
30 | 58 |
31 CompositorSoftwareOutputDevice::CompositorSoftwareOutputDevice() | 59 CompositorSoftwareOutputDevice::CompositorSoftwareOutputDevice() |
32 : front_buffer_(-1), | 60 : current_index_(-1), |
33 num_free_buffers_(0) { | 61 next_buffer_id_(1), |
62 render_thread_(RenderThread::Get()) { | |
34 DetachFromThread(); | 63 DetachFromThread(); |
35 } | 64 } |
36 | 65 |
37 CompositorSoftwareOutputDevice::~CompositorSoftwareOutputDevice() { | 66 CompositorSoftwareOutputDevice::~CompositorSoftwareOutputDevice() { |
38 DCHECK(CalledOnValidThread()); | 67 DCHECK(CalledOnValidThread()); |
39 } | 68 } |
40 | 69 |
41 CompositorSoftwareOutputDevice::DIB* | 70 int CompositorSoftwareOutputDevice::GetNextId() { |
42 CompositorSoftwareOutputDevice::CreateDIB() { | 71 int id = next_buffer_id_++; |
piman
2013/06/05 00:30:55
unsigned int.
slavi
2013/06/06 23:02:47
Done.
| |
43 const size_t size = 4 * viewport_size_.GetArea(); | 72 // Zero is reserved to label invalid frame id. |
44 return new DIB(size); | 73 if (id == 0) |
74 id = next_buffer_id_++; | |
75 return id; | |
76 } | |
77 | |
78 CompositorSoftwareOutputDevice::Buffer* | |
79 CompositorSoftwareOutputDevice::CreateBuffer() { | |
80 const size_t size = 4 * viewport_size_.GetArea(); | |
81 scoped_ptr<base::SharedMemory> mem = | |
82 render_thread_->HostAllocateSharedMemoryBuffer(size).Pass(); | |
83 CHECK(mem); | |
84 bool success = mem->Map(size); | |
85 CHECK(success); | |
86 return new Buffer(GetNextId(), mem.Pass()); | |
87 } | |
88 | |
89 int CompositorSoftwareOutputDevice::FindFreeBuffer(int hint) { | |
90 for (size_t i = 0; i < buffers_.size(); ++i) { | |
91 int index = (hint + i) % buffers_.size(); | |
92 if (buffers_[index]->free()) | |
93 return index; | |
94 } | |
95 | |
96 buffers_.push_back(CreateBuffer()); | |
97 return buffers_.size() - 1; | |
45 } | 98 } |
46 | 99 |
47 void CompositorSoftwareOutputDevice::Resize(gfx::Size viewport_size) { | 100 void CompositorSoftwareOutputDevice::Resize(gfx::Size viewport_size) { |
48 DCHECK(CalledOnValidThread()); | 101 DCHECK(CalledOnValidThread()); |
49 | 102 |
50 if (viewport_size_ == viewport_size) | 103 if (viewport_size_ == viewport_size) |
51 return; | 104 return; |
52 | 105 |
53 // Keep non-ACKed dibs open. | 106 // Keep non-ACKed buffers in awaiting_ack_ until they get acknowledged. |
54 int first_non_free = front_buffer_ + num_free_buffers_ + 1; | 107 for (size_t i = 0; i < buffers_.size(); ++i) { |
55 int num_non_free = dibs_.size() - num_free_buffers_; | 108 if (!buffers_[i]->free()) { |
56 for (int i = 0; i < num_non_free; ++i) { | 109 awaiting_ack_.push_back(buffers_[i]); |
57 int index = (first_non_free + i) % dibs_.size(); | 110 buffers_[i] = NULL; |
58 awaiting_ack_.push_back(dibs_[index]); | 111 } |
59 dibs_[index] = NULL; | |
60 } | 112 } |
61 | 113 |
62 dibs_.clear(); | 114 buffers_.clear(); |
63 front_buffer_ = -1; | 115 current_index_ = -1; |
64 num_free_buffers_ = 0; | |
65 viewport_size_ = viewport_size; | 116 viewport_size_ = viewport_size; |
66 } | 117 } |
67 | 118 |
68 SkCanvas* CompositorSoftwareOutputDevice::BeginPaint(gfx::Rect damage_rect) { | 119 SkCanvas* CompositorSoftwareOutputDevice::BeginPaint(gfx::Rect damage_rect) { |
69 DCHECK(CalledOnValidThread()); | 120 DCHECK(CalledOnValidThread()); |
70 | 121 |
71 gfx::Rect last_damage_rect = damage_rect_; | 122 Buffer* previous = NULL; |
72 damage_rect_ = damage_rect; | 123 if (current_index_ != -1) |
73 | 124 previous = buffers_[current_index_]; |
74 int last_buffer = front_buffer_; | 125 current_index_ = FindFreeBuffer(current_index_ + 1); |
75 if (num_free_buffers_ == 0) { | 126 Buffer* current = buffers_[current_index_]; |
76 dibs_.insert(dibs_.begin() + (front_buffer_ + 1), CreateDIB()); | 127 DCHECK(current->free()); |
77 last_damage_rect = gfx::Rect(viewport_size_); | 128 current->SetFree(false); |
78 } else { | |
79 --num_free_buffers_; | |
80 } | |
81 front_buffer_ = (front_buffer_ + 1) % dibs_.size(); | |
82 | |
83 TransportDIB* front_dib = dibs_[front_buffer_]->dib(); | |
84 DCHECK(front_dib); | |
85 DCHECK(front_dib->memory()); | |
86 | 129 |
87 // Set up a canvas for the current front buffer. | 130 // Set up a canvas for the current front buffer. |
88 bitmap_.setConfig(SkBitmap::kARGB_8888_Config, | 131 bitmap_.setConfig(SkBitmap::kARGB_8888_Config, |
89 viewport_size_.width(), | 132 viewport_size_.width(), |
90 viewport_size_.height()); | 133 viewport_size_.height()); |
91 bitmap_.setPixels(front_dib->memory()); | 134 bitmap_.setPixels(current->memory()); |
92 device_ = skia::AdoptRef(new SkDevice(bitmap_)); | 135 device_ = skia::AdoptRef(new SkDevice(bitmap_)); |
93 canvas_ = skia::AdoptRef(new SkCanvas(device_.get())); | 136 canvas_ = skia::AdoptRef(new SkCanvas(device_.get())); |
94 | 137 |
95 // Copy over previous damage. | 138 if (!previous) { |
96 if (last_buffer != -1) { | 139 DCHECK(damage_rect == gfx::Rect(viewport_size_)); |
97 TransportDIB* last_dib = dibs_[last_buffer]->dib(); | 140 } else { |
98 SkBitmap back_bitmap; | 141 // Find the smallest damage region that needs |
99 back_bitmap.setConfig(SkBitmap::kARGB_8888_Config, | 142 // to be copied from the |previous| buffer. |
100 viewport_size_.width(), | 143 SkRegion region; |
101 viewport_size_.height()); | 144 bool found = |
102 back_bitmap.setPixels(last_dib->memory()); | 145 current->FindDamageDifferenceFrom(previous, ®ion) || |
146 previous->FindDamageDifferenceFrom(current, ®ion); | |
147 if (!found) | |
148 region = SkRegion(RectToSkIRect(gfx::Rect(viewport_size_))); | |
149 region.op(RectToSkIRect(damage_rect), SkRegion::kDifference_Op); | |
103 | 150 |
104 SkRegion region(RectToSkIRect(last_damage_rect)); | 151 // Copy over the damage region. |
105 region.op(RectToSkIRect(damage_rect), SkRegion::kDifference_Op); | 152 if (!region.isEmpty()) { |
106 for (SkRegion::Iterator it(region); !it.done(); it.next()) { | 153 SkBitmap back_bitmap; |
107 const SkIRect& src_rect = it.rect(); | 154 back_bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
108 SkRect dst_rect = SkRect::Make(src_rect); | 155 viewport_size_.width(), |
109 canvas_->drawBitmapRect(back_bitmap, &src_rect, dst_rect, NULL); | 156 viewport_size_.height()); |
157 back_bitmap.setPixels(previous->memory()); | |
158 | |
159 for (SkRegion::Iterator it(region); !it.done(); it.next()) { | |
160 const SkIRect& src_rect = it.rect(); | |
161 SkRect dst_rect = SkRect::Make(src_rect); | |
162 canvas_->drawBitmapRect(back_bitmap, &src_rect, dst_rect, NULL); | |
163 } | |
110 } | 164 } |
111 } | 165 } |
112 | 166 |
167 // Make |current| child of |previous| and orphan all of |current|'s children. | |
168 current->SetParent(previous, damage_rect); | |
169 for (size_t i = 0; i < buffers_.size(); ++i) { | |
170 Buffer* buffer = buffers_[i]; | |
171 if (buffer->parent() == current) | |
172 buffer->SetParent(NULL, gfx::Rect(viewport_size_)); | |
173 } | |
174 damage_rect_ = damage_rect; | |
175 | |
113 return canvas_.get(); | 176 return canvas_.get(); |
114 } | 177 } |
115 | 178 |
116 void CompositorSoftwareOutputDevice::EndPaint( | 179 void CompositorSoftwareOutputDevice::EndPaint( |
117 cc::SoftwareFrameData* frame_data) { | 180 cc::SoftwareFrameData* frame_data) { |
118 DCHECK(CalledOnValidThread()); | 181 DCHECK(CalledOnValidThread()); |
119 | 182 |
120 if (frame_data) { | 183 if (frame_data) { |
184 Buffer* buffer = buffers_[current_index_]; | |
185 frame_data->id = buffer->id(); | |
121 frame_data->size = viewport_size_; | 186 frame_data->size = viewport_size_; |
122 frame_data->damage_rect = damage_rect_; | 187 frame_data->damage_rect = damage_rect_; |
123 frame_data->dib_id = dibs_[front_buffer_]->dib()->id(); | 188 frame_data->handle = buffer->handle(); |
124 } | 189 } |
125 } | 190 } |
126 | 191 |
127 void CompositorSoftwareOutputDevice::ReclaimDIB(const TransportDIB::Id& id) { | 192 void CompositorSoftwareOutputDevice::ReclaimSoftwareFrame(int id) { |
128 DCHECK(CalledOnValidThread()); | 193 DCHECK(CalledOnValidThread()); |
129 | 194 |
130 if (!TransportDIB::is_valid_id(id)) | 195 if (!id) |
131 return; | 196 return; |
132 | 197 |
133 // The reclaimed dib id might not be among the currently | 198 // The reclaimed buffer id might not be among the currently |
134 // active dibs if we got a resize event in the mean time. | 199 // active buffers if we got a resize event in the mean time. |
135 ScopedVector<DIB>::iterator it = | 200 ScopedVector<Buffer>::iterator it = |
136 std::find_if(dibs_.begin(), dibs_.end(), CompareById(id)); | 201 std::find_if(buffers_.begin(), buffers_.end(), CompareById(id)); |
137 if (it != dibs_.end()) { | 202 if (it != buffers_.end()) { |
138 ++num_free_buffers_; | 203 DCHECK(!(*it)->free()); |
139 DCHECK_LE(static_cast<size_t>(num_free_buffers_), dibs_.size()); | 204 (*it)->SetFree(true); |
140 return; | 205 return; |
141 } else { | 206 } else { |
142 it = std::find_if(awaiting_ack_.begin(), awaiting_ack_.end(), | 207 it = std::find_if(awaiting_ack_.begin(), awaiting_ack_.end(), |
143 CompareById(id)); | 208 CompareById(id)); |
144 DCHECK(it != awaiting_ack_.end()); | 209 DCHECK(it != awaiting_ack_.end()); |
145 awaiting_ack_.erase(it); | 210 awaiting_ack_.erase(it); |
146 } | 211 } |
147 } | 212 } |
148 | 213 |
149 } // namespace content | 214 } // namespace content |
OLD | NEW |