Index: cc/resources/resource_provider.cc |
diff --git a/cc/resources/resource_provider.cc b/cc/resources/resource_provider.cc |
index 787757c3b86f62847ec3ae99a27747bd0553b414..04206a7b3127d68400f8b4fa08f009f8d8fdb4eb 100644 |
--- a/cc/resources/resource_provider.cc |
+++ b/cc/resources/resource_provider.cc |
@@ -34,12 +34,12 @@ namespace { |
const double kSoftwareUploadTickRate = 0.000250; |
const double kTextureUploadTickRate = 0.004; |
-GLenum TextureToStorageFormat(GLenum texture_format) { |
+GLenum TextureToStorageFormat(cc::ResourceProvider::TextureFormat format) { |
GLenum storage_format = GL_RGBA8_OES; |
- switch (texture_format) { |
- case GL_RGBA: |
+ switch (format) { |
+ case cc::ResourceProvider::RGBA_8888: |
break; |
- case GL_BGRA_EXT: |
+ case cc::ResourceProvider::BGRA_8888: |
storage_format = GL_BGRA8_EXT; |
break; |
default: |
reveman
2013/09/12 15:57:40
Remove "default:" after removing INVALID_FORMAT en
kaanb
2013/09/13 00:11:08
Done.
|
@@ -50,8 +50,10 @@ GLenum TextureToStorageFormat(GLenum texture_format) { |
return storage_format; |
} |
-bool IsTextureFormatSupportedForStorage(GLenum format) { |
- return (format == GL_RGBA || format == GL_BGRA_EXT); |
+bool IsTextureFormatSupportedForStorage( |
+ cc::ResourceProvider::TextureFormat format) { |
+ return (format == cc::ResourceProvider::RGBA_8888 || |
+ format == cc::ResourceProvider::BGRA_8888); |
reveman
2013/09/12 15:57:40
please change this to a switch statement now that
kaanb
2013/09/13 00:11:08
Done.
|
} |
class ScopedSetActiveTexture { |
@@ -95,24 +97,24 @@ ResourceProvider::Resource::Resource() |
enable_read_lock_fences(false), |
read_lock_fence(NULL), |
size(), |
- format(0), |
filter(0), |
image_id(0), |
texture_pool(0), |
wrap_mode(0), |
hint(TextureUsageAny), |
- type(static_cast<ResourceType>(0)) {} |
+ type(static_cast<ResourceType>(0)), |
+ texture_format(INVALID_FORMAT) {} |
ResourceProvider::Resource::~Resource() {} |
ResourceProvider::Resource::Resource( |
unsigned texture_id, |
gfx::Size size, |
- GLenum format, |
GLenum filter, |
GLenum texture_pool, |
GLint wrap_mode, |
- TextureUsageHint hint) |
+ TextureUsageHint hint, |
+ TextureFormat texture_format) |
: gl_id(texture_id), |
gl_pixel_buffer_id(0), |
gl_upload_query_id(0), |
@@ -130,20 +132,19 @@ ResourceProvider::Resource::Resource( |
enable_read_lock_fences(false), |
read_lock_fence(NULL), |
size(size), |
- format(format), |
filter(filter), |
image_id(0), |
texture_pool(texture_pool), |
wrap_mode(wrap_mode), |
hint(hint), |
- type(GLTexture) { |
+ type(GLTexture), |
+ texture_format(texture_format) { |
DCHECK(wrap_mode == GL_CLAMP_TO_EDGE || wrap_mode == GL_REPEAT); |
} |
ResourceProvider::Resource::Resource( |
uint8_t* pixels, |
gfx::Size size, |
- GLenum format, |
GLenum filter, |
GLint wrap_mode) |
: gl_id(0), |
@@ -163,13 +164,13 @@ ResourceProvider::Resource::Resource( |
enable_read_lock_fences(false), |
read_lock_fence(NULL), |
size(size), |
- format(format), |
filter(filter), |
image_id(0), |
texture_pool(0), |
wrap_mode(wrap_mode), |
hint(TextureUsageAny), |
- type(Bitmap) { |
+ type(Bitmap), |
+ texture_format(RGBA_8888) { |
DCHECK(wrap_mode == GL_CLAMP_TO_EDGE || wrap_mode == GL_REPEAT); |
} |
@@ -181,7 +182,8 @@ scoped_ptr<ResourceProvider> ResourceProvider::Create( |
OutputSurface* output_surface, |
int highp_threshold_min) { |
scoped_ptr<ResourceProvider> resource_provider( |
- new ResourceProvider(output_surface, highp_threshold_min)); |
+ new ResourceProvider(output_surface, |
+ highp_threshold_min)); |
bool success = false; |
if (resource_provider->Context3d()) { |
@@ -211,17 +213,20 @@ bool ResourceProvider::InUseByConsumer(ResourceId id) { |
} |
ResourceProvider::ResourceId ResourceProvider::CreateResource( |
- gfx::Size size, GLenum format, GLint wrap_mode, TextureUsageHint hint) { |
+ gfx::Size size, |
+ GLint wrap_mode, |
+ TextureUsageHint hint, |
+ TextureFormat texture_format) { |
DCHECK(!size.IsEmpty()); |
switch (default_resource_type_) { |
case GLTexture: |
- return CreateGLTexture(size, format, GL_TEXTURE_POOL_UNMANAGED_CHROMIUM, |
- wrap_mode, hint); |
+ return CreateGLTexture(size, |
+ GL_TEXTURE_POOL_UNMANAGED_CHROMIUM, |
+ wrap_mode, |
+ hint, |
+ texture_format); |
case Bitmap: |
- // The only wrap_mode currently implemented in software mode is |
- // GL_CLAMP_TO_EDGE. |
- // http://crbug.com/284796 |
- DCHECK(format == GL_RGBA); |
+ DCHECK(texture_format == RGBA_8888); |
return CreateBitmap(size); |
case InvalidType: |
break; |
@@ -232,14 +237,20 @@ ResourceProvider::ResourceId ResourceProvider::CreateResource( |
} |
ResourceProvider::ResourceId ResourceProvider::CreateManagedResource( |
- gfx::Size size, GLenum format, GLint wrap_mode, TextureUsageHint hint) { |
+ gfx::Size size, |
+ GLint wrap_mode, |
+ TextureUsageHint hint, |
+ TextureFormat texture_format) { |
DCHECK(!size.IsEmpty()); |
switch (default_resource_type_) { |
case GLTexture: |
- return CreateGLTexture(size, format, GL_TEXTURE_POOL_MANAGED_CHROMIUM, |
- wrap_mode, hint); |
+ return CreateGLTexture(size, |
+ GL_TEXTURE_POOL_MANAGED_CHROMIUM, |
+ wrap_mode, |
+ hint, |
+ texture_format); |
case Bitmap: |
- DCHECK(format == GL_RGBA); |
+ DCHECK(texture_format == RGBA_8888); |
return CreateBitmap(size); |
case InvalidType: |
break; |
@@ -251,16 +262,17 @@ ResourceProvider::ResourceId ResourceProvider::CreateManagedResource( |
ResourceProvider::ResourceId ResourceProvider::CreateGLTexture( |
gfx::Size size, |
- GLenum format, |
GLenum texture_pool, |
GLint wrap_mode, |
- TextureUsageHint hint) { |
+ TextureUsageHint hint, |
+ TextureFormat texture_format) { |
DCHECK_LE(size.width(), max_texture_size_); |
DCHECK_LE(size.height(), max_texture_size_); |
DCHECK(thread_checker_.CalledOnValidThread()); |
ResourceId id = next_id_++; |
- Resource resource(0, size, format, GL_LINEAR, texture_pool, wrap_mode, hint); |
+ Resource resource( |
+ 0, size, GL_LINEAR, texture_pool, wrap_mode, hint, texture_format); |
resource.allocated = false; |
resources_[id] = resource; |
return id; |
@@ -272,7 +284,7 @@ ResourceProvider::ResourceId ResourceProvider::CreateBitmap(gfx::Size size) { |
uint8_t* pixels = new uint8_t[4 * size.GetArea()]; |
ResourceId id = next_id_++; |
- Resource resource(pixels, size, GL_RGBA, GL_LINEAR, GL_CLAMP_TO_EDGE); |
+ Resource resource(pixels, size, GL_LINEAR, GL_CLAMP_TO_EDGE); |
resource.allocated = true; |
resources_[id] = resource; |
return id; |
@@ -297,8 +309,13 @@ ResourceProvider::CreateResourceFromExternalTexture( |
texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); |
ResourceId id = next_id_++; |
- Resource resource(texture_id, gfx::Size(), 0, GL_LINEAR, 0, GL_CLAMP_TO_EDGE, |
- TextureUsageAny); |
+ Resource resource(texture_id, |
+ gfx::Size(), |
+ GL_LINEAR, |
+ 0, |
+ GL_CLAMP_TO_EDGE, |
+ TextureUsageAny, |
+ INVALID_FORMAT); |
resource.external = true; |
resource.allocated = true; |
resources_[id] = resource; |
@@ -313,15 +330,20 @@ ResourceProvider::ResourceId ResourceProvider::CreateResourceFromTextureMailbox( |
DCHECK(mailbox.IsValid()); |
Resource& resource = resources_[id]; |
if (mailbox.IsTexture()) { |
- resource = Resource(0, gfx::Size(), 0, GL_LINEAR, 0, GL_CLAMP_TO_EDGE, |
- TextureUsageAny); |
+ resource = Resource(0, |
+ gfx::Size(), |
+ GL_LINEAR, |
+ 0, |
+ GL_CLAMP_TO_EDGE, |
+ TextureUsageAny, |
+ INVALID_FORMAT); |
} else { |
DCHECK(mailbox.IsSharedMemory()); |
base::SharedMemory* shared_memory = mailbox.shared_memory(); |
DCHECK(shared_memory->memory()); |
uint8_t* pixels = reinterpret_cast<uint8_t*>(shared_memory->memory()); |
- resource = Resource(pixels, mailbox.shared_memory_size(), |
- GL_RGBA, GL_LINEAR, GL_CLAMP_TO_EDGE); |
+ resource = Resource( |
+ pixels, mailbox.shared_memory_size(), GL_LINEAR, GL_CLAMP_TO_EDGE); |
} |
resource.external = true; |
resource.allocated = true; |
@@ -432,13 +454,13 @@ void ResourceProvider::SetPixels(ResourceId id, |
image_rect, |
source_rect, |
dest_offset, |
- resource->format, |
+ resource->texture_format, |
resource->size); |
} |
if (resource->pixels) { |
DCHECK(resource->allocated); |
- DCHECK(resource->format == GL_RGBA); |
+ DCHECK(resource->texture_format == RGBA_8888); |
SkBitmap src_full; |
src_full.setConfig( |
SkBitmap::kARGB_8888_Config, image_rect.width(), image_rect.height()); |
@@ -662,7 +684,7 @@ ResourceProvider::ScopedWriteLockGL::~ScopedWriteLockGL() { |
void ResourceProvider::PopulateSkBitmapWithResource( |
SkBitmap* sk_bitmap, const Resource* resource) { |
DCHECK(resource->pixels); |
- DCHECK(resource->format == GL_RGBA); |
+ DCHECK(resource->texture_format == RGBA_8888); |
sk_bitmap->setConfig(SkBitmap::kARGB_8888_Config, |
resource->size.width(), |
resource->size.height()); |
@@ -708,7 +730,7 @@ ResourceProvider::ResourceProvider(OutputSurface* output_surface, |
use_texture_usage_hint_(false), |
use_shallow_flush_(false), |
max_texture_size_(0), |
- best_texture_format_(0) { |
+ best_texture_format_(INVALID_FORMAT) { |
DCHECK(output_surface_->HasClient()); |
} |
@@ -720,7 +742,7 @@ void ResourceProvider::InitializeSoftware() { |
default_resource_type_ = Bitmap; |
max_texture_size_ = INT_MAX / 2; |
- best_texture_format_ = GL_RGBA; |
+ best_texture_format_ = RGBA_8888; |
} |
bool ResourceProvider::InitializeGL() { |
@@ -746,7 +768,9 @@ bool ResourceProvider::InitializeGL() { |
use_texture_usage_hint_ = caps.texture_usage; |
texture_uploader_ = |
- TextureUploader::Create(context3d, use_map_sub, use_shallow_flush_); |
+ TextureUploader::Create(context3d, |
+ use_map_sub, |
+ use_shallow_flush_); |
GLC(context3d, context3d->getIntegerv(GL_MAX_TEXTURE_SIZE, |
&max_texture_size_)); |
best_texture_format_ = PlatformColor::BestTextureFormat(use_bgra); |
@@ -911,8 +935,13 @@ void ResourceProvider::ReceiveFromChild( |
it->mailbox.name)); |
ResourceId id = next_id_++; |
Resource resource( |
- texture_id, it->size, it->format, it->filter, 0, GL_CLAMP_TO_EDGE, |
- TextureUsageAny); |
+ texture_id, |
+ it->size, |
+ it->filter, |
+ 0, |
+ GL_CLAMP_TO_EDGE, |
+ TextureUsageAny, |
+ static_cast<TextureFormat>(it->format)); |
reveman
2013/09/12 15:57:40
I think we should avoid this static_cast. Can you
kaanb
2013/09/13 00:11:08
Done.
|
resource.mailbox.SetName(it->mailbox); |
// Don't allocate a texture for a child. |
resource.allocated = true; |
@@ -964,9 +993,10 @@ void ResourceProvider::TransferResource(WebGraphicsContext3D* context, |
DCHECK(!source->external || (source->external && source->mailbox.IsValid())); |
DCHECK(source->allocated); |
resource->id = id; |
- resource->format = source->format; |
resource->filter = source->filter; |
resource->size = source->size; |
+ // TODO(kaanb): Does TransferableResource::format expect a GLenum? |
+ resource->format = GetGLDataFormat(source->texture_format); |
reveman
2013/09/12 15:57:40
I think this should be "resource->format = source-
kaanb
2013/09/13 00:11:08
Done.
|
// TODO(skaslev) Implement this path for shared memory resources. |
DCHECK(!source->mailbox.IsSharedMemory()); |
@@ -1003,9 +1033,10 @@ void ResourceProvider::AcquirePixelBuffer(ResourceId id) { |
context3d->bindBuffer( |
GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, |
resource->gl_pixel_buffer_id); |
+ size_t bytes_per_pixel = BytesPerPixel(resource->texture_format); |
context3d->bufferData( |
GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, |
- 4 * resource->size.GetArea(), |
+ bytes_per_pixel * resource->size.GetArea(), |
NULL, |
GL_DYNAMIC_DRAW); |
context3d->bindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); |
@@ -1177,25 +1208,27 @@ void ResourceProvider::BeginSetPixels(ResourceId id) { |
GL_ASYNC_PIXEL_UNPACK_COMPLETED_CHROMIUM, |
resource->gl_upload_query_id); |
if (allocate) { |
- context3d->asyncTexImage2DCHROMIUM(GL_TEXTURE_2D, |
- 0, /* level */ |
- resource->format, |
- resource->size.width(), |
- resource->size.height(), |
- 0, /* border */ |
- resource->format, |
- GL_UNSIGNED_BYTE, |
- NULL); |
+ context3d->asyncTexImage2DCHROMIUM( |
+ GL_TEXTURE_2D, |
+ 0, /* level */ |
+ GetGLDataFormat(resource->texture_format), |
reveman
2013/09/12 15:57:40
I think we should use a separate GetGLInternalForm
kaanb
2013/09/13 00:11:08
Done.
|
+ resource->size.width(), |
+ resource->size.height(), |
+ 0, /* border */ |
+ GetGLDataFormat(resource->texture_format), |
+ GetGLDataType(resource->texture_format), |
+ NULL); |
} else { |
- context3d->asyncTexSubImage2DCHROMIUM(GL_TEXTURE_2D, |
- 0, /* level */ |
- 0, /* x */ |
- 0, /* y */ |
- resource->size.width(), |
- resource->size.height(), |
- resource->format, |
- GL_UNSIGNED_BYTE, |
- NULL); |
+ context3d->asyncTexSubImage2DCHROMIUM( |
+ GL_TEXTURE_2D, |
+ 0, /* level */ |
+ 0, /* x */ |
+ 0, /* y */ |
+ resource->size.width(), |
+ resource->size.height(), |
+ GetGLDataFormat(resource->texture_format), |
+ GetGLDataType(resource->texture_format), |
+ NULL); |
} |
context3d->endQueryEXT(GL_ASYNC_PIXEL_UNPACK_COMPLETED_CHROMIUM); |
context3d->bindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); |
@@ -1204,7 +1237,7 @@ void ResourceProvider::BeginSetPixels(ResourceId id) { |
if (resource->pixels) { |
DCHECK(!resource->mailbox.IsValid()); |
DCHECK(resource->pixel_buffer); |
- DCHECK(resource->format == GL_RGBA); |
+ DCHECK(resource->texture_format == RGBA_8888); |
std::swap(resource->pixels, resource->pixel_buffer); |
delete[] resource->pixel_buffer; |
@@ -1310,7 +1343,7 @@ void ResourceProvider::LazyAllocate(Resource* resource) { |
resource->allocated = true; |
WebGraphicsContext3D* context3d = Context3d(); |
gfx::Size& size = resource->size; |
- GLenum format = resource->format; |
+ TextureFormat format = resource->texture_format; |
GLC(context3d, context3d->bindTexture(GL_TEXTURE_2D, resource->gl_id)); |
if (use_texture_storage_ext_ && IsTextureFormatSupportedForStorage(format)) { |
GLenum storage_format = TextureToStorageFormat(format); |
@@ -1322,12 +1355,12 @@ void ResourceProvider::LazyAllocate(Resource* resource) { |
} else { |
GLC(context3d, context3d->texImage2D(GL_TEXTURE_2D, |
0, |
- format, |
+ GetGLDataFormat(format), |
reveman
2013/09/12 15:57:40
GetGLInternalFormat
kaanb
2013/09/13 00:11:08
Done.
|
size.width(), |
size.height(), |
0, |
- format, |
- GL_UNSIGNED_BYTE, |
+ GetGLDataFormat(format), |
+ GetGLDataType(format), |
NULL)); |
} |
} |
@@ -1352,7 +1385,7 @@ void ResourceProvider::AcquireImage(ResourceId id) { |
resource->allocated = true; |
WebGraphicsContext3D* context3d = Context3d(); |
DCHECK(context3d); |
- DCHECK_EQ(static_cast<GLenum>(GL_RGBA), resource->format); |
+ DCHECK_EQ(RGBA_8888, resource->texture_format); |
resource->image_id = context3d->createImageCHROMIUM( |
resource->size.width(), resource->size.height(), GL_RGBA8_OES); |
DCHECK(resource->image_id); |
@@ -1432,4 +1465,72 @@ WebKit::WebGraphicsContext3D* ResourceProvider::Context3d() const { |
return context_provider ? context_provider->Context3d() : NULL; |
} |
+size_t ResourceProvider::BytesPerPixel(TextureFormat format) { |
+ size_t components_per_pixel = 0; |
+ switch (format) { |
+ case RGBA_8888: |
+ case RGBA_4444: |
+ case BGRA_8888: |
+ components_per_pixel = 4; |
+ break; |
+ case LUMINANCE_8: |
+ components_per_pixel = 1; |
+ break; |
+ default: |
reveman
2013/09/12 15:57:40
remove "default:" case after removing INVALID_FORM
kaanb
2013/09/13 00:11:08
Done.
|
+ NOTREACHED(); |
+ } |
+ size_t bits_per_component = 0; |
+ switch (format) { |
+ case RGBA_8888: |
+ case BGRA_8888: |
+ case LUMINANCE_8: |
+ bits_per_component = 8; |
+ break; |
+ case RGBA_4444: |
+ bits_per_component = 4; |
+ break; |
+ default: |
reveman
2013/09/12 15:57:40
remove "default:" case after removing INVALID_FORM
kaanb
2013/09/13 00:11:08
Done.
|
+ NOTREACHED(); |
+ } |
+ const size_t kBitsPerByte = 8; |
+ return (components_per_pixel * bits_per_component) / kBitsPerByte; |
+} |
+ |
+GLenum ResourceProvider::GetGLDataType(TextureFormat format) { |
+ GLenum texture_data_type = GL_UNSIGNED_BYTE; |
+ switch (format) { |
+ case RGBA_4444: |
+ texture_data_type = GL_UNSIGNED_SHORT_4_4_4_4; |
+ break; |
+ case RGBA_8888: |
+ case BGRA_8888: |
+ case LUMINANCE_8: |
+ texture_data_type = GL_UNSIGNED_BYTE; |
+ break; |
+ default: |
reveman
2013/09/12 15:57:40
remove "default:" case after removing INVALID_FORM
kaanb
2013/09/13 00:11:08
Done.
|
+ NOTREACHED(); |
+ break; |
+ } |
+ return texture_data_type; |
+} |
+ |
+GLenum ResourceProvider::GetGLDataFormat(TextureFormat format) { |
+ GLenum data_format = GL_RGBA; |
+ switch (format) { |
+ case RGBA_8888: |
+ case RGBA_4444: |
+ data_format = GL_RGBA; |
+ break; |
+ case BGRA_8888: |
+ data_format = GL_BGRA_EXT; |
+ break; |
+ case LUMINANCE_8: |
+ data_format = GL_LUMINANCE; |
+ break; |
+ default: |
reveman
2013/09/12 15:57:40
remove "default:" case after removing INVALID_FORM
kaanb
2013/09/13 00:11:08
Done.
|
+ NOTREACHED(); |
+ } |
+ return data_format; |
+} |
+ |
} // namespace cc |