1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363

gpu / command_buffer / tests / gl_ext_window_rectangles_unittest.cc [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <GLES3/gl3.h>
#include <stdint.h>

#include "gpu/command_buffer/tests/gl_manager.h"
#include "gpu/command_buffer/tests/gl_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace gpu {

// A collection of tests that exercise the GL_EXT_window_rectangles extension.
class GLEXTWindowRectanglesTest : public testing::Test {
 protected:
  void SetUp() override {
    GLManager::Options options;
    options.context_type = CONTEXT_TYPE_OPENGLES3;
    gl_.Initialize(options);
  }

  void TearDown() override { gl_.Destroy(); }

  bool IsApplicable() const {
    // Not applicable for devices not supporting OpenGLES3.
    if (!gl_.IsInitialized())
      return false;
    bool have_ext = GLTestHelper::HasExtension("GL_EXT_window_rectangles");
    return have_ext;
  }

  GLuint SetupFramebuffer() {
    GLuint fbo = 0;
    glGenFramebuffers(1, &fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    GLuint tex = 0;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 32, 32, 0, GL_RGBA,
                 GL_UNSIGNED_BYTE, nullptr);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
                           tex, 0);
    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), status);
    EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
    return fbo;
  }

  void ClearTo(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
    glClearColor(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  }

  GLManager gl_;
};

#define CHECK_PIXEL(x, y, r, g, b, a)                               \
  do {                                                              \
    uint8_t pixel[4];                                               \
    glReadPixels((x), (y), 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel); \
    ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());      \
    EXPECT_NEAR((r), pixel[0], 2);                                  \
    EXPECT_NEAR((g), pixel[1], 2);                                  \
    EXPECT_NEAR((b), pixel[2], 2);                                  \
    EXPECT_NEAR((a), pixel[3], 2);                                  \
  } while (0)

// TODO(crbug.com/40246425): Re-enable this test
TEST_F(GLEXTWindowRectanglesTest, DISABLED_Defaults) {
  if (!IsApplicable()) {
    return;
  }

  GLint max = -1;
  {
    glGetIntegerv(GL_MAX_WINDOW_RECTANGLES_EXT, &max);
    ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
    EXPECT_GE(max, 4);
  }

  {
    GLint mode = -1;
    glGetIntegerv(GL_WINDOW_RECTANGLE_MODE_EXT, &mode);
    ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
    EXPECT_EQ(GL_EXCLUSIVE_EXT, mode);
  }
  {
    GLint num = -1;
    glGetIntegerv(GL_NUM_WINDOW_RECTANGLES_EXT, &num);
    ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
    EXPECT_EQ(0, num);
  }

  for (int i = 0; i < max; ++i) {
    GLint rect[4] = {-1, -1, -1, -1};
    glGetIntegeri_v(GL_WINDOW_RECTANGLE_EXT, i, rect);
    ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
    EXPECT_EQ(0, rect[0]);
    EXPECT_EQ(0, rect[1]);
    EXPECT_EQ(0, rect[2]);
    EXPECT_EQ(0, rect[3]);
  }
}

TEST_F(GLEXTWindowRectanglesTest, Defaults64) {
  if (!IsApplicable()) {
    return;
  }

  int64_t max = -1;
  {
    glGetInteger64v(GL_MAX_WINDOW_RECTANGLES_EXT, &max);
    ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
    EXPECT_GE(max, 4);
  }

  {
    int64_t mode = -1;
    glGetInteger64v(GL_WINDOW_RECTANGLE_MODE_EXT, &mode);
    ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
    EXPECT_EQ(GL_EXCLUSIVE_EXT, mode);
  }
  {
    int64_t num = -1;
    glGetInteger64v(GL_NUM_WINDOW_RECTANGLES_EXT, &num);
    ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
    EXPECT_EQ(0, num);
  }

  for (int i = 0; i < max; ++i) {
    int64_t rect[4] = {-1, -1, -1, -1};
    glGetInteger64i_v(GL_WINDOW_RECTANGLE_EXT, i, rect);
    ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
    EXPECT_EQ(0, rect[0]);
    EXPECT_EQ(0, rect[1]);
    EXPECT_EQ(0, rect[2]);
    EXPECT_EQ(0, rect[3]);
  }
}

