Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Member Functions | List of all members
SkMipmapAccessor Class Reference

#include <SkMipmapAccessor.h>

Inheritance diagram for SkMipmapAccessor:
SkNoncopyable

Public Member Functions

std::pair< SkPixmap, SkMatrixlevel () const
 
std::pair< SkPixmap, SkMatrixlowerLevel () const
 
float lowerWeight () const
 
 SkMipmapAccessor (const SkImage_Base *, const SkMatrix &inv, SkMipmapMode requestedMode)
 

Static Public Member Functions

static SkMipmapAccessorMake (SkArenaAlloc *, const SkImage *, const SkMatrix &inv, SkMipmapMode)
 

Detailed Description

Definition at line 26 of file SkMipmapAccessor.h.

Constructor & Destructor Documentation

◆ SkMipmapAccessor()

SkMipmapAccessor::SkMipmapAccessor ( const SkImage_Base image,
const SkMatrix inv,
SkMipmapMode  requestedMode 
)

Definition at line 35 of file SkMipmapAccessor.cpp.

36 {
37 SkMipmapMode resolvedMode = requestedMode;
38 fLowerWeight = 0;
39
40 auto load_upper_from_base = [&]() {
41 // only do this once
42 if (fBaseStorage.getPixels() == nullptr) {
43 auto dContext = as_IB(image)->directContext();
44 (void)image->getROPixels(dContext, &fBaseStorage);
45 fUpper.reset(fBaseStorage.info(), fBaseStorage.getPixels(), fBaseStorage.rowBytes());
46 }
47 };
48
49 float level = 0;
50 if (requestedMode != SkMipmapMode::kNone) {
52 if (!inv.decomposeScale(&scale, nullptr)) {
53 resolvedMode = SkMipmapMode::kNone;
54 } else {
55 level = SkMipmap::ComputeLevel({1/scale.width(), 1/scale.height()});
56 if (level <= 0) {
57 resolvedMode = SkMipmapMode::kNone;
58 level = 0;
59 }
60 }
61 }
62
63 auto scale = [image](const SkPixmap& pm) {
64 return SkMatrix::Scale(SkIntToScalar(pm.width()) / image->width(),
65 SkIntToScalar(pm.height()) / image->height());
66 };
67
68 // Nearest mode uses this level, so we round to pick the nearest. In linear mode we use this
69 // level as the lower of the two to interpolate between, so we take the floor.
70 int levelNum = resolvedMode == SkMipmapMode::kNearest ? sk_float_round2int(level)
72 float lowerWeight = level - levelNum; // fract(level)
73 SkASSERT(levelNum >= 0);
74
75 if (levelNum == 0) {
76 load_upper_from_base();
77 }
78 // load fCurrMip if needed
79 if (levelNum > 0 || (resolvedMode == SkMipmapMode::kLinear && lowerWeight > 0)) {
80 fCurrMip = try_load_mips(image);
81 if (!fCurrMip) {
82 load_upper_from_base();
83 resolvedMode = SkMipmapMode::kNone;
84 } else {
85 SkMipmap::Level levelRec;
86
87 SkASSERT(resolvedMode != SkMipmapMode::kNone);
88 if (levelNum > 0) {
89 if (fCurrMip->getLevel(levelNum - 1, &levelRec)) {
90 fUpper = levelRec.fPixmap;
91 } else {
92 load_upper_from_base();
93 resolvedMode = SkMipmapMode::kNone;
94 }
95 }
96
97 if (resolvedMode == SkMipmapMode::kLinear) {
98 if (fCurrMip->getLevel(levelNum, &levelRec)) {
99 fLower = levelRec.fPixmap;
100 fLowerWeight = lowerWeight;
101 fLowerInv = scale(fLower);
102 } else {
103 resolvedMode = SkMipmapMode::kNearest;
104 }
105 }
106 }
107 }
108 fUpperInv = scale(fUpper);
109}
static SkM44 inv(const SkM44 &m)
Definition: 3d.cpp:26
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define sk_float_round2int(x)
#define sk_float_floor2int(x)
static SkImage_Base * as_IB(SkImage *image)
Definition: SkImage_Base.h:201
static sk_sp< const SkMipmap > try_load_mips(const SkImage_Base *image)
SkMipmapMode
#define SkIntToScalar(x)
Definition: SkScalar.h:57
size_t rowBytes() const
Definition: SkBitmap.h:238
void * getPixels() const
Definition: SkBitmap.h:283
const SkImageInfo & info() const
Definition: SkBitmap.h:139
virtual GrDirectContext * directContext() const
Definition: SkImage_Base.h:115
int width() const
Definition: SkImage.h:285
int height() const
Definition: SkImage.h:291
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition: SkMatrix.h:75
float lowerWeight() const
std::pair< SkPixmap, SkMatrix > level() const
static float ComputeLevel(SkSize scaleSize)
Definition: SkMipmap.cpp:195
bool getLevel(int index, Level *) const
Definition: SkMipmap.cpp:280
void reset()
Definition: SkPixmap.cpp:32
sk_sp< const SkImage > image
Definition: SkRecords.h:269
const Scalar scale
SkPixmap fPixmap
Definition: SkMipmap.h:71
Definition: SkSize.h:52

Member Function Documentation

◆ level()

std::pair< SkPixmap, SkMatrix > SkMipmapAccessor::level ( ) const
inline

Definition at line 31 of file SkMipmapAccessor.h.

31 {
32 SkASSERT(fUpper.addr() != nullptr);
33 return std::make_pair(fUpper, fUpperInv);
34 }
const void * addr() const
Definition: SkPixmap.h:153

◆ lowerLevel()

std::pair< SkPixmap, SkMatrix > SkMipmapAccessor::lowerLevel ( ) const
inline

Definition at line 36 of file SkMipmapAccessor.h.

36 {
37 SkASSERT(fLower.addr() != nullptr);
38 return std::make_pair(fLower, fLowerInv);
39 }

◆ lowerWeight()

float SkMipmapAccessor::lowerWeight ( ) const
inline

Definition at line 42 of file SkMipmapAccessor.h.

42{ return fLowerWeight; }

◆ Make()

SkMipmapAccessor * SkMipmapAccessor::Make ( SkArenaAlloc alloc,
const SkImage image,
const SkMatrix inv,
SkMipmapMode  mipmap 
)
static

Definition at line 111 of file SkMipmapAccessor.cpp.

112 {
113 auto* access = alloc->make<SkMipmapAccessor>(as_IB(image), inv, mipmap);
114 // return null if we failed to get the level (so the caller won't try to use it)
115 return access->fUpper.addr() ? access : nullptr;
116}
auto make(Ctor &&ctor) -> decltype(ctor(nullptr))
Definition: SkArenaAlloc.h:120

The documentation for this class was generated from the following files: