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 | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame^] | 15 | #include "SkShader.h" |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 16 | |
| 17 | class SkBitmap; |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 18 | class SkDiscardableMemory; |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 19 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 20 | typedef SkDiscardableMemory* (*SkDiscardableFactoryProc)(size_t bytes); |
| 21 | |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 22 | /* |
| 23 | * SkMipMap will generate mipmap levels when given a base mipmap level image. |
| 24 | * |
| 25 | * Any function which deals with mipmap levels indices will start with index 0 |
| 26 | * being the first mipmap level which was generated. Said another way, it does |
| 27 | * not include the base level in its range. |
| 28 | */ |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 29 | class SkMipMap : public SkCachedData { |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 30 | public: |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame^] | 31 | static SkMipMap* Build(const SkPixmap& src, SkSourceGammaTreatment, SkDiscardableFactoryProc); |
| 32 | static SkMipMap* Build(const SkBitmap& src, SkSourceGammaTreatment, SkDiscardableFactoryProc); |
| 33 | |
| 34 | static SkSourceGammaTreatment DeduceTreatment(const SkShader::ContextRec& rec) { |
| 35 | return (SkShader::ContextRec::kPMColor_DstType == rec.fPreferredDstType) ? |
| 36 | SkSourceGammaTreatment::kIgnore : SkSourceGammaTreatment::kRespect; |
| 37 | } |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 38 | |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 39 | // Determines how many levels a SkMipMap will have without creating that mipmap. |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 40 | // This does not include the base mipmap level that the user provided when |
| 41 | // creating the SkMipMap. |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 42 | static int ComputeLevelCount(int baseWidth, int baseHeight); |
| 43 | |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 44 | // Determines the size of a given mipmap level. |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 45 | // |level| is an index into the generated mipmap levels. It does not include |
| 46 | // the base level. So index 0 represents mipmap level 1. |
cblume | 44e09ec | 2016-06-03 11:59:50 -0700 | [diff] [blame] | 47 | static SkISize ComputeLevelSize(int baseWidth, int baseHeight, int level); |
cblume | f95ff4a | 2016-06-02 09:01:48 -0700 | [diff] [blame] | 48 | |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 49 | struct Level { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 50 | SkPixmap fPixmap; |
fmalita | 921d7ac | 2016-01-22 11:45:39 -0800 | [diff] [blame] | 51 | SkSize fScale; // < 1.0 |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 52 | }; |
skia.committer@gmail.com | a799198 | 2013-07-19 07:00:57 +0000 | [diff] [blame] | 53 | |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 54 | bool extractLevel(const SkSize& scale, Level*) const; |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 55 | |
| 56 | // countLevels returns the number of mipmap levels generated (which does not |
| 57 | // include the base mipmap level). |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 58 | int countLevels() const; |
cblume | 609623b | 2016-06-09 09:44:33 -0700 | [diff] [blame] | 59 | |
| 60 | // |index| is an index into the generated mipmap levels. It does not include |
| 61 | // the base level. So index 0 represents mipmap level 1. |
cblume | e2412d5 | 2016-02-17 14:53:23 -0800 | [diff] [blame] | 62 | bool getLevel(int index, Level*) const; |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 63 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 64 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 65 | void onDataChange(void* oldData, void* newData) override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 66 | fLevels = (Level*)newData; // could be nullptr |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 67 | } |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 68 | |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 69 | private: |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame^] | 70 | sk_sp<SkColorSpace> fCS; |
| 71 | Level* fLevels; // managed by the baseclass, may be null due to onDataChanged. |
| 72 | int fCount; |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 73 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 74 | SkMipMap(void* malloc, size_t size) : INHERITED(malloc, size) {} |
| 75 | SkMipMap(size_t size, SkDiscardableMemory* dm) : INHERITED(size, dm) {} |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 76 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 77 | static size_t AllocLevelsSize(int levelCount, size_t pixelSize); |
| 78 | |
| 79 | typedef SkCachedData INHERITED; |
reed@google.com | eed6f1b | 2013-07-18 19:53:31 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | #endif |