Chromium Code Reviews| 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 "ui/base/layout.h" | 10 #include "ui/base/layout.h" |
| 11 #include "ui/gfx/canvas.h" | 11 #include "ui/gfx/canvas.h" |
| 12 #include "ui/gfx/image/canvas_image_source.h" | 12 #include "ui/gfx/image/canvas_image_source.h" |
| 13 #include "ui/gfx/image/image_skia.h" | 13 #include "ui/gfx/image/image_skia.h" |
| 14 #include "ui/gfx/image/image_skia_rep.h" | 14 #include "ui/gfx/image/image_skia_rep.h" |
| 15 #include "ui/gfx/image/image_skia_source.h" | 15 #include "ui/gfx/image/image_skia_source.h" |
| 16 #include "ui/gfx/insets.h" | 16 #include "ui/gfx/insets.h" |
| 17 #include "ui/gfx/point.h" | |
| 18 #include "ui/gfx/point_conversions.h" | |
| 17 #include "ui/gfx/rect.h" | 19 #include "ui/gfx/rect.h" |
| 18 #include "ui/gfx/rect_conversions.h" | 20 #include "ui/gfx/rect_conversions.h" |
| 19 #include "ui/gfx/size.h" | 21 #include "ui/gfx/size.h" |
| 20 #include "ui/gfx/size_conversions.h" | 22 #include "ui/gfx/size_conversions.h" |
| 21 #include "ui/gfx/skbitmap_operations.h" | 23 #include "ui/gfx/skbitmap_operations.h" |
| 22 #include "ui/gfx/skia_util.h" | 24 #include "ui/gfx/skia_util.h" |
| 23 | 25 |
| 24 namespace gfx { | 26 namespace gfx { |
| 25 namespace { | 27 namespace { |
| 26 | 28 |
| 29 gfx::Size DIPToPixelSize(gfx::Size dip_size, float scale) { | |
| 30 return ToCeiledSize(ScaleSize(dip_size, scale)); | |
| 31 } | |
| 32 | |
| 33 gfx::Rect DIPToPixelBounds(gfx::Rect dip_bounds, float scale) { | |
| 34 return gfx::Rect(ToFlooredPoint(ScalePoint(dip_bounds.origin(), scale)), | |
| 35 DIPToPixelSize(dip_bounds.size(), scale)); | |
| 36 } | |
| 37 | |
| 38 ImageSkiaRep CropImageSkiaRep(ImageSkiaRep& source, | |
| 39 gfx::Rect& bounds_in_pixel) { | |
|
oshima
2013/03/14 18:12:20
Did you define this intentionally? Or can be in Ex
oshima
2013/03/14 19:51:17
you probably missed this comment.
kevers
2013/03/14 20:18:28
Oops..I did miss it. It was pulled out since it w
kevers
2013/03/14 20:41:42
Move back to ExtractSubsetImageSource.
| |
| 40 SkBitmap result; | |
| 41 bool success = source.sk_bitmap().extractSubset(&result, | |
| 42 RectToSkIRect(bounds_in_pixel)); | |
| 43 DCHECK(success); | |
| 44 return ImageSkiaRep(result, source.scale_factor()); | |
| 45 } | |
| 46 | |
| 27 // Returns an image rep for the ImageSkiaSource to return to visually indicate | 47 // Returns an image rep for the ImageSkiaSource to return to visually indicate |
| 28 // an error. | 48 // an error. |
| 29 ImageSkiaRep GetErrorImageRep(ui::ScaleFactor scale_factor, | 49 ImageSkiaRep GetErrorImageRep(ui::ScaleFactor scale_factor, |
| 30 const gfx::Size& pixel_size) { | 50 const gfx::Size& pixel_size) { |
| 31 SkBitmap bitmap; | 51 SkBitmap bitmap; |
| 32 bitmap.setConfig( | 52 bitmap.setConfig( |
| 33 SkBitmap::kARGB_8888_Config, pixel_size.width(), pixel_size.height()); | 53 SkBitmap::kARGB_8888_Config, pixel_size.width(), pixel_size.height()); |
| 34 bitmap.allocPixels(); | 54 bitmap.allocPixels(); |
| 35 bitmap.eraseColor(SK_ColorRED); | 55 bitmap.eraseColor(SK_ColorRED); |
| 36 return gfx::ImageSkiaRep(bitmap, scale_factor); | 56 return gfx::ImageSkiaRep(bitmap, scale_factor); |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 dst_h_(dst_h) { | 229 dst_h_(dst_h) { |
| 210 } | 230 } |
| 211 | 231 |
| 212 virtual ~TiledImageSource() { | 232 virtual ~TiledImageSource() { |
| 213 } | 233 } |
| 214 | 234 |
| 215 // gfx::ImageSkiaSource overrides: | 235 // gfx::ImageSkiaSource overrides: |
| 216 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | 236 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 217 ImageSkiaRep source_rep = source_.GetRepresentation(scale_factor); | 237 ImageSkiaRep source_rep = source_.GetRepresentation(scale_factor); |
| 218 float scale = ui::GetScaleFactorScale(source_rep.scale_factor()); | 238 float scale = ui::GetScaleFactorScale(source_rep.scale_factor()); |
| 239 gfx::Rect bounds = DIPToPixelBounds(gfx::Rect(src_x_, src_y_, dst_w_, | |
| 240 dst_h_), scale); | |
| 219 return ImageSkiaRep( | 241 return ImageSkiaRep( |
| 220 SkBitmapOperations::CreateTiledBitmap( | 242 SkBitmapOperations::CreateTiledBitmap( |
| 221 source_rep.sk_bitmap(), | 243 source_rep.sk_bitmap(), |
| 222 src_x_ * scale, src_y_ * scale, dst_w_ * scale, dst_h_ * scale), | 244 bounds.x(), bounds.y(), bounds.width(), bounds.height()), |
| 223 source_rep.scale_factor()); | 245 source_rep.scale_factor()); |
| 224 } | 246 } |
| 225 | 247 |
| 226 private: | 248 private: |
| 227 const ImageSkia source_; | 249 const ImageSkia source_; |
| 228 const int src_x_; | 250 const int src_x_; |
| 229 const int src_y_; | 251 const int src_y_; |
| 230 const int dst_w_; | 252 const int dst_w_; |
| 231 const int dst_h_; | 253 const int dst_h_; |
| 232 | 254 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 253 } | 275 } |
| 254 | 276 |
| 255 private: | 277 private: |
| 256 const gfx::ImageSkia image_; | 278 const gfx::ImageSkia image_; |
| 257 const color_utils::HSL hsl_shift_; | 279 const color_utils::HSL hsl_shift_; |
| 258 DISALLOW_COPY_AND_ASSIGN(HSLImageSource); | 280 DISALLOW_COPY_AND_ASSIGN(HSLImageSource); |
| 259 }; | 281 }; |
| 260 | 282 |
| 261 // ImageSkiaSource which uses SkBitmapOperations::CreateButtonBackground | 283 // ImageSkiaSource which uses SkBitmapOperations::CreateButtonBackground |
| 262 // to generate image reps for the target image. | 284 // to generate image reps for the target image. |
| 263 class ButtonImageSource: public gfx::ImageSkiaSource { | 285 class ButtonImageSource: public BinaryImageSource { |
|
oshima
2013/03/13 22:08:53
I vaguely remember that doing this caused some iss
kevers
2013/03/14 15:49:46
Tested on Linux Chromeos with a forced scale facto
oshima
2013/03/14 18:12:20
I now remember the reason. crbug.com/171725
In sh
kevers
2013/03/14 19:31:39
Reverted ButtonImageSource change and added a comm
| |
| 264 public: | 286 public: |
| 265 ButtonImageSource(SkColor color, | 287 ButtonImageSource(SkColor color, |
| 266 const ImageSkia& image, | 288 const ImageSkia& image, |
| 267 const ImageSkia& mask) | 289 const ImageSkia& mask) |
| 268 : color_(color), | 290 : BinaryImageSource(image, mask, "ButtonImageSource"), |
| 269 image_(image), | 291 color_(color) { |
| 270 mask_(mask) { | |
| 271 } | 292 } |
| 272 | 293 |
| 273 virtual ~ButtonImageSource() { | 294 virtual ~ButtonImageSource() { |
| 274 } | 295 } |
| 275 | 296 |
| 276 // gfx::ImageSkiaSource overrides: | 297 // BinaryImageSource overrides: |
| 277 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | 298 virtual ImageSkiaRep CreateImageSkiaRep( |
| 278 ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor); | 299 const ImageSkiaRep& first_rep, |
| 279 ImageSkiaRep mask_rep = mask_.GetRepresentation(scale_factor); | 300 const ImageSkiaRep& second_rep) const OVERRIDE { |
| 280 if (image_rep.scale_factor() != mask_rep.scale_factor()) { | |
| 281 image_rep = image_.GetRepresentation(ui::SCALE_FACTOR_100P); | |
| 282 mask_rep = mask_.GetRepresentation(ui::SCALE_FACTOR_100P); | |
| 283 } | |
| 284 return gfx::ImageSkiaRep( | 301 return gfx::ImageSkiaRep( |
| 285 SkBitmapOperations::CreateButtonBackground(color_, | 302 SkBitmapOperations::CreateButtonBackground(color_, |
| 286 image_rep.sk_bitmap(), mask_rep.sk_bitmap()), | 303 first_rep.sk_bitmap(), second_rep.sk_bitmap()), |
| 287 image_rep.scale_factor()); | 304 first_rep.scale_factor()); |
| 288 } | 305 } |
| 289 | 306 |
| 290 private: | 307 private: |
| 291 const SkColor color_; | 308 const SkColor color_; |
| 292 const ImageSkia image_; | |
| 293 const ImageSkia mask_; | |
| 294 | 309 |
| 295 DISALLOW_COPY_AND_ASSIGN(ButtonImageSource); | 310 DISALLOW_COPY_AND_ASSIGN(ButtonImageSource); |
| 296 }; | 311 }; |
| 297 | 312 |
| 298 // ImageSkiaSource which uses SkBitmap::extractSubset to generate image reps | 313 // ImageSkiaSource which uses SkBitmap::extractSubset to generate image reps |
| 299 // for the target image. | 314 // for the target image. |
| 300 class ExtractSubsetImageSource: public gfx::ImageSkiaSource { | 315 class ExtractSubsetImageSource: public gfx::ImageSkiaSource { |
| 301 public: | 316 public: |
| 302 ExtractSubsetImageSource(const gfx::ImageSkia& image, | 317 ExtractSubsetImageSource(const gfx::ImageSkia& image, |
| 303 const gfx::Rect& subset_bounds) | 318 const gfx::Rect& subset_bounds) |
| 304 : image_(image), | 319 : image_(image), |
| 305 subset_bounds_(subset_bounds) { | 320 subset_bounds_(subset_bounds) { |
| 306 } | 321 } |
| 307 | 322 |
| 308 virtual ~ExtractSubsetImageSource() { | 323 virtual ~ExtractSubsetImageSource() { |
| 309 } | 324 } |
| 310 | 325 |
| 311 // gfx::ImageSkiaSource overrides: | 326 // gfx::ImageSkiaSource overrides: |
| 312 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | 327 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 313 ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor); | 328 ImageSkiaRep image_rep = image_.GetRepresentation(scale_factor); |
| 314 float scale_to_pixel = ui::GetScaleFactorScale(image_rep.scale_factor()); | 329 float scale_to_pixel = ui::GetScaleFactorScale(image_rep.scale_factor()); |
| 315 SkIRect subset_bounds_in_pixel = RectToSkIRect(ToFlooredRectDeprecated( | 330 Rect subset_bounds_in_pixel = DIPToPixelBounds(subset_bounds_, |
| 316 gfx::ScaleRect(subset_bounds_, scale_to_pixel))); | 331 scale_to_pixel); |
| 317 SkBitmap dst; | 332 return CropImageSkiaRep(image_rep, subset_bounds_in_pixel); |
| 318 bool success = image_rep.sk_bitmap().extractSubset(&dst, | |
| 319 subset_bounds_in_pixel); | |
| 320 DCHECK(success); | |
| 321 return gfx::ImageSkiaRep(dst, image_rep.scale_factor()); | |
| 322 } | 333 } |
| 323 | 334 |
| 324 private: | 335 private: |
| 325 const gfx::ImageSkia image_; | 336 const gfx::ImageSkia image_; |
| 326 const gfx::Rect subset_bounds_; | 337 const gfx::Rect subset_bounds_; |
| 327 | 338 |
| 328 DISALLOW_COPY_AND_ASSIGN(ExtractSubsetImageSource); | 339 DISALLOW_COPY_AND_ASSIGN(ExtractSubsetImageSource); |
| 329 }; | 340 }; |
| 330 | 341 |
| 331 // ResizeSource resizes relevant image reps in |source| to |target_dip_size| | 342 // ResizeSource resizes relevant image reps in |source| to |target_dip_size| |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 342 virtual ~ResizeSource() {} | 353 virtual ~ResizeSource() {} |
| 343 | 354 |
| 344 // gfx::ImageSkiaSource overrides: | 355 // gfx::ImageSkiaSource overrides: |
| 345 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { | 356 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 346 const ImageSkiaRep& image_rep = source_.GetRepresentation(scale_factor); | 357 const ImageSkiaRep& image_rep = source_.GetRepresentation(scale_factor); |
| 347 if (image_rep.GetWidth() == target_dip_size_.width() && | 358 if (image_rep.GetWidth() == target_dip_size_.width() && |
| 348 image_rep.GetHeight() == target_dip_size_.height()) | 359 image_rep.GetHeight() == target_dip_size_.height()) |
| 349 return image_rep; | 360 return image_rep; |
| 350 | 361 |
| 351 const float scale = ui::GetScaleFactorScale(scale_factor); | 362 const float scale = ui::GetScaleFactorScale(scale_factor); |
| 352 const Size target_pixel_size = gfx::ToFlooredSize( | 363 const Size target_pixel_size = DIPToPixelSize(target_dip_size_, scale); |
| 353 gfx::ScaleSize(target_dip_size_, scale)); | |
| 354 const SkBitmap resized = skia::ImageOperations::Resize( | 364 const SkBitmap resized = skia::ImageOperations::Resize( |
| 355 image_rep.sk_bitmap(), | 365 image_rep.sk_bitmap(), |
| 356 resize_method_, | 366 resize_method_, |
| 357 target_pixel_size.width(), | 367 target_pixel_size.width(), |
| 358 target_pixel_size.height()); | 368 target_pixel_size.height()); |
| 359 return ImageSkiaRep(resized, scale_factor); | 369 return ImageSkiaRep(resized, scale_factor); |
| 360 } | 370 } |
| 361 | 371 |
| 362 private: | 372 private: |
| 363 const ImageSkia source_; | 373 const ImageSkia source_; |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 545 return ImageSkia(); | 555 return ImageSkia(); |
| 546 | 556 |
| 547 return ImageSkia(new RotatedSource(source, rotation), | 557 return ImageSkia(new RotatedSource(source, rotation), |
| 548 SkBitmapOperations::ROTATION_180_CW == rotation ? | 558 SkBitmapOperations::ROTATION_180_CW == rotation ? |
| 549 source.size() : | 559 source.size() : |
| 550 gfx::Size(source.height(), source.width())); | 560 gfx::Size(source.height(), source.width())); |
| 551 | 561 |
| 552 } | 562 } |
| 553 | 563 |
| 554 } // namespace gfx | 564 } // namespace gfx |
| OLD | NEW |