Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 1 | // 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 Chan | c9faddb | 2016-12-08 21:21:16 -0800 | [diff] [blame] | 5 | #include <memory> |
| 6 | |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 7 | #include "base/logging.h" |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 8 | |
| 9 | #include "main.h" |
| 10 | #include "testbase.h" |
| 11 | |
| 12 | |
| 13 | namespace glbench { |
| 14 | |
| 15 | |
| 16 | class ReadPixelTest : public TestBase { |
| 17 | public: |
| 18 | ReadPixelTest() : pixels_(NULL) {} |
| 19 | virtual ~ReadPixelTest() {} |
Ilja H. Friedel | 439ea14 | 2014-02-21 20:29:10 -0800 | [diff] [blame] | 20 | virtual bool TestFunc(uint64_t iterations); |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 21 | virtual bool Run(); |
Alexey Marinichev | f50ecb1 | 2010-06-14 15:21:41 -0700 | [diff] [blame] | 22 | virtual const char* Name() const { return "pixel_read"; } |
Daniel Kurtz | bcad4fb | 2014-06-18 12:31:03 +0800 | [diff] [blame] | 23 | virtual bool IsDrawTest() const { return false; } |
Daniel Kurtz | aace01b | 2014-06-17 21:05:24 +0800 | [diff] [blame] | 24 | virtual const char* Unit() const { return "mpixels_sec"; } |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 25 | |
| 26 | private: |
| 27 | void* pixels_; |
| 28 | DISALLOW_COPY_AND_ASSIGN(ReadPixelTest); |
| 29 | }; |
| 30 | |
| 31 | |
Ilja H. Friedel | 439ea14 | 2014-02-21 20:29:10 -0800 | [diff] [blame] | 32 | bool ReadPixelTest::TestFunc(uint64_t iterations) { |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 33 | glReadPixels(0, 0, g_width, g_height, GL_RGBA, GL_UNSIGNED_BYTE, pixels_); |
| 34 | CHECK(glGetError() == 0); |
Ilja H. Friedel | 439ea14 | 2014-02-21 20:29:10 -0800 | [diff] [blame] | 35 | for (uint64_t i = 0; i < iterations - 1; i++) |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 36 | glReadPixels(0, 0, g_width, g_height, GL_RGBA, GL_UNSIGNED_BYTE, pixels_); |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | bool 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 Chan | c9faddb | 2016-12-08 21:21:16 -0800 | [diff] [blame] | 47 | std::unique_ptr<char[]> buf(new char[((row_size + 3) & ~3) * g_height + 1]); |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 48 | pixels_ = buf.get(); |
Daniel Kurtz | aace01b | 2014-06-17 21:05:24 +0800 | [diff] [blame] | 49 | RunTest(this, "pixel_read", g_width * g_height, g_width, g_height, true); |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 50 | |
| 51 | // Reducing GL_PACK_ALIGNMENT can only make rows smaller. No need to |
| 52 | // reallocate the buffer. |
| 53 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
Daniel Kurtz | aace01b | 2014-06-17 21:05:24 +0800 | [diff] [blame] | 54 | RunTest(this, "pixel_read_2", g_width * g_height, g_width, g_height, true); |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 55 | |
| 56 | pixels_ = static_cast<void*>(buf.get() + 1); |
Daniel Kurtz | aace01b | 2014-06-17 21:05:24 +0800 | [diff] [blame] | 57 | RunTest(this, "pixel_read_3", g_width * g_height, g_width, g_height, true); |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 58 | |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | TestBase* GetReadPixelTest() { |
| 64 | return new ReadPixelTest; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | } // namespace glbench |