| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "config.h" | |
| 6 #include "ThrottledTextureUploader.h" | |
| 7 | |
| 8 #include "Extensions3DChromium.h" | |
| 9 #include "TraceEvent.h" | |
| 10 #include <algorithm> | |
| 11 #include <public/Platform.h> | |
| 12 #include <public/WebGraphicsContext3D.h> | |
| 13 #include <vector> | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // How many previous uploads to use when predicting future throughput. | |
| 18 static const size_t uploadHistorySize = 100; | |
| 19 | |
| 20 // Global estimated number of textures per second to maintain estimates across | |
| 21 // subsequent instances of ThrottledTextureUploader. | |
| 22 // More than one thread will not access this variable, so we do not need to sync
hronize access. | |
| 23 static double estimatedTexturesPerSecondGlobal = 48.0 * 60.0; | |
| 24 | |
| 25 } // anonymous namespace | |
| 26 | |
| 27 namespace cc { | |
| 28 | |
| 29 ThrottledTextureUploader::Query::Query(WebKit::WebGraphicsContext3D* context) | |
| 30 : m_context(context) | |
| 31 , m_queryId(0) | |
| 32 , m_value(0) | |
| 33 , m_hasValue(false) | |
| 34 , m_isNonBlocking(false) | |
| 35 { | |
| 36 m_queryId = m_context->createQueryEXT(); | |
| 37 } | |
| 38 | |
| 39 ThrottledTextureUploader::Query::~Query() | |
| 40 { | |
| 41 m_context->deleteQueryEXT(m_queryId); | |
| 42 } | |
| 43 | |
| 44 void ThrottledTextureUploader::Query::begin() | |
| 45 { | |
| 46 m_hasValue = false; | |
| 47 m_isNonBlocking = false; | |
| 48 m_context->beginQueryEXT(Extensions3DChromium::COMMANDS_ISSUED_CHROMIUM, m_q
ueryId); | |
| 49 } | |
| 50 | |
| 51 void ThrottledTextureUploader::Query::end() | |
| 52 { | |
| 53 m_context->endQueryEXT(Extensions3DChromium::COMMANDS_ISSUED_CHROMIUM); | |
| 54 } | |
| 55 | |
| 56 bool ThrottledTextureUploader::Query::isPending() | |
| 57 { | |
| 58 unsigned available = 1; | |
| 59 m_context->getQueryObjectuivEXT(m_queryId, Extensions3DChromium::QUERY_RESUL
T_AVAILABLE_EXT, &available); | |
| 60 return !available; | |
| 61 } | |
| 62 | |
| 63 void ThrottledTextureUploader::Query::wait() | |
| 64 { | |
| 65 value(); | |
| 66 return; | |
| 67 } | |
| 68 | |
| 69 unsigned ThrottledTextureUploader::Query::value() | |
| 70 { | |
| 71 if (!m_hasValue) { | |
| 72 m_context->getQueryObjectuivEXT(m_queryId, Extensions3DChromium::QUERY_R
ESULT_EXT, &m_value); | |
| 73 m_hasValue = true; | |
| 74 } | |
| 75 return m_value; | |
| 76 } | |
| 77 | |
| 78 void ThrottledTextureUploader::Query::markAsNonBlocking() | |
| 79 { | |
| 80 m_isNonBlocking = true; | |
| 81 } | |
| 82 | |
| 83 bool ThrottledTextureUploader::Query::isNonBlocking() | |
| 84 { | |
| 85 return m_isNonBlocking; | |
| 86 } | |
| 87 | |
| 88 ThrottledTextureUploader::ThrottledTextureUploader(WebKit::WebGraphicsContext3D*
context) | |
| 89 : m_context(context) | |
| 90 , m_texturesPerSecondHistory(uploadHistorySize, estimatedTexturesPerSecondGl
obal) | |
| 91 , m_numBlockingTextureUploads(0) | |
| 92 { | |
| 93 } | |
| 94 | |
| 95 ThrottledTextureUploader::~ThrottledTextureUploader() | |
| 96 { | |
| 97 } | |
| 98 | |
| 99 size_t ThrottledTextureUploader::numBlockingUploads() | |
| 100 { | |
| 101 processQueries(); | |
| 102 return m_numBlockingTextureUploads; | |
| 103 } | |
| 104 | |
| 105 void ThrottledTextureUploader::markPendingUploadsAsNonBlocking() | |
| 106 { | |
| 107 for (Deque<OwnPtr<Query> >::iterator it = m_pendingQueries.begin(); | |
| 108 it != m_pendingQueries.end(); ++it) { | |
| 109 if (it->get()->isNonBlocking()) | |
| 110 continue; | |
| 111 | |
| 112 m_numBlockingTextureUploads--; | |
| 113 it->get()->markAsNonBlocking(); | |
| 114 } | |
| 115 | |
| 116 ASSERT(!m_numBlockingTextureUploads); | |
| 117 } | |
| 118 | |
| 119 double ThrottledTextureUploader::estimatedTexturesPerSecond() | |
| 120 { | |
| 121 processQueries(); | |
| 122 | |
| 123 // The history should never be empty because we initialize all elements with
an estimate. | |
| 124 ASSERT(m_texturesPerSecondHistory.size() == uploadHistorySize); | |
| 125 | |
| 126 // Sort the history and use the median as our estimate. | |
| 127 std::vector<double> sortedHistory(m_texturesPerSecondHistory.begin(), | |
| 128 m_texturesPerSecondHistory.end()); | |
| 129 std::sort(sortedHistory.begin(), sortedHistory.end()); | |
| 130 | |
| 131 estimatedTexturesPerSecondGlobal = sortedHistory[sortedHistory.size() * 2 /
3]; | |
| 132 TRACE_COUNTER1("cc", "estimatedTexturesPerSecond", estimatedTexturesPerSecon
dGlobal); | |
| 133 return estimatedTexturesPerSecondGlobal; | |
| 134 } | |
| 135 | |
| 136 void ThrottledTextureUploader::beginQuery() | |
| 137 { | |
| 138 if (m_availableQueries.isEmpty()) | |
| 139 m_availableQueries.append(Query::create(m_context)); | |
| 140 | |
| 141 m_availableQueries.first()->begin(); | |
| 142 } | |
| 143 | |
| 144 void ThrottledTextureUploader::endQuery() | |
| 145 { | |
| 146 m_availableQueries.first()->end(); | |
| 147 m_pendingQueries.append(m_availableQueries.takeFirst()); | |
| 148 m_numBlockingTextureUploads++; | |
| 149 } | |
| 150 | |
| 151 void ThrottledTextureUploader::uploadTexture(CCResourceProvider* resourceProvide
r, Parameters upload) | |
| 152 { | |
| 153 bool isFullUpload = upload.destOffset.isZero() && | |
| 154 upload.sourceRect.size() == upload.texture->texture()->s
ize(); | |
| 155 | |
| 156 if (isFullUpload) | |
| 157 beginQuery(); | |
| 158 | |
| 159 upload.texture->updateRect(resourceProvider, upload.sourceRect, upload.destO
ffset); | |
| 160 | |
| 161 if (isFullUpload) | |
| 162 endQuery(); | |
| 163 } | |
| 164 | |
| 165 void ThrottledTextureUploader::processQueries() | |
| 166 { | |
| 167 while (!m_pendingQueries.isEmpty()) { | |
| 168 if (m_pendingQueries.first()->isPending()) | |
| 169 break; | |
| 170 | |
| 171 unsigned usElapsed = m_pendingQueries.first()->value(); | |
| 172 WebKit::Platform::current()->histogramCustomCounts("Renderer4.TextureGpu
UploadTimeUS", usElapsed, 0, 100000, 50); | |
| 173 | |
| 174 if (!m_pendingQueries.first()->isNonBlocking()) | |
| 175 m_numBlockingTextureUploads--; | |
| 176 | |
| 177 // Remove the oldest values from our history and insert the new one | |
| 178 double texturesPerSecond = 1.0 / (usElapsed * 1e-6); | |
| 179 m_texturesPerSecondHistory.pop_back(); | |
| 180 m_texturesPerSecondHistory.push_front(texturesPerSecond); | |
| 181 | |
| 182 m_availableQueries.append(m_pendingQueries.takeFirst()); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 } | |
| OLD | NEW |