encoded_view.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2017 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. #include "components/zucchini/encoded_view.h"
  5. #include <algorithm>
  6. #include <utility>
  7. #include "base/check_op.h"
  8. namespace zucchini {
  9. EncodedView::EncodedView(const ImageIndex& image_index)
  10. : image_index_(image_index), pool_infos_(image_index.PoolCount()) {}
  11. EncodedView::~EncodedView() = default;
  12. EncodedView::value_type EncodedView::Projection(offset_t location) const {
  13. DCHECK_LT(location, image_index_.size());
  14. // Find out what lies at |location|.
  15. TypeTag type = image_index_.LookupType(location);
  16. // |location| points into raw data.
  17. if (type == kNoTypeTag) {
  18. // The projection is the identity function on raw content.
  19. return image_index_.GetRawValue(location);
  20. }
  21. // |location| points into a Reference.
  22. const ReferenceSet& ref_set = image_index_.refs(type);
  23. Reference ref = ref_set.at(location);
  24. DCHECK_GE(location, ref.location);
  25. DCHECK_LT(location, ref.location + ref_set.width());
  26. // |location| is not the first byte of the reference.
  27. if (location != ref.location) {
  28. // Trailing bytes of a reference are all projected to the same value.
  29. return kReferencePaddingProjection;
  30. }
  31. PoolTag pool_tag = ref_set.pool_tag();
  32. const auto& target_pool = ref_set.target_pool();
  33. // Targets with an associated Label will use its Label index in projection.
  34. DCHECK_EQ(target_pool.size(), pool_infos_[pool_tag.value()].labels.size());
  35. uint32_t label = pool_infos_[pool_tag.value()]
  36. .labels[target_pool.KeyForOffset(ref.target)];
  37. // Projection is done on (|target|, |type|), shifted by
  38. // kBaseReferenceProjection to avoid collisions with raw content.
  39. value_type projection = label;
  40. projection *= image_index_.TypeCount();
  41. projection += type.value();
  42. return projection + kBaseReferenceProjection;
  43. }
  44. size_t EncodedView::Cardinality() const {
  45. size_t max_width = 0;
  46. for (const auto& pool_info : pool_infos_)
  47. max_width = std::max(max_width, pool_info.bound);
  48. return max_width * image_index_.TypeCount() + kBaseReferenceProjection;
  49. }
  50. void EncodedView::SetLabels(PoolTag pool,
  51. std::vector<uint32_t>&& labels,
  52. size_t bound) {
  53. DCHECK_EQ(labels.size(), image_index_.pool(pool).size());
  54. DCHECK(labels.empty() || *max_element(labels.begin(), labels.end()) < bound);
  55. pool_infos_[pool.value()].labels = std::move(labels);
  56. pool_infos_[pool.value()].bound = bound;
  57. }
  58. EncodedView::PoolInfo::PoolInfo() = default;
  59. EncodedView::PoolInfo::PoolInfo(PoolInfo&&) = default;
  60. EncodedView::PoolInfo::~PoolInfo() = default;
  61. } // namespace zucchini