OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <math.h> | 5 #include <math.h> |
6 #include <stdio.h> // FIXME(brettw) erase me. | 6 #include <stdio.h> // FIXME(brettw) erase me. |
7 #ifndef _WIN32 | 7 #ifndef _WIN32 |
8 #include <sys/time.h> | 8 #include <sys/time.h> |
9 #else | 9 #else |
10 #include <windows.h> | 10 #include <windows.h> |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "ppapi/cpp/graphics_2d.h" | 24 #include "ppapi/cpp/graphics_2d.h" |
25 #include "ppapi/cpp/image_data.h" | 25 #include "ppapi/cpp/image_data.h" |
26 #include "ppapi/cpp/input_event.h" | 26 #include "ppapi/cpp/input_event.h" |
27 #include "ppapi/cpp/private/instance_private.h" | 27 #include "ppapi/cpp/private/instance_private.h" |
28 #include "ppapi/cpp/module.h" | 28 #include "ppapi/cpp/module.h" |
29 #include "ppapi/cpp/private/var_private.h" | 29 #include "ppapi/cpp/private/var_private.h" |
30 #include "ppapi/cpp/rect.h" | 30 #include "ppapi/cpp/rect.h" |
31 #include "ppapi/cpp/url_loader.h" | 31 #include "ppapi/cpp/url_loader.h" |
32 #include "ppapi/cpp/url_request_info.h" | 32 #include "ppapi/cpp/url_request_info.h" |
33 #include "ppapi/cpp/var.h" | 33 #include "ppapi/cpp/var.h" |
| 34 #include "ppapi/cpp/view.h" |
34 | 35 |
35 static const int kStepsPerCircle = 800; | 36 static const int kStepsPerCircle = 800; |
36 | 37 |
37 void FlushCallback(void* data, int32_t result); | 38 void FlushCallback(void* data, int32_t result); |
38 | 39 |
39 void FillRect(pp::ImageData* image, int left, int top, int width, int height, | 40 void FillRect(pp::ImageData* image, int left, int top, int width, int height, |
40 uint32_t color) { | 41 uint32_t color) { |
41 for (int y = std::max(0, top); | 42 for (int y = std::max(0, top); |
42 y < std::min(image->size().height() - 1, top + height); | 43 y < std::min(image->size().height() - 1, top + height); |
43 y++) { | 44 y++) { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 char buf_[4096]; | 160 char buf_[4096]; |
160 std::string data_; | 161 std::string data_; |
161 }; | 162 }; |
162 | 163 |
163 class MyInstance : public pp::InstancePrivate, public MyFetcherClient { | 164 class MyInstance : public pp::InstancePrivate, public MyFetcherClient { |
164 public: | 165 public: |
165 MyInstance(PP_Instance instance) | 166 MyInstance(PP_Instance instance) |
166 : pp::InstancePrivate(instance), | 167 : pp::InstancePrivate(instance), |
167 time_at_last_check_(0.0), | 168 time_at_last_check_(0.0), |
168 fetcher_(NULL), | 169 fetcher_(NULL), |
169 width_(0), | |
170 height_(0), | |
171 animation_counter_(0), | 170 animation_counter_(0), |
172 print_settings_valid_(false), | 171 print_settings_valid_(false), |
173 showing_custom_cursor_(false), | 172 showing_custom_cursor_(false), |
174 cursor_dimension_(50), | 173 cursor_dimension_(50), |
175 expanding_cursor_(false) { | 174 expanding_cursor_(false) { |
176 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE); | 175 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE); |
177 } | 176 } |
178 | 177 |
179 virtual ~MyInstance() { | 178 virtual ~MyInstance() { |
180 if (fetcher_) { | 179 if (fetcher_) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 return true; | 212 return true; |
214 default: | 213 default: |
215 return false; | 214 return false; |
216 } | 215 } |
217 } | 216 } |
218 | 217 |
219 virtual pp::Var GetInstanceObject() { | 218 virtual pp::Var GetInstanceObject() { |
220 return pp::VarPrivate(this, new MyScriptableObject(this)); | 219 return pp::VarPrivate(this, new MyScriptableObject(this)); |
221 } | 220 } |
222 | 221 |
223 pp::ImageData PaintImage(int width, int height) { | 222 pp::ImageData PaintImage(const pp::Size& size) { |
224 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, | 223 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, false); |
225 pp::Size(width, height), false); | |
226 if (image.is_null()) { | 224 if (image.is_null()) { |
227 printf("Couldn't allocate the image data: %d, %d\n", width, height); | 225 printf("Couldn't allocate the image data."); |
228 return image; | 226 return image; |
229 } | 227 } |
230 | 228 |
231 // Fill with semitransparent gradient. | 229 // Fill with semitransparent gradient. |
232 for (int y = 0; y < image.size().height(); y++) { | 230 for (int y = 0; y < image.size().height(); y++) { |
233 char* row = &static_cast<char*>(image.data())[y * image.stride()]; | 231 char* row = &static_cast<char*>(image.data())[y * image.stride()]; |
234 for (int x = 0; x < image.size().width(); x++) { | 232 for (int x = 0; x < image.size().width(); x++) { |
235 row[x * 4 + 0] = y; | 233 row[x * 4 + 0] = y; |
236 row[x * 4 + 1] = y; | 234 row[x * 4 + 1] = y; |
237 row[x * 4 + 2] = 0; | 235 row[x * 4 + 2] = 0; |
238 row[x * 4 + 3] = y; | 236 row[x * 4 + 3] = y; |
239 } | 237 } |
240 } | 238 } |
241 | 239 |
242 // Draw the orbiting box. | 240 // Draw the orbiting box. |
243 float radians = static_cast<float>(animation_counter_) / kStepsPerCircle * | 241 float radians = static_cast<float>(animation_counter_) / kStepsPerCircle * |
244 2 * 3.14159265358979F; | 242 2 * 3.14159265358979F; |
245 | 243 |
246 float radius = static_cast<float>(std::min(width, height)) / 2.0f - 3.0f; | 244 float radius = |
| 245 static_cast<float>(std::min(size.width(), size.height())) / 2.0f - 3.0f; |
247 int x = static_cast<int>(cos(radians) * radius + radius + 2); | 246 int x = static_cast<int>(cos(radians) * radius + radius + 2); |
248 int y = static_cast<int>(sin(radians) * radius + radius + 2); | 247 int y = static_cast<int>(sin(radians) * radius + radius + 2); |
249 | 248 |
250 const uint32_t box_bgra = 0x80000000; // Alpha 50%. | 249 const uint32_t box_bgra = 0x80000000; // Alpha 50%. |
251 FillRect(&image, x - 3, y - 3, 7, 7, box_bgra); | 250 FillRect(&image, x - 3, y - 3, 7, 7, box_bgra); |
252 return image; | 251 return image; |
253 } | 252 } |
254 | 253 |
255 void Paint() { | 254 void Paint() { |
256 pp::ImageData image = PaintImage(width_, height_); | 255 pp::ImageData image = PaintImage(device_context_.size()); |
257 if (!image.is_null()) { | 256 if (!image.is_null()) { |
258 device_context_.ReplaceContents(&image); | 257 device_context_.ReplaceContents(&image); |
259 device_context_.Flush(pp::CompletionCallback(&FlushCallback, this)); | 258 device_context_.Flush(pp::CompletionCallback(&FlushCallback, this)); |
260 } else { | 259 } else { |
261 printf("NullImage: %d, %d\n", width_, height_); | 260 printf("NullImage\n"); |
262 } | 261 } |
263 } | 262 } |
264 | 263 |
265 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { | 264 virtual void DidChangeView(const pp::View& view) { |
266 Log(PP_LOGLEVEL_LOG, "DidChangeView"); | 265 Log(PP_LOGLEVEL_LOG, "DidChangeView"); |
267 if (position.size().width() == width_ && | 266 if (view.GetRect().size() == current_view_.GetRect().size()) |
268 position.size().height() == height_) | |
269 return; // We don't care about the position, only the size. | 267 return; // We don't care about the position, only the size. |
| 268 current_view_ = view; |
270 | 269 |
271 width_ = position.size().width(); | |
272 height_ = position.size().height(); | |
273 printf("DidChangeView relevant change: width=%d height:%d\n", | 270 printf("DidChangeView relevant change: width=%d height:%d\n", |
274 width_, height_); | 271 view.GetRect().width(), view.GetRect().height()); |
275 | 272 |
276 device_context_ = pp::Graphics2D(this, pp::Size(width_, height_), false); | 273 device_context_ = pp::Graphics2D(this, view.GetRect().size(), false); |
277 if (!BindGraphics(device_context_)) { | 274 if (!BindGraphics(device_context_)) { |
278 printf("Couldn't bind the device context\n"); | 275 printf("Couldn't bind the device context\n"); |
279 return; | 276 return; |
280 } | 277 } |
281 | 278 |
282 Paint(); | 279 Paint(); |
283 } | 280 } |
284 | 281 |
285 #if defined(_WIN32) | 282 #if defined(_WIN32) |
286 struct timeval { | 283 struct timeval { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 | 348 |
352 if (page_range_count != 1) | 349 if (page_range_count != 1) |
353 return pp::Resource(); | 350 return pp::Resource(); |
354 | 351 |
355 // Check if the page numbers are valid. We returned 1 in PrintBegin so we | 352 // Check if the page numbers are valid. We returned 1 in PrintBegin so we |
356 // only have 1 page to print. | 353 // only have 1 page to print. |
357 if (page_ranges[0].first_page_number || page_ranges[0].last_page_number) { | 354 if (page_ranges[0].first_page_number || page_ranges[0].last_page_number) { |
358 return pp::Resource(); | 355 return pp::Resource(); |
359 } | 356 } |
360 | 357 |
361 int width = static_cast<int>( | 358 pp::Size size(static_cast<int>( |
362 (print_settings_.printable_area.size.width / 72.0) * | 359 (print_settings_.printable_area.size.width / 72.0) * |
363 print_settings_.dpi); | 360 print_settings_.dpi), |
364 int height = static_cast<int>( | 361 static_cast<int>( |
365 (print_settings_.printable_area.size.height / 72.0) * | 362 (print_settings_.printable_area.size.height / 72.0) * |
366 print_settings_.dpi); | 363 print_settings_.dpi)); |
367 | 364 return PaintImage(size); |
368 return PaintImage(width, height); | |
369 } | 365 } |
370 | 366 |
371 virtual void PrintEnd() { | 367 virtual void PrintEnd() { |
372 print_settings_valid_ = false; | 368 print_settings_valid_ = false; |
373 } | 369 } |
374 | 370 |
375 virtual bool IsScalingDisabled() { | 371 virtual bool IsScalingDisabled() { |
376 return false; | 372 return false; |
377 } | 373 } |
378 | 374 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 expanding_cursor_ = true; | 458 expanding_cursor_ = true; |
463 } | 459 } |
464 } | 460 } |
465 } | 461 } |
466 | 462 |
467 pp::Var console_; | 463 pp::Var console_; |
468 pp::Graphics2D device_context_; | 464 pp::Graphics2D device_context_; |
469 | 465 |
470 double time_at_last_check_; | 466 double time_at_last_check_; |
471 | 467 |
| 468 pp::View current_view_; |
| 469 |
472 MyFetcher* fetcher_; | 470 MyFetcher* fetcher_; |
473 | 471 |
474 int width_; | |
475 int height_; | |
476 | |
477 // Incremented for each flush we get. | 472 // Incremented for each flush we get. |
478 int animation_counter_; | 473 int animation_counter_; |
479 bool print_settings_valid_; | 474 bool print_settings_valid_; |
480 PP_PrintSettings_Dev print_settings_; | 475 PP_PrintSettings_Dev print_settings_; |
481 | 476 |
482 bool showing_custom_cursor_; | 477 bool showing_custom_cursor_; |
483 int cursor_dimension_; | 478 int cursor_dimension_; |
484 bool expanding_cursor_; | 479 bool expanding_cursor_; |
485 }; | 480 }; |
486 | 481 |
(...skipping 12 matching lines...) Expand all Loading... |
499 }; | 494 }; |
500 | 495 |
501 namespace pp { | 496 namespace pp { |
502 | 497 |
503 // Factory function for your specialization of the Module object. | 498 // Factory function for your specialization of the Module object. |
504 Module* CreateModule() { | 499 Module* CreateModule() { |
505 return new MyModule(); | 500 return new MyModule(); |
506 } | 501 } |
507 | 502 |
508 } // namespace pp | 503 } // namespace pp |
OLD | NEW |