1// Copyright 2014 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#include "base/logging.h"
6#include "mojo/examples/pepper_container_app/graphics_3d_resource.h"
7#include "mojo/examples/pepper_container_app/thunk.h"
8#include "mojo/public/c/gles2/gles2.h"
9#include "ppapi/thunk/enter.h"
10#include "ppapi/thunk/ppb_graphics_3d_api.h"
11
12namespace mojo {
13namespace examples {
14
15namespace {
16
17typedef ppapi::thunk::EnterResource<ppapi::thunk::PPB_Graphics3D_API> Enter3D;
18
19bool IsBoundGraphics(Enter3D* enter) {
20  return enter->succeeded() &&
21         static_cast<Graphics3DResource*>(enter->object())->IsBoundGraphics();
22}
23
24void ActiveTexture(PP_Resource context_id, GLenum texture) {
25  Enter3D enter(context_id, true);
26  if (IsBoundGraphics(&enter)) {
27    glActiveTexture(texture);
28  }
29}
30
31void AttachShader(PP_Resource context_id, GLuint program, GLuint shader) {
32  Enter3D enter(context_id, true);
33  if (IsBoundGraphics(&enter)) {
34    glAttachShader(program, shader);
35  }
36}
37
38void BindAttribLocation(PP_Resource context_id,
39                        GLuint program,
40                        GLuint index,
41                        const char* name) {
42  Enter3D enter(context_id, true);
43  if (IsBoundGraphics(&enter)) {
44    glBindAttribLocation(program, index, name);
45  }
46}
47
48void BindBuffer(PP_Resource context_id, GLenum target, GLuint buffer) {
49  Enter3D enter(context_id, true);
50  if (IsBoundGraphics(&enter)) {
51    glBindBuffer(target, buffer);
52  }
53}
54
55void BindFramebuffer(PP_Resource context_id,
56                     GLenum target,
57                     GLuint framebuffer) {
58  Enter3D enter(context_id, true);
59  if (IsBoundGraphics(&enter)) {
60    glBindFramebuffer(target, framebuffer);
61  }
62}
63
64void BindRenderbuffer(PP_Resource context_id,
65                      GLenum target,
66                      GLuint renderbuffer) {
67  Enter3D enter(context_id, true);
68  if (IsBoundGraphics(&enter)) {
69    glBindRenderbuffer(target, renderbuffer);
70  }
71}
72
73void BindTexture(PP_Resource context_id, GLenum target, GLuint texture) {
74  Enter3D enter(context_id, true);
75  if (IsBoundGraphics(&enter)) {
76    glBindTexture(target, texture);
77  }
78}
79
80void BlendColor(PP_Resource context_id,
81                GLclampf red,
82                GLclampf green,
83                GLclampf blue,
84                GLclampf alpha) {
85  Enter3D enter(context_id, true);
86  if (IsBoundGraphics(&enter)) {
87    glBlendColor(red, green, blue, alpha);
88  }
89}
90
91void BlendEquation(PP_Resource context_id, GLenum mode) {
92  Enter3D enter(context_id, true);
93  if (IsBoundGraphics(&enter)) {
94    glBlendEquation(mode);
95  }
96}
97
98void BlendEquationSeparate(PP_Resource context_id,
99                           GLenum modeRGB,
100                           GLenum modeAlpha) {
101  Enter3D enter(context_id, true);
102  if (IsBoundGraphics(&enter)) {
103    glBlendEquationSeparate(modeRGB, modeAlpha);
104  }
105}
106
107void BlendFunc(PP_Resource context_id, GLenum sfactor, GLenum dfactor) {
108  Enter3D enter(context_id, true);
109  if (IsBoundGraphics(&enter)) {
110    glBlendFunc(sfactor, dfactor);
111  }
112}
113
114void BlendFuncSeparate(PP_Resource context_id,
115                       GLenum srcRGB,
116                       GLenum dstRGB,
117                       GLenum srcAlpha,
118                       GLenum dstAlpha) {
119  Enter3D enter(context_id, true);
120  if (IsBoundGraphics(&enter)) {
121    glBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
122  }
123}
124
125void BufferData(PP_Resource context_id,
126                GLenum target,
127                GLsizeiptr size,
128                const void* data,
129                GLenum usage) {
130  Enter3D enter(context_id, true);
131  if (IsBoundGraphics(&enter)) {
132    glBufferData(target, size, data, usage);
133  }
134}
135
136void BufferSubData(PP_Resource context_id,
137                   GLenum target,
138                   GLintptr offset,
139                   GLsizeiptr size,
140                   const void* data) {
141  Enter3D enter(context_id, true);
142  if (IsBoundGraphics(&enter)) {
143    glBufferSubData(target, offset, size, data);
144  }
145}
146
147GLenum CheckFramebufferStatus(PP_Resource context_id, GLenum target) {
148  Enter3D enter(context_id, true);
149  if (IsBoundGraphics(&enter)) {
150    return glCheckFramebufferStatus(target);
151  } else {
152    return 0;
153  }
154}
155
156void Clear(PP_Resource context_id, GLbitfield mask) {
157  Enter3D enter(context_id, true);
158  if (IsBoundGraphics(&enter)) {
159    glClear(mask);
160  }
161}
162
163void ClearColor(PP_Resource context_id,
164                GLclampf red,
165                GLclampf green,
166                GLclampf blue,
167                GLclampf alpha) {
168  Enter3D enter(context_id, true);
169  if (IsBoundGraphics(&enter)) {
170    glClearColor(red, green, blue, alpha);
171  }
172}
173
174void ClearDepthf(PP_Resource context_id, GLclampf depth) {
175  Enter3D enter(context_id, true);
176  if (IsBoundGraphics(&enter)) {
177    glClearDepthf(depth);
178  }
179}
180
181void ClearStencil(PP_Resource context_id, GLint s) {
182  Enter3D enter(context_id, true);
183  if (IsBoundGraphics(&enter)) {
184    glClearStencil(s);
185  }
186}
187
188void ColorMask(PP_Resource context_id,
189               GLboolean red,
190               GLboolean green,
191               GLboolean blue,
192               GLboolean alpha) {
193  Enter3D enter(context_id, true);
194  if (IsBoundGraphics(&enter)) {
195    glColorMask(red, green, blue, alpha);
196  }
197}
198
199void CompileShader(PP_Resource context_id, GLuint shader) {
200  Enter3D enter(context_id, true);
201  if (IsBoundGraphics(&enter)) {
202    glCompileShader(shader);
203  }
204}
205
206void CompressedTexImage2D(PP_Resource context_id,
207                          GLenum target,
208                          GLint level,
209                          GLenum internalformat,
210                          GLsizei width,
211                          GLsizei height,
212                          GLint border,
213                          GLsizei imageSize,
214                          const void* data) {
215  Enter3D enter(context_id, true);
216  if (IsBoundGraphics(&enter)) {
217    glCompressedTexImage2D(
218        target, level, internalformat, width, height, border, imageSize, data);
219  }
220}
221
222void CompressedTexSubImage2D(PP_Resource context_id,
223                             GLenum target,
224                             GLint level,
225                             GLint xoffset,
226                             GLint yoffset,
227                             GLsizei width,
228                             GLsizei height,
229                             GLenum format,
230                             GLsizei imageSize,
231                             const void* data) {
232  Enter3D enter(context_id, true);
233  if (IsBoundGraphics(&enter)) {
234    glCompressedTexSubImage2D(target,
235                              level,
236                              xoffset,
237                              yoffset,
238                              width,
239                              height,
240                              format,
241                              imageSize,
242                              data);
243  }
244}
245
246void CopyTexImage2D(PP_Resource context_id,
247                    GLenum target,
248                    GLint level,
249                    GLenum internalformat,
250                    GLint x,
251                    GLint y,
252                    GLsizei width,
253                    GLsizei height,
254                    GLint border) {
255  Enter3D enter(context_id, true);
256  if (IsBoundGraphics(&enter)) {
257    glCopyTexImage2D(
258        target, level, internalformat, x, y, width, height, border);
259  }
260}
261
262void CopyTexSubImage2D(PP_Resource context_id,
263                       GLenum target,
264                       GLint level,
265                       GLint xoffset,
266                       GLint yoffset,
267                       GLint x,
268                       GLint y,
269                       GLsizei width,
270                       GLsizei height) {
271  Enter3D enter(context_id, true);
272  if (IsBoundGraphics(&enter)) {
273    glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height);
274  }
275}
276
277GLuint CreateProgram(PP_Resource context_id) {
278  Enter3D enter(context_id, true);
279  if (IsBoundGraphics(&enter)) {
280    return glCreateProgram();
281  } else {
282    return 0;
283  }
284}
285
286GLuint CreateShader(PP_Resource context_id, GLenum type) {
287  Enter3D enter(context_id, true);
288  if (IsBoundGraphics(&enter)) {
289    return glCreateShader(type);
290  } else {
291    return 0;
292  }
293}
294
295void CullFace(PP_Resource context_id, GLenum mode) {
296  Enter3D enter(context_id, true);
297  if (IsBoundGraphics(&enter)) {
298    glCullFace(mode);
299  }
300}
301
302void DeleteBuffers(PP_Resource context_id, GLsizei n, const GLuint* buffers) {
303  Enter3D enter(context_id, true);
304  if (IsBoundGraphics(&enter)) {
305    glDeleteBuffers(n, buffers);
306  }
307}
308
309void DeleteFramebuffers(PP_Resource context_id,
310                        GLsizei n,
311                        const GLuint* framebuffers) {
312  Enter3D enter(context_id, true);
313  if (IsBoundGraphics(&enter)) {
314    glDeleteFramebuffers(n, framebuffers);
315  }
316}
317
318void DeleteProgram(PP_Resource context_id, GLuint program) {
319  Enter3D enter(context_id, true);
320  if (IsBoundGraphics(&enter)) {
321    glDeleteProgram(program);
322  }
323}
324
325void DeleteRenderbuffers(PP_Resource context_id,
326                         GLsizei n,
327                         const GLuint* renderbuffers) {
328  Enter3D enter(context_id, true);
329  if (IsBoundGraphics(&enter)) {
330    glDeleteRenderbuffers(n, renderbuffers);
331  }
332}
333
334void DeleteShader(PP_Resource context_id, GLuint shader) {
335  Enter3D enter(context_id, true);
336  if (IsBoundGraphics(&enter)) {
337    glDeleteShader(shader);
338  }
339}
340
341void DeleteTextures(PP_Resource context_id, GLsizei n, const GLuint* textures) {
342  Enter3D enter(context_id, true);
343  if (IsBoundGraphics(&enter)) {
344    glDeleteTextures(n, textures);
345  }
346}
347
348void DepthFunc(PP_Resource context_id, GLenum func) {
349  Enter3D enter(context_id, true);
350  if (IsBoundGraphics(&enter)) {
351    glDepthFunc(func);
352  }
353}
354
355void DepthMask(PP_Resource context_id, GLboolean flag) {
356  Enter3D enter(context_id, true);
357  if (IsBoundGraphics(&enter)) {
358    glDepthMask(flag);
359  }
360}
361
362void DepthRangef(PP_Resource context_id, GLclampf zNear, GLclampf zFar) {
363  Enter3D enter(context_id, true);
364  if (IsBoundGraphics(&enter)) {
365    glDepthRangef(zNear, zFar);
366  }
367}
368
369void DetachShader(PP_Resource context_id, GLuint program, GLuint shader) {
370  Enter3D enter(context_id, true);
371  if (IsBoundGraphics(&enter)) {
372    glDetachShader(program, shader);
373  }
374}
375
376void Disable(PP_Resource context_id, GLenum cap) {
377  Enter3D enter(context_id, true);
378  if (IsBoundGraphics(&enter)) {
379    glDisable(cap);
380  }
381}
382
383void DisableVertexAttribArray(PP_Resource context_id, GLuint index) {
384  Enter3D enter(context_id, true);
385  if (IsBoundGraphics(&enter)) {
386    glDisableVertexAttribArray(index);
387  }
388}
389
390void DrawArrays(PP_Resource context_id,
391                GLenum mode,
392                GLint first,
393                GLsizei count) {
394  Enter3D enter(context_id, true);
395  if (IsBoundGraphics(&enter)) {
396    glDrawArrays(mode, first, count);
397  }
398}
399
400void DrawElements(PP_Resource context_id,
401                  GLenum mode,
402                  GLsizei count,
403                  GLenum type,
404                  const void* indices) {
405  Enter3D enter(context_id, true);
406  if (IsBoundGraphics(&enter)) {
407    glDrawElements(mode, count, type, indices);
408  }
409}
410
411void Enable(PP_Resource context_id, GLenum cap) {
412  Enter3D enter(context_id, true);
413  if (IsBoundGraphics(&enter)) {
414    glEnable(cap);
415  }
416}
417
418void EnableVertexAttribArray(PP_Resource context_id, GLuint index) {
419  Enter3D enter(context_id, true);
420  if (IsBoundGraphics(&enter)) {
421    glEnableVertexAttribArray(index);
422  }
423}
424
425void Finish(PP_Resource context_id) {
426  Enter3D enter(context_id, true);
427  if (IsBoundGraphics(&enter)) {
428    glFinish();
429  }
430}
431
432void Flush(PP_Resource context_id) {
433  Enter3D enter(context_id, true);
434  if (IsBoundGraphics(&enter)) {
435    glFlush();
436  }
437}
438
439void FramebufferRenderbuffer(PP_Resource context_id,
440                             GLenum target,
441                             GLenum attachment,
442                             GLenum renderbuffertarget,
443                             GLuint renderbuffer) {
444  Enter3D enter(context_id, true);
445  if (IsBoundGraphics(&enter)) {
446    glFramebufferRenderbuffer(
447        target, attachment, renderbuffertarget, renderbuffer);
448  }
449}
450
451void FramebufferTexture2D(PP_Resource context_id,
452                          GLenum target,
453                          GLenum attachment,
454                          GLenum textarget,
455                          GLuint texture,
456                          GLint level) {
457  Enter3D enter(context_id, true);
458  if (IsBoundGraphics(&enter)) {
459    glFramebufferTexture2D(target, attachment, textarget, texture, level);
460  }
461}
462
463void FrontFace(PP_Resource context_id, GLenum mode) {
464  Enter3D enter(context_id, true);
465  if (IsBoundGraphics(&enter)) {
466    glFrontFace(mode);
467  }
468}
469
470void GenBuffers(PP_Resource context_id, GLsizei n, GLuint* buffers) {
471  Enter3D enter(context_id, true);
472  if (IsBoundGraphics(&enter)) {
473    glGenBuffers(n, buffers);
474  }
475}
476
477void GenerateMipmap(PP_Resource context_id, GLenum target) {
478  Enter3D enter(context_id, true);
479  if (IsBoundGraphics(&enter)) {
480    glGenerateMipmap(target);
481  }
482}
483
484void GenFramebuffers(PP_Resource context_id, GLsizei n, GLuint* framebuffers) {
485  Enter3D enter(context_id, true);
486  if (IsBoundGraphics(&enter)) {
487    glGenFramebuffers(n, framebuffers);
488  }
489}
490
491void GenRenderbuffers(PP_Resource context_id,
492                      GLsizei n,
493                      GLuint* renderbuffers) {
494  Enter3D enter(context_id, true);
495  if (IsBoundGraphics(&enter)) {
496    glGenRenderbuffers(n, renderbuffers);
497  }
498}
499
500void GenTextures(PP_Resource context_id, GLsizei n, GLuint* textures) {
501  Enter3D enter(context_id, true);
502  if (IsBoundGraphics(&enter)) {
503    glGenTextures(n, textures);
504  }
505}
506
507void GetActiveAttrib(PP_Resource context_id,
508                     GLuint program,
509                     GLuint index,
510                     GLsizei bufsize,
511                     GLsizei* length,
512                     GLint* size,
513                     GLenum* type,
514                     char* name) {
515  Enter3D enter(context_id, true);
516  if (IsBoundGraphics(&enter)) {
517    glGetActiveAttrib(program, index, bufsize, length, size, type, name);
518  }
519}
520
521void GetActiveUniform(PP_Resource context_id,
522                      GLuint program,
523                      GLuint index,
524                      GLsizei bufsize,
525                      GLsizei* length,
526                      GLint* size,
527                      GLenum* type,
528                      char* name) {
529  Enter3D enter(context_id, true);
530  if (IsBoundGraphics(&enter)) {
531    glGetActiveUniform(program, index, bufsize, length, size, type, name);
532  }
533}
534
535void GetAttachedShaders(PP_Resource context_id,
536                        GLuint program,
537                        GLsizei maxcount,
538                        GLsizei* count,
539                        GLuint* shaders) {
540  Enter3D enter(context_id, true);
541  if (IsBoundGraphics(&enter)) {
542    glGetAttachedShaders(program, maxcount, count, shaders);
543  }
544}
545
546GLint GetAttribLocation(PP_Resource context_id,
547                        GLuint program,
548                        const char* name) {
549  Enter3D enter(context_id, true);
550  if (IsBoundGraphics(&enter)) {
551    return glGetAttribLocation(program, name);
552  } else {
553    return -1;
554  }
555}
556
557void GetBooleanv(PP_Resource context_id, GLenum pname, GLboolean* params) {
558  Enter3D enter(context_id, true);
559  if (IsBoundGraphics(&enter)) {
560    glGetBooleanv(pname, params);
561  }
562}
563
564void GetBufferParameteriv(PP_Resource context_id,
565                          GLenum target,
566                          GLenum pname,
567                          GLint* params) {
568  Enter3D enter(context_id, true);
569  if (IsBoundGraphics(&enter)) {
570    glGetBufferParameteriv(target, pname, params);
571  }
572}
573
574GLenum GetError(PP_Resource context_id) {
575  Enter3D enter(context_id, true);
576  if (IsBoundGraphics(&enter)) {
577    return glGetError();
578  } else {
579    return 0;
580  }
581}
582
583void GetFloatv(PP_Resource context_id, GLenum pname, GLfloat* params) {
584  Enter3D enter(context_id, true);
585  if (IsBoundGraphics(&enter)) {
586    glGetFloatv(pname, params);
587  }
588}
589
590void GetFramebufferAttachmentParameteriv(PP_Resource context_id,
591                                         GLenum target,
592                                         GLenum attachment,
593                                         GLenum pname,
594                                         GLint* params) {
595  Enter3D enter(context_id, true);
596  if (IsBoundGraphics(&enter)) {
597    glGetFramebufferAttachmentParameteriv(target, attachment, pname, params);
598  }
599}
600
601void GetIntegerv(PP_Resource context_id, GLenum pname, GLint* params) {
602  Enter3D enter(context_id, true);
603  if (IsBoundGraphics(&enter)) {
604    glGetIntegerv(pname, params);
605  }
606}
607
608void GetProgramiv(PP_Resource context_id,
609                  GLuint program,
610                  GLenum pname,
611                  GLint* params) {
612  Enter3D enter(context_id, true);
613  if (IsBoundGraphics(&enter)) {
614    glGetProgramiv(program, pname, params);
615  }
616}
617
618void GetProgramInfoLog(PP_Resource context_id,
619                       GLuint program,
620                       GLsizei bufsize,
621                       GLsizei* length,
622                       char* infolog) {
623  Enter3D enter(context_id, true);
624  if (IsBoundGraphics(&enter)) {
625    glGetProgramInfoLog(program, bufsize, length, infolog);
626  }
627}
628
629void GetRenderbufferParameteriv(PP_Resource context_id,
630                                GLenum target,
631                                GLenum pname,
632                                GLint* params) {
633  Enter3D enter(context_id, true);
634  if (IsBoundGraphics(&enter)) {
635    glGetRenderbufferParameteriv(target, pname, params);
636  }
637}
638
639void GetShaderiv(PP_Resource context_id,
640                 GLuint shader,
641                 GLenum pname,
642                 GLint* params) {
643  Enter3D enter(context_id, true);
644  if (IsBoundGraphics(&enter)) {
645    glGetShaderiv(shader, pname, params);
646  }
647}
648
649void GetShaderInfoLog(PP_Resource context_id,
650                      GLuint shader,
651                      GLsizei bufsize,
652                      GLsizei* length,
653                      char* infolog) {
654  Enter3D enter(context_id, true);
655  if (IsBoundGraphics(&enter)) {
656    glGetShaderInfoLog(shader, bufsize, length, infolog);
657  }
658}
659
660void GetShaderPrecisionFormat(PP_Resource context_id,
661                              GLenum shadertype,
662                              GLenum precisiontype,
663                              GLint* range,
664                              GLint* precision) {
665  Enter3D enter(context_id, true);
666  if (IsBoundGraphics(&enter)) {
667    glGetShaderPrecisionFormat(shadertype, precisiontype, range, precision);
668  }
669}
670
671void GetShaderSource(PP_Resource context_id,
672                     GLuint shader,
673                     GLsizei bufsize,
674                     GLsizei* length,
675                     char* source) {
676  Enter3D enter(context_id, true);
677  if (IsBoundGraphics(&enter)) {
678    glGetShaderSource(shader, bufsize, length, source);
679  }
680}
681
682const GLubyte* GetString(PP_Resource context_id, GLenum name) {
683  Enter3D enter(context_id, true);
684  if (IsBoundGraphics(&enter)) {
685    return glGetString(name);
686  } else {
687    return NULL;
688  }
689}
690
691void GetTexParameterfv(PP_Resource context_id,
692                       GLenum target,
693                       GLenum pname,
694                       GLfloat* params) {
695  Enter3D enter(context_id, true);
696  if (IsBoundGraphics(&enter)) {
697    glGetTexParameterfv(target, pname, params);
698  }
699}
700
701void GetTexParameteriv(PP_Resource context_id,
702                       GLenum target,
703                       GLenum pname,
704                       GLint* params) {
705  Enter3D enter(context_id, true);
706  if (IsBoundGraphics(&enter)) {
707    glGetTexParameteriv(target, pname, params);
708  }
709}
710
711void GetUniformfv(PP_Resource context_id,
712                  GLuint program,
713                  GLint location,
714                  GLfloat* params) {
715  Enter3D enter(context_id, true);
716  if (IsBoundGraphics(&enter)) {
717    glGetUniformfv(program, location, params);
718  }
719}
720
721void GetUniformiv(PP_Resource context_id,
722                  GLuint program,
723                  GLint location,
724                  GLint* params) {
725  Enter3D enter(context_id, true);
726  if (IsBoundGraphics(&enter)) {
727    glGetUniformiv(program, location, params);
728  }
729}
730
731GLint GetUniformLocation(PP_Resource context_id,
732                         GLuint program,
733                         const char* name) {
734  Enter3D enter(context_id, true);
735  if (IsBoundGraphics(&enter)) {
736    return glGetUniformLocation(program, name);
737  } else {
738    return -1;
739  }
740}
741
742void GetVertexAttribfv(PP_Resource context_id,
743                       GLuint index,
744                       GLenum pname,
745                       GLfloat* params) {
746  Enter3D enter(context_id, true);
747  if (IsBoundGraphics(&enter)) {
748    glGetVertexAttribfv(index, pname, params);
749  }
750}
751
752void GetVertexAttribiv(PP_Resource context_id,
753                       GLuint index,
754                       GLenum pname,
755                       GLint* params) {
756  Enter3D enter(context_id, true);
757  if (IsBoundGraphics(&enter)) {
758    glGetVertexAttribiv(index, pname, params);
759  }
760}
761
762void GetVertexAttribPointerv(PP_Resource context_id,
763                             GLuint index,
764                             GLenum pname,
765                             void** pointer) {
766  Enter3D enter(context_id, true);
767  if (IsBoundGraphics(&enter)) {
768    glGetVertexAttribPointerv(index, pname, pointer);
769  }
770}
771
772void Hint(PP_Resource context_id, GLenum target, GLenum mode) {
773  Enter3D enter(context_id, true);
774  if (IsBoundGraphics(&enter)) {
775    glHint(target, mode);
776  }
777}
778
779GLboolean IsBuffer(PP_Resource context_id, GLuint buffer) {
780  Enter3D enter(context_id, true);
781  if (IsBoundGraphics(&enter)) {
782    return glIsBuffer(buffer);
783  } else {
784    return GL_FALSE;
785  }
786}
787
788GLboolean IsEnabled(PP_Resource context_id, GLenum cap) {
789  Enter3D enter(context_id, true);
790  if (IsBoundGraphics(&enter)) {
791    return glIsEnabled(cap);
792  } else {
793    return GL_FALSE;
794  }
795}
796
797GLboolean IsFramebuffer(PP_Resource context_id, GLuint framebuffer) {
798  Enter3D enter(context_id, true);
799  if (IsBoundGraphics(&enter)) {
800    return glIsFramebuffer(framebuffer);
801  } else {
802    return GL_FALSE;
803  }
804}
805
806GLboolean IsProgram(PP_Resource context_id, GLuint program) {
807  Enter3D enter(context_id, true);
808  if (IsBoundGraphics(&enter)) {
809    return glIsProgram(program);
810  } else {
811    return GL_FALSE;
812  }
813}
814
815GLboolean IsRenderbuffer(PP_Resource context_id, GLuint renderbuffer) {
816  Enter3D enter(context_id, true);
817  if (IsBoundGraphics(&enter)) {
818    return glIsRenderbuffer(renderbuffer);
819  } else {
820    return GL_FALSE;
821  }
822}
823
824GLboolean IsShader(PP_Resource context_id, GLuint shader) {
825  Enter3D enter(context_id, true);
826  if (IsBoundGraphics(&enter)) {
827    return glIsShader(shader);
828  } else {
829    return GL_FALSE;
830  }
831}
832
833GLboolean IsTexture(PP_Resource context_id, GLuint texture) {
834  Enter3D enter(context_id, true);
835  if (IsBoundGraphics(&enter)) {
836    return glIsTexture(texture);
837  } else {
838    return GL_FALSE;
839  }
840}
841
842void LineWidth(PP_Resource context_id, GLfloat width) {
843  Enter3D enter(context_id, true);
844  if (IsBoundGraphics(&enter)) {
845    glLineWidth(width);
846  }
847}
848
849void LinkProgram(PP_Resource context_id, GLuint program) {
850  Enter3D enter(context_id, true);
851  if (IsBoundGraphics(&enter)) {
852    glLinkProgram(program);
853  }
854}
855
856void PixelStorei(PP_Resource context_id, GLenum pname, GLint param) {
857  Enter3D enter(context_id, true);
858  if (IsBoundGraphics(&enter)) {
859    glPixelStorei(pname, param);
860  }
861}
862
863void PolygonOffset(PP_Resource context_id, GLfloat factor, GLfloat units) {
864  Enter3D enter(context_id, true);
865  if (IsBoundGraphics(&enter)) {
866    glPolygonOffset(factor, units);
867  }
868}
869
870void ReadPixels(PP_Resource context_id,
871                GLint x,
872                GLint y,
873                GLsizei width,
874                GLsizei height,
875                GLenum format,
876                GLenum type,
877                void* pixels) {
878  Enter3D enter(context_id, true);
879  if (IsBoundGraphics(&enter)) {
880    glReadPixels(x, y, width, height, format, type, pixels);
881  }
882}
883
884void ReleaseShaderCompiler(PP_Resource context_id) {
885  Enter3D enter(context_id, true);
886  if (IsBoundGraphics(&enter)) {
887    glReleaseShaderCompiler();
888  }
889}
890
891void RenderbufferStorage(PP_Resource context_id,
892                         GLenum target,
893                         GLenum internalformat,
894                         GLsizei width,
895                         GLsizei height) {
896  Enter3D enter(context_id, true);
897  if (IsBoundGraphics(&enter)) {
898    glRenderbufferStorage(target, internalformat, width, height);
899  }
900}
901
902void SampleCoverage(PP_Resource context_id, GLclampf value, GLboolean invert) {
903  Enter3D enter(context_id, true);
904  if (IsBoundGraphics(&enter)) {
905    glSampleCoverage(value, invert);
906  }
907}
908
909void Scissor(PP_Resource context_id,
910             GLint x,
911             GLint y,
912             GLsizei width,
913             GLsizei height) {
914  Enter3D enter(context_id, true);
915  if (IsBoundGraphics(&enter)) {
916    glScissor(x, y, width, height);
917  }
918}
919
920void ShaderBinary(PP_Resource context_id,
921                  GLsizei n,
922                  const GLuint* shaders,
923                  GLenum binaryformat,
924                  const void* binary,
925                  GLsizei length) {
926  Enter3D enter(context_id, true);
927  if (IsBoundGraphics(&enter)) {
928    glShaderBinary(n, shaders, binaryformat, binary, length);
929  }
930}
931
932void ShaderSource(PP_Resource context_id,
933                  GLuint shader,
934                  GLsizei count,
935                  const char** str,
936                  const GLint* length) {
937  Enter3D enter(context_id, true);
938  if (IsBoundGraphics(&enter)) {
939    glShaderSource(shader, count, str, length);
940  }
941}
942
943void StencilFunc(PP_Resource context_id, GLenum func, GLint ref, GLuint mask) {
944  Enter3D enter(context_id, true);
945  if (IsBoundGraphics(&enter)) {
946    glStencilFunc(func, ref, mask);
947  }
948}
949
950void StencilFuncSeparate(PP_Resource context_id,
951                         GLenum face,
952                         GLenum func,
953                         GLint ref,
954                         GLuint mask) {
955  Enter3D enter(context_id, true);
956  if (IsBoundGraphics(&enter)) {
957    glStencilFuncSeparate(face, func, ref, mask);
958  }
959}
960
961void StencilMask(PP_Resource context_id, GLuint mask) {
962  Enter3D enter(context_id, true);
963  if (IsBoundGraphics(&enter)) {
964    glStencilMask(mask);
965  }
966}
967
968void StencilMaskSeparate(PP_Resource context_id, GLenum face, GLuint mask) {
969  Enter3D enter(context_id, true);
970  if (IsBoundGraphics(&enter)) {
971    glStencilMaskSeparate(face, mask);
972  }
973}
974
975void StencilOp(PP_Resource context_id,
976               GLenum fail,
977               GLenum zfail,
978               GLenum zpass) {
979  Enter3D enter(context_id, true);
980  if (IsBoundGraphics(&enter)) {
981    glStencilOp(fail, zfail, zpass);
982  }
983}
984
985void StencilOpSeparate(PP_Resource context_id,
986                       GLenum face,
987                       GLenum fail,
988                       GLenum zfail,
989                       GLenum zpass) {
990  Enter3D enter(context_id, true);
991  if (IsBoundGraphics(&enter)) {
992    glStencilOpSeparate(face, fail, zfail, zpass);
993  }
994}
995
996void TexImage2D(PP_Resource context_id,
997                GLenum target,
998                GLint level,
999                GLint internalformat,
1000                GLsizei width,
1001                GLsizei height,
1002                GLint border,
1003                GLenum format,
1004                GLenum type,
1005                const void* pixels) {
1006  Enter3D enter(context_id, true);
1007  if (IsBoundGraphics(&enter)) {
1008    glTexImage2D(target,
1009                 level,
1010                 internalformat,
1011                 width,
1012                 height,
1013                 border,
1014                 format,
1015                 type,
1016                 pixels);
1017  }
1018}
1019
1020void TexParameterf(PP_Resource context_id,
1021                   GLenum target,
1022                   GLenum pname,
1023                   GLfloat param) {
1024  Enter3D enter(context_id, true);
1025  if (IsBoundGraphics(&enter)) {
1026    glTexParameterf(target, pname, param);
1027  }
1028}
1029
1030void TexParameterfv(PP_Resource context_id,
1031                    GLenum target,
1032                    GLenum pname,
1033                    const GLfloat* params) {
1034  Enter3D enter(context_id, true);
1035  if (IsBoundGraphics(&enter)) {
1036    glTexParameterfv(target, pname, params);
1037  }
1038}
1039
1040void TexParameteri(PP_Resource context_id,
1041                   GLenum target,
1042                   GLenum pname,
1043                   GLint param) {
1044  Enter3D enter(context_id, true);
1045  if (IsBoundGraphics(&enter)) {
1046    glTexParameteri(target, pname, param);
1047  }
1048}
1049
1050void TexParameteriv(PP_Resource context_id,
1051                    GLenum target,
1052                    GLenum pname,
1053                    const GLint* params) {
1054  Enter3D enter(context_id, true);
1055  if (IsBoundGraphics(&enter)) {
1056    glTexParameteriv(target, pname, params);
1057  }
1058}
1059
1060void TexSubImage2D(PP_Resource context_id,
1061                   GLenum target,
1062                   GLint level,
1063                   GLint xoffset,
1064                   GLint yoffset,
1065                   GLsizei width,
1066                   GLsizei height,
1067                   GLenum format,
1068                   GLenum type,
1069                   const void* pixels) {
1070  Enter3D enter(context_id, true);
1071  if (IsBoundGraphics(&enter)) {
1072    glTexSubImage2D(
1073        target, level, xoffset, yoffset, width, height, format, type, pixels);
1074  }
1075}
1076
1077void Uniform1f(PP_Resource context_id, GLint location, GLfloat x) {
1078  Enter3D enter(context_id, true);
1079  if (IsBoundGraphics(&enter)) {
1080    glUniform1f(location, x);
1081  }
1082}
1083
1084void Uniform1fv(PP_Resource context_id,
1085                GLint location,
1086                GLsizei count,
1087                const GLfloat* v) {
1088  Enter3D enter(context_id, true);
1089  if (IsBoundGraphics(&enter)) {
1090    glUniform1fv(location, count, v);
1091  }
1092}
1093
1094void Uniform1i(PP_Resource context_id, GLint location, GLint x) {
1095  Enter3D enter(context_id, true);
1096  if (IsBoundGraphics(&enter)) {
1097    glUniform1i(location, x);
1098  }
1099}
1100
1101void Uniform1iv(PP_Resource context_id,
1102                GLint location,
1103                GLsizei count,
1104                const GLint* v) {
1105  Enter3D enter(context_id, true);
1106  if (IsBoundGraphics(&enter)) {
1107    glUniform1iv(location, count, v);
1108  }
1109}
1110
1111void Uniform2f(PP_Resource context_id, GLint location, GLfloat x, GLfloat y) {
1112  Enter3D enter(context_id, true);
1113  if (IsBoundGraphics(&enter)) {
1114    glUniform2f(location, x, y);
1115  }
1116}
1117
1118void Uniform2fv(PP_Resource context_id,
1119                GLint location,
1120                GLsizei count,
1121                const GLfloat* v) {
1122  Enter3D enter(context_id, true);
1123  if (IsBoundGraphics(&enter)) {
1124    glUniform2fv(location, count, v);
1125  }
1126}
1127
1128void Uniform2i(PP_Resource context_id, GLint location, GLint x, GLint y) {
1129  Enter3D enter(context_id, true);
1130  if (IsBoundGraphics(&enter)) {
1131    glUniform2i(location, x, y);
1132  }
1133}
1134
1135void Uniform2iv(PP_Resource context_id,
1136                GLint location,
1137                GLsizei count,
1138                const GLint* v) {
1139  Enter3D enter(context_id, true);
1140  if (IsBoundGraphics(&enter)) {
1141    glUniform2iv(location, count, v);
1142  }
1143}
1144
1145void Uniform3f(PP_Resource context_id,
1146               GLint location,
1147               GLfloat x,
1148               GLfloat y,
1149               GLfloat z) {
1150  Enter3D enter(context_id, true);
1151  if (IsBoundGraphics(&enter)) {
1152    glUniform3f(location, x, y, z);
1153  }
1154}
1155
1156void Uniform3fv(PP_Resource context_id,
1157                GLint location,
1158                GLsizei count,
1159                const GLfloat* v) {
1160  Enter3D enter(context_id, true);
1161  if (IsBoundGraphics(&enter)) {
1162    glUniform3fv(location, count, v);
1163  }
1164}
1165
1166void Uniform3i(PP_Resource context_id,
1167               GLint location,
1168               GLint x,
1169               GLint y,
1170               GLint z) {
1171  Enter3D enter(context_id, true);
1172  if (IsBoundGraphics(&enter)) {
1173    glUniform3i(location, x, y, z);
1174  }
1175}
1176
1177void Uniform3iv(PP_Resource context_id,
1178                GLint location,
1179                GLsizei count,
1180                const GLint* v) {
1181  Enter3D enter(context_id, true);
1182  if (IsBoundGraphics(&enter)) {
1183    glUniform3iv(location, count, v);
1184  }
1185}
1186
1187void Uniform4f(PP_Resource context_id,
1188               GLint location,
1189               GLfloat x,
1190               GLfloat y,
1191               GLfloat z,
1192               GLfloat w) {
1193  Enter3D enter(context_id, true);
1194  if (IsBoundGraphics(&enter)) {
1195    glUniform4f(location, x, y, z, w);
1196  }
1197}
1198
1199void Uniform4fv(PP_Resource context_id,
1200                GLint location,
1201                GLsizei count,
1202                const GLfloat* v) {
1203  Enter3D enter(context_id, true);
1204  if (IsBoundGraphics(&enter)) {
1205    glUniform4fv(location, count, v);
1206  }
1207}
1208
1209void Uniform4i(PP_Resource context_id,
1210               GLint location,
1211               GLint x,
1212               GLint y,
1213               GLint z,
1214               GLint w) {
1215  Enter3D enter(context_id, true);
1216  if (IsBoundGraphics(&enter)) {
1217    glUniform4i(location, x, y, z, w);
1218  }
1219}
1220
1221void Uniform4iv(PP_Resource context_id,
1222                GLint location,
1223                GLsizei count,
1224                const GLint* v) {
1225  Enter3D enter(context_id, true);
1226  if (IsBoundGraphics(&enter)) {
1227    glUniform4iv(location, count, v);
1228  }
1229}
1230
1231void UniformMatrix2fv(PP_Resource context_id,
1232                      GLint location,
1233                      GLsizei count,
1234                      GLboolean transpose,
1235                      const GLfloat* value) {
1236  Enter3D enter(context_id, true);
1237  if (IsBoundGraphics(&enter)) {
1238    glUniformMatrix2fv(location, count, transpose, value);
1239  }
1240}
1241
1242void UniformMatrix3fv(PP_Resource context_id,
1243                      GLint location,
1244                      GLsizei count,
1245                      GLboolean transpose,
1246                      const GLfloat* value) {
1247  Enter3D enter(context_id, true);
1248  if (IsBoundGraphics(&enter)) {
1249    glUniformMatrix3fv(location, count, transpose, value);
1250  }
1251}
1252
1253void UniformMatrix4fv(PP_Resource context_id,
1254                      GLint location,
1255                      GLsizei count,
1256                      GLboolean transpose,
1257                      const GLfloat* value) {
1258  Enter3D enter(context_id, true);
1259  if (IsBoundGraphics(&enter)) {
1260    glUniformMatrix4fv(location, count, transpose, value);
1261  }
1262}
1263
1264void UseProgram(PP_Resource context_id, GLuint program) {
1265  Enter3D enter(context_id, true);
1266  if (IsBoundGraphics(&enter)) {
1267    glUseProgram(program);
1268  }
1269}
1270
1271void ValidateProgram(PP_Resource context_id, GLuint program) {
1272  Enter3D enter(context_id, true);
1273  if (IsBoundGraphics(&enter)) {
1274    glValidateProgram(program);
1275  }
1276}
1277
1278void VertexAttrib1f(PP_Resource context_id, GLuint indx, GLfloat x) {
1279  Enter3D enter(context_id, true);
1280  if (IsBoundGraphics(&enter)) {
1281    glVertexAttrib1f(indx, x);
1282  }
1283}
1284
1285void VertexAttrib1fv(PP_Resource context_id,
1286                     GLuint indx,
1287                     const GLfloat* values) {
1288  Enter3D enter(context_id, true);
1289  if (IsBoundGraphics(&enter)) {
1290    glVertexAttrib1fv(indx, values);
1291  }
1292}
1293
1294void VertexAttrib2f(PP_Resource context_id, GLuint indx, GLfloat x, GLfloat y) {
1295  Enter3D enter(context_id, true);
1296  if (IsBoundGraphics(&enter)) {
1297    glVertexAttrib2f(indx, x, y);
1298  }
1299}
1300
1301void VertexAttrib2fv(PP_Resource context_id,
1302                     GLuint indx,
1303                     const GLfloat* values) {
1304  Enter3D enter(context_id, true);
1305  if (IsBoundGraphics(&enter)) {
1306    glVertexAttrib2fv(indx, values);
1307  }
1308}
1309
1310void VertexAttrib3f(PP_Resource context_id,
1311                    GLuint indx,
1312                    GLfloat x,
1313                    GLfloat y,
1314                    GLfloat z) {
1315  Enter3D enter(context_id, true);
1316  if (IsBoundGraphics(&enter)) {
1317    glVertexAttrib3f(indx, x, y, z);
1318  }
1319}
1320
1321void VertexAttrib3fv(PP_Resource context_id,
1322                     GLuint indx,
1323                     const GLfloat* values) {
1324  Enter3D enter(context_id, true);
1325  if (IsBoundGraphics(&enter)) {
1326    glVertexAttrib3fv(indx, values);
1327  }
1328}
1329
1330void VertexAttrib4f(PP_Resource context_id,
1331                    GLuint indx,
1332                    GLfloat x,
1333                    GLfloat y,
1334                    GLfloat z,
1335                    GLfloat w) {
1336  Enter3D enter(context_id, true);
1337  if (IsBoundGraphics(&enter)) {
1338    glVertexAttrib4f(indx, x, y, z, w);
1339  }
1340}
1341
1342void VertexAttrib4fv(PP_Resource context_id,
1343                     GLuint indx,
1344                     const GLfloat* values) {
1345  Enter3D enter(context_id, true);
1346  if (IsBoundGraphics(&enter)) {
1347    glVertexAttrib4fv(indx, values);
1348  }
1349}
1350
1351void VertexAttribPointer(PP_Resource context_id,
1352                         GLuint indx,
1353                         GLint size,
1354                         GLenum type,
1355                         GLboolean normalized,
1356                         GLsizei stride,
1357                         const void* ptr) {
1358  Enter3D enter(context_id, true);
1359  if (IsBoundGraphics(&enter)) {
1360    glVertexAttribPointer(indx, size, type, normalized, stride, ptr);
1361  }
1362}
1363
1364void Viewport(PP_Resource context_id,
1365              GLint x,
1366              GLint y,
1367              GLsizei width,
1368              GLsizei height) {
1369  Enter3D enter(context_id, true);
1370  if (IsBoundGraphics(&enter)) {
1371    glViewport(x, y, width, height);
1372  }
1373}
1374
1375}  // namespace
1376
1377const PPB_OpenGLES2* GetPPB_OpenGLES2_Thunk() {
1378  static const struct PPB_OpenGLES2 ppb_opengles2 = {
1379      &ActiveTexture,                       &AttachShader,
1380      &BindAttribLocation,                  &BindBuffer,
1381      &BindFramebuffer,                     &BindRenderbuffer,
1382      &BindTexture,                         &BlendColor,
1383      &BlendEquation,                       &BlendEquationSeparate,
1384      &BlendFunc,                           &BlendFuncSeparate,
1385      &BufferData,                          &BufferSubData,
1386      &CheckFramebufferStatus,              &Clear,
1387      &ClearColor,                          &ClearDepthf,
1388      &ClearStencil,                        &ColorMask,
1389      &CompileShader,                       &CompressedTexImage2D,
1390      &CompressedTexSubImage2D,             &CopyTexImage2D,
1391      &CopyTexSubImage2D,                   &CreateProgram,
1392      &CreateShader,                        &CullFace,
1393      &DeleteBuffers,                       &DeleteFramebuffers,
1394      &DeleteProgram,                       &DeleteRenderbuffers,
1395      &DeleteShader,                        &DeleteTextures,
1396      &DepthFunc,                           &DepthMask,
1397      &DepthRangef,                         &DetachShader,
1398      &Disable,                             &DisableVertexAttribArray,
1399      &DrawArrays,                          &DrawElements,
1400      &Enable,                              &EnableVertexAttribArray,
1401      &Finish,                              &Flush,
1402      &FramebufferRenderbuffer,             &FramebufferTexture2D,
1403      &FrontFace,                           &GenBuffers,
1404      &GenerateMipmap,                      &GenFramebuffers,
1405      &GenRenderbuffers,                    &GenTextures,
1406      &GetActiveAttrib,                     &GetActiveUniform,
1407      &GetAttachedShaders,                  &GetAttribLocation,
1408      &GetBooleanv,                         &GetBufferParameteriv,
1409      &GetError,                            &GetFloatv,
1410      &GetFramebufferAttachmentParameteriv, &GetIntegerv,
1411      &GetProgramiv,                        &GetProgramInfoLog,
1412      &GetRenderbufferParameteriv,          &GetShaderiv,
1413      &GetShaderInfoLog,                    &GetShaderPrecisionFormat,
1414      &GetShaderSource,                     &GetString,
1415      &GetTexParameterfv,                   &GetTexParameteriv,
1416      &GetUniformfv,                        &GetUniformiv,
1417      &GetUniformLocation,                  &GetVertexAttribfv,
1418      &GetVertexAttribiv,                   &GetVertexAttribPointerv,
1419      &Hint,                                &IsBuffer,
1420      &IsEnabled,                           &IsFramebuffer,
1421      &IsProgram,                           &IsRenderbuffer,
1422      &IsShader,                            &IsTexture,
1423      &LineWidth,                           &LinkProgram,
1424      &PixelStorei,                         &PolygonOffset,
1425      &ReadPixels,                          &ReleaseShaderCompiler,
1426      &RenderbufferStorage,                 &SampleCoverage,
1427      &Scissor,                             &ShaderBinary,
1428      &ShaderSource,                        &StencilFunc,
1429      &StencilFuncSeparate,                 &StencilMask,
1430      &StencilMaskSeparate,                 &StencilOp,
1431      &StencilOpSeparate,                   &TexImage2D,
1432      &TexParameterf,                       &TexParameterfv,
1433      &TexParameteri,                       &TexParameteriv,
1434      &TexSubImage2D,                       &Uniform1f,
1435      &Uniform1fv,                          &Uniform1i,
1436      &Uniform1iv,                          &Uniform2f,
1437      &Uniform2fv,                          &Uniform2i,
1438      &Uniform2iv,                          &Uniform3f,
1439      &Uniform3fv,                          &Uniform3i,
1440      &Uniform3iv,                          &Uniform4f,
1441      &Uniform4fv,                          &Uniform4i,
1442      &Uniform4iv,                          &UniformMatrix2fv,
1443      &UniformMatrix3fv,                    &UniformMatrix4fv,
1444      &UseProgram,                          &ValidateProgram,
1445      &VertexAttrib1f,                      &VertexAttrib1fv,
1446      &VertexAttrib2f,                      &VertexAttrib2fv,
1447      &VertexAttrib3f,                      &VertexAttrib3fv,
1448      &VertexAttrib4f,                      &VertexAttrib4fv,
1449      &VertexAttribPointer,                 &Viewport};
1450  return &ppb_opengles2;
1451}
1452
1453}  // namespace examples
1454}  // namespace mojo
1455