blob: 32923e75bbbb6329eb199926b971ba7b69b96a04 [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// 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]41b8f682012-09-19 01:51:1712#include "TraceEvent.h"
13#include <limits>
[email protected]94f206c12012-08-25 00:09:1414#include <wtf/CurrentTime.h>
15
16namespace {
17
[email protected]41b8f682012-09-19 01:51:1718// Number of partial updates we allow.
[email protected]a7c078b2012-09-20 17:52:1219static const size_t partialTextureUpdatesMax = 12;
[email protected]94f206c12012-08-25 00:09:1420
21// Measured in seconds.
22static const double textureUpdateTickRate = 0.004;
23
[email protected]12695152012-09-17 22:28:1424// Measured in seconds.
25static const double uploaderBusyTickRate = 0.001;
26
[email protected]94f206c12012-08-25 00:09:1427// Flush interval when performing texture uploads.
28static const int textureUploadFlushPeriod = 4;
29
30} // anonymous namespace
31
[email protected]9c88e562012-09-14 22:21:3032namespace cc {
[email protected]94f206c12012-08-25 00:09:1433
34size_t CCTextureUpdateController::maxPartialTextureUpdates()
35{
[email protected]a7c078b2012-09-20 17:52:1236 return partialTextureUpdatesMax;
[email protected]41b8f682012-09-19 01:51:1737}
38
39size_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]94f206c12012-08-25 00:09:1444}
45
[email protected]ed4685982012-09-18 03:44:5846CCTextureUpdateController::CCTextureUpdateController(CCTextureUpdateControllerClient* client, CCThread* thread, PassOwnPtr<CCTextureUpdateQueue> queue, CCResourceProvider* resourceProvider, TextureUploader* uploader)
[email protected]3c496dc2012-09-13 23:14:2047 : m_client(client)
48 , m_timer(adoptPtr(new CCTimer(thread, this)))
[email protected]94f206c12012-08-25 00:09:1449 , m_queue(queue)
50 , m_resourceProvider(resourceProvider)
[email protected]94f206c12012-08-25 00:09:1451 , m_uploader(uploader)
52 , m_monotonicTimeLimit(0)
[email protected]41b8f682012-09-19 01:51:1753 , m_textureUpdatesPerTick(maxFullUpdatesPerTick(uploader))
[email protected]94f206c12012-08-25 00:09:1454 , m_firstUpdateAttempt(true)
55{
56}
57
58CCTextureUpdateController::~CCTextureUpdateController()
59{
60}
61
[email protected]12695152012-09-17 22:28:1462void CCTextureUpdateController::performMoreUpdates(
63 double monotonicTimeLimit)
[email protected]94f206c12012-08-25 00:09:1464{
[email protected]3c496dc2012-09-13 23:14:2065 ASSERT(monotonicTimeLimit >= m_monotonicTimeLimit);
[email protected]94f206c12012-08-25 00:09:1466 m_monotonicTimeLimit = monotonicTimeLimit;
67
[email protected]3c496dc2012-09-13 23:14:2068 // Update already in progress.
69 if (m_timer->isActive())
[email protected]94f206c12012-08-25 00:09:1470 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) {
[email protected]3c496dc2012-09-13 23:14:2076 // Post a 0-delay task when no updates were left. When it runs,
[email protected]12695152012-09-17 22:28:1477 // readyToFinalizeTextureUpdates() will be called.
[email protected]3c496dc2012-09-13 23:14:2078 if (!updateMoreTexturesIfEnoughTimeRemaining())
79 m_timer->startOneShot(0);
80
[email protected]94f206c12012-08-25 00:09:1481 m_firstUpdateAttempt = false;
82 } else
83 updateMoreTexturesNow();
84}
85
[email protected]77a60b9b2012-09-19 23:59:0186void CCTextureUpdateController::discardUploadsToEvictedResources()
87{
88 m_queue->clearUploadsToEvictedResources();
89}
90
[email protected]12695152012-09-17 22:28:1491void CCTextureUpdateController::finalize()
92{
[email protected]f961b792012-09-20 07:27:3393 size_t uploadCount = 0;
94 while (m_queue->fullUploadSize()) {
95 if (!(uploadCount % textureUploadFlushPeriod) && uploadCount)
96 m_resourceProvider->shallowFlushIfSupported();
97
98 m_uploader->uploadTexture(
99 m_resourceProvider, m_queue->takeFirstFullUpload());
100 uploadCount++;
101 }
102
103 while (m_queue->partialUploadSize()) {
104 if (!(uploadCount % textureUploadFlushPeriod) && uploadCount)
105 m_resourceProvider->shallowFlushIfSupported();
106
107 m_uploader->uploadTexture(
108 m_resourceProvider, m_queue->takeFirstPartialUpload());
109 uploadCount++;
110 }
111
112 if (uploadCount)
113 m_resourceProvider->shallowFlushIfSupported();
114
115 if (m_queue->copySize()) {
116 TextureCopier* copier = m_resourceProvider->textureCopier();
117 while (m_queue->copySize())
118 copier->copyTexture(m_queue->takeFirstCopy());
119
120 // If we've performed any texture copies, we need to insert a flush
121 // 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
123 // our copy operations.
124 copier->flush();
125 }
[email protected]12695152012-09-17 22:28:14126}
127
[email protected]94f206c12012-08-25 00:09:14128void CCTextureUpdateController::onTimerFired()
129{
[email protected]3c496dc2012-09-13 23:14:20130 if (!updateMoreTexturesIfEnoughTimeRemaining())
[email protected]12695152012-09-17 22:28:14131 m_client->readyToFinalizeTextureUpdates();
[email protected]94f206c12012-08-25 00:09:14132}
133
134double CCTextureUpdateController::monotonicTimeNow() const
135{
136 return monotonicallyIncreasingTime();
137}
138
139double CCTextureUpdateController::updateMoreTexturesTime() const
140{
141 return textureUpdateTickRate;
142}
143
144size_t CCTextureUpdateController::updateMoreTexturesSize() const
145{
[email protected]41b8f682012-09-19 01:51:17146 return m_textureUpdatesPerTick;
[email protected]94f206c12012-08-25 00:09:14147}
148
[email protected]3c496dc2012-09-13 23:14:20149bool CCTextureUpdateController::updateMoreTexturesIfEnoughTimeRemaining()
[email protected]94f206c12012-08-25 00:09:14150{
[email protected]12695152012-09-17 22:28:14151 // 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]3c496dc2012-09-13 23:14:20160 return false;
161
[email protected]94f206c12012-08-25 00:09:14162 bool hasTimeRemaining = monotonicTimeNow() < m_monotonicTimeLimit - updateMoreTexturesTime();
163 if (hasTimeRemaining)
164 updateMoreTexturesNow();
[email protected]3c496dc2012-09-13 23:14:20165
166 return true;
[email protected]94f206c12012-08-25 00:09:14167}
168
169void CCTextureUpdateController::updateMoreTexturesNow()
170{
[email protected]12695152012-09-17 22:28:14171 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]12695152012-09-17 22:28:14179 size_t uploadCount = 0;
[email protected]41b8f682012-09-19 01:51:17180 m_uploader->beginUploads();
181 while (m_queue->fullUploadSize() && uploadCount < uploads) {
182 if (!(uploadCount % textureUploadFlushPeriod) && uploadCount)
[email protected]12695152012-09-17 22:28:14183 m_resourceProvider->shallowFlushIfSupported();
[email protected]41b8f682012-09-19 01:51:17184 m_uploader->uploadTexture(m_resourceProvider, m_queue->takeFirstFullUpload());
185 uploadCount++;
[email protected]12695152012-09-17 22:28:14186 }
[email protected]12695152012-09-17 22:28:14187 m_uploader->endUploads();
[email protected]41b8f682012-09-19 01:51:17188 m_resourceProvider->shallowFlushIfSupported();
[email protected]94f206c12012-08-25 00:09:14189}
190
191}