SkSurface.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. /* Generated by tools/bookmaker from include/core/SkSurface.h and docs/SkSurface_Reference.bmh
  8. on 2018-09-13 13:59:55. Additional documentation and examples can be found at:
  9. https://skia.org/user/api/SkSurface_Reference
  10. You may edit either file directly. Structural changes to public interfaces require
  11. editing both files. After editing docs/SkSurface_Reference.bmh, run:
  12. bookmaker -b docs -i include/core/SkSurface.h -p
  13. to create an updated version of this file.
  14. */
  15. #ifndef SkSurface_DEFINED
  16. #define SkSurface_DEFINED
  17. #include "SkRefCnt.h"
  18. #include "SkImage.h"
  19. #include "SkSurfaceProps.h"
  20. #include "GrTypes.h"
  21. class SkCanvas;
  22. class SkDeferredDisplayList;
  23. class SkPaint;
  24. class SkSurfaceCharacterization;
  25. class GrBackendRenderTarget;
  26. class GrBackendSemaphore;
  27. class GrBackendTexture;
  28. class GrContext;
  29. class GrRenderTarget;
  30. /** \class SkSurface
  31. SkSurface is responsible for managing the pixels that a canvas draws into. The pixels can be
  32. allocated either in CPU memory (a raster surface) or on the GPU (a GrRenderTarget surface).
  33. SkSurface takes care of allocating a SkCanvas that will draw into the surface. Call
  34. surface->getCanvas() to use that canvas (but don't delete it, it is owned by the surface).
  35. SkSurface always has non-zero dimensions. If there is a request for a new surface, and either
  36. of the requested dimensions are zero, then nullptr will be returned.
  37. */
  38. class SK_API SkSurface : public SkRefCnt {
  39. public:
  40. /** Allocates raster SkSurface. SkCanvas returned by SkSurface draws directly into pixels.
  41. SkSurface is returned if all parameters are valid.
  42. Valid parameters include:
  43. info dimensions are greater than zero;
  44. info contains SkColorType and SkAlphaType supported by raster surface;
  45. pixels is not nullptr;
  46. rowBytes is large enough to contain info width pixels of SkColorType.
  47. Pixel buffer size should be info height times computed rowBytes.
  48. Pixels are not initialized.
  49. To access pixels after drawing, call flush() or peekPixels().
  50. @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace,
  51. of raster surface; width and height must be greater than zero
  52. @param pixels pointer to destination pixels buffer
  53. @param rowBytes interval from one SkSurface row to the next
  54. @param surfaceProps LCD striping orientation and setting for device independent fonts;
  55. may be nullptr
  56. @return SkSurface if all parameters are valid; otherwise, nullptr
  57. */
  58. static sk_sp<SkSurface> MakeRasterDirect(const SkImageInfo& imageInfo, void* pixels,
  59. size_t rowBytes,
  60. const SkSurfaceProps* surfaceProps = nullptr);
  61. /** Allocates raster SkSurface. SkCanvas returned by SkSurface draws directly into pixels.
  62. releaseProc is called with pixels and context when SkSurface is deleted.
  63. SkSurface is returned if all parameters are valid.
  64. Valid parameters include:
  65. info dimensions are greater than zero;
  66. info contains SkColorType and SkAlphaType supported by raster surface;
  67. pixels is not nullptr;
  68. rowBytes is large enough to contain info width pixels of SkColorType.
  69. Pixel buffer size should be info height times computed rowBytes.
  70. Pixels are not initialized.
  71. To access pixels after drawing, call flush() or peekPixels().
  72. @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace,
  73. of raster surface; width and height must be greater than zero
  74. @param pixels pointer to destination pixels buffer
  75. @param rowBytes interval from one SkSurface row to the next
  76. @param releaseProc called when SkSurface is deleted; may be nullptr
  77. @param context passed to releaseProc; may be nullptr
  78. @param surfaceProps LCD striping orientation and setting for device independent fonts;
  79. may be nullptr
  80. @return SkSurface if all parameters are valid; otherwise, nullptr
  81. */
  82. static sk_sp<SkSurface> MakeRasterDirectReleaseProc(const SkImageInfo& imageInfo, void* pixels,
  83. size_t rowBytes,
  84. void (*releaseProc)(void* pixels, void* context),
  85. void* context, const SkSurfaceProps* surfaceProps = nullptr);
  86. /** Allocates raster SkSurface. SkCanvas returned by SkSurface draws directly into pixels.
  87. Allocates and zeroes pixel memory. Pixel memory size is imageInfo.height() times
  88. rowBytes, or times imageInfo.minRowBytes() if rowBytes is zero.
  89. Pixel memory is deleted when SkSurface is deleted.
  90. SkSurface is returned if all parameters are valid.
  91. Valid parameters include:
  92. info dimensions are greater than zero;
  93. info contains SkColorType and SkAlphaType supported by raster surface;
  94. rowBytes is large enough to contain info width pixels of SkColorType, or is zero.
  95. If rowBytes is not zero, subsequent images returned by makeImageSnapshot()
  96. have the same rowBytes.
  97. @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace,
  98. of raster surface; width and height must be greater than zero
  99. @param rowBytes interval from one SkSurface row to the next; may be zero
  100. @param surfaceProps LCD striping orientation and setting for device independent fonts;
  101. may be nullptr
  102. @return SkSurface if all parameters are valid; otherwise, nullptr
  103. */
  104. static sk_sp<SkSurface> MakeRaster(const SkImageInfo& imageInfo, size_t rowBytes,
  105. const SkSurfaceProps* surfaceProps);
  106. /** Allocates raster SkSurface. SkCanvas returned by SkSurface draws directly into pixels.
  107. Allocates and zeroes pixel memory. Pixel memory size is imageInfo.height() times
  108. imageInfo.minRowBytes().
  109. Pixel memory is deleted when SkSurface is deleted.
  110. SkSurface is returned if all parameters are valid.
  111. Valid parameters include:
  112. info dimensions are greater than zero;
  113. info contains SkColorType and SkAlphaType supported by raster surface.
  114. @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace,
  115. of raster surface; width and height must be greater than zero
  116. @param props LCD striping orientation and setting for device independent fonts;
  117. may be nullptr
  118. @return SkSurface if all parameters are valid; otherwise, nullptr
  119. */
  120. static sk_sp<SkSurface> MakeRaster(const SkImageInfo& imageInfo,
  121. const SkSurfaceProps* props = nullptr) {
  122. return MakeRaster(imageInfo, 0, props);
  123. }
  124. /** Allocates raster SkSurface. SkCanvas returned by SkSurface draws directly into pixels.
  125. Allocates and zeroes pixel memory. Pixel memory size is height times width times
  126. four. Pixel memory is deleted when SkSurface is deleted.
  127. Internally, sets SkImageInfo to width, height, native color type, and
  128. kPremul_SkAlphaType.
  129. SkSurface is returned if width and height are greater than zero.
  130. Use to create SkSurface that matches SkPMColor, the native pixel arrangement on
  131. the platform. SkSurface drawn to output device skips converting its pixel format.
  132. @param width pixel column count; must be greater than zero
  133. @param height pixel row count; must be greater than zero
  134. @param surfaceProps LCD striping orientation and setting for device independent
  135. fonts; may be nullptr
  136. @return SkSurface if all parameters are valid; otherwise, nullptr
  137. */
  138. static sk_sp<SkSurface> MakeRasterN32Premul(int width, int height,
  139. const SkSurfaceProps* surfaceProps = nullptr) {
  140. return MakeRaster(SkImageInfo::MakeN32Premul(width, height), surfaceProps);
  141. }
  142. /** Wraps a GPU-backed texture into SkSurface. Caller must ensure the texture is
  143. valid for the lifetime of returned SkSurface. If sampleCnt greater than zero,
  144. creates an intermediate MSAA SkSurface which is used for drawing backendTexture.
  145. SkSurface is returned if all parameters are valid. backendTexture is valid if
  146. its pixel configuration agrees with colorSpace and context; for instance, if
  147. backendTexture has an sRGB configuration, then context must support sRGB,
  148. and colorSpace must be present. Further, backendTexture width and height must
  149. not exceed context capabilities, and the context must be able to support
  150. back-end textures.
  151. If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
  152. @param context GPU context
  153. @param backendTexture texture residing on GPU
  154. @param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin
  155. @param sampleCnt samples per pixel, or 0 to disable full scene anti-aliasing
  156. @param colorType one of:
  157. kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
  158. kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
  159. kRGB_888x_SkColorType, kBGRA_8888_SkColorType,
  160. kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
  161. kGray_8_SkColorType, kRGBA_F16_SkColorType
  162. @param colorSpace range of colors; may be nullptr
  163. @param surfaceProps LCD striping orientation and setting for device independent
  164. fonts; may be nullptr
  165. @return SkSurface if all parameters are valid; otherwise, nullptr
  166. */
  167. static sk_sp<SkSurface> MakeFromBackendTexture(GrContext* context,
  168. const GrBackendTexture& backendTexture,
  169. GrSurfaceOrigin origin, int sampleCnt,
  170. SkColorType colorType,
  171. sk_sp<SkColorSpace> colorSpace,
  172. const SkSurfaceProps* surfaceProps);
  173. /** Wraps a GPU-backed buffer into SkSurface. Caller must ensure backendRenderTarget
  174. is valid for the lifetime of returned SkSurface.
  175. SkSurface is returned if all parameters are valid. backendRenderTarget is valid if
  176. its pixel configuration agrees with colorSpace and context; for instance, if
  177. backendRenderTarget has an sRGB configuration, then context must support sRGB,
  178. and colorSpace must be present. Further, backendRenderTarget width and height must
  179. not exceed context capabilities, and the context must be able to support
  180. back-end render targets.
  181. If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
  182. @param context GPU context
  183. @param backendRenderTarget GPU intermediate memory buffer
  184. @param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin
  185. @param colorType one of:
  186. kUnknown_SkColorType, kAlpha_8_SkColorType,
  187. kRGB_565_SkColorType,
  188. kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
  189. kRGB_888x_SkColorType, kBGRA_8888_SkColorType,
  190. kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
  191. kGray_8_SkColorType, kRGBA_F16_SkColorType
  192. @param colorSpace range of colors
  193. @param surfaceProps LCD striping orientation and setting for device independent
  194. fonts; may be nullptr
  195. @return SkSurface if all parameters are valid; otherwise, nullptr
  196. */
  197. static sk_sp<SkSurface> MakeFromBackendRenderTarget(GrContext* context,
  198. const GrBackendRenderTarget& backendRenderTarget,
  199. GrSurfaceOrigin origin,
  200. SkColorType colorType,
  201. sk_sp<SkColorSpace> colorSpace,
  202. const SkSurfaceProps* surfaceProps);
  203. /** Wraps a GPU-backed texture into SkSurface. Caller must ensure backendTexture is
  204. valid for the lifetime of returned SkSurface. If sampleCnt greater than zero,
  205. creates an intermediate MSAA SkSurface which is used for drawing backendTexture.
  206. SkSurface is returned if all parameters are valid. backendTexture is valid if
  207. its pixel configuration agrees with colorSpace and context; for instance, if
  208. backendTexture has an sRGB configuration, then context must support sRGB,
  209. and colorSpace must be present. Further, backendTexture width and height must
  210. not exceed context capabilities.
  211. Returned SkSurface is available only for drawing into, and cannot generate an
  212. SkImage.
  213. If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
  214. @param context GPU context
  215. @param backendTexture texture residing on GPU
  216. @param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin
  217. @param sampleCnt samples per pixel, or 0 to disable full scene anti-aliasing
  218. @param colorType one of:
  219. kUnknown_SkColorType, kAlpha_8_SkColorType, kRGB_565_SkColorType,
  220. kARGB_4444_SkColorType, kRGBA_8888_SkColorType,
  221. kRGB_888x_SkColorType, kBGRA_8888_SkColorType,
  222. kRGBA_1010102_SkColorType, kRGB_101010x_SkColorType,
  223. kGray_8_SkColorType, kRGBA_F16_SkColorType
  224. @param colorSpace range of colors; may be nullptr
  225. @param surfaceProps LCD striping orientation and setting for device independent
  226. fonts; may be nullptr
  227. @return SkSurface if all parameters are valid; otherwise, nullptr
  228. */
  229. static sk_sp<SkSurface> MakeFromBackendTextureAsRenderTarget(GrContext* context,
  230. const GrBackendTexture& backendTexture,
  231. GrSurfaceOrigin origin,
  232. int sampleCnt,
  233. SkColorType colorType,
  234. sk_sp<SkColorSpace> colorSpace,
  235. const SkSurfaceProps* surfaceProps);
  236. /** Returns SkSurface on GPU indicated by context. Allocates memory for
  237. pixels, based on the width, height, and SkColorType in SkImageInfo. budgeted
  238. selects whether allocation for pixels is tracked by context. imageInfo
  239. describes the pixel format in SkColorType, and transparency in
  240. SkAlphaType, and color matching in SkColorSpace.
  241. sampleCount requests the number of samples per pixel.
  242. Pass zero to disable multi-sample anti-aliasing. The request is rounded
  243. up to the next supported count, or rounded down if it is larger than the
  244. maximum supported count.
  245. surfaceOrigin pins either the top-left or the bottom-left corner to the origin.
  246. shouldCreateWithMips hints that SkImage returned by makeImageSnapshot() is mip map.
  247. If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
  248. @param context GPU context
  249. @param budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes
  250. @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace;
  251. width, or height, or both, may be zero
  252. @param sampleCount samples per pixel, or 0 to disable full scene anti-aliasing
  253. @param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin
  254. @param surfaceProps LCD striping orientation and setting for device independent
  255. fonts; may be nullptr
  256. @param shouldCreateWithMips hint that SkSurface will host mip map images
  257. @return SkSurface if all parameters are valid; otherwise, nullptr
  258. */
  259. static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
  260. const SkImageInfo& imageInfo,
  261. int sampleCount, GrSurfaceOrigin surfaceOrigin,
  262. const SkSurfaceProps* surfaceProps,
  263. bool shouldCreateWithMips = false);
  264. /** Returns SkSurface on GPU indicated by context. Allocates memory for
  265. pixels, based on the width, height, and SkColorType in SkImageInfo. budgeted
  266. selects whether allocation for pixels is tracked by context. imageInfo
  267. describes the pixel format in SkColorType, and transparency in
  268. SkAlphaType, and color matching in SkColorSpace.
  269. sampleCount requests the number of samples per pixel.
  270. Pass zero to disable multi-sample anti-aliasing. The request is rounded
  271. up to the next supported count, or rounded down if it is larger than the
  272. maximum supported count.
  273. SkSurface bottom-left corner is pinned to the origin.
  274. @param context GPU context
  275. @param budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes
  276. @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace,
  277. of raster surface; width, or height, or both, may be zero
  278. @param sampleCount samples per pixel, or 0 to disable multi-sample anti-aliasing
  279. @param props LCD striping orientation and setting for device independent
  280. fonts; may be nullptr
  281. @return SkSurface if all parameters are valid; otherwise, nullptr
  282. */
  283. static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
  284. const SkImageInfo& imageInfo, int sampleCount,
  285. const SkSurfaceProps* props) {
  286. return MakeRenderTarget(context, budgeted, imageInfo, sampleCount,
  287. kBottomLeft_GrSurfaceOrigin, props);
  288. }
  289. /** Returns SkSurface on GPU indicated by context. Allocates memory for
  290. pixels, based on the width, height, and SkColorType in SkImageInfo. budgeted
  291. selects whether allocation for pixels is tracked by context. imageInfo
  292. describes the pixel format in SkColorType, and transparency in
  293. SkAlphaType, and color matching in SkColorSpace.
  294. SkSurface bottom-left corner is pinned to the origin.
  295. @param context GPU context
  296. @param budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes
  297. @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace,
  298. of raster surface; width, or height, or both, may be zero
  299. @return SkSurface if all parameters are valid; otherwise, nullptr
  300. */
  301. static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
  302. const SkImageInfo& imageInfo) {
  303. if (!imageInfo.width() || !imageInfo.height()) {
  304. return nullptr;
  305. }
  306. return MakeRenderTarget(context, budgeted, imageInfo, 0, kBottomLeft_GrSurfaceOrigin,
  307. nullptr);
  308. }
  309. /** Returns SkSurface on GPU indicated by context that is compatible with the provided
  310. characterization. budgeted selects whether allocation for pixels is tracked by context.
  311. @param context GPU context
  312. @param characterization description of the desired SkSurface
  313. @param budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes
  314. @return SkSurface if all parameters are valid; otherwise, nullptr
  315. */
  316. static sk_sp<SkSurface> MakeRenderTarget(GrContext* context,
  317. const SkSurfaceCharacterization& characterization,
  318. SkBudgeted budgeted);
  319. /** Returns SkSurface without backing pixels. Drawing to SkCanvas returned from SkSurface
  320. has no effect. Calling makeImageSnapshot() on returned SkSurface returns nullptr.
  321. @param width one or greater
  322. @param height one or greater
  323. @return SkSurface if width and height are positive; otherwise, nullptr
  324. */
  325. static sk_sp<SkSurface> MakeNull(int width, int height);
  326. /** Returns pixel count in each row; may be zero or greater.
  327. @return number of pixel columns
  328. */
  329. int width() const { return fWidth; }
  330. /** Returns pixel row count; may be zero or greater.
  331. @return number of pixel rows
  332. */
  333. int height() const { return fHeight; }
  334. /** Returns unique value identifying the content of SkSurface. Returned value changes
  335. each time the content changes. Content is changed by drawing, or by calling
  336. notifyContentWillChange().
  337. @return unique content identifier
  338. */
  339. uint32_t generationID();
  340. /** \enum SkSurface::ContentChangeMode
  341. ContentChangeMode members are parameters to notifyContentWillChange().
  342. */
  343. enum ContentChangeMode {
  344. kDiscard_ContentChangeMode, //!< discards surface on change
  345. kRetain_ContentChangeMode, //!< preserves surface on change
  346. };
  347. /** Notifies that SkSurface contents will be changed by code outside of Skia.
  348. Subsequent calls to generationID() return a different value.
  349. TODO: Can kRetain_ContentChangeMode be deprecated?
  350. @param mode one of: kDiscard_ContentChangeMode, kRetain_ContentChangeMode
  351. */
  352. void notifyContentWillChange(ContentChangeMode mode);
  353. enum BackendHandleAccess {
  354. kFlushRead_BackendHandleAccess, //!< back-end object is readable
  355. kFlushWrite_BackendHandleAccess, //!< back-end object is writable
  356. kDiscardWrite_BackendHandleAccess, //!< back-end object must be overwritten
  357. };
  358. /** Deprecated.
  359. */
  360. static const BackendHandleAccess kFlushRead_TextureHandleAccess =
  361. kFlushRead_BackendHandleAccess;
  362. /** Deprecated.
  363. */
  364. static const BackendHandleAccess kFlushWrite_TextureHandleAccess =
  365. kFlushWrite_BackendHandleAccess;
  366. /** Deprecated.
  367. */
  368. static const BackendHandleAccess kDiscardWrite_TextureHandleAccess =
  369. kDiscardWrite_BackendHandleAccess;
  370. /** Retrieves the back-end texture. If SkSurface has no back-end texture, an invalid
  371. object is returned. Call GrBackendTexture::isValid to determine if the result
  372. is valid.
  373. The returned GrBackendTexture should be discarded if the SkSurface is drawn to or deleted.
  374. @param backendHandleAccess one of: kFlushRead_BackendHandleAccess,
  375. kFlushWrite_BackendHandleAccess,
  376. kDiscardWrite_BackendHandleAccess
  377. @return GPU texture reference; invalid on failure
  378. */
  379. GrBackendTexture getBackendTexture(BackendHandleAccess backendHandleAccess);
  380. /** Retrieves the back-end render target. If SkSurface has no back-end render target, an invalid
  381. object is returned. Call GrBackendRenderTarget::isValid to determine if the result
  382. is valid.
  383. The returned GrBackendRenderTarget should be discarded if the SkSurface is drawn to
  384. or deleted.
  385. @param backendHandleAccess one of: kFlushRead_BackendHandleAccess,
  386. kFlushWrite_BackendHandleAccess,
  387. kDiscardWrite_BackendHandleAccess
  388. @return GPU render target reference; invalid on failure
  389. */
  390. GrBackendRenderTarget getBackendRenderTarget(BackendHandleAccess backendHandleAccess);
  391. /** Returns SkCanvas that draws into SkSurface. Subsequent calls return the same SkCanvas.
  392. SkCanvas returned is managed and owned by SkSurface, and is deleted when SkSurface
  393. is deleted.
  394. @return drawing SkCanvas for SkSurface
  395. */
  396. SkCanvas* getCanvas();
  397. /** Returns a compatible SkSurface, or nullptr. Returned SkSurface contains
  398. the same raster, GPU, or null properties as the original. Returned SkSurface
  399. does not share the same pixels.
  400. Returns nullptr if imageInfo width or height are zero, or if imageInfo
  401. is incompatible with SkSurface.
  402. @param imageInfo width, height, SkColorType, SkAlphaType, SkColorSpace,
  403. of SkSurface; width and height must be greater than zero
  404. @return compatible SkSurface or nullptr
  405. */
  406. sk_sp<SkSurface> makeSurface(const SkImageInfo& imageInfo);
  407. /** Returns SkImage capturing SkSurface contents. Subsequent drawing to SkSurface contents
  408. are not captured. SkImage allocation is accounted for if SkSurface was created with
  409. SkBudgeted::kYes.
  410. @return SkImage initialized with SkSurface contents
  411. */
  412. sk_sp<SkImage> makeImageSnapshot();
  413. /**
  414. * Like the no-parameter version, this returns an image of the current surface contents.
  415. * This variant takes a rectangle specifying the subset of the surface that is of interest.
  416. * These bounds will be sanitized before being used.
  417. * - If bounds extends beyond the surface, it will be trimmed to just the intersection of
  418. * it and the surface.
  419. * - If bounds does not intersect the surface, then this returns nullptr.
  420. * - If bounds == the surface, then this is the same as calling the no-parameter variant.
  421. */
  422. sk_sp<SkImage> makeImageSnapshot(const SkIRect& bounds);
  423. /** Draws SkSurface contents to canvas, with its top-left corner at (x, y).
  424. If SkPaint paint is not nullptr, apply SkColorFilter, alpha, SkImageFilter,
  425. SkBlendMode, and SkDrawLooper.
  426. @param canvas SkCanvas drawn into
  427. @param x horizontal offset in SkCanvas
  428. @param y vertical offset in SkCanvas
  429. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  430. and so on; or nullptr
  431. */
  432. void draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint);
  433. /** Copies SkSurface pixel address, row bytes, and SkImageInfo to SkPixmap, if address
  434. is available, and returns true. If pixel address is not available, return
  435. false and leave SkPixmap unchanged.
  436. pixmap contents become invalid on any future change to SkSurface.
  437. @param pixmap storage for pixel state if pixels are readable; otherwise, ignored
  438. @return true if SkSurface has direct access to pixels
  439. */
  440. bool peekPixels(SkPixmap* pixmap);
  441. /** Copies SkRect of pixels to dst.
  442. Source SkRect corners are (srcX, srcY) and SkSurface (width(), height()).
  443. Destination SkRect corners are (0, 0) and (dst.width(), dst.height()).
  444. Copies each readable pixel intersecting both rectangles, without scaling,
  445. converting to dst.colorType() and dst.alphaType() if required.
  446. Pixels are readable when SkSurface is raster, or backed by a GPU.
  447. The destination pixel storage must be allocated by the caller.
  448. Pixel values are converted only if SkColorType and SkAlphaType
  449. do not match. Only pixels within both source and destination rectangles
  450. are copied. dst contents outside SkRect intersection are unchanged.
  451. Pass negative values for srcX or srcY to offset pixels across or down destination.
  452. Does not copy, and returns false if:
  453. - Source and destination rectangles do not intersect.
  454. - SkPixmap pixels could not be allocated.
  455. - dst.rowBytes() is too small to contain one row of pixels.
  456. @param dst storage for pixels copied from SkSurface
  457. @param srcX offset into readable pixels on x-axis; may be negative
  458. @param srcY offset into readable pixels on y-axis; may be negative
  459. @return true if pixels were copied
  460. */
  461. bool readPixels(const SkPixmap& dst, int srcX, int srcY);
  462. /** Copies SkRect of pixels from SkCanvas into dstPixels.
  463. Source SkRect corners are (srcX, srcY) and SkSurface (width(), height()).
  464. Destination SkRect corners are (0, 0) and (dstInfo.width(), dstInfo.height()).
  465. Copies each readable pixel intersecting both rectangles, without scaling,
  466. converting to dstInfo.colorType() and dstInfo.alphaType() if required.
  467. Pixels are readable when SkSurface is raster, or backed by a GPU.
  468. The destination pixel storage must be allocated by the caller.
  469. Pixel values are converted only if SkColorType and SkAlphaType
  470. do not match. Only pixels within both source and destination rectangles
  471. are copied. dstPixels contents outside SkRect intersection are unchanged.
  472. Pass negative values for srcX or srcY to offset pixels across or down destination.
  473. Does not copy, and returns false if:
  474. - Source and destination rectangles do not intersect.
  475. - SkSurface pixels could not be converted to dstInfo.colorType() or dstInfo.alphaType().
  476. - dstRowBytes is too small to contain one row of pixels.
  477. @param dstInfo width, height, SkColorType, and SkAlphaType of dstPixels
  478. @param dstPixels storage for pixels; dstInfo.height() times dstRowBytes, or larger
  479. @param dstRowBytes size of one destination row; dstInfo.width() times pixel size, or larger
  480. @param srcX offset into readable pixels on x-axis; may be negative
  481. @param srcY offset into readable pixels on y-axis; may be negative
  482. @return true if pixels were copied
  483. */
  484. bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
  485. int srcX, int srcY);
  486. /** Copies SkRect of pixels from SkSurface into bitmap.
  487. Source SkRect corners are (srcX, srcY) and SkSurface (width(), height()).
  488. Destination SkRect corners are (0, 0) and (bitmap.width(), bitmap.height()).
  489. Copies each readable pixel intersecting both rectangles, without scaling,
  490. converting to bitmap.colorType() and bitmap.alphaType() if required.
  491. Pixels are readable when SkSurface is raster, or backed by a GPU.
  492. The destination pixel storage must be allocated by the caller.
  493. Pixel values are converted only if SkColorType and SkAlphaType
  494. do not match. Only pixels within both source and destination rectangles
  495. are copied. dst contents outside SkRect intersection are unchanged.
  496. Pass negative values for srcX or srcY to offset pixels across or down destination.
  497. Does not copy, and returns false if:
  498. - Source and destination rectangles do not intersect.
  499. - SkSurface pixels could not be converted to dst.colorType() or dst.alphaType().
  500. - dst pixels could not be allocated.
  501. - dst.rowBytes() is too small to contain one row of pixels.
  502. @param dst storage for pixels copied from SkSurface
  503. @param srcX offset into readable pixels on x-axis; may be negative
  504. @param srcY offset into readable pixels on y-axis; may be negative
  505. @return true if pixels were copied
  506. */
  507. bool readPixels(const SkBitmap& dst, int srcX, int srcY);
  508. /** Copies SkRect of pixels from the src SkPixmap to the SkSurface.
  509. Source SkRect corners are (0, 0) and (src.width(), src.height()).
  510. Destination SkRect corners are (dstX, dstY) and
  511. (dstX + Surface width(), dstY + Surface height()).
  512. Copies each readable pixel intersecting both rectangles, without scaling,
  513. converting to SkSurface colorType() and SkSurface alphaType() if required.
  514. @param src storage for pixels to copy to SkSurface
  515. @param dstX x-axis position relative to SkSurface to begin copy; may be negative
  516. @param dstY y-axis position relative to SkSurface to begin copy; may be negative
  517. */
  518. void writePixels(const SkPixmap& src, int dstX, int dstY);
  519. /** Copies SkRect of pixels from the src SkBitmap to the SkSurface.
  520. Source SkRect corners are (0, 0) and (src.width(), src.height()).
  521. Destination SkRect corners are (dstX, dstY) and
  522. (dstX + Surface width(), dstY + Surface height()).
  523. Copies each readable pixel intersecting both rectangles, without scaling,
  524. converting to SkSurface colorType() and SkSurface alphaType() if required.
  525. @param src storage for pixels to copy to SkSurface
  526. @param dstX x-axis position relative to SkSurface to begin copy; may be negative
  527. @param dstY y-axis position relative to SkSurface to begin copy; may be negative
  528. */
  529. void writePixels(const SkBitmap& src, int dstX, int dstY);
  530. /** Returns SkSurfaceProps for surface.
  531. @return LCD striping orientation and setting for device independent fonts
  532. */
  533. const SkSurfaceProps& props() const { return fProps; }
  534. /** Deprecated.
  535. */
  536. void prepareForExternalIO();
  537. /** Issues pending SkSurface commands to the GPU-backed API and resolves any SkSurface MSAA.
  538. Skia flushes as needed, so it is not necessary to call this if Skia manages
  539. drawing and object lifetime. Call when interleaving Skia calls with native
  540. GPU calls.
  541. */
  542. void flush();
  543. /** Issues pending SkSurface commands to the GPU-backed API and resolves any SkSurface MSAA.
  544. After issuing all commands, signalSemaphores of count numSemaphores semaphores
  545. are signaled by the GPU.
  546. For each GrBackendSemaphore in signalSemaphores:
  547. if GrBackendSemaphore is initialized, the GPU back-end uses the semaphore as is;
  548. otherwise, a new semaphore is created and initializes GrBackendSemaphore.
  549. The caller must delete the semaphores created and returned in signalSemaphores.
  550. GrBackendSemaphore can be deleted as soon as this function returns.
  551. If the back-end API is OpenGL only uninitialized backend semaphores are supported.
  552. If the back-end API is Vulkan semaphores may be initialized or uninitialized.
  553. If uninitialized, created semaphores are valid only with the VkDevice
  554. with which they were created.
  555. If GrSemaphoresSubmitted::kNo is returned, the GPU back-end did not create or
  556. add any semaphores to signal on the GPU; the caller should not instruct the GPU
  557. to wait on any of the semaphores.
  558. Pending surface commands are flushed regardless of the return result.
  559. @param numSemaphores size of signalSemaphores array
  560. @param signalSemaphores array of semaphore containers
  561. @return one of: GrSemaphoresSubmitted::kYes, GrSemaphoresSubmitted::kNo
  562. */
  563. GrSemaphoresSubmitted flushAndSignalSemaphores(int numSemaphores,
  564. GrBackendSemaphore signalSemaphores[]);
  565. /** Inserts a list of GPU semaphores that the current GPU-backed API must wait on before
  566. executing any more commands on the GPU for this surface. Skia will take ownership of the
  567. underlying semaphores and delete them once they have been signaled and waited on.
  568. If this call returns false, then the GPU back-end will not wait on any passed in semaphores,
  569. and the client will still own the semaphores.
  570. @param numSemaphores size of waitSemaphores array
  571. @param waitSemaphores array of semaphore containers
  572. @return true if GPU is waiting on semaphores
  573. */
  574. bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores);
  575. /** Initializes SkSurfaceCharacterization that can be used to perform GPU back-end
  576. processing in a separate thread. Typically this is used to divide drawing
  577. into multiple tiles. SkDeferredDisplayListRecorder records the drawing commands
  578. for each tile.
  579. Return true if SkSurface supports characterization. raster surface returns false.
  580. @param characterization properties for parallel drawing
  581. @return true if supported
  582. */
  583. bool characterize(SkSurfaceCharacterization* characterization) const;
  584. /** Draws deferred display list created using SkDeferredDisplayListRecorder.
  585. Has no effect and returns false if SkSurfaceCharacterization stored in
  586. deferredDisplayList is not compatible with SkSurface.
  587. raster surface returns false.
  588. @param deferredDisplayList drawing commands
  589. @return false if deferredDisplayList is not compatible
  590. */
  591. bool draw(SkDeferredDisplayList* deferredDisplayList);
  592. protected:
  593. SkSurface(int width, int height, const SkSurfaceProps* surfaceProps);
  594. SkSurface(const SkImageInfo& imageInfo, const SkSurfaceProps* surfaceProps);
  595. // called by subclass if their contents have changed
  596. void dirtyGenerationID() {
  597. fGenerationID = 0;
  598. }
  599. private:
  600. const SkSurfaceProps fProps;
  601. const int fWidth;
  602. const int fHeight;
  603. uint32_t fGenerationID;
  604. typedef SkRefCnt INHERITED;
  605. };
  606. #endif