OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ppapi/tests/test_compositor.h" |
| 6 |
| 7 #include <GLES2/gl2.h> |
| 8 #include <GLES2/gl2ext.h> |
| 9 #include <stdio.h> |
| 10 #include <stdlib.h> |
| 11 #include <string.h> |
| 12 |
| 13 #include "ppapi/c/ppb_opengles2.h" |
| 14 #include "ppapi/cpp/compositor.h" |
| 15 #include "ppapi/cpp/compositor_layer.h" |
| 16 #include "ppapi/cpp/image_data.h" |
| 17 #include "ppapi/lib/gl/gles2/gl2ext_ppapi.h" |
| 18 #include "ppapi/lib/gl/include/GLES2/gl2.h" |
| 19 #include "ppapi/lib/gl/include/GLES2/gl2ext.h" |
| 20 #include "ppapi/tests/test_utils.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 const float kMatrix[16] = { |
| 25 1.0f, 0.0f, 0.0f, 0.0f, |
| 26 0.0f, 1.0f, 0.0f, 0.0f, |
| 27 0.0f, 0.0f, 1.0f, 0.0f, |
| 28 0.0f, 0.0f, 0.0f, 1.0f, |
| 29 }; |
| 30 |
| 31 } // namespace |
| 32 |
| 33 REGISTER_TEST_CASE(Compositor); |
| 34 |
| 35 #define VERIFY(r) do { \ |
| 36 std::string result = (r); \ |
| 37 if (result != "") \ |
| 38 return result; \ |
| 39 } while (false) |
| 40 |
| 41 bool TestCompositor::Init() { |
| 42 if (!CheckTestingInterface()) |
| 43 return false; |
| 44 |
| 45 if (!glInitializePPAPI(pp::Module::Get()->get_browser_interface())) |
| 46 return false; |
| 47 |
| 48 return true; |
| 49 } |
| 50 |
| 51 void TestCompositor::RunTests(const std::string& filter) { |
| 52 RUN_CALLBACK_TEST(TestCompositor, Release, filter); |
| 53 RUN_CALLBACK_TEST(TestCompositor, ReleaseWithoutCommit, filter); |
| 54 RUN_CALLBACK_TEST(TestCompositor, CommitTwoTimesWithoutChange, filter); |
| 55 RUN_CALLBACK_TEST(TestCompositor, General, filter); |
| 56 |
| 57 RUN_CALLBACK_TEST(TestCompositor, ReleaseUnbound, filter); |
| 58 RUN_CALLBACK_TEST(TestCompositor, ReleaseWithoutCommitUnbound, filter); |
| 59 RUN_CALLBACK_TEST(TestCompositor, CommitTwoTimesWithoutChangeUnbound, filter); |
| 60 RUN_CALLBACK_TEST(TestCompositor, GeneralUnbound, filter); |
| 61 |
| 62 RUN_CALLBACK_TEST(TestCompositor, BindUnbind, filter); |
| 63 } |
| 64 |
| 65 std::string TestCompositor::TestRelease() { |
| 66 return TestReleaseInternal(true); |
| 67 } |
| 68 |
| 69 std::string TestCompositor::TestReleaseWithoutCommit() { |
| 70 return TestReleaseWithoutCommitInternal(true); |
| 71 } |
| 72 |
| 73 std::string TestCompositor::TestCommitTwoTimesWithoutChange() { |
| 74 return TestCommitTwoTimesWithoutChangeInternal(true); |
| 75 } |
| 76 |
| 77 std::string TestCompositor::TestGeneral() { |
| 78 return TestGeneralInternal(true); |
| 79 } |
| 80 |
| 81 std::string TestCompositor::TestReleaseUnbound() { |
| 82 return TestReleaseInternal(false); |
| 83 } |
| 84 |
| 85 std::string TestCompositor::TestReleaseWithoutCommitUnbound() { |
| 86 return TestReleaseWithoutCommitInternal(false); |
| 87 } |
| 88 |
| 89 std::string TestCompositor::TestCommitTwoTimesWithoutChangeUnbound() { |
| 90 return TestCommitTwoTimesWithoutChangeInternal(false); |
| 91 } |
| 92 |
| 93 std::string TestCompositor::TestGeneralUnbound() { |
| 94 return TestGeneralInternal(false); |
| 95 } |
| 96 |
| 97 std::string TestCompositor::TestBindUnbind() { |
| 98 // Setup GLES2 |
| 99 const int32_t attribs[] = { |
| 100 PP_GRAPHICS3DATTRIB_WIDTH, 16, |
| 101 PP_GRAPHICS3DATTRIB_HEIGHT, 16, |
| 102 PP_GRAPHICS3DATTRIB_NONE |
| 103 }; |
| 104 pp::Graphics3D graphics_3d(instance_, attribs); |
| 105 ASSERT_FALSE(graphics_3d.is_null()); |
| 106 glSetCurrentContextPPAPI(graphics_3d.pp_resource()); |
| 107 |
| 108 pp::Compositor compositor = pp::Compositor(instance_); |
| 109 ASSERT_FALSE(compositor.is_null()); |
| 110 |
| 111 // Add layers on an unbound compositor. |
| 112 pp::CompositorLayer color_layer = compositor.AddLayer(); |
| 113 ASSERT_FALSE(color_layer.is_null()); |
| 114 |
| 115 VERIFY(SetColorLayer(color_layer, PP_OK)); |
| 116 |
| 117 uint32_t texture = 0; |
| 118 VERIFY(CreateTexture(&texture)); |
| 119 pp::CompositorLayer texture_layer = compositor.AddLayer(); |
| 120 ASSERT_FALSE(texture_layer.is_null()); |
| 121 TestCompletionCallback texture_release_callback(instance_->pp_instance(), |
| 122 PP_REQUIRED); |
| 123 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 124 texture_layer.SetTexture(graphics_3d, texture, pp::Size(100, 100), |
| 125 texture_release_callback.GetCallback())); |
| 126 |
| 127 pp::ImageData image; |
| 128 VERIFY(CreateImage(&image)); |
| 129 pp::CompositorLayer image_layer = compositor.AddLayer(); |
| 130 TestCompletionCallback image_release_callback(instance_->pp_instance(), |
| 131 PP_REQUIRED); |
| 132 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 133 image_layer.SetImage(image, pp::Size(100, 100), |
| 134 image_release_callback.GetCallback())); |
| 135 |
| 136 // Commit layers to the chromium compositor. |
| 137 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); |
| 138 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 139 CHECK_CALLBACK_BEHAVIOR(callback); |
| 140 ASSERT_EQ(PP_OK, callback.result()); |
| 141 |
| 142 // Bind the compositor and call CommitLayers() again. |
| 143 ASSERT_TRUE(instance_->BindGraphics(compositor)); |
| 144 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 145 CHECK_CALLBACK_BEHAVIOR(callback); |
| 146 ASSERT_EQ(PP_OK, callback.result()); |
| 147 |
| 148 // Unbind the compositor and call CommitLayers() again. |
| 149 ASSERT_TRUE(instance_->BindGraphics(pp::Compositor())); |
| 150 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 151 CHECK_CALLBACK_BEHAVIOR(callback); |
| 152 ASSERT_EQ(PP_OK, callback.result()); |
| 153 |
| 154 // Release the compositor. |
| 155 compositor = pp::Compositor(); |
| 156 |
| 157 texture_release_callback.WaitForResult(PP_OK_COMPLETIONPENDING); |
| 158 ASSERT_EQ(PP_OK, texture_release_callback.result()); |
| 159 ReleaseTexture(texture); |
| 160 |
| 161 image_release_callback.WaitForResult(PP_OK_COMPLETIONPENDING); |
| 162 ASSERT_EQ(PP_OK, image_release_callback.result()); |
| 163 |
| 164 // Reset |
| 165 glSetCurrentContextPPAPI(0); |
| 166 |
| 167 PASS(); |
| 168 } |
| 169 |
| 170 std::string TestCompositor::TestReleaseInternal(bool bind) { |
| 171 // Setup GLES2 |
| 172 const int32_t attribs[] = { |
| 173 PP_GRAPHICS3DATTRIB_WIDTH, 16, |
| 174 PP_GRAPHICS3DATTRIB_HEIGHT, 16, |
| 175 PP_GRAPHICS3DATTRIB_NONE |
| 176 }; |
| 177 pp::Graphics3D graphics_3d(instance_, attribs); |
| 178 ASSERT_FALSE(graphics_3d.is_null()); |
| 179 glSetCurrentContextPPAPI(graphics_3d.pp_resource()); |
| 180 |
| 181 pp::Compositor compositor = pp::Compositor(instance_); |
| 182 ASSERT_FALSE(compositor.is_null()); |
| 183 |
| 184 // Bind the compositor to the instance |
| 185 if (bind) |
| 186 ASSERT_TRUE(instance_->BindGraphics(compositor)); |
| 187 |
| 188 pp::CompositorLayer color_layer = compositor.AddLayer(); |
| 189 ASSERT_FALSE(color_layer.is_null()); |
| 190 |
| 191 VERIFY(SetColorLayer(color_layer, PP_OK)); |
| 192 |
| 193 uint32_t texture = 0; |
| 194 VERIFY(CreateTexture(&texture)); |
| 195 pp::CompositorLayer texture_layer = compositor.AddLayer(); |
| 196 ASSERT_FALSE(texture_layer.is_null()); |
| 197 TestCompletionCallback texture_release_callback(instance_->pp_instance(), |
| 198 PP_REQUIRED); |
| 199 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 200 texture_layer.SetTexture(graphics_3d, texture, pp::Size(100, 100), |
| 201 texture_release_callback.GetCallback())); |
| 202 |
| 203 pp::ImageData image; |
| 204 VERIFY(CreateImage(&image)); |
| 205 pp::CompositorLayer image_layer = compositor.AddLayer(); |
| 206 TestCompletionCallback image_release_callback(instance_->pp_instance(), |
| 207 PP_REQUIRED); |
| 208 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 209 image_layer.SetImage(image, pp::Size(100, 100), |
| 210 image_release_callback.GetCallback())); |
| 211 |
| 212 // Commit layers to the chromium compositor. |
| 213 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); |
| 214 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 215 CHECK_CALLBACK_BEHAVIOR(callback); |
| 216 ASSERT_EQ(PP_OK, callback.result()); |
| 217 |
| 218 // Release the compositor, it will unbind the compositor from the instance, |
| 219 // and then the chrome compositor will release textures and images in the |
| 220 // current layers. |
| 221 compositor = pp::Compositor(); |
| 222 |
| 223 texture_release_callback.WaitForResult(PP_OK_COMPLETIONPENDING); |
| 224 ASSERT_EQ(PP_OK, texture_release_callback.result()); |
| 225 ReleaseTexture(texture); |
| 226 |
| 227 image_release_callback.WaitForResult(PP_OK_COMPLETIONPENDING); |
| 228 ASSERT_EQ(PP_OK, image_release_callback.result()); |
| 229 |
| 230 // Reset |
| 231 glSetCurrentContextPPAPI(0); |
| 232 |
| 233 PASS(); |
| 234 } |
| 235 |
| 236 std::string TestCompositor::TestReleaseWithoutCommitInternal(bool bind) { |
| 237 // Setup GLES2 |
| 238 const int32_t attribs[] = { |
| 239 PP_GRAPHICS3DATTRIB_WIDTH, 16, |
| 240 PP_GRAPHICS3DATTRIB_HEIGHT, 16, |
| 241 PP_GRAPHICS3DATTRIB_NONE |
| 242 }; |
| 243 pp::Graphics3D graphics_3d(instance_, attribs); |
| 244 ASSERT_FALSE(graphics_3d.is_null()); |
| 245 glSetCurrentContextPPAPI(graphics_3d.pp_resource()); |
| 246 |
| 247 pp::Compositor compositor = pp::Compositor(instance_); |
| 248 ASSERT_FALSE(compositor.is_null()); |
| 249 |
| 250 // Bind the compositor to the instance |
| 251 if (bind) |
| 252 ASSERT_TRUE(instance_->BindGraphics(compositor)); |
| 253 |
| 254 pp::CompositorLayer color_layer = compositor.AddLayer(); |
| 255 ASSERT_FALSE(color_layer.is_null()); |
| 256 |
| 257 VERIFY(SetColorLayer(color_layer, PP_OK)); |
| 258 |
| 259 uint32_t texture = 0; |
| 260 VERIFY(CreateTexture(&texture)); |
| 261 pp::CompositorLayer texture_layer = compositor.AddLayer(); |
| 262 ASSERT_FALSE(texture_layer.is_null()); |
| 263 TestCompletionCallback texture_release_callback(instance_->pp_instance(), |
| 264 PP_REQUIRED); |
| 265 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 266 texture_layer.SetTexture(graphics_3d, texture, pp::Size(100, 100), |
| 267 texture_release_callback.GetCallback())); |
| 268 |
| 269 pp::ImageData image; |
| 270 VERIFY(CreateImage(&image)); |
| 271 pp::CompositorLayer image_layer = compositor.AddLayer(); |
| 272 TestCompletionCallback image_release_callback(instance_->pp_instance(), |
| 273 PP_REQUIRED); |
| 274 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 275 image_layer.SetImage(image, pp::Size(100, 100), |
| 276 image_release_callback.GetCallback())); |
| 277 |
| 278 // Release the compositor. |
| 279 compositor = pp::Compositor(); |
| 280 |
| 281 // All release_callbacks should be called. |
| 282 texture_release_callback.WaitForResult(PP_OK_COMPLETIONPENDING); |
| 283 ASSERT_EQ(PP_OK, texture_release_callback.result()); |
| 284 ReleaseTexture(texture); |
| 285 |
| 286 image_release_callback.WaitForResult(PP_OK_COMPLETIONPENDING); |
| 287 ASSERT_EQ(PP_OK, image_release_callback.result()); |
| 288 |
| 289 // The layer associated to the compositor will become invalidated. |
| 290 VERIFY(SetColorLayer(color_layer, PP_ERROR_BADRESOURCE)); |
| 291 |
| 292 // Reset |
| 293 glSetCurrentContextPPAPI(0); |
| 294 |
| 295 PASS(); |
| 296 } |
| 297 |
| 298 std::string TestCompositor::TestCommitTwoTimesWithoutChangeInternal(bool bind) { |
| 299 pp::Compositor compositor(instance_); |
| 300 ASSERT_FALSE(compositor.is_null()); |
| 301 if (bind) |
| 302 ASSERT_TRUE(instance_->BindGraphics(compositor)); |
| 303 pp::CompositorLayer layer = compositor.AddLayer(); |
| 304 ASSERT_FALSE(layer.is_null()); |
| 305 VERIFY(SetColorLayer(layer, PP_OK)); |
| 306 |
| 307 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); |
| 308 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 309 CHECK_CALLBACK_BEHAVIOR(callback); |
| 310 ASSERT_EQ(PP_OK, callback.result()); |
| 311 |
| 312 // CommitLayers() without any change. |
| 313 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 314 CHECK_CALLBACK_BEHAVIOR(callback); |
| 315 ASSERT_EQ(PP_OK, callback.result()); |
| 316 |
| 317 PASS(); |
| 318 } |
| 319 |
| 320 std::string TestCompositor::TestGeneralInternal(bool bind) { |
| 321 // Setup GLES2 |
| 322 const int32_t attribs[] = { |
| 323 PP_GRAPHICS3DATTRIB_WIDTH, 16, |
| 324 PP_GRAPHICS3DATTRIB_HEIGHT, 16, |
| 325 PP_GRAPHICS3DATTRIB_NONE |
| 326 }; |
| 327 pp::Graphics3D graphics_3d(instance_, attribs); |
| 328 ASSERT_FALSE(graphics_3d.is_null()); |
| 329 glSetCurrentContextPPAPI(graphics_3d.pp_resource()); |
| 330 |
| 331 // All functions should work with a bound compositor |
| 332 pp::Compositor compositor(instance_); |
| 333 ASSERT_FALSE(compositor.is_null()); |
| 334 if (bind) |
| 335 ASSERT_TRUE(instance_->BindGraphics(compositor)); |
| 336 |
| 337 pp::CompositorLayer color_layer = compositor.AddLayer(); |
| 338 ASSERT_FALSE(color_layer.is_null()); |
| 339 VERIFY(SetColorLayer(color_layer, PP_OK)); |
| 340 |
| 341 uint32_t texture = 0; |
| 342 VERIFY(CreateTexture(&texture)); |
| 343 pp::CompositorLayer texture_layer = compositor.AddLayer(); |
| 344 ASSERT_FALSE(texture_layer.is_null()); |
| 345 TestCompletionCallback texture_release_callback(instance_->pp_instance(), |
| 346 PP_REQUIRED); |
| 347 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 348 texture_layer.SetTexture(graphics_3d, texture, pp::Size(100, 100), |
| 349 texture_release_callback.GetCallback())); |
| 350 |
| 351 pp::ImageData image; |
| 352 VERIFY(CreateImage(&image)); |
| 353 pp::CompositorLayer image_layer = compositor.AddLayer(); |
| 354 TestCompletionCallback image_release_callback(instance_->pp_instance(), |
| 355 PP_REQUIRED); |
| 356 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 357 image_layer.SetImage(image, pp::Size(100, 100), |
| 358 image_release_callback.GetCallback())); |
| 359 |
| 360 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); |
| 361 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 362 CHECK_CALLBACK_BEHAVIOR(callback); |
| 363 ASSERT_EQ(PP_OK, callback.result()); |
| 364 |
| 365 // After ResetLayers(), all layers should be invalidated. |
| 366 ASSERT_EQ(PP_OK, compositor.ResetLayers()); |
| 367 VERIFY(SetColorLayer(color_layer, PP_ERROR_BADRESOURCE)); |
| 368 |
| 369 // Commit empty layer stack to the chromium compositor, and then the texture |
| 370 // and the image will be released by the chromium compositor soon. |
| 371 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 372 CHECK_CALLBACK_BEHAVIOR(callback); |
| 373 ASSERT_EQ(PP_OK, callback.result()); |
| 374 |
| 375 texture_release_callback.WaitForResult(PP_OK_COMPLETIONPENDING); |
| 376 ASSERT_EQ(PP_OK, texture_release_callback.result()); |
| 377 ReleaseTexture(texture); |
| 378 |
| 379 image_release_callback.WaitForResult(PP_OK_COMPLETIONPENDING); |
| 380 ASSERT_EQ(PP_OK, image_release_callback.result()); |
| 381 |
| 382 // Reset |
| 383 glSetCurrentContextPPAPI(0); |
| 384 |
| 385 PASS(); |
| 386 } |
| 387 |
| 388 std::string TestCompositor::CreateTexture(uint32_t* texture) { |
| 389 glGenTextures(1, texture); |
| 390 ASSERT_NE(0, *texture); |
| 391 glBindTexture(GL_TEXTURE_2D, *texture); |
| 392 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 400, 400, 0, |
| 393 GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 394 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 395 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 396 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 397 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 398 glBindTexture(GL_TEXTURE_2D, 0); |
| 399 |
| 400 return std::string(); |
| 401 } |
| 402 |
| 403 std::string TestCompositor::ReleaseTexture(uint32_t texture) { |
| 404 ASSERT_NE(0u, texture); |
| 405 glDeleteTextures(1, &texture); |
| 406 |
| 407 return std::string(); |
| 408 } |
| 409 |
| 410 std::string TestCompositor::CreateImage(pp::ImageData* image) { |
| 411 *image = pp::ImageData(instance_, PP_IMAGEDATAFORMAT_RGBA_PREMUL, |
| 412 pp::Size(400, 400), false); |
| 413 ASSERT_FALSE(image->is_null()); |
| 414 |
| 415 return std::string(); |
| 416 } |
| 417 |
| 418 std::string TestCompositor::SetColorLayer( |
| 419 pp::CompositorLayer layer, int32_t result) { |
| 420 ASSERT_EQ(result, layer.SetColor(255, 255, 255, 255, pp::Size(100, 100))); |
| 421 ASSERT_EQ(result, layer.SetClipRect(pp::Rect(0, 0, 50, 50))); |
| 422 ASSERT_EQ(result, layer.SetTransform(kMatrix)); |
| 423 ASSERT_EQ(result, layer.SetOpacity(128)); |
| 424 |
| 425 return std::string(); |
| 426 } |
| 427 |
| 428 |
OLD | NEW |