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/gfx/gpu_memory_buffer.h"
21 #include "ui/gl/gl_image.h"
24 using testing::IgnoreResult
;
25 using testing::InvokeWithoutArgs
;
26 using testing::Invoke
;
27 using testing::Return
;
28 using testing::SetArgPointee
;
29 using testing::StrictMock
;
34 static const int kImageWidth
= 32;
35 static const int kImageHeight
= 32;
36 static const int kImageBytesPerPixel
= 4;
38 class GpuMemoryBufferTest
: public testing::Test
{
40 void SetUp() override
{
41 gl_
.Initialize(GLManager::Options());
44 glGenTextures(2, texture_ids_
);
45 glBindTexture(GL_TEXTURE_2D
, texture_ids_
[1]);
47 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
48 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
49 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
50 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
52 glGenFramebuffers(1, &framebuffer_id_
);
53 glBindFramebuffer(GL_FRAMEBUFFER
, framebuffer_id_
);
54 glFramebufferTexture2D(GL_FRAMEBUFFER
,
61 void TearDown() override
{
62 glDeleteTextures(2, texture_ids_
);
63 glDeleteFramebuffers(1, &framebuffer_id_
);
69 GLuint texture_ids_
[2];
70 GLuint framebuffer_id_
;
73 // An end to end test that tests the whole GpuMemoryBuffer lifecycle.
74 TEST_F(GpuMemoryBufferTest
, Lifecycle
) {
75 uint8 pixels
[1 * 4] = { 255u, 0u, 0u, 255u };
77 scoped_ptr
<gfx::GpuMemoryBuffer
> buffer(gl_
.CreateGpuMemoryBuffer(
78 gfx::Size(kImageWidth
, kImageHeight
), gfx::GpuMemoryBuffer::RGBA_8888
));
80 // Map buffer for writing.
82 bool rv
= buffer
->Map(&data
);
85 uint8
* mapped_buffer
= static_cast<uint8
*>(data
);
86 ASSERT_TRUE(mapped_buffer
!= NULL
);
88 // Assign a value to each pixel.
89 int stride
= kImageWidth
* kImageBytesPerPixel
;
90 for (int x
= 0; x
< kImageWidth
; ++x
) {
91 for (int y
= 0; y
< kImageHeight
; ++y
) {
92 mapped_buffer
[y
* stride
+ x
* kImageBytesPerPixel
+ 0] = pixels
[0];
93 mapped_buffer
[y
* stride
+ x
* kImageBytesPerPixel
+ 1] = pixels
[1];
94 mapped_buffer
[y
* stride
+ x
* kImageBytesPerPixel
+ 2] = pixels
[2];
95 mapped_buffer
[y
* stride
+ x
* kImageBytesPerPixel
+ 3] = pixels
[3];
102 // Create the image. This should add the image ID to the ImageManager.
103 GLuint image_id
= glCreateImageCHROMIUM(
104 buffer
->AsClientBuffer(), kImageWidth
, kImageHeight
, GL_RGBA
);
105 EXPECT_NE(0u, image_id
);
106 EXPECT_TRUE(gl_
.decoder()->GetImageManager()->LookupImage(image_id
) != NULL
);
108 // Bind the texture and the image.
109 glBindTexture(GL_TEXTURE_2D
, texture_ids_
[0]);
110 glBindTexImage2DCHROMIUM(GL_TEXTURE_2D
, image_id
);
112 // Copy texture so we can verify result using CheckPixels.
113 glCopyTextureCHROMIUM(GL_TEXTURE_2D
,
118 EXPECT_TRUE(glGetError() == GL_NO_ERROR
);
120 // Check if pixels match the values that were assigned to the mapped buffer.
121 GLTestHelper::CheckPixels(0, 0, kImageWidth
, kImageHeight
, 0, pixels
);
122 EXPECT_TRUE(GL_NO_ERROR
== glGetError());
124 // Release the image.
125 glReleaseTexImage2DCHROMIUM(GL_TEXTURE_2D
, image_id
);
127 // Destroy the image.
128 glDestroyImageCHROMIUM(image_id
);