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.GetViewportRect().size() != current_view_.GetViewportRect().size()) |
dmichael (off chromium)
2011/12/16 04:14:31
That's not bad, though it doesn't handle the initi
brettw
2011/12/19 22:53:40
It does since the current_view_ is intitialized to
| |
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. |
270 | 268 |
271 width_ = position.size().width(); | |
272 height_ = position.size().height(); | |
273 printf("DidChangeView relevant change: width=%d height:%d\n", | 269 printf("DidChangeView relevant change: width=%d height:%d\n", |
274 width_, height_); | 270 view.GetViewportRect().width(), view.GetViewportRect().height()); |
275 | 271 |
276 device_context_ = pp::Graphics2D(this, pp::Size(width_, height_), false); | 272 device_context_ = pp::Graphics2D(this, view.GetViewportRect.size(), false); |
277 if (!BindGraphics(device_context_)) { | 273 if (!BindGraphics(device_context_)) { |
278 printf("Couldn't bind the device context\n"); | 274 printf("Couldn't bind the device context\n"); |
279 return; | 275 return; |
280 } | 276 } |
281 | 277 |
282 Paint(); | 278 Paint(); |
283 } | 279 } |
284 | 280 |
285 #if defined(_WIN32) | 281 #if defined(_WIN32) |
286 struct timeval { | 282 struct timeval { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
351 | 347 |
352 if (page_range_count != 1) | 348 if (page_range_count != 1) |
353 return pp::Resource(); | 349 return pp::Resource(); |
354 | 350 |
355 // Check if the page numbers are valid. We returned 1 in PrintBegin so we | 351 // Check if the page numbers are valid. We returned 1 in PrintBegin so we |
356 // only have 1 page to print. | 352 // only have 1 page to print. |
357 if (page_ranges[0].first_page_number || page_ranges[0].last_page_number) { | 353 if (page_ranges[0].first_page_number || page_ranges[0].last_page_number) { |
358 return pp::Resource(); | 354 return pp::Resource(); |
359 } | 355 } |
360 | 356 |
361 int width = static_cast<int>( | 357 pp::Size size(static_cast<int>( |
362 (print_settings_.printable_area.size.width / 72.0) * | 358 (print_settings_.printable_area.size.width / 72.0) * |
363 print_settings_.dpi); | 359 print_settings_.dpi), |
364 int height = static_cast<int>( | 360 static_cast<int>( |
365 (print_settings_.printable_area.size.height / 72.0) * | 361 (print_settings_.printable_area.size.height / 72.0) * |
366 print_settings_.dpi); | 362 print_settings_.dpi)); |
367 | 363 return PaintImage(size); |
368 return PaintImage(width, height); | |
369 } | 364 } |
370 | 365 |
371 virtual void PrintEnd() { | 366 virtual void PrintEnd() { |
372 print_settings_valid_ = false; | 367 print_settings_valid_ = false; |
373 } | 368 } |
374 | 369 |
375 virtual bool IsScalingDisabled() { | 370 virtual bool IsScalingDisabled() { |
376 return false; | 371 return false; |
377 } | 372 } |
378 | 373 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
462 expanding_cursor_ = true; | 457 expanding_cursor_ = true; |
463 } | 458 } |
464 } | 459 } |
465 } | 460 } |
466 | 461 |
467 pp::Var console_; | 462 pp::Var console_; |
468 pp::Graphics2D device_context_; | 463 pp::Graphics2D device_context_; |
469 | 464 |
470 double time_at_last_check_; | 465 double time_at_last_check_; |
471 | 466 |
467 pp::View current_view_; | |
468 | |
472 MyFetcher* fetcher_; | 469 MyFetcher* fetcher_; |
473 | 470 |
474 int width_; | |
475 int height_; | |
476 | |
477 // Incremented for each flush we get. | 471 // Incremented for each flush we get. |
478 int animation_counter_; | 472 int animation_counter_; |
479 bool print_settings_valid_; | 473 bool print_settings_valid_; |
480 PP_PrintSettings_Dev print_settings_; | 474 PP_PrintSettings_Dev print_settings_; |
481 | 475 |
482 bool showing_custom_cursor_; | 476 bool showing_custom_cursor_; |
483 int cursor_dimension_; | 477 int cursor_dimension_; |
484 bool expanding_cursor_; | 478 bool expanding_cursor_; |
485 }; | 479 }; |
486 | 480 |
(...skipping 12 matching lines...) Expand all Loading... | |
499 }; | 493 }; |
500 | 494 |
501 namespace pp { | 495 namespace pp { |
502 | 496 |
503 // Factory function for your specialization of the Module object. | 497 // Factory function for your specialization of the Module object. |
504 Module* CreateModule() { | 498 Module* CreateModule() { |
505 return new MyModule(); | 499 return new MyModule(); |
506 } | 500 } |
507 | 501 |
508 } // namespace pp | 502 } // namespace pp |
OLD | NEW |