| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "ppapi/tests/test_device_context_2d.h" | 5 #include "ppapi/tests/test_device_context_2d.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "ppapi/c/pp_errors.h" |
| 9 #include "ppapi/c/pp_rect.h" | 10 #include "ppapi/c/pp_rect.h" |
| 10 #include "ppapi/c/ppb_testing.h" | 11 #include "ppapi/c/ppb_testing.h" |
| 12 #include "ppapi/c/ppb_device_context_2d.h" |
| 13 #include "ppapi/cpp/completion_callback.h" |
| 11 #include "ppapi/cpp/device_context_2d.h" | 14 #include "ppapi/cpp/device_context_2d.h" |
| 12 #include "ppapi/cpp/image_data.h" | 15 #include "ppapi/cpp/image_data.h" |
| 13 #include "ppapi/cpp/instance.h" | 16 #include "ppapi/cpp/instance.h" |
| 14 #include "ppapi/cpp/module.h" | 17 #include "ppapi/cpp/module.h" |
| 15 #include "ppapi/tests/test_instance.h" | 18 #include "ppapi/tests/test_instance.h" |
| 16 | 19 |
| 17 namespace { | 20 namespace { |
| 18 | 21 |
| 19 // A NOP flush callback for use in various tests. | 22 // A NOP flush callback for use in various tests. |
| 20 void FlushCallbackNOP(PP_Resource context, void* data) { | 23 void FlushCallbackNOP(void* data, int32_t result) { |
| 21 } | 24 } |
| 22 | 25 |
| 23 void FlushCallbackQuitMessageLoop(PP_Resource context, void* data) { | 26 void FlushCallbackQuitMessageLoop(void* data, int32_t result) { |
| 24 reinterpret_cast<TestDeviceContext2D*>(data)->QuitMessageLoop(); | 27 reinterpret_cast<TestDeviceContext2D*>(data)->QuitMessageLoop(); |
| 25 } | 28 } |
| 26 | 29 |
| 27 } // namespace | 30 } // namespace |
| 28 | 31 |
| 29 bool TestDeviceContext2D::Init() { | 32 bool TestDeviceContext2D::Init() { |
| 30 device_context_interface_ = reinterpret_cast<PPB_DeviceContext2D const*>( | 33 device_context_interface_ = reinterpret_cast<PPB_DeviceContext2D const*>( |
| 31 pp::Module::Get()->GetBrowserInterface(PPB_DEVICECONTEXT2D_INTERFACE)); | 34 pp::Module::Get()->GetBrowserInterface(PPB_DEVICECONTEXT2D_INTERFACE)); |
| 32 image_data_interface_ = reinterpret_cast<PPB_ImageData const*>( | 35 image_data_interface_ = reinterpret_cast<PPB_ImageData const*>( |
| 33 pp::Module::Get()->GetBrowserInterface(PPB_IMAGEDATA_INTERFACE)); | 36 pp::Module::Get()->GetBrowserInterface(PPB_IMAGEDATA_INTERFACE)); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 if (readback.is_null()) | 81 if (readback.is_null()) |
| 79 return false; | 82 return false; |
| 80 if (!ReadImageData(dc, &readback, 0, 0)) | 83 if (!ReadImageData(dc, &readback, 0, 0)) |
| 81 return false; | 84 return false; |
| 82 return IsSquareInImage(readback, 0, | 85 return IsSquareInImage(readback, 0, |
| 83 PP_MakeRectFromXYWH(0, 0, dc.width(), dc.height()), | 86 PP_MakeRectFromXYWH(0, 0, dc.width(), dc.height()), |
| 84 color); | 87 color); |
| 85 } | 88 } |
| 86 | 89 |
| 87 bool TestDeviceContext2D::FlushAndWaitForDone(pp::DeviceContext2D* context) { | 90 bool TestDeviceContext2D::FlushAndWaitForDone(pp::DeviceContext2D* context) { |
| 88 if (!context->Flush(&FlushCallbackQuitMessageLoop, this)) | 91 pp::CompletionCallback cc(&FlushCallbackQuitMessageLoop, this); |
| 92 int32_t rv = context->Flush(cc); |
| 93 if (rv == PP_OK) |
| 94 return true; |
| 95 if (rv != PP_Error_WouldBlock) |
| 89 return false; | 96 return false; |
| 90 testing_interface_->RunMessageLoop(); | 97 testing_interface_->RunMessageLoop(); |
| 91 return true; | 98 return true; |
| 92 } | 99 } |
| 93 | 100 |
| 94 void TestDeviceContext2D::FillRectInImage(pp::ImageData* image, | 101 void TestDeviceContext2D::FillRectInImage(pp::ImageData* image, |
| 95 const PP_Rect& rect, | 102 const PP_Rect& rect, |
| 96 uint32_t color) const { | 103 uint32_t color) const { |
| 97 for (int y = rect.point.y; y < rect.point.y + rect.size.height; y++) { | 104 for (int y = rect.point.y; y < rect.point.y + rect.size.height; y++) { |
| 98 uint32_t* row = image->GetAddr32(rect.point.x, y); | 105 uint32_t* row = image->GetAddr32(rect.point.x, y); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 | 175 |
| 169 // ReplaceContents. | 176 // ReplaceContents. |
| 170 if (device_context_interface_->ReplaceContents(image.pp_resource(), | 177 if (device_context_interface_->ReplaceContents(image.pp_resource(), |
| 171 image.pp_resource())) | 178 image.pp_resource())) |
| 172 return "ReplaceContents succeeded with a different resource"; | 179 return "ReplaceContents succeeded with a different resource"; |
| 173 if (device_context_interface_->ReplaceContents(null_context.pp_resource(), | 180 if (device_context_interface_->ReplaceContents(null_context.pp_resource(), |
| 174 image.pp_resource())) | 181 image.pp_resource())) |
| 175 return "ReplaceContents succeeded with a NULL resource"; | 182 return "ReplaceContents succeeded with a NULL resource"; |
| 176 | 183 |
| 177 // Flush. | 184 // Flush. |
| 178 if (device_context_interface_->Flush(image.pp_resource(), | 185 if (device_context_interface_->Flush( |
| 179 &FlushCallbackNOP, NULL)) | 186 image.pp_resource(), |
| 187 PP_MakeCompletionCallback(&FlushCallbackNOP, NULL)) == PP_OK) |
| 180 return "Flush succeeded with a different resource"; | 188 return "Flush succeeded with a different resource"; |
| 181 if (device_context_interface_->Flush(null_context.pp_resource(), | 189 if (device_context_interface_->Flush( |
| 182 &FlushCallbackNOP, NULL)) | 190 null_context.pp_resource(), |
| 191 PP_MakeCompletionCallback(&FlushCallbackNOP, NULL)) == PP_OK) |
| 183 return "Flush succeeded with a NULL resource"; | 192 return "Flush succeeded with a NULL resource"; |
| 184 | 193 |
| 185 // ReadImageData. | 194 // ReadImageData. |
| 186 if (testing_interface_->ReadImageData(image.pp_resource(), | 195 if (testing_interface_->ReadImageData(image.pp_resource(), |
| 187 image.pp_resource(), 0, 0)) | 196 image.pp_resource(), 0, 0)) |
| 188 return "ReadImageData succeeded with a different resource"; | 197 return "ReadImageData succeeded with a different resource"; |
| 189 if (testing_interface_->ReadImageData(null_context.pp_resource(), | 198 if (testing_interface_->ReadImageData(null_context.pp_resource(), |
| 190 image.pp_resource(), 0, 0)) | 199 image.pp_resource(), 0, 0)) |
| 191 return "ReadImageData succeeded with a NULL resource"; | 200 return "ReadImageData succeeded with a NULL resource"; |
| 192 | 201 |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 if (dc.is_null()) | 430 if (dc.is_null()) |
| 422 return "Failure creating a boring device"; | 431 return "Failure creating a boring device"; |
| 423 | 432 |
| 424 // Fill the background with blue but don't flush yet. | 433 // Fill the background with blue but don't flush yet. |
| 425 pp::ImageData background(PP_IMAGEDATAFORMAT_BGRA_PREMUL, w, h, true); | 434 pp::ImageData background(PP_IMAGEDATAFORMAT_BGRA_PREMUL, w, h, true); |
| 426 if (background.is_null()) | 435 if (background.is_null()) |
| 427 return "Failure to allocate background image"; | 436 return "Failure to allocate background image"; |
| 428 if (!dc.PaintImageData(background, 0, 0, NULL)) | 437 if (!dc.PaintImageData(background, 0, 0, NULL)) |
| 429 return "Couldn't paint the background."; | 438 return "Couldn't paint the background."; |
| 430 | 439 |
| 431 if (dc.Flush(NULL, NULL)) | 440 int32_t rv = dc.Flush(pp::CompletionCallback::Block()); |
| 441 if (rv == PP_OK || rv == PP_Error_WouldBlock) |
| 432 return "Flush succeeded from the main thread with no callback."; | 442 return "Flush succeeded from the main thread with no callback."; |
| 433 | 443 |
| 434 // Test flushing with no operations still issues a callback. | 444 // Test flushing with no operations still issues a callback. |
| 435 // (This may also hang if the browser never issues the callback). | 445 // (This may also hang if the browser never issues the callback). |
| 436 pp::DeviceContext2D dc_nopaints(w, h, false); | 446 pp::DeviceContext2D dc_nopaints(w, h, false); |
| 437 if (dc.is_null()) | 447 if (dc.is_null()) |
| 438 return "Failure creating the nopaint device"; | 448 return "Failure creating the nopaint device"; |
| 439 if (!FlushAndWaitForDone(&dc_nopaints)) | 449 if (!FlushAndWaitForDone(&dc_nopaints)) |
| 440 return "Couldn't flush the nopaint device"; | 450 return "Couldn't flush the nopaint device"; |
| 441 | 451 |
| 442 // Test that multiple flushes fail if we don't get a callback in between. | 452 // Test that multiple flushes fail if we don't get a callback in between. |
| 443 if (!dc_nopaints.Flush(&FlushCallbackNOP, NULL)) | 453 rv = dc_nopaints.Flush(pp::CompletionCallback(&FlushCallbackNOP, NULL)); |
| 454 if (rv != PP_OK && rv != PP_Error_WouldBlock) |
| 444 return "Couldn't flush first time for multiple flush test."; | 455 return "Couldn't flush first time for multiple flush test."; |
| 445 if (dc_nopaints.Flush(&FlushCallbackNOP, NULL)) | 456 |
| 446 return "Second flush succeeded before callback ran."; | 457 if (rv != PP_OK) { |
| 458 // If the first flush would block, then a second should fail. |
| 459 rv = dc_nopaints.Flush(pp::CompletionCallback(&FlushCallbackNOP, NULL)); |
| 460 if (rv == PP_OK || rv == PP_Error_WouldBlock) |
| 461 return "Second flush succeeded before callback ran."; |
| 462 } |
| 447 | 463 |
| 448 return ""; | 464 return ""; |
| 449 } | 465 } |
| OLD | NEW |