[chromium] Use WebGraphicsContext3D in rate limiting logic inside compositor
[WebKit.git] / Source / WebCore / platform / graphics / chromium / TextureLayerChromium.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #if USE(ACCELERATED_COMPOSITING)
29
30 #include "TextureLayerChromium.h"
31
32 #include "Extensions3D.h"
33 #include "GraphicsContext3DPrivate.h"
34 #include "cc/CCLayerTreeHost.h"
35 #include "cc/CCTextureLayerImpl.h"
36
37 namespace WebCore {
38
39 PassRefPtr<TextureLayerChromium> TextureLayerChromium::create(TextureLayerChromiumClient* client)
40 {
41     return adoptRef(new TextureLayerChromium(client));
42 }
43
44 TextureLayerChromium::TextureLayerChromium(TextureLayerChromiumClient* client)
45     : LayerChromium()
46     , m_client(client)
47     , m_flipped(true)
48     , m_uvRect(0, 0, 1, 1)
49     , m_premultipliedAlpha(true)
50     , m_rateLimitContext(false)
51     , m_contextLost(false)
52     , m_textureId(0)
53 {
54 }
55
56 TextureLayerChromium::~TextureLayerChromium()
57 {
58     if (layerTreeHost()) {
59         if (m_textureId)
60             layerTreeHost()->acquireLayerTextures();
61         if (m_rateLimitContext && m_client)
62             layerTreeHost()->stopRateLimiter(GraphicsContext3DPrivate::extractWebGraphicsContext3D(m_client->context()));
63     }
64 }
65
66 PassOwnPtr<CCLayerImpl> TextureLayerChromium::createCCLayerImpl()
67 {
68     return CCTextureLayerImpl::create(m_layerId);
69 }
70
71 void TextureLayerChromium::setFlipped(bool flipped)
72 {
73     m_flipped = flipped;
74     setNeedsCommit();
75 }
76
77 void TextureLayerChromium::setUVRect(const FloatRect& rect)
78 {
79     m_uvRect = rect;
80     setNeedsCommit();
81 }
82
83 void TextureLayerChromium::setPremultipliedAlpha(bool premultipliedAlpha)
84 {
85     m_premultipliedAlpha = premultipliedAlpha;
86     setNeedsCommit();
87 }
88
89 void TextureLayerChromium::setRateLimitContext(bool rateLimit)
90 {
91     if (!rateLimit && m_rateLimitContext && m_client && layerTreeHost())
92         layerTreeHost()->stopRateLimiter(GraphicsContext3DPrivate::extractWebGraphicsContext3D(m_client->context()));
93
94     m_rateLimitContext = rateLimit;
95 }
96
97 void TextureLayerChromium::setTextureId(unsigned id)
98 {
99     if (m_textureId == id)
100         return;
101     if (m_textureId && layerTreeHost())
102         layerTreeHost()->acquireLayerTextures();
103     m_textureId = id;
104     setNeedsCommit();
105 }
106
107 void TextureLayerChromium::willModifyTexture()
108 {
109     if (layerTreeHost())
110         layerTreeHost()->acquireLayerTextures();
111 }
112
113 void TextureLayerChromium::setNeedsDisplayRect(const FloatRect& dirtyRect)
114 {
115     LayerChromium::setNeedsDisplayRect(dirtyRect);
116
117     if (m_rateLimitContext && m_client && layerTreeHost())
118         layerTreeHost()->startRateLimiter(GraphicsContext3DPrivate::extractWebGraphicsContext3D(m_client->context()));
119 }
120
121 void TextureLayerChromium::setLayerTreeHost(CCLayerTreeHost* host)
122 {
123     if (m_textureId && layerTreeHost() && host != layerTreeHost())
124         layerTreeHost()->acquireLayerTextures();
125     LayerChromium::setLayerTreeHost(host);
126 }
127
128 bool TextureLayerChromium::drawsContent() const
129 {
130     return (m_client || m_textureId) && !m_contextLost && LayerChromium::drawsContent();
131 }
132
133 void TextureLayerChromium::update(CCTextureUpdater& updater, const CCOcclusionTracker*)
134 {
135     if (m_client) {
136         m_textureId = m_client->prepareTexture(updater);
137         m_contextLost = m_client->context()->getExtensions()->getGraphicsResetStatusARB() != GraphicsContext3D::NO_ERROR;
138     }
139
140     m_needsDisplay = false;
141 }
142
143 void TextureLayerChromium::pushPropertiesTo(CCLayerImpl* layer)
144 {
145     LayerChromium::pushPropertiesTo(layer);
146
147     CCTextureLayerImpl* textureLayer = static_cast<CCTextureLayerImpl*>(layer);
148     textureLayer->setFlipped(m_flipped);
149     textureLayer->setUVRect(m_uvRect);
150     textureLayer->setPremultipliedAlpha(m_premultipliedAlpha);
151     textureLayer->setTextureId(m_textureId);
152 }
153
154 }
155 #endif // USE(ACCELERATED_COMPOSITING)