Sync with impl thread when removing references to external textures
[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 "cc/CCLayerTreeHost.h"
34 #include "cc/CCTextureLayerImpl.h"
35
36 namespace WebCore {
37
38 PassRefPtr<TextureLayerChromium> TextureLayerChromium::create(TextureLayerChromiumClient* client)
39 {
40     return adoptRef(new TextureLayerChromium(client));
41 }
42
43 TextureLayerChromium::TextureLayerChromium(TextureLayerChromiumClient* client)
44     : LayerChromium()
45     , m_client(client)
46     , m_flipped(true)
47     , m_uvRect(0, 0, 1, 1)
48     , m_premultipliedAlpha(true)
49     , m_rateLimitContext(false)
50     , m_contextLost(false)
51     , m_textureId(0)
52 {
53 }
54
55 TextureLayerChromium::~TextureLayerChromium()
56 {
57     if (layerTreeHost()) {
58         if (m_textureId)
59             layerTreeHost()->acquireLayerTextures();
60         if (m_rateLimitContext && m_client)
61             layerTreeHost()->stopRateLimiter(m_client->context());
62     }
63 }
64
65 PassOwnPtr<CCLayerImpl> TextureLayerChromium::createCCLayerImpl()
66 {
67     return CCTextureLayerImpl::create(m_layerId);
68 }
69
70 void TextureLayerChromium::setFlipped(bool flipped)
71 {
72     m_flipped = flipped;
73     setNeedsCommit();
74 }
75
76 void TextureLayerChromium::setUVRect(const FloatRect& rect)
77 {
78     m_uvRect = rect;
79     setNeedsCommit();
80 }
81
82 void TextureLayerChromium::setPremultipliedAlpha(bool premultipliedAlpha)
83 {
84     m_premultipliedAlpha = premultipliedAlpha;
85     setNeedsCommit();
86 }
87
88 void TextureLayerChromium::setRateLimitContext(bool rateLimit)
89 {
90     if (!rateLimit && m_rateLimitContext && m_client && layerTreeHost())
91         layerTreeHost()->stopRateLimiter(m_client->context());
92
93     m_rateLimitContext = rateLimit;
94 }
95
96 void TextureLayerChromium::setTextureId(unsigned id)
97 {
98     if (m_textureId == id)
99         return;
100     if (m_textureId && layerTreeHost())
101         layerTreeHost()->acquireLayerTextures();
102     m_textureId = id;
103     setNeedsCommit();
104 }
105
106 void TextureLayerChromium::setNeedsDisplayRect(const FloatRect& dirtyRect)
107 {
108     LayerChromium::setNeedsDisplayRect(dirtyRect);
109
110     if (m_rateLimitContext && m_client && layerTreeHost())
111         layerTreeHost()->startRateLimiter(m_client->context());
112 }
113
114 void TextureLayerChromium::setLayerTreeHost(CCLayerTreeHost* host)
115 {
116     if (m_textureId && layerTreeHost() && host != layerTreeHost())
117         layerTreeHost()->acquireLayerTextures();
118     LayerChromium::setLayerTreeHost(host);
119 }
120
121 bool TextureLayerChromium::drawsContent() const
122 {
123     return (m_client || m_textureId) && !m_contextLost && LayerChromium::drawsContent();
124 }
125
126 void TextureLayerChromium::update(CCTextureUpdater& updater, const CCOcclusionTracker*)
127 {
128     if (m_client) {
129         m_textureId = m_client->prepareTexture(updater);
130         m_contextLost = m_client->context()->getExtensions()->getGraphicsResetStatusARB() != GraphicsContext3D::NO_ERROR;
131     }
132
133     m_needsDisplay = false;
134 }
135
136 void TextureLayerChromium::pushPropertiesTo(CCLayerImpl* layer)
137 {
138     LayerChromium::pushPropertiesTo(layer);
139
140     CCTextureLayerImpl* textureLayer = static_cast<CCTextureLayerImpl*>(layer);
141     textureLayer->setFlipped(m_flipped);
142     textureLayer->setUVRect(m_uvRect);
143     textureLayer->setPremultipliedAlpha(m_premultipliedAlpha);
144     textureLayer->setTextureId(m_textureId);
145 }
146
147 }
148 #endif // USE(ACCELERATED_COMPOSITING)