GrMockGpu.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #include "src/gpu/mock/GrMockBuffer.h"
  8. #include "src/gpu/mock/GrMockCaps.h"
  9. #include "src/gpu/mock/GrMockGpu.h"
  10. #include "src/gpu/mock/GrMockGpuCommandBuffer.h"
  11. #include "src/gpu/mock/GrMockStencilAttachment.h"
  12. #include "src/gpu/mock/GrMockTexture.h"
  13. #include <atomic>
  14. int GrMockGpu::NextInternalTextureID() {
  15. static std::atomic<int> nextID{1};
  16. int id;
  17. do {
  18. id = nextID.fetch_add(1);
  19. } while (0 == id); // Reserve 0 for an invalid ID.
  20. return id;
  21. }
  22. int GrMockGpu::NextExternalTextureID() {
  23. // We use negative ints for the "testing only external textures" so they can easily be
  24. // identified when debugging.
  25. static std::atomic<int> nextID{-1};
  26. return nextID--;
  27. }
  28. int GrMockGpu::NextInternalRenderTargetID() {
  29. // We start off with large numbers to differentiate from texture IDs, even though they're
  30. // technically in a different space.
  31. static std::atomic<int> nextID{SK_MaxS32};
  32. return nextID--;
  33. }
  34. int GrMockGpu::NextExternalRenderTargetID() {
  35. // We use large negative ints for the "testing only external render targets" so they can easily
  36. // be identified when debugging.
  37. static std::atomic<int> nextID{SK_MinS32};
  38. return nextID++;
  39. }
  40. sk_sp<GrGpu> GrMockGpu::Make(const GrMockOptions* mockOptions,
  41. const GrContextOptions& contextOptions, GrContext* context) {
  42. static const GrMockOptions kDefaultOptions = GrMockOptions();
  43. if (!mockOptions) {
  44. mockOptions = &kDefaultOptions;
  45. }
  46. return sk_sp<GrGpu>(new GrMockGpu(context, *mockOptions, contextOptions));
  47. }
  48. GrGpuRTCommandBuffer* GrMockGpu::getCommandBuffer(
  49. GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
  50. const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
  51. const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) {
  52. return new GrMockGpuRTCommandBuffer(this, rt, origin);
  53. }
  54. GrGpuTextureCommandBuffer* GrMockGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
  55. return new GrMockGpuTextureCommandBuffer(texture, origin);
  56. }
  57. void GrMockGpu::submit(GrGpuCommandBuffer* buffer) {
  58. if (buffer->asRTCommandBuffer()) {
  59. this->submitCommandBuffer(
  60. static_cast<GrMockGpuRTCommandBuffer*>(buffer->asRTCommandBuffer()));
  61. }
  62. delete buffer;
  63. }
  64. void GrMockGpu::submitCommandBuffer(const GrMockGpuRTCommandBuffer* cmdBuffer) {
  65. for (int i = 0; i < cmdBuffer->numDraws(); ++i) {
  66. fStats.incNumDraws();
  67. }
  68. }
  69. GrMockGpu::GrMockGpu(GrContext* context, const GrMockOptions& options,
  70. const GrContextOptions& contextOptions)
  71. : INHERITED(context)
  72. , fMockOptions(options) {
  73. fCaps.reset(new GrMockCaps(contextOptions, options));
  74. }
  75. void GrMockGpu::querySampleLocations(GrRenderTarget* rt, SkTArray<SkPoint>* sampleLocations) {
  76. sampleLocations->reset();
  77. int numRemainingSamples = rt->numSamples();
  78. while (numRemainingSamples > 0) {
  79. // Use standard D3D sample locations.
  80. switch (numRemainingSamples) {
  81. case 0:
  82. case 1:
  83. sampleLocations->push_back().set(.5, .5);
  84. break;
  85. case 2:
  86. sampleLocations->push_back().set(.75, .75);
  87. sampleLocations->push_back().set(.25, .25);
  88. break;
  89. case 3:
  90. case 4:
  91. sampleLocations->push_back().set(.375, .125);
  92. sampleLocations->push_back().set(.875, .375);
  93. sampleLocations->push_back().set(.125, .625);
  94. sampleLocations->push_back().set(.625, .875);
  95. break;
  96. case 5:
  97. case 6:
  98. case 7:
  99. case 8:
  100. sampleLocations->push_back().set(.5625, .3125);
  101. sampleLocations->push_back().set(.4375, .6875);
  102. sampleLocations->push_back().set(.8125, .5625);
  103. sampleLocations->push_back().set(.3125, .1875);
  104. sampleLocations->push_back().set(.1875, .8125);
  105. sampleLocations->push_back().set(.0625, .4375);
  106. sampleLocations->push_back().set(.6875, .4375);
  107. sampleLocations->push_back().set(.4375, .0625);
  108. break;
  109. default:
  110. sampleLocations->push_back().set(.5625, .5625);
  111. sampleLocations->push_back().set(.4375, .3125);
  112. sampleLocations->push_back().set(.3125, .6250);
  113. sampleLocations->push_back().set(.2500, .4375);
  114. sampleLocations->push_back().set(.1875, .3750);
  115. sampleLocations->push_back().set(.6250, .8125);
  116. sampleLocations->push_back().set(.8125, .6875);
  117. sampleLocations->push_back().set(.6875, .1875);
  118. sampleLocations->push_back().set(.3750, .8750);
  119. sampleLocations->push_back().set(.5000, .0625);
  120. sampleLocations->push_back().set(.2500, .1250);
  121. sampleLocations->push_back().set(.1250, .2500);
  122. sampleLocations->push_back().set(.0000, .5000);
  123. sampleLocations->push_back().set(.4375, .2500);
  124. sampleLocations->push_back().set(.8750, .4375);
  125. sampleLocations->push_back().set(.0625, .0000);
  126. break;
  127. }
  128. numRemainingSamples = rt->numSamples() - sampleLocations->count();
  129. }
  130. }
  131. sk_sp<GrTexture> GrMockGpu::onCreateTexture(const GrSurfaceDesc& desc, GrRenderable renderable,
  132. int renderTargetSampleCnt, SkBudgeted budgeted,
  133. GrProtected isProtected, const GrMipLevel texels[],
  134. int mipLevelCount) {
  135. if (fMockOptions.fFailTextureAllocations) {
  136. return nullptr;
  137. }
  138. GrColorType ct = GrPixelConfigToColorType(desc.fConfig);
  139. if (GrColorType::kUnknown == ct) {
  140. return nullptr;
  141. }
  142. GrMipMapsStatus mipMapsStatus = mipLevelCount > 1 ? GrMipMapsStatus::kValid
  143. : GrMipMapsStatus::kNotAllocated;
  144. GrMockTextureInfo texInfo(ct, NextInternalTextureID());
  145. if (renderable == GrRenderable::kYes) {
  146. GrMockRenderTargetInfo rtInfo(ct, NextInternalRenderTargetID());
  147. return sk_sp<GrTexture>(new GrMockTextureRenderTarget(this, budgeted, desc,
  148. renderTargetSampleCnt, isProtected,
  149. mipMapsStatus, texInfo, rtInfo));
  150. }
  151. return sk_sp<GrTexture>(
  152. new GrMockTexture(this, budgeted, desc, isProtected, mipMapsStatus, texInfo));
  153. }
  154. sk_sp<GrTexture> GrMockGpu::onCreateCompressedTexture(int width, int height,
  155. SkImage::CompressionType compressionType,
  156. SkBudgeted budgeted, const void* data) {
  157. return nullptr;
  158. }
  159. sk_sp<GrTexture> GrMockGpu::onWrapBackendTexture(const GrBackendTexture& tex, GrColorType colorType,
  160. GrWrapOwnership ownership,
  161. GrWrapCacheable wrapType, GrIOType ioType) {
  162. GrMockTextureInfo texInfo;
  163. SkAssertResult(tex.getMockTextureInfo(&texInfo));
  164. SkASSERT(colorType == texInfo.fColorType);
  165. GrSurfaceDesc desc;
  166. desc.fWidth = tex.width();
  167. desc.fHeight = tex.height();
  168. desc.fConfig = texInfo.pixelConfig();
  169. GrMipMapsStatus mipMapsStatus = tex.hasMipMaps() ? GrMipMapsStatus::kValid
  170. : GrMipMapsStatus::kNotAllocated;
  171. auto isProtected = GrProtected(tex.isProtected());
  172. return sk_sp<GrTexture>(
  173. new GrMockTexture(this, desc, isProtected, mipMapsStatus, texInfo, wrapType, ioType));
  174. }
  175. sk_sp<GrTexture> GrMockGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
  176. int sampleCnt,
  177. GrColorType colorType,
  178. GrWrapOwnership ownership,
  179. GrWrapCacheable cacheable) {
  180. GrMockTextureInfo texInfo;
  181. SkAssertResult(tex.getMockTextureInfo(&texInfo));
  182. SkASSERT(colorType == texInfo.fColorType);
  183. GrSurfaceDesc desc;
  184. desc.fWidth = tex.width();
  185. desc.fHeight = tex.height();
  186. desc.fConfig = texInfo.pixelConfig();
  187. GrMipMapsStatus mipMapsStatus =
  188. tex.hasMipMaps() ? GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
  189. // The client gave us the texture ID but we supply the render target ID.
  190. GrMockRenderTargetInfo rtInfo(texInfo.fColorType, NextInternalRenderTargetID());
  191. auto isProtected = GrProtected(tex.isProtected());
  192. return sk_sp<GrTexture>(new GrMockTextureRenderTarget(
  193. this, desc, sampleCnt, isProtected, mipMapsStatus, texInfo, rtInfo, cacheable));
  194. }
  195. sk_sp<GrRenderTarget> GrMockGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt,
  196. GrColorType colorType) {
  197. GrMockRenderTargetInfo info;
  198. SkAssertResult(rt.getMockRenderTargetInfo(&info));
  199. SkASSERT(colorType == info.colorType());
  200. GrSurfaceDesc desc;
  201. desc.fWidth = rt.width();
  202. desc.fHeight = rt.height();
  203. desc.fConfig = info.pixelConfig();
  204. auto isProtected = GrProtected(rt.isProtected());
  205. return sk_sp<GrRenderTarget>(new GrMockRenderTarget(this, GrMockRenderTarget::kWrapped, desc,
  206. rt.sampleCnt(), isProtected, info));
  207. }
  208. sk_sp<GrRenderTarget> GrMockGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
  209. int sampleCnt,
  210. GrColorType colorType) {
  211. GrMockTextureInfo texInfo;
  212. SkAssertResult(tex.getMockTextureInfo(&texInfo));
  213. SkASSERT(colorType == texInfo.fColorType);
  214. GrSurfaceDesc desc;
  215. desc.fWidth = tex.width();
  216. desc.fHeight = tex.height();
  217. desc.fConfig = texInfo.pixelConfig();
  218. // The client gave us the texture ID but we supply the render target ID.
  219. GrMockRenderTargetInfo rtInfo(texInfo.fColorType, NextInternalRenderTargetID());
  220. auto isProtected = GrProtected(tex.isProtected());
  221. return sk_sp<GrRenderTarget>(new GrMockRenderTarget(this, GrMockRenderTarget::kWrapped, desc,
  222. sampleCnt, isProtected, rtInfo));
  223. }
  224. sk_sp<GrGpuBuffer> GrMockGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
  225. GrAccessPattern accessPattern, const void*) {
  226. return sk_sp<GrGpuBuffer>(new GrMockBuffer(this, sizeInBytes, type, accessPattern));
  227. }
  228. GrStencilAttachment* GrMockGpu::createStencilAttachmentForRenderTarget(
  229. const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
  230. SkASSERT(numStencilSamples == rt->numSamples());
  231. static constexpr int kBits = 8;
  232. fStats.incStencilAttachmentCreates();
  233. return new GrMockStencilAttachment(this, width, height, kBits, rt->numSamples());
  234. }
  235. GrBackendTexture GrMockGpu::createBackendTexture(int w, int h,
  236. const GrBackendFormat& format,
  237. GrMipMapped mipMapped,
  238. GrRenderable /* renderable */,
  239. const void* /* pixels */,
  240. size_t /* rowBytes */,
  241. const SkColor4f* /* color */,
  242. GrProtected /* isProtected */) {
  243. if (!format.getMockColorType()) {
  244. return GrBackendTexture(); // invalid;
  245. }
  246. if (!this->caps()->isFormatTexturable(*format.getMockColorType(), format)) {
  247. return GrBackendTexture(); // invalid
  248. }
  249. GrMockTextureInfo info(*format.getMockColorType(), NextExternalTextureID());
  250. fOutstandingTestingOnlyTextureIDs.add(info.fID);
  251. return GrBackendTexture(w, h, mipMapped, info);
  252. }
  253. void GrMockGpu::deleteBackendTexture(const GrBackendTexture& tex) {
  254. SkASSERT(GrBackendApi::kMock == tex.backend());
  255. GrMockTextureInfo info;
  256. if (tex.getMockTextureInfo(&info)) {
  257. fOutstandingTestingOnlyTextureIDs.remove(info.fID);
  258. }
  259. }
  260. #if GR_TEST_UTILS
  261. bool GrMockGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
  262. SkASSERT(GrBackendApi::kMock == tex.backend());
  263. GrMockTextureInfo info;
  264. if (!tex.getMockTextureInfo(&info)) {
  265. return false;
  266. }
  267. return fOutstandingTestingOnlyTextureIDs.contains(info.fID);
  268. }
  269. GrBackendRenderTarget GrMockGpu::createTestingOnlyBackendRenderTarget(int w, int h,
  270. GrColorType colorType) {
  271. GrMockRenderTargetInfo info(colorType, NextExternalRenderTargetID());
  272. static constexpr int kSampleCnt = 1;
  273. static constexpr int kStencilBits = 8;
  274. return GrBackendRenderTarget(w, h, kSampleCnt, kStencilBits, info);
  275. }
  276. void GrMockGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) {}
  277. #endif