Commit b5d0192d authored by jyasskin@chromium.org's avatar jyasskin@chromium.org

Avoid crashing when an ImageSkia has no representations and is converted to SkBitmap.


Review URL: https://chromiumcodereview.appspot.com/10828296

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@151570 0039d316-1c4b-4281-b951-d872f2087c98
parent 7d574f0b
......@@ -165,13 +165,6 @@ ImageSkia& ImageSkia::operator=(const SkBitmap& other) {
return *this;
}
ImageSkia::operator SkBitmap&() const {
if (isNull())
return const_cast<SkBitmap&>(NullImageRep().sk_bitmap());
return const_cast<SkBitmap&>(storage_->image_reps()[0].sk_bitmap());
}
ImageSkia::~ImageSkia() {
}
......@@ -279,16 +272,18 @@ std::vector<ImageSkiaRep> ImageSkia::image_reps() const {
return image_reps;
}
const SkBitmap* ImageSkia::bitmap() const {
ImageSkia::operator SkBitmap&() const {
if (isNull()) {
// Callers expect a ImageSkiaRep even if it is |isNull()|.
// TODO(pkotwicz): Fix this.
return &NullImageRep().sk_bitmap();
return NullImageRep().mutable_sk_bitmap();
}
ImageSkiaReps::iterator it =
storage_->FindRepresentation(ui::SCALE_FACTOR_100P, true);
return &it->sk_bitmap();
if (it != storage_->image_reps().end())
return it->mutable_sk_bitmap();
return NullImageRep().mutable_sk_bitmap();
}
void ImageSkia::Init(const ImageSkiaRep& image_rep) {
......
......@@ -113,7 +113,7 @@ class UI_EXPORT ImageSkia {
// This function should only be used in unittests and on platforms which do
// not support scale factors other than 1x.
// TODO(pkotwicz): Return null SkBitmap when the object has no 1x bitmap.
const SkBitmap* bitmap() const;
const SkBitmap* bitmap() const { return &operator SkBitmap&(); }
// Returns a vector with the image reps contained in this object.
// There is no guarantee that this will return all images rep for
......
......@@ -46,6 +46,9 @@ class UI_EXPORT ImageSkiaRep {
const SkBitmap& sk_bitmap() const { return bitmap_; }
private:
friend class ImageSkia;
SkBitmap& mutable_sk_bitmap() { return bitmap_; }
SkBitmap bitmap_;
ui::ScaleFactor scale_factor_;
};
......
......@@ -173,4 +173,27 @@ TEST(ImageSkiaTest, GetBitmap) {
EXPECT_FALSE(bitmap->isNull());
}
TEST(ImageSkiaTest, GetBitmapFromEmpty) {
// Create an image with 1 representation and remove it so the ImageSkiaStorage
// is left with no representations.
ImageSkia empty_image(ImageSkiaRep(Size(100, 200), ui::SCALE_FACTOR_100P));
ImageSkia empty_image_copy(empty_image);
empty_image.RemoveRepresentation(ui::SCALE_FACTOR_100P);
// Check that ImageSkia::bitmap() still returns a valid SkBitmap pointer for
// the image and all its copies.
const SkBitmap* bitmap = empty_image_copy.bitmap();
ASSERT_NE(static_cast<SkBitmap*>(NULL), bitmap);
EXPECT_TRUE(bitmap->isNull());
EXPECT_TRUE(bitmap->empty());
}
TEST(ImageSkiaTest, OperatorBitmapFromSource) {
ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200));
// ImageSkia should use the source to create the bitmap.
const SkBitmap& bitmap = image_skia;
ASSERT_NE(static_cast<SkBitmap*>(NULL), &bitmap);
EXPECT_FALSE(bitmap.isNull());
}
} // namespace gfx
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment