OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkMipMap_DEFINED | 8 #ifndef SkMipMap_DEFINED |
9 #define SkMipMap_DEFINED | 9 #define SkMipMap_DEFINED |
10 | 10 |
11 #include "SkCachedData.h" | 11 #include "SkCachedData.h" |
12 #include "SkPixmap.h" | 12 #include "SkPixmap.h" |
13 #include "SkScalar.h" | 13 #include "SkScalar.h" |
14 #include "SkSize.h" | 14 #include "SkSize.h" |
15 | 15 |
16 class SkBitmap; | 16 class SkBitmap; |
17 class SkDiscardableMemory; | 17 class SkDiscardableMemory; |
18 | 18 |
19 typedef SkDiscardableMemory* (*SkDiscardableFactoryProc)(size_t bytes); | 19 typedef SkDiscardableMemory* (*SkDiscardableFactoryProc)(size_t bytes); |
20 | 20 |
| 21 /* |
| 22 * SkMipMap will generate mipmap levels when given a base mipmap level image. |
| 23 * |
| 24 * Any function which deals with mipmap levels indices will start with index 0 |
| 25 * being the first mipmap level which was generated. Said another way, it does |
| 26 * not include the base level in its range. |
| 27 */ |
21 class SkMipMap : public SkCachedData { | 28 class SkMipMap : public SkCachedData { |
22 public: | 29 public: |
23 static SkMipMap* Build(const SkPixmap& src, SkDiscardableFactoryProc); | 30 static SkMipMap* Build(const SkPixmap& src, SkDiscardableFactoryProc); |
24 static SkMipMap* Build(const SkBitmap& src, SkDiscardableFactoryProc); | 31 static SkMipMap* Build(const SkBitmap& src, SkDiscardableFactoryProc); |
25 | 32 |
26 // Determines how many levels a SkMipMap will have without creating that mip
map. | 33 // Determines how many levels a SkMipMap will have without creating that mip
map. |
| 34 // This does not include the base mipmap level that the user provided when |
| 35 // creating the SkMipMap. |
27 static int ComputeLevelCount(int baseWidth, int baseHeight); | 36 static int ComputeLevelCount(int baseWidth, int baseHeight); |
28 | 37 |
29 // Determines the size of a given mipmap level. | 38 // Determines the size of a given mipmap level. |
| 39 // |level| is an index into the generated mipmap levels. It does not include |
| 40 // the base level. So index 0 represents mipmap level 1. |
30 static SkISize ComputeLevelSize(int baseWidth, int baseHeight, int level); | 41 static SkISize ComputeLevelSize(int baseWidth, int baseHeight, int level); |
31 | 42 |
32 struct Level { | 43 struct Level { |
33 SkPixmap fPixmap; | 44 SkPixmap fPixmap; |
34 SkSize fScale; // < 1.0 | 45 SkSize fScale; // < 1.0 |
35 }; | 46 }; |
36 | 47 |
37 bool extractLevel(const SkSize& scale, Level*) const; | 48 bool extractLevel(const SkSize& scale, Level*) const; |
| 49 |
| 50 // countLevels returns the number of mipmap levels generated (which does not |
| 51 // include the base mipmap level). |
38 int countLevels() const; | 52 int countLevels() const; |
| 53 |
| 54 // |index| is an index into the generated mipmap levels. It does not include |
| 55 // the base level. So index 0 represents mipmap level 1. |
39 bool getLevel(int index, Level*) const; | 56 bool getLevel(int index, Level*) const; |
40 | 57 |
41 protected: | 58 protected: |
42 void onDataChange(void* oldData, void* newData) override { | 59 void onDataChange(void* oldData, void* newData) override { |
43 fLevels = (Level*)newData; // could be nullptr | 60 fLevels = (Level*)newData; // could be nullptr |
44 } | 61 } |
45 | 62 |
46 private: | 63 private: |
47 Level* fLevels; | 64 Level* fLevels; |
48 int fCount; | 65 int fCount; |
49 | 66 |
50 // we take ownership of levels, and will free it with sk_free() | 67 // we take ownership of levels, and will free it with sk_free() |
51 SkMipMap(void* malloc, size_t size) : INHERITED(malloc, size) {} | 68 SkMipMap(void* malloc, size_t size) : INHERITED(malloc, size) {} |
52 SkMipMap(size_t size, SkDiscardableMemory* dm) : INHERITED(size, dm) {} | 69 SkMipMap(size_t size, SkDiscardableMemory* dm) : INHERITED(size, dm) {} |
53 | 70 |
54 static size_t AllocLevelsSize(int levelCount, size_t pixelSize); | 71 static size_t AllocLevelsSize(int levelCount, size_t pixelSize); |
55 | 72 |
56 typedef SkCachedData INHERITED; | 73 typedef SkCachedData INHERITED; |
57 }; | 74 }; |
58 | 75 |
59 #endif | 76 #endif |
OLD | NEW |