Skip to content

Commit

Permalink
Factor TextureMap and Texture class out of resource_provider_unittest…
Browse files Browse the repository at this point in the history
….cc and into TestWebGraphicsContext3D

So that in a future patch, TestWebGraphicsContext3D will be able to store texture parameters.

Review URL: https://codereview.chromium.org/23895005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@227097 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
mvujovic@adobe.com committed Oct 4, 2013
1 parent c54e197 commit 84bbc6f
Show file tree
Hide file tree
Showing 10 changed files with 330 additions and 131 deletions.
4 changes: 4 additions & 0 deletions cc/cc.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
'debug/frame_rate_counter.h',
'debug/layer_tree_debug_state.cc',
'debug/layer_tree_debug_state.h',
'debug/ordered_texture_map.cc',
'debug/ordered_texture_map.h',
'debug/overdraw_metrics.cc',
'debug/overdraw_metrics.h',
'debug/paint_time_counter.cc',
Expand All @@ -91,6 +93,8 @@
'debug/ring_buffer.h',
'debug/test_context_provider.cc',
'debug/test_context_provider.h',
'debug/test_texture.cc',
'debug/test_texture.h',
'debug/test_web_graphics_context_3d.cc',
'debug/test_web_graphics_context_3d.h',
'debug/traced_picture.cc',
Expand Down
62 changes: 62 additions & 0 deletions cc/debug/ordered_texture_map.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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/debug/ordered_texture_map.h"

#include "base/logging.h"
#include "cc/debug/test_texture.h"

namespace cc {

OrderedTextureMap::OrderedTextureMap() {}

OrderedTextureMap::~OrderedTextureMap() {}

void OrderedTextureMap::Append(WebKit::WebGLId id,
scoped_refptr<TestTexture> texture) {
DCHECK(texture.get());
DCHECK(!ContainsId(id));

textures_[id] = texture;
ordered_textures_.push_back(id);
}

void OrderedTextureMap::Replace(WebKit::WebGLId id,
scoped_refptr<TestTexture> texture) {
DCHECK(texture.get());
DCHECK(ContainsId(id));

textures_[id] = texture;
}

void OrderedTextureMap::Remove(WebKit::WebGLId id) {
TextureMap::iterator map_it = textures_.find(id);
DCHECK(map_it != textures_.end());
textures_.erase(map_it);

TextureList::iterator list_it =
std::find(ordered_textures_.begin(), ordered_textures_.end(), id);
DCHECK(list_it != ordered_textures_.end());
ordered_textures_.erase(list_it);
}

size_t OrderedTextureMap::Size() { return ordered_textures_.size(); }

bool OrderedTextureMap::ContainsId(WebKit::WebGLId id) {
return textures_.find(id) != textures_.end();
}

scoped_refptr<TestTexture> OrderedTextureMap::TextureForId(WebKit::WebGLId id) {
DCHECK(ContainsId(id));
scoped_refptr<TestTexture> texture = textures_[id];
DCHECK(texture.get());
return texture;
}

WebKit::WebGLId OrderedTextureMap::IdAt(size_t index) {
DCHECK(index < Size());
return ordered_textures_[index];
}

} // namespace cc
46 changes: 46 additions & 0 deletions cc/debug/ordered_texture_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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.

#ifndef CC_DEBUG_ORDERED_TEXTURE_MAP_H_
#define CC_DEBUG_ORDERED_TEXTURE_MAP_H_

#include <vector>

#include "base/containers/hash_tables.h"
#include "base/memory/ref_counted.h"
#include "cc/base/cc_export.h"
#include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"

namespace cc {

struct TestTexture;

class CC_EXPORT OrderedTextureMap {
public:
OrderedTextureMap();
~OrderedTextureMap();

void Append(WebKit::WebGLId id, scoped_refptr<TestTexture> texture);
void Replace(WebKit::WebGLId id, scoped_refptr<TestTexture> texture);
void Remove(WebKit::WebGLId id);

size_t Size();

bool ContainsId(WebKit::WebGLId id);

scoped_refptr<TestTexture> TextureForId(WebKit::WebGLId id);
WebKit::WebGLId IdAt(size_t index);

private:
typedef base::hash_map<WebKit::WebGLId, scoped_refptr<TestTexture> >
TextureMap;
typedef std::vector<WebKit::WebGLId> TextureList;

TextureMap textures_;
TextureList ordered_textures_;
};

} // namespace cc

