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

Side by Side Diff: native_client_sdk/src/examples/api/graphics_2d/graphics_2d.cc

Issue 397173003: NaCl SDK: Demonstrate use of pp::Graphics2D::SetScale() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 <stdio.h> 5 #include <stdio.h>
6 #include <stdlib.h> 6 #include <stdlib.h>
7 7
8 #include "ppapi/c/ppb_image_data.h" 8 #include "ppapi/c/ppb_image_data.h"
9 #include "ppapi/cpp/graphics_2d.h" 9 #include "ppapi/cpp/graphics_2d.h"
10 #include "ppapi/cpp/image_data.h" 10 #include "ppapi/cpp/image_data.h"
(...skipping 30 matching lines...) Expand all
41 } 41 }
42 42
43 } // namespace 43 } // namespace
44 44
45 class Graphics2DInstance : public pp::Instance { 45 class Graphics2DInstance : public pp::Instance {
46 public: 46 public:
47 explicit Graphics2DInstance(PP_Instance instance) 47 explicit Graphics2DInstance(PP_Instance instance)
48 : pp::Instance(instance), 48 : pp::Instance(instance),
49 callback_factory_(this), 49 callback_factory_(this),
50 mouse_down_(false), 50 mouse_down_(false),
51 buffer_(NULL) {} 51 buffer_(NULL),
52 device_scale_(1.0f) {}
52 53
53 ~Graphics2DInstance() { delete[] buffer_; } 54 ~Graphics2DInstance() { delete[] buffer_; }
54 55
55 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { 56 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
56 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE); 57 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE);
57 58
58 unsigned int seed = 1; 59 unsigned int seed = 1;
59 srand(seed); 60 srand(seed);
60 CreatePalette(); 61 CreatePalette();
61 return true; 62 return true;
62 } 63 }
63 64
64 virtual void DidChangeView(const pp::View& view) { 65 virtual void DidChangeView(const pp::View& view) {
65 pp::Size new_size = view.GetRect().size(); 66 device_scale_ = view.GetDeviceScale();
67 pp::Size new_size = pp::Size(view.GetRect().width() * device_scale_,
68 view.GetRect().height() * device_scale_);
66 69
67 if (!CreateContext(new_size)) 70 if (!CreateContext(new_size))
68 return; 71 return;
69 72
70 // When flush_context_ is null, it means there is no Flush callback in 73 // When flush_context_ is null, it means there is no Flush callback in
71 // flight. This may have happened if the context was not created 74 // flight. This may have happened if the context was not created
72 // successfully, or if this is the first call to DidChangeView (when the 75 // successfully, or if this is the first call to DidChangeView (when the
73 // module first starts). In either case, start the main loop. 76 // module first starts). In either case, start the main loop.
74 if (flush_context_.is_null()) 77 if (flush_context_.is_null())
75 MainLoop(0); 78 MainLoop(0);
76 } 79 }
77 80
78 virtual bool HandleInputEvent(const pp::InputEvent& event) { 81 virtual bool HandleInputEvent(const pp::InputEvent& event) {
79 if (!buffer_) 82 if (!buffer_)
80 return true; 83 return true;
81 84
82 if (event.GetType() == PP_INPUTEVENT_TYPE_MOUSEDOWN || 85 if (event.GetType() == PP_INPUTEVENT_TYPE_MOUSEDOWN ||
83 event.GetType() == PP_INPUTEVENT_TYPE_MOUSEMOVE) { 86 event.GetType() == PP_INPUTEVENT_TYPE_MOUSEMOVE) {
84 pp::MouseInputEvent mouse_event(event); 87 pp::MouseInputEvent mouse_event(event);
85 88
86 if (mouse_event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_NONE) 89 if (mouse_event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_NONE)
87 return true; 90 return true;
88 91
89 mouse_ = mouse_event.GetPosition(); 92 mouse_ = pp::Point(mouse_event.GetPosition().x() * device_scale_,
93 mouse_event.GetPosition().y() * device_scale_);
90 mouse_down_ = true; 94 mouse_down_ = true;
91 } 95 }
92 96
93 if (event.GetType() == PP_INPUTEVENT_TYPE_MOUSEUP) 97 if (event.GetType() == PP_INPUTEVENT_TYPE_MOUSEUP)
94 mouse_down_ = false; 98 mouse_down_ = false;
95 99
96 return true; 100 return true;
97 } 101 }
98 102
99 private: 103 private:
100 void CreatePalette() { 104 void CreatePalette() {
101 for (int i = 0; i < 64; ++i) { 105 for (int i = 0; i < 64; ++i) {
102 // Black -> Red 106 // Black -> Red
103 palette_[i] = MakeColor(i * 2, 0, 0); 107 palette_[i] = MakeColor(i * 2, 0, 0);
104 palette_[i + 64] = MakeColor(128 + i * 2, 0, 0); 108 palette_[i + 64] = MakeColor(128 + i * 2, 0, 0);
105 // Red -> Yellow 109 // Red -> Yellow
106 palette_[i + 128] = MakeColor(255, i * 4, 0); 110 palette_[i + 128] = MakeColor(255, i * 4, 0);
107 // Yellow -> White 111 // Yellow -> White
108 palette_[i + 192] = MakeColor(255, 255, i * 4); 112 palette_[i + 192] = MakeColor(255, 255, i * 4);
109 } 113 }
110 } 114 }
111 115
112 bool CreateContext(const pp::Size& new_size) { 116 bool CreateContext(const pp::Size& new_size) {
113 const bool kIsAlwaysOpaque = true; 117 const bool kIsAlwaysOpaque = true;
114 context_ = pp::Graphics2D(this, new_size, kIsAlwaysOpaque); 118 context_ = pp::Graphics2D(this, new_size, kIsAlwaysOpaque);
119 // Call SetScale before BindGraphics so the image is scaled correctly on
120 // HiDPI displays.
121 context_.SetScale(1.0f / device_scale_);
115 if (!BindGraphics(context_)) { 122 if (!BindGraphics(context_)) {
116 fprintf(stderr, "Unable to bind 2d context!\n"); 123 fprintf(stderr, "Unable to bind 2d context!\n");
117 context_ = pp::Graphics2D(); 124 context_ = pp::Graphics2D();
118 return false; 125 return false;
119 } 126 }
120 127
121 // Allocate a buffer of palette entries of the same size as the new context. 128 // Allocate a buffer of palette entries of the same size as the new context.
122 buffer_ = new uint8_t[new_size.width() * new_size.height()]; 129 buffer_ = new uint8_t[new_size.width() * new_size.height()];
123 size_ = new_size; 130 size_ = new_size;
124 131
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 } 182 }
176 183
177 void DrawMouse() { 184 void DrawMouse() {
178 if (!mouse_down_) 185 if (!mouse_down_)
179 return; 186 return;
180 187
181 int width = size_.width(); 188 int width = size_.width();
182 int height = size_.height(); 189 int height = size_.height();
183 190
184 // Draw a circle at the mouse position. 191 // Draw a circle at the mouse position.
185 int radius = kMouseRadius; 192 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
186 int cx = mouse_.x(); 193 int cx = mouse_.x();
187 int cy = mouse_.y(); 194 int cy = mouse_.y();
188 int minx = cx - radius <= 0 ? 1 : cx - radius; 195 int minx = cx - radius <= 0 ? 1 : cx - radius;
189 int maxx = cx + radius >= width ? width - 1 : cx + radius; 196 int maxx = cx + radius >= width ? width - 1 : cx + radius;
190 int miny = cy - radius <= 0 ? 1 : cy - radius; 197 int miny = cy - radius <= 0 ? 1 : cy - radius;
191 int maxy = cy + radius >= height ? height - 1 : cy + radius; 198 int maxy = cy + radius >= height ? height - 1 : cy + radius;
192 for (int y = miny; y < maxy; ++y) { 199 for (int y = miny; y < maxy; ++y) {
193 for (int x = minx; x < maxx; ++x) { 200 for (int x = minx; x < maxx; ++x) {
194 if ((x - cx) * (x - cx) + (y - cy) * (y - cy) < radius * radius) 201 if ((x - cx) * (x - cx) + (y - cy) * (y - cy) < radius * radius)
195 buffer_[y * width + x] = RandUint8(192, 255); 202 buffer_[y * width + x] = RandUint8(192, 255);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 } 261 }
255 262
256 pp::CompletionCallbackFactory<Graphics2DInstance> callback_factory_; 263 pp::CompletionCallbackFactory<Graphics2DInstance> callback_factory_;
257 pp::Graphics2D context_; 264 pp::Graphics2D context_;
258 pp::Graphics2D flush_context_; 265 pp::Graphics2D flush_context_;
259 pp::Size size_; 266 pp::Size size_;
260 pp::Point mouse_; 267 pp::Point mouse_;
261 bool mouse_down_; 268 bool mouse_down_;
262 uint8_t* buffer_; 269 uint8_t* buffer_;
263 uint32_t palette_[256]; 270 uint32_t palette_[256];
271 float device_scale_;
264 }; 272 };
265 273
266 class Graphics2DModule : public pp::Module { 274 class Graphics2DModule : public pp::Module {
267 public: 275 public:
268 Graphics2DModule() : pp::Module() {} 276 Graphics2DModule() : pp::Module() {}
269 virtual ~Graphics2DModule() {} 277 virtual ~Graphics2DModule() {}
270 278
271 virtual pp::Instance* CreateInstance(PP_Instance instance) { 279 virtual pp::Instance* CreateInstance(PP_Instance instance) {
272 return new Graphics2DInstance(instance); 280 return new Graphics2DInstance(instance);
273 } 281 }
274 }; 282 };
275 283
276 namespace pp { 284 namespace pp {
277 Module* CreateModule() { return new Graphics2DModule(); } 285 Module* CreateModule() { return new Graphics2DModule(); }
278 } // namespace pp 286 } // namespace pp
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698