| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/image/image_skia_operations.h" | 5 #include "ui/gfx/image/image_skia_operations.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "skia/ext/image_operations.h" | 9 #include "skia/ext/image_operations.h" |
| 10 #include "skia/ext/platform_canvas.h" | |
| 11 #include "ui/base/layout.h" | 10 #include "ui/base/layout.h" |
| 12 #include "ui/gfx/canvas.h" | 11 #include "ui/gfx/canvas.h" |
| 13 #include "ui/gfx/image/canvas_image_source.h" | 12 #include "ui/gfx/image/canvas_image_source.h" |
| 14 #include "ui/gfx/image/image_skia.h" | 13 #include "ui/gfx/image/image_skia.h" |
| 15 #include "ui/gfx/image/image_skia_rep.h" | 14 #include "ui/gfx/image/image_skia_rep.h" |
| 16 #include "ui/gfx/image/image_skia_source.h" | 15 #include "ui/gfx/image/image_skia_source.h" |
| 17 #include "ui/gfx/insets.h" | 16 #include "ui/gfx/insets.h" |
| 18 #include "ui/gfx/rect.h" | 17 #include "ui/gfx/rect.h" |
| 19 #include "ui/gfx/rect_conversions.h" | 18 #include "ui/gfx/rect_conversions.h" |
| 20 #include "ui/gfx/size.h" | 19 #include "ui/gfx/size.h" |
| 21 #include "ui/gfx/size_conversions.h" | 20 #include "ui/gfx/size_conversions.h" |
| 22 #include "ui/gfx/skbitmap_operations.h" | 21 #include "ui/gfx/skbitmap_operations.h" |
| 23 #include "ui/gfx/skia_util.h" | 22 #include "ui/gfx/skia_util.h" |
| 24 #include "ui/gfx/switches.h" | 23 #include "ui/gfx/switches.h" |
| 25 | 24 |
| 26 namespace gfx { | 25 namespace gfx { |
| 27 namespace { | 26 namespace { |
| 28 | 27 |
| 29 bool ScalingEnabled() { | 28 bool ScalingEnabled() { |
| 30 static bool scale_images = !CommandLine::ForCurrentProcess()->HasSwitch( | 29 static bool scale_images = !CommandLine::ForCurrentProcess()->HasSwitch( |
| 31 switches::kDisableScalingInImageSkiaOperations); | 30 switches::kDisableScalingInImageSkiaOperations); |
| 32 return scale_images; | 31 return scale_images; |
| 33 } | 32 } |
| 34 | 33 |
| 35 // Creates 2x scaled image of the give |source|. | 34 // Creates 2x scaled image of the give |source|. |
| 36 ImageSkiaRep Create2XImageSkiaRep(const ImageSkiaRep& source) { | 35 ImageSkiaRep Create2XImageSkiaRep(const ImageSkiaRep& source) { |
| 37 gfx::Size size(source.GetWidth() * 2.0f, source.GetHeight() * 2.0f); | 36 gfx::Size size(source.GetWidth() * 2.0f, source.GetHeight() * 2.0f); |
| 38 skia::PlatformCanvas canvas(size.width(), size.height(), false); | 37 |
| 38 SkBitmap resized_bitmap; |
| 39 resized_bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width(), |
| 40 size.height()); |
| 41 if (!resized_bitmap.allocPixels()) |
| 42 SK_CRASH(); |
| 43 SkCanvas canvas(resized_bitmap); |
| 39 SkRect resized_bounds = RectToSkRect(gfx::Rect(size)); | 44 SkRect resized_bounds = RectToSkRect(gfx::Rect(size)); |
| 40 canvas.drawBitmapRect(source.sk_bitmap(), NULL, resized_bounds); | 45 canvas.drawBitmapRect(source.sk_bitmap(), NULL, resized_bounds); |
| 41 SkBitmap resized_bitmap = canvas.getDevice()->accessBitmap(false); | |
| 42 return ImageSkiaRep(resized_bitmap, ui::SCALE_FACTOR_200P); | 46 return ImageSkiaRep(resized_bitmap, ui::SCALE_FACTOR_200P); |
| 43 } | 47 } |
| 44 | 48 |
| 45 // A utility function to synchronize the scale factor of the two images. | 49 // A utility function to synchronize the scale factor of the two images. |
| 46 // When the command line option "--disable-scaling-in-image-skia-operation" | 50 // When the command line option "--disable-scaling-in-image-skia-operation" |
| 47 // is provided, this function will fail if the scale factors of the two images | 51 // is provided, this function will fail if the scale factors of the two images |
| 48 // are different. This assumes that the platform only supports | 52 // are different. This assumes that the platform only supports |
| 49 // 1x and 2x scale factors. | 53 // 1x and 2x scale factors. |
| 50 // TODO(oshima): Remove and replace this with plain CHECK once | 54 // TODO(oshima): Remove and replace this with plain CHECK once |
| 51 // 2x images for all resources are provided. | 55 // 2x images for all resources are provided. |
| (...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 const ImageSkia& source, | 458 const ImageSkia& source, |
| 455 const ShadowValues& shadows) { | 459 const ShadowValues& shadows) { |
| 456 const gfx::Insets shadow_padding = -gfx::ShadowValue::GetMargin(shadows); | 460 const gfx::Insets shadow_padding = -gfx::ShadowValue::GetMargin(shadows); |
| 457 gfx::Size shadow_image_size = source.size(); | 461 gfx::Size shadow_image_size = source.size(); |
| 458 shadow_image_size.Enlarge(shadow_padding.width(), | 462 shadow_image_size.Enlarge(shadow_padding.width(), |
| 459 shadow_padding.height()); | 463 shadow_padding.height()); |
| 460 return ImageSkia(new DropShadowSource(source, shadows), shadow_image_size); | 464 return ImageSkia(new DropShadowSource(source, shadows), shadow_image_size); |
| 461 } | 465 } |
| 462 | 466 |
| 463 } // namespace gfx | 467 } // namespace gfx |
| OLD | NEW |