| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_graphics_2d.h" | 5 #include "ppapi/tests/test_graphics_2d.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <set> | 10 #include <set> |
| 11 | 11 |
| 12 #include "ppapi/c/dev/ppb_testing_dev.h" | 12 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 13 #include "ppapi/c/pp_errors.h" | 13 #include "ppapi/c/pp_errors.h" |
| 14 #include "ppapi/c/ppb_graphics_2d.h" | 14 #include "ppapi/c/ppb_graphics_2d.h" |
| 15 #include "ppapi/cpp/completion_callback.h" | 15 #include "ppapi/cpp/completion_callback.h" |
| 16 #include "ppapi/cpp/dev/graphics_2d_dev.h" | 16 #include "ppapi/cpp/dev/graphics_2d_dev.h" |
| 17 #include "ppapi/cpp/dev/graphics_2d_dev.h" |
| 17 #include "ppapi/cpp/graphics_2d.h" | 18 #include "ppapi/cpp/graphics_2d.h" |
| 19 #include "ppapi/cpp/graphics_3d.h" |
| 18 #include "ppapi/cpp/image_data.h" | 20 #include "ppapi/cpp/image_data.h" |
| 19 #include "ppapi/cpp/instance.h" | 21 #include "ppapi/cpp/instance.h" |
| 20 #include "ppapi/cpp/module.h" | 22 #include "ppapi/cpp/module.h" |
| 21 #include "ppapi/cpp/rect.h" | 23 #include "ppapi/cpp/rect.h" |
| 24 #include "ppapi/tests/test_utils.h" |
| 22 #include "ppapi/tests/testing_instance.h" | 25 #include "ppapi/tests/testing_instance.h" |
| 23 | 26 |
| 24 REGISTER_TEST_CASE(Graphics2D); | 27 REGISTER_TEST_CASE(Graphics2D); |
| 25 | 28 |
| 26 namespace { | 29 namespace { |
| 27 | 30 |
| 28 // A NOP flush callback for use in various tests. | 31 // A NOP flush callback for use in various tests. |
| 29 void FlushCallbackNOP(void* data, int32_t result) { | 32 void FlushCallbackNOP(void* data, int32_t result) { |
| 30 } | 33 } |
| 31 | 34 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 56 RUN_TEST(Humongous, filter); | 59 RUN_TEST(Humongous, filter); |
| 57 RUN_TEST(InitToZero, filter); | 60 RUN_TEST(InitToZero, filter); |
| 58 RUN_TEST(Describe, filter); | 61 RUN_TEST(Describe, filter); |
| 59 RUN_TEST_FORCEASYNC_AND_NOT(Paint, filter); | 62 RUN_TEST_FORCEASYNC_AND_NOT(Paint, filter); |
| 60 RUN_TEST_FORCEASYNC_AND_NOT(Scroll, filter); | 63 RUN_TEST_FORCEASYNC_AND_NOT(Scroll, filter); |
| 61 RUN_TEST_FORCEASYNC_AND_NOT(Replace, filter); | 64 RUN_TEST_FORCEASYNC_AND_NOT(Replace, filter); |
| 62 RUN_TEST_FORCEASYNC_AND_NOT(Flush, filter); | 65 RUN_TEST_FORCEASYNC_AND_NOT(Flush, filter); |
| 63 RUN_TEST_FORCEASYNC_AND_NOT(FlushOffscreenUpdate, filter); | 66 RUN_TEST_FORCEASYNC_AND_NOT(FlushOffscreenUpdate, filter); |
| 64 RUN_TEST(Dev, filter); | 67 RUN_TEST(Dev, filter); |
| 65 RUN_TEST(ReplaceContentsCaching, filter); | 68 RUN_TEST(ReplaceContentsCaching, filter); |
| 69 RUN_TEST(BindNull, filter); |
| 66 } | 70 } |
| 67 | 71 |
| 68 void TestGraphics2D::QuitMessageLoop() { | 72 void TestGraphics2D::QuitMessageLoop() { |
| 69 testing_interface_->QuitMessageLoop(instance_->pp_instance()); | 73 testing_interface_->QuitMessageLoop(instance_->pp_instance()); |
| 70 } | 74 } |
| 71 | 75 |
| 72 bool TestGraphics2D::ReadImageData(const pp::Graphics2D& dc, | 76 bool TestGraphics2D::ReadImageData(const pp::Graphics2D& dc, |
| 73 pp::ImageData* image, | 77 pp::ImageData* image, |
| 74 const pp::Point& top_left) const { | 78 const pp::Point& top_left) const { |
| 75 return PP_ToBool(testing_interface_->ReadImageData( | 79 return PP_ToBool(testing_interface_->ReadImageData( |
| 76 dc.pp_resource(), | 80 dc.pp_resource(), |
| 77 image->pp_resource(), | 81 image->pp_resource(), |
| 78 &top_left.pp_point())); | 82 &top_left.pp_point())); |
| 79 } | 83 } |
| 80 | 84 |
| 81 bool TestGraphics2D::IsDCUniformColor(const pp::Graphics2D& dc, | 85 bool TestGraphics2D::IsDCUniformColor(const pp::Graphics2D& dc, |
| 82 uint32_t color) const { | 86 uint32_t color) const { |
| 83 pp::ImageData readback(instance_, PP_IMAGEDATAFORMAT_BGRA_PREMUL, | 87 pp::ImageData readback(instance_, PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
| 84 dc.size(), false); | 88 dc.size(), false); |
| 85 if (readback.is_null()) | 89 if (readback.is_null()) |
| 86 return false; | 90 return false; |
| 87 if (!ReadImageData(dc, &readback, pp::Point(0, 0))) | 91 if (!ReadImageData(dc, &readback, pp::Point(0, 0))) |
| 88 return false; | 92 return false; |
| 89 return IsSquareInImage(readback, 0, pp::Rect(dc.size()), color); | 93 return IsSquareInImage(readback, 0, pp::Rect(dc.size()), color); |
| 90 } | 94 } |
| 91 | 95 |
| 96 bool TestGraphics2D::ResourceHealthCheck(pp::Instance* instance, |
| 97 pp::Graphics2D* context) { |
| 98 TestCompletionCallback callback(instance->pp_instance(), callback_type()); |
| 99 callback.WaitForResult(context->Flush(callback)); |
| 100 if (callback.result() < 0) |
| 101 return callback.result() != PP_ERROR_FAILED; |
| 102 else if (callback.result() == 0) |
| 103 return false; |
| 104 return true; |
| 105 } |
| 106 |
| 107 bool TestGraphics2D::ResourceHealthCheckForC(pp::Instance* instance, |
| 108 PP_Resource graphics_2d) { |
| 109 TestCompletionCallback callback(instance->pp_instance(), callback_type()); |
| 110 callback.WaitForResult(graphics_2d_interface_->Flush( |
| 111 graphics_2d, callback.GetCallback().pp_completion_callback())); |
| 112 if (callback.result() < 0) |
| 113 return callback.result() != PP_ERROR_FAILED; |
| 114 else if (callback.result() == 0) |
| 115 return false; |
| 116 return true; |
| 117 } |
| 118 |
| 92 bool TestGraphics2D::FlushAndWaitForDone(pp::Graphics2D* context) { | 119 bool TestGraphics2D::FlushAndWaitForDone(pp::Graphics2D* context) { |
| 93 int32_t flags = (force_async_ ? 0 : PP_COMPLETIONCALLBACK_FLAG_OPTIONAL); | 120 int32_t flags = (force_async_ ? 0 : PP_COMPLETIONCALLBACK_FLAG_OPTIONAL); |
| 94 pp::CompletionCallback cc(&FlushCallbackQuitMessageLoop, this, flags); | 121 pp::CompletionCallback cc(&FlushCallbackQuitMessageLoop, this, flags); |
| 95 int32_t rv = context->Flush(cc); | 122 int32_t rv = context->Flush(cc); |
| 96 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) | 123 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
| 97 return false; | 124 return false; |
| 98 if (rv == PP_OK) | 125 if (rv == PP_OK) |
| 99 return true; | 126 return true; |
| 100 if (rv != PP_OK_COMPLETIONPENDING) | 127 if (rv != PP_OK_COMPLETIONPENDING) |
| 101 return false; | 128 return false; |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 if (testing_interface_->ReadImageData(null_context.pp_resource(), | 280 if (testing_interface_->ReadImageData(null_context.pp_resource(), |
| 254 image.pp_resource(), | 281 image.pp_resource(), |
| 255 &zero_zero)) | 282 &zero_zero)) |
| 256 return "ReadImageData succeeded with a NULL resource"; | 283 return "ReadImageData succeeded with a NULL resource"; |
| 257 | 284 |
| 258 PASS(); | 285 PASS(); |
| 259 } | 286 } |
| 260 | 287 |
| 261 std::string TestGraphics2D::TestInvalidSize() { | 288 std::string TestGraphics2D::TestInvalidSize() { |
| 262 pp::Graphics2D a(instance_, pp::Size(16, 0), false); | 289 pp::Graphics2D a(instance_, pp::Size(16, 0), false); |
| 263 if (!a.is_null()) | 290 if (ResourceHealthCheck(instance_, &a)) |
| 264 return "0 height accepted"; | 291 return "0 height accepted"; |
| 265 | 292 |
| 266 pp::Graphics2D b(instance_, pp::Size(0, 16), false); | 293 pp::Graphics2D b(instance_, pp::Size(0, 16), false); |
| 267 if (!b.is_null()) | 294 if (ResourceHealthCheck(instance_, &b)) |
| 268 return "0 width accepted"; | 295 return "0 height accepted"; |
| 269 | 296 |
| 270 // Need to use the C API since pp::Size prevents negative sizes. | 297 // Need to use the C API since pp::Size prevents negative sizes. |
| 271 PP_Size size; | 298 PP_Size size; |
| 272 size.width = 16; | 299 size.width = 16; |
| 273 size.height = -16; | 300 size.height = -16; |
| 274 ASSERT_FALSE(!!graphics_2d_interface_->Create( | 301 PP_Resource graphics = graphics_2d_interface_->Create( |
| 275 instance_->pp_instance(), &size, PP_FALSE)); | 302 instance_->pp_instance(), &size, PP_FALSE); |
| 303 ASSERT_FALSE(ResourceHealthCheckForC(instance_, graphics)); |
| 276 | 304 |
| 277 size.width = -16; | 305 size.width = -16; |
| 278 size.height = 16; | 306 size.height = 16; |
| 279 ASSERT_FALSE(!!graphics_2d_interface_->Create( | 307 graphics = graphics_2d_interface_->Create( |
| 280 instance_->pp_instance(), &size, PP_FALSE)); | 308 instance_->pp_instance(), &size, PP_FALSE); |
| 309 ASSERT_FALSE(ResourceHealthCheckForC(instance_, graphics)); |
| 310 |
| 311 // Overflow to negative size |
| 312 size.width = std::numeric_limits<int32_t>::max(); |
| 313 size.height = std::numeric_limits<int32_t>::max(); |
| 314 graphics = graphics_2d_interface_->Create( |
| 315 instance_->pp_instance(), &size, PP_FALSE); |
| 316 ASSERT_FALSE(ResourceHealthCheckForC(instance_, graphics)); |
| 281 | 317 |
| 282 PASS(); | 318 PASS(); |
| 283 } | 319 } |
| 284 | 320 |
| 285 std::string TestGraphics2D::TestHumongous() { | 321 std::string TestGraphics2D::TestHumongous() { |
| 286 pp::Graphics2D a(instance_, pp::Size(100000, 100000), false); | 322 pp::Graphics2D a(instance_, pp::Size(100000, 100000), false); |
| 287 if (!a.is_null()) | 323 if (ResourceHealthCheck(instance_, &a)) |
| 288 return "Humongous device created"; | 324 return "Humongous device created"; |
| 289 PASS(); | 325 PASS(); |
| 290 } | 326 } |
| 291 | 327 |
| 292 std::string TestGraphics2D::TestInitToZero() { | 328 std::string TestGraphics2D::TestInitToZero() { |
| 293 const int w = 15, h = 17; | 329 const int w = 15, h = 17; |
| 294 pp::Graphics2D dc(instance_, pp::Size(w, h), false); | 330 pp::Graphics2D dc(instance_, pp::Size(w, h), false); |
| 295 if (dc.is_null()) | 331 if (dc.is_null()) |
| 296 return "Failure creating a boring device"; | 332 return "Failure creating a boring device"; |
| 297 | 333 |
| (...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 809 | 845 |
| 810 // Now doing more replace contents should re-use older IDs if the cache is | 846 // Now doing more replace contents should re-use older IDs if the cache is |
| 811 // working. | 847 // working. |
| 812 imageres = ReplaceContentsAndReturnID(&dc, size); | 848 imageres = ReplaceContentsAndReturnID(&dc, size); |
| 813 ASSERT_TRUE(resources.find(imageres) != resources.end()); | 849 ASSERT_TRUE(resources.find(imageres) != resources.end()); |
| 814 imageres = ReplaceContentsAndReturnID(&dc, size); | 850 imageres = ReplaceContentsAndReturnID(&dc, size); |
| 815 ASSERT_TRUE(resources.find(imageres) != resources.end()); | 851 ASSERT_TRUE(resources.find(imageres) != resources.end()); |
| 816 | 852 |
| 817 PASS(); | 853 PASS(); |
| 818 } | 854 } |
| 855 |
| 856 std::string TestGraphics2D::TestBindNull() { |
| 857 // Binding a null resource is not an error, it should clear all bound |
| 858 // resources. We can't easily test what resource is bound, but we can test |
| 859 // that this doesn't throw an error. |
| 860 ASSERT_TRUE(instance_->BindGraphics(pp::Graphics2D())); |
| 861 ASSERT_TRUE(instance_->BindGraphics(pp::Graphics3D())); |
| 862 |
| 863 const int w = 115, h = 117; |
| 864 pp::Graphics2D dc(instance_, pp::Size(w, h), false); |
| 865 if (dc.is_null()) |
| 866 return "Failure creating device."; |
| 867 if (!instance_->BindGraphics(dc)) |
| 868 return "Failure to bind the boring device."; |
| 869 |
| 870 ASSERT_TRUE(instance_->BindGraphics(pp::Graphics2D())); |
| 871 ASSERT_TRUE(instance_->BindGraphics(pp::Graphics3D())); |
| 872 |
| 873 PASS(); |
| 874 } |
| 875 |
| OLD | NEW |