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

Unified 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, 1 month 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cc/test/test_gles2_interface.cc
diff --git a/cc/test/test_gles2_interface.cc b/cc/test/test_gles2_interface.cc
new file mode 100644
index 0000000000000000000000000000000000000000..585a4bd9833cf2b107d5e94cb2dfbf6beac7c7e1
--- /dev/null
+++ b/cc/test/test_gles2_interface.cc
@@ -0,0 +1,592 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "cc/test/test_gles2_interface.h"
+
+#include <algorithm>
+#include <string>
+
+#include "base/bind.h"
+#include "base/lazy_instance.h"
+#include "base/logging.h"
+#include "base/message_loop/message_loop.h"
+#include "cc/test/test_context_support.h"
+#include "gpu/GLES2/gl2extchromium.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/khronos/GLES2/gl2ext.h"
+
+namespace cc {
+
+static const GLuint kFramebufferId = 1;
+static const GLuint kRenderbufferId = 3;
+
+static uint8 s_context_id = 1;
+
+const GLuint TestGLES2Interface::kExternalTextureId = 1337;
+
+static base::LazyInstance<base::Lock>::Leaky
+ g_shared_namespace_lock = LAZY_INSTANCE_INITIALIZER;
+
+TestGLES2Interface::Namespace*
+ TestGLES2Interface::shared_namespace_ = NULL;
+
+TestGLES2Interface::Namespace::Namespace()
+ : next_buffer_id(1),
+ next_image_id(1),
+ next_texture_id(1) {
+}
+
+TestGLES2Interface::Namespace::~Namespace() {
+ g_shared_namespace_lock.Get().AssertAcquired();
+ if (shared_namespace_ == this)
+ shared_namespace_ = NULL;
+}
+
+// static
+scoped_ptr<TestGLES2Interface> TestGLES2Interface::Create() {
+ return make_scoped_ptr(new TestGLES2Interface());
+}
+
+TestGLES2Interface::TestGLES2Interface()
+ : context_id_(s_context_id++),
+ times_make_current_succeeds_(-1),
+ times_bind_texture_succeeds_(-1),
+ times_end_query_succeeds_(-1),
+ times_gen_mailbox_succeeds_(-1),
+ context_lost_(false),
+ times_map_image_chromium_succeeds_(-1),
+ times_map_buffer_chromium_succeeds_(-1),
+ next_program_id_(1000u),
+ next_shader_id_(2000u),
+ max_texture_size_(2048),
+ width_(0),
+ height_(0),
+ test_support_(NULL),
+ bound_buffer_(0),
+ weak_ptr_factory_(this) {
+ CreateNamespace();
+ test_capabilities_.swapbuffers_complete_callback = true;
+}
+
+TestGLES2Interface::~TestGLES2Interface() {
+ base::AutoLock lock(g_shared_namespace_lock.Get());
+ namespace_ = NULL;
+}
+
+void TestGLES2Interface::CreateNamespace() {
+ base::AutoLock lock(g_shared_namespace_lock.Get());
+ if (shared_namespace_) {
+ namespace_ = shared_namespace_;
+ } else {
+ namespace_ = new Namespace;
+ shared_namespace_ = namespace_.get();
+ }
+}
+
+bool TestGLES2Interface::makeContextCurrent() {
+ if (times_make_current_succeeds_ >= 0) {
+ if (!times_make_current_succeeds_) {
+ LoseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB,
+ GL_INNOCENT_CONTEXT_RESET_ARB);
+ }
+ --times_make_current_succeeds_;
+ }
+ return !context_lost_;
+}
+
+void TestGLES2Interface::reshapeWithScaleFactor(
+ int width, int height, float scale_factor) {
+ width_ = width;
+ height_ = height;
+}
+
+bool TestGLES2Interface::isContextLost() {
+ return context_lost_;
+}
+
+GLuint TestGLES2Interface::CreateExternalTexture() {
+ base::AutoLock lock(namespace_->lock);
+ namespace_->textures.Append(kExternalTextureId, new TestTexture());
+ return kExternalTextureId;
+}
+
+GLenum TestGLES2Interface::CheckFramebufferStatus(
+ GLenum target) {
+ if (context_lost_)
+ return GL_FRAMEBUFFER_UNDEFINED_OES;
+ return GL_FRAMEBUFFER_COMPLETE;
+}
+
+const GLubyte* TestGLES2Interface::GetString(GLenum name) {
+ return NULL;
+}
+
+GLint TestGLES2Interface::GetUniformLocation(
+ GLuint program,
+ const GLchar* name) {
+ return 0;
+}
+
+void TestGLES2Interface::GetVertexAttribPointerv(
+ GLuint index,
+ GLenum pname,
+ void** pointer) {
+ *pointer = NULL;
+}
+
+void TestGLES2Interface::GenBuffers(GLsizei count, GLuint* ids) {
+ for (int i = 0; i < count; ++i)
+ ids[i] = NextBufferId();
+}
+
+void TestGLES2Interface::GenFramebuffers(
+ GLsizei count, GLuint* ids) {
+ for (int i = 0; i < count; ++i)
+ ids[i] = kFramebufferId | context_id_ << 16;
+}
+
+void TestGLES2Interface::GenRenderbuffers(
+ GLsizei count, GLuint* ids) {
+ for (int i = 0; i < count; ++i)
+ ids[i] = kRenderbufferId | context_id_ << 16;
+}
+
+void TestGLES2Interface::GenTextures(GLsizei count, GLuint* ids) {
+ for (int i = 0; i < count; ++i) {
+ ids[i] = NextTextureId();
+ DCHECK_NE(ids[i], kExternalTextureId);
+ }
+ base::AutoLock lock(namespace_->lock);
+ for (int i = 0; i < count; ++i)
+ namespace_->textures.Append(ids[i], new TestTexture());
+}
+
+void TestGLES2Interface::DeleteBuffers(GLsizei count, const GLuint* ids) {
+ for (int i = 0; i < count; ++i)
+ RetireBufferId(ids[i]);
+}
+
+void TestGLES2Interface::DeleteFramebuffers(
+ GLsizei count, const GLuint* ids) {
+ for (int i = 0; i < count; ++i)
+ DCHECK_EQ(kFramebufferId | context_id_ << 16, ids[i]);
+}
+
+void TestGLES2Interface::DeleteRenderbuffers(
+ GLsizei count, const GLuint* ids) {
+ for (int i = 0; i < count; ++i)
+ DCHECK_EQ(kRenderbufferId | context_id_ << 16, ids[i]);
+}
+
+void TestGLES2Interface::DeleteTextures(GLsizei count, const GLuint* ids) {
+ for (int i = 0; i < count; ++i)
+ RetireTextureId(ids[i]);
+ base::AutoLock lock(namespace_->lock);
+ for (int i = 0; i < count; ++i) {
+ namespace_->textures.Remove(ids[i]);
+ texture_targets_.UnbindTexture(ids[i]);
+ }
+}
+
+GLuint TestGLES2Interface::CreateProgram() {
+ GLuint program = next_program_id_++ | context_id_ << 16;
+ program_set_.insert(program);
+ return program;
+}
+
+GLuint TestGLES2Interface::CreateShader(GLenum) {
+ GLuint shader = next_shader_id_++ | context_id_ << 16;
+ shader_set_.insert(shader);
+ return shader;
+}
+
+void TestGLES2Interface::DeleteProgram(GLuint id) {
+ if (!program_set_.count(id))
+ ADD_FAILURE() << "DeleteProgram called on unknown program " << id;
+ program_set_.erase(id);
+}
+
+void TestGLES2Interface::DeleteShader(GLuint id) {
+ if (!shader_set_.count(id))
+ ADD_FAILURE() << "DeleteShader called on unknown shader " << id;
+ shader_set_.erase(id);
+}
+
+void TestGLES2Interface::AttachShader(GLuint program, GLuint shader) {
+ if (!program_set_.count(program))
+ ADD_FAILURE() << "AttachShader called with unknown program " << program;
+ if (!shader_set_.count(shader))
+ ADD_FAILURE() << "AttachShader called with unknown shader " << shader;
+}
+
+void TestGLES2Interface::UseProgram(GLuint program) {
+ if (!program)
+ return;
+ if (!program_set_.count(program))
+ ADD_FAILURE() << "UseProgram called on unknown program " << program;
+}
+
+void TestGLES2Interface::BindFramebuffer(
+ GLenum target, GLuint framebuffer) {
+ if (!framebuffer)
+ return;
+ DCHECK_EQ(kFramebufferId | context_id_ << 16, framebuffer);
+}
+
+void TestGLES2Interface::BindRenderbuffer(
+ GLenum target, GLuint renderbuffer) {
+ if (!renderbuffer)
+ return;
+ DCHECK_EQ(kRenderbufferId | context_id_ << 16, renderbuffer);
+}
+
+void TestGLES2Interface::BindTexture(
+ GLenum target, GLuint texture_id) {
+ if (times_bind_texture_succeeds_ >= 0) {
+ if (!times_bind_texture_succeeds_) {
+ LoseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB,
+ GL_INNOCENT_CONTEXT_RESET_ARB);
+ }
+ --times_bind_texture_succeeds_;
+ }
+
+ if (!texture_id)
+ return;
+ base::AutoLock lock(namespace_->lock);
+ DCHECK(namespace_->textures.ContainsId(texture_id));
+ texture_targets_.BindTexture(target, texture_id);
+ used_textures_.insert(texture_id);
+}
+
+GLuint TestGLES2Interface::BoundTextureId(
+ GLenum target) {
+ return texture_targets_.BoundTexture(target);
+}
+
+void TestGLES2Interface::EndQueryEXT(GLenum target) {
+ if (times_end_query_succeeds_ >= 0) {
+ if (!times_end_query_succeeds_) {
+ LoseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB,
+ GL_INNOCENT_CONTEXT_RESET_ARB);
+ }
+ --times_end_query_succeeds_;
+ }
+}
+
+void TestGLES2Interface::GetQueryObjectuivEXT(
+ GLuint query,
+ GLenum pname,
+ GLuint* params) {
+ // If the context is lost, behave as if result is available.
+ if (pname == GL_QUERY_RESULT_AVAILABLE_EXT)
+ *params = 1;
+}
+
+void TestGLES2Interface::GetIntegerv(
+ GLenum pname,
+ GLint* value) {
+ if (pname == GL_MAX_TEXTURE_SIZE)
+ *value = max_texture_size_;
+ else if (pname == GL_ACTIVE_TEXTURE)
+ *value = GL_TEXTURE0;
+}
+
+void TestGLES2Interface::GenMailboxCHROMIUM(GLbyte* mailbox) {
+ if (times_gen_mailbox_succeeds_ >= 0) {
+ if (!times_gen_mailbox_succeeds_) {
+ LoseContextCHROMIUM(GL_GUILTY_CONTEXT_RESET_ARB,
+ GL_INNOCENT_CONTEXT_RESET_ARB);
+ }
+ --times_gen_mailbox_succeeds_;
+ }
+ if (context_lost_) {
+ memset(mailbox, 0, 64);
+ return;
+ }
+
+ static char mailbox_name1 = '1';
+ static char mailbox_name2 = '1';
+ mailbox[0] = mailbox_name1;
+ mailbox[1] = mailbox_name2;
+ mailbox[2] = '\0';
+ if (++mailbox_name1 == 0) {
+ mailbox_name1 = '1';
+ ++mailbox_name2;
+ }
+}
+
+void TestGLES2Interface::LoseContextCHROMIUM(GLenum current,
+ GLenum other) {
+ if (context_lost_)
+ return;
+ context_lost_ = true;
+ /*
+ if (context_lost_callback_)
+ context_lost_callback_->onContextLost();
+ */
+ for (size_t i = 0; i < shared_contexts_.size(); ++i)
+ shared_contexts_[i]->LoseContextCHROMIUM(current, other);
+ shared_contexts_.clear();
+}
+
+/*
+void TestGLES2Interface::prepareTexture() {
+ // TODO(jamesr): This should implemented as ContextSupport::SwapBuffers().
+ if (swap_buffers_callback_) {
+ base::MessageLoop::current()->PostTask(
+ FROM_HERE, base::Bind(&TestGLES2Interface::SwapBuffersComplete,
+ weak_ptr_factory_.GetWeakPtr()));
+ }
+ test_support_->CallAllSyncPointCallbacks();
+}
+*/
+
+void TestGLES2Interface::Finish() {
+ test_support_->CallAllSyncPointCallbacks();
+}
+
+void TestGLES2Interface::Flush() {
+ test_support_->CallAllSyncPointCallbacks();
+}
+
+/*
+void TestGLES2Interface::SwapBuffersComplete() {
+ if (swap_buffers_callback_)
+ swap_buffers_callback_->onSwapBuffersComplete();
+}
+*/
+
+void TestGLES2Interface::BindBuffer(GLenum target,
+ GLuint buffer) {
+ bound_buffer_ = buffer;
+ if (!bound_buffer_)
+ return;
+ uint8 context_id = buffer >> 16;
+ uint8 buffer_id = buffer & 0xffff;
+ base::AutoLock lock(namespace_->lock);
+ DCHECK(buffer_id);
+ DCHECK_LT(buffer_id, namespace_->next_buffer_id);
+ DCHECK_EQ(context_id, context_id_);
+
+ base::ScopedPtrHashMap<uint8, Buffer>& buffers = namespace_->buffers;
+ if (buffers.count(bound_buffer_) == 0)
+ buffers.set(bound_buffer_, make_scoped_ptr(new Buffer).Pass());
+
+ buffers.get(bound_buffer_)->target = target;
+}
+
+void TestGLES2Interface::BufferData(GLenum target,
+ GLsizeiptr size,
+ const void* data,
+ GLenum usage) {
+ base::AutoLock lock(namespace_->lock);
+ base::ScopedPtrHashMap<uint8, Buffer>& buffers = namespace_->buffers;
+ DCHECK_GT(buffers.count(bound_buffer_), 0u);
+ DCHECK_EQ(target, buffers.get(bound_buffer_)->target);
+ Buffer* buffer = buffers.get(bound_buffer_);
+ if (context_lost_) {
+ buffer->pixels.reset();
+ return;
+ }
+
+ buffer->pixels.reset(new uint8[size]);
+ buffer->size = size;
+ if (data != NULL)
+ memcpy(buffer->pixels.get(), data, size);
+}
+
+void* TestGLES2Interface::MapBufferCHROMIUM(GLenum target,
+ GLenum access) {
+ base::AutoLock lock(namespace_->lock);
+ base::ScopedPtrHashMap<uint8, Buffer>& buffers = namespace_->buffers;
+ DCHECK_GT(buffers.count(bound_buffer_), 0u);
+ DCHECK_EQ(target, buffers.get(bound_buffer_)->target);
+ if (times_map_buffer_chromium_succeeds_ >= 0) {
+ if (!times_map_buffer_chromium_succeeds_) {
+ return NULL;
+ }
+ --times_map_buffer_chromium_succeeds_;
+ }
+ return buffers.get(bound_buffer_)->pixels.get();
+}
+
+GLboolean TestGLES2Interface::UnmapBufferCHROMIUM(
+ GLenum target) {
+ base::AutoLock lock(namespace_->lock);
+ base::ScopedPtrHashMap<uint8, Buffer>& buffers = namespace_->buffers;
+ DCHECK_GT(buffers.count(bound_buffer_), 0u);
+ DCHECK_EQ(target, buffers.get(bound_buffer_)->target);
+ buffers.get(bound_buffer_)->pixels.reset();
+ return true;
+}
+
+GLuint TestGLES2Interface::CreateImageCHROMIUM(
+ GLsizei width, GLsizei height,
+ GLenum internalformat) {
+ DCHECK_EQ(GL_RGBA8_OES, static_cast<int>(internalformat));
+ GLuint image_id = NextImageId();
+ base::AutoLock lock(namespace_->lock);
+ base::ScopedPtrHashMap<uint8, Image>& images = namespace_->images;
+ images.set(image_id, make_scoped_ptr(new Image).Pass());
+ images.get(image_id)->pixels.reset(new uint8[width * height * 4]);
+ return image_id;
+}
+
+void TestGLES2Interface::DestroyImageCHROMIUM(
+ GLuint id) {
+ RetireImageId(id);
+}
+
+void TestGLES2Interface::GetImageParameterivCHROMIUM(
+ GLuint image_id,
+ GLenum pname,
+ GLint* params) {
+ base::AutoLock lock(namespace_->lock);
+ DCHECK_GT(namespace_->images.count(image_id), 0u);
+ DCHECK_EQ(GL_IMAGE_ROWBYTES_CHROMIUM, static_cast<int>(pname));
+ *params = 0;
+}
+
+void* TestGLES2Interface::MapImageCHROMIUM(GLuint image_id,
+ GLenum access) {
+ base::AutoLock lock(namespace_->lock);
+ base::ScopedPtrHashMap<uint8, Image>& images = namespace_->images;
+ DCHECK_GT(images.count(image_id), 0u);
+ if (times_map_image_chromium_succeeds_ >= 0) {
+ if (!times_map_image_chromium_succeeds_) {
+ return NULL;
+ }
+ --times_map_image_chromium_succeeds_;
+ }
+ return images.get(image_id)->pixels.get();
+}
+
+void TestGLES2Interface::UnmapImageCHROMIUM(
+ GLuint image_id) {
+ base::AutoLock lock(namespace_->lock);
+ DCHECK_GT(namespace_->images.count(image_id), 0u);
+}
+
+size_t TestGLES2Interface::NumTextures() const {
+ base::AutoLock lock(namespace_->lock);
+ return namespace_->textures.Size();
+}
+
+GLuint TestGLES2Interface::TextureAt(int i) const {
+ base::AutoLock lock(namespace_->lock);
+ return namespace_->textures.IdAt(i);
+}
+
+GLuint TestGLES2Interface::NextTextureId() {
+ base::AutoLock lock(namespace_->lock);
+ GLuint texture_id = namespace_->next_texture_id++;
+ DCHECK(texture_id < (1 << 16));
+ texture_id |= context_id_ << 16;
+ return texture_id;
+}
+
+void TestGLES2Interface::RetireTextureId(GLuint id) {
+ base::AutoLock lock(namespace_->lock);
+ uint8 context_id = id >> 16;
+ uint8 texture_id = id & 0xffff;
+ DCHECK(texture_id);
+ DCHECK_LT(texture_id, namespace_->next_texture_id);
+ DCHECK_EQ(context_id, context_id_);
+}
+
+GLuint TestGLES2Interface::NextBufferId() {
+ base::AutoLock lock(namespace_->lock);
+ GLuint buffer_id = namespace_->next_buffer_id++;
+ DCHECK(buffer_id < (1 << 16));
+ buffer_id |= context_id_ << 16;
+ return buffer_id;
+}
+
+void TestGLES2Interface::RetireBufferId(GLuint id) {
+ base::AutoLock lock(namespace_->lock);
+ uint8 context_id = id >> 16;
+ uint8 buffer_id = id & 0xffff;
+ DCHECK(buffer_id);
+ DCHECK_LT(buffer_id, namespace_->next_buffer_id);
+ DCHECK_EQ(context_id, context_id_);
+}
+
+GLuint TestGLES2Interface::NextImageId() {
+ base::AutoLock lock(namespace_->lock);
+ GLuint image_id = namespace_->next_image_id++;
+ DCHECK(image_id < (1 << 16));
+ image_id |= context_id_ << 16;
+ return image_id;
+}
+
+void TestGLES2Interface::RetireImageId(GLuint id) {
+ base::AutoLock lock(namespace_->lock);
+ uint8 context_id = id >> 16;
+ uint8 image_id = id & 0xffff;
+ DCHECK(image_id);
+ DCHECK_LT(image_id, namespace_->next_image_id);
+ DCHECK_EQ(context_id, context_id_);
+}
+
+size_t TestGLES2Interface::GetTransferBufferMemoryUsedBytes() const {
+ size_t total_bytes = 0;
+ base::ScopedPtrHashMap<uint8, Buffer>& buffers = namespace_->buffers;
+ base::ScopedPtrHashMap<uint8, Buffer>::iterator it = buffers.begin();
+ for (; it != buffers.end(); ++it) {
+ Buffer* buffer = it->second;
+ if (buffer->target == GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM)
+ total_bytes += buffer->size;
+ }
+ return total_bytes;
+}
+
+void TestGLES2Interface::SetMaxTransferBufferUsageBytes(
+ size_t max_transfer_buffer_usage_bytes) {
+ test_capabilities_.max_transfer_buffer_usage_bytes =
+ max_transfer_buffer_usage_bytes;
+}
+
+TestGLES2Interface::TextureTargets::TextureTargets() {
+ // Initialize default bindings.
+ bound_textures_[GL_TEXTURE_2D] = 0;
+ bound_textures_[GL_TEXTURE_EXTERNAL_OES] = 0;
+ bound_textures_[GL_TEXTURE_RECTANGLE_ARB] = 0;
+}
+
+TestGLES2Interface::TextureTargets::~TextureTargets() {}
+
+void TestGLES2Interface::TextureTargets::BindTexture(
+ GLenum target,
+ GLuint id) {
+ // Make sure this is a supported target by seeing if it was bound to before.
+ DCHECK(bound_textures_.find(target) != bound_textures_.end());
+ bound_textures_[target] = id;
+}
+
+void TestGLES2Interface::TextureTargets::UnbindTexture(
+ GLuint id) {
+ // Bind zero to any targets that the id is bound to.
+ for (TargetTextureMap::iterator it = bound_textures_.begin();
+ it != bound_textures_.end();
+ it++) {
+ if (it->second == id)
+ it->second = 0;
+ }
+}
+
+GLuint TestGLES2Interface::TextureTargets::BoundTexture(
+ GLenum target) {
+ DCHECK(bound_textures_.find(target) != bound_textures_.end());
+ return bound_textures_[target];
+}
+
+TestGLES2Interface::Buffer::Buffer() : target(0), size(0) {}
+
+TestGLES2Interface::Buffer::~Buffer() {}
+
+TestGLES2Interface::Image::Image() {}
+
+TestGLES2Interface::Image::~Image() {}
+
+} // namespace cc
« 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