blob: 1e335b78484bbc7eef3fa6a1934dc7d907b43325 [file] [log] [blame]
/*-------------------------------------------------------------------------
* drawElements Quality Program EGL Module
* ---------------------------------------
*
* Copyright 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*//*!
* \file
* \brief Test EGL_SWAP_BEHAVIOR_PRESERVED_BIT.
*//*--------------------------------------------------------------------*/
#include "teglPreservingSwapTests.hpp"
#include "tcuImageCompare.hpp"
#include "tcuTestLog.hpp"
#include "tcuSurface.hpp"
#include "tcuTextureUtil.hpp"
#include "tcuEgl.hpp"
#include "egluNativeWindow.hpp"
#include "egluUtil.hpp"
#include "gluDefs.hpp"
#include "gluRenderContext.hpp"
#include "glwDefs.hpp"
#include "glwEnums.hpp"
#include "glwFunctions.hpp"
#include "deRandom.hpp"
#include "deString.h"
#include <vector>
#include <string>
using std::vector;
using std::string;
namespace deqp
{
namespace egl
{
namespace
{
class GLES2Program;
class ReferenceProgram;
class PreservingSwapTest : public TestCase
{
public:
enum DrawType
{
DRAWTYPE_NONE = 0,
DRAWTYPE_GLES2_CLEAR,
DRAWTYPE_GLES2_RENDER
};
PreservingSwapTest (EglTestContext& eglTestCtx, bool preserveColorbuffer, bool readPixelsBeforeSwap, DrawType preSwapDrawType, DrawType postSwapDrawType, const char* name, const char* description);
~PreservingSwapTest (void);
void init (void);
void deinit (void);
IterateResult iterate (void);
private:
const int m_seed;
const bool m_preserveColorbuffer;
const bool m_readPixelsBeforeSwap;
const DrawType m_preSwapDrawType;
const DrawType m_postSwapDrawType;
eglu::NativeWindow* m_window;
tcu::egl::WindowSurface* m_eglSurface;
EGLConfig m_eglConfig;
tcu::egl::Context* m_eglContext;
glw::Functions m_gl;
GLES2Program* m_gles2Program;
ReferenceProgram* m_refProgram;
void initEGLSurface (EGLConfig config);
void initEGLContext (EGLConfig config);
};
class GLES2Program
{
public:
GLES2Program (const glw::Functions& gl);
~GLES2Program (void);
void render (int width, int height, float x1, float y1, float x2, float y2, PreservingSwapTest::DrawType drawType);
private:
const glw::Functions& m_gl;
glw::GLuint m_glProgram;
glw::GLuint m_coordLoc;
glw::GLuint m_colorLoc;
GLES2Program& operator= (const GLES2Program&);
GLES2Program (const GLES2Program&);
};
GLES2Program::GLES2Program (const glw::Functions& gl)
: m_gl (gl)
, m_glProgram (0)
, m_coordLoc ((glw::GLuint)-1)
, m_colorLoc ((glw::GLuint)-1)
{
const char* const vertexShaderSource =
"attribute mediump vec4 a_pos;\n"
"attribute mediump vec4 a_color;\n"
"varying mediump vec4 v_color;\n"
"void main(void)\n"
"{\n"
"\tv_color = a_color;\n"
"\tgl_Position = a_pos;\n"
"}";
const char* const fragmentShaderSource =
"varying mediump vec4 v_color;\n"
"void main(void)\n"
"{\n"
"\tgl_FragColor = v_color;\n"
"}";
glw::GLuint vtxShader = (glw::GLuint)-1;
glw::GLuint fragShader = (glw::GLuint)-1;
try
{
vtxShader = m_gl.createShader(GL_VERTEX_SHADER);
fragShader = m_gl.createShader(GL_FRAGMENT_SHADER);
m_glProgram = m_gl.createProgram();
GLU_EXPECT_NO_ERROR(m_gl.getError(), "Failed to create resources for shader program");
m_gl.shaderSource(vtxShader, 1, &vertexShaderSource, DE_NULL);
m_gl.shaderSource(fragShader, 1, &fragmentShaderSource, DE_NULL);
GLU_EXPECT_NO_ERROR(m_gl.getError(), "Failed to set shader sources");
m_gl.compileShader(vtxShader);
m_gl.compileShader(fragShader);
GLU_EXPECT_NO_ERROR(m_gl.getError(), "Shader compilation failed");
m_gl.attachShader(m_glProgram, vtxShader);
m_gl.attachShader(m_glProgram, fragShader);
GLU_EXPECT_NO_ERROR(m_gl.getError(), "Failed to attach shaders to program");
m_gl.linkProgram(m_glProgram);
GLU_EXPECT_NO_ERROR(m_gl.getError(), "Failed to link program");
m_gl.deleteShader(fragShader);
fragShader = (glw::GLuint)-1;
m_gl.deleteShader(vtxShader);
vtxShader = (glw::GLuint)-1;
GLU_EXPECT_NO_ERROR(m_gl.getError(), "Failed to delete shaders");
m_colorLoc = m_gl.getAttribLocation(m_glProgram, "a_color");
m_coordLoc = m_gl.getAttribLocation(m_glProgram, "a_pos");
GLU_EXPECT_NO_ERROR(m_gl.getError(), "Failed to get attribute locations");
TCU_CHECK(m_colorLoc != (glw::GLuint)-1);
TCU_CHECK(m_coordLoc != (glw::GLuint)-1);
}
catch (...)
{
if (vtxShader != (glw::GLuint)-1)
m_gl.deleteShader(vtxShader);
if (fragShader != (glw::GLuint)-1)
m_gl.deleteShader(fragShader);
if (m_glProgram != (glw::GLuint)-1)
m_gl.deleteProgram(m_glProgram);
m_glProgram = (glw::GLuint)-1;
throw;
}
}
GLES2Program::~GLES2Program (void)
{
if (m_glProgram != (glw::GLuint)-1)
m_gl.deleteProgram(m_glProgram);
}
void GLES2Program::render (int width, int height, float x1, float y1, float x2, float y2, PreservingSwapTest::DrawType drawType)
{
if (drawType == PreservingSwapTest::DRAWTYPE_GLES2_RENDER)
{
const glw::GLfloat coords[] =
{
x1, y1, 0.0f, 1.0f,
x1, y2, 0.0f, 1.0f,
x2, y2, 0.0f, 1.0f,
x2, y2, 0.0f, 1.0f,
x2, y1, 0.0f, 1.0f,
x1, y1, 0.0f, 1.0f
};
const glw::GLubyte colors[] =
{
127, 127, 127, 255,
127, 127, 127, 255,
127, 127, 127, 255,
127, 127, 127, 255,
127, 127, 127, 255,
127, 127, 127, 255
};
m_gl.useProgram(m_glProgram);
GLU_EXPECT_NO_ERROR(m_gl.getError(), "glUseProgram() failed");
m_gl.enableVertexAttribArray(m_coordLoc);
m_gl.enableVertexAttribArray(m_colorLoc);
GLU_EXPECT_NO_ERROR(m_gl.getError(), "Failed to enable attributes");
m_gl.vertexAttribPointer(m_coordLoc, 4, GL_FLOAT, GL_FALSE, 0, coords);
m_gl.vertexAttribPointer(m_colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, colors);
GLU_EXPECT_NO_ERROR(m_gl.getError(), "Failed to set attribute pointers");
m_gl.drawArrays(GL_TRIANGLES, 0, 6);
GLU_EXPECT_NO_ERROR(m_gl.getError(), "glDrawArrays() failed");
m_gl.disableVertexAttribArray(m_coordLoc);
m_gl.disableVertexAttribArray(m_colorLoc);
GLU_EXPECT_NO_ERROR(m_gl.getError(), "Failed to disable attributes");
m_gl.useProgram(0);
GLU_EXPECT_NO_ERROR(m_gl.getError(), "glUseProgram() failed");
}
else if (drawType == PreservingSwapTest::DRAWTYPE_GLES2_CLEAR)
{
const int ox = width/2;
const int oy = height/2;
const int px = width;
const int py = height;
const int x1i = (int)((px/2.0f) * x1 + ox);
const int y1i = (int)((py/2.0f) * y1 + oy);
const int x2i = (int)((px/2.0f) * x2 + ox);
const int y2i = (int)((py/2.0f) * y2 + oy);
m_gl.enable(GL_SCISSOR_TEST);
m_gl.scissor(x1i, y1i, x2i-x1i, y2i-y1i);
m_gl.clearColor(0.5f, 0.5f, 0.5f, 1.0f);
m_gl.clear(GL_COLOR_BUFFER_BIT);
m_gl.disable(GL_SCISSOR_TEST);
}
else
DE_ASSERT(false);
}
class ReferenceProgram
{
public:
ReferenceProgram (void);
~ReferenceProgram (void);
void render (tcu::Surface* target, float x1, float y1, float x2, float y2, PreservingSwapTest::DrawType drawType);
private:
ReferenceProgram (const ReferenceProgram&);
ReferenceProgram& operator= (const ReferenceProgram&);
};
ReferenceProgram::ReferenceProgram (void)
{
}
ReferenceProgram::~ReferenceProgram (void)
{
}
void ReferenceProgram::render (tcu::Surface* target, float x1, float y1, float x2, float y2, PreservingSwapTest::DrawType drawType)
{
if (drawType == PreservingSwapTest::DRAWTYPE_GLES2_RENDER || drawType == PreservingSwapTest::DRAWTYPE_GLES2_CLEAR)
{
const int ox = target->getWidth()/2;
const int oy = target->getHeight()/2;
const int px = target->getWidth();
const int py = target->getHeight();
const int x1i = (int)((px/2.0) * x1 + ox);
const int y1i = (int)((py/2.0) * y1 + oy);
const int x2i = (int)((px/2.0) * x2 + ox);
const int y2i = (int)((py/2.0) * y2 + oy);
const tcu::RGBA color(127, 127, 127, 255);
for (int y = y1i; y <= y2i; y++)
{
for (int x = x1i; x <= x2i; x++)
target->setPixel(x, y, color);
}
}
else
DE_ASSERT(false);
}
PreservingSwapTest::PreservingSwapTest (EglTestContext& eglTestCtx, bool preserveColorbuffer, bool readPixelsBeforeSwap, DrawType preSwapDrawType, DrawType postSwapDrawType, const char* name, const char* description)
: TestCase (eglTestCtx, name, description)
, m_seed (deStringHash(name))
, m_preserveColorbuffer (preserveColorbuffer)
, m_readPixelsBeforeSwap (readPixelsBeforeSwap)
, m_preSwapDrawType (preSwapDrawType)
, m_postSwapDrawType (postSwapDrawType)
, m_window (DE_NULL)
, m_eglSurface (DE_NULL)
, m_eglContext (DE_NULL)
, m_gles2Program (DE_NULL)
, m_refProgram (DE_NULL)
{
}
PreservingSwapTest::~PreservingSwapTest (void)
{
deinit();
}
EGLConfig getEGLConfig (tcu::egl::Display& eglDisplay, bool preserveColorbuffer)
{
vector<EGLConfig> configs;
const EGLint attribList[] =
{
EGL_SURFACE_TYPE, EGL_WINDOW_BIT | (preserveColorbuffer ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0),
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
eglDisplay.chooseConfig(attribList, configs);
if (configs.size() == 0)
return DE_NULL;
else
return configs[0];
}
void clearColorScreen(const glw::Functions& gl, float red, float green, float blue, float alpha)
{
gl.clearColor(red, green, blue, alpha);
gl.clear(GL_COLOR_BUFFER_BIT);
}
void clearColorReference(tcu::Surface* ref, float red, float green, float blue, float alpha)
{
tcu::clear(ref->getAccess(), tcu::Vec4(red, green, blue, alpha));
}
void readPixels (const glw::Functions& gl, tcu::Surface* screen)
{
gl.readPixels(0, 0, screen->getWidth(), screen->getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, screen->getAccess().getDataPtr());
}
void PreservingSwapTest::initEGLSurface (EGLConfig config)
{
m_window = m_eglTestCtx.createNativeWindow(m_eglTestCtx.getDisplay().getEGLDisplay(), config, DE_NULL, 480, 480, eglu::parseWindowVisibility(m_testCtx.getCommandLine()));
m_eglSurface = new tcu::egl::WindowSurface(m_eglTestCtx.getDisplay(), eglu::createWindowSurface(m_eglTestCtx.getNativeDisplay(), *m_window, m_eglTestCtx.getDisplay().getEGLDisplay(), config, DE_NULL));
}
void PreservingSwapTest::initEGLContext (EGLConfig config)
{
const EGLint attribList[] =
{
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
m_eglContext = new tcu::egl::Context(m_eglTestCtx.getDisplay(), config, attribList, EGL_OPENGL_ES_API);
}
void PreservingSwapTest::init (void)
{
m_eglConfig = getEGLConfig(m_eglTestCtx.getDisplay(), m_preserveColorbuffer);
if (m_eglConfig == DE_NULL)
throw tcu::NotSupportedError("No supported config found", "", __FILE__, __LINE__);
initEGLSurface(m_eglConfig);
initEGLContext(m_eglConfig);
m_eglContext->makeCurrent(*m_eglSurface, *m_eglSurface);
m_eglTestCtx.getGLFunctions(m_gl, glu::ApiType::es(2,0));
m_gles2Program = new GLES2Program(m_gl);
m_refProgram = new ReferenceProgram();
}
void PreservingSwapTest::deinit (void)
{
delete m_refProgram;
m_refProgram = DE_NULL;
delete m_gles2Program;
m_gles2Program = DE_NULL;
delete m_eglContext;
m_eglContext = DE_NULL;
delete m_eglSurface;
m_eglSurface = DE_NULL;
delete m_window;
m_window = DE_NULL;
}
bool compareToReference (tcu::TestLog& log, const char* name, const char* description, const tcu::Surface& reference, const tcu::Surface& screen, int x, int y, int width, int height)
{
return tcu::fuzzyCompare(log, name, description, reference.getSubAccess(x, y, width, height), screen.getSubAccess(x, y, width, height), 0.05f, tcu::COMPARE_LOG_RESULT);
}
bool comparePreAndPostSwapFramebuffers (tcu::TestLog& log, const tcu::Surface& preSwap, const tcu::Surface& postSwap)
{
return tcu::pixelThresholdCompare(log, "Pre- / Post framebuffer compare", "Compare pre- and post-swap framebuffers", preSwap, postSwap, tcu::RGBA(0, 0, 0, 0), tcu::COMPARE_LOG_RESULT);
}
TestCase::IterateResult PreservingSwapTest::iterate (void)
{
tcu::TestLog& log = m_testCtx.getLog();
de::Random rnd(m_seed);
const int width = m_eglSurface->getWidth();
const int height = m_eglSurface->getHeight();
const float clearRed = rnd.getFloat();
const float clearGreen = rnd.getFloat();
const float clearBlue = rnd.getFloat();
const float clearAlpha = 1.0f;
const float preSwapX1 = -0.9f * rnd.getFloat();
const float preSwapY1 = -0.9f * rnd.getFloat();
const float preSwapX2 = 0.9f * rnd.getFloat();
const float preSwapY2 = 0.9f * rnd.getFloat();
const float postSwapX1 = -0.9f * rnd.getFloat();
const float postSwapY1 = -0.9f * rnd.getFloat();
const float postSwapX2 = 0.9f * rnd.getFloat();
const float postSwapY2 = 0.9f * rnd.getFloat();
tcu::Surface postSwapFramebufferReference(width, height);
tcu::Surface preSwapFramebufferReference(width, height);
tcu::Surface postSwapFramebuffer(width, height);
tcu::Surface preSwapFramebuffer(width, height);
if (m_preserveColorbuffer)
m_eglSurface->setAttribute(EGL_SWAP_BEHAVIOR, EGL_BUFFER_PRESERVED);
m_eglContext->makeCurrent(*m_eglSurface, *m_eglSurface);
clearColorScreen(m_gl, clearRed, clearGreen, clearBlue, clearAlpha);
if (m_readPixelsBeforeSwap)
clearColorReference(&preSwapFramebufferReference, clearRed, clearGreen, clearBlue, clearAlpha);
clearColorReference(&postSwapFramebufferReference, clearRed, clearGreen, clearBlue, clearAlpha);
if (m_preSwapDrawType != DRAWTYPE_NONE)
{
m_gles2Program->render(width, height, preSwapX1, preSwapY1, preSwapX2, preSwapY2, m_preSwapDrawType);
m_refProgram->render(&postSwapFramebufferReference, preSwapX1, preSwapY1, preSwapX2, preSwapY2, m_preSwapDrawType);
}
if (m_readPixelsBeforeSwap)
{
if (m_preSwapDrawType != DRAWTYPE_NONE)
m_refProgram->render(&preSwapFramebufferReference, preSwapX1, preSwapY1, preSwapX2, preSwapY2, m_preSwapDrawType);
readPixels(m_gl, &preSwapFramebuffer);
}
m_eglSurface->swapBuffers();
if (m_postSwapDrawType != DRAWTYPE_NONE)
{
m_refProgram->render(&postSwapFramebufferReference, postSwapX1, postSwapY1, postSwapX2, postSwapY2, m_postSwapDrawType);
m_gles2Program->render(width, height, postSwapX1, postSwapY1, postSwapX2, postSwapY2, m_postSwapDrawType);
}
readPixels(m_gl, &postSwapFramebuffer);
bool isOk = true;
if (m_preserveColorbuffer)
{
if (m_readPixelsBeforeSwap)
isOk = isOk && compareToReference(log, "Compare pre-swap framebuffer to reference", "Compare pre-swap framebuffer to reference", preSwapFramebufferReference, preSwapFramebuffer, 0, 0, width, height);
isOk = isOk && compareToReference(log, "Compare post-swap framebuffer to reference", "Compare post-swap framebuffer to reference", postSwapFramebufferReference, postSwapFramebuffer, 0, 0, width, height);
if (m_readPixelsBeforeSwap && m_postSwapDrawType == PreservingSwapTest::DRAWTYPE_NONE)
isOk = isOk && comparePreAndPostSwapFramebuffers(log, preSwapFramebuffer, postSwapFramebuffer);
}
else
{
const int ox = width/2;
const int oy = height/2;
const int px = width;
const int py = height;
const int x1i = (int)((px/2.0f) * postSwapX1 + ox);
const int y1i = (int)((py/2.0f) * postSwapY1 + oy);
const int x2i = (int)((px/2.0f) * postSwapX2 + ox);
const int y2i = (int)((py/2.0f) * postSwapY2 + oy);
if (m_readPixelsBeforeSwap)
isOk = isOk && compareToReference(log, "Compare pre-swap framebuffer to reference", "Compare pre-swap framebuffer to reference", preSwapFramebufferReference, preSwapFramebuffer, 0, 0, width, height);
DE_ASSERT(m_postSwapDrawType != DRAWTYPE_NONE);
isOk = isOk && compareToReference(log, "Compare valid are of post-swap framebuffer to reference", "Compare valid area of post-swap framebuffer to reference", postSwapFramebufferReference, postSwapFramebuffer, x1i, y1i, x2i - x1i, y2i - y1i);
}
if (isOk)
m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
else
m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
return STOP;
}
string generateTestName (PreservingSwapTest::DrawType preSwapDrawType, PreservingSwapTest::DrawType postSwapDrawType)
{
std::ostringstream stream;
if (preSwapDrawType == PreservingSwapTest::DRAWTYPE_NONE && postSwapDrawType == PreservingSwapTest::DRAWTYPE_NONE)
stream << "no_draw";
else
{
switch (preSwapDrawType)
{
case PreservingSwapTest::DRAWTYPE_NONE:
// Do nothing
break;
case PreservingSwapTest::DRAWTYPE_GLES2_RENDER:
stream << "pre_render";
break;
case PreservingSwapTest::DRAWTYPE_GLES2_CLEAR:
stream << "pre_clear";
break;
default:
DE_ASSERT(false);
}
if (preSwapDrawType != PreservingSwapTest::DRAWTYPE_NONE && postSwapDrawType != PreservingSwapTest::DRAWTYPE_NONE)
stream << "_";
switch (postSwapDrawType)
{
case PreservingSwapTest::DRAWTYPE_NONE:
// Do nothing
break;
case PreservingSwapTest::DRAWTYPE_GLES2_RENDER:
stream << "post_render";
break;
case PreservingSwapTest::DRAWTYPE_GLES2_CLEAR:
stream << "post_clear";
break;
default:
DE_ASSERT(false);
}
}
return stream.str();
}
} // anonymous
PreservingSwapTests::PreservingSwapTests (EglTestContext& eglTestCtx)
: TestCaseGroup(eglTestCtx, "preserve_swap", "Color buffer preserving swap tests")
{
}
void PreservingSwapTests::init (void)
{
const PreservingSwapTest::DrawType preSwapDrawTypes[] =
{
PreservingSwapTest::DRAWTYPE_NONE,
PreservingSwapTest::DRAWTYPE_GLES2_CLEAR,
PreservingSwapTest::DRAWTYPE_GLES2_RENDER
};
const PreservingSwapTest::DrawType postSwapDrawTypes[] =
{
PreservingSwapTest::DRAWTYPE_NONE,
PreservingSwapTest::DRAWTYPE_GLES2_CLEAR,
PreservingSwapTest::DRAWTYPE_GLES2_RENDER
};
for (int preserveNdx = 0; preserveNdx < 2; preserveNdx++)
{
const bool preserve = (preserveNdx == 0);
TestCaseGroup* const preserveGroup = new TestCaseGroup(m_eglTestCtx, (preserve ? "preserve" : "no_preserve"), "");
for (int readPixelsNdx = 0; readPixelsNdx < 2; readPixelsNdx++)
{
const bool readPixelsBeforeSwap = (readPixelsNdx == 1);
TestCaseGroup* const readPixelsBeforeSwapGroup = new TestCaseGroup(m_eglTestCtx, (readPixelsBeforeSwap ? "read_before_swap" : "no_read_before_swap"), "");
for (int preSwapDrawTypeNdx = 0; preSwapDrawTypeNdx < DE_LENGTH_OF_ARRAY(preSwapDrawTypes); preSwapDrawTypeNdx++)
{
const PreservingSwapTest::DrawType preSwapDrawType = preSwapDrawTypes[preSwapDrawTypeNdx];
for (int postSwapDrawTypeNdx = 0; postSwapDrawTypeNdx < DE_LENGTH_OF_ARRAY(postSwapDrawTypes); postSwapDrawTypeNdx++)
{
const PreservingSwapTest::DrawType postSwapDrawType = postSwapDrawTypes[postSwapDrawTypeNdx];
// If not preserving and rendering after swap, then there is nothing to verify
if (!preserve && postSwapDrawType == PreservingSwapTest::DRAWTYPE_NONE)
continue;
const std::string name = generateTestName(preSwapDrawType, postSwapDrawType);
readPixelsBeforeSwapGroup->addChild(new PreservingSwapTest(m_eglTestCtx, preserve, readPixelsBeforeSwap, preSwapDrawType, postSwapDrawType, name.c_str(), ""));
}
}
preserveGroup->addChild(readPixelsBeforeSwapGroup);
}
addChild(preserveGroup);
}
}
} // egl
} // deqp