Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / cc / resources / texture_uploader.cc
blob9526fcb9238bdf5f1d4591aab7184c544d0022f2
1 // Copyright 2014 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 #include "cc/resources/texture_uploader.h"
7 #include <algorithm>
8 #include <vector>
10 #include "base/debug/trace_event.h"
11 #include "base/metrics/histogram.h"
12 #include "cc/base/util.h"
13 #include "cc/resources/resource.h"
14 #include "gpu/GLES2/gl2extchromium.h"
15 #include "gpu/command_buffer/client/gles2_interface.h"
16 #include "third_party/khronos/GLES2/gl2.h"
17 #include "third_party/khronos/GLES2/gl2ext.h"
18 #include "ui/gfx/geometry/rect.h"
19 #include "ui/gfx/geometry/vector2d.h"
21 using gpu::gles2::GLES2Interface;
23 namespace {
25 // How many previous uploads to use when predicting future throughput.
26 static const size_t kUploadHistorySizeMax = 1000;
27 static const size_t kUploadHistorySizeInitial = 100;
29 // Global estimated number of textures per second to maintain estimates across
30 // subsequent instances of TextureUploader.
31 // More than one thread will not access this variable, so we do not need to
32 // synchronize access.
33 static const double kDefaultEstimatedTexturesPerSecond = 48.0 * 60.0;
35 // Flush interval when performing texture uploads.
36 static const size_t kTextureUploadFlushPeriod = 4;
38 } // anonymous namespace
40 namespace cc {
42 TextureUploader::Query::Query(GLES2Interface* gl)
43 : gl_(gl),
44 query_id_(0),
45 value_(0),
46 has_value_(false),
47 is_non_blocking_(false) {
48 gl_->GenQueriesEXT(1, &query_id_);
51 TextureUploader::Query::~Query() { gl_->DeleteQueriesEXT(1, &query_id_); }
53 void TextureUploader::Query::Begin() {
54 has_value_ = false;
55 is_non_blocking_ = false;
56 gl_->BeginQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM, query_id_);
59 void TextureUploader::Query::End() {
60 gl_->EndQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM);
63 bool TextureUploader::Query::IsPending() {
64 unsigned available = 1;
65 gl_->GetQueryObjectuivEXT(
66 query_id_, GL_QUERY_RESULT_AVAILABLE_EXT, &available);
67 return !available;
70 unsigned TextureUploader::Query::Value() {
71 if (!has_value_) {
72 gl_->GetQueryObjectuivEXT(query_id_, GL_QUERY_RESULT_EXT, &value_);
73 has_value_ = true;
75 return value_;
78 TextureUploader::TextureUploader(GLES2Interface* gl)
79 : gl_(gl),
80 num_blocking_texture_uploads_(0),
81 sub_image_size_(0),
82 num_texture_uploads_since_last_flush_(0) {
83 for (size_t i = kUploadHistorySizeInitial; i > 0; i--)
84 textures_per_second_history_.insert(kDefaultEstimatedTexturesPerSecond);
87 TextureUploader::~TextureUploader() {}
89 size_t TextureUploader::NumBlockingUploads() {
90 ProcessQueries();
91 return num_blocking_texture_uploads_;
94 void TextureUploader::MarkPendingUploadsAsNonBlocking() {
95 for (ScopedPtrDeque<Query>::iterator it = pending_queries_.begin();
96 it != pending_queries_.end();
97 ++it) {
98 if ((*it)->is_non_blocking())
99 continue;
101 num_blocking_texture_uploads_--;
102 (*it)->mark_as_non_blocking();
105 DCHECK(!num_blocking_texture_uploads_);
108 double TextureUploader::EstimatedTexturesPerSecond() {
109 ProcessQueries();
111 // Use the median as our estimate.
112 std::multiset<double>::iterator median = textures_per_second_history_.begin();
113 std::advance(median, textures_per_second_history_.size() / 2);
114 return *median;
117 void TextureUploader::BeginQuery() {
118 // Check to see if any of the pending queries are free before allocating a
119 // new one. If this is not done, queries may be allocated without bound.
120 // http://crbug.com/398072
121 if (available_queries_.empty())
122 ProcessQueries();
124 if (available_queries_.empty())
125 available_queries_.push_back(Query::Create(gl_));
127 available_queries_.front()->Begin();
130 void TextureUploader::EndQuery() {
131 available_queries_.front()->End();
132 pending_queries_.push_back(available_queries_.take_front());
133 num_blocking_texture_uploads_++;
136 void TextureUploader::Upload(const uint8* image,
137 const gfx::Rect& image_rect,
138 const gfx::Rect& source_rect,
139 gfx::Vector2d dest_offset,
140 ResourceFormat format,
141 const gfx::Size& size) {
142 CHECK(image_rect.Contains(source_rect));
144 bool is_full_upload = dest_offset.IsZero() && source_rect.size() == size;
146 if (is_full_upload)
147 BeginQuery();
149 if (format == ETC1) {
150 // ETC1 does not support subimage uploads.
151 DCHECK(is_full_upload);
152 UploadWithTexImageETC1(image, size);
153 } else {
154 UploadWithMapTexSubImage(
155 image, image_rect, source_rect, dest_offset, format);
158 if (is_full_upload)
159 EndQuery();
161 num_texture_uploads_since_last_flush_++;
162 if (num_texture_uploads_since_last_flush_ >= kTextureUploadFlushPeriod)
163 Flush();
166 void TextureUploader::Flush() {
167 if (!num_texture_uploads_since_last_flush_)
168 return;
170 gl_->ShallowFlushCHROMIUM();
172 num_texture_uploads_since_last_flush_ = 0;
175 void TextureUploader::ReleaseCachedQueries() {
176 ProcessQueries();
177 available_queries_.clear();
180 void TextureUploader::UploadWithTexSubImage(const uint8* image,
181 const gfx::Rect& image_rect,
182 const gfx::Rect& source_rect,
183 gfx::Vector2d dest_offset,
184 ResourceFormat format) {
185 TRACE_EVENT0("cc", "TextureUploader::UploadWithTexSubImage");
187 // Early-out if this is a no-op, and assert that |image| be valid if this is
188 // not a no-op.
189 if (source_rect.IsEmpty())
190 return;
191 DCHECK(image);
193 // Offset from image-rect to source-rect.
194 gfx::Vector2d offset(source_rect.origin() - image_rect.origin());
196 const uint8* pixel_source;
197 unsigned bytes_per_pixel = BitsPerPixel(format) / 8;
198 // Use 4-byte row alignment (OpenGL default) for upload performance.
199 // Assuming that GL_UNPACK_ALIGNMENT has not changed from default.
200 unsigned upload_image_stride =
201 RoundUp(bytes_per_pixel * source_rect.width(), 4u);
203 if (upload_image_stride == image_rect.width() * bytes_per_pixel &&
204 !offset.x()) {
205 pixel_source = &image[image_rect.width() * bytes_per_pixel * offset.y()];
206 } else {
207 size_t needed_size = upload_image_stride * source_rect.height();
208 if (sub_image_size_ < needed_size) {
209 sub_image_.reset(new uint8[needed_size]);
210 sub_image_size_ = needed_size;
212 // Strides not equal, so do a row-by-row memcpy from the
213 // paint results into a temp buffer for uploading.
214 for (int row = 0; row < source_rect.height(); ++row)
215 memcpy(&sub_image_[upload_image_stride * row],
216 &image[bytes_per_pixel *
217 (offset.x() + (offset.y() + row) * image_rect.width())],
218 source_rect.width() * bytes_per_pixel);
220 pixel_source = &sub_image_[0];
223 gl_->TexSubImage2D(GL_TEXTURE_2D,
225 dest_offset.x(),
226 dest_offset.y(),
227 source_rect.width(),
228 source_rect.height(),
229 GLDataFormat(format),
230 GLDataType(format),
231 pixel_source);
234 void TextureUploader::UploadWithMapTexSubImage(const uint8* image,
235 const gfx::Rect& image_rect,
236 const gfx::Rect& source_rect,
237 gfx::Vector2d dest_offset,
238 ResourceFormat format) {
239 TRACE_EVENT0("cc", "TextureUploader::UploadWithMapTexSubImage");
241 // Early-out if this is a no-op, and assert that |image| be valid if this is
242 // not a no-op.
243 if (source_rect.IsEmpty())
244 return;
245 DCHECK(image);
246 // Compressed textures have no implementation of mapTexSubImage.
247 DCHECK_NE(ETC1, format);
249 // Offset from image-rect to source-rect.
250 gfx::Vector2d offset(source_rect.origin() - image_rect.origin());
252 unsigned bytes_per_pixel = BitsPerPixel(format) / 8;
253 // Use 4-byte row alignment (OpenGL default) for upload performance.
254 // Assuming that GL_UNPACK_ALIGNMENT has not changed from default.
255 unsigned upload_image_stride =
256 RoundUp(bytes_per_pixel * source_rect.width(), 4u);
258 // Upload tile data via a mapped transfer buffer
259 uint8* pixel_dest =
260 static_cast<uint8*>(gl_->MapTexSubImage2DCHROMIUM(GL_TEXTURE_2D,
262 dest_offset.x(),
263 dest_offset.y(),
264 source_rect.width(),
265 source_rect.height(),
266 GLDataFormat(format),
267 GLDataType(format),
268 GL_WRITE_ONLY));
270 if (!pixel_dest) {
271 UploadWithTexSubImage(image, image_rect, source_rect, dest_offset, format);
272 return;
275 if (upload_image_stride == image_rect.width() * bytes_per_pixel &&
276 !offset.x()) {
277 memcpy(pixel_dest,
278 &image[image_rect.width() * bytes_per_pixel * offset.y()],
279 source_rect.height() * image_rect.width() * bytes_per_pixel);
280 } else {
281 // Strides not equal, so do a row-by-row memcpy from the
282 // paint results into the pixel_dest.
283 for (int row = 0; row < source_rect.height(); ++row) {
284 memcpy(&pixel_dest[upload_image_stride * row],
285 &image[bytes_per_pixel *
286 (offset.x() + (offset.y() + row) * image_rect.width())],
287 source_rect.width() * bytes_per_pixel);
291 gl_->UnmapTexSubImage2DCHROMIUM(pixel_dest);
294 void TextureUploader::UploadWithTexImageETC1(const uint8* image,
295 const gfx::Size& size) {
296 TRACE_EVENT0("cc", "TextureUploader::UploadWithTexImageETC1");
297 DCHECK_EQ(0, size.width() % 4);
298 DCHECK_EQ(0, size.height() % 4);
300 gl_->CompressedTexImage2D(GL_TEXTURE_2D,
302 GLInternalFormat(ETC1),
303 size.width(),
304 size.height(),
306 Resource::MemorySizeBytes(size, ETC1),
307 image);
310 void TextureUploader::ProcessQueries() {
311 while (!pending_queries_.empty()) {
312 if (pending_queries_.front()->IsPending())
313 break;
315 unsigned us_elapsed = pending_queries_.front()->Value();
316 UMA_HISTOGRAM_CUSTOM_COUNTS(
317 "Renderer4.TextureGpuUploadTimeUS", us_elapsed, 0, 100000, 50);
319 // Clamp the queries to saner values in case the queries fail.
320 us_elapsed = std::max(1u, us_elapsed);
321 us_elapsed = std::min(15000u, us_elapsed);
323 if (!pending_queries_.front()->is_non_blocking())
324 num_blocking_texture_uploads_--;
326 // Remove the min and max value from our history and insert the new one.
327 double textures_per_second = 1.0 / (us_elapsed * 1e-6);
328 if (textures_per_second_history_.size() >= kUploadHistorySizeMax) {
329 textures_per_second_history_.erase(textures_per_second_history_.begin());
330 textures_per_second_history_.erase(--textures_per_second_history_.end());
332 textures_per_second_history_.insert(textures_per_second);
334 available_queries_.push_back(pending_queries_.take_front());
338 } // namespace cc