SkCanvas.h 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  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/SkCanvas.h and docs/SkCanvas_Reference.bmh
  8. on 2018-08-28 10:32:58. Additional documentation and examples can be found at:
  9. https://skia.org/user/api/SkCanvas_Reference
  10. You may edit either file directly. Structural changes to public interfaces require
  11. editing both files. After editing docs/SkCanvas_Reference.bmh, run:
  12. bookmaker -b docs -i include/core/SkCanvas.h -p
  13. to create an updated version of this file.
  14. */
  15. #ifndef SkCanvas_DEFINED
  16. #define SkCanvas_DEFINED
  17. #include "../private/SkMacros.h"
  18. #include "SkBlendMode.h"
  19. #include "SkClipOp.h"
  20. #include "SkDeque.h"
  21. #include "SkPaint.h"
  22. #include "SkRasterHandleAllocator.h"
  23. #include "SkSurfaceProps.h"
  24. #include "SkVertices.h"
  25. class GrContext;
  26. class GrRenderTargetContext;
  27. class SkAndroidFrameworkUtils;
  28. class SkBaseDevice;
  29. class SkBitmap;
  30. class SkData;
  31. class SkDraw;
  32. class SkDrawable;
  33. struct SkDrawShadowRec;
  34. class SkFont;
  35. class SkGlyphRunBuilder;
  36. class SkImage;
  37. class SkImageFilter;
  38. class SkMetaData;
  39. class SkPath;
  40. class SkPicture;
  41. class SkPixmap;
  42. class SkRegion;
  43. class SkRRect;
  44. struct SkRSXform;
  45. class SkSurface;
  46. class SkSurface_Base;
  47. class SkTextBlob;
  48. /** \class SkCanvas
  49. SkCanvas provides an interface for drawing, and how the drawing is clipped and transformed.
  50. SkCanvas contains a stack of SkMatrix and clip values.
  51. SkCanvas and SkPaint together provide the state to draw into SkSurface or SkBaseDevice.
  52. Each SkCanvas draw call transforms the geometry of the object by the concatenation of all
  53. SkMatrix values in the stack. The transformed geometry is clipped by the intersection
  54. of all of clip values in the stack. The SkCanvas draw calls use SkPaint to supply drawing
  55. state such as color, SkTypeface, text size, stroke width, SkShader and so on.
  56. To draw to a pixel-based destination, create raster surface or GPU surface.
  57. Request SkCanvas from SkSurface to obtain the interface to draw.
  58. SkCanvas generated by raster surface draws to memory visible to the CPU.
  59. SkCanvas generated by GPU surface uses Vulkan or OpenGL to draw to the GPU.
  60. To draw to a document, obtain SkCanvas from SVG canvas, document PDF, or SkPictureRecorder.
  61. SkDocument based SkCanvas and other SkCanvas subclasses reference SkBaseDevice describing the
  62. destination.
  63. SkCanvas can be constructed to draw to SkBitmap without first creating raster surface.
  64. This approach may be deprecated in the future.
  65. */
  66. class SK_API SkCanvas {
  67. enum PrivateSaveLayerFlags {
  68. kDontClipToLayer_PrivateSaveLayerFlag = 1U << 31,
  69. };
  70. public:
  71. /** Allocates raster SkCanvas that will draw directly into pixels.
  72. SkCanvas is returned if all parameters are valid.
  73. Valid parameters include:
  74. info dimensions are zero or positive;
  75. info contains SkColorType and SkAlphaType supported by raster surface;
  76. pixels is not nullptr;
  77. rowBytes is zero or large enough to contain info width pixels of SkColorType.
  78. Pass zero for rowBytes to compute rowBytes from info width and size of pixel.
  79. If rowBytes is greater than zero, it must be equal to or greater than
  80. info width times bytes required for SkColorType.
  81. Pixel buffer size should be info height times computed rowBytes.
  82. Pixels are not initialized.
  83. To access pixels after drawing, call flush() or peekPixels().
  84. @param info width, height, SkColorType, SkAlphaType, SkColorSpace, of raster surface;
  85. width, or height, or both, may be zero
  86. @param pixels pointer to destination pixels buffer
  87. @param rowBytes interval from one SkSurface row to the next, or zero
  88. @param props LCD striping orientation and setting for device independent fonts;
  89. may be nullptr
  90. @return SkCanvas if all parameters are valid; otherwise, nullptr
  91. */
  92. static std::unique_ptr<SkCanvas> MakeRasterDirect(const SkImageInfo& info, void* pixels,
  93. size_t rowBytes,
  94. const SkSurfaceProps* props = nullptr);
  95. /** Allocates raster SkCanvas specified by inline image specification. Subsequent SkCanvas
  96. calls draw into pixels.
  97. SkColorType is set to kN32_SkColorType.
  98. SkAlphaType is set to kPremul_SkAlphaType.
  99. To access pixels after drawing, call flush() or peekPixels().
  100. SkCanvas is returned if all parameters are valid.
  101. Valid parameters include:
  102. width and height are zero or positive;
  103. pixels is not nullptr;
  104. rowBytes is zero or large enough to contain width pixels of kN32_SkColorType.
  105. Pass zero for rowBytes to compute rowBytes from width and size of pixel.
  106. If rowBytes is greater than zero, it must be equal to or greater than
  107. width times bytes required for SkColorType.
  108. Pixel buffer size should be height times rowBytes.
  109. @param width pixel column count on raster surface created; must be zero or greater
  110. @param height pixel row count on raster surface created; must be zero or greater
  111. @param pixels pointer to destination pixels buffer; buffer size should be height
  112. times rowBytes
  113. @param rowBytes interval from one SkSurface row to the next, or zero
  114. @return SkCanvas if all parameters are valid; otherwise, nullptr
  115. */
  116. static std::unique_ptr<SkCanvas> MakeRasterDirectN32(int width, int height, SkPMColor* pixels,
  117. size_t rowBytes) {
  118. return MakeRasterDirect(SkImageInfo::MakeN32Premul(width, height), pixels, rowBytes);
  119. }
  120. /** Creates an empty SkCanvas with no backing device or pixels, with
  121. a width and height of zero.
  122. @return empty SkCanvas
  123. */
  124. SkCanvas();
  125. /** Creates SkCanvas of the specified dimensions without a SkSurface.
  126. Used by subclasses with custom implementations for draw member functions.
  127. If props equals nullptr, SkSurfaceProps are created with
  128. SkSurfaceProps::InitType settings, which choose the pixel striping
  129. direction and order. Since a platform may dynamically change its direction when
  130. the device is rotated, and since a platform may have multiple monitors with
  131. different characteristics, it is best not to rely on this legacy behavior.
  132. @param width zero or greater
  133. @param height zero or greater
  134. @param props LCD striping orientation and setting for device independent fonts;
  135. may be nullptr
  136. @return SkCanvas placeholder with dimensions
  137. */
  138. SkCanvas(int width, int height, const SkSurfaceProps* props = nullptr);
  139. /** Deprecated.
  140. */
  141. explicit SkCanvas(sk_sp<SkBaseDevice> device);
  142. /** Constructs a canvas that draws into bitmap.
  143. Sets SkSurfaceProps::kLegacyFontHost_InitType in constructed SkSurface.
  144. SkBitmap is copied so that subsequently editing bitmap will not affect
  145. constructed SkCanvas.
  146. May be deprecated in the future.
  147. @param bitmap width, height, SkColorType, SkAlphaType, and pixel
  148. storage of raster surface
  149. @return SkCanvas that can be used to draw into bitmap
  150. */
  151. explicit SkCanvas(const SkBitmap& bitmap);
  152. #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
  153. /** Private.
  154. */
  155. enum class ColorBehavior {
  156. kLegacy, //!< placeholder
  157. };
  158. /** Private. For use by Android framework only.
  159. @param bitmap specifies a bitmap for the canvas to draw into
  160. @param behavior specializes this constructor; value is unused
  161. @return SkCanvas that can be used to draw into bitmap
  162. */
  163. SkCanvas(const SkBitmap& bitmap, ColorBehavior behavior);
  164. #endif
  165. /** Constructs a canvas that draws into bitmap.
  166. Use props to match the device characteristics, like LCD striping.
  167. bitmap is copied so that subsequently editing bitmap will not affect
  168. constructed SkCanvas.
  169. @param bitmap width, height, SkColorType, SkAlphaType,
  170. and pixel storage of raster surface
  171. @param props order and orientation of RGB striping; and whether to use
  172. device independent fonts
  173. @return SkCanvas that can be used to draw into bitmap
  174. */
  175. SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props);
  176. /** Draws saved layers, if any.
  177. Frees up resources used by SkCanvas.
  178. */
  179. virtual ~SkCanvas();
  180. /** Returns storage to associate additional data with the canvas.
  181. The storage is freed when SkCanvas is deleted.
  182. @return storage that can be read from and written to
  183. */
  184. SkMetaData& getMetaData();
  185. /** Returns SkImageInfo for SkCanvas. If SkCanvas is not associated with raster surface or
  186. GPU surface, returned SkColorType is set to kUnknown_SkColorType.
  187. @return dimensions and SkColorType of SkCanvas
  188. */
  189. SkImageInfo imageInfo() const;
  190. /** Copies SkSurfaceProps, if SkCanvas is associated with raster surface or
  191. GPU surface, and returns true. Otherwise, returns false and leave props unchanged.
  192. @param props storage for writable SkSurfaceProps
  193. @return true if SkSurfaceProps was copied
  194. */
  195. bool getProps(SkSurfaceProps* props) const;
  196. /** Triggers the immediate execution of all pending draw operations.
  197. If SkCanvas is associated with GPU surface, resolves all pending GPU operations.
  198. If SkCanvas is associated with raster surface, has no effect; raster draw
  199. operations are never deferred.
  200. */
  201. void flush();
  202. /** Gets the size of the base or root layer in global canvas coordinates. The
  203. origin of the base layer is always (0,0). The area available for drawing may be
  204. smaller (due to clipping or saveLayer).
  205. @return integral width and height of base layer
  206. */
  207. virtual SkISize getBaseLayerSize() const;
  208. /** Creates SkSurface matching info and props, and associates it with SkCanvas.
  209. Returns nullptr if no match found.
  210. If props is nullptr, matches SkSurfaceProps in SkCanvas. If props is nullptr and SkCanvas
  211. does not have SkSurfaceProps, creates SkSurface with default SkSurfaceProps.
  212. @param info width, height, SkColorType, SkAlphaType, and SkColorSpace
  213. @param props SkSurfaceProps to match; may be nullptr to match SkCanvas
  214. @return SkSurface matching info and props, or nullptr if no match is available
  215. */
  216. sk_sp<SkSurface> makeSurface(const SkImageInfo& info, const SkSurfaceProps* props = nullptr);
  217. /** Returns GPU context of the GPU surface associated with SkCanvas.
  218. @return GPU context, if available; nullptr otherwise
  219. */
  220. virtual GrContext* getGrContext();
  221. /** Returns the pixel base address, SkImageInfo, rowBytes, and origin if the pixels
  222. can be read directly. The returned address is only valid
  223. while SkCanvas is in scope and unchanged. Any SkCanvas call or SkSurface call
  224. may invalidate the returned address and other returned values.
  225. If pixels are inaccessible, info, rowBytes, and origin are unchanged.
  226. @param info storage for writable pixels' SkImageInfo; may be nullptr
  227. @param rowBytes storage for writable pixels' row bytes; may be nullptr
  228. @param origin storage for SkCanvas top layer origin, its top-left corner;
  229. may be nullptr
  230. @return address of pixels, or nullptr if inaccessible
  231. */
  232. void* accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin = nullptr);
  233. /** Returns custom context that tracks the SkMatrix and clip.
  234. Use SkRasterHandleAllocator to blend Skia drawing with custom drawing, typically performed
  235. by the host platform user interface. The custom context returned is generated by
  236. SkRasterHandleAllocator::MakeCanvas, which creates a custom canvas with raster storage for
  237. the drawing destination.
  238. @return context of custom allocation
  239. */
  240. SkRasterHandleAllocator::Handle accessTopRasterHandle() const;
  241. /** Returns true if SkCanvas has direct access to its pixels.
  242. Pixels are readable when SkBaseDevice is raster. Pixels are not readable when SkCanvas
  243. is returned from GPU surface, returned by SkDocument::beginPage, returned by
  244. SkPictureRecorder::beginRecording, or SkCanvas is the base of a utility class
  245. like SkDebugCanvas.
  246. pixmap is valid only while SkCanvas is in scope and unchanged. Any
  247. SkCanvas or SkSurface call may invalidate the pixmap values.
  248. @param pixmap storage for pixel state if pixels are readable; otherwise, ignored
  249. @return true if SkCanvas has direct access to pixels
  250. */
  251. bool peekPixels(SkPixmap* pixmap);
  252. /** Copies SkRect of pixels from SkCanvas into dstPixels. SkMatrix and clip are
  253. ignored.
  254. Source SkRect corners are (srcX, srcY) and (imageInfo().width(), imageInfo().height()).
  255. Destination SkRect corners are (0, 0) and (dstInfo.width(), dstInfo.height()).
  256. Copies each readable pixel intersecting both rectangles, without scaling,
  257. converting to dstInfo.colorType() and dstInfo.alphaType() if required.
  258. Pixels are readable when SkBaseDevice is raster, or backed by a GPU.
  259. Pixels are not readable when SkCanvas is returned by SkDocument::beginPage,
  260. returned by SkPictureRecorder::beginRecording, or SkCanvas is the base of a utility
  261. class like SkDebugCanvas.
  262. The destination pixel storage must be allocated by the caller.
  263. Pixel values are converted only if SkColorType and SkAlphaType
  264. do not match. Only pixels within both source and destination rectangles
  265. are copied. dstPixels contents outside SkRect intersection are unchanged.
  266. Pass negative values for srcX or srcY to offset pixels across or down destination.
  267. Does not copy, and returns false if:
  268. - Source and destination rectangles do not intersect.
  269. - SkCanvas pixels could not be converted to dstInfo.colorType() or dstInfo.alphaType().
  270. - SkCanvas pixels are not readable; for instance, SkCanvas is document-based.
  271. - dstRowBytes is too small to contain one row of pixels.
  272. @param dstInfo width, height, SkColorType, and SkAlphaType of dstPixels
  273. @param dstPixels storage for pixels; dstInfo.height() times dstRowBytes, or larger
  274. @param dstRowBytes size of one destination row; dstInfo.width() times pixel size, or larger
  275. @param srcX offset into readable pixels on x-axis; may be negative
  276. @param srcY offset into readable pixels on y-axis; may be negative
  277. @return true if pixels were copied
  278. */
  279. bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
  280. int srcX, int srcY);
  281. /** Copies SkRect of pixels from SkCanvas into pixmap. SkMatrix and clip are
  282. ignored.
  283. Source SkRect corners are (srcX, srcY) and (imageInfo().width(), imageInfo().height()).
  284. Destination SkRect corners are (0, 0) and (pixmap.width(), pixmap.height()).
  285. Copies each readable pixel intersecting both rectangles, without scaling,
  286. converting to pixmap.colorType() and pixmap.alphaType() if required.
  287. Pixels are readable when SkBaseDevice is raster, or backed by a GPU.
  288. Pixels are not readable when SkCanvas is returned by SkDocument::beginPage,
  289. returned by SkPictureRecorder::beginRecording, or SkCanvas is the base of a utility
  290. class like SkDebugCanvas.
  291. Caller must allocate pixel storage in pixmap if needed.
  292. Pixel values are converted only if SkColorType and SkAlphaType
  293. do not match. Only pixels within both source and destination SkRect
  294. are copied. pixmap pixels contents outside SkRect intersection are unchanged.
  295. Pass negative values for srcX or srcY to offset pixels across or down pixmap.
  296. Does not copy, and returns false if:
  297. - Source and destination rectangles do not intersect.
  298. - SkCanvas pixels could not be converted to pixmap.colorType() or pixmap.alphaType().
  299. - SkCanvas pixels are not readable; for instance, SkCanvas is document-based.
  300. - SkPixmap pixels could not be allocated.
  301. - pixmap.rowBytes() is too small to contain one row of pixels.
  302. @param pixmap storage for pixels copied from SkCanvas
  303. @param srcX offset into readable pixels on x-axis; may be negative
  304. @param srcY offset into readable pixels on y-axis; may be negative
  305. @return true if pixels were copied
  306. */
  307. bool readPixels(const SkPixmap& pixmap, int srcX, int srcY);
  308. /** Copies SkRect of pixels from SkCanvas into bitmap. SkMatrix and clip are
  309. ignored.
  310. Source SkRect corners are (srcX, srcY) and (imageInfo().width(), imageInfo().height()).
  311. Destination SkRect corners are (0, 0) and (bitmap.width(), bitmap.height()).
  312. Copies each readable pixel intersecting both rectangles, without scaling,
  313. converting to bitmap.colorType() and bitmap.alphaType() if required.
  314. Pixels are readable when SkBaseDevice is raster, or backed by a GPU.
  315. Pixels are not readable when SkCanvas is returned by SkDocument::beginPage,
  316. returned by SkPictureRecorder::beginRecording, or SkCanvas is the base of a utility
  317. class like SkDebugCanvas.
  318. Caller must allocate pixel storage in bitmap if needed.
  319. SkBitmap values are converted only if SkColorType and SkAlphaType
  320. do not match. Only pixels within both source and destination rectangles
  321. are copied. SkBitmap pixels outside SkRect intersection are unchanged.
  322. Pass negative values for srcX or srcY to offset pixels across or down bitmap.
  323. Does not copy, and returns false if:
  324. - Source and destination rectangles do not intersect.
  325. - SkCanvas pixels could not be converted to bitmap.colorType() or bitmap.alphaType().
  326. - SkCanvas pixels are not readable; for instance, SkCanvas is document-based.
  327. - bitmap pixels could not be allocated.
  328. - bitmap.rowBytes() is too small to contain one row of pixels.
  329. @param bitmap storage for pixels copied from SkCanvas
  330. @param srcX offset into readable pixels on x-axis; may be negative
  331. @param srcY offset into readable pixels on y-axis; may be negative
  332. @return true if pixels were copied
  333. */
  334. bool readPixels(const SkBitmap& bitmap, int srcX, int srcY);
  335. /** Copies SkRect from pixels to SkCanvas. SkMatrix and clip are ignored.
  336. Source SkRect corners are (0, 0) and (info.width(), info.height()).
  337. Destination SkRect corners are (x, y) and
  338. (imageInfo().width(), imageInfo().height()).
  339. Copies each readable pixel intersecting both rectangles, without scaling,
  340. converting to imageInfo().colorType() and imageInfo().alphaType() if required.
  341. Pixels are writable when SkBaseDevice is raster, or backed by a GPU.
  342. Pixels are not writable when SkCanvas is returned by SkDocument::beginPage,
  343. returned by SkPictureRecorder::beginRecording, or SkCanvas is the base of a utility
  344. class like SkDebugCanvas.
  345. Pixel values are converted only if SkColorType and SkAlphaType
  346. do not match. Only pixels within both source and destination rectangles
  347. are copied. SkCanvas pixels outside SkRect intersection are unchanged.
  348. Pass negative values for x or y to offset pixels to the left or
  349. above SkCanvas pixels.
  350. Does not copy, and returns false if:
  351. - Source and destination rectangles do not intersect.
  352. - pixels could not be converted to SkCanvas imageInfo().colorType() or
  353. imageInfo().alphaType().
  354. - SkCanvas pixels are not writable; for instance, SkCanvas is document-based.
  355. - rowBytes is too small to contain one row of pixels.
  356. @param info width, height, SkColorType, and SkAlphaType of pixels
  357. @param pixels pixels to copy, of size info.height() times rowBytes, or larger
  358. @param rowBytes size of one row of pixels; info.width() times pixel size, or larger
  359. @param x offset into SkCanvas writable pixels on x-axis; may be negative
  360. @param y offset into SkCanvas writable pixels on y-axis; may be negative
  361. @return true if pixels were written to SkCanvas
  362. */
  363. bool writePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes, int x, int y);
  364. /** Copies SkRect from pixels to SkCanvas. SkMatrix and clip are ignored.
  365. Source SkRect corners are (0, 0) and (bitmap.width(), bitmap.height()).
  366. Destination SkRect corners are (x, y) and
  367. (imageInfo().width(), imageInfo().height()).
  368. Copies each readable pixel intersecting both rectangles, without scaling,
  369. converting to imageInfo().colorType() and imageInfo().alphaType() if required.
  370. Pixels are writable when SkBaseDevice is raster, or backed by a GPU.
  371. Pixels are not writable when SkCanvas is returned by SkDocument::beginPage,
  372. returned by SkPictureRecorder::beginRecording, or SkCanvas is the base of a utility
  373. class like SkDebugCanvas.
  374. Pixel values are converted only if SkColorType and SkAlphaType
  375. do not match. Only pixels within both source and destination rectangles
  376. are copied. SkCanvas pixels outside SkRect intersection are unchanged.
  377. Pass negative values for x or y to offset pixels to the left or
  378. above SkCanvas pixels.
  379. Does not copy, and returns false if:
  380. - Source and destination rectangles do not intersect.
  381. - bitmap does not have allocated pixels.
  382. - bitmap pixels could not be converted to SkCanvas imageInfo().colorType() or
  383. imageInfo().alphaType().
  384. - SkCanvas pixels are not writable; for instance, SkCanvas is document based.
  385. - bitmap pixels are inaccessible; for instance, bitmap wraps a texture.
  386. @param bitmap contains pixels copied to SkCanvas
  387. @param x offset into SkCanvas writable pixels on x-axis; may be negative
  388. @param y offset into SkCanvas writable pixels on y-axis; may be negative
  389. @return true if pixels were written to SkCanvas
  390. */
  391. bool writePixels(const SkBitmap& bitmap, int x, int y);
  392. /** Saves SkMatrix and clip.
  393. Calling restore() discards changes to SkMatrix and clip,
  394. restoring the SkMatrix and clip to their state when save() was called.
  395. SkMatrix may be changed by translate(), scale(), rotate(), skew(), concat(), setMatrix(),
  396. and resetMatrix(). Clip may be changed by clipRect(), clipRRect(), clipPath(), clipRegion().
  397. Saved SkCanvas state is put on a stack; multiple calls to save() should be balance
  398. by an equal number of calls to restore().
  399. Call restoreToCount() with result to restore this and subsequent saves.
  400. @return depth of saved stack
  401. */
  402. int save();
  403. /** Saves SkMatrix and clip, and allocates a SkBitmap for subsequent drawing.
  404. Calling restore() discards changes to SkMatrix and clip, and draws the SkBitmap.
  405. SkMatrix may be changed by translate(), scale(), rotate(), skew(), concat(),
  406. setMatrix(), and resetMatrix(). Clip may be changed by clipRect(), clipRRect(),
  407. clipPath(), clipRegion().
  408. SkRect bounds suggests but does not define the SkBitmap size. To clip drawing to
  409. a specific rectangle, use clipRect().
  410. Optional SkPaint paint applies alpha, SkColorFilter, SkImageFilter, and
  411. SkBlendMode when restore() is called.
  412. Call restoreToCount() with returned value to restore this and subsequent saves.
  413. @param bounds hint to limit the size of the layer; may be nullptr
  414. @param paint graphics state for layer; may be nullptr
  415. @return depth of saved stack
  416. */
  417. int saveLayer(const SkRect* bounds, const SkPaint* paint);
  418. /** Saves SkMatrix and clip, and allocates a SkBitmap for subsequent drawing.
  419. Calling restore() discards changes to SkMatrix and clip, and draws the SkBitmap.
  420. SkMatrix may be changed by translate(), scale(), rotate(), skew(), concat(),
  421. setMatrix(), and resetMatrix(). Clip may be changed by clipRect(), clipRRect(),
  422. clipPath(), clipRegion().
  423. SkRect bounds suggests but does not define the layer size. To clip drawing to
  424. a specific rectangle, use clipRect().
  425. Optional SkPaint paint applies alpha, SkColorFilter, SkImageFilter, and
  426. SkBlendMode when restore() is called.
  427. Call restoreToCount() with returned value to restore this and subsequent saves.
  428. @param bounds hint to limit the size of layer; may be nullptr
  429. @param paint graphics state for layer; may be nullptr
  430. @return depth of saved stack
  431. */
  432. int saveLayer(const SkRect& bounds, const SkPaint* paint) {
  433. return this->saveLayer(&bounds, paint);
  434. }
  435. /** Saves SkMatrix and clip, and allocates a SkBitmap for subsequent drawing.
  436. LCD text is preserved when the layer is drawn to the prior layer.
  437. Calling restore() discards changes to SkMatrix and clip, and draws layer.
  438. SkMatrix may be changed by translate(), scale(), rotate(), skew(), concat(),
  439. setMatrix(), and resetMatrix(). Clip may be changed by clipRect(), clipRRect(),
  440. clipPath(), clipRegion().
  441. SkRect bounds suggests but does not define the layer size. To clip drawing to
  442. a specific rectangle, use clipRect().
  443. Optional SkPaint paint applies alpha, SkColorFilter, SkImageFilter, and
  444. SkBlendMode when restore() is called.
  445. Call restoreToCount() with returned value to restore this and subsequent saves.
  446. Draw text on an opaque background so that LCD text blends correctly with the
  447. prior layer. LCD text drawn on a background with transparency may result in
  448. incorrect blending.
  449. @param bounds hint to limit the size of layer; may be nullptr
  450. @param paint graphics state for layer; may be nullptr
  451. @return depth of saved stack
  452. */
  453. int saveLayerPreserveLCDTextRequests(const SkRect* bounds, const SkPaint* paint);
  454. /** Saves SkMatrix and clip, and allocates SkBitmap for subsequent drawing.
  455. Calling restore() discards changes to SkMatrix and clip,
  456. and blends layer with alpha opacity onto prior layer.
  457. SkMatrix may be changed by translate(), scale(), rotate(), skew(), concat(),
  458. setMatrix(), and resetMatrix(). Clip may be changed by clipRect(), clipRRect(),
  459. clipPath(), clipRegion().
  460. SkRect bounds suggests but does not define layer size. To clip drawing to
  461. a specific rectangle, use clipRect().
  462. alpha of zero is fully transparent, 255 is fully opaque.
  463. Call restoreToCount() with returned value to restore this and subsequent saves.
  464. @param bounds hint to limit the size of layer; may be nullptr
  465. @param alpha opacity of layer
  466. @return depth of saved stack
  467. */
  468. int saveLayerAlpha(const SkRect* bounds, U8CPU alpha);
  469. /** \enum SkCanvas::SaveLayerFlagsSet
  470. SaveLayerFlags provides options that may be used in any combination in SaveLayerRec,
  471. defining how layer allocated by saveLayer() operates. It may be set to zero,
  472. kPreserveLCDText_SaveLayerFlag, kInitWithPrevious_SaveLayerFlag, or both flags.
  473. */
  474. enum SaveLayerFlagsSet {
  475. kPreserveLCDText_SaveLayerFlag = 1 << 1, //!< creates layer for LCD text
  476. kInitWithPrevious_SaveLayerFlag = 1 << 2, //!< initializes with previous contents
  477. kMaskAgainstCoverage_EXPERIMENTAL_DONT_USE_SaveLayerFlag =
  478. 1 << 3, //!< experimental: do not use
  479. #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
  480. kDontClipToLayer_Legacy_SaveLayerFlag =
  481. kDontClipToLayer_PrivateSaveLayerFlag, //!< deprecated
  482. #endif
  483. };
  484. typedef uint32_t SaveLayerFlags;
  485. /** \struct SkCanvas::SaveLayerRec
  486. SaveLayerRec contains the state used to create the layer.
  487. */
  488. struct SaveLayerRec {
  489. /** Sets fBounds, fPaint, and fBackdrop to nullptr. Clears fSaveLayerFlags.
  490. @return empty SaveLayerRec
  491. */
  492. SaveLayerRec() {}
  493. /** Sets fBounds, fPaint, and fSaveLayerFlags; sets fBackdrop to nullptr.
  494. @param bounds layer dimensions; may be nullptr
  495. @param paint applied to layer when overlaying prior layer; may be nullptr
  496. @param saveLayerFlags SaveLayerRec options to modify layer
  497. @return SaveLayerRec with empty fBackdrop
  498. */
  499. SaveLayerRec(const SkRect* bounds, const SkPaint* paint, SaveLayerFlags saveLayerFlags = 0)
  500. : fBounds(bounds)
  501. , fPaint(paint)
  502. , fSaveLayerFlags(saveLayerFlags)
  503. {}
  504. /** Sets fBounds, fPaint, fBackdrop, and fSaveLayerFlags.
  505. @param bounds layer dimensions; may be nullptr
  506. @param paint applied to layer when overlaying prior layer;
  507. may be nullptr
  508. @param backdrop prior layer copied with SkImageFilter; may be nullptr
  509. @param saveLayerFlags SaveLayerRec options to modify layer
  510. @return SaveLayerRec fully specified
  511. */
  512. SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop,
  513. SaveLayerFlags saveLayerFlags)
  514. : fBounds(bounds)
  515. , fPaint(paint)
  516. , fBackdrop(backdrop)
  517. , fSaveLayerFlags(saveLayerFlags)
  518. {}
  519. /** Experimental. Not ready for general use.
  520. Sets fBounds, fPaint, fBackdrop, fClipMask, fClipMatrix, and fSaveLayerFlags.
  521. clipMatrix uses alpha channel of image, transformed by clipMatrix, to clip
  522. layer when drawn to SkCanvas.
  523. Implementation is not complete; has no effect if SkBaseDevice is GPU-backed.
  524. @param bounds layer dimensions; may be nullptr
  525. @param paint graphics state applied to layer when overlaying prior
  526. layer; may be nullptr
  527. @param backdrop prior layer copied with SkImageFilter;
  528. may be nullptr
  529. @param clipMask clip applied to layer; may be nullptr
  530. @param clipMatrix matrix applied to clipMask; may be nullptr to use
  531. identity matrix
  532. @param saveLayerFlags SaveLayerRec options to modify layer
  533. @return SaveLayerRec fully specified
  534. */
  535. SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop,
  536. const SkImage* clipMask, const SkMatrix* clipMatrix,
  537. SaveLayerFlags saveLayerFlags)
  538. : fBounds(bounds)
  539. , fPaint(paint)
  540. , fBackdrop(backdrop)
  541. , fClipMask(clipMask)
  542. , fClipMatrix(clipMatrix)
  543. , fSaveLayerFlags(saveLayerFlags)
  544. {}
  545. /** hints at layer size limit */
  546. const SkRect* fBounds = nullptr;
  547. /** modifies overlay */
  548. const SkPaint* fPaint = nullptr;
  549. /** applies SkImageFilter to prior layer */
  550. const SkImageFilter* fBackdrop = nullptr;
  551. /** clips layer with mask alpha */
  552. const SkImage* fClipMask = nullptr;
  553. /** transforms mask alpha used to clip */
  554. const SkMatrix* fClipMatrix = nullptr;
  555. /** preserves LCD text, creates with prior layer contents */
  556. SaveLayerFlags fSaveLayerFlags = 0;
  557. };
  558. /** Saves SkMatrix and clip, and allocates SkBitmap for subsequent drawing.
  559. Calling restore() discards changes to SkMatrix and clip,
  560. and blends SkBitmap with alpha opacity onto the prior layer.
  561. SkMatrix may be changed by translate(), scale(), rotate(), skew(), concat(),
  562. setMatrix(), and resetMatrix(). Clip may be changed by clipRect(), clipRRect(),
  563. clipPath(), clipRegion().
  564. SaveLayerRec contains the state used to create the layer.
  565. Call restoreToCount() with returned value to restore this and subsequent saves.
  566. @param layerRec layer state
  567. @return depth of save state stack
  568. */
  569. int saveLayer(const SaveLayerRec& layerRec);
  570. /** Removes changes to SkMatrix and clip since SkCanvas state was
  571. last saved. The state is removed from the stack.
  572. Does nothing if the stack is empty.
  573. */
  574. void restore();
  575. /** Returns the number of saved states, each containing: SkMatrix and clip.
  576. Equals the number of save() calls less the number of restore() calls plus one.
  577. The save count of a new canvas is one.
  578. @return depth of save state stack
  579. */
  580. int getSaveCount() const;
  581. /** Restores state to SkMatrix and clip values when save(), saveLayer(),
  582. saveLayerPreserveLCDTextRequests(), or saveLayerAlpha() returned saveCount.
  583. Does nothing if saveCount is greater than state stack count.
  584. Restores state to initial values if saveCount is less than or equal to one.
  585. @param saveCount depth of state stack to restore
  586. */
  587. void restoreToCount(int saveCount);
  588. /** Translates SkMatrix by dx along the x-axis and dy along the y-axis.
  589. Mathematically, replaces SkMatrix with a translation matrix
  590. premultiplied with SkMatrix.
  591. This has the effect of moving the drawing by (dx, dy) before transforming
  592. the result with SkMatrix.
  593. @param dx distance to translate on x-axis
  594. @param dy distance to translate on y-axis
  595. */
  596. void translate(SkScalar dx, SkScalar dy);
  597. /** Scales SkMatrix by sx on the x-axis and sy on the y-axis.
  598. Mathematically, replaces SkMatrix with a scale matrix
  599. premultiplied with SkMatrix.
  600. This has the effect of scaling the drawing by (sx, sy) before transforming
  601. the result with SkMatrix.
  602. @param sx amount to scale on x-axis
  603. @param sy amount to scale on y-axis
  604. */
  605. void scale(SkScalar sx, SkScalar sy);
  606. /** Rotates SkMatrix by degrees. Positive degrees rotates clockwise.
  607. Mathematically, replaces SkMatrix with a rotation matrix
  608. premultiplied with SkMatrix.
  609. This has the effect of rotating the drawing by degrees before transforming
  610. the result with SkMatrix.
  611. @param degrees amount to rotate, in degrees
  612. */
  613. void rotate(SkScalar degrees);
  614. /** Rotates SkMatrix by degrees about a point at (px, py). Positive degrees rotates
  615. clockwise.
  616. Mathematically, constructs a rotation matrix; premultiplies the rotation matrix by
  617. a translation matrix; then replaces SkMatrix with the resulting matrix
  618. premultiplied with SkMatrix.
  619. This has the effect of rotating the drawing about a given point before
  620. transforming the result with SkMatrix.
  621. @param degrees amount to rotate, in degrees
  622. @param px x-axis value of the point to rotate about
  623. @param py y-axis value of the point to rotate about
  624. */
  625. void rotate(SkScalar degrees, SkScalar px, SkScalar py);
  626. /** Skews SkMatrix by sx on the x-axis and sy on the y-axis. A positive value of sx
  627. skews the drawing right as y-axis values increase; a positive value of sy skews
  628. the drawing down as x-axis values increase.
  629. Mathematically, replaces SkMatrix with a skew matrix premultiplied with SkMatrix.
  630. This has the effect of skewing the drawing by (sx, sy) before transforming
  631. the result with SkMatrix.
  632. @param sx amount to skew on x-axis
  633. @param sy amount to skew on y-axis
  634. */
  635. void skew(SkScalar sx, SkScalar sy);
  636. /** Replaces SkMatrix with matrix premultiplied with existing SkMatrix.
  637. This has the effect of transforming the drawn geometry by matrix, before
  638. transforming the result with existing SkMatrix.
  639. @param matrix matrix to premultiply with existing SkMatrix
  640. */
  641. void concat(const SkMatrix& matrix);
  642. /** Replaces SkMatrix with matrix.
  643. Unlike concat(), any prior matrix state is overwritten.
  644. @param matrix matrix to copy, replacing existing SkMatrix
  645. */
  646. void setMatrix(const SkMatrix& matrix);
  647. /** Sets SkMatrix to the identity matrix.
  648. Any prior matrix state is overwritten.
  649. */
  650. void resetMatrix();
  651. /** Replaces clip with the intersection or difference of clip and rect,
  652. with an aliased or anti-aliased clip edge. rect is transformed by SkMatrix
  653. before it is combined with clip.
  654. @param rect SkRect to combine with clip
  655. @param op SkClipOp to apply to clip
  656. @param doAntiAlias true if clip is to be anti-aliased
  657. */
  658. void clipRect(const SkRect& rect, SkClipOp op, bool doAntiAlias);
  659. /** Replaces clip with the intersection or difference of clip and rect.
  660. Resulting clip is aliased; pixels are fully contained by the clip.
  661. rect is transformed by SkMatrix before it is combined with clip.
  662. @param rect SkRect to combine with clip
  663. @param op SkClipOp to apply to clip
  664. */
  665. void clipRect(const SkRect& rect, SkClipOp op) {
  666. this->clipRect(rect, op, false);
  667. }
  668. /** Replaces clip with the intersection of clip and rect.
  669. Resulting clip is aliased; pixels are fully contained by the clip.
  670. rect is transformed by SkMatrix
  671. before it is combined with clip.
  672. @param rect SkRect to combine with clip
  673. @param doAntiAlias true if clip is to be anti-aliased
  674. */
  675. void clipRect(const SkRect& rect, bool doAntiAlias = false) {
  676. this->clipRect(rect, SkClipOp::kIntersect, doAntiAlias);
  677. }
  678. /** Sets the maximum clip rectangle, which can be set by clipRect(), clipRRect() and
  679. clipPath() and intersect the current clip with the specified rect.
  680. The maximum clip affects only future clipping operations; it is not retroactive.
  681. The clip restriction is not recorded in pictures.
  682. Pass an empty rect to disable maximum clip.
  683. This private API is for use by Android framework only.
  684. @param rect maximum allowed clip in device coordinates
  685. */
  686. void androidFramework_setDeviceClipRestriction(const SkIRect& rect);
  687. /** Replaces clip with the intersection or difference of clip and rrect,
  688. with an aliased or anti-aliased clip edge.
  689. rrect is transformed by SkMatrix
  690. before it is combined with clip.
  691. @param rrect SkRRect to combine with clip
  692. @param op SkClipOp to apply to clip
  693. @param doAntiAlias true if clip is to be anti-aliased
  694. */
  695. void clipRRect(const SkRRect& rrect, SkClipOp op, bool doAntiAlias);
  696. /** Replaces clip with the intersection or difference of clip and rrect.
  697. Resulting clip is aliased; pixels are fully contained by the clip.
  698. rrect is transformed by SkMatrix before it is combined with clip.
  699. @param rrect SkRRect to combine with clip
  700. @param op SkClipOp to apply to clip
  701. */
  702. void clipRRect(const SkRRect& rrect, SkClipOp op) {
  703. this->clipRRect(rrect, op, false);
  704. }
  705. /** Replaces clip with the intersection of clip and rrect,
  706. with an aliased or anti-aliased clip edge.
  707. rrect is transformed by SkMatrix before it is combined with clip.
  708. @param rrect SkRRect to combine with clip
  709. @param doAntiAlias true if clip is to be anti-aliased
  710. */
  711. void clipRRect(const SkRRect& rrect, bool doAntiAlias = false) {
  712. this->clipRRect(rrect, SkClipOp::kIntersect, doAntiAlias);
  713. }
  714. /** Replaces clip with the intersection or difference of clip and path,
  715. with an aliased or anti-aliased clip edge. SkPath::FillType determines if path
  716. describes the area inside or outside its contours; and if path contour overlaps
  717. itself or another path contour, whether the overlaps form part of the area.
  718. path is transformed by SkMatrix before it is combined with clip.
  719. @param path SkPath to combine with clip
  720. @param op SkClipOp to apply to clip
  721. @param doAntiAlias true if clip is to be anti-aliased
  722. */
  723. void clipPath(const SkPath& path, SkClipOp op, bool doAntiAlias);
  724. /** Replaces clip with the intersection or difference of clip and path.
  725. Resulting clip is aliased; pixels are fully contained by the clip.
  726. SkPath::FillType determines if path
  727. describes the area inside or outside its contours; and if path contour overlaps
  728. itself or another path contour, whether the overlaps form part of the area.
  729. path is transformed by SkMatrix
  730. before it is combined with clip.
  731. @param path SkPath to combine with clip
  732. @param op SkClipOp to apply to clip
  733. */
  734. void clipPath(const SkPath& path, SkClipOp op) {
  735. this->clipPath(path, op, false);
  736. }
  737. /** Replaces clip with the intersection of clip and path.
  738. Resulting clip is aliased; pixels are fully contained by the clip.
  739. SkPath::FillType determines if path
  740. describes the area inside or outside its contours; and if path contour overlaps
  741. itself or another path contour, whether the overlaps form part of the area.
  742. path is transformed by SkMatrix before it is combined with clip.
  743. @param path SkPath to combine with clip
  744. @param doAntiAlias true if clip is to be anti-aliased
  745. */
  746. void clipPath(const SkPath& path, bool doAntiAlias = false) {
  747. this->clipPath(path, SkClipOp::kIntersect, doAntiAlias);
  748. }
  749. /** Experimental. For testing only.
  750. Set to simplify clip stack using PathOps.
  751. */
  752. void setAllowSimplifyClip(bool allow) {
  753. fAllowSimplifyClip = allow;
  754. }
  755. /** Replaces clip with the intersection or difference of clip and SkRegion deviceRgn.
  756. Resulting clip is aliased; pixels are fully contained by the clip.
  757. deviceRgn is unaffected by SkMatrix.
  758. @param deviceRgn SkRegion to combine with clip
  759. @param op SkClipOp to apply to clip
  760. */
  761. void clipRegion(const SkRegion& deviceRgn, SkClipOp op = SkClipOp::kIntersect);
  762. /** Returns true if SkRect rect, transformed by SkMatrix, can be quickly determined to be
  763. outside of clip. May return false even though rect is outside of clip.
  764. Use to check if an area to be drawn is clipped out, to skip subsequent draw calls.
  765. @param rect SkRect to compare with clip
  766. @return true if rect, transformed by SkMatrix, does not intersect clip
  767. */
  768. bool quickReject(const SkRect& rect) const;
  769. /** Returns true if path, transformed by SkMatrix, can be quickly determined to be
  770. outside of clip. May return false even though path is outside of clip.
  771. Use to check if an area to be drawn is clipped out, to skip subsequent draw calls.
  772. @param path SkPath to compare with clip
  773. @return true if path, transformed by SkMatrix, does not intersect clip
  774. */
  775. bool quickReject(const SkPath& path) const;
  776. /** Returns bounds of clip, transformed by inverse of SkMatrix. If clip is empty,
  777. return SkRect::MakeEmpty, where all SkRect sides equal zero.
  778. SkRect returned is outset by one to account for partial pixel coverage if clip
  779. is anti-aliased.
  780. @return bounds of clip in local coordinates
  781. */
  782. SkRect getLocalClipBounds() const;
  783. /** Returns bounds of clip, transformed by inverse of SkMatrix. If clip is empty,
  784. return false, and set bounds to SkRect::MakeEmpty, where all SkRect sides equal zero.
  785. bounds is outset by one to account for partial pixel coverage if clip
  786. is anti-aliased.
  787. @param bounds SkRect of clip in local coordinates
  788. @return true if clip bounds is not empty
  789. */
  790. bool getLocalClipBounds(SkRect* bounds) const {
  791. *bounds = this->getLocalClipBounds();
  792. return !bounds->isEmpty();
  793. }
  794. /** Returns SkIRect bounds of clip, unaffected by SkMatrix. If clip is empty,
  795. return SkRect::MakeEmpty, where all SkRect sides equal zero.
  796. Unlike getLocalClipBounds(), returned SkIRect is not outset.
  797. @return bounds of clip in SkBaseDevice coordinates
  798. */
  799. SkIRect getDeviceClipBounds() const;
  800. /** Returns SkIRect bounds of clip, unaffected by SkMatrix. If clip is empty,
  801. return false, and set bounds to SkRect::MakeEmpty, where all SkRect sides equal zero.
  802. Unlike getLocalClipBounds(), bounds is not outset.
  803. @param bounds SkRect of clip in device coordinates
  804. @return true if clip bounds is not empty
  805. */
  806. bool getDeviceClipBounds(SkIRect* bounds) const {
  807. *bounds = this->getDeviceClipBounds();
  808. return !bounds->isEmpty();
  809. }
  810. /** Fills clip with color color.
  811. mode determines how ARGB is combined with destination.
  812. @param color unpremultiplied ARGB
  813. @param mode SkBlendMode used to combine source color and destination
  814. */
  815. void drawColor(SkColor color, SkBlendMode mode = SkBlendMode::kSrcOver);
  816. /** Fills clip with color color using SkBlendMode::kSrc.
  817. This has the effect of replacing all pixels contained by clip with color.
  818. @param color unpremultiplied ARGB
  819. */
  820. void clear(SkColor color) {
  821. this->drawColor(color, SkBlendMode::kSrc);
  822. }
  823. /** Makes SkCanvas contents undefined. Subsequent calls that read SkCanvas pixels,
  824. such as drawing with SkBlendMode, return undefined results. discard() does
  825. not change clip or SkMatrix.
  826. discard() may do nothing, depending on the implementation of SkSurface or SkBaseDevice
  827. that created SkCanvas.
  828. discard() allows optimized performance on subsequent draws by removing
  829. cached data associated with SkSurface or SkBaseDevice.
  830. It is not necessary to call discard() once done with SkCanvas;
  831. any cached data is deleted when owning SkSurface or SkBaseDevice is deleted.
  832. */
  833. void discard() { this->onDiscard(); }
  834. /** Fills clip with SkPaint paint. SkPaint components SkMaskFilter, SkShader,
  835. SkColorFilter, SkImageFilter, and SkBlendMode affect drawing;
  836. SkPathEffect in paint is ignored.
  837. @param paint graphics state used to fill SkCanvas
  838. */
  839. void drawPaint(const SkPaint& paint);
  840. /** \enum SkCanvas::PointMode
  841. Selects if an array of points are drawn as discrete points, as lines, or as
  842. an open polygon.
  843. */
  844. enum PointMode {
  845. kPoints_PointMode, //!< draw each point separately
  846. kLines_PointMode, //!< draw each pair of points as a line segment
  847. kPolygon_PointMode, //!< draw the array of points as a open polygon
  848. };
  849. /** Draws pts using clip, SkMatrix and SkPaint paint.
  850. count is the number of points; if count is less than one, has no effect.
  851. mode may be one of: kPoints_PointMode, kLines_PointMode, or kPolygon_PointMode.
  852. If mode is kPoints_PointMode, the shape of point drawn depends on paint
  853. SkPaint::Cap. If paint is set to SkPaint::kRound_Cap, each point draws a
  854. circle of diameter SkPaint stroke width. If paint is set to SkPaint::kSquare_Cap
  855. or SkPaint::kButt_Cap, each point draws a square of width and height
  856. SkPaint stroke width.
  857. If mode is kLines_PointMode, each pair of points draws a line segment.
  858. One line is drawn for every two points; each point is used once. If count is odd,
  859. the final point is ignored.
  860. If mode is kPolygon_PointMode, each adjacent pair of points draws a line segment.
  861. count minus one lines are drawn; the first and last point are used once.
  862. Each line segment respects paint SkPaint::Cap and SkPaint stroke width.
  863. SkPaint::Style is ignored, as if were set to SkPaint::kStroke_Style.
  864. Always draws each element one at a time; is not affected by
  865. SkPaint::Join, and unlike drawPath(), does not create a mask from all points
  866. and lines before drawing.
  867. @param mode whether pts draws points or lines
  868. @param count number of points in the array
  869. @param pts array of points to draw
  870. @param paint stroke, blend, color, and so on, used to draw
  871. */
  872. void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint);
  873. /** Draws point at (x, y) using clip, SkMatrix and SkPaint paint.
  874. The shape of point drawn depends on paint SkPaint::Cap.
  875. If paint is set to SkPaint::kRound_Cap, draw a circle of diameter
  876. SkPaint stroke width. If paint is set to SkPaint::kSquare_Cap or SkPaint::kButt_Cap,
  877. draw a square of width and height SkPaint stroke width.
  878. SkPaint::Style is ignored, as if were set to SkPaint::kStroke_Style.
  879. @param x left edge of circle or square
  880. @param y top edge of circle or square
  881. @param paint stroke, blend, color, and so on, used to draw
  882. */
  883. void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint);
  884. /** Draws point p using clip, SkMatrix and SkPaint paint.
  885. The shape of point drawn depends on paint SkPaint::Cap.
  886. If paint is set to SkPaint::kRound_Cap, draw a circle of diameter
  887. SkPaint stroke width. If paint is set to SkPaint::kSquare_Cap or SkPaint::kButt_Cap,
  888. draw a square of width and height SkPaint stroke width.
  889. SkPaint::Style is ignored, as if were set to SkPaint::kStroke_Style.
  890. @param p top-left edge of circle or square
  891. @param paint stroke, blend, color, and so on, used to draw
  892. */
  893. void drawPoint(SkPoint p, const SkPaint& paint) {
  894. this->drawPoint(p.x(), p.y(), paint);
  895. }
  896. /** Draws line segment from (x0, y0) to (x1, y1) using clip, SkMatrix, and SkPaint paint.
  897. In paint: SkPaint stroke width describes the line thickness;
  898. SkPaint::Cap draws the end rounded or square;
  899. SkPaint::Style is ignored, as if were set to SkPaint::kStroke_Style.
  900. @param x0 start of line segment on x-axis
  901. @param y0 start of line segment on y-axis
  902. @param x1 end of line segment on x-axis
  903. @param y1 end of line segment on y-axis
  904. @param paint stroke, blend, color, and so on, used to draw
  905. */
  906. void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint& paint);
  907. /** Draws line segment from p0 to p1 using clip, SkMatrix, and SkPaint paint.
  908. In paint: SkPaint stroke width describes the line thickness;
  909. SkPaint::Cap draws the end rounded or square;
  910. SkPaint::Style is ignored, as if were set to SkPaint::kStroke_Style.
  911. @param p0 start of line segment
  912. @param p1 end of line segment
  913. @param paint stroke, blend, color, and so on, used to draw
  914. */
  915. void drawLine(SkPoint p0, SkPoint p1, const SkPaint& paint) {
  916. this->drawLine(p0.x(), p0.y(), p1.x(), p1.y(), paint);
  917. }
  918. /** Draws SkRect rect using clip, SkMatrix, and SkPaint paint.
  919. In paint: SkPaint::Style determines if rectangle is stroked or filled;
  920. if stroked, SkPaint stroke width describes the line thickness, and
  921. SkPaint::Join draws the corners rounded or square.
  922. @param rect rectangle to draw
  923. @param paint stroke or fill, blend, color, and so on, used to draw
  924. */
  925. void drawRect(const SkRect& rect, const SkPaint& paint);
  926. /** Draws SkIRect rect using clip, SkMatrix, and SkPaint paint.
  927. In paint: SkPaint::Style determines if rectangle is stroked or filled;
  928. if stroked, SkPaint stroke width describes the line thickness, and
  929. SkPaint::Join draws the corners rounded or square.
  930. @param rect rectangle to draw
  931. @param paint stroke or fill, blend, color, and so on, used to draw
  932. */
  933. void drawIRect(const SkIRect& rect, const SkPaint& paint) {
  934. SkRect r;
  935. r.set(rect); // promotes the ints to scalars
  936. this->drawRect(r, paint);
  937. }
  938. /** Draws SkRegion region using clip, SkMatrix, and SkPaint paint.
  939. In paint: SkPaint::Style determines if rectangle is stroked or filled;
  940. if stroked, SkPaint stroke width describes the line thickness, and
  941. SkPaint::Join draws the corners rounded or square.
  942. @param region region to draw
  943. @param paint SkPaint stroke or fill, blend, color, and so on, used to draw
  944. */
  945. void drawRegion(const SkRegion& region, const SkPaint& paint);
  946. /** Draws oval oval using clip, SkMatrix, and SkPaint.
  947. In paint: SkPaint::Style determines if oval is stroked or filled;
  948. if stroked, SkPaint stroke width describes the line thickness.
  949. @param oval SkRect bounds of oval
  950. @param paint SkPaint stroke or fill, blend, color, and so on, used to draw
  951. */
  952. void drawOval(const SkRect& oval, const SkPaint& paint);
  953. /** Draws SkRRect rrect using clip, SkMatrix, and SkPaint paint.
  954. In paint: SkPaint::Style determines if rrect is stroked or filled;
  955. if stroked, SkPaint stroke width describes the line thickness.
  956. rrect may represent a rectangle, circle, oval, uniformly rounded rectangle, or
  957. may have any combination of positive non-square radii for the four corners.
  958. @param rrect SkRRect with up to eight corner radii to draw
  959. @param paint SkPaint stroke or fill, blend, color, and so on, used to draw
  960. */
  961. void drawRRect(const SkRRect& rrect, const SkPaint& paint);
  962. /** Draws SkRRect outer and inner
  963. using clip, SkMatrix, and SkPaint paint.
  964. outer must contain inner or the drawing is undefined.
  965. In paint: SkPaint::Style determines if SkRRect is stroked or filled;
  966. if stroked, SkPaint stroke width describes the line thickness.
  967. If stroked and SkRRect corner has zero length radii, SkPaint::Join can
  968. draw corners rounded or square.
  969. GPU-backed platforms optimize drawing when both outer and inner are
  970. concave and outer contains inner. These platforms may not be able to draw
  971. SkPath built with identical data as fast.
  972. @param outer SkRRect outer bounds to draw
  973. @param inner SkRRect inner bounds to draw
  974. @param paint SkPaint stroke or fill, blend, color, and so on, used to draw
  975. */
  976. void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint);
  977. /** Draws circle at (cx, cy) with radius using clip, SkMatrix, and SkPaint paint.
  978. If radius is zero or less, nothing is drawn.
  979. In paint: SkPaint::Style determines if circle is stroked or filled;
  980. if stroked, SkPaint stroke width describes the line thickness.
  981. @param cx circle center on the x-axis
  982. @param cy circle center on the y-axis
  983. @param radius half the diameter of circle
  984. @param paint SkPaint stroke or fill, blend, color, and so on, used to draw
  985. */
  986. void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint& paint);
  987. /** Draws circle at center with radius using clip, SkMatrix, and SkPaint paint.
  988. If radius is zero or less, nothing is drawn.
  989. In paint: SkPaint::Style determines if circle is stroked or filled;
  990. if stroked, SkPaint stroke width describes the line thickness.
  991. @param center circle center
  992. @param radius half the diameter of circle
  993. @param paint SkPaint stroke or fill, blend, color, and so on, used to draw
  994. */
  995. void drawCircle(SkPoint center, SkScalar radius, const SkPaint& paint) {
  996. this->drawCircle(center.x(), center.y(), radius, paint);
  997. }
  998. /** Draws arc using clip, SkMatrix, and SkPaint paint.
  999. Arc is part of oval bounded by oval, sweeping from startAngle to startAngle plus
  1000. sweepAngle. startAngle and sweepAngle are in degrees.
  1001. startAngle of zero places start point at the right middle edge of oval.
  1002. A positive sweepAngle places arc end point clockwise from start point;
  1003. a negative sweepAngle places arc end point counterclockwise from start point.
  1004. sweepAngle may exceed 360 degrees, a full circle.
  1005. If useCenter is true, draw a wedge that includes lines from oval
  1006. center to arc end points. If useCenter is false, draw arc between end points.
  1007. If SkRect oval is empty or sweepAngle is zero, nothing is drawn.
  1008. @param oval SkRect bounds of oval containing arc to draw
  1009. @param startAngle angle in degrees where arc begins
  1010. @param sweepAngle sweep angle in degrees; positive is clockwise
  1011. @param useCenter if true, include the center of the oval
  1012. @param paint SkPaint stroke or fill, blend, color, and so on, used to draw
  1013. */
  1014. void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
  1015. bool useCenter, const SkPaint& paint);
  1016. /** Draws SkRRect bounded by SkRect rect, with corner radii (rx, ry) using clip,
  1017. SkMatrix, and SkPaint paint.
  1018. In paint: SkPaint::Style determines if SkRRect is stroked or filled;
  1019. if stroked, SkPaint stroke width describes the line thickness.
  1020. If rx or ry are less than zero, they are treated as if they are zero.
  1021. If rx plus ry exceeds rect width or rect height, radii are scaled down to fit.
  1022. If rx and ry are zero, SkRRect is drawn as SkRect and if stroked is affected by
  1023. SkPaint::Join.
  1024. @param rect SkRect bounds of SkRRect to draw
  1025. @param rx axis length on x-axis of oval describing rounded corners
  1026. @param ry axis length on y-axis of oval describing rounded corners
  1027. @param paint stroke, blend, color, and so on, used to draw
  1028. */
  1029. void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry, const SkPaint& paint);
  1030. /** Draws SkPath path using clip, SkMatrix, and SkPaint paint.
  1031. SkPath contains an array of path contour, each of which may be open or closed.
  1032. In paint: SkPaint::Style determines if SkRRect is stroked or filled:
  1033. if filled, SkPath::FillType determines whether path contour describes inside or
  1034. outside of fill; if stroked, SkPaint stroke width describes the line thickness,
  1035. SkPaint::Cap describes line ends, and SkPaint::Join describes how
  1036. corners are drawn.
  1037. @param path SkPath to draw
  1038. @param paint stroke, blend, color, and so on, used to draw
  1039. */
  1040. void drawPath(const SkPath& path, const SkPaint& paint);
  1041. /** Draws SkImage image, with its top-left corner at (left, top),
  1042. using clip, SkMatrix, and optional SkPaint paint.
  1043. If paint is supplied, apply SkColorFilter, alpha, SkImageFilter, SkBlendMode,
  1044. and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader.
  1045. If paint contains SkMaskFilter, generate mask from image bounds. If generated
  1046. mask extends beyond image bounds, replicate image edge colors, just as SkShader
  1047. made from SkImage::makeShader with SkShader::kClamp_TileMode set replicates the
  1048. image edge color when it samples outside of its bounds.
  1049. @param image uncompressed rectangular map of pixels
  1050. @param left left side of image
  1051. @param top top side of image
  1052. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1053. and so on; or nullptr
  1054. */
  1055. void drawImage(const SkImage* image, SkScalar left, SkScalar top,
  1056. const SkPaint* paint = nullptr);
  1057. /** Draws SkImage image, with its top-left corner at (left, top),
  1058. using clip, SkMatrix, and optional SkPaint paint.
  1059. If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter,
  1060. SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader.
  1061. If paint contains SkMaskFilter, generate mask from image bounds. If generated
  1062. mask extends beyond image bounds, replicate image edge colors, just as SkShader
  1063. made from SkImage::makeShader with SkShader::kClamp_TileMode set replicates the
  1064. image edge color when it samples outside of its bounds.
  1065. @param image uncompressed rectangular map of pixels
  1066. @param left left side of image
  1067. @param top pop side of image
  1068. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1069. and so on; or nullptr
  1070. */
  1071. void drawImage(const sk_sp<SkImage>& image, SkScalar left, SkScalar top,
  1072. const SkPaint* paint = nullptr) {
  1073. this->drawImage(image.get(), left, top, paint);
  1074. }
  1075. /** \enum SkCanvas::SrcRectConstraint
  1076. SrcRectConstraint controls the behavior at the edge of source SkRect,
  1077. provided to drawImageRect(), trading off speed for precision.
  1078. SkFilterQuality in SkPaint may sample multiple pixels in the image. Source SkRect
  1079. restricts the bounds of pixels that may be read. SkFilterQuality may slow down if
  1080. it cannot read outside the bounds, when sampling near the edge of source SkRect.
  1081. SrcRectConstraint specifies whether an SkImageFilter is allowed to read pixels
  1082. outside source SkRect.
  1083. */
  1084. enum SrcRectConstraint {
  1085. kStrict_SrcRectConstraint, //!< sample only inside bounds; slower
  1086. kFast_SrcRectConstraint, //!< sample outside bounds; faster
  1087. };
  1088. /** Draws SkRect src of SkImage image, scaled and translated to fill SkRect dst.
  1089. Additionally transform draw using clip, SkMatrix, and optional SkPaint paint.
  1090. If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter,
  1091. SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader.
  1092. If paint contains SkMaskFilter, generate mask from image bounds.
  1093. If generated mask extends beyond image bounds, replicate image edge colors, just
  1094. as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set
  1095. replicates the image edge color when it samples outside of its bounds.
  1096. constraint set to kStrict_SrcRectConstraint limits SkPaint SkFilterQuality to
  1097. sample within src; set to kFast_SrcRectConstraint allows sampling outside to
  1098. improve performance.
  1099. @param image SkImage containing pixels, dimensions, and format
  1100. @param src source SkRect of image to draw from
  1101. @param dst destination SkRect of image to draw to
  1102. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1103. and so on; or nullptr
  1104. @param constraint filter strictly within src or draw faster
  1105. */
  1106. void drawImageRect(const SkImage* image, const SkRect& src, const SkRect& dst,
  1107. const SkPaint* paint,
  1108. SrcRectConstraint constraint = kStrict_SrcRectConstraint);
  1109. /** Draws SkIRect isrc of SkImage image, scaled and translated to fill SkRect dst.
  1110. Note that isrc is on integer pixel boundaries; dst may include fractional
  1111. boundaries. Additionally transform draw using clip, SkMatrix, and optional SkPaint
  1112. paint.
  1113. If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter,
  1114. SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader.
  1115. If paint contains SkMaskFilter, generate mask from image bounds.
  1116. If generated mask extends beyond image bounds, replicate image edge colors, just
  1117. as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set
  1118. replicates the image edge color when it samples outside of its bounds.
  1119. constraint set to kStrict_SrcRectConstraint limits SkPaint SkFilterQuality to
  1120. sample within isrc; set to kFast_SrcRectConstraint allows sampling outside to
  1121. improve performance.
  1122. @param image SkImage containing pixels, dimensions, and format
  1123. @param isrc source SkIRect of image to draw from
  1124. @param dst destination SkRect of image to draw to
  1125. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1126. and so on; or nullptr
  1127. @param constraint filter strictly within isrc or draw faster
  1128. */
  1129. void drawImageRect(const SkImage* image, const SkIRect& isrc, const SkRect& dst,
  1130. const SkPaint* paint,
  1131. SrcRectConstraint constraint = kStrict_SrcRectConstraint);
  1132. /** Draws SkImage image, scaled and translated to fill SkRect dst, using clip, SkMatrix,
  1133. and optional SkPaint paint.
  1134. If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter,
  1135. SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader.
  1136. If paint contains SkMaskFilter, generate mask from image bounds.
  1137. If generated mask extends beyond image bounds, replicate image edge colors, just
  1138. as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set
  1139. replicates the image edge color when it samples outside of its bounds.
  1140. @param image SkImage containing pixels, dimensions, and format
  1141. @param dst destination SkRect of image to draw to
  1142. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1143. and so on; or nullptr
  1144. */
  1145. void drawImageRect(const SkImage* image, const SkRect& dst, const SkPaint* paint);
  1146. /** Draws SkRect src of SkImage image, scaled and translated to fill SkRect dst.
  1147. Additionally transform draw using clip, SkMatrix, and optional SkPaint paint.
  1148. If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter,
  1149. SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader.
  1150. If paint contains SkMaskFilter, generate mask from image bounds.
  1151. If generated mask extends beyond image bounds, replicate image edge colors, just
  1152. as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set
  1153. replicates the image edge color when it samples outside of its bounds.
  1154. @param image SkImage containing pixels, dimensions, and format
  1155. @param src source SkRect of image to draw from
  1156. @param dst destination SkRect of image to draw to
  1157. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1158. and so on; or nullptr
  1159. @param constraint filter strictly within src or draw faster
  1160. */
  1161. void drawImageRect(const sk_sp<SkImage>& image, const SkRect& src, const SkRect& dst,
  1162. const SkPaint* paint,
  1163. SrcRectConstraint constraint = kStrict_SrcRectConstraint) {
  1164. this->drawImageRect(image.get(), src, dst, paint, constraint);
  1165. }
  1166. /** Draws SkIRect isrc of SkImage image, scaled and translated to fill SkRect dst.
  1167. isrc is on integer pixel boundaries; dst may include fractional boundaries.
  1168. Additionally transform draw using clip, SkMatrix, and optional SkPaint paint.
  1169. If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter,
  1170. SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader.
  1171. If paint contains SkMaskFilter, generate mask from image bounds.
  1172. If generated mask extends beyond image bounds, replicate image edge colors, just
  1173. as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set
  1174. replicates the image edge color when it samples outside of its bounds.
  1175. constraint set to kStrict_SrcRectConstraint limits SkPaint SkFilterQuality to
  1176. sample within image; set to kFast_SrcRectConstraint allows sampling outside to
  1177. improve performance.
  1178. @param image SkImage containing pixels, dimensions, and format
  1179. @param isrc source SkIRect of image to draw from
  1180. @param dst destination SkRect of image to draw to
  1181. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1182. and so on; or nullptr
  1183. @param constraint filter strictly within image or draw faster
  1184. */
  1185. void drawImageRect(const sk_sp<SkImage>& image, const SkIRect& isrc, const SkRect& dst,
  1186. const SkPaint* paint,
  1187. SrcRectConstraint constraint = kStrict_SrcRectConstraint) {
  1188. this->drawImageRect(image.get(), isrc, dst, paint, constraint);
  1189. }
  1190. /** Draws SkImage image, scaled and translated to fill SkRect dst,
  1191. using clip, SkMatrix, and optional SkPaint paint.
  1192. If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter,
  1193. SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader.
  1194. If paint contains SkMaskFilter, generate mask from image bounds.
  1195. If generated mask extends beyond image bounds, replicate image edge colors, just
  1196. as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set
  1197. replicates the image edge color when it samples outside of its bounds.
  1198. constraint set to kStrict_SrcRectConstraint limits SkPaint SkFilterQuality to
  1199. sample within image; set to kFast_SrcRectConstraint allows sampling outside to
  1200. improve performance.
  1201. @param image SkImage containing pixels, dimensions, and format
  1202. @param dst destination SkRect of image to draw to
  1203. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1204. and so on; or nullptr
  1205. */
  1206. void drawImageRect(const sk_sp<SkImage>& image, const SkRect& dst, const SkPaint* paint) {
  1207. this->drawImageRect(image.get(), dst, paint);
  1208. }
  1209. /** Draws SkImage image stretched proportionally to fit into SkRect dst.
  1210. SkIRect center divides the image into nine sections: four sides, four corners, and
  1211. the center. Corners are unmodified or scaled down proportionately if their sides
  1212. are larger than dst; center and four sides are scaled to fit remaining space, if any.
  1213. Additionally transform draw using clip, SkMatrix, and optional SkPaint paint.
  1214. If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter,
  1215. SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader.
  1216. If paint contains SkMaskFilter, generate mask from image bounds. If paint
  1217. SkFilterQuality set to kNone_SkFilterQuality, disable pixel filtering. For all
  1218. other values of paint SkFilterQuality, use kLow_SkFilterQuality to filter pixels.
  1219. Any SkMaskFilter on paint is ignored as is paint anti-aliasing state.
  1220. If generated mask extends beyond image bounds, replicate image edge colors, just
  1221. as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set
  1222. replicates the image edge color when it samples outside of its bounds.
  1223. @param image SkImage containing pixels, dimensions, and format
  1224. @param center SkIRect edge of image corners and sides
  1225. @param dst destination SkRect of image to draw to
  1226. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1227. and so on; or nullptr
  1228. */
  1229. void drawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst,
  1230. const SkPaint* paint = nullptr);
  1231. /** Draws SkImage image stretched proportionally to fit into SkRect dst.
  1232. SkIRect center divides the image into nine sections: four sides, four corners, and
  1233. the center. Corners are not scaled, or scaled down proportionately if their sides
  1234. are larger than dst; center and four sides are scaled to fit remaining space, if any.
  1235. Additionally transform draw using clip, SkMatrix, and optional SkPaint paint.
  1236. If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter,
  1237. SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader.
  1238. If paint contains SkMaskFilter, generate mask from image bounds. If paint
  1239. SkFilterQuality set to kNone_SkFilterQuality, disable pixel filtering. For all
  1240. other values of paint SkFilterQuality, use kLow_SkFilterQuality to filter pixels.
  1241. Any SkMaskFilter on paint is ignored as is paint anti-aliasing state.
  1242. If generated mask extends beyond image bounds, replicate image edge colors, just
  1243. as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set
  1244. replicates the image edge color when it samples outside of its bounds.
  1245. @param image SkImage containing pixels, dimensions, and format
  1246. @param center SkIRect edge of image corners and sides
  1247. @param dst destination SkRect of image to draw to
  1248. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1249. and so on; or nullptr
  1250. */
  1251. void drawImageNine(const sk_sp<SkImage>& image, const SkIRect& center, const SkRect& dst,
  1252. const SkPaint* paint = nullptr) {
  1253. this->drawImageNine(image.get(), center, dst, paint);
  1254. }
  1255. /** Draws SkBitmap bitmap, with its top-left corner at (left, top),
  1256. using clip, SkMatrix, and optional SkPaint paint.
  1257. If SkPaint paint is not nullptr, apply SkColorFilter, alpha, SkImageFilter,
  1258. SkBlendMode, and SkDrawLooper. If bitmap is kAlpha_8_SkColorType, apply SkShader.
  1259. If paint contains SkMaskFilter, generate mask from bitmap bounds.
  1260. If generated mask extends beyond bitmap bounds, replicate bitmap edge colors,
  1261. just as SkShader made from SkShader::MakeBitmapShader with
  1262. SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples
  1263. outside of its bounds.
  1264. @param bitmap SkBitmap containing pixels, dimensions, and format
  1265. @param left left side of bitmap
  1266. @param top top side of bitmap
  1267. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1268. and so on; or nullptr
  1269. */
  1270. void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
  1271. const SkPaint* paint = nullptr);
  1272. /** Draws SkRect src of SkBitmap bitmap, scaled and translated to fill SkRect dst.
  1273. Additionally transform draw using clip, SkMatrix, and optional SkPaint paint.
  1274. If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter,
  1275. SkBlendMode, and SkDrawLooper. If bitmap is kAlpha_8_SkColorType, apply SkShader.
  1276. If paint contains SkMaskFilter, generate mask from bitmap bounds.
  1277. If generated mask extends beyond bitmap bounds, replicate bitmap edge colors,
  1278. just as SkShader made from SkShader::MakeBitmapShader with
  1279. SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples
  1280. outside of its bounds.
  1281. constraint set to kStrict_SrcRectConstraint limits SkPaint SkFilterQuality to
  1282. sample within src; set to kFast_SrcRectConstraint allows sampling outside to
  1283. improve performance.
  1284. @param bitmap SkBitmap containing pixels, dimensions, and format
  1285. @param src source SkRect of image to draw from
  1286. @param dst destination SkRect of image to draw to
  1287. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1288. and so on; or nullptr
  1289. @param constraint filter strictly within src or draw faster
  1290. */
  1291. void drawBitmapRect(const SkBitmap& bitmap, const SkRect& src, const SkRect& dst,
  1292. const SkPaint* paint,
  1293. SrcRectConstraint constraint = kStrict_SrcRectConstraint);
  1294. /** Draws SkIRect isrc of SkBitmap bitmap, scaled and translated to fill SkRect dst.
  1295. isrc is on integer pixel boundaries; dst may include fractional boundaries.
  1296. Additionally transform draw using clip, SkMatrix, and optional SkPaint paint.
  1297. If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter,
  1298. SkBlendMode, and SkDrawLooper. If bitmap is kAlpha_8_SkColorType, apply SkShader.
  1299. If paint contains SkMaskFilter, generate mask from bitmap bounds.
  1300. If generated mask extends beyond bitmap bounds, replicate bitmap edge colors,
  1301. just as SkShader made from SkShader::MakeBitmapShader with
  1302. SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples
  1303. outside of its bounds.
  1304. constraint set to kStrict_SrcRectConstraint limits SkPaint SkFilterQuality to
  1305. sample within isrc; set to kFast_SrcRectConstraint allows sampling outside to
  1306. improve performance.
  1307. @param bitmap SkBitmap containing pixels, dimensions, and format
  1308. @param isrc source SkIRect of image to draw from
  1309. @param dst destination SkRect of image to draw to
  1310. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1311. and so on; or nullptr
  1312. @param constraint sample strictly within isrc, or draw faster
  1313. */
  1314. void drawBitmapRect(const SkBitmap& bitmap, const SkIRect& isrc, const SkRect& dst,
  1315. const SkPaint* paint,
  1316. SrcRectConstraint constraint = kStrict_SrcRectConstraint);
  1317. /** Draws SkBitmap bitmap, scaled and translated to fill SkRect dst.
  1318. bitmap bounds is on integer pixel boundaries; dst may include fractional boundaries.
  1319. Additionally transform draw using clip, SkMatrix, and optional SkPaint paint.
  1320. If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter,
  1321. SkBlendMode, and SkDrawLooper. If bitmap is kAlpha_8_SkColorType, apply SkShader.
  1322. If paint contains SkMaskFilter, generate mask from bitmap bounds.
  1323. If generated mask extends beyond bitmap bounds, replicate bitmap edge colors,
  1324. just as SkShader made from SkShader::MakeBitmapShader with
  1325. SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples
  1326. outside of its bounds.
  1327. constraint set to kStrict_SrcRectConstraint limits SkPaint SkFilterQuality to
  1328. sample within bitmap; set to kFast_SrcRectConstraint allows sampling outside to
  1329. improve performance.
  1330. @param bitmap SkBitmap containing pixels, dimensions, and format
  1331. @param dst destination SkRect of image to draw to
  1332. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1333. and so on; or nullptr
  1334. @param constraint filter strictly within bitmap or draw faster
  1335. */
  1336. void drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst, const SkPaint* paint,
  1337. SrcRectConstraint constraint = kStrict_SrcRectConstraint);
  1338. /** Draws SkBitmap bitmap stretched proportionally to fit into SkRect dst.
  1339. SkIRect center divides the bitmap into nine sections: four sides, four corners,
  1340. and the center. Corners are not scaled, or scaled down proportionately if their
  1341. sides are larger than dst; center and four sides are scaled to fit remaining
  1342. space, if any.
  1343. Additionally transform draw using clip, SkMatrix, and optional SkPaint paint.
  1344. If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter,
  1345. SkBlendMode, and SkDrawLooper. If bitmap is kAlpha_8_SkColorType, apply SkShader.
  1346. If paint contains SkMaskFilter, generate mask from bitmap bounds. If paint
  1347. SkFilterQuality set to kNone_SkFilterQuality, disable pixel filtering. For all
  1348. other values of paint SkFilterQuality, use kLow_SkFilterQuality to filter pixels.
  1349. Any SkMaskFilter on paint is ignored as is paint anti-aliasing state.
  1350. If generated mask extends beyond bitmap bounds, replicate bitmap edge colors,
  1351. just as SkShader made from SkShader::MakeBitmapShader with
  1352. SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples
  1353. outside of its bounds.
  1354. @param bitmap SkBitmap containing pixels, dimensions, and format
  1355. @param center SkIRect edge of image corners and sides
  1356. @param dst destination SkRect of image to draw to
  1357. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1358. and so on; or nullptr
  1359. */
  1360. void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst,
  1361. const SkPaint* paint = nullptr);
  1362. /** \struct SkCanvas::Lattice
  1363. SkCanvas::Lattice divides SkBitmap or SkImage into a rectangular grid.
  1364. Grid entries on even columns and even rows are fixed; these entries are
  1365. always drawn at their original size if the destination is large enough.
  1366. If the destination side is too small to hold the fixed entries, all fixed
  1367. entries are proportionately scaled down to fit.
  1368. The grid entries not on even columns and rows are scaled to fit the
  1369. remaining space, if any.
  1370. */
  1371. struct Lattice {
  1372. /** \enum SkCanvas::Lattice::RectType
  1373. Optional setting per rectangular grid entry to make it transparent,
  1374. or to fill the grid entry with a color.
  1375. */
  1376. enum RectType : uint8_t {
  1377. kDefault = 0, //!< draws SkBitmap into lattice rectangle
  1378. kTransparent, //!< skips lattice rectangle by making it transparent
  1379. kFixedColor, //!< draws one of fColors into lattice rectangle
  1380. };
  1381. const int* fXDivs; //!< x-axis values dividing bitmap
  1382. const int* fYDivs; //!< y-axis values dividing bitmap
  1383. const RectType* fRectTypes; //!< array of fill types
  1384. int fXCount; //!< number of x-coordinates
  1385. int fYCount; //!< number of y-coordinates
  1386. const SkIRect* fBounds; //!< source bounds to draw from
  1387. const SkColor* fColors; //!< array of colors
  1388. };
  1389. /** Draws SkBitmap bitmap stretched proportionally to fit into SkRect dst.
  1390. SkCanvas::Lattice lattice divides bitmap into a rectangular grid.
  1391. Each intersection of an even-numbered row and column is fixed; like the corners
  1392. of drawBitmapNine(), fixed lattice elements never scale larger than their initial
  1393. size and shrink proportionately when all fixed elements exceed the bitmap
  1394. dimension. All other grid elements scale to fill the available space, if any.
  1395. Additionally transform draw using clip, SkMatrix, and optional SkPaint paint.
  1396. If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter,
  1397. SkBlendMode, and SkDrawLooper. If bitmap is kAlpha_8_SkColorType, apply SkShader.
  1398. If paint contains SkMaskFilter, generate mask from bitmap bounds. If paint
  1399. SkFilterQuality set to kNone_SkFilterQuality, disable pixel filtering. For all
  1400. other values of paint SkFilterQuality, use kLow_SkFilterQuality to filter pixels.
  1401. Any SkMaskFilter on paint is ignored as is paint anti-aliasing state.
  1402. If generated mask extends beyond bitmap bounds, replicate bitmap edge colors,
  1403. just as SkShader made from SkShader::MakeBitmapShader with
  1404. SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples
  1405. outside of its bounds.
  1406. @param bitmap SkBitmap containing pixels, dimensions, and format
  1407. @param lattice division of bitmap into fixed and variable rectangles
  1408. @param dst destination SkRect of image to draw to
  1409. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1410. and so on; or nullptr
  1411. */
  1412. void drawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice, const SkRect& dst,
  1413. const SkPaint* paint = nullptr);
  1414. /** Draws SkImage image stretched proportionally to fit into SkRect dst.
  1415. SkCanvas::Lattice lattice divides image into a rectangular grid.
  1416. Each intersection of an even-numbered row and column is fixed; like the corners
  1417. of drawBitmapNine(), fixed lattice elements never scale larger than their initial
  1418. size and shrink proportionately when all fixed elements exceed the bitmap
  1419. dimension. All other grid elements scale to fill the available space, if any.
  1420. Additionally transform draw using clip, SkMatrix, and optional SkPaint paint.
  1421. If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter,
  1422. SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader.
  1423. If paint contains SkMaskFilter, generate mask from image bounds. If paint
  1424. SkFilterQuality set to kNone_SkFilterQuality, disable pixel filtering. For all
  1425. other values of paint SkFilterQuality, use kLow_SkFilterQuality to filter pixels.
  1426. Any SkMaskFilter on paint is ignored as is paint anti-aliasing state.
  1427. If generated mask extends beyond bitmap bounds, replicate bitmap edge colors,
  1428. just as SkShader made from SkShader::MakeBitmapShader with
  1429. SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples
  1430. outside of its bounds.
  1431. @param image SkImage containing pixels, dimensions, and format
  1432. @param lattice division of bitmap into fixed and variable rectangles
  1433. @param dst destination SkRect of image to draw to
  1434. @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter,
  1435. and so on; or nullptr
  1436. */
  1437. void drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst,
  1438. const SkPaint* paint = nullptr);
  1439. /**
  1440. * Experimental. Controls anti-aliasing of each edge of images in an image-set.
  1441. */
  1442. enum QuadAAFlags : unsigned {
  1443. kLeft_QuadAAFlag = 0b0001,
  1444. kTop_QuadAAFlag = 0b0010,
  1445. kRight_QuadAAFlag = 0b0100,
  1446. kBottom_QuadAAFlag = 0b1000,
  1447. kNone_QuadAAFlags = 0b0000,
  1448. kAll_QuadAAFlags = 0b1111,
  1449. };
  1450. /** This is used by the experimental API below. */
  1451. struct ImageSetEntry {
  1452. sk_sp<const SkImage> fImage;
  1453. SkRect fSrcRect;
  1454. SkRect fDstRect;
  1455. float fAlpha;
  1456. unsigned fAAFlags; // QuadAAFlags
  1457. };
  1458. /**
  1459. * This is an experimental API for the SkiaRenderer Chromium project. The signature will
  1460. * surely evolve if this is not removed. It currently offers no performance advantage over
  1461. * drawing images independently, though may in the future. The antialiasing flags are intended
  1462. * to allow control over each edge's AA status, to allow perfect seaming for tile sets. The
  1463. * current implementation only antialiases if all edges are flagged, however.
  1464. * Results are undefined if an image's src rect is not within the image's bounds.
  1465. */
  1466. void experimental_DrawImageSetV1(const ImageSetEntry imageSet[], int cnt,
  1467. SkFilterQuality quality, SkBlendMode mode);
  1468. /** Draws text, with origin at (x, y), using clip, SkMatrix, and SkPaint paint.
  1469. text meaning depends on SkPaint::TextEncoding; by default, text is encoded as
  1470. UTF-8.
  1471. x and y meaning depends on SkPaint::Align and SkPaint vertical text; by default
  1472. text draws left to right, positioning the first glyph left side bearing at x
  1473. and its baseline at y. Text size is affected by SkMatrix and SkPaint text size.
  1474. All elements of paint: SkPathEffect, SkMaskFilter, SkShader,
  1475. SkColorFilter, SkImageFilter, and SkDrawLooper; apply to text. By default, draws
  1476. filled 12 point black glyphs.
  1477. @param text character code points or glyphs drawn
  1478. @param byteLength byte length of text array
  1479. @param x start of text on x-axis
  1480. @param y start of text on y-axis
  1481. @param paint text size, blend, color, and so on, used to draw
  1482. */
  1483. void drawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
  1484. const SkPaint& paint);
  1485. // Experimental
  1486. void drawSimpleText(const void* text, size_t byteLength, SkTextEncoding encoding,
  1487. SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint);
  1488. /** Draws null terminated string, with origin at (x, y), using clip, SkMatrix, and
  1489. SkPaint paint.
  1490. string meaning depends on SkPaint::TextEncoding; by default, strings are encoded
  1491. as UTF-8. Other values of SkPaint::TextEncoding are unlikely to produce the desired
  1492. results, since zero bytes may be embedded in the string.
  1493. x and y meaning depends on SkPaint::Align and SkPaint vertical text; by default
  1494. string draws left to right, positioning the first glyph left side bearing at x
  1495. and its baseline at y. Text size is affected by SkMatrix and SkPaint text size.
  1496. All elements of paint: SkPathEffect, SkMaskFilter, SkShader,
  1497. SkColorFilter, SkImageFilter, and SkDrawLooper; apply to text. By default, draws
  1498. filled 12 point black glyphs.
  1499. @param string character code points or glyphs drawn,
  1500. ending with a char value of zero
  1501. @param x start of string on x-axis
  1502. @param y start of string on y-axis
  1503. @param paint text size, blend, color, and so on, used to draw
  1504. */
  1505. void drawString(const char* string, SkScalar x, SkScalar y, const SkPaint& paint) {
  1506. if (!string) {
  1507. return;
  1508. }
  1509. this->drawText(string, strlen(string), x, y, paint);
  1510. }
  1511. /** Draws null terminated string, with origin at (x, y), using clip, SkMatrix, and
  1512. SkPaint paint.
  1513. string meaning depends on SkPaint::TextEncoding; by default, strings are encoded
  1514. as UTF-8. Other values of SkPaint::TextEncoding are unlikely to produce the desired
  1515. results, since zero bytes may be embedded in the string.
  1516. x and y meaning depends on SkPaint::Align and SkPaint vertical text; by default
  1517. string draws left to right, positioning the first glyph left side bearing at x
  1518. and its baseline at y. Text size is affected by SkMatrix and SkPaint text size.
  1519. All elements of paint: SkPathEffect, SkMaskFilter, SkShader,
  1520. SkColorFilter, SkImageFilter, and SkDrawLooper; apply to text. By default, draws
  1521. filled 12 point black glyphs.
  1522. @param string character code points or glyphs drawn,
  1523. ending with a char value of zero
  1524. @param x start of string on x-axis
  1525. @param y start of string on y-axis
  1526. @param paint text size, blend, color, and so on, used to draw
  1527. */
  1528. void drawString(const SkString& string, SkScalar x, SkScalar y, const SkPaint& paint);
  1529. /** Draws each glyph in text with the origin in pos array, using clip, SkMatrix, and
  1530. SkPaint paint. The number of entries in pos array must match the number of glyphs
  1531. described by byteLength of text.
  1532. text meaning depends on SkPaint::TextEncoding; by default, text is encoded as
  1533. UTF-8. pos elements meaning depends on SkPaint vertical text; by default
  1534. glyph left side bearing and baseline are relative to SkPoint in pos array.
  1535. Text size is affected by SkMatrix and SkPaint text size.
  1536. All elements of paint: SkPathEffect, SkMaskFilter, SkShader,
  1537. SkColorFilter, SkImageFilter, and SkDrawLooper; apply to text. By default, draws
  1538. filled 12 point black glyphs.
  1539. Layout engines such as Harfbuzz typically position each glyph
  1540. rather than using the font advance widths.
  1541. @param text character code points or glyphs drawn
  1542. @param byteLength byte length of text array
  1543. @param pos array of glyph origins
  1544. @param paint text size, blend, color, and so on, used to draw
  1545. */
  1546. void drawPosText(const void* text, size_t byteLength, const SkPoint pos[],
  1547. const SkPaint& paint);
  1548. /** Draws each glyph in text with its origin composed from xpos array and
  1549. constY, using clip, SkMatrix, and SkPaint paint. The number of entries in xpos array
  1550. must match the number of glyphs described by byteLength of text.
  1551. text meaning depends on SkPaint::TextEncoding; by default, text is encoded as
  1552. UTF-8. xpos elements meaning depends on SkPaint vertical text;
  1553. by default each glyph left side bearing is positioned at an xpos element and
  1554. its baseline is positioned at constY. Text size is affected by SkMatrix and
  1555. SkPaint text size.
  1556. All elements of paint: SkPathEffect, SkMaskFilter, SkShader,
  1557. SkColorFilter, SkImageFilter, and SkDrawLooper; apply to text. By default, draws
  1558. filled 12 point black glyphs.
  1559. Layout engines such as Harfbuzz typically position each glyph
  1560. rather than using the font advance widths if all glyphs share the same
  1561. baseline.
  1562. @param text character code points or glyphs drawn
  1563. @param byteLength byte length of text array
  1564. @param xpos array of x-axis positions, used to position each glyph
  1565. @param constY shared y-axis value for all of x-axis positions
  1566. @param paint text size, blend, color, and so on, used to draw
  1567. */
  1568. void drawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], SkScalar constY,
  1569. const SkPaint& paint);
  1570. /** Draws text, transforming each glyph by the corresponding SkRSXform,
  1571. using clip, SkMatrix, and SkPaint paint.
  1572. SkRSXform xform array specifies a separate square scale, rotation, and translation
  1573. for each glyph. xform does not affect paint SkShader.
  1574. Optional SkRect cullRect is a conservative bounds of text, taking into account
  1575. SkRSXform and paint. If cullRect is outside of clip, canvas can skip drawing.
  1576. All elements of paint: SkPathEffect, SkMaskFilter, SkShader,
  1577. SkColorFilter, SkImageFilter, and SkDrawLooper; apply to text. By default, draws
  1578. filled 12 point black glyphs.
  1579. @param text character code points or glyphs drawn
  1580. @param byteLength byte length of text array
  1581. @param xform SkRSXform rotates, scales, and translates each glyph individually
  1582. @param cullRect SkRect bounds of text for efficient clipping; or nullptr
  1583. @param paint text size, blend, color, and so on, used to draw
  1584. */
  1585. void drawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
  1586. const SkRect* cullRect, const SkPaint& paint);
  1587. /** Draws SkTextBlob blob at (x, y), using clip, SkMatrix, and SkPaint paint.
  1588. blob contains glyphs, their positions, and paint attributes specific to text:
  1589. SkTypeface, SkPaint text size, SkPaint text scale x,
  1590. SkPaint text skew x, SkPaint::Align, SkPaint::Hinting, anti-alias, SkPaint fake bold,
  1591. SkPaint font embedded bitmaps, SkPaint full hinting spacing, LCD text, SkPaint linear text,
  1592. and SkPaint subpixel text.
  1593. SkPaint::TextEncoding must be set to SkPaint::kGlyphID_TextEncoding.
  1594. Elements of paint: anti-alias, SkBlendMode, color including alpha,
  1595. SkColorFilter, SkPaint dither, SkDrawLooper, SkMaskFilter, SkPathEffect, SkShader, and
  1596. SkPaint::Style; apply to blob. If SkPaint contains SkPaint::kStroke_Style:
  1597. SkPaint miter limit, SkPaint::Cap, SkPaint::Join, and SkPaint stroke width;
  1598. apply to SkPath created from blob.
  1599. @param blob glyphs, positions, and their paints' text size, typeface, and so on
  1600. @param x horizontal offset applied to blob
  1601. @param y vertical offset applied to blob
  1602. @param paint blend, color, stroking, and so on, used to draw
  1603. */
  1604. void drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint);
  1605. /** Draws SkTextBlob blob at (x, y), using clip, SkMatrix, and SkPaint paint.
  1606. blob contains glyphs, their positions, and paint attributes specific to text:
  1607. SkTypeface, SkPaint text size, SkPaint text scale x,
  1608. SkPaint text skew x, SkPaint::Align, SkPaint::Hinting, anti-alias, SkPaint fake bold,
  1609. SkPaint font embedded bitmaps, SkPaint full hinting spacing, LCD text, SkPaint linear text,
  1610. and SkPaint subpixel text.
  1611. SkPaint::TextEncoding must be set to SkPaint::kGlyphID_TextEncoding.
  1612. Elements of paint: SkPathEffect, SkMaskFilter, SkShader, SkColorFilter,
  1613. SkImageFilter, and SkDrawLooper; apply to blob.
  1614. @param blob glyphs, positions, and their paints' text size, typeface, and so on
  1615. @param x horizontal offset applied to blob
  1616. @param y vertical offset applied to blob
  1617. @param paint blend, color, stroking, and so on, used to draw
  1618. */
  1619. void drawTextBlob(const sk_sp<SkTextBlob>& blob, SkScalar x, SkScalar y, const SkPaint& paint) {
  1620. this->drawTextBlob(blob.get(), x, y, paint);
  1621. }
  1622. /** Draws SkPicture picture, using clip and SkMatrix.
  1623. Clip and SkMatrix are unchanged by picture contents, as if
  1624. save() was called before and restore() was called after drawPicture().
  1625. SkPicture records a series of draw commands for later playback.
  1626. @param picture recorded drawing commands to play
  1627. */
  1628. void drawPicture(const SkPicture* picture) {
  1629. this->drawPicture(picture, nullptr, nullptr);
  1630. }
  1631. /** Draws SkPicture picture, using clip and SkMatrix.
  1632. Clip and SkMatrix are unchanged by picture contents, as if
  1633. save() was called before and restore() was called after drawPicture().
  1634. SkPicture records a series of draw commands for later playback.
  1635. @param picture recorded drawing commands to play
  1636. */
  1637. void drawPicture(const sk_sp<SkPicture>& picture) {
  1638. this->drawPicture(picture.get());
  1639. }
  1640. /** Draws SkPicture picture, using clip and SkMatrix; transforming picture with
  1641. SkMatrix matrix, if provided; and use SkPaint paint alpha, SkColorFilter,
  1642. SkImageFilter, and SkBlendMode, if provided.
  1643. matrix transformation is equivalent to: save(), concat(), drawPicture(), restore().
  1644. paint use is equivalent to: saveLayer(), drawPicture(), restore().
  1645. @param picture recorded drawing commands to play
  1646. @param matrix SkMatrix to rotate, scale, translate, and so on; may be nullptr
  1647. @param paint SkPaint to apply transparency, filtering, and so on; may be nullptr
  1648. */
  1649. void drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint);
  1650. /** Draws SkPicture picture, using clip and SkMatrix; transforming picture with
  1651. SkMatrix matrix, if provided; and use SkPaint paint alpha, SkColorFilter,
  1652. SkImageFilter, and SkBlendMode, if provided.
  1653. matrix transformation is equivalent to: save(), concat(), drawPicture(), restore().
  1654. paint use is equivalent to: saveLayer(), drawPicture(), restore().
  1655. @param picture recorded drawing commands to play
  1656. @param matrix SkMatrix to rotate, scale, translate, and so on; may be nullptr
  1657. @param paint SkPaint to apply transparency, filtering, and so on; may be nullptr
  1658. */
  1659. void drawPicture(const sk_sp<SkPicture>& picture, const SkMatrix* matrix,
  1660. const SkPaint* paint) {
  1661. this->drawPicture(picture.get(), matrix, paint);
  1662. }
  1663. /** Draws SkVertices vertices, a triangle mesh, using clip and SkMatrix.
  1664. If vertices texs and vertices colors are defined in vertices, and SkPaint paint
  1665. contains SkShader, SkBlendMode mode combines vertices colors with SkShader.
  1666. @param vertices triangle mesh to draw
  1667. @param mode combines vertices colors with SkShader, if both are present
  1668. @param paint specifies the SkShader, used as SkVertices texture; may be nullptr
  1669. */
  1670. void drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint);
  1671. /** Draws SkVertices vertices, a triangle mesh, using clip and SkMatrix.
  1672. If vertices texs and vertices colors are defined in vertices, and SkPaint paint
  1673. contains SkShader, SkBlendMode mode combines vertices colors with SkShader.
  1674. @param vertices triangle mesh to draw
  1675. @param mode combines vertices colors with SkShader, if both are present
  1676. @param paint specifies the SkShader, used as SkVertices texture, may be nullptr
  1677. */
  1678. void drawVertices(const sk_sp<SkVertices>& vertices, SkBlendMode mode, const SkPaint& paint);
  1679. /** Draws SkVertices vertices, a triangle mesh, using clip and SkMatrix. Bone data is used to
  1680. deform vertices with bone weights.
  1681. If vertices texs and vertices colors are defined in vertices, and SkPaint paint
  1682. contains SkShader, SkBlendMode mode combines vertices colors with SkShader.
  1683. The first element of bones should be an object to world space transformation matrix that
  1684. will be applied before performing mesh deformations. If no such transformation is needed,
  1685. it should be the identity matrix.
  1686. boneCount must be at most 80, and thus the size of bones should be at most 80.
  1687. @param vertices triangle mesh to draw
  1688. @param bones bone matrix data
  1689. @param boneCount number of bone matrices
  1690. @param mode combines vertices colors with SkShader, if both are present
  1691. @param paint specifies the SkShader, used as SkVertices texture, may be nullptr
  1692. */
  1693. void drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[], int boneCount,
  1694. SkBlendMode mode, const SkPaint& paint);
  1695. /** Draws SkVertices vertices, a triangle mesh, using clip and SkMatrix. Bone data is used to
  1696. deform vertices with bone weights.
  1697. If vertices texs and vertices colors are defined in vertices, and SkPaint paint
  1698. contains SkShader, SkBlendMode mode combines vertices colors with SkShader.
  1699. The first element of bones should be an object to world space transformation matrix that
  1700. will be applied before performing mesh deformations. If no such transformation is needed,
  1701. it should be the identity matrix.
  1702. boneCount must be at most 80, and thus the size of bones should be at most 80.
  1703. @param vertices triangle mesh to draw
  1704. @param bones bone matrix data
  1705. @param boneCount number of bone matrices
  1706. @param mode combines vertices colors with SkShader, if both are present
  1707. @param paint specifies the SkShader, used as SkVertices texture, may be nullptr
  1708. */
  1709. void drawVertices(const sk_sp<SkVertices>& vertices, const SkVertices::Bone bones[],
  1710. int boneCount, SkBlendMode mode, const SkPaint& paint);
  1711. /** Draws a Coons patch: the interpolation of four cubics with shared corners,
  1712. associating a color, and optionally a texture SkPoint, with each corner.
  1713. Coons patch uses clip and SkMatrix, paint SkShader, SkColorFilter,
  1714. alpha, SkImageFilter, and SkBlendMode. If SkShader is provided it is treated
  1715. as Coons patch texture; SkBlendMode mode combines color colors and SkShader if
  1716. both are provided.
  1717. SkPoint array cubics specifies four SkPath cubic starting at the top-left corner,
  1718. in clockwise order, sharing every fourth point. The last SkPath cubic ends at the
  1719. first point.
  1720. Color array color associates colors with corners in top-left, top-right,
  1721. bottom-right, bottom-left order.
  1722. If paint contains SkShader, SkPoint array texCoords maps SkShader as texture to
  1723. corners in top-left, top-right, bottom-right, bottom-left order.
  1724. @param cubics SkPath cubic array, sharing common points
  1725. @param colors color array, one for each corner
  1726. @param texCoords SkPoint array of texture coordinates, mapping SkShader to corners;
  1727. may be nullptr
  1728. @param mode SkBlendMode for colors, and for SkShader if paint has one
  1729. @param paint SkShader, SkColorFilter, SkBlendMode, used to draw
  1730. */
  1731. void drawPatch(const SkPoint cubics[12], const SkColor colors[4],
  1732. const SkPoint texCoords[4], SkBlendMode mode, const SkPaint& paint);
  1733. /** Draws SkPath cubic Coons patch: the interpolation of four cubics with shared corners,
  1734. associating a color, and optionally a texture SkPoint, with each corner.
  1735. Coons patch uses clip and SkMatrix, paint SkShader, SkColorFilter,
  1736. alpha, SkImageFilter, and SkBlendMode. If SkShader is provided it is treated
  1737. as Coons patch texture; SkBlendMode mode combines color colors and SkShader if
  1738. both are provided.
  1739. SkPoint array cubics specifies four SkPath cubic starting at the top-left corner,
  1740. in clockwise order, sharing every fourth point. The last SkPath cubic ends at the
  1741. first point.
  1742. Color array color associates colors with corners in top-left, top-right,
  1743. bottom-right, bottom-left order.
  1744. If paint contains SkShader, SkPoint array texCoords maps SkShader as texture to
  1745. corners in top-left, top-right, bottom-right, bottom-left order.
  1746. @param cubics SkPath cubic array, sharing common points
  1747. @param colors color array, one for each corner
  1748. @param texCoords SkPoint array of texture coordinates, mapping SkShader to corners;
  1749. may be nullptr
  1750. @param paint SkShader, SkColorFilter, SkBlendMode, used to draw
  1751. */
  1752. void drawPatch(const SkPoint cubics[12], const SkColor colors[4],
  1753. const SkPoint texCoords[4], const SkPaint& paint) {
  1754. this->drawPatch(cubics, colors, texCoords, SkBlendMode::kModulate, paint);
  1755. }
  1756. /** Draws a set of sprites from atlas, using clip, SkMatrix, and optional SkPaint paint.
  1757. paint uses anti-alias, alpha, SkColorFilter, SkImageFilter, and SkBlendMode
  1758. to draw, if present. For each entry in the array, SkRect tex locates sprite in
  1759. atlas, and SkRSXform xform transforms it into destination space.
  1760. xform, text, and colors if present, must contain count entries.
  1761. Optional colors are applied for each sprite using SkBlendMode mode, treating
  1762. sprite as source and colors as destination.
  1763. Optional cullRect is a conservative bounds of all transformed sprites.
  1764. If cullRect is outside of clip, canvas can skip drawing.
  1765. @param atlas SkImage containing sprites
  1766. @param xform SkRSXform mappings for sprites in atlas
  1767. @param tex SkRect locations of sprites in atlas
  1768. @param colors one per sprite, blended with sprite using SkBlendMode; may be nullptr
  1769. @param count number of sprites to draw
  1770. @param mode SkBlendMode combining colors and sprites
  1771. @param cullRect bounds of transformed sprites for efficient clipping; may be nullptr
  1772. @param paint SkColorFilter, SkImageFilter, SkBlendMode, and so on; may be nullptr
  1773. */
  1774. void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
  1775. const SkColor colors[], int count, SkBlendMode mode, const SkRect* cullRect,
  1776. const SkPaint* paint);
  1777. /** Draws a set of sprites from atlas, using clip, SkMatrix, and optional SkPaint paint.
  1778. paint uses anti-alias, alpha, SkColorFilter, SkImageFilter, and SkBlendMode
  1779. to draw, if present. For each entry in the array, SkRect tex locates sprite in
  1780. atlas, and SkRSXform xform transforms it into destination space.
  1781. xform, text, and colors if present, must contain count entries.
  1782. Optional colors is applied for each sprite using SkBlendMode.
  1783. Optional cullRect is a conservative bounds of all transformed sprites.
  1784. If cullRect is outside of clip, canvas can skip drawing.
  1785. @param atlas SkImage containing sprites
  1786. @param xform SkRSXform mappings for sprites in atlas
  1787. @param tex SkRect locations of sprites in atlas
  1788. @param colors one per sprite, blended with sprite using SkBlendMode; may be nullptr
  1789. @param count number of sprites to draw
  1790. @param mode SkBlendMode combining colors and sprites
  1791. @param cullRect bounds of transformed sprites for efficient clipping; may be nullptr
  1792. @param paint SkColorFilter, SkImageFilter, SkBlendMode, and so on; may be nullptr
  1793. */
  1794. void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[],
  1795. const SkColor colors[], int count, SkBlendMode mode, const SkRect* cullRect,
  1796. const SkPaint* paint) {
  1797. this->drawAtlas(atlas.get(), xform, tex, colors, count, mode, cullRect, paint);
  1798. }
  1799. /** Draws a set of sprites from atlas, using clip, SkMatrix, and optional SkPaint paint.
  1800. paint uses anti-alias, alpha, SkColorFilter, SkImageFilter, and SkBlendMode
  1801. to draw, if present. For each entry in the array, SkRect tex locates sprite in
  1802. atlas, and SkRSXform xform transforms it into destination space.
  1803. xform and text must contain count entries.
  1804. Optional cullRect is a conservative bounds of all transformed sprites.
  1805. If cullRect is outside of clip, canvas can skip drawing.
  1806. @param atlas SkImage containing sprites
  1807. @param xform SkRSXform mappings for sprites in atlas
  1808. @param tex SkRect locations of sprites in atlas
  1809. @param count number of sprites to draw
  1810. @param cullRect bounds of transformed sprites for efficient clipping; may be nullptr
  1811. @param paint SkColorFilter, SkImageFilter, SkBlendMode, and so on; may be nullptr
  1812. */
  1813. void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], int count,
  1814. const SkRect* cullRect, const SkPaint* paint) {
  1815. this->drawAtlas(atlas, xform, tex, nullptr, count, SkBlendMode::kDst, cullRect, paint);
  1816. }
  1817. /** Draws a set of sprites from atlas, using clip, SkMatrix, and optional SkPaint paint.
  1818. paint uses anti-alias, alpha, SkColorFilter, SkImageFilter, and SkBlendMode
  1819. to draw, if present. For each entry in the array, SkRect tex locates sprite in
  1820. atlas, and SkRSXform xform transforms it into destination space.
  1821. xform and text must contain count entries.
  1822. Optional cullRect is a conservative bounds of all transformed sprites.
  1823. If cullRect is outside of clip, canvas can skip drawing.
  1824. @param atlas SkImage containing sprites
  1825. @param xform SkRSXform mappings for sprites in atlas
  1826. @param tex SkRect locations of sprites in atlas
  1827. @param count number of sprites to draw
  1828. @param cullRect bounds of transformed sprites for efficient clipping; may be nullptr
  1829. @param paint SkColorFilter, SkImageFilter, SkBlendMode, and so on; may be nullptr
  1830. */
  1831. void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[],
  1832. int count, const SkRect* cullRect, const SkPaint* paint) {
  1833. this->drawAtlas(atlas.get(), xform, tex, nullptr, count, SkBlendMode::kDst,
  1834. cullRect, paint);
  1835. }
  1836. /** Draws SkDrawable drawable using clip and SkMatrix, concatenated with
  1837. optional matrix.
  1838. If SkCanvas has an asynchronous implementation, as is the case
  1839. when it is recording into SkPicture, then drawable will be referenced,
  1840. so that SkDrawable::draw() can be called when the operation is finalized. To force
  1841. immediate drawing, call SkDrawable::draw() instead.
  1842. @param drawable custom struct encapsulating drawing commands
  1843. @param matrix transformation applied to drawing; may be nullptr
  1844. */
  1845. void drawDrawable(SkDrawable* drawable, const SkMatrix* matrix = nullptr);
  1846. /** Draws SkDrawable drawable using clip and SkMatrix, offset by (x, y).
  1847. If SkCanvas has an asynchronous implementation, as is the case
  1848. when it is recording into SkPicture, then drawable will be referenced,
  1849. so that SkDrawable::draw() can be called when the operation is finalized. To force
  1850. immediate drawing, call SkDrawable::draw() instead.
  1851. @param drawable custom struct encapsulating drawing commands
  1852. @param x offset into SkCanvas writable pixels on x-axis
  1853. @param y offset into SkCanvas writable pixels on y-axis
  1854. */
  1855. void drawDrawable(SkDrawable* drawable, SkScalar x, SkScalar y);
  1856. /** Associates SkRect on SkCanvas with an annotation; a key-value pair, where the key is
  1857. a null-terminated UTF-8 string, and optional value is stored as SkData.
  1858. Only some canvas implementations, such as recording to SkPicture, or drawing to
  1859. document PDF, use annotations.
  1860. @param rect SkRect extent of canvas to annotate
  1861. @param key string used for lookup
  1862. @param value data holding value stored in annotation
  1863. */
  1864. void drawAnnotation(const SkRect& rect, const char key[], SkData* value);
  1865. /** Associates SkRect on SkCanvas when an annotation; a key-value pair, where the key is
  1866. a null-terminated UTF-8 string, and optional value is stored as SkData.
  1867. Only some canvas implementations, such as recording to SkPicture, or drawing to
  1868. document PDF, use annotations.
  1869. @param rect SkRect extent of canvas to annotate
  1870. @param key string used for lookup
  1871. @param value data holding value stored in annotation
  1872. */
  1873. void drawAnnotation(const SkRect& rect, const char key[], const sk_sp<SkData>& value) {
  1874. this->drawAnnotation(rect, key, value.get());
  1875. }
  1876. /** Returns true if clip is empty; that is, nothing will draw.
  1877. May do work when called; it should not be called
  1878. more often than needed. However, once called, subsequent calls perform no
  1879. work until clip changes.
  1880. @return true if clip is empty
  1881. */
  1882. virtual bool isClipEmpty() const;
  1883. /** Returns true if clip is SkRect and not empty.
  1884. Returns false if the clip is empty, or if it is not SkRect.
  1885. @return true if clip is SkRect and not empty
  1886. */
  1887. virtual bool isClipRect() const;
  1888. /** Returns SkMatrix.
  1889. This does not account for translation by SkBaseDevice or SkSurface.
  1890. @return SkMatrix in SkCanvas
  1891. */
  1892. const SkMatrix& getTotalMatrix() const;
  1893. ///////////////////////////////////////////////////////////////////////////
  1894. // don't call
  1895. virtual GrRenderTargetContext* internal_private_accessTopLayerRenderTargetContext();
  1896. SkIRect internal_private_getTopLayerBounds() const { return getTopLayerBounds(); }
  1897. // TEMP helpers until we switch virtual over to const& for src-rect
  1898. void legacy_drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
  1899. const SkPaint* paint,
  1900. SrcRectConstraint constraint = kStrict_SrcRectConstraint);
  1901. void legacy_drawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
  1902. const SkPaint* paint,
  1903. SrcRectConstraint constraint = kStrict_SrcRectConstraint);
  1904. /**
  1905. * Returns the global clip as a region. If the clip contains AA, then only the bounds
  1906. * of the clip may be returned.
  1907. */
  1908. void temporary_internal_getRgnClip(SkRegion* region);
  1909. void private_draw_shadow_rec(const SkPath&, const SkDrawShadowRec&);
  1910. protected:
  1911. // default impl defers to getDevice()->newSurface(info)
  1912. virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props);
  1913. // default impl defers to its device
  1914. virtual bool onPeekPixels(SkPixmap* pixmap);
  1915. virtual bool onAccessTopLayerPixels(SkPixmap* pixmap);
  1916. virtual SkImageInfo onImageInfo() const;
  1917. virtual bool onGetProps(SkSurfaceProps* props) const;
  1918. virtual void onFlush();
  1919. // Subclass save/restore notifiers.
  1920. // Overriders should call the corresponding INHERITED method up the inheritance chain.
  1921. // getSaveLayerStrategy()'s return value may suppress full layer allocation.
  1922. enum SaveLayerStrategy {
  1923. kFullLayer_SaveLayerStrategy,
  1924. kNoLayer_SaveLayerStrategy,
  1925. };
  1926. virtual void willSave() {}
  1927. // Overriders should call the corresponding INHERITED method up the inheritance chain.
  1928. virtual SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& ) {
  1929. return kFullLayer_SaveLayerStrategy;
  1930. }
  1931. virtual void willRestore() {}
  1932. virtual void didRestore() {}
  1933. virtual void didConcat(const SkMatrix& ) {}
  1934. virtual void didSetMatrix(const SkMatrix& ) {}
  1935. virtual void didTranslate(SkScalar dx, SkScalar dy) {
  1936. this->didConcat(SkMatrix::MakeTrans(dx, dy));
  1937. }
  1938. // NOTE: If you are adding a new onDraw virtual to SkCanvas, PLEASE add an override to
  1939. // SkCanvasVirtualEnforcer (in SkCanvasVirtualEnforcer.h). This ensures that subclasses using
  1940. // that mechanism will be required to implement the new function.
  1941. virtual void onDrawPaint(const SkPaint& paint);
  1942. virtual void onDrawRect(const SkRect& rect, const SkPaint& paint);
  1943. virtual void onDrawRRect(const SkRRect& rrect, const SkPaint& paint);
  1944. virtual void onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint);
  1945. virtual void onDrawOval(const SkRect& rect, const SkPaint& paint);
  1946. virtual void onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
  1947. bool useCenter, const SkPaint& paint);
  1948. virtual void onDrawPath(const SkPath& path, const SkPaint& paint);
  1949. virtual void onDrawRegion(const SkRegion& region, const SkPaint& paint);
  1950. virtual void onDrawText(const void* text, size_t byteLength, SkScalar x,
  1951. SkScalar y, const SkPaint& paint);
  1952. virtual void onDrawPosText(const void* text, size_t byteLength,
  1953. const SkPoint pos[], const SkPaint& paint);
  1954. virtual void onDrawPosTextH(const void* text, size_t byteLength,
  1955. const SkScalar xpos[], SkScalar constY,
  1956. const SkPaint& paint);
  1957. virtual void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
  1958. const SkRect* cullRect, const SkPaint& paint);
  1959. virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
  1960. const SkPaint& paint);
  1961. virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
  1962. const SkPoint texCoords[4], SkBlendMode mode, const SkPaint& paint);
  1963. virtual void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
  1964. const SkPaint& paint);
  1965. // TODO: Remove old signature
  1966. virtual void onDrawVerticesObject(const SkVertices* vertices, SkBlendMode mode,
  1967. const SkPaint& paint) {
  1968. this->onDrawVerticesObject(vertices, nullptr, 0, mode, paint);
  1969. }
  1970. virtual void onDrawVerticesObject(const SkVertices* vertices, const SkVertices::Bone bones[],
  1971. int boneCount, SkBlendMode mode, const SkPaint& paint);
  1972. virtual void onDrawImage(const SkImage* image, SkScalar dx, SkScalar dy, const SkPaint* paint);
  1973. virtual void onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
  1974. const SkPaint* paint, SrcRectConstraint constraint);
  1975. virtual void onDrawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst,
  1976. const SkPaint* paint);
  1977. virtual void onDrawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst,
  1978. const SkPaint* paint);
  1979. virtual void onDrawImageSet(const ImageSetEntry imageSet[], int count, SkFilterQuality,
  1980. SkBlendMode);
  1981. virtual void onDrawBitmap(const SkBitmap& bitmap, SkScalar dx, SkScalar dy,
  1982. const SkPaint* paint);
  1983. virtual void onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
  1984. const SkPaint* paint, SrcRectConstraint constraint);
  1985. virtual void onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst,
  1986. const SkPaint* paint);
  1987. virtual void onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice,
  1988. const SkRect& dst, const SkPaint* paint);
  1989. virtual void onDrawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect rect[],
  1990. const SkColor colors[], int count, SkBlendMode mode,
  1991. const SkRect* cull, const SkPaint* paint);
  1992. virtual void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value);
  1993. virtual void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&);
  1994. virtual void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix);
  1995. virtual void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
  1996. const SkPaint* paint);
  1997. enum ClipEdgeStyle {
  1998. kHard_ClipEdgeStyle,
  1999. kSoft_ClipEdgeStyle
  2000. };
  2001. virtual void onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle);
  2002. virtual void onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle);
  2003. virtual void onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle);
  2004. virtual void onClipRegion(const SkRegion& deviceRgn, SkClipOp op);
  2005. virtual void onDiscard();
  2006. // Clip rectangle bounds. Called internally by saveLayer.
  2007. // returns false if the entire rectangle is entirely clipped out
  2008. // If non-NULL, The imageFilter parameter will be used to expand the clip
  2009. // and offscreen bounds for any margin required by the filter DAG.
  2010. bool clipRectBounds(const SkRect* bounds, SaveLayerFlags flags, SkIRect* intersection,
  2011. const SkImageFilter* imageFilter = nullptr);
  2012. SkBaseDevice* getTopDevice() const;
  2013. private:
  2014. /** After calling saveLayer(), there can be any number of devices that make
  2015. up the top-most drawing area. LayerIter can be used to iterate through
  2016. those devices. Note that the iterator is only valid until the next API
  2017. call made on the canvas. Ownership of all pointers in the iterator stays
  2018. with the canvas, so none of them should be modified or deleted.
  2019. */
  2020. class LayerIter /*: SkNoncopyable*/ {
  2021. public:
  2022. /** Initialize iterator with canvas, and set values for 1st device */
  2023. LayerIter(SkCanvas*);
  2024. ~LayerIter();
  2025. /** Return true if the iterator is done */
  2026. bool done() const { return fDone; }
  2027. /** Cycle to the next device */
  2028. void next();
  2029. // These reflect the current device in the iterator
  2030. SkBaseDevice* device() const;
  2031. const SkMatrix& matrix() const;
  2032. SkIRect clipBounds() const;
  2033. const SkPaint& paint() const;
  2034. int x() const;
  2035. int y() const;
  2036. private:
  2037. // used to embed the SkDrawIter object directly in our instance, w/o
  2038. // having to expose that class def to the public. There is an assert
  2039. // in our constructor to ensure that fStorage is large enough
  2040. // (though needs to be a compile-time-assert!). We use intptr_t to work
  2041. // safely with 32 and 64 bit machines (to ensure the storage is enough)
  2042. intptr_t fStorage[32];
  2043. class SkDrawIter* fImpl; // this points at fStorage
  2044. SkPaint fDefaultPaint;
  2045. bool fDone;
  2046. };
  2047. static bool BoundsAffectsClip(SaveLayerFlags);
  2048. static void DrawDeviceWithFilter(SkBaseDevice* src, const SkImageFilter* filter,
  2049. SkBaseDevice* dst, const SkIPoint& dstOrigin,
  2050. const SkMatrix& ctm);
  2051. enum ShaderOverrideOpacity {
  2052. kNone_ShaderOverrideOpacity, //!< there is no overriding shader (bitmap or image)
  2053. kOpaque_ShaderOverrideOpacity, //!< the overriding shader is opaque
  2054. kNotOpaque_ShaderOverrideOpacity, //!< the overriding shader may not be opaque
  2055. };
  2056. // notify our surface (if we have one) that we are about to draw, so it
  2057. // can perform copy-on-write or invalidate any cached images
  2058. void predrawNotify(bool willOverwritesEntireSurface = false);
  2059. void predrawNotify(const SkRect* rect, const SkPaint* paint, ShaderOverrideOpacity);
  2060. void predrawNotify(const SkRect* rect, const SkPaint* paint, bool shaderOverrideIsOpaque) {
  2061. this->predrawNotify(rect, paint, shaderOverrideIsOpaque ? kOpaque_ShaderOverrideOpacity
  2062. : kNotOpaque_ShaderOverrideOpacity);
  2063. }
  2064. SkBaseDevice* getDevice() const;
  2065. class MCRec;
  2066. SkDeque fMCStack;
  2067. // points to top of stack
  2068. MCRec* fMCRec;
  2069. // the first N recs that can fit here mean we won't call malloc
  2070. static constexpr int kMCRecSize = 128; // most recent measurement
  2071. static constexpr int kMCRecCount = 32; // common depth for save/restores
  2072. static constexpr int kDeviceCMSize = 224; // most recent measurement
  2073. intptr_t fMCRecStorage[kMCRecSize * kMCRecCount / sizeof(intptr_t)];
  2074. intptr_t fDeviceCMStorage[kDeviceCMSize / sizeof(intptr_t)];
  2075. const SkSurfaceProps fProps;
  2076. int fSaveCount; // value returned by getSaveCount()
  2077. SkMetaData* fMetaData;
  2078. std::unique_ptr<SkRasterHandleAllocator> fAllocator;
  2079. SkSurface_Base* fSurfaceBase;
  2080. SkSurface_Base* getSurfaceBase() const { return fSurfaceBase; }
  2081. void setSurfaceBase(SkSurface_Base* sb) {
  2082. fSurfaceBase = sb;
  2083. }
  2084. friend class SkSurface_Base;
  2085. friend class SkSurface_Gpu;
  2086. SkIRect fClipRestrictionRect = SkIRect::MakeEmpty();
  2087. void doSave();
  2088. void checkForDeferredSave();
  2089. void internalSetMatrix(const SkMatrix&);
  2090. friend class SkAndroidFrameworkUtils;
  2091. friend class SkCanvasPriv; // needs kDontClipToLayer_PrivateSaveLayerFlag
  2092. friend class SkDrawIter; // needs setupDrawForLayerDevice()
  2093. friend class AutoDrawLooper;
  2094. friend class SkDebugCanvas; // needs experimental fAllowSimplifyClip
  2095. friend class SkSurface_Raster; // needs getDevice()
  2096. friend class SkNoDrawCanvas; // needs resetForNextPicture()
  2097. friend class SkPictureRecord; // predrawNotify (why does it need it? <reed>)
  2098. friend class SkOverdrawCanvas;
  2099. friend class SkRasterHandleAllocator;
  2100. protected:
  2101. // For use by SkNoDrawCanvas (via SkCanvasVirtualEnforcer, which can't be a friend)
  2102. SkCanvas(const SkIRect& bounds);
  2103. private:
  2104. SkCanvas(const SkBitmap&, std::unique_ptr<SkRasterHandleAllocator>,
  2105. SkRasterHandleAllocator::Handle);
  2106. SkCanvas(SkCanvas&&) = delete;
  2107. SkCanvas(const SkCanvas&) = delete;
  2108. SkCanvas& operator=(SkCanvas&&) = delete;
  2109. SkCanvas& operator=(const SkCanvas&) = delete;
  2110. void resetForNextPicture(const SkIRect& bounds);
  2111. // needs gettotalclip()
  2112. friend class SkCanvasStateUtils;
  2113. // call this each time we attach ourselves to a device
  2114. // - constructor
  2115. // - internalSaveLayer
  2116. void setupDevice(SkBaseDevice*);
  2117. void init(sk_sp<SkBaseDevice>);
  2118. /**
  2119. * Gets the bounds of the top level layer in global canvas coordinates. We don't want this
  2120. * to be public because it exposes decisions about layer sizes that are internal to the canvas.
  2121. */
  2122. SkIRect getTopLayerBounds() const;
  2123. void internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src,
  2124. const SkRect& dst, const SkPaint* paint,
  2125. SrcRectConstraint);
  2126. void internalDrawPaint(const SkPaint& paint);
  2127. void internalSaveLayer(const SaveLayerRec&, SaveLayerStrategy);
  2128. void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*, SkImage* clipImage,
  2129. const SkMatrix& clipMatrix);
  2130. // shared by save() and saveLayer()
  2131. void internalSave();
  2132. void internalRestore();
  2133. /*
  2134. * Returns true if drawing the specified rect (or all if it is null) with the specified
  2135. * paint (or default if null) would overwrite the entire root device of the canvas
  2136. * (i.e. the canvas' surface if it had one).
  2137. */
  2138. bool wouldOverwriteEntireSurface(const SkRect*, const SkPaint*, ShaderOverrideOpacity) const;
  2139. /**
  2140. * Returns true if the paint's imagefilter can be invoked directly, without needed a layer.
  2141. */
  2142. bool canDrawBitmapAsSprite(SkScalar x, SkScalar y, int w, int h, const SkPaint&);
  2143. /**
  2144. * Returns true if the clip (for any active layer) contains antialiasing.
  2145. * If the clip is empty, this will return false.
  2146. */
  2147. bool androidFramework_isClipAA() const;
  2148. /**
  2149. * Keep track of the device clip bounds and if the matrix is scale-translate. This allows
  2150. * us to do a fast quick reject in the common case.
  2151. */
  2152. bool fIsScaleTranslate;
  2153. SkRect fDeviceClipBounds;
  2154. bool fAllowSoftClip;
  2155. bool fAllowSimplifyClip;
  2156. class AutoValidateClip {
  2157. public:
  2158. explicit AutoValidateClip(SkCanvas* canvas) : fCanvas(canvas) {
  2159. fCanvas->validateClip();
  2160. }
  2161. ~AutoValidateClip() { fCanvas->validateClip(); }
  2162. private:
  2163. const SkCanvas* fCanvas;
  2164. AutoValidateClip(AutoValidateClip&&) = delete;
  2165. AutoValidateClip(const AutoValidateClip&) = delete;
  2166. AutoValidateClip& operator=(AutoValidateClip&&) = delete;
  2167. AutoValidateClip& operator=(const AutoValidateClip&) = delete;
  2168. };
  2169. #ifdef SK_DEBUG
  2170. void validateClip() const;
  2171. #else
  2172. void validateClip() const {}
  2173. #endif
  2174. std::unique_ptr<SkGlyphRunBuilder> fScratchGlyphRunBuilder;
  2175. typedef SkRefCnt INHERITED;
  2176. };
  2177. /** \class SkAutoCanvasRestore
  2178. Stack helper class calls SkCanvas::restoreToCount when SkAutoCanvasRestore
  2179. goes out of scope. Use this to guarantee that the canvas is restored to a known
  2180. state.
  2181. */
  2182. class SkAutoCanvasRestore {
  2183. public:
  2184. /** Preserves SkCanvas::save() count. Optionally saves SkCanvas clip and SkCanvas matrix.
  2185. @param canvas SkCanvas to guard
  2186. @param doSave call SkCanvas::save()
  2187. @return utility to restore SkCanvas state on destructor
  2188. */
  2189. SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) : fCanvas(canvas), fSaveCount(0) {
  2190. if (fCanvas) {
  2191. fSaveCount = canvas->getSaveCount();
  2192. if (doSave) {
  2193. canvas->save();
  2194. }
  2195. }
  2196. }
  2197. /** Restores SkCanvas to saved state. Destructor is called when container goes out of
  2198. scope.
  2199. */
  2200. ~SkAutoCanvasRestore() {
  2201. if (fCanvas) {
  2202. fCanvas->restoreToCount(fSaveCount);
  2203. }
  2204. }
  2205. /** Restores SkCanvas to saved state immediately. Subsequent calls and
  2206. ~SkAutoCanvasRestore have no effect.
  2207. */
  2208. void restore() {
  2209. if (fCanvas) {
  2210. fCanvas->restoreToCount(fSaveCount);
  2211. fCanvas = nullptr;
  2212. }
  2213. }
  2214. private:
  2215. SkCanvas* fCanvas;
  2216. int fSaveCount;
  2217. SkAutoCanvasRestore(SkAutoCanvasRestore&&) = delete;
  2218. SkAutoCanvasRestore(const SkAutoCanvasRestore&) = delete;
  2219. SkAutoCanvasRestore& operator=(SkAutoCanvasRestore&&) = delete;
  2220. SkAutoCanvasRestore& operator=(const SkAutoCanvasRestore&) = delete;
  2221. };
  2222. #define SkAutoCanvasRestore(...) SK_REQUIRE_LOCAL_VAR(SkAutoCanvasRestore)
  2223. #endif