Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "cc/output/color_lut_cache.h" | 5 #include "cc/output/color_lut_cache.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "gpu/command_buffer/client/gles2_interface.h" | 11 #include "gpu/command_buffer/client/gles2_interface.h" |
| 12 #include "third_party/khronos/GLES2/gl2ext.h" | |
| 12 #include "ui/gfx/color_transform.h" | 13 #include "ui/gfx/color_transform.h" |
| 13 | 14 |
| 14 // After a LUT has not been used for this many frames, we release it. | 15 // After a LUT has not been used for this many frames, we release it. |
| 15 const uint32_t kMaxFramesUnused = 10; | 16 const uint32_t kMaxFramesUnused = 10; |
| 16 | 17 |
| 17 ColorLUTCache::ColorLUTCache(gpu::gles2::GLES2Interface* gl) | 18 ColorLUTCache::ColorLUTCache(gpu::gles2::GLES2Interface* gl, |
| 18 : lut_cache_(0), gl_(gl) {} | 19 bool texture_half_float_linear) |
| 20 : lut_cache_(0), | |
| 21 gl_(gl), | |
| 22 texture_half_float_linear_(texture_half_float_linear) {} | |
| 19 | 23 |
| 20 ColorLUTCache::~ColorLUTCache() { | 24 ColorLUTCache::~ColorLUTCache() { |
| 21 GLuint textures[10]; | 25 GLuint textures[10]; |
| 22 size_t n = 0; | 26 size_t n = 0; |
| 23 for (const auto& cache_entry : lut_cache_) { | 27 for (const auto& cache_entry : lut_cache_) { |
| 24 textures[n++] = cache_entry.second.texture; | 28 textures[n++] = cache_entry.second.lut.texture; |
| 25 if (n == arraysize(textures)) { | 29 if (n == arraysize(textures)) { |
| 26 gl_->DeleteTextures(n, textures); | 30 gl_->DeleteTextures(n, textures); |
| 27 n = 0; | 31 n = 0; |
| 28 } | 32 } |
| 29 } | 33 } |
| 30 if (n) | 34 if (n) |
| 31 gl_->DeleteTextures(n, textures); | 35 gl_->DeleteTextures(n, textures); |
| 32 } | 36 } |
| 33 | 37 |
| 34 namespace { | |
| 35 | |
| 36 unsigned char FloatToLUT(float f) { | |
| 37 return std::min<int>(255, std::max<int>(0, floorf(f * 255.0f + 0.5f))); | |
| 38 } | |
| 39 }; | |
| 40 | |
| 41 unsigned int ColorLUTCache::MakeLUT(const gfx::ColorSpace& from, | 38 unsigned int ColorLUTCache::MakeLUT(const gfx::ColorSpace& from, |
| 42 gfx::ColorSpace to, | 39 gfx::ColorSpace to, |
| 43 int lut_samples) { | 40 int lut_samples, |
| 41 int data_type) { | |
|
ccameron
2017/01/23 18:30:25
Small nit: should be "GLenum type" instead of "int
hubbe
2017/01/24 18:32:17
Acknowledged.
| |
| 44 if (to == gfx::ColorSpace()) { | 42 if (to == gfx::ColorSpace()) { |
| 45 to = gfx::ColorSpace::CreateSRGB(); | 43 to = gfx::ColorSpace::CreateSRGB(); |
| 46 } | 44 } |
| 47 std::unique_ptr<gfx::ColorTransform> transform( | 45 std::unique_ptr<gfx::ColorTransform> transform( |
| 48 gfx::ColorTransform::NewColorTransform( | 46 gfx::ColorTransform::NewColorTransform( |
| 49 from, to, gfx::ColorTransform::Intent::INTENT_PERCEPTUAL)); | 47 from, to, gfx::ColorTransform::Intent::INTENT_PERCEPTUAL)); |
| 50 | 48 |
| 51 int lut_entries = lut_samples * lut_samples * lut_samples; | 49 int lut_entries = lut_samples * lut_samples * lut_samples; |
| 52 float inverse = 1.0f / (lut_samples - 1); | 50 float inverse = 1.0f / (lut_samples - 1); |
| 53 std::vector<unsigned char> lut(lut_entries * 4); | 51 std::vector<float> lut(lut_entries * 4); |
| 54 std::vector<gfx::ColorTransform::TriStim> samples(lut_samples); | 52 std::vector<gfx::ColorTransform::TriStim> samples(lut_samples); |
| 55 unsigned char* lutp = lut.data(); | 53 float* lutp = lut.data(); |
| 56 for (int v = 0; v < lut_samples; v++) { | 54 for (int v = 0; v < lut_samples; v++) { |
| 57 for (int u = 0; u < lut_samples; u++) { | 55 for (int u = 0; u < lut_samples; u++) { |
| 58 for (int y = 0; y < lut_samples; y++) { | 56 for (int y = 0; y < lut_samples; y++) { |
| 59 samples[y].set_x(y * inverse); | 57 samples[y].set_x(y * inverse); |
| 60 samples[y].set_y(u * inverse); | 58 samples[y].set_y(u * inverse); |
| 61 samples[y].set_z(v * inverse); | 59 samples[y].set_z(v * inverse); |
| 62 } | 60 } |
| 63 transform->transform(samples.data(), samples.size()); | 61 transform->transform(samples.data(), samples.size()); |
| 64 for (int y = 0; y < lut_samples; y++) { | 62 for (int y = 0; y < lut_samples; y++) { |
| 65 *(lutp++) = FloatToLUT(samples[y].x()); | 63 *(lutp++) = samples[y].x(); |
| 66 *(lutp++) = FloatToLUT(samples[y].y()); | 64 *(lutp++) = samples[y].y(); |
| 67 *(lutp++) = FloatToLUT(samples[y].z()); | 65 *(lutp++) = samples[y].z(); |
| 68 *(lutp++) = 255; // alpha | 66 *(lutp++) = 1.0; // alpha |
| 69 } | 67 } |
| 70 } | 68 } |
| 71 } | 69 } |
| 72 | 70 |
| 73 unsigned int lut_texture; | 71 unsigned int lut_texture; |
| 74 gl_->GenTextures(1, &lut_texture); | 72 gl_->GenTextures(1, &lut_texture); |
| 75 gl_->BindTexture(GL_TEXTURE_2D, lut_texture); | 73 gl_->BindTexture(GL_TEXTURE_2D, lut_texture); |
| 76 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 74 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 77 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 75 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 78 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 76 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 79 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 77 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 80 gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, lut_samples, | 78 gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, lut_samples, |
| 81 lut_samples * lut_samples, 0, GL_RGBA, GL_UNSIGNED_BYTE, | 79 lut_samples * lut_samples, 0, GL_RGBA, data_type, nullptr); |
|
ccameron
2017/01/23 18:30:25
So I'm not 100% sure what exactly triggers use of
hubbe
2017/01/24 18:32:17
Seems clear enough to me that it's the data type t
| |
| 82 lut.data()); | 80 gl_->TexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, lut_samples, |
| 81 lut_samples * lut_samples, GL_RGBA, GL_FLOAT, lut.data()); | |
| 83 return lut_texture; | 82 return lut_texture; |
| 84 } | 83 } |
| 85 | 84 |
| 86 unsigned int ColorLUTCache::GetLUT(const gfx::ColorSpace& from, | 85 ColorLUTCache::LUT ColorLUTCache::GetLUT(const gfx::ColorSpace& from, |
| 87 const gfx::ColorSpace& to, | 86 const gfx::ColorSpace& to) { |
| 88 int lut_samples) { | 87 CacheKey key(from, to); |
| 89 CacheKey key(from, std::make_pair(to, lut_samples)); | |
| 90 auto iter = lut_cache_.Get(key); | 88 auto iter = lut_cache_.Get(key); |
| 91 if (iter != lut_cache_.end()) { | 89 if (iter != lut_cache_.end()) { |
| 92 iter->second.last_used_frame = current_frame_; | 90 iter->second.last_used_frame = current_frame_; |
| 93 return iter->second.texture; | 91 return iter->second.lut; |
| 94 } | 92 } |
| 95 | 93 |
| 96 unsigned int lut = MakeLUT(from, to, lut_samples); | 94 LUT lut; |
| 95 // If input is HDR, and the output is scRGB, we're going to need | |
| 96 // to produce values outside of 0-1, so we'll need to make a half-float | |
| 97 // LUT. Also, we'll need to build a larger lut to maintain accuracy. | |
| 98 // All LUT sizes should be odd a some transforms hav a knee at 0.5. | |
| 99 if (to == gfx::ColorSpace::CreateSCRGBLinear() && from.IsHDR() && | |
| 100 texture_half_float_linear_) { | |
| 101 lut.size = 37; | |
| 102 lut.texture = MakeLUT(from, to, lut.size, GL_HALF_FLOAT_OES); | |
| 103 } else { | |
| 104 lut.size = 17; | |
| 105 lut.texture = MakeLUT(from, to, lut.size, GL_UNSIGNED_BYTE); | |
| 106 } | |
| 97 lut_cache_.Put(key, CacheVal(lut, current_frame_)); | 107 lut_cache_.Put(key, CacheVal(lut, current_frame_)); |
| 98 return lut; | 108 return lut; |
| 99 } | 109 } |
| 100 | 110 |
| 101 void ColorLUTCache::Swap() { | 111 void ColorLUTCache::Swap() { |
| 102 current_frame_++; | 112 current_frame_++; |
| 103 while (!lut_cache_.empty() && | 113 while (!lut_cache_.empty() && |
| 104 current_frame_ - lut_cache_.rbegin()->second.last_used_frame > | 114 current_frame_ - lut_cache_.rbegin()->second.last_used_frame > |
| 105 kMaxFramesUnused) { | 115 kMaxFramesUnused) { |
| 106 gl_->DeleteTextures(1, &lut_cache_.rbegin()->second.texture); | 116 gl_->DeleteTextures(1, &lut_cache_.rbegin()->second.lut.texture); |
| 107 lut_cache_.ShrinkToSize(lut_cache_.size() - 1); | 117 lut_cache_.ShrinkToSize(lut_cache_.size() - 1); |
| 108 } | 118 } |
| 109 } | 119 } |
| OLD | NEW |