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