2010-04-23 Zhenyao Mo <zmo@google.com>
[WebKit-https.git] / WebKit / chromium / src / GraphicsContext3D.cpp
1 /*
2  * Copyright (C) 2009 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32
33 #if ENABLE(3D_CANVAS)
34
35 #include "GraphicsContext3D.h"
36
37 #include "CachedImage.h"
38 #include "HTMLCanvasElement.h"
39 #include "HTMLImageElement.h"
40 #include "ImageBuffer.h"
41 #include "ImageData.h"
42 #include "WebGLBuffer.h"
43 #include "WebGLByteArray.h"
44 #include "WebGLFloatArray.h"
45 #include "WebGLFramebuffer.h"
46 #include "WebGLIntArray.h"
47 #include "WebGLProgram.h"
48 #include "WebGLRenderbuffer.h"
49 #include "WebGLRenderingContext.h"
50 #include "WebGLShader.h"
51 #include "WebGLTexture.h"
52 #include "WebGLUnsignedByteArray.h"
53 #include "WebGraphicsContext3D.h"
54 #include "WebGraphicsContext3DDefaultImpl.h"
55 #include "WebKit.h"
56 #include "WebKitClient.h"
57
58 #include <stdio.h>
59 #include <wtf/FastMalloc.h>
60 #include <wtf/text/CString.h>
61
62 #if PLATFORM(CG)
63 #include "GraphicsContext.h"
64 #include <CoreGraphics/CGContext.h>
65 #include <CoreGraphics/CGImage.h>
66 #endif
67
68 // using namespace std;
69
70 // There are two levels of delegation in this file:
71 //
72 //   1. GraphicsContext3D delegates to GraphicsContext3DInternal. This is done
73 //      so that we have some place to store data members common among
74 //      implementations; GraphicsContext3D only provides us the m_internal
75 //      pointer. We always delegate to the GraphicsContext3DInternal. While we
76 //      could sidestep it and go directly to the WebGraphicsContext3D in some
77 //      cases, it is better for consistency to always delegate through it.
78 //
79 //   2. GraphicsContext3DInternal delegates to an implementation of
80 //      WebGraphicsContext3D. This is done so we have a place to inject an
81 //      implementation which remotes the OpenGL calls across processes.
82 //
83 // The legacy, in-process, implementation uses WebGraphicsContext3DDefaultImpl.
84
85 namespace WebCore {
86
87 //----------------------------------------------------------------------
88 // GraphicsContext3DInternal
89
90 // Uncomment this to render to a separate window for debugging
91 // #define RENDER_TO_DEBUGGING_WINDOW
92
93 #define EXTRACT(val) (!val ? 0 : val->object())
94
95 class GraphicsContext3DInternal {
96 public:
97     GraphicsContext3DInternal();
98     ~GraphicsContext3DInternal();
99
100     bool initialize(GraphicsContext3D::Attributes attrs);
101
102     PlatformGraphicsContext3D platformGraphicsContext3D() const;
103     Platform3DObject platformTexture() const;
104
105     bool makeContextCurrent();
106
107     int sizeInBytes(int type);
108
109     void reshape(int width, int height);
110
111     void beginPaint(WebGLRenderingContext* context);
112     void endPaint();
113
114     bool isGLES2Compliant() const;
115
116     //----------------------------------------------------------------------
117     // Entry points for WebGL.
118     //
119     void activeTexture(unsigned long texture);
120     void attachShader(WebGLProgram* program, WebGLShader* shader);
121     void bindAttribLocation(WebGLProgram*, unsigned long index, const String& name);
122     void bindBuffer(unsigned long target, WebGLBuffer*);
123     void bindFramebuffer(unsigned long target, WebGLFramebuffer*);
124     void bindRenderbuffer(unsigned long target, WebGLRenderbuffer*);
125     void bindTexture(unsigned long target, WebGLTexture* texture);
126     void blendColor(double red, double green, double blue, double alpha);
127     void blendEquation(unsigned long mode);
128     void blendEquationSeparate(unsigned long modeRGB, unsigned long modeAlpha);
129     void blendFunc(unsigned long sfactor, unsigned long dfactor);
130     void blendFuncSeparate(unsigned long srcRGB, unsigned long dstRGB, unsigned long srcAlpha, unsigned long dstAlpha);
131
132     void bufferData(unsigned long target, int size, unsigned long usage);
133     void bufferData(unsigned long target, WebGLArray* data, unsigned long usage);
134     void bufferSubData(unsigned long target, long offset, WebGLArray* data);
135
136     unsigned long checkFramebufferStatus(unsigned long target);
137     void clear(unsigned long mask);
138     void clearColor(double red, double green, double blue, double alpha);
139     void clearDepth(double depth);
140     void clearStencil(long s);
141     void colorMask(bool red, bool green, bool blue, bool alpha);
142     void compileShader(WebGLShader*);
143
144     void copyTexImage2D(unsigned long target, long level, unsigned long internalformat, long x, long y, unsigned long width, unsigned long height, long border);
145     void copyTexSubImage2D(unsigned long target, long level, long xoffset, long yoffset, long x, long y, unsigned long width, unsigned long height);
146     void cullFace(unsigned long mode);
147     void depthFunc(unsigned long func);
148     void depthMask(bool flag);
149     void depthRange(double zNear, double zFar);
150     void detachShader(WebGLProgram*, WebGLShader*);
151     void disable(unsigned long cap);
152     void disableVertexAttribArray(unsigned long index);
153     void drawArrays(unsigned long mode, long first, long count);
154     void drawElements(unsigned long mode, unsigned long count, unsigned long type, long offset);
155
156     void enable(unsigned long cap);
157     void enableVertexAttribArray(unsigned long index);
158     void finish();
159     void flush();
160     void framebufferRenderbuffer(unsigned long target, unsigned long attachment, unsigned long renderbuffertarget, WebGLRenderbuffer*);
161     void framebufferTexture2D(unsigned long target, unsigned long attachment, unsigned long textarget, WebGLTexture*, long level);
162     void frontFace(unsigned long mode);
163     void generateMipmap(unsigned long target);
164
165     bool getActiveAttrib(WebGLProgram* program, unsigned long index, ActiveInfo&);
166     bool getActiveUniform(WebGLProgram* program, unsigned long index, ActiveInfo&);
167
168     int  getAttribLocation(WebGLProgram*, const String& name);
169
170     void getBooleanv(unsigned long pname, unsigned char* value);
171
172     void getBufferParameteriv(unsigned long target, unsigned long pname, int* value);
173
174     GraphicsContext3D::Attributes getContextAttributes();
175
176     unsigned long getError();
177
178     void getFloatv(unsigned long pname, float* value);
179
180     void getFramebufferAttachmentParameteriv(unsigned long target, unsigned long attachment, unsigned long pname, int* value);
181
182     void getIntegerv(unsigned long pname, int* value);
183
184     void getProgramiv(WebGLProgram* program, unsigned long pname, int* value);
185
186     String getProgramInfoLog(WebGLProgram*);
187
188     void getRenderbufferParameteriv(unsigned long target, unsigned long pname, int* value);
189
190     void getShaderiv(WebGLShader*, unsigned long pname, int* value);
191
192     String getShaderInfoLog(WebGLShader*);
193
194     String getShaderSource(WebGLShader*);
195     String getString(unsigned long name);
196
197     void getTexParameterfv(unsigned long target, unsigned long pname, float* value);
198     void getTexParameteriv(unsigned long target, unsigned long pname, int* value);
199
200     void getUniformfv(WebGLProgram* program, long location, float* value);
201     void getUniformiv(WebGLProgram* program, long location, int* value);
202
203     long getUniformLocation(WebGLProgram*, const String& name);
204
205     void getVertexAttribfv(unsigned long index, unsigned long pname, float* value);
206     void getVertexAttribiv(unsigned long index, unsigned long pname, int* value);
207
208     long getVertexAttribOffset(unsigned long index, unsigned long pname);
209
210     void hint(unsigned long target, unsigned long mode);
211     bool isBuffer(WebGLBuffer*);
212     bool isEnabled(unsigned long cap);
213     bool isFramebuffer(WebGLFramebuffer*);
214     bool isProgram(WebGLProgram*);
215     bool isRenderbuffer(WebGLRenderbuffer*);
216     bool isShader(WebGLShader*);
217     bool isTexture(WebGLTexture*);
218     void lineWidth(double);
219     void linkProgram(WebGLProgram*);
220     void pixelStorei(unsigned long pname, long param);
221     void polygonOffset(double factor, double units);
222
223     void readPixels(long x, long y, unsigned long width, unsigned long height, unsigned long format, unsigned long type, void* data);
224
225     void releaseShaderCompiler();
226     void renderbufferStorage(unsigned long target, unsigned long internalformat, unsigned long width, unsigned long height);
227     void sampleCoverage(double value, bool invert);
228     void scissor(long x, long y, unsigned long width, unsigned long height);
229     void shaderSource(WebGLShader*, const String& string);
230     void stencilFunc(unsigned long func, long ref, unsigned long mask);
231     void stencilFuncSeparate(unsigned long face, unsigned long func, long ref, unsigned long mask);
232     void stencilMask(unsigned long mask);
233     void stencilMaskSeparate(unsigned long face, unsigned long mask);
234     void stencilOp(unsigned long fail, unsigned long zfail, unsigned long zpass);
235     void stencilOpSeparate(unsigned long face, unsigned long fail, unsigned long zfail, unsigned long zpass);
236
237     // These next several functions return an error code (0 if no errors) rather than using an ExceptionCode.
238     // Currently they return -1 on any error.
239     int texImage2D(unsigned target, unsigned level, unsigned internalformat, unsigned width, unsigned height, unsigned border, unsigned format, unsigned type, void* pixels);
240
241     void texParameterf(unsigned target, unsigned pname, float param);
242     void texParameteri(unsigned target, unsigned pname, int param);
243
244     int texSubImage2D(unsigned target, unsigned level, unsigned xoffset, unsigned yoffset, unsigned width, unsigned height, unsigned format, unsigned type, void* pixels);
245
246     void uniform1f(long location, float x);
247     void uniform1fv(long location, float* v, int size);
248     void uniform1i(long location, int x);
249     void uniform1iv(long location, int* v, int size);
250     void uniform2f(long location, float x, float y);
251     void uniform2fv(long location, float* v, int size);
252     void uniform2i(long location, int x, int y);
253     void uniform2iv(long location, int* v, int size);
254     void uniform3f(long location, float x, float y, float z);
255     void uniform3fv(long location, float* v, int size);
256     void uniform3i(long location, int x, int y, int z);
257     void uniform3iv(long location, int* v, int size);
258     void uniform4f(long location, float x, float y, float z, float w);
259     void uniform4fv(long location, float* v, int size);
260     void uniform4i(long location, int x, int y, int z, int w);
261     void uniform4iv(long location, int* v, int size);
262     void uniformMatrix2fv(long location, bool transpose, float* value, int size);
263     void uniformMatrix3fv(long location, bool transpose, float* value, int size);
264     void uniformMatrix4fv(long location, bool transpose, float* value, int size);
265
266     void useProgram(WebGLProgram*);
267     void validateProgram(WebGLProgram*);
268
269     void vertexAttrib1f(unsigned long indx, float x);
270     void vertexAttrib1fv(unsigned long indx, float* values);
271     void vertexAttrib2f(unsigned long indx, float x, float y);
272     void vertexAttrib2fv(unsigned long indx, float* values);
273     void vertexAttrib3f(unsigned long indx, float x, float y, float z);
274     void vertexAttrib3fv(unsigned long indx, float* values);
275     void vertexAttrib4f(unsigned long indx, float x, float y, float z, float w);
276     void vertexAttrib4fv(unsigned long indx, float* values);
277     void vertexAttribPointer(unsigned long indx, int size, int type, bool normalized,
278                              unsigned long stride, unsigned long offset);
279
280     void viewport(long x, long y, unsigned long width, unsigned long height);
281
282     unsigned createBuffer();
283     unsigned createFramebuffer();
284     unsigned createProgram();
285     unsigned createRenderbuffer();
286     unsigned createShader(unsigned long);
287     unsigned createTexture();
288
289     void deleteBuffer(unsigned);
290     void deleteFramebuffer(unsigned);
291     void deleteProgram(unsigned);
292     void deleteRenderbuffer(unsigned);
293     void deleteShader(unsigned);
294     void deleteTexture(unsigned);
295
296     void synthesizeGLError(unsigned long error);
297
298 private:
299     OwnPtr<WebKit::WebGraphicsContext3D> m_impl;
300 #if PLATFORM(SKIA)
301     // If the width and height of the Canvas's backing store don't
302     // match those that we were given in the most recent call to
303     // reshape(), then we need an intermediate bitmap to read back the
304     // frame buffer into. This seems to happen when CSS styles are
305     // used to resize the Canvas.
306     SkBitmap m_resizingBitmap;
307 #endif
308
309 #if PLATFORM(CG)
310     unsigned char* m_renderOutput;
311 #endif
312 };
313
314 GraphicsContext3DInternal::GraphicsContext3DInternal()
315 #if PLATFORM(SKIA)
316 #elif PLATFORM(CG)
317     : m_renderOutput(0)
318 #else
319 #error Must port to your platform
320 #endif
321 {
322 }
323
324 GraphicsContext3DInternal::~GraphicsContext3DInternal()
325 {
326 #if PLATFORM(CG)
327     if (m_renderOutput)
328         delete[] m_renderOutput;
329 #endif
330 }
331
332 bool GraphicsContext3DInternal::initialize(GraphicsContext3D::Attributes attrs)
333 {
334     WebKit::WebGraphicsContext3D::Attributes webAttributes;
335     webAttributes.alpha = attrs.alpha;
336     webAttributes.depth = attrs.depth;
337     webAttributes.stencil = attrs.stencil;
338     webAttributes.antialias = attrs.antialias;
339     webAttributes.premultipliedAlpha = attrs.premultipliedAlpha;
340     WebKit::WebGraphicsContext3D* webContext = WebKit::webKitClient()->createGraphicsContext3D();
341     if (!webContext)
342         return false;
343     if (!webContext->initialize(webAttributes)) {
344         delete webContext;
345         return false;
346     }
347     m_impl.set(webContext);
348     return true;
349 }
350
351 PlatformGraphicsContext3D GraphicsContext3DInternal::platformGraphicsContext3D() const
352 {
353     return 0;
354 }
355
356 Platform3DObject GraphicsContext3DInternal::platformTexture() const
357 {
358     return 0;
359 }
360
361 void GraphicsContext3DInternal::beginPaint(WebGLRenderingContext* context)
362 {
363     HTMLCanvasElement* canvas = context->canvas();
364     ImageBuffer* imageBuffer = canvas->buffer();
365     unsigned char* pixels = 0;
366 #if PLATFORM(SKIA)
367     const SkBitmap* canvasBitmap = imageBuffer->context()->platformContext()->bitmap();
368     const SkBitmap* readbackBitmap = 0;
369     ASSERT(canvasBitmap->config() == SkBitmap::kARGB_8888_Config);
370     if (canvasBitmap->width() == m_impl->width() && canvasBitmap->height() == m_impl->height()) {
371         // This is the fastest and most common case. We read back
372         // directly into the canvas's backing store.
373         readbackBitmap = canvasBitmap;
374         m_resizingBitmap.reset();
375     } else {
376         // We need to allocate a temporary bitmap for reading back the
377         // pixel data. We will then use Skia to rescale this bitmap to
378         // the size of the canvas's backing store.
379         if (m_resizingBitmap.width() != m_impl->width() || m_resizingBitmap.height() != m_impl->height()) {
380             m_resizingBitmap.setConfig(SkBitmap::kARGB_8888_Config,
381                                        m_impl->width(),
382                                        m_impl->height());
383             if (!m_resizingBitmap.allocPixels()) {
384                 return;
385             }
386         }
387         readbackBitmap = &m_resizingBitmap;
388     }
389
390     // Read back the frame buffer.
391     SkAutoLockPixels bitmapLock(*readbackBitmap);
392     pixels = static_cast<unsigned char*>(readbackBitmap->getPixels());
393 #elif PLATFORM(CG)
394     if (m_renderOutput)
395         pixels = m_renderOutput;
396 #else
397 #error Must port to your platform
398 #endif
399
400     m_impl->readBackFramebuffer(pixels, 4 * m_impl->width() * m_impl->height());
401
402 #if PLATFORM(SKIA)
403     if (m_resizingBitmap.readyToDraw()) {
404         // We need to draw the resizing bitmap into the canvas's backing store.
405         SkCanvas canvas(*canvasBitmap);
406         SkRect dst;
407         dst.set(SkIntToScalar(0), SkIntToScalar(0), canvasBitmap->width(), canvasBitmap->height());
408         canvas.drawBitmapRect(m_resizingBitmap, 0, dst);
409     }
410 #elif PLATFORM(CG)
411     if (m_renderOutput) {
412         int rowBytes = m_impl->width() * 4;
413         CGDataProviderRef dataProvider = CGDataProviderCreateWithData(0, m_renderOutput, rowBytes * m_impl->height(), 0);
414         CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
415         CGImageRef cgImage = CGImageCreate(m_impl->width(),
416                                            m_impl->height(),
417                                            8,
418                                            32,
419                                            rowBytes,
420                                            colorSpace,
421                                            kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host,
422                                            dataProvider,
423                                            0,
424                                            false,
425                                            kCGRenderingIntentDefault);
426         // CSS styling may cause the canvas's content to be resized on
427         // the page. Go back to the Canvas to figure out the correct
428         // width and height to draw.
429         CGRect rect = CGRectMake(0, 0,
430                                  context->canvas()->width(),
431                                  context->canvas()->height());
432         // We want to completely overwrite the previous frame's
433         // rendering results.
434         CGContextSetBlendMode(imageBuffer->context()->platformContext(),
435                               kCGBlendModeCopy);
436         CGContextSetInterpolationQuality(imageBuffer->context()->platformContext(),
437                                          kCGInterpolationNone);
438         CGContextDrawImage(imageBuffer->context()->platformContext(),
439                            rect, cgImage);
440         CGImageRelease(cgImage);
441         CGColorSpaceRelease(colorSpace);
442         CGDataProviderRelease(dataProvider);
443     }
444 #else
445 #error Must port to your platform
446 #endif
447 }
448
449 void GraphicsContext3DInternal::endPaint()
450 {
451 }
452
453 void GraphicsContext3DInternal::reshape(int width, int height)
454 {
455     if (width == m_impl->width() && height == m_impl->height())
456         return;
457
458     m_impl->reshape(width, height);
459
460 #if PLATFORM(CG)
461     // Need to reallocate the client-side backing store.
462     // FIXME: make this more efficient.
463     if (m_renderOutput) {
464         delete[] m_renderOutput;
465         m_renderOutput = 0;
466     }
467     int rowBytes = width * 4;
468     m_renderOutput = new unsigned char[height * rowBytes];
469 #endif // PLATFORM(CG)
470 }
471
472 // Macros to assist in delegating from GraphicsContext3DInternal to
473 // WebGraphicsContext3D.
474
475 #define DELEGATE_TO_IMPL(name) \
476 void GraphicsContext3DInternal::name() \
477 { \
478     m_impl->name(); \
479 }
480
481 #define DELEGATE_TO_IMPL_R(name, rt)           \
482 rt GraphicsContext3DInternal::name() \
483 { \
484     return m_impl->name(); \
485 }
486
487 #define DELEGATE_TO_IMPL_1(name, t1) \
488 void GraphicsContext3DInternal::name(t1 a1) \
489 { \
490     m_impl->name(a1); \
491 }
492
493 #define DELEGATE_TO_IMPL_1_X(name, t1) \
494 void GraphicsContext3DInternal::name(t1 a1) \
495 { \
496     m_impl->name(EXTRACT(a1));                  \
497 }
498
499 #define DELEGATE_TO_IMPL_1R(name, t1, rt)    \
500 rt GraphicsContext3DInternal::name(t1 a1) \
501 { \
502     return m_impl->name(a1); \
503 }
504
505 #define DELEGATE_TO_IMPL_1R_X(name, t1, rt)    \
506 rt GraphicsContext3DInternal::name(t1 a1) \
507 { \
508     return m_impl->name(EXTRACT(a1));           \
509 }
510
511 #define DELEGATE_TO_IMPL_2(name, t1, t2) \
512 void GraphicsContext3DInternal::name(t1 a1, t2 a2) \
513 { \
514     m_impl->name(a1, a2); \
515 }
516
517 #define DELEGATE_TO_IMPL_2_X12(name, t1, t2) \
518 void GraphicsContext3DInternal::name(t1 a1, t2 a2) \
519 { \
520     m_impl->name(EXTRACT(a1), EXTRACT(a2));     \
521 }
522
523 #define DELEGATE_TO_IMPL_2_X2(name, t1, t2) \
524 void GraphicsContext3DInternal::name(t1 a1, t2 a2) \
525 { \
526     m_impl->name(a1, EXTRACT(a2));     \
527 }
528
529 #define DELEGATE_TO_IMPL_2R(name, t1, t2, rt)  \
530 rt GraphicsContext3DInternal::name(t1 a1, t2 a2) \
531 { \
532     return m_impl->name(a1, a2); \
533 }
534
535 #define DELEGATE_TO_IMPL_3(name, t1, t2, t3)   \
536 void GraphicsContext3DInternal::name(t1 a1, t2 a2, t3 a3)    \
537 { \
538     m_impl->name(a1, a2, a3);                  \
539 }
540
541 #define DELEGATE_TO_IMPL_3_X1(name, t1, t2, t3)   \
542 void GraphicsContext3DInternal::name(t1 a1, t2 a2, t3 a3)    \
543 { \
544     m_impl->name(EXTRACT(a1), a2, a3);          \
545 }
546
547 #define DELEGATE_TO_IMPL_3R(name, t1, t2, t3, rt)   \
548 rt GraphicsContext3DInternal::name(t1 a1, t2 a2, t3 a3)    \
549 { \
550     return m_impl->name(a1, a2, a3);                  \
551 }
552
553 #define DELEGATE_TO_IMPL_4(name, t1, t2, t3, t4)    \
554 void GraphicsContext3DInternal::name(t1 a1, t2 a2, t3 a3, t4 a4)  \
555 { \
556     m_impl->name(a1, a2, a3, a4);              \
557 }
558
559 #define DELEGATE_TO_IMPL_4_X4(name, t1, t2, t3, t4)    \
560 void GraphicsContext3DInternal::name(t1 a1, t2 a2, t3 a3, t4 a4)  \
561 { \
562     m_impl->name(a1, a2, a3, EXTRACT(a4));      \
563 }
564
565 #define DELEGATE_TO_IMPL_5(name, t1, t2, t3, t4, t5)      \
566 void GraphicsContext3DInternal::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5)        \
567 { \
568     m_impl->name(a1, a2, a3, a4, a5);   \
569 }
570
571 #define DELEGATE_TO_IMPL_5_X4(name, t1, t2, t3, t4, t5)                \
572 void GraphicsContext3DInternal::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5)        \
573 { \
574     m_impl->name(a1, a2, a3, EXTRACT(a4), a5);  \
575 }
576
577 #define DELEGATE_TO_IMPL_5R(name, t1, t2, t3, t4, t5, rt)      \
578 rt GraphicsContext3DInternal::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5)        \
579 { \
580     return m_impl->name(a1, a2, a3, a4, a5);   \
581 }
582
583 #define DELEGATE_TO_IMPL_6(name, t1, t2, t3, t4, t5, t6)  \
584 void GraphicsContext3DInternal::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6) \
585 { \
586     m_impl->name(a1, a2, a3, a4, a5, a6);       \
587 }
588
589 #define DELEGATE_TO_IMPL_6R(name, t1, t2, t3, t4, t5, t6, rt)  \
590 rt GraphicsContext3DInternal::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6) \
591 { \
592     return m_impl->name(a1, a2, a3, a4, a5, a6);       \
593 }
594
595 #define DELEGATE_TO_IMPL_7(name, t1, t2, t3, t4, t5, t6, t7) \
596 void GraphicsContext3DInternal::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7) \
597 { \
598     m_impl->name(a1, a2, a3, a4, a5, a6, a7);   \
599 }
600
601 #define DELEGATE_TO_IMPL_7R(name, t1, t2, t3, t4, t5, t6, t7, rt) \
602 rt GraphicsContext3DInternal::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7) \
603 { \
604     return m_impl->name(a1, a2, a3, a4, a5, a6, a7);   \
605 }
606
607 #define DELEGATE_TO_IMPL_8(name, t1, t2, t3, t4, t5, t6, t7, t8)       \
608 void GraphicsContext3DInternal::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8) \
609 { \
610     m_impl->name(a1, a2, a3, a4, a5, a6, a7, a8);      \
611 }
612
613 #define DELEGATE_TO_IMPL_9R(name, t1, t2, t3, t4, t5, t6, t7, t8, t9, rt) \
614 rt GraphicsContext3DInternal::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8, t9 a9) \
615 { \
616     return m_impl->name(a1, a2, a3, a4, a5, a6, a7, a8, a9);   \
617 }
618
619 DELEGATE_TO_IMPL_R(makeContextCurrent, bool)
620 DELEGATE_TO_IMPL_1R(sizeInBytes, int, int)
621
622 bool GraphicsContext3DInternal::isGLES2Compliant() const
623 {
624     return m_impl->isGLES2Compliant();
625 }
626
627 DELEGATE_TO_IMPL_1(activeTexture, unsigned long)
628 DELEGATE_TO_IMPL_2_X12(attachShader, WebGLProgram*, WebGLShader*)
629
630 void GraphicsContext3DInternal::bindAttribLocation(WebGLProgram* program, unsigned long index, const String& name)
631 {
632     m_impl->bindAttribLocation(EXTRACT(program), index, name.utf8().data());
633 }
634
635 DELEGATE_TO_IMPL_2_X2(bindBuffer, unsigned long, WebGLBuffer*)
636 DELEGATE_TO_IMPL_2_X2(bindFramebuffer, unsigned long, WebGLFramebuffer*)
637 DELEGATE_TO_IMPL_2_X2(bindRenderbuffer, unsigned long, WebGLRenderbuffer*)
638
639 static const int kTextureWrapR = 0x8072;
640
641 // If we didn't have to hack GL_TEXTURE_WRAP_R for cube maps,
642 // we could just use:
643 // DELEGATE_TO_IMPL_2_X2(bindTexture, unsigned long, WebGLTexture*)
644 void GraphicsContext3DInternal::bindTexture(unsigned long target,
645                                             WebGLTexture* texture)
646 {
647     unsigned int textureObject = EXTRACT(texture);
648
649     m_impl->bindTexture(target, textureObject);
650
651     // FIXME: GL_TEXTURE_WRAP_R isn't exposed in the OpenGL ES 2.0
652     // API. On desktop OpenGL implementations it seems necessary to
653     // set this wrap mode to GL_CLAMP_TO_EDGE to get correct behavior
654     // of cube maps.
655     if (texture)
656         if (target == GraphicsContext3D::TEXTURE_CUBE_MAP) {
657             if (!texture->isCubeMapRWrapModeInitialized()) {
658                 m_impl->texParameteri(GraphicsContext3D::TEXTURE_CUBE_MAP, kTextureWrapR, GraphicsContext3D::CLAMP_TO_EDGE);
659                 texture->setCubeMapRWrapModeInitialized(true);
660             }
661         } else
662             texture->setCubeMapRWrapModeInitialized(false);
663 }
664
665 DELEGATE_TO_IMPL_4(blendColor, double, double, double, double)
666 DELEGATE_TO_IMPL_1(blendEquation, unsigned long)
667 DELEGATE_TO_IMPL_2(blendEquationSeparate, unsigned long, unsigned long)
668 DELEGATE_TO_IMPL_2(blendFunc, unsigned long, unsigned long)
669 DELEGATE_TO_IMPL_4(blendFuncSeparate, unsigned long, unsigned long, unsigned long, unsigned long)
670
671 void GraphicsContext3DInternal::bufferData(unsigned long target, int size, unsigned long usage)
672 {
673     m_impl->bufferData(target, size, 0, usage);
674 }
675
676 void GraphicsContext3DInternal::bufferData(unsigned long target, WebGLArray* array, unsigned long usage)
677 {
678     m_impl->bufferData(target, array->byteLength(), array->baseAddress(), usage);
679 }
680
681 void GraphicsContext3DInternal::bufferSubData(unsigned long target, long offset, WebGLArray* array)
682 {
683     m_impl->bufferSubData(target, offset, array->byteLength(), array->baseAddress());
684 }
685
686 DELEGATE_TO_IMPL_1R(checkFramebufferStatus, unsigned long, unsigned long)
687 DELEGATE_TO_IMPL_1(clear, unsigned long)
688 DELEGATE_TO_IMPL_4(clearColor, double, double, double, double)
689 DELEGATE_TO_IMPL_1(clearDepth, double)
690 DELEGATE_TO_IMPL_1(clearStencil, long)
691 DELEGATE_TO_IMPL_4(colorMask, bool, bool, bool, bool)
692 DELEGATE_TO_IMPL_1_X(compileShader, WebGLShader*)
693
694 DELEGATE_TO_IMPL_8(copyTexImage2D, unsigned long, long, unsigned long, long, long, unsigned long, unsigned long, long)
695 DELEGATE_TO_IMPL_8(copyTexSubImage2D, unsigned long, long, long, long, long, long, unsigned long, unsigned long)
696 DELEGATE_TO_IMPL_1(cullFace, unsigned long)
697 DELEGATE_TO_IMPL_1(depthFunc, unsigned long)
698 DELEGATE_TO_IMPL_1(depthMask, bool)
699 DELEGATE_TO_IMPL_2(depthRange, double, double)
700 DELEGATE_TO_IMPL_2_X12(detachShader, WebGLProgram*, WebGLShader*)
701 DELEGATE_TO_IMPL_1(disable, unsigned long)
702 DELEGATE_TO_IMPL_1(disableVertexAttribArray, unsigned long)
703 DELEGATE_TO_IMPL_3(drawArrays, unsigned long, long, long)
704 DELEGATE_TO_IMPL_4(drawElements, unsigned long, unsigned long, unsigned long, long)
705
706 DELEGATE_TO_IMPL_1(enable, unsigned long)
707 DELEGATE_TO_IMPL_1(enableVertexAttribArray, unsigned long)
708 DELEGATE_TO_IMPL(finish)
709 DELEGATE_TO_IMPL(flush)
710 DELEGATE_TO_IMPL_4_X4(framebufferRenderbuffer, unsigned long, unsigned long, unsigned long, WebGLRenderbuffer*)
711 DELEGATE_TO_IMPL_5_X4(framebufferTexture2D, unsigned long, unsigned long, unsigned long, WebGLTexture*, long)
712 DELEGATE_TO_IMPL_1(frontFace, unsigned long)
713 DELEGATE_TO_IMPL_1(generateMipmap, unsigned long)
714
715 bool GraphicsContext3DInternal::getActiveAttrib(WebGLProgram* program, unsigned long index, ActiveInfo& info)
716 {
717     WebKit::WebGraphicsContext3D::ActiveInfo webInfo;
718     if (!m_impl->getActiveAttrib(EXTRACT(program), index, webInfo))
719         return false;
720     info.name = webInfo.name;
721     info.type = webInfo.type;
722     info.size = webInfo.size;
723     return true;
724 }
725
726 bool GraphicsContext3DInternal::getActiveUniform(WebGLProgram* program, unsigned long index, ActiveInfo& info)
727 {
728     WebKit::WebGraphicsContext3D::ActiveInfo webInfo;
729     if (!m_impl->getActiveUniform(EXTRACT(program), index, webInfo))
730         return false;
731     info.name = webInfo.name;
732     info.type = webInfo.type;
733     info.size = webInfo.size;
734     return true;
735 }
736
737 int GraphicsContext3DInternal::getAttribLocation(WebGLProgram* program, const String& name)
738 {
739     return m_impl->getAttribLocation(EXTRACT(program), name.utf8().data());
740 }
741
742 DELEGATE_TO_IMPL_2(getBooleanv, unsigned long, unsigned char*)
743
744 DELEGATE_TO_IMPL_3(getBufferParameteriv, unsigned long, unsigned long, int*)
745
746 GraphicsContext3D::Attributes GraphicsContext3DInternal::getContextAttributes()
747 {
748     WebKit::WebGraphicsContext3D::Attributes webAttributes = m_impl->getContextAttributes();
749     GraphicsContext3D::Attributes attributes;
750     attributes.alpha = webAttributes.alpha;
751     attributes.depth = webAttributes.depth;
752     attributes.stencil = webAttributes.stencil;
753     attributes.antialias = webAttributes.antialias;
754     attributes.premultipliedAlpha = webAttributes.premultipliedAlpha;
755     return attributes;
756 }
757
758 DELEGATE_TO_IMPL_R(getError, unsigned long)
759
760 DELEGATE_TO_IMPL_2(getFloatv, unsigned long, float*)
761
762 DELEGATE_TO_IMPL_4(getFramebufferAttachmentParameteriv, unsigned long, unsigned long, unsigned long, int*)
763
764 DELEGATE_TO_IMPL_2(getIntegerv, unsigned long, int*)
765
766 DELEGATE_TO_IMPL_3_X1(getProgramiv, WebGLProgram*, unsigned long, int*)
767
768 String GraphicsContext3DInternal::getProgramInfoLog(WebGLProgram* program)
769 {
770     return m_impl->getProgramInfoLog(EXTRACT(program));
771 }
772
773 DELEGATE_TO_IMPL_3(getRenderbufferParameteriv, unsigned long, unsigned long, int*)
774
775 DELEGATE_TO_IMPL_3_X1(getShaderiv, WebGLShader*, unsigned long, int*)
776
777 String GraphicsContext3DInternal::getShaderInfoLog(WebGLShader* shader)
778 {
779     return m_impl->getShaderInfoLog(EXTRACT(shader));
780 }
781
782 String GraphicsContext3DInternal::getShaderSource(WebGLShader* shader)
783 {
784     return m_impl->getShaderSource(EXTRACT(shader));
785 }
786
787 String GraphicsContext3DInternal::getString(unsigned long name)
788 {
789     return m_impl->getString(name);
790 }
791
792 DELEGATE_TO_IMPL_3(getTexParameterfv, unsigned long, unsigned long, float*)
793 DELEGATE_TO_IMPL_3(getTexParameteriv, unsigned long, unsigned long, int*)
794
795 DELEGATE_TO_IMPL_3_X1(getUniformfv, WebGLProgram*, long, float*)
796 DELEGATE_TO_IMPL_3_X1(getUniformiv, WebGLProgram*, long, int*)
797
798 long GraphicsContext3DInternal::getUniformLocation(WebGLProgram* program, const String& name)
799 {
800     return m_impl->getUniformLocation(EXTRACT(program), name.utf8().data());
801 }
802
803 DELEGATE_TO_IMPL_3(getVertexAttribfv, unsigned long, unsigned long, float*)
804 DELEGATE_TO_IMPL_3(getVertexAttribiv, unsigned long, unsigned long, int*)
805
806 DELEGATE_TO_IMPL_2R(getVertexAttribOffset, unsigned long, unsigned long, long)
807
808 DELEGATE_TO_IMPL_2(hint, unsigned long, unsigned long)
809 DELEGATE_TO_IMPL_1R_X(isBuffer, WebGLBuffer*, bool)
810 DELEGATE_TO_IMPL_1R(isEnabled, unsigned long, bool)
811 DELEGATE_TO_IMPL_1R_X(isFramebuffer, WebGLFramebuffer*, bool)
812 DELEGATE_TO_IMPL_1R_X(isProgram, WebGLProgram*, bool)
813 DELEGATE_TO_IMPL_1R_X(isRenderbuffer, WebGLRenderbuffer*, bool)
814 DELEGATE_TO_IMPL_1R_X(isShader, WebGLShader*, bool)
815 DELEGATE_TO_IMPL_1R_X(isTexture, WebGLTexture*, bool)
816 DELEGATE_TO_IMPL_1(lineWidth, double)
817 DELEGATE_TO_IMPL_1_X(linkProgram, WebGLProgram*)
818 DELEGATE_TO_IMPL_2(pixelStorei, unsigned long, long)
819 DELEGATE_TO_IMPL_2(polygonOffset, double, double)
820 DELEGATE_TO_IMPL_7(readPixels, long, long, unsigned long, unsigned long, unsigned long, unsigned long, void*)
821 DELEGATE_TO_IMPL(releaseShaderCompiler)
822 DELEGATE_TO_IMPL_4(renderbufferStorage, unsigned long, unsigned long, unsigned long, unsigned long)
823 DELEGATE_TO_IMPL_2(sampleCoverage, double, bool)
824 DELEGATE_TO_IMPL_4(scissor, long, long, unsigned long, unsigned long)
825
826 void GraphicsContext3DInternal::shaderSource(WebGLShader* shader, const String& string)
827 {
828     m_impl->shaderSource(EXTRACT(shader), string.utf8().data());
829 }
830
831 DELEGATE_TO_IMPL_3(stencilFunc, unsigned long, long, unsigned long)
832 DELEGATE_TO_IMPL_4(stencilFuncSeparate, unsigned long, unsigned long, long, unsigned long)
833 DELEGATE_TO_IMPL_1(stencilMask, unsigned long)
834 DELEGATE_TO_IMPL_2(stencilMaskSeparate, unsigned long, unsigned long)
835 DELEGATE_TO_IMPL_3(stencilOp, unsigned long, unsigned long, unsigned long)
836 DELEGATE_TO_IMPL_4(stencilOpSeparate, unsigned long, unsigned long, unsigned long, unsigned long)
837
838 int GraphicsContext3DInternal::texImage2D(unsigned target, unsigned level, unsigned internalformat, unsigned width, unsigned height, unsigned border, unsigned format, unsigned type, void* pixels)
839 {
840     m_impl->texImage2D(target, level, internalformat, width, height, border, format, type, pixels);
841     return 0;
842 }
843
844 DELEGATE_TO_IMPL_3(texParameterf, unsigned, unsigned, float)
845 DELEGATE_TO_IMPL_3(texParameteri, unsigned, unsigned, int)
846
847 int GraphicsContext3DInternal::texSubImage2D(unsigned target, unsigned level, unsigned xoffset, unsigned yoffset, unsigned width, unsigned height, unsigned format, unsigned type, void* pixels)
848 {
849     m_impl->texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels);
850     return 0;
851 }
852
853 DELEGATE_TO_IMPL_2(uniform1f, long, float)
854
855 void GraphicsContext3DInternal::uniform1fv(long location, float* v, int size)
856 {
857     m_impl->uniform1fv(location, size, v);
858 }
859
860 DELEGATE_TO_IMPL_2(uniform1i, long, int)
861
862 void GraphicsContext3DInternal::uniform1iv(long location, int* v, int size)
863 {
864     m_impl->uniform1iv(location, size, v);
865 }
866
867 DELEGATE_TO_IMPL_3(uniform2f, long, float, float)
868
869 void GraphicsContext3DInternal::uniform2fv(long location, float* v, int size)
870 {
871     m_impl->uniform2fv(location, size, v);
872 }
873
874 DELEGATE_TO_IMPL_3(uniform2i, long, int, int)
875
876 void GraphicsContext3DInternal::uniform2iv(long location, int* v, int size)
877 {
878     m_impl->uniform2iv(location, size, v);
879 }
880
881 DELEGATE_TO_IMPL_4(uniform3f, long, float, float, float)
882
883 void GraphicsContext3DInternal::uniform3fv(long location, float* v, int size)
884 {
885     m_impl->uniform3fv(location, size, v);
886 }
887
888 DELEGATE_TO_IMPL_4(uniform3i, long, int, int, int)
889
890 void GraphicsContext3DInternal::uniform3iv(long location, int* v, int size)
891 {
892     m_impl->uniform3iv(location, size, v);
893 }
894
895 DELEGATE_TO_IMPL_5(uniform4f, long, float, float, float, float)
896
897 void GraphicsContext3DInternal::uniform4fv(long location, float* v, int size)
898 {
899     m_impl->uniform4fv(location, size, v);
900 }
901
902 DELEGATE_TO_IMPL_5(uniform4i, long, int, int, int, int)
903
904 void GraphicsContext3DInternal::uniform4iv(long location, int* v, int size)
905 {
906     m_impl->uniform4iv(location, size, v);
907 }
908
909 void GraphicsContext3DInternal::uniformMatrix2fv(long location, bool transpose, float* value, int size)
910 {
911     m_impl->uniformMatrix2fv(location, size, transpose, value);
912 }
913
914 void GraphicsContext3DInternal::uniformMatrix3fv(long location, bool transpose, float* value, int size)
915 {
916     m_impl->uniformMatrix3fv(location, size, transpose, value);
917 }
918
919 void GraphicsContext3DInternal::uniformMatrix4fv(long location, bool transpose, float* value, int size)
920 {
921     m_impl->uniformMatrix4fv(location, size, transpose, value);
922 }
923
924 DELEGATE_TO_IMPL_1_X(useProgram, WebGLProgram*)
925 DELEGATE_TO_IMPL_1_X(validateProgram, WebGLProgram*)
926
927 DELEGATE_TO_IMPL_2(vertexAttrib1f, unsigned long, float)
928 DELEGATE_TO_IMPL_2(vertexAttrib1fv, unsigned long, float*)
929 DELEGATE_TO_IMPL_3(vertexAttrib2f, unsigned long, float, float)
930 DELEGATE_TO_IMPL_2(vertexAttrib2fv, unsigned long, float*)
931 DELEGATE_TO_IMPL_4(vertexAttrib3f, unsigned long, float, float, float)
932 DELEGATE_TO_IMPL_2(vertexAttrib3fv, unsigned long, float*)
933 DELEGATE_TO_IMPL_5(vertexAttrib4f, unsigned long, float, float, float, float)
934 DELEGATE_TO_IMPL_2(vertexAttrib4fv, unsigned long, float*)
935 DELEGATE_TO_IMPL_6(vertexAttribPointer, unsigned long, int, int, bool, unsigned long, unsigned long)
936
937 DELEGATE_TO_IMPL_4(viewport, long, long, unsigned long, unsigned long)
938
939 DELEGATE_TO_IMPL_R(createBuffer, unsigned)
940 DELEGATE_TO_IMPL_R(createFramebuffer, unsigned)
941 DELEGATE_TO_IMPL_R(createProgram, unsigned)
942 DELEGATE_TO_IMPL_R(createRenderbuffer, unsigned)
943 DELEGATE_TO_IMPL_1R(createShader, unsigned long, unsigned)
944 DELEGATE_TO_IMPL_R(createTexture, unsigned)
945
946 DELEGATE_TO_IMPL_1(deleteBuffer, unsigned)
947 DELEGATE_TO_IMPL_1(deleteFramebuffer, unsigned)
948 DELEGATE_TO_IMPL_1(deleteProgram, unsigned)
949 DELEGATE_TO_IMPL_1(deleteRenderbuffer, unsigned)
950 DELEGATE_TO_IMPL_1(deleteShader, unsigned)
951 DELEGATE_TO_IMPL_1(deleteTexture, unsigned)
952
953 DELEGATE_TO_IMPL_1(synthesizeGLError, unsigned long)
954
955 //----------------------------------------------------------------------
956 // GraphicsContext3D
957 //
958
959 // Macros to assist in delegating from GraphicsContext3D to
960 // GraphicsContext3DInternal.
961
962 #define DELEGATE_TO_INTERNAL(name) \
963 void GraphicsContext3D::name() \
964 { \
965     m_internal->name(); \
966 }
967
968 #define DELEGATE_TO_INTERNAL_R(name, rt)           \
969 rt GraphicsContext3D::name() \
970 { \
971     return m_internal->name(); \
972 }
973
974 #define DELEGATE_TO_INTERNAL_1(name, t1) \
975 void GraphicsContext3D::name(t1 a1) \
976 { \
977     m_internal->name(a1); \
978 }
979
980 #define DELEGATE_TO_INTERNAL_1R(name, t1, rt)    \
981 rt GraphicsContext3D::name(t1 a1) \
982 { \
983     return m_internal->name(a1); \
984 }
985
986 #define DELEGATE_TO_INTERNAL_2(name, t1, t2) \
987 void GraphicsContext3D::name(t1 a1, t2 a2) \
988 { \
989     m_internal->name(a1, a2); \
990 }
991
992 #define DELEGATE_TO_INTERNAL_2R(name, t1, t2, rt)  \
993 rt GraphicsContext3D::name(t1 a1, t2 a2) \
994 { \
995     return m_internal->name(a1, a2); \
996 }
997
998 #define DELEGATE_TO_INTERNAL_3(name, t1, t2, t3)   \
999 void GraphicsContext3D::name(t1 a1, t2 a2, t3 a3)    \
1000 { \
1001     m_internal->name(a1, a2, a3);                  \
1002 }
1003
1004 #define DELEGATE_TO_INTERNAL_3R(name, t1, t2, t3, rt)   \
1005 rt GraphicsContext3D::name(t1 a1, t2 a2, t3 a3)    \
1006 { \
1007     return m_internal->name(a1, a2, a3);                  \
1008 }
1009
1010 #define DELEGATE_TO_INTERNAL_4(name, t1, t2, t3, t4)    \
1011 void GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4)  \
1012 { \
1013     m_internal->name(a1, a2, a3, a4);              \
1014 }
1015
1016 #define DELEGATE_TO_INTERNAL_5(name, t1, t2, t3, t4, t5)      \
1017 void GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5)        \
1018 { \
1019     m_internal->name(a1, a2, a3, a4, a5);   \
1020 }
1021
1022 #define DELEGATE_TO_INTERNAL_6(name, t1, t2, t3, t4, t5, t6)  \
1023 void GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6) \
1024 { \
1025     m_internal->name(a1, a2, a3, a4, a5, a6);   \
1026 }
1027
1028 #define DELEGATE_TO_INTERNAL_6R(name, t1, t2, t3, t4, t5, t6, rt)  \
1029 rt GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6) \
1030 { \
1031     return m_internal->name(a1, a2, a3, a4, a5, a6);       \
1032 }
1033
1034 #define DELEGATE_TO_INTERNAL_7(name, t1, t2, t3, t4, t5, t6, t7) \
1035 void GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7) \
1036 { \
1037     m_internal->name(a1, a2, a3, a4, a5, a6, a7);   \
1038 }
1039
1040 #define DELEGATE_TO_INTERNAL_7R(name, t1, t2, t3, t4, t5, t6, t7, rt) \
1041 rt GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7) \
1042 { \
1043     return m_internal->name(a1, a2, a3, a4, a5, a6, a7);   \
1044 }
1045
1046 #define DELEGATE_TO_INTERNAL_8(name, t1, t2, t3, t4, t5, t6, t7, t8)       \
1047 void GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8) \
1048 { \
1049     m_internal->name(a1, a2, a3, a4, a5, a6, a7, a8);      \
1050 }
1051
1052 #define DELEGATE_TO_INTERNAL_9R(name, t1, t2, t3, t4, t5, t6, t7, t8, t9, rt) \
1053 rt GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8, t9 a9) \
1054 { \
1055     return m_internal->name(a1, a2, a3, a4, a5, a6, a7, a8, a9);   \
1056 }
1057
1058 GraphicsContext3D::GraphicsContext3D(GraphicsContext3D::Attributes, HostWindow*)
1059 {
1060 }
1061
1062 GraphicsContext3D::~GraphicsContext3D()
1063 {
1064 }
1065
1066 PassOwnPtr<GraphicsContext3D> GraphicsContext3D::create(GraphicsContext3D::Attributes attrs, HostWindow* hostWindow)
1067 {
1068     GraphicsContext3DInternal* internal = new GraphicsContext3DInternal();
1069     if (!internal->initialize(attrs)) {
1070         delete internal;
1071         return 0;
1072     }
1073     PassOwnPtr<GraphicsContext3D> result = new GraphicsContext3D(attrs, hostWindow);
1074     result->m_internal.set(internal);
1075     return result;
1076 }
1077
1078 PlatformGraphicsContext3D GraphicsContext3D::platformGraphicsContext3D() const
1079 {
1080     return m_internal->platformGraphicsContext3D();
1081 }
1082
1083 Platform3DObject GraphicsContext3D::platformTexture() const
1084 {
1085     return m_internal->platformTexture();
1086 }
1087
1088 DELEGATE_TO_INTERNAL(makeContextCurrent)
1089 DELEGATE_TO_INTERNAL_1R(sizeInBytes, int, int)
1090 DELEGATE_TO_INTERNAL_2(reshape, int, int)
1091
1092 DELEGATE_TO_INTERNAL_1(activeTexture, unsigned long)
1093 DELEGATE_TO_INTERNAL_2(attachShader, WebGLProgram*, WebGLShader*)
1094 DELEGATE_TO_INTERNAL_3(bindAttribLocation, WebGLProgram*, unsigned long, const String&)
1095
1096 DELEGATE_TO_INTERNAL_2(bindBuffer, unsigned long, WebGLBuffer*)
1097 DELEGATE_TO_INTERNAL_2(bindFramebuffer, unsigned long, WebGLFramebuffer*)
1098 DELEGATE_TO_INTERNAL_2(bindRenderbuffer, unsigned long, WebGLRenderbuffer*)
1099 DELEGATE_TO_INTERNAL_2(bindTexture, unsigned long, WebGLTexture*)
1100 DELEGATE_TO_INTERNAL_4(blendColor, double, double, double, double)
1101 DELEGATE_TO_INTERNAL_1(blendEquation, unsigned long)
1102 DELEGATE_TO_INTERNAL_2(blendEquationSeparate, unsigned long, unsigned long)
1103 DELEGATE_TO_INTERNAL_2(blendFunc, unsigned long, unsigned long)
1104 DELEGATE_TO_INTERNAL_4(blendFuncSeparate, unsigned long, unsigned long, unsigned long, unsigned long)
1105
1106 DELEGATE_TO_INTERNAL_3(bufferData, unsigned long, int, unsigned long)
1107 DELEGATE_TO_INTERNAL_3(bufferData, unsigned long, WebGLArray*, unsigned long)
1108 DELEGATE_TO_INTERNAL_3(bufferSubData, unsigned long, long, WebGLArray*)
1109
1110 DELEGATE_TO_INTERNAL_1R(checkFramebufferStatus, unsigned long, unsigned long)
1111 DELEGATE_TO_INTERNAL_1(clear, unsigned long)
1112 DELEGATE_TO_INTERNAL_4(clearColor, double, double, double, double)
1113 DELEGATE_TO_INTERNAL_1(clearDepth, double)
1114 DELEGATE_TO_INTERNAL_1(clearStencil, long)
1115 DELEGATE_TO_INTERNAL_4(colorMask, bool, bool, bool, bool)
1116 DELEGATE_TO_INTERNAL_1(compileShader, WebGLShader*)
1117
1118 DELEGATE_TO_INTERNAL_8(copyTexImage2D, unsigned long, long, unsigned long, long, long, unsigned long, unsigned long, long)
1119 DELEGATE_TO_INTERNAL_8(copyTexSubImage2D, unsigned long, long, long, long, long, long, unsigned long, unsigned long)
1120 DELEGATE_TO_INTERNAL_1(cullFace, unsigned long)
1121 DELEGATE_TO_INTERNAL_1(depthFunc, unsigned long)
1122 DELEGATE_TO_INTERNAL_1(depthMask, bool)
1123 DELEGATE_TO_INTERNAL_2(depthRange, double, double)
1124 DELEGATE_TO_INTERNAL_2(detachShader, WebGLProgram*, WebGLShader*)
1125 DELEGATE_TO_INTERNAL_1(disable, unsigned long)
1126 DELEGATE_TO_INTERNAL_1(disableVertexAttribArray, unsigned long)
1127 DELEGATE_TO_INTERNAL_3(drawArrays, unsigned long, long, long)
1128 DELEGATE_TO_INTERNAL_4(drawElements, unsigned long, unsigned long, unsigned long, long)
1129
1130 DELEGATE_TO_INTERNAL_1(enable, unsigned long)
1131 DELEGATE_TO_INTERNAL_1(enableVertexAttribArray, unsigned long)
1132 DELEGATE_TO_INTERNAL(finish)
1133 DELEGATE_TO_INTERNAL(flush)
1134 DELEGATE_TO_INTERNAL_4(framebufferRenderbuffer, unsigned long, unsigned long, unsigned long, WebGLRenderbuffer*)
1135 DELEGATE_TO_INTERNAL_5(framebufferTexture2D, unsigned long, unsigned long, unsigned long, WebGLTexture*, long)
1136 DELEGATE_TO_INTERNAL_1(frontFace, unsigned long)
1137 DELEGATE_TO_INTERNAL_1(generateMipmap, unsigned long)
1138
1139 DELEGATE_TO_INTERNAL_3R(getActiveAttrib, WebGLProgram*, unsigned long, ActiveInfo&, bool)
1140 DELEGATE_TO_INTERNAL_3R(getActiveUniform, WebGLProgram*, unsigned long, ActiveInfo&, bool)
1141
1142 DELEGATE_TO_INTERNAL_2R(getAttribLocation, WebGLProgram*, const String&, int)
1143
1144 DELEGATE_TO_INTERNAL_2(getBooleanv, unsigned long, unsigned char*)
1145
1146 DELEGATE_TO_INTERNAL_3(getBufferParameteriv, unsigned long, unsigned long, int*)
1147
1148 DELEGATE_TO_INTERNAL_R(getContextAttributes, GraphicsContext3D::Attributes)
1149
1150 DELEGATE_TO_INTERNAL_R(getError, unsigned long)
1151
1152 DELEGATE_TO_INTERNAL_2(getFloatv, unsigned long, float*)
1153
1154 DELEGATE_TO_INTERNAL_4(getFramebufferAttachmentParameteriv, unsigned long, unsigned long, unsigned long, int*)
1155
1156 DELEGATE_TO_INTERNAL_2(getIntegerv, unsigned long, int*)
1157
1158 DELEGATE_TO_INTERNAL_3(getProgramiv, WebGLProgram*, unsigned long, int*)
1159
1160 DELEGATE_TO_INTERNAL_1R(getProgramInfoLog, WebGLProgram*, String)
1161
1162 DELEGATE_TO_INTERNAL_3(getRenderbufferParameteriv, unsigned long, unsigned long, int*)
1163
1164 DELEGATE_TO_INTERNAL_3(getShaderiv, WebGLShader*, unsigned long, int*)
1165
1166 DELEGATE_TO_INTERNAL_1R(getShaderInfoLog, WebGLShader*, String)
1167
1168 DELEGATE_TO_INTERNAL_1R(getShaderSource, WebGLShader*, String)
1169 DELEGATE_TO_INTERNAL_1R(getString, unsigned long, String)
1170
1171 DELEGATE_TO_INTERNAL_3(getTexParameterfv, unsigned long, unsigned long, float*)
1172 DELEGATE_TO_INTERNAL_3(getTexParameteriv, unsigned long, unsigned long, int*)
1173
1174 DELEGATE_TO_INTERNAL_3(getUniformfv, WebGLProgram*, long, float*)
1175 DELEGATE_TO_INTERNAL_3(getUniformiv, WebGLProgram*, long, int*)
1176
1177 DELEGATE_TO_INTERNAL_2R(getUniformLocation, WebGLProgram*, const String&, long)
1178
1179 DELEGATE_TO_INTERNAL_3(getVertexAttribfv, unsigned long, unsigned long, float*)
1180 DELEGATE_TO_INTERNAL_3(getVertexAttribiv, unsigned long, unsigned long, int*)
1181
1182 DELEGATE_TO_INTERNAL_2R(getVertexAttribOffset, unsigned long, unsigned long, long)
1183
1184 DELEGATE_TO_INTERNAL_2(hint, unsigned long, unsigned long)
1185 DELEGATE_TO_INTERNAL_1R(isBuffer, WebGLBuffer*, bool)
1186 DELEGATE_TO_INTERNAL_1R(isEnabled, unsigned long, bool)
1187 DELEGATE_TO_INTERNAL_1R(isFramebuffer, WebGLFramebuffer*, bool)
1188 DELEGATE_TO_INTERNAL_1R(isProgram, WebGLProgram*, bool)
1189 DELEGATE_TO_INTERNAL_1R(isRenderbuffer, WebGLRenderbuffer*, bool)
1190 DELEGATE_TO_INTERNAL_1R(isShader, WebGLShader*, bool)
1191 DELEGATE_TO_INTERNAL_1R(isTexture, WebGLTexture*, bool)
1192 DELEGATE_TO_INTERNAL_1(lineWidth, double)
1193 DELEGATE_TO_INTERNAL_1(linkProgram, WebGLProgram*)
1194 DELEGATE_TO_INTERNAL_2(pixelStorei, unsigned long, long)
1195 DELEGATE_TO_INTERNAL_2(polygonOffset, double, double)
1196
1197 DELEGATE_TO_INTERNAL_7(readPixels, long, long, unsigned long, unsigned long, unsigned long, unsigned long, void*)
1198
1199 DELEGATE_TO_INTERNAL(releaseShaderCompiler)
1200 DELEGATE_TO_INTERNAL_4(renderbufferStorage, unsigned long, unsigned long, unsigned long, unsigned long)
1201 DELEGATE_TO_INTERNAL_2(sampleCoverage, double, bool)
1202 DELEGATE_TO_INTERNAL_4(scissor, long, long, unsigned long, unsigned long)
1203 DELEGATE_TO_INTERNAL_2(shaderSource, WebGLShader*, const String&)
1204 DELEGATE_TO_INTERNAL_3(stencilFunc, unsigned long, long, unsigned long)
1205 DELEGATE_TO_INTERNAL_4(stencilFuncSeparate, unsigned long, unsigned long, long, unsigned long)
1206 DELEGATE_TO_INTERNAL_1(stencilMask, unsigned long)
1207 DELEGATE_TO_INTERNAL_2(stencilMaskSeparate, unsigned long, unsigned long)
1208 DELEGATE_TO_INTERNAL_3(stencilOp, unsigned long, unsigned long, unsigned long)
1209 DELEGATE_TO_INTERNAL_4(stencilOpSeparate, unsigned long, unsigned long, unsigned long, unsigned long)
1210
1211 DELEGATE_TO_INTERNAL_9R(texImage2D, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned, void*, int)
1212
1213 int GraphicsContext3D::texImage2D(unsigned target, unsigned level, Image* image,
1214                                   bool flipY, bool premultiplyAlpha)
1215 {
1216     Vector<uint8_t> imageData;
1217     unsigned int format, internalFormat;
1218     if (!extractImageData(image, flipY, premultiplyAlpha, imageData, &format, &internalFormat))
1219         return -1;
1220     return m_internal->texImage2D(target, level, internalFormat,
1221                                   image->width(), image->height(), 0,
1222                                   format, UNSIGNED_BYTE, imageData.data());
1223 }
1224
1225 DELEGATE_TO_INTERNAL_3(texParameterf, unsigned, unsigned, float)
1226 DELEGATE_TO_INTERNAL_3(texParameteri, unsigned, unsigned, int)
1227
1228 DELEGATE_TO_INTERNAL_9R(texSubImage2D, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned, void*, int)
1229
1230 int GraphicsContext3D::texSubImage2D(unsigned target,
1231                                      unsigned level,
1232                                      unsigned xoffset,
1233                                      unsigned yoffset,
1234                                      Image* image,
1235                                      bool flipY,
1236                                      bool premultiplyAlpha)
1237 {
1238     Vector<uint8_t> imageData;
1239     unsigned int format, internalFormat;
1240     if (!extractImageData(image, flipY, premultiplyAlpha, imageData, &format, &internalFormat))
1241         return -1;
1242     return m_internal->texSubImage2D(target, level, xoffset, yoffset,
1243                                      image->width(), image->height(),
1244                                      format, UNSIGNED_BYTE, imageData.data());
1245 }
1246
1247 DELEGATE_TO_INTERNAL_2(uniform1f, long, float)
1248 DELEGATE_TO_INTERNAL_3(uniform1fv, long, float*, int)
1249 DELEGATE_TO_INTERNAL_2(uniform1i, long, int)
1250 DELEGATE_TO_INTERNAL_3(uniform1iv, long, int*, int)
1251 DELEGATE_TO_INTERNAL_3(uniform2f, long, float, float)
1252 DELEGATE_TO_INTERNAL_3(uniform2fv, long, float*, int)
1253 DELEGATE_TO_INTERNAL_3(uniform2i, long, int, int)
1254 DELEGATE_TO_INTERNAL_3(uniform2iv, long, int*, int)
1255 DELEGATE_TO_INTERNAL_4(uniform3f, long, float, float, float)
1256 DELEGATE_TO_INTERNAL_3(uniform3fv, long, float*, int)
1257 DELEGATE_TO_INTERNAL_4(uniform3i, long, int, int, int)
1258 DELEGATE_TO_INTERNAL_3(uniform3iv, long, int*, int)
1259 DELEGATE_TO_INTERNAL_5(uniform4f, long, float, float, float, float)
1260 DELEGATE_TO_INTERNAL_3(uniform4fv, long, float*, int)
1261 DELEGATE_TO_INTERNAL_5(uniform4i, long, int, int, int, int)
1262 DELEGATE_TO_INTERNAL_3(uniform4iv, long, int*, int)
1263 DELEGATE_TO_INTERNAL_4(uniformMatrix2fv, long, bool, float*, int)
1264 DELEGATE_TO_INTERNAL_4(uniformMatrix3fv, long, bool, float*, int)
1265 DELEGATE_TO_INTERNAL_4(uniformMatrix4fv, long, bool, float*, int)
1266
1267 DELEGATE_TO_INTERNAL_1(useProgram, WebGLProgram*)
1268 DELEGATE_TO_INTERNAL_1(validateProgram, WebGLProgram*)
1269
1270 DELEGATE_TO_INTERNAL_2(vertexAttrib1f, unsigned long, float)
1271 DELEGATE_TO_INTERNAL_2(vertexAttrib1fv, unsigned long, float*)
1272 DELEGATE_TO_INTERNAL_3(vertexAttrib2f, unsigned long, float, float)
1273 DELEGATE_TO_INTERNAL_2(vertexAttrib2fv, unsigned long, float*)
1274 DELEGATE_TO_INTERNAL_4(vertexAttrib3f, unsigned long, float, float, float)
1275 DELEGATE_TO_INTERNAL_2(vertexAttrib3fv, unsigned long, float*)
1276 DELEGATE_TO_INTERNAL_5(vertexAttrib4f, unsigned long, float, float, float, float)
1277 DELEGATE_TO_INTERNAL_2(vertexAttrib4fv, unsigned long, float*)
1278 DELEGATE_TO_INTERNAL_6(vertexAttribPointer, unsigned long, int, int, bool, unsigned long, unsigned long)
1279
1280 DELEGATE_TO_INTERNAL_4(viewport, long, long, unsigned long, unsigned long)
1281
1282 DELEGATE_TO_INTERNAL_1(beginPaint, WebGLRenderingContext*)
1283 DELEGATE_TO_INTERNAL(endPaint)
1284
1285 DELEGATE_TO_INTERNAL_R(createBuffer, unsigned)
1286 DELEGATE_TO_INTERNAL_R(createFramebuffer, unsigned)
1287 DELEGATE_TO_INTERNAL_R(createProgram, unsigned)
1288 DELEGATE_TO_INTERNAL_R(createRenderbuffer, unsigned)
1289 DELEGATE_TO_INTERNAL_1R(createShader, unsigned long, unsigned)
1290 DELEGATE_TO_INTERNAL_R(createTexture, unsigned)
1291
1292 DELEGATE_TO_INTERNAL_1(deleteBuffer, unsigned)
1293 DELEGATE_TO_INTERNAL_1(deleteFramebuffer, unsigned)
1294 DELEGATE_TO_INTERNAL_1(deleteProgram, unsigned)
1295 DELEGATE_TO_INTERNAL_1(deleteRenderbuffer, unsigned)
1296 DELEGATE_TO_INTERNAL_1(deleteShader, unsigned)
1297 DELEGATE_TO_INTERNAL_1(deleteTexture, unsigned)
1298
1299 DELEGATE_TO_INTERNAL_1(synthesizeGLError, unsigned long)
1300
1301 bool GraphicsContext3D::isGLES2Compliant() const
1302 {
1303     return m_internal->isGLES2Compliant();
1304 }
1305
1306 } // namespace WebCore
1307
1308 #endif // ENABLE(3D_CANVAS)