TEST_F(GLEXTWindowRectanglesTest, BasicState) {
  if (!IsApplicable()) {
    return;
  }

  GLint box_exp[12] = {};
  for (int i = 0; i < 12; ++i) {
    box_exp[i] = i;
  }
  glWindowRectanglesEXT(GL_INCLUSIVE_EXT, 3, box_exp);
  ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());

  {
    GLint mode = -1;
    glGetIntegerv(GL_WINDOW_RECTANGLE_MODE_EXT, &mode);
    ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
    EXPECT_EQ(GL_INCLUSIVE_EXT, mode);
  }
  {
    GLint num = -1;
    glGetIntegerv(GL_NUM_WINDOW_RECTANGLES_EXT, &num);
    ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
    EXPECT_EQ(3, num);
  }
  {
    GLint box[12] = {};
    for (int i = 0; i < 12; ++i) {
      box[i] = -1;
    }
    glGetIntegeri_v(GL_WINDOW_RECTANGLE_EXT, 0, &box[0]);
    glGetIntegeri_v(GL_WINDOW_RECTANGLE_EXT, 1, &box[4]);
    glGetIntegeri_v(GL_WINDOW_RECTANGLE_EXT, 2, &box[8]);
    ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
    for (int i = 0; i < 12; ++i) {
      EXPECT_EQ(box_exp[i], box[i]);
    }
  }
}

TEST_F(GLEXTWindowRectanglesTest, DefaultFramebuffer) {
  if (!IsApplicable()) {
    return;
  }

  glBindFramebuffer(GL_FRAMEBUFFER, 0);

  GLint box[4] = {0, 0, 1, 1};

  ClearTo(255, 0, 0, 255);
  // This says "DO NOT render inside 0,0", but
  // it should have no effect for the default framebuffer.
  glWindowRectanglesEXT(GL_EXCLUSIVE_EXT, 1, box);
  ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());

  ClearTo(0, 255, 0, 255);
  // Inside rectangle
  CHECK_PIXEL(0, 0, 0, 255, 0, 255);

  ClearTo(255, 0, 0, 255);
  // This says "ONLY render inside 0,0", but
  // it should have no effect for the default framebuffer.
  glWindowRectanglesEXT(GL_INCLUSIVE_EXT, 1, box);
  ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());

  ClearTo(0, 255, 0, 255);
  // Inside rectangle
  CHECK_PIXEL(0, 0, 0, 255, 0, 255);
}

TEST_F(GLEXTWindowRectanglesTest, OneRectangle) {
  if (!IsApplicable()) {
    return;
  }

  GLint box[4] = {
      8, 4, 1, 1,
  };
  SetupFramebuffer();

  ClearTo(255, 0, 0, 255);
  // "DO NOT render inside the pixel 8,4"
  glWindowRectanglesEXT(GL_EXCLUSIVE_EXT, 1, box);
  ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());

  ClearTo(0, 255, 0, 255);
  // Inside rectangle
  CHECK_PIXEL(8, 4, 255, 0, 0, 255);
  // Outside rectangle
  CHECK_PIXEL(4, 8, 0, 255, 0, 255);

  glWindowRectanglesEXT(GL_EXCLUSIVE_EXT, 0, nullptr);
  ClearTo(255, 0, 0, 255);
  // "ONLY render inside the pixel 8,4"
  glWindowRectanglesEXT(GL_INCLUSIVE_EXT, 1, box);
  ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());

  ClearTo(0, 255, 0, 255);
  // Inside rectangle
  CHECK_PIXEL(8, 4, 0, 255, 0, 255);
  // Outside rectangle
  CHECK_PIXEL(4, 8, 255, 0, 0, 255);
}

