GrGpu.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. * Copyright 2011 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 GrGpu_DEFINED
  8. #define GrGpu_DEFINED
  9. #include "include/core/SkPath.h"
  10. #include "include/core/SkSurface.h"
  11. #include "include/gpu/GrTypes.h"
  12. #include "include/private/SkTArray.h"
  13. #include "src/gpu/GrAllocator.h"
  14. #include "src/gpu/GrCaps.h"
  15. #include "src/gpu/GrGpuCommandBuffer.h"
  16. #include "src/gpu/GrProgramDesc.h"
  17. #include "src/gpu/GrSamplePatternDictionary.h"
  18. #include "src/gpu/GrSwizzle.h"
  19. #include "src/gpu/GrTextureProducer.h"
  20. #include "src/gpu/GrXferProcessor.h"
  21. #include <map>
  22. class GrBackendRenderTarget;
  23. class GrBackendSemaphore;
  24. class GrGpuBuffer;
  25. class GrContext;
  26. struct GrContextOptions;
  27. class GrGLContext;
  28. class GrMesh;
  29. class GrPath;
  30. class GrPathRenderer;
  31. class GrPathRendererChain;
  32. class GrPathRendering;
  33. class GrPipeline;
  34. class GrPrimitiveProcessor;
  35. class GrRenderTarget;
  36. class GrSemaphore;
  37. class GrStencilAttachment;
  38. class GrStencilSettings;
  39. class GrSurface;
  40. class GrTexture;
  41. class SkJSONWriter;
  42. class GrGpu : public SkRefCnt {
  43. public:
  44. GrGpu(GrContext* context);
  45. ~GrGpu() override;
  46. GrContext* getContext() { return fContext; }
  47. const GrContext* getContext() const { return fContext; }
  48. /**
  49. * Gets the capabilities of the draw target.
  50. */
  51. const GrCaps* caps() const { return fCaps.get(); }
  52. sk_sp<const GrCaps> refCaps() const { return fCaps; }
  53. GrPathRendering* pathRendering() { return fPathRendering.get(); }
  54. enum class DisconnectType {
  55. // No cleanup should be attempted, immediately cease making backend API calls
  56. kAbandon,
  57. // Free allocated resources (not known by GrResourceCache) before returning and
  58. // ensure no backend backend 3D API calls will be made after disconnect() returns.
  59. kCleanup,
  60. };
  61. // Called by GrContext when the underlying backend context is already or will be destroyed
  62. // before GrContext.
  63. virtual void disconnect(DisconnectType);
  64. /**
  65. * The GrGpu object normally assumes that no outsider is setting state
  66. * within the underlying 3D API's context/device/whatever. This call informs
  67. * the GrGpu that the state was modified and it shouldn't make assumptions
  68. * about the state.
  69. */
  70. void markContextDirty(uint32_t state = kAll_GrBackendState) { fResetBits |= state; }
  71. /**
  72. * Creates a texture object. If renderable is kYes then the returned texture can
  73. * be used as a render target by calling GrTexture::asRenderTarget(). Not all
  74. * pixel configs can be used as render targets. Support for configs as textures
  75. * or render targets can be checked using GrCaps.
  76. *
  77. * @param desc describes the texture to be created.
  78. * @param renderable should the resulting texture be renderable
  79. * @param renderTargetSampleCnt The number of samples to use for rendering if renderable is
  80. * kYes. If renderable is kNo then this must be 1.
  81. * @param budgeted does this texture count against the resource cache budget?
  82. * @param isProtected should the texture be created as protected.
  83. * @param texels array of mipmap levels containing texel data to load.
  84. * If level i has pixels then it is assumed that its dimensions are
  85. * max(1, floor(desc.fWidth / 2)) by max(1, floor(desc.fHeight / 2)).
  86. * If texels[i].fPixels == nullptr for all i <= mipLevelCount or
  87. * mipLevelCount is 0 then the texture's contents are uninitialized.
  88. * If a level has non-null pixels, its row bytes must be a multiple of the
  89. * config's bytes-per-pixel. The row bytes must be tight to the
  90. * level width if !caps->writePixelsRowBytesSupport().
  91. * If mipLevelCount > 1 and texels[i].fPixels != nullptr for any i > 0
  92. * then all levels must have non-null pixels. All levels must have
  93. * non-null pixels if GrCaps::createTextureMustSpecifyAllLevels() is true.
  94. * @param mipLevelCount the number of levels in 'texels'. May be 0, 1, or
  95. * floor(max((log2(desc.fWidth), log2(desc.fHeight)))). It must be the
  96. * latter if GrCaps::createTextureMustSpecifyAllLevels() is true.
  97. * @return The texture object if successful, otherwise nullptr.
  98. */
  99. sk_sp<GrTexture> createTexture(const GrSurfaceDesc& desc, GrRenderable renderable,
  100. int renderTargetSampleCnt, SkBudgeted, GrProtected isProtected,
  101. const GrMipLevel texels[], int mipLevelCount);
  102. /**
  103. * Simplified createTexture() interface for when there is no initial texel data to upload.
  104. */
  105. sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, GrRenderable, int renderTargetSampleCnt,
  106. SkBudgeted, GrProtected);
  107. sk_sp<GrTexture> createCompressedTexture(int width, int height, SkImage::CompressionType,
  108. SkBudgeted, const void* data, size_t dataSize);
  109. /**
  110. * Implements GrResourceProvider::wrapBackendTexture
  111. */
  112. sk_sp<GrTexture> wrapBackendTexture(const GrBackendTexture&, GrColorType,
  113. GrWrapOwnership, GrWrapCacheable, GrIOType);
  114. /**
  115. * Implements GrResourceProvider::wrapRenderableBackendTexture
  116. */
  117. sk_sp<GrTexture> wrapRenderableBackendTexture(const GrBackendTexture&, int sampleCnt,
  118. GrColorType, GrWrapOwnership, GrWrapCacheable);
  119. /**
  120. * Implements GrResourceProvider::wrapBackendRenderTarget
  121. */
  122. sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTarget&,
  123. GrColorType colorType);
  124. /**
  125. * Implements GrResourceProvider::wrapBackendTextureAsRenderTarget
  126. */
  127. sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTexture&,
  128. int sampleCnt,
  129. GrColorType colorType);
  130. /**
  131. * Implements GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget
  132. */
  133. sk_sp<GrRenderTarget> wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
  134. const GrVkDrawableInfo&);
  135. /**
  136. * Creates a buffer in GPU memory. For a client-side buffer use GrBuffer::CreateCPUBacked.
  137. *
  138. * @param size size of buffer to create.
  139. * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
  140. * @param accessPattern hint to the graphics subsystem about how the data will be accessed.
  141. * @param data optional data with which to initialize the buffer.
  142. *
  143. * @return the buffer if successful, otherwise nullptr.
  144. */
  145. sk_sp<GrGpuBuffer> createBuffer(size_t size, GrGpuBufferType intendedType,
  146. GrAccessPattern accessPattern, const void* data = nullptr);
  147. /**
  148. * Resolves MSAA.
  149. */
  150. void resolveRenderTarget(GrRenderTarget*);
  151. /**
  152. * Uses the base of the texture to recompute the contents of the other levels.
  153. */
  154. bool regenerateMipMapLevels(GrTexture*);
  155. /**
  156. * If the backend API has stateful texture bindings, this resets them back to defaults.
  157. */
  158. void resetTextureBindings();
  159. /**
  160. * Reads a rectangle of pixels from a render target. No sRGB/linear conversions are performed.
  161. *
  162. * @param surface The surface to read from
  163. * @param left left edge of the rectangle to read (inclusive)
  164. * @param top top edge of the rectangle to read (inclusive)
  165. * @param width width of rectangle to read in pixels.
  166. * @param height height of rectangle to read in pixels.
  167. * @param dstColorType the color type of the destination buffer.
  168. * @param buffer memory to read the rectangle into.
  169. * @param rowBytes the number of bytes between consecutive rows. Must be a multiple of
  170. * dstColorType's bytes-per-pixel. Must be tight to width if
  171. * !caps->readPixelsRowBytesSupport().
  172. * @param invertY buffer should be populated bottom-to-top as opposed
  173. * to top-to-bottom (skia's usual order)
  174. *
  175. * @return true if the read succeeded, false if not. The read can fail
  176. * because of the surface doesn't support reading, the color type
  177. * is not allowed for the format of the surface or if the rectangle
  178. * read is not contained in the surface.
  179. */
  180. bool readPixels(GrSurface* surface, int left, int top, int width, int height,
  181. GrColorType dstColorType, void* buffer, size_t rowBytes);
  182. /**
  183. * Updates the pixels in a rectangle of a surface. No sRGB/linear conversions are performed.
  184. *
  185. * @param surface The surface to write to.
  186. * @param left left edge of the rectangle to write (inclusive)
  187. * @param top top edge of the rectangle to write (inclusive)
  188. * @param width width of rectangle to write in pixels.
  189. * @param height height of rectangle to write in pixels.
  190. * @param srcColorType the color type of the source buffer.
  191. * @param texels array of mipmap levels containing texture data. Row bytes must be a
  192. * multiple of srcColorType's bytes-per-pixel. Must be tight to level width
  193. * if !caps->writePixelsRowBytesSupport().
  194. * @param mipLevelCount number of levels in 'texels'
  195. *
  196. * @return true if the write succeeded, false if not. The read can fail
  197. * because of the surface doesn't support writing (e.g. read only),
  198. * the color type is not allowed for the format of the surface or
  199. * if the rectangle written is not contained in the surface.
  200. */
  201. bool writePixels(GrSurface* surface, int left, int top, int width, int height,
  202. GrColorType srcColorType, const GrMipLevel texels[], int mipLevelCount);
  203. /**
  204. * Helper for the case of a single level.
  205. */
  206. bool writePixels(GrSurface* surface, int left, int top, int width, int height,
  207. GrColorType srcColorType, const void* buffer, size_t rowBytes) {
  208. GrMipLevel mipLevel = {buffer, rowBytes};
  209. return this->writePixels(surface, left, top, width, height, srcColorType, &mipLevel, 1);
  210. }
  211. /**
  212. * Updates the pixels in a rectangle of a texture using a buffer. If the texture is MIP mapped,
  213. * the base level is written to.
  214. *
  215. * @param texture The texture to write to.
  216. * @param left left edge of the rectangle to write (inclusive)
  217. * @param top top edge of the rectangle to write (inclusive)
  218. * @param width width of rectangle to write in pixels.
  219. * @param height height of rectangle to write in pixels.
  220. * @param bufferColorType the color type of the transfer buffer's pixel data
  221. * @param transferBuffer GrBuffer to read pixels from (type must be "kXferCpuToGpu")
  222. * @param offset offset from the start of the buffer
  223. * @param rowBytes number of bytes between consecutive rows in the buffer. Must be a
  224. * multiple of bufferColorType's bytes-per-pixel. Must be tight to width
  225. * if !caps->writePixelsRowBytesSupport().
  226. */
  227. bool transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
  228. GrColorType bufferColorType, GrGpuBuffer* transferBuffer, size_t offset,
  229. size_t rowBytes);
  230. /**
  231. * Reads the pixels from a rectangle of a surface into a buffer. Use
  232. * GrCaps::transferFromOffsetAlignment to determine the requirements for the buffer offset
  233. * alignment. If the surface is a MIP mapped texture, the base level is read.
  234. *
  235. * If successful the row bytes in the buffer is always:
  236. * GrColorTypeBytesPerPixel(bufferColorType) * width
  237. *
  238. * Asserts that the caller has passed a properly aligned offset and that the buffer is
  239. * large enough to hold the result
  240. *
  241. * @param surface The surface to read from.
  242. * @param left left edge of the rectangle to read (inclusive)
  243. * @param top top edge of the rectangle to read (inclusive)
  244. * @param width width of rectangle to read in pixels.
  245. * @param height height of rectangle to read in pixels.
  246. * @param bufferColorType the color type of the transfer buffer's pixel data
  247. * @param transferBuffer GrBuffer to write pixels to (type must be "kXferGpuToCpu")
  248. * @param offset offset from the start of the buffer
  249. */
  250. bool transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
  251. GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
  252. size_t offset);
  253. // Called to perform a surface to surface copy. Fallbacks to issuing a draw from the src to dst
  254. // take place at the GrOpList level and this function implement faster copy paths. The rect
  255. // and point are pre-clipped. The src rect and implied dst rect are guaranteed to be within the
  256. // src/dst bounds and non-empty. They must also be in their exact device space coords, including
  257. // already being transformed for origin if need be. If canDiscardOutsideDstRect is set to true
  258. // then we don't need to preserve any data on the dst surface outside of the copy.
  259. bool copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
  260. const SkIPoint& dstPoint, bool canDiscardOutsideDstRect = false);
  261. // Queries the per-pixel HW sample locations for the given render target, and then finds or
  262. // assigns a key that uniquely identifies the sample pattern. The actual sample locations can be
  263. // retrieved with retrieveSampleLocations().
  264. int findOrAssignSamplePatternKey(GrRenderTarget*);
  265. // Retrieves the per-pixel HW sample locations for the given sample pattern key, and, as a
  266. // by-product, the actual number of samples in use. (This may differ from the number of samples
  267. // requested by the render target.) Sample locations are returned as 0..1 offsets relative to
  268. // the top-left corner of the pixel.
  269. const SkTArray<SkPoint>& retrieveSampleLocations(int samplePatternKey) const {
  270. return fSamplePatternDictionary.retrieveSampleLocations(samplePatternKey);
  271. }
  272. // Returns a GrGpuRTCommandBuffer which GrOpLists send draw commands to instead of directly
  273. // to the Gpu object. The 'bounds' rect is the content rect of the destination.
  274. virtual GrGpuRTCommandBuffer* getCommandBuffer(
  275. GrRenderTarget*, GrSurfaceOrigin, const SkRect& bounds,
  276. const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
  277. const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) = 0;
  278. // Returns a GrGpuTextureCommandBuffer which GrOpLists send texture commands to instead of
  279. // directly to the Gpu object.
  280. virtual GrGpuTextureCommandBuffer* getCommandBuffer(GrTexture*, GrSurfaceOrigin) = 0;
  281. // Called by GrDrawingManager when flushing.
  282. // Provides a hook for post-flush actions (e.g. Vulkan command buffer submits). This will also
  283. // insert any numSemaphore semaphores on the gpu and set the backendSemaphores to match the
  284. // inserted semaphores.
  285. GrSemaphoresSubmitted finishFlush(GrSurfaceProxy*[], int n,
  286. SkSurface::BackendSurfaceAccess access, const GrFlushInfo&,
  287. const GrPrepareForExternalIORequests&);
  288. virtual void submit(GrGpuCommandBuffer*) = 0;
  289. virtual GrFence SK_WARN_UNUSED_RESULT insertFence() = 0;
  290. virtual bool waitFence(GrFence, uint64_t timeout = 1000) = 0;
  291. virtual void deleteFence(GrFence) const = 0;
  292. virtual sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned = true) = 0;
  293. virtual sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
  294. GrResourceProvider::SemaphoreWrapType wrapType,
  295. GrWrapOwnership ownership) = 0;
  296. virtual void insertSemaphore(sk_sp<GrSemaphore> semaphore) = 0;
  297. virtual void waitSemaphore(sk_sp<GrSemaphore> semaphore) = 0;
  298. virtual void checkFinishProcs() = 0;
  299. /**
  300. * Put this texture in a safe and known state for use across multiple GrContexts. Depending on
  301. * the backend, this may return a GrSemaphore. If so, other contexts should wait on that
  302. * semaphore before using this texture.
  303. */
  304. virtual sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) = 0;
  305. ///////////////////////////////////////////////////////////////////////////
  306. // Debugging and Stats
  307. class Stats {
  308. public:
  309. #if GR_GPU_STATS
  310. Stats() = default;
  311. void reset() { *this = {}; }
  312. int renderTargetBinds() const { return fRenderTargetBinds; }
  313. void incRenderTargetBinds() { fRenderTargetBinds++; }
  314. int shaderCompilations() const { return fShaderCompilations; }
  315. void incShaderCompilations() { fShaderCompilations++; }
  316. int textureCreates() const { return fTextureCreates; }
  317. void incTextureCreates() { fTextureCreates++; }
  318. int textureUploads() const { return fTextureUploads; }
  319. void incTextureUploads() { fTextureUploads++; }
  320. int transfersToTexture() const { return fTransfersToTexture; }
  321. void incTransfersToTexture() { fTransfersToTexture++; }
  322. int transfersFromSurface() const { return fTransfersFromSurface; }
  323. void incTransfersFromSurface() { fTransfersFromSurface++; }
  324. int stencilAttachmentCreates() const { return fStencilAttachmentCreates; }
  325. void incStencilAttachmentCreates() { fStencilAttachmentCreates++; }
  326. int numDraws() const { return fNumDraws; }
  327. void incNumDraws() { fNumDraws++; }
  328. int numFailedDraws() const { return fNumFailedDraws; }
  329. void incNumFailedDraws() { ++fNumFailedDraws; }
  330. int numFinishFlushes() const { return fNumFinishFlushes; }
  331. void incNumFinishFlushes() { ++fNumFinishFlushes; }
  332. int numScratchTexturesReused() const { return fNumScratchTexturesReused; }
  333. void incNumScratchTexturesReused() { ++fNumScratchTexturesReused; }
  334. #if GR_TEST_UTILS
  335. void dump(SkString*);
  336. void dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values);
  337. #endif
  338. private:
  339. int fRenderTargetBinds = 0;
  340. int fShaderCompilations = 0;
  341. int fTextureCreates = 0;
  342. int fTextureUploads = 0;
  343. int fTransfersToTexture = 0;
  344. int fTransfersFromSurface = 0;
  345. int fStencilAttachmentCreates = 0;
  346. int fNumDraws = 0;
  347. int fNumFailedDraws = 0;
  348. int fNumFinishFlushes = 0;
  349. int fNumScratchTexturesReused = 0;
  350. #else
  351. #if GR_TEST_UTILS
  352. void dump(SkString*) {}
  353. void dumpKeyValuePairs(SkTArray<SkString>*, SkTArray<double>*) {}
  354. #endif
  355. void incRenderTargetBinds() {}
  356. void incShaderCompilations() {}
  357. void incTextureCreates() {}
  358. void incTextureUploads() {}
  359. void incTransfersToTexture() {}
  360. void incStencilAttachmentCreates() {}
  361. void incNumDraws() {}
  362. void incNumFailedDraws() {}
  363. void incNumFinishFlushes() {}
  364. #endif
  365. };
  366. Stats* stats() { return &fStats; }
  367. void dumpJSON(SkJSONWriter*) const;
  368. /**
  369. * Creates a texture directly in the backend API without wrapping it in a GrTexture.
  370. * Must be matched with a call to deleteBackendTexture().
  371. * Right now, the color is ignored if pixel data is provided.
  372. * In the future, if neither a color nor pixels are provided then the backend texture
  373. * will be uninitialized.
  374. */
  375. virtual GrBackendTexture createBackendTexture(int w, int h, const GrBackendFormat&,
  376. GrMipMapped, GrRenderable,
  377. const void* pixels, size_t rowBytes,
  378. const SkColor4f* color,
  379. GrProtected isProtected) = 0;
  380. /**
  381. * Frees a texture created by createBackendTexture(). If ownership of the backend
  382. * texture has been transferred to a GrContext using adopt semantics this should not be called.
  383. */
  384. virtual void deleteBackendTexture(const GrBackendTexture&) = 0;
  385. #if GR_TEST_UTILS
  386. /** Check a handle represents an actual texture in the backend API that has not been freed. */
  387. virtual bool isTestingOnlyBackendTexture(const GrBackendTexture&) const = 0;
  388. virtual GrBackendRenderTarget createTestingOnlyBackendRenderTarget(int w, int h,
  389. GrColorType) = 0;
  390. virtual void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) = 0;
  391. // This is only to be used in GL-specific tests.
  392. virtual const GrGLContext* glContextForTesting() const { return nullptr; }
  393. // This is only to be used by testing code
  394. virtual void resetShaderCacheForTesting() const {}
  395. /**
  396. * Flushes all work to the gpu and forces the GPU to wait until all the gpu work has completed.
  397. * This is for testing purposes only.
  398. */
  399. virtual void testingOnly_flushGpuAndSync() = 0;
  400. #endif
  401. // width and height may be larger than rt (if underlying API allows it).
  402. // Returns nullptr if compatible sb could not be created, otherwise the caller owns the ref on
  403. // the GrStencilAttachment.
  404. virtual GrStencilAttachment* createStencilAttachmentForRenderTarget(
  405. const GrRenderTarget*, int width, int height, int numStencilSamples) = 0;
  406. // Determines whether a texture will need to be rescaled in order to be used with the
  407. // GrSamplerState.
  408. static bool IsACopyNeededForRepeatWrapMode(const GrCaps*, GrTextureProxy* texProxy,
  409. int width, int height,
  410. GrSamplerState::Filter,
  411. GrTextureProducer::CopyParams*,
  412. SkScalar scaleAdjust[2]);
  413. // Determines whether a texture will need to be copied because the draw requires mips but the
  414. // texutre doesn't have any. This call should be only checked if IsACopyNeededForTextureParams
  415. // fails. If the previous call succeeds, then a copy should be done using those params and the
  416. // mip mapping requirements will be handled there.
  417. static bool IsACopyNeededForMips(const GrCaps* caps, const GrTextureProxy* texProxy,
  418. GrSamplerState::Filter filter,
  419. GrTextureProducer::CopyParams* copyParams);
  420. void handleDirtyContext() {
  421. if (fResetBits) {
  422. this->resetContext();
  423. }
  424. }
  425. /**
  426. * Returns a key that represents the sampler that will be created for the passed in parameters.
  427. * Currently this key is only used when we are building a vulkan pipeline with immutable
  428. * samplers. In that case, we need our cache key to also contain this key.
  429. *
  430. * A return value of 0 indicates that the program/pipeline we are creating is not affected by
  431. * the sampler.
  432. */
  433. virtual uint32_t getExtraSamplerKeyForProgram(const GrSamplerState&, const GrBackendFormat&) {
  434. return 0;
  435. }
  436. virtual void storeVkPipelineCacheData() {}
  437. protected:
  438. // Handles cases where a surface will be updated without a call to flushRenderTarget.
  439. void didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
  440. uint32_t mipLevels = 1) const;
  441. Stats fStats;
  442. std::unique_ptr<GrPathRendering> fPathRendering;
  443. // Subclass must initialize this in its constructor.
  444. sk_sp<const GrCaps> fCaps;
  445. private:
  446. // called when the 3D context state is unknown. Subclass should emit any
  447. // assumed 3D context state and dirty any state cache.
  448. virtual void onResetContext(uint32_t resetBits) = 0;
  449. // Implementation of resetTextureBindings.
  450. virtual void onResetTextureBindings() {}
  451. // Queries the effective number of samples in use by the hardware for the given render target,
  452. // and queries the individual sample locations.
  453. virtual void querySampleLocations(GrRenderTarget*, SkTArray<SkPoint>*) = 0;
  454. // Called before certain draws in order to guarantee coherent results from dst reads.
  455. virtual void xferBarrier(GrRenderTarget*, GrXferBarrierType) = 0;
  456. // overridden by backend-specific derived class to create objects.
  457. // Texture size and sample size will have already been validated in base class before
  458. // onCreateTexture is called.
  459. virtual sk_sp<GrTexture> onCreateTexture(const GrSurfaceDesc&, GrRenderable,
  460. int renderTargetSampleCnt, SkBudgeted, GrProtected,
  461. const GrMipLevel[], int mipLevelCount) = 0;
  462. virtual sk_sp<GrTexture> onCreateCompressedTexture(int width, int height,
  463. SkImage::CompressionType, SkBudgeted,
  464. const void* data) = 0;
  465. virtual sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, GrColorType,
  466. GrWrapOwnership, GrWrapCacheable, GrIOType) = 0;
  467. virtual sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&, int sampleCnt,
  468. GrColorType, GrWrapOwnership,
  469. GrWrapCacheable) = 0;
  470. virtual sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&,
  471. GrColorType) = 0;
  472. virtual sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&,
  473. int sampleCnt,
  474. GrColorType) = 0;
  475. virtual sk_sp<GrRenderTarget> onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
  476. const GrVkDrawableInfo&);
  477. virtual sk_sp<GrGpuBuffer> onCreateBuffer(size_t size, GrGpuBufferType intendedType,
  478. GrAccessPattern, const void* data) = 0;
  479. // overridden by backend-specific derived class to perform the surface read
  480. virtual bool onReadPixels(GrSurface*, int left, int top, int width, int height, GrColorType,
  481. void* buffer, size_t rowBytes) = 0;
  482. // overridden by backend-specific derived class to perform the surface write
  483. virtual bool onWritePixels(GrSurface*, int left, int top, int width, int height, GrColorType,
  484. const GrMipLevel texels[], int mipLevelCount) = 0;
  485. // overridden by backend-specific derived class to perform the texture transfer
  486. virtual bool onTransferPixelsTo(GrTexture*, int left, int top, int width, int height,
  487. GrColorType colorType, GrGpuBuffer* transferBuffer,
  488. size_t offset, size_t rowBytes) = 0;
  489. // overridden by backend-specific derived class to perform the surface transfer
  490. virtual bool onTransferPixelsFrom(GrSurface*, int left, int top, int width, int height,
  491. GrColorType colorType, GrGpuBuffer* transferBuffer,
  492. size_t offset) = 0;
  493. // overridden by backend-specific derived class to perform the resolve
  494. virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
  495. // overridden by backend specific derived class to perform mip map level regeneration.
  496. virtual bool onRegenerateMipMapLevels(GrTexture*) = 0;
  497. // overridden by backend specific derived class to perform the copy surface
  498. virtual bool onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
  499. const SkIPoint& dstPoint, bool canDiscardOutsideDstRect) = 0;
  500. virtual void onFinishFlush(GrSurfaceProxy*[], int n, SkSurface::BackendSurfaceAccess access,
  501. const GrFlushInfo&, const GrPrepareForExternalIORequests&) = 0;
  502. #ifdef SK_ENABLE_DUMP_GPU
  503. virtual void onDumpJSON(SkJSONWriter*) const {}
  504. #endif
  505. void resetContext() {
  506. this->onResetContext(fResetBits);
  507. fResetBits = 0;
  508. }
  509. uint32_t fResetBits;
  510. // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu.
  511. GrContext* fContext;
  512. GrSamplePatternDictionary fSamplePatternDictionary;
  513. friend class GrPathRendering;
  514. typedef SkRefCnt INHERITED;
  515. };
  516. #endif