1 //
2 // Copyright 2015 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 
7 #include "test_utils/ANGLETest.h"
8 
9 using namespace angle;
10 
11 namespace
12 {
13 
14 class UnpackRowLengthTest : public ANGLETest
15 {
16   protected:
UnpackRowLengthTest()17     UnpackRowLengthTest()
18     {
19         setWindowWidth(128);
20         setWindowHeight(128);
21         setConfigRedBits(8);
22         setConfigGreenBits(8);
23         setConfigBlueBits(8);
24         setConfigAlphaBits(8);
25         setConfigDepthBits(24);
26 
27         mProgram = 0;
28     }
29 
testSetUp()30     void testSetUp() override
31     {
32         constexpr char kFS[] = R"(uniform sampler2D tex;
33 void main()
34 {
35     gl_FragColor = texture2D(tex, vec2(0.0, 1.0));
36 })";
37 
38         mProgram = CompileProgram(essl1_shaders::vs::Simple(), kFS);
39         if (mProgram == 0)
40         {
41             FAIL() << "shader compilation failed.";
42         }
43     }
44 
testTearDown()45     void testTearDown() override { glDeleteProgram(mProgram); }
46 
testRowLength(int texSize,int rowLength)47     void testRowLength(int texSize, int rowLength)
48     {
49         glPixelStorei(GL_UNPACK_ROW_LENGTH, rowLength);
50 
51         if ((getClientMajorVersion() == 3) || IsGLExtensionEnabled("GL_EXT_unpack_subimage"))
52         {
53             // Only texSize * texSize region is filled as WHITE, other parts are BLACK.
54             // If the UNPACK_ROW_LENGTH is implemented correctly, all texels inside this texture are
55             // WHITE.
56             std::vector<GLubyte> buf(rowLength * texSize * 4);
57             for (int y = 0; y < texSize; y++)
58             {
59                 std::vector<GLubyte>::iterator rowIter = buf.begin() + y * rowLength * 4;
60                 std::fill(rowIter, rowIter + texSize * 4, static_cast<GLubyte>(255u));
61                 std::fill(rowIter + texSize * 4, rowIter + rowLength * 4, static_cast<GLubyte>(0u));
62             }
63 
64             GLuint tex;
65             glGenTextures(1, &tex);
66             glBindTexture(GL_TEXTURE_2D, tex);
67 
68             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texSize, texSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
69                          &buf[0]);
70             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
71             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
72             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
73 
74             drawQuad(mProgram, essl1_shaders::PositionAttrib(), 0.5f);
75 
76             EXPECT_PIXEL_EQ(0, 0, 255, 255, 255, 255);
77             EXPECT_PIXEL_EQ(1, 0, 255, 255, 255, 255);
78 
79             glDeleteTextures(1, &tex);
80         }
81         else
82         {
83             EXPECT_GL_ERROR(GL_INVALID_ENUM);
84         }
85     }
86 
87     GLuint mProgram;
88 };
89 
TEST_P(UnpackRowLengthTest,RowLength128)90 TEST_P(UnpackRowLengthTest, RowLength128)
91 {
92     testRowLength(128, 128);
93 }
94 
TEST_P(UnpackRowLengthTest,RowLength1024)95 TEST_P(UnpackRowLengthTest, RowLength1024)
96 {
97     testRowLength(128, 1024);
98 }
99 
100 // Use this to select which configurations (e.g. which renderer, which GLES major version) these
101 // tests should be run against.
102 ANGLE_INSTANTIATE_TEST_ES2_AND_ES3(UnpackRowLengthTest);
103 
104 }  // namespace
105