| 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 | 6 |
| 7 #include "CCTextureUpdateController.h" | 7 #include "CCTextureUpdateController.h" |
| 8 | 8 |
| 9 #include "GraphicsContext3D.h" | 9 #include "GraphicsContext3D.h" |
| 10 #include "TextureCopier.h" | 10 #include "TextureCopier.h" |
| 11 #include "TextureUploader.h" | 11 #include "TextureUploader.h" |
| 12 #include "TraceEvent.h" | 12 #include "TraceEvent.h" |
| 13 #include <limits> | 13 #include <limits> |
| 14 #include <wtf/CurrentTime.h> | 14 #include <wtf/CurrentTime.h> |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // Number of partial updates we allow. | 18 // Number of partial updates we allow. |
| 19 static const size_t partialTextureUpdatesMax = 12; | 19 static const int partialTextureUpdatesMax = 12; |
| 20 | 20 |
| 21 // Measured in seconds. | 21 // Measured in seconds. |
| 22 static const double textureUpdateTickRate = 0.004; | 22 static const double textureUpdateTickRate = 0.004; |
| 23 | 23 |
| 24 // Measured in seconds. | 24 // Measured in seconds. |
| 25 static const double uploaderBusyTickRate = 0.001; | 25 static const double uploaderBusyTickRate = 0.001; |
| 26 | 26 |
| 27 // Flush interval when performing texture uploads. | 27 // Flush interval when performing texture uploads. |
| 28 static const int textureUploadFlushPeriod = 4; | 28 static const int textureUploadFlushPeriod = 4; |
| 29 | 29 |
| 30 // Number of blocking update intervals to allow. | 30 // Number of blocking update intervals to allow. |
| 31 static const size_t maxBlockingUpdateIntervals = 4; | 31 static const double maxBlockingUpdateIntervals = 4; |
| 32 |
| 33 // We will upload this many intervals worth of textures initially |
| 34 // then attempt to keep at least this many intervals queued. |
| 35 static const double targetBlockingUpdateIntervals = 2; |
| 32 | 36 |
| 33 } // anonymous namespace | 37 } // anonymous namespace |
| 34 | 38 |
| 35 namespace cc { | 39 namespace cc { |
| 36 | 40 |
| 37 size_t CCTextureUpdateController::maxPartialTextureUpdates() | 41 int CCTextureUpdateController::maxPartialTextureUpdates() |
| 38 { | 42 { |
| 39 return partialTextureUpdatesMax; | 43 return partialTextureUpdatesMax; |
| 40 } | 44 } |
| 41 | 45 |
| 42 size_t CCTextureUpdateController::maxFullUpdatesPerTick(TextureUploader* uploade
r) | |
| 43 { | |
| 44 double texturesPerSecond = uploader->estimatedTexturesPerSecond(); | |
| 45 size_t texturesPerTick = floor(textureUpdateTickRate * texturesPerSecond); | |
| 46 return texturesPerTick ? texturesPerTick : 1; | |
| 47 } | |
| 48 | |
| 49 CCTextureUpdateController::CCTextureUpdateController(CCTextureUpdateControllerCl
ient* client, CCThread* thread, PassOwnPtr<CCTextureUpdateQueue> queue, CCResour
ceProvider* resourceProvider, TextureUploader* uploader) | 46 CCTextureUpdateController::CCTextureUpdateController(CCTextureUpdateControllerCl
ient* client, CCThread* thread, PassOwnPtr<CCTextureUpdateQueue> queue, CCResour
ceProvider* resourceProvider, TextureUploader* uploader) |
| 50 : m_client(client) | 47 : m_client(client) |
| 51 , m_timer(adoptPtr(new CCTimer(thread, this))) | 48 , m_timer(adoptPtr(new CCTimer(thread, this))) |
| 52 , m_queue(queue) | 49 , m_queue(queue) |
| 53 , m_resourceProvider(resourceProvider) | 50 , m_resourceProvider(resourceProvider) |
| 54 , m_uploader(uploader) | 51 , m_uploader(uploader) |
| 55 , m_textureUpdatesPerTick(maxFullUpdatesPerTick(uploader)) | 52 , m_estimatedTexturesPerSecond(uploader->estimatedTexturesPerSecond()) |
| 56 , m_firstUpdateAttempt(true) | 53 , m_firstUpdateAttempt(true) |
| 57 { | 54 { |
| 58 } | 55 } |
| 59 | 56 |
| 60 CCTextureUpdateController::~CCTextureUpdateController() | 57 CCTextureUpdateController::~CCTextureUpdateController() |
| 61 { | 58 { |
| 62 } | 59 } |
| 63 | 60 |
| 64 void CCTextureUpdateController::performMoreUpdates( | 61 void CCTextureUpdateController::performMoreUpdates( |
| 65 base::TimeTicks timeLimit) | 62 base::TimeTicks timeLimit) |
| 66 { | 63 { |
| 67 m_timeLimit = timeLimit; | 64 m_timeLimit = timeLimit; |
| 68 | 65 |
| 69 // Update already in progress. | 66 // Update already in progress. |
| 70 if (m_timer->isActive()) | 67 if (m_timer->isActive()) |
| 71 return; | 68 return; |
| 72 | 69 |
| 73 // Call updateMoreTexturesNow() directly unless it's the first update | 70 // Call updateMoreTexturesNow() directly unless it's the first update |
| 74 // attempt. This ensures that we empty the update queue in a finite | 71 // attempt. This ensures that we empty the update queue in a finite |
| 75 // amount of time. | 72 // amount of time. |
| 76 if (m_firstUpdateAttempt) { | 73 if (m_firstUpdateAttempt) { |
| 77 // Post a 0-delay task when no updates were left. When it runs, | 74 // Post a 0-delay task when no updates were left. When it runs, |
| 78 // readyToFinalizeTextureUpdates() will be called. | 75 // readyToFinalizeTextureUpdates() will be called. |
| 79 if (!updateMoreTexturesIfEnoughTimeRemaining()) | 76 if (!updateMoreTexturesIfEnoughTimeRemaining()) |
| 80 m_timer->startOneShot(0); | 77 m_timer->startOneShot(0); |
| 81 | 78 |
| 82 m_firstUpdateAttempt = false; | 79 m_firstUpdateAttempt = false; |
| 83 } else | 80 } else |
| 84 updateMoreTexturesNow(); | 81 updateMoreTexturesNow(updateMoreTexturesSize()); |
| 85 } | 82 } |
| 86 | 83 |
| 87 void CCTextureUpdateController::discardUploadsToEvictedResources() | 84 void CCTextureUpdateController::discardUploadsToEvictedResources() |
| 88 { | 85 { |
| 89 m_queue->clearUploadsToEvictedResources(); | 86 m_queue->clearUploadsToEvictedResources(); |
| 90 } | 87 } |
| 91 | 88 |
| 92 void CCTextureUpdateController::finalize() | 89 void CCTextureUpdateController::finalize() |
| 93 { | 90 { |
| 94 size_t uploadCount = 0; | 91 int uploadCount = 0; |
| 95 while (m_queue->fullUploadSize()) { | 92 while (m_queue->fullUploadSize()) { |
| 96 if (!(uploadCount % textureUploadFlushPeriod) && uploadCount) | 93 if (!(uploadCount % textureUploadFlushPeriod) && uploadCount) |
| 97 m_resourceProvider->shallowFlushIfSupported(); | 94 m_resourceProvider->shallowFlushIfSupported(); |
| 98 | 95 |
| 99 m_uploader->uploadTexture( | 96 m_uploader->uploadTexture( |
| 100 m_resourceProvider, m_queue->takeFirstFullUpload()); | 97 m_resourceProvider, m_queue->takeFirstFullUpload()); |
| 101 uploadCount++; | 98 uploadCount++; |
| 102 } | 99 } |
| 103 | 100 |
| 104 while (m_queue->partialUploadSize()) { | 101 while (m_queue->partialUploadSize()) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 135 base::TimeTicks CCTextureUpdateController::now() const | 132 base::TimeTicks CCTextureUpdateController::now() const |
| 136 { | 133 { |
| 137 return base::TimeTicks::Now(); | 134 return base::TimeTicks::Now(); |
| 138 } | 135 } |
| 139 | 136 |
| 140 base::TimeDelta CCTextureUpdateController::updateMoreTexturesTime() const | 137 base::TimeDelta CCTextureUpdateController::updateMoreTexturesTime() const |
| 141 { | 138 { |
| 142 return base::TimeDelta::FromMilliseconds(textureUpdateTickRate * 1000); | 139 return base::TimeDelta::FromMilliseconds(textureUpdateTickRate * 1000); |
| 143 } | 140 } |
| 144 | 141 |
| 145 size_t CCTextureUpdateController::updateMoreTexturesSize() const | 142 int CCTextureUpdateController::updateMoreTexturesSize() const |
| 146 { | 143 { |
| 147 return m_textureUpdatesPerTick; | 144 return m_estimatedTexturesPerSecond * textureUpdateTickRate; |
| 148 } | 145 } |
| 149 | 146 |
| 150 size_t CCTextureUpdateController::maxBlockingUpdates() const | 147 int CCTextureUpdateController::maxBlockingUploads() const |
| 151 { | 148 { |
| 152 return updateMoreTexturesSize() * maxBlockingUpdateIntervals; | 149 return updateMoreTexturesSize() * maxBlockingUpdateIntervals; |
| 153 } | 150 } |
| 154 | 151 |
| 152 int CCTextureUpdateController::targetBlockingUploads() const |
| 153 { |
| 154 return updateMoreTexturesSize() * targetBlockingUpdateIntervals; |
| 155 } |
| 156 |
| 157 int CCTextureUpdateController::maxTexturesBeforeTimeLimit() |
| 158 { |
| 159 if (m_timeLimit.is_null()) |
| 160 return std::numeric_limits<int>::max(); |
| 161 |
| 162 return m_estimatedTexturesPerSecond * |
| 163 (m_timeLimit - this->now()).InSecondsF(); |
| 164 } |
| 165 |
| 155 bool CCTextureUpdateController::updateMoreTexturesIfEnoughTimeRemaining() | 166 bool CCTextureUpdateController::updateMoreTexturesIfEnoughTimeRemaining() |
| 156 { | 167 { |
| 168 if (!m_queue->fullUploadSize()) |
| 169 return false; |
| 170 |
| 157 // Blocking uploads will increase when we're too aggressive in our upload | 171 // Blocking uploads will increase when we're too aggressive in our upload |
| 158 // time estimate. We use a different timeout here to prevent unnecessary | 172 // time estimate. We use a different timeout here to prevent unnecessary |
| 159 // amounts of idle time when blocking uploads have reached the max. | 173 // amounts of idle time when blocking uploads have reached the max. |
| 160 if (m_uploader->numBlockingUploads() >= maxBlockingUpdates()) { | 174 int numBlockingUploads = m_uploader->numBlockingUploads(); |
| 175 if (numBlockingUploads >= maxBlockingUploads()) { |
| 161 m_timer->startOneShot(uploaderBusyTickRate); | 176 m_timer->startOneShot(uploaderBusyTickRate); |
| 162 return true; | 177 return true; |
| 163 } | 178 } |
| 164 | 179 |
| 165 if (!m_queue->fullUploadSize()) | 180 int maxTextures = 0; |
| 166 return false; | 181 bool highLatencyMode = numBlockingUploads >= targetBlockingUploads(); |
| 182 if (highLatencyMode) { |
| 183 // If we are in a high latency mode, we do not have enough feedback |
| 184 // so we blindly upload the estimated number of textures. |
| 185 maxTextures = updateMoreTexturesSize(); |
| 186 } else { |
| 187 // If we are in a low latency mode, we can throttle our uploads |
| 188 // to a well defined target. |
| 189 maxTextures = targetBlockingUploads() - numBlockingUploads; |
| 190 } |
| 167 | 191 |
| 168 bool hasTimeRemaining = m_timeLimit.is_null() || | 192 // Make sure our texture uploads don't block drawing. |
| 169 this->now() < m_timeLimit - updateMoreTexturesTime(); | 193 maxTextures = std::min(maxTextures, maxTexturesBeforeTimeLimit()); |
| 170 if (hasTimeRemaining) | |
| 171 updateMoreTexturesNow(); | |
| 172 | 194 |
| 173 return true; | 195 updateMoreTexturesNow(maxTextures); |
| 196 return m_queue->fullUploadSize(); |
| 174 } | 197 } |
| 175 | 198 |
| 176 void CCTextureUpdateController::updateMoreTexturesNow() | 199 void CCTextureUpdateController::updateMoreTexturesNow(int maxTextures) |
| 177 { | 200 { |
| 178 size_t uploads = std::min( | 201 int uploads = std::min((int)m_queue->fullUploadSize(), maxTextures); |
| 179 m_queue->fullUploadSize(), updateMoreTexturesSize()); | 202 ASSERT(uploads >= 0); |
| 180 m_timer->startOneShot( | |
| 181 updateMoreTexturesTime().InSecondsF() / updateMoreTexturesSize() * | |
| 182 uploads); | |
| 183 | 203 |
| 184 if (!uploads) | 204 if (uploads <= 0) { |
| 205 m_timer->startOneShot(uploaderBusyTickRate); |
| 185 return; | 206 return; |
| 207 } else if (uploads == m_queue->fullUploadSize()) { |
| 208 m_timer->startOneShot( |
| 209 updateMoreTexturesTime().InSecondsF() / updateMoreTexturesSize() * |
| 210 uploads); |
| 211 } else { |
| 212 m_timer->startOneShot(updateMoreTexturesTime().InSecondsF()); |
| 213 } |
| 186 | 214 |
| 187 size_t uploadCount = 0; | 215 int uploadCount = 0; |
| 188 while (m_queue->fullUploadSize() && uploadCount < uploads) { | 216 while (m_queue->fullUploadSize() && uploadCount < uploads) { |
| 189 if (!(uploadCount % textureUploadFlushPeriod) && uploadCount) | 217 if (!(uploadCount % textureUploadFlushPeriod) && uploadCount) |
| 190 m_resourceProvider->shallowFlushIfSupported(); | 218 m_resourceProvider->shallowFlushIfSupported(); |
| 191 m_uploader->uploadTexture(m_resourceProvider, m_queue->takeFirstFullUplo
ad()); | 219 m_uploader->uploadTexture(m_resourceProvider, m_queue->takeFirstFullUplo
ad()); |
| 192 uploadCount++; | 220 uploadCount++; |
| 193 } | 221 } |
| 194 m_resourceProvider->shallowFlushIfSupported(); | 222 m_resourceProvider->shallowFlushIfSupported(); |
| 195 } | 223 } |
| 196 | 224 |
| 197 } | 225 } |
| OLD | NEW |