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, Unbind, filter); |
| 53 RUN_CALLBACK_TEST(TestCompositor, Release, filter); |
| 54 RUN_CALLBACK_TEST(TestCompositor, ReleaseWithoutCommit, filter); |
| 55 RUN_CALLBACK_TEST(TestCompositor, CommitTwoTimesWithoutChange, filter); |
| 56 RUN_CALLBACK_TEST(TestCompositor, General, filter); |
| 57 } |
| 58 |
| 59 std::string TestCompositor::TestUnbind() { |
| 60 pp::Compositor compositor; |
| 61 pp::CompositorLayer layer; |
| 62 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); |
| 63 |
| 64 compositor = pp::Compositor(instance_); |
| 65 ASSERT_FALSE(compositor.is_null()); |
| 66 |
| 67 // AddLayer() does not work for an unbound compositor. |
| 68 layer = compositor.AddLayer(); |
| 69 ASSERT_TRUE(layer.is_null()); |
| 70 |
| 71 // CommitLayers() does not work for an unbound compositor. |
| 72 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 73 CHECK_CALLBACK_BEHAVIOR(callback); |
| 74 ASSERT_EQ(PP_ERROR_FAILED, callback.result()); |
| 75 |
| 76 // ResetLayers() does not work for an unbound compositor. |
| 77 ASSERT_EQ(PP_ERROR_FAILED, compositor.ResetLayers()); |
| 78 |
| 79 // Bind the compositor to the instance |
| 80 ASSERT_TRUE(instance_->BindGraphics(compositor)); |
| 81 |
| 82 // CommitLayers() should work now. |
| 83 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 84 CHECK_CALLBACK_BEHAVIOR(callback); |
| 85 ASSERT_EQ(PP_OK, callback.result()); |
| 86 |
| 87 // ResetLayer() should work now. |
| 88 ASSERT_EQ(PP_OK, compositor.ResetLayers()); |
| 89 |
| 90 // AddLayers() should work now. |
| 91 layer = compositor.AddLayer(); |
| 92 ASSERT_FALSE(layer.is_null()); |
| 93 VERIFY(SetColorLayer(layer, PP_OK)); |
| 94 |
| 95 // Unbind the compositor, all functions should return error again. |
| 96 ASSERT_TRUE(instance_->BindGraphics(pp::Compositor())); |
| 97 |
| 98 // The existing layer will become invalidated. |
| 99 VERIFY(SetColorLayer(layer, PP_ERROR_BADRESOURCE)); |
| 100 |
| 101 // All functions should return error again. |
| 102 layer = compositor.AddLayer(); |
| 103 ASSERT_TRUE(layer.is_null()); |
| 104 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 105 CHECK_CALLBACK_BEHAVIOR(callback); |
| 106 ASSERT_EQ(PP_ERROR_FAILED, callback.result()); |
| 107 ASSERT_EQ(PP_ERROR_FAILED, compositor.ResetLayers()); |
| 108 |
| 109 PASS(); |
| 110 } |
| 111 |
| 112 std::string TestCompositor::TestRelease() { |
| 113 // Setup GLES2 |
| 114 const int32_t attribs[] = { |
| 115 PP_GRAPHICS3DATTRIB_WIDTH, 16, |
| 116 PP_GRAPHICS3DATTRIB_HEIGHT, 16, |
| 117 PP_GRAPHICS3DATTRIB_NONE |
| 118 }; |
| 119 pp::Graphics3D graphics_3d(instance_, attribs); |
| 120 ASSERT_FALSE(graphics_3d.is_null()); |
| 121 glSetCurrentContextPPAPI(graphics_3d.pp_resource()); |
| 122 |
| 123 pp::Compositor compositor = pp::Compositor(instance_); |
| 124 ASSERT_FALSE(compositor.is_null()); |
| 125 |
| 126 // Bind the compositor to the instance |
| 127 ASSERT_TRUE(instance_->BindGraphics(compositor)); |
| 128 |
| 129 pp::CompositorLayer color_layer = compositor.AddLayer(); |
| 130 ASSERT_FALSE(color_layer.is_null()); |
| 131 |
| 132 VERIFY(SetColorLayer(color_layer, PP_OK)); |
| 133 |
| 134 uint32_t texture = 0; |
| 135 VERIFY(CreateTexture(&texture)); |
| 136 pp::CompositorLayer texture_layer = compositor.AddLayer(); |
| 137 ASSERT_FALSE(texture_layer.is_null()); |
| 138 TestCompletionCallback texture_release_callback(instance_->pp_instance(), |
| 139 PP_REQUIRED); |
| 140 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 141 texture_layer.SetTexture(graphics_3d, texture, pp::Size(100, 100), |
| 142 texture_release_callback.GetCallback())); |
| 143 |
| 144 pp::ImageData image; |
| 145 VERIFY(CreateImage(&image)); |
| 146 pp::CompositorLayer image_layer = compositor.AddLayer(); |
| 147 TestCompletionCallback image_release_callback(instance_->pp_instance(), |
| 148 PP_REQUIRED); |
| 149 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 150 image_layer.SetImage(image, pp::Size(100, 100), |
| 151 image_release_callback.GetCallback())); |
| 152 |
| 153 // Commit layers to the chromium compositor. |
| 154 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); |
| 155 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 156 CHECK_CALLBACK_BEHAVIOR(callback); |
| 157 ASSERT_EQ(PP_OK, callback.result()); |
| 158 |
| 159 // Release the compositor, it will unbind the compositor from the instance, |
| 160 // and then the chrome compositor will release textures and images in the |
| 161 // current layers. |
| 162 compositor = pp::Compositor(); |
| 163 |
| 164 texture_release_callback.WaitForResult(PP_OK_COMPLETIONPENDING); |
| 165 ASSERT_EQ(PP_OK, texture_release_callback.result()); |
| 166 ReleaseTexture(texture); |
| 167 |
| 168 image_release_callback.WaitForResult(PP_OK_COMPLETIONPENDING); |
| 169 ASSERT_EQ(PP_OK, image_release_callback.result()); |
| 170 |
| 171 // The layer associated to the compositor will become invalidated. |
| 172 VERIFY(SetColorLayer(color_layer, PP_ERROR_BADRESOURCE)); |
| 173 |
| 174 // Reset |
| 175 glSetCurrentContextPPAPI(0); |
| 176 |
| 177 PASS(); |
| 178 } |
| 179 |
| 180 std::string TestCompositor::TestReleaseWithoutCommit() { |
| 181 // Setup GLES2 |
| 182 const int32_t attribs[] = { |
| 183 PP_GRAPHICS3DATTRIB_WIDTH, 16, |
| 184 PP_GRAPHICS3DATTRIB_HEIGHT, 16, |
| 185 PP_GRAPHICS3DATTRIB_NONE |
| 186 }; |
| 187 pp::Graphics3D graphics_3d(instance_, attribs); |
| 188 ASSERT_FALSE(graphics_3d.is_null()); |
| 189 glSetCurrentContextPPAPI(graphics_3d.pp_resource()); |
| 190 |
| 191 pp::Compositor compositor = pp::Compositor(instance_); |
| 192 ASSERT_FALSE(compositor.is_null()); |
| 193 |
| 194 // Bind the compositor to the instance |
| 195 ASSERT_TRUE(instance_->BindGraphics(compositor)); |
| 196 |
| 197 pp::CompositorLayer color_layer = compositor.AddLayer(); |
| 198 ASSERT_FALSE(color_layer.is_null()); |
| 199 |
| 200 VERIFY(SetColorLayer(color_layer, PP_OK)); |
| 201 |
| 202 uint32_t texture = 0; |
| 203 VERIFY(CreateTexture(&texture)); |
| 204 pp::CompositorLayer texture_layer = compositor.AddLayer(); |
| 205 ASSERT_FALSE(texture_layer.is_null()); |
| 206 TestCompletionCallback texture_release_callback(instance_->pp_instance(), |
| 207 PP_REQUIRED); |
| 208 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 209 texture_layer.SetTexture(graphics_3d, texture, pp::Size(100, 100), |
| 210 texture_release_callback.GetCallback())); |
| 211 |
| 212 pp::ImageData image; |
| 213 VERIFY(CreateImage(&image)); |
| 214 pp::CompositorLayer image_layer = compositor.AddLayer(); |
| 215 TestCompletionCallback image_release_callback(instance_->pp_instance(), |
| 216 PP_REQUIRED); |
| 217 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 218 image_layer.SetImage(image, pp::Size(100, 100), |
| 219 image_release_callback.GetCallback())); |
| 220 |
| 221 // Release the compositor. |
| 222 compositor = pp::Compositor(); |
| 223 |
| 224 // All release_callbacks should be called. |
| 225 texture_release_callback.WaitForResult(PP_OK_COMPLETIONPENDING); |
| 226 ASSERT_EQ(PP_OK, texture_release_callback.result()); |
| 227 ReleaseTexture(texture); |
| 228 |
| 229 image_release_callback.WaitForResult(PP_OK_COMPLETIONPENDING); |
| 230 ASSERT_EQ(PP_OK, image_release_callback.result()); |
| 231 |
| 232 // The layer associated to the compositor will become invalidated. |
| 233 VERIFY(SetColorLayer(color_layer, PP_ERROR_BADRESOURCE)); |
| 234 |
| 235 // Reset |
| 236 glSetCurrentContextPPAPI(0); |
| 237 |
| 238 PASS(); |
| 239 } |
| 240 |
| 241 std::string TestCompositor::TestCommitTwoTimesWithoutChange() { |
| 242 pp::Compositor compositor(instance_); |
| 243 ASSERT_FALSE(compositor.is_null()); |
| 244 ASSERT_TRUE(instance_->BindGraphics(compositor)); |
| 245 pp::CompositorLayer layer = compositor.AddLayer(); |
| 246 ASSERT_FALSE(layer.is_null()); |
| 247 VERIFY(SetColorLayer(layer, PP_OK)); |
| 248 |
| 249 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); |
| 250 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 251 CHECK_CALLBACK_BEHAVIOR(callback); |
| 252 ASSERT_EQ(PP_OK, callback.result()); |
| 253 |
| 254 // CommitLayers() without any change. |
| 255 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 256 CHECK_CALLBACK_BEHAVIOR(callback); |
| 257 ASSERT_EQ(PP_OK, callback.result()); |
| 258 |
| 259 PASS(); |
| 260 } |
| 261 |
| 262 std::string TestCompositor::TestGeneral() { |
| 263 // Setup GLES2 |
| 264 const int32_t attribs[] = { |
| 265 PP_GRAPHICS3DATTRIB_WIDTH, 16, |
| 266 PP_GRAPHICS3DATTRIB_HEIGHT, 16, |
| 267 PP_GRAPHICS3DATTRIB_NONE |
| 268 }; |
| 269 pp::Graphics3D graphics_3d(instance_, attribs); |
| 270 ASSERT_FALSE(graphics_3d.is_null()); |
| 271 glSetCurrentContextPPAPI(graphics_3d.pp_resource()); |
| 272 |
| 273 // All functions should work with a bound compositor |
| 274 pp::Compositor compositor(instance_); |
| 275 ASSERT_FALSE(compositor.is_null()); |
| 276 ASSERT_TRUE(instance_->BindGraphics(compositor)); |
| 277 |
| 278 pp::CompositorLayer color_layer = compositor.AddLayer(); |
| 279 ASSERT_FALSE(color_layer.is_null()); |
| 280 VERIFY(SetColorLayer(color_layer, PP_OK)); |
| 281 |
| 282 uint32_t texture = 0; |
| 283 VERIFY(CreateTexture(&texture)); |
| 284 pp::CompositorLayer texture_layer = compositor.AddLayer(); |
| 285 ASSERT_FALSE(texture_layer.is_null()); |
| 286 TestCompletionCallback texture_release_callback(instance_->pp_instance(), |
| 287 PP_REQUIRED); |
| 288 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 289 texture_layer.SetTexture(graphics_3d, texture, pp::Size(100, 100), |
| 290 texture_release_callback.GetCallback())); |
| 291 |
| 292 pp::ImageData image; |
| 293 VERIFY(CreateImage(&image)); |
| 294 pp::CompositorLayer image_layer = compositor.AddLayer(); |
| 295 TestCompletionCallback image_release_callback(instance_->pp_instance(), |
| 296 PP_REQUIRED); |
| 297 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 298 image_layer.SetImage(image, pp::Size(100, 100), |
| 299 image_release_callback.GetCallback())); |
| 300 |
| 301 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); |
| 302 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 303 CHECK_CALLBACK_BEHAVIOR(callback); |
| 304 ASSERT_EQ(PP_OK, callback.result()); |
| 305 |
| 306 // After ResetLayers(), all layers should be invalidated. |
| 307 ASSERT_EQ(PP_OK, compositor.ResetLayers()); |
| 308 VERIFY(SetColorLayer(color_layer, PP_ERROR_BADRESOURCE)); |
| 309 |
| 310 // Commit empty layer stack to the chromium compositor, and then the texture |
| 311 // and the image will be released by the chromium compositor soon. |
| 312 callback.WaitForResult(compositor.CommitLayers(callback.GetCallback())); |
| 313 CHECK_CALLBACK_BEHAVIOR(callback); |
| 314 ASSERT_EQ(PP_OK, callback.result()); |
| 315 |
| 316 texture_release_callback.WaitForResult(PP_OK_COMPLETIONPENDING); |
| 317 ASSERT_EQ(PP_OK, texture_release_callback.result()); |
| 318 ReleaseTexture(texture); |
| 319 |
| 320 image_release_callback.WaitForResult(PP_OK_COMPLETIONPENDING); |
| 321 ASSERT_EQ(PP_OK, image_release_callback.result()); |
| 322 |
| 323 // Reset |
| 324 glSetCurrentContextPPAPI(0); |
| 325 |
| 326 PASS(); |
| 327 } |
| 328 |
| 329 std::string TestCompositor::CreateTexture(uint32_t* texture) { |
| 330 glGenTextures(1, texture); |
| 331 ASSERT_NE(0, *texture); |
| 332 glBindTexture(GL_TEXTURE_2D, *texture); |
| 333 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 400, 400, 0, |
| 334 GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 335 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 336 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 337 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 338 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 339 glBindTexture(GL_TEXTURE_2D, 0); |
| 340 |
| 341 return std::string(); |
| 342 } |
| 343 |
| 344 std::string TestCompositor::ReleaseTexture(uint32_t texture) { |
| 345 ASSERT_NE(0u, texture); |
| 346 glDeleteTextures(1, &texture); |
| 347 |
| 348 return std::string(); |
| 349 } |
| 350 |
| 351 std::string TestCompositor::CreateImage(pp::ImageData* image) { |
| 352 *image = pp::ImageData(instance_, PP_IMAGEDATAFORMAT_RGBA_PREMUL, |
| 353 pp::Size(400, 400), false); |
| 354 ASSERT_FALSE(image->is_null()); |
| 355 |
| 356 return std::string(); |
| 357 } |
| 358 |
| 359 std::string TestCompositor::SetColorLayer( |
| 360 pp::CompositorLayer layer, int32_t result) { |
| 361 ASSERT_EQ(result, layer.SetColor(255, 255, 255, 255, pp::Size(100, 100))); |
| 362 ASSERT_EQ(result, layer.SetClipRect(pp::Rect(0, 0, 50, 50))); |
| 363 ASSERT_EQ(result, layer.SetTransform(kMatrix)); |
| 364 ASSERT_EQ(result, layer.SetOpacity(128)); |
| 365 |
| 366 return std::string(); |
| 367 } |
| 368 |
| 369 |
OLD | NEW |