| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/speech/speech_recognition_bubble.h" |
| 6 |
| 5 #include "base/bind.h" | 7 #include "base/bind.h" |
| 6 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 7 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 8 #include "chrome/browser/speech/speech_recognition_bubble.h" | |
| 9 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
| 10 #include "grit/generated_resources.h" | 11 #include "grit/generated_resources.h" |
| 11 #include "grit/theme_resources.h" | 12 #include "grit/theme_resources.h" |
| 12 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
| 13 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
| 15 #include "ui/gfx/display.h" |
| 14 #include "ui/gfx/rect.h" | 16 #include "ui/gfx/rect.h" |
| 17 #include "ui/gfx/screen.h" |
| 15 #include "ui/gfx/skbitmap_operations.h" | 18 #include "ui/gfx/skbitmap_operations.h" |
| 16 | 19 |
| 17 using content::WebContents; | 20 using content::WebContents; |
| 18 | 21 |
| 19 namespace { | 22 namespace { |
| 20 | 23 |
| 21 const color_utils::HSL kGrayscaleShift = { -1, 0, 0.6 }; | 24 const color_utils::HSL kGrayscaleShift = { -1, 0, 0.6 }; |
| 22 const int kWarmingUpAnimationStartMs = 500; | 25 const int kWarmingUpAnimationStartMs = 500; |
| 23 const int kWarmingUpAnimationStepMs = 100; | 26 const int kWarmingUpAnimationStepMs = 100; |
| 24 const int kRecognizingAnimationStepMs = 100; | 27 const int kRecognizingAnimationStepMs = 100; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 if (!web_contents) | 121 if (!web_contents) |
| 119 return NULL; | 122 return NULL; |
| 120 | 123 |
| 121 return CreateNativeBubble(web_contents, delegate, element_rect); | 124 return CreateNativeBubble(web_contents, delegate, element_rect); |
| 122 } | 125 } |
| 123 | 126 |
| 124 SpeechRecognitionBubbleBase::SpeechRecognitionBubbleBase( | 127 SpeechRecognitionBubbleBase::SpeechRecognitionBubbleBase( |
| 125 WebContents* web_contents) | 128 WebContents* web_contents) |
| 126 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 129 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 127 display_mode_(DISPLAY_MODE_RECORDING), | 130 display_mode_(DISPLAY_MODE_RECORDING), |
| 128 web_contents_(web_contents) { | 131 web_contents_(web_contents), |
| 132 scale_factor_(ui::SCALE_FACTOR_NONE) { |
| 133 gfx::Display display = gfx::Screen::GetDisplayNearestWindow( |
| 134 web_contents_ ? web_contents_->GetNativeView() : NULL); |
| 135 scale_factor_ = ui::GetScaleFactorFromScale( |
| 136 display.device_scale_factor()); |
| 137 |
| 138 const gfx::ImageSkiaRep& rep = |
| 139 g_images.Get().mic_empty()->GetRepresentation(scale_factor_); |
| 129 mic_image_.reset(new SkBitmap()); | 140 mic_image_.reset(new SkBitmap()); |
| 130 mic_image_->setConfig(SkBitmap::kARGB_8888_Config, | 141 mic_image_->setConfig(SkBitmap::kARGB_8888_Config, |
| 131 g_images.Get().mic_empty()->width(), | 142 rep.pixel_width(), rep.pixel_height()); |
| 132 g_images.Get().mic_empty()->height()); | |
| 133 mic_image_->allocPixels(); | 143 mic_image_->allocPixels(); |
| 134 | 144 |
| 135 buffer_image_.reset(new SkBitmap()); | 145 buffer_image_.reset(new SkBitmap()); |
| 136 buffer_image_->setConfig(SkBitmap::kARGB_8888_Config, | 146 buffer_image_->setConfig(SkBitmap::kARGB_8888_Config, |
| 137 g_images.Get().mic_empty()->width(), | 147 rep.pixel_width(), rep.pixel_height()); |
| 138 g_images.Get().mic_empty()->height()); | |
| 139 buffer_image_->allocPixels(); | 148 buffer_image_->allocPixels(); |
| 140 } | 149 } |
| 141 | 150 |
| 142 SpeechRecognitionBubbleBase::~SpeechRecognitionBubbleBase() { | 151 SpeechRecognitionBubbleBase::~SpeechRecognitionBubbleBase() { |
| 143 // This destructor is added to make sure members such as the scoped_ptr | 152 // This destructor is added to make sure members such as the scoped_ptr |
| 144 // get destroyed here and the derived classes don't have to care about such | 153 // get destroyed here and the derived classes don't have to care about such |
| 145 // member variables which they don't use. | 154 // member variables which they don't use. |
| 146 } | 155 } |
| 147 | 156 |
| 148 void SpeechRecognitionBubbleBase::SetWarmUpMode() { | 157 void SpeechRecognitionBubbleBase::SetWarmUpMode() { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 int width = mic_image_->width(); | 215 int width = mic_image_->width(); |
| 207 int height = mic_image_->height(); | 216 int height = mic_image_->height(); |
| 208 SkCanvas buffer_canvas(*buffer_image_); | 217 SkCanvas buffer_canvas(*buffer_image_); |
| 209 | 218 |
| 210 buffer_canvas.save(); | 219 buffer_canvas.save(); |
| 211 const int kVolumeSteps = 12; | 220 const int kVolumeSteps = 12; |
| 212 SkScalar clip_right = | 221 SkScalar clip_right = |
| 213 (((1.0f - volume) * (width * (kVolumeSteps + 1))) - width) / kVolumeSteps; | 222 (((1.0f - volume) * (width * (kVolumeSteps + 1))) - width) / kVolumeSteps; |
| 214 buffer_canvas.clipRect(SkRect::MakeLTRB(0, 0, | 223 buffer_canvas.clipRect(SkRect::MakeLTRB(0, 0, |
| 215 SkIntToScalar(width) - clip_right, SkIntToScalar(height))); | 224 SkIntToScalar(width) - clip_right, SkIntToScalar(height))); |
| 216 buffer_canvas.drawBitmap(image, 0, 0); | 225 buffer_canvas.drawBitmap( |
| 226 image.GetRepresentation(scale_factor_).sk_bitmap(), 0, 0); |
| 217 buffer_canvas.restore(); | 227 buffer_canvas.restore(); |
| 218 SkPaint multiply_paint; | 228 SkPaint multiply_paint; |
| 219 multiply_paint.setXfermode(SkXfermode::Create(SkXfermode::kMultiply_Mode)); | 229 multiply_paint.setXfermode(SkXfermode::Create(SkXfermode::kMultiply_Mode)); |
| 220 buffer_canvas.drawBitmap(*g_images.Get().mic_mask(), -clip_right, 0, | 230 buffer_canvas.drawBitmap( |
| 221 &multiply_paint); | 231 g_images.Get().mic_mask()->GetRepresentation(scale_factor_).sk_bitmap(), |
| 232 -clip_right, 0, &multiply_paint); |
| 222 | 233 |
| 223 canvas->drawBitmap(*buffer_image_.get(), 0, 0); | 234 canvas->drawBitmap(*buffer_image_.get(), 0, 0); |
| 224 } | 235 } |
| 225 | 236 |
| 226 void SpeechRecognitionBubbleBase::SetInputVolume(float volume, | 237 void SpeechRecognitionBubbleBase::SetInputVolume(float volume, |
| 227 float noise_volume) { | 238 float noise_volume) { |
| 228 mic_image_->eraseARGB(0, 0, 0, 0); | 239 mic_image_->eraseARGB(0, 0, 0, 0); |
| 229 SkCanvas canvas(*mic_image_); | 240 SkCanvas canvas(*mic_image_); |
| 230 | 241 |
| 231 // Draw the empty volume image first and the current volume image on top, | 242 // Draw the empty volume image first and the current volume image on top, |
| 232 // and then the noise volume image on top of both. | 243 // and then the noise volume image on top of both. |
| 233 canvas.drawBitmap(*g_images.Get().mic_empty(), 0, 0); | 244 canvas.drawBitmap( |
| 245 g_images.Get().mic_empty()->GetRepresentation(scale_factor_).sk_bitmap(), |
| 246 0, 0); |
| 234 DrawVolumeOverlay(&canvas, *g_images.Get().mic_full(), volume); | 247 DrawVolumeOverlay(&canvas, *g_images.Get().mic_full(), volume); |
| 235 DrawVolumeOverlay(&canvas, *g_images.Get().mic_noise(), noise_volume); | 248 DrawVolumeOverlay(&canvas, *g_images.Get().mic_noise(), noise_volume); |
| 236 | 249 |
| 237 SetImage(*mic_image_.get()); | 250 gfx::ImageSkia image(gfx::ImageSkiaRep(*mic_image_.get(), scale_factor_)); |
| 251 SetImage(image); |
| 238 } | 252 } |
| 239 | 253 |
| 240 WebContents* SpeechRecognitionBubbleBase::GetWebContents() { | 254 WebContents* SpeechRecognitionBubbleBase::GetWebContents() { |
| 241 return web_contents_; | 255 return web_contents_; |
| 242 } | 256 } |
| 243 | 257 |
| 244 void SpeechRecognitionBubbleBase::SetImage(const gfx::ImageSkia& image) { | 258 void SpeechRecognitionBubbleBase::SetImage(const gfx::ImageSkia& image) { |
| 245 icon_image_.reset(new gfx::ImageSkia(image)); | 259 icon_image_.reset(new gfx::ImageSkia(image)); |
| 246 UpdateImage(); | 260 UpdateImage(); |
| 247 } | 261 } |
| 248 | 262 |
| 249 gfx::ImageSkia SpeechRecognitionBubbleBase::icon_image() { | 263 gfx::ImageSkia SpeechRecognitionBubbleBase::icon_image() { |
| 250 return (icon_image_ != NULL) ? *icon_image_ : gfx::ImageSkia(); | 264 return (icon_image_ != NULL) ? *icon_image_ : gfx::ImageSkia(); |
| 251 } | 265 } |
| OLD | NEW |