Home | History | Annotate | Download | only in chromium
      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 are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 
     33 #if USE(ACCELERATED_COMPOSITING)
     34 
     35 #include "WebGLLayerChromium.h"
     36 
     37 #include "GraphicsContext3D.h"
     38 #include "LayerRendererChromium.h"
     39 
     40 namespace WebCore {
     41 
     42 PassRefPtr<WebGLLayerChromium> WebGLLayerChromium::create(GraphicsLayerChromium* owner)
     43 {
     44     return adoptRef(new WebGLLayerChromium(owner));
     45 }
     46 
     47 WebGLLayerChromium::WebGLLayerChromium(GraphicsLayerChromium* owner)
     48     : CanvasLayerChromium(owner)
     49     , m_context(0)
     50     , m_textureUpdated(false)
     51 {
     52 }
     53 
     54 WebGLLayerChromium::~WebGLLayerChromium()
     55 {
     56     if (m_context && layerRenderer())
     57         layerRenderer()->removeChildContext(m_context);
     58 }
     59 
     60 void WebGLLayerChromium::updateCompositorResources()
     61 {
     62     if (!m_contentsDirty)
     63         return;
     64 
     65     GraphicsContext3D* rendererContext = layerRendererContext();
     66     ASSERT(m_context);
     67     if (m_textureChanged) {
     68         rendererContext->bindTexture(GraphicsContext3D::TEXTURE_2D, m_textureId);
     69         // Set the min-mag filters to linear and wrap modes to GL_CLAMP_TO_EDGE
     70         // to get around NPOT texture limitations of GLES.
     71         rendererContext->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_MIN_FILTER, GraphicsContext3D::LINEAR);
     72         rendererContext->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_MAG_FILTER, GraphicsContext3D::LINEAR);
     73         rendererContext->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_WRAP_S, GraphicsContext3D::CLAMP_TO_EDGE);
     74         rendererContext->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_WRAP_T, GraphicsContext3D::CLAMP_TO_EDGE);
     75         m_textureChanged = false;
     76     }
     77     // Update the contents of the texture used by the compositor.
     78     if (m_contentsDirty && m_textureUpdated) {
     79         // prepareTexture copies the contents of the off-screen render target into the texture
     80         // used by the compositor.
     81         //
     82         m_context->prepareTexture();
     83         m_context->markLayerComposited();
     84         m_contentsDirty = false;
     85         m_textureUpdated = false;
     86     }
     87 }
     88 
     89 void WebGLLayerChromium::setTextureUpdated()
     90 {
     91     m_textureUpdated = true;
     92 }
     93 
     94 void WebGLLayerChromium::setContext(const GraphicsContext3D* context)
     95 {
     96     if (m_context != context && layerRenderer()) {
     97         if (m_context)
     98             layerRenderer()->removeChildContext(m_context);
     99         if (context)
    100             layerRenderer()->addChildContext(const_cast<GraphicsContext3D*>(context));
    101     }
    102 
    103     m_context = const_cast<GraphicsContext3D*>(context);
    104 
    105     unsigned int textureId = m_context->platformTexture();
    106     if (textureId != m_textureId) {
    107         m_textureChanged = true;
    108         m_textureUpdated = true;
    109     }
    110     m_textureId = textureId;
    111     m_premultipliedAlpha = m_context->getContextAttributes().premultipliedAlpha;
    112 }
    113 
    114 void WebGLLayerChromium::setLayerRenderer(LayerRendererChromium* newLayerRenderer)
    115 {
    116     if (layerRenderer() != newLayerRenderer) {
    117         if (m_context) {
    118             if (layerRenderer())
    119                 layerRenderer()->removeChildContext(m_context);
    120             if (newLayerRenderer)
    121                 newLayerRenderer->addChildContext(m_context);
    122         }
    123 
    124         LayerChromium::setLayerRenderer(newLayerRenderer);
    125     }
    126 }
    127 
    128 }
    129 #endif // USE(ACCELERATED_COMPOSITING)
    130