1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program EGL Module
3 * ---------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Tests for resizing the native window of a surface.
22 *//*--------------------------------------------------------------------*/
24 #include "teglResizeTests.hpp"
26 #include "teglSimpleConfigCase.hpp"
28 #include "tcuImageCompare.hpp"
29 #include "tcuSurface.hpp"
30 #include "tcuPlatform.hpp"
31 #include "tcuTestLog.hpp"
32 #include "tcuInterval.hpp"
33 #include "tcuTextureUtil.hpp"
34 #include "tcuResultCollector.hpp"
36 #include "egluNativeDisplay.hpp"
37 #include "egluNativeWindow.hpp"
38 #include "egluNativePixmap.hpp"
39 #include "egluUnique.hpp"
40 #include "egluUtil.hpp"
42 #include "eglwLibrary.hpp"
43 #include "eglwEnums.hpp"
45 #include "gluDefs.hpp"
46 #include "glwFunctions.hpp"
47 #include "glwEnums.hpp"
49 #include "tcuTestLog.hpp"
50 #include "tcuVector.hpp"
53 #include "deUniquePtr.hpp"
64 using std::ostringstream;
66 using tcu::CommandLine;
67 using tcu::ConstPixelBufferAccess;
73 using tcu::ResultCollector;
76 using eglu::AttribMap;
77 using eglu::NativeDisplay;
78 using eglu::NativeWindow;
79 using eglu::ScopedCurrentContext;
80 using eglu::UniqueSurface;
81 using eglu::UniqueContext;
82 using eglu::NativeWindowFactory;
83 using eglu::WindowParams;
86 typedef eglu::WindowParams::Visibility Visibility;
87 typedef TestCase::IterateResult IterateResult;
97 class ResizeTest : public TestCase
100 ResizeTest (EglTestContext& eglTestCtx,
101 const ResizeParams& params)
102 : TestCase (eglTestCtx,
104 params.description.c_str())
105 , m_oldSize (params.oldSize)
106 , m_newSize (params.newSize)
107 , m_display (EGL_NO_DISPLAY)
109 , m_log (m_testCtx.getLog())
110 , m_status (m_log) {}
116 virtual EGLenum surfaceType (void) const { return EGL_WINDOW_BIT; }
117 void resize (IVec2 size);
119 const IVec2 m_oldSize;
120 const IVec2 m_newSize;
121 EGLDisplay m_display;
123 MovePtr<NativeWindow> m_nativeWindow;
124 MovePtr<UniqueSurface> m_surface;
125 MovePtr<UniqueContext> m_context;
127 ResultCollector m_status;
131 EGLConfig getEGLConfig (const Library& egl, const EGLDisplay eglDisplay, EGLenum surfaceType)
135 attribMap[EGL_SURFACE_TYPE] = surfaceType;
136 attribMap[EGL_RENDERABLE_TYPE] = EGL_OPENGL_ES2_BIT;
138 return eglu::chooseSingleConfig(egl, eglDisplay, attribMap);
141 void ResizeTest::init (void)
145 const Library& egl = m_eglTestCtx.getLibrary();
146 const CommandLine& cmdLine = m_testCtx.getCommandLine();
147 const EGLDisplay eglDisplay = eglu::getAndInitDisplay(m_eglTestCtx.getNativeDisplay());
148 const EGLConfig eglConfig = getEGLConfig(egl, eglDisplay, surfaceType());
149 const EGLint ctxAttribs[] =
151 EGL_CONTEXT_CLIENT_VERSION, 2,
154 EGLContext eglContext = egl.createContext(eglDisplay,
158 EGLU_CHECK_MSG(egl, "eglCreateContext()");
159 MovePtr<UniqueContext> context (new UniqueContext(egl, eglDisplay, eglContext));
160 const EGLint configId = eglu::getConfigAttribInt(egl,
164 const Visibility visibility = eglu::parseWindowVisibility(cmdLine);
165 NativeDisplay& nativeDisplay = m_eglTestCtx.getNativeDisplay();
166 const NativeWindowFactory& windowFactory = eglu::selectNativeWindowFactory(m_eglTestCtx.getNativeDisplayFactory(),
169 const WindowParams windowParams (m_oldSize.x(), m_oldSize.y(), visibility);
170 MovePtr<NativeWindow> nativeWindow (windowFactory.createWindow(&nativeDisplay,
175 const EGLSurface eglSurface = eglu::createWindowSurface(nativeDisplay,
180 MovePtr<UniqueSurface> surface (new UniqueSurface(egl, eglDisplay, eglSurface));
182 m_log << TestLog::Message
183 << "Chose EGLConfig with id " << configId << ".\n"
184 << "Created initial surface with size " << m_oldSize
185 << TestLog::EndMessage;
187 m_eglTestCtx.initGLFunctions(&m_gl, glu::ApiType::es(2, 0));
188 m_config = eglConfig;
191 m_display = eglDisplay;
192 m_nativeWindow = nativeWindow;
193 EGLU_CHECK_MSG(egl, "init");
196 void ResizeTest::deinit (void)
201 m_nativeWindow.clear();
203 if (m_display != EGL_NO_DISPLAY)
205 m_eglTestCtx.getLibrary().terminate(m_display);
206 m_display = EGL_NO_DISPLAY;
210 void ResizeTest::resize (IVec2 size)
212 m_nativeWindow->setSurfaceSize(size);
213 m_testCtx.getPlatform().processEvents();
214 m_log << TestLog::Message
215 << "Resized surface to size " << size
216 << TestLog::EndMessage;
219 class ChangeSurfaceSizeCase : public ResizeTest
222 ChangeSurfaceSizeCase (EglTestContext& eglTestCtx,
223 const ResizeParams& params)
224 : ResizeTest(eglTestCtx, params) {}
226 IterateResult iterate (void);
229 void drawRectangle (const glw::Functions& gl, IVec2 pos, IVec2 size, Vec3 color)
231 gl.clearColor(color.x(), color.y(), color.z(), 1.0);
232 gl.scissor(pos.x(), pos.y(), size.x(), size.y());
233 gl.enable(GL_SCISSOR_TEST);
234 gl.clear(GL_COLOR_BUFFER_BIT);
235 gl.disable(GL_SCISSOR_TEST);
236 GLU_EXPECT_NO_ERROR(gl.getError(),
237 "Rectangle drawing with glScissor and glClear failed.");
240 void initSurface (const glw::Functions& gl, IVec2 oldSize)
242 const Vec3 frameColor (0.0f, 0.0f, 1.0f);
243 const Vec3 fillColor (1.0f, 0.0f, 0.0f);
244 const Vec3 markColor (0.0f, 1.0f, 0.0f);
246 drawRectangle(gl, IVec2(0, 0), oldSize, frameColor);
247 drawRectangle(gl, IVec2(2, 2), oldSize - IVec2(4, 4), fillColor);
249 drawRectangle(gl, IVec2(0, 0), IVec2(8, 4), markColor);
250 drawRectangle(gl, oldSize - IVec2(16, 16), IVec2(8, 4), markColor);
251 drawRectangle(gl, IVec2(0, oldSize.y() - 16), IVec2(8, 4), markColor);
252 drawRectangle(gl, IVec2(oldSize.x() - 16, 0), IVec2(8, 4), markColor);
255 bool compareRectangles (const ConstPixelBufferAccess& rectA,
256 const ConstPixelBufferAccess& rectB)
258 const int width = rectA.getWidth();
259 const int height = rectA.getHeight();
260 const int depth = rectA.getDepth();
262 if (rectB.getWidth() != width || rectB.getHeight() != height || rectB.getDepth() != depth)
265 for (int z = 0; z < depth; ++z)
266 for (int y = 0; y < height; ++y)
267 for (int x = 0; x < width; ++x)
268 if (rectA.getPixel(x, y, z) != rectB.getPixel(x, y, z))
274 // Check whether `oldSurface` and `newSurface` share a common corner.
275 bool compareCorners (const Surface& oldSurface, const Surface& newSurface)
277 const int oldWidth = oldSurface.getWidth();
278 const int oldHeight = oldSurface.getHeight();
279 const int newWidth = newSurface.getWidth();
280 const int newHeight = newSurface.getHeight();
281 const int minWidth = de::min(oldWidth, newWidth);
282 const int minHeight = de::min(oldHeight, newHeight);
284 for (int xCorner = 0; xCorner < 2; ++xCorner)
286 const int oldX = xCorner == 0 ? 0 : oldWidth - minWidth;
287 const int newX = xCorner == 0 ? 0 : newWidth - minWidth;
289 for (int yCorner = 0; yCorner < 2; ++yCorner)
291 const int oldY = yCorner == 0 ? 0 : oldHeight - minHeight;
292 const int newY = yCorner == 0 ? 0 : newHeight - minHeight;
293 ConstPixelBufferAccess oldAccess =
294 getSubregion(oldSurface.getAccess(), oldX, oldY, minWidth, minHeight);
295 ConstPixelBufferAccess newAccess =
296 getSubregion(newSurface.getAccess(), newX, newY, minWidth, minHeight);
298 if (compareRectangles(oldAccess, newAccess))
306 Surface readSurface (const glw::Functions& gl, IVec2 size)
308 Surface ret (size.x(), size.y());
309 gl.readPixels(0, 0, size.x(), size.y(), GL_RGBA, GL_UNSIGNED_BYTE,
310 ret.getAccess().getDataPtr());
312 GLU_EXPECT_NO_ERROR(gl.getError(), "glReadPixels() failed");
316 template <typename T>
317 inline bool hasBits (T bitSet, T requiredBits)
319 return (bitSet & requiredBits) == requiredBits;
322 IVec2 getNativeSurfaceSize (const NativeWindow& nativeWindow,
325 if (hasBits(nativeWindow.getCapabilities(), NativeWindow::CAPABILITY_GET_SURFACE_SIZE))
326 return nativeWindow.getSurfaceSize();
327 return reqSize; // assume we got the requested size
330 IVec2 checkSurfaceSize (const Library& egl,
331 EGLDisplay eglDisplay,
332 EGLSurface eglSurface,
333 const NativeWindow& nativeWindow,
335 ResultCollector& status)
337 const IVec2 nativeSize = getNativeSurfaceSize(nativeWindow, reqSize);
338 IVec2 eglSize = eglu::getSurfaceSize(egl, eglDisplay, eglSurface);
341 oss << "Size of EGL surface " << eglSize
342 << " differs from size of native window " << nativeSize;
343 status.check(eglSize == nativeSize, oss.str());
348 IterateResult ChangeSurfaceSizeCase::iterate (void)
350 const Library& egl = m_eglTestCtx.getLibrary();
351 ScopedCurrentContext currentCtx (egl, m_display, **m_surface, **m_surface, **m_context);
352 egl.swapBuffers(m_display, **m_surface);
353 IVec2 oldEglSize = checkSurfaceSize(egl,
360 initSurface(m_gl, oldEglSize);
362 this->resize(m_newSize);
364 egl.swapBuffers(m_display, **m_surface);
365 EGLU_CHECK_MSG(egl, "eglSwapBuffers()");
366 checkSurfaceSize(egl, m_display, **m_surface, *m_nativeWindow, m_newSize, m_status);
368 m_status.setTestContextResult(m_testCtx);
372 class PreserveBackBufferCase : public ResizeTest
375 PreserveBackBufferCase (EglTestContext& eglTestCtx,
376 const ResizeParams& params)
377 : ResizeTest(eglTestCtx, params) {}
379 IterateResult iterate (void);
380 EGLenum surfaceType (void) const;
383 EGLenum PreserveBackBufferCase::surfaceType (void) const
385 return EGL_WINDOW_BIT | EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
388 IterateResult PreserveBackBufferCase::iterate (void)
390 const Library& egl = m_eglTestCtx.getLibrary();
391 ScopedCurrentContext currentCtx (egl, m_display, **m_surface, **m_surface, **m_context);
393 EGLU_CHECK_CALL(egl, surfaceAttrib(m_display, **m_surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_PRESERVED));
395 GLU_EXPECT_NO_ERROR(m_gl.getError(), "GL state erroneous upon initialization!");
398 const IVec2 oldEglSize = eglu::getSurfaceSize(egl, m_display, **m_surface);
399 initSurface(m_gl, oldEglSize);
402 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glFinish() failed");
405 const Surface oldSurface = readSurface(m_gl, oldEglSize);
407 egl.swapBuffers(m_display, **m_surface);
408 this->resize(m_newSize);
409 egl.swapBuffers(m_display, **m_surface);
410 EGLU_CHECK_MSG(egl, "eglSwapBuffers()");
413 const IVec2 newEglSize = eglu::getSurfaceSize(egl, m_display, **m_surface);
414 const Surface newSurface = readSurface(m_gl, newEglSize);
416 m_log << TestLog::ImageSet("Corner comparison",
417 "Comparing old and new surfaces at all corners")
418 << TestLog::Image("Before resizing", "Before resizing", oldSurface)
419 << TestLog::Image("After resizing", "After resizing", newSurface)
420 << TestLog::EndImageSet;
422 m_status.checkResult(compareCorners(oldSurface, newSurface),
423 QP_TEST_RESULT_QUALITY_WARNING,
424 "Resizing the native window changed the contents of "
430 m_status.setTestContextResult(m_testCtx);
434 typedef tcu::Vector<Interval, 2> IvVec2;
436 class UpdateResolutionCase : public ResizeTest
439 UpdateResolutionCase (EglTestContext& eglTestCtx,
440 const ResizeParams& params)
441 : ResizeTest(eglTestCtx, params) {}
443 IterateResult iterate (void);
446 IvVec2 getNativePixelsPerInch (void);
449 IvVec2 ivVec2 (const IVec2& vec)
451 return IvVec2(double(vec.x()), double(vec.y()));
454 Interval approximateInt (int i)
456 const Interval margin(-1.0, 1.0); // The resolution may be rounded
458 return (Interval(i) + margin) & Interval(0.0, TCU_INFINITY);
461 IvVec2 UpdateResolutionCase::getNativePixelsPerInch (void)
463 const Library& egl = m_eglTestCtx.getLibrary();
464 const int inchPer10km = 254 * EGL_DISPLAY_SCALING;
465 const IVec2 bufSize = eglu::getSurfaceSize(egl, m_display, **m_surface);
466 const IVec2 winSize = m_nativeWindow->getScreenSize();
467 const IVec2 bufPp10km = eglu::getSurfaceResolution(egl, m_display, **m_surface);
468 const IvVec2 bufPpiI = (IvVec2(approximateInt(bufPp10km.x()),
469 approximateInt(bufPp10km.y()))
470 / Interval(inchPer10km));
471 const IvVec2 winPpiI = ivVec2(winSize) * bufPpiI / ivVec2(bufSize);
472 const IVec2 winPpi (int(winPpiI.x().midpoint()), int(winPpiI.y().midpoint()));
474 m_log << TestLog::Message
475 << "EGL surface size: " << bufSize << "\n"
476 << "EGL surface pixel density (pixels / 10 km): " << bufPp10km << "\n"
477 << "Native window size: " << winSize << "\n"
478 << "Native pixel density (ppi): " << winPpi << "\n"
479 << TestLog::EndMessage;
481 m_status.checkResult(bufPp10km.x() >= 1 && bufPp10km.y() >= 1,
482 QP_TEST_RESULT_QUALITY_WARNING,
483 "Surface pixel density is less than one pixel per 10 km. "
484 "Is the surface really visible from space?");
489 IterateResult UpdateResolutionCase::iterate (void)
491 const Library& egl = m_eglTestCtx.getLibrary();
492 ScopedCurrentContext currentCtx (egl, m_display, **m_surface, **m_surface, **m_context);
494 if (!hasBits(m_nativeWindow->getCapabilities(),
495 NativeWindow::CAPABILITY_GET_SCREEN_SIZE))
496 TCU_THROW(NotSupportedError, "Unable to determine surface size in screen pixels");
499 const IVec2 oldEglSize = eglu::getSurfaceSize(egl, m_display, **m_surface);
500 initSurface(m_gl, oldEglSize);
503 const IvVec2 oldPpi = this->getNativePixelsPerInch();
504 this->resize(m_newSize);
505 EGLU_CHECK_CALL(egl, swapBuffers(m_display, **m_surface));
507 const IvVec2 newPpi = this->getNativePixelsPerInch();
508 m_status.check(oldPpi.x().intersects(newPpi.x()) &&
509 oldPpi.y().intersects(newPpi.y()),
510 "Window PPI differs after resizing");
514 m_status.setTestContextResult(m_testCtx);
518 ResizeTests::ResizeTests (EglTestContext& eglTestCtx)
519 : TestCaseGroup(eglTestCtx, "resize", "Tests resizing the native surface")
523 template <class Case>
524 TestCaseGroup* createCaseGroup(EglTestContext& eglTestCtx,
528 const ResizeParams params[] =
530 { "shrink", "Shrink in both dimensions",
531 IVec2(128, 128), IVec2(32, 32) },
532 { "grow", "Grow in both dimensions",
533 IVec2(32, 32), IVec2(128, 128) },
534 { "stretch_width", "Grow horizontally, shrink vertically",
535 IVec2(32, 128), IVec2(128, 32) },
536 { "stretch_height", "Grow vertically, shrink horizontally",
537 IVec2(128, 32), IVec2(32, 128) },
539 TestCaseGroup* const group =
540 new TestCaseGroup(eglTestCtx, name.c_str(), desc.c_str());
542 for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(params); ++ndx)
543 group->addChild(new Case(eglTestCtx, params[ndx]));
548 void ResizeTests::init (void)
550 addChild(createCaseGroup<ChangeSurfaceSizeCase>(m_eglTestCtx,
552 "EGL surface size update"));
553 addChild(createCaseGroup<PreserveBackBufferCase>(m_eglTestCtx,
555 "Back buffer contents"));
556 addChild(createCaseGroup<UpdateResolutionCase>(m_eglTestCtx,