1 // Copyright 2011 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.
5 #include "cc/test/test_web_graphics_context_3d.h"
10 #include "base/logging.h"
11 #include "gpu/GLES2/gl2extchromium.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/khronos/GLES2/gl2ext.h"
15 using WebKit::WGC3Dboolean
;
16 using WebKit::WGC3Dchar
;
17 using WebKit::WGC3Denum
;
18 using WebKit::WGC3Dint
;
19 using WebKit::WGC3Dsizei
;
20 using WebKit::WGC3Dsizeiptr
;
21 using WebKit::WGC3Duint
;
22 using WebKit::WebGLId
;
23 using WebKit::WebGraphicsContext3D
;
27 static const WebGLId kBufferId
= 1;
28 static const WebGLId kFramebufferId
= 2;
29 static const WebGLId kProgramId
= 3;
30 static const WebGLId kRenderbufferId
= 4;
31 static const WebGLId kShaderId
= 5;
33 static unsigned s_context_id
= 1;
35 const WebGLId
TestWebGraphicsContext3D::kExternalTextureId
= 1337;
37 TestWebGraphicsContext3D::TestWebGraphicsContext3D()
38 : FakeWebGraphicsContext3D(),
39 context_id_(s_context_id
++),
41 have_extension_io_surface_(false),
42 have_extension_egl_image_(false),
43 times_make_current_succeeds_(-1),
44 times_bind_texture_succeeds_(-1),
45 times_end_query_succeeds_(-1),
47 context_lost_callback_(NULL
),
48 max_texture_size_(1024),
53 TestWebGraphicsContext3D::TestWebGraphicsContext3D(
54 const WebGraphicsContext3D::Attributes
& attributes
)
55 : FakeWebGraphicsContext3D(),
56 context_id_(s_context_id
++),
58 attributes_(attributes
),
59 have_extension_io_surface_(false),
60 have_extension_egl_image_(false),
61 times_make_current_succeeds_(-1),
62 times_bind_texture_succeeds_(-1),
63 times_end_query_succeeds_(-1),
65 context_lost_callback_(NULL
),
66 max_texture_size_(1024),
71 TestWebGraphicsContext3D::~TestWebGraphicsContext3D() {
74 bool TestWebGraphicsContext3D::makeContextCurrent() {
75 if (times_make_current_succeeds_
>= 0) {
76 if (!times_make_current_succeeds_
) {
77 loseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB
,
78 GL_INNOCENT_CONTEXT_RESET_ARB
);
80 --times_make_current_succeeds_
;
82 return !context_lost_
;
85 int TestWebGraphicsContext3D::width() {
89 int TestWebGraphicsContext3D::height() {
93 void TestWebGraphicsContext3D::reshape(int width
, int height
) {
98 bool TestWebGraphicsContext3D::isContextLost() {
102 WGC3Denum
TestWebGraphicsContext3D::getGraphicsResetStatusARB() {
103 return context_lost_
? GL_UNKNOWN_CONTEXT_RESET_ARB
: GL_NO_ERROR
;
106 WGC3Denum
TestWebGraphicsContext3D::checkFramebufferStatus(
109 return GL_FRAMEBUFFER_UNDEFINED_OES
;
110 return GL_FRAMEBUFFER_COMPLETE
;
113 WebGraphicsContext3D::Attributes
114 TestWebGraphicsContext3D::getContextAttributes() {
118 WebKit::WebString
TestWebGraphicsContext3D::getString(WGC3Denum name
) {
121 if (name
== GL_EXTENSIONS
) {
122 if (have_extension_io_surface_
)
123 string
+= "GL_CHROMIUM_iosurface GL_ARB_texture_rectangle ";
124 if (have_extension_egl_image_
)
125 string
+= "GL_OES_EGL_image_external";
128 return WebKit::WebString::fromUTF8(string
.c_str());
131 WGC3Dint
TestWebGraphicsContext3D::getUniformLocation(
133 const WGC3Dchar
* name
) {
137 WGC3Dsizeiptr
TestWebGraphicsContext3D::getVertexAttribOffset(
143 WGC3Dboolean
TestWebGraphicsContext3D::isBuffer(
148 WGC3Dboolean
TestWebGraphicsContext3D::isEnabled(
153 WGC3Dboolean
TestWebGraphicsContext3D::isFramebuffer(
154 WebGLId framebuffer
) {
158 WGC3Dboolean
TestWebGraphicsContext3D::isProgram(
163 WGC3Dboolean
TestWebGraphicsContext3D::isRenderbuffer(
164 WebGLId renderbuffer
) {
168 WGC3Dboolean
TestWebGraphicsContext3D::isShader(
173 WGC3Dboolean
TestWebGraphicsContext3D::isTexture(
178 WebGLId
TestWebGraphicsContext3D::createBuffer() {
179 return kBufferId
| context_id_
<< 16;
182 void TestWebGraphicsContext3D::deleteBuffer(WebGLId id
) {
183 EXPECT_EQ(kBufferId
| context_id_
<< 16, id
);
186 WebGLId
TestWebGraphicsContext3D::createFramebuffer() {
187 return kFramebufferId
| context_id_
<< 16;
190 void TestWebGraphicsContext3D::deleteFramebuffer(WebGLId id
) {
191 EXPECT_EQ(kFramebufferId
| context_id_
<< 16, id
);
194 WebGLId
TestWebGraphicsContext3D::createProgram() {
195 return kProgramId
| context_id_
<< 16;
198 void TestWebGraphicsContext3D::deleteProgram(WebGLId id
) {
199 EXPECT_EQ(kProgramId
| context_id_
<< 16, id
);
202 WebGLId
TestWebGraphicsContext3D::createRenderbuffer() {
203 return kRenderbufferId
| context_id_
<< 16;
206 void TestWebGraphicsContext3D::deleteRenderbuffer(WebGLId id
) {
207 EXPECT_EQ(kRenderbufferId
| context_id_
<< 16, id
);
210 WebGLId
TestWebGraphicsContext3D::createShader(WGC3Denum
) {
211 return kShaderId
| context_id_
<< 16;
214 void TestWebGraphicsContext3D::deleteShader(WebGLId id
) {
215 EXPECT_EQ(kShaderId
| context_id_
<< 16, id
);
218 WebGLId
TestWebGraphicsContext3D::createTexture() {
219 WebGLId texture_id
= NextTextureId();
220 DCHECK_NE(texture_id
, kExternalTextureId
);
221 textures_
.push_back(texture_id
);
225 void TestWebGraphicsContext3D::deleteTexture(WebGLId texture_id
) {
226 DCHECK(std::find(textures_
.begin(), textures_
.end(), texture_id
) !=
228 textures_
.erase(std::find(textures_
.begin(), textures_
.end(), texture_id
));
231 void TestWebGraphicsContext3D::attachShader(WebGLId program
, WebGLId shader
) {
232 EXPECT_EQ(kProgramId
| context_id_
<< 16, program
);
233 EXPECT_EQ(kShaderId
| context_id_
<< 16, shader
);
236 void TestWebGraphicsContext3D::useProgram(WebGLId program
) {
239 EXPECT_EQ(kProgramId
| context_id_
<< 16, program
);
242 void TestWebGraphicsContext3D::bindBuffer(WGC3Denum target
, WebGLId buffer
) {
245 EXPECT_EQ(kBufferId
| context_id_
<< 16, buffer
);
248 void TestWebGraphicsContext3D::bindFramebuffer(
249 WGC3Denum target
, WebGLId framebuffer
) {
252 EXPECT_EQ(kFramebufferId
| context_id_
<< 16, framebuffer
);
255 void TestWebGraphicsContext3D::bindRenderbuffer(
256 WGC3Denum target
, WebGLId renderbuffer
) {
259 EXPECT_EQ(kRenderbufferId
| context_id_
<< 16, renderbuffer
);
262 void TestWebGraphicsContext3D::bindTexture(
263 WGC3Denum target
, WebGLId texture_id
) {
264 if (times_bind_texture_succeeds_
>= 0) {
265 if (!times_bind_texture_succeeds_
) {
266 loseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB
,
267 GL_INNOCENT_CONTEXT_RESET_ARB
);
269 --times_bind_texture_succeeds_
;
274 if (texture_id
== kExternalTextureId
)
276 DCHECK(std::find(textures_
.begin(), textures_
.end(), texture_id
) !=
278 used_textures_
.insert(texture_id
);
281 void TestWebGraphicsContext3D::endQueryEXT(WGC3Denum target
) {
282 if (times_end_query_succeeds_
>= 0) {
283 if (!times_end_query_succeeds_
) {
284 loseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB
,
285 GL_INNOCENT_CONTEXT_RESET_ARB
);
287 --times_end_query_succeeds_
;
291 void TestWebGraphicsContext3D::getQueryObjectuivEXT(
295 // If the context is lost, behave as if result is available.
296 if (pname
== GL_QUERY_RESULT_AVAILABLE_EXT
)
300 void TestWebGraphicsContext3D::getIntegerv(
302 WebKit::WGC3Dint
* value
) {
303 if (pname
== GL_MAX_TEXTURE_SIZE
)
304 *value
= max_texture_size_
;
307 void TestWebGraphicsContext3D::genMailboxCHROMIUM(WebKit::WGC3Dbyte
* mailbox
) {
308 static char mailbox_name1
= '1';
309 static char mailbox_name2
= '1';
310 mailbox
[0] = mailbox_name1
;
311 mailbox
[1] = mailbox_name2
;
313 if (++mailbox_name1
== 0) {
319 void TestWebGraphicsContext3D::setContextLostCallback(
320 WebGraphicsContextLostCallback
* callback
) {
321 context_lost_callback_
= callback
;
324 void TestWebGraphicsContext3D::loseContextCHROMIUM(WGC3Denum current
,
328 context_lost_
= true;
329 if (context_lost_callback_
)
330 context_lost_callback_
->onContextLost();
332 for (size_t i
= 0; i
< shared_contexts_
.size(); ++i
)
333 shared_contexts_
[i
]->loseContextCHROMIUM(current
, other
);
334 shared_contexts_
.clear();
337 WebGLId
TestWebGraphicsContext3D::NextTextureId() {
338 WebGLId texture_id
= next_texture_id_
++;
339 DCHECK(texture_id
< (1 << 16));
340 texture_id
|= context_id_
<< 16;