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