#endif // CC_DEBUG_ORDERED_TEXTURE_MAP_H_
29 changes: 29 additions & 0 deletions cc/debug/test_texture.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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/debug/test_texture.h"

#include "third_party/khronos/GLES2/gl2.h"

namespace cc {

size_t TextureSizeBytes(gfx::Size size, ResourceFormat format) {
unsigned int components_per_pixel = 4;
unsigned int bytes_per_component = 1;
return size.width() * size.height() * components_per_pixel *
bytes_per_component;
}

TestTexture::TestTexture()
: format(RGBA_8888), filter(GL_NEAREST_MIPMAP_LINEAR) {}

TestTexture::~TestTexture() {}

void TestTexture::Reallocate(gfx::Size size, ResourceFormat format) {
this->size = size;
this->format = format;
this->data.reset(new uint8_t[TextureSizeBytes(size, format)]);
}

} // namespace cc
39 changes: 39 additions & 0 deletions cc/debug/test_texture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "cc/base/cc_export.h"
#include "cc/resources/resource_format.h"
#include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
#include "ui/gfx/rect.h"

#ifndef CC_DEBUG_TEST_TEXTURE_H_
#define CC_DEBUG_TEST_TEXTURE_H_

namespace cc {

size_t CC_EXPORT TextureSizeBytes(gfx::Size size, ResourceFormat format);

struct CC_EXPORT TestTexture : public base::RefCounted<TestTexture> {
TestTexture();

void Reallocate(gfx::Size size, ResourceFormat format);

gfx::Size size;
ResourceFormat format;
scoped_ptr<uint8_t[]> data;

// TODO(mvujovic): Replace this with a hash map of texture parameter names
// and values, which can hold this filter parameter value and more.
WebKit::WGC3Denum filter;

private:
friend class base::RefCounted<TestTexture>;
~TestTexture();
};

} // namespace cc

#endif // CC_DEBUG_TEST_TEXTURE_H_
64 changes: 52 additions & 12 deletions cc/debug/test_web_graphics_context_3d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,20 @@ WebGLId TestWebGraphicsContext3D::createTexture() {
WebGLId texture_id = NextTextureId();
DCHECK_NE(texture_id, kExternalTextureId);
base::AutoLock lock(namespace_->lock);
namespace_->textures.push_back(texture_id);
namespace_->textures.Append(texture_id, new TestTexture());
return texture_id;
}

WebGLId TestWebGraphicsContext3D::createExternalTexture() {
base::AutoLock lock(namespace_->lock);
namespace_->textures.Append(kExternalTextureId, new TestTexture());
return kExternalTextureId;
}

void TestWebGraphicsContext3D::deleteTexture(WebGLId texture_id) {
base::AutoLock lock(namespace_->lock);
std::vector<WebKit::WebGLId>& textures = namespace_->textures;
DCHECK(std::find(textures.begin(), textures.end(), texture_id) !=
textures.end());
textures.erase(std::find(textures.begin(), textures.end(), texture_id));
namespace_->textures.Remove(texture_id);
texture_targets_.UnbindTexture(texture_id);
}