TEST_F(GLEXTWindowRectanglesTest, TwoRectangles) {
  if (!IsApplicable()) {
    return;
  }

  GLint box[8] = {
      8, 4, 1, 1, 2, 5, 1, 1,
  };
  SetupFramebuffer();

  ClearTo(255, 0, 0, 255);
  // "DO NOT render inside the pixels 8,4 and 2,5"
  glWindowRectanglesEXT(GL_EXCLUSIVE_EXT, 2, box);
  ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());

  ClearTo(0, 255, 0, 255);
  // Inside rectangles
  CHECK_PIXEL(8, 4, 255, 0, 0, 255);
  CHECK_PIXEL(2, 5, 255, 0, 0, 255);
  // Outside rectangles
  CHECK_PIXEL(4, 8, 0, 255, 0, 255);
  CHECK_PIXEL(5, 2, 0, 255, 0, 255);

  glWindowRectanglesEXT(GL_EXCLUSIVE_EXT, 0, nullptr);
  ClearTo(255, 0, 0, 255);
  // "ONLY render inside the pixels 8,4 and 4,8"
  glWindowRectanglesEXT(GL_INCLUSIVE_EXT, 2, box);
  ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());

  ClearTo(0, 255, 0, 255);
  // Inside rectangles
  CHECK_PIXEL(8, 4, 0, 255, 0, 255);
  CHECK_PIXEL(2, 5, 0, 255, 0, 255);
  // Outside rectangles
  CHECK_PIXEL(4, 8, 255, 0, 0, 255);
  CHECK_PIXEL(5, 2, 255, 0, 0, 255);
}

TEST_F(GLEXTWindowRectanglesTest, InclusiveEmptyBox) {
  if (!IsApplicable()) {
    return;
  }

  GLint box[4] = {
      0, 0, 0, 0,
  };
  SetupFramebuffer();

  ClearTo(255, 0, 0, 255);
  // "ONLY render inside the 0-by-0 box"
  glWindowRectanglesEXT(GL_INCLUSIVE_EXT, 1, box);
  ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());

  ClearTo(0, 255, 0, 255);
  // Outside rectangle
  CHECK_PIXEL(4, 8, 255, 0, 0, 255);
}

TEST_F(GLEXTWindowRectanglesTest, TextureClearStateClearAndRestore) {
  if (!IsApplicable()) {
    return;
  }

  GLint box[4] = {
      0, 0, 0, 0,
  };
  GLuint main_fbo = SetupFramebuffer();

  ClearTo(255, 0, 0, 255);
  // "ONLY render inside the 0-by-0 box"
  glWindowRectanglesEXT(GL_INCLUSIVE_EXT, 1, box);
  ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());

  GLuint test_fbo = 0;
  {
    glGenFramebuffers(1, &test_fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, test_fbo);
    GLuint tex = 0;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 32, 32, 0, GL_RGBA,
                 GL_UNSIGNED_BYTE, nullptr);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
                           tex, 0);
    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    ASSERT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), status);
    ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  }

  glBindFramebuffer(GL_FRAMEBUFFER, test_fbo);
  ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  // Check the texture is initialized
  {
    constexpr size_t PIXELS_SIZE = 4 * 32 * 32;
    uint8_t pixels[PIXELS_SIZE];
    glReadPixels(0, 0, 32, 32, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
    ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
    for (size_t i = 0; i < PIXELS_SIZE; ++i) {
      EXPECT_NEAR(0, pixels[i], 2);
    }
  }

  glBindFramebuffer(GL_FRAMEBUFFER, main_fbo);
  ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  // Make sure the window rectangle state is preserved
  ClearTo(0, 255, 0, 255);
  // So this clear doesn't do anything.
  CHECK_PIXEL(4, 8, 255, 0, 0, 255);
}

}  // namespace gpu