Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: cc/test/test_gles2_interface.cc

Issue 93283002: Add GLES2Interface version of TWGC3D Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « cc/test/test_gles2_interface.h ('k') | cc/test/test_web_graphics_context_3d.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 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 "cc/test/test_gles2_interface.h"
6
7 #include <algorithm>
8 #include <string>
9
10 #include "base/bind.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/message_loop/message_loop.h"
14 #include "cc/test/test_context_support.h"
15 #include "gpu/GLES2/gl2extchromium.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/khronos/GLES2/gl2ext.h"
18
19 namespace cc {
20
21 static const GLuint kFramebufferId = 1;
22 static const GLuint kRenderbufferId = 3;
23
24 static uint8 s_context_id = 1;
25
26 const GLuint TestGLES2Interface::kExternalTextureId = 1337;
27
28 static base::LazyInstance<base::Lock>::Leaky
29 g_shared_namespace_lock = LAZY_INSTANCE_INITIALIZER;
30
31 TestGLES2Interface::Namespace*
32 TestGLES2Interface::shared_namespace_ = NULL;
33
34 TestGLES2Interface::Namespace::Namespace()
35 : next_buffer_id(1),
36 next_image_id(1),
37 next_texture_id(1) {
38 }
39
40 TestGLES2Interface::Namespace::~Namespace() {
41 g_shared_namespace_lock.Get().AssertAcquired();
42 if (shared_namespace_ == this)
43 shared_namespace_ = NULL;
44 }
45
46 // static
47 scoped_ptr<TestGLES2Interface> TestGLES2Interface::Create() {
48 return make_scoped_ptr(new TestGLES2Interface());
49 }
50
51 TestGLES2Interface::TestGLES2Interface()
52 : context_id_(s_context_id++),
53 times_make_current_succeeds_(-1),
54 times_bind_texture_succeeds_(-1),
55 times_end_query_succeeds_(-1),
56 times_gen_mailbox_succeeds_(-1),
57 context_lost_(false),
58 times_map_image_chromium_succeeds_(-1),
59 times_map_buffer_chromium_succeeds_(-1),
60 next_program_id_(1000u),
61 next_shader_id_(2000u),
62 max_texture_size_(2048),
63 width_(0),
64 height_(0),
65 test_support_(NULL),
66 bound_buffer_(0),
67 weak_ptr_factory_(this) {
68 CreateNamespace();
69 test_capabilities_.swapbuffers_complete_callback = true;
70 }
71
72 TestGLES2Interface::~TestGLES2Interface() {
73 base::AutoLock lock(g_shared_namespace_lock.Get());
74 namespace_ = NULL;
75 }
76
77 void TestGLES2Interface::CreateNamespace() {
78 base::AutoLock lock(g_shared_namespace_lock.Get());
79 if (shared_namespace_) {
80 namespace_ = shared_namespace_;
81 } else {
82 namespace_ = new Namespace;
83 shared_namespace_ = namespace_.get();
84 }
85 }
86
87 bool TestGLES2Interface::makeContextCurrent() {
88 if (times_make_current_succeeds_ >= 0) {
89 if (!times_make_current_succeeds_) {
90 LoseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB,
91 GL_INNOCENT_CONTEXT_RESET_ARB);
92 }
93 --times_make_current_succeeds_;
94 }
95 return !context_lost_;
96 }
97
98 void TestGLES2Interface::reshapeWithScaleFactor(
99 int width, int height, float scale_factor) {
100 width_ = width;
101 height_ = height;
102 }
103
104 bool TestGLES2Interface::isContextLost() {
105 return context_lost_;
106 }
107
108 GLuint TestGLES2Interface::CreateExternalTexture() {
109 base::AutoLock lock(namespace_->lock);
110 namespace_->textures.Append(kExternalTextureId, new TestTexture());
111 return kExternalTextureId;
112 }
113
114 GLenum TestGLES2Interface::CheckFramebufferStatus(
115 GLenum target) {
116 if (context_lost_)
117 return GL_FRAMEBUFFER_UNDEFINED_OES;
118 return GL_FRAMEBUFFER_COMPLETE;
119 }
120
121 const GLubyte* TestGLES2Interface::GetString(GLenum name) {
122 return NULL;
123 }
124
125 GLint TestGLES2Interface::GetUniformLocation(
126 GLuint program,
127 const GLchar* name) {
128 return 0;
129 }
130
131 void TestGLES2Interface::GetVertexAttribPointerv(
132 GLuint index,
133 GLenum pname,
134 void** pointer) {
135 *pointer = NULL;
136 }
137
138 void TestGLES2Interface::GenBuffers(GLsizei count, GLuint* ids) {
139 for (int i = 0; i < count; ++i)
140 ids[i] = NextBufferId();
141 }
142
143 void TestGLES2Interface::GenFramebuffers(
144 GLsizei count, GLuint* ids) {
145 for (int i = 0; i < count; ++i)
146 ids[i] = kFramebufferId | context_id_ << 16;
147 }
148
149 void TestGLES2Interface::GenRenderbuffers(
150 GLsizei count, GLuint* ids) {
151 for (int i = 0; i < count; ++i)
152 ids[i] = kRenderbufferId | context_id_ << 16;
153 }
154
155 void TestGLES2Interface::GenTextures(GLsizei count, GLuint* ids) {
156 for (int i = 0; i < count; ++i) {
157 ids[i] = NextTextureId();
158 DCHECK_NE(ids[i], kExternalTextureId);
159 }
160 base::AutoLock lock(namespace_->lock);
161 for (int i = 0; i < count; ++i)
162 namespace_->textures.Append(ids[i], new TestTexture());
163 }
164
165 void TestGLES2Interface::DeleteBuffers(GLsizei count, const GLuint* ids) {
166 for (int i = 0; i < count; ++i)
167 RetireBufferId(ids[i]);
168 }
169
170 void TestGLES2Interface::DeleteFramebuffers(
171 GLsizei count, const GLuint* ids) {
172 for (int i = 0; i < count; ++i)
173 DCHECK_EQ(kFramebufferId | context_id_ << 16, ids[i]);
174 }
175
176 void TestGLES2Interface::DeleteRenderbuffers(
177 GLsizei count, const GLuint* ids) {
178 for (int i = 0; i < count; ++i)
179 DCHECK_EQ(kRenderbufferId | context_id_ << 16, ids[i]);
180 }
181
182 void TestGLES2Interface::DeleteTextures(GLsizei count, const GLuint* ids) {
183 for (int i = 0; i < count; ++i)
184 RetireTextureId(ids[i]);
185 base::AutoLock lock(namespace_->lock);
186 for (int i = 0; i < count; ++i) {
187 namespace_->textures.Remove(ids[i]);
188 texture_targets_.UnbindTexture(ids[i]);
189 }
190 }
191
192 GLuint TestGLES2Interface::CreateProgram() {
193 GLuint program = next_program_id_++ | context_id_ << 16;
194 program_set_.insert(program);
195 return program;
196 }
197
198 GLuint TestGLES2Interface::CreateShader(GLenum) {
199 GLuint shader = next_shader_id_++ | context_id_ << 16;
200 shader_set_.insert(shader);
201 return shader;
202 }
203
204 void TestGLES2Interface::DeleteProgram(GLuint id) {
205 if (!program_set_.count(id))
206 ADD_FAILURE() << "DeleteProgram called on unknown program " << id;
207 program_set_.erase(id);
208 }
209
210 void TestGLES2Interface::DeleteShader(GLuint id) {
211 if (!shader_set_.count(id))
212 ADD_FAILURE() << "DeleteShader called on unknown shader " << id;
213 shader_set_.erase(id);
214 }
215
216 void TestGLES2Interface::AttachShader(GLuint program, GLuint shader) {
217 if (!program_set_.count(program))
218 ADD_FAILURE() << "AttachShader called with unknown program " << program;
219 if (!shader_set_.count(shader))
220 ADD_FAILURE() << "AttachShader called with unknown shader " << shader;
221 }
222
223 void TestGLES2Interface::UseProgram(GLuint program) {
224 if (!program)
225 return;
226 if (!program_set_.count(program))
227 ADD_FAILURE() << "UseProgram called on unknown program " << program;
228 }
229
230 void TestGLES2Interface::BindFramebuffer(
231 GLenum target, GLuint framebuffer) {
232 if (!framebuffer)
233 return;
234 DCHECK_EQ(kFramebufferId | context_id_ << 16, framebuffer);
235 }
236
237 void TestGLES2Interface::BindRenderbuffer(
238 GLenum target, GLuint renderbuffer) {
239 if (!renderbuffer)
240 return;
241 DCHECK_EQ(kRenderbufferId | context_id_ << 16, renderbuffer);
242 }
243
244 void TestGLES2Interface::BindTexture(
245 GLenum target, GLuint texture_id) {
246 if (times_bind_texture_succeeds_ >= 0) {
247 if (!times_bind_texture_succeeds_) {
248 LoseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB,
249 GL_INNOCENT_CONTEXT_RESET_ARB);
250 }
251 --times_bind_texture_succeeds_;
252 }
253
254 if (!texture_id)
255 return;
256 base::AutoLock lock(namespace_->lock);
257 DCHECK(namespace_->textures.ContainsId(texture_id));
258 texture_targets_.BindTexture(target, texture_id);
259 used_textures_.insert(texture_id);
260 }
261
262 GLuint TestGLES2Interface::BoundTextureId(
263 GLenum target) {
264 return texture_targets_.BoundTexture(target);
265 }
266
267 void TestGLES2Interface::EndQueryEXT(GLenum target) {
268 if (times_end_query_succeeds_ >= 0) {
269 if (!times_end_query_succeeds_) {
270 LoseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB,
271 GL_INNOCENT_CONTEXT_RESET_ARB);
272 }
273 --times_end_query_succeeds_;
274 }
275 }
276
277 void TestGLES2Interface::GetQueryObjectuivEXT(
278 GLuint query,
279 GLenum pname,
280 GLuint* params) {
281 // If the context is lost, behave as if result is available.
282 if (pname == GL_QUERY_RESULT_AVAILABLE_EXT)
283 *params = 1;
284 }
285
286 void TestGLES2Interface::GetIntegerv(
287 GLenum pname,
288 GLint* value) {
289 if (pname == GL_MAX_TEXTURE_SIZE)
290 *value = max_texture_size_;
291 else if (pname == GL_ACTIVE_TEXTURE)
292 *value = GL_TEXTURE0;
293 }
294
295 void TestGLES2Interface::GenMailboxCHROMIUM(GLbyte* mailbox) {
296 if (times_gen_mailbox_succeeds_ >= 0) {
297 if (!times_gen_mailbox_succeeds_) {
298 LoseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB,
299 GL_INNOCENT_CONTEXT_RESET_ARB);
300 }
301 --times_gen_mailbox_succeeds_;
302 }
303 if (context_lost_) {
304 memset(mailbox, 0, 64);
305 return;
306 }
307
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;
316 }
317 }
318
319 void TestGLES2Interface::LoseContextCHROMIUM(GLenum current,
320 GLenum other) {
321 if (context_lost_)
322 return;
323 context_lost_ = true;
324 /*
325 if (context_lost_callback_)
326 context_lost_callback_->onContextLost();
327 */
328 for (size_t i = 0; i < shared_contexts_.size(); ++i)
329 shared_contexts_[i]->LoseContextCHROMIUM(current, other);
330 shared_contexts_.clear();
331 }
332
333 /*
334 void TestGLES2Interface::prepareTexture() {
335 // TODO(jamesr): This should implemented as ContextSupport::SwapBuffers().
336 if (swap_buffers_callback_) {
337 base::MessageLoop::current()->PostTask(
338 FROM_HERE, base::Bind(&TestGLES2Interface::SwapBuffersComplete,
339 weak_ptr_factory_.GetWeakPtr()));
340 }
341 test_support_->CallAllSyncPointCallbacks();
342 }
343 */
344
345 void TestGLES2Interface::Finish() {
346 test_support_->CallAllSyncPointCallbacks();
347 }
348
349 void TestGLES2Interface::Flush() {
350 test_support_->CallAllSyncPointCallbacks();
351 }
352
353 /*
354 void TestGLES2Interface::SwapBuffersComplete() {
355 if (swap_buffers_callback_)
356 swap_buffers_callback_->onSwapBuffersComplete();
357 }
358 */
359
360 void TestGLES2Interface::BindBuffer(GLenum target,
361 GLuint buffer) {
362 bound_buffer_ = buffer;
363 if (!bound_buffer_)
364 return;
365 uint8 context_id = buffer >> 16;
366 uint8 buffer_id = buffer & 0xffff;
367 base::AutoLock lock(namespace_->lock);
368 DCHECK(buffer_id);
369 DCHECK_LT(buffer_id, namespace_->next_buffer_id);
370 DCHECK_EQ(context_id, context_id_);
371
372 base::ScopedPtrHashMap<uint8, Buffer>& buffers = namespace_->buffers;
373 if (buffers.count(bound_buffer_) == 0)
374 buffers.set(bound_buffer_, make_scoped_ptr(new Buffer).Pass());
375
376 buffers.get(bound_buffer_)->target = target;
377 }
378
379 void TestGLES2Interface::BufferData(GLenum target,
380 GLsizeiptr size,
381 const void* data,
382 GLenum usage) {
383 base::AutoLock lock(namespace_->lock);
384 base::ScopedPtrHashMap<uint8, Buffer>& buffers = namespace_->buffers;
385 DCHECK_GT(buffers.count(bound_buffer_), 0u);
386 DCHECK_EQ(target, buffers.get(bound_buffer_)->target);
387 Buffer* buffer = buffers.get(bound_buffer_);
388 if (context_lost_) {
389 buffer->pixels.reset();
390 return;
391 }
392
393 buffer->pixels.reset(new uint8[size]);
394 buffer->size = size;
395 if (data != NULL)
396 memcpy(buffer->pixels.get(), data, size);
397 }
398
399 void* TestGLES2Interface::MapBufferCHROMIUM(GLenum target,
400 GLenum access) {
401 base::AutoLock lock(namespace_->lock);
402 base::ScopedPtrHashMap<uint8, Buffer>& buffers = namespace_->buffers;
403 DCHECK_GT(buffers.count(bound_buffer_), 0u);
404 DCHECK_EQ(target, buffers.get(bound_buffer_)->target);
405 if (times_map_buffer_chromium_succeeds_ >= 0) {
406 if (!times_map_buffer_chromium_succeeds_) {
407 return NULL;
408 }
409 --times_map_buffer_chromium_succeeds_;
410 }
411 return buffers.get(bound_buffer_)->pixels.get();
412 }
413
414 GLboolean TestGLES2Interface::UnmapBufferCHROMIUM(
415 GLenum target) {
416 base::AutoLock lock(namespace_->lock);
417 base::ScopedPtrHashMap<uint8, Buffer>& buffers = namespace_->buffers;
418 DCHECK_GT(buffers.count(bound_buffer_), 0u);
419 DCHECK_EQ(target, buffers.get(bound_buffer_)->target);
420 buffers.get(bound_buffer_)->pixels.reset();
421 return true;
422 }
423
424 GLuint TestGLES2Interface::CreateImageCHROMIUM(
425 GLsizei width, GLsizei height,
426 GLenum internalformat) {
427 DCHECK_EQ(GL_RGBA8_OES, static_cast<int>(internalformat));
428 GLuint image_id = NextImageId();
429 base::AutoLock lock(namespace_->lock);
430 base::ScopedPtrHashMap<uint8, Image>& images = namespace_->images;
431 images.set(image_id, make_scoped_ptr(new Image).Pass());
432 images.get(image_id)->pixels.reset(new uint8[width * height * 4]);
433 return image_id;
434 }
435
436 void TestGLES2Interface::DestroyImageCHROMIUM(
437 GLuint id) {
438 RetireImageId(id);
439 }
440
441 void TestGLES2Interface::GetImageParameterivCHROMIUM(
442 GLuint image_id,
443 GLenum pname,
444 GLint* params) {
445 base::AutoLock lock(namespace_->lock);
446 DCHECK_GT(namespace_->images.count(image_id), 0u);
447 DCHECK_EQ(GL_IMAGE_ROWBYTES_CHROMIUM, static_cast<int>(pname));
448 *params = 0;
449 }
450
451 void* TestGLES2Interface::MapImageCHROMIUM(GLuint image_id,
452 GLenum access) {
453 base::AutoLock lock(namespace_->lock);
454 base::ScopedPtrHashMap<uint8, Image>& images = namespace_->images;
455 DCHECK_GT(images.count(image_id), 0u);
456 if (times_map_image_chromium_succeeds_ >= 0) {
457 if (!times_map_image_chromium_succeeds_) {
458 return NULL;
459 }
460 --times_map_image_chromium_succeeds_;
461 }
462 return images.get(image_id)->pixels.get();
463 }
464
465 void TestGLES2Interface::UnmapImageCHROMIUM(
466 GLuint image_id) {
467 base::AutoLock lock(namespace_->lock);
468 DCHECK_GT(namespace_->images.count(image_id), 0u);
469 }
470
471 size_t TestGLES2Interface::NumTextures() const {
472 base::AutoLock lock(namespace_->lock);
473 return namespace_->textures.Size();
474 }
475
476 GLuint TestGLES2Interface::TextureAt(int i) const {
477 base::AutoLock lock(namespace_->lock);
478 return namespace_->textures.IdAt(i);
479 }
480
481 GLuint TestGLES2Interface::NextTextureId() {
482 base::AutoLock lock(namespace_->lock);
483 GLuint texture_id = namespace_->next_texture_id++;
484 DCHECK(texture_id < (1 << 16));
485 texture_id |= context_id_ << 16;
486 return texture_id;
487 }
488
489 void TestGLES2Interface::RetireTextureId(GLuint id) {
490 base::AutoLock lock(namespace_->lock);
491 uint8 context_id = id >> 16;
492 uint8 texture_id = id & 0xffff;
493 DCHECK(texture_id);
494 DCHECK_LT(texture_id, namespace_->next_texture_id);
495 DCHECK_EQ(context_id, context_id_);
496 }
497
498 GLuint TestGLES2Interface::NextBufferId() {
499 base::AutoLock lock(namespace_->lock);
500 GLuint buffer_id = namespace_->next_buffer_id++;
501 DCHECK(buffer_id < (1 << 16));
502 buffer_id |= context_id_ << 16;
503 return buffer_id;
504 }
505
506 void TestGLES2Interface::RetireBufferId(GLuint id) {
507 base::AutoLock lock(namespace_->lock);
508 uint8 context_id = id >> 16;
509 uint8 buffer_id = id & 0xffff;
510 DCHECK(buffer_id);
511 DCHECK_LT(buffer_id, namespace_->next_buffer_id);
512 DCHECK_EQ(context_id, context_id_);
513 }
514
515 GLuint TestGLES2Interface::NextImageId() {
516 base::AutoLock lock(namespace_->lock);
517 GLuint image_id = namespace_->next_image_id++;
518 DCHECK(image_id < (1 << 16));
519 image_id |= context_id_ << 16;
520 return image_id;
521 }
522
523 void TestGLES2Interface::RetireImageId(GLuint id) {
524 base::AutoLock lock(namespace_->lock);
525 uint8 context_id = id >> 16;
526 uint8 image_id = id & 0xffff;
527 DCHECK(image_id);
528 DCHECK_LT(image_id, namespace_->next_image_id);
529 DCHECK_EQ(context_id, context_id_);
530 }
531
532 size_t TestGLES2Interface::GetTransferBufferMemoryUsedBytes() const {
533 size_t total_bytes = 0;
534 base::ScopedPtrHashMap<uint8, Buffer>& buffers = namespace_->buffers;
535 base::ScopedPtrHashMap<uint8, Buffer>::iterator it = buffers.begin();
536 for (; it != buffers.end(); ++it) {
537 Buffer* buffer = it->second;
538 if (buffer->target == GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM)
539 total_bytes += buffer->size;
540 }
541 return total_bytes;
542 }
543
544 void TestGLES2Interface::SetMaxTransferBufferUsageBytes(
545 size_t max_transfer_buffer_usage_bytes) {
546 test_capabilities_.max_transfer_buffer_usage_bytes =
547 max_transfer_buffer_usage_bytes;
548 }
549
550 TestGLES2Interface::TextureTargets::TextureTargets() {
551 // Initialize default bindings.
552 bound_textures_[GL_TEXTURE_2D] = 0;
553 bound_textures_[GL_TEXTURE_EXTERNAL_OES] = 0;
554 bound_textures_[GL_TEXTURE_RECTANGLE_ARB] = 0;
555 }
556
557 TestGLES2Interface::TextureTargets::~TextureTargets() {}
558
559 void TestGLES2Interface::TextureTargets::BindTexture(
560 GLenum target,
561 GLuint id) {
562 // Make sure this is a supported target by seeing if it was bound to before.
563 DCHECK(bound_textures_.find(target) != bound_textures_.end());
564 bound_textures_[target] = id;
565 }
566
567 void TestGLES2Interface::TextureTargets::UnbindTexture(
568 GLuint id) {
569 // Bind zero to any targets that the id is bound to.
570 for (TargetTextureMap::iterator it = bound_textures_.begin();
571 it != bound_textures_.end();
572 it++) {
573 if (it->second == id)
574 it->second = 0;
575 }
576 }
577
578 GLuint TestGLES2Interface::TextureTargets::BoundTexture(
579 GLenum target) {
580 DCHECK(bound_textures_.find(target) != bound_textures_.end());
581 return bound_textures_[target];
582 }
583
584 TestGLES2Interface::Buffer::Buffer() : target(0), size(0) {}
585
586 TestGLES2Interface::Buffer::~Buffer() {}
587
588 TestGLES2Interface::Image::Image() {}
589
590 TestGLES2Interface::Image::~Image() {}
591
592 } // namespace cc
OLDNEW
« no previous file with comments | « cc/test/test_gles2_interface.h ('k') | cc/test/test_web_graphics_context_3d.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698