void TestWebGraphicsContext3D::attachShader(WebGLId program, WebGLId shader) {
Expand Down Expand Up @@ -310,15 +314,17 @@ void TestWebGraphicsContext3D::bindTexture(

if (!texture_id)
return;
if (texture_id == kExternalTextureId)
return;
base::AutoLock lock(namespace_->lock);
std::vector<WebKit::WebGLId>& textures = namespace_->textures;
DCHECK(std::find(textures.begin(), textures.end(), texture_id) !=
textures.end());
DCHECK(namespace_->textures.ContainsId(texture_id));
texture_targets_.BindTexture(target, texture_id);
used_textures_.insert(texture_id);
}

WebKit::WebGLId TestWebGraphicsContext3D::BoundTextureId(
WebKit::WGC3Denum target) {
return texture_targets_.BoundTexture(target);
}

void TestWebGraphicsContext3D::endQueryEXT(WGC3Denum target) {
if (times_end_query_succeeds_ >= 0) {
if (!times_end_query_succeeds_) {
Expand Down Expand Up @@ -563,12 +569,12 @@ void TestWebGraphicsContext3D::unmapImageCHROMIUM(

size_t TestWebGraphicsContext3D::NumTextures() const {
base::AutoLock lock(namespace_->lock);
return namespace_->textures.size();
return namespace_->textures.Size();
}

WebKit::WebGLId TestWebGraphicsContext3D::TextureAt(int i) const {
base::AutoLock lock(namespace_->lock);
return namespace_->textures[i];
return namespace_->textures.IdAt(i);
}

WebGLId TestWebGraphicsContext3D::NextTextureId() {
Expand Down Expand Up @@ -613,6 +619,40 @@ void TestWebGraphicsContext3D::SetMaxTransferBufferUsageBytes(
max_transfer_buffer_usage_bytes;
}

TestWebGraphicsContext3D::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;
}

TestWebGraphicsContext3D::TextureTargets::~TextureTargets() {}

void TestWebGraphicsContext3D::TextureTargets::BindTexture(
WebKit::WGC3Denum target,
WebKit::WebGLId 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 TestWebGraphicsContext3D::TextureTargets::UnbindTexture(
WebKit::WebGLId 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;
}
}

WebKit::WebGLId TestWebGraphicsContext3D::TextureTargets::BoundTexture(
WebKit::WGC3Denum target) {
DCHECK(bound_textures_.find(target) != bound_textures_.end());
return bound_textures_[target];
}

TestWebGraphicsContext3D::Buffer::Buffer() : target(0), size(0) {}

TestWebGraphicsContext3D::Buffer::~Buffer() {}
Expand Down
21 changes: 20 additions & 1 deletion cc/debug/test_web_graphics_context_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "base/synchronization/lock.h"
#include "cc/base/cc_export.h"
#include "cc/debug/fake_web_graphics_context_3d.h"
#include "cc/debug/ordered_texture_map.h"
#include "cc/debug/test_texture.h"
#include "cc/output/context_provider.h"
#include "third_party/khronos/GLES2/gl2.h"

Expand Down Expand Up @@ -73,6 +75,7 @@ class CC_EXPORT TestWebGraphicsContext3D : public FakeWebGraphicsContext3D {
virtual WebKit::WebGLId createRenderbuffer();
virtual WebKit::WebGLId createShader(WebKit::WGC3Denum);
virtual WebKit::WebGLId createTexture();
virtual WebKit::WebGLId createExternalTexture();

virtual void deleteBuffer(WebKit::WebGLId id);
virtual void deleteFramebuffer(WebKit::WebGLId id);
Expand Down Expand Up @@ -210,6 +213,20 @@ class CC_EXPORT TestWebGraphicsContext3D : public FakeWebGraphicsContext3D {
void SetMaxTransferBufferUsageBytes(size_t max_transfer_buffer_usage_bytes);

protected:
struct TextureTargets {
TextureTargets();
~TextureTargets();

void BindTexture(WebKit::WGC3Denum target, WebKit::WebGLId id);
void UnbindTexture(WebKit::WebGLId id);

WebKit::WebGLId BoundTexture(WebKit::WGC3Denum target);

private:
typedef base::hash_map<WebKit::WGC3Denum, WebKit::WebGLId> TargetTextureMap;
TargetTextureMap bound_textures_;
};

struct Buffer {
Buffer();
~Buffer();
Expand Down Expand Up @@ -240,9 +257,9 @@ class CC_EXPORT TestWebGraphicsContext3D : public FakeWebGraphicsContext3D {
unsigned next_buffer_id;
unsigned next_image_id;
unsigned next_texture_id;
std::vector<WebKit::WebGLId> textures;
base::ScopedPtrHashMap<unsigned, Buffer> buffers;
base::ScopedPtrHashMap<unsigned, Image> images;
OrderedTextureMap textures;

private:
friend class base::RefCountedThreadSafe<Namespace>;
Expand All @@ -257,6 +274,7 @@ class CC_EXPORT TestWebGraphicsContext3D : public FakeWebGraphicsContext3D {
void CallAllSyncPointCallbacks();
void SwapBuffersComplete();
void CreateNamespace();
WebKit::WebGLId BoundTextureId(WebKit::WGC3Denum target);

unsigned context_id_;
Attributes attributes_;
Expand All @@ -278,6 +296,7 @@ class CC_EXPORT TestWebGraphicsContext3D : public FakeWebGraphicsContext3D {
int height_;

unsigned bound_buffer_;
TextureTargets texture_targets_;

scoped_refptr<Namespace> namespace_;
static Namespace* shared_namespace_;
Expand Down
Loading

0 comments on commit 84bbc6f

Please sign in to comment.