1/*
2 * Copyright (C) 2012 Research In Motion Limited. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19#include "config.h"
20#include "EGLImageLayerCompositingThreadClient.h"
21
22#if USE(ACCELERATED_COMPOSITING)
23
24#include "LayerCompositingThread.h"
25#include <BlackBerryPlatformGLES2Program.h>
26#include <BlackBerryPlatformGLES2SharedTexture.h>
27#include <BlackBerryPlatformGraphics.h>
28#include <BlackBerryPlatformLog.h>
29#include <EGL/egl.h>
30#include <EGL/eglext.h>
31#include <GLES2/gl2.h>
32#include <GLES2/gl2ext.h>
33
34using namespace BlackBerry::Platform::Graphics;
35
36namespace WebCore {
37
38EGLImageLayerCompositingThreadClient::~EGLImageLayerCompositingThreadClient()
39{
40    // Someone should have called deleteTextures() by now
41    ASSERT(!m_textureAccessor);
42}
43
44void EGLImageLayerCompositingThreadClient::uploadTexturesIfNeeded(LayerCompositingThread*)
45{
46}
47
48void EGLImageLayerCompositingThreadClient::drawTextures(LayerCompositingThread* layer, const GLES2Program& program, double /*scale*/, const FloatRect& /*clipRect*/)
49{
50    if (!m_textureAccessor || !m_textureAccessor->textureID())
51        return;
52
53    glEnable(GL_BLEND);
54    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
55    glUseProgram(program.m_program);
56    glUniform1f(program.opacityLocation(), layer->drawOpacity());
57    glVertexAttribPointer(program.positionLocation(), 2, GL_FLOAT, GL_FALSE, 0, layer->transformedBounds().data());
58    glVertexAttribPointer(program.texCoordLocation(), 2, GL_FLOAT, GL_FALSE, 0, layer->textureCoordinates(LayerCompositingThread::UpsideDown).data());
59    glBindTexture(GL_TEXTURE_2D, m_textureAccessor->textureID());
60    glDrawArrays(GL_TRIANGLE_FAN, 0, layer->transformedBounds().size());
61}
62
63void EGLImageLayerCompositingThreadClient::deleteTextures(LayerCompositingThread*)
64{
65    delete m_textureAccessor;
66    m_textureAccessor = 0;
67}
68
69void EGLImageLayerCompositingThreadClient::bindContentsTexture(LayerCompositingThread*)
70{
71    if (m_textureAccessor)
72        glBindTexture(GL_TEXTURE_2D, m_textureAccessor->textureID());
73}
74
75void EGLImageLayerCompositingThreadClient::setTextureAccessor(GLES2SharedTextureAccessor* textureAccessor)
76{
77    if (m_textureAccessor == textureAccessor)
78        return;
79
80    delete m_textureAccessor;
81    m_textureAccessor = textureAccessor;
82}
83
84} // namespace WebCore
85
86#endif // USE(ACCELERATED_COMPOSITING)
87