Commit 8add2987 authored by pkotwicz@chromium.org's avatar pkotwicz@chromium.org

On the Mac, we populate NSImage from bitmaps in resource bundles. Introduce...

On the Mac, we populate NSImage from bitmaps in resource bundles. Introduce ImageSkia which contains vector of SkBitmaps. Previously this functionality was within ImageRepSkia. ImageSkia exposes this.

Move gfx::Image::GetSkBitmapAtIndex and gfx::Image::GetNumSkBitmaps to ImageSkia

BUG=122992
TEST=None

Review URL: http://codereview.chromium.org/10086023

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@133607 0039d316-1c4b-4281-b951-d872f2087c98
parent 50ed7c91
......@@ -16,6 +16,7 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/size.h"
using content::BrowserThread;
......@@ -212,9 +213,11 @@ TEST_F(ImageLoadingTrackerTest, MultipleImages) {
EXPECT_EQ(1, image_loaded_count());
// Check that all images were loaded.
ASSERT_EQ(2u, image_.GetNumberOfSkBitmaps());
const SkBitmap* bmp1 = image_.GetSkBitmapAtIndex(0);
const SkBitmap* bmp2 = image_.GetSkBitmapAtIndex(1);
const std::vector<const SkBitmap*>& bitmaps =
image_.ToImageSkia()->bitmaps();
ASSERT_EQ(2u, bitmaps.size());
const SkBitmap* bmp1 = bitmaps[0];
const SkBitmap* bmp2 = bitmaps[1];
if (bmp1->width() > bmp2->width()) {
std::swap(bmp1, bmp2);
}
......
......@@ -24,6 +24,7 @@
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/skbitmap_operations.h"
using content::BrowserThread;
......@@ -309,13 +310,15 @@ RefCountedMemory* ReadFileData(const FilePath& path) {
// the returned image.
gfx::Image* CreateHSLShiftedImage(const gfx::Image& image,
const color_utils::HSL& hsl_shift) {
std::vector<const SkBitmap*> bitmaps;
for (size_t i = 0; i < image.GetNumberOfSkBitmaps(); ++i) {
const SkBitmap* bitmap = image.GetSkBitmapAtIndex(i);
bitmaps.push_back(new SkBitmap(SkBitmapOperations::CreateHSLShiftedBitmap(
*bitmap, hsl_shift)));
const std::vector<const SkBitmap*>& src_bitmaps =
image.ToImageSkia()->bitmaps();
std::vector<const SkBitmap*> dst_bitmaps;
for (size_t i = 0; i < src_bitmaps.size(); ++i) {
const SkBitmap* bitmap = src_bitmaps[i];
dst_bitmaps.push_back(new SkBitmap(
SkBitmapOperations::CreateHSLShiftedBitmap(*bitmap, hsl_shift)));
}
return new gfx::Image(bitmaps);
return new gfx::Image(dst_bitmaps);
}
} // namespace
......@@ -988,10 +991,12 @@ void BrowserThemePack::GenerateTabBackgroundImages(ImageCache* bitmaps) const {
ImageCache::const_iterator it = bitmaps->find(prs_base_id);
if (it != bitmaps->end()) {
const gfx::Image& image_to_tint = *(it->second);
const std::vector<const SkBitmap*>& bitmaps_to_tint =
image_to_tint.ToImageSkia()->bitmaps();
std::vector<const SkBitmap*> tinted_bitmaps;
for (size_t j = 0; j < image_to_tint.GetNumberOfSkBitmaps(); ++j) {
for (size_t j = 0; j < bitmaps_to_tint.size(); ++j) {
SkBitmap bg_tint = SkBitmapOperations::CreateHSLShiftedBitmap(
*image_to_tint.GetSkBitmapAtIndex(j), GetTintInternal(
*bitmaps_to_tint[j], GetTintInternal(
ThemeService::TINT_BACKGROUND_TAB));
int vertical_offset = bitmaps->count(prs_id)
? kRestoredTabVerticalOffset : 0;
......
......@@ -22,6 +22,7 @@
#include "skia/ext/skia_utils_mac.h"
#include "third_party/icon_family/IconFamily.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/gfx/image/image_skia.h"
namespace {
......@@ -197,9 +198,10 @@ bool WebAppShortcutCreator::UpdateIcon(const FilePath& app_path) const {
scoped_nsobject<IconFamily> icon_family([[IconFamily alloc] init]);
bool image_added = false;
for (size_t i = 0; i < info_.favicon.GetNumberOfSkBitmaps(); ++i) {
NSBitmapImageRep* image_rep =
SkBitmapToImageRep(*info_.favicon.GetSkBitmapAtIndex(i));
const std::vector<const SkBitmap*>& bitmaps =
info_.favicon.ToImageSkia()->bitmaps();
for (size_t i = 0; i < bitmaps.size(); ++i) {
NSBitmapImageRep* image_rep = SkBitmapToImageRep(*bitmaps[i]);
if (!image_rep)
continue;
......
......@@ -278,6 +278,18 @@ void Canvas::DrawBitmapInt(const SkBitmap& bitmap,
int dest_x, int dest_y, int dest_w, int dest_h,
bool filter,
const SkPaint& paint) {
DrawBitmapFloat(bitmap, static_cast<float>(src_x), static_cast<float>(src_y),
static_cast<float>(src_w), static_cast<float>(src_h),
static_cast<float>(dest_x), static_cast<float>(dest_y),
static_cast<float>(dest_w), static_cast<float>(dest_h),
filter, paint);
}
void Canvas::DrawBitmapFloat(const SkBitmap& bitmap,
float src_x, float src_y, float src_w, float src_h,
float dest_x, float dest_y, float dest_w, float dest_h,
bool filter,
const SkPaint& paint) {
DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() &&
src_y + src_h < std::numeric_limits<int16_t>::max());
if (src_w <= 0 || src_h <= 0) {
......@@ -288,10 +300,10 @@ void Canvas::DrawBitmapInt(const SkBitmap& bitmap,
if (!IntersectsClipRectInt(dest_x, dest_y, dest_w, dest_h))
return;
SkRect dest_rect = { SkIntToScalar(dest_x),
SkIntToScalar(dest_y),
SkIntToScalar(dest_x + dest_w),
SkIntToScalar(dest_y + dest_h) };
SkRect dest_rect = { SkFloatToScalar(dest_x),
SkFloatToScalar(dest_y),
SkFloatToScalar(dest_x + dest_w),
SkFloatToScalar(dest_y + dest_h) };
if (src_w == dest_w && src_h == dest_h) {
// Workaround for apparent bug in Skia that causes image to occasionally
......@@ -309,10 +321,10 @@ void Canvas::DrawBitmapInt(const SkBitmap& bitmap,
SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode);
SkMatrix shader_scale;
shader_scale.setScale(SkFloatToScalar(static_cast<float>(dest_w) / src_w),
SkFloatToScalar(static_cast<float>(dest_h) / src_h));
shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y));
shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y));
shader_scale.setScale(SkFloatToScalar(dest_w / src_w),
SkFloatToScalar(dest_h / src_h));
shader_scale.preTranslate(SkFloatToScalar(-src_x), SkFloatToScalar(-src_y));
shader_scale.postTranslate(SkFloatToScalar(dest_x), SkFloatToScalar(dest_y));
shader->setLocalMatrix(shader_scale);
// Set up our paint to use the shader & release our reference (now just owned
......
......@@ -273,6 +273,14 @@ class UI_EXPORT Canvas {
bool filter,
const SkPaint& paint);
// TODO(pkotwicz): make this function private once gfx::ImageSkia stops
// calling this method.
void DrawBitmapFloat(const SkBitmap& bitmap,
float src_x, float src_y, float src_w, float src_h,
float dest_x, float dest_y, float dest_w, float dest_h,
bool filter,
const SkPaint& paint);
// Draws text with the specified color, font and location. The text is
// aligned to the left, vertically centered, clipped to the region. If the
// text is too big, it is truncated and '...' is added to the end.
......
......@@ -7,8 +7,9 @@
#include <algorithm>
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/memory/scoped_ptr.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/size.h"
#if defined(TOOLKIT_GTK)
......@@ -96,31 +97,18 @@ class ImageRep {
class ImageRepSkia : public ImageRep {
public:
explicit ImageRepSkia(const SkBitmap* bitmap)
: ImageRep(Image::kImageRepSkia) {
CHECK(bitmap);
// TODO(rohitrao): Add a CHECK to ensure that !bitmap->isNull().
bitmaps_.push_back(bitmap);
}
explicit ImageRepSkia(const std::vector<const SkBitmap*>& bitmaps)
explicit ImageRepSkia(ImageSkia* image)
: ImageRep(Image::kImageRepSkia),
bitmaps_(bitmaps) {
CHECK(!bitmaps_.empty());
// TODO(rohitrao): Add a CHECK to ensure that !bitmap->isNull() for each
// vector element.
image_(image) {
}
virtual ~ImageRepSkia() {
STLDeleteElements(&bitmaps_);
}
const SkBitmap* bitmap() const { return bitmaps_[0]; }
const std::vector<const SkBitmap*>& bitmaps() const { return bitmaps_; }
ImageSkia* image() { return image_.get(); }
private:
std::vector<const SkBitmap*> bitmaps_;
scoped_ptr<ImageSkia> image_;
DISALLOW_COPY_AND_ASSIGN(ImageRepSkia);
};
......@@ -238,20 +226,22 @@ Image::Image() {
Image::Image(const SkBitmap* bitmap)
: storage_(new internal::ImageStorage(Image::kImageRepSkia)) {
internal::ImageRepSkia* rep = new internal::ImageRepSkia(bitmap);
internal::ImageRepSkia* rep = new internal::ImageRepSkia(
new ImageSkia(bitmap));
AddRepresentation(rep);
}
Image::Image(const SkBitmap& bitmap)
: storage_(new internal::ImageStorage(Image::kImageRepSkia)) {
internal::ImageRepSkia* rep =
new internal::ImageRepSkia(new SkBitmap(bitmap));
new internal::ImageRepSkia(new ImageSkia(new SkBitmap(bitmap)));
AddRepresentation(rep);
}
Image::Image(const std::vector<const SkBitmap*>& bitmaps)
: storage_(new internal::ImageStorage(Image::kImageRepSkia)) {
internal::ImageRepSkia* rep = new internal::ImageRepSkia(bitmaps);
internal::ImageRepSkia* rep = new internal::ImageRepSkia(
new ImageSkia(bitmaps));
AddRepresentation(rep);
}
......@@ -284,7 +274,12 @@ Image::~Image() {
const SkBitmap* Image::ToSkBitmap() const {
internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia);
return rep->AsImageRepSkia()->bitmap();
return rep->AsImageRepSkia()->image()->bitmaps()[0];
}
const ImageSkia* Image::ToImageSkia() const {
internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia);
return rep->AsImageRepSkia()->image();
}
#if defined(TOOLKIT_GTK)
......@@ -382,8 +377,8 @@ internal::ImageRep* Image::GetRepresentation(
#if defined(TOOLKIT_GTK)
if (storage_->default_representation_type() == Image::kImageRepGdk) {
internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk();
rep = new internal::ImageRepSkia(
internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf()));
rep = new internal::ImageRepSkia(new ImageSkia(
internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf())));
}
// We don't do conversions from CairoCachedSurfaces to Skia because the
// data lives on the display server and we'll always have a GdkPixbuf if we
......@@ -393,7 +388,7 @@ internal::ImageRep* Image::GetRepresentation(
internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa();
std::vector<const SkBitmap*> bitmaps;
CHECK(internal::NSImageToSkBitmaps(nsimage_rep->image(), &bitmaps));
rep = new internal::ImageRepSkia(bitmaps);
rep = new internal::ImageRepSkia(new ImageSkia(bitmaps));
}
#endif
CHECK(rep);
......@@ -422,13 +417,13 @@ internal::ImageRep* Image::GetRepresentation(
#elif defined(TOOLKIT_GTK)
if (rep_type == Image::kImageRepGdk) {
GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(
default_rep->AsImageRepSkia()->bitmap());
default_rep->AsImageRepSkia()->image()->bitmaps()[0]);
native_rep = new internal::ImageRepGdk(pixbuf);
}
#elif defined(OS_MACOSX)
if (rep_type == Image::kImageRepCocoa) {
NSImage* image = gfx::SkBitmapsToNSImage(
default_rep->AsImageRepSkia()->bitmaps());
default_rep->AsImageRepSkia()->image()->bitmaps());
base::mac::NSObjectRetain(image);
native_rep = new internal::ImageRepCocoa(image);
}
......@@ -447,14 +442,4 @@ void Image::AddRepresentation(internal::ImageRep* rep) const {
storage_->representations().insert(std::make_pair(rep->type(), rep));
}
size_t Image::GetNumberOfSkBitmaps() const {
return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()->
bitmaps().size();
}
const SkBitmap* Image::GetSkBitmapAtIndex(size_t index) const {
return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()->
bitmaps()[index];
}
} // namespace gfx
......@@ -37,6 +37,7 @@ class ImageMacTest;
}
namespace gfx {
class ImageSkia;
#if defined(TOOLKIT_GTK)
class CairoCachedSurface;
......@@ -97,6 +98,7 @@ class UI_EXPORT Image {
// The returned result is a weak pointer owned by and scoped to the life of
// the Image.
const SkBitmap* ToSkBitmap() const;
const ImageSkia* ToImageSkia() const;
#if defined(TOOLKIT_GTK)
GdkPixbuf* ToGdkPixbuf() const;
CairoCachedSurface* const ToCairo() const;
......@@ -124,16 +126,6 @@ class UI_EXPORT Image {
#endif
// ---------------------------------------------------------------------------
// Gets the number of bitmaps in this image. This may cause a conversion
// to a bitmap representation. Note, this function and GetSkBitmapAtIndex()
// are primarily meant to be used by the theme provider.
size_t GetNumberOfSkBitmaps() const;
// Gets the bitmap at the given index. This may cause a conversion
// to a bitmap representation. Note, the internal ordering of bitmaps is not
// guaranteed.
const SkBitmap* GetSkBitmapAtIndex(size_t index) const;
// Inspects the representations map to see if the given type exists.
bool HasRepresentation(RepresentationType type) const;
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
......@@ -9,6 +9,7 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_unittest_util.h"
namespace {
......@@ -48,11 +49,12 @@ TEST_F(ImageMacTest, MultiResolutionNSImageToSkBitmap) {
gfx::Image image(ns_image.release());
EXPECT_EQ(1u, image.RepresentationCount());
EXPECT_EQ(2u, image.GetNumberOfSkBitmaps());
const std::vector<const SkBitmap*>& bitmaps = image.ToImageSkia()->bitmaps();
EXPECT_EQ(2u, bitmaps.size());
const SkBitmap* bitmap1 = image.GetSkBitmapAtIndex(0);
const SkBitmap* bitmap1 = bitmaps[0];
EXPECT_TRUE(bitmap1);
const SkBitmap* bitmap2 = image.GetSkBitmapAtIndex(1);
const SkBitmap* bitmap2 = bitmaps[1];
EXPECT_TRUE(bitmap2);
if (bitmap1->width() == width1) {
......@@ -66,8 +68,7 @@ TEST_F(ImageMacTest, MultiResolutionNSImageToSkBitmap) {
EXPECT_EQ(bitmap2->height(), height1);
}
// GetNumberOfSkBitmaps and GetSkBitmapAtIndex should create a second
// representation.
// ToImageSkia should create a second representation.
EXPECT_EQ(2u, image.RepresentationCount());
}
......@@ -83,7 +84,7 @@ TEST_F(ImageMacTest, MultiResolutionSkBitmapToNSImage) {
gfx::Image image(bitmaps);
EXPECT_EQ(1u, image.RepresentationCount());
EXPECT_EQ(2u, image.GetNumberOfSkBitmaps());
EXPECT_EQ(2u, image.ToImageSkia()->bitmaps().size());
NSImage* ns_image = image;
EXPECT_TRUE(ns_image);
......
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/image/image_skia.h"
#include <limits>
#include <cmath>
#include "base/logging.h"
#include "base/stl_util.h"
namespace gfx {
ImageSkia::ImageSkia(const SkBitmap* bitmap)
: size_(bitmap->width(), bitmap->height()),
mip_map_build_pending_(false) {
CHECK(bitmap);
// TODO(pkotwicz): Add a CHECK to ensure that !bitmap->isNull()
bitmaps_.push_back(bitmap);
}
ImageSkia::ImageSkia(const std::vector<const SkBitmap*>& bitmaps)
: bitmaps_(bitmaps),
mip_map_build_pending_(false) {
CHECK(!bitmaps_.empty());
// TODO(pkotwicz): Add a CHECK to ensure that !bitmap->isNull() for each
// vector element.
// Assume that the smallest bitmap represents 1x scale factor.
for (size_t i = 0; i < bitmaps_.size(); ++i) {
gfx::Size bitmap_size(bitmaps_[i]->width(), bitmaps_[i]->height());
if (size_.IsEmpty() || bitmap_size.GetArea() < size_.GetArea())
size_ = bitmap_size;
}
}
ImageSkia::~ImageSkia() {
STLDeleteElements(&bitmaps_);
}
void ImageSkia::BuildMipMap() {
mip_map_build_pending_ = true;
}
void ImageSkia::DrawToCanvasInt(gfx::Canvas* canvas, int x, int y) {
SkPaint p;
DrawToCanvasInt(canvas, x, y, p);
}
void ImageSkia::DrawToCanvasInt(gfx::Canvas* canvas,
int x, int y,
const SkPaint& paint) {
if (IsZeroSized())
return;
SkMatrix m = canvas->sk_canvas()->getTotalMatrix();
float scale_x = std::abs(SkScalarToFloat(m.getScaleX()));
float scale_y = std::abs(SkScalarToFloat(m.getScaleY()));
const SkBitmap* bitmap = GetBitmapForScale(scale_x, scale_y);
if (mip_map_build_pending_) {
const_cast<SkBitmap*>(bitmap)->buildMipMap();
mip_map_build_pending_ = false;
}
float bitmap_scale_x = static_cast<float>(bitmap->width()) / width();
float bitmap_scale_y = static_cast<float>(bitmap->height()) / height();
canvas->Save();
canvas->sk_canvas()->scale(1.0f / bitmap_scale_x,
1.0f / bitmap_scale_y);
canvas->sk_canvas()->drawBitmap(*bitmap, SkFloatToScalar(x * bitmap_scale_x),
SkFloatToScalar(y * bitmap_scale_y));
canvas->Restore();
}
void ImageSkia::DrawToCanvasInt(gfx::Canvas* canvas,
int src_x, int src_y, int src_w, int src_h,
int dest_x, int dest_y, int dest_w, int dest_h,
bool filter) {
SkPaint p;
DrawToCanvasInt(canvas, src_x, src_y, src_w, src_h, dest_x, dest_y,
dest_w, dest_h, filter, p);
}
void ImageSkia::DrawToCanvasInt(gfx::Canvas* canvas,
int src_x, int src_y, int src_w, int src_h,
int dest_x, int dest_y, int dest_w, int dest_h,
bool filter,
const SkPaint& paint) {
if (IsZeroSized())
return;
SkMatrix m = canvas->sk_canvas()->getTotalMatrix();
float scale_x = std::abs(SkScalarToFloat(m.getScaleX()));
float scale_y = std::abs(SkScalarToFloat(m.getScaleY()));
const SkBitmap* bitmap = GetBitmapForScale(scale_x, scale_y);
if (mip_map_build_pending_) {
const_cast<SkBitmap*>(bitmap)->buildMipMap();
mip_map_build_pending_ = false;
}
float bitmap_scale_x = static_cast<float>(bitmap->width()) / width();
float bitmap_scale_y = static_cast<float>(bitmap->height()) / height();
canvas->Save();
canvas->sk_canvas()->scale(1.0f / bitmap_scale_x,
1.0f / bitmap_scale_y);
canvas->DrawBitmapFloat(*bitmap,
src_x * bitmap_scale_x, src_y * bitmap_scale_x,
src_w * bitmap_scale_x, src_h * bitmap_scale_y,
dest_x * bitmap_scale_x, dest_y * bitmap_scale_y,
dest_w * bitmap_scale_x, dest_h * bitmap_scale_y,
filter, paint);
canvas->Restore();
}
const SkBitmap* ImageSkia::GetBitmapForScale(float x_scale_factor,
float y_scale_factor) const {
// Get the desired bitmap width and height given |x_scale_factor|,
// |y_scale_factor| and |size_| at 1x density.
float desired_width = size_.width() * x_scale_factor;
float desired_height = size_.height() * y_scale_factor;
size_t closest_index = 0;
float smallest_diff = std::numeric_limits<float>::max();
for (size_t i = 0; i < bitmaps_.size(); ++i) {
if (bitmaps_[i]->isNull())
continue;
float diff = std::abs(bitmaps_[i]->width() - desired_width) +
std::abs(bitmaps_[i]->height() - desired_height);
if (diff < smallest_diff) {
closest_index = i;
smallest_diff = diff;
}
}
return bitmaps_[closest_index];
}
} // namespace gfx
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_GFX_IMAGE_IMAGE_SKIA_H_
#define UI_GFX_IMAGE_IMAGE_SKIA_H_
#pragma once
#include <vector>
#include "base/basictypes.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/size.h"
namespace gfx {
// Container for the same image at different densities, similar to NSImage.
// Smallest image is assumed to represent 1x density.
// ImageSkia should be used for caching and drawing images of different
// densities. It should not be used as an SkBitmap wrapper.
class UI_EXPORT ImageSkia {
public:
explicit ImageSkia(const SkBitmap* bitmap);
explicit ImageSkia(const std::vector<const SkBitmap*>& bitmaps);
~ImageSkia();
// Build mipmap at time of next call to |DrawToCanvasInt|.
void BuildMipMap();
// Draws the image with the origin at the specified location. The upper left
// corner of the image is rendered at the specified location.
void DrawToCanvasInt(Canvas* canvas, int x, int y);
// Draws the image with the origin at the specified location, using the
// specified paint. The upper left corner of the image is rendered at the
// specified location.
void DrawToCanvasInt(Canvas* canvas,
int x, int y,
const SkPaint& paint);
// Draws a portion of the image in the specified location. The src parameters
// correspond to the region of the image to draw in the region defined
// by the dest coordinates.
//
// If the width or height of the source differs from that of the destination,
// the image will be scaled. When scaling down, it is highly recommended
// that you call BuildMipMap() on your image to ensure that it has
// a mipmap, which will result in much higher-quality output. Set |filter| to
// use filtering for bitmaps, otherwise the nearest-neighbor algorithm is used
// for resampling.
//
// An optional custom SkPaint can be provided.
void DrawToCanvasInt(Canvas* canvas,
int src_x, int src_y, int src_w, int src_h,
int dest_x, int dest_y, int dest_w, int dest_h,
bool filter);
void DrawToCanvasInt(Canvas* canvas,
int src_x, int src_y, int src_w, int src_h,
int dest_x, int dest_y, int dest_w, int dest_h,
bool filter,
const SkPaint& paint);
// Returns true if |size_| is empty.
bool IsZeroSized() const { return size_.IsEmpty(); }
// Width and height of image in DIP coordinate system.
int width() const { return size_.width(); }
int height() const { return size_.height(); }
// Returns a vector with the SkBitmaps contained in this object.
const std::vector<const SkBitmap*>& bitmaps() const { return bitmaps_; }
private:
// Returns the bitmap whose density best matches |x_scale_factor| and
// |y_scale_factor|.
const SkBitmap* GetBitmapForScale(float x_scale_factor,
float y_scale_factor) const;
std::vector<const SkBitmap*> bitmaps_;
gfx::Size size_;
bool mip_map_build_pending_;
DISALLOW_COPY_AND_ASSIGN(ImageSkia);
};
} // namespace gfx
#endif // UI_GFX_IMAGE_IMAGE_SKIA_H_
......@@ -6,6 +6,7 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_unittest_util.h"
#if defined(TOOLKIT_GTK)
......@@ -262,11 +263,13 @@ TEST_F(ImageTest, MultiResolutionSkBitmap) {
gfx::Image image(bitmaps);
EXPECT_EQ(1u, image.RepresentationCount());
EXPECT_EQ(2u, image.GetNumberOfSkBitmaps());
const std::vector<const SkBitmap*>& image_bitmaps =
image.ToImageSkia()->bitmaps();
EXPECT_EQ(2u, image_bitmaps.size());
const SkBitmap* bitmap1 = image.GetSkBitmapAtIndex(0);
const SkBitmap* bitmap1 = image_bitmaps[0];
EXPECT_TRUE(bitmap1);
const SkBitmap* bitmap2 = image.GetSkBitmapAtIndex(1);
const SkBitmap* bitmap2 = image_bitmaps[1];
EXPECT_TRUE(bitmap2);
if (bitmap1->width() == width1) {
......@@ -282,7 +285,7 @@ TEST_F(ImageTest, MultiResolutionSkBitmap) {
// Sanity check.
EXPECT_EQ(1u, image.RepresentationCount());
EXPECT_EQ(2u, image.GetNumberOfSkBitmaps());
EXPECT_EQ(2u, image.ToImageSkia()->bitmaps().size());
}
// Integration tests with UI toolkit frameworks require linking against the
......
......@@ -315,6 +315,8 @@
'gfx/image/image.cc',
'gfx/image/image.h',
'gfx/image/image_mac.mm',
'gfx/image/image_skia.cc',
'gfx/image/image_skia.h',
'gfx/image/image_util.cc',
'gfx/image/image_util.h',
'gfx/insets.cc',
......
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