| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file looks like a unit test, but it contains benchmarks and test | 5 // This file looks like a unit test, but it contains benchmarks and test |
| 6 // utilities intended for manual evaluation of the scalers in | 6 // utilities intended for manual evaluation of the scalers in |
| 7 // gl_helper*. These tests produce output in the form of files and printouts, | 7 // gl_helper*. These tests produce output in the form of files and printouts, |
| 8 // but cannot really "fail". There is no point in making these tests part | 8 // but cannot really "fail". There is no point in making these tests part |
| 9 // of any test automation run. | 9 // of any test automation run. |
| 10 | 10 |
| 11 #include <stddef.h> | 11 #include <stddef.h> |
| 12 #include <stdio.h> | 12 #include <stdio.h> |
| 13 #include <cmath> | 13 #include <cmath> |
| 14 #include <string> | 14 #include <string> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include <GLES2/gl2.h> | 17 #include <GLES2/gl2.h> |
| 18 #include <GLES2/gl2ext.h> | 18 #include <GLES2/gl2ext.h> |
| 19 #include <GLES2/gl2extchromium.h> | 19 #include <GLES2/gl2extchromium.h> |
| 20 | 20 |
| 21 #include "base/at_exit.h" | 21 #include "base/at_exit.h" |
| 22 #include "base/command_line.h" | 22 #include "base/command_line.h" |
| 23 #include "base/files/file_util.h" | 23 #include "base/files/file_util.h" |
| 24 #include "base/macros.h" | 24 #include "base/macros.h" |
| 25 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
| 26 #include "base/time/time.h" | 26 #include "base/time/time.h" |
| 27 #include "content/common/gpu/client/gl_helper.h" | 27 #include "content/browser/compositor/gl_helper.h" |
| 28 #include "content/common/gpu/client/gl_helper_scaling.h" | 28 #include "content/browser/compositor/gl_helper_scaling.h" |
| 29 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h" | 29 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 31 #include "third_party/skia/include/core/SkBitmap.h" | 31 #include "third_party/skia/include/core/SkBitmap.h" |
| 32 #include "third_party/skia/include/core/SkTypes.h" | 32 #include "third_party/skia/include/core/SkTypes.h" |
| 33 #include "ui/gfx/codec/png_codec.h" | 33 #include "ui/gfx/codec/png_codec.h" |
| 34 #include "ui/gl/gl_surface.h" | 34 #include "ui/gl/gl_surface.h" |
| 35 | 35 |
| 36 namespace content { | 36 namespace content { |
| 37 | 37 |
| 38 using blink::WebGLId; | 38 using blink::WebGLId; |
| 39 using blink::WebGraphicsContext3D; | 39 using blink::WebGraphicsContext3D; |
| 40 | 40 |
| 41 content::GLHelper::ScalerQuality kQualities[] = { | 41 content::GLHelper::ScalerQuality kQualities[] = { |
| 42 content::GLHelper::SCALER_QUALITY_BEST, | 42 content::GLHelper::SCALER_QUALITY_BEST, |
| 43 content::GLHelper::SCALER_QUALITY_GOOD, | 43 content::GLHelper::SCALER_QUALITY_GOOD, |
| 44 content::GLHelper::SCALER_QUALITY_FAST, | 44 content::GLHelper::SCALER_QUALITY_FAST, |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 const char *kQualityNames[] = { | 47 const char* kQualityNames[] = { |
| 48 "best", | 48 "best", "good", "fast", |
| 49 "good", | |
| 50 "fast", | |
| 51 }; | 49 }; |
| 52 | 50 |
| 53 class GLHelperTest : public testing::Test { | 51 class GLHelperTest : public testing::Test { |
| 54 protected: | 52 protected: |
| 55 void SetUp() override { | 53 void SetUp() override { |
| 56 WebGraphicsContext3D::Attributes attributes; | 54 WebGraphicsContext3D::Attributes attributes; |
| 57 bool lose_context_when_out_of_memory = false; | 55 bool lose_context_when_out_of_memory = false; |
| 58 context_ = gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl:: | 56 context_ = gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl:: |
| 59 CreateOffscreenContext(attributes, lose_context_when_out_of_memory); | 57 CreateOffscreenContext(attributes, lose_context_when_out_of_memory); |
| 60 context_->InitializeOnCurrentThread(); | 58 context_->InitializeOnCurrentThread(); |
| 61 | 59 |
| 62 helper_.reset( | 60 helper_.reset(new content::GLHelper(context_->GetGLInterface(), |
| 63 new content::GLHelper(context_->GetGLInterface(), | 61 context_->GetContextSupport())); |
| 64 context_->GetContextSupport())); | |
| 65 helper_scaling_.reset(new content::GLHelperScaling( | 62 helper_scaling_.reset(new content::GLHelperScaling( |
| 66 context_->GetGLInterface(), | 63 context_->GetGLInterface(), helper_.get())); |
| 67 helper_.get())); | |
| 68 } | 64 } |
| 69 | 65 |
| 70 void TearDown() override { | 66 void TearDown() override { |
| 71 helper_scaling_.reset(NULL); | 67 helper_scaling_.reset(NULL); |
| 72 helper_.reset(NULL); | 68 helper_.reset(NULL); |
| 73 context_.reset(NULL); | 69 context_.reset(NULL); |
| 74 } | 70 } |
| 75 | 71 |
| 76 | 72 void LoadPngFileToSkBitmap(const base::FilePath& filename, SkBitmap* bitmap) { |
| 77 void LoadPngFileToSkBitmap(const base::FilePath& filename, | |
| 78 SkBitmap* bitmap) { | |
| 79 std::string compressed; | 73 std::string compressed; |
| 80 base::ReadFileToString(base::MakeAbsoluteFilePath(filename), &compressed); | 74 base::ReadFileToString(base::MakeAbsoluteFilePath(filename), &compressed); |
| 81 ASSERT_TRUE(compressed.size()); | 75 ASSERT_TRUE(compressed.size()); |
| 82 ASSERT_TRUE(gfx::PNGCodec::Decode( | 76 ASSERT_TRUE(gfx::PNGCodec::Decode( |
| 83 reinterpret_cast<const unsigned char*>(compressed.data()), | 77 reinterpret_cast<const unsigned char*>(compressed.data()), |
| 84 compressed.size(), bitmap)); | 78 compressed.size(), bitmap)); |
| 85 } | 79 } |
| 86 | 80 |
| 87 // Save the image to a png file. Used to create the initial test files. | 81 // Save the image to a png file. Used to create the initial test files. |
| 88 void SaveToFile(SkBitmap* bitmap, const base::FilePath& filename) { | 82 void SaveToFile(SkBitmap* bitmap, const base::FilePath& filename) { |
| 89 std::vector<unsigned char> compressed; | 83 std::vector<unsigned char> compressed; |
| 90 ASSERT_TRUE(gfx::PNGCodec::Encode( | 84 ASSERT_TRUE(gfx::PNGCodec::Encode( |
| 91 static_cast<unsigned char*>(bitmap->getPixels()), | 85 static_cast<unsigned char*>(bitmap->getPixels()), |
| 92 gfx::PNGCodec::FORMAT_BGRA, | 86 gfx::PNGCodec::FORMAT_BGRA, |
| 93 gfx::Size(bitmap->width(), bitmap->height()), | 87 gfx::Size(bitmap->width(), bitmap->height()), |
| 94 static_cast<int>(bitmap->rowBytes()), | 88 static_cast<int>(bitmap->rowBytes()), true, |
| 95 true, | 89 std::vector<gfx::PNGCodec::Comment>(), &compressed)); |
| 96 std::vector<gfx::PNGCodec::Comment>(), | |
| 97 &compressed)); | |
| 98 ASSERT_TRUE(compressed.size()); | 90 ASSERT_TRUE(compressed.size()); |
| 99 FILE* f = base::OpenFile(filename, "wb"); | 91 FILE* f = base::OpenFile(filename, "wb"); |
| 100 ASSERT_TRUE(f); | 92 ASSERT_TRUE(f); |
| 101 ASSERT_EQ(fwrite(&*compressed.begin(), 1, compressed.size(), f), | 93 ASSERT_EQ(fwrite(&*compressed.begin(), 1, compressed.size(), f), |
| 102 compressed.size()); | 94 compressed.size()); |
| 103 base::CloseFile(f); | 95 base::CloseFile(f); |
| 104 } | 96 } |
| 105 | 97 |
| 106 scoped_ptr<gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl> | 98 scoped_ptr<gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl> |
| 107 context_; | 99 context_; |
| 108 scoped_ptr<content::GLHelper> helper_; | 100 scoped_ptr<content::GLHelper> helper_; |
| 109 scoped_ptr<content::GLHelperScaling> helper_scaling_; | 101 scoped_ptr<content::GLHelperScaling> helper_scaling_; |
| 110 std::deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_; | 102 std::deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_; |
| 111 }; | 103 }; |
| 112 | 104 |
| 113 | |
| 114 TEST_F(GLHelperTest, ScaleBenchmark) { | 105 TEST_F(GLHelperTest, ScaleBenchmark) { |
| 115 int output_sizes[] = { 1920, 1080, | 106 int output_sizes[] = {1920, 1080, 1249, 720, // Output size on pixel |
| 116 1249, 720, // Output size on pixel | 107 256, 144}; |
| 117 256, 144 }; | 108 int input_sizes[] = {3200, 2040, 2560, 1476, // Pixel tab size |
| 118 int input_sizes[] = { 3200, 2040, | 109 1920, 1080, 1280, 720, 800, 480, 256, 144}; |
| 119 2560, 1476, // Pixel tab size | |
| 120 1920, 1080, | |
| 121 1280, 720, | |
| 122 800, 480, | |
| 123 256, 144 }; | |
| 124 | 110 |
| 125 for (size_t q = 0; q < arraysize(kQualities); q++) { | 111 for (size_t q = 0; q < arraysize(kQualities); q++) { |
| 126 for (size_t outsize = 0; | 112 for (size_t outsize = 0; outsize < arraysize(output_sizes); outsize += 2) { |
| 127 outsize < arraysize(output_sizes); | 113 for (size_t insize = 0; insize < arraysize(input_sizes); insize += 2) { |
| 128 outsize += 2) { | |
| 129 for (size_t insize = 0; | |
| 130 insize < arraysize(input_sizes); | |
| 131 insize += 2) { | |
| 132 WebGLId src_texture = context_->createTexture(); | 114 WebGLId src_texture = context_->createTexture(); |
| 133 WebGLId dst_texture = context_->createTexture(); | 115 WebGLId dst_texture = context_->createTexture(); |
| 134 WebGLId framebuffer = context_->createFramebuffer(); | 116 WebGLId framebuffer = context_->createFramebuffer(); |
| 135 const gfx::Size src_size(input_sizes[insize], | 117 const gfx::Size src_size(input_sizes[insize], input_sizes[insize + 1]); |
| 136 input_sizes[insize + 1]); | |
| 137 const gfx::Size dst_size(output_sizes[outsize], | 118 const gfx::Size dst_size(output_sizes[outsize], |
| 138 output_sizes[outsize + 1]); | 119 output_sizes[outsize + 1]); |
| 139 SkBitmap input; | 120 SkBitmap input; |
| 140 input.allocN32Pixels(src_size.width(), src_size.height()); | 121 input.allocN32Pixels(src_size.width(), src_size.height()); |
| 141 | 122 |
| 142 SkBitmap output_pixels; | 123 SkBitmap output_pixels; |
| 143 output_pixels.allocN32Pixels(dst_size.width(), dst_size.height()); | 124 output_pixels.allocN32Pixels(dst_size.width(), dst_size.height()); |
| 144 | 125 |
| 145 context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer); | 126 context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer); |
| 146 context_->bindTexture(GL_TEXTURE_2D, dst_texture); | 127 context_->bindTexture(GL_TEXTURE_2D, dst_texture); |
| 147 context_->texImage2D(GL_TEXTURE_2D, | 128 context_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dst_size.width(), |
| 148 0, | 129 dst_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, |
| 149 GL_RGBA, | |
| 150 dst_size.width(), | |
| 151 dst_size.height(), | |
| 152 0, | |
| 153 GL_RGBA, | |
| 154 GL_UNSIGNED_BYTE, | |
| 155 0); | 130 0); |
| 156 context_->bindTexture(GL_TEXTURE_2D, src_texture); | 131 context_->bindTexture(GL_TEXTURE_2D, src_texture); |
| 157 context_->texImage2D(GL_TEXTURE_2D, | 132 context_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_size.width(), |
| 158 0, | 133 src_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, |
| 159 GL_RGBA, | |
| 160 src_size.width(), | |
| 161 src_size.height(), | |
| 162 0, | |
| 163 GL_RGBA, | |
| 164 GL_UNSIGNED_BYTE, | |
| 165 input.getPixels()); | 134 input.getPixels()); |
| 166 | 135 |
| 167 gfx::Rect src_subrect(0, 0, | 136 gfx::Rect src_subrect(0, 0, src_size.width(), src_size.height()); |
| 168 src_size.width(), src_size.height()); | |
| 169 scoped_ptr<content::GLHelper::ScalerInterface> scaler( | 137 scoped_ptr<content::GLHelper::ScalerInterface> scaler( |
| 170 helper_->CreateScaler(kQualities[q], | 138 helper_->CreateScaler(kQualities[q], src_size, src_subrect, |
| 171 src_size, | 139 dst_size, false, false)); |
| 172 src_subrect, | |
| 173 dst_size, | |
| 174 false, | |
| 175 false)); | |
| 176 // Scale once beforehand before we start measuring. | 140 // Scale once beforehand before we start measuring. |
| 177 scaler->Scale(src_texture, dst_texture); | 141 scaler->Scale(src_texture, dst_texture); |
| 178 context_->finish(); | 142 context_->finish(); |
| 179 | 143 |
| 180 base::TimeTicks start_time = base::TimeTicks::Now(); | 144 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 181 int iterations = 0; | 145 int iterations = 0; |
| 182 base::TimeTicks end_time; | 146 base::TimeTicks end_time; |
| 183 while (true) { | 147 while (true) { |
| 184 for (int i = 0; i < 50; i++) { | 148 for (int i = 0; i < 50; i++) { |
| 185 iterations++; | 149 iterations++; |
| 186 scaler->Scale(src_texture, dst_texture); | 150 scaler->Scale(src_texture, dst_texture); |
| 187 context_->flush(); | 151 context_->flush(); |
| 188 } | 152 } |
| 189 context_->finish(); | 153 context_->finish(); |
| 190 end_time = base::TimeTicks::Now(); | 154 end_time = base::TimeTicks::Now(); |
| 191 if (iterations > 2000) { | 155 if (iterations > 2000) { |
| 192 break; | 156 break; |
| 193 } | 157 } |
| 194 if ((end_time - start_time).InMillisecondsF() > 1000) { | 158 if ((end_time - start_time).InMillisecondsF() > 1000) { |
| 195 break; | 159 break; |
| 196 } | 160 } |
| 197 } | 161 } |
| 198 context_->deleteTexture(dst_texture); | 162 context_->deleteTexture(dst_texture); |
| 199 context_->deleteTexture(src_texture); | 163 context_->deleteTexture(src_texture); |
| 200 context_->deleteFramebuffer(framebuffer); | 164 context_->deleteFramebuffer(framebuffer); |
| 201 | 165 |
| 202 std::string name; | 166 std::string name; |
| 203 name = base::StringPrintf("scale_%dx%d_to_%dx%d_%s", | 167 name = base::StringPrintf("scale_%dx%d_to_%dx%d_%s", src_size.width(), |
| 204 src_size.width(), | 168 src_size.height(), dst_size.width(), |
| 205 src_size.height(), | 169 dst_size.height(), kQualityNames[q]); |
| 206 dst_size.width(), | |
| 207 dst_size.height(), | |
| 208 kQualityNames[q]); | |
| 209 | 170 |
| 210 float ms = (end_time - start_time).InMillisecondsF() / iterations; | 171 float ms = (end_time - start_time).InMillisecondsF() / iterations; |
| 211 printf("*RESULT gpu_scale_time: %s=%.2f ms\n", name.c_str(), ms); | 172 VLOG(0) << base::StringPrintf("*RESULT gpu_scale_time: %s=%.2f ms\n", |
| 173 name.c_str(), ms); |
| 212 } | 174 } |
| 213 } | 175 } |
| 214 } | 176 } |
| 215 } | 177 } |
| 216 | 178 |
| 217 // This is more of a test utility than a test. | 179 // This is more of a test utility than a test. |
| 218 // Put an PNG image called "testimage.png" in your | 180 // Put an PNG image called "testimage.png" in your |
| 219 // current directory, then run this test. It will | 181 // current directory, then run this test. It will |
| 220 // create testoutput_Q_P.png, where Q is the scaling | 182 // create testoutput_Q_P.png, where Q is the scaling |
| 221 // mode and P is the scaling percentage taken from | 183 // mode and P is the scaling percentage taken from |
| 222 // the table below. | 184 // the table below. |
| 223 TEST_F(GLHelperTest, DISABLED_ScaleTestImage) { | 185 TEST_F(GLHelperTest, DISABLED_ScaleTestImage) { |
| 224 int percents[] = { | 186 int percents[] = { |
| 225 230, | 187 230, 180, 150, 110, 90, 70, 50, 49, 40, 20, 10, |
| 226 180, | |
| 227 150, | |
| 228 110, | |
| 229 90, | |
| 230 70, | |
| 231 50, | |
| 232 49, | |
| 233 40, | |
| 234 20, | |
| 235 10, | |
| 236 }; | 188 }; |
| 237 | 189 |
| 238 SkBitmap input; | 190 SkBitmap input; |
| 239 LoadPngFileToSkBitmap(base::FilePath( | 191 LoadPngFileToSkBitmap(base::FilePath(FILE_PATH_LITERAL("testimage.png")), |
| 240 FILE_PATH_LITERAL("testimage.png")), &input); | 192 &input); |
| 241 | 193 |
| 242 WebGLId framebuffer = context_->createFramebuffer(); | 194 WebGLId framebuffer = context_->createFramebuffer(); |
| 243 WebGLId src_texture = context_->createTexture(); | 195 WebGLId src_texture = context_->createTexture(); |
| 244 const gfx::Size src_size(input.width(), input.height()); | 196 const gfx::Size src_size(input.width(), input.height()); |
| 245 context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer); | 197 context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer); |
| 246 context_->bindTexture(GL_TEXTURE_2D, src_texture); | 198 context_->bindTexture(GL_TEXTURE_2D, src_texture); |
| 247 context_->texImage2D(GL_TEXTURE_2D, | 199 context_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_size.width(), |
| 248 0, | 200 src_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, |
| 249 GL_RGBA, | |
| 250 src_size.width(), | |
| 251 src_size.height(), | |
| 252 0, | |
| 253 GL_RGBA, | |
| 254 GL_UNSIGNED_BYTE, | |
| 255 input.getPixels()); | 201 input.getPixels()); |
| 256 | 202 |
| 257 for (size_t q = 0; q < arraysize(kQualities); q++) { | 203 for (size_t q = 0; q < arraysize(kQualities); q++) { |
| 258 for (size_t p = 0; p < arraysize(percents); p++) { | 204 for (size_t p = 0; p < arraysize(percents); p++) { |
| 259 const gfx::Size dst_size(input.width() * percents[p] / 100, | 205 const gfx::Size dst_size(input.width() * percents[p] / 100, |
| 260 input.height() * percents[p] / 100); | 206 input.height() * percents[p] / 100); |
| 261 WebGLId dst_texture = helper_->CopyAndScaleTexture( | 207 WebGLId dst_texture = helper_->CopyAndScaleTexture( |
| 262 src_texture, | 208 src_texture, src_size, dst_size, false, kQualities[q]); |
| 263 src_size, | |
| 264 dst_size, | |
| 265 false, | |
| 266 kQualities[q]); | |
| 267 | 209 |
| 268 SkBitmap output_pixels; | 210 SkBitmap output_pixels; |
| 269 output_pixels.allocN32Pixels(dst_size.width(), dst_size.height()); | 211 output_pixels.allocN32Pixels(dst_size.width(), dst_size.height()); |
| 270 | 212 |
| 271 helper_->ReadbackTextureSync( | 213 helper_->ReadbackTextureSync( |
| 272 dst_texture, | 214 dst_texture, gfx::Rect(0, 0, dst_size.width(), dst_size.height()), |
| 273 gfx::Rect(0, 0, | 215 static_cast<unsigned char*>(output_pixels.getPixels()), |
| 274 dst_size.width(), | |
| 275 dst_size.height()), | |
| 276 static_cast<unsigned char *>(output_pixels.getPixels()), | |
| 277 kN32_SkColorType); | 216 kN32_SkColorType); |
| 278 context_->deleteTexture(dst_texture); | 217 context_->deleteTexture(dst_texture); |
| 279 std::string filename = base::StringPrintf("testoutput_%s_%d.ppm", | 218 std::string filename = base::StringPrintf("testoutput_%s_%d.ppm", |
| 280 kQualityNames[q], | 219 kQualityNames[q], percents[p]); |
| 281 percents[p]); | 220 VLOG(0) << "Writing " << filename; |
| 282 VLOG(0) << "Writing " << filename; | |
| 283 SaveToFile(&output_pixels, base::FilePath::FromUTF8Unsafe(filename)); | 221 SaveToFile(&output_pixels, base::FilePath::FromUTF8Unsafe(filename)); |
| 284 } | 222 } |
| 285 } | 223 } |
| 286 context_->deleteTexture(src_texture); | 224 context_->deleteTexture(src_texture); |
| 287 context_->deleteFramebuffer(framebuffer); | 225 context_->deleteFramebuffer(framebuffer); |
| 288 } | 226 } |
| 289 | 227 |
| 290 } // namespace | 228 } // namespace |
| OLD | NEW |