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.
6 #include <GLES2/gl2chromium.h>
7 #include <GLES2/gl2ext.h>
8 #include <GLES2/gl2extchromium.h>
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"
23 using testing::IgnoreResult;
24 using testing::InvokeWithoutArgs;
25 using testing::Invoke;
26 using testing::Return;
27 using testing::SetArgPointee;
28 using testing::StrictMock;
33 static const int kImageWidth = 32;
34 static const int kImageHeight = 32;
35 static const int kImageBytesPerPixel = 4;
37 class GpuMemoryBufferTest : public testing::Test {
39 virtual void SetUp() {
40 gl_.Initialize(GLManager::Options());
43 glGenTextures(2, texture_ids_);
44 glBindTexture(GL_TEXTURE_2D, texture_ids_[1]);
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);
51 glGenFramebuffers(1, &framebuffer_id_);
52 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id_);
53 glFramebufferTexture2D(GL_FRAMEBUFFER,
60 virtual void TearDown() {
61 glDeleteTextures(2, texture_ids_);
62 glDeleteFramebuffers(1, &framebuffer_id_);
68 GLuint texture_ids_[2];
69 GLuint framebuffer_id_;
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 };
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);
82 // Map image for writing.
83 uint8* mapped_buffer = static_cast<uint8*>(glMapImageCHROMIUM(image_id));
84 ASSERT_TRUE(mapped_buffer != NULL);
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];
98 glUnmapImageCHROMIUM(image_id);
100 // Bind the texture and the image.
101 glBindTexture(GL_TEXTURE_2D, texture_ids_[0]);
102 glBindTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id);
104 // Copy texture so we can verify result using CheckPixels.
105 glCopyTextureCHROMIUM(GL_TEXTURE_2D,
111 EXPECT_TRUE(glGetError() == GL_NO_ERROR);
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());
117 // Release the image.
118 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id);
120 // Destroy the image.
121 glDestroyImageCHROMIUM(image_id);