Chromium Code Reviews| Index: native_client_sdk/src/examples/api/graphics_2d/graphics_2d.cc |
| diff --git a/native_client_sdk/src/examples/api/graphics_2d/graphics_2d.cc b/native_client_sdk/src/examples/api/graphics_2d/graphics_2d.cc |
| index c519cfd192925dff61d5de83af980959b15e3939..7a56619c6634b0cf1a7e0dd3405069ecda6c6173 100644 |
| --- a/native_client_sdk/src/examples/api/graphics_2d/graphics_2d.cc |
| +++ b/native_client_sdk/src/examples/api/graphics_2d/graphics_2d.cc |
| @@ -48,7 +48,8 @@ class Graphics2DInstance : public pp::Instance { |
| : pp::Instance(instance), |
| callback_factory_(this), |
| mouse_down_(false), |
| - buffer_(NULL) {} |
| + buffer_(NULL), |
| + device_scale_(1.0f) {} |
| ~Graphics2DInstance() { delete[] buffer_; } |
| @@ -62,7 +63,9 @@ class Graphics2DInstance : public pp::Instance { |
| } |
| virtual void DidChangeView(const pp::View& view) { |
| - pp::Size new_size = view.GetRect().size(); |
| + device_scale_ = view.GetDeviceScale(); |
| + pp::Size new_size = pp::Size(view.GetRect().width() * device_scale_, |
| + view.GetRect().height() * device_scale_); |
| if (!CreateContext(new_size)) |
| return; |
| @@ -86,7 +89,8 @@ class Graphics2DInstance : public pp::Instance { |
| if (mouse_event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_NONE) |
| return true; |
| - mouse_ = mouse_event.GetPosition(); |
| + mouse_ = pp::Point(mouse_event.GetPosition().x() * device_scale_, |
| + mouse_event.GetPosition().y() * device_scale_); |
| mouse_down_ = true; |
| } |
| @@ -112,6 +116,9 @@ class Graphics2DInstance : public pp::Instance { |
| bool CreateContext(const pp::Size& new_size) { |
| const bool kIsAlwaysOpaque = true; |
| context_ = pp::Graphics2D(this, new_size, kIsAlwaysOpaque); |
| + // Call SetScale before BindGraphics so the image is scaled correctly on |
| + // HiDPI displays. |
| + context_.SetScale(1.0f / device_scale_); |
| if (!BindGraphics(context_)) { |
| fprintf(stderr, "Unable to bind 2d context!\n"); |
| context_ = pp::Graphics2D(); |
| @@ -182,7 +189,7 @@ class Graphics2DInstance : public pp::Instance { |
| int height = size_.height(); |
| // Draw a circle at the mouse position. |
| - int radius = kMouseRadius; |
| + int radius = kMouseRadius * device_scale_; |
|
binji
2014/07/17 00:11:38
no warning here about truncating from float to int
Josh Horwich
2014/07/17 00:20:58
I haven't seen any (here, and the pp::Size / pp::P
|
| int cx = mouse_.x(); |
| int cy = mouse_.y(); |
| int minx = cx - radius <= 0 ? 1 : cx - radius; |
| @@ -261,6 +268,7 @@ class Graphics2DInstance : public pp::Instance { |
| bool mouse_down_; |
| uint8_t* buffer_; |
| uint32_t palette_[256]; |
| + float device_scale_; |
| }; |
| class Graphics2DModule : public pp::Module { |