Skip to content

Commit

Permalink
SkColorType instead of (deprecated) SkBitmap::Config
Browse files Browse the repository at this point in the history
Part of this refactoring was the recognition of a common pattern:
- setConfig + alloc + setImmutable + pass_to_UIResourceBitmap

This CL introduces a direct way on UIResourceBitmap to create such a bitmap, by just specifying its dimensions. This encapsulates internal requirements (e.g. colortype and immutability).

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@259349 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
reed@google.com committed Mar 25, 2014
1 parent c14865c commit 0046982
Show file tree
Hide file tree
Showing 24 changed files with 93 additions and 159 deletions.
11 changes: 6 additions & 5 deletions cc/layers/heads_up_display_layer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,14 @@ void HeadsUpDisplayLayerImpl::UpdateHudTexture(
}

TRACE_EVENT0("cc", "UploadHudTexture");
const SkBitmap* bitmap = &hud_canvas_->getDevice()->accessBitmap(false);
SkAutoLockPixels locker(*bitmap);

SkImageInfo info;
size_t row_bytes = 0;
const void* pixels = hud_canvas_->peekPixels(&info, &row_bytes);
DCHECK(pixels);
gfx::Rect content_rect(content_bounds());
DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config);
DCHECK(info.colorType() == kPMColor_SkColorType);
resource_provider->SetPixels(hud_resource_->id(),
static_cast<const uint8_t*>(bitmap->getPixels()),
static_cast<const uint8_t*>(pixels),
content_rect,
content_rect,
gfx::Vector2d());
Expand Down
8 changes: 2 additions & 6 deletions cc/layers/nine_patch_layer_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,8 @@ void NinePatchLayerLayoutTest(const gfx::Size& bitmap_size,
layer->draw_properties().render_target = layer.get();

UIResourceId uid = 1;
SkBitmap skbitmap;
skbitmap.setConfig(
SkBitmap::kARGB_8888_Config, bitmap_size.width(), bitmap_size.height());
skbitmap.allocPixels();
skbitmap.setImmutable();
UIResourceBitmap bitmap(skbitmap);
bool is_opaque = false;
UIResourceBitmap bitmap(bitmap_size, is_opaque);

host_impl.CreateUIResource(uid, bitmap);
layer->SetUIResourceId(uid);
Expand Down
8 changes: 2 additions & 6 deletions cc/layers/nine_patch_layer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,9 @@ TEST_F(NinePatchLayerTest, SetLayerProperties) {

EXPECT_FALSE(test_layer->DrawsContent());

SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
bitmap.allocPixels();
bitmap.setImmutable();

bool is_opaque = false;
scoped_ptr<ScopedUIResource> resource = ScopedUIResource::Create(
layer_tree_host_.get(), UIResourceBitmap(bitmap));
layer_tree_host_.get(), UIResourceBitmap(gfx::Size(10, 10), is_opaque));
gfx::Rect aperture(5, 5, 1, 1);
bool fill_center = true;
test_layer->SetAperture(aperture);
Expand Down
6 changes: 1 addition & 5 deletions cc/layers/painted_scrollbar_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,7 @@ UIResourceBitmap PaintedScrollbarLayer::RasterizeScrollbarPart(
DCHECK(!layer_rect.size().IsEmpty());

SkBitmap skbitmap;
skbitmap.setConfig(SkBitmap::kARGB_8888_Config,
content_rect.width(),
content_rect.height());
skbitmap.allocPixels();

skbitmap.allocN32Pixels(content_rect.width(), content_rect.height());
SkCanvas skcanvas(skbitmap);

float scale_x =
Expand Down
10 changes: 1 addition & 9 deletions cc/layers/ui_resource_layer_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,7 @@ scoped_ptr<UIResourceLayerImpl> GenerateUIResourceLayer(
layer->CreateRenderSurface();
layer->draw_properties().render_target = layer.get();

SkBitmap skbitmap;
skbitmap.setConfig(SkBitmap::kARGB_8888_Config,
bitmap_size.width(),
bitmap_size.height(),
0,
opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
skbitmap.allocPixels();
skbitmap.setImmutable();
UIResourceBitmap bitmap(skbitmap);
UIResourceBitmap bitmap(bitmap_size, opaque);

host_impl->CreateUIResource(uid, bitmap);
layer->SetUIResourceId(uid);
Expand Down
11 changes: 3 additions & 8 deletions cc/layers/ui_resource_layer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ TEST_F(UIResourceLayerTest, SetBitmap) {
EXPECT_FALSE(test_layer->DrawsContent());

SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
bitmap.allocPixels();
bitmap.allocN32Pixels(10, 10);
bitmap.setImmutable();

test_layer->SetBitmap(bitmap);
Expand All @@ -93,13 +92,9 @@ TEST_F(UIResourceLayerTest, SetUIResourceId) {

EXPECT_FALSE(test_layer->DrawsContent());

SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
bitmap.allocPixels();
bitmap.setImmutable();

bool is_opaque = false;
scoped_ptr<ScopedUIResource> resource = ScopedUIResource::Create(
layer_tree_host_.get(), UIResourceBitmap(bitmap));
layer_tree_host_.get(), UIResourceBitmap(gfx::Size(10, 10), is_opaque));
test_layer->SetUIResourceId(resource->id());
test_layer->Update(&queue, &occlusion_tracker);

Expand Down
10 changes: 3 additions & 7 deletions cc/output/gl_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1739,10 +1739,8 @@ void GLRenderer::DrawPictureQuad(const DrawingFrame* frame,
const PictureDrawQuad* quad) {
if (on_demand_tile_raster_bitmap_.width() != quad->texture_size.width() ||
on_demand_tile_raster_bitmap_.height() != quad->texture_size.height()) {
on_demand_tile_raster_bitmap_.setConfig(SkBitmap::kARGB_8888_Config,
quad->texture_size.width(),
quad->texture_size.height());
on_demand_tile_raster_bitmap_.allocPixels();
on_demand_tile_raster_bitmap_.allocN32Pixels(quad->texture_size.width(),
quad->texture_size.height());

if (on_demand_tile_raster_resource_id_)
resource_provider_->DeleteResource(on_demand_tile_raster_resource_id_);
Expand Down Expand Up @@ -2298,9 +2296,7 @@ void GLRenderer::GetFramebufferPixelsAsync(
DCHECK(request->force_bitmap_result());

scoped_ptr<SkBitmap> bitmap(new SkBitmap);
bitmap->setConfig(
SkBitmap::kARGB_8888_Config, window_rect.width(), window_rect.height());
bitmap->allocPixels();
bitmap->allocN32Pixels(window_rect.width(), window_rect.height());

scoped_ptr<SkAutoLockPixels> lock(new SkAutoLockPixels(*bitmap));

Expand Down
3 changes: 1 addition & 2 deletions cc/output/renderer_pixeltest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1805,8 +1805,7 @@ TYPED_TEST(RendererPixelTest, PictureDrawQuadDisableImageFiltering) {
CreateTestRenderPass(id, viewport, transform_to_root);

SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
bitmap.allocPixels();
bitmap.allocN32Pixels(2, 2);
{
SkAutoLockPixels lock(bitmap);
SkCanvas canvas(bitmap);
Expand Down
7 changes: 2 additions & 5 deletions cc/resources/bitmap_content_layer_updater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,8 @@ void BitmapContentLayerUpdater::PrepareToUpdate(
devtools_instrumentation::ScopedLayerTask paint_setup(
devtools_instrumentation::kPaintSetup, layer_id_);
canvas_size_ = content_rect.size();
bitmap_backing_.setConfig(
SkBitmap::kARGB_8888_Config,
canvas_size_.width(), canvas_size_.height(),
0, layer_is_opaque_ ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
bitmap_backing_.allocPixels();
bitmap_backing_.allocN32Pixels(
canvas_size_.width(), canvas_size_.height(), layer_is_opaque_);
canvas_ = skia::AdoptRef(new SkCanvas(bitmap_backing_));
}

Expand Down
10 changes: 2 additions & 8 deletions cc/resources/picture_pile_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -674,10 +674,7 @@ TEST(PicturePileImpl, RasterContentsOpaque) {
canvas_rect.Inset(0, 0, -1, -1);

SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config,
canvas_rect.width(),
canvas_rect.height());
bitmap.allocPixels();
bitmap.allocN32Pixels(canvas_rect.width(), canvas_rect.height());
SkCanvas canvas(bitmap);
canvas.clear(SK_ColorTRANSPARENT);

Expand Down Expand Up @@ -726,10 +723,7 @@ TEST(PicturePileImpl, RasterContentsTransparent) {
canvas_rect.Inset(0, 0, -1, -1);

SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config,
canvas_rect.width(),
canvas_rect.height());
bitmap.allocPixels();
bitmap.allocN32Pixels(canvas_rect.width(), canvas_rect.height());
SkCanvas canvas(bitmap);

FakeRenderingStatsInstrumentation rendering_stats_instrumentation;
Expand Down
53 changes: 24 additions & 29 deletions cc/resources/resource_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,8 @@ skia::RefPtr<SkSurface> ResourceProvider::DirectRasterBuffer::CreateSurface() {
DCHECK_EQ(RGBA_8888, resource()->format);
SkImageInfo image_info = SkImageInfo::MakeN32Premul(
resource()->size.width(), resource()->size.height());
size_t row_bytes = SkBitmap::ComputeRowBytes(SkBitmap::kARGB_8888_Config,
resource()->size.width());
surface = skia::AdoptRef(SkSurface::NewRasterDirect(
image_info, resource()->pixels, row_bytes));
image_info, resource()->pixels, image_info.minRowBytes()));
break;
}
default:
Expand Down Expand Up @@ -486,19 +484,18 @@ SkCanvas* ResourceProvider::BitmapRasterBuffer::DoLockForWrite() {
case RGBA_4444:
// Use the default stride if we will eventually convert this
// bitmap to 4444.
raster_bitmap_.setConfig(SkBitmap::kARGB_8888_Config,
resource()->size.width(),
resource()->size.height());
raster_bitmap_.allocPixels();
raster_bitmap_.allocN32Pixels(resource()->size.width(),
resource()->size.height());
break;
case RGBA_8888:
case BGRA_8888:
raster_bitmap_.setConfig(SkBitmap::kARGB_8888_Config,
resource()->size.width(),
resource()->size.height(),
stride);
raster_bitmap_.setPixels(mapped_buffer_);
case BGRA_8888: {
SkImageInfo info = SkImageInfo::MakeN32Premul(resource()->size.width(),
resource()->size.height());
if (0 == stride)
stride = info.minRowBytes();
raster_bitmap_.installPixels(info, mapped_buffer_, stride);
break;
}
case LUMINANCE_8:
case RGB_565:
case ETC1:
Expand Down Expand Up @@ -936,21 +933,20 @@ void ResourceProvider::SetPixels(ResourceId id,
DCHECK_EQ(Bitmap, resource->type);
DCHECK(resource->allocated);
DCHECK_EQ(RGBA_8888, resource->format);
SkBitmap src_full;
src_full.setConfig(
SkBitmap::kARGB_8888_Config, image_rect.width(), image_rect.height());
src_full.setPixels(const_cast<uint8_t*>(image));
SkBitmap src_subset;
SkIRect sk_source_rect = SkIRect::MakeXYWH(source_rect.x(),
source_rect.y(),
source_rect.width(),
source_rect.height());
sk_source_rect.offset(-image_rect.x(), -image_rect.y());
src_full.extractSubset(&src_subset, sk_source_rect);
DCHECK(source_rect.x() >= image_rect.x());
DCHECK(source_rect.y() >= image_rect.y());
DCHECK(source_rect.right() <= image_rect.right());
DCHECK(source_rect.bottom() <= image_rect.bottom());
SkImageInfo source_info =
SkImageInfo::MakeN32Premul(source_rect.width(), source_rect.height());
size_t image_row_bytes = image_rect.width() * 4;
gfx::Vector2d source_offset = source_rect.origin() - image_rect.origin();
image += source_offset.y() * image_row_bytes + source_offset.x() * 4;

ScopedWriteLockSoftware lock(this, id);
SkCanvas* dest = lock.sk_canvas();
dest->writePixels(src_subset, dest_offset.x(), dest_offset.y());
dest->writePixels(
source_info, image, image_row_bytes, dest_offset.x(), dest_offset.y());
}
}

Expand Down Expand Up @@ -1178,10 +1174,9 @@ ResourceProvider::ScopedWriteLockGL::~ScopedWriteLockGL() {
void ResourceProvider::PopulateSkBitmapWithResource(
SkBitmap* sk_bitmap, const Resource* resource) {
DCHECK_EQ(RGBA_8888, resource->format);
sk_bitmap->setConfig(SkBitmap::kARGB_8888_Config,
resource->size.width(),
resource->size.height());
sk_bitmap->setPixels(resource->pixels);
SkImageInfo info = SkImageInfo::MakeN32Premul(resource->size.width(),
resource->size.height());
sk_bitmap->installPixels(info, resource->pixels, info.minRowBytes());
}

ResourceProvider::ScopedReadLockSoftware::ScopedReadLockSoftware(
Expand Down
16 changes: 6 additions & 10 deletions cc/resources/resource_provider_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -848,11 +848,9 @@ TEST_P(ResourceProviderTest, TransferSoftwareResources) {
ResourceProvider::ResourceId id3 = child_resource_provider_->CreateResource(
size, GL_CLAMP_TO_EDGE, ResourceProvider::TextureUsageAny, format);
uint8_t data3[4] = { 6, 7, 8, 9 };
SkBitmap bitmap3;
bitmap3.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height());
bitmap3.setPixels(data3);
SkImageInfo info = SkImageInfo::MakeN32Premul(size.width(), size.height());
SkCanvas* raster_canvas = child_resource_provider_->MapImageRasterBuffer(id3);
raster_canvas->writePixels(bitmap3, 0, 0);
raster_canvas->writePixels(info, data3, info.minRowBytes(), 0, 0);
child_resource_provider_->UnmapImageRasterBuffer(id3);

scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory());
Expand Down Expand Up @@ -962,7 +960,7 @@ TEST_P(ResourceProviderTest, TransferSoftwareResources) {
expected_ids.insert(id3);
expected_ids.insert(id4);
std::set<ResourceProvider::ResourceId> returned_ids;
for(unsigned i = 0; i < 4; i++)
for (unsigned i = 0; i < 4; i++)
returned_ids.insert(returned_to_child[i].id);
EXPECT_EQ(expected_ids, returned_ids);
EXPECT_FALSE(returned_to_child[0].lost);
Expand Down Expand Up @@ -1042,7 +1040,7 @@ TEST_P(ResourceProviderTest, TransferSoftwareResources) {
expected_ids.insert(id3);
expected_ids.insert(id4);
std::set<ResourceProvider::ResourceId> returned_ids;
for(unsigned i = 0; i < 4; i++)
for (unsigned i = 0; i < 4; i++)
returned_ids.insert(returned_to_child[i].id);
EXPECT_EQ(expected_ids, returned_ids);
EXPECT_FALSE(returned_to_child[0].lost);
Expand Down Expand Up @@ -2844,8 +2842,7 @@ TEST_P(ResourceProviderTest, PixelBuffer_Bitmap) {
resource_provider->AcquirePixelRasterBuffer(id);

SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height());
bitmap.allocPixels();
bitmap.allocN32Pixels(size.width(), size.height());
*(bitmap.getAddr32(0, 0)) = kBadBeef;
SkCanvas* canvas = resource_provider->MapPixelRasterBuffer(id);
canvas->writePixels(bitmap, 0, 0);
Expand Down Expand Up @@ -3064,8 +3061,7 @@ TEST_P(ResourceProviderTest, Image_Bitmap) {
size, GL_CLAMP_TO_EDGE, ResourceProvider::TextureUsageAny, format);

SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height());
bitmap.allocPixels();
bitmap.allocN32Pixels(size.width(), size.height());
*(bitmap.getAddr32(0, 0)) = kBadBeef;
SkCanvas* canvas = resource_provider->MapImageRasterBuffer(id);
ASSERT_TRUE(!!canvas);
Expand Down
3 changes: 1 addition & 2 deletions cc/resources/resource_update_controller_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ class ResourceUpdateControllerTest : public Test {

protected:
virtual void SetUp() {
bitmap_.setConfig(SkBitmap::kARGB_8888_Config, 300, 150);
bitmap_.allocPixels();
bitmap_.allocN32Pixels(300, 150);

for (int i = 0; i < 4; i++) {
textures_[i] = PrioritizedResource::Create(resource_manager_.get(),
Expand Down
17 changes: 14 additions & 3 deletions cc/resources/ui_resource_bitmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace cc {

void UIResourceBitmap::Create(const skia::RefPtr<SkPixelRef>& pixel_ref,
gfx::Size size,
const gfx::Size& size,
UIResourceFormat format) {
DCHECK(size.width());
DCHECK(size.height());
Expand All @@ -29,7 +29,7 @@ void UIResourceBitmap::Create(const skia::RefPtr<SkPixelRef>& pixel_ref,
}

UIResourceBitmap::UIResourceBitmap(const SkBitmap& skbitmap) {
DCHECK_EQ(skbitmap.config(), SkBitmap::kARGB_8888_Config);
DCHECK_EQ(skbitmap.colorType(), kPMColor_SkColorType);
DCHECK_EQ(skbitmap.width(), skbitmap.rowBytesAsPixels());
DCHECK(skbitmap.isImmutable());

Expand All @@ -41,8 +41,19 @@ UIResourceBitmap::UIResourceBitmap(const SkBitmap& skbitmap) {
SetOpaque(skbitmap.isOpaque());
}

UIResourceBitmap::UIResourceBitmap(const gfx::Size& size, bool is_opaque) {
SkAlphaType alphaType = is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
SkImageInfo info =
SkImageInfo::MakeN32(size.width(), size.height(), alphaType);
skia::RefPtr<SkPixelRef> pixel_ref = skia::AdoptRef(
SkMallocPixelRef::NewAllocate(info, info.minRowBytes(), NULL));
pixel_ref->setImmutable();
Create(pixel_ref, size, UIResourceBitmap::RGBA8);
SetOpaque(is_opaque);
}

UIResourceBitmap::UIResourceBitmap(const skia::RefPtr<SkPixelRef>& pixel_ref,
gfx::Size size) {
const gfx::Size& size) {
Create(pixel_ref, size, UIResourceBitmap::ETC1);
}

Expand Down
6 changes: 4 additions & 2 deletions cc/resources/ui_resource_bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ class CC_EXPORT UIResourceBitmap {
// User must ensure that |skbitmap| is immutable. The SkBitmap Format should
// be 32-bit RGBA.
explicit UIResourceBitmap(const SkBitmap& skbitmap);
UIResourceBitmap(const skia::RefPtr<SkPixelRef>& pixel_ref, gfx::Size size);
UIResourceBitmap(const gfx::Size& size, bool is_opaque);
UIResourceBitmap(const skia::RefPtr<SkPixelRef>& pixel_ref,
const gfx::Size& size);
~UIResourceBitmap();

private:
friend class AutoLockUIResourceBitmap;

void Create(const skia::RefPtr<SkPixelRef>& pixel_ref,
gfx::Size size,
const gfx::Size& size,
UIResourceFormat format);

skia::RefPtr<SkPixelRef> pixel_ref_;
Expand Down
Loading

0 comments on commit 0046982

Please sign in to comment.