1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Mozilla Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2009
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Vladimir Vukicevic <vladimir@pobox.com> (original author)
24 : * Mark Steele <mwsteele@gmail.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include <stdarg.h>
41 :
42 : #include "WebGLContext.h"
43 :
44 : #include "prprf.h"
45 :
46 : #include "nsIJSContextStack.h"
47 : #include "jsapi.h"
48 : #include "nsIScriptSecurityManager.h"
49 : #include "nsServiceManagerUtils.h"
50 : #include "nsIVariant.h"
51 :
52 : #include "nsIDOMDocument.h"
53 : #include "nsIDOMEvent.h"
54 : #include "nsIDOMEventTarget.h"
55 : #include "nsIPrivateDOMEvent.h"
56 : #include "nsIDOMDataContainerEvent.h"
57 :
58 : #include "nsContentUtils.h"
59 : #include "mozilla/Preferences.h"
60 :
61 : #if 0
62 : #include "nsIContentURIGrouper.h"
63 : #include "nsIContentPrefService.h"
64 : #endif
65 :
66 : using namespace mozilla;
67 :
68 : void
69 0 : WebGLContext::LogMessage(const char *fmt, ...)
70 : {
71 : va_list ap;
72 0 : va_start(ap, fmt);
73 :
74 0 : LogMessage(fmt, ap);
75 :
76 0 : va_end(ap);
77 0 : }
78 :
79 : void
80 0 : WebGLContext::LogMessage(const char *fmt, va_list ap)
81 : {
82 0 : if (!fmt) return;
83 :
84 : char buf[1024];
85 0 : PR_vsnprintf(buf, 1024, fmt, ap);
86 :
87 : // no need to print to stderr, as JS_ReportWarning takes care of this for us.
88 :
89 0 : nsCOMPtr<nsIJSContextStack> stack = do_GetService("@mozilla.org/js/xpc/ContextStack;1");
90 0 : JSContext* ccx = nsnull;
91 0 : if (stack && NS_SUCCEEDED(stack->Peek(&ccx)) && ccx)
92 0 : JS_ReportWarning(ccx, "WebGL: %s", buf);
93 : }
94 :
95 : void
96 0 : WebGLContext::LogMessageIfVerbose(const char *fmt, ...)
97 : {
98 : va_list ap;
99 0 : va_start(ap, fmt);
100 :
101 0 : LogMessageIfVerbose(fmt, ap);
102 :
103 0 : va_end(ap);
104 0 : }
105 :
106 : void
107 0 : WebGLContext::LogMessageIfVerbose(const char *fmt, va_list ap)
108 : {
109 : static bool firstTime = true;
110 :
111 0 : if (mVerbose)
112 0 : LogMessage(fmt, ap);
113 0 : else if (firstTime)
114 : LogMessage("There are WebGL warnings or messages in this page, but they are hidden. To see them, "
115 0 : "go to about:config, set the webgl.verbose preference, and reload this page.");
116 :
117 0 : firstTime = false;
118 0 : }
119 :
120 : CheckedUint32
121 0 : WebGLContext::GetImageSize(WebGLsizei height,
122 : WebGLsizei width,
123 : PRUint32 pixelSize,
124 : PRUint32 packOrUnpackAlignment)
125 : {
126 0 : CheckedUint32 checked_plainRowSize = CheckedUint32(width) * pixelSize;
127 :
128 : // alignedRowSize = row size rounded up to next multiple of packAlignment
129 0 : CheckedUint32 checked_alignedRowSize = RoundedToNextMultipleOf(checked_plainRowSize, packOrUnpackAlignment);
130 :
131 : // if height is 0, we don't need any memory to store this; without this check, we'll get an overflow
132 : CheckedUint32 checked_neededByteLength
133 0 : = height <= 0 ? 0 : (height-1) * checked_alignedRowSize + checked_plainRowSize;
134 :
135 : return checked_neededByteLength;
136 : }
137 :
138 : nsresult
139 0 : WebGLContext::SynthesizeGLError(WebGLenum err)
140 : {
141 : // If there is already a pending error, don't overwrite it;
142 : // but if there isn't, then we need to check for a gl error
143 : // that may have occurred before this one and use that code
144 : // instead.
145 :
146 0 : MakeContextCurrent();
147 :
148 0 : UpdateWebGLErrorAndClearGLError();
149 :
150 0 : if (!mWebGLError)
151 0 : mWebGLError = err;
152 :
153 0 : return NS_OK;
154 : }
155 :
156 : nsresult
157 0 : WebGLContext::SynthesizeGLError(WebGLenum err, const char *fmt, ...)
158 : {
159 : va_list va;
160 0 : va_start(va, fmt);
161 0 : LogMessageIfVerbose(fmt, va);
162 0 : va_end(va);
163 :
164 0 : return SynthesizeGLError(err);
165 : }
166 :
167 : nsresult
168 0 : WebGLContext::ErrorInvalidEnum(const char *fmt, ...)
169 : {
170 : va_list va;
171 0 : va_start(va, fmt);
172 0 : LogMessageIfVerbose(fmt, va);
173 0 : va_end(va);
174 :
175 0 : return SynthesizeGLError(LOCAL_GL_INVALID_ENUM);
176 : }
177 :
178 : nsresult
179 0 : WebGLContext::ErrorInvalidOperation(const char *fmt, ...)
180 : {
181 : va_list va;
182 0 : va_start(va, fmt);
183 0 : LogMessageIfVerbose(fmt, va);
184 0 : va_end(va);
185 :
186 0 : return SynthesizeGLError(LOCAL_GL_INVALID_OPERATION);
187 : }
188 :
189 : nsresult
190 0 : WebGLContext::ErrorInvalidValue(const char *fmt, ...)
191 : {
192 : va_list va;
193 0 : va_start(va, fmt);
194 0 : LogMessageIfVerbose(fmt, va);
195 0 : va_end(va);
196 :
197 0 : return SynthesizeGLError(LOCAL_GL_INVALID_VALUE);
198 : }
199 :
200 : nsresult
201 0 : WebGLContext::ErrorInvalidFramebufferOperation(const char *fmt, ...)
202 : {
203 : va_list va;
204 0 : va_start(va, fmt);
205 0 : LogMessageIfVerbose(fmt, va);
206 0 : va_end(va);
207 :
208 0 : return SynthesizeGLError(LOCAL_GL_INVALID_FRAMEBUFFER_OPERATION);
209 : }
210 :
211 : nsresult
212 0 : WebGLContext::ErrorOutOfMemory(const char *fmt, ...)
213 : {
214 : va_list va;
215 0 : va_start(va, fmt);
216 0 : LogMessageIfVerbose(fmt, va);
217 0 : va_end(va);
218 :
219 0 : return SynthesizeGLError(LOCAL_GL_OUT_OF_MEMORY);
220 : }
221 :
222 : const char *
223 0 : WebGLContext::ErrorName(GLenum error)
224 : {
225 0 : switch(error) {
226 : case LOCAL_GL_INVALID_ENUM:
227 0 : return "INVALID_ENUM";
228 : case LOCAL_GL_INVALID_OPERATION:
229 0 : return "INVALID_OPERATION";
230 : case LOCAL_GL_INVALID_VALUE:
231 0 : return "INVALID_VALUE";
232 : case LOCAL_GL_OUT_OF_MEMORY:
233 0 : return "OUT_OF_MEMORY";
234 : case LOCAL_GL_INVALID_FRAMEBUFFER_OPERATION:
235 0 : return "INVALID_FRAMEBUFFER_OPERATION";
236 : case LOCAL_GL_NO_ERROR:
237 0 : return "NO_ERROR";
238 : default:
239 0 : NS_ABORT();
240 0 : return "[unknown WebGL error!]";
241 : }
242 : };
|