[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 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 | |
| 7 | #include "CCTextureUpdateController.h" |
| 8 | |
[email protected] | ea8af90e | 2012-10-13 10:20:27 | [diff] [blame] | 9 | #include "CCResourceProvider.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 10 | #include "TextureCopier.h" |
| 11 | #include "TextureUploader.h" |
[email protected] | 41b8f68 | 2012-09-19 01:51:17 | [diff] [blame] | 12 | #include "TraceEvent.h" |
| 13 | #include <limits> |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 14 | #include <wtf/CurrentTime.h> |
| 15 | |
| 16 | namespace { |
| 17 | |
[email protected] | 41b8f68 | 2012-09-19 01:51:17 | [diff] [blame] | 18 | // Number of partial updates we allow. |
[email protected] | 2d86b92 | 2012-10-13 16:57:47 | [diff] [blame] | 19 | const size_t partialTextureUpdatesMax = 12; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 20 | |
| 21 | // Measured in seconds. |
[email protected] | 2d86b92 | 2012-10-13 16:57:47 | [diff] [blame] | 22 | const double textureUpdateTickRate = 0.004; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 23 | |
[email protected] | 1269515 | 2012-09-17 22:28:14 | [diff] [blame] | 24 | // Measured in seconds. |
[email protected] | 2d86b92 | 2012-10-13 16:57:47 | [diff] [blame] | 25 | const double uploaderBusyTickRate = 0.001; |
[email protected] | 1269515 | 2012-09-17 22:28:14 | [diff] [blame] | 26 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 27 | // Flush interval when performing texture uploads. |
[email protected] | 2d86b92 | 2012-10-13 16:57:47 | [diff] [blame] | 28 | const int textureUploadFlushPeriod = 4; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 29 | |
[email protected] | b914e10 | 2012-10-02 08:11:52 | [diff] [blame] | 30 | // Number of blocking update intervals to allow. |
[email protected] | 2d86b92 | 2012-10-13 16:57:47 | [diff] [blame] | 31 | const size_t maxBlockingUpdateIntervals = 4; |
[email protected] | 549526e9 | 2012-09-29 15:43:08 | [diff] [blame] | 32 | |
[email protected] | 2d86b92 | 2012-10-13 16:57:47 | [diff] [blame] | 33 | } // namespace |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 34 | |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 35 | namespace cc { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 36 | |
| 37 | size_t CCTextureUpdateController::maxPartialTextureUpdates() |
| 38 | { |
[email protected] | a7c078b | 2012-09-20 17:52:12 | [diff] [blame] | 39 | return partialTextureUpdatesMax; |
[email protected] | 41b8f68 | 2012-09-19 01:51:17 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | size_t CCTextureUpdateController::maxFullUpdatesPerTick(TextureUploader* uploader) |
| 43 | { |
| 44 | double texturesPerSecond = uploader->estimatedTexturesPerSecond(); |
| 45 | size_t texturesPerTick = floor(textureUpdateTickRate * texturesPerSecond); |
| 46 | return texturesPerTick ? texturesPerTick : 1; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 47 | } |
| 48 | |
[email protected] | b1b800c | 2012-10-16 05:03:59 | [diff] [blame^] | 49 | CCTextureUpdateController::CCTextureUpdateController(CCTextureUpdateControllerClient* client, CCThread* thread, scoped_ptr<CCTextureUpdateQueue> queue, CCResourceProvider* resourceProvider, TextureUploader* uploader) |
[email protected] | 3c496dc | 2012-09-13 23:14:20 | [diff] [blame] | 50 | : m_client(client) |
[email protected] | 2d86b92 | 2012-10-13 16:57:47 | [diff] [blame] | 51 | , m_timer(new CCTimer(thread, this)) |
[email protected] | b1b800c | 2012-10-16 05:03:59 | [diff] [blame^] | 52 | , m_queue(queue.Pass()) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 53 | , m_resourceProvider(resourceProvider) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 54 | , m_uploader(uploader) |
[email protected] | 41b8f68 | 2012-09-19 01:51:17 | [diff] [blame] | 55 | , m_textureUpdatesPerTick(maxFullUpdatesPerTick(uploader)) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 56 | , m_firstUpdateAttempt(true) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | CCTextureUpdateController::~CCTextureUpdateController() |
| 61 | { |
| 62 | } |
| 63 | |
[email protected] | 1269515 | 2012-09-17 22:28:14 | [diff] [blame] | 64 | void CCTextureUpdateController::performMoreUpdates( |
[email protected] | d918afe | 2012-09-27 19:27:21 | [diff] [blame] | 65 | base::TimeTicks timeLimit) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 66 | { |
[email protected] | d918afe | 2012-09-27 19:27:21 | [diff] [blame] | 67 | m_timeLimit = timeLimit; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 68 | |
[email protected] | 3c496dc | 2012-09-13 23:14:20 | [diff] [blame] | 69 | // Update already in progress. |
| 70 | if (m_timer->isActive()) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 71 | return; |
| 72 | |
| 73 | // Call updateMoreTexturesNow() directly unless it's the first update |
| 74 | // attempt. This ensures that we empty the update queue in a finite |
| 75 | // amount of time. |
| 76 | if (m_firstUpdateAttempt) { |
[email protected] | 3c496dc | 2012-09-13 23:14:20 | [diff] [blame] | 77 | // Post a 0-delay task when no updates were left. When it runs, |
[email protected] | 1269515 | 2012-09-17 22:28:14 | [diff] [blame] | 78 | // readyToFinalizeTextureUpdates() will be called. |
[email protected] | 3c496dc | 2012-09-13 23:14:20 | [diff] [blame] | 79 | if (!updateMoreTexturesIfEnoughTimeRemaining()) |
| 80 | m_timer->startOneShot(0); |
| 81 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 82 | m_firstUpdateAttempt = false; |
| 83 | } else |
| 84 | updateMoreTexturesNow(); |
| 85 | } |
| 86 | |
[email protected] | 77a60b9b | 2012-09-19 23:59:01 | [diff] [blame] | 87 | void CCTextureUpdateController::discardUploadsToEvictedResources() |
| 88 | { |
| 89 | m_queue->clearUploadsToEvictedResources(); |
| 90 | } |
| 91 | |
[email protected] | 1269515 | 2012-09-17 22:28:14 | [diff] [blame] | 92 | void CCTextureUpdateController::finalize() |
| 93 | { |
[email protected] | f961b79 | 2012-09-20 07:27:33 | [diff] [blame] | 94 | size_t uploadCount = 0; |
| 95 | while (m_queue->fullUploadSize()) { |
| 96 | if (!(uploadCount % textureUploadFlushPeriod) && uploadCount) |
| 97 | m_resourceProvider->shallowFlushIfSupported(); |
| 98 | |
| 99 | m_uploader->uploadTexture( |
| 100 | m_resourceProvider, m_queue->takeFirstFullUpload()); |
| 101 | uploadCount++; |
| 102 | } |
| 103 | |
| 104 | while (m_queue->partialUploadSize()) { |
| 105 | if (!(uploadCount % textureUploadFlushPeriod) && uploadCount) |
| 106 | m_resourceProvider->shallowFlushIfSupported(); |
| 107 | |
| 108 | m_uploader->uploadTexture( |
| 109 | m_resourceProvider, m_queue->takeFirstPartialUpload()); |
| 110 | uploadCount++; |
| 111 | } |
| 112 | |
| 113 | if (uploadCount) |
| 114 | m_resourceProvider->shallowFlushIfSupported(); |
| 115 | |
| 116 | if (m_queue->copySize()) { |
| 117 | TextureCopier* copier = m_resourceProvider->textureCopier(); |
| 118 | while (m_queue->copySize()) |
| 119 | copier->copyTexture(m_queue->takeFirstCopy()); |
| 120 | |
| 121 | // If we've performed any texture copies, we need to insert a flush |
| 122 | // here into the compositor context before letting the main thread |
| 123 | // proceed as it may make draw calls to the source texture of one of |
| 124 | // our copy operations. |
| 125 | copier->flush(); |
| 126 | } |
[email protected] | 1269515 | 2012-09-17 22:28:14 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 129 | void CCTextureUpdateController::onTimerFired() |
| 130 | { |
[email protected] | 3c496dc | 2012-09-13 23:14:20 | [diff] [blame] | 131 | if (!updateMoreTexturesIfEnoughTimeRemaining()) |
[email protected] | 1269515 | 2012-09-17 22:28:14 | [diff] [blame] | 132 | m_client->readyToFinalizeTextureUpdates(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | d918afe | 2012-09-27 19:27:21 | [diff] [blame] | 135 | base::TimeTicks CCTextureUpdateController::now() const |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 136 | { |
[email protected] | d918afe | 2012-09-27 19:27:21 | [diff] [blame] | 137 | return base::TimeTicks::Now(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 138 | } |
| 139 | |
[email protected] | d918afe | 2012-09-27 19:27:21 | [diff] [blame] | 140 | base::TimeDelta CCTextureUpdateController::updateMoreTexturesTime() const |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 141 | { |
[email protected] | d918afe | 2012-09-27 19:27:21 | [diff] [blame] | 142 | return base::TimeDelta::FromMilliseconds(textureUpdateTickRate * 1000); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | size_t CCTextureUpdateController::updateMoreTexturesSize() const |
| 146 | { |
[email protected] | 41b8f68 | 2012-09-19 01:51:17 | [diff] [blame] | 147 | return m_textureUpdatesPerTick; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | b914e10 | 2012-10-02 08:11:52 | [diff] [blame] | 150 | size_t CCTextureUpdateController::maxBlockingUpdates() const |
[email protected] | 549526e9 | 2012-09-29 15:43:08 | [diff] [blame] | 151 | { |
[email protected] | b914e10 | 2012-10-02 08:11:52 | [diff] [blame] | 152 | return updateMoreTexturesSize() * maxBlockingUpdateIntervals; |
[email protected] | 549526e9 | 2012-09-29 15:43:08 | [diff] [blame] | 153 | } |
| 154 | |
[email protected] | 3c496dc | 2012-09-13 23:14:20 | [diff] [blame] | 155 | bool CCTextureUpdateController::updateMoreTexturesIfEnoughTimeRemaining() |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 156 | { |
[email protected] | b914e10 | 2012-10-02 08:11:52 | [diff] [blame] | 157 | // Blocking uploads will increase when we're too aggressive in our upload |
[email protected] | 549526e9 | 2012-09-29 15:43:08 | [diff] [blame] | 158 | // time estimate. We use a different timeout here to prevent unnecessary |
[email protected] | b914e10 | 2012-10-02 08:11:52 | [diff] [blame] | 159 | // amounts of idle time when blocking uploads have reached the max. |
| 160 | if (m_uploader->numBlockingUploads() >= maxBlockingUpdates()) { |
[email protected] | 1269515 | 2012-09-17 22:28:14 | [diff] [blame] | 161 | m_timer->startOneShot(uploaderBusyTickRate); |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | if (!m_queue->fullUploadSize()) |
[email protected] | 3c496dc | 2012-09-13 23:14:20 | [diff] [blame] | 166 | return false; |
| 167 | |
[email protected] | e8e410d | 2012-09-28 01:47:01 | [diff] [blame] | 168 | bool hasTimeRemaining = m_timeLimit.is_null() || |
| 169 | this->now() < m_timeLimit - updateMoreTexturesTime(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 170 | if (hasTimeRemaining) |
| 171 | updateMoreTexturesNow(); |
[email protected] | 3c496dc | 2012-09-13 23:14:20 | [diff] [blame] | 172 | |
| 173 | return true; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void CCTextureUpdateController::updateMoreTexturesNow() |
| 177 | { |
[email protected] | 1269515 | 2012-09-17 22:28:14 | [diff] [blame] | 178 | size_t uploads = std::min( |
| 179 | m_queue->fullUploadSize(), updateMoreTexturesSize()); |
| 180 | m_timer->startOneShot( |
[email protected] | d918afe | 2012-09-27 19:27:21 | [diff] [blame] | 181 | updateMoreTexturesTime().InSecondsF() / updateMoreTexturesSize() * |
| 182 | uploads); |
[email protected] | 1269515 | 2012-09-17 22:28:14 | [diff] [blame] | 183 | |
| 184 | if (!uploads) |
| 185 | return; |
| 186 | |
[email protected] | 1269515 | 2012-09-17 22:28:14 | [diff] [blame] | 187 | size_t uploadCount = 0; |
[email protected] | 41b8f68 | 2012-09-19 01:51:17 | [diff] [blame] | 188 | while (m_queue->fullUploadSize() && uploadCount < uploads) { |
| 189 | if (!(uploadCount % textureUploadFlushPeriod) && uploadCount) |
[email protected] | 1269515 | 2012-09-17 22:28:14 | [diff] [blame] | 190 | m_resourceProvider->shallowFlushIfSupported(); |
[email protected] | 41b8f68 | 2012-09-19 01:51:17 | [diff] [blame] | 191 | m_uploader->uploadTexture(m_resourceProvider, m_queue->takeFirstFullUpload()); |
| 192 | uploadCount++; |
[email protected] | 1269515 | 2012-09-17 22:28:14 | [diff] [blame] | 193 | } |
[email protected] | 41b8f68 | 2012-09-19 01:51:17 | [diff] [blame] | 194 | m_resourceProvider->shallowFlushIfSupported(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 195 | } |
| 196 | |
[email protected] | 2d86b92 | 2012-10-13 16:57:47 | [diff] [blame] | 197 | } // namespace cc |