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 | { |
| 46 | program = InitShaderProgram(yuv_to_rgb_vertex, yuv_to_rgb_fragment); |
| 47 | |
| 48 | int imageWidthUniform = glGetUniformLocation(program, "imageWidth"); |
| 49 | int imageHeightUniform = glGetUniformLocation(program, "imageHeight"); |
| 50 | int textureSampler = glGetUniformLocation(program, "textureSampler"); |
| 51 | int evenLinesSampler = glGetUniformLocation(program, "paritySampler"); |
| 52 | |
| 53 | glUniform1f(imageWidthUniform, width); |
| 54 | glUniform1f(imageHeightUniform, height); |
| 55 | glUniform1i(textureSampler, 0); |
| 56 | glUniform1i(evenLinesSampler, 1); |
| 57 | |
| 58 | int attribute_index = glGetAttribLocation(program, "c"); |
| 59 | glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); |
| 60 | glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, NULL); |
| 61 | glEnableVertexAttribArray(attribute_index); |
| 62 | return program; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | done: |
| 67 | munmap(yuv_to_rgb_fragment, size_fragment); |
| 68 | munmap(yuv_to_rgb_fragment, size_vertex); |
| 69 | return program; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | bool YuvToRgbTest::Run() { |
| 74 | size_t size = 0; |
| 75 | GLuint texture[2]; |
| 76 | GLuint program = 0; |
| 77 | GLuint vertex_buffer = 0; |
| 78 | GLfloat vertices[8] = { |
| 79 | 0.f, 0.f, |
| 80 | 1.f, 0.f, |
| 81 | 0.f, 1.f, |
| 82 | 1.f, 1.f, |
| 83 | }; |
| 84 | char evenodd[2] = {0, 255}; |
| 85 | const int pixel_height = YUV2RGB_HEIGHT * 2 / 3; |
| 86 | |
| 87 | char *pixels = static_cast<char *>(MmapFile(YUV2RGB_NAME, &size)); |
| 88 | if (!pixels) { |
| 89 | printf("# Could not open image file: %s\n", YUV2RGB_NAME); |
| 90 | goto done; |
| 91 | } |
| 92 | if (size != YUV2RGB_SIZE) { |
| 93 | printf("# Image file of wrong size, got %d, expected %d\n", |
| 94 | static_cast<int>(size), YUV2RGB_SIZE); |
| 95 | goto done; |
| 96 | } |
| 97 | |
| 98 | glGenTextures(2, texture); |
| 99 | glActiveTexture(GL_TEXTURE0); |
| 100 | glBindTexture(GL_TEXTURE_2D, texture[0]); |
| 101 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 102 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 103 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, YUV2RGB_WIDTH, YUV2RGB_HEIGHT, |
| 104 | 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels); |
| 105 | |
| 106 | glActiveTexture(GL_TEXTURE1); |
| 107 | glBindTexture(GL_TEXTURE_2D, texture[1]); |
| 108 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 109 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 110 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 2, 1, |
| 111 | 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, evenodd); |
| 112 | |
| 113 | glViewport(-YUV2RGB_WIDTH, -pixel_height, YUV2RGB_WIDTH*2, pixel_height * 2); |
| 114 | vertex_buffer = SetupVBO(GL_ARRAY_BUFFER, sizeof(vertices), vertices); |
| 115 | |
| 116 | program = YuvToRgbShaderProgram(type_, vertex_buffer, |
| 117 | YUV2RGB_WIDTH, pixel_height); |
| 118 | |
| 119 | if (program) { |
| 120 | FillRateTestNormalSubWindow(name_, std::min(YUV2RGB_WIDTH, g_width), |
| 121 | std::min(pixel_height, g_height)); |
| 122 | } else { |
| 123 | printf("# Could not set up YUV shader.\n"); |
| 124 | } |
| 125 | |
| 126 | done: |
| 127 | glDeleteProgram(program); |
| 128 | glDeleteTextures(2, texture); |
| 129 | glDeleteBuffers(1, &vertex_buffer); |
| 130 | munmap(pixels, size); |
| 131 | |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | TestBase* GetYuvToRgbTest(int type, const char* name) { |
| 137 | return new YuvToRgbTest(type, name); |
| 138 | } |
| 139 | |
| 140 | } // namespace glbench |