1#include "ANGLETest.h"
2
3class SRGBTextureTest : public ANGLETest
4{
5protected:
6    SRGBTextureTest()
7    {
8        setWindowWidth(128);
9        setWindowHeight(128);
10        setConfigRedBits(8);
11        setConfigGreenBits(8);
12        setConfigBlueBits(8);
13        setConfigAlphaBits(8);
14    }
15
16    virtual void SetUp()
17    {
18        ANGLETest::SetUp();
19    }
20
21    virtual void TearDown()
22    {
23        ANGLETest::TearDown();
24    }
25};
26
27TEST_F(SRGBTextureTest, SRGBValidation)
28{
29    bool supported = extensionEnabled("GL_EXT_sRGB") || getClientVersion() == 3;
30
31    GLuint tex = 0;
32    glGenTextures(1, &tex);
33    glBindTexture(GL_TEXTURE_2D, tex);
34
35    GLubyte pixel[3] = { 0 };
36    glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB, 1, 1, 0, GL_SRGB, GL_UNSIGNED_BYTE, pixel);
37    if (supported)
38    {
39        EXPECT_GL_NO_ERROR();
40
41        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, GL_SRGB, GL_UNSIGNED_BYTE, pixel);
42        EXPECT_GL_NO_ERROR();
43
44        glGenerateMipmap(GL_TEXTURE_2D);
45        EXPECT_GL_ERROR(GL_INVALID_OPERATION);
46    }
47    else
48    {
49        EXPECT_GL_ERROR(GL_INVALID_ENUM);
50    }
51
52    glDeleteTextures(1, &tex);
53}
54
55TEST_F(SRGBTextureTest, SRGBAValidation)
56{
57    bool supported = extensionEnabled("GL_EXT_sRGB") || getClientVersion() == 3;
58
59    GLuint tex = 0;
60    glGenTextures(1, &tex);
61    glBindTexture(GL_TEXTURE_2D, tex);
62
63    GLubyte pixel[4] = { 0 };
64    glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB_ALPHA_EXT, 1, 1, 0, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE, pixel);
65    if (supported)
66    {
67        EXPECT_GL_NO_ERROR();
68
69        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE, pixel);
70        EXPECT_GL_NO_ERROR();
71
72        glGenerateMipmap(GL_TEXTURE_2D);
73        if (getClientVersion() == 2)
74        {
75            EXPECT_GL_ERROR(GL_INVALID_OPERATION);
76        }
77        else
78        {
79            EXPECT_GL_NO_ERROR();
80        }
81    }
82    else
83    {
84        EXPECT_GL_ERROR(GL_INVALID_ENUM);
85    }
86
87    glDeleteTextures(1, &tex);
88}
89
90TEST_F(SRGBTextureTest, SRGBARenderbuffer)
91{
92    bool supported = extensionEnabled("GL_EXT_sRGB") || getClientVersion() == 3;
93
94    GLuint rbo = 0;
95    glGenRenderbuffers(1, &rbo);
96    glBindRenderbuffer(GL_RENDERBUFFER, rbo);
97
98    glRenderbufferStorage(GL_RENDERBUFFER, GL_SRGB8_ALPHA8_EXT, 1, 1);
99    if (supported)
100    {
101        EXPECT_GL_NO_ERROR();
102    }
103    else
104    {
105        EXPECT_GL_ERROR(GL_INVALID_ENUM);
106
107        // Make sure the rbo has a size for future tests
108        glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8_OES, 1, 1);
109        EXPECT_GL_NO_ERROR();
110    }
111
112    GLuint fbo = 0;
113    glGenFramebuffers(1, &fbo);
114    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
115    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
116    EXPECT_GL_NO_ERROR();
117
118    GLint colorEncoding = 0;
119    glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
120                                          GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING_EXT, &colorEncoding);
121    if (supported)
122    {
123        EXPECT_GL_NO_ERROR();
124        EXPECT_EQ(GL_SRGB_EXT, colorEncoding);
125    }
126    else
127    {
128        EXPECT_GL_ERROR(GL_INVALID_ENUM);
129    }
130
131    glDeleteFramebuffers(1, &fbo);
132    glDeleteRenderbuffers(1, &rbo);
133}
134