summaryrefslogtreecommitdiffstats
path: root/cc/texture_layer.cc
blob: ddfa09797241a028f9052398a1bec59d446cbbd2 (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
// Copyright 2010 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/texture_layer.h"

#include "cc/layer_tree_host.h"
#include "cc/texture_layer_client.h"
#include "cc/texture_layer_impl.h"
#include "cc/thread.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3D.h"

namespace cc {

static void runCallbackOnMainThread(const TextureLayer::MailboxCallback& callback, unsigned syncPoint)
{
    callback.Run(syncPoint);
}

static void postCallbackToMainThread(Thread *mainThread, const TextureLayer::MailboxCallback& callback, unsigned syncPoint)
{
    mainThread->postTask(base::Bind(&runCallbackOnMainThread, callback, syncPoint));
}

scoped_refptr<TextureLayer> TextureLayer::create(TextureLayerClient* client)
{
    return scoped_refptr<TextureLayer>(new TextureLayer(client, false));
}

scoped_refptr<TextureLayer> TextureLayer::createForMailbox()
{
    return scoped_refptr<TextureLayer>(new TextureLayer(0, true));
}

TextureLayer::TextureLayer(TextureLayerClient* client, bool usesMailbox)
    : Layer()
    , m_client(client)
    , m_usesMailbox(usesMailbox)
    , m_flipped(true)
    , m_uvRect(0, 0, 1, 1)
    , m_premultipliedAlpha(true)
    , m_rateLimitContext(false)
    , m_contextLost(false)
    , m_textureId(0)
    , m_contentCommitted(false)
{
  m_vertexOpacity[0] = 1.0f;
  m_vertexOpacity[1] = 1.0f;
  m_vertexOpacity[2] = 1.0f;
  m_vertexOpacity[3] = 1.0f;
}

TextureLayer::~TextureLayer()
{
    if (layerTreeHost()) {
        if (m_textureId)
            layerTreeHost()->acquireLayerTextures();
        if (m_rateLimitContext && m_client)
            layerTreeHost()->stopRateLimiter(m_client->context());
    }
    if (!m_contentCommitted && !m_mailboxName.empty())
        m_mailboxReleaseCallback.Run(0);
}

scoped_ptr<LayerImpl> TextureLayer::createLayerImpl(LayerTreeImpl* treeImpl)
{
    return TextureLayerImpl::create(treeImpl, m_layerId, m_usesMailbox).PassAs<LayerImpl>();
}

void TextureLayer::setFlipped(bool flipped)
{
    m_flipped = flipped;
    setNeedsCommit();
}

void TextureLayer::setUVRect(const gfx::RectF& rect)
{
    m_uvRect = rect;
    setNeedsCommit();
}

void TextureLayer::setVertexOpacity(float bottomLeft,
                                    float topLeft,
                                    float topRight,
                                    float bottomRight) {
  // Indexing according to the quad vertex generation:
  // 1--2
  // |  |
  // 0--3
  m_vertexOpacity[0] = bottomLeft;
  m_vertexOpacity[1] = topLeft;
  m_vertexOpacity[2] = topRight;
  m_vertexOpacity[3] = bottomRight;
  setNeedsCommit();
}

void TextureLayer::setPremultipliedAlpha(bool premultipliedAlpha)
{
    m_premultipliedAlpha = premultipliedAlpha;
    setNeedsCommit();
}

void TextureLayer::setRateLimitContext(bool rateLimit)
{
    if (!rateLimit && m_rateLimitContext && m_client && layerTreeHost())
        layerTreeHost()->stopRateLimiter(m_client->context());

    m_rateLimitContext = rateLimit;
}

void TextureLayer::setTextureId(unsigned id)
{
    DCHECK(!m_usesMailbox);
    if (m_textureId == id)
        return;
    if (m_textureId && layerTreeHost())
        layerTreeHost()->acquireLayerTextures();
    m_textureId = id;
    setNeedsCommit();
}

void TextureLayer::setTextureMailbox(const std::string& mailboxName, const MailboxCallback& callback)
{
    DCHECK(m_usesMailbox);
    DCHECK(mailboxName.empty() == callback.is_null());
    if (m_mailboxName.compare(mailboxName) == 0)
        return;
    // If we never commited the mailbox, we need to release it here
    if (!m_contentCommitted && !m_mailboxName.empty())
        m_mailboxReleaseCallback.Run(0);
    m_mailboxReleaseCallback = callback;
    m_mailboxName = mailboxName;

    setNeedsCommit();
}

void TextureLayer::willModifyTexture()
{
    if (layerTreeHost() && (drawsContent() || m_contentCommitted)) {
        layerTreeHost()->acquireLayerTextures();
        m_contentCommitted = false;
    }
}

void TextureLayer::setNeedsDisplayRect(const gfx::RectF& dirtyRect)
{
    Layer::setNeedsDisplayRect(dirtyRect);

    if (m_rateLimitContext && m_client && layerTreeHost() && drawsContent())
        layerTreeHost()->startRateLimiter(m_client->context());
}

void TextureLayer::setLayerTreeHost(LayerTreeHost* host)
{
    if (m_textureId && layerTreeHost() && host != layerTreeHost())
        layerTreeHost()->acquireLayerTextures();
    Layer::setLayerTreeHost(host);
}

bool TextureLayer::drawsContent() const
{
    return (m_client || m_textureId || !m_mailboxName.empty()) && !m_contextLost && Layer::drawsContent();
}

void TextureLayer::update(ResourceUpdateQueue& queue, const OcclusionTracker*, RenderingStats&)
{
    if (m_client) {
        m_textureId = m_client->prepareTexture(queue);
        m_contextLost = m_client->context()->getGraphicsResetStatusARB() != GL_NO_ERROR;
    }

    m_needsDisplay = false;
}

void TextureLayer::pushPropertiesTo(LayerImpl* layer)
{
    Layer::pushPropertiesTo(layer);

    TextureLayerImpl* textureLayer = static_cast<TextureLayerImpl*>(layer);
    textureLayer->setFlipped(m_flipped);
    textureLayer->setUVRect(m_uvRect);
    textureLayer->setVertexOpacity(m_vertexOpacity);
    textureLayer->setPremultipliedAlpha(m_premultipliedAlpha);
    if (m_usesMailbox) {
        Thread* mainThread = layerTreeHost()->proxy()->mainThread();
        textureLayer->setTextureMailbox(m_mailboxName, base::Bind(&postCallbackToMainThread, mainThread, m_mailboxReleaseCallback));
    } else {
        textureLayer->setTextureId(m_textureId);
    }
    m_contentCommitted = drawsContent();
}

bool TextureLayer::blocksPendingCommit() const
{
    // Double-buffered texture layers need to be blocked until they can be made
    // triple-buffered.  Single-buffered layers already prevent draws, so
    // can block too for simplicity.
    return true;
}

}  // namespace cc