summaryrefslogtreecommitdiffstats
path: root/ui
diff options
context:
space:
mode:
authorxiyuan@chromium.org <xiyuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-26 00:29:16 +0000
committerxiyuan@chromium.org <xiyuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-26 00:29:16 +0000
commit58e068f0d606b2440721e6ee4607355d4778a03d (patch)
treeca03311375b387189e5f89b55410e55562bb78d8 /ui
parent230aad77e35f61fe1843f60a3631425687b6423a (diff)
downloadchromium_src-58e068f0d606b2440721e6ee4607355d4778a03d.zip
chromium_src-58e068f0d606b2440721e6ee4607355d4778a03d.tar.gz
chromium_src-58e068f0d606b2440721e6ee4607355d4778a03d.tar.bz2
Clean up SkBitmapOperations.
- Remove CreateResizedBitmap since there is a better skia::ImageOperations::Resize; - Update CreateDropShadow to use ShadowValue for shadow definition; BUG=None. TEST=None. All should work as before this change. R=asvitkine@chromium.org Review URL: https://chromiumcodereview.appspot.com/10453035 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@139162 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r--ui/app_list/app_list_item_view.cc39
-rw-r--r--ui/gfx/shadow_value.cc4
-rw-r--r--ui/gfx/shadow_value.h11
-rw-r--r--ui/gfx/shadow_value_unittest.cc8
-rw-r--r--ui/gfx/skbitmap_operations.cc67
-rw-r--r--ui/gfx/skbitmap_operations.h22
6 files changed, 55 insertions, 96 deletions
diff --git a/ui/app_list/app_list_item_view.cc b/ui/app_list/app_list_item_view.cc
index f3e6803..f2f0906 100644
--- a/ui/app_list/app_list_item_view.cc
+++ b/ui/app_list/app_list_item_view.cc
@@ -11,6 +11,7 @@
#include "base/synchronization/cancellation_flag.h"
#include "base/threading/worker_pool.h"
#include "base/utf_string_conversions.h"
+#include "skia/ext/image_operations.h"
#include "ui/app_list/app_list_item_model.h"
#include "ui/app_list/apps_grid_view.h"
#include "ui/app_list/drop_shadow_label.h"
@@ -126,39 +127,27 @@ class AppListItemView::IconOperation
static const int kShadowPadding = 15;
void ResizeAndGenerateShadow() {
- // If you change shadow radius and shadow offset, please also update
- // kShadowPaddingAbove.
- const SkColor kShadowColor[] = {
- SkColorSetARGB(0xCC, 0, 0, 0),
- SkColorSetARGB(0x33, 0, 0, 0),
- SkColorSetARGB(0x4C, 0, 0, 0),
- };
- const gfx::Point kShadowOffset[] = {
- gfx::Point(0, 0),
- gfx::Point(0, 4),
- gfx::Point(0, 5),
- };
- const SkScalar kShadowRadius[] = {
- SkIntToScalar(2),
- SkIntToScalar(4),
- SkIntToScalar(10),
- };
-
if (cancel_flag_.IsSet())
return;
- if (size_ != gfx::Size(bitmap_.width(), bitmap_.height()))
- bitmap_ = SkBitmapOperations::CreateResizedBitmap(bitmap_, size_);
+ if (size_ != gfx::Size(bitmap_.width(), bitmap_.height())) {
+ bitmap_ = skia::ImageOperations::Resize(bitmap_,
+ skia::ImageOperations::RESIZE_BEST, size_.width(), size_.height());
+ }
if (cancel_flag_.IsSet())
return;
+ // If you change shadow radius and shadow offset, please also update
+ // kShadowPaddingAbove.
+ const gfx::ShadowValue kShadows[] = {
+ gfx::ShadowValue(gfx::Point(0, 0), 2, SkColorSetARGB(0xCC, 0, 0, 0)),
+ gfx::ShadowValue(gfx::Point(0, 4), 4, SkColorSetARGB(0x33, 0, 0, 0)),
+ gfx::ShadowValue(gfx::Point(0, 5), 10, SkColorSetARGB(0x4C, 0, 0, 0)),
+ };
+
bitmap_ = SkBitmapOperations::CreateDropShadow(
- bitmap_,
- arraysize(kShadowColor),
- kShadowColor,
- kShadowOffset,
- kShadowRadius);
+ bitmap_, gfx::ShadowValues(kShadows, kShadows + arraysize(kShadows)));
}
void Cancel() {
diff --git a/ui/gfx/shadow_value.cc b/ui/gfx/shadow_value.cc
index 4df5013..c8915e2 100644
--- a/ui/gfx/shadow_value.cc
+++ b/ui/gfx/shadow_value.cc
@@ -4,6 +4,8 @@
#include "ui/gfx/shadow_value.h"
+#include <algorithm>
+
#include "base/stringprintf.h"
#include "ui/gfx/insets.h"
@@ -37,7 +39,7 @@ std::string ShadowValue::ToString() const {
}
// static
-Insets ShadowValue::GetMargin(const std::vector<ShadowValue>& shadows) {
+Insets ShadowValue::GetMargin(const ShadowValues& shadows) {
int left = 0;
int top = 0;
int right = 0;
diff --git a/ui/gfx/shadow_value.h b/ui/gfx/shadow_value.h
index 8ca9196..06f5026 100644
--- a/ui/gfx/shadow_value.h
+++ b/ui/gfx/shadow_value.h
@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef UI_GFX_SHADOW_VALUE_
-#define UI_GFX_SHADOW_VALUE_
+#ifndef UI_GFX_SHADOW_VALUE_H_
+#define UI_GFX_SHADOW_VALUE_H_
#pragma once
#include <string>
@@ -17,6 +17,9 @@ namespace gfx {
class Insets;
+class ShadowValue;
+typedef std::vector<ShadowValue> ShadowValues;
+
// ShadowValue encapsulates parameters needed to define a shadow, including the
// shadow's offset, blur amount and color.
class UI_EXPORT ShadowValue {
@@ -35,7 +38,7 @@ class UI_EXPORT ShadowValue {
// Gets margin space needed for shadows. Note that values in returned Insets
// are negative because shadow margins are outside a boundary.
- static Insets GetMargin(const std::vector<ShadowValue>& shadows);
+ static Insets GetMargin(const ShadowValues& shadows);
private:
gfx::Point offset_;
@@ -55,4 +58,4 @@ class UI_EXPORT ShadowValue {
} // namespace gfx
-#endif // UI_GFX_SHADOW_VALUE_
+#endif // UI_GFX_SHADOW_VALUE_H_
diff --git a/ui/gfx/shadow_value_unittest.cc b/ui/gfx/shadow_value_unittest.cc
index 8c30b2b..57515dc 100644
--- a/ui/gfx/shadow_value_unittest.cc
+++ b/ui/gfx/shadow_value_unittest.cc
@@ -52,11 +52,9 @@ TEST(ShadowValueTest, GetMargin) {
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
- std::vector<ShadowValue> shadows(
- &kTestCases[i].shadows[0],
- &kTestCases[i].shadows[kTestCases[i].shadow_count]);
-
- Insets margin = ShadowValue::GetMargin(shadows);
+ Insets margin = ShadowValue::GetMargin(
+ ShadowValues(kTestCases[i].shadows,
+ kTestCases[i].shadows + kTestCases[i].shadow_count));
EXPECT_EQ(kTestCases[i].expected_margin, margin) << " i=" << i;
}
diff --git a/ui/gfx/skbitmap_operations.cc b/ui/gfx/skbitmap_operations.cc
index 96d5275..2cc053f 100644
--- a/ui/gfx/skbitmap_operations.cc
+++ b/ui/gfx/skbitmap_operations.cc
@@ -14,6 +14,7 @@
#include "third_party/skia/include/core/SkColorPriv.h"
#include "third_party/skia/include/core/SkUnPreMultiply.h"
#include "third_party/skia/include/effects/SkBlurImageFilter.h"
+#include "ui/gfx/insets.h"
#include "ui/gfx/point.h"
#include "ui/gfx/size.h"
@@ -743,30 +744,6 @@ SkBitmap SkBitmapOperations::CreateTransposedBtmap(const SkBitmap& image) {
}
// static
-SkBitmap SkBitmapOperations::CreateResizedBitmap(const SkBitmap& bitmap,
- const gfx::Size& size) {
- DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config);
-
- SkBitmap src = bitmap;
- src.buildMipMap(false);
-
- SkBitmap resized;
- resized.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height());
- resized.allocPixels();
- resized.eraseARGB(0, 0, 0, 0);
-
- SkCanvas canvas(resized);
-
- SkIRect src_rect = SkIRect::MakeWH(src.width(), src.height());
- SkRect dst_rect = SkRect::MakeWH(size.width(), size.height());
-
- SkPaint paint;
- paint.setFilterBitmap(true);
- canvas.drawBitmapRect(src, &src_rect, dst_rect, &paint);
- return resized;
-}
-
-// static
SkBitmap SkBitmapOperations::CreateColorMask(const SkBitmap& bitmap,
SkColor c) {
DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config);
@@ -788,43 +765,41 @@ SkBitmap SkBitmapOperations::CreateColorMask(const SkBitmap& bitmap,
}
// static
-SkBitmap SkBitmapOperations::CreateDropShadow(const SkBitmap& bitmap,
- int shadow_count,
- const SkColor* shadow_color,
- const gfx::Point* shadow_offset,
- const SkScalar* shadow_radius) {
+SkBitmap SkBitmapOperations::CreateDropShadow(
+ const SkBitmap& bitmap,
+ const gfx::ShadowValues& shadows) {
DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config);
- int padding = 0;
- for (int i = 0; i < shadow_count; ++i) {
- int shadow_space = std::max(abs(shadow_offset[i].x()),
- abs(shadow_offset[i].y())) + shadow_radius[i];
- if (shadow_space > padding)
- padding = shadow_space;
- }
+ // Shadow margin insets are negative values because they grow outside.
+ // Negate them here as grow direction is not important and only pixel value
+ // is of interest here.
+ gfx::Insets shadow_margin = -gfx::ShadowValue::GetMargin(shadows);
SkBitmap image_with_shadow;
image_with_shadow.setConfig(SkBitmap::kARGB_8888_Config,
- bitmap.width() + 2 * padding,
- bitmap.height() + 2 * padding);
+ bitmap.width() + shadow_margin.width(),
+ bitmap.height() + shadow_margin.height());
image_with_shadow.allocPixels();
image_with_shadow.eraseARGB(0, 0, 0, 0);
SkCanvas canvas(image_with_shadow);
- canvas.translate(SkIntToScalar(padding), SkIntToScalar(padding));
+ canvas.translate(SkIntToScalar(shadow_margin.left()),
+ SkIntToScalar(shadow_margin.top()));
SkPaint paint;
- for (int i = 0; i < shadow_count; ++i) {
- SkBitmap shadow = SkBitmapOperations::CreateColorMask(bitmap,
- shadow_color[i]);
+ for (size_t i = 0; i < shadows.size(); ++i) {
+ const gfx::ShadowValue& shadow = shadows[i];
+ SkBitmap shadow_image = SkBitmapOperations::CreateColorMask(bitmap,
+ shadow.color());
paint.setImageFilter(
- new SkBlurImageFilter(shadow_radius[i], shadow_radius[i]))->unref();
+ new SkBlurImageFilter(SkDoubleToScalar(shadow.blur()),
+ SkDoubleToScalar(shadow.blur())))->unref();
canvas.saveLayer(0, &paint);
- canvas.drawBitmap(shadow,
- SkIntToScalar(shadow_offset[i].x()),
- SkIntToScalar(shadow_offset[i].y()));
+ canvas.drawBitmap(shadow_image,
+ SkIntToScalar(shadow.x()),
+ SkIntToScalar(shadow.y()));
canvas.restore();
}
diff --git a/ui/gfx/skbitmap_operations.h b/ui/gfx/skbitmap_operations.h
index fc0a9da4..da05f66 100644
--- a/ui/gfx/skbitmap_operations.h
+++ b/ui/gfx/skbitmap_operations.h
@@ -9,6 +9,7 @@
#include "base/gtest_prod_util.h"
#include "ui/base/ui_export.h"
#include "ui/gfx/color_utils.h"
+#include "ui/gfx/shadow_value.h"
namespace gfx {
class Point;
@@ -98,26 +99,17 @@ class UI_EXPORT SkBitmapOperations {
// Transpose the pixels in |bitmap| by swapping x and y.
static SkBitmap CreateTransposedBtmap(const SkBitmap& bitmap);
- // Create a copy of |bitmap| with specified |size|. The image must use the
- // kARGB_8888_Config config.
- static SkBitmap CreateResizedBitmap(const SkBitmap& bitmap,
- const gfx::Size& size);
-
// Create a bitmap by combining alpha channel of |bitmap| and color |c|.
// The image must use the kARGB_8888_Config config.
static SkBitmap CreateColorMask(const SkBitmap& bitmap, SkColor c);
- // Create a bitmap with drop shadow added to |bitmap|. |shadow_count| is the
- // number of shadows to add. |shadow_offset| and |shadow_radius| are arrays
- // with |shadow_count| elements to provide definition for each shadow. The
- // created bitmap would be padded to have enough space for shadows and have
- // original bitmap in the center. The image must use the kARGB_8888_Config
- // config.
+ // Create a bitmap with drop shadow added to |bitmap|. |shadows| defines
+ // the shadows to add. The created bitmap would be padded to have enough space
+ // for shadows and have original bitmap in the center. The image must use the
+ // kARGB_8888_Config config.
static SkBitmap CreateDropShadow(const SkBitmap& bitmap,
- int shadow_count,
- const SkColor* shadow_color,
- const gfx::Point* shadow_offset,
- const SkScalar* shadow_radius);
+ const gfx::ShadowValues& shadows);
+
private:
SkBitmapOperations(); // Class for scoping only.