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/child/child_shared_bitmap_manager.h" | 9 #include "content/child/child_shared_bitmap_manager.h" |
10 #include "content/renderer/render_process.h" | 10 #include "content/renderer/render_process.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 unsigned id = next_buffer_id_++; | 71 unsigned id = next_buffer_id_++; |
72 // Zero is reserved to label invalid frame id. | 72 // Zero is reserved to label invalid frame id. |
73 if (id == 0) | 73 if (id == 0) |
74 id = next_buffer_id_++; | 74 id = next_buffer_id_++; |
75 return id; | 75 return id; |
76 } | 76 } |
77 | 77 |
78 CompositorSoftwareOutputDevice::Buffer* | 78 CompositorSoftwareOutputDevice::Buffer* |
79 CompositorSoftwareOutputDevice::CreateBuffer() { | 79 CompositorSoftwareOutputDevice::CreateBuffer() { |
80 scoped_ptr<cc::SharedBitmap> shared_bitmap = | 80 scoped_ptr<cc::SharedBitmap> shared_bitmap = |
81 shared_bitmap_manager_->AllocateSharedBitmap(viewport_size_); | 81 shared_bitmap_manager_->AllocateSharedBitmap(viewport_pixel_size_); |
82 CHECK(shared_bitmap); | 82 CHECK(shared_bitmap); |
83 return new Buffer(GetNextId(), shared_bitmap.Pass()); | 83 return new Buffer(GetNextId(), shared_bitmap.Pass()); |
84 } | 84 } |
85 | 85 |
86 size_t CompositorSoftwareOutputDevice::FindFreeBuffer(size_t hint) { | 86 size_t CompositorSoftwareOutputDevice::FindFreeBuffer(size_t hint) { |
87 for (size_t i = 0; i < buffers_.size(); ++i) { | 87 for (size_t i = 0; i < buffers_.size(); ++i) { |
88 size_t index = (hint + i) % buffers_.size(); | 88 size_t index = (hint + i) % buffers_.size(); |
89 if (buffers_[index]->free()) | 89 if (buffers_[index]->free()) |
90 return index; | 90 return index; |
91 } | 91 } |
92 | 92 |
93 buffers_.push_back(CreateBuffer()); | 93 buffers_.push_back(CreateBuffer()); |
94 return buffers_.size() - 1; | 94 return buffers_.size() - 1; |
95 } | 95 } |
96 | 96 |
97 void CompositorSoftwareOutputDevice::Resize(const gfx::Size& viewport_size) { | 97 void CompositorSoftwareOutputDevice::Resize( |
| 98 const gfx::Size& viewport_pixel_size, |
| 99 float scale_factor) { |
98 DCHECK(CalledOnValidThread()); | 100 DCHECK(CalledOnValidThread()); |
99 | 101 |
100 if (viewport_size_ == viewport_size) | 102 scale_factor_ = scale_factor; |
| 103 |
| 104 if (viewport_pixel_size_ == viewport_pixel_size) |
101 return; | 105 return; |
102 | 106 |
103 // Keep non-ACKed buffers in awaiting_ack_ until they get acknowledged. | 107 // Keep non-ACKed buffers in awaiting_ack_ until they get acknowledged. |
104 for (size_t i = 0; i < buffers_.size(); ++i) { | 108 for (size_t i = 0; i < buffers_.size(); ++i) { |
105 if (!buffers_[i]->free()) { | 109 if (!buffers_[i]->free()) { |
106 awaiting_ack_.push_back(buffers_[i]); | 110 awaiting_ack_.push_back(buffers_[i]); |
107 buffers_[i] = NULL; | 111 buffers_[i] = NULL; |
108 } | 112 } |
109 } | 113 } |
110 | 114 |
111 buffers_.clear(); | 115 buffers_.clear(); |
112 current_index_ = -1; | 116 current_index_ = -1; |
113 viewport_size_ = viewport_size; | 117 viewport_pixel_size_ = viewport_pixel_size; |
114 } | 118 } |
115 | 119 |
116 void CompositorSoftwareOutputDevice::DiscardBackbuffer() { | 120 void CompositorSoftwareOutputDevice::DiscardBackbuffer() { |
117 // Keep non-ACKed buffers in awaiting_ack_ until they get acknowledged. | 121 // Keep non-ACKed buffers in awaiting_ack_ until they get acknowledged. |
118 for (size_t i = 0; i < buffers_.size(); ++i) { | 122 for (size_t i = 0; i < buffers_.size(); ++i) { |
119 if (!buffers_[i]->free()) { | 123 if (!buffers_[i]->free()) { |
120 awaiting_ack_.push_back(buffers_[i]); | 124 awaiting_ack_.push_back(buffers_[i]); |
121 buffers_[i] = NULL; | 125 buffers_[i] = NULL; |
122 } | 126 } |
123 } | 127 } |
(...skipping 10 matching lines...) Expand all Loading... |
134 | 138 |
135 Buffer* previous = NULL; | 139 Buffer* previous = NULL; |
136 if (current_index_ != size_t(-1)) | 140 if (current_index_ != size_t(-1)) |
137 previous = buffers_[current_index_]; | 141 previous = buffers_[current_index_]; |
138 current_index_ = FindFreeBuffer(current_index_ + 1); | 142 current_index_ = FindFreeBuffer(current_index_ + 1); |
139 Buffer* current = buffers_[current_index_]; | 143 Buffer* current = buffers_[current_index_]; |
140 DCHECK(current->free()); | 144 DCHECK(current->free()); |
141 current->SetFree(false); | 145 current->SetFree(false); |
142 | 146 |
143 // Set up a canvas for the current front buffer. | 147 // Set up a canvas for the current front buffer. |
144 SkImageInfo info = SkImageInfo::MakeN32Premul(viewport_size_.width(), | 148 SkImageInfo info = SkImageInfo::MakeN32Premul(viewport_pixel_size_.width(), |
145 viewport_size_.height()); | 149 viewport_pixel_size_.height()); |
146 SkBitmap bitmap; | 150 SkBitmap bitmap; |
147 bitmap.installPixels(info, current->memory(), info.minRowBytes()); | 151 bitmap.installPixels(info, current->memory(), info.minRowBytes()); |
148 canvas_ = skia::AdoptRef(new SkCanvas(bitmap)); | 152 canvas_ = skia::AdoptRef(new SkCanvas(bitmap)); |
149 | 153 |
150 if (!previous) { | 154 if (!previous) { |
151 DCHECK(damage_rect == gfx::Rect(viewport_size_)); | 155 DCHECK(damage_rect == gfx::Rect(viewport_pixel_size_)); |
152 } else { | 156 } else { |
153 // Find the smallest damage region that needs | 157 // Find the smallest damage region that needs |
154 // to be copied from the |previous| buffer. | 158 // to be copied from the |previous| buffer. |
155 SkRegion region; | 159 SkRegion region; |
156 bool found = | 160 bool found = |
157 current->FindDamageDifferenceFrom(previous, ®ion) || | 161 current->FindDamageDifferenceFrom(previous, ®ion) || |
158 previous->FindDamageDifferenceFrom(current, ®ion); | 162 previous->FindDamageDifferenceFrom(current, ®ion); |
159 if (!found) | 163 if (!found) |
160 region = SkRegion(RectToSkIRect(gfx::Rect(viewport_size_))); | 164 region = SkRegion(RectToSkIRect(gfx::Rect(viewport_pixel_size_))); |
161 region.op(RectToSkIRect(damage_rect), SkRegion::kDifference_Op); | 165 region.op(RectToSkIRect(damage_rect), SkRegion::kDifference_Op); |
162 | 166 |
163 // Copy over the damage region. | 167 // Copy over the damage region. |
164 if (!region.isEmpty()) { | 168 if (!region.isEmpty()) { |
165 SkBitmap back_bitmap; | 169 SkBitmap back_bitmap; |
166 back_bitmap.setConfig(SkBitmap::kARGB_8888_Config, | 170 back_bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
167 viewport_size_.width(), | 171 viewport_pixel_size_.width(), |
168 viewport_size_.height()); | 172 viewport_pixel_size_.height()); |
169 back_bitmap.setPixels(previous->memory()); | 173 back_bitmap.setPixels(previous->memory()); |
170 | 174 |
171 for (SkRegion::Iterator it(region); !it.done(); it.next()) { | 175 for (SkRegion::Iterator it(region); !it.done(); it.next()) { |
172 const SkIRect& src_rect = it.rect(); | 176 const SkIRect& src_rect = it.rect(); |
173 SkRect dst_rect = SkRect::Make(src_rect); | 177 SkRect dst_rect = SkRect::Make(src_rect); |
174 canvas_->drawBitmapRect(back_bitmap, &src_rect, dst_rect, NULL); | 178 canvas_->drawBitmapRect(back_bitmap, &src_rect, dst_rect, NULL); |
175 } | 179 } |
176 } | 180 } |
177 } | 181 } |
178 | 182 |
179 // Make |current| child of |previous| and orphan all of |current|'s children. | 183 // Make |current| child of |previous| and orphan all of |current|'s children. |
180 current->SetParent(previous, damage_rect); | 184 current->SetParent(previous, damage_rect); |
181 for (size_t i = 0; i < buffers_.size(); ++i) { | 185 for (size_t i = 0; i < buffers_.size(); ++i) { |
182 Buffer* buffer = buffers_[i]; | 186 Buffer* buffer = buffers_[i]; |
183 if (buffer->parent() == current) | 187 if (buffer->parent() == current) |
184 buffer->SetParent(NULL, gfx::Rect(viewport_size_)); | 188 buffer->SetParent(NULL, gfx::Rect(viewport_pixel_size_)); |
185 } | 189 } |
186 damage_rect_ = damage_rect; | 190 damage_rect_ = damage_rect; |
187 | 191 |
188 return canvas_.get(); | 192 return canvas_.get(); |
189 } | 193 } |
190 | 194 |
191 void CompositorSoftwareOutputDevice::EndPaint( | 195 void CompositorSoftwareOutputDevice::EndPaint( |
192 cc::SoftwareFrameData* frame_data) { | 196 cc::SoftwareFrameData* frame_data) { |
193 DCHECK(CalledOnValidThread()); | 197 DCHECK(CalledOnValidThread()); |
194 DCHECK(frame_data); | 198 DCHECK(frame_data); |
195 | 199 |
196 Buffer* buffer = buffers_[current_index_]; | 200 Buffer* buffer = buffers_[current_index_]; |
197 frame_data->id = buffer->id(); | 201 frame_data->id = buffer->id(); |
198 frame_data->size = viewport_size_; | 202 frame_data->size = viewport_pixel_size_; |
199 frame_data->damage_rect = damage_rect_; | 203 frame_data->damage_rect = damage_rect_; |
200 frame_data->bitmap_id = buffer->shared_bitmap_id(); | 204 frame_data->bitmap_id = buffer->shared_bitmap_id(); |
201 } | 205 } |
202 | 206 |
203 void CompositorSoftwareOutputDevice::ReclaimSoftwareFrame(unsigned id) { | 207 void CompositorSoftwareOutputDevice::ReclaimSoftwareFrame(unsigned id) { |
204 DCHECK(CalledOnValidThread()); | 208 DCHECK(CalledOnValidThread()); |
205 | 209 |
206 if (!id) | 210 if (!id) |
207 return; | 211 return; |
208 | 212 |
209 // The reclaimed buffer id might not be among the currently | 213 // The reclaimed buffer id might not be among the currently |
210 // active buffers if we got a resize event in the mean time. | 214 // active buffers if we got a resize event in the mean time. |
211 ScopedVector<Buffer>::iterator it = | 215 ScopedVector<Buffer>::iterator it = |
212 std::find_if(buffers_.begin(), buffers_.end(), CompareById(id)); | 216 std::find_if(buffers_.begin(), buffers_.end(), CompareById(id)); |
213 if (it != buffers_.end()) { | 217 if (it != buffers_.end()) { |
214 DCHECK(!(*it)->free()); | 218 DCHECK(!(*it)->free()); |
215 (*it)->SetFree(true); | 219 (*it)->SetFree(true); |
216 return; | 220 return; |
217 } else { | 221 } else { |
218 it = std::find_if(awaiting_ack_.begin(), awaiting_ack_.end(), | 222 it = std::find_if(awaiting_ack_.begin(), awaiting_ack_.end(), |
219 CompareById(id)); | 223 CompareById(id)); |
220 DCHECK(it != awaiting_ack_.end()); | 224 DCHECK(it != awaiting_ack_.end()); |
221 awaiting_ack_.erase(it); | 225 awaiting_ack_.erase(it); |
222 } | 226 } |
223 } | 227 } |
224 | 228 |
225 } // namespace content | 229 } // namespace content |
OLD | NEW |