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 | |
| 5 | #include <stdio.h> |
| 6 | #include <sys/mman.h> |
| 7 | |
| 8 | #include "base/logging.h" |
| 9 | |
| 10 | #include "main.h" |
| 11 | #include "testbase.h" |
| 12 | #include "utils.h" |
| 13 | #include "yuv2rgb.h" |
| 14 | |
| 15 | namespace glbench { |
| 16 | |
| 17 | class YuvToRgbTest : public DrawArraysTestFunc { |
| 18 | public: |
| 19 | YuvToRgbTest(int type, const char* name) : type_(type), name_(name) {} |
| 20 | virtual ~YuvToRgbTest() {} |
| 21 | virtual bool Run(); |
| 22 | |
| 23 | private: |
| 24 | int type_; |
| 25 | const char* name_; |
| 26 | DISALLOW_COPY_AND_ASSIGN(YuvToRgbTest); |
| 27 | }; |
| 28 | |
| 29 | |
| 30 | GLuint YuvToRgbShaderProgram(int type, GLuint vertex_buffer, |
| 31 | int width, int height) { |
| 32 | const char *vertex = type == 1 ? YUV2RGB_VERTEX_1 : YUV2RGB_VERTEX_2; |
| 33 | const char *fragment = type == 1 ? YUV2RGB_FRAGMENT_1 : YUV2RGB_FRAGMENT_2; |
| 34 | size_t size_vertex = 0; |
| 35 | size_t size_fragment = 0; |
| 36 | char *yuv_to_rgb_vertex = static_cast<char *>( |
| 37 | MmapFile(vertex, &size_vertex)); |
| 38 | char *yuv_to_rgb_fragment = static_cast<char *>( |
| 39 | MmapFile(fragment, &size_fragment)); |
| 40 | GLuint program = 0; |
| 41 | |
| 42 | if (!yuv_to_rgb_fragment || !yuv_to_rgb_vertex) |
| 43 | goto done; |
| 44 | |
| 45 | { |
Alexey Marinichev | 219b708 | 2010-05-24 16:18:51 -0700 | [diff] [blame] | 46 | #if defined(I915_WORKAROUND) |
| 47 | const char* header = "#define I915_WORKAROUND 1\n"; |
| 48 | #else |
| 49 | const char* header = NULL; |
| 50 | #endif |
| 51 | program = InitShaderProgramWithHeader(header, yuv_to_rgb_vertex, |
| 52 | yuv_to_rgb_fragment); |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 53 | |
| 54 | int imageWidthUniform = glGetUniformLocation(program, "imageWidth"); |
| 55 | int imageHeightUniform = glGetUniformLocation(program, "imageHeight"); |
| 56 | int textureSampler = glGetUniformLocation(program, "textureSampler"); |
| 57 | int evenLinesSampler = glGetUniformLocation(program, "paritySampler"); |
| 58 | |
| 59 | glUniform1f(imageWidthUniform, width); |
| 60 | glUniform1f(imageHeightUniform, height); |
| 61 | glUniform1i(textureSampler, 0); |
| 62 | glUniform1i(evenLinesSampler, 1); |
| 63 | |
| 64 | int attribute_index = glGetAttribLocation(program, "c"); |
| 65 | glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); |
| 66 | glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, NULL); |
| 67 | glEnableVertexAttribArray(attribute_index); |
| 68 | return program; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | done: |
| 73 | munmap(yuv_to_rgb_fragment, size_fragment); |
| 74 | munmap(yuv_to_rgb_fragment, size_vertex); |
| 75 | return program; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | bool YuvToRgbTest::Run() { |
| 80 | size_t size = 0; |
| 81 | GLuint texture[2]; |
| 82 | GLuint program = 0; |
| 83 | GLuint vertex_buffer = 0; |
| 84 | GLfloat vertices[8] = { |
| 85 | 0.f, 0.f, |
| 86 | 1.f, 0.f, |
| 87 | 0.f, 1.f, |
| 88 | 1.f, 1.f, |
| 89 | }; |
| 90 | char evenodd[2] = {0, 255}; |
| 91 | const int pixel_height = YUV2RGB_HEIGHT * 2 / 3; |
| 92 | |
| 93 | char *pixels = static_cast<char *>(MmapFile(YUV2RGB_NAME, &size)); |
| 94 | if (!pixels) { |
| 95 | printf("# Could not open image file: %s\n", YUV2RGB_NAME); |
| 96 | goto done; |
| 97 | } |
| 98 | if (size != YUV2RGB_SIZE) { |
| 99 | printf("# Image file of wrong size, got %d, expected %d\n", |
| 100 | static_cast<int>(size), YUV2RGB_SIZE); |
| 101 | goto done; |
| 102 | } |
| 103 | |
| 104 | glGenTextures(2, texture); |
| 105 | glActiveTexture(GL_TEXTURE0); |
| 106 | glBindTexture(GL_TEXTURE_2D, texture[0]); |
| 107 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 108 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 109 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, YUV2RGB_WIDTH, YUV2RGB_HEIGHT, |
| 110 | 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels); |
| 111 | |
| 112 | glActiveTexture(GL_TEXTURE1); |
| 113 | glBindTexture(GL_TEXTURE_2D, texture[1]); |
| 114 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 115 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 116 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 2, 1, |
| 117 | 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, evenodd); |
| 118 | |
| 119 | glViewport(-YUV2RGB_WIDTH, -pixel_height, YUV2RGB_WIDTH*2, pixel_height * 2); |
| 120 | vertex_buffer = SetupVBO(GL_ARRAY_BUFFER, sizeof(vertices), vertices); |
| 121 | |
| 122 | program = YuvToRgbShaderProgram(type_, vertex_buffer, |
| 123 | YUV2RGB_WIDTH, pixel_height); |
| 124 | |
| 125 | if (program) { |
| 126 | FillRateTestNormalSubWindow(name_, std::min(YUV2RGB_WIDTH, g_width), |
| 127 | std::min(pixel_height, g_height)); |
| 128 | } else { |
| 129 | printf("# Could not set up YUV shader.\n"); |
| 130 | } |
| 131 | |
| 132 | done: |
| 133 | glDeleteProgram(program); |
| 134 | glDeleteTextures(2, texture); |
| 135 | glDeleteBuffers(1, &vertex_buffer); |
| 136 | munmap(pixels, size); |
| 137 | |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | TestBase* GetYuvToRgbTest(int type, const char* name) { |
| 143 | return new YuvToRgbTest(type, name); |
| 144 | } |
| 145 | |
| 146 | } // namespace glbench |