Skip to content

Commit

Permalink
Clean up SkBitmapOperations.
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
xiyuan@chromium.org committed May 26, 2012
1 parent 230aad7 commit 58e068f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 117 deletions.
30 changes: 9 additions & 21 deletions ash/launcher/launcher_button.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "ash/launcher/launcher_button_host.h"
#include "grit/ui_resources.h"
#include "skia/ext/image_operations.h"
#include "ui/base/accessibility/accessible_view_state.h"
#include "ui/base/animation/animation_delegate.h"
#include "ui/base/animation/throb_animation.h"
Expand All @@ -16,6 +17,7 @@
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/shadow_value.h"
#include "ui/gfx/skbitmap_operations.h"
#include "ui/views/controls/image_view.h"

Expand Down Expand Up @@ -123,28 +125,14 @@ LauncherButton::~LauncherButton() {
}

void LauncherButton::SetShadowedImage(const SkBitmap& bitmap) {
const SkColor kShadowColor[] = {
SkColorSetARGB(0x1A, 0, 0, 0),
SkColorSetARGB(0x1A, 0, 0, 0),
SkColorSetARGB(0x54, 0, 0, 0),
};
const gfx::Point kShadowOffset[] = {
gfx::Point(0, 2),
gfx::Point(0, 3),
gfx::Point(0, 0),
};
const SkScalar kShadowRadius[] = {
SkIntToScalar(0),
SkIntToScalar(1),
SkIntToScalar(1),
const gfx::ShadowValue kShadows[] = {
gfx::ShadowValue(gfx::Point(0, 2), 0, SkColorSetARGB(0x1A, 0, 0, 0)),
gfx::ShadowValue(gfx::Point(0, 3), 1, SkColorSetARGB(0x1A, 0, 0, 0)),
gfx::ShadowValue(gfx::Point(0, 0), 1, SkColorSetARGB(0x54, 0, 0, 0)),
};

SkBitmap shadowed_bitmap = SkBitmapOperations::CreateDropShadow(
bitmap,
arraysize(kShadowColor) - 1,
kShadowColor,
kShadowOffset,
kShadowRadius);
bitmap, gfx::ShadowValues(kShadows, kShadows + arraysize(kShadows)));
icon_view_->SetImage(shadowed_bitmap);
}

Expand Down Expand Up @@ -176,8 +164,8 @@ void LauncherButton::SetImage(const SkBitmap& image) {
return;
}

SkBitmap resized_image = SkBitmapOperations::CreateResizedBitmap(
image, gfx::Size(width, height));
SkBitmap resized_image = skia::ImageOperations::Resize(
image, skia::ImageOperations::RESIZE_BEST, width, height);
SetShadowedImage(resized_image);
}

Expand Down
39 changes: 14 additions & 25 deletions ui/app_list/app_list_item_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 3 additions & 1 deletion ui/gfx/shadow_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "ui/gfx/shadow_value.h"

#include <algorithm>

#include "base/stringprintf.h"
#include "ui/gfx/insets.h"

Expand Down Expand Up @@ -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;
Expand Down
11 changes: 7 additions & 4 deletions ui/gfx/shadow_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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 {
Expand All @@ -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_;
Expand All @@ -55,4 +58,4 @@ class UI_EXPORT ShadowValue {

} // namespace gfx

#endif // UI_GFX_SHADOW_VALUE_
#endif // UI_GFX_SHADOW_VALUE_H_
8 changes: 3 additions & 5 deletions ui/gfx/shadow_value_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
67 changes: 21 additions & 46 deletions ui/gfx/skbitmap_operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -742,30 +743,6 @@ SkBitmap SkBitmapOperations::CreateTransposedBtmap(const SkBitmap& image) {
return transposed;
}

// 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) {
Expand All @@ -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();
}

Expand Down
22 changes: 7 additions & 15 deletions ui/gfx/skbitmap_operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.

Expand Down

0 comments on commit 58e068f

Please sign in to comment.