OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "cc/scheduler/texture_uploader.h" | 5 #include "cc/scheduler/texture_uploader.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 available_queries_.front()->End(); | 129 available_queries_.front()->End(); |
130 pending_queries_.push_back(available_queries_.take_front()); | 130 pending_queries_.push_back(available_queries_.take_front()); |
131 num_blocking_texture_uploads_++; | 131 num_blocking_texture_uploads_++; |
132 } | 132 } |
133 | 133 |
134 void TextureUploader::Upload(const uint8* image, | 134 void TextureUploader::Upload(const uint8* image, |
135 gfx::Rect image_rect, | 135 gfx::Rect image_rect, |
136 gfx::Rect source_rect, | 136 gfx::Rect source_rect, |
137 gfx::Vector2d dest_offset, | 137 gfx::Vector2d dest_offset, |
138 GLenum format, | 138 GLenum format, |
139 gfx::Size size) { | 139 gfx::Size size, |
| 140 ResourceProvider::TextureType type) { |
140 CHECK(image_rect.Contains(source_rect)); | 141 CHECK(image_rect.Contains(source_rect)); |
141 | 142 |
142 bool is_full_upload = dest_offset.IsZero() && source_rect.size() == size; | 143 bool is_full_upload = dest_offset.IsZero() && source_rect.size() == size; |
143 | 144 |
144 if (is_full_upload) | 145 if (is_full_upload) |
145 BeginQuery(); | 146 BeginQuery(); |
146 | 147 |
147 if (use_map_tex_sub_image_) { | 148 if (use_map_tex_sub_image_) { |
148 UploadWithMapTexSubImage( | 149 UploadWithMapTexSubImage( |
149 image, image_rect, source_rect, dest_offset, format); | 150 image, image_rect, source_rect, dest_offset, format, type); |
150 } else { | 151 } else { |
151 UploadWithTexSubImage(image, image_rect, source_rect, dest_offset, format); | 152 UploadWithTexSubImage( |
| 153 image, image_rect, source_rect, dest_offset, format, type); |
152 } | 154 } |
153 | 155 |
154 if (is_full_upload) | 156 if (is_full_upload) |
155 EndQuery(); | 157 EndQuery(); |
156 | 158 |
157 num_texture_uploads_since_last_flush_++; | 159 num_texture_uploads_since_last_flush_++; |
158 if (num_texture_uploads_since_last_flush_ >= kTextureUploadFlushPeriod) | 160 if (num_texture_uploads_since_last_flush_ >= kTextureUploadFlushPeriod) |
159 Flush(); | 161 Flush(); |
160 } | 162 } |
161 | 163 |
162 void TextureUploader::Flush() { | 164 void TextureUploader::Flush() { |
163 if (!num_texture_uploads_since_last_flush_) | 165 if (!num_texture_uploads_since_last_flush_) |
164 return; | 166 return; |
165 | 167 |
166 if (use_shallow_flush_) | 168 if (use_shallow_flush_) |
167 context_->shallowFlushCHROMIUM(); | 169 context_->shallowFlushCHROMIUM(); |
168 | 170 |
169 num_texture_uploads_since_last_flush_ = 0; | 171 num_texture_uploads_since_last_flush_ = 0; |
170 } | 172 } |
171 | 173 |
172 void TextureUploader::ReleaseCachedQueries() { | 174 void TextureUploader::ReleaseCachedQueries() { |
173 ProcessQueries(); | 175 ProcessQueries(); |
174 available_queries_.clear(); | 176 available_queries_.clear(); |
175 } | 177 } |
176 | 178 |
177 void TextureUploader::UploadWithTexSubImage(const uint8* image, | 179 void TextureUploader::UploadWithTexSubImage( |
178 gfx::Rect image_rect, | 180 const uint8* image, |
179 gfx::Rect source_rect, | 181 gfx::Rect image_rect, |
180 gfx::Vector2d dest_offset, | 182 gfx::Rect source_rect, |
181 GLenum format) { | 183 gfx::Vector2d dest_offset, |
| 184 GLenum format, |
| 185 ResourceProvider::TextureType type) { |
182 // Instrumentation to debug issue 156107 | 186 // Instrumentation to debug issue 156107 |
183 int source_rect_x = source_rect.x(); | 187 int source_rect_x = source_rect.x(); |
184 int source_rect_y = source_rect.y(); | 188 int source_rect_y = source_rect.y(); |
185 int source_rect_width = source_rect.width(); | 189 int source_rect_width = source_rect.width(); |
186 int source_rect_height = source_rect.height(); | 190 int source_rect_height = source_rect.height(); |
187 int image_rect_x = image_rect.x(); | 191 int image_rect_x = image_rect.x(); |
188 int image_rect_y = image_rect.y(); | 192 int image_rect_y = image_rect.y(); |
189 int image_rect_width = image_rect.width(); | 193 int image_rect_width = image_rect.width(); |
190 int image_rect_height = image_rect.height(); | 194 int image_rect_height = image_rect.height(); |
191 int dest_offset_x = dest_offset.x(); | 195 int dest_offset_x = dest_offset.x(); |
192 int dest_offset_y = dest_offset.y(); | 196 int dest_offset_y = dest_offset.y(); |
193 base::debug::Alias(&image); | 197 base::debug::Alias(&image); |
194 base::debug::Alias(&source_rect_x); | 198 base::debug::Alias(&source_rect_x); |
195 base::debug::Alias(&source_rect_y); | 199 base::debug::Alias(&source_rect_y); |
196 base::debug::Alias(&source_rect_width); | 200 base::debug::Alias(&source_rect_width); |
197 base::debug::Alias(&source_rect_height); | 201 base::debug::Alias(&source_rect_height); |
198 base::debug::Alias(&image_rect_x); | 202 base::debug::Alias(&image_rect_x); |
199 base::debug::Alias(&image_rect_y); | 203 base::debug::Alias(&image_rect_y); |
200 base::debug::Alias(&image_rect_width); | 204 base::debug::Alias(&image_rect_width); |
201 base::debug::Alias(&image_rect_height); | 205 base::debug::Alias(&image_rect_height); |
202 base::debug::Alias(&dest_offset_x); | 206 base::debug::Alias(&dest_offset_x); |
203 base::debug::Alias(&dest_offset_y); | 207 base::debug::Alias(&dest_offset_y); |
204 TRACE_EVENT0("cc", "TextureUploader::UploadWithTexSubImage"); | 208 TRACE_EVENT0("cc", "TextureUploader::UploadWithTexSubImage"); |
205 | 209 |
206 // Offset from image-rect to source-rect. | 210 // Offset from image-rect to source-rect. |
207 gfx::Vector2d offset(source_rect.origin() - image_rect.origin()); | 211 gfx::Vector2d offset(source_rect.origin() - image_rect.origin()); |
208 | 212 |
209 const uint8* pixel_source; | 213 const uint8* pixel_source; |
210 unsigned int bytes_per_pixel = Resource::BytesPerPixel(format); | 214 unsigned int bytes_per_pixel = |
211 // Use 4-byte row alignment (OpenGL default) for upload performance. | 215 ResourceProvider::BytesPerPixel(format, type); |
212 // Assuming that GL_UNPACK_ALIGNMENT has not changed from default. | 216 DCHECK_EQ(0, source_rect.width() % 2); |
213 unsigned int upload_image_stride = | 217 unsigned int upload_image_stride = |
214 RoundUp(bytes_per_pixel * source_rect.width(), 4u); | 218 RoundUp(bytes_per_pixel * source_rect.width(), bytes_per_pixel); |
215 | 219 |
216 if (upload_image_stride == image_rect.width() * bytes_per_pixel && | 220 if (upload_image_stride == image_rect.width() * bytes_per_pixel && |
217 !offset.x()) { | 221 !offset.x()) { |
218 pixel_source = &image[image_rect.width() * bytes_per_pixel * offset.y()]; | 222 pixel_source = &image[image_rect.width() * bytes_per_pixel * offset.y()]; |
219 } else { | 223 } else { |
220 size_t needed_size = upload_image_stride * source_rect.height(); | 224 size_t needed_size = upload_image_stride * source_rect.height(); |
221 if (sub_image_size_ < needed_size) { | 225 if (sub_image_size_ < needed_size) { |
222 sub_image_.reset(new uint8[needed_size]); | 226 sub_image_.reset(new uint8[needed_size]); |
223 sub_image_size_ = needed_size; | 227 sub_image_size_ = needed_size; |
224 } | 228 } |
225 // Strides not equal, so do a row-by-row memcpy from the | 229 // Strides not equal, so do a row-by-row memcpy from the |
226 // paint results into a temp buffer for uploading. | 230 // paint results into a temp buffer for uploading. |
227 for (int row = 0; row < source_rect.height(); ++row) | 231 for (int row = 0; row < source_rect.height(); ++row) |
228 memcpy(&sub_image_[upload_image_stride * row], | 232 memcpy(&sub_image_[upload_image_stride * row], |
229 &image[bytes_per_pixel * | 233 &image[bytes_per_pixel * |
230 (offset.x() + (offset.y() + row) * image_rect.width())], | 234 (offset.x() + (offset.y() + row) * image_rect.width())], |
231 source_rect.width() * bytes_per_pixel); | 235 source_rect.width() * bytes_per_pixel); |
232 | 236 |
233 pixel_source = &sub_image_[0]; | 237 pixel_source = &sub_image_[0]; |
234 } | 238 } |
235 | 239 |
236 context_->texSubImage2D(GL_TEXTURE_2D, | 240 context_->texSubImage2D(GL_TEXTURE_2D, |
237 0, | 241 0, |
238 dest_offset.x(), | 242 dest_offset.x(), |
239 dest_offset.y(), | 243 dest_offset.y(), |
240 source_rect.width(), | 244 source_rect.width(), |
241 source_rect.height(), | 245 source_rect.height(), |
242 format, | 246 format, |
243 GL_UNSIGNED_BYTE, | 247 ResourceProvider::GetTextureDataType(type), |
244 pixel_source); | 248 pixel_source); |
245 } | 249 } |
246 | 250 |
247 void TextureUploader::UploadWithMapTexSubImage(const uint8* image, | 251 void TextureUploader::UploadWithMapTexSubImage( |
248 gfx::Rect image_rect, | 252 const uint8* image, |
249 gfx::Rect source_rect, | 253 gfx::Rect image_rect, |
250 gfx::Vector2d dest_offset, | 254 gfx::Rect source_rect, |
251 GLenum format) { | 255 gfx::Vector2d dest_offset, |
| 256 GLenum format, |
| 257 ResourceProvider::TextureType type) { |
252 // Instrumentation to debug issue 156107 | 258 // Instrumentation to debug issue 156107 |
253 int source_rect_x = source_rect.x(); | 259 int source_rect_x = source_rect.x(); |
254 int source_rect_y = source_rect.y(); | 260 int source_rect_y = source_rect.y(); |
255 int source_rect_width = source_rect.width(); | 261 int source_rect_width = source_rect.width(); |
256 int source_rect_height = source_rect.height(); | 262 int source_rect_height = source_rect.height(); |
257 int image_rect_x = image_rect.x(); | 263 int image_rect_x = image_rect.x(); |
258 int image_rect_y = image_rect.y(); | 264 int image_rect_y = image_rect.y(); |
259 int image_rect_width = image_rect.width(); | 265 int image_rect_width = image_rect.width(); |
260 int image_rect_height = image_rect.height(); | 266 int image_rect_height = image_rect.height(); |
261 int dest_offset_x = dest_offset.x(); | 267 int dest_offset_x = dest_offset.x(); |
262 int dest_offset_y = dest_offset.y(); | 268 int dest_offset_y = dest_offset.y(); |
263 base::debug::Alias(&image); | 269 base::debug::Alias(&image); |
264 base::debug::Alias(&source_rect_x); | 270 base::debug::Alias(&source_rect_x); |
265 base::debug::Alias(&source_rect_y); | 271 base::debug::Alias(&source_rect_y); |
266 base::debug::Alias(&source_rect_width); | 272 base::debug::Alias(&source_rect_width); |
267 base::debug::Alias(&source_rect_height); | 273 base::debug::Alias(&source_rect_height); |
268 base::debug::Alias(&image_rect_x); | 274 base::debug::Alias(&image_rect_x); |
269 base::debug::Alias(&image_rect_y); | 275 base::debug::Alias(&image_rect_y); |
270 base::debug::Alias(&image_rect_width); | 276 base::debug::Alias(&image_rect_width); |
271 base::debug::Alias(&image_rect_height); | 277 base::debug::Alias(&image_rect_height); |
272 base::debug::Alias(&dest_offset_x); | 278 base::debug::Alias(&dest_offset_x); |
273 base::debug::Alias(&dest_offset_y); | 279 base::debug::Alias(&dest_offset_y); |
274 | 280 |
275 TRACE_EVENT0("cc", "TextureUploader::UploadWithMapTexSubImage"); | 281 TRACE_EVENT0("cc", "TextureUploader::UploadWithMapTexSubImage"); |
276 | 282 |
277 // Offset from image-rect to source-rect. | 283 // Offset from image-rect to source-rect. |
278 gfx::Vector2d offset(source_rect.origin() - image_rect.origin()); | 284 gfx::Vector2d offset(source_rect.origin() - image_rect.origin()); |
279 | 285 |
280 unsigned int bytes_per_pixel = Resource::BytesPerPixel(format); | 286 unsigned int bytes_per_pixel = |
281 // Use 4-byte row alignment (OpenGL default) for upload performance. | 287 ResourceProvider::BytesPerPixel(format, type); |
282 // Assuming that GL_UNPACK_ALIGNMENT has not changed from default. | 288 DCHECK_EQ(0, source_rect.width() % 2); |
283 unsigned int upload_image_stride = | 289 unsigned int upload_image_stride = |
284 RoundUp(bytes_per_pixel * source_rect.width(), 4u); | 290 RoundUp(bytes_per_pixel * source_rect.width(), bytes_per_pixel); |
285 | 291 |
286 // Upload tile data via a mapped transfer buffer | |
287 uint8* pixel_dest = static_cast<uint8*>( | 292 uint8* pixel_dest = static_cast<uint8*>( |
288 context_->mapTexSubImage2DCHROMIUM(GL_TEXTURE_2D, | 293 context_->mapTexSubImage2DCHROMIUM(GL_TEXTURE_2D, |
289 0, | 294 0, |
290 dest_offset.x(), | 295 dest_offset.x(), |
291 dest_offset.y(), | 296 dest_offset.y(), |
292 source_rect.width(), | 297 source_rect.width(), |
293 source_rect.height(), | 298 source_rect.height(), |
294 format, | 299 format, |
295 GL_UNSIGNED_BYTE, | 300 ResourceProvider::GetTextureDataType( |
| 301 type), |
296 GL_WRITE_ONLY)); | 302 GL_WRITE_ONLY)); |
297 | 303 |
298 if (!pixel_dest) { | 304 if (!pixel_dest) { |
299 UploadWithTexSubImage(image, image_rect, source_rect, dest_offset, format); | 305 UploadWithTexSubImage( |
| 306 image, image_rect, source_rect, dest_offset, format, type); |
300 return; | 307 return; |
301 } | 308 } |
302 | 309 |
303 if (upload_image_stride == image_rect.width() * bytes_per_pixel && | 310 if (upload_image_stride == image_rect.width() * bytes_per_pixel && |
304 !offset.x()) { | 311 !offset.x()) { |
305 memcpy(pixel_dest, | 312 memcpy(pixel_dest, |
306 &image[image_rect.width() * bytes_per_pixel * offset.y()], | 313 &image[image_rect.width() * bytes_per_pixel * offset.y()], |
307 source_rect.height() * image_rect.width() * bytes_per_pixel); | 314 source_rect.height() * image_rect.width() * bytes_per_pixel); |
308 } else { | 315 } else { |
309 // Strides not equal, so do a row-by-row memcpy from the | 316 // Strides not equal, so do a row-by-row memcpy from the |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 textures_per_second_history_.erase(textures_per_second_history_.begin()); | 348 textures_per_second_history_.erase(textures_per_second_history_.begin()); |
342 textures_per_second_history_.erase(--textures_per_second_history_.end()); | 349 textures_per_second_history_.erase(--textures_per_second_history_.end()); |
343 } | 350 } |
344 textures_per_second_history_.insert(textures_per_second); | 351 textures_per_second_history_.insert(textures_per_second); |
345 | 352 |
346 available_queries_.push_back(pending_queries_.take_front()); | 353 available_queries_.push_back(pending_queries_.take_front()); |
347 } | 354 } |
348 } | 355 } |
349 | 356 |
350 } // namespace cc | 357 } // namespace cc |
OLD | NEW |