SkMipMap.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #ifndef SkMipMap_DEFINED
  8. #define SkMipMap_DEFINED
  9. #include "include/core/SkPixmap.h"
  10. #include "include/core/SkScalar.h"
  11. #include "include/core/SkSize.h"
  12. #include "include/private/SkImageInfoPriv.h"
  13. #include "src/core/SkCachedData.h"
  14. #include "src/shaders/SkShaderBase.h"
  15. class SkBitmap;
  16. class SkDiscardableMemory;
  17. typedef SkDiscardableMemory* (*SkDiscardableFactoryProc)(size_t bytes);
  18. /*
  19. * SkMipMap will generate mipmap levels when given a base mipmap level image.
  20. *
  21. * Any function which deals with mipmap levels indices will start with index 0
  22. * being the first mipmap level which was generated. Said another way, it does
  23. * not include the base level in its range.
  24. */
  25. class SkMipMap : public SkCachedData {
  26. public:
  27. static SkMipMap* Build(const SkPixmap& src, SkDiscardableFactoryProc);
  28. static SkMipMap* Build(const SkBitmap& src, SkDiscardableFactoryProc);
  29. // Determines how many levels a SkMipMap will have without creating that mipmap.
  30. // This does not include the base mipmap level that the user provided when
  31. // creating the SkMipMap.
  32. static int ComputeLevelCount(int baseWidth, int baseHeight);
  33. // Determines the size of a given mipmap level.
  34. // |level| is an index into the generated mipmap levels. It does not include
  35. // the base level. So index 0 represents mipmap level 1.
  36. static SkISize ComputeLevelSize(int baseWidth, int baseHeight, int level);
  37. // We use a block of (possibly discardable) memory to hold an array of Level structs, followed
  38. // by the pixel data for each level. On 32-bit platforms, Level would naturally be 4 byte
  39. // aligned, so the pixel data could end up with 4 byte alignment. If the pixel data is F16,
  40. // it must be 8 byte aligned. To ensure this, keep the Level struct 8 byte aligned as well.
  41. struct alignas(8) Level {
  42. SkPixmap fPixmap;
  43. SkSize fScale; // < 1.0
  44. };
  45. bool extractLevel(const SkSize& scale, Level*) const;
  46. // countLevels returns the number of mipmap levels generated (which does not
  47. // include the base mipmap level).
  48. int countLevels() const;
  49. // |index| is an index into the generated mipmap levels. It does not include
  50. // the base level. So index 0 represents mipmap level 1.
  51. bool getLevel(int index, Level*) const;
  52. protected:
  53. void onDataChange(void* oldData, void* newData) override {
  54. fLevels = (Level*)newData; // could be nullptr
  55. }
  56. private:
  57. sk_sp<SkColorSpace> fCS;
  58. Level* fLevels; // managed by the baseclass, may be null due to onDataChanged.
  59. int fCount;
  60. SkMipMap(void* malloc, size_t size) : INHERITED(malloc, size) {}
  61. SkMipMap(size_t size, SkDiscardableMemory* dm) : INHERITED(size, dm) {}
  62. static size_t AllocLevelsSize(int levelCount, size_t pixelSize);
  63. typedef SkCachedData INHERITED;
  64. };
  65. #endif