reed@google.com | dc6c8ba | 2013-07-18 21:14:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 8 | #ifndef SkMipMap_DEFINED |
| 9 | #define SkMipMap_DEFINED |
| 10 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 11 | #include "SkCachedData.h" |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 12 | #include "SkPixmap.h" |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 13 | #include "SkScalar.h" |
fmalita | 921d7ac | 2016-01-22 11:45:39 -0800 | [diff] [blame] | 14 | #include "SkSize.h" |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 15 | |
| 16 | class SkBitmap; |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 17 | class SkDiscardableMemory; |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 18 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 19 | typedef SkDiscardableMemory* (*SkDiscardableFactoryProc)(size_t bytes); |
| 20 | |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame^] | 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 | */ |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 28 | class SkMipMap : public SkCachedData { |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 29 | public: |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 30 | static SkMipMap* Build(const SkPixmap& src, SkDiscardableFactoryProc); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 31 | static SkMipMap* Build(const SkBitmap& src, SkDiscardableFactoryProc); |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 32 | |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 33 | // Determines how many levels a SkMipMap will have without creating that mipmap. |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame^] | 34 | // This does not include the base mipmap level that the user provided when |
| 35 | // creating the SkMipMap. |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 36 | static int ComputeLevelCount(int baseWidth, int baseHeight); |
| 37 | |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 38 | // Determines the size of a given mipmap level. |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame^] | 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. |
cblume | 44e09ec | 2016-06-03 11:59:50 -0700 | [diff] [blame] | 41 | static SkISize ComputeLevelSize(int baseWidth, int baseHeight, int level); |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 42 | |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 43 | struct Level { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 44 | SkPixmap fPixmap; |
fmalita | 921d7ac | 2016-01-22 11:45:39 -0800 | [diff] [blame] | 45 | SkSize fScale; // < 1.0 |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 46 | }; |
skia.committer@gmail.com | a799198 | 2013-07-19 07:00:57 +0000 | [diff] [blame] | 47 | |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 48 | bool extractLevel(const SkSize& scale, Level*) const; |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame^] | 49 | |
| 50 | // countLevels returns the number of mipmap levels generated (which does not |
| 51 | // include the base mipmap level). |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 52 | int countLevels() const; |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame^] | 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. |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 56 | bool getLevel(int index, Level*) const; |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 57 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 58 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 59 | void onDataChange(void* oldData, void* newData) override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 60 | fLevels = (Level*)newData; // could be nullptr |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 61 | } |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 62 | |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 63 | private: |
| 64 | Level* fLevels; |
| 65 | int fCount; |
| 66 | |
| 67 | // we take ownership of levels, and will free it with sk_free() |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 68 | SkMipMap(void* malloc, size_t size) : INHERITED(malloc, size) {} |
| 69 | SkMipMap(size_t size, SkDiscardableMemory* dm) : INHERITED(size, dm) {} |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 70 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 71 | static size_t AllocLevelsSize(int levelCount, size_t pixelSize); |
| 72 | |
| 73 | typedef SkCachedData INHERITED; |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | #endif |