2ff411b0c44831afbd0342218985f4a34c26ab8a
[platform/framework/web/crosswalk.git] / src / content / common / gpu / client / webgraphicscontext3d_command_buffer_impl.h
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CONTENT_COMMON_GPU_CLIENT_WEBGRAPHICSCONTEXT3D_COMMAND_BUFFER_IMPL_H_
6 #define CONTENT_COMMON_GPU_CLIENT_WEBGRAPHICSCONTEXT3D_COMMAND_BUFFER_IMPL_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/synchronization/lock.h"
15 #include "content/common/content_export.h"
16 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
17 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
18 #include "third_party/WebKit/public/platform/WebString.h"
19 #include "ui/gfx/native_widget_types.h"
20 #include "ui/gl/gpu_preference.h"
21 #include "url/gurl.h"
22
23 namespace gpu {
24
25 class ContextSupport;
26 class TransferBuffer;
27
28 namespace gles2 {
29 class GLES2CmdHelper;
30 class GLES2Implementation;
31 class GLES2Interface;
32 }
33 }
34
35 using blink::WebGLId;
36
37 using blink::WGC3Dbyte;
38 using blink::WGC3Dchar;
39 using blink::WGC3Denum;
40 using blink::WGC3Dboolean;
41 using blink::WGC3Dbitfield;
42 using blink::WGC3Dint;
43 using blink::WGC3Dsizei;
44 using blink::WGC3Duint;
45 using blink::WGC3Dfloat;
46 using blink::WGC3Dclampf;
47 using blink::WGC3Dintptr;
48 using blink::WGC3Dsizeiptr;
49
50 namespace content {
51 class GpuChannelHost;
52
53 const size_t kDefaultCommandBufferSize = 1024 * 1024;
54 const size_t kDefaultStartTransferBufferSize = 1 * 1024 * 1024;
55 const size_t kDefaultMinTransferBufferSize = 1 * 256 * 1024;
56 const size_t kDefaultMaxTransferBufferSize = 16 * 1024 * 1024;
57
58 class WebGraphicsContext3DErrorMessageCallback;
59
60 class WebGraphicsContext3DCommandBufferImpl
61     : public blink::WebGraphicsContext3D {
62  public:
63   enum MappedMemoryReclaimLimit {
64     kNoLimit = 0,
65   };
66
67   struct CONTENT_EXPORT SharedMemoryLimits {
68     SharedMemoryLimits();
69
70     size_t command_buffer_size;
71     size_t start_transfer_buffer_size;
72     size_t min_transfer_buffer_size;
73     size_t max_transfer_buffer_size;
74     size_t mapped_memory_reclaim_limit;
75   };
76
77   class ShareGroup : public base::RefCountedThreadSafe<ShareGroup> {
78    public:
79     ShareGroup();
80
81     WebGraphicsContext3DCommandBufferImpl* GetAnyContextLocked() {
82       // In order to ensure that the context returned is not removed while
83       // in use, the share group's lock should be aquired before calling this
84       // function.
85       lock_.AssertAcquired();
86       if (contexts_.empty())
87         return NULL;
88       return contexts_.front();
89     }
90
91     void AddContextLocked(WebGraphicsContext3DCommandBufferImpl* context) {
92       lock_.AssertAcquired();
93       contexts_.push_back(context);
94     }
95
96     void RemoveContext(WebGraphicsContext3DCommandBufferImpl* context) {
97       base::AutoLock auto_lock(lock_);
98       contexts_.erase(std::remove(contexts_.begin(), contexts_.end(), context),
99           contexts_.end());
100     }
101
102     void RemoveAllContexts() {
103       base::AutoLock auto_lock(lock_);
104       contexts_.clear();
105     }
106
107     base::Lock& lock() {
108       return lock_;
109     }
110
111    private:
112     friend class base::RefCountedThreadSafe<ShareGroup>;
113     virtual ~ShareGroup();
114
115     std::vector<WebGraphicsContext3DCommandBufferImpl*> contexts_;
116     base::Lock lock_;
117
118     DISALLOW_COPY_AND_ASSIGN(ShareGroup);
119   };
120
121   WebGraphicsContext3DCommandBufferImpl(
122       int surface_id,
123       const GURL& active_url,
124       GpuChannelHost* host,
125       const Attributes& attributes,
126       bool bind_generates_resources,
127       const SharedMemoryLimits& limits,
128       WebGraphicsContext3DCommandBufferImpl* share_context);
129
130   virtual ~WebGraphicsContext3DCommandBufferImpl();
131
132   CommandBufferProxyImpl* GetCommandBufferProxy() {
133     return command_buffer_.get();
134   }
135
136   CONTENT_EXPORT gpu::ContextSupport* GetContextSupport();
137
138   gpu::gles2::GLES2Implementation* GetImplementation() {
139     return real_gl_.get();
140   }
141
142   // Return true if GPU process reported context lost or there was a
143   // problem communicating with the GPU process.
144   bool IsCommandBufferContextLost();
145
146   // Create & initialize a WebGraphicsContext3DCommandBufferImpl.  Return NULL
147   // on any failure.
148   static CONTENT_EXPORT WebGraphicsContext3DCommandBufferImpl*
149       CreateOffscreenContext(
150           GpuChannelHost* host,
151           const WebGraphicsContext3D::Attributes& attributes,
152           const GURL& active_url,
153           const SharedMemoryLimits& limits,
154           WebGraphicsContext3DCommandBufferImpl* share_context);
155
156   size_t GetMappedMemoryLimit() {
157     return mem_limits_.mapped_memory_reclaim_limit;
158   }
159
160   //----------------------------------------------------------------------
161   // WebGraphicsContext3D methods
162
163   // Must be called after initialize() and before any of the following methods.
164   // Permanently binds to the first calling thread. Returns false if the
165   // graphics context fails to create. Do not call from more than one thread.
166   virtual bool makeContextCurrent();
167
168   virtual uint32_t lastFlushID();
169
170   virtual unsigned int insertSyncPoint();
171   virtual void waitSyncPoint(unsigned int sync_point);
172
173   virtual void loseContextCHROMIUM(WGC3Denum current, WGC3Denum other);
174
175   virtual void reshapeWithScaleFactor(
176       int width, int height, float scale_factor);
177
178   virtual void prepareTexture();
179   virtual void postSubBufferCHROMIUM(int x, int y, int width, int height);
180
181   virtual void activeTexture(WGC3Denum texture);
182   virtual void attachShader(WebGLId program, WebGLId shader);
183   virtual void bindAttribLocation(WebGLId program, WGC3Duint index,
184                                   const WGC3Dchar* name);
185   virtual void bindBuffer(WGC3Denum target, WebGLId buffer);
186   virtual void bindFramebuffer(WGC3Denum target, WebGLId framebuffer);
187   virtual void bindRenderbuffer(WGC3Denum target, WebGLId renderbuffer);
188   virtual void bindTexture(WGC3Denum target, WebGLId texture);
189   virtual void blendColor(WGC3Dclampf red, WGC3Dclampf green,
190                           WGC3Dclampf blue, WGC3Dclampf alpha);
191   virtual void blendEquation(WGC3Denum mode);
192   virtual void blendEquationSeparate(WGC3Denum modeRGB,
193                                      WGC3Denum modeAlpha);
194   virtual void blendFunc(WGC3Denum sfactor, WGC3Denum dfactor);
195   virtual void blendFuncSeparate(WGC3Denum srcRGB,
196                                  WGC3Denum dstRGB,
197                                  WGC3Denum srcAlpha,
198                                  WGC3Denum dstAlpha);
199
200   virtual void bufferData(WGC3Denum target, WGC3Dsizeiptr size,
201                           const void* data, WGC3Denum usage);
202   virtual void bufferSubData(WGC3Denum target, WGC3Dintptr offset,
203                              WGC3Dsizeiptr size, const void* data);
204
205   virtual WGC3Denum checkFramebufferStatus(WGC3Denum target);
206   virtual void clear(WGC3Dbitfield mask);
207   virtual void clearColor(WGC3Dclampf red, WGC3Dclampf green,
208                           WGC3Dclampf blue, WGC3Dclampf alpha);
209   virtual void clearDepth(WGC3Dclampf depth);
210   virtual void clearStencil(WGC3Dint s);
211   virtual void colorMask(WGC3Dboolean red, WGC3Dboolean green,
212                          WGC3Dboolean blue, WGC3Dboolean alpha);
213   virtual void compileShader(WebGLId shader);
214
215   virtual void compressedTexImage2D(WGC3Denum target,
216                                     WGC3Dint level,
217                                     WGC3Denum internalformat,
218                                     WGC3Dsizei width,
219                                     WGC3Dsizei height,
220                                     WGC3Dint border,
221                                     WGC3Dsizei imageSize,
222                                     const void* data);
223   virtual void compressedTexSubImage2D(WGC3Denum target,
224                                        WGC3Dint level,
225                                        WGC3Dint xoffset,
226                                        WGC3Dint yoffset,
227                                        WGC3Dsizei width,
228                                        WGC3Dsizei height,
229                                        WGC3Denum format,
230                                        WGC3Dsizei imageSize,
231                                        const void* data);
232   virtual void copyTexImage2D(WGC3Denum target,
233                               WGC3Dint level,
234                               WGC3Denum internalformat,
235                               WGC3Dint x,
236                               WGC3Dint y,
237                               WGC3Dsizei width,
238                               WGC3Dsizei height,
239                               WGC3Dint border);
240   virtual void copyTexSubImage2D(WGC3Denum target,
241                                  WGC3Dint level,
242                                  WGC3Dint xoffset,
243                                  WGC3Dint yoffset,
244                                  WGC3Dint x,
245                                  WGC3Dint y,
246                                  WGC3Dsizei width,
247                                  WGC3Dsizei height);
248   virtual void cullFace(WGC3Denum mode);
249   virtual void depthFunc(WGC3Denum func);
250   virtual void depthMask(WGC3Dboolean flag);
251   virtual void depthRange(WGC3Dclampf zNear, WGC3Dclampf zFar);
252   virtual void detachShader(WebGLId program, WebGLId shader);
253   virtual void disable(WGC3Denum cap);
254   virtual void disableVertexAttribArray(WGC3Duint index);
255   virtual void drawArrays(WGC3Denum mode, WGC3Dint first, WGC3Dsizei count);
256   virtual void drawElements(WGC3Denum mode,
257                             WGC3Dsizei count,
258                             WGC3Denum type,
259                             WGC3Dintptr offset);
260
261   virtual void enable(WGC3Denum cap);
262   virtual void enableVertexAttribArray(WGC3Duint index);
263   virtual void finish();
264   virtual void flush();
265   virtual void framebufferRenderbuffer(WGC3Denum target,
266                                        WGC3Denum attachment,
267                                        WGC3Denum renderbuffertarget,
268                                        WebGLId renderbuffer);
269   virtual void framebufferTexture2D(WGC3Denum target,
270                                     WGC3Denum attachment,
271                                     WGC3Denum textarget,
272                                     WebGLId texture,
273                                     WGC3Dint level);
274   virtual void frontFace(WGC3Denum mode);
275   virtual void generateMipmap(WGC3Denum target);
276
277   virtual bool getActiveAttrib(WebGLId program,
278                                WGC3Duint index,
279                                ActiveInfo&);
280   virtual bool getActiveUniform(WebGLId program,
281                                 WGC3Duint index,
282                                 ActiveInfo&);
283
284   virtual void getAttachedShaders(WebGLId program,
285                                   WGC3Dsizei maxCount,
286                                   WGC3Dsizei* count,
287                                   WebGLId* shaders);
288
289   virtual WGC3Dint  getAttribLocation(WebGLId program, const WGC3Dchar* name);
290
291   virtual void getBooleanv(WGC3Denum pname, WGC3Dboolean* value);
292
293   virtual void getBufferParameteriv(WGC3Denum target,
294                                     WGC3Denum pname,
295                                     WGC3Dint* value);
296
297   virtual Attributes getContextAttributes();
298
299   virtual WGC3Denum getError();
300
301   virtual bool isContextLost();
302
303   virtual void getFloatv(WGC3Denum pname, WGC3Dfloat* value);
304
305   virtual void getFramebufferAttachmentParameteriv(WGC3Denum target,
306                                                    WGC3Denum attachment,
307                                                    WGC3Denum pname,
308                                                    WGC3Dint* value);
309
310   virtual void getIntegerv(WGC3Denum pname, WGC3Dint* value);
311
312   virtual void getProgramiv(WebGLId program, WGC3Denum pname, WGC3Dint* value);
313
314   virtual blink::WebString getProgramInfoLog(WebGLId program);
315
316   virtual void getRenderbufferParameteriv(WGC3Denum target,
317                                           WGC3Denum pname,
318                                           WGC3Dint* value);
319
320   virtual void getShaderiv(WebGLId shader, WGC3Denum pname, WGC3Dint* value);
321
322   virtual blink::WebString getShaderInfoLog(WebGLId shader);
323
324   virtual void getShaderPrecisionFormat(WGC3Denum shadertype,
325                                         WGC3Denum precisiontype,
326                                         WGC3Dint* range,
327                                         WGC3Dint* precision);
328
329   virtual blink::WebString getShaderSource(WebGLId shader);
330   virtual blink::WebString getString(WGC3Denum name);
331
332   virtual void getTexParameterfv(WGC3Denum target,
333                                  WGC3Denum pname,
334                                  WGC3Dfloat* value);
335   virtual void getTexParameteriv(WGC3Denum target,
336                                  WGC3Denum pname,
337                                  WGC3Dint* value);
338
339   virtual void getUniformfv(WebGLId program,
340                             WGC3Dint location,
341                             WGC3Dfloat* value);
342   virtual void getUniformiv(WebGLId program,
343                             WGC3Dint location,
344                             WGC3Dint* value);
345
346   virtual WGC3Dint getUniformLocation(WebGLId program, const WGC3Dchar* name);
347
348   virtual void getVertexAttribfv(WGC3Duint index, WGC3Denum pname,
349                                  WGC3Dfloat* value);
350   virtual void getVertexAttribiv(WGC3Duint index, WGC3Denum pname,
351                                  WGC3Dint* value);
352
353   virtual WGC3Dsizeiptr getVertexAttribOffset(WGC3Duint index, WGC3Denum pname);
354
355   virtual void hint(WGC3Denum target, WGC3Denum mode);
356   virtual WGC3Dboolean isBuffer(WebGLId buffer);
357   virtual WGC3Dboolean isEnabled(WGC3Denum cap);
358   virtual WGC3Dboolean isFramebuffer(WebGLId framebuffer);
359   virtual WGC3Dboolean isProgram(WebGLId program);
360   virtual WGC3Dboolean isRenderbuffer(WebGLId renderbuffer);
361   virtual WGC3Dboolean isShader(WebGLId shader);
362   virtual WGC3Dboolean isTexture(WebGLId texture);
363   virtual void lineWidth(WGC3Dfloat);
364   virtual void linkProgram(WebGLId program);
365   virtual void pixelStorei(WGC3Denum pname, WGC3Dint param);
366   virtual void polygonOffset(WGC3Dfloat factor, WGC3Dfloat units);
367
368   virtual void readPixels(WGC3Dint x,
369                           WGC3Dint y,
370                           WGC3Dsizei width,
371                           WGC3Dsizei height,
372                           WGC3Denum format,
373                           WGC3Denum type,
374                           void* pixels);
375
376   virtual void releaseShaderCompiler();
377   virtual void renderbufferStorage(WGC3Denum target,
378                                    WGC3Denum internalformat,
379                                    WGC3Dsizei width,
380                                    WGC3Dsizei height);
381   virtual void sampleCoverage(WGC3Dfloat value, WGC3Dboolean invert);
382   virtual void scissor(WGC3Dint x, WGC3Dint y,
383                        WGC3Dsizei width, WGC3Dsizei height);
384   virtual void shaderSource(WebGLId shader, const WGC3Dchar* string);
385   virtual void stencilFunc(WGC3Denum func, WGC3Dint ref, WGC3Duint mask);
386   virtual void stencilFuncSeparate(WGC3Denum face,
387                                    WGC3Denum func,
388                                    WGC3Dint ref,
389                                    WGC3Duint mask);
390   virtual void stencilMask(WGC3Duint mask);
391   virtual void stencilMaskSeparate(WGC3Denum face, WGC3Duint mask);
392   virtual void stencilOp(WGC3Denum fail,
393                          WGC3Denum zfail,
394                          WGC3Denum zpass);
395   virtual void stencilOpSeparate(WGC3Denum face,
396                                  WGC3Denum fail,
397                                  WGC3Denum zfail,
398                                  WGC3Denum zpass);
399
400   virtual void texImage2D(WGC3Denum target,
401                           WGC3Dint level,
402                           WGC3Denum internalformat,
403                           WGC3Dsizei width,
404                           WGC3Dsizei height,
405                           WGC3Dint border,
406                           WGC3Denum format,
407                           WGC3Denum type,
408                           const void* pixels);
409
410   virtual void texParameterf(WGC3Denum target,
411                              WGC3Denum pname,
412                              WGC3Dfloat param);
413   virtual void texParameteri(WGC3Denum target,
414                              WGC3Denum pname,
415                              WGC3Dint param);
416
417   virtual void texSubImage2D(WGC3Denum target,
418                              WGC3Dint level,
419                              WGC3Dint xoffset,
420                              WGC3Dint yoffset,
421                              WGC3Dsizei width,
422                              WGC3Dsizei height,
423                              WGC3Denum format,
424                              WGC3Denum type,
425                              const void* pixels);
426
427   virtual void uniform1f(WGC3Dint location, WGC3Dfloat x);
428   virtual void uniform1fv(WGC3Dint location,
429                           WGC3Dsizei count, const WGC3Dfloat* v);
430   virtual void uniform1i(WGC3Dint location, WGC3Dint x);
431   virtual void uniform1iv(WGC3Dint location,
432                           WGC3Dsizei count, const WGC3Dint* v);
433   virtual void uniform2f(WGC3Dint location, WGC3Dfloat x, WGC3Dfloat y);
434   virtual void uniform2fv(WGC3Dint location,
435                           WGC3Dsizei count, const WGC3Dfloat* v);
436   virtual void uniform2i(WGC3Dint location, WGC3Dint x, WGC3Dint y);
437   virtual void uniform2iv(WGC3Dint location,
438                           WGC3Dsizei count, const WGC3Dint* v);
439   virtual void uniform3f(WGC3Dint location,
440                          WGC3Dfloat x, WGC3Dfloat y, WGC3Dfloat z);
441   virtual void uniform3fv(WGC3Dint location,
442                           WGC3Dsizei count, const WGC3Dfloat* v);
443   virtual void uniform3i(WGC3Dint location,
444                          WGC3Dint x, WGC3Dint y, WGC3Dint z);
445   virtual void uniform3iv(WGC3Dint location,
446                           WGC3Dsizei count, const WGC3Dint* v);
447   virtual void uniform4f(WGC3Dint location,
448                          WGC3Dfloat x, WGC3Dfloat y,
449                          WGC3Dfloat z, WGC3Dfloat w);
450   virtual void uniform4fv(WGC3Dint location,
451                           WGC3Dsizei count, const WGC3Dfloat* v);
452   virtual void uniform4i(WGC3Dint location,
453                          WGC3Dint x, WGC3Dint y, WGC3Dint z, WGC3Dint w);
454   virtual void uniform4iv(WGC3Dint location,
455                           WGC3Dsizei count, const WGC3Dint* v);
456   virtual void uniformMatrix2fv(WGC3Dint location,
457                                 WGC3Dsizei count,
458                                 WGC3Dboolean transpose,
459                                 const WGC3Dfloat* value);
460   virtual void uniformMatrix3fv(WGC3Dint location,
461                                 WGC3Dsizei count,
462                                 WGC3Dboolean transpose,
463                                 const WGC3Dfloat* value);
464   virtual void uniformMatrix4fv(WGC3Dint location,
465                                 WGC3Dsizei count,
466                                 WGC3Dboolean transpose,
467                                 const WGC3Dfloat* value);
468
469   virtual void useProgram(WebGLId program);
470   virtual void validateProgram(WebGLId program);
471
472   virtual void vertexAttrib1f(WGC3Duint index, WGC3Dfloat x);
473   virtual void vertexAttrib1fv(WGC3Duint index, const WGC3Dfloat* values);
474   virtual void vertexAttrib2f(WGC3Duint index, WGC3Dfloat x, WGC3Dfloat y);
475   virtual void vertexAttrib2fv(WGC3Duint index, const WGC3Dfloat* values);
476   virtual void vertexAttrib3f(WGC3Duint index,
477                               WGC3Dfloat x, WGC3Dfloat y, WGC3Dfloat z);
478   virtual void vertexAttrib3fv(WGC3Duint index, const WGC3Dfloat* values);
479   virtual void vertexAttrib4f(WGC3Duint index,
480                               WGC3Dfloat x, WGC3Dfloat y,
481                               WGC3Dfloat z, WGC3Dfloat w);
482   virtual void vertexAttrib4fv(WGC3Duint index, const WGC3Dfloat* values);
483   virtual void vertexAttribPointer(WGC3Duint index,
484                                    WGC3Dint size,
485                                    WGC3Denum type,
486                                    WGC3Dboolean normalized,
487                                    WGC3Dsizei stride,
488                                    WGC3Dintptr offset);
489
490   virtual void viewport(WGC3Dint x, WGC3Dint y,
491                         WGC3Dsizei width, WGC3Dsizei height);
492
493   // Support for buffer creation and deletion
494   virtual void genBuffers(WGC3Dsizei count, WebGLId* ids);
495   virtual void genFramebuffers(WGC3Dsizei count, WebGLId* ids);
496   virtual void genRenderbuffers(WGC3Dsizei count, WebGLId* ids);
497   virtual void genTextures(WGC3Dsizei count, WebGLId* ids);
498
499   virtual void deleteBuffers(WGC3Dsizei count, WebGLId* ids);
500   virtual void deleteFramebuffers(WGC3Dsizei count, WebGLId* ids);
501   virtual void deleteRenderbuffers(WGC3Dsizei count, WebGLId* ids);
502   virtual void deleteTextures(WGC3Dsizei count, WebGLId* ids);
503
504   virtual WebGLId createBuffer();
505   virtual WebGLId createFramebuffer();
506   virtual WebGLId createRenderbuffer();
507   virtual WebGLId createTexture();
508
509   virtual void deleteBuffer(WebGLId);
510   virtual void deleteFramebuffer(WebGLId);
511   virtual void deleteRenderbuffer(WebGLId);
512   virtual void deleteTexture(WebGLId);
513
514   virtual WebGLId createProgram();
515   virtual WebGLId createShader(WGC3Denum);
516
517   virtual void deleteProgram(WebGLId);
518   virtual void deleteShader(WebGLId);
519
520   virtual void synthesizeGLError(WGC3Denum);
521
522   virtual void* mapBufferSubDataCHROMIUM(
523       WGC3Denum target, WGC3Dintptr offset,
524       WGC3Dsizeiptr size, WGC3Denum access);
525   virtual void unmapBufferSubDataCHROMIUM(const void*);
526   virtual void* mapTexSubImage2DCHROMIUM(
527       WGC3Denum target,
528       WGC3Dint level,
529       WGC3Dint xoffset,
530       WGC3Dint yoffset,
531       WGC3Dsizei width,
532       WGC3Dsizei height,
533       WGC3Denum format,
534       WGC3Denum type,
535       WGC3Denum access);
536   virtual void unmapTexSubImage2DCHROMIUM(const void*);
537
538   virtual void setVisibilityCHROMIUM(bool visible);
539
540   virtual void discardFramebufferEXT(WGC3Denum target,
541                                      WGC3Dsizei numAttachments,
542                                      const WGC3Denum* attachments);
543   virtual void copyTextureToParentTextureCHROMIUM(
544       WebGLId texture, WebGLId parentTexture);
545
546   virtual blink::WebString getRequestableExtensionsCHROMIUM();
547   virtual void requestExtensionCHROMIUM(const char*);
548
549   virtual void blitFramebufferCHROMIUM(
550       WGC3Dint srcX0, WGC3Dint srcY0, WGC3Dint srcX1, WGC3Dint srcY1,
551       WGC3Dint dstX0, WGC3Dint dstY0, WGC3Dint dstX1, WGC3Dint dstY1,
552       WGC3Dbitfield mask, WGC3Denum filter);
553   virtual void renderbufferStorageMultisampleCHROMIUM(
554       WGC3Denum target, WGC3Dsizei samples, WGC3Denum internalformat,
555       WGC3Dsizei width, WGC3Dsizei height);
556
557   virtual blink::WebString getTranslatedShaderSourceANGLE(WebGLId shader);
558
559   virtual void setContextLostCallback(
560       WebGraphicsContext3D::WebGraphicsContextLostCallback* callback);
561
562   virtual WGC3Denum getGraphicsResetStatusARB();
563
564   virtual void setErrorMessageCallback(
565       WebGraphicsContext3D::WebGraphicsErrorMessageCallback* callback);
566
567   virtual void texImageIOSurface2DCHROMIUM(
568       WGC3Denum target, WGC3Dint width, WGC3Dint height,
569       WGC3Duint ioSurfaceId, WGC3Duint plane);
570
571   virtual void texStorage2DEXT(
572       WGC3Denum target, WGC3Dint levels, WGC3Duint internalformat,
573       WGC3Dint width, WGC3Dint height);
574
575   virtual WebGLId createQueryEXT();
576   virtual void deleteQueryEXT(WebGLId query);
577   virtual WGC3Dboolean isQueryEXT(WGC3Duint query);
578   virtual void beginQueryEXT(WGC3Denum target, WebGLId query);
579   virtual void endQueryEXT(WGC3Denum target);
580   virtual void getQueryivEXT(
581       WGC3Denum target, WGC3Denum pname, WGC3Dint* params);
582   virtual void getQueryObjectuivEXT(
583       WebGLId query, WGC3Denum pname, WGC3Duint* params);
584
585   virtual void copyTextureCHROMIUM(WGC3Denum target, WebGLId source_id,
586                                    WebGLId dest_id, WGC3Dint level,
587                                    WGC3Denum internal_format,
588                                    WGC3Denum dest_type);
589
590   virtual void bindUniformLocationCHROMIUM(WebGLId program, WGC3Dint location,
591                                            const WGC3Dchar* uniform);
592
593   virtual void shallowFlushCHROMIUM();
594   virtual void shallowFinishCHROMIUM();
595
596   virtual void genMailboxCHROMIUM(WGC3Dbyte* mailbox);
597   virtual void produceTextureCHROMIUM(WGC3Denum target,
598                                       const WGC3Dbyte* mailbox);
599   virtual void consumeTextureCHROMIUM(WGC3Denum target,
600                                       const WGC3Dbyte* mailbox);
601
602   virtual void insertEventMarkerEXT(const WGC3Dchar* marker);
603   virtual void pushGroupMarkerEXT(const WGC3Dchar* marker);
604   virtual void popGroupMarkerEXT();
605
606   // GL_OES_vertex_array_object
607   virtual WebGLId createVertexArrayOES();
608   virtual void deleteVertexArrayOES(WebGLId array);
609   virtual WGC3Dboolean isVertexArrayOES(WebGLId array);
610   virtual void bindVertexArrayOES(WebGLId array);
611
612   virtual void bindTexImage2DCHROMIUM(WGC3Denum target, WGC3Dint image_id);
613   virtual void releaseTexImage2DCHROMIUM(WGC3Denum target, WGC3Dint image_id);
614
615   virtual void* mapBufferCHROMIUM(WGC3Denum target, WGC3Denum access);
616   virtual WGC3Dboolean unmapBufferCHROMIUM(WGC3Denum target);
617
618   // Async pixel transfer functions.
619   virtual void asyncTexImage2DCHROMIUM(
620       WGC3Denum target,
621       WGC3Dint level,
622       WGC3Denum internalformat,
623       WGC3Dsizei width,
624       WGC3Dsizei height,
625       WGC3Dint border,
626       WGC3Denum format,
627       WGC3Denum type,
628       const void* pixels);
629   virtual void asyncTexSubImage2DCHROMIUM(
630       WGC3Denum target,
631       WGC3Dint level,
632       WGC3Dint xoffset,
633       WGC3Dint yoffset,
634       WGC3Dsizei width,
635       WGC3Dsizei height,
636       WGC3Denum format,
637       WGC3Denum type,
638       const void* pixels);
639   virtual void waitAsyncTexImage2DCHROMIUM(WGC3Denum target);
640
641   // GL_EXT_draw_buffers
642   virtual void drawBuffersEXT(
643       WGC3Dsizei n,
644       const WGC3Denum* bufs);
645
646   // GL_ANGLE_instanced_arrays
647   virtual void drawArraysInstancedANGLE(WGC3Denum mode, WGC3Dint first,
648       WGC3Dsizei count, WGC3Dsizei primcount);
649   virtual void drawElementsInstancedANGLE(WGC3Denum mode, WGC3Dsizei count,
650       WGC3Denum type, WGC3Dintptr offset, WGC3Dsizei primcount);
651   virtual void vertexAttribDivisorANGLE(WGC3Duint index, WGC3Duint divisor);
652
653   // GL_CHROMIUM_map_image
654   virtual WGC3Duint createImageCHROMIUM(
655       WGC3Dsizei width, WGC3Dsizei height, WGC3Denum internalformat);
656   virtual void destroyImageCHROMIUM(WGC3Duint image_id);
657   virtual void getImageParameterivCHROMIUM(
658       WGC3Duint image_id, WGC3Denum pname, WGC3Dint* params);
659   virtual void* mapImageCHROMIUM(WGC3Duint image_id, WGC3Denum access);
660   virtual void unmapImageCHROMIUM(WGC3Duint image_id);
661
662   // GL_EXT_multisampled_render_to_texture
663   virtual void framebufferTexture2DMultisampleEXT(WGC3Denum target,
664                                     WGC3Denum attachment,
665                                     WGC3Denum textarget,
666                                     WebGLId texture,
667                                     WGC3Dint level,
668                                     WGC3Dsizei samples);
669   virtual void renderbufferStorageMultisampleEXT(
670       WGC3Denum target, WGC3Dsizei samples, WGC3Denum internalformat,
671       WGC3Dsizei width, WGC3Dsizei height);
672
673   virtual GrGLInterface* createGrGLInterface();
674
675  private:
676   // These are the same error codes as used by EGL.
677   enum Error {
678     SUCCESS               = 0x3000,
679     BAD_ATTRIBUTE         = 0x3004,
680     CONTEXT_LOST          = 0x300E
681   };
682   // WebGraphicsContext3DCommandBufferImpl configuration attributes. Those in
683   // the 16-bit range are the same as used by EGL. Those outside the 16-bit
684   // range are unique to Chromium. Attributes are matched using a closest fit
685   // algorithm.
686   // Changes to this enum should also be copied to
687   // gpu/command_buffer/common/gles2_cmd_utils.cc and to
688   // gpu/command_buffer/client/gl_in_process_context.cc
689   enum Attribute {
690     ALPHA_SIZE                = 0x3021,
691     BLUE_SIZE                 = 0x3022,
692     GREEN_SIZE                = 0x3023,
693     RED_SIZE                  = 0x3024,
694     DEPTH_SIZE                = 0x3025,
695     STENCIL_SIZE              = 0x3026,
696     SAMPLES                   = 0x3031,
697     SAMPLE_BUFFERS            = 0x3032,
698     HEIGHT                    = 0x3056,
699     WIDTH                     = 0x3057,
700     NONE                      = 0x3038,  // Attrib list = terminator
701     SHARE_RESOURCES           = 0x10000,
702     BIND_GENERATES_RESOURCES  = 0x10001,
703     FAIL_IF_MAJOR_PERF_CAVEAT = 0x10002
704   };
705   friend class WebGraphicsContext3DErrorMessageCallback;
706
707   // Initialize the underlying GL context. May be called multiple times; second
708   // and subsequent calls are ignored. Must be called from the thread that is
709   // going to use this object to issue GL commands (which might not be the main
710   // thread).
711   bool MaybeInitializeGL();
712
713   bool InitializeCommandBuffer(bool onscreen,
714       WebGraphicsContext3DCommandBufferImpl* share_context);
715
716   void Destroy();
717
718   // Create a CommandBufferProxy that renders directly to a view. The view and
719   // the associated window must not be destroyed until the returned
720   // CommandBufferProxy has been destroyed, otherwise the GPU process might
721   // attempt to render to an invalid window handle.
722   //
723   // NOTE: on Mac OS X, this entry point is only used to set up the
724   // accelerated compositor's output. On this platform, we actually pass
725   // a gfx::PluginWindowHandle in place of the gfx::NativeViewId,
726   // because the facility to allocate a fake PluginWindowHandle is
727   // already in place. We could add more entry points and messages to
728   // allocate both fake PluginWindowHandles and NativeViewIds and map
729   // from fake NativeViewIds to PluginWindowHandles, but this seems like
730   // unnecessary complexity at the moment.
731   bool CreateContext(bool onscreen);
732
733   virtual void OnGpuChannelLost();
734   virtual void OnErrorMessage(const std::string& message, int id);
735
736   bool initialize_failed_;
737
738   bool visible_;
739
740   // State needed by MaybeInitializeGL.
741   scoped_refptr<GpuChannelHost> host_;
742   int32 surface_id_;
743   GURL active_url_;
744
745   WebGraphicsContext3D::WebGraphicsContextLostCallback* context_lost_callback_;
746   WGC3Denum context_lost_reason_;
747
748   WebGraphicsContext3D::WebGraphicsErrorMessageCallback*
749       error_message_callback_;
750   scoped_ptr<WebGraphicsContext3DErrorMessageCallback>
751       client_error_message_callback_;
752
753   blink::WebGraphicsContext3D::Attributes attributes_;
754   gfx::GpuPreference gpu_preference_;
755
756   // Errors raised by synthesizeGLError().
757   std::vector<WGC3Denum> synthetic_errors_;
758
759   base::WeakPtrFactory<WebGraphicsContext3DCommandBufferImpl> weak_ptr_factory_;
760
761   bool initialized_;
762   scoped_ptr<CommandBufferProxyImpl> command_buffer_;
763   scoped_ptr<gpu::gles2::GLES2CmdHelper> gles2_helper_;
764   scoped_ptr<gpu::TransferBuffer> transfer_buffer_;
765   gpu::gles2::GLES2Interface* gl_;
766   scoped_ptr<gpu::gles2::GLES2Implementation> real_gl_;
767   scoped_ptr<gpu::gles2::GLES2Interface> trace_gl_;
768   Error last_error_;
769   bool bind_generates_resources_;
770   SharedMemoryLimits mem_limits_;
771
772   uint32_t flush_id_;
773   scoped_refptr<ShareGroup> share_group_;
774 };
775
776 }  // namespace content
777
778 #endif  // CONTENT_COMMON_GPU_CLIENT_WEBGRAPHICSCONTEXT3D_COMMAND_BUFFER_IMPL_H_