| 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 constexpr SkScalar kSigma = SkIntToScalar(10); |
| 54 SkPaint paint_blur; | 56 SkPaint paint_blur; |
| 55 paint_blur.setImageFilter(SkBlurImageFilter::Make(sigma, sigma, nullptr)); | 57 paint_blur.setImageFilter(SkBlurImageFilter::Make(kSigma, kSigma, 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. |
| 60 // After |paint| draws, the new canvas should look like this: | 62 // After |paint| draws, the new canvas should look like this: |
| 61 // _________________________________ | 63 // _________________________________ |
| 62 // | | | 64 // | | |
| 63 // | | | 65 // | | |
| 64 // | [ ] | | 66 // | [ ] | |
| 65 // | Casting Video... | | 67 // | Casting Video... | |
| 66 // | | | 68 // | | |
| 67 // |________________________________| | 69 // |________________________________| |
| 68 // | 70 // |
| 69 // Both text and icon are centered horizontally. Together, they are | 71 // Both text and icon are centered horizontally. Together, they are |
| 70 // centered vertically. | 72 // centered vertically. |
| 71 SkPaint paint; | 73 SkPaint paint; |
| 72 int text_size = SkIntToScalar(canvas_size.height() / 18); | |
| 73 paint.setAntiAlias(true); | 74 paint.setAntiAlias(true); |
| 74 paint.setFilterQuality(kHigh_SkFilterQuality); | 75 paint.setFilterQuality(kHigh_SkFilterQuality); |
| 75 paint.setColor(SK_ColorLTGRAY); | 76 paint.setColor(SK_ColorLTGRAY); |
| 76 paint.setTypeface(SkTypeface::MakeFromName( | 77 paint.setTypeface(SkTypeface::MakeFromName( |
| 77 "sans", SkFontStyle::FromOldStyle(SkTypeface::kNormal))); | 78 "sans", SkFontStyle::FromOldStyle(SkTypeface::kNormal))); |
| 78 paint.setTextSize(text_size); | 79 const SkScalar text_height = SkIntToScalar(canvas_size.height() / 18); |
| 80 paint.setTextSize(text_height); |
| 79 | 81 |
| 80 // Draw the appropriate text. | 82 // Draw the appropriate text. |
| 81 const std::string remote_playback_message = | 83 const std::string 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 SkScalar display_text_width = |
| 86 remote_playback_message.size()); | 88 paint.measureText(message.data(), message.size()); |
| 87 SkScalar sk_text_offset_x = (canvas_size.width() - display_text_width) / 2.0; | 89 SkScalar sk_text_offset_x = |
| 88 SkScalar sk_text_offset_y = (canvas_size.height() / 2.0) + text_size; | 90 SkScalarFloorToScalar((canvas_size.width() - display_text_width) / 2.0); |
| 89 canvas->drawText(remote_playback_message.data(), | 91 SkScalar sk_text_offset_y = |
| 90 remote_playback_message.size(), sk_text_offset_x, | 92 SkScalarFloorToScalar((canvas_size.height() / 2.0) + text_height); |
| 93 canvas->drawText(message.data(), message.size(), sk_text_offset_x, |
| 91 sk_text_offset_y, paint); | 94 sk_text_offset_y, paint); |
| 92 | 95 |
| 93 // Draw the appropriate Cast icon. | 96 // Draw the appropriate Cast icon. |
| 94 gfx::VectorIconId current_icon = | 97 gfx::VectorIconId current_icon = |
| 95 (type == RemotingInterstitialType::IN_SESSION | 98 (type == InterstitialType::IN_SESSION |
| 96 ? gfx::VectorIconId::MEDIA_ROUTER_ACTIVE | 99 ? gfx::VectorIconId::MEDIA_ROUTER_ACTIVE |
| 97 : gfx::VectorIconId::MEDIA_ROUTER_WARNING); | 100 : gfx::VectorIconId::MEDIA_ROUTER_WARNING); |
| 98 gfx::ImageSkia icon_image = gfx::CreateVectorIcon( | 101 gfx::ImageSkia icon_image = gfx::CreateVectorIcon( |
| 99 current_icon, canvas_size.height() / 6, SK_ColorLTGRAY); | 102 current_icon, canvas_size.height() / 6, SK_ColorLTGRAY); |
| 100 const SkBitmap* icon_bitmap = icon_image.bitmap(); | 103 const SkBitmap* icon_bitmap = icon_image.bitmap(); |
| 101 SkScalar sk_image_offset_x = (canvas_size.width() - icon_image.width()) / 2.0; | 104 SkScalar sk_image_offset_x = (canvas_size.width() - icon_image.width()) / 2.0; |
| 102 SkScalar sk_image_offset_y = | 105 SkScalar sk_image_offset_y = |
| 103 (canvas_size.height() / 2.0) - icon_image.height(); | 106 (canvas_size.height() / 2.0) - icon_image.height(); |
| 104 canvas->drawBitmap(*icon_bitmap, sk_image_offset_x, sk_image_offset_y, | 107 canvas->drawBitmap(*icon_bitmap, sk_image_offset_x, sk_image_offset_y, |
| 105 &paint); | 108 &paint); |
| 106 } | 109 } |
| 107 | 110 |
| 108 gfx::Size GetCanvasSize(const gfx::Size& image_size, | 111 gfx::Size GetCanvasSize(const gfx::Size& image_size, |
| 109 const gfx::Size& natural_size) { | 112 const gfx::Size& natural_size) { |
| 110 int width = std::max(image_size.width(), kDefaultFrameWidth) & ~1; | 113 int width = std::max(image_size.width(), kDefaultFrameWidth) & ~1; |
| 111 base::CheckedNumeric<int> height = width; | 114 base::CheckedNumeric<int> height = width; |
| 112 height *= natural_size.height(); | 115 height *= natural_size.height(); |
| 113 height /= natural_size.width(); | 116 height /= natural_size.width(); |
| 114 height &= ~1; | 117 height &= ~1; |
| 115 gfx::Size result = gfx::Size(width, height.ValueOrDefault(0)); | 118 gfx::Size result = gfx::Size(width, height.ValueOrDefault(0)); |
| 116 return result.IsEmpty() ? gfx::Size(kDefaultFrameWidth, kDefaultFrameHeight) | 119 return result.IsEmpty() ? gfx::Size(kDefaultFrameWidth, kDefaultFrameHeight) |
| 117 : result; | 120 : result; |
| 118 } | 121 } |
| 119 | 122 |
| 120 scoped_refptr<VideoFrame> RenderInterstitialFrame( | 123 scoped_refptr<VideoFrame> RenderInterstitialFrame(const SkBitmap& image, |
| 121 const SkBitmap& image, | 124 const gfx::Size& natural_size, |
| 122 const gfx::Size& natural_size, | 125 InterstitialType type) { |
| 123 RemotingInterstitialType type) { | |
| 124 gfx::Size canvas_size = | 126 gfx::Size canvas_size = |
| 125 GetCanvasSize(gfx::Size(image.width(), image.height()), natural_size); | 127 GetCanvasSize(gfx::Size(image.width(), image.height()), natural_size); |
| 126 SkBitmap canvas_bitmap; | 128 SkBitmap canvas_bitmap; |
| 127 canvas_bitmap.allocN32Pixels(canvas_size.width(), canvas_size.height()); | 129 canvas_bitmap.allocN32Pixels(canvas_size.width(), canvas_size.height()); |
| 128 canvas_bitmap.eraseColor(SK_ColorBLACK); | 130 canvas_bitmap.eraseColor(SK_ColorBLACK); |
| 129 SkCanvas canvas(canvas_bitmap); | 131 SkCanvas canvas(canvas_bitmap); |
| 130 | 132 |
| 131 // Draw background image on the canvas. | 133 // Draw background image on the canvas. |
| 132 if (!image.drawsNothing()) { | 134 if (!image.drawsNothing()) { |
| 133 gfx::Rect centered_rect = ComputeLetterboxRegion( | 135 gfx::Rect centered_rect = ComputeLetterboxRegion( |
| 134 gfx::Rect(canvas_size), gfx::Size(image.width(), image.height())); | 136 gfx::Rect(canvas_size), gfx::Size(image.width(), image.height())); |
| 135 SkBitmap processed_image = ResizeImage(image, centered_rect.size()); | 137 SkBitmap processed_image = ResizeImage(image, centered_rect.size()); |
| 136 if (type != RemotingInterstitialType::BETWEEN_SESSIONS) { | 138 if (type != InterstitialType::BETWEEN_SESSIONS) { |
| 137 color_utils::HSL shift = {-1, 0, 0.2}; // Make monochromatic. | 139 color_utils::HSL shift = {-1, 0, 0.2}; // Make monochromatic. |
| 138 processed_image = | 140 processed_image = |
| 139 SkBitmapOperations::CreateHSLShiftedBitmap(processed_image, shift); | 141 SkBitmapOperations::CreateHSLShiftedBitmap(processed_image, shift); |
| 140 } | 142 } |
| 141 canvas.writePixels(processed_image, centered_rect.x(), centered_rect.y()); | 143 canvas.writePixels(processed_image, centered_rect.x(), centered_rect.y()); |
| 142 } | 144 } |
| 143 | 145 |
| 144 RenderCastMessage(canvas_size, type, &canvas); | 146 RenderCastMessage(canvas_size, type, &canvas); |
| 145 | 147 |
| 146 // Create a new VideoFrame, copy the bitmap, then return it. | 148 // Create a new VideoFrame, copy the bitmap, then return it. |
| 147 scoped_refptr<media::VideoFrame> video_frame = media::VideoFrame::CreateFrame( | 149 scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateFrame( |
| 148 media::PIXEL_FORMAT_I420, canvas_size, gfx::Rect(canvas_size), | 150 PIXEL_FORMAT_I420, canvas_size, gfx::Rect(canvas_size), canvas_size, |
| 149 canvas_size, base::TimeDelta()); | 151 base::TimeDelta()); |
| 150 canvas_bitmap.lockPixels(); | 152 canvas_bitmap.lockPixels(); |
| 151 media::CopyRGBToVideoFrame( | 153 CopyRGBToVideoFrame(reinterpret_cast<uint8_t*>(canvas_bitmap.getPixels()), |
| 152 reinterpret_cast<uint8_t*>(canvas_bitmap.getPixels()), | 154 canvas_bitmap.rowBytes(), |
| 153 canvas_bitmap.rowBytes(), | 155 gfx::Rect(canvas_size.width(), canvas_size.height()), |
| 154 gfx::Rect(canvas_size.width(), canvas_size.height()), video_frame.get()); | 156 video_frame.get()); |
| 155 canvas_bitmap.unlockPixels(); | 157 canvas_bitmap.unlockPixels(); |
| 156 return video_frame; | 158 return video_frame; |
| 157 } | 159 } |
| 158 | 160 |
| 159 } // namespace | 161 } // namespace |
| 160 | 162 |
| 161 void PaintRemotingInterstitial(const SkBitmap& image, | 163 void PaintInterstitial(const SkBitmap& image, |
| 162 const gfx::Size& natural_size, | 164 const gfx::Size& natural_size, |
| 163 RemotingInterstitialType interstitial_type, | 165 InterstitialType interstitial_type, |
| 164 VideoRendererSink* video_renderer_sink) { | 166 VideoRendererSink* video_renderer_sink) { |
| 165 if (!video_renderer_sink) | 167 if (!video_renderer_sink) |
| 166 return; | 168 return; |
| 167 | 169 |
| 168 const scoped_refptr<VideoFrame> interstitial = | 170 const scoped_refptr<VideoFrame> interstitial = |
| 169 RenderInterstitialFrame(image, natural_size, interstitial_type); | 171 RenderInterstitialFrame(image, natural_size, interstitial_type); |
| 170 | 172 |
| 171 video_renderer_sink->PaintSingleFrame(interstitial); | 173 video_renderer_sink->PaintSingleFrame(interstitial); |
| 172 } | 174 } |
| 173 | 175 |
| 176 } // namespace remoting |
| 174 } // namespace media | 177 } // namespace media |
| OLD | NEW |