Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(198)

Side by Side Diff: third_party/WebKit/Source/platform/graphics/PlaceholderImage.cpp

Issue 2915353002: Stylize PlaceholderImages with icons. (Closed)
Patch Set: Scale down a larger image to improve appearance on high DPI devices. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/geometry/IntPoint.h"
9 #include "platform/geometry/IntRect.h"
10 #include "platform/graphics/BitmapImage.h"
8 #include "platform/graphics/Color.h" 11 #include "platform/graphics/Color.h"
12 #include "platform/graphics/GraphicsContext.h"
9 #include "platform/graphics/ImageObserver.h" 13 #include "platform/graphics/ImageObserver.h"
14 #include "platform/graphics/paint/PaintCanvas.h"
15 #include "platform/graphics/paint/PaintFlags.h"
10 #include "platform/graphics/paint/PaintRecord.h" 16 #include "platform/graphics/paint/PaintRecord.h"
11 #include "platform/graphics/paint/PaintRecorder.h" 17 #include "platform/graphics/paint/PaintRecorder.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
kinuko 2017/06/05 06:30:13 Can you add a link to the 'spec' that explain thes
sclittle 2017/06/05 22:26:08 Done.
57 PaintFlags flags(base_flags); 60 PaintFlags flags(base_flags);
58 flags.setStyle(PaintFlags::kFill_Style); 61 flags.setStyle(PaintFlags::kFill_Style);
59 flags.setColor(kFillColor); 62 flags.setColor(0x80D9D9D9);
vmpstr 2017/06/05 18:05:28 nit: You can use SkColorSetARGB or SkColorSetARGBM
sclittle 2017/06/05 22:26:08 Done.
60 canvas->drawRect(dest_rect, flags); 63 canvas->drawRect(dest_rect, flags);
64
65 constexpr int kIconWidth = 24;
66 constexpr int kIconHeight = 24;
67 constexpr int kIconPaddingX = 8;
68 constexpr int kIconPaddingY = 5;
f(malita) 2017/06/05 13:04:27 Instead of hard-coding UI constants here, is it fe
sclittle 2017/06/05 22:26:08 Right now, the resource itself is a 4x version of
f(malita) 2017/06/06 14:04:21 Ah, yes, makes sense to have higher res for hidpi.
69
70 if (dest_rect.Width() < kIconWidth + 2 * kIconPaddingX ||
71 dest_rect.Height() < kIconHeight + 2 * kIconPaddingY) {
72 return;
73 }
74
75 DEFINE_STATIC_REF(Image, icon_image,
76 (Image::LoadPlatformResource("placeholderIcon")));
77 DCHECK(!icon_image->IsNull());
78
79 FloatRect icon_dest_rect(
80 dest_rect.X() + (dest_rect.Width() - kIconWidth) / 2.0f,
81 dest_rect.Y() + (dest_rect.Height() - kIconHeight) / 2.0f, kIconWidth,
82 kIconHeight);
83
84 // Note that the |icon_image| is not scaled according to dest_rect / src_rect,
85 // and is always drawn at the same size. This is so that placeholder icons are
86 // visible (e.g. when replacing a large image that's scaled down to a small
87 // area) and so that all placeholder images on the same page look consistent.
88 icon_image->Draw(canvas, base_flags, icon_dest_rect,
f(malita) 2017/06/05 13:04:27 Do we want/need the placeholder to observe Respect
sclittle 2017/06/05 22:26:08 We probably don't want it to observe the enum, nic
f(malita) 2017/06/06 14:04:21 You can use PaintCanvas::drawImageRect() to scale.
sclittle 2017/06/07 19:43:54 Done.
89 IntRect(IntPoint::Zero(), icon_image->Size()),
90 respect_orientation, image_clamping_mode);
91 }
92
93 void PlaceholderImage::DrawPattern(GraphicsContext& context,
94 const FloatRect& src_rect,
95 const FloatSize& scale,
96 const FloatPoint& phase,
97 SkBlendMode mode,
98 const FloatRect& dest_rect,
99 const FloatSize& repeat_spacing) {
100 DCHECK(context.Canvas());
101
102 PaintFlags flags = context.FillFlags();
103 flags.setBlendMode(mode);
104
105 // Ignore the pattern specifications and just draw a single placeholder image
106 // over the whole |dest_rect|. This is done in order to prevent repeated icons
107 // from cluttering tiled background images.
108 Draw(context.Canvas(), flags, dest_rect, src_rect,
109 kDoNotRespectImageOrientation, kClampImageToSourceRect);
61 } 110 }
62 111
63 void PlaceholderImage::DestroyDecodedData() { 112 void PlaceholderImage::DestroyDecodedData() {
64 image_for_current_frame_.reset(); 113 image_for_current_frame_.reset();
65 } 114 }
66 115
67 } // namespace blink 116 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/graphics/PlaceholderImage.h ('k') | third_party/WebKit/public/blink_image_resources.grd » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698