Commit f19a42a5 authored by joaoe@opera.com's avatar joaoe@opera.com

Added a new overload of gfx::Image::CreateFrom1xPNGBytes

... that accepts a RefCountedMemory. This saves some buffer copying in
many places, specially welcome with big images.

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@243492 0039d316-1c4b-4281-b951-d872f2087c98
parent d359b613
...@@ -130,8 +130,7 @@ ScopedJavaLocalRef<jobject> FaviconHelper::GetSyncedFaviconImageForURL( ...@@ -130,8 +130,7 @@ ScopedJavaLocalRef<jobject> FaviconHelper::GetSyncedFaviconImageForURL(
return ScopedJavaLocalRef<jobject>(); return ScopedJavaLocalRef<jobject>();
// Convert favicon_image_result to java objects. // Convert favicon_image_result to java objects.
gfx::Image favicon_image = gfx::Image::CreateFrom1xPNGBytes( gfx::Image favicon_image = gfx::Image::CreateFrom1xPNGBytes(favicon_png);
favicon_png->front(), favicon_png->size());
SkBitmap favicon_bitmap = favicon_image.AsBitmap(); SkBitmap favicon_bitmap = favicon_image.AsBitmap();
ScopedJavaLocalRef<jobject> j_favicon_bitmap; ScopedJavaLocalRef<jobject> j_favicon_bitmap;
......
...@@ -155,10 +155,8 @@ void ReadBitmap(const base::FilePath& image_path, ...@@ -155,10 +155,8 @@ void ReadBitmap(const base::FilePath& image_path,
return; return;
} }
const unsigned char* data = gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(
reinterpret_cast<const unsigned char*>(image_data.data()); base::RefCountedString::TakeString(&image_data));
gfx::Image image =
gfx::Image::CreateFrom1xPNGBytes(data, image_data.length());
if (image.IsEmpty()) { if (image.IsEmpty()) {
LOG(ERROR) << "Failed to decode PNG file."; LOG(ERROR) << "Failed to decode PNG file.";
return; return;
......
...@@ -742,7 +742,7 @@ gfx::Image Create1xFaviconFromPNGFile(const std::string& path) { ...@@ -742,7 +742,7 @@ gfx::Image Create1xFaviconFromPNGFile(const std::string& path) {
std::string contents; std::string contents;
base::ReadFileToString(full_path, &contents); base::ReadFileToString(full_path, &contents);
return gfx::Image::CreateFrom1xPNGBytes( return gfx::Image::CreateFrom1xPNGBytes(
reinterpret_cast<const unsigned char*>(contents.data()), contents.size()); base::RefCountedString::TakeString(&contents));
} }
std::string IndexedURL(int i) { std::string IndexedURL(int i) {
......
...@@ -593,8 +593,7 @@ void RecentTabsSubMenuModel::AddTabFavicon(int command_id, const GURL& url) { ...@@ -593,8 +593,7 @@ void RecentTabsSubMenuModel::AddTabFavicon(int command_id, const GURL& url) {
scoped_refptr<base::RefCountedMemory> favicon_png; scoped_refptr<base::RefCountedMemory> favicon_png;
if (open_tabs && if (open_tabs &&
open_tabs->GetSyncedFaviconForPageURL(url.spec(), &favicon_png)) { open_tabs->GetSyncedFaviconForPageURL(url.spec(), &favicon_png)) {
gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(favicon_png->front(), gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(favicon_png);
favicon_png->size());
SetIcon(index_in_menu, image); SetIcon(index_in_menu, image);
return; return;
} }
......
...@@ -292,7 +292,8 @@ scoped_ptr<SkBitmap> IconUtil::CreateSkBitmapFromIconResource(HMODULE module, ...@@ -292,7 +292,8 @@ scoped_ptr<SkBitmap> IconUtil::CreateSkBitmapFromIconResource(HMODULE module,
const unsigned char* png_bytes = const unsigned char* png_bytes =
reinterpret_cast<const unsigned char*>(png_data); reinterpret_cast<const unsigned char*>(png_data);
gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(png_bytes, png_size); gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(
new base::RefCountedStaticMemory(png_bytes, png_size));
return scoped_ptr<SkBitmap>(new SkBitmap(image.AsBitmap())); return scoped_ptr<SkBitmap>(new SkBitmap(image.AsBitmap()));
} }
......
...@@ -602,8 +602,17 @@ Image Image::CreateFrom1xPNGBytes(const unsigned char* input, ...@@ -602,8 +602,17 @@ Image Image::CreateFrom1xPNGBytes(const unsigned char* input,
scoped_refptr<base::RefCountedBytes> raw_data(new base::RefCountedBytes()); scoped_refptr<base::RefCountedBytes> raw_data(new base::RefCountedBytes());
raw_data->data().assign(input, input + input_size); raw_data->data().assign(input, input + input_size);
return CreateFrom1xPNGBytes(raw_data);
}
Image Image::CreateFrom1xPNGBytes(
const scoped_refptr<base::RefCountedMemory>& input) {
if (!input.get() || input->size() == 0u)
return gfx::Image();
std::vector<gfx::ImagePNGRep> image_reps; std::vector<gfx::ImagePNGRep> image_reps;
image_reps.push_back(ImagePNGRep(raw_data, 1.0f)); image_reps.push_back(ImagePNGRep(input, 1.0f));
return gfx::Image(image_reps); return gfx::Image(image_reps);
} }
......
...@@ -113,6 +113,10 @@ class GFX_EXPORT Image { ...@@ -113,6 +113,10 @@ class GFX_EXPORT Image {
static Image CreateFrom1xPNGBytes(const unsigned char* input, static Image CreateFrom1xPNGBytes(const unsigned char* input,
size_t input_size); size_t input_size);
// Creates an image from the PNG encoded input.
static Image CreateFrom1xPNGBytes(
const scoped_refptr<base::RefCountedMemory>& input);
// Converts the Image to the desired representation and stores it internally. // Converts the Image to the desired representation and stores it internally.
// The returned result is a weak pointer owned by and scoped to the life of // The returned result is a weak pointer owned by and scoped to the life of
// the Image. Must only be called if IsEmpty() is false. // the Image. Must only be called if IsEmpty() is false.
......
...@@ -100,7 +100,7 @@ class SnapshotAuraTest : public testing::Test { ...@@ -100,7 +100,7 @@ class SnapshotAuraTest : public testing::Test {
gfx::Rect local_bounds(test_window_->bounds().size()); gfx::Rect local_bounds(test_window_->bounds().size());
ui::GrabWindowSnapshot(test_window(), &png_representation, local_bounds); ui::GrabWindowSnapshot(test_window(), &png_representation, local_bounds);
return gfx::Image::CreateFrom1xPNGBytes( return gfx::Image::CreateFrom1xPNGBytes(
&(png_representation[0]), png_representation.size()); base::RefCountedBytes::TakeVector(&png_representation));
} }
private: private:
......
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