GrLegacyDirectContext.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #include "include/gpu/GrContext.h"
  8. #include "include/gpu/GrContextThreadSafeProxy.h"
  9. #include "src/gpu/GrContextPriv.h"
  10. #include "src/gpu/GrContextThreadSafeProxyPriv.h"
  11. #include "src/gpu/GrGpu.h"
  12. #include "src/gpu/effects/GrSkSLFP.h"
  13. #include "src/gpu/gl/GrGLGpu.h"
  14. #include "src/gpu/mock/GrMockGpu.h"
  15. #include "src/gpu/text/GrStrikeCache.h"
  16. #ifdef SK_METAL
  17. #include "src/gpu/mtl/GrMtlTrampoline.h"
  18. #endif
  19. #ifdef SK_VULKAN
  20. #include "src/gpu/vk/GrVkGpu.h"
  21. #endif
  22. #ifdef SK_DAWN
  23. #include "dawn/GrDawnGpu.h"
  24. #endif
  25. #ifdef SK_DISABLE_REDUCE_OPLIST_SPLITTING
  26. static const bool kDefaultReduceOpListSplitting = false;
  27. #else
  28. static const bool kDefaultReduceOpListSplitting = false;
  29. #endif
  30. class SK_API GrLegacyDirectContext : public GrContext {
  31. public:
  32. GrLegacyDirectContext(GrBackendApi backend, const GrContextOptions& options)
  33. : INHERITED(backend, options)
  34. , fAtlasManager(nullptr) {
  35. }
  36. ~GrLegacyDirectContext() override {
  37. // this if-test protects against the case where the context is being destroyed
  38. // before having been fully created
  39. if (this->priv().getGpu()) {
  40. this->flush();
  41. }
  42. delete fAtlasManager;
  43. }
  44. void abandonContext() override {
  45. INHERITED::abandonContext();
  46. fAtlasManager->freeAll();
  47. }
  48. void releaseResourcesAndAbandonContext() override {
  49. INHERITED::releaseResourcesAndAbandonContext();
  50. fAtlasManager->freeAll();
  51. }
  52. void freeGpuResources() override {
  53. this->flush();
  54. fAtlasManager->freeAll();
  55. INHERITED::freeGpuResources();
  56. }
  57. protected:
  58. bool init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) override {
  59. SkASSERT(caps && !FPFactoryCache);
  60. SkASSERT(!fThreadSafeProxy);
  61. FPFactoryCache.reset(new GrSkSLFPFactoryCache());
  62. fThreadSafeProxy = GrContextThreadSafeProxyPriv::Make(this->backend(),
  63. this->options(),
  64. this->contextID(),
  65. caps, FPFactoryCache);
  66. if (!INHERITED::init(std::move(caps), std::move(FPFactoryCache))) {
  67. return false;
  68. }
  69. bool reduceOpListSplitting = kDefaultReduceOpListSplitting;
  70. if (GrContextOptions::Enable::kNo == this->options().fReduceOpListSplitting) {
  71. reduceOpListSplitting = false;
  72. } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpListSplitting) {
  73. reduceOpListSplitting = true;
  74. }
  75. this->setupDrawingManager(true, reduceOpListSplitting);
  76. SkASSERT(this->caps());
  77. GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
  78. if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
  79. // multitexturing supported only if range can represent the index + texcoords fully
  80. !(this->caps()->shaderCaps()->floatIs32Bits() ||
  81. this->caps()->shaderCaps()->integerSupport())) {
  82. allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
  83. } else {
  84. allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
  85. }
  86. GrStrikeCache* glyphCache = this->priv().getGrStrikeCache();
  87. GrProxyProvider* proxyProvider = this->priv().proxyProvider();
  88. fAtlasManager = new GrAtlasManager(proxyProvider, glyphCache,
  89. this->options().fGlyphCacheTextureMaximumBytes,
  90. allowMultitexturing);
  91. this->priv().addOnFlushCallbackObject(fAtlasManager);
  92. return true;
  93. }
  94. GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
  95. private:
  96. GrAtlasManager* fAtlasManager;
  97. typedef GrContext INHERITED;
  98. };
  99. sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
  100. GrContextOptions defaultOptions;
  101. return MakeGL(std::move(interface), defaultOptions);
  102. }
  103. sk_sp<GrContext> GrContext::MakeGL(const GrContextOptions& options) {
  104. return MakeGL(nullptr, options);
  105. }
  106. sk_sp<GrContext> GrContext::MakeGL() {
  107. GrContextOptions defaultOptions;
  108. return MakeGL(nullptr, defaultOptions);
  109. }
  110. sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
  111. const GrContextOptions& options) {
  112. sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options));
  113. context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
  114. if (!context->fGpu) {
  115. return nullptr;
  116. }
  117. if (!context->init(context->fGpu->refCaps(), nullptr)) {
  118. return nullptr;
  119. }
  120. return context;
  121. }
  122. sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
  123. GrContextOptions defaultOptions;
  124. return MakeMock(mockOptions, defaultOptions);
  125. }
  126. sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
  127. const GrContextOptions& options) {
  128. sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options));
  129. context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
  130. if (!context->fGpu) {
  131. return nullptr;
  132. }
  133. if (!context->init(context->fGpu->refCaps(), nullptr)) {
  134. return nullptr;
  135. }
  136. return context;
  137. }
  138. sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext) {
  139. #ifdef SK_VULKAN
  140. GrContextOptions defaultOptions;
  141. return MakeVulkan(backendContext, defaultOptions);
  142. #else
  143. return nullptr;
  144. #endif
  145. }
  146. sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
  147. const GrContextOptions& options) {
  148. #ifdef SK_VULKAN
  149. GrContextOptions defaultOptions;
  150. sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options));
  151. context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
  152. if (!context->fGpu) {
  153. return nullptr;
  154. }
  155. if (!context->init(context->fGpu->refCaps(), nullptr)) {
  156. return nullptr;
  157. }
  158. return context;
  159. #else
  160. return nullptr;
  161. #endif
  162. }
  163. #ifdef SK_METAL
  164. sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
  165. GrContextOptions defaultOptions;
  166. return MakeMetal(device, queue, defaultOptions);
  167. }
  168. sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
  169. sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options));
  170. context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
  171. if (!context->fGpu) {
  172. return nullptr;
  173. }
  174. if (!context->init(context->fGpu->refCaps(), nullptr)) {
  175. return nullptr;
  176. }
  177. return context;
  178. }
  179. #endif
  180. #ifdef SK_DAWN
  181. sk_sp<GrContext> GrContext::MakeDawn(const dawn::Device& device) {
  182. GrContextOptions defaultOptions;
  183. return MakeDawn(device, defaultOptions);
  184. }
  185. sk_sp<GrContext> GrContext::MakeDawn(const dawn::Device& device, const GrContextOptions& options) {
  186. sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDawn, options));
  187. context->fGpu = GrDawnGpu::Make(device, options, context.get());
  188. if (!context->fGpu) {
  189. return nullptr;
  190. }
  191. if (!context->init(context->fGpu->refCaps(), nullptr)) {
  192. return nullptr;
  193. }
  194. return context;
  195. }
  196. #endif