| 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> | |
| 15 | 14 |
| 16 namespace { | 15 namespace { |
| 17 | 16 |
| 18 // Number of partial updates we allow. | 17 // Number of partial updates we allow. |
| 19 static const size_t partialTextureUpdatesMax = 12; | 18 static const size_t partialTextureUpdatesMax = 12; |
| 20 | 19 |
| 21 // Measured in seconds. | 20 // Measured in seconds. |
| 22 static const double textureUpdateTickRate = 0.004; | 21 static const double textureUpdateTickRate = 0.004; |
| 23 | 22 |
| 24 // Measured in seconds. | 23 // Measured in seconds. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 42 size_t texturesPerTick = floor(textureUpdateTickRate * texturesPerSecond); | 41 size_t texturesPerTick = floor(textureUpdateTickRate * texturesPerSecond); |
| 43 return texturesPerTick ? texturesPerTick : 1; | 42 return texturesPerTick ? texturesPerTick : 1; |
| 44 } | 43 } |
| 45 | 44 |
| 46 CCTextureUpdateController::CCTextureUpdateController(CCTextureUpdateControllerCl
ient* client, CCThread* thread, PassOwnPtr<CCTextureUpdateQueue> queue, CCResour
ceProvider* resourceProvider, TextureUploader* uploader) | 45 CCTextureUpdateController::CCTextureUpdateController(CCTextureUpdateControllerCl
ient* client, CCThread* thread, PassOwnPtr<CCTextureUpdateQueue> queue, CCResour
ceProvider* resourceProvider, TextureUploader* uploader) |
| 47 : m_client(client) | 46 : m_client(client) |
| 48 , m_timer(adoptPtr(new CCTimer(thread, this))) | 47 , m_timer(adoptPtr(new CCTimer(thread, this))) |
| 49 , m_queue(queue) | 48 , m_queue(queue) |
| 50 , m_resourceProvider(resourceProvider) | 49 , m_resourceProvider(resourceProvider) |
| 51 , m_uploader(uploader) | 50 , m_uploader(uploader) |
| 52 , m_monotonicTimeLimit(0) | |
| 53 , m_textureUpdatesPerTick(maxFullUpdatesPerTick(uploader)) | 51 , m_textureUpdatesPerTick(maxFullUpdatesPerTick(uploader)) |
| 54 , m_firstUpdateAttempt(true) | |
| 55 { | 52 { |
| 56 } | 53 } |
| 57 | 54 |
| 58 CCTextureUpdateController::~CCTextureUpdateController() | 55 CCTextureUpdateController::~CCTextureUpdateController() |
| 59 { | 56 { |
| 60 } | 57 } |
| 61 | 58 |
| 62 void CCTextureUpdateController::performMoreUpdates( | 59 void CCTextureUpdateController::start() |
| 63 double monotonicTimeLimit) | |
| 64 { | 60 { |
| 65 ASSERT(monotonicTimeLimit >= m_monotonicTimeLimit); | 61 // Post a 0-delay task when no updates were left. When it runs, |
| 66 m_monotonicTimeLimit = monotonicTimeLimit; | 62 // updateTexturesCompleted() will be called. |
| 67 | 63 if (!updateMoreTextures()) |
| 68 // Update already in progress. | 64 m_timer->startOneShot(0); |
| 69 if (m_timer->isActive()) | |
| 70 return; | |
| 71 | |
| 72 // Call updateMoreTexturesNow() directly unless it's the first update | |
| 73 // attempt. This ensures that we empty the update queue in a finite | |
| 74 // amount of time. | |
| 75 if (m_firstUpdateAttempt) { | |
| 76 // Post a 0-delay task when no updates were left. When it runs, | |
| 77 // readyToFinalizeTextureUpdates() will be called. | |
| 78 if (!updateMoreTexturesIfEnoughTimeRemaining()) | |
| 79 m_timer->startOneShot(0); | |
| 80 | |
| 81 m_firstUpdateAttempt = false; | |
| 82 } else | |
| 83 updateMoreTexturesNow(); | |
| 84 } | 65 } |
| 85 | 66 |
| 86 void CCTextureUpdateController::discardUploadsToEvictedResources() | 67 void CCTextureUpdateController::discardUploadsToEvictedResources() |
| 87 { | 68 { |
| 88 m_queue->clearUploadsToEvictedResources(); | 69 m_queue->clearUploadsToEvictedResources(); |
| 89 } | 70 } |
| 90 | 71 |
| 91 void CCTextureUpdateController::finalize() | 72 void CCTextureUpdateController::finalize() |
| 92 { | 73 { |
| 93 size_t uploadCount = 0; | 74 size_t uploadCount = 0; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 120 // If we've performed any texture copies, we need to insert a flush | 101 // If we've performed any texture copies, we need to insert a flush |
| 121 // here into the compositor context before letting the main thread | 102 // here into the compositor context before letting the main thread |
| 122 // proceed as it may make draw calls to the source texture of one of | 103 // proceed as it may make draw calls to the source texture of one of |
| 123 // our copy operations. | 104 // our copy operations. |
| 124 copier->flush(); | 105 copier->flush(); |
| 125 } | 106 } |
| 126 } | 107 } |
| 127 | 108 |
| 128 void CCTextureUpdateController::onTimerFired() | 109 void CCTextureUpdateController::onTimerFired() |
| 129 { | 110 { |
| 130 if (!updateMoreTexturesIfEnoughTimeRemaining()) | 111 if (!updateMoreTextures()) |
| 131 m_client->readyToFinalizeTextureUpdates(); | 112 m_client->readyToFinalizeTextureUpdates(); |
| 132 } | 113 } |
| 133 | 114 |
| 134 double CCTextureUpdateController::monotonicTimeNow() const | |
| 135 { | |
| 136 return monotonicallyIncreasingTime(); | |
| 137 } | |
| 138 | |
| 139 double CCTextureUpdateController::updateMoreTexturesTime() const | 115 double CCTextureUpdateController::updateMoreTexturesTime() const |
| 140 { | 116 { |
| 141 return textureUpdateTickRate; | 117 return textureUpdateTickRate; |
| 142 } | 118 } |
| 143 | 119 |
| 144 size_t CCTextureUpdateController::updateMoreTexturesSize() const | 120 size_t CCTextureUpdateController::updateMoreTexturesSize() const |
| 145 { | 121 { |
| 146 return m_textureUpdatesPerTick; | 122 return m_textureUpdatesPerTick; |
| 147 } | 123 } |
| 148 | 124 |
| 149 bool CCTextureUpdateController::updateMoreTexturesIfEnoughTimeRemaining() | 125 // Performs lazy texture updates. Currently only full uploads. |
| 126 bool CCTextureUpdateController::updateMoreTextures() |
| 150 { | 127 { |
| 151 // Uploader might be busy when we're too aggressive in our upload time | 128 // Uploader might be busy when we're too aggressive in our upload time |
| 152 // estimate. We use a different timeout here to prevent unnecessary | 129 // estimate. We use a different timeout here to prevent unnecessary |
| 153 // amounts of idle time. | 130 // amounts of idle time. |
| 154 if (m_uploader->isBusy()) { | 131 if (m_uploader->isBusy()) { |
| 155 m_timer->startOneShot(uploaderBusyTickRate); | 132 m_timer->startOneShot(uploaderBusyTickRate); |
| 156 return true; | 133 return true; |
| 157 } | 134 } |
| 158 | 135 |
| 159 if (!m_queue->fullUploadSize()) | 136 if (!m_queue->fullUploadSize()) |
| 160 return false; | 137 return false; |
| 161 | 138 |
| 162 bool hasTimeRemaining = monotonicTimeNow() < m_monotonicTimeLimit - updateMo
reTexturesTime(); | |
| 163 if (hasTimeRemaining) | |
| 164 updateMoreTexturesNow(); | |
| 165 | |
| 166 return true; | |
| 167 } | |
| 168 | |
| 169 void CCTextureUpdateController::updateMoreTexturesNow() | |
| 170 { | |
| 171 size_t uploads = std::min( | 139 size_t uploads = std::min( |
| 172 m_queue->fullUploadSize(), updateMoreTexturesSize()); | 140 m_queue->fullUploadSize(), updateMoreTexturesSize()); |
| 173 m_timer->startOneShot( | 141 m_timer->startOneShot( |
| 174 updateMoreTexturesTime() / updateMoreTexturesSize() * uploads); | 142 updateMoreTexturesTime() / updateMoreTexturesSize() * uploads); |
| 175 | 143 |
| 176 if (!uploads) | 144 ASSERT(uploads); |
| 177 return; | |
| 178 | 145 |
| 179 size_t uploadCount = 0; | 146 size_t uploadCount = 0; |
| 180 m_uploader->beginUploads(); | 147 m_uploader->beginUploads(); |
| 181 while (m_queue->fullUploadSize() && uploadCount < uploads) { | 148 while (m_queue->fullUploadSize() && uploadCount < uploads) { |
| 182 if (!(uploadCount % textureUploadFlushPeriod) && uploadCount) | 149 if (!(uploadCount % textureUploadFlushPeriod) && uploadCount) |
| 183 m_resourceProvider->shallowFlushIfSupported(); | 150 m_resourceProvider->shallowFlushIfSupported(); |
| 184 m_uploader->uploadTexture(m_resourceProvider, m_queue->takeFirstFullUplo
ad()); | 151 m_uploader->uploadTexture(m_resourceProvider, m_queue->takeFirstFullUplo
ad()); |
| 185 uploadCount++; | 152 uploadCount++; |
| 186 } | 153 } |
| 187 m_uploader->endUploads(); | 154 m_uploader->endUploads(); |
| 188 m_resourceProvider->shallowFlushIfSupported(); | 155 m_resourceProvider->shallowFlushIfSupported(); |
| 156 return true; |
| 189 } | 157 } |
| 190 | 158 |
| 191 } | 159 } |
| OLD | NEW |