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.
5 #include "cc/resources/texture_uploader.h"
7 #include "cc/base/util.h"
8 #include "cc/resources/prioritized_resource.h"
9 #include "gpu/command_buffer/client/gles2_interface_stub.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/khronos/GLES2/gl2.h"
13 #include "third_party/khronos/GLES2/gl2ext.h"
18 class TextureUploadTestContext
: public gpu::gles2::GLES2InterfaceStub
{
20 TextureUploadTestContext() : result_available_(0), unpack_alignment_(4) {}
22 virtual void PixelStorei(GLenum pname
, GLint param
) OVERRIDE
{
24 case GL_UNPACK_ALIGNMENT
:
25 // Param should be a power of two <= 8.
26 EXPECT_EQ(0, param
& (param
- 1));
33 unpack_alignment_
= param
;
44 virtual void GetQueryObjectuivEXT(GLuint
,
46 GLuint
* value
) OVERRIDE
{
48 case GL_QUERY_RESULT_AVAILABLE_EXT
:
49 *value
= result_available_
;
57 virtual void TexSubImage2D(GLenum target
,
65 const void* pixels
) OVERRIDE
{
66 EXPECT_EQ(static_cast<unsigned>(GL_TEXTURE_2D
), target
);
70 EXPECT_LE(0, xoffset
);
71 EXPECT_LE(0, yoffset
);
75 // Check for allowed format/type combination.
76 unsigned int bytes_per_pixel
= 0;
79 EXPECT_EQ(static_cast<unsigned>(GL_UNSIGNED_BYTE
), type
);
83 EXPECT_NE(static_cast<unsigned>(GL_UNSIGNED_SHORT_4_4_4_4
), type
);
84 EXPECT_NE(static_cast<unsigned>(GL_UNSIGNED_SHORT_5_5_5_1
), type
);
86 case GL_UNSIGNED_BYTE
:
89 case GL_UNSIGNED_SHORT_5_6_5
:
95 EXPECT_NE(static_cast<unsigned>(GL_UNSIGNED_SHORT_5_6_5
), type
);
97 case GL_UNSIGNED_BYTE
:
100 case GL_UNSIGNED_SHORT_4_4_4_4
:
103 case GL_UNSIGNED_SHORT_5_5_5_1
:
109 EXPECT_EQ(static_cast<unsigned>(GL_UNSIGNED_BYTE
), type
);
112 case GL_LUMINANCE_ALPHA
:
113 EXPECT_EQ(static_cast<unsigned>(GL_UNSIGNED_BYTE
), type
);
118 // If NULL, we aren't checking texture contents.
122 const uint8
* bytes
= static_cast<const uint8
*>(pixels
);
123 // We'll expect the first byte of every row to be 0x1, and the last byte to
125 const unsigned int stride
=
126 RoundUp(bytes_per_pixel
* width
, unpack_alignment_
);
127 for (GLsizei row
= 0; row
< height
; ++row
) {
128 const uint8
* row_bytes
=
129 bytes
+ (xoffset
* bytes_per_pixel
+ (yoffset
+ row
) * stride
);
130 EXPECT_EQ(0x1, row_bytes
[0]);
131 EXPECT_EQ(0x2, row_bytes
[width
* bytes_per_pixel
- 1]);
135 void SetResultAvailable(unsigned result_available
) {
136 result_available_
= result_available
;
140 unsigned result_available_
;
141 unsigned unpack_alignment_
;
143 DISALLOW_COPY_AND_ASSIGN(TextureUploadTestContext
);
146 void UploadTexture(TextureUploader
* uploader
,
147 ResourceFormat format
,
148 const gfx::Size
& size
,
151 data
, gfx::Rect(size
), gfx::Rect(size
), gfx::Vector2d(), format
, size
);
154 TEST(TextureUploaderTest
, NumBlockingUploads
) {
155 TextureUploadTestContext context
;
156 scoped_ptr
<TextureUploader
> uploader
= TextureUploader::Create(&context
);
158 context
.SetResultAvailable(0);
159 EXPECT_EQ(0u, uploader
->NumBlockingUploads());
160 UploadTexture(uploader
.get(), RGBA_8888
, gfx::Size(), NULL
);
161 EXPECT_EQ(1u, uploader
->NumBlockingUploads());
162 UploadTexture(uploader
.get(), RGBA_8888
, gfx::Size(), NULL
);
163 EXPECT_EQ(2u, uploader
->NumBlockingUploads());
165 context
.SetResultAvailable(1);
166 EXPECT_EQ(0u, uploader
->NumBlockingUploads());
167 UploadTexture(uploader
.get(), RGBA_8888
, gfx::Size(), NULL
);
168 EXPECT_EQ(0u, uploader
->NumBlockingUploads());
169 UploadTexture(uploader
.get(), RGBA_8888
, gfx::Size(), NULL
);
170 UploadTexture(uploader
.get(), RGBA_8888
, gfx::Size(), NULL
);
171 EXPECT_EQ(0u, uploader
->NumBlockingUploads());
174 TEST(TextureUploaderTest
, MarkPendingUploadsAsNonBlocking
) {
175 TextureUploadTestContext context
;
176 scoped_ptr
<TextureUploader
> uploader
= TextureUploader::Create(&context
);
178 context
.SetResultAvailable(0);
179 EXPECT_EQ(0u, uploader
->NumBlockingUploads());
180 UploadTexture(uploader
.get(), RGBA_8888
, gfx::Size(), NULL
);
181 UploadTexture(uploader
.get(), RGBA_8888
, gfx::Size(), NULL
);
182 EXPECT_EQ(2u, uploader
->NumBlockingUploads());
184 uploader
->MarkPendingUploadsAsNonBlocking();
185 EXPECT_EQ(0u, uploader
->NumBlockingUploads());
186 UploadTexture(uploader
.get(), RGBA_8888
, gfx::Size(), NULL
);
187 EXPECT_EQ(1u, uploader
->NumBlockingUploads());
189 context
.SetResultAvailable(1);
190 EXPECT_EQ(0u, uploader
->NumBlockingUploads());
191 UploadTexture(uploader
.get(), RGBA_8888
, gfx::Size(), NULL
);
192 uploader
->MarkPendingUploadsAsNonBlocking();
193 EXPECT_EQ(0u, uploader
->NumBlockingUploads());
196 TEST(TextureUploaderTest
, UploadContentsTest
) {
197 TextureUploadTestContext context
;
198 scoped_ptr
<TextureUploader
> uploader
= TextureUploader::Create(&context
);
200 uint8 buffer
[256 * 256 * 4];
202 // Upload a tightly packed 256x256 RGBA texture.
203 memset(buffer
, 0, sizeof(buffer
));
204 for (int i
= 0; i
< 256; ++i
) {
205 // Mark the beginning and end of each row, for the test.
206 buffer
[i
* 4 * 256] = 0x1;
207 buffer
[(i
+ 1) * 4 * 256 - 1] = 0x2;
209 UploadTexture(uploader
.get(), RGBA_8888
, gfx::Size(256, 256), buffer
);
211 // Upload a tightly packed 41x43 RGBA texture.
212 memset(buffer
, 0, sizeof(buffer
));
213 for (int i
= 0; i
< 43; ++i
) {
214 // Mark the beginning and end of each row, for the test.
215 buffer
[i
* 4 * 41] = 0x1;
216 buffer
[(i
+ 1) * 4 * 41 - 1] = 0x2;
218 UploadTexture(uploader
.get(), RGBA_8888
, gfx::Size(41, 43), buffer
);
220 // Upload a tightly packed 82x86 LUMINANCE texture.
221 memset(buffer
, 0, sizeof(buffer
));
222 for (int i
= 0; i
< 86; ++i
) {
223 // Mark the beginning and end of each row, for the test.
224 buffer
[i
* 1 * 82] = 0x1;
225 buffer
[(i
+ 1) * 82 - 1] = 0x2;
227 UploadTexture(uploader
.get(), LUMINANCE_8
, gfx::Size(82, 86), buffer
);