Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / canvas / WebGLProgram.cpp
1 /*
2  * Copyright (C) 2009 Apple 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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #include "core/html/canvas/WebGLProgram.h"
29
30 #include "core/html/canvas/WebGLContextGroup.h"
31 #include "core/html/canvas/WebGLRenderingContextBase.h"
32
33 namespace blink {
34
35 PassRefPtrWillBeRawPtr<WebGLProgram> WebGLProgram::create(WebGLRenderingContextBase* ctx)
36 {
37     return adoptRefWillBeNoop(new WebGLProgram(ctx));
38 }
39
40 WebGLProgram::WebGLProgram(WebGLRenderingContextBase* ctx)
41     : WebGLSharedObject(ctx)
42     , m_linkStatus(false)
43     , m_linkCount(0)
44     , m_infoValid(true)
45 {
46     ScriptWrappable::init(this);
47     setObject(ctx->webContext()->createProgram());
48 }
49
50 WebGLProgram::~WebGLProgram()
51 {
52 #if ENABLE(OILPAN)
53     // These heap objects handle detachment on their own. Clear out
54     // the references to prevent deleteObjectImpl() from trying to do
55     // same, as we cannot safely access other heap objects from this
56     // destructor.
57     m_vertexShader = nullptr;
58     m_fragmentShader = nullptr;
59 #endif
60     // Always call detach here to ensure that platform object deletion
61     // happens with Oilpan enabled. It keeps the code regular to do it
62     // with or without Oilpan enabled.
63     //
64     // See comment in WebGLBuffer's destructor for additional
65     // information on why this is done for WebGLSharedObject-derived
66     // objects.
67     detachAndDeleteObject();
68 }
69
70 void WebGLProgram::deleteObjectImpl(blink::WebGraphicsContext3D* context3d, Platform3DObject obj)
71 {
72     context3d->deleteProgram(obj);
73     if (m_vertexShader) {
74         m_vertexShader->onDetached(context3d);
75         m_vertexShader = nullptr;
76     }
77     if (m_fragmentShader) {
78         m_fragmentShader->onDetached(context3d);
79         m_fragmentShader = nullptr;
80     }
81 }
82
83 unsigned WebGLProgram::numActiveAttribLocations()
84 {
85     cacheInfoIfNeeded();
86     return m_activeAttribLocations.size();
87 }
88
89 GLint WebGLProgram::getActiveAttribLocation(GLuint index)
90 {
91     cacheInfoIfNeeded();
92     if (index >= numActiveAttribLocations())
93         return -1;
94     return m_activeAttribLocations[index];
95 }
96
97 bool WebGLProgram::isUsingVertexAttrib0()
98 {
99     cacheInfoIfNeeded();
100     for (unsigned ii = 0; ii < numActiveAttribLocations(); ++ii) {
101         if (!getActiveAttribLocation(ii))
102             return true;
103     }
104     return false;
105 }
106
107 bool WebGLProgram::linkStatus()
108 {
109     cacheInfoIfNeeded();
110     return m_linkStatus;
111 }
112
113 void WebGLProgram::increaseLinkCount()
114 {
115     ++m_linkCount;
116     m_infoValid = false;
117 }
118
119 WebGLShader* WebGLProgram::getAttachedShader(GLenum type)
120 {
121     switch (type) {
122     case GL_VERTEX_SHADER:
123         return m_vertexShader.get();
124     case GL_FRAGMENT_SHADER:
125         return m_fragmentShader.get();
126     default:
127         return 0;
128     }
129 }
130
131 bool WebGLProgram::attachShader(WebGLShader* shader)
132 {
133     if (!shader || !shader->object())
134         return false;
135     switch (shader->type()) {
136     case GL_VERTEX_SHADER:
137         if (m_vertexShader)
138             return false;
139         m_vertexShader = shader;
140         return true;
141     case GL_FRAGMENT_SHADER:
142         if (m_fragmentShader)
143             return false;
144         m_fragmentShader = shader;
145         return true;
146     default:
147         return false;
148     }
149 }
150
151 bool WebGLProgram::detachShader(WebGLShader* shader)
152 {
153     if (!shader || !shader->object())
154         return false;
155     switch (shader->type()) {
156     case GL_VERTEX_SHADER:
157         if (m_vertexShader != shader)
158             return false;
159         m_vertexShader = nullptr;
160         return true;
161     case GL_FRAGMENT_SHADER:
162         if (m_fragmentShader != shader)
163             return false;
164         m_fragmentShader = nullptr;
165         return true;
166     default:
167         return false;
168     }
169 }
170
171 void WebGLProgram::cacheActiveAttribLocations(blink::WebGraphicsContext3D* context3d)
172 {
173     m_activeAttribLocations.clear();
174
175     GLint numAttribs = 0;
176     context3d->getProgramiv(object(), GL_ACTIVE_ATTRIBUTES, &numAttribs);
177     m_activeAttribLocations.resize(static_cast<size_t>(numAttribs));
178     for (int i = 0; i < numAttribs; ++i) {
179         blink::WebGraphicsContext3D::ActiveInfo info;
180         context3d->getActiveAttrib(object(), i, info);
181         m_activeAttribLocations[i] = context3d->getAttribLocation(object(), info.name.utf8().data());
182     }
183 }
184
185 void WebGLProgram::cacheInfoIfNeeded()
186 {
187     if (m_infoValid)
188         return;
189
190     if (!object())
191         return;
192
193     if (!contextGroup())
194         return;
195     blink::WebGraphicsContext3D* context = contextGroup()->getAWebGraphicsContext3D();
196     if (!context)
197         return;
198     GLint linkStatus = 0;
199     context->getProgramiv(object(), GL_LINK_STATUS, &linkStatus);
200     m_linkStatus = linkStatus;
201     if (m_linkStatus)
202         cacheActiveAttribLocations(context);
203     m_infoValid = true;
204 }
205
206 void WebGLProgram::trace(Visitor* visitor)
207 {
208     visitor->trace(m_vertexShader);
209     visitor->trace(m_fragmentShader);
210     WebGLSharedObject::trace(visitor);
211 }
212
213 }