tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / efl / LayerShader.cpp
1 /*
2     Copyright (C) 2011 Samsung Electronics
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library 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     Library General Public License for more details.
13
14     You should have received a copy of the GNU Library General Public License
15     along with this library; see the file COPYING.LIB.  If not, write to
16     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21
22 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING)
23 #if USE(ACCELERATED_COMPOSITING)
24
25 #include "LayerShader.h"
26
27 #include "GraphicsContext3D.h"
28 #include "LayerManager.h"
29 #include "Logging.h"
30
31 #include <wtf/text/CString.h>
32 namespace WebCore {
33
34 LayerShader::LayerShader(GraphicsContext3D* context, unsigned program)
35     : m_context(context)
36     , m_program(program)
37 {
38 }
39
40 LayerShader::~LayerShader()
41 {
42     m_context->deleteProgram(m_program);
43 }
44
45 void LayerShader::use()
46 {
47     m_context->useProgram(m_program);
48 }
49
50 unsigned LayerShader::loadShader(GraphicsContext3D* context, unsigned type, const char* shaderSource)
51 {
52     unsigned shader = context->createShader(type);
53     if (!shader)
54         return 0;
55
56     String sourceString(shaderSource);
57     context->shaderSource(shader, sourceString);
58
59     context->compileShader(shader);
60
61     int compiled;
62     context->getShaderiv(shader, GraphicsContext3D::COMPILE_STATUS, &compiled);
63     if (!compiled) {
64         String infoLog = context->getShaderInfoLog(shader);
65         LOG_ERROR("%s", infoLog.utf8().data());
66
67         context->deleteShader(shader);
68         return 0;
69     }
70     return shader;
71 }
72
73 unsigned LayerShader::loadProgram(GraphicsContext3D* context, const char* vertexShaderSource, const char* fragmentShaderSource)
74 {
75     unsigned vertexShader = loadShader(context, GraphicsContext3D::VERTEX_SHADER, vertexShaderSource);
76     if (!vertexShader) {
77         LOG_ERROR("LayerShader::%s() - Failed to create vertex shader\n", __func__);
78         return 0;
79     }
80
81     unsigned fragmentShader = loadShader(context, GraphicsContext3D::FRAGMENT_SHADER, fragmentShaderSource);
82     if (!fragmentShader) {
83         context->deleteShader(vertexShader);
84         LOG_ERROR("LayerShader::%s() - Failed to create fragment shader\n", __func__);
85         return 0;
86     }
87
88     unsigned programObject = context->createProgram();
89     if (!programObject) {
90         LOG_ERROR("LayerShader::%s() - Failed to create shader program\n", __func__);
91         return 0;
92     }
93
94     context->attachShader(programObject, vertexShader);
95     context->attachShader(programObject, fragmentShader);
96
97     context->bindAttribLocation(programObject, LayerManager::s_positionAttribLocation, "attr_pos");
98     context->bindAttribLocation(programObject, LayerManager::s_texCoordAttribLocation, "attr_tex");
99
100     context->linkProgram(programObject);
101     int linked;
102     context->getProgramiv(programObject, GraphicsContext3D::LINK_STATUS, &linked);
103     if (!linked) {
104         LOG_ERROR("LayerShader::%s() - Failed to link shader program\n", __func__);
105         context->deleteProgram(programObject);
106         return 0;
107     }
108
109     context->deleteShader(vertexShader);
110     context->deleteShader(fragmentShader);
111     return programObject;
112 }
113
114 }
115
116 #endif
117 #endif