SkDeferredDisplayListRecorder.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 SkDeferredDisplayListMaker_DEFINED
  8. #define SkDeferredDisplayListMaker_DEFINED
  9. #include "SkImageInfo.h"
  10. #include "SkRefCnt.h"
  11. #include "SkSurfaceCharacterization.h"
  12. #include "SkTypes.h"
  13. #include "../private/SkDeferredDisplayList.h"
  14. class GrBackendFormat;
  15. class GrBackendTexture;
  16. class GrContext;
  17. class SkCanvas;
  18. class SkImage;
  19. class SkSurface;
  20. struct SkYUVAIndex;
  21. struct SkYUVASizeInfo;
  22. /*
  23. * This class is intended to be used as:
  24. * Get an SkSurfaceCharacterization representing the intended gpu-backed destination SkSurface
  25. * Create one of these (an SkDDLMaker) on the stack
  26. * Get the canvas and render into it
  27. * Snap off and hold on to an SkDeferredDisplayList
  28. * Once your app actually needs the pixels, call SkSurface::draw(SkDeferredDisplayList*)
  29. *
  30. * This class never accesses the GPU but performs all the cpu work it can. It
  31. * is thread-safe (i.e., one can break a scene into tiles and perform their cpu-side
  32. * work in parallel ahead of time).
  33. */
  34. class SK_API SkDeferredDisplayListRecorder {
  35. public:
  36. SkDeferredDisplayListRecorder(const SkSurfaceCharacterization&);
  37. ~SkDeferredDisplayListRecorder();
  38. const SkSurfaceCharacterization& characterization() const {
  39. return fCharacterization;
  40. }
  41. // The backing canvas will become invalid (and this entry point will return
  42. // null) once 'detach' is called.
  43. // Note: ownership of the SkCanvas is not transfered via this call.
  44. SkCanvas* getCanvas();
  45. std::unique_ptr<SkDeferredDisplayList> detach();
  46. // Matches the defines in SkImage_GpuBase.h
  47. typedef void* TextureContext;
  48. typedef void (*TextureReleaseProc)(TextureContext textureContext);
  49. typedef void (*TextureFulfillProc)(TextureContext textureContext, GrBackendTexture* outTexture);
  50. typedef void (*PromiseDoneProc)(TextureContext textureContext);
  51. /**
  52. Create a new SkImage that is very similar to an SkImage created by MakeFromTexture. The main
  53. difference is that the client doesn't have the backend texture on the gpu yet but they know
  54. all the properties of the texture. So instead of passing in a GrBackendTexture the client
  55. supplies a GrBackendFormat, width, height, and GrMipMapped state.
  56. When we actually send the draw calls to the GPU, we will call the textureFulfillProc and
  57. the client will return a GrBackendTexture to us. The properties of the GrBackendTexture must
  58. match those set during the SkImage creation, and it must have a valid backend gpu texture.
  59. The gpu texture supplied by the client must stay valid until we call the textureReleaseProc.
  60. When we are done with the texture returned by the textureFulfillProc we will call the
  61. textureReleaseProc passing in the textureContext. This is a signal to the client that they
  62. are free to delete the underlying gpu texture. If future draws also use the same promise
  63. image we will call the textureFulfillProc again if we've already called the
  64. textureReleaseProc. We will always call textureFulfillProc and textureReleaseProc in pairs.
  65. In other words we will never call textureFulfillProc or textureReleaseProc multiple times
  66. for the same textureContext before calling the other.
  67. We call the promiseDoneProc when we will no longer call the textureFulfillProc again. We
  68. pass in the textureContext as a parameter to the promiseDoneProc. We also guarantee that
  69. there will be no outstanding textureReleaseProcs that still need to be called when we call
  70. the textureDoneProc. Thus when the textureDoneProc gets called the client is able to cleanup
  71. all GPU objects and meta data needed for the textureFulfill call.
  72. This call is only valid if the SkDeferredDisplayListRecorder is backed by a gpu context.
  73. @param backendFormat format of promised gpu texture
  74. @param width width of promised gpu texture
  75. @param height height of promised gpu texture
  76. @param mipMapped mip mapped state of promised gpu texture
  77. @param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin
  78. @param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
  79. kRGB_565_SkColorType, kARGB_4444_SkColorType,
  80. kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
  81. kGray_8_SkColorType, kRGBA_F16_SkColorType
  82. @param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
  83. kPremul_SkAlphaType, kUnpremul_SkAlphaType
  84. @param colorSpace range of colors; may be nullptr
  85. @param textureFulfillProc function called to get actual gpu texture
  86. @param textureReleaseProc function called when texture can be released
  87. @param promiseDoneProc function called when we will no longer call textureFulfillProc
  88. @param textureContext state passed to textureFulfillProc and textureReleaseProc
  89. @return created SkImage, or nullptr
  90. */
  91. sk_sp<SkImage> makePromiseTexture(const GrBackendFormat& backendFormat,
  92. int width,
  93. int height,
  94. GrMipMapped mipMapped,
  95. GrSurfaceOrigin origin,
  96. SkColorType colorType,
  97. SkAlphaType alphaType,
  98. sk_sp<SkColorSpace> colorSpace,
  99. TextureFulfillProc textureFulfillProc,
  100. TextureReleaseProc textureReleaseProc,
  101. PromiseDoneProc promiseDoneProc,
  102. TextureContext textureContext);
  103. /**
  104. This entry point operates the same as 'makePromiseTexture' except that its
  105. textureFulfillProc can be called up to four times to fetch the required YUVA
  106. planes (passing a different textureContext to each call). So, if the 'yuvaIndices'
  107. indicate that only the first two backend textures are used, 'textureFulfillProc' will
  108. be called with the first two 'textureContexts'.
  109. */
  110. sk_sp<SkImage> makeYUVAPromiseTexture(SkYUVColorSpace yuvColorSpace,
  111. const GrBackendFormat yuvaFormats[],
  112. const SkISize yuvaSizes[],
  113. const SkYUVAIndex yuvaIndices[4],
  114. int imageWidth,
  115. int imageHeight,
  116. GrSurfaceOrigin imageOrigin,
  117. sk_sp<SkColorSpace> imageColorSpace,
  118. TextureFulfillProc textureFulfillProc,
  119. TextureReleaseProc textureReleaseProc,
  120. PromiseDoneProc promiseDoneProc,
  121. TextureContext textureContexts[]);
  122. // deprecated version that doesn't take yuvaSizeInfo
  123. sk_sp<SkImage> makeYUVAPromiseTexture(SkYUVColorSpace yuvColorSpace,
  124. const GrBackendFormat yuvaFormats[],
  125. const SkYUVAIndex yuvaIndices[4],
  126. int imageWidth,
  127. int imageHeight,
  128. GrSurfaceOrigin imageOrigin,
  129. sk_sp<SkColorSpace> imageColorSpace,
  130. TextureFulfillProc textureFulfillProc,
  131. TextureReleaseProc textureReleaseProc,
  132. PromiseDoneProc promiseDoneProc,
  133. TextureContext textureContexts[]);
  134. private:
  135. bool init();
  136. const SkSurfaceCharacterization fCharacterization;
  137. #if SK_SUPPORT_GPU
  138. sk_sp<GrContext> fContext;
  139. sk_sp<SkDeferredDisplayList::LazyProxyData> fLazyProxyData;
  140. sk_sp<SkSurface> fSurface;
  141. #endif
  142. };
  143. #endif