initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / chromium / ProgramBinding.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 "ProgramBinding.h"
31
32 #include "GeometryBinding.h"
33 #include "GraphicsContext.h"
34 #include "GraphicsContext3D.h"
35 #include "LayerRendererChromium.h"
36 #include "TraceEvent.h"
37
38 namespace WebCore {
39
40 ProgramBindingBase::ProgramBindingBase()
41     : m_program(0)
42     , m_initialized(false)
43 {
44 }
45
46 ProgramBindingBase::~ProgramBindingBase()
47 {
48     // If you hit these asserts, you initialized but forgot to call cleanup().
49     ASSERT(!m_program);
50     ASSERT(!m_initialized);
51 }
52
53 void ProgramBindingBase::init(GraphicsContext3D* context, const String& vertexShader, const String& fragmentShader)
54 {
55     m_program = createShaderProgram(context, vertexShader, fragmentShader);
56     ASSERT(m_program);
57 }
58
59 void ProgramBindingBase::cleanup(GraphicsContext3D* context)
60 {
61     m_initialized = false;
62     if (!m_program)
63         return;
64
65     ASSERT(context);
66     GLC(context, context->deleteProgram(m_program));
67     m_program = 0;
68 }
69
70 unsigned ProgramBindingBase::loadShader(GraphicsContext3D* context, unsigned type, const String& shaderSource)
71 {
72     unsigned shader = context->createShader(type);
73     if (!shader)
74         return 0;
75     String sourceString(shaderSource);
76     GLC(context, context->shaderSource(shader, sourceString));
77     GLC(context, context->compileShader(shader));
78 #ifndef NDEBUG
79     int compiled = 0;
80     GLC(context, context->getShaderiv(shader, GraphicsContext3D::COMPILE_STATUS, &compiled));
81     if (!compiled) {
82         GLC(context, context->deleteShader(shader));
83         return 0;
84     }
85 #endif
86     return shader;
87 }
88
89 unsigned ProgramBindingBase::createShaderProgram(GraphicsContext3D* context, const String& vertexShaderSource, const String& fragmentShaderSource)
90 {
91     TRACE_EVENT("ProgramBindingBase::createShaderProgram", this, 0);
92     unsigned vertexShader = loadShader(context, GraphicsContext3D::VERTEX_SHADER, vertexShaderSource);
93     if (!vertexShader) {
94         LOG_ERROR("Failed to create vertex shader");
95         return 0;
96     }
97
98     unsigned fragmentShader = loadShader(context, GraphicsContext3D::FRAGMENT_SHADER, fragmentShaderSource);
99     if (!fragmentShader) {
100         GLC(context, context->deleteShader(vertexShader));
101         LOG_ERROR("Failed to create fragment shader");
102         return 0;
103     }
104
105     unsigned programObject = context->createProgram();
106     if (!programObject) {
107         LOG_ERROR("Failed to create shader program");
108         return 0;
109     }
110
111     GLC(context, context->attachShader(programObject, vertexShader));
112     GLC(context, context->attachShader(programObject, fragmentShader));
113
114     // Bind the common attrib locations.
115     GLC(context, context->bindAttribLocation(programObject, GeometryBinding::positionAttribLocation(), "a_position"));
116     GLC(context, context->bindAttribLocation(programObject, GeometryBinding::texCoordAttribLocation(), "a_texCoord"));
117
118     GLC(context, context->linkProgram(programObject));
119 #ifndef NDEBUG
120     int linked = 0;
121     GLC(context, context->getProgramiv(programObject, GraphicsContext3D::LINK_STATUS, &linked));
122     if (!linked) {
123         LOG_ERROR("Failed to link shader program");
124         GLC(context, context->deleteProgram(programObject));
125         return 0;
126     }
127 #endif
128
129     GLC(context, context->deleteShader(vertexShader));
130     GLC(context, context->deleteShader(fragmentShader));
131     return programObject;
132 }
133
134 } // namespace WebCore
135
136 #endif // USE(ACCELERATED_COMPOSITING)