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