GrSurfaceContext.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright 2016 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 GrSurfaceContext_DEFINED
  8. #define GrSurfaceContext_DEFINED
  9. #include "include/core/SkFilterQuality.h"
  10. #include "include/core/SkImageInfo.h"
  11. #include "include/core/SkRect.h"
  12. #include "include/core/SkRefCnt.h"
  13. #include "include/core/SkSurface.h"
  14. #include "src/gpu/GrColorSpaceInfo.h"
  15. #include "src/gpu/GrDataUtils.h"
  16. #include "src/gpu/GrSurfaceProxy.h"
  17. class GrAuditTrail;
  18. class GrDrawingManager;
  19. class GrOpList;
  20. class GrRecordingContext;
  21. class GrRenderTargetContext;
  22. class GrRenderTargetProxy;
  23. class GrSingleOwner;
  24. class GrSurface;
  25. class GrSurfaceContextPriv;
  26. class GrSurfaceProxy;
  27. class GrTextureProxy;
  28. struct SkIPoint;
  29. struct SkIRect;
  30. /**
  31. * A helper object to orchestrate commands for a particular surface
  32. */
  33. class SK_API GrSurfaceContext : public SkRefCnt {
  34. public:
  35. ~GrSurfaceContext() override {}
  36. const GrColorSpaceInfo& colorSpaceInfo() const { return fColorSpaceInfo; }
  37. // TODO: these two calls would be way cooler if this object had a GrSurfaceProxy pointer
  38. int width() const { return this->asSurfaceProxy()->width(); }
  39. int height() const { return this->asSurfaceProxy()->height(); }
  40. /**
  41. * Reads a rectangle of pixels from the render target context.
  42. * @param dstInfo image info for the destination
  43. * @param dst destination pixels for the read
  44. * @param rowBytes bytes in a row of 'dst'
  45. * @param srcPt offset w/in the surface context from which to read
  46. * @param direct The direct context to use. If null will use our GrRecordingContext if it
  47. * is a GrDirectContext and fail otherwise.
  48. */
  49. bool readPixels(const GrPixelInfo& dstInfo, void* dst, size_t rowBytes, SkIPoint srcPt,
  50. GrContext* direct = nullptr);
  51. /**
  52. * Writes a rectangle of pixels [srcInfo, srcBuffer, srcRowbytes] into the
  53. * renderTargetContext at the specified position.
  54. * @param srcInfo image info for the source pixels
  55. * @param src source for the write
  56. * @param rowBytes bytes in a row of 'src'
  57. * @param dstPt offset w/in the surface context at which to write
  58. * @param direct The direct context to use. If null will use our GrRecordingContext if it
  59. * is a GrDirectContext and fail otherwise.
  60. */
  61. bool writePixels(const GrPixelInfo& srcInfo, const void* src, size_t rowBytes, SkIPoint dstPt,
  62. GrContext* direct = nullptr);
  63. // TODO: this is virtual b.c. this object doesn't have a pointer to the wrapped GrSurfaceProxy?
  64. virtual GrSurfaceProxy* asSurfaceProxy() = 0;
  65. virtual const GrSurfaceProxy* asSurfaceProxy() const = 0;
  66. virtual sk_sp<GrSurfaceProxy> asSurfaceProxyRef() = 0;
  67. virtual GrTextureProxy* asTextureProxy() = 0;
  68. virtual const GrTextureProxy* asTextureProxy() const = 0;
  69. virtual sk_sp<GrTextureProxy> asTextureProxyRef() = 0;
  70. virtual GrRenderTargetProxy* asRenderTargetProxy() = 0;
  71. virtual sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() = 0;
  72. virtual GrRenderTargetContext* asRenderTargetContext() { return nullptr; }
  73. GrAuditTrail* auditTrail();
  74. // Provides access to functions that aren't part of the public API.
  75. GrSurfaceContextPriv surfPriv();
  76. const GrSurfaceContextPriv surfPriv() const;
  77. #if GR_TEST_UTILS
  78. bool testCopy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
  79. return this->copy(src, srcRect, dstPoint);
  80. }
  81. bool testCopy(GrSurfaceProxy* src) {
  82. return this->copy(src);
  83. }
  84. #endif
  85. protected:
  86. friend class GrSurfaceContextPriv;
  87. GrSurfaceContext(GrRecordingContext*, GrColorType, SkAlphaType, sk_sp<SkColorSpace>);
  88. GrDrawingManager* drawingManager();
  89. const GrDrawingManager* drawingManager() const;
  90. virtual GrOpList* getOpList() = 0;
  91. SkDEBUGCODE(virtual void validate() const = 0;)
  92. SkDEBUGCODE(GrSingleOwner* singleOwner();)
  93. GrRecordingContext* fContext;
  94. // The rescaling step of asyncRescaleAndReadPixels[YUV420]().
  95. sk_sp<GrRenderTargetContext> rescale(const SkImageInfo& info, const SkIRect& srcRect,
  96. SkSurface::RescaleGamma rescaleGamma,
  97. SkFilterQuality rescaleQuality);
  98. private:
  99. friend class GrSurfaceProxy; // for copy
  100. /**
  101. * Copy 'src' into the proxy backing this context. This call will not do any draw fallback.
  102. * Currently only writePixels and replaceRenderTarget call this directly. All other copies
  103. * should go through GrSurfaceProxy::Copy.
  104. * @param src src of pixels
  105. * @param srcRect the subset of 'src' to copy
  106. * @param dstPoint the origin of the 'srcRect' in the destination coordinate space
  107. * @return true if the copy succeeded; false otherwise
  108. *
  109. * Note: Notionally, 'srcRect' is clipped to 'src's extent with 'dstPoint' being adjusted.
  110. * Then the 'srcRect' offset by 'dstPoint' is clipped against the dst's extent.
  111. * The end result is only valid src pixels and dst pixels will be touched but the copied
  112. * regions will not be shifted. The 'src' must have the same origin as the backing proxy
  113. * of fSurfaceContext.
  114. */
  115. bool copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
  116. bool copy(GrSurfaceProxy* src) {
  117. return this->copy(src, SkIRect::MakeWH(src->width(), src->height()), SkIPoint::Make(0, 0));
  118. }
  119. GrColorSpaceInfo fColorSpaceInfo;
  120. typedef SkRefCnt INHERITED;
  121. };
  122. #endif