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/paint_vector_icon.h" | 16 #include "ui/gfx/paint_vector_icon.h" |
17 #include "ui/gfx/skbitmap_operations.h" | 17 #include "ui/gfx/skbitmap_operations.h" |
18 #include "ui/gfx/vector_icons_public.h" | 18 #include "ui/gfx/vector_icons_public.h" |
19 | 19 |
20 namespace media { | 20 namespace media { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 gfx::Size GetRotatedVideoSize(VideoRotation rotation, gfx::Size natural_size) { | 24 // Resizes |image| to fit in the center of |canvas_size| and preserves the |
25 if (rotation == VIDEO_ROTATION_90 || rotation == VIDEO_ROTATION_270) | 25 // image's aspect ratio as close as possible. |
26 return gfx::Size(natural_size.height(), natural_size.width()); | 26 SkBitmap ResizeImage(const SkBitmap& image, const gfx::Size& canvas_size) { |
27 return natural_size; | 27 DCHECK(!canvas_size.IsEmpty()); |
| 28 |
| 29 const gfx::Size image_size = gfx::Size(image.width(), image.height()); |
| 30 if (image_size == canvas_size) |
| 31 return image; |
| 32 |
| 33 gfx::Size scaled_size = ScaleSizeToFitWithinTarget(image_size, canvas_size); |
| 34 if (scaled_size == image_size) |
| 35 return image; |
| 36 |
| 37 return skia::ImageOperations::Resize( |
| 38 image, skia::ImageOperations::RESIZE_BEST, scaled_size.width(), |
| 39 scaled_size.height()); |
28 } | 40 } |
29 | 41 |
30 } // namespace | 42 scoped_refptr<VideoFrame> GetInterstitial(const SkBitmap& image, |
| 43 const gfx::Size& canvas_size, |
| 44 bool is_remoting_successful) { |
| 45 SkBitmap modified_bitmap; |
| 46 modified_bitmap.allocN32Pixels(canvas_size.width(), canvas_size.height()); |
| 47 modified_bitmap.eraseColor(SK_ColorBLACK); |
| 48 SkCanvas canvas(modified_bitmap); |
31 | 49 |
32 RemotingInterstitialUI::RemotingInterstitialUI( | 50 SkBitmap background_image; |
33 VideoRendererSink* video_renderer_sink, | 51 // Draw |background_image| on the canvas. |
34 const PipelineMetadata& pipeline_metadata) | 52 if (!image.drawsNothing()) { |
35 : video_renderer_sink_(video_renderer_sink), | 53 background_image = ResizeImage(image, canvas_size); |
36 pipeline_metadata_(pipeline_metadata) { | 54 color_utils::HSL shift = {-1, 0, 0.2}; // Make monochromatic. |
37 DCHECK(pipeline_metadata_.has_video); | 55 SkBitmap monochromatic_image = |
38 pipeline_metadata_.natural_size = GetRotatedVideoSize( | 56 SkBitmapOperations::CreateHSLShiftedBitmap(background_image, shift); |
39 pipeline_metadata_.video_rotation, pipeline_metadata_.natural_size); | |
40 } | |
41 | 57 |
42 RemotingInterstitialUI::~RemotingInterstitialUI() {} | 58 const gfx::Size image_size = |
43 | 59 gfx::Size(background_image.width(), background_image.height()); |
44 scoped_refptr<VideoFrame> RemotingInterstitialUI::GetInterstitial( | 60 if (image_size != canvas_size) { |
45 const SkBitmap& background_image, | 61 // Centered the background image. |
46 bool is_remoting_successful) { | 62 gfx::Rect centered_rect = |
47 const gfx::Size canvas_size = | 63 ComputeLetterboxRegion(gfx::Rect(canvas_size), image_size); |
48 gfx::Size(background_image.width(), background_image.height()); | 64 canvas.writePixels(monochromatic_image, centered_rect.x(), |
49 DCHECK(canvas_size == pipeline_metadata_.natural_size); | 65 centered_rect.y()); |
50 | 66 } else { |
51 color_utils::HSL shift = {-1, 0, 0.2}; // Make monochromatic. | 67 canvas.writePixels(monochromatic_image, 0, 0); |
52 SkBitmap modified_bitmap = | 68 } |
53 SkBitmapOperations::CreateHSLShiftedBitmap(background_image, shift); | 69 } |
54 | |
55 SkCanvas canvas(modified_bitmap); | |
56 | 70 |
57 // Blur the background image. | 71 // Blur the background image. |
58 SkScalar sigma = SkDoubleToScalar(10); | 72 SkScalar sigma = SkDoubleToScalar(10); |
59 SkPaint paint_blur; | 73 SkPaint paint_blur; |
60 paint_blur.setImageFilter(SkBlurImageFilter::Make(sigma, sigma, nullptr)); | 74 paint_blur.setImageFilter(SkBlurImageFilter::Make(sigma, sigma, nullptr)); |
61 canvas.saveLayer(0, &paint_blur); | 75 canvas.saveLayer(0, &paint_blur); |
62 canvas.restore(); | 76 canvas.restore(); |
63 | 77 |
64 // Create SkPaint for text and icon bitmap. | 78 // Create SkPaint for text and icon bitmap. |
65 // After |paint| draws, the new canvas should look like this: | 79 // After |paint| draws, the new canvas should look like this: |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 canvas_size, base::TimeDelta()); | 127 canvas_size, base::TimeDelta()); |
114 modified_bitmap.lockPixels(); | 128 modified_bitmap.lockPixels(); |
115 media::CopyRGBToVideoFrame( | 129 media::CopyRGBToVideoFrame( |
116 reinterpret_cast<uint8_t*>(modified_bitmap.getPixels()), | 130 reinterpret_cast<uint8_t*>(modified_bitmap.getPixels()), |
117 modified_bitmap.rowBytes(), | 131 modified_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 modified_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 RemotingInterstitialUI::RemotingInterstitialUI( |
| 140 VideoRendererSink* video_renderer_sink) |
| 141 : video_renderer_sink_(video_renderer_sink), weak_factory_(this) { |
| 142 DCHECK(video_renderer_sink_); |
| 143 } |
| 144 |
| 145 RemotingInterstitialUI::~RemotingInterstitialUI() {} |
| 146 |
| 147 void RemotingInterstitialUI::ShowInterstitialOnSink( |
| 148 const SkBitmap& image, |
| 149 const gfx::Size& canvas_size, |
| 150 RemotingInterstitialType interstial_type) { |
| 151 if (canvas_size.IsEmpty()) |
125 return; | 152 return; |
126 | 153 |
127 // TODO(xjz): Provide poster image, if available, rather than a blank, black | 154 scoped_refptr<VideoFrame> interstitial = |
128 // image. | 155 VideoFrame::CreateBlackFrame(canvas_size); |
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 | 156 |
134 const scoped_refptr<VideoFrame> interstitial = | 157 if (interstial_type != RemotingInterstitialType::NONE) { |
135 GetInterstitial(background_image, is_remoting_successful); | 158 interstitial = |
| 159 GetInterstitial(image, canvas_size, |
| 160 interstial_type == RemotingInterstitialType::SUCCESS); |
| 161 } |
| 162 |
136 if (!interstitial) | 163 if (!interstitial) |
137 return; | 164 return; |
138 | |
139 video_renderer_sink_->PaintSingleFrame(interstitial); | 165 video_renderer_sink_->PaintSingleFrame(interstitial); |
140 } | 166 } |
141 | 167 |
142 } // namespace media | 168 } // namespace media |
OLD | NEW |