OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/remoting/remoting_interstitial_ui.h" | |
6 | |
7 #include <algorithm> // for std::max() | |
8 | |
9 #include "media/base/media_resources.h" | |
10 #include "media/base/video_frame.h" | |
11 #include "media/base/video_renderer_sink.h" | |
12 #include "media/base/video_util.h" | |
13 #include "skia/ext/image_operations.h" | |
14 #include "third_party/skia/include/core/SkCanvas.h" | |
15 #include "third_party/skia/include/core/SkTypeface.h" | |
16 #include "third_party/skia/include/effects/SkBlurImageFilter.h" | |
17 #include "ui/gfx/color_palette.h" | |
18 #include "ui/gfx/geometry/size.h" | |
19 #include "ui/gfx/paint_vector_icon.h" | |
20 #include "ui/gfx/skbitmap_operations.h" | |
21 #include "ui/gfx/vector_icons_public.h" | |
22 | |
23 namespace media { | |
24 | |
25 namespace { | |
26 | |
27 // The interstitial frame size when |background_image| is empty or has low | |
28 // resolution. The frame height may be adjusted according to the aspect ratio of | |
29 // |natural_size|. | |
30 constexpr int kDefaultFrameWidth = 1280; | |
31 constexpr int kDefaultFrameHeight = 720; | |
32 | |
33 SkBitmap ResizeImage(const SkBitmap& image, const gfx::Size& scaled_size) { | |
34 DCHECK(!scaled_size.IsEmpty()); | |
35 | |
36 if (image.width() == scaled_size.width() && | |
37 image.height() == scaled_size.height()) | |
38 return image; | |
39 | |
40 return skia::ImageOperations::Resize( | |
41 image, skia::ImageOperations::RESIZE_BEST, scaled_size.width(), | |
42 scaled_size.height()); | |
43 } | |
44 | |
45 void RenderCastMessage(const gfx::Size& canvas_size, | |
46 RemotingInterstitialType type, | |
47 SkCanvas* canvas) { | |
48 DCHECK(canvas); | |
49 if (type == RemotingInterstitialType::BETWEEN_SESSIONS) | |
50 return; | |
51 | |
52 // Blur the background image. | |
53 SkScalar sigma = SkDoubleToScalar(10); | |
54 SkPaint paint_blur; | |
55 paint_blur.setImageFilter(SkBlurImageFilter::Make(sigma, sigma, nullptr)); | |
56 canvas->saveLayer(0, &paint_blur); | |
57 canvas->restore(); | |
58 | |
59 // Create SkPaint for text and icon bitmap. | |
60 // After |paint| draws, the new canvas should look like this: | |
61 // _________________________________ | |
62 // | | | |
63 // | | | |
64 // | [ ] | | |
65 // | Casting Video... | | |
66 // | | | |
67 // |________________________________| | |
68 // | |
69 // Both text and icon are centered horizontally. Together, they are | |
70 // centered vertically. | |
71 SkPaint paint; | |
72 int text_size = SkIntToScalar(canvas_size.height() / 18); | |
73 paint.setAntiAlias(true); | |
74 paint.setFilterQuality(kHigh_SkFilterQuality); | |
75 paint.setColor(SK_ColorLTGRAY); | |
76 paint.setTypeface(SkTypeface::MakeFromName( | |
77 "sans", SkFontStyle::FromOldStyle(SkTypeface::kNormal))); | |
78 paint.setTextSize(text_size); | |
79 | |
80 // Draw the appropriate text. | |
81 const std::string remote_playback_message = | |
82 (type == RemotingInterstitialType::IN_SESSION | |
83 ? GetLocalizedStringUTF8(MEDIA_REMOTING_CASTING_VIDEO_TEXT) | |
84 : GetLocalizedStringUTF8(MEDIA_REMOTING_CAST_ERROR_TEXT)); | |
85 size_t display_text_width = paint.measureText(remote_playback_message.data(), | |
86 remote_playback_message.size()); | |
87 SkScalar sk_text_offset_x = (canvas_size.width() - display_text_width) / 2.0; | |
88 SkScalar sk_text_offset_y = (canvas_size.height() / 2.0) + text_size; | |
89 canvas->drawText(remote_playback_message.data(), | |
90 remote_playback_message.size(), sk_text_offset_x, | |
91 sk_text_offset_y, paint); | |
92 | |
93 // Draw the appropriate Cast icon. | |
94 gfx::VectorIconId current_icon = | |
95 (type == RemotingInterstitialType::IN_SESSION | |
96 ? gfx::VectorIconId::MEDIA_ROUTER_ACTIVE | |
97 : gfx::VectorIconId::MEDIA_ROUTER_WARNING); | |
98 gfx::ImageSkia icon_image = gfx::CreateVectorIcon( | |
99 current_icon, canvas_size.height() / 6, SK_ColorLTGRAY); | |
100 const SkBitmap* icon_bitmap = icon_image.bitmap(); | |
101 SkScalar sk_image_offset_x = (canvas_size.width() - icon_image.width()) / 2.0; | |
102 SkScalar sk_image_offset_y = | |
103 (canvas_size.height() / 2.0) - icon_image.height(); | |
104 canvas->drawBitmap(*icon_bitmap, sk_image_offset_x, sk_image_offset_y, | |
105 &paint); | |
106 } | |
107 | |
108 gfx::Size GetCanvasSize(const gfx::Size& image_size, | |
109 const gfx::Size& natural_size) { | |
110 int width = std::max(image_size.width(), kDefaultFrameWidth) & ~1; | |
111 base::CheckedNumeric<int> height = width; | |
112 height *= natural_size.height(); | |
113 height /= natural_size.width(); | |
114 height &= ~1; | |
115 gfx::Size result = gfx::Size(width, height.ValueOrDefault(0)); | |
116 return result.IsEmpty() ? gfx::Size(kDefaultFrameWidth, kDefaultFrameHeight) | |
117 : result; | |
118 } | |
119 | |
120 scoped_refptr<VideoFrame> RenderInterstitialFrame( | |
121 const SkBitmap& image, | |
122 const gfx::Size& natural_size, | |
123 RemotingInterstitialType type) { | |
124 gfx::Size canvas_size = | |
125 GetCanvasSize(gfx::Size(image.width(), image.height()), natural_size); | |
126 SkBitmap canvas_bitmap; | |
127 canvas_bitmap.allocN32Pixels(canvas_size.width(), canvas_size.height()); | |
128 canvas_bitmap.eraseColor(SK_ColorBLACK); | |
129 SkCanvas canvas(canvas_bitmap); | |
130 | |
131 // Draw background image on the canvas. | |
132 if (!image.drawsNothing()) { | |
133 gfx::Rect centered_rect = ComputeLetterboxRegion( | |
134 gfx::Rect(canvas_size), gfx::Size(image.width(), image.height())); | |
135 SkBitmap processed_image = ResizeImage(image, centered_rect.size()); | |
136 if (type != RemotingInterstitialType::BETWEEN_SESSIONS) { | |
137 color_utils::HSL shift = {-1, 0, 0.2}; // Make monochromatic. | |
138 processed_image = | |
139 SkBitmapOperations::CreateHSLShiftedBitmap(processed_image, shift); | |
140 } | |
141 canvas.writePixels(processed_image, centered_rect.x(), centered_rect.y()); | |
142 } | |
143 | |
144 RenderCastMessage(canvas_size, type, &canvas); | |
145 | |
146 // Create a new VideoFrame, copy the bitmap, then return it. | |
147 scoped_refptr<media::VideoFrame> video_frame = media::VideoFrame::CreateFrame( | |
148 media::PIXEL_FORMAT_I420, canvas_size, gfx::Rect(canvas_size), | |
149 canvas_size, base::TimeDelta()); | |
150 canvas_bitmap.lockPixels(); | |
151 media::CopyRGBToVideoFrame( | |
152 reinterpret_cast<uint8_t*>(canvas_bitmap.getPixels()), | |
153 canvas_bitmap.rowBytes(), | |
154 gfx::Rect(canvas_size.width(), canvas_size.height()), video_frame.get()); | |
155 canvas_bitmap.unlockPixels(); | |
156 return video_frame; | |
157 } | |
158 | |
159 } // namespace | |
160 | |
161 void PaintRemotingInterstitial(const SkBitmap& image, | |
162 const gfx::Size& natural_size, | |
163 RemotingInterstitialType interstitial_type, | |
164 VideoRendererSink* video_renderer_sink) { | |
165 if (!video_renderer_sink) | |
166 return; | |
167 | |
168 const scoped_refptr<VideoFrame> interstitial = | |
169 RenderInterstitialFrame(image, natural_size, interstitial_type); | |
170 | |
171 video_renderer_sink->PaintSingleFrame(interstitial); | |
172 } | |
173 | |
174 } // namespace media | |
OLD | NEW |