Add signalSyncPoint to the WebGraphicsContext3D command buffer impls.
[chromium-blink-merge.git] / cc / test / test_web_graphics_context_3d.cc
blobe315f65dd80fc9192f46fdff846714b78c9c6d09
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"
7 #include <algorithm>
8 #include <string>
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;
25 namespace cc {
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++),
40 next_texture_id_(1),
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),
46 context_lost_(false),
47 context_lost_callback_(NULL),
48 max_texture_size_(1024),
49 width_(0),
50 height_(0) {
53 TestWebGraphicsContext3D::TestWebGraphicsContext3D(
54 const WebGraphicsContext3D::Attributes& attributes)
55 : FakeWebGraphicsContext3D(),
56 context_id_(s_context_id++),
57 next_texture_id_(1),
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),
64 context_lost_(false),
65 context_lost_callback_(NULL),
66 max_texture_size_(1024),
67 width_(0),
68 height_(0) {
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() {
86 return width_;
89 int TestWebGraphicsContext3D::height() {
90 return height_;
93 void TestWebGraphicsContext3D::reshape(int width, int height) {
94 width_ = width;
95 height_ = height;
98 bool TestWebGraphicsContext3D::isContextLost() {
99 return context_lost_;
102 WGC3Denum TestWebGraphicsContext3D::getGraphicsResetStatusARB() {
103 return context_lost_ ? GL_UNKNOWN_CONTEXT_RESET_ARB : GL_NO_ERROR;
106 WGC3Denum TestWebGraphicsContext3D::checkFramebufferStatus(
107 WGC3Denum target) {
108 if (context_lost_)
109 return GL_FRAMEBUFFER_UNDEFINED_OES;
110 return GL_FRAMEBUFFER_COMPLETE;
113 WebGraphicsContext3D::Attributes
114 TestWebGraphicsContext3D::getContextAttributes() {
115 return attributes_;
118 WebKit::WebString TestWebGraphicsContext3D::getString(WGC3Denum name) {
119 std::string string;
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(
132 WebGLId program,
133 const WGC3Dchar* name) {
134 return 0;
137 WGC3Dsizeiptr TestWebGraphicsContext3D::getVertexAttribOffset(
138 WGC3Duint index,
139 WGC3Denum pname) {
140 return 0;
143 WGC3Dboolean TestWebGraphicsContext3D::isBuffer(
144 WebGLId buffer) {
145 return false;
148 WGC3Dboolean TestWebGraphicsContext3D::isEnabled(
149 WGC3Denum cap) {
150 return false;
153 WGC3Dboolean TestWebGraphicsContext3D::isFramebuffer(
154 WebGLId framebuffer) {
155 return false;
158 WGC3Dboolean TestWebGraphicsContext3D::isProgram(
159 WebGLId program) {
160 return false;
163 WGC3Dboolean TestWebGraphicsContext3D::isRenderbuffer(
164 WebGLId renderbuffer) {
165 return false;
168 WGC3Dboolean TestWebGraphicsContext3D::isShader(
169 WebGLId shader) {
170 return false;
173 WGC3Dboolean TestWebGraphicsContext3D::isTexture(
174 WebGLId texture) {
175 return false;
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);
222 return texture_id;
225 void TestWebGraphicsContext3D::deleteTexture(WebGLId texture_id) {
226 DCHECK(std::find(textures_.begin(), textures_.end(), texture_id) !=
227 textures_.end());
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) {
237 if (!program)
238 return;
239 EXPECT_EQ(kProgramId | context_id_ << 16, program);
242 void TestWebGraphicsContext3D::bindBuffer(WGC3Denum target, WebGLId buffer) {
243 if (!buffer)
244 return;
245 EXPECT_EQ(kBufferId | context_id_ << 16, buffer);
248 void TestWebGraphicsContext3D::bindFramebuffer(
249 WGC3Denum target, WebGLId framebuffer) {
250 if (!framebuffer)
251 return;
252 EXPECT_EQ(kFramebufferId | context_id_ << 16, framebuffer);
255 void TestWebGraphicsContext3D::bindRenderbuffer(
256 WGC3Denum target, WebGLId renderbuffer) {
257 if (!renderbuffer)
258 return;
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_;
272 if (!texture_id)
273 return;
274 if (texture_id == kExternalTextureId)
275 return;
276 DCHECK(std::find(textures_.begin(), textures_.end(), texture_id) !=
277 textures_.end());
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(
292 WebGLId query,
293 WGC3Denum pname,
294 WGC3Duint* params) {
295 // If the context is lost, behave as if result is available.
296 if (pname == GL_QUERY_RESULT_AVAILABLE_EXT)
297 *params = 1;
300 void TestWebGraphicsContext3D::getIntegerv(
301 WGC3Denum pname,
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;
312 mailbox[2] = '\0';
313 if (++mailbox_name1 == 0) {
314 mailbox_name1 = '1';
315 ++mailbox_name2;
319 void TestWebGraphicsContext3D::setContextLostCallback(
320 WebGraphicsContextLostCallback* callback) {
321 context_lost_callback_ = callback;
324 void TestWebGraphicsContext3D::loseContextCHROMIUM(WGC3Denum current,
325 WGC3Denum other) {
326 if (context_lost_)
327 return;
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;
341 return texture_id;
344 } // namespace cc