blob: e1b4e336b61e5c951ab5d053ac870d9f189de928 [file] [log] [blame]
[email protected]5b71eaa2012-06-14 22:37:051// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]190d41f2011-08-30 15:58:482// 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]d44d52b82011-09-02 16:31:397#include <GLES2/gl2.h>
[email protected]5b71eaa2012-06-14 22:37:058#include <GLES2/gl2ext.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
[email protected]d44d52b82011-09-02 16:31:3912
[email protected]d44d52b82011-09-02 16:31:3913#include "ppapi/c/dev/ppb_testing_dev.h"
[email protected]016807782011-09-15 17:29:2014#include "ppapi/c/ppb_opengles2.h"
[email protected]fae0e942011-09-07 17:10:4615#include "ppapi/cpp/graphics_3d.h"
[email protected]190d41f2011-08-30 15:58:4816#include "ppapi/cpp/module.h"
[email protected]5b71eaa2012-06-14 22:37:0517#include "ppapi/lib/gl/gles2/gl2ext_ppapi.h"
[email protected]d44d52b82011-09-02 16:31:3918#include "ppapi/tests/test_utils.h"
19#include "ppapi/tests/testing_instance.h"
[email protected]190d41f2011-08-30 15:58:4820
[email protected]5b71eaa2012-06-14 22:37:0521const int32_t kInvalidContext = 0;
22
[email protected]190d41f2011-08-30 15:58:4823REGISTER_TEST_CASE(Graphics3D);
24
25bool TestGraphics3D::Init() {
[email protected]4d529232011-12-08 22:31:2126 opengl_es2_ = static_cast<const PPB_OpenGLES2*>(
[email protected]fae0e942011-09-07 17:10:4627 pp::Module::Get()->GetBrowserInterface(PPB_OPENGLES2_INTERFACE));
[email protected]5b71eaa2012-06-14 22:37:0528 glInitializePPAPI(pp::Module::Get()->get_browser_interface());
[email protected]a732cec2011-12-22 08:35:5229 return opengl_es2_ && CheckTestingInterface();
[email protected]190d41f2011-08-30 15:58:4830}
31
[email protected]2622d6b2011-11-16 04:28:0232void TestGraphics3D::RunTests(const std::string& filter) {
[email protected]5b71eaa2012-06-14 22:37:0533 RUN_TEST(FramePPAPI, filter);
34 RUN_TEST(FrameGL, filter);
35 RUN_TEST(ExtensionsGL, filter);
[email protected]d44d52b82011-09-02 16:31:3936}
37
[email protected]5b71eaa2012-06-14 22:37:0538std::string TestGraphics3D::TestFramePPAPI() {
[email protected]d44d52b82011-09-02 16:31:3939 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]379472802011-09-13 04:59:0546 pp::Graphics3D context(instance_, attribs);
[email protected]d44d52b82011-09-02 16:31:3947 ASSERT_FALSE(context.is_null());
48
[email protected]5b71eaa2012-06-14 22:37:0549 const uint8_t red_color[4] = {255, 0, 0, 255};
50
51 // Access OpenGLES API through the PPAPI interface.
[email protected]d44d52b82011-09-02 16:31:3952 // 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]5b71eaa2012-06-14 22:37:0556 std::string error = CheckPixelPPAPI(&context, width/2, height/2, red_color);
[email protected]d44d52b82011-09-02 16:31:3957 if (!error.empty())
58 return error;
59
60 int32_t rv = SwapBuffersSync(&context);
61 ASSERT_EQ(rv, PP_OK);
[email protected]5b71eaa2012-06-14 22:37:0562
63 PASS();
64}
65
66std::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
94std::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]d44d52b82011-09-02 16:31:39134 PASS();
135}
136
[email protected]fae0e942011-09-07 17:10:46137int32_t TestGraphics3D::SwapBuffersSync(pp::Graphics3D* context) {
[email protected]d44d52b82011-09-02 16:31:39138 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]5b71eaa2012-06-14 22:37:05146std::string TestGraphics3D::CheckPixelPPAPI(
[email protected]fae0e942011-09-07 17:10:46147 pp::Graphics3D* context,
[email protected]d44d52b82011-09-02 16:31:39148 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]190d41f2011-08-30 15:58:48158}
159
[email protected]5b71eaa2012-06-14 22:37:05160std::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