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 8 #ifndef SkMipmap_DEFINED 9 #define SkMipmap_DEFINED 10 11 #include "include/core/SkPixmap.h" 12 #include "include/core/SkScalar.h" 13 #include "include/core/SkSize.h" 14 #include "src/core/SkCachedData.h" 15 #include "src/core/SkImageInfoPriv.h" 16 #include "src/shaders/SkShaderBase.h" 17 18 class SkBitmap; 19 class SkData; 20 class SkDiscardableMemory; 21 class SkMipmapBuilder; 22 23 typedef SkDiscardableMemory* (*SkDiscardableFactoryProc)(size_t bytes); 24 25 /* 26 * SkMipmap will generate mipmap levels when given a base mipmap level image. 27 * 28 * Any function which deals with mipmap levels indices will start with index 0 29 * being the first mipmap level which was generated. Said another way, it does 30 * not include the base level in its range. 31 */ 32 class SkMipmap : public SkCachedData { 33 public: 34 ~SkMipmap() override; 35 // Allocate and fill-in a mipmap. If computeContents is false, we just allocated 36 // and compute the sizes/rowbytes, but leave the pixel-data uninitialized. 37 static SkMipmap* Build(const SkPixmap& src, SkDiscardableFactoryProc, 38 bool computeContents = true); 39 40 static SkMipmap* Build(const SkBitmap& src, SkDiscardableFactoryProc); 41 42 // Determines how many levels a SkMipmap will have without creating that mipmap. 43 // This does not include the base mipmap level that the user provided when 44 // creating the SkMipmap. 45 static int ComputeLevelCount(int baseWidth, int baseHeight); ComputeLevelCount(SkISize s)46 static int ComputeLevelCount(SkISize s) { return ComputeLevelCount(s.width(), s.height()); } 47 48 // Determines the size of a given mipmap level. 49 // |level| is an index into the generated mipmap levels. It does not include 50 // the base level. So index 0 represents mipmap level 1. 51 static SkISize ComputeLevelSize(int baseWidth, int baseHeight, int level); 52 53 // Computes the fractional level based on the scaling in X and Y. 54 static float ComputeLevel(SkSize scaleSize); 55 56 // We use a block of (possibly discardable) memory to hold an array of Level structs, followed 57 // by the pixel data for each level. On 32-bit platforms, Level would naturally be 4 byte 58 // aligned, so the pixel data could end up with 4 byte alignment. If the pixel data is F16, 59 // it must be 8 byte aligned. To ensure this, keep the Level struct 8 byte aligned as well. 60 struct alignas(8) Level { 61 SkPixmap fPixmap; 62 SkSize fScale; // < 1.0 63 }; 64 65 bool extractLevel(SkSize scale, Level*) const; 66 67 // countLevels returns the number of mipmap levels generated (which does not 68 // include the base mipmap level). 69 int countLevels() const; 70 71 // |index| is an index into the generated mipmap levels. It does not include 72 // the base level. So index 0 represents mipmap level 1. 73 bool getLevel(int index, Level*) const; 74 75 bool validForRootLevel(const SkImageInfo&) const; 76 77 sk_sp<SkData> serialize() const; 78 static bool Deserialize(SkMipmapBuilder*, const void* data, size_t size); 79 80 protected: onDataChange(void * oldData,void * newData)81 void onDataChange(void* oldData, void* newData) override { 82 fLevels = (Level*)newData; // could be nullptr 83 } 84 85 private: 86 sk_sp<SkColorSpace> fCS; 87 Level* fLevels; // managed by the baseclass, may be null due to onDataChanged. 88 int fCount; 89 90 SkMipmap(void* malloc, size_t size); 91 SkMipmap(size_t size, SkDiscardableMemory* dm); 92 93 static size_t AllocLevelsSize(int levelCount, size_t pixelSize); 94 }; 95 96 #endif 97