Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "platform/graphics/PlaceholderImage.h" | 5 #include "platform/graphics/PlaceholderImage.h" |
| 6 | 6 |
| 7 #include "platform/geometry/FloatRect.h" | 7 #include "platform/geometry/FloatRect.h" |
| 8 #include "platform/graphics/Color.h" | 8 #include "platform/geometry/IntPoint.h" |
| 9 #include "platform/geometry/IntRect.h" | |
| 10 #include "platform/graphics/BitmapImage.h" | |
| 11 #include "platform/graphics/GraphicsContext.h" | |
| 9 #include "platform/graphics/ImageObserver.h" | 12 #include "platform/graphics/ImageObserver.h" |
| 13 #include "platform/graphics/paint/PaintCanvas.h" | |
| 14 #include "platform/graphics/paint/PaintFlags.h" | |
| 10 #include "platform/graphics/paint/PaintRecord.h" | 15 #include "platform/graphics/paint/PaintRecord.h" |
| 11 #include "platform/graphics/paint/PaintRecorder.h" | 16 #include "platform/graphics/paint/PaintRecorder.h" |
| 17 #include "platform/wtf/StdLibExtras.h" | |
| 12 #include "third_party/skia/include/core/SkColor.h" | 18 #include "third_party/skia/include/core/SkColor.h" |
| 13 #include "third_party/skia/include/core/SkRect.h" | 19 #include "third_party/skia/include/core/SkRect.h" |
| 14 #include "third_party/skia/include/core/SkSize.h" | 20 #include "third_party/skia/include/core/SkSize.h" |
| 15 | 21 |
| 16 namespace blink { | 22 namespace blink { |
| 17 | 23 |
| 18 namespace { | 24 PlaceholderImage::PlaceholderImage(ImageObserver* observer, const IntSize& size) |
| 19 | 25 : Image(observer), size_(size) {} |
| 20 // Gray with 40% opacity. | |
| 21 const RGBA32 kFillColor = 0x66808080; | |
| 22 | |
| 23 } // namespace | |
| 24 | 26 |
| 25 PlaceholderImage::~PlaceholderImage() {} | 27 PlaceholderImage::~PlaceholderImage() {} |
| 26 | 28 |
| 27 sk_sp<SkImage> PlaceholderImage::ImageForCurrentFrame() { | 29 sk_sp<SkImage> PlaceholderImage::ImageForCurrentFrame() { |
| 28 if (image_for_current_frame_) | 30 if (image_for_current_frame_) |
| 29 return image_for_current_frame_; | 31 return image_for_current_frame_; |
| 30 | 32 |
| 31 const FloatRect dest_rect(0.0f, 0.0f, static_cast<float>(size_.Width()), | 33 const FloatRect dest_rect(0.0f, 0.0f, static_cast<float>(size_.Width()), |
| 32 static_cast<float>(size_.Height())); | 34 static_cast<float>(size_.Height())); |
| 35 | |
| 33 PaintRecorder paint_recorder; | 36 PaintRecorder paint_recorder; |
| 34 Draw(paint_recorder.beginRecording(dest_rect), PaintFlags(), dest_rect, | 37 Draw(paint_recorder.beginRecording(dest_rect), PaintFlags(), dest_rect, |
| 35 dest_rect, kDoNotRespectImageOrientation, kClampImageToSourceRect); | 38 dest_rect, kDoNotRespectImageOrientation, kClampImageToSourceRect); |
| 36 | 39 |
| 37 image_for_current_frame_ = SkImage::MakeFromPicture( | 40 image_for_current_frame_ = SkImage::MakeFromPicture( |
| 38 ToSkPicture(paint_recorder.finishRecordingAsPicture(), dest_rect), | 41 ToSkPicture(paint_recorder.finishRecordingAsPicture(), dest_rect), |
| 39 SkISize::Make(size_.Width(), size_.Height()), nullptr, nullptr, | 42 SkISize::Make(size_.Width(), size_.Height()), nullptr, nullptr, |
| 40 SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB()); | 43 SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB()); |
| 41 | 44 |
| 42 return image_for_current_frame_; | 45 return image_for_current_frame_; |
| 43 } | 46 } |
| 44 | 47 |
| 45 void PlaceholderImage::Draw(PaintCanvas* canvas, | 48 void PlaceholderImage::Draw(PaintCanvas* canvas, |
| 46 const PaintFlags& base_flags, | 49 const PaintFlags& base_flags, |
| 47 const FloatRect& dest_rect, | 50 const FloatRect& dest_rect, |
| 48 const FloatRect& src_rect, | 51 const FloatRect& src_rect, |
| 49 RespectImageOrientationEnum, | 52 RespectImageOrientationEnum respect_orientation, |
| 50 ImageClampingMode) { | 53 ImageClampingMode image_clamping_mode) { |
| 51 if (!src_rect.Intersects(FloatRect(0.0f, 0.0f, | 54 if (!src_rect.Intersects(FloatRect(0.0f, 0.0f, |
| 52 static_cast<float>(size_.Width()), | 55 static_cast<float>(size_.Width()), |
| 53 static_cast<float>(size_.Height())))) { | 56 static_cast<float>(size_.Height())))) { |
| 54 return; | 57 return; |
| 55 } | 58 } |
| 56 | 59 |
| 60 // Placeholder image visual specifications: | |
| 61 // https://docs.google.com/document/d/1BHeA1azbgCdZgCnr16VN2g7A9MHPQ_dwKn5szh8 evMQ/edit | |
| 62 | |
| 57 PaintFlags flags(base_flags); | 63 PaintFlags flags(base_flags); |
| 58 flags.setStyle(PaintFlags::kFill_Style); | 64 flags.setStyle(PaintFlags::kFill_Style); |
| 59 flags.setColor(kFillColor); | 65 flags.setColor(SkColorSetARGBMacro(0x80, 0xD9, 0xD9, 0xD9)); |
|
f(malita)
2017/06/06 14:04:21
nit: SkColorSetARGB()
sclittle
2017/06/07 19:43:54
Done.
| |
| 60 canvas->drawRect(dest_rect, flags); | 66 canvas->drawRect(dest_rect, flags); |
| 67 | |
| 68 constexpr int kIconWidth = 24; | |
| 69 constexpr int kIconHeight = 24; | |
| 70 constexpr int kIconPaddingX = 8; | |
| 71 constexpr int kIconPaddingY = 5; | |
| 72 | |
| 73 if (dest_rect.Width() < kIconWidth + 2 * kIconPaddingX || | |
| 74 dest_rect.Height() < kIconHeight + 2 * kIconPaddingY) { | |
| 75 return; | |
| 76 } | |
| 77 | |
| 78 DEFINE_STATIC_REF(Image, icon_image, | |
| 79 (Image::LoadPlatformResource("placeholderIcon"))); | |
| 80 DCHECK(!icon_image->IsNull()); | |
| 81 | |
| 82 FloatRect icon_dest_rect( | |
| 83 dest_rect.X() + (dest_rect.Width() - kIconWidth) / 2.0f, | |
| 84 dest_rect.Y() + (dest_rect.Height() - kIconHeight) / 2.0f, kIconWidth, | |
| 85 kIconHeight); | |
| 86 | |
| 87 // Note that the |icon_image| is not scaled according to dest_rect / src_rect, | |
| 88 // and is always drawn at the same size. This is so that placeholder icons are | |
| 89 // visible (e.g. when replacing a large image that's scaled down to a small | |
| 90 // area) and so that all placeholder images on the same page look consistent. | |
| 91 icon_image->Draw(canvas, base_flags, icon_dest_rect, | |
| 92 IntRect(IntPoint::Zero(), icon_image->Size()), | |
| 93 kDoNotRespectImageOrientation, kClampImageToSourceRect); | |
| 94 } | |
| 95 | |
| 96 void PlaceholderImage::DrawPattern(GraphicsContext& context, | |
| 97 const FloatRect& src_rect, | |
| 98 const FloatSize& scale, | |
| 99 const FloatPoint& phase, | |
| 100 SkBlendMode mode, | |
| 101 const FloatRect& dest_rect, | |
| 102 const FloatSize& repeat_spacing) { | |
| 103 DCHECK(context.Canvas()); | |
| 104 | |
| 105 PaintFlags flags = context.FillFlags(); | |
| 106 flags.setBlendMode(mode); | |
| 107 | |
| 108 // Ignore the pattern specifications and just draw a single placeholder image | |
| 109 // over the whole |dest_rect|. This is done in order to prevent repeated icons | |
| 110 // from cluttering tiled background images. | |
| 111 Draw(context.Canvas(), flags, dest_rect, src_rect, | |
| 112 kDoNotRespectImageOrientation, kClampImageToSourceRect); | |
| 61 } | 113 } |
| 62 | 114 |
| 63 void PlaceholderImage::DestroyDecodedData() { | 115 void PlaceholderImage::DestroyDecodedData() { |
| 64 image_for_current_frame_.reset(); | 116 image_for_current_frame_.reset(); |
| 65 } | 117 } |
| 66 | 118 |
| 67 } // namespace blink | 119 } // namespace blink |
| OLD | NEW |