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