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