ec14521408a951a795adc339644a01144d83fa26
[WebKit-https.git] / Source / WebKit / chromium / src / GraphicsContext3DChromium.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(WEBGL)
34
35 #include "GraphicsContext3D.h"
36
37 #include "CachedImage.h"
38 #include "CanvasRenderingContext.h"
39 #include "Chrome.h"
40 #include "ChromeClientImpl.h"
41 #include "DrawingBuffer.h"
42 #include "Extensions3DChromium.h"
43 #include "GraphicsContext3DPrivate.h"
44 #include "HTMLCanvasElement.h"
45 #include "HTMLImageElement.h"
46 #include "ImageBuffer.h"
47 #include "ImageData.h"
48 #include "WebKit.h"
49 #include "WebViewClient.h"
50 #include "WebViewImpl.h"
51 #include "platform/WebGraphicsContext3D.h"
52 #include "platform/WebKitPlatformSupport.h"
53
54 #include <stdio.h>
55 #include <wtf/FastMalloc.h>
56 #include <wtf/text/CString.h>
57
58 #if USE(CG)
59 #include "GraphicsContext.h"
60 #include "WebGLRenderingContext.h"
61 #include <CoreGraphics/CGContext.h>
62 #include <CoreGraphics/CGImage.h>
63 #endif
64
65 #if USE(SKIA)
66 #include "GrContext.h"
67 #include "GrGLInterface.h"
68 #endif
69
70 namespace {
71
72 // The limit of the number of textures we hold in the GrContext's bitmap->texture cache.
73 const int maxGaneshTextureCacheCount = 512;
74 // The limit of the bytes allocated toward textures in the GrContext's bitmap->texture cache.
75 const size_t maxGaneshTextureCacheBytes = 96 * 1024 * 1024;
76
77 }
78
79 // There are two levels of delegation in this file:
80 //
81 //   1. GraphicsContext3D delegates to GraphicsContext3DPrivate. This is done
82 //      so that we have some place to store data members common among
83 //      implementations; GraphicsContext3D only provides us the m_private
84 //      pointer. We always delegate to the GraphicsContext3DPrivate. While we
85 //      could sidestep it and go directly to the WebGraphicsContext3D in some
86 //      cases, it is better for consistency to always delegate through it.
87 //
88 //   2. GraphicsContext3DPrivate delegates to an implementation of
89 //      WebGraphicsContext3D. This is done so we have a place to inject an
90 //      implementation which remotes the OpenGL calls across processes.
91
92 namespace WebCore {
93
94 //----------------------------------------------------------------------
95 // GraphicsContext3DPrivate
96
97 GraphicsContext3DPrivate::GraphicsContext3DPrivate(PassOwnPtr<WebKit::WebGraphicsContext3D> webContext, bool preserveDrawingBuffer)
98     : m_impl(webContext)
99     , m_initializedAvailableExtensions(false)
100     , m_layerComposited(false)
101     , m_preserveDrawingBuffer(preserveDrawingBuffer)
102     , m_resourceSafety(ResourceSafetyUnknown)
103 #if USE(SKIA)
104     , m_grContext(0)
105 #elif USE(CG)
106     , m_renderOutputSize(0)
107 #else
108 #error Must port to your platform
109 #endif
110 {
111 }
112
113 GraphicsContext3DPrivate::~GraphicsContext3DPrivate()
114 {
115 #if USE(SKIA)
116     if (m_grContext) {
117         m_grContext->contextDestroyed();
118         GrSafeUnref(m_grContext);
119     }
120 #endif
121 }
122
123 PassRefPtr<GraphicsContext3D> GraphicsContext3DPrivate::createGraphicsContextFromWebContext(PassOwnPtr<WebKit::WebGraphicsContext3D> webContext, GraphicsContext3D::RenderStyle renderStyle, bool preserveDrawingBuffer)
124 {
125     bool renderDirectlyToHostWindow = renderStyle == GraphicsContext3D::RenderDirectlyToHostWindow;
126
127     RefPtr<GraphicsContext3D> context = adoptRef(new GraphicsContext3D(GraphicsContext3D::Attributes(), 0, renderDirectlyToHostWindow));
128
129     OwnPtr<GraphicsContext3DPrivate> priv = adoptPtr(new GraphicsContext3DPrivate(webContext, preserveDrawingBuffer));
130     context->m_private = priv.release();
131     return context.release();
132 }
133
134 namespace {
135
136 void getDrawingParameters(DrawingBuffer* drawingBuffer, WebKit::WebGraphicsContext3D* graphicsContext3D,
137                           Platform3DObject* frameBufferId, int* width, int* height)
138 {
139     if (drawingBuffer) {
140         *frameBufferId = drawingBuffer->framebuffer();
141         *width = drawingBuffer->size().width();
142         *height = drawingBuffer->size().height();
143     } else {
144         *frameBufferId = 0;
145         *width = graphicsContext3D->width();
146         *height = graphicsContext3D->height();
147     }
148 }
149
150 } // anonymous namespace
151
152 WebKit::WebGraphicsContext3D* GraphicsContext3DPrivate::extractWebGraphicsContext3D(GraphicsContext3D* context)
153 {
154     if (!context)
155         return 0;
156     return context->m_private->m_impl.get();
157 }
158
159 PlatformGraphicsContext3D GraphicsContext3DPrivate::platformGraphicsContext3D() const
160 {
161     return m_impl.get();
162 }
163
164 Platform3DObject GraphicsContext3DPrivate::platformTexture() const
165 {
166     return m_impl->getPlatformTextureId();
167 }
168
169 #if USE(SKIA)
170 class GrMemoryAllocationChangedCallback : public Extensions3DChromium::GpuMemoryAllocationChangedCallbackCHROMIUM {
171 public:
172     GrMemoryAllocationChangedCallback(GraphicsContext3DPrivate* context)
173         : m_context(context)
174     {
175     }
176
177     virtual void onGpuMemoryAllocationChanged(Extensions3DChromium::GpuMemoryAllocationCHROMIUM allocation)
178     {
179         GrContext* context = m_context->grContext();
180         if (!context)
181             return;
182
183         if (!allocation.gpuResourceSizeInBytes) {
184             context->freeGpuResources();
185             context->setTextureCacheLimits(0, 0);
186         } else
187             context->setTextureCacheLimits(maxGaneshTextureCacheCount, maxGaneshTextureCacheBytes);
188     }
189
190 private:
191     GraphicsContext3DPrivate* m_context;
192 };
193
194 GrContext* GraphicsContext3DPrivate::grContext()
195 {
196     if (!m_grContext) {
197         SkAutoTUnref<GrGLInterface> interface(m_impl->createGrGLInterface());
198         m_grContext = GrContext::Create(kOpenGL_Shaders_GrEngine, reinterpret_cast<GrPlatform3DContext>(interface.get()));
199         if (m_grContext) {
200             m_grContext->setTextureCacheLimits(maxGaneshTextureCacheCount, maxGaneshTextureCacheBytes);
201             Extensions3DChromium* extensions3DChromium = static_cast<Extensions3DChromium*>(getExtensions());
202             if (extensions3DChromium->supports("GL_CHROMIUM_gpu_memory_manager"))
203                 extensions3DChromium->setGpuMemoryAllocationChangedCallbackCHROMIUM(adoptPtr(new GrMemoryAllocationChangedCallback(this)));
204         }
205     }
206     return m_grContext;
207 }
208 #endif
209
210 void GraphicsContext3DPrivate::prepareTexture()
211 {
212     m_impl->prepareTexture();
213 }
214
215 void GraphicsContext3DPrivate::markContextChanged()
216 {
217     m_layerComposited = false;
218 }
219
220 void GraphicsContext3DPrivate::markLayerComposited()
221 {
222     m_layerComposited = true;
223 }
224
225 bool GraphicsContext3DPrivate::layerComposited() const
226 {
227     return m_layerComposited;
228 }
229
230 void GraphicsContext3DPrivate::paintFramebufferToCanvas(int framebuffer, int width, int height, bool premultiplyAlpha, ImageBuffer* imageBuffer)
231 {
232     unsigned char* pixels = 0;
233     size_t bufferSize = 4 * width * height;
234 #if USE(SKIA)
235     const SkBitmap* canvasBitmap = imageBuffer->context()->platformContext()->bitmap();
236     const SkBitmap* readbackBitmap = 0;
237     ASSERT(canvasBitmap->config() == SkBitmap::kARGB_8888_Config);
238     if (canvasBitmap->width() == width && canvasBitmap->height() == height) {
239         // This is the fastest and most common case. We read back
240         // directly into the canvas's backing store.
241         readbackBitmap = canvasBitmap;
242         m_resizingBitmap.reset();
243     } else {
244         // We need to allocate a temporary bitmap for reading back the
245         // pixel data. We will then use Skia to rescale this bitmap to
246         // the size of the canvas's backing store.
247         if (m_resizingBitmap.width() != width || m_resizingBitmap.height() != height) {
248             m_resizingBitmap.setConfig(SkBitmap::kARGB_8888_Config,
249                                        width,
250                                        height);
251             if (!m_resizingBitmap.allocPixels())
252                 return;
253         }
254         readbackBitmap = &m_resizingBitmap;
255     }
256
257     // Read back the frame buffer.
258     SkAutoLockPixels bitmapLock(*readbackBitmap);
259     pixels = static_cast<unsigned char*>(readbackBitmap->getPixels());
260 #elif USE(CG)
261     if (!m_renderOutput || m_renderOutputSize != bufferSize) {
262         m_renderOutput = adoptArrayPtr(new unsigned char[bufferSize]);
263         m_renderOutputSize = bufferSize;
264     }
265
266     pixels = m_renderOutput.get();
267 #else
268 #error Must port to your platform
269 #endif
270
271     m_impl->readBackFramebuffer(pixels, 4 * width * height, framebuffer, width, height);
272
273     if (premultiplyAlpha) {
274         for (size_t i = 0; i < bufferSize; i += 4) {
275             pixels[i + 0] = std::min(255, pixels[i + 0] * pixels[i + 3] / 255);
276             pixels[i + 1] = std::min(255, pixels[i + 1] * pixels[i + 3] / 255);
277             pixels[i + 2] = std::min(255, pixels[i + 2] * pixels[i + 3] / 255);
278         }
279     }
280
281 #if USE(SKIA)
282     readbackBitmap->notifyPixelsChanged();
283     if (m_resizingBitmap.readyToDraw()) {
284         // We need to draw the resizing bitmap into the canvas's backing store.
285         SkCanvas canvas(*canvasBitmap);
286         SkRect dst;
287         dst.set(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(canvasBitmap->width()), SkIntToScalar(canvasBitmap->height()));
288         canvas.drawBitmapRect(m_resizingBitmap, 0, dst);
289     }
290 #elif USE(CG)
291     GraphicsContext3D::paintToCanvas(pixels, width, height, imageBuffer->width(), imageBuffer->height(), imageBuffer->context()->platformContext());
292 #else
293 #error Must port to your platform
294 #endif
295 }
296
297 void GraphicsContext3DPrivate::paintRenderingResultsToCanvas(CanvasRenderingContext* context, DrawingBuffer* drawingBuffer)
298 {
299     ImageBuffer* imageBuffer = context->canvas()->buffer();
300     Platform3DObject framebufferId;
301     int width, height;
302     getDrawingParameters(drawingBuffer, m_impl.get(), &framebufferId, &width, &height);
303     paintFramebufferToCanvas(framebufferId, width, height, !m_impl->getContextAttributes().premultipliedAlpha, imageBuffer);
304 }
305
306 bool GraphicsContext3DPrivate::paintCompositedResultsToCanvas(CanvasRenderingContext* context)
307 {
308     return false;
309 }
310
311 PassRefPtr<ImageData> GraphicsContext3DPrivate::paintRenderingResultsToImageData(DrawingBuffer* drawingBuffer)
312 {
313     if (m_impl->getContextAttributes().premultipliedAlpha)
314         return 0;
315
316     Platform3DObject framebufferId;
317     int width, height;
318     getDrawingParameters(drawingBuffer, m_impl.get(), &framebufferId, &width, &height);
319
320     RefPtr<ImageData> imageData = ImageData::create(IntSize(width, height));
321     unsigned char* pixels = imageData->data()->data()->data();
322     size_t bufferSize = 4 * width * height;
323
324     m_impl->readBackFramebuffer(pixels, bufferSize, framebufferId, width, height);
325
326     for (size_t i = 0; i < bufferSize; i += 4)
327         std::swap(pixels[i], pixels[i + 2]);
328
329     return imageData.release();
330 }
331
332 void GraphicsContext3DPrivate::reshape(int width, int height)
333 {
334     if (width == m_impl->width() && height == m_impl->height())
335         return;
336
337     m_impl->reshape(width, height);
338 }
339
340 IntSize GraphicsContext3DPrivate::getInternalFramebufferSize() const
341 {
342     return IntSize(m_impl->width(), m_impl->height());
343 }
344
345 bool GraphicsContext3DPrivate::isContextLost()
346 {
347     return m_impl->isContextLost();
348 }
349
350 // Macros to assist in delegating from GraphicsContext3DPrivate to
351 // WebGraphicsContext3D.
352
353 #define DELEGATE_TO_IMPL(name) \
354 void GraphicsContext3DPrivate::name() \
355 { \
356     m_impl->name(); \
357 }
358
359 #define DELEGATE_TO_IMPL_R(name, rt)           \
360 rt GraphicsContext3DPrivate::name() \
361 { \
362     return m_impl->name(); \
363 }
364
365 #define DELEGATE_TO_IMPL_1(name, t1) \
366 void GraphicsContext3DPrivate::name(t1 a1) \
367 { \
368     m_impl->name(a1); \
369 }
370
371 #define DELEGATE_TO_IMPL_1R(name, t1, rt)    \
372 rt GraphicsContext3DPrivate::name(t1 a1) \
373 { \
374     return m_impl->name(a1); \
375 }
376
377 #define DELEGATE_TO_IMPL_2(name, t1, t2) \
378 void GraphicsContext3DPrivate::name(t1 a1, t2 a2) \
379 { \
380     m_impl->name(a1, a2); \
381 }
382
383 #define DELEGATE_TO_IMPL_2R(name, t1, t2, rt)  \
384 rt GraphicsContext3DPrivate::name(t1 a1, t2 a2) \
385 { \
386     return m_impl->name(a1, a2); \
387 }
388
389 #define DELEGATE_TO_IMPL_3(name, t1, t2, t3)   \
390 void GraphicsContext3DPrivate::name(t1 a1, t2 a2, t3 a3)    \
391 { \
392     m_impl->name(a1, a2, a3);                  \
393 }
394
395 #define DELEGATE_TO_IMPL_3R(name, t1, t2, t3, rt)   \
396 rt GraphicsContext3DPrivate::name(t1 a1, t2 a2, t3 a3)    \
397 { \
398     return m_impl->name(a1, a2, a3);                  \
399 }
400
401 #define DELEGATE_TO_IMPL_4(name, t1, t2, t3, t4)    \
402 void GraphicsContext3DPrivate::name(t1 a1, t2 a2, t3 a3, t4 a4)  \
403 { \
404     m_impl->name(a1, a2, a3, a4);              \
405 }
406
407 #define DELEGATE_TO_IMPL_4R(name, t1, t2, t3, t4, rt)       \
408 rt GraphicsContext3DPrivate::name(t1 a1, t2 a2, t3 a3, t4 a4)        \
409 { \
410     return m_impl->name(a1, a2, a3, a4);           \
411 }
412
413 #define DELEGATE_TO_IMPL_5(name, t1, t2, t3, t4, t5)      \
414 void GraphicsContext3DPrivate::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5)        \
415 { \
416     m_impl->name(a1, a2, a3, a4, a5);   \
417 }
418
419 #define DELEGATE_TO_IMPL_5R(name, t1, t2, t3, t4, t5, rt)      \
420 rt GraphicsContext3DPrivate::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5)        \
421 { \
422     return m_impl->name(a1, a2, a3, a4, a5);   \
423 }
424
425 #define DELEGATE_TO_IMPL_6(name, t1, t2, t3, t4, t5, t6)  \
426 void GraphicsContext3DPrivate::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6) \
427 { \
428     m_impl->name(a1, a2, a3, a4, a5, a6);       \
429 }
430
431 #define DELEGATE_TO_IMPL_6R(name, t1, t2, t3, t4, t5, t6, rt)  \
432 rt GraphicsContext3DPrivate::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6) \
433 { \
434     return m_impl->name(a1, a2, a3, a4, a5, a6);       \
435 }
436
437 #define DELEGATE_TO_IMPL_7(name, t1, t2, t3, t4, t5, t6, t7) \
438 void GraphicsContext3DPrivate::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7) \
439 { \
440     m_impl->name(a1, a2, a3, a4, a5, a6, a7);   \
441 }
442
443 #define DELEGATE_TO_IMPL_7R(name, t1, t2, t3, t4, t5, t6, t7, rt) \
444 rt GraphicsContext3DPrivate::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7) \
445 { \
446     return m_impl->name(a1, a2, a3, a4, a5, a6, a7);   \
447 }
448
449 #define DELEGATE_TO_IMPL_8(name, t1, t2, t3, t4, t5, t6, t7, t8)       \
450 void GraphicsContext3DPrivate::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8) \
451 { \
452     m_impl->name(a1, a2, a3, a4, a5, a6, a7, a8);      \
453 }
454
455 #define DELEGATE_TO_IMPL_9(name, t1, t2, t3, t4, t5, t6, t7, t8, t9)       \
456 void GraphicsContext3DPrivate::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8, t9 a9) \
457 { \
458     m_impl->name(a1, a2, a3, a4, a5, a6, a7, a8, a9);      \
459 }
460
461 #define DELEGATE_TO_IMPL_9R(name, t1, t2, t3, t4, t5, t6, t7, t8, t9, rt) \
462 rt GraphicsContext3DPrivate::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8, t9 a9) \
463 { \
464     return m_impl->name(a1, a2, a3, a4, a5, a6, a7, a8, a9);   \
465 }
466
467 #define DELEGATE_TO_IMPL_10(name, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) \
468 void GraphicsContext3DPrivate::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8, t9 a9, t10 a10) \
469 { \
470     m_impl->name(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); \
471 }
472
473 DELEGATE_TO_IMPL_R(makeContextCurrent, bool)
474
475 bool GraphicsContext3DPrivate::isGLES2Compliant() const
476 {
477     return m_impl->isGLES2Compliant();
478 }
479
480 DELEGATE_TO_IMPL_1(activeTexture, GC3Denum)
481 DELEGATE_TO_IMPL_2(attachShader, Platform3DObject, Platform3DObject)
482
483 void GraphicsContext3DPrivate::bindAttribLocation(Platform3DObject program, GC3Duint index, const String& name)
484 {
485     m_impl->bindAttribLocation(program, index, name.utf8().data());
486 }
487
488 DELEGATE_TO_IMPL_2(bindBuffer, GC3Denum, Platform3DObject)
489 DELEGATE_TO_IMPL_2(bindFramebuffer, GC3Denum, Platform3DObject)
490 DELEGATE_TO_IMPL_2(bindRenderbuffer, GC3Denum, Platform3DObject)
491 DELEGATE_TO_IMPL_2(bindTexture, GC3Denum, Platform3DObject)
492 DELEGATE_TO_IMPL_4(blendColor, GC3Dclampf, GC3Dclampf, GC3Dclampf, GC3Dclampf)
493 DELEGATE_TO_IMPL_1(blendEquation, GC3Denum)
494 DELEGATE_TO_IMPL_2(blendEquationSeparate, GC3Denum, GC3Denum)
495 DELEGATE_TO_IMPL_2(blendFunc, GC3Denum, GC3Denum)
496 DELEGATE_TO_IMPL_4(blendFuncSeparate, GC3Denum, GC3Denum, GC3Denum, GC3Denum)
497
498 void GraphicsContext3DPrivate::bufferData(GC3Denum target, GC3Dsizeiptr size, GC3Denum usage)
499 {
500     m_impl->bufferData(target, size, 0, usage);
501 }
502
503 void GraphicsContext3DPrivate::bufferData(GC3Denum target, GC3Dsizeiptr size, const void* data, GC3Denum usage)
504 {
505     m_impl->bufferData(target, size, data, usage);
506 }
507
508 void GraphicsContext3DPrivate::bufferSubData(GC3Denum target, GC3Dintptr offset, GC3Dsizeiptr size, const void* data)
509 {
510     m_impl->bufferSubData(target, offset, size, data);
511 }
512
513 DELEGATE_TO_IMPL_1R(checkFramebufferStatus, GC3Denum, GC3Denum)
514 DELEGATE_TO_IMPL_1(clear, GC3Dbitfield)
515 DELEGATE_TO_IMPL_4(clearColor, GC3Dclampf, GC3Dclampf, GC3Dclampf, GC3Dclampf)
516 DELEGATE_TO_IMPL_1(clearDepth, GC3Dclampf)
517 DELEGATE_TO_IMPL_1(clearStencil, GC3Dint)
518 DELEGATE_TO_IMPL_4(colorMask, GC3Dboolean, GC3Dboolean, GC3Dboolean, GC3Dboolean)
519 DELEGATE_TO_IMPL_1(compileShader, Platform3DObject)
520
521 DELEGATE_TO_IMPL_8(compressedTexImage2D, GC3Denum, GC3Dint, GC3Denum, GC3Dint, GC3Dint, GC3Dsizei, GC3Dsizei, const void*)
522 DELEGATE_TO_IMPL_9(compressedTexSubImage2D, GC3Denum, GC3Dint, GC3Dint, GC3Dint, GC3Dint, GC3Dint, GC3Denum, GC3Dsizei, const void*)
523 DELEGATE_TO_IMPL_8(copyTexImage2D, GC3Denum, GC3Dint, GC3Denum, GC3Dint, GC3Dint, GC3Dsizei, GC3Dsizei, GC3Dint)
524 DELEGATE_TO_IMPL_8(copyTexSubImage2D, GC3Denum, GC3Dint, GC3Dint, GC3Dint, GC3Dint, GC3Dint, GC3Dsizei, GC3Dsizei)
525 DELEGATE_TO_IMPL_1(cullFace, GC3Denum)
526 DELEGATE_TO_IMPL_1(depthFunc, GC3Denum)
527 DELEGATE_TO_IMPL_1(depthMask, GC3Dboolean)
528 DELEGATE_TO_IMPL_2(depthRange, GC3Dclampf, GC3Dclampf)
529 DELEGATE_TO_IMPL_2(detachShader, Platform3DObject, Platform3DObject)
530 DELEGATE_TO_IMPL_1(disable, GC3Denum)
531 DELEGATE_TO_IMPL_1(disableVertexAttribArray, GC3Duint)
532 DELEGATE_TO_IMPL_3(drawArrays, GC3Denum, GC3Dint, GC3Dsizei)
533 DELEGATE_TO_IMPL_4(drawElements, GC3Denum, GC3Dsizei, GC3Denum, GC3Dsizeiptr)
534
535 DELEGATE_TO_IMPL_1(enable, GC3Denum)
536 DELEGATE_TO_IMPL_1(enableVertexAttribArray, GC3Duint)
537 DELEGATE_TO_IMPL(finish)
538 DELEGATE_TO_IMPL(flush)
539 DELEGATE_TO_IMPL_4(framebufferRenderbuffer, GC3Denum, GC3Denum, GC3Denum, Platform3DObject)
540 DELEGATE_TO_IMPL_5(framebufferTexture2D, GC3Denum, GC3Denum, GC3Denum, Platform3DObject, GC3Dint)
541 DELEGATE_TO_IMPL_1(frontFace, GC3Denum)
542 DELEGATE_TO_IMPL_1(generateMipmap, GC3Denum)
543
544 bool GraphicsContext3DPrivate::getActiveAttrib(Platform3DObject program, GC3Duint index, ActiveInfo& info)
545 {
546     WebKit::WebGraphicsContext3D::ActiveInfo webInfo;
547     if (!m_impl->getActiveAttrib(program, index, webInfo))
548         return false;
549     info.name = webInfo.name;
550     info.type = webInfo.type;
551     info.size = webInfo.size;
552     return true;
553 }
554
555 bool GraphicsContext3DPrivate::getActiveUniform(Platform3DObject program, GC3Duint index, ActiveInfo& info)
556 {
557     WebKit::WebGraphicsContext3D::ActiveInfo webInfo;
558     if (!m_impl->getActiveUniform(program, index, webInfo))
559         return false;
560     info.name = webInfo.name;
561     info.type = webInfo.type;
562     info.size = webInfo.size;
563     return true;
564 }
565
566 DELEGATE_TO_IMPL_4(getAttachedShaders, Platform3DObject, GC3Dsizei, GC3Dsizei*, Platform3DObject*)
567
568 GC3Dint GraphicsContext3DPrivate::getAttribLocation(Platform3DObject program, const String& name)
569 {
570     return m_impl->getAttribLocation(program, name.utf8().data());
571 }
572
573 DELEGATE_TO_IMPL_2(getBooleanv, GC3Denum, GC3Dboolean*)
574
575 DELEGATE_TO_IMPL_3(getBufferParameteriv, GC3Denum, GC3Denum, GC3Dint*)
576
577 GraphicsContext3D::Attributes GraphicsContext3DPrivate::getContextAttributes()
578 {
579     WebKit::WebGraphicsContext3D::Attributes webAttributes = m_impl->getContextAttributes();
580     GraphicsContext3D::Attributes attributes;
581     attributes.alpha = webAttributes.alpha;
582     attributes.depth = webAttributes.depth;
583     attributes.stencil = webAttributes.stencil;
584     attributes.antialias = webAttributes.antialias;
585     attributes.premultipliedAlpha = webAttributes.premultipliedAlpha;
586     attributes.preserveDrawingBuffer = m_preserveDrawingBuffer;
587     attributes.preferDiscreteGPU = webAttributes.preferDiscreteGPU;
588     return attributes;
589 }
590
591 DELEGATE_TO_IMPL_R(getError, GC3Denum)
592
593 DELEGATE_TO_IMPL_2(getFloatv, GC3Denum, GC3Dfloat*)
594
595 DELEGATE_TO_IMPL_4(getFramebufferAttachmentParameteriv, GC3Denum, GC3Denum, GC3Denum, GC3Dint*)
596
597 DELEGATE_TO_IMPL_2(getIntegerv, GC3Denum, GC3Dint*)
598
599 DELEGATE_TO_IMPL_3(getProgramiv, Platform3DObject, GC3Denum, GC3Dint*)
600
601 String GraphicsContext3DPrivate::getProgramInfoLog(Platform3DObject program)
602 {
603     return m_impl->getProgramInfoLog(program);
604 }
605
606 DELEGATE_TO_IMPL_3(getRenderbufferParameteriv, GC3Denum, GC3Denum, GC3Dint*)
607
608 DELEGATE_TO_IMPL_3(getShaderiv, Platform3DObject, GC3Denum, GC3Dint*)
609
610 String GraphicsContext3DPrivate::getShaderInfoLog(Platform3DObject shader)
611 {
612     return m_impl->getShaderInfoLog(shader);
613 }
614
615 String GraphicsContext3DPrivate::getShaderSource(Platform3DObject shader)
616 {
617     return m_impl->getShaderSource(shader);
618 }
619
620 String GraphicsContext3DPrivate::getString(GC3Denum name)
621 {
622     return m_impl->getString(name);
623 }
624
625 DELEGATE_TO_IMPL_3(getTexParameterfv, GC3Denum, GC3Denum, GC3Dfloat*)
626 DELEGATE_TO_IMPL_3(getTexParameteriv, GC3Denum, GC3Denum, GC3Dint*)
627
628 DELEGATE_TO_IMPL_3(getUniformfv, Platform3DObject, GC3Dint, GC3Dfloat*)
629 DELEGATE_TO_IMPL_3(getUniformiv, Platform3DObject, GC3Dint, GC3Dint*)
630
631 GC3Dint GraphicsContext3DPrivate::getUniformLocation(Platform3DObject program, const String& name)
632 {
633     return m_impl->getUniformLocation(program, name.utf8().data());
634 }
635
636 DELEGATE_TO_IMPL_3(getVertexAttribfv, GC3Duint, GC3Denum, GC3Dfloat*)
637 DELEGATE_TO_IMPL_3(getVertexAttribiv, GC3Duint, GC3Denum, GC3Dint*)
638
639 DELEGATE_TO_IMPL_2R(getVertexAttribOffset, GC3Duint, GC3Denum, GC3Dsizeiptr)
640
641 DELEGATE_TO_IMPL_2(hint, GC3Denum, GC3Denum)
642 DELEGATE_TO_IMPL_1R(isBuffer, Platform3DObject, GC3Dboolean)
643 DELEGATE_TO_IMPL_1R(isEnabled, GC3Denum, GC3Dboolean)
644 DELEGATE_TO_IMPL_1R(isFramebuffer, Platform3DObject, GC3Dboolean)
645 DELEGATE_TO_IMPL_1R(isProgram, Platform3DObject, GC3Dboolean)
646 DELEGATE_TO_IMPL_1R(isRenderbuffer, Platform3DObject, GC3Dboolean)
647 DELEGATE_TO_IMPL_1R(isShader, Platform3DObject, GC3Dboolean)
648 DELEGATE_TO_IMPL_1R(isTexture, Platform3DObject, GC3Dboolean)
649 DELEGATE_TO_IMPL_1(lineWidth, GC3Dfloat)
650 DELEGATE_TO_IMPL_1(linkProgram, Platform3DObject)
651 DELEGATE_TO_IMPL_2(pixelStorei, GC3Denum, GC3Dint)
652 DELEGATE_TO_IMPL_2(polygonOffset, GC3Dfloat, GC3Dfloat)
653 DELEGATE_TO_IMPL_7(readPixels, GC3Dint, GC3Dint, GC3Dsizei, GC3Dsizei, GC3Denum, GC3Denum, void*)
654 DELEGATE_TO_IMPL(releaseShaderCompiler)
655 DELEGATE_TO_IMPL_4(renderbufferStorage, GC3Denum, GC3Denum, GC3Dsizei, GC3Dsizei)
656 DELEGATE_TO_IMPL_2(sampleCoverage, GC3Dclampf, GC3Dboolean)
657 DELEGATE_TO_IMPL_4(scissor, GC3Dint, GC3Dint, GC3Dsizei, GC3Dsizei)
658
659 void GraphicsContext3DPrivate::shaderSource(Platform3DObject shader, const String& string)
660 {
661     m_impl->shaderSource(shader, string.utf8().data());
662 }
663
664 DELEGATE_TO_IMPL_3(stencilFunc, GC3Denum, GC3Dint, GC3Duint)
665 DELEGATE_TO_IMPL_4(stencilFuncSeparate, GC3Denum, GC3Denum, GC3Dint, GC3Duint)
666 DELEGATE_TO_IMPL_1(stencilMask, GC3Duint)
667 DELEGATE_TO_IMPL_2(stencilMaskSeparate, GC3Denum, GC3Duint)
668 DELEGATE_TO_IMPL_3(stencilOp, GC3Denum, GC3Denum, GC3Denum)
669 DELEGATE_TO_IMPL_4(stencilOpSeparate, GC3Denum, GC3Denum, GC3Denum, GC3Denum)
670
671 bool GraphicsContext3DPrivate::texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, const void* pixels)
672 {
673     m_impl->texImage2D(target, level, internalformat, width, height, border, format, type, pixels);
674     return true;
675 }
676
677 DELEGATE_TO_IMPL_3(texParameterf, GC3Denum, GC3Denum, GC3Dfloat)
678 DELEGATE_TO_IMPL_3(texParameteri, GC3Denum, GC3Denum, GC3Dint)
679
680 void GraphicsContext3DPrivate::texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, const void* pixels)
681 {
682     m_impl->texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels);
683 }
684
685 DELEGATE_TO_IMPL_2(uniform1f, GC3Dint, GC3Dfloat)
686
687 void GraphicsContext3DPrivate::uniform1fv(GC3Dint location, GC3Dsizei size, GC3Dfloat* v)
688 {
689     m_impl->uniform1fv(location, size, v);
690 }
691
692 DELEGATE_TO_IMPL_2(uniform1i, GC3Dint, GC3Dint)
693
694 void GraphicsContext3DPrivate::uniform1iv(GC3Dint location, GC3Dsizei size, GC3Dint* v)
695 {
696     m_impl->uniform1iv(location, size, v);
697 }
698
699 DELEGATE_TO_IMPL_3(uniform2f, GC3Dint, GC3Dfloat, GC3Dfloat)
700
701 void GraphicsContext3DPrivate::uniform2fv(GC3Dint location, GC3Dsizei size, GC3Dfloat* v)
702 {
703     m_impl->uniform2fv(location, size, v);
704 }
705
706 DELEGATE_TO_IMPL_3(uniform2i, GC3Dint, GC3Dint, GC3Dint)
707
708 void GraphicsContext3DPrivate::uniform2iv(GC3Dint location, GC3Dsizei size, GC3Dint* v)
709 {
710     m_impl->uniform2iv(location, size, v);
711 }
712
713 DELEGATE_TO_IMPL_4(uniform3f, GC3Dint, GC3Dfloat, GC3Dfloat, GC3Dfloat)
714
715 void GraphicsContext3DPrivate::uniform3fv(GC3Dint location, GC3Dsizei size, GC3Dfloat* v)
716 {
717     m_impl->uniform3fv(location, size, v);
718 }
719
720 DELEGATE_TO_IMPL_4(uniform3i, GC3Dint, GC3Dint, GC3Dint, GC3Dint)
721
722 void GraphicsContext3DPrivate::uniform3iv(GC3Dint location, GC3Dsizei size, GC3Dint* v)
723 {
724     m_impl->uniform3iv(location, size, v);
725 }
726
727 DELEGATE_TO_IMPL_5(uniform4f, GC3Dint, GC3Dfloat, GC3Dfloat, GC3Dfloat, GC3Dfloat)
728
729 void GraphicsContext3DPrivate::uniform4fv(GC3Dint location, GC3Dsizei size, GC3Dfloat* v)
730 {
731     m_impl->uniform4fv(location, size, v);
732 }
733
734 DELEGATE_TO_IMPL_5(uniform4i, GC3Dint, GC3Dint, GC3Dint, GC3Dint, GC3Dint)
735
736 void GraphicsContext3DPrivate::uniform4iv(GC3Dint location, GC3Dsizei size, GC3Dint* v)
737 {
738     m_impl->uniform4iv(location, size, v);
739 }
740
741 void GraphicsContext3DPrivate::uniformMatrix2fv(GC3Dint location, GC3Dsizei size, GC3Dboolean transpose, GC3Dfloat* value)
742 {
743     m_impl->uniformMatrix2fv(location, size, transpose, value);
744 }
745
746 void GraphicsContext3DPrivate::uniformMatrix3fv(GC3Dint location, GC3Dsizei size, GC3Dboolean transpose, GC3Dfloat* value)
747 {
748     m_impl->uniformMatrix3fv(location, size, transpose, value);
749 }
750
751 void GraphicsContext3DPrivate::uniformMatrix4fv(GC3Dint location, GC3Dsizei size, GC3Dboolean transpose, GC3Dfloat* value)
752 {
753     m_impl->uniformMatrix4fv(location, size, transpose, value);
754 }
755
756 DELEGATE_TO_IMPL_1(useProgram, Platform3DObject)
757 DELEGATE_TO_IMPL_1(validateProgram, Platform3DObject)
758
759 DELEGATE_TO_IMPL_2(vertexAttrib1f, GC3Duint, GC3Dfloat)
760 DELEGATE_TO_IMPL_2(vertexAttrib1fv, GC3Duint, GC3Dfloat*)
761 DELEGATE_TO_IMPL_3(vertexAttrib2f, GC3Duint, GC3Dfloat, GC3Dfloat)
762 DELEGATE_TO_IMPL_2(vertexAttrib2fv, GC3Duint, GC3Dfloat*)
763 DELEGATE_TO_IMPL_4(vertexAttrib3f, GC3Duint, GC3Dfloat, GC3Dfloat, GC3Dfloat)
764 DELEGATE_TO_IMPL_2(vertexAttrib3fv, GC3Duint, GC3Dfloat*)
765 DELEGATE_TO_IMPL_5(vertexAttrib4f, GC3Duint, GC3Dfloat, GC3Dfloat, GC3Dfloat, GC3Dfloat)
766 DELEGATE_TO_IMPL_2(vertexAttrib4fv, GC3Duint, GC3Dfloat*)
767 DELEGATE_TO_IMPL_6(vertexAttribPointer, GC3Duint, GC3Dint, GC3Denum, GC3Dboolean, GC3Dsizei, GC3Dsizeiptr)
768
769 DELEGATE_TO_IMPL_4(viewport, GC3Dint, GC3Dint, GC3Dsizei, GC3Dsizei)
770
771 DELEGATE_TO_IMPL_R(createBuffer, Platform3DObject)
772 DELEGATE_TO_IMPL_R(createFramebuffer, Platform3DObject)
773 DELEGATE_TO_IMPL_R(createProgram, Platform3DObject)
774 DELEGATE_TO_IMPL_R(createRenderbuffer, Platform3DObject)
775 DELEGATE_TO_IMPL_1R(createShader, GC3Denum, Platform3DObject)
776 DELEGATE_TO_IMPL_R(createTexture, Platform3DObject)
777
778 DELEGATE_TO_IMPL_1(deleteBuffer, Platform3DObject)
779 DELEGATE_TO_IMPL_1(deleteFramebuffer, Platform3DObject)
780 DELEGATE_TO_IMPL_1(deleteProgram, Platform3DObject)
781 DELEGATE_TO_IMPL_1(deleteRenderbuffer, Platform3DObject)
782 DELEGATE_TO_IMPL_1(deleteShader, Platform3DObject)
783 DELEGATE_TO_IMPL_1(deleteTexture, Platform3DObject)
784
785 DELEGATE_TO_IMPL_1(synthesizeGLError, GC3Denum)
786
787 Extensions3D* GraphicsContext3DPrivate::getExtensions()
788 {
789     if (!m_extensions)
790         m_extensions = adoptPtr(new Extensions3DChromium(this));
791     return m_extensions.get();
792 }
793
794 bool GraphicsContext3DPrivate::isResourceSafe()
795 {
796     if (m_resourceSafety == ResourceSafetyUnknown)
797         m_resourceSafety = getExtensions()->isEnabled("GL_CHROMIUM_resource_safe") ? ResourceSafe : ResourceUnsafe;
798     return m_resourceSafety == ResourceSafe;
799 }
800
801 namespace {
802
803 void splitStringHelper(const String& str, HashSet<String>& set)
804 {
805     Vector<String> substrings;
806     str.split(" ", substrings);
807     for (size_t i = 0; i < substrings.size(); ++i)
808         set.add(substrings[i]);
809 }
810
811 String mapExtensionName(const String& name)
812 {
813     if (name == "GL_ANGLE_framebuffer_blit"
814         || name == "GL_ANGLE_framebuffer_multisample")
815         return "GL_CHROMIUM_framebuffer_multisample";
816     return name;
817 }
818
819 } // anonymous namespace
820
821 void GraphicsContext3DPrivate::initializeExtensions()
822 {
823     if (m_initializedAvailableExtensions)
824         return;
825
826     m_initializedAvailableExtensions = true;
827     bool success = makeContextCurrent();
828     ASSERT(success);
829     if (!success)
830         return;
831
832     String extensionsString = getString(GraphicsContext3D::EXTENSIONS);
833     splitStringHelper(extensionsString, m_enabledExtensions);
834
835     String requestableExtensionsString = m_impl->getRequestableExtensionsCHROMIUM();
836     splitStringHelper(requestableExtensionsString, m_requestableExtensions);
837 }
838
839
840 bool GraphicsContext3DPrivate::supportsExtension(const String& name)
841 {
842     initializeExtensions();
843     String mappedName = mapExtensionName(name);
844     return m_enabledExtensions.contains(mappedName) || m_requestableExtensions.contains(mappedName);
845 }
846
847 bool GraphicsContext3DPrivate::ensureExtensionEnabled(const String& name)
848 {
849     initializeExtensions();
850
851     String mappedName = mapExtensionName(name);
852     if (m_enabledExtensions.contains(mappedName))
853         return true;
854
855     if (m_requestableExtensions.contains(mappedName)) {
856         m_impl->requestExtensionCHROMIUM(mappedName.ascii().data());
857         m_enabledExtensions.clear();
858         m_requestableExtensions.clear();
859         m_initializedAvailableExtensions = false;
860     }
861
862     initializeExtensions();
863     return m_enabledExtensions.contains(mappedName);
864 }
865
866 bool GraphicsContext3DPrivate::isExtensionEnabled(const String& name)
867 {
868     initializeExtensions();
869     String mappedName = mapExtensionName(name);
870     return m_enabledExtensions.contains(mappedName);
871 }
872
873 DELEGATE_TO_IMPL_4(postSubBufferCHROMIUM, int, int, int, int)
874
875 DELEGATE_TO_IMPL_4R(mapBufferSubDataCHROMIUM, GC3Denum, GC3Dsizeiptr, GC3Dsizei, GC3Denum, void*)
876 DELEGATE_TO_IMPL_1(unmapBufferSubDataCHROMIUM, const void*)
877 DELEGATE_TO_IMPL_9R(mapTexSubImage2DCHROMIUM, GC3Denum, GC3Dint, GC3Dint, GC3Dint, GC3Dsizei, GC3Dsizei, GC3Denum, GC3Denum, GC3Denum, void*)
878 DELEGATE_TO_IMPL_1(unmapTexSubImage2DCHROMIUM, const void*)
879
880 DELEGATE_TO_IMPL_1(setVisibilityCHROMIUM, bool);
881
882 DELEGATE_TO_IMPL_10(blitFramebufferCHROMIUM, GC3Dint, GC3Dint, GC3Dint, GC3Dint, GC3Dint, GC3Dint, GC3Dint, GC3Dint, GC3Dbitfield, GC3Denum)
883 DELEGATE_TO_IMPL_5(renderbufferStorageMultisampleCHROMIUM, GC3Denum, GC3Dsizei, GC3Denum, GC3Dsizei, GC3Dsizei)
884
885 DELEGATE_TO_IMPL(rateLimitOffscreenContextCHROMIUM)
886 DELEGATE_TO_IMPL_R(getGraphicsResetStatusARB, GC3Denum)
887
888 DELEGATE_TO_IMPL_1R(getTranslatedShaderSourceANGLE, Platform3DObject, String)
889 DELEGATE_TO_IMPL_5(texImageIOSurface2DCHROMIUM, GC3Denum, GC3Dint, GC3Dint, GC3Duint, GC3Duint)
890 DELEGATE_TO_IMPL_5(texStorage2DEXT, GC3Denum, GC3Dint, GC3Duint, GC3Dint, GC3Dint)
891
892 DELEGATE_TO_IMPL_R(createQueryEXT, Platform3DObject)
893 DELEGATE_TO_IMPL_1(deleteQueryEXT, Platform3DObject)
894 DELEGATE_TO_IMPL_1R(isQueryEXT, Platform3DObject, GC3Dboolean)
895 DELEGATE_TO_IMPL_2(beginQueryEXT, GC3Denum, Platform3DObject)
896 DELEGATE_TO_IMPL_1(endQueryEXT, GC3Denum)
897 DELEGATE_TO_IMPL_3(getQueryivEXT, GC3Denum, GC3Denum, GC3Dint*)
898 DELEGATE_TO_IMPL_3(getQueryObjectuivEXT, Platform3DObject, GC3Denum, GC3Duint*)
899
900 //----------------------------------------------------------------------
901 // GraphicsContext3D
902 //
903
904 // Macros to assist in delegating from GraphicsContext3D to
905 // GraphicsContext3DPrivate.
906
907 #define DELEGATE_TO_INTERNAL(name) \
908 void GraphicsContext3D::name() \
909 { \
910     m_private->name(); \
911 }
912
913 #define DELEGATE_TO_INTERNAL_R(name, rt)           \
914 rt GraphicsContext3D::name() \
915 { \
916     return m_private->name(); \
917 }
918
919 #define DELEGATE_TO_INTERNAL_1(name, t1) \
920 void GraphicsContext3D::name(t1 a1) \
921 { \
922     m_private->name(a1); \
923 }
924
925 #define DELEGATE_TO_INTERNAL_1R(name, t1, rt)    \
926 rt GraphicsContext3D::name(t1 a1) \
927 { \
928     return m_private->name(a1); \
929 }
930
931 #define DELEGATE_TO_INTERNAL_2(name, t1, t2) \
932 void GraphicsContext3D::name(t1 a1, t2 a2) \
933 { \
934     m_private->name(a1, a2); \
935 }
936
937 #define DELEGATE_TO_INTERNAL_2R(name, t1, t2, rt)  \
938 rt GraphicsContext3D::name(t1 a1, t2 a2) \
939 { \
940     return m_private->name(a1, a2); \
941 }
942
943 #define DELEGATE_TO_INTERNAL_3(name, t1, t2, t3)   \
944 void GraphicsContext3D::name(t1 a1, t2 a2, t3 a3)    \
945 { \
946     m_private->name(a1, a2, a3);                  \
947 }
948
949 #define DELEGATE_TO_INTERNAL_3R(name, t1, t2, t3, rt)   \
950 rt GraphicsContext3D::name(t1 a1, t2 a2, t3 a3)    \
951 { \
952     return m_private->name(a1, a2, a3);                  \
953 }
954
955 #define DELEGATE_TO_INTERNAL_4(name, t1, t2, t3, t4)    \
956 void GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4)  \
957 { \
958     m_private->name(a1, a2, a3, a4);              \
959 }
960
961 #define DELEGATE_TO_INTERNAL_4R(name, t1, t2, t3, t4, rt)    \
962 rt GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4)  \
963 { \
964     return m_private->name(a1, a2, a3, a4);           \
965 }
966
967 #define DELEGATE_TO_INTERNAL_5(name, t1, t2, t3, t4, t5)      \
968 void GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5)        \
969 { \
970     m_private->name(a1, a2, a3, a4, a5);   \
971 }
972
973 #define DELEGATE_TO_INTERNAL_6(name, t1, t2, t3, t4, t5, t6)  \
974 void GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6) \
975 { \
976     m_private->name(a1, a2, a3, a4, a5, a6);   \
977 }
978
979 #define DELEGATE_TO_INTERNAL_6R(name, t1, t2, t3, t4, t5, t6, rt)  \
980 rt GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6) \
981 { \
982     return m_private->name(a1, a2, a3, a4, a5, a6);       \
983 }
984
985 #define DELEGATE_TO_INTERNAL_7(name, t1, t2, t3, t4, t5, t6, t7) \
986 void GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7) \
987 { \
988     m_private->name(a1, a2, a3, a4, a5, a6, a7);   \
989 }
990
991 #define DELEGATE_TO_INTERNAL_7R(name, t1, t2, t3, t4, t5, t6, t7, rt) \
992 rt GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7) \
993 { \
994     return m_private->name(a1, a2, a3, a4, a5, a6, a7);   \
995 }
996
997 #define DELEGATE_TO_INTERNAL_8(name, t1, t2, t3, t4, t5, t6, t7, t8)       \
998 void GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8) \
999 { \
1000     m_private->name(a1, a2, a3, a4, a5, a6, a7, a8);      \
1001 }
1002
1003 #define DELEGATE_TO_INTERNAL_9(name, t1, t2, t3, t4, t5, t6, t7, t8, t9) \
1004 void GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8, t9 a9) \
1005 { \
1006     m_private->name(a1, a2, a3, a4, a5, a6, a7, a8, a9);   \
1007 }
1008
1009 #define DELEGATE_TO_INTERNAL_9R(name, t1, t2, t3, t4, t5, t6, t7, t8, t9, rt) \
1010 rt GraphicsContext3D::name(t1 a1, t2 a2, t3 a3, t4 a4, t5 a5, t6 a6, t7 a7, t8 a8, t9 a9) \
1011 { \
1012     return m_private->name(a1, a2, a3, a4, a5, a6, a7, a8, a9);   \
1013 }
1014
1015 GraphicsContext3D::GraphicsContext3D(GraphicsContext3D::Attributes, HostWindow*, bool)
1016 {
1017 }
1018
1019 GraphicsContext3D::~GraphicsContext3D()
1020 {
1021     m_private->setContextLostCallback(nullptr);
1022     m_private->setErrorMessageCallback(nullptr);
1023     m_private->setSwapBuffersCompleteCallbackCHROMIUM(nullptr);
1024     m_private->setGpuMemoryAllocationChangedCallbackCHROMIUM(nullptr);
1025 }
1026
1027 PassRefPtr<GraphicsContext3D> GraphicsContext3D::create(GraphicsContext3D::Attributes attrs, HostWindow*, GraphicsContext3D::RenderStyle renderStyle)
1028 {
1029     ASSERT(renderStyle != GraphicsContext3D::RenderDirectlyToHostWindow);
1030
1031     WebKit::WebGraphicsContext3D::Attributes webAttributes;
1032     webAttributes.alpha = attrs.alpha;
1033     webAttributes.depth = attrs.depth;
1034     webAttributes.stencil = attrs.stencil;
1035     webAttributes.antialias = attrs.antialias;
1036     webAttributes.premultipliedAlpha = attrs.premultipliedAlpha;
1037     webAttributes.noExtensions = attrs.noExtensions;
1038     webAttributes.shareResources = attrs.shareResources;
1039     webAttributes.preferDiscreteGPU = attrs.preferDiscreteGPU;
1040
1041     OwnPtr<WebKit::WebGraphicsContext3D> webContext = adoptPtr(WebKit::webKitPlatformSupport()->createOffscreenGraphicsContext3D(webAttributes));
1042     if (!webContext)
1043         return 0;
1044
1045     return GraphicsContext3DPrivate::createGraphicsContextFromWebContext(webContext.release(), renderStyle, attrs.preserveDrawingBuffer);
1046 }
1047
1048 PlatformGraphicsContext3D GraphicsContext3D::platformGraphicsContext3D() const
1049 {
1050     return m_private->platformGraphicsContext3D();
1051 }
1052
1053 Platform3DObject GraphicsContext3D::platformTexture() const
1054 {
1055     return m_private->platformTexture();
1056 }
1057
1058 #if USE(SKIA)
1059 GrContext* GraphicsContext3D::grContext()
1060 {
1061     return m_private->grContext();
1062 }
1063 #endif
1064
1065 void GraphicsContext3D::prepareTexture()
1066 {
1067     return m_private->prepareTexture();
1068 }
1069
1070 IntSize GraphicsContext3D::getInternalFramebufferSize() const
1071 {
1072     return m_private->getInternalFramebufferSize();
1073 }
1074
1075 bool GraphicsContext3D::isResourceSafe()
1076 {
1077     return m_private->isResourceSafe();
1078 }
1079
1080 #if USE(ACCELERATED_COMPOSITING)
1081 PlatformLayer* GraphicsContext3D::platformLayer() const
1082 {
1083     return 0;
1084 }
1085 #endif
1086
1087 DELEGATE_TO_INTERNAL_R(makeContextCurrent, bool)
1088 DELEGATE_TO_INTERNAL_2(reshape, int, int)
1089
1090 DELEGATE_TO_INTERNAL_1(activeTexture, GC3Denum)
1091 DELEGATE_TO_INTERNAL_2(attachShader, Platform3DObject, Platform3DObject)
1092 DELEGATE_TO_INTERNAL_3(bindAttribLocation, Platform3DObject, GC3Duint, const String&)
1093
1094 DELEGATE_TO_INTERNAL_2(bindBuffer, GC3Denum, Platform3DObject)
1095 DELEGATE_TO_INTERNAL_2(bindFramebuffer, GC3Denum, Platform3DObject)
1096 DELEGATE_TO_INTERNAL_2(bindRenderbuffer, GC3Denum, Platform3DObject)
1097 DELEGATE_TO_INTERNAL_2(bindTexture, GC3Denum, Platform3DObject)
1098 DELEGATE_TO_INTERNAL_4(blendColor, GC3Dclampf, GC3Dclampf, GC3Dclampf, GC3Dclampf)
1099 DELEGATE_TO_INTERNAL_1(blendEquation, GC3Denum)
1100 DELEGATE_TO_INTERNAL_2(blendEquationSeparate, GC3Denum, GC3Denum)
1101 DELEGATE_TO_INTERNAL_2(blendFunc, GC3Denum, GC3Denum)
1102 DELEGATE_TO_INTERNAL_4(blendFuncSeparate, GC3Denum, GC3Denum, GC3Denum, GC3Denum)
1103
1104 DELEGATE_TO_INTERNAL_3(bufferData, GC3Denum, GC3Dsizeiptr, GC3Denum)
1105 DELEGATE_TO_INTERNAL_4(bufferData, GC3Denum, GC3Dsizeiptr, const void*, GC3Denum)
1106 DELEGATE_TO_INTERNAL_4(bufferSubData, GC3Denum, GC3Dintptr, GC3Dsizeiptr, const void*)
1107
1108 DELEGATE_TO_INTERNAL_1R(checkFramebufferStatus, GC3Denum, GC3Denum)
1109 DELEGATE_TO_INTERNAL_1(clear, GC3Dbitfield)
1110 DELEGATE_TO_INTERNAL_4(clearColor, GC3Dclampf, GC3Dclampf, GC3Dclampf, GC3Dclampf)
1111 DELEGATE_TO_INTERNAL_1(clearDepth, GC3Dclampf)
1112 DELEGATE_TO_INTERNAL_1(clearStencil, GC3Dint)
1113 DELEGATE_TO_INTERNAL_4(colorMask, GC3Dboolean, GC3Dboolean, GC3Dboolean, GC3Dboolean)
1114 DELEGATE_TO_INTERNAL_1(compileShader, Platform3DObject)
1115
1116 DELEGATE_TO_INTERNAL_8(compressedTexImage2D, GC3Denum, GC3Dint, GC3Denum, GC3Dint, GC3Dint, GC3Dsizei, GC3Dsizei, const void*)
1117 DELEGATE_TO_INTERNAL_9(compressedTexSubImage2D, GC3Denum, GC3Dint, GC3Dint, GC3Dint, GC3Dint, GC3Dint, GC3Denum, GC3Dsizei, const void*)
1118 DELEGATE_TO_INTERNAL_8(copyTexImage2D, GC3Denum, GC3Dint, GC3Denum, GC3Dint, GC3Dint, GC3Dsizei, GC3Dsizei, GC3Dint)
1119 DELEGATE_TO_INTERNAL_8(copyTexSubImage2D, GC3Denum, GC3Dint, GC3Dint, GC3Dint, GC3Dint, GC3Dint, GC3Dsizei, GC3Dsizei)
1120 DELEGATE_TO_INTERNAL_1(cullFace, GC3Denum)
1121 DELEGATE_TO_INTERNAL_1(depthFunc, GC3Denum)
1122 DELEGATE_TO_INTERNAL_1(depthMask, GC3Dboolean)
1123 DELEGATE_TO_INTERNAL_2(depthRange, GC3Dclampf, GC3Dclampf)
1124 DELEGATE_TO_INTERNAL_2(detachShader, Platform3DObject, Platform3DObject)
1125 DELEGATE_TO_INTERNAL_1(disable, GC3Denum)
1126 DELEGATE_TO_INTERNAL_1(disableVertexAttribArray, GC3Duint)
1127 DELEGATE_TO_INTERNAL_3(drawArrays, GC3Denum, GC3Dint, GC3Dsizei)
1128 DELEGATE_TO_INTERNAL_4(drawElements, GC3Denum, GC3Dsizei, GC3Denum, GC3Dintptr)
1129
1130 DELEGATE_TO_INTERNAL_1(enable, GC3Denum)
1131 DELEGATE_TO_INTERNAL_1(enableVertexAttribArray, GC3Duint)
1132 DELEGATE_TO_INTERNAL(finish)
1133 DELEGATE_TO_INTERNAL(flush)
1134 DELEGATE_TO_INTERNAL_4(framebufferRenderbuffer, GC3Denum, GC3Denum, GC3Denum, Platform3DObject)
1135 DELEGATE_TO_INTERNAL_5(framebufferTexture2D, GC3Denum, GC3Denum, GC3Denum, Platform3DObject, GC3Dint)
1136 DELEGATE_TO_INTERNAL_1(frontFace, GC3Denum)
1137 DELEGATE_TO_INTERNAL_1(generateMipmap, GC3Denum)
1138
1139 DELEGATE_TO_INTERNAL_3R(getActiveAttrib, Platform3DObject, GC3Duint, ActiveInfo&, bool)
1140 DELEGATE_TO_INTERNAL_3R(getActiveUniform, Platform3DObject, GC3Duint, ActiveInfo&, bool)
1141 DELEGATE_TO_INTERNAL_4(getAttachedShaders, Platform3DObject, GC3Dsizei, GC3Dsizei*, Platform3DObject*)
1142 DELEGATE_TO_INTERNAL_2R(getAttribLocation, Platform3DObject, const String&, GC3Dint)
1143 DELEGATE_TO_INTERNAL_2(getBooleanv, GC3Denum, GC3Dboolean*)
1144 DELEGATE_TO_INTERNAL_3(getBufferParameteriv, GC3Denum, GC3Denum, GC3Dint*)
1145 DELEGATE_TO_INTERNAL_R(getContextAttributes, GraphicsContext3D::Attributes)
1146 DELEGATE_TO_INTERNAL_R(getError, GC3Denum)
1147 DELEGATE_TO_INTERNAL_2(getFloatv, GC3Denum, GC3Dfloat*)
1148 DELEGATE_TO_INTERNAL_4(getFramebufferAttachmentParameteriv, GC3Denum, GC3Denum, GC3Denum, GC3Dint*)
1149 DELEGATE_TO_INTERNAL_2(getIntegerv, GC3Denum, GC3Dint*)
1150 DELEGATE_TO_INTERNAL_3(getProgramiv, Platform3DObject, GC3Denum, GC3Dint*)
1151 DELEGATE_TO_INTERNAL_1R(getProgramInfoLog, Platform3DObject, String)
1152 DELEGATE_TO_INTERNAL_3(getRenderbufferParameteriv, GC3Denum, GC3Denum, GC3Dint*)
1153 DELEGATE_TO_INTERNAL_3(getShaderiv, Platform3DObject, GC3Denum, GC3Dint*)
1154 DELEGATE_TO_INTERNAL_1R(getShaderInfoLog, Platform3DObject, String)
1155 DELEGATE_TO_INTERNAL_1R(getShaderSource, Platform3DObject, String)
1156 DELEGATE_TO_INTERNAL_1R(getString, GC3Denum, String)
1157 DELEGATE_TO_INTERNAL_3(getTexParameterfv, GC3Denum, GC3Denum, GC3Dfloat*)
1158 DELEGATE_TO_INTERNAL_3(getTexParameteriv, GC3Denum, GC3Denum, GC3Dint*)
1159 DELEGATE_TO_INTERNAL_3(getUniformfv, Platform3DObject, GC3Dint, GC3Dfloat*)
1160 DELEGATE_TO_INTERNAL_3(getUniformiv, Platform3DObject, GC3Dint, GC3Dint*)
1161 DELEGATE_TO_INTERNAL_2R(getUniformLocation, Platform3DObject, const String&, GC3Dint)
1162 DELEGATE_TO_INTERNAL_3(getVertexAttribfv, GC3Duint, GC3Denum, GC3Dfloat*)
1163 DELEGATE_TO_INTERNAL_3(getVertexAttribiv, GC3Duint, GC3Denum, GC3Dint*)
1164 DELEGATE_TO_INTERNAL_2R(getVertexAttribOffset, GC3Duint, GC3Denum, GC3Dsizeiptr)
1165
1166 DELEGATE_TO_INTERNAL_2(hint, GC3Denum, GC3Denum)
1167 DELEGATE_TO_INTERNAL_1R(isBuffer, Platform3DObject, GC3Dboolean)
1168 DELEGATE_TO_INTERNAL_1R(isEnabled, GC3Denum, GC3Dboolean)
1169 DELEGATE_TO_INTERNAL_1R(isFramebuffer, Platform3DObject, GC3Dboolean)
1170 DELEGATE_TO_INTERNAL_1R(isProgram, Platform3DObject, GC3Dboolean)
1171 DELEGATE_TO_INTERNAL_1R(isRenderbuffer, Platform3DObject, GC3Dboolean)
1172 DELEGATE_TO_INTERNAL_1R(isShader, Platform3DObject, GC3Dboolean)
1173 DELEGATE_TO_INTERNAL_1R(isTexture, Platform3DObject, GC3Dboolean)
1174 DELEGATE_TO_INTERNAL_1(lineWidth, GC3Dfloat)
1175 DELEGATE_TO_INTERNAL_1(linkProgram, Platform3DObject)
1176 DELEGATE_TO_INTERNAL_2(pixelStorei, GC3Denum, GC3Dint)
1177 DELEGATE_TO_INTERNAL_2(polygonOffset, GC3Dfloat, GC3Dfloat)
1178
1179 DELEGATE_TO_INTERNAL_7(readPixels, GC3Dint, GC3Dint, GC3Dsizei, GC3Dsizei, GC3Denum, GC3Denum, void*)
1180
1181 DELEGATE_TO_INTERNAL(releaseShaderCompiler)
1182 DELEGATE_TO_INTERNAL_4(renderbufferStorage, GC3Denum, GC3Denum, GC3Dsizei, GC3Dsizei)
1183 DELEGATE_TO_INTERNAL_2(sampleCoverage, GC3Dclampf, GC3Dboolean)
1184 DELEGATE_TO_INTERNAL_4(scissor, GC3Dint, GC3Dint, GC3Dsizei, GC3Dsizei)
1185 DELEGATE_TO_INTERNAL_2(shaderSource, Platform3DObject, const String&)
1186 DELEGATE_TO_INTERNAL_3(stencilFunc, GC3Denum, GC3Dint, GC3Duint)
1187 DELEGATE_TO_INTERNAL_4(stencilFuncSeparate, GC3Denum, GC3Denum, GC3Dint, GC3Duint)
1188 DELEGATE_TO_INTERNAL_1(stencilMask, GC3Duint)
1189 DELEGATE_TO_INTERNAL_2(stencilMaskSeparate, GC3Denum, GC3Duint)
1190 DELEGATE_TO_INTERNAL_3(stencilOp, GC3Denum, GC3Denum, GC3Denum)
1191 DELEGATE_TO_INTERNAL_4(stencilOpSeparate, GC3Denum, GC3Denum, GC3Denum, GC3Denum)
1192
1193 DELEGATE_TO_INTERNAL_9R(texImage2D, GC3Denum, GC3Dint, GC3Denum, GC3Dsizei, GC3Dsizei, GC3Dint, GC3Denum, GC3Denum, const void*, bool)
1194 DELEGATE_TO_INTERNAL_3(texParameterf, GC3Denum, GC3Denum, GC3Dfloat)
1195 DELEGATE_TO_INTERNAL_3(texParameteri, GC3Denum, GC3Denum, GC3Dint)
1196 DELEGATE_TO_INTERNAL_9(texSubImage2D, GC3Denum, GC3Dint, GC3Dint, GC3Dint, GC3Dsizei, GC3Dsizei, GC3Denum, GC3Denum, const void*)
1197
1198 DELEGATE_TO_INTERNAL_2(uniform1f, GC3Dint, GC3Dfloat)
1199 DELEGATE_TO_INTERNAL_3(uniform1fv, GC3Dint, GC3Dsizei, GC3Dfloat*)
1200 DELEGATE_TO_INTERNAL_2(uniform1i, GC3Dint, GC3Dint)
1201 DELEGATE_TO_INTERNAL_3(uniform1iv, GC3Dint, GC3Dsizei, GC3Dint*)
1202 DELEGATE_TO_INTERNAL_3(uniform2f, GC3Dint, GC3Dfloat, GC3Dfloat)
1203 DELEGATE_TO_INTERNAL_3(uniform2fv, GC3Dint, GC3Dsizei, GC3Dfloat*)
1204 DELEGATE_TO_INTERNAL_3(uniform2i, GC3Dint, GC3Dint, GC3Dint)
1205 DELEGATE_TO_INTERNAL_3(uniform2iv, GC3Dint, GC3Dsizei, GC3Dint*)
1206 DELEGATE_TO_INTERNAL_4(uniform3f, GC3Dint, GC3Dfloat, GC3Dfloat, GC3Dfloat)
1207 DELEGATE_TO_INTERNAL_3(uniform3fv, GC3Dint, GC3Dsizei, GC3Dfloat*)
1208 DELEGATE_TO_INTERNAL_4(uniform3i, GC3Dint, GC3Dint, GC3Dint, GC3Dint)
1209 DELEGATE_TO_INTERNAL_3(uniform3iv, GC3Dint, GC3Dsizei, GC3Dint*)
1210 DELEGATE_TO_INTERNAL_5(uniform4f, GC3Dint, GC3Dfloat, GC3Dfloat, GC3Dfloat, GC3Dfloat)
1211 DELEGATE_TO_INTERNAL_3(uniform4fv, GC3Dint, GC3Dsizei, GC3Dfloat*)
1212 DELEGATE_TO_INTERNAL_5(uniform4i, GC3Dint, GC3Dint, GC3Dint, GC3Dint, GC3Dint)
1213 DELEGATE_TO_INTERNAL_3(uniform4iv, GC3Dint, GC3Dsizei, GC3Dint*)
1214 DELEGATE_TO_INTERNAL_4(uniformMatrix2fv, GC3Dint, GC3Dsizei, GC3Dboolean, GC3Dfloat*)
1215 DELEGATE_TO_INTERNAL_4(uniformMatrix3fv, GC3Dint, GC3Dsizei, GC3Dboolean, GC3Dfloat*)
1216 DELEGATE_TO_INTERNAL_4(uniformMatrix4fv, GC3Dint, GC3Dsizei, GC3Dboolean, GC3Dfloat*)
1217
1218 DELEGATE_TO_INTERNAL_1(useProgram, Platform3DObject)
1219 DELEGATE_TO_INTERNAL_1(validateProgram, Platform3DObject)
1220
1221 DELEGATE_TO_INTERNAL_2(vertexAttrib1f, GC3Duint, GC3Dfloat)
1222 DELEGATE_TO_INTERNAL_2(vertexAttrib1fv, GC3Duint, GC3Dfloat*)
1223 DELEGATE_TO_INTERNAL_3(vertexAttrib2f, GC3Duint, GC3Dfloat, GC3Dfloat)
1224 DELEGATE_TO_INTERNAL_2(vertexAttrib2fv, GC3Duint, GC3Dfloat*)
1225 DELEGATE_TO_INTERNAL_4(vertexAttrib3f, GC3Duint, GC3Dfloat, GC3Dfloat, GC3Dfloat)
1226 DELEGATE_TO_INTERNAL_2(vertexAttrib3fv, GC3Duint, GC3Dfloat*)
1227 DELEGATE_TO_INTERNAL_5(vertexAttrib4f, GC3Duint, GC3Dfloat, GC3Dfloat, GC3Dfloat, GC3Dfloat)
1228 DELEGATE_TO_INTERNAL_2(vertexAttrib4fv, GC3Duint, GC3Dfloat*)
1229 DELEGATE_TO_INTERNAL_6(vertexAttribPointer, GC3Duint, GC3Dint, GC3Denum, GC3Dboolean, GC3Dsizei, GC3Dintptr)
1230
1231 DELEGATE_TO_INTERNAL_4(viewport, GC3Dint, GC3Dint, GC3Dsizei, GC3Dsizei)
1232
1233 DELEGATE_TO_INTERNAL(markLayerComposited)
1234 DELEGATE_TO_INTERNAL(markContextChanged)
1235
1236 bool GraphicsContext3D::layerComposited() const
1237 {
1238     return m_private->layerComposited();
1239 }
1240
1241 void GraphicsContext3D::paintRenderingResultsToCanvas(CanvasRenderingContext* context, DrawingBuffer* drawingBuffer)
1242 {
1243     return m_private->paintRenderingResultsToCanvas(context, drawingBuffer);
1244 }
1245
1246 PassRefPtr<ImageData> GraphicsContext3D::paintRenderingResultsToImageData(DrawingBuffer* drawingBuffer)
1247 {
1248     return m_private->paintRenderingResultsToImageData(drawingBuffer);
1249 }
1250
1251 DELEGATE_TO_INTERNAL_1R(paintCompositedResultsToCanvas, CanvasRenderingContext*, bool)
1252
1253 DELEGATE_TO_INTERNAL_R(createBuffer, Platform3DObject)
1254 DELEGATE_TO_INTERNAL_R(createFramebuffer, Platform3DObject)
1255 DELEGATE_TO_INTERNAL_R(createProgram, Platform3DObject)
1256 DELEGATE_TO_INTERNAL_R(createRenderbuffer, Platform3DObject)
1257 DELEGATE_TO_INTERNAL_1R(createShader, GC3Denum, Platform3DObject)
1258 DELEGATE_TO_INTERNAL_R(createTexture, Platform3DObject)
1259
1260 DELEGATE_TO_INTERNAL_1(deleteBuffer, Platform3DObject)
1261 DELEGATE_TO_INTERNAL_1(deleteFramebuffer, Platform3DObject)
1262 DELEGATE_TO_INTERNAL_1(deleteProgram, Platform3DObject)
1263 DELEGATE_TO_INTERNAL_1(deleteRenderbuffer, Platform3DObject)
1264 DELEGATE_TO_INTERNAL_1(deleteShader, Platform3DObject)
1265 DELEGATE_TO_INTERNAL_1(deleteTexture, Platform3DObject)
1266
1267 DELEGATE_TO_INTERNAL_1(synthesizeGLError, GC3Denum)
1268 DELEGATE_TO_INTERNAL_R(getExtensions, Extensions3D*)
1269
1270 DELEGATE_TO_INTERNAL_1(setContextLostCallback, PassOwnPtr<GraphicsContext3D::ContextLostCallback>)
1271 DELEGATE_TO_INTERNAL_1(setErrorMessageCallback, PassOwnPtr<GraphicsContext3D::ErrorMessageCallback>)
1272
1273 class GraphicsContextLostCallbackAdapter : public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback {
1274 public:
1275     virtual void onContextLost();
1276     static PassOwnPtr<GraphicsContextLostCallbackAdapter> create(PassOwnPtr<GraphicsContext3D::ContextLostCallback>);
1277     virtual ~GraphicsContextLostCallbackAdapter() { }
1278 private:
1279     GraphicsContextLostCallbackAdapter(PassOwnPtr<GraphicsContext3D::ContextLostCallback> cb) : m_contextLostCallback(cb) { }
1280     OwnPtr<GraphicsContext3D::ContextLostCallback> m_contextLostCallback;
1281 };
1282
1283 void GraphicsContextLostCallbackAdapter::onContextLost()
1284 {
1285     if (m_contextLostCallback)
1286         m_contextLostCallback->onContextLost();
1287 }
1288
1289 PassOwnPtr<GraphicsContextLostCallbackAdapter> GraphicsContextLostCallbackAdapter::create(PassOwnPtr<GraphicsContext3D::ContextLostCallback> cb)
1290 {
1291     return adoptPtr(cb.get() ? new GraphicsContextLostCallbackAdapter(cb) : 0);
1292 }
1293
1294 void GraphicsContext3DPrivate::setContextLostCallback(PassOwnPtr<GraphicsContext3D::ContextLostCallback> cb)
1295 {
1296     m_contextLostCallbackAdapter = GraphicsContextLostCallbackAdapter::create(cb);
1297     m_impl->setContextLostCallback(m_contextLostCallbackAdapter.get());
1298 }
1299
1300 class GraphicsErrorMessageCallbackAdapter : public WebKit::WebGraphicsContext3D::WebGraphicsErrorMessageCallback {
1301 public:
1302     virtual void onErrorMessage(const WebKit::WebString&, WebKit::WGC3Dint);
1303     static PassOwnPtr<GraphicsErrorMessageCallbackAdapter> create(PassOwnPtr<GraphicsContext3D::ErrorMessageCallback>);
1304     virtual ~GraphicsErrorMessageCallbackAdapter() { }
1305 private:
1306     GraphicsErrorMessageCallbackAdapter(PassOwnPtr<GraphicsContext3D::ErrorMessageCallback> cb) : m_errorMessageCallback(cb) { }
1307     OwnPtr<GraphicsContext3D::ErrorMessageCallback> m_errorMessageCallback;
1308 };
1309
1310 void GraphicsErrorMessageCallbackAdapter::onErrorMessage(const WebKit::WebString& message, WebKit::WGC3Dint id)
1311 {
1312     if (m_errorMessageCallback)
1313         m_errorMessageCallback->onErrorMessage(message, id);
1314 }
1315
1316 PassOwnPtr<GraphicsErrorMessageCallbackAdapter> GraphicsErrorMessageCallbackAdapter::create(PassOwnPtr<GraphicsContext3D::ErrorMessageCallback> cb)
1317 {
1318     return adoptPtr(cb.get() ? new GraphicsErrorMessageCallbackAdapter(cb) : 0);
1319 }
1320
1321 void GraphicsContext3DPrivate::setErrorMessageCallback(PassOwnPtr<GraphicsContext3D::ErrorMessageCallback> cb)
1322 {
1323     m_errorMessageCallbackAdapter = GraphicsErrorMessageCallbackAdapter::create(cb);
1324     m_impl->setErrorMessageCallback(m_errorMessageCallbackAdapter.get());
1325 }
1326
1327 bool GraphicsContext3D::isGLES2Compliant() const
1328 {
1329     return m_private->isGLES2Compliant();
1330 }
1331
1332 class GraphicsContext3DSwapBuffersCompleteCallbackAdapter : public WebKit::WebGraphicsContext3D::WebGraphicsSwapBuffersCompleteCallbackCHROMIUM {
1333 public:
1334     virtual void onSwapBuffersComplete();
1335     static PassOwnPtr<GraphicsContext3DSwapBuffersCompleteCallbackAdapter> create(PassOwnPtr<Extensions3DChromium::SwapBuffersCompleteCallbackCHROMIUM>);
1336     virtual ~GraphicsContext3DSwapBuffersCompleteCallbackAdapter() { }
1337
1338 private:
1339     GraphicsContext3DSwapBuffersCompleteCallbackAdapter(PassOwnPtr<Extensions3DChromium::SwapBuffersCompleteCallbackCHROMIUM> cb) : m_swapBuffersCompleteCallback(cb) { }
1340     OwnPtr<Extensions3DChromium::SwapBuffersCompleteCallbackCHROMIUM> m_swapBuffersCompleteCallback;
1341 };
1342
1343 void GraphicsContext3DSwapBuffersCompleteCallbackAdapter::onSwapBuffersComplete()
1344 {
1345     if (m_swapBuffersCompleteCallback)
1346         m_swapBuffersCompleteCallback->onSwapBuffersComplete();
1347 }
1348
1349 PassOwnPtr<GraphicsContext3DSwapBuffersCompleteCallbackAdapter> GraphicsContext3DSwapBuffersCompleteCallbackAdapter::create(PassOwnPtr<Extensions3DChromium::SwapBuffersCompleteCallbackCHROMIUM> cb)
1350 {
1351     return adoptPtr(cb.get() ? new GraphicsContext3DSwapBuffersCompleteCallbackAdapter(cb) : 0);
1352 }
1353
1354 void GraphicsContext3DPrivate::setSwapBuffersCompleteCallbackCHROMIUM(PassOwnPtr<Extensions3DChromium::SwapBuffersCompleteCallbackCHROMIUM> cb)
1355 {
1356     m_swapBuffersCompleteCallbackAdapter = GraphicsContext3DSwapBuffersCompleteCallbackAdapter::create(cb);
1357     m_impl->setSwapBuffersCompleteCallbackCHROMIUM(m_swapBuffersCompleteCallbackAdapter.get());
1358 }
1359
1360 class GraphicsContext3DMemoryAllocationChangedCallbackAdapter : public WebKit::WebGraphicsContext3D::WebGraphicsMemoryAllocationChangedCallbackCHROMIUM {
1361 public:
1362     GraphicsContext3DMemoryAllocationChangedCallbackAdapter(PassOwnPtr<Extensions3DChromium::GpuMemoryAllocationChangedCallbackCHROMIUM> cb)
1363         : m_memoryAllocationChangedCallback(cb)
1364     {
1365     }
1366
1367     virtual ~GraphicsContext3DMemoryAllocationChangedCallbackAdapter() { }
1368
1369     virtual void onMemoryAllocationChanged(size_t gpuResourceSizeInBytes)
1370     {
1371         // FIXME: Remove this once clients start using WebGraphicsMemoryAllocation exclusively.
1372         onMemoryAllocationChanged(WebKit::WebGraphicsMemoryAllocation(gpuResourceSizeInBytes, true));
1373     }
1374
1375     virtual void onMemoryAllocationChanged(WebKit::WebGraphicsMemoryAllocation allocation)
1376     {
1377         if (m_memoryAllocationChangedCallback)
1378             m_memoryAllocationChangedCallback->onGpuMemoryAllocationChanged(Extensions3DChromium::GpuMemoryAllocationCHROMIUM(allocation.gpuResourceSizeInBytes, allocation.suggestHaveBackbuffer));
1379     }
1380
1381 private:
1382     OwnPtr<Extensions3DChromium::GpuMemoryAllocationChangedCallbackCHROMIUM> m_memoryAllocationChangedCallback;
1383 };
1384
1385 void GraphicsContext3DPrivate::setGpuMemoryAllocationChangedCallbackCHROMIUM(PassOwnPtr<Extensions3DChromium::GpuMemoryAllocationChangedCallbackCHROMIUM> cb)
1386 {
1387     m_memoryAllocationChangedCallbackAdapter = adoptPtr(new GraphicsContext3DMemoryAllocationChangedCallbackAdapter(cb));
1388     m_impl->setMemoryAllocationChangedCallbackCHROMIUM(m_memoryAllocationChangedCallbackAdapter.get());
1389 }
1390
1391 } // namespace WebCore
1392
1393 #endif // ENABLE(WEBGL)