summaryrefslogtreecommitdiff
path: root/Source/WebCore/platform/graphics/chromium/cc/CCTextureUpdater.cpp
blob: 967ad5e3fd5b741f6d330cc59edfe80938c23383 (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
/*
 * Copyright (C) 2011 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#if USE(ACCELERATED_COMPOSITING)

#include "cc/CCTextureUpdater.h"

#include "CCPrioritizedTexture.h"
#include "GraphicsContext3D.h"
#include "LayerTextureUpdater.h"
#include "TextureCopier.h"
#include "TextureUploader.h"

using namespace std;

namespace WebCore {

static const int kUploadFlushPeriod = 4;

CCTextureUpdater::CCTextureUpdater()
    : m_entryIndex(0)
{
}

CCTextureUpdater::~CCTextureUpdater()
{
}

void CCTextureUpdater::appendUpdate(LayerTextureUpdater::Texture* texture, const IntRect& sourceRect, const IntRect& destRect, Vector<UpdateEntry>& entries)
{
    ASSERT(texture);

    UpdateEntry entry;
    entry.texture = texture;
    entry.sourceRect = sourceRect;
    entry.destRect = destRect;
    entries.append(entry);
}

void CCTextureUpdater::appendFullUpdate(LayerTextureUpdater::Texture* texture, const IntRect& sourceRect, const IntRect& destRect)
{
    appendUpdate(texture, sourceRect, destRect, m_fullEntries);
}

void CCTextureUpdater::appendPartialUpdate(LayerTextureUpdater::Texture* texture, const IntRect& sourceRect, const IntRect& destRect)
{
    appendUpdate(texture, sourceRect, destRect, m_partialEntries);
}

void CCTextureUpdater::appendCopy(unsigned sourceTexture, unsigned destTexture, const IntSize& size)
{
    CopyEntry copy;
    copy.sourceTexture = sourceTexture;
    copy.destTexture = destTexture;
    copy.size = size;
    m_copyEntries.append(copy);
}

bool CCTextureUpdater::hasMoreUpdates() const
{
    return m_fullEntries.size() || m_partialEntries.size() || m_copyEntries.size();
}

void CCTextureUpdater::update(CCResourceProvider* resourceProvider, TextureCopier* copier, TextureUploader* uploader, size_t count)
{
    size_t index;

    if (m_fullEntries.size() || m_partialEntries.size()) {
        if (uploader->isBusy())
            return;

        uploader->beginUploads();

        int fullUploadCount = 0;
        size_t maxIndex = min(m_entryIndex + count, m_fullEntries.size());
        for (index = m_entryIndex; index < maxIndex; ++index) {
            UpdateEntry& entry = m_fullEntries[index];
            uploader->uploadTexture(entry.texture, resourceProvider, entry.sourceRect, entry.destRect);
            fullUploadCount++;
            if (!(fullUploadCount % kUploadFlushPeriod))
                resourceProvider->flush();
        }

        // Make sure there are no dangling uploads without a flush.
        if (fullUploadCount % kUploadFlushPeriod)
            resourceProvider->flush();

        bool moreUploads = maxIndex < m_fullEntries.size();

        ASSERT(m_partialEntries.size() <= count);
        // We need another update batch if the number of updates remaining
        // in |count| is greater than the remaining partial entries.
        if ((count - (index - m_entryIndex)) < m_partialEntries.size())
            moreUploads = true;

        if (moreUploads) {
            m_entryIndex = index;
            uploader->endUploads();
            return;
        }

        for (index = 0; index < m_partialEntries.size(); ++index) {
            UpdateEntry& entry = m_partialEntries[index];
            uploader->uploadTexture(entry.texture, resourceProvider, entry.sourceRect, entry.destRect);
            if (!((index+1) % kUploadFlushPeriod))
                resourceProvider->flush();
        }

        // Make sure there are no dangling partial uploads without a flush.
        // Note: We don't need to use (index+1) in this case because index was
        // incremented at the end of the for loop.
        if (index % kUploadFlushPeriod)
            resourceProvider->flush();

        uploader->endUploads();
    }

    for (index = 0; index < m_copyEntries.size(); ++index) {
        CopyEntry& copyEntry = m_copyEntries[index];
        copier->copyTexture(copyEntry.sourceTexture, copyEntry.destTexture, copyEntry.size);
    }

    // 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.
    if (m_copyEntries.size())
        copier->flush();

    // If no entries left to process, auto-clear.
    clear();
}

void CCTextureUpdater::clear()
{
    m_entryIndex = 0;
    m_fullEntries.clear();
    m_partialEntries.clear();
    m_copyEntries.clear();
}

}

#endif // USE(ACCELERATED_COMPOSITING)