| 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/browser/compositor/gl_helper.h" | 27 #include "content/browser/compositor/gl_helper.h" |
| 28 #include "content/browser/compositor/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/command_buffer/client/gl_in_process_context.h" |
| 30 #include "gpu/command_buffer/client/gles2_implementation.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 31 #include "third_party/skia/include/core/SkBitmap.h" | 32 #include "third_party/skia/include/core/SkBitmap.h" |
| 32 #include "third_party/skia/include/core/SkTypes.h" | 33 #include "third_party/skia/include/core/SkTypes.h" |
| 33 #include "ui/gfx/codec/png_codec.h" | 34 #include "ui/gfx/codec/png_codec.h" |
| 34 #include "ui/gl/gl_surface.h" | 35 #include "ui/gl/gl_surface.h" |
| 35 | 36 |
| 36 namespace content { | 37 namespace content { |
| 37 | 38 |
| 38 using blink::WebGLId; | |
| 39 using blink::WebGraphicsContext3D; | |
| 40 | |
| 41 content::GLHelper::ScalerQuality kQualities[] = { | 39 content::GLHelper::ScalerQuality kQualities[] = { |
| 42 content::GLHelper::SCALER_QUALITY_BEST, | 40 content::GLHelper::SCALER_QUALITY_BEST, |
| 43 content::GLHelper::SCALER_QUALITY_GOOD, | 41 content::GLHelper::SCALER_QUALITY_GOOD, |
| 44 content::GLHelper::SCALER_QUALITY_FAST, | 42 content::GLHelper::SCALER_QUALITY_FAST, |
| 45 }; | 43 }; |
| 46 | 44 |
| 47 const char* kQualityNames[] = { | 45 const char* kQualityNames[] = { |
| 48 "best", "good", "fast", | 46 "best", "good", "fast", |
| 49 }; | 47 }; |
| 50 | 48 |
| 51 class GLHelperTest : public testing::Test { | 49 class GLHelperTest : public testing::Test { |
| 52 protected: | 50 protected: |
| 53 void SetUp() override { | 51 void SetUp() override { |
| 54 WebGraphicsContext3D::Attributes attributes; | 52 gpu::gles2::ContextCreationAttribHelper attributes; |
| 55 bool lose_context_when_out_of_memory = false; | 53 attributes.alpha_size = 8; |
| 56 context_ = gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl:: | 54 attributes.depth_size = 24; |
| 57 CreateOffscreenContext(attributes, lose_context_when_out_of_memory); | 55 attributes.red_size = 8; |
| 58 context_->InitializeOnCurrentThread(); | 56 attributes.green_size = 8; |
| 57 attributes.blue_size = 8; |
| 58 attributes.stencil_size = 8; |
| 59 attributes.samples = 4; |
| 60 attributes.sample_buffers = 1; |
| 61 attributes.bind_generates_resource = false; |
| 59 | 62 |
| 60 helper_.reset(new content::GLHelper(context_->GetGLInterface(), | 63 context_.reset(gpu::GLInProcessContext::Create( |
| 61 context_->GetContextSupport())); | 64 nullptr, /* service */ |
| 62 helper_scaling_.reset(new content::GLHelperScaling( | 65 nullptr, /* surface */ |
| 63 context_->GetGLInterface(), helper_.get())); | 66 true, /* offscreen */ |
| 67 gfx::kNullAcceleratedWidget, /* window */ |
| 68 gfx::Size(1, 1), /* size */ |
| 69 nullptr, /* share_context */ |
| 70 true, /* use_global_share_group */ |
| 71 attributes, gfx::PreferDiscreteGpu, |
| 72 ::gpu::GLInProcessContextSharedMemoryLimits(), |
| 73 nullptr, /* gpu_memory_buffer_manager */ |
| 74 nullptr /* image_factory */)); |
| 75 gl_ = context_->GetImplementation(); |
| 76 gpu::ContextSupport* support = context_->GetImplementation(); |
| 77 |
| 78 helper_.reset(new content::GLHelper(gl_, support)); |
| 79 helper_scaling_.reset(new content::GLHelperScaling(gl_, helper_.get())); |
| 64 } | 80 } |
| 65 | 81 |
| 66 void TearDown() override { | 82 void TearDown() override { |
| 67 helper_scaling_.reset(NULL); | 83 helper_scaling_.reset(NULL); |
| 68 helper_.reset(NULL); | 84 helper_.reset(NULL); |
| 69 context_.reset(NULL); | 85 context_.reset(NULL); |
| 70 } | 86 } |
| 71 | 87 |
| 72 void LoadPngFileToSkBitmap(const base::FilePath& filename, SkBitmap* bitmap) { | 88 void LoadPngFileToSkBitmap(const base::FilePath& filename, SkBitmap* bitmap) { |
| 73 std::string compressed; | 89 std::string compressed; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 88 static_cast<int>(bitmap->rowBytes()), true, | 104 static_cast<int>(bitmap->rowBytes()), true, |
| 89 std::vector<gfx::PNGCodec::Comment>(), &compressed)); | 105 std::vector<gfx::PNGCodec::Comment>(), &compressed)); |
| 90 ASSERT_TRUE(compressed.size()); | 106 ASSERT_TRUE(compressed.size()); |
| 91 FILE* f = base::OpenFile(filename, "wb"); | 107 FILE* f = base::OpenFile(filename, "wb"); |
| 92 ASSERT_TRUE(f); | 108 ASSERT_TRUE(f); |
| 93 ASSERT_EQ(fwrite(&*compressed.begin(), 1, compressed.size(), f), | 109 ASSERT_EQ(fwrite(&*compressed.begin(), 1, compressed.size(), f), |
| 94 compressed.size()); | 110 compressed.size()); |
| 95 base::CloseFile(f); | 111 base::CloseFile(f); |
| 96 } | 112 } |
| 97 | 113 |
| 98 scoped_ptr<gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl> | 114 scoped_ptr<gpu::GLInProcessContext> context_; |
| 99 context_; | 115 gpu::gles2::GLES2Interface* gl_; |
| 100 scoped_ptr<content::GLHelper> helper_; | 116 scoped_ptr<content::GLHelper> helper_; |
| 101 scoped_ptr<content::GLHelperScaling> helper_scaling_; | 117 scoped_ptr<content::GLHelperScaling> helper_scaling_; |
| 102 std::deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_; | 118 std::deque<GLHelperScaling::ScaleOp> x_ops_, y_ops_; |
| 103 }; | 119 }; |
| 104 | 120 |
| 105 TEST_F(GLHelperTest, ScaleBenchmark) { | 121 TEST_F(GLHelperTest, ScaleBenchmark) { |
| 106 int output_sizes[] = {1920, 1080, 1249, 720, // Output size on pixel | 122 int output_sizes[] = {1920, 1080, 1249, 720, // Output size on pixel |
| 107 256, 144}; | 123 256, 144}; |
| 108 int input_sizes[] = {3200, 2040, 2560, 1476, // Pixel tab size | 124 int input_sizes[] = {3200, 2040, 2560, 1476, // Pixel tab size |
| 109 1920, 1080, 1280, 720, 800, 480, 256, 144}; | 125 1920, 1080, 1280, 720, 800, 480, 256, 144}; |
| 110 | 126 |
| 111 for (size_t q = 0; q < arraysize(kQualities); q++) { | 127 for (size_t q = 0; q < arraysize(kQualities); q++) { |
| 112 for (size_t outsize = 0; outsize < arraysize(output_sizes); outsize += 2) { | 128 for (size_t outsize = 0; outsize < arraysize(output_sizes); outsize += 2) { |
| 113 for (size_t insize = 0; insize < arraysize(input_sizes); insize += 2) { | 129 for (size_t insize = 0; insize < arraysize(input_sizes); insize += 2) { |
| 114 WebGLId src_texture = context_->createTexture(); | 130 uint32_t src_texture; |
| 115 WebGLId dst_texture = context_->createTexture(); | 131 gl_->GenTextures(1, &src_texture); |
| 116 WebGLId framebuffer = context_->createFramebuffer(); | 132 uint32_t dst_texture; |
| 133 gl_->GenTextures(1, &dst_texture); |
| 134 uint32_t framebuffer; |
| 135 gl_->GenFramebuffers(1, &framebuffer); |
| 117 const gfx::Size src_size(input_sizes[insize], input_sizes[insize + 1]); | 136 const gfx::Size src_size(input_sizes[insize], input_sizes[insize + 1]); |
| 118 const gfx::Size dst_size(output_sizes[outsize], | 137 const gfx::Size dst_size(output_sizes[outsize], |
| 119 output_sizes[outsize + 1]); | 138 output_sizes[outsize + 1]); |
| 120 SkBitmap input; | 139 SkBitmap input; |
| 121 input.allocN32Pixels(src_size.width(), src_size.height()); | 140 input.allocN32Pixels(src_size.width(), src_size.height()); |
| 122 | 141 |
| 123 SkBitmap output_pixels; | 142 SkBitmap output_pixels; |
| 124 output_pixels.allocN32Pixels(dst_size.width(), dst_size.height()); | 143 output_pixels.allocN32Pixels(dst_size.width(), dst_size.height()); |
| 125 | 144 |
| 126 context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer); | 145 gl_->BindFramebuffer(GL_FRAMEBUFFER, framebuffer); |
| 127 context_->bindTexture(GL_TEXTURE_2D, dst_texture); | 146 gl_->BindTexture(GL_TEXTURE_2D, dst_texture); |
| 128 context_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dst_size.width(), | 147 gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dst_size.width(), |
| 129 dst_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, | 148 dst_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); |
| 130 0); | 149 gl_->BindTexture(GL_TEXTURE_2D, src_texture); |
| 131 context_->bindTexture(GL_TEXTURE_2D, src_texture); | 150 gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_size.width(), |
| 132 context_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_size.width(), | 151 src_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, |
| 133 src_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, | 152 input.getPixels()); |
| 134 input.getPixels()); | |
| 135 | 153 |
| 136 gfx::Rect src_subrect(0, 0, src_size.width(), src_size.height()); | 154 gfx::Rect src_subrect(0, 0, src_size.width(), src_size.height()); |
| 137 scoped_ptr<content::GLHelper::ScalerInterface> scaler( | 155 scoped_ptr<content::GLHelper::ScalerInterface> scaler( |
| 138 helper_->CreateScaler(kQualities[q], src_size, src_subrect, | 156 helper_->CreateScaler(kQualities[q], src_size, src_subrect, |
| 139 dst_size, false, false)); | 157 dst_size, false, false)); |
| 140 // Scale once beforehand before we start measuring. | 158 // Scale once beforehand before we start measuring. |
| 141 scaler->Scale(src_texture, dst_texture); | 159 scaler->Scale(src_texture, dst_texture); |
| 142 context_->finish(); | 160 gl_->Finish(); |
| 143 | 161 |
| 144 base::TimeTicks start_time = base::TimeTicks::Now(); | 162 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 145 int iterations = 0; | 163 int iterations = 0; |
| 146 base::TimeTicks end_time; | 164 base::TimeTicks end_time; |
| 147 while (true) { | 165 while (true) { |
| 148 for (int i = 0; i < 50; i++) { | 166 for (int i = 0; i < 50; i++) { |
| 149 iterations++; | 167 iterations++; |
| 150 scaler->Scale(src_texture, dst_texture); | 168 scaler->Scale(src_texture, dst_texture); |
| 151 context_->flush(); | 169 gl_->Flush(); |
| 152 } | 170 } |
| 153 context_->finish(); | 171 gl_->Finish(); |
| 154 end_time = base::TimeTicks::Now(); | 172 end_time = base::TimeTicks::Now(); |
| 155 if (iterations > 2000) { | 173 if (iterations > 2000) { |
| 156 break; | 174 break; |
| 157 } | 175 } |
| 158 if ((end_time - start_time).InMillisecondsF() > 1000) { | 176 if ((end_time - start_time).InMillisecondsF() > 1000) { |
| 159 break; | 177 break; |
| 160 } | 178 } |
| 161 } | 179 } |
| 162 context_->deleteTexture(dst_texture); | 180 gl_->DeleteTextures(1, &dst_texture); |
| 163 context_->deleteTexture(src_texture); | 181 gl_->DeleteTextures(1, &src_texture); |
| 164 context_->deleteFramebuffer(framebuffer); | 182 gl_->DeleteFramebuffers(1, &framebuffer); |
| 165 | 183 |
| 166 std::string name; | 184 std::string name; |
| 167 name = base::StringPrintf("scale_%dx%d_to_%dx%d_%s", src_size.width(), | 185 name = base::StringPrintf("scale_%dx%d_to_%dx%d_%s", src_size.width(), |
| 168 src_size.height(), dst_size.width(), | 186 src_size.height(), dst_size.width(), |
| 169 dst_size.height(), kQualityNames[q]); | 187 dst_size.height(), kQualityNames[q]); |
| 170 | 188 |
| 171 float ms = (end_time - start_time).InMillisecondsF() / iterations; | 189 float ms = (end_time - start_time).InMillisecondsF() / iterations; |
| 172 VLOG(0) << base::StringPrintf("*RESULT gpu_scale_time: %s=%.2f ms\n", | 190 VLOG(0) << base::StringPrintf("*RESULT gpu_scale_time: %s=%.2f ms\n", |
| 173 name.c_str(), ms); | 191 name.c_str(), ms); |
| 174 } | 192 } |
| 175 } | 193 } |
| 176 } | 194 } |
| 177 } | 195 } |
| 178 | 196 |
| 179 // This is more of a test utility than a test. | 197 // This is more of a test utility than a test. |
| 180 // Put an PNG image called "testimage.png" in your | 198 // Put an PNG image called "testimage.png" in your |
| 181 // current directory, then run this test. It will | 199 // current directory, then run this test. It will |
| 182 // create testoutput_Q_P.png, where Q is the scaling | 200 // create testoutput_Q_P.png, where Q is the scaling |
| 183 // mode and P is the scaling percentage taken from | 201 // mode and P is the scaling percentage taken from |
| 184 // the table below. | 202 // the table below. |
| 185 TEST_F(GLHelperTest, DISABLED_ScaleTestImage) { | 203 TEST_F(GLHelperTest, DISABLED_ScaleTestImage) { |
| 186 int percents[] = { | 204 int percents[] = { |
| 187 230, 180, 150, 110, 90, 70, 50, 49, 40, 20, 10, | 205 230, 180, 150, 110, 90, 70, 50, 49, 40, 20, 10, |
| 188 }; | 206 }; |
| 189 | 207 |
| 190 SkBitmap input; | 208 SkBitmap input; |
| 191 LoadPngFileToSkBitmap(base::FilePath(FILE_PATH_LITERAL("testimage.png")), | 209 LoadPngFileToSkBitmap(base::FilePath(FILE_PATH_LITERAL("testimage.png")), |
| 192 &input); | 210 &input); |
| 193 | 211 |
| 194 WebGLId framebuffer = context_->createFramebuffer(); | 212 uint32_t framebuffer; |
| 195 WebGLId src_texture = context_->createTexture(); | 213 gl_->GenFramebuffers(1, &framebuffer); |
| 214 uint32_t src_texture; |
| 215 gl_->GenTextures(1, &src_texture); |
| 196 const gfx::Size src_size(input.width(), input.height()); | 216 const gfx::Size src_size(input.width(), input.height()); |
| 197 context_->bindFramebuffer(GL_FRAMEBUFFER, framebuffer); | 217 gl_->BindFramebuffer(GL_FRAMEBUFFER, framebuffer); |
| 198 context_->bindTexture(GL_TEXTURE_2D, src_texture); | 218 gl_->BindTexture(GL_TEXTURE_2D, src_texture); |
| 199 context_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_size.width(), | 219 gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_size.width(), |
| 200 src_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, | 220 src_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, |
| 201 input.getPixels()); | 221 input.getPixels()); |
| 202 | 222 |
| 203 for (size_t q = 0; q < arraysize(kQualities); q++) { | 223 for (size_t q = 0; q < arraysize(kQualities); q++) { |
| 204 for (size_t p = 0; p < arraysize(percents); p++) { | 224 for (size_t p = 0; p < arraysize(percents); p++) { |
| 205 const gfx::Size dst_size(input.width() * percents[p] / 100, | 225 const gfx::Size dst_size(input.width() * percents[p] / 100, |
| 206 input.height() * percents[p] / 100); | 226 input.height() * percents[p] / 100); |
| 207 WebGLId dst_texture = helper_->CopyAndScaleTexture( | 227 uint32_t dst_texture = helper_->CopyAndScaleTexture( |
| 208 src_texture, src_size, dst_size, false, kQualities[q]); | 228 src_texture, src_size, dst_size, false, kQualities[q]); |
| 209 | 229 |
| 210 SkBitmap output_pixels; | 230 SkBitmap output_pixels; |
| 211 output_pixels.allocN32Pixels(dst_size.width(), dst_size.height()); | 231 output_pixels.allocN32Pixels(dst_size.width(), dst_size.height()); |
| 212 | 232 |
| 213 helper_->ReadbackTextureSync( | 233 helper_->ReadbackTextureSync( |
| 214 dst_texture, gfx::Rect(0, 0, dst_size.width(), dst_size.height()), | 234 dst_texture, gfx::Rect(0, 0, dst_size.width(), dst_size.height()), |
| 215 static_cast<unsigned char*>(output_pixels.getPixels()), | 235 static_cast<unsigned char*>(output_pixels.getPixels()), |
| 216 kN32_SkColorType); | 236 kN32_SkColorType); |
| 217 context_->deleteTexture(dst_texture); | 237 gl_->DeleteTextures(1, &dst_texture); |
| 218 std::string filename = base::StringPrintf("testoutput_%s_%d.ppm", | 238 std::string filename = base::StringPrintf("testoutput_%s_%d.ppm", |
| 219 kQualityNames[q], percents[p]); | 239 kQualityNames[q], percents[p]); |
| 220 VLOG(0) << "Writing " << filename; | 240 VLOG(0) << "Writing " << filename; |
| 221 SaveToFile(&output_pixels, base::FilePath::FromUTF8Unsafe(filename)); | 241 SaveToFile(&output_pixels, base::FilePath::FromUTF8Unsafe(filename)); |
| 222 } | 242 } |
| 223 } | 243 } |
| 224 context_->deleteTexture(src_texture); | 244 gl_->DeleteTextures(1, &src_texture); |
| 225 context_->deleteFramebuffer(framebuffer); | 245 gl_->DeleteFramebuffers(1, &framebuffer); |
| 226 } | 246 } |
| 227 | 247 |
| 228 } // namespace | 248 } // namespace |
| OLD | NEW |