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