Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(544)

Side by Side Diff: media/remoting/remoting_interstitial_ui.cc

Issue 2566223005: Media Remoting: Update remoting interstitial when status changes. (Closed)
Patch Set: Show unprocessed background image when exiting remoting. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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);
miu 2016/12/20 00:16:17 Don't need this (see later comment).
xjz 2016/12/20 19:32:28 Done.
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 void DrawInterstitial(SkCanvas& canvas,
miu 2016/12/20 00:16:17 style: Pass by pointer, not by reference. (and it
xjz 2016/12/20 19:32:28 Done.
31 43 const gfx::Size& canvas_size,
32 RemotingInterstitialUI::RemotingInterstitialUI( 44 bool is_remoting_successful) {
miu 2016/12/20 00:16:16 nit: Instead of the boolean, just pass the "type"
xjz 2016/12/20 19:32:28 Done.
33 VideoRendererSink* video_renderer_sink,
34 const PipelineMetadata& pipeline_metadata)
35 : video_renderer_sink_(video_renderer_sink),
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
57 // Blur the background image. 45 // Blur the background image.
58 SkScalar sigma = SkDoubleToScalar(10); 46 SkScalar sigma = SkDoubleToScalar(10);
59 SkPaint paint_blur; 47 SkPaint paint_blur;
60 paint_blur.setImageFilter(SkBlurImageFilter::Make(sigma, sigma, nullptr)); 48 paint_blur.setImageFilter(SkBlurImageFilter::Make(sigma, sigma, nullptr));
61 canvas.saveLayer(0, &paint_blur); 49 canvas.saveLayer(0, &paint_blur);
62 canvas.restore(); 50 canvas.restore();
63 51
64 // Create SkPaint for text and icon bitmap. 52 // Create SkPaint for text and icon bitmap.
65 // After |paint| draws, the new canvas should look like this: 53 // After |paint| draws, the new canvas should look like this:
66 // _________________________________ 54 // _________________________________
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 gfx::VectorIconId current_icon = 87 gfx::VectorIconId current_icon =
100 is_remoting_successful ? gfx::VectorIconId::MEDIA_ROUTER_ACTIVE 88 is_remoting_successful ? gfx::VectorIconId::MEDIA_ROUTER_ACTIVE
101 : gfx::VectorIconId::MEDIA_ROUTER_WARNING; 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, &paint);
97 }
98
99 scoped_refptr<VideoFrame> GetInterstitial(const SkBitmap& image,
miu 2016/12/20 00:16:17 naming nit: How about RenderInterstitialFrame()?
xjz 2016/12/20 19:32:28 Done.
100 const gfx::Size& canvas_size,
101 RemotingInterstitialType type) {
102 SkBitmap black_background;
miu 2016/12/20 00:16:17 naming nit: This isn't "black background" by the e
xjz 2016/12/20 19:32:28 Done. Renamed it as "canvas_bitmap".
103 black_background.allocN32Pixels(canvas_size.width(), canvas_size.height());
104 black_background.eraseColor(SK_ColorBLACK);
105 SkCanvas canvas(black_background);
106
107 SkBitmap background_image;
108 // Draw |background_image| on the canvas.
109 if (!image.drawsNothing()) {
110 background_image = ResizeImage(image, canvas_size);
miu 2016/12/20 00:16:16 Looks like you should call ComputeLetterboxRegion(
xjz 2016/12/20 19:32:28 Done.
111 SkBitmap processed_image;
112 if (type == RemotingInterstitialType::NONE) {
113 processed_image = background_image;
114 } else {
115 color_utils::HSL shift = {-1, 0, 0.2}; // Make monochromatic.
116 processed_image =
117 SkBitmapOperations::CreateHSLShiftedBitmap(background_image, shift);
118 }
119
120 const gfx::Size image_size =
miu 2016/12/20 00:16:16 Given the suggestions above, this and the followin
xjz 2016/12/20 19:32:28 Done.
121 gfx::Size(background_image.width(), background_image.height());
122 if (image_size != canvas_size) {
123 // Centered the background image.
124 gfx::Rect centered_rect =
125 ComputeLetterboxRegion(gfx::Rect(canvas_size), image_size);
126 canvas.writePixels(processed_image, centered_rect.x(), centered_rect.y());
127 } else {
128 canvas.writePixels(processed_image, 0, 0);
129 }
130 }
131
132 if (type != RemotingInterstitialType::NONE) {
133 DrawInterstitial(canvas, canvas_size,
miu 2016/12/20 00:16:17 naming: Instead of DrawInterstitial(), how about R
xjz 2016/12/20 19:32:28 Done.
134 type == RemotingInterstitialType::SUCCESS);
135 }
109 136
110 // Create a new VideoFrame, copy the bitmap, then return it. 137 // Create a new VideoFrame, copy the bitmap, then return it.
111 scoped_refptr<media::VideoFrame> video_frame = media::VideoFrame::CreateFrame( 138 scoped_refptr<media::VideoFrame> video_frame = media::VideoFrame::CreateFrame(
112 media::PIXEL_FORMAT_I420, canvas_size, gfx::Rect(canvas_size), 139 media::PIXEL_FORMAT_I420, canvas_size, gfx::Rect(canvas_size),
113 canvas_size, base::TimeDelta()); 140 canvas_size, base::TimeDelta());
114 modified_bitmap.lockPixels(); 141 black_background.lockPixels();
115 media::CopyRGBToVideoFrame( 142 media::CopyRGBToVideoFrame(
116 reinterpret_cast<uint8_t*>(modified_bitmap.getPixels()), 143 reinterpret_cast<uint8_t*>(black_background.getPixels()),
117 modified_bitmap.rowBytes(), 144 black_background.rowBytes(),
118 gfx::Rect(canvas_size.width(), canvas_size.height()), video_frame.get()); 145 gfx::Rect(canvas_size.width(), canvas_size.height()), video_frame.get());
119 modified_bitmap.unlockPixels(); 146 black_background.unlockPixels();
120 return video_frame; 147 return video_frame;
121 } 148 }
122 149
123 void RemotingInterstitialUI::ShowInterstitial(bool is_remoting_successful) { 150 } // namespace
124 if (!video_renderer_sink_) 151
152 void ShowRemotingInterstitial(VideoRendererSink* video_renderer_sink,
miu 2016/12/20 00:16:17 naming nit: "Show" infers the interstitial can als
xjz 2016/12/20 19:32:28 Done.
153 const SkBitmap& image,
154 const gfx::Size& canvas_size,
155 RemotingInterstitialType interstitial_type) {
156 if (canvas_size.IsEmpty())
125 return; 157 return;
126 158
127 // TODO(xjz): Provide poster image, if available, rather than a blank, black 159 const scoped_refptr<VideoFrame> interstitial =
128 // image. 160 GetInterstitial(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 161
134 const scoped_refptr<VideoFrame> interstitial =
135 GetInterstitial(background_image, is_remoting_successful);
136 if (!interstitial) 162 if (!interstitial)
137 return; 163 return;
138 164 video_renderer_sink->PaintSingleFrame(interstitial);
139 video_renderer_sink_->PaintSingleFrame(interstitial);
140 } 165 }
141 166
142 } // namespace media 167 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698