summaryrefslogtreecommitdiffstats
path: root/native_client_sdk
diff options
context:
space:
mode:
authorjhorwich@chromium.org <jhorwich@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-22 22:50:06 +0000
committerjhorwich@chromium.org <jhorwich@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-22 22:50:06 +0000
commita86a4ceac432b0d5efb42cf3ca8a722622e1abc8 (patch)
tree31306e82cdc19297647fcf3096fed01762cbb7c6 /native_client_sdk
parente3c595797a4f2a56a456bfff5508818abad2182d (diff)
downloadchromium_src-a86a4ceac432b0d5efb42cf3ca8a722622e1abc8.zip
chromium_src-a86a4ceac432b0d5efb42cf3ca8a722622e1abc8.tar.gz
chromium_src-a86a4ceac432b0d5efb42cf3ca8a722622e1abc8.tar.bz2
Update pi_generator NaCl SDK example for HiDPI
Simple update to the example code to use pp::View::GetDeviceScale and pp::Graphics2D::SetScale to render at device pixel resolution even on HiDPI devices. BUG=114071 TEST=Build nacl SDK, build pi_generator, run in regular and HiDPI modes Review URL: https://chromiumcodereview.appspot.com/14166004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@195631 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'native_client_sdk')
-rw-r--r--native_client_sdk/src/examples/pi_generator/pi_generator.cc27
-rw-r--r--native_client_sdk/src/examples/pi_generator/pi_generator.h3
2 files changed, 21 insertions, 9 deletions
diff --git a/native_client_sdk/src/examples/pi_generator/pi_generator.cc b/native_client_sdk/src/examples/pi_generator/pi_generator.cc
index 2b5766f..1106343f 100644
--- a/native_client_sdk/src/examples/pi_generator/pi_generator.cc
+++ b/native_client_sdk/src/examples/pi_generator/pi_generator.cc
@@ -81,7 +81,8 @@ PiGenerator::PiGenerator(PP_Instance instance)
flush_pending_(false),
quit_(false),
thread_create_result_(0),
- pi_(0.0) {
+ pi_(0.0),
+ device_scale_(1.0) {
pthread_mutex_init(&pixel_buffer_mutex_, NULL);
}
@@ -98,13 +99,18 @@ PiGenerator::~PiGenerator() {
}
void PiGenerator::DidChangeView(const pp::View& view) {
- pp::Rect position = view.GetRect();
- if (pixel_buffer_ && position.size() == pixel_buffer_->size())
- return; // Size didn't change, no need to update anything.
-
- // Create a new device context with the new size.
+ pp::Size size = view.GetRect().size();
+ float device_scale = view.GetDeviceScale();
+ size.set_width(static_cast<int>(size.width() * device_scale));
+ size.set_height(static_cast<int>(size.height() * device_scale));
+ if (pixel_buffer_ && size == pixel_buffer_->size() &&
+ device_scale == device_scale_)
+ return; // Size and scale didn't change, no need to update anything.
+
+ // Create a new device context with the new size and scale.
DestroyContext();
- CreateContext(position.size());
+ device_scale_ = device_scale;
+ CreateContext(size, device_scale_);
// Delete the old pixel buffer and create a new one.
ScopedMutexLock scoped_mutex(&pixel_buffer_mutex_);
delete pixel_buffer_;
@@ -164,7 +170,7 @@ void PiGenerator::Paint() {
PostMessage(pi_estimate);
}
-void PiGenerator::CreateContext(const pp::Size& size) {
+void PiGenerator::CreateContext(const pp::Size& size, float device_scale) {
ScopedMutexLock scoped_mutex(&pixel_buffer_mutex_);
if (!scoped_mutex.is_valid()) {
return;
@@ -172,6 +178,11 @@ void PiGenerator::CreateContext(const pp::Size& size) {
if (IsContextValid())
return;
graphics_2d_context_ = new pp::Graphics2D(this, size, false);
+ // Scale the contents of the graphics context down by the inverse of the
+ // device scale. This makes each pixel in the context represent a single
+ // physical pixel on the device when running on high-DPI displays.
+ // See pp::Graphics2D::SetScale for more details.
+ graphics_2d_context_->SetScale(1.0 / device_scale);
if (!BindGraphics(*graphics_2d_context_)) {
printf("Couldn't bind the device context\n");
}
diff --git a/native_client_sdk/src/examples/pi_generator/pi_generator.h b/native_client_sdk/src/examples/pi_generator/pi_generator.h
index 556b091..50f2dbd 100644
--- a/native_client_sdk/src/examples/pi_generator/pi_generator.h
+++ b/native_client_sdk/src/examples/pi_generator/pi_generator.h
@@ -83,7 +83,7 @@ class PiGenerator : public pp::Instance {
private:
// Create and initialize the 2D context used for drawing.
- void CreateContext(const pp::Size& size);
+ void CreateContext(const pp::Size& size, float device_scale);
// Destroy the 2D drawing context.
void DestroyContext();
// Push the pixels to the browser, then attempt to flush the 2D context. If
@@ -103,6 +103,7 @@ class PiGenerator : public pp::Instance {
pthread_t compute_pi_thread_;
int thread_create_result_;
double pi_;
+ float device_scale_;
// ComputePi() estimates Pi using Monte Carlo method and it is executed by a
// separate thread created in SetWindow(). ComputePi() puts kMaxPointCount