GrMockTexture.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright 2017 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 GrMockTexture_DEFINED
  8. #define GrMockTexture_DEFINED
  9. #include "include/gpu/GrRenderTarget.h"
  10. #include "include/gpu/GrTexture.h"
  11. #include "include/gpu/mock/GrMockTypes.h"
  12. #include "src/gpu/GrRenderTargetPriv.h"
  13. #include "src/gpu/GrStencilAttachment.h"
  14. #include "src/gpu/GrTexturePriv.h"
  15. #include "src/gpu/mock/GrMockGpu.h"
  16. class GrMockTexture : public GrTexture {
  17. public:
  18. GrMockTexture(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
  19. GrProtected isProtected, GrMipMapsStatus mipMapsStatus,
  20. const GrMockTextureInfo& info)
  21. : GrMockTexture(gpu, desc, isProtected, mipMapsStatus, info) {
  22. this->registerWithCache(budgeted);
  23. }
  24. GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrProtected isProtected,
  25. GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& info,
  26. GrWrapCacheable cacheable, GrIOType ioType)
  27. : GrMockTexture(gpu, desc, isProtected, mipMapsStatus, info) {
  28. if (ioType == kRead_GrIOType) {
  29. this->setReadOnly();
  30. }
  31. this->registerWithCacheWrapped(cacheable);
  32. }
  33. ~GrMockTexture() override {}
  34. GrBackendTexture getBackendTexture() const override {
  35. return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(),
  36. fInfo);
  37. }
  38. GrBackendFormat backendFormat() const override {
  39. return fInfo.getBackendFormat();
  40. }
  41. void textureParamsModified() override {}
  42. protected:
  43. // constructor for subclasses
  44. GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrProtected isProtected,
  45. GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& info)
  46. : GrSurface(gpu, desc, isProtected)
  47. , INHERITED(gpu, desc, isProtected, GrTextureType::k2D, mipMapsStatus)
  48. , fInfo(info) {}
  49. void onRelease() override {
  50. INHERITED::onRelease();
  51. }
  52. void onAbandon() override {
  53. INHERITED::onAbandon();
  54. }
  55. bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override {
  56. return false;
  57. }
  58. private:
  59. GrMockTextureInfo fInfo;
  60. typedef GrTexture INHERITED;
  61. };
  62. class GrMockRenderTarget : public GrRenderTarget {
  63. public:
  64. GrMockRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
  65. int sampleCnt, GrProtected isProtected, const GrMockRenderTargetInfo& info)
  66. : GrSurface(gpu, desc, isProtected)
  67. , INHERITED(gpu, desc, sampleCnt, isProtected)
  68. , fInfo(info) {
  69. this->registerWithCache(budgeted);
  70. }
  71. enum Wrapped { kWrapped };
  72. GrMockRenderTarget(GrMockGpu* gpu, Wrapped, const GrSurfaceDesc& desc, int sampleCnt,
  73. GrProtected isProtected, const GrMockRenderTargetInfo& info)
  74. : GrSurface(gpu, desc, isProtected)
  75. , INHERITED(gpu, desc, sampleCnt, isProtected)
  76. , fInfo(info) {
  77. this->registerWithCacheWrapped(GrWrapCacheable::kNo);
  78. }
  79. ResolveType getResolveType() const override { return kCanResolve_ResolveType; }
  80. bool canAttemptStencilAttachment() const override { return true; }
  81. bool completeStencilAttachment() override { return true; }
  82. size_t onGpuMemorySize() const override {
  83. int numColorSamples = this->numSamples();
  84. if (numColorSamples > 1) {
  85. // Add one to account for the resolve buffer.
  86. ++numColorSamples;
  87. }
  88. return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
  89. numColorSamples, GrMipMapped::kNo);
  90. }
  91. GrBackendRenderTarget getBackendRenderTarget() const override {
  92. int numStencilBits = 0;
  93. if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
  94. numStencilBits = stencil->bits();
  95. }
  96. return {this->width(), this->height(), this->numSamples(), numStencilBits, fInfo};
  97. }
  98. GrBackendFormat backendFormat() const override {
  99. return fInfo.getBackendFormat();
  100. }
  101. protected:
  102. // constructor for subclasses
  103. GrMockRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc, int sampleCnt,
  104. GrProtected isProtected, const GrMockRenderTargetInfo& info)
  105. : GrSurface(gpu, desc, isProtected)
  106. , INHERITED(gpu, desc, sampleCnt, isProtected)
  107. , fInfo(info) {}
  108. private:
  109. GrMockRenderTargetInfo fInfo;
  110. typedef GrRenderTarget INHERITED;
  111. };
  112. class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget {
  113. public:
  114. // Internally created.
  115. GrMockTextureRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
  116. int sampleCnt, GrProtected isProtected, GrMipMapsStatus mipMapsStatus,
  117. const GrMockTextureInfo& texInfo,
  118. const GrMockRenderTargetInfo& rtInfo)
  119. : GrSurface(gpu, desc, isProtected)
  120. , GrMockTexture(gpu, desc, isProtected, mipMapsStatus, texInfo)
  121. , GrMockRenderTarget(gpu, desc, sampleCnt, isProtected, rtInfo) {
  122. this->registerWithCache(budgeted);
  123. }
  124. // Renderable wrapped backend texture.
  125. GrMockTextureRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc, int sampleCnt,
  126. GrProtected isProtected, GrMipMapsStatus mipMapsStatus,
  127. const GrMockTextureInfo& texInfo,
  128. const GrMockRenderTargetInfo& rtInfo, GrWrapCacheable cacheble)
  129. : GrSurface(gpu, desc, isProtected)
  130. , GrMockTexture(gpu, desc, isProtected, mipMapsStatus, texInfo)
  131. , GrMockRenderTarget(gpu, desc, sampleCnt, isProtected, rtInfo) {
  132. this->registerWithCacheWrapped(cacheble);
  133. }
  134. GrTexture* asTexture() override { return this; }
  135. GrRenderTarget* asRenderTarget() override { return this; }
  136. const GrTexture* asTexture() const override { return this; }
  137. const GrRenderTarget* asRenderTarget() const override { return this; }
  138. GrBackendFormat backendFormat() const override {
  139. return GrMockTexture::backendFormat();
  140. }
  141. protected:
  142. // This avoids an inherits via dominance warning on MSVC.
  143. void willRemoveLastRefOrPendingIO() override { GrTexture::willRemoveLastRefOrPendingIO(); }
  144. private:
  145. void onAbandon() override {
  146. GrRenderTarget::onAbandon();
  147. GrMockTexture::onAbandon();
  148. }
  149. void onRelease() override {
  150. GrRenderTarget::onRelease();
  151. GrMockTexture::onRelease();
  152. }
  153. size_t onGpuMemorySize() const override {
  154. int numColorSamples = this->numSamples();
  155. if (numColorSamples > 1) {
  156. // Add one to account for the resolve buffer.
  157. ++numColorSamples;
  158. }
  159. return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
  160. numColorSamples,
  161. this->texturePriv().mipMapped());
  162. }
  163. void computeScratchKey(GrScratchKey* key) const override {
  164. GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
  165. GrRenderable::kYes, this->numSamples(),
  166. this->texturePriv().mipMapped(), key);
  167. }
  168. };
  169. #endif