OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "cc/output/color_lut_cache.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 #include <vector> |
| 9 |
| 10 #include "gpu/command_buffer/client/gles2_interface.h" |
| 11 #include "ui/gfx/color_transform.h" |
| 12 |
| 13 // After a LUT has not been used for this many frames, we release it. |
| 14 const uint32_t kMaxFramesUnused = 10; |
| 15 |
| 16 ColorLUTCache::ColorLUTCache(gpu::gles2::GLES2Interface* gl) |
| 17 : lut_cache_(0), gl_(gl) {} |
| 18 |
| 19 ColorLUTCache::~ColorLUTCache() { |
| 20 GLuint textures[10]; |
| 21 size_t n = 0; |
| 22 for (const auto& cache_entry : lut_cache_) { |
| 23 textures[n++] = cache_entry.second.first; |
| 24 if (n == arraysize(textures)) { |
| 25 gl_->DeleteTextures(n, textures); |
| 26 n = 0; |
| 27 } |
| 28 } |
| 29 if (n) |
| 30 gl_->DeleteTextures(n, textures); |
| 31 } |
| 32 |
| 33 namespace { |
| 34 |
| 35 unsigned char FloatToLUT(float f) { |
| 36 return std::min<int>(255, std::max<int>(0, floorf(f * 255.0f + 0.5f))); |
| 37 } |
| 38 }; |
| 39 |
| 40 unsigned int ColorLUTCache::MakeLUT(const gfx::ColorSpace& from, |
| 41 gfx::ColorSpace to, |
| 42 int lut_samples) { |
| 43 if (to == gfx::ColorSpace()) { |
| 44 to = gfx::ColorSpace::CreateSRGB(); |
| 45 } |
| 46 std::unique_ptr<gfx::ColorTransform> transform( |
| 47 gfx::ColorTransform::NewColorTransform( |
| 48 from, to, gfx::ColorTransform::Intent::PERCEPTUAL)); |
| 49 |
| 50 int lut_entries = lut_samples * lut_samples * lut_samples; |
| 51 float inverse = 1.0f / (lut_samples - 1); |
| 52 std::vector<unsigned char> lut(lut_entries * 4); |
| 53 std::vector<gfx::ColorTransform::TriStim> samples(lut_samples * 3); |
| 54 unsigned char* lutp = lut.data(); |
| 55 for (int y = 0; y < lut_samples; y++) { |
| 56 for (int v = 0; v < lut_samples; v++) { |
| 57 for (int u = 0; u < lut_samples; u++) { |
| 58 samples[u].set_x(y * inverse); |
| 59 samples[u].set_y(u * inverse); |
| 60 samples[u].set_z(v * inverse); |
| 61 } |
| 62 transform->transform(samples.data(), samples.size()); |
| 63 for (int u = 0; u < lut_samples; u++) { |
| 64 *(lutp++) = FloatToLUT(samples[u].x()); |
| 65 *(lutp++) = FloatToLUT(samples[u].y()); |
| 66 *(lutp++) = FloatToLUT(samples[u].z()); |
| 67 *(lutp++) = 255; // alpha |
| 68 } |
| 69 } |
| 70 } |
| 71 |
| 72 unsigned int lut_texture; |
| 73 gl_->GenTextures(1, &lut_texture); |
| 74 gl_->BindTexture(GL_TEXTURE_2D, lut_texture); |
| 75 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 76 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 77 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 78 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 79 gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, lut_samples, |
| 80 lut_samples * lut_samples, 0, GL_RGBA, GL_UNSIGNED_BYTE, |
| 81 lut.data()); |
| 82 return lut_texture; |
| 83 } |
| 84 |
| 85 unsigned int ColorLUTCache::GetLUT(const gfx::ColorSpace& from, |
| 86 const gfx::ColorSpace& to, |
| 87 int lut_samples) { |
| 88 CacheKey key(from, std::make_pair(to, lut_samples)); |
| 89 auto iter = lut_cache_.Get(key); |
| 90 if (iter != lut_cache_.end()) { |
| 91 iter->second.second = current_frame_; |
| 92 return iter->second.first; |
| 93 } |
| 94 |
| 95 unsigned int lut = MakeLUT(from, to, lut_samples); |
| 96 lut_cache_.Put(key, std::make_pair(lut, current_frame_)); |
| 97 return lut; |
| 98 } |
| 99 |
| 100 void ColorLUTCache::Swap() { |
| 101 current_frame_++; |
| 102 while (!lut_cache_.empty() && |
| 103 current_frame_ - lut_cache_.rbegin()->second.first > |
| 104 kMaxFramesUnused) { |
| 105 gl_->DeleteTextures(1, &lut_cache_.rbegin()->second.first); |
| 106 lut_cache_.ShrinkToSize(lut_cache_.size() - 1); |
| 107 } |
| 108 } |
OLD | NEW |