SkCanvas.h 131 KB

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