summaryrefslogtreecommitdiffstats
path: root/cc/resource_update_controller.cc
blob: d60b5bbed1ef361dd03c64fa06e53eb7be0f5a61 (plain)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// 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 "cc/resource_update_controller.h"

#include <limits>

#include "base/debug/trace_event.h"
#include "cc/prioritized_resource.h"
#include "cc/resource_provider.h"
#include "cc/texture_copier.h"
#include "cc/thread.h"
#include "skia/ext/refptr.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3D.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebSharedGraphicsContext3D.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "third_party/skia/include/gpu/SkGpuDevice.h"

using WebKit::WebGraphicsContext3D;
using WebKit::WebSharedGraphicsContext3D;

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;

// Number of blocking update intervals to allow.
const size_t maxBlockingUpdateIntervals = 4;

skia::RefPtr<SkCanvas> createAcceleratedCanvas(
    GrContext* grContext, gfx::Size canvasSize, unsigned textureId)
{
    GrBackendTextureDesc textureDesc;
    textureDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
    textureDesc.fWidth = canvasSize.width();
    textureDesc.fHeight = canvasSize.height();
    textureDesc.fConfig = kSkia8888_GrPixelConfig;
    textureDesc.fTextureHandle = textureId;
    skia::RefPtr<GrTexture> target =
        skia::AdoptRef(grContext->wrapBackendTexture(textureDesc));
    skia::RefPtr<SkDevice> device =
        skia::AdoptRef(new SkGpuDevice(grContext, target.get()));
    return skia::AdoptRef(new SkCanvas(device.get()));
}

}  // namespace

namespace cc {

size_t ResourceUpdateController::maxPartialTextureUpdates()
{
    return partialTextureUpdatesMax;
}

size_t ResourceUpdateController::maxFullUpdatesPerTick(
    ResourceProvider* resourceProvider)
{
    double texturesPerSecond = resourceProvider->estimatedUploadsPerSecond();
    size_t texturesPerTick = floor(textureUpdateTickRate * texturesPerSecond);
    return texturesPerTick ? texturesPerTick : 1;
}

ResourceUpdateController::ResourceUpdateController(ResourceUpdateControllerClient* client, Thread* thread, scoped_ptr<ResourceUpdateQueue> queue, ResourceProvider* resourceProvider, bool hasImplThread)
    : m_client(client)
    , m_hasImplThread(hasImplThread)
    , m_queue(queue.Pass())
    , m_resourceProvider(resourceProvider)
    , m_textureUpdatesPerTick(maxFullUpdatesPerTick(resourceProvider))
    , m_firstUpdateAttempt(true)
    , m_thread(thread)
    , m_weakFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this))
    , m_taskPosted(false)
{
}

ResourceUpdateController::~ResourceUpdateController()
{
}

void ResourceUpdateController::performMoreUpdates(
    base::TimeTicks timeLimit)
{
    m_timeLimit = timeLimit;

    // Update already in progress.
    if (m_taskPosted)
        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)
        updateMoreTexturesNow();

    // Post a 0-delay task when no updates were left. When it runs,
    // readyToFinalizeTextureUpdates() will be called.
    if (!updateMoreTexturesIfEnoughTimeRemaining()) {
        m_taskPosted = true;
        m_thread->postTask(
            base::Bind(&ResourceUpdateController::onTimerFired,
                       m_weakFactory.GetWeakPtr()));
    }

    m_firstUpdateAttempt = false;
}

void ResourceUpdateController::discardUploadsToEvictedResources()
{
    m_queue->clearUploadsToEvictedResources();
}

