This source file includes following definitions.
- images_generated_
- BeginVisitRenderNode
- BeginVisitCCNode
- DiscoverInputIDs
- GenerateGLTexIDs
- AssignIDMapping
- WriteOutNewIDs
- AllocateImageArray
- BuildTextureImages
- HandleTexture
- GenerateImageForTexture
#include "gpu/tools/compositor_model_bench/render_model_utils.h"
#include <cstdlib>
#include <map>
#include <set>
#include <vector>
#include "base/logging.h"
TextureGenerator::TextureGenerator(RenderNode* root)
: stage_(DiscoveryStage),
images_generated_(0) {
DiscoverInputIDs(root);
GenerateGLTexIDs();
AssignIDMapping();
WriteOutNewIDs(root);
AllocateImageArray();
BuildTextureImages(root);
}
TextureGenerator::~TextureGenerator() {
if (tex_ids_.get()) {
glDeleteTextures(discovered_ids_.size(), tex_ids_.get());
}
}
void TextureGenerator::BeginVisitRenderNode(RenderNode* node) {
for (size_t n = 0; n < node->num_tiles(); ++n) {
Tile* i = node->tile(n);
HandleTexture(&i->texID,
node->tile_width(),
node->tile_height(),
GL_RGBA);
}
}
void TextureGenerator::BeginVisitCCNode(CCNode* node) {
for (size_t n = 0; n < node->num_textures(); ++n) {
Texture* i = node->texture(n);
HandleTexture(&i->texID, i->width, i->height, i->format);
}
BeginVisitRenderNode(node);
}
void TextureGenerator::DiscoverInputIDs(RenderNode* root) {
stage_ = DiscoveryStage;
root->Accept(this);
}
void TextureGenerator::GenerateGLTexIDs() {
int numTextures = discovered_ids_.size();
tex_ids_.reset(new GLuint[numTextures]);
glGenTextures(numTextures, tex_ids_.get());
}
void TextureGenerator::AssignIDMapping() {
int j = 0;
typedef std::set<int>::iterator id_itr;
for (id_itr i = discovered_ids_.begin();
i != discovered_ids_.end();
++i, ++j) {
remapped_ids_[*i] = tex_ids_[j];
}
}
void TextureGenerator::WriteOutNewIDs(RenderNode* root) {
stage_ = RemappingStage;
root->Accept(this);
}
void TextureGenerator::AllocateImageArray() {
image_data_.reset(new ImagePtr[discovered_ids_.size()]);
images_generated_ = 0;
}
void TextureGenerator::BuildTextureImages(RenderNode* root) {
stage_ = ImageGenerationStage;
root->Accept(this);
}
void TextureGenerator::HandleTexture(int* texID,
int width,
int height,
GLenum format) {
if (*texID == -1)
return;
switch (stage_) {
case DiscoveryStage:
discovered_ids_.insert(*texID);
break;
case RemappingStage:
*texID = remapped_ids_[*texID];
break;
case ImageGenerationStage:
if (ids_for_completed_textures_.count(*texID))
return;
GenerateImageForTexture(*texID, width, height, format);
ids_for_completed_textures_.insert(*texID);
break;
}
}
void TextureGenerator::GenerateImageForTexture(int texID,
int width,
int height,
GLenum format) {
int bytes_per_pixel = FormatBytesPerPixel(format);
DCHECK_LE(bytes_per_pixel, 4);
int imgID = images_generated_++;
image_data_[imgID].reset(new uint8[width*height*bytes_per_pixel]);
uint8 random_color[4];
for (int c = 0; c < 4; ++c) {
random_color[c] = std::rand() % 255;
}
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
int pix_addr = (y * width + x) * bytes_per_pixel;
for (int c = 0; c < bytes_per_pixel; ++c) {
bool on = ((x/8) + (y/8)) % 2;
uint8 v = on ? random_color[c] : ~random_color[c];
(image_data_[imgID])[pix_addr + c] = v;
}
if (bytes_per_pixel == 4) {
image_data_[imgID][pix_addr + 3] = std::rand() % 255;
}
}
}
glBindTexture(GL_TEXTURE_2D, texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D,
0,
format,
width, height,
0,
format,
GL_UNSIGNED_BYTE,
image_data_[imgID].get());
}