SkGpuDevice.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright 2010 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 SkGpuDevice_DEFINED
  8. #define SkGpuDevice_DEFINED
  9. #include "include/core/SkBitmap.h"
  10. #include "include/core/SkPicture.h"
  11. #include "include/core/SkRegion.h"
  12. #include "include/core/SkSurface.h"
  13. #include "include/gpu/GrContext.h"
  14. #include "include/gpu/GrTypes.h"
  15. #include "src/core/SkClipStackDevice.h"
  16. #include "src/gpu/GrClipStackClip.h"
  17. #include "src/gpu/GrContextPriv.h"
  18. #include "src/gpu/GrRenderTargetContext.h"
  19. #include "src/gpu/SkGr.h"
  20. class GrAccelData;
  21. class GrTextureMaker;
  22. class GrTextureProducer;
  23. struct GrCachedLayer;
  24. class SkSpecialImage;
  25. /**
  26. * Subclass of SkBaseDevice, which directs all drawing to the GrGpu owned by the
  27. * canvas.
  28. */
  29. class SkGpuDevice : public SkClipStackDevice {
  30. public:
  31. enum InitContents {
  32. kClear_InitContents,
  33. kUninit_InitContents
  34. };
  35. /**
  36. * Creates an SkGpuDevice from a GrRenderTargetContext whose backing width/height is
  37. * different than its actual width/height (e.g., approx-match scratch texture).
  38. */
  39. static sk_sp<SkGpuDevice> Make(GrContext*, sk_sp<GrRenderTargetContext> renderTargetContext,
  40. int width, int height, InitContents);
  41. /**
  42. * New device that will create an offscreen renderTarget based on the ImageInfo and
  43. * sampleCount. The mipMapped flag tells the gpu to create the underlying render target with
  44. * mips. The Budgeted param controls whether the device's backing store counts against the
  45. * resource cache budget. On failure, returns nullptr.
  46. * This entry point creates a kExact backing store. It is used when creating SkGpuDevices
  47. * for SkSurfaces.
  48. */
  49. static sk_sp<SkGpuDevice> Make(GrContext*, SkBudgeted, const SkImageInfo&,
  50. int sampleCount, GrSurfaceOrigin, const SkSurfaceProps*,
  51. GrMipMapped mipMapped, InitContents);
  52. ~SkGpuDevice() override {}
  53. GrContext* context() const override { return fContext.get(); }
  54. // set all pixels to 0
  55. void clearAll();
  56. void replaceRenderTargetContext(bool shouldRetainContent);
  57. void replaceRenderTargetContext(sk_sp<GrRenderTargetContext>, bool shouldRetainContent);
  58. GrRenderTargetContext* accessRenderTargetContext() override;
  59. void drawPaint(const SkPaint& paint) override;
  60. void drawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint[],
  61. const SkPaint& paint) override;
  62. void drawRect(const SkRect& r, const SkPaint& paint) override;
  63. void drawRRect(const SkRRect& r, const SkPaint& paint) override;
  64. void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) override;
  65. void drawRegion(const SkRegion& r, const SkPaint& paint) override;
  66. void drawOval(const SkRect& oval, const SkPaint& paint) override;
  67. void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
  68. bool useCenter, const SkPaint& paint) override;
  69. void drawPath(const SkPath& path, const SkPaint& paint, bool pathIsMutable) override;
  70. void drawBitmapRect(const SkBitmap&, const SkRect* srcOrNull, const SkRect& dst,
  71. const SkPaint& paint, SkCanvas::SrcRectConstraint) override;
  72. void drawSprite(const SkBitmap& bitmap, int x, int y,
  73. const SkPaint& paint) override;
  74. void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override;
  75. void drawVertices(const SkVertices*, const SkVertices::Bone bones[], int boneCount, SkBlendMode,
  76. const SkPaint&) override;
  77. void drawShadow(const SkPath&, const SkDrawShadowRec&) override;
  78. void drawAtlas(const SkImage* atlas, const SkRSXform[], const SkRect[],
  79. const SkColor[], int count, SkBlendMode, const SkPaint&) override;
  80. void drawDevice(SkBaseDevice*, int x, int y, const SkPaint&) override;
  81. void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
  82. const SkPaint&, SkCanvas::SrcRectConstraint) override;
  83. void drawImageNine(const SkImage* image, const SkIRect& center,
  84. const SkRect& dst, const SkPaint& paint) override;
  85. void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
  86. const SkRect& dst, const SkPaint& paint) override;
  87. void drawImageLattice(const SkImage*, const SkCanvas::Lattice&,
  88. const SkRect& dst, const SkPaint&) override;
  89. void drawBitmapLattice(const SkBitmap&, const SkCanvas::Lattice&,
  90. const SkRect& dst, const SkPaint&) override;
  91. void drawDrawable(SkDrawable*, const SkMatrix*, SkCanvas* canvas) override;
  92. void drawSpecial(SkSpecialImage*, int left, int top, const SkPaint& paint,
  93. SkImage*, const SkMatrix&) override;
  94. void drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
  95. SkCanvas::QuadAAFlags aaFlags, SkColor color, SkBlendMode mode) override;
  96. void drawEdgeAAImageSet(const SkCanvas::ImageSetEntry[], int count, const SkPoint dstClips[],
  97. const SkMatrix[], const SkPaint&, SkCanvas::SrcRectConstraint) override;
  98. sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
  99. sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
  100. sk_sp<SkSpecialImage> snapSpecial() override;
  101. sk_sp<SkSpecialImage> snapBackImage(const SkIRect&) override;
  102. void flush() override;
  103. GrSemaphoresSubmitted flush(SkSurface::BackendSurfaceAccess access, const GrFlushInfo&);
  104. bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores);
  105. bool onAccessPixels(SkPixmap*) override;
  106. protected:
  107. bool onReadPixels(const SkPixmap&, int, int) override;
  108. bool onWritePixels(const SkPixmap&, int, int) override;
  109. private:
  110. // We want these unreffed in RenderTargetContext, GrContext order.
  111. sk_sp<GrContext> fContext;
  112. sk_sp<GrRenderTargetContext> fRenderTargetContext;
  113. SkISize fSize;
  114. enum Flags {
  115. kNeedClear_Flag = 1 << 0, //!< Surface requires an initial clear
  116. kIsOpaque_Flag = 1 << 1, //!< Hint from client that rendering to this device will be
  117. // opaque even if the config supports alpha.
  118. };
  119. static bool CheckAlphaTypeAndGetFlags(const SkImageInfo* info, InitContents init,
  120. unsigned* flags);
  121. SkGpuDevice(GrContext*, sk_sp<GrRenderTargetContext>, int width, int height, unsigned flags);
  122. SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
  123. sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
  124. SkImageFilterCache* getImageFilterCache() override;
  125. bool forceConservativeRasterClip() const override { return true; }
  126. GrClipStackClip clip() const { return GrClipStackClip(&this->cs()); }
  127. const GrCaps* caps() const;
  128. /**
  129. * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's
  130. * matrix, clip, and the device's render target has already been set on GrContext.
  131. */
  132. // The tileSize and clippedSrcRect will be valid only if true is returned.
  133. bool shouldTileImageID(uint32_t imageID,
  134. const SkIRect& imageRect,
  135. const SkMatrix& viewMatrix,
  136. const SkMatrix& srcToDstRectMatrix,
  137. const GrSamplerState& params,
  138. const SkRect* srcRectPtr,
  139. int maxTileSize,
  140. int* tileSize,
  141. SkIRect* clippedSubset) const;
  142. // Just returns the predicate, not the out-tileSize or out-clippedSubset, as they are not
  143. // needed at the moment.
  144. bool shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
  145. SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
  146. const SkMatrix& viewMatrix, const SkMatrix& srcToDstRect) const;
  147. sk_sp<SkSpecialImage> filterTexture(SkSpecialImage*,
  148. int left, int top,
  149. SkIPoint* offset,
  150. const SkImageFilter* filter);
  151. // Splits bitmap into tiles of tileSize and draws them using separate textures for each tile.
  152. void drawTiledBitmap(const SkBitmap& bitmap,
  153. const SkMatrix& viewMatrix,
  154. const SkMatrix& srcToDstMatrix,
  155. const SkRect& srcRect,
  156. const SkIRect& clippedSrcRect,
  157. const GrSamplerState& params,
  158. const SkPaint& paint,
  159. SkCanvas::SrcRectConstraint,
  160. int tileSize,
  161. bool bicubic);
  162. // Used by drawTiledBitmap to draw each tile.
  163. void drawBitmapTile(const SkBitmap&,
  164. const SkMatrix& viewMatrix,
  165. const SkRect& dstRect,
  166. const SkRect& srcRect,
  167. const GrSamplerState& samplerState,
  168. const SkPaint& paint,
  169. SkCanvas::SrcRectConstraint,
  170. bool bicubic,
  171. bool needsTextureDomain);
  172. // If not null, dstClip must be contained inside dst and will also respect the edge AA flags.
  173. // If 'preViewMatrix' is not null, final CTM will be this->ctm() * preViewMatrix.
  174. void drawImageQuad(const SkImage*, const SkRect* src, const SkRect* dst,
  175. const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
  176. const SkMatrix* preViewMatrix, const SkPaint&, SkCanvas::SrcRectConstraint);
  177. // TODO(michaelludwig): This can be removed once drawBitmapRect is removed from SkDevice
  178. // so that drawImageQuad is the sole entry point into the draw-single-image op
  179. void drawTextureProducer(GrTextureProducer*,
  180. const SkRect* srcRect,
  181. const SkRect* dstRect,
  182. SkCanvas::SrcRectConstraint,
  183. const SkMatrix& viewMatrix,
  184. const SkPaint&,
  185. bool attemptDrawTexture);
  186. void drawProducerLattice(GrTextureProducer*, std::unique_ptr<SkLatticeIter>, const SkRect& dst,
  187. const SkPaint&);
  188. void drawStrokedLine(const SkPoint pts[2], const SkPaint&);
  189. void wireframeVertices(SkVertices::VertexMode, int vertexCount, const SkPoint verts[],
  190. const SkVertices::Bone bones[], int boneCount, SkBlendMode,
  191. const uint16_t indices[], int indexCount, const SkPaint&);
  192. static sk_sp<GrRenderTargetContext> MakeRenderTargetContext(GrContext*,
  193. SkBudgeted,
  194. const SkImageInfo&,
  195. int sampleCount,
  196. GrSurfaceOrigin,
  197. const SkSurfaceProps*,
  198. GrMipMapped);
  199. friend class GrAtlasTextContext;
  200. friend class SkSurface_Gpu; // for access to surfaceProps
  201. typedef SkClipStackDevice INHERITED;
  202. };
  203. #endif