blob: 6a05b7d533cdf03d5dc57260311ed2dc814437c0 [file] [log] [blame]
Alexey Marinichev9f9b8732010-05-20 19:33:44 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chanc9faddb2016-12-08 21:21:16 -08005#include <memory>
6
Alexey Marinichev9f9b8732010-05-20 19:33:44 -07007#include "base/logging.h"
Alexey Marinichev9f9b8732010-05-20 19:33:44 -07008
9#include "main.h"
10#include "testbase.h"
11
12
13namespace glbench {
14
15
16class ReadPixelTest : public TestBase {
17 public:
18 ReadPixelTest() : pixels_(NULL) {}
19 virtual ~ReadPixelTest() {}
Ilja H. Friedel439ea142014-02-21 20:29:10 -080020 virtual bool TestFunc(uint64_t iterations);
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070021 virtual bool Run();
Alexey Marinichevf50ecb12010-06-14 15:21:41 -070022 virtual const char* Name() const { return "pixel_read"; }
Daniel Kurtzbcad4fb2014-06-18 12:31:03 +080023 virtual bool IsDrawTest() const { return false; }
Daniel Kurtzaace01b2014-06-17 21:05:24 +080024 virtual const char* Unit() const { return "mpixels_sec"; }
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070025
26 private:
27 void* pixels_;
28 DISALLOW_COPY_AND_ASSIGN(ReadPixelTest);
29};
30
31
Ilja H. Friedel439ea142014-02-21 20:29:10 -080032bool ReadPixelTest::TestFunc(uint64_t iterations) {
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070033 glReadPixels(0, 0, g_width, g_height, GL_RGBA, GL_UNSIGNED_BYTE, pixels_);
34 CHECK(glGetError() == 0);
Ilja H. Friedel439ea142014-02-21 20:29:10 -080035 for (uint64_t i = 0; i < iterations - 1; i++)
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070036 glReadPixels(0, 0, g_width, g_height, GL_RGBA, GL_UNSIGNED_BYTE, pixels_);
37 return true;
38}
39
40
41bool ReadPixelTest::Run() {
42 // One GL_RGBA pixel takes 4 bytes.
43 const int row_size = g_width * 4;
44 // Default GL_PACK_ALIGNMENT is 4, round up pixel row size to multiple of 4.
45 // This is a no-op because row_size is already divisible by 4.
46 // One is added so that we can test reads into unaligned location.
Ben Chanc9faddb2016-12-08 21:21:16 -080047 std::unique_ptr<char[]> buf(new char[((row_size + 3) & ~3) * g_height + 1]);
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070048 pixels_ = buf.get();
Daniel Kurtzaace01b2014-06-17 21:05:24 +080049 RunTest(this, "pixel_read", g_width * g_height, g_width, g_height, true);
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070050
51 // Reducing GL_PACK_ALIGNMENT can only make rows smaller. No need to
52 // reallocate the buffer.
53 glPixelStorei(GL_PACK_ALIGNMENT, 1);
Daniel Kurtzaace01b2014-06-17 21:05:24 +080054 RunTest(this, "pixel_read_2", g_width * g_height, g_width, g_height, true);
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070055
56 pixels_ = static_cast<void*>(buf.get() + 1);
Daniel Kurtzaace01b2014-06-17 21:05:24 +080057 RunTest(this, "pixel_read_3", g_width * g_height, g_width, g_height, true);
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070058
59 return true;
60}
61
62
63TestBase* GetReadPixelTest() {
64 return new ReadPixelTest;
65}
66
67
68} // namespace glbench