[chromium] Use TextureLayerChromium for WebGL content instead of a dedicated layer...
[WebKit.git] / Source / WebCore / platform / graphics / chromium / cc / CCTextureLayerImpl.cpp
1 /*
2  * Copyright (C) 2011 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 "cc/CCTextureLayerImpl.h"
31
32 #include "Extensions3DChromium.h"
33 #include "GraphicsContext3D.h"
34 #include "LayerRendererChromium.h"
35 #include "cc/CCProxy.h"
36 #include "cc/CCQuadCuller.h"
37 #include "cc/CCTextureDrawQuad.h"
38
39 namespace WebCore {
40
41 CCTextureLayerImpl::CCTextureLayerImpl(int id)
42     : CCLayerImpl(id)
43     , m_textureId(0)
44     , m_premultipliedAlpha(true)
45     , m_flipped(true)
46     , m_uvRect(0, 0, 1, 1)
47     , m_ioSurfaceId(0)
48     , m_ioSurfaceChanged(false)
49     , m_ioSurfaceTextureId(0)
50 {
51 }
52
53 CCTextureLayerImpl::~CCTextureLayerImpl()
54 {
55     // FIXME: it seems there is no layer renderer / GraphicsContext3D available here. Ideally we
56     // would like to delete m_ioSurfaceTextureId.
57     m_ioSurfaceTextureId = 0;
58 }
59
60 void CCTextureLayerImpl::willDraw(LayerRendererChromium* layerRenderer)
61 {
62     if (m_ioSurfaceChanged) {
63         GraphicsContext3D* context = layerRenderer->context();
64         Extensions3DChromium* extensions = static_cast<Extensions3DChromium*>(context->getExtensions());
65         ASSERT(extensions->supports("GL_CHROMIUM_iosurface"));
66         ASSERT(extensions->supports("GL_ARB_texture_rectangle"));
67
68         if (!m_ioSurfaceTextureId)
69             m_ioSurfaceTextureId = context->createTexture();
70
71         GLC(context, context->activeTexture(GraphicsContext3D::TEXTURE0));
72         GLC(context, context->bindTexture(Extensions3D::TEXTURE_RECTANGLE_ARB, m_ioSurfaceTextureId));
73         GLC(context, context->texParameteri(Extensions3D::TEXTURE_RECTANGLE_ARB, GraphicsContext3D::TEXTURE_MIN_FILTER, GraphicsContext3D::LINEAR));
74         GLC(context, context->texParameteri(Extensions3D::TEXTURE_RECTANGLE_ARB, GraphicsContext3D::TEXTURE_MAG_FILTER, GraphicsContext3D::LINEAR));
75         GLC(context, context->texParameteri(Extensions3D::TEXTURE_RECTANGLE_ARB, GraphicsContext3D::TEXTURE_WRAP_S, GraphicsContext3D::CLAMP_TO_EDGE));
76         GLC(context, context->texParameteri(Extensions3D::TEXTURE_RECTANGLE_ARB, GraphicsContext3D::TEXTURE_WRAP_T, GraphicsContext3D::CLAMP_TO_EDGE));
77         extensions->texImageIOSurface2DCHROMIUM(Extensions3D::TEXTURE_RECTANGLE_ARB,
78                                                 m_ioSurfaceSize.width(),
79                                                 m_ioSurfaceSize.height(),
80                                                 m_ioSurfaceId,
81                                                 0);
82         // Do not check for error conditions. texImageIOSurface2DCHROMIUM is supposed to hold on to
83         // the last good IOSurface if the new one is already closed. This is only a possibility
84         // during live resizing of plugins. However, it seems that this is not sufficient to
85         // completely guard against garbage being drawn. If this is found to be a significant issue,
86         // it may be necessary to explicitly tell the embedder when to free the surfaces it has
87         // allocated.
88         m_ioSurfaceChanged = false;
89     }
90 }
91
92 void CCTextureLayerImpl::appendQuads(CCQuadCuller& quadList, const CCSharedQuadState* sharedQuadState, bool&)
93 {
94     IntRect quadRect(IntPoint(), bounds());
95     quadList.append(CCTextureDrawQuad::create(sharedQuadState, quadRect, m_textureId, m_premultipliedAlpha, m_uvRect, m_flipped, m_ioSurfaceSize, m_ioSurfaceTextureId));
96 }
97
98 void CCTextureLayerImpl::dumpLayerProperties(TextStream& ts, int indent) const
99 {
100     writeIndent(ts, indent);
101     ts << "texture layer texture id: " << m_textureId << " premultiplied: " << m_premultipliedAlpha << "\n";
102     CCLayerImpl::dumpLayerProperties(ts, indent);
103 }
104
105 void CCTextureLayerImpl::didLoseContext()
106 {
107     m_textureId = 0;
108     if (m_ioSurfaceId) {
109         // We don't have a valid texture ID in the new context; however,
110         // the IOSurface is still valid.
111         m_ioSurfaceTextureId = 0;
112         m_ioSurfaceChanged = true;
113     }
114 }
115
116 void CCTextureLayerImpl::setIOSurfaceProperties(const IntSize& size, unsigned ioSurfaceId)
117 {
118     if (m_ioSurfaceId != ioSurfaceId)
119         m_ioSurfaceChanged = true;
120
121     m_ioSurfaceSize = size;
122     m_ioSurfaceId = ioSurfaceId;
123 }
124 }
125
126 #endif // USE(ACCELERATED_COMPOSITING)