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