Skip to content

Commit

Permalink
CC: Organize and optimize format lookups.
Browse files Browse the repository at this point in the history
There was a minor regression in tile management by
using one of these functions. This puts them all in one
place and uses array look-ups to fix the perf regression.

BUG=297578

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@228578 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
epenner@chromium.org committed Oct 15, 2013
1 parent 55dd0b6 commit c5f1a84
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 85 deletions.
4 changes: 2 additions & 2 deletions cc/output/gl_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1677,7 +1677,7 @@ void GLRenderer::DrawPictureQuad(const DrawingFrame* frame,

uint8_t* bitmap_pixels = NULL;
SkBitmap on_demand_tile_raster_bitmap_dest;
SkBitmap::Config config = SkBitmapConfigFromFormat(quad->texture_format);
SkBitmap::Config config = SkBitmapConfig(quad->texture_format);
if (on_demand_tile_raster_bitmap_.getConfig() != config) {
on_demand_tile_raster_bitmap_.copyTo(&on_demand_tile_raster_bitmap_dest,
config);
Expand Down Expand Up @@ -2430,7 +2430,7 @@ void GLRenderer::GetFramebufferTexture(
context_->copyTexImage2D(
GL_TEXTURE_2D,
0,
ResourceProvider::GetGLDataFormat(texture_format),
GLDataFormat(texture_format),
window_rect.x(),
window_rect.y(),
window_rect.width(),
Expand Down
3 changes: 1 addition & 2 deletions cc/resources/raster_worker_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ class RasterWorkerPoolTaskImpl : public internal::RasterWorkerPoolTask {
void ChangeBitmapConfigIfNeeded(const SkBitmap& bitmap,
void* buffer) {
TRACE_EVENT0("cc", "RasterWorkerPoolTaskImpl::ChangeBitmapConfigIfNeeded");
SkBitmap::Config config = SkBitmapConfigFromFormat(
resource()->format());
SkBitmap::Config config = SkBitmapConfig(resource()->format());
if (bitmap.getConfig() != config) {
SkBitmap bitmap_dest;
IdentityAllocator allocator(buffer);
Expand Down
3 changes: 0 additions & 3 deletions cc/resources/resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@ size_t Resource::bytes() const {
return MemorySizeBytes(size_, format_);
}

size_t Resource::MemorySizeBytes(gfx::Size size, ResourceFormat format) {
return ResourceProvider::BytesPerPixel(format) * size.width() * size.height();
}

} // namespace cc
4 changes: 3 additions & 1 deletion cc/resources/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class CC_EXPORT Resource {
ResourceFormat format() const { return format_; }
size_t bytes() const;

static size_t MemorySizeBytes(gfx::Size size, ResourceFormat format);
inline static size_t MemorySizeBytes(gfx::Size size, ResourceFormat format) {
return BytesPerPixel(format) * size.width() * size.height();
}

protected:
void set_id(ResourceProvider::ResourceId id) { id_ = id; }
Expand Down
2 changes: 1 addition & 1 deletion cc/resources/resource_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace cc {

SkBitmap::Config SkBitmapConfigFromFormat(ResourceFormat format) {
SkBitmap::Config SkBitmapConfig(ResourceFormat format) {
switch (format) {
case RGBA_4444:
return SkBitmap::kARGB_4444_Config;
Expand Down
3 changes: 2 additions & 1 deletion cc/resources/resource_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace cc {

// Keep in sync with arrays below.
enum ResourceFormat {
RGBA_8888,
RGBA_4444,
Expand All @@ -19,7 +20,7 @@ enum ResourceFormat {
RESOURCE_FORMAT_MAX = RGB_565,
};

SkBitmap::Config SkBitmapConfigFromFormat(ResourceFormat format);
SkBitmap::Config SkBitmapConfig(ResourceFormat format);

} // namespace cc

Expand Down
66 changes: 8 additions & 58 deletions cc/resources/resource_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1393,12 +1393,12 @@ void ResourceProvider::BeginSetPixels(ResourceId id) {
context3d->asyncTexImage2DCHROMIUM(
GL_TEXTURE_2D,
0, /* level */
GetGLInternalFormat(resource->format),
GLInternalFormat(resource->format),
resource->size.width(),
resource->size.height(),
0, /* border */
GetGLDataFormat(resource->format),
GetGLDataType(resource->format),
GLDataFormat(resource->format),
GLDataType(resource->format),
NULL);
} else {
context3d->asyncTexSubImage2DCHROMIUM(
Expand All @@ -1408,8 +1408,8 @@ void ResourceProvider::BeginSetPixels(ResourceId id) {
0, /* y */
resource->size.width(),
resource->size.height(),
GetGLDataFormat(resource->format),
GetGLDataType(resource->format),
GLDataFormat(resource->format),
GLDataType(resource->format),
NULL);
}
context3d->endQueryEXT(GL_ASYNC_PIXEL_UNPACK_COMPLETED_CHROMIUM);
Expand Down Expand Up @@ -1548,12 +1548,12 @@ void ResourceProvider::LazyAllocate(Resource* resource) {
} else {
GLC(context3d, context3d->texImage2D(GL_TEXTURE_2D,
0,
GetGLInternalFormat(format),
GLInternalFormat(format),
size.width(),
size.height(),
0,
GetGLDataFormat(format),
GetGLDataType(format),
GLDataFormat(format),
GLDataType(format),
NULL));
}
}
Expand Down Expand Up @@ -1658,54 +1658,4 @@ WebKit::WebGraphicsContext3D* ResourceProvider::Context3d() const {
return context_provider ? context_provider->Context3d() : NULL;
}

size_t ResourceProvider::BytesPerPixel(ResourceFormat format) {
switch (format) {
case RGBA_8888:
case BGRA_8888:
return 4;
case RGBA_4444:
case RGB_565:
return 2;
case LUMINANCE_8:
return 1;
}
NOTREACHED();
return 4;
}

GLenum ResourceProvider::GetGLDataType(ResourceFormat format) {
switch (format) {
case RGBA_4444:
return GL_UNSIGNED_SHORT_4_4_4_4;
case RGBA_8888:
case BGRA_8888:
case LUMINANCE_8:
return GL_UNSIGNED_BYTE;
case RGB_565:
return GL_UNSIGNED_SHORT_5_6_5;
}
NOTREACHED();
return GL_UNSIGNED_BYTE;
}

GLenum ResourceProvider::GetGLDataFormat(ResourceFormat format) {
switch (format) {
case RGBA_8888:
case RGBA_4444:
return GL_RGBA;
case BGRA_8888:
return GL_BGRA_EXT;
case LUMINANCE_8:
return GL_LUMINANCE;
case RGB_565:
return GL_RGB;
}
NOTREACHED();
return GL_RGBA;
}

GLenum ResourceProvider::GetGLInternalFormat(ResourceFormat format) {
return GetGLDataFormat(format);
}

} // namespace cc
48 changes: 44 additions & 4 deletions cc/resources/resource_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "cc/resources/texture_mailbox.h"
#include "cc/resources/transferable_resource.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "third_party/khronos/GLES2/gl2ext.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "ui/gfx/size.h"
Expand Down Expand Up @@ -344,10 +345,6 @@ class CC_EXPORT ResourceProvider {
bool CanLockForWrite(ResourceId id);

static GLint GetActiveTextureUnit(WebKit::WebGraphicsContext3D* context);
static size_t BytesPerPixel(ResourceFormat format);
static GLenum GetGLDataType(ResourceFormat format);
static GLenum GetGLDataFormat(ResourceFormat format);
static GLenum GetGLInternalFormat(ResourceFormat format);

private:
struct Resource {
Expand Down Expand Up @@ -488,6 +485,49 @@ class CC_EXPORT ResourceProvider {
DISALLOW_COPY_AND_ASSIGN(ResourceProvider);
};


// TODO(epenner): Move these format conversions to resource_format.h
// once that builds on mac (npapi.h currently #includes OpenGL.h).
inline unsigned BytesPerPixel(ResourceFormat format) {
DCHECK_LE(format, RESOURCE_FORMAT_MAX);
static const unsigned format_bytes_per_pixel[RESOURCE_FORMAT_MAX + 1] = {
4, // RGBA_8888
2, // RGBA_4444
4, // BGRA_8888
1, // LUMINANCE_8
2 // RGB_565
};
return format_bytes_per_pixel[format];
}

inline GLenum GLDataType(ResourceFormat format) {
DCHECK_LE(format, RESOURCE_FORMAT_MAX);
static const unsigned format_gl_data_type[RESOURCE_FORMAT_MAX + 1] = {
GL_UNSIGNED_BYTE, // RGBA_8888
GL_UNSIGNED_SHORT_4_4_4_4, // RGBA_4444
GL_UNSIGNED_BYTE, // BGRA_8888
GL_UNSIGNED_BYTE, // LUMINANCE_8
GL_UNSIGNED_SHORT_5_6_5 // RGB_565
};
return format_gl_data_type[format];
}

inline GLenum GLDataFormat(ResourceFormat format) {
DCHECK_LE(format, RESOURCE_FORMAT_MAX);
static const unsigned format_gl_data_format[RESOURCE_FORMAT_MAX + 1] = {
GL_RGBA, // RGBA_8888
GL_RGBA, // RGBA_4444
GL_BGRA_EXT, // BGRA_8888
GL_LUMINANCE, // LUMINANCE_8
GL_RGB // RGB_565
};
return format_gl_data_format[format];
}

inline GLenum GLInternalFormat(ResourceFormat format) {
return GLDataFormat(format);
}

} // namespace cc

#endif // CC_RESOURCES_RESOURCE_PROVIDER_H_
3 changes: 1 addition & 2 deletions cc/resources/resource_provider_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ class ResourceProviderContext : public TestWebGraphicsContext3D {
CheckTextureIsBound(target);
ASSERT_EQ(static_cast<unsigned>(GL_TEXTURE_2D), target);
ASSERT_FALSE(level);
ASSERT_EQ(ResourceProvider::GetGLDataFormat(BoundTexture(target)->format),
format);
ASSERT_EQ(GLDataFormat(BoundTexture(target)->format), format);
ASSERT_EQ(static_cast<unsigned>(GL_UNSIGNED_BYTE), type);
ASSERT_TRUE(pixels);
SetPixels(xoffset, yoffset, width, height, pixels);
Expand Down
14 changes: 6 additions & 8 deletions cc/scheduler/texture_uploader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void TextureUploader::UploadWithTexSubImage(const uint8* image,
gfx::Vector2d offset(source_rect.origin() - image_rect.origin());

const uint8* pixel_source;
unsigned bytes_per_pixel = ResourceProvider::BytesPerPixel(format);
unsigned bytes_per_pixel = BytesPerPixel(format);
// Use 4-byte row alignment (OpenGL default) for upload performance.
// Assuming that GL_UNPACK_ALIGNMENT has not changed from default.
unsigned upload_image_stride =
Expand Down Expand Up @@ -222,8 +222,8 @@ void TextureUploader::UploadWithTexSubImage(const uint8* image,
dest_offset.y(),
source_rect.width(),
source_rect.height(),
ResourceProvider::GetGLDataFormat(format),
ResourceProvider::GetGLDataType(format),
GLDataFormat(format),
GLDataType(format),
pixel_source);
}

Expand All @@ -243,7 +243,7 @@ void TextureUploader::UploadWithMapTexSubImage(const uint8* image,
// Offset from image-rect to source-rect.
gfx::Vector2d offset(source_rect.origin() - image_rect.origin());

unsigned bytes_per_pixel = ResourceProvider::BytesPerPixel(format);
unsigned bytes_per_pixel = BytesPerPixel(format);
// Use 4-byte row alignment (OpenGL default) for upload performance.
// Assuming that GL_UNPACK_ALIGNMENT has not changed from default.
unsigned upload_image_stride =
Expand All @@ -257,10 +257,8 @@ void TextureUploader::UploadWithMapTexSubImage(const uint8* image,
dest_offset.y(),
source_rect.width(),
source_rect.height(),
ResourceProvider::GetGLDataFormat(
format),
ResourceProvider::GetGLDataType(
format),
GLDataFormat(format),
GLDataType(format),
GL_WRITE_ONLY));

if (!pixel_dest) {
Expand Down
6 changes: 3 additions & 3 deletions content/renderer/gpu/mailbox_output_surface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ void MailboxOutputSurface::EnsureBackbuffer() {
context3d->texImage2D(
GL_TEXTURE_2D,
0,
ResourceProvider::GetGLInternalFormat(format_),
GLInternalFormat(format_),
surface_size_.width(),
surface_size_.height(),
0,
ResourceProvider::GetGLDataFormat(format_),
ResourceProvider::GetGLDataType(format_),
GLDataFormat(format_),
GLDataType(format_),
NULL);
context3d->genMailboxCHROMIUM(current_backing_.mailbox.name);
context3d->produceTextureCHROMIUM(
Expand Down

0 comments on commit c5f1a84

Please sign in to comment.