GrAtlasManager.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2018 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 GrAtlasManager_DEFINED
  8. #define GrAtlasManager_DEFINED
  9. #include "src/gpu/GrCaps.h"
  10. #include "src/gpu/GrDrawOpAtlas.h"
  11. #include "src/gpu/GrOnFlushResourceProvider.h"
  12. #include "src/gpu/GrProxyProvider.h"
  13. struct GrGlyph;
  14. class GrTextStrike;
  15. //////////////////////////////////////////////////////////////////////////////////////////////////
  16. /** The GrAtlasManager manages the lifetime of and access to GrDrawOpAtlases.
  17. * It is only available at flush and only via the GrOpFlushState.
  18. *
  19. * This implies that all of the advanced atlasManager functionality (i.e.,
  20. * adding glyphs to the atlas) are only available at flush time.
  21. */
  22. class GrAtlasManager : public GrOnFlushCallbackObject {
  23. public:
  24. GrAtlasManager(GrProxyProvider*, GrStrikeCache*,
  25. size_t maxTextureBytes, GrDrawOpAtlas::AllowMultitexturing);
  26. ~GrAtlasManager() override;
  27. // Change an expected 565 mask format to 8888 if 565 is not supported (will happen when using
  28. // Metal on macOS). The actual conversion of the data is handled in get_packed_glyph_image() in
  29. // GrStrikeCache.cpp
  30. GrMaskFormat resolveMaskFormat(GrMaskFormat format) const {
  31. if (kA565_GrMaskFormat == format &&
  32. !fProxyProvider->caps()->isConfigTexturable(kRGB_565_GrPixelConfig)) {
  33. format = kARGB_GrMaskFormat;
  34. }
  35. return format;
  36. }
  37. // if getProxies returns nullptr, the client must not try to use other functions on the
  38. // GrStrikeCache which use the atlas. This function *must* be called first, before other
  39. // functions which use the atlas. Note that we can have proxies available but none active
  40. // (i.e., none instantiated).
  41. const sk_sp<GrTextureProxy>* getProxies(GrMaskFormat format, unsigned int* numActiveProxies) {
  42. format = this->resolveMaskFormat(format);
  43. if (this->initAtlas(format)) {
  44. *numActiveProxies = this->getAtlas(format)->numActivePages();
  45. return this->getAtlas(format)->getProxies();
  46. }
  47. *numActiveProxies = 0;
  48. return nullptr;
  49. }
  50. void freeAll();
  51. bool hasGlyph(GrGlyph* glyph);
  52. // To ensure the GrDrawOpAtlas does not evict the Glyph Mask from its texture backing store,
  53. // the client must pass in the current op token along with the GrGlyph.
  54. // A BulkUseTokenUpdater is used to manage bulk last use token updating in the Atlas.
  55. // For convenience, this function will also set the use token for the current glyph if required
  56. // NOTE: the bulk uploader is only valid if the subrun has a valid atlasGeneration
  57. void addGlyphToBulkAndSetUseToken(GrDrawOpAtlas::BulkUseTokenUpdater*, GrGlyph*,
  58. GrDeferredUploadToken);
  59. void setUseTokenBulk(const GrDrawOpAtlas::BulkUseTokenUpdater& updater,
  60. GrDeferredUploadToken token,
  61. GrMaskFormat format) {
  62. this->getAtlas(format)->setLastUseTokenBulk(updater, token);
  63. }
  64. // add to texture atlas that matches this format
  65. GrDrawOpAtlas::ErrorCode addToAtlas(
  66. GrResourceProvider*, GrStrikeCache*, GrTextStrike*,
  67. GrDrawOpAtlas::AtlasID*, GrDeferredUploadTarget*, GrMaskFormat,
  68. int width, int height, const void* image, SkIPoint16* loc);
  69. // Some clients may wish to verify the integrity of the texture backing store of the
  70. // GrDrawOpAtlas. The atlasGeneration returned below is a monotonically increasing number which
  71. // changes every time something is removed from the texture backing store.
  72. uint64_t atlasGeneration(GrMaskFormat format) const {
  73. return this->getAtlas(format)->atlasGeneration();
  74. }
  75. // GrOnFlushCallbackObject overrides
  76. void preFlush(GrOnFlushResourceProvider* onFlushResourceProvider, const uint32_t*, int,
  77. SkTArray<sk_sp<GrRenderTargetContext>>*) override {
  78. for (int i = 0; i < kMaskFormatCount; ++i) {
  79. if (fAtlases[i]) {
  80. fAtlases[i]->instantiate(onFlushResourceProvider);
  81. }
  82. }
  83. }
  84. void postFlush(GrDeferredUploadToken startTokenForNextFlush,
  85. const uint32_t* opListIDs, int numOpListIDs) override {
  86. for (int i = 0; i < kMaskFormatCount; ++i) {
  87. if (fAtlases[i]) {
  88. fAtlases[i]->compact(startTokenForNextFlush);
  89. }
  90. }
  91. }
  92. // The AtlasGlyph cache always survives freeGpuResources so we want it to remain in the active
  93. // OnFlushCallbackObject list
  94. bool retainOnFreeGpuResources() override { return true; }
  95. ///////////////////////////////////////////////////////////////////////////
  96. // Functions intended debug only
  97. #ifdef SK_DEBUG
  98. void dump(GrContext* context) const;
  99. #endif
  100. void setAtlasSizesToMinimum_ForTesting();
  101. void setMaxPages_TestingOnly(uint32_t maxPages);
  102. private:
  103. bool initAtlas(GrMaskFormat);
  104. // There is a 1:1 mapping between GrMaskFormats and atlas indices
  105. static int MaskFormatToAtlasIndex(GrMaskFormat format) { return static_cast<int>(format); }
  106. static GrMaskFormat AtlasIndexToMaskFormat(int idx) { return static_cast<GrMaskFormat>(idx); }
  107. GrDrawOpAtlas* getAtlas(GrMaskFormat format) const {
  108. format = this->resolveMaskFormat(format);
  109. int atlasIndex = MaskFormatToAtlasIndex(format);
  110. SkASSERT(fAtlases[atlasIndex]);
  111. return fAtlases[atlasIndex].get();
  112. }
  113. GrDrawOpAtlas::AllowMultitexturing fAllowMultitexturing;
  114. std::unique_ptr<GrDrawOpAtlas> fAtlases[kMaskFormatCount];
  115. GrProxyProvider* fProxyProvider;
  116. sk_sp<const GrCaps> fCaps;
  117. GrStrikeCache* fGlyphCache;
  118. GrDrawOpAtlasConfig fAtlasConfig;
  119. typedef GrOnFlushCallbackObject INHERITED;
  120. };
  121. #endif // GrAtlasManager_DEFINED