SkSurface_Base.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2012 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 SkSurface_Base_DEFINED
  8. #define SkSurface_Base_DEFINED
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkSurface.h"
  11. #include "src/core/SkImagePriv.h"
  12. #include "src/core/SkSurfacePriv.h"
  13. class SkSurface_Base : public SkSurface {
  14. public:
  15. SkSurface_Base(int width, int height, const SkSurfaceProps*);
  16. SkSurface_Base(const SkImageInfo&, const SkSurfaceProps*);
  17. virtual ~SkSurface_Base();
  18. virtual GrBackendTexture onGetBackendTexture(BackendHandleAccess);
  19. virtual GrBackendRenderTarget onGetBackendRenderTarget(BackendHandleAccess);
  20. virtual bool onReplaceBackendTexture(const GrBackendTexture&,
  21. GrSurfaceOrigin,
  22. TextureReleaseProc,
  23. ReleaseContext);
  24. /**
  25. * Allocate a canvas that will draw into this surface. We will cache this
  26. * canvas, to return the same object to the caller multiple times. We
  27. * take ownership, and will call unref() on the canvas when we go out of
  28. * scope.
  29. */
  30. virtual SkCanvas* onNewCanvas() = 0;
  31. virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&) = 0;
  32. /**
  33. * Allocate an SkImage that represents the current contents of the surface.
  34. * This needs to be able to outlive the surface itself (if need be), and
  35. * must faithfully represent the current contents, even if the surface
  36. * is changed after this called (e.g. it is drawn to via its canvas).
  37. *
  38. * If a subset is specified, the the impl must make a copy, rather than try to wait
  39. * on copy-on-write.
  40. */
  41. virtual sk_sp<SkImage> onNewImageSnapshot(const SkIRect* subset = nullptr) { return nullptr; }
  42. virtual void onWritePixels(const SkPixmap&, int x, int y) = 0;
  43. /**
  44. * Default implementation does a rescale/read and then calls the callback.
  45. */
  46. virtual void onAsyncRescaleAndReadPixels(const SkImageInfo& info, const SkIRect& srcRect,
  47. RescaleGamma rescaleGamma,
  48. SkFilterQuality rescaleQuality,
  49. ReadPixelsCallback callback,
  50. ReadPixelsContext context);
  51. /**
  52. * Default implementation does a rescale/read/yuv conversion and then calls the callback.
  53. */
  54. virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
  55. sk_sp<SkColorSpace> dstColorSpace,
  56. const SkIRect& srcRect, int dstW, int dstH,
  57. RescaleGamma rescaleGamma,
  58. SkFilterQuality rescaleQuality,
  59. ReadPixelsCallbackYUV420 callback,
  60. ReadPixelsContext context);
  61. /**
  62. * Default implementation:
  63. *
  64. * image = this->newImageSnapshot();
  65. * if (image) {
  66. * image->draw(canvas, ...);
  67. * image->unref();
  68. * }
  69. */
  70. virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
  71. /**
  72. * Called as a performance hint when the Surface is allowed to make it's contents
  73. * undefined.
  74. */
  75. virtual void onDiscard() {}
  76. /**
  77. * If the surface is about to change, we call this so that our subclass
  78. * can optionally fork their backend (copy-on-write) in case it was
  79. * being shared with the cachedImage.
  80. */
  81. virtual void onCopyOnWrite(ContentChangeMode) = 0;
  82. /**
  83. * Signal the surface to remind its backing store that it's mutable again.
  84. * Called only when we _didn't_ copy-on-write; we assume the copies start mutable.
  85. */
  86. virtual void onRestoreBackingMutability() {}
  87. /**
  88. * Issue any pending surface IO to the current backend 3D API and resolve any surface MSAA.
  89. * Inserts the requested number of semaphores for the gpu to signal when work is complete on the
  90. * gpu and inits the array of GrBackendSemaphores with the signaled semaphores.
  91. */
  92. virtual GrSemaphoresSubmitted onFlush(BackendSurfaceAccess access, const GrFlushInfo&) {
  93. return GrSemaphoresSubmitted::kNo;
  94. }
  95. /**
  96. * Caused the current backend 3D API to wait on the passed in semaphores before executing new
  97. * commands on the gpu. Any previously submitting commands will not be blocked by these
  98. * semaphores.
  99. */
  100. virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
  101. return false;
  102. }
  103. virtual bool onCharacterize(SkSurfaceCharacterization*) const { return false; }
  104. virtual bool onIsCompatible(const SkSurfaceCharacterization&) const { return false; }
  105. virtual bool onDraw(const SkDeferredDisplayList*) { return false; }
  106. inline SkCanvas* getCachedCanvas();
  107. inline sk_sp<SkImage> refCachedImage();
  108. bool hasCachedImage() const { return fCachedImage != nullptr; }
  109. // called by SkSurface to compute a new genID
  110. uint32_t newGenerationID();
  111. private:
  112. std::unique_ptr<SkCanvas> fCachedCanvas;
  113. sk_sp<SkImage> fCachedImage;
  114. void aboutToDraw(ContentChangeMode mode);
  115. // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw
  116. // would trigger a copy-on-write.
  117. bool outstandingImageSnapshot() const;
  118. friend class SkCanvas;
  119. friend class SkSurface;
  120. typedef SkSurface INHERITED;
  121. };
  122. SkCanvas* SkSurface_Base::getCachedCanvas() {
  123. if (nullptr == fCachedCanvas) {
  124. fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas());
  125. if (fCachedCanvas) {
  126. fCachedCanvas->setSurfaceBase(this);
  127. }
  128. }
  129. return fCachedCanvas.get();
  130. }
  131. sk_sp<SkImage> SkSurface_Base::refCachedImage() {
  132. if (fCachedImage) {
  133. return fCachedImage;
  134. }
  135. fCachedImage = this->onNewImageSnapshot();
  136. SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
  137. return fCachedImage;
  138. }
  139. #endif