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