| 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 "media/base/media_resources.h" | |
| 8 #include "media/base/video_frame.h" | |
| 9 #include "media/base/video_renderer_sink.h" | |
| 10 #include "media/base/video_util.h" | |
| 11 #include "skia/ext/image_operations.h" | |
| 12 #include "third_party/skia/include/core/SkCanvas.h" | |
| 13 #include "third_party/skia/include/core/SkTypeface.h" | |
| 14 #include "third_party/skia/include/effects/SkBlurImageFilter.h" | |
| 15 #include "ui/gfx/color_palette.h" | |
| 16 #include "ui/gfx/geometry/size.h" | |
| 17 #include "ui/gfx/paint_vector_icon.h" | |
| 18 #include "ui/gfx/skbitmap_operations.h" | |
| 19 #include "ui/gfx/vector_icons_public.h" | |
| 20 | |
| 21 namespace media { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 SkBitmap ResizeImage(const SkBitmap& image, const gfx::Size& scaled_size) { | |
| 26 DCHECK(!scaled_size.IsEmpty()); | |
| 27 | |
| 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()); | |
| 35 } | |
| 36 | |
| 37 void RenderCastMessage(const gfx::Size& canvas_size, | |
| 38 RemotingInterstitialType type, | |
| 39 SkCanvas* canvas) { | |
| 40 DCHECK(canvas); | |
| 41 if (type == RemotingInterstitialType::BETWEEN_SESSIONS) | |
| 42 return; | |
| 43 | |
| 44 // Blur the background image. | |
| 45 SkScalar sigma = SkDoubleToScalar(10); | |
| 46 SkPaint paint_blur; | |
| 47 paint_blur.setImageFilter(SkBlurImageFilter::Make(sigma, sigma, nullptr)); | |
| 48 canvas->saveLayer(0, &paint_blur); | |
| 49 canvas->restore(); | |
| 50 | |
| 51 // Create SkPaint for text and icon bitmap. | |
| 52 // After |paint| draws, the new canvas should look like this: | |
| 53 // _________________________________ | |
| 54 // | | | |
| 55 // | | | |
| 56 // | [ ] | | |
| 57 // | Casting Video... | | |
| 58 // | | | |
| 59 // |________________________________| | |
| 60 // | |
| 61 // Both text and icon are centered horizontally. Together, they are | |
| 62 // centered vertically. | |
| 63 SkPaint paint; | |
| 64 int text_size = SkIntToScalar(30); | |
| 65 paint.setAntiAlias(true); | |
| 66 paint.setFilterQuality(kHigh_SkFilterQuality); | |
| 67 paint.setColor(SK_ColorLTGRAY); | |
| 68 paint.setTypeface(SkTypeface::MakeFromName( | |
| 69 "sans", SkFontStyle::FromOldStyle(SkTypeface::kNormal))); | |
| 70 paint.setTextSize(text_size); | |
| 71 | |
| 72 // Draw the appropriate text. | |
| 73 const std::string remote_playback_message = | |
| 74 (type == RemotingInterstitialType::IN_SESSION | |
| 75 ? GetLocalizedStringUTF8(MEDIA_REMOTING_CASTING_VIDEO_TEXT) | |
| 76 : GetLocalizedStringUTF8(MEDIA_REMOTING_CAST_ERROR_TEXT)); | |
| 77 size_t display_text_width = paint.measureText(remote_playback_message.data(), | |
| 78 remote_playback_message.size()); | |
| 79 SkScalar sk_text_offset_x = (canvas_size.width() - display_text_width) / 2.0; | |
| 80 SkScalar sk_text_offset_y = (canvas_size.height() / 2.0) + text_size; | |
| 81 canvas->drawText(remote_playback_message.data(), | |
| 82 remote_playback_message.size(), sk_text_offset_x, | |
| 83 sk_text_offset_y, paint); | |
| 84 | |
| 85 // Draw the appropriate Cast icon. | |
| 86 gfx::VectorIconId current_icon = | |
| 87 (type == RemotingInterstitialType::IN_SESSION | |
| 88 ? gfx::VectorIconId::MEDIA_ROUTER_ACTIVE | |
| 89 : gfx::VectorIconId::MEDIA_ROUTER_WARNING); | |
| 90 gfx::ImageSkia icon_image = | |
| 91 gfx::CreateVectorIcon(current_icon, 65, SK_ColorLTGRAY); | |
| 92 const SkBitmap* icon_bitmap = icon_image.bitmap(); | |
| 93 SkScalar sk_image_offset_x = (canvas_size.width() - icon_image.width()) / 2.0; | |
| 94 SkScalar sk_image_offset_y = | |
| 95 (canvas_size.height() / 2.0) - icon_image.height(); | |
| 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); | |
| 123 | |
| 124 // Create a new VideoFrame, copy the bitmap, then return it. | |
| 125 scoped_refptr<media::VideoFrame> video_frame = media::VideoFrame::CreateFrame( | |
| 126 media::PIXEL_FORMAT_I420, canvas_size, gfx::Rect(canvas_size), | |
| 127 canvas_size, base::TimeDelta()); | |
| 128 canvas_bitmap.lockPixels(); | |
| 129 media::CopyRGBToVideoFrame( | |
| 130 reinterpret_cast<uint8_t*>(canvas_bitmap.getPixels()), | |
| 131 canvas_bitmap.rowBytes(), | |
| 132 gfx::Rect(canvas_size.width(), canvas_size.height()), video_frame.get()); | |
| 133 canvas_bitmap.unlockPixels(); | |
| 134 return video_frame; | |
| 135 } | |
| 136 | |
| 137 } // namespace | |
| 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()) | |
| 144 return; | |
| 145 | |
| 146 const scoped_refptr<VideoFrame> interstitial = | |
| 147 RenderInterstitialFrame(image, canvas_size, interstitial_type); | |
| 148 | |
| 149 if (!interstitial) | |
| 150 return; | |
| 151 video_renderer_sink->PaintSingleFrame(interstitial); | |
| 152 } | |
| 153 | |
| 154 } // namespace media | |
| OLD | NEW |