| 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" | |
| 35 #include "ppapi/utility/completion_callback_factory.h" | |
| 36 | 34 |
| 37 static const int kStepsPerCircle = 800; | 35 static const int kStepsPerCircle = 800; |
| 38 | 36 |
| 39 void FlushCallback(void* data, int32_t result); | 37 void FlushCallback(void* data, int32_t result); |
| 40 | 38 |
| 41 void FillRect(pp::ImageData* image, int left, int top, int width, int height, | 39 void FillRect(pp::ImageData* image, int left, int top, int width, int height, |
| 42 uint32_t color) { | 40 uint32_t color) { |
| 43 for (int y = std::max(0, top); | 41 for (int y = std::max(0, top); |
| 44 y < std::min(image->size().height() - 1, top + height); | 42 y < std::min(image->size().height() - 1, top + height); |
| 45 y++) { | 43 y++) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 public: | 90 public: |
| 93 virtual void DidFetch(bool success, const std::string& data) = 0; | 91 virtual void DidFetch(bool success, const std::string& data) = 0; |
| 94 }; | 92 }; |
| 95 | 93 |
| 96 class MyFetcher { | 94 class MyFetcher { |
| 97 public: | 95 public: |
| 98 MyFetcher() : client_(NULL) { | 96 MyFetcher() : client_(NULL) { |
| 99 callback_factory_.Initialize(this); | 97 callback_factory_.Initialize(this); |
| 100 } | 98 } |
| 101 | 99 |
| 102 void Start(pp::InstancePrivate& instance, | 100 void Start(const pp::InstancePrivate& instance, |
| 103 const pp::Var& url, | 101 const pp::Var& url, |
| 104 MyFetcherClient* client) { | 102 MyFetcherClient* client) { |
| 105 pp::URLRequestInfo request(&instance); | 103 pp::URLRequestInfo request; |
| 106 request.SetURL(url); | 104 request.SetURL(url); |
| 107 request.SetMethod("GET"); | 105 request.SetMethod("GET"); |
| 108 | 106 |
| 109 loader_ = pp::URLLoader(instance); | 107 loader_ = pp::URLLoader(instance); |
| 110 client_ = client; | 108 client_ = client; |
| 111 | 109 |
| 112 pp::CompletionCallback callback = | 110 pp::CompletionCallback callback = |
| 113 callback_factory_.NewOptionalCallback(&MyFetcher::DidOpen); | 111 callback_factory_.NewOptionalCallback(&MyFetcher::DidOpen); |
| 114 int rv = loader_.Open(request, callback); | 112 int rv = loader_.Open(request, callback); |
| 115 if (rv != PP_OK_COMPLETIONPENDING) | 113 if (rv != PP_OK_COMPLETIONPENDING) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 char buf_[4096]; | 159 char buf_[4096]; |
| 162 std::string data_; | 160 std::string data_; |
| 163 }; | 161 }; |
| 164 | 162 |
| 165 class MyInstance : public pp::InstancePrivate, public MyFetcherClient { | 163 class MyInstance : public pp::InstancePrivate, public MyFetcherClient { |
| 166 public: | 164 public: |
| 167 MyInstance(PP_Instance instance) | 165 MyInstance(PP_Instance instance) |
| 168 : pp::InstancePrivate(instance), | 166 : pp::InstancePrivate(instance), |
| 169 time_at_last_check_(0.0), | 167 time_at_last_check_(0.0), |
| 170 fetcher_(NULL), | 168 fetcher_(NULL), |
| 169 width_(0), |
| 170 height_(0), |
| 171 animation_counter_(0), | 171 animation_counter_(0), |
| 172 print_settings_valid_(false), | 172 print_settings_valid_(false), |
| 173 showing_custom_cursor_(false), | 173 showing_custom_cursor_(false) { |
| 174 cursor_dimension_(50), | |
| 175 expanding_cursor_(false) { | |
| 176 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE); | 174 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE); |
| 177 } | 175 } |
| 178 | 176 |
| 179 virtual ~MyInstance() { | 177 virtual ~MyInstance() { |
| 180 if (fetcher_) { | 178 if (fetcher_) { |
| 181 delete fetcher_; | 179 delete fetcher_; |
| 182 fetcher_ = NULL; | 180 fetcher_ = NULL; |
| 183 } | 181 } |
| 184 } | 182 } |
| 185 | 183 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 213 return true; | 211 return true; |
| 214 default: | 212 default: |
| 215 return false; | 213 return false; |
| 216 } | 214 } |
| 217 } | 215 } |
| 218 | 216 |
| 219 virtual pp::Var GetInstanceObject() { | 217 virtual pp::Var GetInstanceObject() { |
| 220 return pp::VarPrivate(this, new MyScriptableObject(this)); | 218 return pp::VarPrivate(this, new MyScriptableObject(this)); |
| 221 } | 219 } |
| 222 | 220 |
| 223 pp::ImageData PaintImage(const pp::Size& size) { | 221 pp::ImageData PaintImage(int width, int height) { |
| 224 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, false); | 222 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
| 223 pp::Size(width, height), false); |
| 225 if (image.is_null()) { | 224 if (image.is_null()) { |
| 226 printf("Couldn't allocate the image data."); | 225 printf("Couldn't allocate the image data: %d, %d\n", width, height); |
| 227 return image; | 226 return image; |
| 228 } | 227 } |
| 229 | 228 |
| 230 // Fill with semitransparent gradient. | 229 // Fill with semitransparent gradient. |
| 231 for (int y = 0; y < image.size().height(); y++) { | 230 for (int y = 0; y < image.size().height(); y++) { |
| 232 char* row = &static_cast<char*>(image.data())[y * image.stride()]; | 231 char* row = &static_cast<char*>(image.data())[y * image.stride()]; |
| 233 for (int x = 0; x < image.size().width(); x++) { | 232 for (int x = 0; x < image.size().width(); x++) { |
| 234 row[x * 4 + 0] = y; | 233 row[x * 4 + 0] = y; |
| 235 row[x * 4 + 1] = y; | 234 row[x * 4 + 1] = y; |
| 236 row[x * 4 + 2] = 0; | 235 row[x * 4 + 2] = 0; |
| 237 row[x * 4 + 3] = y; | 236 row[x * 4 + 3] = y; |
| 238 } | 237 } |
| 239 } | 238 } |
| 240 | 239 |
| 241 // Draw the orbiting box. | 240 // Draw the orbiting box. |
| 242 float radians = static_cast<float>(animation_counter_) / kStepsPerCircle * | 241 float radians = static_cast<float>(animation_counter_) / kStepsPerCircle * |
| 243 2 * 3.14159265358979F; | 242 2 * 3.14159265358979F; |
| 244 | 243 |
| 245 float radius = | 244 float radius = static_cast<float>(std::min(width, height)) / 2.0f - 3.0f; |
| 246 static_cast<float>(std::min(size.width(), size.height())) / 2.0f - 3.0f; | |
| 247 int x = static_cast<int>(cos(radians) * radius + radius + 2); | 245 int x = static_cast<int>(cos(radians) * radius + radius + 2); |
| 248 int y = static_cast<int>(sin(radians) * radius + radius + 2); | 246 int y = static_cast<int>(sin(radians) * radius + radius + 2); |
| 249 | 247 |
| 250 const uint32_t box_bgra = 0x80000000; // Alpha 50%. | 248 const uint32_t box_bgra = 0x80000000; // Alpha 50%. |
| 251 FillRect(&image, x - 3, y - 3, 7, 7, box_bgra); | 249 FillRect(&image, x - 3, y - 3, 7, 7, box_bgra); |
| 252 return image; | 250 return image; |
| 253 } | 251 } |
| 254 | 252 |
| 255 void Paint() { | 253 void Paint() { |
| 256 pp::ImageData image = PaintImage(device_context_.size()); | 254 pp::ImageData image = PaintImage(width_, height_); |
| 257 if (!image.is_null()) { | 255 if (!image.is_null()) { |
| 258 device_context_.ReplaceContents(&image); | 256 device_context_.ReplaceContents(&image); |
| 259 device_context_.Flush(pp::CompletionCallback(&FlushCallback, this)); | 257 device_context_.Flush(pp::CompletionCallback(&FlushCallback, this)); |
| 260 } else { | 258 } else { |
| 261 printf("NullImage\n"); | 259 printf("NullImage: %d, %d\n", width_, height_); |
| 262 } | 260 } |
| 263 } | 261 } |
| 264 | 262 |
| 265 virtual void DidChangeView(const pp::View& view) { | 263 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { |
| 266 Log(PP_LOGLEVEL_LOG, "DidChangeView"); | 264 Log(PP_LOGLEVEL_LOG, "DidChangeView"); |
| 267 if (view.GetRect().size() == current_view_.GetRect().size()) | 265 if (position.size().width() == width_ && |
| 266 position.size().height() == height_) |
| 268 return; // We don't care about the position, only the size. | 267 return; // We don't care about the position, only the size. |
| 269 current_view_ = view; | |
| 270 | 268 |
| 269 width_ = position.size().width(); |
| 270 height_ = position.size().height(); |
| 271 printf("DidChangeView relevant change: width=%d height:%d\n", | 271 printf("DidChangeView relevant change: width=%d height:%d\n", |
| 272 view.GetRect().width(), view.GetRect().height()); | 272 width_, height_); |
| 273 | 273 |
| 274 device_context_ = pp::Graphics2D(this, view.GetRect().size(), false); | 274 device_context_ = pp::Graphics2D(this, pp::Size(width_, height_), false); |
| 275 if (!BindGraphics(device_context_)) { | 275 if (!BindGraphics(device_context_)) { |
| 276 printf("Couldn't bind the device context\n"); | 276 printf("Couldn't bind the device context\n"); |
| 277 return; | 277 return; |
| 278 } | 278 } |
| 279 | 279 |
| 280 Paint(); | 280 Paint(); |
| 281 } | 281 } |
| 282 | 282 |
| 283 #if defined(_WIN32) | 283 #if defined(_WIN32) |
| 284 struct timeval { | 284 struct timeval { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 char fps_text[64]; | 321 char fps_text[64]; |
| 322 sprintf(fps_text, "%g fps", | 322 sprintf(fps_text, "%g fps", |
| 323 kStepsPerCircle / (time_now - time_at_last_check_)); | 323 kStepsPerCircle / (time_now - time_at_last_check_)); |
| 324 fps.SetProperty("innerHTML", fps_text); | 324 fps.SetProperty("innerHTML", fps_text); |
| 325 } | 325 } |
| 326 | 326 |
| 327 time_at_last_check_ = time_now; | 327 time_at_last_check_ = time_now; |
| 328 } | 328 } |
| 329 | 329 |
| 330 // Print interfaces. | 330 // Print interfaces. |
| 331 virtual uint32_t QuerySupportedPrintOutputFormats() { | 331 // TODO(mball,dmichael) Replace this with the PPP_PRINTING_DEV_USE_0_4 version |
| 332 return PP_PRINTOUTPUTFORMAT_RASTER; | 332 virtual PP_PrintOutputFormat_Dev* QuerySupportedPrintOutputFormats( |
| 333 uint32_t* format_count) { |
| 334 pp::Memory_Dev memory; |
| 335 PP_PrintOutputFormat_Dev* format = |
| 336 static_cast<PP_PrintOutputFormat_Dev*>( |
| 337 memory.MemAlloc(sizeof(PP_PrintOutputFormat_Dev))); |
| 338 *format = PP_PRINTOUTPUTFORMAT_RASTER; |
| 339 *format_count = 1; |
| 340 return format; |
| 333 } | 341 } |
| 334 | 342 |
| 335 virtual int32_t PrintBegin(const PP_PrintSettings_Dev& print_settings) { | 343 virtual int32_t PrintBegin(const PP_PrintSettings_Dev& print_settings) { |
| 336 if (print_settings_.format != PP_PRINTOUTPUTFORMAT_RASTER) | 344 if (print_settings_.format != PP_PRINTOUTPUTFORMAT_RASTER) |
| 337 return 0; | 345 return 0; |
| 338 | 346 |
| 339 print_settings_ = print_settings; | 347 print_settings_ = print_settings; |
| 340 print_settings_valid_ = true; | 348 print_settings_valid_ = true; |
| 341 return 1; | 349 return 1; |
| 342 } | 350 } |
| 343 | 351 |
| 344 virtual pp::Resource PrintPages( | 352 virtual pp::Resource PrintPages( |
| 345 const PP_PrintPageNumberRange_Dev* page_ranges, | 353 const PP_PrintPageNumberRange_Dev* page_ranges, |
| 346 uint32_t page_range_count) { | 354 uint32_t page_range_count) { |
| 347 if (!print_settings_valid_) | 355 if (!print_settings_valid_) |
| 348 return pp::Resource(); | 356 return pp::Resource(); |
| 349 | 357 |
| 350 if (page_range_count != 1) | 358 if (page_range_count != 1) |
| 351 return pp::Resource(); | 359 return pp::Resource(); |
| 352 | 360 |
| 353 // Check if the page numbers are valid. We returned 1 in PrintBegin so we | 361 // Check if the page numbers are valid. We returned 1 in PrintBegin so we |
| 354 // only have 1 page to print. | 362 // only have 1 page to print. |
| 355 if (page_ranges[0].first_page_number || page_ranges[0].last_page_number) { | 363 if (page_ranges[0].first_page_number || page_ranges[0].last_page_number) { |
| 356 return pp::Resource(); | 364 return pp::Resource(); |
| 357 } | 365 } |
| 358 | 366 |
| 359 pp::Size size(static_cast<int>( | 367 int width = static_cast<int>( |
| 360 (print_settings_.printable_area.size.width / 72.0) * | 368 (print_settings_.printable_area.size.width / 72.0) * |
| 361 print_settings_.dpi), | 369 print_settings_.dpi); |
| 362 static_cast<int>( | 370 int height = static_cast<int>( |
| 363 (print_settings_.printable_area.size.height / 72.0) * | 371 (print_settings_.printable_area.size.height / 72.0) * |
| 364 print_settings_.dpi)); | 372 print_settings_.dpi); |
| 365 return PaintImage(size); | 373 |
| 374 return PaintImage(width, height); |
| 366 } | 375 } |
| 367 | 376 |
| 368 virtual void PrintEnd() { | 377 virtual void PrintEnd() { |
| 369 print_settings_valid_ = false; | 378 print_settings_valid_ = false; |
| 370 } | 379 } |
| 371 | 380 |
| 372 virtual bool IsScalingDisabled() { | |
| 373 return false; | |
| 374 } | |
| 375 | |
| 376 void OnFlush() { | 381 void OnFlush() { |
| 377 if (animation_counter_ % kStepsPerCircle == 0) | 382 if (animation_counter_ % kStepsPerCircle == 0) |
| 378 UpdateFps(); | 383 UpdateFps(); |
| 379 animation_counter_++; | 384 animation_counter_++; |
| 380 Paint(); | 385 Paint(); |
| 381 if (showing_custom_cursor_) | |
| 382 SetCursor(); | |
| 383 } | 386 } |
| 384 | 387 |
| 385 private: | 388 private: |
| 386 void SayHello() { | 389 void SayHello() { |
| 387 pp::VarPrivate window = GetWindowObject(); | 390 pp::VarPrivate window = GetWindowObject(); |
| 388 pp::VarPrivate doc = window.GetProperty("document"); | 391 pp::VarPrivate doc = window.GetProperty("document"); |
| 389 pp::VarPrivate body = doc.GetProperty("body"); | 392 pp::VarPrivate body = doc.GetProperty("body"); |
| 390 | 393 |
| 391 pp::VarPrivate obj(this, new MyScriptableObject(this)); | 394 pp::VarPrivate obj(this, new MyScriptableObject(this)); |
| 392 | 395 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 420 if (success) { | 423 if (success) { |
| 421 Log(PP_LOGLEVEL_LOG, data); | 424 Log(PP_LOGLEVEL_LOG, data); |
| 422 } else { | 425 } else { |
| 423 Log(PP_LOGLEVEL_ERROR, "Failed to download."); | 426 Log(PP_LOGLEVEL_ERROR, "Failed to download."); |
| 424 } | 427 } |
| 425 delete fetcher_; | 428 delete fetcher_; |
| 426 fetcher_ = NULL; | 429 fetcher_ = NULL; |
| 427 } | 430 } |
| 428 | 431 |
| 429 void ToggleCursor() { | 432 void ToggleCursor() { |
| 430 showing_custom_cursor_ = !showing_custom_cursor_; | |
| 431 SetCursor(); | |
| 432 } | |
| 433 | |
| 434 void SetCursor() { | |
| 435 const PPB_CursorControl_Dev* cursor_control = | 433 const PPB_CursorControl_Dev* cursor_control = |
| 436 reinterpret_cast<const PPB_CursorControl_Dev*>( | 434 reinterpret_cast<const PPB_CursorControl_Dev*>( |
| 437 pp::Module::Get()->GetBrowserInterface( | 435 pp::Module::Get()->GetBrowserInterface( |
| 438 PPB_CURSOR_CONTROL_DEV_INTERFACE)); | 436 PPB_CURSOR_CONTROL_DEV_INTERFACE)); |
| 439 if (!cursor_control) | 437 if (!cursor_control) |
| 440 return; | 438 return; |
| 441 | 439 |
| 442 if (!showing_custom_cursor_) { | 440 if (showing_custom_cursor_) { |
| 443 cursor_control->SetCursor(pp_instance(), PP_CURSORTYPE_POINTER, 0, NULL); | 441 cursor_control->SetCursor(pp_instance(), PP_CURSORTYPE_POINTER, 0, NULL); |
| 444 } else { | 442 } else { |
| 445 pp::ImageData image_data(this, pp::ImageData::GetNativeImageDataFormat(), | 443 pp::ImageData image_data(this, pp::ImageData::GetNativeImageDataFormat(), |
| 446 pp::Size(cursor_dimension_, cursor_dimension_), | 444 pp::Size(50, 50), false); |
| 447 false); | 445 FillRect(&image_data, 0, 0, 50, 50, |
| 448 FillRect(&image_data, 0, 0, cursor_dimension_, cursor_dimension_, | |
| 449 image_data.format() == PP_IMAGEDATAFORMAT_BGRA_PREMUL ? | 446 image_data.format() == PP_IMAGEDATAFORMAT_BGRA_PREMUL ? |
| 450 0x80800000 : 0x80000080); | 447 0x80800000 : 0x80000080); |
| 451 pp::Point hot_spot(cursor_dimension_ / 2, cursor_dimension_ / 2); | 448 pp::Point hot_spot(0, 0); |
| 452 cursor_control->SetCursor(pp_instance(), PP_CURSORTYPE_CUSTOM, | 449 cursor_control->SetCursor(pp_instance(), PP_CURSORTYPE_CUSTOM, |
| 453 image_data.pp_resource(), &hot_spot.pp_point()); | 450 image_data.pp_resource(), &hot_spot.pp_point()); |
| 454 if (expanding_cursor_) { | |
| 455 if (++cursor_dimension_ >= 50) | |
| 456 expanding_cursor_ = false; | |
| 457 } else { | |
| 458 if (--cursor_dimension_ <= 5) | |
| 459 expanding_cursor_ = true; | |
| 460 } | |
| 461 } | 451 } |
| 452 |
| 453 showing_custom_cursor_ = !showing_custom_cursor_; |
| 462 } | 454 } |
| 463 | 455 |
| 464 pp::Var console_; | 456 pp::Var console_; |
| 465 pp::Graphics2D device_context_; | 457 pp::Graphics2D device_context_; |
| 466 | 458 |
| 467 double time_at_last_check_; | 459 double time_at_last_check_; |
| 468 | 460 |
| 469 pp::View current_view_; | 461 MyFetcher* fetcher_; |
| 470 | 462 |
| 471 MyFetcher* fetcher_; | 463 int width_; |
| 464 int height_; |
| 472 | 465 |
| 473 // Incremented for each flush we get. | 466 // Incremented for each flush we get. |
| 474 int animation_counter_; | 467 int animation_counter_; |
| 475 bool print_settings_valid_; | 468 bool print_settings_valid_; |
| 476 PP_PrintSettings_Dev print_settings_; | 469 PP_PrintSettings_Dev print_settings_; |
| 477 | 470 |
| 478 bool showing_custom_cursor_; | 471 bool showing_custom_cursor_; |
| 479 int cursor_dimension_; | |
| 480 bool expanding_cursor_; | |
| 481 }; | 472 }; |
| 482 | 473 |
| 483 void FlushCallback(void* data, int32_t result) { | 474 void FlushCallback(void* data, int32_t result) { |
| 484 static_cast<MyInstance*>(data)->OnFlush(); | 475 static_cast<MyInstance*>(data)->OnFlush(); |
| 485 } | 476 } |
| 486 | 477 |
| 487 class MyModule : public pp::Module { | 478 class MyModule : public pp::Module { |
| 488 public: | 479 public: |
| 489 MyModule() : pp::Module() {} | 480 MyModule() : pp::Module() {} |
| 490 virtual ~MyModule() {} | 481 virtual ~MyModule() {} |
| 491 | 482 |
| 492 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 483 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 493 return new MyInstance(instance); | 484 return new MyInstance(instance); |
| 494 } | 485 } |
| 495 }; | 486 }; |
| 496 | 487 |
| 497 namespace pp { | 488 namespace pp { |
| 498 | 489 |
| 499 // Factory function for your specialization of the Module object. | 490 // Factory function for your specialization of the Module object. |
| 500 Module* CreateModule() { | 491 Module* CreateModule() { |
| 501 return new MyModule(); | 492 return new MyModule(); |
| 502 } | 493 } |
| 503 | 494 |
| 504 } // namespace pp | 495 } // namespace pp |
| OLD | NEW |