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 "SkCachedData.h" 12 #include "SkPixmap.h" 13 #include "SkScalar.h" 14 #include "SkSize.h" 15 16 class SkBitmap; 17 class SkDiscardableMemory; 18 19 typedef SkDiscardableMemory* (*SkDiscardableFactoryProc)(size_t bytes); 20 21 class SkMipMap : public SkCachedData { 22 public: 23 static SkMipMap* Build(const SkPixmap& src, SkDiscardableFactoryProc); 24 static SkMipMap* Build(const SkBitmap& src, SkDiscardableFactoryProc); 25 26 // This function lets you determine how many levels a SkMipMap will have without 27 // creating that mipmap. 28 static int ComputeLevelCount(int baseWidth, int baseHeight); 29 30 struct Level { 31 SkPixmap fPixmap; 32 SkSize fScale; // < 1.0 33 }; 34 35 bool extractLevel(const SkSize& scale, Level*) const; 36 int countLevels() const; 37 bool getLevel(int index, Level*) const; 38 39 protected: onDataChange(void * oldData,void * newData)40 void onDataChange(void* oldData, void* newData) override { 41 fLevels = (Level*)newData; // could be nullptr 42 } 43 44 private: 45 Level* fLevels; 46 int fCount; 47 48 // we take ownership of levels, and will free it with sk_free() SkMipMap(void * malloc,size_t size)49 SkMipMap(void* malloc, size_t size) : INHERITED(malloc, size) {} SkMipMap(size_t size,SkDiscardableMemory * dm)50 SkMipMap(size_t size, SkDiscardableMemory* dm) : INHERITED(size, dm) {} 51 52 static size_t AllocLevelsSize(int levelCount, size_t pixelSize); 53 54 typedef SkCachedData INHERITED; 55 }; 56 57 #endif 58