Call eglSwapBuffers() to ensure wayland surface is attached
[platform/upstream/VK-GL-CTS.git] / modules / egl / teglResizeTests.cpp
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program EGL Module
3  * ---------------------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  *//*!
20  * \file
21  * \brief Tests for resizing the native window of a surface.
22  *//*--------------------------------------------------------------------*/
23
24 #include "teglResizeTests.hpp"
25
26 #include "teglSimpleConfigCase.hpp"
27
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"
35
36 #include "egluNativeDisplay.hpp"
37 #include "egluNativeWindow.hpp"
38 #include "egluNativePixmap.hpp"
39 #include "egluUnique.hpp"
40 #include "egluUtil.hpp"
41
42 #include "eglwLibrary.hpp"
43 #include "eglwEnums.hpp"
44
45 #include "gluDefs.hpp"
46 #include "glwFunctions.hpp"
47 #include "glwEnums.hpp"
48
49 #include "tcuTestLog.hpp"
50 #include "tcuVector.hpp"
51
52 #include "deThread.h"
53 #include "deUniquePtr.hpp"
54
55 #include <sstream>
56
57 namespace deqp
58 {
59 namespace egl
60 {
61
62 using std::vector;
63 using std::string;
64 using std::ostringstream;
65 using de::MovePtr;
66 using tcu::CommandLine;
67 using tcu::ConstPixelBufferAccess;
68 using tcu::Interval;
69 using tcu::IVec2;
70 using tcu::Vec3;
71 using tcu::Vec4;
72 using tcu::UVec4;
73 using tcu::ResultCollector;
74 using tcu::Surface;
75 using tcu::TestLog;
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;
84 using namespace eglw;
85
86 typedef eglu::WindowParams::Visibility  Visibility;
87 typedef TestCase::IterateResult                 IterateResult;
88
89 struct ResizeParams
90 {
91         string  name;
92         string  description;
93         IVec2   oldSize;
94         IVec2   newSize;
95 };
96
97 class ResizeTest : public TestCase
98 {
99 public:
100                                                                 ResizeTest      (EglTestContext&                eglTestCtx,
101                                                                                          const ResizeParams&    params)
102                                                                         : TestCase      (eglTestCtx,
103                                                                                                  params.name.c_str(),
104                                                                                                  params.description.c_str())
105                                                                         , m_oldSize     (params.oldSize)
106                                                                         , m_newSize     (params.newSize)
107                                                                         , m_display     (EGL_NO_DISPLAY)
108                                                                         , m_config      (DE_NULL)
109                                                                         , m_log         (m_testCtx.getLog())
110                                                                         , m_status      (m_log) {}
111
112         void                                            init            (void);
113         void                                            deinit          (void);
114
115 protected:
116         virtual EGLenum                         surfaceType     (void) const { return EGL_WINDOW_BIT; }
117         void                                            resize          (IVec2 size);
118
119         const IVec2                                     m_oldSize;
120         const IVec2                                     m_newSize;
121         EGLDisplay                                      m_display;
122         EGLConfig                                       m_config;
123         MovePtr<NativeWindow>           m_nativeWindow;
124         MovePtr<UniqueSurface>          m_surface;
125         MovePtr<UniqueContext>          m_context;
126         TestLog&                                        m_log;
127         ResultCollector                         m_status;
128         glw::Functions                          m_gl;
129 };
130
131 EGLConfig getEGLConfig (const Library& egl, const EGLDisplay eglDisplay, EGLenum surfaceType)
132 {
133         AttribMap attribMap;
134
135         attribMap[EGL_SURFACE_TYPE]             = surfaceType;
136         attribMap[EGL_RENDERABLE_TYPE]  = EGL_OPENGL_ES2_BIT;
137
138         return eglu::chooseSingleConfig(egl, eglDisplay, attribMap);
139 }
140
141 void ResizeTest::init (void)
142 {
143         TestCase::init();
144
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[]    =
150         {
151                 EGL_CONTEXT_CLIENT_VERSION, 2,
152                 EGL_NONE
153         };
154         EGLContext                                      eglContext              = egl.createContext(eglDisplay,
155                                                                                                                                    eglConfig,
156                                                                                                                                    EGL_NO_CONTEXT,
157                                                                                                                                    ctxAttribs);
158         EGLU_CHECK_MSG(egl, "eglCreateContext()");
159         MovePtr<UniqueContext>          context                 (new UniqueContext(egl, eglDisplay, eglContext));
160         const EGLint                            configId                = eglu::getConfigAttribInt(egl,
161                                                                                                                                                    eglDisplay,
162                                                                                                                                                    eglConfig,
163                                                                                                                                                    EGL_CONFIG_ID);
164         const Visibility                        visibility              = eglu::parseWindowVisibility(cmdLine);
165         NativeDisplay&                          nativeDisplay   = m_eglTestCtx.getNativeDisplay();
166         const NativeWindowFactory&      windowFactory   = eglu::selectNativeWindowFactory(m_eglTestCtx.getNativeDisplayFactory(),
167                                                                                                                                                                   cmdLine);
168
169         const WindowParams                      windowParams    (m_oldSize.x(), m_oldSize.y(), visibility);
170         MovePtr<NativeWindow>           nativeWindow    (windowFactory.createWindow(&nativeDisplay,
171                                                                                                                                                          eglDisplay,
172                                                                                                                                                          eglConfig,
173                                                                                                                                                          DE_NULL,
174                                                                                                                                                          windowParams));
175         const EGLSurface                        eglSurface              = eglu::createWindowSurface(nativeDisplay,
176                                                                                                                                                         *nativeWindow,
177                                                                                                                                                         eglDisplay,
178                                                                                                                                                         eglConfig,
179                                                                                                                                                         DE_NULL);
180         MovePtr<UniqueSurface>          surface                 (new UniqueSurface(egl, eglDisplay, eglSurface));
181
182         m_log << TestLog::Message
183                   << "Chose EGLConfig with id " << configId << ".\n"
184                   << "Created initial surface with size " << m_oldSize
185                   << TestLog::EndMessage;
186
187         m_eglTestCtx.initGLFunctions(&m_gl, glu::ApiType::es(2, 0));
188         m_config                = eglConfig;
189         m_surface               = surface;
190         m_context               = context;
191         m_display               = eglDisplay;
192         m_nativeWindow  = nativeWindow;
193         EGLU_CHECK_MSG(egl, "init");
194 }
195
196 void ResizeTest::deinit (void)
197 {
198         if (m_display != EGL_NO_DISPLAY)
199                 m_eglTestCtx.getLibrary().terminate(m_display);
200
201         m_config                = DE_NULL;
202         m_display               = EGL_NO_DISPLAY;
203         m_context.clear();
204         m_surface.clear();
205         m_nativeWindow.clear();
206 }
207
208 void ResizeTest::resize (IVec2 size)
209 {
210         m_nativeWindow->setSurfaceSize(size);
211         m_testCtx.getPlatform().processEvents();
212         m_log << TestLog::Message
213                   << "Resized surface to size " << size
214                   << TestLog::EndMessage;
215 }
216
217 class ChangeSurfaceSizeCase : public ResizeTest
218 {
219 public:
220                                         ChangeSurfaceSizeCase   (EglTestContext&                eglTestCtx,
221                                                                                          const ResizeParams&    params)
222                                                 : ResizeTest(eglTestCtx, params) {}
223
224         IterateResult   iterate                                 (void);
225 };
226
227 void drawRectangle (const glw::Functions& gl, IVec2 pos, IVec2 size, Vec3 color)
228 {
229         gl.clearColor(color.x(), color.y(), color.z(), 1.0);
230         gl.scissor(pos.x(), pos.y(), size.x(), size.y());
231         gl.enable(GL_SCISSOR_TEST);
232         gl.clear(GL_COLOR_BUFFER_BIT);
233         gl.disable(GL_SCISSOR_TEST);
234         GLU_EXPECT_NO_ERROR(gl.getError(),
235                                                 "Rectangle drawing with glScissor and glClear failed.");
236 }
237
238 void initSurface (const glw::Functions& gl, IVec2 oldSize)
239 {
240         const Vec3      frameColor      (0.0f, 0.0f, 1.0f);
241         const Vec3      fillColor       (1.0f, 0.0f, 0.0f);
242         const Vec3      markColor       (0.0f, 1.0f, 0.0f);
243
244         drawRectangle(gl, IVec2(0, 0), oldSize, frameColor);
245         drawRectangle(gl, IVec2(2, 2), oldSize - IVec2(4, 4), fillColor);
246
247         drawRectangle(gl, IVec2(0, 0), IVec2(8, 4), markColor);
248         drawRectangle(gl, oldSize - IVec2(16, 16), IVec2(8, 4), markColor);
249         drawRectangle(gl, IVec2(0, oldSize.y() - 16), IVec2(8, 4), markColor);
250         drawRectangle(gl, IVec2(oldSize.x() - 16, 0), IVec2(8, 4), markColor);
251 }
252
253 bool compareRectangles (const ConstPixelBufferAccess& rectA,
254                                                 const ConstPixelBufferAccess& rectB)
255 {
256         const int width         = rectA.getWidth();
257         const int height        = rectA.getHeight();
258         const int depth         = rectA.getDepth();
259
260         if (rectB.getWidth() != width || rectB.getHeight() != height || rectB.getDepth() != depth)
261                 return false;
262
263         for (int z = 0; z < depth; ++z)
264                 for (int y = 0; y < height; ++y)
265                         for (int x = 0; x < width; ++x)
266                                 if (rectA.getPixel(x, y, z) != rectB.getPixel(x, y, z))
267                                         return false;
268
269         return true;
270 }
271
272 // Check whether `oldSurface` and `newSurface` share a common corner.
273 bool compareCorners (const Surface& oldSurface, const Surface& newSurface)
274 {
275         const int       oldWidth        = oldSurface.getWidth();
276         const int       oldHeight       = oldSurface.getHeight();
277         const int       newWidth        = newSurface.getWidth();
278         const int       newHeight       = newSurface.getHeight();
279         const int       minWidth        = de::min(oldWidth, newWidth);
280         const int       minHeight       = de::min(oldHeight, newHeight);
281
282         for (int xCorner = 0; xCorner < 2; ++xCorner)
283         {
284                 const int oldX = xCorner == 0 ? 0 : oldWidth - minWidth;
285                 const int newX = xCorner == 0 ? 0 : newWidth - minWidth;
286
287                 for (int yCorner = 0; yCorner < 2; ++yCorner)
288                 {
289                         const int                               oldY            = yCorner == 0 ? 0 : oldHeight - minHeight;
290                         const int                               newY            = yCorner == 0 ? 0 : newHeight - minHeight;
291                         ConstPixelBufferAccess  oldAccess       =
292                                 getSubregion(oldSurface.getAccess(), oldX, oldY, minWidth, minHeight);
293                         ConstPixelBufferAccess  newAccess       =
294                                 getSubregion(newSurface.getAccess(), newX, newY, minWidth, minHeight);
295
296                         if (compareRectangles(oldAccess, newAccess))
297                                 return true;
298                 }
299         }
300
301         return false;
302 }
303
304 Surface readSurface (const glw::Functions& gl, IVec2 size)
305 {
306         Surface ret (size.x(), size.y());
307         gl.readPixels(0, 0, size.x(), size.y(), GL_RGBA, GL_UNSIGNED_BYTE,
308                                   ret.getAccess().getDataPtr());
309
310         GLU_EXPECT_NO_ERROR(gl.getError(), "glReadPixels() failed");
311         return ret;
312 }
313
314 template <typename T>
315 inline bool hasBits (T bitSet, T requiredBits)
316 {
317         return (bitSet & requiredBits) == requiredBits;
318 }
319
320 IVec2 getNativeSurfaceSize (const NativeWindow& nativeWindow,
321                                                         IVec2                           reqSize)
322 {
323         if (hasBits(nativeWindow.getCapabilities(), NativeWindow::CAPABILITY_GET_SURFACE_SIZE))
324                 return nativeWindow.getSurfaceSize();
325         return reqSize; // assume we got the requested size
326 }
327
328 IVec2 checkSurfaceSize (const Library&          egl,
329                                                 EGLDisplay                      eglDisplay,
330                                                 EGLSurface                      eglSurface,
331                                                 const NativeWindow&     nativeWindow,
332                                                 IVec2                           reqSize,
333                                                 ResultCollector&        status)
334 {
335         const IVec2             nativeSize      = getNativeSurfaceSize(nativeWindow, reqSize);
336         IVec2                   eglSize         = eglu::getSurfaceSize(egl, eglDisplay, eglSurface);
337         ostringstream   oss;
338
339         oss << "Size of EGL surface " << eglSize
340                 << " differs from size of native window " << nativeSize;
341         status.check(eglSize == nativeSize, oss.str());
342
343         return eglSize;
344 }
345
346 IterateResult ChangeSurfaceSizeCase::iterate (void)
347 {
348         const Library&                  egl                     = m_eglTestCtx.getLibrary();
349         ScopedCurrentContext    currentCtx      (egl, m_display, **m_surface, **m_surface, **m_context);
350         egl.swapBuffers(m_display, **m_surface);
351         IVec2                                   oldEglSize      = checkSurfaceSize(egl,
352                                                                                                                    m_display,
353                                                                                                                    **m_surface,
354                                                                                                                    *m_nativeWindow,
355                                                                                                                    m_oldSize,
356                                                                                                                    m_status);
357
358         initSurface(m_gl, oldEglSize);
359
360         this->resize(m_newSize);
361
362         egl.swapBuffers(m_display, **m_surface);
363         EGLU_CHECK_MSG(egl, "eglSwapBuffers()");
364         checkSurfaceSize(egl, m_display, **m_surface, *m_nativeWindow, m_newSize, m_status);
365
366         m_status.setTestContextResult(m_testCtx);
367         return STOP;
368 }
369
370 class PreserveBackBufferCase : public ResizeTest
371 {
372 public:
373                                         PreserveBackBufferCase  (EglTestContext&                eglTestCtx,
374                                                                                          const ResizeParams&    params)
375                                                 : ResizeTest(eglTestCtx, params) {}
376
377         IterateResult   iterate                                 (void);
378         EGLenum                 surfaceType                             (void) const;
379 };
380
381 EGLenum PreserveBackBufferCase::surfaceType (void) const
382 {
383         return EGL_WINDOW_BIT | EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
384 }
385
386 IterateResult PreserveBackBufferCase::iterate (void)
387 {
388         const Library&                  egl                     = m_eglTestCtx.getLibrary();
389         ScopedCurrentContext    currentCtx      (egl, m_display, **m_surface, **m_surface, **m_context);
390
391         EGLU_CHECK_CALL(egl, surfaceAttrib(m_display, **m_surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_PRESERVED));
392
393         GLU_EXPECT_NO_ERROR(m_gl.getError(), "GL state erroneous upon initialization!");
394
395         {
396                 const IVec2 oldEglSize = eglu::getSurfaceSize(egl, m_display, **m_surface);
397                 initSurface(m_gl, oldEglSize);
398
399                 m_gl.finish();
400                 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glFinish() failed");
401
402                 {
403                         const Surface oldSurface = readSurface(m_gl, oldEglSize);
404
405                         egl.swapBuffers(m_display, **m_surface);
406                         this->resize(m_newSize);
407                         egl.swapBuffers(m_display, **m_surface);
408                         EGLU_CHECK_MSG(egl, "eglSwapBuffers()");
409
410                         {
411                                 const IVec2             newEglSize      = eglu::getSurfaceSize(egl, m_display, **m_surface);
412                                 const Surface   newSurface      = readSurface(m_gl, newEglSize);
413
414                                 m_log << TestLog::ImageSet("Corner comparison",
415                                                                                    "Comparing old and new surfaces at all corners")
416                                           << TestLog::Image("Before resizing", "Before resizing", oldSurface)
417                                           << TestLog::Image("After resizing", "After resizing", newSurface)
418                                           << TestLog::EndImageSet;
419
420                                 m_status.checkResult(compareCorners(oldSurface, newSurface),
421                                                                          QP_TEST_RESULT_QUALITY_WARNING,
422                                                                          "Resizing the native window changed the contents of "
423                                                                          "the EGL surface");
424                         }
425                 }
426         }
427
428         m_status.setTestContextResult(m_testCtx);
429         return STOP;
430 }
431
432 typedef tcu::Vector<Interval, 2> IvVec2;
433
434 class UpdateResolutionCase : public ResizeTest
435 {
436 public:
437                                         UpdateResolutionCase    (EglTestContext&                eglTestCtx,
438                                                                                          const ResizeParams&    params)
439                                                 : ResizeTest(eglTestCtx, params) {}
440
441         IterateResult   iterate                                 (void);
442
443 private:
444         IvVec2                  getNativePixelsPerInch  (void);
445 };
446
447 IvVec2 ivVec2 (const IVec2& vec)
448 {
449         return IvVec2(double(vec.x()), double(vec.y()));
450 }
451
452 Interval approximateInt (int i)
453 {
454         const Interval margin(-1.0, 1.0); // The resolution may be rounded
455
456         return (Interval(i) + margin) & Interval(0.0, TCU_INFINITY);
457 }
458
459 IvVec2 UpdateResolutionCase::getNativePixelsPerInch     (void)
460 {
461         const Library&  egl                     = m_eglTestCtx.getLibrary();
462         const int               inchPer10km     = 254 * EGL_DISPLAY_SCALING;
463         const IVec2             bufSize         = eglu::getSurfaceSize(egl, m_display, **m_surface);
464         const IVec2             winSize         = m_nativeWindow->getScreenSize();
465         const IVec2             bufPp10km       = eglu::getSurfaceResolution(egl, m_display, **m_surface);
466         const IvVec2    bufPpiI         = (IvVec2(approximateInt(bufPp10km.x()),
467                                                                                   approximateInt(bufPp10km.y()))
468                                                                    / Interval(inchPer10km));
469         const IvVec2    winPpiI         = ivVec2(winSize) * bufPpiI / ivVec2(bufSize);
470         const IVec2             winPpi          (int(winPpiI.x().midpoint()), int(winPpiI.y().midpoint()));
471
472         m_log << TestLog::Message
473                   << "EGL surface size: "                                                       << bufSize              << "\n"
474                   << "EGL surface pixel density (pixels / 10 km): "     << bufPp10km    << "\n"
475                   << "Native window size: "                                                     << winSize              << "\n"
476                   << "Native pixel density (ppi): "                                     << winPpi               << "\n"
477                   << TestLog::EndMessage;
478
479         m_status.checkResult(bufPp10km.x() >= 1 && bufPp10km.y() >= 1,
480                                                  QP_TEST_RESULT_QUALITY_WARNING,
481                                                  "Surface pixel density is less than one pixel per 10 km. "
482                                                  "Is the surface really visible from space?");
483
484         return winPpiI;
485 }
486
487 IterateResult UpdateResolutionCase::iterate (void)
488 {
489         const Library&                  egl                     = m_eglTestCtx.getLibrary();
490         ScopedCurrentContext    currentCtx      (egl, m_display, **m_surface, **m_surface, **m_context);
491
492         if (!hasBits(m_nativeWindow->getCapabilities(),
493                                  NativeWindow::CAPABILITY_GET_SCREEN_SIZE))
494                 TCU_THROW(NotSupportedError, "Unable to determine surface size in screen pixels");
495
496         {
497                 const IVec2 oldEglSize = eglu::getSurfaceSize(egl, m_display, **m_surface);
498                 initSurface(m_gl, oldEglSize);
499         }
500         {
501                 const IvVec2 oldPpi = this->getNativePixelsPerInch();
502                 this->resize(m_newSize);
503                 EGLU_CHECK_CALL(egl, swapBuffers(m_display, **m_surface));
504                 {
505                         const IvVec2 newPpi = this->getNativePixelsPerInch();
506                         m_status.check(oldPpi.x().intersects(newPpi.x()) &&
507                                                    oldPpi.y().intersects(newPpi.y()),
508                                                    "Window PPI differs after resizing");
509                 }
510         }
511
512         m_status.setTestContextResult(m_testCtx);
513         return STOP;
514 }
515
516 ResizeTests::ResizeTests (EglTestContext& eglTestCtx)
517         : TestCaseGroup(eglTestCtx, "resize", "Tests resizing the native surface")
518 {
519 }
520
521 template <class Case>
522 TestCaseGroup* createCaseGroup(EglTestContext&  eglTestCtx,
523                                                            const string&        name,
524                                                            const string&        desc)
525 {
526         const ResizeParams              params[]        =
527         {
528                 { "shrink",                     "Shrink in both dimensions",
529                   IVec2(128, 128),      IVec2(32, 32) },
530                 { "grow",                       "Grow in both dimensions",
531                   IVec2(32, 32),        IVec2(128, 128) },
532                 { "stretch_width",      "Grow horizontally, shrink vertically",
533                   IVec2(32, 128),       IVec2(128, 32) },
534                 { "stretch_height",     "Grow vertically, shrink horizontally",
535                   IVec2(128, 32),       IVec2(32, 128) },
536         };
537         TestCaseGroup* const    group           =
538                 new TestCaseGroup(eglTestCtx, name.c_str(), desc.c_str());
539
540         for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(params); ++ndx)
541                 group->addChild(new Case(eglTestCtx, params[ndx]));
542
543         return group;
544 }
545
546 void ResizeTests::init (void)
547 {
548         addChild(createCaseGroup<ChangeSurfaceSizeCase>(m_eglTestCtx,
549                                                                                                         "surface_size",
550                                                                                                         "EGL surface size update"));
551         addChild(createCaseGroup<PreserveBackBufferCase>(m_eglTestCtx,
552                                                                                                          "back_buffer",
553                                                                                                          "Back buffer contents"));
554         addChild(createCaseGroup<UpdateResolutionCase>(m_eglTestCtx,
555                                                                                                    "pixel_density",
556                                                                                                    "Pixel density"));
557 }
558
559 } // egl
560 } // deqp