[email protected] | 5b71eaa | 2012-06-14 22:37:05 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 190d41f | 2011-08-30 15:58:48 | [diff] [blame] | 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_graphics_3d.h" |
| 6 | |
[email protected] | d44d52b8 | 2011-09-02 16:31:39 | [diff] [blame] | 7 | #include <GLES2/gl2.h> |
[email protected] | 5b71eaa | 2012-06-14 22:37:05 | [diff] [blame] | 8 | #include <GLES2/gl2ext.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
[email protected] | d44d52b8 | 2011-09-02 16:31:39 | [diff] [blame] | 12 | |
[email protected] | d44d52b8 | 2011-09-02 16:31:39 | [diff] [blame] | 13 | #include "ppapi/c/dev/ppb_testing_dev.h" |
[email protected] | 01680778 | 2011-09-15 17:29:20 | [diff] [blame] | 14 | #include "ppapi/c/ppb_opengles2.h" |
[email protected] | fae0e94 | 2011-09-07 17:10:46 | [diff] [blame] | 15 | #include "ppapi/cpp/graphics_3d.h" |
[email protected] | 190d41f | 2011-08-30 15:58:48 | [diff] [blame] | 16 | #include "ppapi/cpp/module.h" |
[email protected] | 5b71eaa | 2012-06-14 22:37:05 | [diff] [blame] | 17 | #include "ppapi/lib/gl/gles2/gl2ext_ppapi.h" |
[email protected] | d44d52b8 | 2011-09-02 16:31:39 | [diff] [blame] | 18 | #include "ppapi/tests/test_utils.h" |
| 19 | #include "ppapi/tests/testing_instance.h" |
[email protected] | 190d41f | 2011-08-30 15:58:48 | [diff] [blame] | 20 | |
[email protected] | 5b71eaa | 2012-06-14 22:37:05 | [diff] [blame] | 21 | const int32_t kInvalidContext = 0; |
| 22 | |
[email protected] | 190d41f | 2011-08-30 15:58:48 | [diff] [blame] | 23 | REGISTER_TEST_CASE(Graphics3D); |
| 24 | |
| 25 | bool TestGraphics3D::Init() { |
[email protected] | 4d52923 | 2011-12-08 22:31:21 | [diff] [blame] | 26 | opengl_es2_ = static_cast<const PPB_OpenGLES2*>( |
[email protected] | fae0e94 | 2011-09-07 17:10:46 | [diff] [blame] | 27 | pp::Module::Get()->GetBrowserInterface(PPB_OPENGLES2_INTERFACE)); |
[email protected] | 5b71eaa | 2012-06-14 22:37:05 | [diff] [blame] | 28 | glInitializePPAPI(pp::Module::Get()->get_browser_interface()); |
[email protected] | a732cec | 2011-12-22 08:35:52 | [diff] [blame] | 29 | return opengl_es2_ && CheckTestingInterface(); |
[email protected] | 190d41f | 2011-08-30 15:58:48 | [diff] [blame] | 30 | } |
| 31 | |
[email protected] | 2622d6b | 2011-11-16 04:28:02 | [diff] [blame] | 32 | void TestGraphics3D::RunTests(const std::string& filter) { |
[email protected] | 5b71eaa | 2012-06-14 22:37:05 | [diff] [blame] | 33 | RUN_TEST(FramePPAPI, filter); |
| 34 | RUN_TEST(FrameGL, filter); |
| 35 | RUN_TEST(ExtensionsGL, filter); |
[email protected] | d44d52b8 | 2011-09-02 16:31:39 | [diff] [blame] | 36 | } |
| 37 | |
[email protected] | 5b71eaa | 2012-06-14 22:37:05 | [diff] [blame] | 38 | std::string TestGraphics3D::TestFramePPAPI() { |
[email protected] | d44d52b8 | 2011-09-02 16:31:39 | [diff] [blame] | 39 | const int width = 16; |
| 40 | const int height = 16; |
| 41 | const int32_t attribs[] = { |
| 42 | PP_GRAPHICS3DATTRIB_WIDTH, width, |
| 43 | PP_GRAPHICS3DATTRIB_HEIGHT, height, |
| 44 | PP_GRAPHICS3DATTRIB_NONE |
| 45 | }; |
[email protected] | 37947280 | 2011-09-13 04:59:05 | [diff] [blame] | 46 | pp::Graphics3D context(instance_, attribs); |
[email protected] | d44d52b8 | 2011-09-02 16:31:39 | [diff] [blame] | 47 | ASSERT_FALSE(context.is_null()); |
| 48 | |
[email protected] | 5b71eaa | 2012-06-14 22:37:05 | [diff] [blame] | 49 | const uint8_t red_color[4] = {255, 0, 0, 255}; |
| 50 | |
| 51 | // Access OpenGLES API through the PPAPI interface. |
[email protected] | d44d52b8 | 2011-09-02 16:31:39 | [diff] [blame] | 52 | // Clear color buffer to opaque red. |
| 53 | opengl_es2_->ClearColor(context.pp_resource(), 1.0f, 0.0f, 0.0f, 1.0f); |
| 54 | opengl_es2_->Clear(context.pp_resource(), GL_COLOR_BUFFER_BIT); |
| 55 | // Check if the color buffer has opaque red. |
[email protected] | 5b71eaa | 2012-06-14 22:37:05 | [diff] [blame] | 56 | std::string error = CheckPixelPPAPI(&context, width/2, height/2, red_color); |
[email protected] | d44d52b8 | 2011-09-02 16:31:39 | [diff] [blame] | 57 | if (!error.empty()) |
| 58 | return error; |
| 59 | |
| 60 | int32_t rv = SwapBuffersSync(&context); |
| 61 | ASSERT_EQ(rv, PP_OK); |
[email protected] | 5b71eaa | 2012-06-14 22:37:05 | [diff] [blame] | 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 = CheckPixelGL(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 | |
[email protected] | d44d52b8 | 2011-09-02 16:31:39 | [diff] [blame] | 134 | PASS(); |
| 135 | } |
| 136 | |
[email protected] | fae0e94 | 2011-09-07 17:10:46 | [diff] [blame] | 137 | int32_t TestGraphics3D::SwapBuffersSync(pp::Graphics3D* context) { |
[email protected] | d44d52b8 | 2011-09-02 16:31:39 | [diff] [blame] | 138 | TestCompletionCallback callback(instance_->pp_instance(), true); |
| 139 | int32_t rv = context->SwapBuffers(callback); |
| 140 | if (rv != PP_OK_COMPLETIONPENDING) |
| 141 | return rv; |
| 142 | |
| 143 | return callback.WaitForResult(); |
| 144 | } |
| 145 | |
[email protected] | 5b71eaa | 2012-06-14 22:37:05 | [diff] [blame] | 146 | std::string TestGraphics3D::CheckPixelPPAPI( |
[email protected] | fae0e94 | 2011-09-07 17:10:46 | [diff] [blame] | 147 | pp::Graphics3D* context, |
[email protected] | d44d52b8 | 2011-09-02 16:31:39 | [diff] [blame] | 148 | int x, int y, const uint8_t expected_color[4]) { |
| 149 | GLubyte pixel_color[4]; |
| 150 | opengl_es2_->ReadPixels(context->pp_resource(), |
| 151 | x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel_color); |
| 152 | |
| 153 | ASSERT_EQ(pixel_color[0], expected_color[0]); |
| 154 | ASSERT_EQ(pixel_color[1], expected_color[1]); |
| 155 | ASSERT_EQ(pixel_color[2], expected_color[2]); |
| 156 | ASSERT_EQ(pixel_color[3], expected_color[3]); |
| 157 | PASS(); |
[email protected] | 190d41f | 2011-08-30 15:58:48 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | 5b71eaa | 2012-06-14 22:37:05 | [diff] [blame] | 160 | std::string TestGraphics3D::CheckPixelGL( |
| 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 | |