| OLD | NEW |
| 1 // Copyright (c) 2011 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_3d.h" | 5 #include "ppapi/tests/test_graphics_3d.h" |
| 6 | 6 |
| 7 #include <GLES2/gl2.h> | 7 #include <GLES2/gl2.h> |
| 8 #include <GLES2/gl2ext.h> |
| 9 #include <stdio.h> |
| 10 #include <stdlib.h> |
| 11 #include <string.h> |
| 8 | 12 |
| 9 #include "ppapi/c/dev/ppb_testing_dev.h" | 13 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 10 #include "ppapi/c/ppb_opengles2.h" | 14 #include "ppapi/c/ppb_opengles2.h" |
| 11 #include "ppapi/cpp/graphics_3d.h" | 15 #include "ppapi/cpp/graphics_3d.h" |
| 12 #include "ppapi/cpp/module.h" | 16 #include "ppapi/cpp/module.h" |
| 17 #include "ppapi/lib/gl/gles2/gl2ext_ppapi.h" |
| 13 #include "ppapi/tests/test_utils.h" | 18 #include "ppapi/tests/test_utils.h" |
| 14 #include "ppapi/tests/testing_instance.h" | 19 #include "ppapi/tests/testing_instance.h" |
| 15 | 20 |
| 21 const int32_t kInvalidContext = 0; |
| 22 |
| 16 REGISTER_TEST_CASE(Graphics3D); | 23 REGISTER_TEST_CASE(Graphics3D); |
| 17 | 24 |
| 18 bool TestGraphics3D::Init() { | 25 bool TestGraphics3D::Init() { |
| 19 opengl_es2_ = static_cast<const PPB_OpenGLES2*>( | 26 opengl_es2_ = static_cast<const PPB_OpenGLES2*>( |
| 20 pp::Module::Get()->GetBrowserInterface(PPB_OPENGLES2_INTERFACE)); | 27 pp::Module::Get()->GetBrowserInterface(PPB_OPENGLES2_INTERFACE)); |
| 28 glInitializePPAPI(pp::Module::Get()->get_browser_interface()); |
| 21 return opengl_es2_ && CheckTestingInterface(); | 29 return opengl_es2_ && CheckTestingInterface(); |
| 22 } | 30 } |
| 23 | 31 |
| 24 void TestGraphics3D::RunTests(const std::string& filter) { | 32 void TestGraphics3D::RunTests(const std::string& filter) { |
| 25 RUN_TEST(Frame, filter); | 33 RUN_TEST(FramePPAPI, filter); |
| 34 RUN_TEST(FrameGL, filter); |
| 35 RUN_TEST(ExtensionsGL, filter); |
| 26 } | 36 } |
| 27 | 37 |
| 28 std::string TestGraphics3D::TestFrame() { | 38 std::string TestGraphics3D::TestFramePPAPI() { |
| 29 const int width = 16; | 39 const int width = 16; |
| 30 const int height = 16; | 40 const int height = 16; |
| 31 const int32_t attribs[] = { | 41 const int32_t attribs[] = { |
| 32 PP_GRAPHICS3DATTRIB_WIDTH, width, | 42 PP_GRAPHICS3DATTRIB_WIDTH, width, |
| 33 PP_GRAPHICS3DATTRIB_HEIGHT, height, | 43 PP_GRAPHICS3DATTRIB_HEIGHT, height, |
| 34 PP_GRAPHICS3DATTRIB_NONE | 44 PP_GRAPHICS3DATTRIB_NONE |
| 35 }; | 45 }; |
| 36 pp::Graphics3D context(instance_, attribs); | 46 pp::Graphics3D context(instance_, attribs); |
| 37 ASSERT_FALSE(context.is_null()); | 47 ASSERT_FALSE(context.is_null()); |
| 38 | 48 |
| 49 const uint8_t red_color[4] = {255, 0, 0, 255}; |
| 50 |
| 51 // Access OpenGLES API through the PPAPI interface. |
| 39 // Clear color buffer to opaque red. | 52 // Clear color buffer to opaque red. |
| 40 opengl_es2_->ClearColor(context.pp_resource(), 1.0f, 0.0f, 0.0f, 1.0f); | 53 opengl_es2_->ClearColor(context.pp_resource(), 1.0f, 0.0f, 0.0f, 1.0f); |
| 41 opengl_es2_->Clear(context.pp_resource(), GL_COLOR_BUFFER_BIT); | 54 opengl_es2_->Clear(context.pp_resource(), GL_COLOR_BUFFER_BIT); |
| 42 // Check if the color buffer has opaque red. | 55 // Check if the color buffer has opaque red. |
| 43 const uint8_t red_color[4] = {255, 0, 0, 255}; | 56 std::string error = TestPixelPPAPI(&context, width/2, height/2, red_color); |
| 44 std::string error = TestPixel(&context, width/2, height/2, red_color); | |
| 45 if (!error.empty()) | 57 if (!error.empty()) |
| 46 return error; | 58 return error; |
| 47 | 59 |
| 48 int32_t rv = SwapBuffersSync(&context); | 60 int32_t rv = SwapBuffersSync(&context); |
| 49 ASSERT_EQ(rv, PP_OK); | 61 ASSERT_EQ(rv, PP_OK); |
| 62 |
| 63 PASS(); |
| 64 } |
| 65 |
| 66 std::string TestGraphics3D::TestFrameGL() { |
| 67 const int width = 16; |
| 68 const int height = 16; |
| 69 const int32_t attribs[] = { |
| 70 PP_GRAPHICS3DATTRIB_WIDTH, width, |
| 71 PP_GRAPHICS3DATTRIB_HEIGHT, height, |
| 72 PP_GRAPHICS3DATTRIB_NONE |
| 73 }; |
| 74 pp::Graphics3D context(instance_, attribs); |
| 75 ASSERT_FALSE(context.is_null()); |
| 76 |
| 77 const uint8_t red_color[4] = {255, 0, 0, 255}; |
| 78 // Perform same operations as TestFramePPAPI, but use OpenGLES API directly. |
| 79 // This is how most developers will use OpenGLES. |
| 80 glSetCurrentContextPPAPI(context.pp_resource()); |
| 81 glClearColor(1.0f, 0.0f, 0.0f, 1.0f); |
| 82 glClear(GL_COLOR_BUFFER_BIT); |
| 83 std::string error = TestPixelGL(width/2, height/2, red_color); |
| 84 glSetCurrentContextPPAPI(kInvalidContext); |
| 85 if (!error.empty()) |
| 86 return error; |
| 87 |
| 88 int32_t rv = SwapBuffersSync(&context); |
| 89 ASSERT_EQ(rv, PP_OK); |
| 90 |
| 91 PASS(); |
| 92 } |
| 93 |
| 94 std::string TestGraphics3D::TestExtensionsGL() { |
| 95 const int width = 16; |
| 96 const int height = 16; |
| 97 const int32_t attribs[] = { |
| 98 PP_GRAPHICS3DATTRIB_WIDTH, width, |
| 99 PP_GRAPHICS3DATTRIB_HEIGHT, height, |
| 100 PP_GRAPHICS3DATTRIB_NONE |
| 101 }; |
| 102 pp::Graphics3D context(instance_, attribs); |
| 103 ASSERT_FALSE(context.is_null()); |
| 104 |
| 105 glSetCurrentContextPPAPI(context.pp_resource()); |
| 106 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); |
| 107 glClear(GL_COLOR_BUFFER_BIT); |
| 108 |
| 109 // Ask about a couple of extensions via glGetString. If an extension is |
| 110 // available, try a couple of trivial calls. This test is not intended |
| 111 // to be exhaustive; check the source can compile, link, and run without |
| 112 // crashing. |
| 113 ASSERT_NE(glGetString(GL_VERSION), NULL); |
| 114 const char* ext = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS)); |
| 115 if (strstr(ext, "GL_EXT_occlusion_query_boolean")) { |
| 116 GLuint a_query; |
| 117 GLboolean is_a_query; |
| 118 glGenQueriesEXT(1, &a_query); |
| 119 ASSERT_NE(a_query, 0); |
| 120 glBeginQueryEXT(GL_ANY_SAMPLES_PASSED_EXT, a_query); |
| 121 is_a_query = glIsQueryEXT(a_query); |
| 122 ASSERT_EQ(is_a_query, GL_TRUE); |
| 123 glEndQueryEXT(GL_ANY_SAMPLES_PASSED_EXT); |
| 124 glDeleteQueriesEXT(1, &a_query); |
| 125 } |
| 126 if (strstr(ext, "GL_ANGLE_instanced_arrays")) { |
| 127 glDrawArraysInstancedANGLE(GL_TRIANGLE_STRIP, 0, 0, 0); |
| 128 } |
| 129 glSetCurrentContextPPAPI(kInvalidContext); |
| 130 |
| 131 int32_t rv = SwapBuffersSync(&context); |
| 132 ASSERT_EQ(rv, PP_OK); |
| 133 |
| 50 PASS(); | 134 PASS(); |
| 51 } | 135 } |
| 52 | 136 |
| 53 int32_t TestGraphics3D::SwapBuffersSync(pp::Graphics3D* context) { | 137 int32_t TestGraphics3D::SwapBuffersSync(pp::Graphics3D* context) { |
| 54 TestCompletionCallback callback(instance_->pp_instance(), true); | 138 TestCompletionCallback callback(instance_->pp_instance(), true); |
| 55 int32_t rv = context->SwapBuffers(callback); | 139 int32_t rv = context->SwapBuffers(callback); |
| 56 if (rv != PP_OK_COMPLETIONPENDING) | 140 if (rv != PP_OK_COMPLETIONPENDING) |
| 57 return rv; | 141 return rv; |
| 58 | 142 |
| 59 return callback.WaitForResult(); | 143 return callback.WaitForResult(); |
| 60 } | 144 } |
| 61 | 145 |
| 62 std::string TestGraphics3D::TestPixel( | 146 std::string TestGraphics3D::TestPixelPPAPI( |
| 63 pp::Graphics3D* context, | 147 pp::Graphics3D* context, |
| 64 int x, int y, const uint8_t expected_color[4]) { | 148 int x, int y, const uint8_t expected_color[4]) { |
| 65 GLubyte pixel_color[4]; | 149 GLubyte pixel_color[4]; |
| 66 opengl_es2_->ReadPixels(context->pp_resource(), | 150 opengl_es2_->ReadPixels(context->pp_resource(), |
| 67 x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel_color); | 151 x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel_color); |
| 68 | 152 |
| 69 ASSERT_EQ(pixel_color[0], expected_color[0]); | 153 ASSERT_EQ(pixel_color[0], expected_color[0]); |
| 70 ASSERT_EQ(pixel_color[1], expected_color[1]); | 154 ASSERT_EQ(pixel_color[1], expected_color[1]); |
| 71 ASSERT_EQ(pixel_color[2], expected_color[2]); | 155 ASSERT_EQ(pixel_color[2], expected_color[2]); |
| 72 ASSERT_EQ(pixel_color[3], expected_color[3]); | 156 ASSERT_EQ(pixel_color[3], expected_color[3]); |
| 73 PASS(); | 157 PASS(); |
| 74 } | 158 } |
| 75 | 159 |
| 160 std::string TestGraphics3D::TestPixelGL( |
| 161 int x, int y, const uint8_t expected_color[4]) { |
| 162 GLubyte pixel_color[4]; |
| 163 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel_color); |
| 164 |
| 165 ASSERT_EQ(pixel_color[0], expected_color[0]); |
| 166 ASSERT_EQ(pixel_color[1], expected_color[1]); |
| 167 ASSERT_EQ(pixel_color[2], expected_color[2]); |
| 168 ASSERT_EQ(pixel_color[3], expected_color[3]); |
| 169 PASS(); |
| 170 } |
| 171 |
| OLD | NEW |