1 // Copyright 2014 The Chromium 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 <GLES2/gl2.h> 6 #include <GLES2/gl2chromium.h> 7 #include <GLES2/gl2ext.h> 8 #include <GLES2/gl2extchromium.h> 9 10 #include "base/bind.h" 11 #include "base/memory/ref_counted.h" 12 #include "base/process/process_handle.h" 13 #include "gpu/command_buffer/client/gles2_implementation.h" 14 #include "gpu/command_buffer/service/command_buffer_service.h" 15 #include "gpu/command_buffer/service/image_manager.h" 16 #include "gpu/command_buffer/tests/gl_manager.h" 17 #include "gpu/command_buffer/tests/gl_test_utils.h" 18 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "ui/gl/gl_image.h" 21 22 using testing::_; 23 using testing::IgnoreResult; 24 using testing::InvokeWithoutArgs; 25 using testing::Invoke; 26 using testing::Return; 27 using testing::SetArgPointee; 28 using testing::StrictMock; 29 30 namespace gpu { 31 namespace gles2 { 32 33 static const int kImageWidth = 32; 34 static const int kImageHeight = 32; 35 static const int kImageBytesPerPixel = 4; 36 37 class GpuMemoryBufferTest : public testing::Test { 38 protected: 39 virtual void SetUp() { 40 gl_.Initialize(GLManager::Options()); 41 gl_.MakeCurrent(); 42 43 glGenTextures(2, texture_ids_); 44 glBindTexture(GL_TEXTURE_2D, texture_ids_[1]); 45 46 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 47 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 48 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 50 51 glGenFramebuffers(1, &framebuffer_id_); 52 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_); 53 glFramebufferTexture2D(GL_FRAMEBUFFER, 54 GL_COLOR_ATTACHMENT0, 55 GL_TEXTURE_2D, 56 texture_ids_[1], 57 0); 58 } 59 60 virtual void TearDown() { 61 glDeleteTextures(2, texture_ids_); 62 glDeleteFramebuffers(1, &framebuffer_id_); 63 64 gl_.Destroy(); 65 } 66 67 GLManager gl_; 68 GLuint texture_ids_[2]; 69 GLuint framebuffer_id_; 70 }; 71 72 // An end to end test that tests the whole GpuMemoryBuffer lifecycle. 73 TEST_F(GpuMemoryBufferTest, Lifecycle) { 74 uint8 pixels[1 * 4] = { 255u, 0u, 0u, 255u }; 75 76 // Create the image. This should add the image ID to the ImageManager. 77 GLuint image_id = glCreateImageCHROMIUM( 78 kImageWidth, kImageHeight, GL_RGBA8_OES, GL_IMAGE_MAP_CHROMIUM); 79 EXPECT_NE(0u, image_id); 80 EXPECT_TRUE(gl_.decoder()->GetImageManager()->LookupImage(image_id) != NULL); 81 82 // Map image for writing. 83 uint8* mapped_buffer = static_cast<uint8*>(glMapImageCHROMIUM(image_id)); 84 ASSERT_TRUE(mapped_buffer != NULL); 85 86 // Assign a value to each pixel. 87 int stride = kImageWidth * kImageBytesPerPixel; 88 for (int x = 0; x < kImageWidth; ++x) { 89 for (int y = 0; y < kImageHeight; ++y) { 90 mapped_buffer[y * stride + x * kImageBytesPerPixel + 0] = pixels[0]; 91 mapped_buffer[y * stride + x * kImageBytesPerPixel + 1] = pixels[1]; 92 mapped_buffer[y * stride + x * kImageBytesPerPixel + 2] = pixels[2]; 93 mapped_buffer[y * stride + x * kImageBytesPerPixel + 3] = pixels[3]; 94 } 95 } 96 97 // Unmap the image. 98 glUnmapImageCHROMIUM(image_id); 99 100 // Bind the texture and the image. 101 glBindTexture(GL_TEXTURE_2D, texture_ids_[0]); 102 glBindTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); 103 104 // Copy texture so we can verify result using CheckPixels. 105 glCopyTextureCHROMIUM(GL_TEXTURE_2D, 106 texture_ids_[0], 107 texture_ids_[1], 108 0, 109 GL_RGBA, 110 GL_UNSIGNED_BYTE); 111 EXPECT_TRUE(glGetError() == GL_NO_ERROR); 112 113 // Check if pixels match the values that were assigned to the mapped buffer. 114 GLTestHelper::CheckPixels(0, 0, kImageWidth, kImageHeight, 0, pixels); 115 EXPECT_TRUE(GL_NO_ERROR == glGetError()); 116 117 // Release the image. 118 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id); 119 120 // Destroy the image. 121 glDestroyImageCHROMIUM(image_id); 122 } 123 124 } // namespace gles2 125 } // namespace gpu 126