1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97

cc / tiles / mipmap_util.cc [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/tiles/mipmap_util.h"

#include <algorithm>
#include <limits>

#include "base/numerics/safe_math.h"

namespace cc {
namespace {
// Calculates the size of |axis_base_size| at the given |mip_level|. Note that
// the calculation here rounds up for consistency with size calculations in the
// JPEG decoder. This allows us to decode images to the mip size directly.
int ScaleAxisToMipLevel(int axis_base_size, int mip_level) {
  DCHECK_GE(mip_level, 0);
  DCHECK_LT(mip_level, 32);

  if (mip_level == 0)
    return axis_base_size;

  // Increment the size by (2^mip_level - 1) so we round on when dividing it
  // below.
  base::CheckedNumeric<int> base_size = axis_base_size;
  base_size += (1u << mip_level) - 1u;
  axis_base_size = base_size.ValueOrDefault(std::numeric_limits<int>::max());
  return std::max(1, axis_base_size >> mip_level);
}

}  // namespace

int MipMapUtil::GetLevelForSize(const gfx::Size& src_size,
                                const gfx::Size& target_size) {
  int src_height = src_size.height();
  int src_width = src_size.width();
  int target_height = target_size.height();
  int target_width = target_size.width();
  DCHECK_GT(target_height, 0);
  DCHECK_GT(target_width, 0);
  DCHECK_GT(src_width, 0);
  DCHECK_GT(src_height, 0);

  int next_mip_height = src_height;
  int next_mip_width = src_width;
  for (int current_mip_level = 0;; current_mip_level++) {
    int mip_height = next_mip_height;
    int mip_width = next_mip_width;

    next_mip_height = ScaleAxisToMipLevel(src_height, current_mip_level + 1);
    next_mip_width = ScaleAxisToMipLevel(src_width, current_mip_level + 1);

    // Check if an axis on the next mip level would be smaller than the target.
    // If so, use the current mip level.
    // This effectively always uses the larger image and always scales down.
    if (next_mip_height < target_height || next_mip_width < target_width) {
      return current_mip_level;
    }

    if (mip_height == 1 && mip_width == 1) {
      // We have reached the final mip level
      return current_mip_level;
    }
  }
}

SkSize MipMapUtil::GetScaleAdjustmentForLevel(const gfx::Size& src_size,
                                              int mip_level) {
  DCHECK_GT(src_size.width(), 0);
  DCHECK_GT(src_size.height(), 0);
  DCHECK_GE(mip_level, 0);

  gfx::Size target_size = GetSizeForLevel(src_size, mip_level);

  return SkSize::Make(
      static_cast<float>(target_size.width()) / src_size.width(),
      static_cast<float>(target_size.height()) / src_size.height());
}

gfx::Size MipMapUtil::GetSizeForLevel(const gfx::Size& src_size,
                                      int mip_level) {
  DCHECK_GT(src_size.width(), 0);
  DCHECK_GT(src_size.height(), 0);
  DCHECK_GE(mip_level, 0);

  return gfx::Size(ScaleAxisToMipLevel(src_size.width(), mip_level),
                   ScaleAxisToMipLevel(src_size.height(), mip_level));
}

SkSize MipMapUtil::GetScaleAdjustmentForSize(const gfx::Size& src_size,
                                             const gfx::Size& target_size) {
  int target_mip_level = GetLevelForSize(src_size, target_size);
  return GetScaleAdjustmentForLevel(src_size, target_mip_level);
}

}  // namespace cc