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
|
// 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 "ThrottledTextureUploader.h"
#include "Extensions3DChromium.h"
#include <algorithm>
#include <public/WebGraphicsContext3D.h>
#include <vector>
namespace {
// Number of pending texture update queries to allow.
static const size_t maxPendingQueries = 2;
// How many previous uploads to use when predicting future throughput.
static const size_t uploadHistorySize = 10;
// Global estimated number of textures per second to maintain estimates across
// subsequent instances of ThrottledTextureUploader.
// More than one thread will not access this variable, so we do not need to synchronize access.
static double estimatedTexturesPerSecondGlobal = 48.0 * 60.0;
} // anonymous namespace
namespace cc {
ThrottledTextureUploader::Query::Query(WebKit::WebGraphicsContext3D* context)
: m_context(context)
, m_queryId(0)
, m_value(0)
, m_hasValue(false)
, m_texturesUploaded(0)
{
m_queryId = m_context->createQueryEXT();
}
ThrottledTextureUploader::Query::~Query()
{
m_context->deleteQueryEXT(m_queryId);
}
void ThrottledTextureUploader::Query::begin()
{
m_hasValue = false;
m_context->beginQueryEXT(Extensions3DChromium::COMMANDS_ISSUED_CHROMIUM, m_queryId);
}
void ThrottledTextureUploader::Query::end(double texturesUploaded)
{
m_context->endQueryEXT(Extensions3DChromium::COMMANDS_ISSUED_CHROMIUM);
m_texturesUploaded = texturesUploaded;
}
bool ThrottledTextureUploader::Query::isPending()
{
unsigned available = 1;
m_context->getQueryObjectuivEXT(m_queryId, Extensions3DChromium::QUERY_RESULT_AVAILABLE_EXT, &available);
return !available;
}
void ThrottledTextureUploader::Query::wait()
{
value();
return;
}
unsigned ThrottledTextureUploader::Query::value()
{
if (!m_hasValue) {
m_context->getQueryObjectuivEXT(m_queryId, Extensions3DChromium::QUERY_RESULT_EXT, &m_value);
m_hasValue = true;
}
return m_value;
}
double ThrottledTextureUploader::Query::texturesUploaded()
{
return m_texturesUploaded;
}
ThrottledTextureUploader::ThrottledTextureUploader(WebKit::WebGraphicsContext3D* context)
: m_context(context)
, m_maxPendingQueries(maxPendingQueries)
, m_texturesPerSecondHistory(uploadHistorySize, estimatedTexturesPerSecondGlobal)
, m_texturesUploaded(0)
{
}
ThrottledTextureUploader::ThrottledTextureUploader(WebKit::WebGraphicsContext3D* context, size_t pendingUploadLimit)
: m_context(context)
, m_maxPendingQueries(pendingUploadLimit)
, m_texturesPerSecondHistory(uploadHistorySize, estimatedTexturesPerSecondGlobal)
, m_texturesUploaded(0)
{
ASSERT(m_context);
}
ThrottledTextureUploader::~ThrottledTextureUploader()
{
}
bool ThrottledTextureUploader::isBusy()
{
processQueries();
if (!m_availableQueries.isEmpty())
return false;
if (m_pendingQueries.size() == m_maxPendingQueries)
return true;
m_availableQueries.append(Query::create(m_context));
return false;
}
double ThrottledTextureUploader::estimatedTexturesPerSecond()
{
processQueries();
// The history should never be empty because we initialize all elements with an estimate.
ASSERT(m_texturesPerSecondHistory.size() == uploadHistorySize);
// Sort the history and use the median as our estimate.
std::vector<double> sortedHistory(m_texturesPerSecondHistory.begin(),
m_texturesPerSecondHistory.end());
std::sort(sortedHistory.begin(), sortedHistory.end());
estimatedTexturesPerSecondGlobal = sortedHistory[sortedHistory.size() / 2];
return estimatedTexturesPerSecondGlobal;
}
void ThrottledTextureUploader::beginUploads()
{
m_texturesUploaded = 0;
// Wait for query to become available.
while (isBusy())
m_pendingQueries.first()->wait();
ASSERT(!m_availableQueries.isEmpty());
m_availableQueries.first()->begin();
}
void ThrottledTextureUploader::endUploads()
{
m_availableQueries.first()->end(m_texturesUploaded);
m_pendingQueries.append(m_availableQueries.takeFirst());
}
void ThrottledTextureUploader::uploadTexture(CCResourceProvider* resourceProvider, Parameters upload)
{
m_texturesUploaded++;
upload.texture->updateRect(resourceProvider, upload.sourceRect, upload.destOffset);
}
void ThrottledTextureUploader::processQueries()
{
while (!m_pendingQueries.isEmpty()) {
if (m_pendingQueries.first()->isPending())
break;
unsigned usElapsed = m_pendingQueries.first()->value();
double texturesPerSecond = m_pendingQueries.first()->texturesUploaded() / (usElapsed * 1e-6);
// Remove the oldest values from our history and insert the new one
m_texturesPerSecondHistory.pop_back();
m_texturesPerSecondHistory.push_front(texturesPerSecond);
m_availableQueries.append(m_pendingQueries.takeFirst());
}
}
}
|