1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/core/SkBitmap.h"
9 #include "include/core/SkMatrix.h"
10 #include "include/private/SkTemplates.h"
11 #include "src/core/SkArenaAlloc.h"
12 #include "src/core/SkBitmapCache.h"
13 #include "src/core/SkMipmap.h"
14 #include "src/core/SkMipmapAccessor.h"
15 #include "src/image/SkImage_Base.h"
16
17 // Try to load from the base image, or from the cache
try_load_mips(const SkImage_Base * image)18 static sk_sp<const SkMipmap> try_load_mips(const SkImage_Base* image) {
19 sk_sp<const SkMipmap> mips = image->refMips();
20 if (!mips) {
21 mips.reset(SkMipmapCache::FindAndRef(SkBitmapCacheDesc::Make(image)));
22 }
23 if (!mips) {
24 mips.reset(SkMipmapCache::AddAndRef(image));
25 }
26 return mips;
27 }
28
SkMipmapAccessor(const SkImage_Base * image,const SkMatrix & inv,SkMipmapMode requestedMode)29 SkMipmapAccessor::SkMipmapAccessor(const SkImage_Base* image, const SkMatrix& inv,
30 SkMipmapMode requestedMode) {
31 fResolvedMode = requestedMode;
32 fLowerWeight = 0;
33
34 auto load_upper_from_base = [&]() {
35 // only do this once
36 if (fBaseStorage.getPixels() == nullptr) {
37 (void)image->getROPixels(nullptr, &fBaseStorage);
38 fUpper.reset(fBaseStorage.info(), fBaseStorage.getPixels(), fBaseStorage.rowBytes());
39 }
40 };
41
42 float level = 0;
43 if (requestedMode != SkMipmapMode::kNone) {
44 SkSize scale;
45 if (!inv.decomposeScale(&scale, nullptr)) {
46 fResolvedMode = SkMipmapMode::kNone;
47 } else {
48 level = SkMipmap::ComputeLevel({1/scale.width(), 1/scale.height()});
49 if (level <= 0) {
50 fResolvedMode = SkMipmapMode::kNone;
51 level = 0;
52 }
53 }
54 }
55
56 auto post_scale = [image, inv](const SkPixmap& pm) {
57 return SkMatrix::Scale(SkIntToScalar(pm.width()) / image->width(),
58 SkIntToScalar(pm.height()) / image->height()) * inv;
59 };
60
61 int levelNum = sk_float_floor2int(level);
62 float lowerWeight = level - levelNum; // fract(level)
63 SkASSERT(levelNum >= 0);
64
65 if (levelNum == 0) {
66 load_upper_from_base();
67 }
68 // load fCurrMip if needed
69 if (levelNum > 0 || (fResolvedMode == SkMipmapMode::kLinear && lowerWeight > 0)) {
70 fCurrMip = try_load_mips(image);
71 if (!fCurrMip) {
72 load_upper_from_base();
73 fResolvedMode = SkMipmapMode::kNone;
74 } else {
75 SkMipmap::Level levelRec;
76
77 SkASSERT(fResolvedMode != SkMipmapMode::kNone);
78 if (levelNum > 0) {
79 if (fCurrMip->getLevel(levelNum - 1, &levelRec)) {
80 fUpper = levelRec.fPixmap;
81 } else {
82 load_upper_from_base();
83 fResolvedMode = SkMipmapMode::kNone;
84 }
85 }
86
87 if (fResolvedMode == SkMipmapMode::kLinear) {
88 if (fCurrMip->getLevel(levelNum, &levelRec)) {
89 fLower = levelRec.fPixmap;
90 fLowerWeight = lowerWeight;
91 fLowerInv = post_scale(fLower);
92 } else {
93 fResolvedMode = SkMipmapMode::kNearest;
94 }
95 }
96 }
97 }
98 fUpperInv = post_scale(fUpper);
99 }
100
Make(SkArenaAlloc * alloc,const SkImage * image,const SkMatrix & inv,SkMipmapMode mipmap)101 SkMipmapAccessor* SkMipmapAccessor::Make(SkArenaAlloc* alloc, const SkImage* image,
102 const SkMatrix& inv, SkMipmapMode mipmap) {
103 auto* access = alloc->make<SkMipmapAccessor>(as_IB(image), inv, mipmap);
104 // return null if we failed to get the level (so the caller won't try to use it)
105 return access->fUpper.addr() ? access : nullptr;
106 }
107