1// 2// Copyright 2021 The ANGLE Project Authors. All rights reserved. 3// Use of this source code is governed by a BSD-style license that can be 4// found in the LICENSE file. 5// 6// EGLSurfaceTestMac: 7// Tests pertaining to egl::Surface. 8// 9 10#include <gtest/gtest.h> 11 12#include <vector> 13 14#include <AppKit/AppKit.h> 15 16#include "common/Color.h" 17#include "common/platform.h" 18#include "test_utils/ANGLETest.h" 19#include "test_utils/gl_raii.h" 20#include "util/EGLWindow.h" 21#include "util/OSWindow.h" 22 23using namespace angle; 24 25namespace 26{ 27 28class EGLSurfaceTestMac : public ANGLETest 29{ 30 protected: 31 void testSetUp() override 32 { 33 // Get display. 34 EGLAttrib dispattrs[] = {EGL_PLATFORM_ANGLE_TYPE_ANGLE, GetParam().getRenderer(), EGL_NONE}; 35 mDisplay = eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE, 36 reinterpret_cast<void *>(EGL_DEFAULT_DISPLAY), dispattrs); 37 ASSERT_TRUE(mDisplay != EGL_NO_DISPLAY); 38 39 ASSERT_TRUE(eglInitialize(mDisplay, nullptr, nullptr) == EGL_TRUE); 40 41 // Find a default config. 42 const EGLint configAttributes[] = { 43 EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, EGL_DONT_CARE, EGL_GREEN_SIZE, 44 EGL_DONT_CARE, EGL_BLUE_SIZE, EGL_DONT_CARE, EGL_ALPHA_SIZE, EGL_DONT_CARE, 45 EGL_DEPTH_SIZE, EGL_DONT_CARE, EGL_STENCIL_SIZE, EGL_DONT_CARE, EGL_NONE}; 46 47 EGLint configCount; 48 EGLint ret = eglChooseConfig(mDisplay, configAttributes, &mConfig, 1, &configCount); 49 50 if (!ret || configCount == 0) 51 { 52 return; 53 } 54 55 // Create a window, context and surface if multisampling is possible. 56 mOSWindow = OSWindow::New(); 57 mOSWindow->initialize("EGLSurfaceTestMac", kWindowWidth, kWindowHeight); 58 setWindowVisible(mOSWindow, true); 59 60 EGLint contextAttributes[] = { 61 EGL_CONTEXT_MAJOR_VERSION_KHR, 62 GetParam().majorVersion, 63 EGL_CONTEXT_MINOR_VERSION_KHR, 64 GetParam().minorVersion, 65 EGL_NONE, 66 }; 67 68 mContext = eglCreateContext(mDisplay, mConfig, EGL_NO_CONTEXT, contextAttributes); 69 ASSERT_TRUE(mContext != EGL_NO_CONTEXT); 70 } 71 72 void testTearDown() override 73 { 74 if (mSurface) 75 { 76 eglSwapBuffers(mDisplay, mSurface); 77 } 78 79 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); 80 81 if (mSurface) 82 { 83 eglDestroySurface(mDisplay, mSurface); 84 ASSERT_EGL_SUCCESS(); 85 } 86 87 if (mContext != EGL_NO_CONTEXT) 88 { 89 eglDestroyContext(mDisplay, mContext); 90 ASSERT_EGL_SUCCESS(); 91 } 92 93 if (mOSWindow) 94 { 95 OSWindow::Delete(&mOSWindow); 96 } 97 98 eglTerminate(mDisplay); 99 } 100 101 void initializeSurface(int contentsScale) 102 { 103 EGLNativeWindowType nativeWindow = mOSWindow->getNativeWindow(); 104 CALayer *layer = reinterpret_cast<CALayer *>(nativeWindow); 105 layer.contentsScale = contentsScale; 106 107 mSurface = eglCreateWindowSurface(mDisplay, mConfig, nativeWindow, nullptr); 108 ASSERT_EGL_SUCCESS(); 109 110 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext); 111 ASSERT_EGL_SUCCESS(); 112 } 113 114 protected: 115 static constexpr int kWindowWidth = 16; 116 static constexpr int kWindowHeight = 8; 117 118 OSWindow *mOSWindow = nullptr; 119 EGLDisplay mDisplay = EGL_NO_DISPLAY; 120 EGLContext mContext = EGL_NO_CONTEXT; 121 EGLSurface mSurface = EGL_NO_SURFACE; 122 EGLConfig mConfig; 123}; 124 125// Test Mac's layer's content scale > 1 and check the initial viewport is set correctly. 126TEST_P(EGLSurfaceTestMac, ContentsScale) 127{ 128 constexpr int contentsScale = 2; 129 initializeSurface(contentsScale); 130 131 GLint viewport[4]; 132 glGetIntegerv(GL_VIEWPORT, viewport); 133 ASSERT_EQ(viewport[0], 0); 134 ASSERT_EQ(viewport[1], 0); 135 ASSERT_EQ(viewport[2], kWindowWidth * contentsScale); 136 ASSERT_EQ(viewport[3], kWindowHeight * contentsScale); 137 138 GLColor clearColor(255, 0, 0, 255); 139 glClearColor(clearColor.R, clearColor.G, clearColor.B, clearColor.A); 140 glClear(GL_COLOR_BUFFER_BIT); 141 ASSERT_GL_NO_ERROR(); 142 143 EXPECT_PIXEL_COLOR_EQ(0, 0, clearColor); 144 EXPECT_PIXEL_COLOR_EQ(kWindowWidth, 0, clearColor); 145 EXPECT_PIXEL_COLOR_EQ(0, kWindowHeight, clearColor); 146 EXPECT_PIXEL_COLOR_EQ(kWindowHeight * contentsScale - 1, kWindowHeight * contentsScale - 1, 147 clearColor); 148} 149} // anonymous namespace 150 151ANGLE_INSTANTIATE_TEST(EGLSurfaceTestMac, 152 WithNoFixture(ES2_OPENGL()), 153 WithNoFixture(ES3_OPENGL()), 154 WithNoFixture(ES2_METAL()), 155 WithNoFixture(ES3_METAL())); 156