void ResourceUpdateController::updateTexture(ResourceUpdate update)
{
    if (update.picture) {
        PrioritizedResource* texture = update.texture;
        gfx::Rect pictureRect = update.content_rect;
        gfx::Rect sourceRect = update.source_rect;
        gfx::Vector2d destOffset = update.dest_offset;

        texture->acquireBackingTexture(m_resourceProvider);
        DCHECK(texture->haveBackingTexture());

        DCHECK(m_resourceProvider->resourceType(texture->resourceId()) ==
               ResourceProvider::GLTexture);

        WebGraphicsContext3D* paintContext = m_hasImplThread ?
            WebSharedGraphicsContext3D::compositorThreadContext() :
            WebSharedGraphicsContext3D::mainThreadContext();
        GrContext* paintGrContext = m_hasImplThread ?
            WebSharedGraphicsContext3D::compositorThreadGrContext() :
            WebSharedGraphicsContext3D::mainThreadGrContext();

        // Flush the context in which the backing texture is created so that it
        // is available in other shared contexts. It is important to do here
        // because the backing texture is created in one context while it is
        // being written to in another.
        ResourceProvider::ScopedWriteLockGL lock(
            m_resourceProvider, texture->resourceId());
        m_resourceProvider->flush();

        // Make sure ganesh uses the correct GL context.
        paintContext->makeContextCurrent();

        // Create an accelerated canvas to draw on.
        skia::RefPtr<SkCanvas> canvas = createAcceleratedCanvas(
            paintGrContext, texture->size(), lock.textureId());

        // The compositor expects the textures to be upside-down so it can flip
        // the final composited image. Ganesh renders the image upright so we
        // need to do a y-flip.
        canvas->translate(0.0, texture->size().height());
        canvas->scale(1.0, -1.0);
        // Clip to the destination on the texture that must be updated.
        canvas->clipRect(SkRect::MakeXYWH(destOffset.x(),
                                          destOffset.y(),
                                          sourceRect.width(),
                                          sourceRect.height()));
        // Translate the origin of pictureRect to destOffset.
        // Note that destOffset is defined relative to sourceRect.
        canvas->translate(
            pictureRect.x() - sourceRect.x() + destOffset.x(),
            pictureRect.y() - sourceRect.y() + destOffset.y());
        canvas->drawPicture(*update.picture);

        // Flush ganesh context so that all the rendered stuff appears on the
        // texture.
        paintGrContext->flush();

        // Flush the GL context so rendering results from this context are
        // visible in the compositor's context.
        paintContext->flush();
    }

    if (update.bitmap) {
        update.bitmap->lockPixels();
        update.texture->setPixels(
            m_resourceProvider,
            static_cast<const uint8_t*>(update.bitmap->getPixels()),
            update.content_rect,
            update.source_rect,
            update.dest_offset);
        update.bitmap->unlockPixels();
    }
}

void ResourceUpdateController::finalize()
{
    while (m_queue->fullUploadSize())
        updateTexture(m_queue->takeFirstFullUpload());

    while (m_queue->partialUploadSize())
        updateTexture(m_queue->takeFirstPartialUpload());

    m_resourceProvider->flushUploads();

    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 ResourceUpdateController::onTimerFired()
{
    m_taskPosted = false;
    if (!updateMoreTexturesIfEnoughTimeRemaining())
        m_client->readyToFinalizeTextureUpdates();
}

base::TimeTicks ResourceUpdateController::now() const
{
    return base::TimeTicks::Now();
}

base::TimeDelta ResourceUpdateController::updateMoreTexturesTime() const
{
    return base::TimeDelta::FromMilliseconds(textureUpdateTickRate * 1000);
}

size_t ResourceUpdateController::updateMoreTexturesSize() const
{
    return m_textureUpdatesPerTick;
}

size_t ResourceUpdateController::maxBlockingUpdates() const
{
    return updateMoreTexturesSize() * maxBlockingUpdateIntervals;
}

base::TimeDelta ResourceUpdateController::pendingUpdateTime() const
{
    base::TimeDelta updateOneResourceTime =
        updateMoreTexturesTime() / updateMoreTexturesSize();
    return updateOneResourceTime * m_resourceProvider->numBlockingUploads();
}

bool ResourceUpdateController::updateMoreTexturesIfEnoughTimeRemaining()
{
    while (m_resourceProvider->numBlockingUploads() < maxBlockingUpdates()) {
        if (!m_queue->fullUploadSize())
            return false;

        if (!m_timeLimit.is_null()) {
            // Estimated completion time of all pending updates.
            base::TimeTicks completionTime = this->now() + pendingUpdateTime();

            // Time remaining based on current completion estimate.
            base::TimeDelta timeRemaining = m_timeLimit - completionTime;

            if (timeRemaining < updateMoreTexturesTime())
                return true;
        }

        updateMoreTexturesNow();
    }

    m_taskPosted = true;
    m_thread->postDelayedTask(
        base::Bind(&ResourceUpdateController::onTimerFired,
                   m_weakFactory.GetWeakPtr()),
        uploaderBusyTickRate * 1000);
    return true;
}

void ResourceUpdateController::updateMoreTexturesNow()
{
    size_t uploads = std::min(
        m_queue->fullUploadSize(), updateMoreTexturesSize());

    if (!uploads)
        return;

    while (m_queue->fullUploadSize() && uploads--)
        updateTexture(m_queue->takeFirstFullUpload());

    m_resourceProvider->flushUploads();
}

}  // namespace cc